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

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