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

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