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

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