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

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