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

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