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

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.20.2.8! brad       26: RCSID("$OpenBSD: auth2.c,v 1.85 2002/02/24 19:14:59 markus Exp $");
1.1       markus     27:
                     28: #include <openssl/evp.h>
                     29:
1.20.2.1  jason      30: #include "ssh2.h"
1.1       markus     31: #include "xmalloc.h"
                     32: #include "rsa.h"
1.20.2.3  jason      33: #include "sshpty.h"
1.1       markus     34: #include "packet.h"
                     35: #include "buffer.h"
1.20.2.1  jason      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.20.2.1  jason      45: #include "cipher.h"
1.1       markus     46: #include "kex.h"
1.20.2.1  jason      47: #include "pathnames.h"
1.1       markus     48: #include "uidswap.h"
1.10      markus     49: #include "auth-options.h"
1.20.2.3  jason      50: #include "misc.h"
1.20.2.4  jason      51: #include "hostfile.h"
                     52: #include "canohost.h"
1.20.2.5  miod       53: #include "match.h"
1.1       markus     54:
                     55: /* import */
                     56: extern ServerOptions options;
1.20.2.1  jason      57: extern u_char *session_id2;
1.1       markus     58: extern int session_id2_len;
                     59:
1.18      markus     60: static Authctxt        *x_authctxt = NULL;
                     61: static int one = 1;
                     62:
                     63: typedef struct Authmethod Authmethod;
                     64: struct Authmethod {
                     65:        char    *name;
                     66:        int     (*userauth)(Authctxt *authctxt);
                     67:        int     *enabled;
                     68: };
                     69:
1.1       markus     70: /* protocol */
                     71:
1.20.2.8! brad       72: static void input_service_request(int, u_int32_t, void *);
        !            73: static void input_userauth_request(int, u_int32_t, void *);
1.1       markus     74:
                     75: /* helper */
1.20.2.5  miod       76: static Authmethod *authmethod_lookup(const char *);
                     77: static char *authmethods_get(void);
                     78: static int user_key_allowed(struct passwd *, Key *);
                     79: static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
1.1       markus     80:
1.18      markus     81: /* auth */
1.20.2.5  miod       82: static void userauth_banner(void);
                     83: static int userauth_none(Authctxt *);
                     84: static int userauth_passwd(Authctxt *);
                     85: static int userauth_pubkey(Authctxt *);
                     86: static int userauth_hostbased(Authctxt *);
                     87: static int userauth_kbdint(Authctxt *);
1.18      markus     88:
                     89: Authmethod authmethods[] = {
                     90:        {"none",
                     91:                userauth_none,
                     92:                &one},
                     93:        {"publickey",
                     94:                userauth_pubkey,
1.20.2.1  jason      95:                &options.pubkey_authentication},
1.18      markus     96:        {"password",
                     97:                userauth_passwd,
                     98:                &options.password_authentication},
1.20.2.1  jason      99:        {"keyboard-interactive",
                    100:                userauth_kbdint,
                    101:                &options.kbd_interactive_authentication},
1.20.2.4  jason     102:        {"hostbased",
                    103:                userauth_hostbased,
                    104:                &options.hostbased_authentication},
1.18      markus    105:        {NULL, NULL, NULL}
1.1       markus    106: };
                    107:
                    108: /*
1.18      markus    109:  * loop until authctxt->success == TRUE
1.1       markus    110:  */
                    111:
                    112: void
1.20.2.8! brad      113: do_authentication2(void)
1.1       markus    114: {
1.20.2.1  jason     115:        Authctxt *authctxt = authctxt_new();
                    116:
1.18      markus    117:        x_authctxt = authctxt;          /*XXX*/
                    118:
1.20.2.6  miod      119:        /* challenge-response is implemented via keyboard interactive */
1.20.2.5  miod      120:        if (options.challenge_response_authentication)
1.20.2.1  jason     121:                options.kbd_interactive_authentication = 1;
                    122:
1.20.2.8! brad      123:        dispatch_init(&dispatch_protocol_error);
1.1       markus    124:        dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18      markus    125:        dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.20.2.4  jason     126:        do_authenticated(authctxt);
1.1       markus    127: }
                    128:
1.20.2.5  miod      129: static void
1.20.2.8! brad      130: input_service_request(int type, u_int32_t seq, void *ctxt)
1.1       markus    131: {
1.18      markus    132:        Authctxt *authctxt = ctxt;
1.20.2.1  jason     133:        u_int len;
1.1       markus    134:        int accept = 0;
                    135:        char *service = packet_get_string(&len);
1.20.2.8! brad      136:        packet_check_eom();
1.1       markus    137:
1.18      markus    138:        if (authctxt == NULL)
                    139:                fatal("input_service_request: no authctxt");
                    140:
1.1       markus    141:        if (strcmp(service, "ssh-userauth") == 0) {
1.18      markus    142:                if (!authctxt->success) {
1.1       markus    143:                        accept = 1;
                    144:                        /* now we can handle user-auth requests */
                    145:                        dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
                    146:                }
                    147:        }
                    148:        /* XXX all other service requests are denied */
                    149:
                    150:        if (accept) {
                    151:                packet_start(SSH2_MSG_SERVICE_ACCEPT);
                    152:                packet_put_cstring(service);
                    153:                packet_send();
                    154:                packet_write_wait();
                    155:        } else {
                    156:                debug("bad service request %s", service);
                    157:                packet_disconnect("bad service request %s", service);
                    158:        }
                    159:        xfree(service);
                    160: }
                    161:
1.20.2.5  miod      162: static void
1.20.2.8! brad      163: input_userauth_request(int type, u_int32_t seq, void *ctxt)
1.1       markus    164: {
1.18      markus    165:        Authctxt *authctxt = ctxt;
                    166:        Authmethod *m = NULL;
1.20.2.1  jason     167:        char *user, *service, *method, *style = NULL;
1.1       markus    168:        int authenticated = 0;
                    169:
1.18      markus    170:        if (authctxt == NULL)
                    171:                fatal("input_userauth_request: no authctxt");
1.1       markus    172:
1.18      markus    173:        user = packet_get_string(NULL);
                    174:        service = packet_get_string(NULL);
                    175:        method = packet_get_string(NULL);
1.1       markus    176:        debug("userauth-request for user %s service %s method %s", user, service, method);
1.20.2.1  jason     177:        debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
                    178:
                    179:        if ((style = strchr(user, ':')) != NULL)
                    180:                *style++ = 0;
1.1       markus    181:
1.20.2.1  jason     182:        if (authctxt->attempt++ == 0) {
1.18      markus    183:                /* setup auth context */
                    184:                struct passwd *pw = NULL;
                    185:                pw = getpwnam(user);
                    186:                if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
                    187:                        authctxt->pw = pwcopy(pw);
                    188:                        authctxt->valid = 1;
                    189:                        debug2("input_userauth_request: setting up authctxt for %s", user);
                    190:                } else {
                    191:                        log("input_userauth_request: illegal user %s", user);
                    192:                }
1.20.2.1  jason     193:                setproctitle("%s", pw ? user : "unknown");
1.18      markus    194:                authctxt->user = xstrdup(user);
                    195:                authctxt->service = xstrdup(service);
1.20.2.5  miod      196:                authctxt->style = style ? xstrdup(style) : NULL;
                    197:        } else if (strcmp(user, authctxt->user) != 0 ||
                    198:            strcmp(service, authctxt->service) != 0) {
                    199:                packet_disconnect("Change of username or service not allowed: "
                    200:                    "(%s,%s) -> (%s,%s)",
                    201:                    authctxt->user, authctxt->service, user, service);
1.1       markus    202:        }
1.20.2.1  jason     203:        /* reset state */
1.20.2.8! brad      204:        auth2_challenge_stop(authctxt);
1.20.2.1  jason     205:        authctxt->postponed = 0;
1.18      markus    206:
1.20.2.1  jason     207:        /* try to authenticate user */
1.18      markus    208:        m = authmethod_lookup(method);
                    209:        if (m != NULL) {
                    210:                debug2("input_userauth_request: try method %s", method);
                    211:                authenticated = m->userauth(authctxt);
                    212:        }
1.20.2.4  jason     213:        userauth_finish(authctxt, authenticated, method);
                    214:
                    215:        xfree(service);
                    216:        xfree(user);
                    217:        xfree(method);
                    218: }
                    219:
                    220: void
                    221: userauth_finish(Authctxt *authctxt, int authenticated, char *method)
                    222: {
1.20.2.5  miod      223:        char *methods;
                    224:
1.20.2.1  jason     225:        if (!authctxt->valid && authenticated)
                    226:                fatal("INTERNAL ERROR: authenticated invalid user %s",
                    227:                    authctxt->user);
1.18      markus    228:
                    229:        /* Special handling for root */
1.20.2.1  jason     230:        if (authenticated && authctxt->pw->pw_uid == 0 &&
                    231:            !auth_root_allowed(method))
1.3       markus    232:                authenticated = 0;
                    233:
1.18      markus    234:        /* Log before sending the reply */
1.20.2.1  jason     235:        auth_log(authctxt, authenticated, method, " ssh2");
                    236:
1.20.2.5  miod      237:        if (authctxt->postponed)
                    238:                return;
                    239:
                    240:        /* XXX todo: check if multiple auth methods are needed */
                    241:        if (authenticated == 1) {
                    242:                /* turn off userauth */
1.20.2.8! brad      243:                dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
1.20.2.5  miod      244:                packet_start(SSH2_MSG_USERAUTH_SUCCESS);
                    245:                packet_send();
                    246:                packet_write_wait();
                    247:                /* now we can break out */
                    248:                authctxt->success = 1;
                    249:        } else {
                    250:                if (authctxt->failures++ > AUTH_FAIL_MAX)
                    251:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
                    252:                methods = authmethods_get();
                    253:                packet_start(SSH2_MSG_USERAUTH_FAILURE);
                    254:                packet_put_cstring(methods);
                    255:                packet_put_char(0);     /* XXX partial success, unused */
                    256:                packet_send();
                    257:                packet_write_wait();
                    258:                xfree(methods);
                    259:        }
1.18      markus    260: }
                    261:
1.20.2.5  miod      262: static void
1.20.2.1  jason     263: userauth_banner(void)
1.18      markus    264: {
1.20.2.1  jason     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.20.2.4  jason     272:        if ((fd = open(options.banner, O_RDONLY)) < 0)
1.20.2.1  jason     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;
1.18      markus    291: }
1.6       markus    292:
1.20.2.5  miod      293: static int
1.18      markus    294: userauth_none(Authctxt *authctxt)
1.1       markus    295: {
1.18      markus    296:        /* disable method "none", only allowed one time */
                    297:        Authmethod *m = authmethod_lookup("none");
                    298:        if (m != NULL)
                    299:                m->enabled = NULL;
1.20.2.8! brad      300:        packet_check_eom();
1.20.2.1  jason     301:        userauth_banner();
1.20.2.4  jason     302:        return authctxt->valid ? auth_password(authctxt, "") : 0;
1.1       markus    303: }
1.18      markus    304:
1.20.2.5  miod      305: static int
1.18      markus    306: userauth_passwd(Authctxt *authctxt)
1.1       markus    307: {
                    308:        char *password;
                    309:        int authenticated = 0;
                    310:        int change;
1.20.2.1  jason     311:        u_int len;
1.1       markus    312:        change = packet_get_char();
                    313:        if (change)
                    314:                log("password change not supported");
                    315:        password = packet_get_string(&len);
1.20.2.8! brad      316:        packet_check_eom();
1.18      markus    317:        if (authctxt->valid &&
1.20.2.4  jason     318:            auth_password(authctxt, password) == 1)
1.1       markus    319:                authenticated = 1;
                    320:        memset(password, 0, len);
                    321:        xfree(password);
                    322:        return authenticated;
                    323: }
1.18      markus    324:
1.20.2.5  miod      325: static int
1.18      markus    326: userauth_kbdint(Authctxt *authctxt)
                    327: {
                    328:        int authenticated = 0;
1.20.2.5  miod      329:        char *lang, *devs;
1.20.2.8! brad      330:
1.18      markus    331:        lang = packet_get_string(NULL);
                    332:        devs = packet_get_string(NULL);
1.20.2.8! brad      333:        packet_check_eom();
1.18      markus    334:
1.20.2.5  miod      335:        debug("keyboard-interactive devs %s", devs);
1.20.2.1  jason     336:
1.20.2.5  miod      337:        if (options.challenge_response_authentication)
1.20.2.1  jason     338:                authenticated = auth2_challenge(authctxt, devs);
                    339:
1.18      markus    340:        xfree(devs);
1.20.2.5  miod      341:        xfree(lang);
1.18      markus    342:        return authenticated;
                    343: }
                    344:
1.20.2.5  miod      345: static int
1.18      markus    346: userauth_pubkey(Authctxt *authctxt)
1.1       markus    347: {
                    348:        Buffer b;
1.20.2.8! brad      349:        Key *key = NULL;
        !           350:        char *pkalg;
        !           351:        u_char *pkblob, *sig;
1.20.2.1  jason     352:        u_int alen, blen, slen;
                    353:        int have_sig, pktype;
1.1       markus    354:        int authenticated = 0;
                    355:
1.18      markus    356:        if (!authctxt->valid) {
                    357:                debug2("userauth_pubkey: disabled because of invalid user");
1.8       markus    358:                return 0;
                    359:        }
1.1       markus    360:        have_sig = packet_get_char();
1.20.2.1  jason     361:        if (datafellows & SSH_BUG_PKAUTH) {
                    362:                debug2("userauth_pubkey: SSH_BUG_PKAUTH");
                    363:                /* no explicit pkalg given */
                    364:                pkblob = packet_get_string(&blen);
                    365:                buffer_init(&b);
                    366:                buffer_append(&b, pkblob, blen);
                    367:                /* so we have to extract the pkalg from the pkblob */
                    368:                pkalg = buffer_get_string(&b, &alen);
                    369:                buffer_free(&b);
                    370:        } else {
                    371:                pkalg = packet_get_string(&alen);
                    372:                pkblob = packet_get_string(&blen);
                    373:        }
                    374:        pktype = key_type_from_name(pkalg);
                    375:        if (pktype == KEY_UNSPEC) {
                    376:                /* this is perfectly legal */
1.20.2.8! brad      377:                log("userauth_pubkey: unsupported public key algorithm: %s",
        !           378:                    pkalg);
        !           379:                goto done;
1.1       markus    380:        }
1.20.2.1  jason     381:        key = key_from_blob(pkblob, blen);
1.20.2.8! brad      382:        if (key == NULL) {
        !           383:                error("userauth_pubkey: cannot decode key: %s", pkalg);
        !           384:                goto done;
        !           385:        }
        !           386:        if (key->type != pktype) {
        !           387:                error("userauth_pubkey: type mismatch for decoded key "
        !           388:                    "(received %d, expected %d)", key->type, pktype);
        !           389:                goto done;
        !           390:        }
        !           391:        if (have_sig) {
        !           392:                sig = packet_get_string(&slen);
        !           393:                packet_check_eom();
        !           394:                buffer_init(&b);
        !           395:                if (datafellows & SSH_OLD_SESSIONID) {
        !           396:                        buffer_append(&b, session_id2, session_id2_len);
        !           397:                } else {
        !           398:                        buffer_put_string(&b, session_id2, session_id2_len);
        !           399:                }
        !           400:                /* reconstruct packet */
        !           401:                buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
        !           402:                buffer_put_cstring(&b, authctxt->user);
        !           403:                buffer_put_cstring(&b,
        !           404:                    datafellows & SSH_BUG_PKSERVICE ?
        !           405:                    "ssh-userauth" :
        !           406:                    authctxt->service);
        !           407:                if (datafellows & SSH_BUG_PKAUTH) {
        !           408:                        buffer_put_char(&b, have_sig);
        !           409:                } else {
        !           410:                        buffer_put_cstring(&b, "publickey");
        !           411:                        buffer_put_char(&b, have_sig);
        !           412:                        buffer_put_cstring(&b, pkalg);
        !           413:                }
        !           414:                buffer_put_string(&b, pkblob, blen);
1.20.2.1  jason     415: #ifdef DEBUG_PK
1.20.2.8! brad      416:                buffer_dump(&b);
1.1       markus    417: #endif
1.20.2.8! brad      418:                /* test for correct signature */
        !           419:                if (user_key_allowed(authctxt->pw, key) &&
        !           420:                    key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
        !           421:                        authenticated = 1;
        !           422:                buffer_clear(&b);
        !           423:                xfree(sig);
        !           424:        } else {
        !           425:                debug("test whether pkalg/pkblob are acceptable");
        !           426:                packet_check_eom();
1.18      markus    427:
1.20.2.8! brad      428:                /* XXX fake reply and always send PK_OK ? */
        !           429:                /*
        !           430:                 * XXX this allows testing whether a user is allowed
        !           431:                 * to login: if you happen to have a valid pubkey this
        !           432:                 * message is sent. the message is NEVER sent at all
        !           433:                 * if a user is not allowed to login. is this an
        !           434:                 * issue? -markus
        !           435:                 */
        !           436:                if (user_key_allowed(authctxt->pw, key)) {
        !           437:                        packet_start(SSH2_MSG_USERAUTH_PK_OK);
        !           438:                        packet_put_string(pkalg, alen);
        !           439:                        packet_put_string(pkblob, blen);
        !           440:                        packet_send();
        !           441:                        packet_write_wait();
        !           442:                        authctxt->postponed = 1;
1.1       markus    443:                }
                    444:        }
1.20.2.8! brad      445:        if (authenticated != 1)
        !           446:                auth_clear_options();
        !           447: done:
1.20.2.1  jason     448:        debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
1.20.2.8! brad      449:        if (key != NULL)
        !           450:                key_free(key);
1.1       markus    451:        xfree(pkalg);
                    452:        xfree(pkblob);
                    453:        return authenticated;
                    454: }
                    455:
1.20.2.5  miod      456: static int
1.20.2.4  jason     457: userauth_hostbased(Authctxt *authctxt)
                    458: {
                    459:        Buffer b;
1.20.2.8! brad      460:        Key *key = NULL;
        !           461:        char *pkalg, *cuser, *chost, *service;
        !           462:        u_char *pkblob, *sig;
1.20.2.4  jason     463:        u_int alen, blen, slen;
                    464:        int pktype;
                    465:        int authenticated = 0;
                    466:
                    467:        if (!authctxt->valid) {
                    468:                debug2("userauth_hostbased: disabled because of invalid user");
                    469:                return 0;
                    470:        }
                    471:        pkalg = packet_get_string(&alen);
                    472:        pkblob = packet_get_string(&blen);
                    473:        chost = packet_get_string(NULL);
                    474:        cuser = packet_get_string(NULL);
                    475:        sig = packet_get_string(&slen);
                    476:
                    477:        debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
                    478:            cuser, chost, pkalg, slen);
                    479: #ifdef DEBUG_PK
                    480:        debug("signature:");
                    481:        buffer_init(&b);
                    482:        buffer_append(&b, sig, slen);
                    483:        buffer_dump(&b);
                    484:        buffer_free(&b);
                    485: #endif
                    486:        pktype = key_type_from_name(pkalg);
                    487:        if (pktype == KEY_UNSPEC) {
                    488:                /* this is perfectly legal */
                    489:                log("userauth_hostbased: unsupported "
                    490:                    "public key algorithm: %s", pkalg);
                    491:                goto done;
                    492:        }
                    493:        key = key_from_blob(pkblob, blen);
                    494:        if (key == NULL) {
1.20.2.8! brad      495:                error("userauth_hostbased: cannot decode key: %s", pkalg);
        !           496:                goto done;
        !           497:        }
        !           498:        if (key->type != pktype) {
        !           499:                error("userauth_hostbased: type mismatch for decoded key "
        !           500:                    "(received %d, expected %d)", key->type, pktype);
1.20.2.4  jason     501:                goto done;
                    502:        }
                    503:        service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
                    504:            authctxt->service;
                    505:        buffer_init(&b);
                    506:        buffer_put_string(&b, session_id2, session_id2_len);
                    507:        /* reconstruct packet */
                    508:        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
                    509:        buffer_put_cstring(&b, authctxt->user);
                    510:        buffer_put_cstring(&b, service);
                    511:        buffer_put_cstring(&b, "hostbased");
                    512:        buffer_put_string(&b, pkalg, alen);
                    513:        buffer_put_string(&b, pkblob, blen);
                    514:        buffer_put_cstring(&b, chost);
                    515:        buffer_put_cstring(&b, cuser);
                    516: #ifdef DEBUG_PK
                    517:        buffer_dump(&b);
                    518: #endif
                    519:        /* test for allowed key and correct signature */
                    520:        if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
                    521:            key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
                    522:                authenticated = 1;
                    523:
                    524:        buffer_clear(&b);
                    525: done:
                    526:        debug2("userauth_hostbased: authenticated %d", authenticated);
1.20.2.8! brad      527:        if (key != NULL)
        !           528:                key_free(key);
1.20.2.4  jason     529:        xfree(pkalg);
                    530:        xfree(pkblob);
                    531:        xfree(cuser);
                    532:        xfree(chost);
                    533:        xfree(sig);
                    534:        return authenticated;
                    535: }
                    536:
1.18      markus    537: /* get current user */
1.1       markus    538:
                    539: struct passwd*
                    540: auth_get_user(void)
                    541: {
1.18      markus    542:        return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
1.1       markus    543: }
                    544:
1.18      markus    545: #define        DELIM   ","
                    546:
1.20.2.5  miod      547: static char *
1.18      markus    548: authmethods_get(void)
1.1       markus    549: {
1.18      markus    550:        Authmethod *method = NULL;
1.20.2.8! brad      551:        Buffer b;
1.18      markus    552:        char *list;
1.1       markus    553:
1.20.2.8! brad      554:        buffer_init(&b);
1.18      markus    555:        for (method = authmethods; method->name != NULL; method++) {
                    556:                if (strcmp(method->name, "none") == 0)
                    557:                        continue;
                    558:                if (method->enabled != NULL && *(method->enabled) != 0) {
1.20.2.8! brad      559:                        if (buffer_len(&b) > 0)
        !           560:                                buffer_append(&b, ",", 1);
        !           561:                        buffer_append(&b, method->name, strlen(method->name));
1.1       markus    562:                }
                    563:        }
1.20.2.8! brad      564:        buffer_append(&b, "\0", 1);
        !           565:        list = xstrdup(buffer_ptr(&b));
        !           566:        buffer_free(&b);
1.18      markus    567:        return list;
                    568: }
                    569:
1.20.2.5  miod      570: static Authmethod *
1.18      markus    571: authmethod_lookup(const char *name)
                    572: {
                    573:        Authmethod *method = NULL;
                    574:        if (name != NULL)
                    575:                for (method = authmethods; method->name != NULL; method++)
                    576:                        if (method->enabled != NULL &&
                    577:                            *(method->enabled) != 0 &&
                    578:                            strcmp(name, method->name) == 0)
                    579:                                return method;
                    580:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                    581:        return NULL;
1.1       markus    582: }
                    583:
                    584: /* return 1 if user allows given key */
1.20.2.5  miod      585: static int
                    586: user_key_allowed2(struct passwd *pw, Key *key, char *file)
1.1       markus    587: {
1.20.2.5  miod      588:        char line[8192];
1.1       markus    589:        int found_key = 0;
                    590:        FILE *f;
1.20.2.1  jason     591:        u_long linenum = 0;
1.1       markus    592:        struct stat st;
                    593:        Key *found;
1.20.2.8! brad      594:        char *fp;
1.1       markus    595:
1.18      markus    596:        if (pw == NULL)
                    597:                return 0;
                    598:
1.1       markus    599:        /* Temporarily use the user's uid. */
1.20.2.4  jason     600:        temporarily_use_uid(pw);
1.1       markus    601:
1.20.2.5  miod      602:        debug("trying public key file %s", file);
1.1       markus    603:
                    604:        /* Fail quietly if file does not exist */
                    605:        if (stat(file, &st) < 0) {
                    606:                /* Restore the privileged uid. */
                    607:                restore_uid();
                    608:                return 0;
                    609:        }
                    610:        /* Open the file containing the authorized keys. */
                    611:        f = fopen(file, "r");
                    612:        if (!f) {
                    613:                /* Restore the privileged uid. */
                    614:                restore_uid();
                    615:                return 0;
                    616:        }
1.20.2.5  miod      617:        if (options.strict_modes &&
                    618:            secure_filename(f, file, pw, line, sizeof(line)) != 0) {
                    619:                fclose(f);
                    620:                log("Authentication refused: %s", line);
                    621:                restore_uid();
                    622:                return 0;
1.1       markus    623:        }
1.20.2.5  miod      624:
1.1       markus    625:        found_key = 0;
1.16      markus    626:        found = key_new(key->type);
1.1       markus    627:
                    628:        while (fgets(line, sizeof(line), f)) {
1.10      markus    629:                char *cp, *options = NULL;
1.1       markus    630:                linenum++;
                    631:                /* Skip leading whitespace, empty and comment lines. */
                    632:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    633:                        ;
                    634:                if (!*cp || *cp == '\n' || *cp == '#')
                    635:                        continue;
1.10      markus    636:
1.20.2.5  miod      637:                if (key_read(found, &cp) != 1) {
1.10      markus    638:                        /* no key?  check if there are options for this key */
                    639:                        int quoted = 0;
1.20.2.1  jason     640:                        debug2("user_key_allowed: check options: '%s'", cp);
1.10      markus    641:                        options = cp;
                    642:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    643:                                if (*cp == '\\' && cp[1] == '"')
                    644:                                        cp++;   /* Skip both */
                    645:                                else if (*cp == '"')
                    646:                                        quoted = !quoted;
                    647:                        }
                    648:                        /* Skip remaining whitespace. */
                    649:                        for (; *cp == ' ' || *cp == '\t'; cp++)
                    650:                                ;
1.20.2.5  miod      651:                        if (key_read(found, &cp) != 1) {
1.20.2.1  jason     652:                                debug2("user_key_allowed: advance: '%s'", cp);
1.10      markus    653:                                /* still no key?  advance to next line*/
                    654:                                continue;
                    655:                        }
                    656:                }
                    657:                if (key_equal(found, key) &&
1.20.2.1  jason     658:                    auth_parse_options(pw, options, file, linenum) == 1) {
1.1       markus    659:                        found_key = 1;
1.20.2.5  miod      660:                        debug("matching key found: file %s, line %lu",
1.1       markus    661:                            file, linenum);
1.20.2.8! brad      662:                        fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
        !           663:                        verbose("Found matching %s key: %s",
        !           664:                            key_type(found), fp);
        !           665:                        xfree(fp);
1.1       markus    666:                        break;
                    667:                }
                    668:        }
                    669:        restore_uid();
                    670:        fclose(f);
                    671:        key_free(found);
1.20.2.3  jason     672:        if (!found_key)
                    673:                debug2("key not found");
1.1       markus    674:        return found_key;
1.20.2.4  jason     675: }
                    676:
1.20.2.5  miod      677: /* check whether given key is in .ssh/authorized_keys* */
                    678: static int
                    679: user_key_allowed(struct passwd *pw, Key *key)
                    680: {
                    681:        int success;
                    682:        char *file;
                    683:
                    684:        file = authorized_keys_file(pw);
                    685:        success = user_key_allowed2(pw, key, file);
                    686:        xfree(file);
                    687:        if (success)
                    688:                return success;
                    689:
                    690:        /* try suffix "2" for backward compat, too */
                    691:        file = authorized_keys_file2(pw);
                    692:        success = user_key_allowed2(pw, key, file);
                    693:        xfree(file);
                    694:        return success;
                    695: }
                    696:
1.20.2.4  jason     697: /* return 1 if given hostkey is allowed */
1.20.2.5  miod      698: static int
1.20.2.4  jason     699: hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
                    700:     Key *key)
                    701: {
                    702:        const char *resolvedname, *ipaddr, *lookup;
1.20.2.8! brad      703:        HostStatus host_status;
        !           704:        int len;
1.20.2.4  jason     705:
1.20.2.8! brad      706:        resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
1.20.2.4  jason     707:        ipaddr = get_remote_ipaddr();
                    708:
                    709:        debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
                    710:            chost, resolvedname, ipaddr);
                    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 {
                    717:                if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
                    718:                        debug2("stripping trailing dot from chost %s", chost);
                    719:                        chost[len - 1] = '\0';
                    720:                }
                    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:
1.20.2.5  miod      731:        host_status = check_key_in_hostfiles(pw, key, lookup,
                    732:            _PATH_SSH_SYSTEM_HOSTFILE,
                    733:            options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
                    734:
                    735:        /* backward compat if no key has been found. */
                    736:        if (host_status == HOST_NEW)
                    737:                host_status = check_key_in_hostfiles(pw, key, lookup,
                    738:                    _PATH_SSH_SYSTEM_HOSTFILE2,
                    739:                    options.ignore_user_known_hosts ? NULL :
                    740:                    _PATH_SSH_USER_HOSTFILE2);
1.20.2.4  jason     741:
                    742:        return (host_status == HOST_OK);
1.1       markus    743: }