[BACK]Return to auth2.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/auth2.c, Revision 1.58

1.1       markus      1: /*
                      2:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
1.14      deraadt    24:
1.1       markus     25: #include "includes.h"
1.58    ! markus     26: RCSID("$OpenBSD: auth2.c,v 1.57 2001/05/18 14:13:28 markus Exp $");
1.1       markus     27:
                     28: #include <openssl/evp.h>
                     29:
1.32      markus     30: #include "ssh2.h"
1.1       markus     31: #include "xmalloc.h"
                     32: #include "rsa.h"
1.45      djm        33: #include "sshpty.h"
1.1       markus     34: #include "packet.h"
                     35: #include "buffer.h"
1.32      markus     36: #include "log.h"
1.1       markus     37: #include "servconf.h"
                     38: #include "compat.h"
                     39: #include "channels.h"
                     40: #include "bufaux.h"
                     41: #include "auth.h"
                     42: #include "session.h"
                     43: #include "dispatch.h"
                     44: #include "key.h"
1.32      markus     45: #include "cipher.h"
                     46: #include "kex.h"
1.29      markus     47: #include "pathnames.h"
1.1       markus     48: #include "uidswap.h"
1.10      markus     49: #include "auth-options.h"
1.43      markus     50: #include "misc.h"
1.52      markus     51: #include "hostfile.h"
                     52: #include "canohost.h"
                     53: #include "tildexpand.h"
1.57      markus     54: #include "match.h"
1.1       markus     55:
                     56: /* import */
                     57: extern ServerOptions options;
1.23      markus     58: extern u_char *session_id2;
1.1       markus     59: extern int session_id2_len;
                     60:
1.18      markus     61: static Authctxt        *x_authctxt = NULL;
                     62: static int one = 1;
                     63:
                     64: typedef struct Authmethod Authmethod;
                     65: struct Authmethod {
                     66:        char    *name;
                     67:        int     (*userauth)(Authctxt *authctxt);
                     68:        int     *enabled;
                     69: };
                     70:
1.1       markus     71: /* protocol */
                     72:
1.15      markus     73: void   input_service_request(int type, int plen, void *ctxt);
                     74: void   input_userauth_request(int type, int plen, void *ctxt);
                     75: void   protocol_error(int type, int plen, void *ctxt);
1.1       markus     76:
                     77: /* helper */
1.18      markus     78: Authmethod     *authmethod_lookup(const char *name);
1.52      markus     79: char   *authmethods_get(void);
1.21      markus     80: int    user_key_allowed(struct passwd *pw, Key *key);
1.52      markus     81: int
1.54      markus     82: hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1.52      markus     83:     Key *key);
1.1       markus     84:
1.18      markus     85: /* auth */
1.25      markus     86: void   userauth_banner(void);
1.49      markus     87: void   userauth_reply(Authctxt *authctxt, int authenticated);
1.18      markus     88: int    userauth_none(Authctxt *authctxt);
                     89: int    userauth_passwd(Authctxt *authctxt);
                     90: int    userauth_pubkey(Authctxt *authctxt);
1.52      markus     91: int    userauth_hostbased(Authctxt *authctxt);
1.18      markus     92: int    userauth_kbdint(Authctxt *authctxt);
                     93:
                     94: Authmethod authmethods[] = {
                     95:        {"none",
                     96:                userauth_none,
                     97:                &one},
                     98:        {"publickey",
                     99:                userauth_pubkey,
1.21      markus    100:                &options.pubkey_authentication},
1.40      markus    101:        {"password",
                    102:                userauth_passwd,
                    103:                &options.password_authentication},
1.18      markus    104:        {"keyboard-interactive",
                    105:                userauth_kbdint,
                    106:                &options.kbd_interactive_authentication},
1.52      markus    107:        {"hostbased",
                    108:                userauth_hostbased,
                    109:                &options.hostbased_authentication},
1.18      markus    110:        {NULL, NULL, NULL}
1.1       markus    111: };
                    112:
                    113: /*
1.18      markus    114:  * loop until authctxt->success == TRUE
1.1       markus    115:  */
                    116:
                    117: void
                    118: do_authentication2()
                    119: {
1.28      markus    120:        Authctxt *authctxt = authctxt_new();
                    121:
1.18      markus    122:        x_authctxt = authctxt;          /*XXX*/
                    123:
1.34      markus    124:        /* challenge-reponse is implemented via keyboard interactive */
1.57      markus    125:        if (options.challenge_response_authentication)
1.34      markus    126:                options.kbd_interactive_authentication = 1;
                    127:
1.1       markus    128:        dispatch_init(&protocol_error);
                    129:        dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18      markus    130:        dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.48      markus    131:        do_authenticated(authctxt);
1.1       markus    132: }
                    133:
                    134: void
1.15      markus    135: protocol_error(int type, int plen, void *ctxt)
1.1       markus    136: {
                    137:        log("auth: protocol error: type %d plen %d", type, plen);
                    138:        packet_start(SSH2_MSG_UNIMPLEMENTED);
                    139:        packet_put_int(0);
                    140:        packet_send();
                    141:        packet_write_wait();
                    142: }
                    143:
                    144: void
1.15      markus    145: input_service_request(int type, int plen, void *ctxt)
1.1       markus    146: {
1.18      markus    147:        Authctxt *authctxt = ctxt;
1.23      markus    148:        u_int len;
1.1       markus    149:        int accept = 0;
                    150:        char *service = packet_get_string(&len);
                    151:        packet_done();
                    152:
1.18      markus    153:        if (authctxt == NULL)
                    154:                fatal("input_service_request: no authctxt");
                    155:
1.1       markus    156:        if (strcmp(service, "ssh-userauth") == 0) {
1.18      markus    157:                if (!authctxt->success) {
1.1       markus    158:                        accept = 1;
                    159:                        /* now we can handle user-auth requests */
                    160:                        dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
                    161:                }
                    162:        }
                    163:        /* XXX all other service requests are denied */
                    164:
                    165:        if (accept) {
                    166:                packet_start(SSH2_MSG_SERVICE_ACCEPT);
                    167:                packet_put_cstring(service);
                    168:                packet_send();
                    169:                packet_write_wait();
                    170:        } else {
                    171:                debug("bad service request %s", service);
                    172:                packet_disconnect("bad service request %s", service);
                    173:        }
                    174:        xfree(service);
                    175: }
                    176:
                    177: void
1.15      markus    178: input_userauth_request(int type, int plen, void *ctxt)
1.1       markus    179: {
1.18      markus    180:        Authctxt *authctxt = ctxt;
                    181:        Authmethod *m = NULL;
1.28      markus    182:        char *user, *service, *method, *style = NULL;
1.1       markus    183:        int authenticated = 0;
                    184:
1.18      markus    185:        if (authctxt == NULL)
                    186:                fatal("input_userauth_request: no authctxt");
1.1       markus    187:
1.18      markus    188:        user = packet_get_string(NULL);
                    189:        service = packet_get_string(NULL);
                    190:        method = packet_get_string(NULL);
1.1       markus    191:        debug("userauth-request for user %s service %s method %s", user, service, method);
1.24      markus    192:        debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1       markus    193:
1.28      markus    194:        if ((style = strchr(user, ':')) != NULL)
                    195:                *style++ = 0;
                    196:
1.36      stevesk   197:        if (authctxt->attempt++ == 0) {
1.18      markus    198:                /* setup auth context */
                    199:                struct passwd *pw = NULL;
                    200:                pw = getpwnam(user);
                    201:                if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
                    202:                        authctxt->pw = pwcopy(pw);
                    203:                        authctxt->valid = 1;
                    204:                        debug2("input_userauth_request: setting up authctxt for %s", user);
                    205:                } else {
                    206:                        log("input_userauth_request: illegal user %s", user);
                    207:                }
1.42      markus    208:                setproctitle("%s", pw ? user : "unknown");
1.18      markus    209:                authctxt->user = xstrdup(user);
                    210:                authctxt->service = xstrdup(service);
1.28      markus    211:                authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
1.18      markus    212:        } else if (authctxt->valid) {
                    213:                if (strcmp(user, authctxt->user) != 0 ||
                    214:                    strcmp(service, authctxt->service) != 0) {
1.52      markus    215:                        log("input_userauth_request: mismatch: (%s,%s)!=(%s,%s)",
1.18      markus    216:                            user, service, authctxt->user, authctxt->service);
                    217:                        authctxt->valid = 0;
1.1       markus    218:                }
                    219:        }
1.28      markus    220:        /* reset state */
                    221:        dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
                    222:        authctxt->postponed = 0;
1.47      markus    223: #ifdef BSD_AUTH
                    224:        if (authctxt->as) {
                    225:                auth_close(authctxt->as);
                    226:                authctxt->as = NULL;
                    227:        }
                    228: #endif
1.18      markus    229:
1.28      markus    230:        /* try to authenticate user */
1.18      markus    231:        m = authmethod_lookup(method);
                    232:        if (m != NULL) {
                    233:                debug2("input_userauth_request: try method %s", method);
                    234:                authenticated = m->userauth(authctxt);
                    235:        }
1.49      markus    236:        userauth_finish(authctxt, authenticated, method);
                    237:
                    238:        xfree(service);
                    239:        xfree(user);
                    240:        xfree(method);
                    241: }
                    242:
                    243: void
                    244: userauth_finish(Authctxt *authctxt, int authenticated, char *method)
                    245: {
1.28      markus    246:        if (!authctxt->valid && authenticated)
                    247:                fatal("INTERNAL ERROR: authenticated invalid user %s",
                    248:                    authctxt->user);
1.18      markus    249:
                    250:        /* Special handling for root */
1.41      markus    251:        if (authenticated && authctxt->pw->pw_uid == 0 &&
                    252:            !auth_root_allowed(method))
1.3       markus    253:                authenticated = 0;
                    254:
1.18      markus    255:        /* Log before sending the reply */
1.28      markus    256:        auth_log(authctxt, authenticated, method, " ssh2");
                    257:
                    258:        if (!authctxt->postponed)
                    259:                userauth_reply(authctxt, authenticated);
1.18      markus    260: }
                    261:
1.25      markus    262: void
                    263: userauth_banner(void)
                    264: {
                    265:        struct stat st;
                    266:        char *banner = NULL;
                    267:        off_t len, n;
                    268:        int fd;
                    269:
                    270:        if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
                    271:                return;
1.50      markus    272:        if ((fd = open(options.banner, O_RDONLY)) < 0)
1.25      markus    273:                return;
                    274:        if (fstat(fd, &st) < 0)
                    275:                goto done;
                    276:        len = st.st_size;
                    277:        banner = xmalloc(len + 1);
                    278:        if ((n = read(fd, banner, len)) < 0)
                    279:                goto done;
                    280:        banner[n] = '\0';
                    281:        packet_start(SSH2_MSG_USERAUTH_BANNER);
                    282:        packet_put_cstring(banner);
                    283:        packet_put_cstring("");         /* language, unused */
                    284:        packet_send();
                    285:        debug("userauth_banner: sent");
                    286: done:
                    287:        if (banner)
                    288:                xfree(banner);
                    289:        close(fd);
                    290:        return;
                    291: }
1.18      markus    292:
1.36      stevesk   293: void
1.18      markus    294: userauth_reply(Authctxt *authctxt, int authenticated)
                    295: {
1.24      markus    296:        char *methods;
1.28      markus    297:
1.3       markus    298:        /* XXX todo: check if multiple auth methods are needed */
1.39      markus    299:        if (authenticated == 1) {
1.1       markus    300:                /* turn off userauth */
                    301:                dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
                    302:                packet_start(SSH2_MSG_USERAUTH_SUCCESS);
                    303:                packet_send();
                    304:                packet_write_wait();
                    305:                /* now we can break out */
1.18      markus    306:                authctxt->success = 1;
1.28      markus    307:        } else {
                    308:                if (authctxt->failures++ > AUTH_FAIL_MAX)
1.36      stevesk   309:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.24      markus    310:                methods = authmethods_get();
1.1       markus    311:                packet_start(SSH2_MSG_USERAUTH_FAILURE);
1.18      markus    312:                packet_put_cstring(methods);
                    313:                packet_put_char(0);     /* XXX partial success, unused */
1.1       markus    314:                packet_send();
                    315:                packet_write_wait();
1.18      markus    316:                xfree(methods);
1.1       markus    317:        }
                    318: }
                    319:
                    320: int
1.18      markus    321: userauth_none(Authctxt *authctxt)
1.1       markus    322: {
1.18      markus    323:        /* disable method "none", only allowed one time */
                    324:        Authmethod *m = authmethod_lookup("none");
                    325:        if (m != NULL)
                    326:                m->enabled = NULL;
1.1       markus    327:        packet_done();
1.25      markus    328:        userauth_banner();
1.47      markus    329:        return authctxt->valid ? auth_password(authctxt, "") : 0;
1.1       markus    330: }
1.18      markus    331:
1.1       markus    332: int
1.18      markus    333: userauth_passwd(Authctxt *authctxt)
1.1       markus    334: {
                    335:        char *password;
                    336:        int authenticated = 0;
                    337:        int change;
1.23      markus    338:        u_int len;
1.1       markus    339:        change = packet_get_char();
                    340:        if (change)
                    341:                log("password change not supported");
                    342:        password = packet_get_string(&len);
                    343:        packet_done();
1.18      markus    344:        if (authctxt->valid &&
1.47      markus    345:            auth_password(authctxt, password) == 1)
1.1       markus    346:                authenticated = 1;
                    347:        memset(password, 0, len);
                    348:        xfree(password);
                    349:        return authenticated;
                    350: }
1.18      markus    351:
                    352: int
                    353: userauth_kbdint(Authctxt *authctxt)
                    354: {
                    355:        int authenticated = 0;
1.57      markus    356:        char *lang, *devs;
                    357:
1.18      markus    358:        lang = packet_get_string(NULL);
                    359:        devs = packet_get_string(NULL);
                    360:        packet_done();
                    361:
1.57      markus    362:        debug("keyboard-interactive devs %s", devs);
1.28      markus    363:
1.57      markus    364:        if (options.challenge_response_authentication)
1.34      markus    365:                authenticated = auth2_challenge(authctxt, devs);
1.28      markus    366:
1.57      markus    367:        xfree(devs);
1.18      markus    368:        xfree(lang);
                    369:        return authenticated;
                    370: }
                    371:
1.1       markus    372: int
1.18      markus    373: userauth_pubkey(Authctxt *authctxt)
1.1       markus    374: {
                    375:        Buffer b;
                    376:        Key *key;
                    377:        char *pkalg, *pkblob, *sig;
1.23      markus    378:        u_int alen, blen, slen;
1.21      markus    379:        int have_sig, pktype;
1.1       markus    380:        int authenticated = 0;
                    381:
1.18      markus    382:        if (!authctxt->valid) {
                    383:                debug2("userauth_pubkey: disabled because of invalid user");
1.8       markus    384:                return 0;
                    385:        }
1.1       markus    386:        have_sig = packet_get_char();
1.22      markus    387:        if (datafellows & SSH_BUG_PKAUTH) {
                    388:                debug2("userauth_pubkey: SSH_BUG_PKAUTH");
                    389:                /* no explicit pkalg given */
                    390:                pkblob = packet_get_string(&blen);
                    391:                buffer_init(&b);
                    392:                buffer_append(&b, pkblob, blen);
                    393:                /* so we have to extract the pkalg from the pkblob */
                    394:                pkalg = buffer_get_string(&b, &alen);
                    395:                buffer_free(&b);
                    396:        } else {
                    397:                pkalg = packet_get_string(&alen);
                    398:                pkblob = packet_get_string(&blen);
                    399:        }
1.21      markus    400:        pktype = key_type_from_name(pkalg);
                    401:        if (pktype == KEY_UNSPEC) {
1.22      markus    402:                /* this is perfectly legal */
                    403:                log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
1.1       markus    404:                xfree(pkalg);
1.22      markus    405:                xfree(pkblob);
1.1       markus    406:                return 0;
                    407:        }
1.21      markus    408:        key = key_from_blob(pkblob, blen);
1.2       markus    409:        if (key != NULL) {
                    410:                if (have_sig) {
                    411:                        sig = packet_get_string(&slen);
                    412:                        packet_done();
                    413:                        buffer_init(&b);
1.20      markus    414:                        if (datafellows & SSH_OLD_SESSIONID) {
                    415:                                buffer_append(&b, session_id2, session_id2_len);
                    416:                        } else {
1.11      markus    417:                                buffer_put_string(&b, session_id2, session_id2_len);
                    418:                        }
1.9       markus    419:                        /* reconstruct packet */
1.2       markus    420:                        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.18      markus    421:                        buffer_put_cstring(&b, authctxt->user);
1.9       markus    422:                        buffer_put_cstring(&b,
1.22      markus    423:                            datafellows & SSH_BUG_PKSERVICE ?
1.9       markus    424:                            "ssh-userauth" :
1.18      markus    425:                            authctxt->service);
1.22      markus    426:                        if (datafellows & SSH_BUG_PKAUTH) {
                    427:                                buffer_put_char(&b, have_sig);
                    428:                        } else {
                    429:                                buffer_put_cstring(&b, "publickey");
                    430:                                buffer_put_char(&b, have_sig);
1.56      markus    431:                                buffer_put_cstring(&b, pkalg);
1.22      markus    432:                        }
1.9       markus    433:                        buffer_put_string(&b, pkblob, blen);
1.21      markus    434: #ifdef DEBUG_PK
1.2       markus    435:                        buffer_dump(&b);
1.1       markus    436: #endif
1.2       markus    437:                        /* test for correct signature */
1.21      markus    438:                        if (user_key_allowed(authctxt->pw, key) &&
                    439:                            key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
1.2       markus    440:                                authenticated = 1;
                    441:                        buffer_clear(&b);
                    442:                        xfree(sig);
                    443:                } else {
1.18      markus    444:                        debug("test whether pkalg/pkblob are acceptable");
1.2       markus    445:                        packet_done();
1.18      markus    446:
1.2       markus    447:                        /* XXX fake reply and always send PK_OK ? */
1.6       markus    448:                        /*
                    449:                         * XXX this allows testing whether a user is allowed
                    450:                         * to login: if you happen to have a valid pubkey this
                    451:                         * message is sent. the message is NEVER sent at all
                    452:                         * if a user is not allowed to login. is this an
                    453:                         * issue? -markus
                    454:                         */
1.21      markus    455:                        if (user_key_allowed(authctxt->pw, key)) {
1.2       markus    456:                                packet_start(SSH2_MSG_USERAUTH_PK_OK);
                    457:                                packet_put_string(pkalg, alen);
                    458:                                packet_put_string(pkblob, blen);
                    459:                                packet_send();
                    460:                                packet_write_wait();
1.38      markus    461:                                authctxt->postponed = 1;
1.2       markus    462:                        }
1.1       markus    463:                }
1.17      markus    464:                if (authenticated != 1)
                    465:                        auth_clear_options();
1.2       markus    466:                key_free(key);
1.1       markus    467:        }
1.21      markus    468:        debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
1.1       markus    469:        xfree(pkalg);
                    470:        xfree(pkblob);
                    471:        return authenticated;
                    472: }
                    473:
1.52      markus    474: int
                    475: userauth_hostbased(Authctxt *authctxt)
                    476: {
                    477:        Buffer b;
                    478:        Key *key;
1.55      markus    479:        char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
1.52      markus    480:        u_int alen, blen, slen;
                    481:        int pktype;
                    482:        int authenticated = 0;
                    483:
                    484:        if (!authctxt->valid) {
                    485:                debug2("userauth_hostbased: disabled because of invalid user");
                    486:                return 0;
                    487:        }
                    488:        pkalg = packet_get_string(&alen);
                    489:        pkblob = packet_get_string(&blen);
                    490:        chost = packet_get_string(NULL);
                    491:        cuser = packet_get_string(NULL);
                    492:        sig = packet_get_string(&slen);
                    493:
                    494:        debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
                    495:            cuser, chost, pkalg, slen);
                    496: #ifdef DEBUG_PK
                    497:        debug("signature:");
                    498:        buffer_init(&b);
                    499:        buffer_append(&b, sig, slen);
                    500:        buffer_dump(&b);
                    501:        buffer_free(&b);
                    502: #endif
                    503:        pktype = key_type_from_name(pkalg);
                    504:        if (pktype == KEY_UNSPEC) {
                    505:                /* this is perfectly legal */
                    506:                log("userauth_hostbased: unsupported "
                    507:                    "public key algorithm: %s", pkalg);
                    508:                goto done;
                    509:        }
                    510:        key = key_from_blob(pkblob, blen);
                    511:        if (key == NULL) {
                    512:                debug("userauth_hostbased: cannot decode key: %s", pkalg);
                    513:                goto done;
                    514:        }
1.55      markus    515:        service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
                    516:            authctxt->service;
1.52      markus    517:        buffer_init(&b);
1.55      markus    518:        buffer_put_string(&b, session_id2, session_id2_len);
1.52      markus    519:        /* reconstruct packet */
                    520:        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
                    521:        buffer_put_cstring(&b, authctxt->user);
1.55      markus    522:        buffer_put_cstring(&b, service);
1.52      markus    523:        buffer_put_cstring(&b, "hostbased");
                    524:        buffer_put_string(&b, pkalg, alen);
                    525:        buffer_put_string(&b, pkblob, blen);
                    526:        buffer_put_cstring(&b, chost);
                    527:        buffer_put_cstring(&b, cuser);
                    528: #ifdef DEBUG_PK
                    529:        buffer_dump(&b);
                    530: #endif
                    531:        /* test for allowed key and correct signature */
                    532:        if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
                    533:            key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
                    534:                authenticated = 1;
                    535:
                    536:        buffer_clear(&b);
                    537:        key_free(key);
                    538:
                    539: done:
                    540:        debug2("userauth_hostbased: authenticated %d", authenticated);
                    541:        xfree(pkalg);
                    542:        xfree(pkblob);
                    543:        xfree(cuser);
                    544:        xfree(chost);
                    545:        xfree(sig);
                    546:        return authenticated;
                    547: }
                    548:
1.18      markus    549: /* get current user */
1.1       markus    550:
                    551: struct passwd*
                    552: auth_get_user(void)
                    553: {
1.18      markus    554:        return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
1.1       markus    555: }
                    556:
1.18      markus    557: #define        DELIM   ","
                    558:
                    559: char *
                    560: authmethods_get(void)
1.1       markus    561: {
1.18      markus    562:        Authmethod *method = NULL;
1.23      markus    563:        u_int size = 0;
1.18      markus    564:        char *list;
1.1       markus    565:
1.18      markus    566:        for (method = authmethods; method->name != NULL; method++) {
                    567:                if (strcmp(method->name, "none") == 0)
                    568:                        continue;
                    569:                if (method->enabled != NULL && *(method->enabled) != 0) {
                    570:                        if (size != 0)
                    571:                                size += strlen(DELIM);
                    572:                        size += strlen(method->name);
1.1       markus    573:                }
1.18      markus    574:        }
                    575:        size++;                 /* trailing '\0' */
                    576:        list = xmalloc(size);
                    577:        list[0] = '\0';
                    578:
                    579:        for (method = authmethods; method->name != NULL; method++) {
                    580:                if (strcmp(method->name, "none") == 0)
                    581:                        continue;
                    582:                if (method->enabled != NULL && *(method->enabled) != 0) {
                    583:                        if (list[0] != '\0')
                    584:                                strlcat(list, DELIM, size);
                    585:                        strlcat(list, method->name, size);
1.1       markus    586:                }
                    587:        }
1.18      markus    588:        return list;
                    589: }
                    590:
                    591: Authmethod *
                    592: authmethod_lookup(const char *name)
                    593: {
                    594:        Authmethod *method = NULL;
                    595:        if (name != NULL)
                    596:                for (method = authmethods; method->name != NULL; method++)
                    597:                        if (method->enabled != NULL &&
                    598:                            *(method->enabled) != 0 &&
                    599:                            strcmp(name, method->name) == 0)
                    600:                                return method;
                    601:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                    602:        return NULL;
1.1       markus    603: }
                    604:
                    605: /* return 1 if user allows given key */
                    606: int
1.21      markus    607: user_key_allowed(struct passwd *pw, Key *key)
1.1       markus    608: {
1.58    ! markus    609:        char line[8192], *file;
1.1       markus    610:        int found_key = 0;
                    611:        FILE *f;
1.23      markus    612:        u_long linenum = 0;
1.1       markus    613:        struct stat st;
                    614:        Key *found;
                    615:
1.18      markus    616:        if (pw == NULL)
                    617:                return 0;
                    618:
1.1       markus    619:        /* Temporarily use the user's uid. */
1.51      markus    620:        temporarily_use_uid(pw);
1.1       markus    621:
                    622:        /* The authorized keys. */
1.58    ! markus    623:        file = authorized_keys_file2(pw);
        !           624:        debug("trying public key file %s", file);
1.1       markus    625:
                    626:        /* Fail quietly if file does not exist */
                    627:        if (stat(file, &st) < 0) {
                    628:                /* Restore the privileged uid. */
                    629:                restore_uid();
1.58    ! markus    630:                xfree(file);
1.1       markus    631:                return 0;
                    632:        }
                    633:        /* Open the file containing the authorized keys. */
                    634:        f = fopen(file, "r");
                    635:        if (!f) {
                    636:                /* Restore the privileged uid. */
                    637:                restore_uid();
1.58    ! markus    638:                xfree(file);
1.1       markus    639:                return 0;
                    640:        }
1.58    ! markus    641:        if (options.strict_modes &&
        !           642:            secure_filename(f, file, pw->pw_uid, line, sizeof(line)) != 0) {
        !           643:                xfree(file);
        !           644:                fclose(f);
        !           645:                log("Authentication refused: %s", line);
        !           646:                restore_uid();
        !           647:                return 0;
1.1       markus    648:        }
1.58    ! markus    649:
1.1       markus    650:        found_key = 0;
1.16      markus    651:        found = key_new(key->type);
1.1       markus    652:
                    653:        while (fgets(line, sizeof(line), f)) {
1.10      markus    654:                char *cp, *options = NULL;
1.1       markus    655:                linenum++;
                    656:                /* Skip leading whitespace, empty and comment lines. */
                    657:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    658:                        ;
                    659:                if (!*cp || *cp == '\n' || *cp == '#')
                    660:                        continue;
1.10      markus    661:
1.21      markus    662:                if (key_read(found, &cp) == -1) {
1.10      markus    663:                        /* no key?  check if there are options for this key */
                    664:                        int quoted = 0;
1.21      markus    665:                        debug2("user_key_allowed: check options: '%s'", cp);
1.10      markus    666:                        options = cp;
                    667:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    668:                                if (*cp == '\\' && cp[1] == '"')
                    669:                                        cp++;   /* Skip both */
                    670:                                else if (*cp == '"')
                    671:                                        quoted = !quoted;
                    672:                        }
                    673:                        /* Skip remaining whitespace. */
                    674:                        for (; *cp == ' ' || *cp == '\t'; cp++)
                    675:                                ;
1.21      markus    676:                        if (key_read(found, &cp) == -1) {
                    677:                                debug2("user_key_allowed: advance: '%s'", cp);
1.10      markus    678:                                /* still no key?  advance to next line*/
                    679:                                continue;
                    680:                        }
                    681:                }
                    682:                if (key_equal(found, key) &&
1.30      markus    683:                    auth_parse_options(pw, options, file, linenum) == 1) {
1.1       markus    684:                        found_key = 1;
                    685:                        debug("matching key found: file %s, line %ld",
                    686:                            file, linenum);
                    687:                        break;
                    688:                }
                    689:        }
                    690:        restore_uid();
                    691:        fclose(f);
1.58    ! markus    692:        xfree(file);
1.1       markus    693:        key_free(found);
1.46      markus    694:        if (!found_key)
                    695:                debug2("key not found");
1.1       markus    696:        return found_key;
1.52      markus    697: }
                    698:
                    699: /* return 1 if given hostkey is allowed */
                    700: int
1.54      markus    701: hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1.52      markus    702:     Key *key)
                    703: {
                    704:        Key *found;
                    705:        const char *resolvedname, *ipaddr, *lookup;
                    706:        struct stat st;
                    707:        char *user_hostfile;
1.53      markus    708:        int host_status, len;
1.52      markus    709:
                    710:        resolvedname = get_canonical_hostname(options.reverse_mapping_check);
                    711:        ipaddr = get_remote_ipaddr();
                    712:
1.53      markus    713:        debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
                    714:            chost, resolvedname, ipaddr);
1.52      markus    715:
                    716:        if (options.hostbased_uses_name_from_packet_only) {
                    717:                if (auth_rhosts2(pw, cuser, chost, chost) == 0)
                    718:                        return 0;
                    719:                lookup = chost;
                    720:        } else {
1.53      markus    721:                if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
                    722:                        debug2("stripping trailing dot from chost %s", chost);
                    723:                        chost[len - 1] = '\0';
                    724:                }
1.52      markus    725:                if (strcasecmp(resolvedname, chost) != 0)
                    726:                        log("userauth_hostbased mismatch: "
                    727:                            "client sends %s, but we resolve %s to %s",
                    728:                            chost, ipaddr, resolvedname);
                    729:                if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
                    730:                        return 0;
                    731:                lookup = resolvedname;
                    732:        }
                    733:        debug2("userauth_hostbased: access allowed by auth_rhosts2");
                    734:
                    735:        /* XXX this is copied from auth-rh-rsa.c and should be shared */
                    736:        found = key_new(key->type);
                    737:        host_status = check_host_in_hostfile(_PATH_SSH_SYSTEM_HOSTFILE2, lookup,
                    738:            key, found, NULL);
                    739:
                    740:        if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
                    741:                user_hostfile = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE2,
                    742:                    pw->pw_uid);
                    743:                if (options.strict_modes &&
                    744:                    (stat(user_hostfile, &st) == 0) &&
                    745:                    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    746:                     (st.st_mode & 022) != 0)) {
                    747:                        log("Hostbased authentication refused for %.100s: "
                    748:                            "bad owner or modes for %.200s",
                    749:                            pw->pw_name, user_hostfile);
                    750:                } else {
                    751:                        temporarily_use_uid(pw);
                    752:                        host_status = check_host_in_hostfile(user_hostfile,
                    753:                            lookup, key, found, NULL);
                    754:                        restore_uid();
                    755:                }
                    756:                xfree(user_hostfile);
                    757:        }
                    758:        key_free(found);
                    759:
                    760:        debug2("userauth_hostbased: key %s for %s", host_status == HOST_OK ?
                    761:            "ok" : "not found", lookup);
                    762:        return (host_status == HOST_OK);
1.1       markus    763: }