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

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.86    ! provos     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.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"
1.57      markus     53: #include "match.h"
1.1       markus     54:
                     55: /* import */
                     56: extern ServerOptions options;
1.23      markus     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.80      markus     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.66      itojun     76: static Authmethod *authmethod_lookup(const char *);
1.67      stevesk    77: static char *authmethods_get(void);
1.66      itojun     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.66      itojun     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.21      markus     95:                &options.pubkey_authentication},
1.40      markus     96:        {"password",
                     97:                userauth_passwd,
                     98:                &options.password_authentication},
1.18      markus     99:        {"keyboard-interactive",
                    100:                userauth_kbdint,
                    101:                &options.kbd_interactive_authentication},
1.52      markus    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.74      itojun    113: do_authentication2(void)
1.1       markus    114: {
1.28      markus    115:        Authctxt *authctxt = authctxt_new();
                    116:
1.18      markus    117:        x_authctxt = authctxt;          /*XXX*/
                    118:
1.71      markus    119:        /* challenge-response is implemented via keyboard interactive */
1.57      markus    120:        if (options.challenge_response_authentication)
1.34      markus    121:                options.kbd_interactive_authentication = 1;
                    122:
1.81      markus    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.48      markus    126:        do_authenticated(authctxt);
1.1       markus    127: }
                    128:
1.66      itojun    129: static void
1.80      markus    130: input_service_request(int type, u_int32_t seq, void *ctxt)
1.1       markus    131: {
1.18      markus    132:        Authctxt *authctxt = ctxt;
1.23      markus    133:        u_int len;
1.1       markus    134:        int accept = 0;
                    135:        char *service = packet_get_string(&len);
1.79      markus    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.66      itojun    162: static void
1.80      markus    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.28      markus    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.24      markus    177:        debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1       markus    178:
1.28      markus    179:        if ((style = strchr(user, ':')) != NULL)
                    180:                *style++ = 0;
                    181:
1.36      stevesk   182:        if (authctxt->attempt++ == 0) {
1.18      markus    183:                /* setup auth context */
                    184:                struct passwd *pw = NULL;
1.86    ! provos    185:                pw = getpwnamallow(user);
        !           186:                if (pw && strcmp(service, "ssh-connection")==0) {
1.18      markus    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.42      markus    193:                setproctitle("%s", pw ? user : "unknown");
1.18      markus    194:                authctxt->user = xstrdup(user);
                    195:                authctxt->service = xstrdup(service);
1.62      markus    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.28      markus    203:        /* reset state */
1.75      markus    204:        auth2_challenge_stop(authctxt);
1.28      markus    205:        authctxt->postponed = 0;
1.18      markus    206:
1.28      markus    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.49      markus    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.60      markus    223:        char *methods;
                    224:
1.28      markus    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.41      markus    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.28      markus    235:        auth_log(authctxt, authenticated, method, " ssh2");
                    236:
1.60      markus    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.81      markus    243:                dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
1.60      markus    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.66      itojun    262: static void
1.25      markus    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;
1.1       markus    291: }
                    292:
1.66      itojun    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.79      markus    300:        packet_check_eom();
1.25      markus    301:        userauth_banner();
1.47      markus    302:        return authctxt->valid ? auth_password(authctxt, "") : 0;
1.1       markus    303: }
1.18      markus    304:
1.66      itojun    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.23      markus    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.79      markus    316:        packet_check_eom();
1.18      markus    317:        if (authctxt->valid &&
1.47      markus    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.66      itojun    325: static int
1.18      markus    326: userauth_kbdint(Authctxt *authctxt)
                    327: {
                    328:        int authenticated = 0;
1.57      markus    329:        char *lang, *devs;
1.77      deraadt   330:
1.18      markus    331:        lang = packet_get_string(NULL);
                    332:        devs = packet_get_string(NULL);
1.79      markus    333:        packet_check_eom();
1.18      markus    334:
1.57      markus    335:        debug("keyboard-interactive devs %s", devs);
1.28      markus    336:
1.57      markus    337:        if (options.challenge_response_authentication)
1.34      markus    338:                authenticated = auth2_challenge(authctxt, devs);
1.28      markus    339:
1.57      markus    340:        xfree(devs);
1.18      markus    341:        xfree(lang);
                    342:        return authenticated;
                    343: }
                    344:
1.66      itojun    345: static int
1.18      markus    346: userauth_pubkey(Authctxt *authctxt)
1.1       markus    347: {
                    348:        Buffer b;
1.84      markus    349:        Key *key = NULL;
1.85      markus    350:        char *pkalg;
                    351:        u_char *pkblob, *sig;
1.23      markus    352:        u_int alen, blen, slen;
1.21      markus    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.22      markus    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:        }
1.21      markus    374:        pktype = key_type_from_name(pkalg);
                    375:        if (pktype == KEY_UNSPEC) {
1.22      markus    376:                /* this is perfectly legal */
1.84      markus    377:                log("userauth_pubkey: unsupported public key algorithm: %s",
                    378:                    pkalg);
                    379:                goto done;
1.1       markus    380:        }
1.21      markus    381:        key = key_from_blob(pkblob, blen);
1.84      markus    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.21      markus    415: #ifdef DEBUG_PK
1.84      markus    416:                buffer_dump(&b);
1.1       markus    417: #endif
1.84      markus    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.84      markus    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.84      markus    445:        if (authenticated != 1)
                    446:                auth_clear_options();
                    447: done:
1.21      markus    448:        debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
1.84      markus    449:        if (key != NULL)
                    450:                key_free(key);
1.1       markus    451:        xfree(pkalg);
                    452:        xfree(pkblob);
                    453:        return authenticated;
                    454: }
                    455:
1.66      itojun    456: static int
1.52      markus    457: userauth_hostbased(Authctxt *authctxt)
                    458: {
                    459:        Buffer b;
1.84      markus    460:        Key *key = NULL;
1.85      markus    461:        char *pkalg, *cuser, *chost, *service;
                    462:        u_char *pkblob, *sig;
1.52      markus    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.84      markus    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.52      markus    501:                goto done;
                    502:        }
1.55      markus    503:        service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
                    504:            authctxt->service;
1.52      markus    505:        buffer_init(&b);
1.55      markus    506:        buffer_put_string(&b, session_id2, session_id2_len);
1.52      markus    507:        /* reconstruct packet */
                    508:        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
                    509:        buffer_put_cstring(&b, authctxt->user);
1.55      markus    510:        buffer_put_cstring(&b, service);
1.52      markus    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.84      markus    527:        if (key != NULL)
                    528:                key_free(key);
1.52      markus    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.67      stevesk   547: static char *
1.18      markus    548: authmethods_get(void)
1.1       markus    549: {
1.18      markus    550:        Authmethod *method = NULL;
1.82      markus    551:        Buffer b;
1.18      markus    552:        char *list;
1.1       markus    553:
1.82      markus    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.82      markus    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.82      markus    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.66      itojun    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.66      itojun    585: static int
1.63      markus    586: user_key_allowed2(struct passwd *pw, Key *key, char *file)
1.1       markus    587: {
1.63      markus    588:        char line[8192];
1.1       markus    589:        int found_key = 0;
                    590:        FILE *f;
1.23      markus    591:        u_long linenum = 0;
1.1       markus    592:        struct stat st;
                    593:        Key *found;
1.76      jakob     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.51      markus    600:        temporarily_use_uid(pw);
1.1       markus    601:
1.58      markus    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.58      markus    617:        if (options.strict_modes &&
1.68      markus    618:            secure_filename(f, file, pw, line, sizeof(line)) != 0) {
1.58      markus    619:                fclose(f);
                    620:                log("Authentication refused: %s", line);
                    621:                restore_uid();
                    622:                return 0;
1.1       markus    623:        }
1.58      markus    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.70      markus    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.21      markus    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.70      markus    651:                        if (key_read(found, &cp) != 1) {
1.21      markus    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.30      markus    658:                    auth_parse_options(pw, options, file, linenum) == 1) {
1.1       markus    659:                        found_key = 1;
1.69      stevesk   660:                        debug("matching key found: file %s, line %lu",
1.1       markus    661:                            file, linenum);
1.76      jakob     662:                        fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
                    663:                        verbose("Found matching %s key: %s",
1.77      deraadt   664:                            key_type(found), fp);
1.76      jakob     665:                        xfree(fp);
1.1       markus    666:                        break;
                    667:                }
                    668:        }
                    669:        restore_uid();
                    670:        fclose(f);
                    671:        key_free(found);
1.46      markus    672:        if (!found_key)
                    673:                debug2("key not found");
1.1       markus    674:        return found_key;
1.63      markus    675: }
                    676:
                    677: /* check whether given key is in .ssh/authorized_keys* */
1.66      itojun    678: static int
1.63      markus    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;
1.52      markus    695: }
                    696:
                    697: /* return 1 if given hostkey is allowed */
1.66      itojun    698: static int
1.54      markus    699: hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1.52      markus    700:     Key *key)
                    701: {
                    702:        const char *resolvedname, *ipaddr, *lookup;
1.73      stevesk   703:        HostStatus host_status;
                    704:        int len;
1.52      markus    705:
1.83      markus    706:        resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
1.52      markus    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:
1.64      markus    731:        host_status = check_key_in_hostfiles(pw, key, lookup,
                    732:            _PATH_SSH_SYSTEM_HOSTFILE,
1.65      markus    733:            options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
1.64      markus    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,
1.65      markus    739:                    options.ignore_user_known_hosts ? NULL :
                    740:                    _PATH_SSH_USER_HOSTFILE2);
1.52      markus    741:
                    742:        return (host_status == HOST_OK);
1.1       markus    743: }