[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.61

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.61    ! markus     26: RCSID("$OpenBSD: auth2.c,v 1.60 2001/05/30 23:31:14 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"
1.61    ! markus     39: #include "channels.h"
1.1       markus     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.60      markus    246:        char *methods;
                    247:
1.28      markus    248:        if (!authctxt->valid && authenticated)
                    249:                fatal("INTERNAL ERROR: authenticated invalid user %s",
                    250:                    authctxt->user);
1.18      markus    251:
                    252:        /* Special handling for root */
1.41      markus    253:        if (authenticated && authctxt->pw->pw_uid == 0 &&
                    254:            !auth_root_allowed(method))
1.3       markus    255:                authenticated = 0;
                    256:
1.18      markus    257:        /* Log before sending the reply */
1.28      markus    258:        auth_log(authctxt, authenticated, method, " ssh2");
                    259:
1.60      markus    260:        if (authctxt->postponed)
                    261:                return;
                    262:
                    263:        /* XXX todo: check if multiple auth methods are needed */
                    264:        if (authenticated == 1) {
                    265:                /* turn off userauth */
                    266:                dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
                    267:                packet_start(SSH2_MSG_USERAUTH_SUCCESS);
                    268:                packet_send();
                    269:                packet_write_wait();
                    270:                /* now we can break out */
                    271:                authctxt->success = 1;
                    272:        } else {
                    273:                if (authctxt->failures++ > AUTH_FAIL_MAX)
                    274:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
                    275:                methods = authmethods_get();
                    276:                packet_start(SSH2_MSG_USERAUTH_FAILURE);
                    277:                packet_put_cstring(methods);
                    278:                packet_put_char(0);     /* XXX partial success, unused */
                    279:                packet_send();
                    280:                packet_write_wait();
                    281:                xfree(methods);
                    282:        }
1.18      markus    283: }
                    284:
1.25      markus    285: void
                    286: userauth_banner(void)
                    287: {
                    288:        struct stat st;
                    289:        char *banner = NULL;
                    290:        off_t len, n;
                    291:        int fd;
                    292:
                    293:        if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
                    294:                return;
1.50      markus    295:        if ((fd = open(options.banner, O_RDONLY)) < 0)
1.25      markus    296:                return;
                    297:        if (fstat(fd, &st) < 0)
                    298:                goto done;
                    299:        len = st.st_size;
                    300:        banner = xmalloc(len + 1);
                    301:        if ((n = read(fd, banner, len)) < 0)
                    302:                goto done;
                    303:        banner[n] = '\0';
                    304:        packet_start(SSH2_MSG_USERAUTH_BANNER);
                    305:        packet_put_cstring(banner);
                    306:        packet_put_cstring("");         /* language, unused */
                    307:        packet_send();
                    308:        debug("userauth_banner: sent");
                    309: done:
                    310:        if (banner)
                    311:                xfree(banner);
                    312:        close(fd);
                    313:        return;
1.1       markus    314: }
                    315:
                    316: int
1.18      markus    317: userauth_none(Authctxt *authctxt)
1.1       markus    318: {
1.18      markus    319:        /* disable method "none", only allowed one time */
                    320:        Authmethod *m = authmethod_lookup("none");
                    321:        if (m != NULL)
                    322:                m->enabled = NULL;
1.1       markus    323:        packet_done();
1.25      markus    324:        userauth_banner();
1.47      markus    325:        return authctxt->valid ? auth_password(authctxt, "") : 0;
1.1       markus    326: }
1.18      markus    327:
1.1       markus    328: int
1.18      markus    329: userauth_passwd(Authctxt *authctxt)
1.1       markus    330: {
                    331:        char *password;
                    332:        int authenticated = 0;
                    333:        int change;
1.23      markus    334:        u_int len;
1.1       markus    335:        change = packet_get_char();
                    336:        if (change)
                    337:                log("password change not supported");
                    338:        password = packet_get_string(&len);
                    339:        packet_done();
1.18      markus    340:        if (authctxt->valid &&
1.47      markus    341:            auth_password(authctxt, password) == 1)
1.1       markus    342:                authenticated = 1;
                    343:        memset(password, 0, len);
                    344:        xfree(password);
                    345:        return authenticated;
                    346: }
1.18      markus    347:
                    348: int
                    349: userauth_kbdint(Authctxt *authctxt)
                    350: {
                    351:        int authenticated = 0;
1.57      markus    352:        char *lang, *devs;
                    353:
1.18      markus    354:        lang = packet_get_string(NULL);
                    355:        devs = packet_get_string(NULL);
                    356:        packet_done();
                    357:
1.57      markus    358:        debug("keyboard-interactive devs %s", devs);
1.28      markus    359:
1.57      markus    360:        if (options.challenge_response_authentication)
1.34      markus    361:                authenticated = auth2_challenge(authctxt, devs);
1.28      markus    362:
1.57      markus    363:        xfree(devs);
1.18      markus    364:        xfree(lang);
                    365:        return authenticated;
                    366: }
                    367:
1.1       markus    368: int
1.18      markus    369: userauth_pubkey(Authctxt *authctxt)
1.1       markus    370: {
                    371:        Buffer b;
                    372:        Key *key;
                    373:        char *pkalg, *pkblob, *sig;
1.23      markus    374:        u_int alen, blen, slen;
1.21      markus    375:        int have_sig, pktype;
1.1       markus    376:        int authenticated = 0;
                    377:
1.18      markus    378:        if (!authctxt->valid) {
                    379:                debug2("userauth_pubkey: disabled because of invalid user");
1.8       markus    380:                return 0;
                    381:        }
1.1       markus    382:        have_sig = packet_get_char();
1.22      markus    383:        if (datafellows & SSH_BUG_PKAUTH) {
                    384:                debug2("userauth_pubkey: SSH_BUG_PKAUTH");
                    385:                /* no explicit pkalg given */
                    386:                pkblob = packet_get_string(&blen);
                    387:                buffer_init(&b);
                    388:                buffer_append(&b, pkblob, blen);
                    389:                /* so we have to extract the pkalg from the pkblob */
                    390:                pkalg = buffer_get_string(&b, &alen);
                    391:                buffer_free(&b);
                    392:        } else {
                    393:                pkalg = packet_get_string(&alen);
                    394:                pkblob = packet_get_string(&blen);
                    395:        }
1.21      markus    396:        pktype = key_type_from_name(pkalg);
                    397:        if (pktype == KEY_UNSPEC) {
1.22      markus    398:                /* this is perfectly legal */
                    399:                log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
1.1       markus    400:                xfree(pkalg);
1.22      markus    401:                xfree(pkblob);
1.1       markus    402:                return 0;
                    403:        }
1.21      markus    404:        key = key_from_blob(pkblob, blen);
1.2       markus    405:        if (key != NULL) {
                    406:                if (have_sig) {
                    407:                        sig = packet_get_string(&slen);
                    408:                        packet_done();
                    409:                        buffer_init(&b);
1.20      markus    410:                        if (datafellows & SSH_OLD_SESSIONID) {
                    411:                                buffer_append(&b, session_id2, session_id2_len);
                    412:                        } else {
1.11      markus    413:                                buffer_put_string(&b, session_id2, session_id2_len);
                    414:                        }
1.9       markus    415:                        /* reconstruct packet */
1.2       markus    416:                        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.18      markus    417:                        buffer_put_cstring(&b, authctxt->user);
1.9       markus    418:                        buffer_put_cstring(&b,
1.22      markus    419:                            datafellows & SSH_BUG_PKSERVICE ?
1.9       markus    420:                            "ssh-userauth" :
1.18      markus    421:                            authctxt->service);
1.22      markus    422:                        if (datafellows & SSH_BUG_PKAUTH) {
                    423:                                buffer_put_char(&b, have_sig);
                    424:                        } else {
                    425:                                buffer_put_cstring(&b, "publickey");
                    426:                                buffer_put_char(&b, have_sig);
1.56      markus    427:                                buffer_put_cstring(&b, pkalg);
1.22      markus    428:                        }
1.9       markus    429:                        buffer_put_string(&b, pkblob, blen);
1.21      markus    430: #ifdef DEBUG_PK
1.2       markus    431:                        buffer_dump(&b);
1.1       markus    432: #endif
1.2       markus    433:                        /* test for correct signature */
1.21      markus    434:                        if (user_key_allowed(authctxt->pw, key) &&
                    435:                            key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
1.2       markus    436:                                authenticated = 1;
                    437:                        buffer_clear(&b);
                    438:                        xfree(sig);
                    439:                } else {
1.18      markus    440:                        debug("test whether pkalg/pkblob are acceptable");
1.2       markus    441:                        packet_done();
1.18      markus    442:
1.2       markus    443:                        /* XXX fake reply and always send PK_OK ? */
1.6       markus    444:                        /*
                    445:                         * XXX this allows testing whether a user is allowed
                    446:                         * to login: if you happen to have a valid pubkey this
                    447:                         * message is sent. the message is NEVER sent at all
                    448:                         * if a user is not allowed to login. is this an
                    449:                         * issue? -markus
                    450:                         */
1.21      markus    451:                        if (user_key_allowed(authctxt->pw, key)) {
1.2       markus    452:                                packet_start(SSH2_MSG_USERAUTH_PK_OK);
                    453:                                packet_put_string(pkalg, alen);
                    454:                                packet_put_string(pkblob, blen);
                    455:                                packet_send();
                    456:                                packet_write_wait();
1.38      markus    457:                                authctxt->postponed = 1;
1.2       markus    458:                        }
1.1       markus    459:                }
1.17      markus    460:                if (authenticated != 1)
                    461:                        auth_clear_options();
1.2       markus    462:                key_free(key);
1.1       markus    463:        }
1.21      markus    464:        debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
1.1       markus    465:        xfree(pkalg);
                    466:        xfree(pkblob);
                    467:        return authenticated;
                    468: }
                    469:
1.52      markus    470: int
                    471: userauth_hostbased(Authctxt *authctxt)
                    472: {
                    473:        Buffer b;
                    474:        Key *key;
1.55      markus    475:        char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
1.52      markus    476:        u_int alen, blen, slen;
                    477:        int pktype;
                    478:        int authenticated = 0;
                    479:
                    480:        if (!authctxt->valid) {
                    481:                debug2("userauth_hostbased: disabled because of invalid user");
                    482:                return 0;
                    483:        }
                    484:        pkalg = packet_get_string(&alen);
                    485:        pkblob = packet_get_string(&blen);
                    486:        chost = packet_get_string(NULL);
                    487:        cuser = packet_get_string(NULL);
                    488:        sig = packet_get_string(&slen);
                    489:
                    490:        debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
                    491:            cuser, chost, pkalg, slen);
                    492: #ifdef DEBUG_PK
                    493:        debug("signature:");
                    494:        buffer_init(&b);
                    495:        buffer_append(&b, sig, slen);
                    496:        buffer_dump(&b);
                    497:        buffer_free(&b);
                    498: #endif
                    499:        pktype = key_type_from_name(pkalg);
                    500:        if (pktype == KEY_UNSPEC) {
                    501:                /* this is perfectly legal */
                    502:                log("userauth_hostbased: unsupported "
                    503:                    "public key algorithm: %s", pkalg);
                    504:                goto done;
                    505:        }
                    506:        key = key_from_blob(pkblob, blen);
                    507:        if (key == NULL) {
                    508:                debug("userauth_hostbased: cannot decode key: %s", pkalg);
                    509:                goto done;
                    510:        }
1.55      markus    511:        service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
                    512:            authctxt->service;
1.52      markus    513:        buffer_init(&b);
1.55      markus    514:        buffer_put_string(&b, session_id2, session_id2_len);
1.52      markus    515:        /* reconstruct packet */
                    516:        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
                    517:        buffer_put_cstring(&b, authctxt->user);
1.55      markus    518:        buffer_put_cstring(&b, service);
1.52      markus    519:        buffer_put_cstring(&b, "hostbased");
                    520:        buffer_put_string(&b, pkalg, alen);
                    521:        buffer_put_string(&b, pkblob, blen);
                    522:        buffer_put_cstring(&b, chost);
                    523:        buffer_put_cstring(&b, cuser);
                    524: #ifdef DEBUG_PK
                    525:        buffer_dump(&b);
                    526: #endif
                    527:        /* test for allowed key and correct signature */
                    528:        if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
                    529:            key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
                    530:                authenticated = 1;
                    531:
                    532:        buffer_clear(&b);
                    533:        key_free(key);
                    534:
                    535: done:
                    536:        debug2("userauth_hostbased: authenticated %d", authenticated);
                    537:        xfree(pkalg);
                    538:        xfree(pkblob);
                    539:        xfree(cuser);
                    540:        xfree(chost);
                    541:        xfree(sig);
                    542:        return authenticated;
                    543: }
                    544:
1.18      markus    545: /* get current user */
1.1       markus    546:
                    547: struct passwd*
                    548: auth_get_user(void)
                    549: {
1.18      markus    550:        return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
1.1       markus    551: }
                    552:
1.18      markus    553: #define        DELIM   ","
                    554:
                    555: char *
                    556: authmethods_get(void)
1.1       markus    557: {
1.18      markus    558:        Authmethod *method = NULL;
1.23      markus    559:        u_int size = 0;
1.18      markus    560:        char *list;
1.1       markus    561:
1.18      markus    562:        for (method = authmethods; method->name != NULL; method++) {
                    563:                if (strcmp(method->name, "none") == 0)
                    564:                        continue;
                    565:                if (method->enabled != NULL && *(method->enabled) != 0) {
                    566:                        if (size != 0)
                    567:                                size += strlen(DELIM);
                    568:                        size += strlen(method->name);
1.1       markus    569:                }
1.18      markus    570:        }
                    571:        size++;                 /* trailing '\0' */
                    572:        list = xmalloc(size);
                    573:        list[0] = '\0';
                    574:
                    575:        for (method = authmethods; method->name != NULL; method++) {
                    576:                if (strcmp(method->name, "none") == 0)
                    577:                        continue;
                    578:                if (method->enabled != NULL && *(method->enabled) != 0) {
                    579:                        if (list[0] != '\0')
                    580:                                strlcat(list, DELIM, size);
                    581:                        strlcat(list, method->name, size);
1.1       markus    582:                }
                    583:        }
1.18      markus    584:        return list;
                    585: }
                    586:
                    587: Authmethod *
                    588: authmethod_lookup(const char *name)
                    589: {
                    590:        Authmethod *method = NULL;
                    591:        if (name != NULL)
                    592:                for (method = authmethods; method->name != NULL; method++)
                    593:                        if (method->enabled != NULL &&
                    594:                            *(method->enabled) != 0 &&
                    595:                            strcmp(name, method->name) == 0)
                    596:                                return method;
                    597:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                    598:        return NULL;
1.1       markus    599: }
                    600:
                    601: /* return 1 if user allows given key */
                    602: int
1.21      markus    603: user_key_allowed(struct passwd *pw, Key *key)
1.1       markus    604: {
1.58      markus    605:        char line[8192], *file;
1.1       markus    606:        int found_key = 0;
                    607:        FILE *f;
1.23      markus    608:        u_long linenum = 0;
1.1       markus    609:        struct stat st;
                    610:        Key *found;
                    611:
1.18      markus    612:        if (pw == NULL)
                    613:                return 0;
                    614:
1.1       markus    615:        /* Temporarily use the user's uid. */
1.51      markus    616:        temporarily_use_uid(pw);
1.1       markus    617:
                    618:        /* The authorized keys. */
1.58      markus    619:        file = authorized_keys_file2(pw);
                    620:        debug("trying public key file %s", file);
1.1       markus    621:
                    622:        /* Fail quietly if file does not exist */
                    623:        if (stat(file, &st) < 0) {
                    624:                /* Restore the privileged uid. */
                    625:                restore_uid();
1.58      markus    626:                xfree(file);
1.1       markus    627:                return 0;
                    628:        }
                    629:        /* Open the file containing the authorized keys. */
                    630:        f = fopen(file, "r");
                    631:        if (!f) {
                    632:                /* Restore the privileged uid. */
                    633:                restore_uid();
1.58      markus    634:                xfree(file);
1.1       markus    635:                return 0;
                    636:        }
1.58      markus    637:        if (options.strict_modes &&
                    638:            secure_filename(f, file, pw->pw_uid, line, sizeof(line)) != 0) {
                    639:                xfree(file);
                    640:                fclose(f);
                    641:                log("Authentication refused: %s", line);
                    642:                restore_uid();
                    643:                return 0;
1.1       markus    644:        }
1.58      markus    645:
1.1       markus    646:        found_key = 0;
1.16      markus    647:        found = key_new(key->type);
1.1       markus    648:
                    649:        while (fgets(line, sizeof(line), f)) {
1.10      markus    650:                char *cp, *options = NULL;
1.1       markus    651:                linenum++;
                    652:                /* Skip leading whitespace, empty and comment lines. */
                    653:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    654:                        ;
                    655:                if (!*cp || *cp == '\n' || *cp == '#')
                    656:                        continue;
1.10      markus    657:
1.21      markus    658:                if (key_read(found, &cp) == -1) {
1.10      markus    659:                        /* no key?  check if there are options for this key */
                    660:                        int quoted = 0;
1.21      markus    661:                        debug2("user_key_allowed: check options: '%s'", cp);
1.10      markus    662:                        options = cp;
                    663:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    664:                                if (*cp == '\\' && cp[1] == '"')
                    665:                                        cp++;   /* Skip both */
                    666:                                else if (*cp == '"')
                    667:                                        quoted = !quoted;
                    668:                        }
                    669:                        /* Skip remaining whitespace. */
                    670:                        for (; *cp == ' ' || *cp == '\t'; cp++)
                    671:                                ;
1.21      markus    672:                        if (key_read(found, &cp) == -1) {
                    673:                                debug2("user_key_allowed: advance: '%s'", cp);
1.10      markus    674:                                /* still no key?  advance to next line*/
                    675:                                continue;
                    676:                        }
                    677:                }
                    678:                if (key_equal(found, key) &&
1.30      markus    679:                    auth_parse_options(pw, options, file, linenum) == 1) {
1.1       markus    680:                        found_key = 1;
                    681:                        debug("matching key found: file %s, line %ld",
                    682:                            file, linenum);
                    683:                        break;
                    684:                }
                    685:        }
                    686:        restore_uid();
                    687:        fclose(f);
1.58      markus    688:        xfree(file);
1.1       markus    689:        key_free(found);
1.46      markus    690:        if (!found_key)
                    691:                debug2("key not found");
1.1       markus    692:        return found_key;
1.52      markus    693: }
                    694:
                    695: /* return 1 if given hostkey is allowed */
                    696: int
1.54      markus    697: hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1.52      markus    698:     Key *key)
                    699: {
                    700:        Key *found;
                    701:        const char *resolvedname, *ipaddr, *lookup;
                    702:        struct stat st;
                    703:        char *user_hostfile;
1.53      markus    704:        int host_status, len;
1.52      markus    705:
                    706:        resolvedname = get_canonical_hostname(options.reverse_mapping_check);
                    707:        ipaddr = get_remote_ipaddr();
                    708:
1.53      markus    709:        debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
                    710:            chost, resolvedname, ipaddr);
1.52      markus    711:
                    712:        if (options.hostbased_uses_name_from_packet_only) {
                    713:                if (auth_rhosts2(pw, cuser, chost, chost) == 0)
                    714:                        return 0;
                    715:                lookup = chost;
                    716:        } else {
1.53      markus    717:                if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
                    718:                        debug2("stripping trailing dot from chost %s", chost);
                    719:                        chost[len - 1] = '\0';
                    720:                }
1.52      markus    721:                if (strcasecmp(resolvedname, chost) != 0)
                    722:                        log("userauth_hostbased mismatch: "
                    723:                            "client sends %s, but we resolve %s to %s",
                    724:                            chost, ipaddr, resolvedname);
                    725:                if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
                    726:                        return 0;
                    727:                lookup = resolvedname;
                    728:        }
                    729:        debug2("userauth_hostbased: access allowed by auth_rhosts2");
                    730:
                    731:        /* XXX this is copied from auth-rh-rsa.c and should be shared */
                    732:        found = key_new(key->type);
                    733:        host_status = check_host_in_hostfile(_PATH_SSH_SYSTEM_HOSTFILE2, lookup,
                    734:            key, found, NULL);
                    735:
                    736:        if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
                    737:                user_hostfile = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE2,
                    738:                    pw->pw_uid);
                    739:                if (options.strict_modes &&
                    740:                    (stat(user_hostfile, &st) == 0) &&
                    741:                    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    742:                     (st.st_mode & 022) != 0)) {
                    743:                        log("Hostbased authentication refused for %.100s: "
                    744:                            "bad owner or modes for %.200s",
                    745:                            pw->pw_name, user_hostfile);
                    746:                } else {
                    747:                        temporarily_use_uid(pw);
                    748:                        host_status = check_host_in_hostfile(user_hostfile,
                    749:                            lookup, key, found, NULL);
                    750:                        restore_uid();
                    751:                }
                    752:                xfree(user_hostfile);
                    753:        }
                    754:        key_free(found);
                    755:
                    756:        debug2("userauth_hostbased: key %s for %s", host_status == HOST_OK ?
                    757:            "ok" : "not found", lookup);
                    758:        return (host_status == HOST_OK);
1.1       markus    759: }