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

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.51    ! markus     26: RCSID("$OpenBSD: auth2.c,v 1.50 2001/04/04 20:32:56 markus Exp $");
1.1       markus     27:
                     28: #include <openssl/evp.h>
                     29:
1.32      markus     30: #include "ssh2.h"
1.1       markus     31: #include "xmalloc.h"
                     32: #include "rsa.h"
1.45      djm        33: #include "sshpty.h"
1.1       markus     34: #include "packet.h"
                     35: #include "buffer.h"
1.32      markus     36: #include "log.h"
1.1       markus     37: #include "servconf.h"
                     38: #include "compat.h"
                     39: #include "channels.h"
                     40: #include "bufaux.h"
                     41: #include "auth.h"
                     42: #include "session.h"
                     43: #include "dispatch.h"
                     44: #include "key.h"
1.32      markus     45: #include "cipher.h"
                     46: #include "kex.h"
1.29      markus     47: #include "pathnames.h"
1.1       markus     48: #include "uidswap.h"
1.10      markus     49: #include "auth-options.h"
1.43      markus     50: #include "misc.h"
1.1       markus     51:
                     52: /* import */
                     53: extern ServerOptions options;
1.23      markus     54: extern u_char *session_id2;
1.1       markus     55: extern int session_id2_len;
                     56:
1.18      markus     57: static Authctxt        *x_authctxt = NULL;
                     58: static int one = 1;
                     59:
                     60: typedef struct Authmethod Authmethod;
                     61: struct Authmethod {
                     62:        char    *name;
                     63:        int     (*userauth)(Authctxt *authctxt);
                     64:        int     *enabled;
                     65: };
                     66:
1.1       markus     67: /* protocol */
                     68:
1.15      markus     69: void   input_service_request(int type, int plen, void *ctxt);
                     70: void   input_userauth_request(int type, int plen, void *ctxt);
                     71: void   protocol_error(int type, int plen, void *ctxt);
1.1       markus     72:
                     73: /* helper */
1.18      markus     74: Authmethod     *authmethod_lookup(const char *name);
1.21      markus     75: int    user_key_allowed(struct passwd *pw, Key *key);
1.18      markus     76: char   *authmethods_get(void);
1.1       markus     77:
1.18      markus     78: /* auth */
1.25      markus     79: void   userauth_banner(void);
1.49      markus     80: void   userauth_reply(Authctxt *authctxt, int authenticated);
1.18      markus     81: int    userauth_none(Authctxt *authctxt);
                     82: int    userauth_passwd(Authctxt *authctxt);
                     83: int    userauth_pubkey(Authctxt *authctxt);
                     84: int    userauth_kbdint(Authctxt *authctxt);
                     85:
                     86: Authmethod authmethods[] = {
                     87:        {"none",
                     88:                userauth_none,
                     89:                &one},
                     90:        {"publickey",
                     91:                userauth_pubkey,
1.21      markus     92:                &options.pubkey_authentication},
1.40      markus     93:        {"password",
                     94:                userauth_passwd,
                     95:                &options.password_authentication},
1.18      markus     96:        {"keyboard-interactive",
                     97:                userauth_kbdint,
                     98:                &options.kbd_interactive_authentication},
                     99:        {NULL, NULL, NULL}
1.1       markus    100: };
                    101:
                    102: /*
1.18      markus    103:  * loop until authctxt->success == TRUE
1.1       markus    104:  */
                    105:
                    106: void
                    107: do_authentication2()
                    108: {
1.28      markus    109:        Authctxt *authctxt = authctxt_new();
                    110:
1.18      markus    111:        x_authctxt = authctxt;          /*XXX*/
                    112:
1.34      markus    113:        /* challenge-reponse is implemented via keyboard interactive */
                    114:        if (options.challenge_reponse_authentication)
                    115:                options.kbd_interactive_authentication = 1;
                    116:
1.1       markus    117:        dispatch_init(&protocol_error);
                    118:        dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18      markus    119:        dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.48      markus    120:        do_authenticated(authctxt);
1.1       markus    121: }
                    122:
                    123: void
1.15      markus    124: protocol_error(int type, int plen, void *ctxt)
1.1       markus    125: {
                    126:        log("auth: protocol error: type %d plen %d", type, plen);
                    127:        packet_start(SSH2_MSG_UNIMPLEMENTED);
                    128:        packet_put_int(0);
                    129:        packet_send();
                    130:        packet_write_wait();
                    131: }
                    132:
                    133: void
1.15      markus    134: input_service_request(int type, int plen, void *ctxt)
1.1       markus    135: {
1.18      markus    136:        Authctxt *authctxt = ctxt;
1.23      markus    137:        u_int len;
1.1       markus    138:        int accept = 0;
                    139:        char *service = packet_get_string(&len);
                    140:        packet_done();
                    141:
1.18      markus    142:        if (authctxt == NULL)
                    143:                fatal("input_service_request: no authctxt");
                    144:
1.1       markus    145:        if (strcmp(service, "ssh-userauth") == 0) {
1.18      markus    146:                if (!authctxt->success) {
1.1       markus    147:                        accept = 1;
                    148:                        /* now we can handle user-auth requests */
                    149:                        dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
                    150:                }
                    151:        }
                    152:        /* XXX all other service requests are denied */
                    153:
                    154:        if (accept) {
                    155:                packet_start(SSH2_MSG_SERVICE_ACCEPT);
                    156:                packet_put_cstring(service);
                    157:                packet_send();
                    158:                packet_write_wait();
                    159:        } else {
                    160:                debug("bad service request %s", service);
                    161:                packet_disconnect("bad service request %s", service);
                    162:        }
                    163:        xfree(service);
                    164: }
                    165:
                    166: void
1.15      markus    167: input_userauth_request(int type, int plen, void *ctxt)
1.1       markus    168: {
1.18      markus    169:        Authctxt *authctxt = ctxt;
                    170:        Authmethod *m = NULL;
1.28      markus    171:        char *user, *service, *method, *style = NULL;
1.1       markus    172:        int authenticated = 0;
                    173:
1.18      markus    174:        if (authctxt == NULL)
                    175:                fatal("input_userauth_request: no authctxt");
1.1       markus    176:
1.18      markus    177:        user = packet_get_string(NULL);
                    178:        service = packet_get_string(NULL);
                    179:        method = packet_get_string(NULL);
1.1       markus    180:        debug("userauth-request for user %s service %s method %s", user, service, method);
1.24      markus    181:        debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1       markus    182:
1.28      markus    183:        if ((style = strchr(user, ':')) != NULL)
                    184:                *style++ = 0;
                    185:
1.36      stevesk   186:        if (authctxt->attempt++ == 0) {
1.18      markus    187:                /* setup auth context */
                    188:                struct passwd *pw = NULL;
                    189:                pw = getpwnam(user);
                    190:                if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
                    191:                        authctxt->pw = pwcopy(pw);
                    192:                        authctxt->valid = 1;
                    193:                        debug2("input_userauth_request: setting up authctxt for %s", user);
                    194:                } else {
                    195:                        log("input_userauth_request: illegal user %s", user);
                    196:                }
1.42      markus    197:                setproctitle("%s", pw ? user : "unknown");
1.18      markus    198:                authctxt->user = xstrdup(user);
                    199:                authctxt->service = xstrdup(service);
1.28      markus    200:                authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
1.18      markus    201:        } else if (authctxt->valid) {
                    202:                if (strcmp(user, authctxt->user) != 0 ||
                    203:                    strcmp(service, authctxt->service) != 0) {
                    204:                        log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
                    205:                            user, service, authctxt->user, authctxt->service);
                    206:                        authctxt->valid = 0;
1.1       markus    207:                }
                    208:        }
1.28      markus    209:        /* reset state */
                    210:        dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
                    211:        authctxt->postponed = 0;
1.47      markus    212: #ifdef BSD_AUTH
                    213:        if (authctxt->as) {
                    214:                auth_close(authctxt->as);
                    215:                authctxt->as = NULL;
                    216:        }
                    217: #endif
1.18      markus    218:
1.28      markus    219:        /* try to authenticate user */
1.18      markus    220:        m = authmethod_lookup(method);
                    221:        if (m != NULL) {
                    222:                debug2("input_userauth_request: try method %s", method);
                    223:                authenticated = m->userauth(authctxt);
                    224:        }
1.49      markus    225:        userauth_finish(authctxt, authenticated, method);
                    226:
                    227:        xfree(service);
                    228:        xfree(user);
                    229:        xfree(method);
                    230: }
                    231:
                    232: void
                    233: userauth_finish(Authctxt *authctxt, int authenticated, char *method)
                    234: {
1.28      markus    235:        if (!authctxt->valid && authenticated)
                    236:                fatal("INTERNAL ERROR: authenticated invalid user %s",
                    237:                    authctxt->user);
1.18      markus    238:
                    239:        /* Special handling for root */
1.41      markus    240:        if (authenticated && authctxt->pw->pw_uid == 0 &&
                    241:            !auth_root_allowed(method))
1.3       markus    242:                authenticated = 0;
                    243:
1.18      markus    244:        /* Log before sending the reply */
1.28      markus    245:        auth_log(authctxt, authenticated, method, " ssh2");
                    246:
                    247:        if (!authctxt->postponed)
                    248:                userauth_reply(authctxt, authenticated);
1.18      markus    249: }
                    250:
1.25      markus    251: void
                    252: userauth_banner(void)
                    253: {
                    254:        struct stat st;
                    255:        char *banner = NULL;
                    256:        off_t len, n;
                    257:        int fd;
                    258:
                    259:        if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
                    260:                return;
1.50      markus    261:        if ((fd = open(options.banner, O_RDONLY)) < 0)
1.25      markus    262:                return;
                    263:        if (fstat(fd, &st) < 0)
                    264:                goto done;
                    265:        len = st.st_size;
                    266:        banner = xmalloc(len + 1);
                    267:        if ((n = read(fd, banner, len)) < 0)
                    268:                goto done;
                    269:        banner[n] = '\0';
                    270:        packet_start(SSH2_MSG_USERAUTH_BANNER);
                    271:        packet_put_cstring(banner);
                    272:        packet_put_cstring("");         /* language, unused */
                    273:        packet_send();
                    274:        debug("userauth_banner: sent");
                    275: done:
                    276:        if (banner)
                    277:                xfree(banner);
                    278:        close(fd);
                    279:        return;
                    280: }
1.18      markus    281:
1.36      stevesk   282: void
1.18      markus    283: userauth_reply(Authctxt *authctxt, int authenticated)
                    284: {
1.24      markus    285:        char *methods;
1.28      markus    286:
1.3       markus    287:        /* XXX todo: check if multiple auth methods are needed */
1.39      markus    288:        if (authenticated == 1) {
1.1       markus    289:                /* turn off userauth */
                    290:                dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
                    291:                packet_start(SSH2_MSG_USERAUTH_SUCCESS);
                    292:                packet_send();
                    293:                packet_write_wait();
                    294:                /* now we can break out */
1.18      markus    295:                authctxt->success = 1;
1.28      markus    296:        } else {
                    297:                if (authctxt->failures++ > AUTH_FAIL_MAX)
1.36      stevesk   298:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.24      markus    299:                methods = authmethods_get();
1.1       markus    300:                packet_start(SSH2_MSG_USERAUTH_FAILURE);
1.18      markus    301:                packet_put_cstring(methods);
                    302:                packet_put_char(0);     /* XXX partial success, unused */
1.1       markus    303:                packet_send();
                    304:                packet_write_wait();
1.18      markus    305:                xfree(methods);
1.1       markus    306:        }
                    307: }
                    308:
                    309: int
1.18      markus    310: userauth_none(Authctxt *authctxt)
1.1       markus    311: {
1.18      markus    312:        /* disable method "none", only allowed one time */
                    313:        Authmethod *m = authmethod_lookup("none");
                    314:        if (m != NULL)
                    315:                m->enabled = NULL;
1.1       markus    316:        packet_done();
1.25      markus    317:        userauth_banner();
1.47      markus    318:        return authctxt->valid ? auth_password(authctxt, "") : 0;
1.1       markus    319: }
1.18      markus    320:
1.1       markus    321: int
1.18      markus    322: userauth_passwd(Authctxt *authctxt)
1.1       markus    323: {
                    324:        char *password;
                    325:        int authenticated = 0;
                    326:        int change;
1.23      markus    327:        u_int len;
1.1       markus    328:        change = packet_get_char();
                    329:        if (change)
                    330:                log("password change not supported");
                    331:        password = packet_get_string(&len);
                    332:        packet_done();
1.18      markus    333:        if (authctxt->valid &&
1.47      markus    334:            auth_password(authctxt, password) == 1)
1.1       markus    335:                authenticated = 1;
                    336:        memset(password, 0, len);
                    337:        xfree(password);
                    338:        return authenticated;
                    339: }
1.18      markus    340:
                    341: int
                    342: userauth_kbdint(Authctxt *authctxt)
                    343: {
                    344:        int authenticated = 0;
                    345:        char *lang = NULL;
                    346:        char *devs = NULL;
                    347:
                    348:        lang = packet_get_string(NULL);
                    349:        devs = packet_get_string(NULL);
                    350:        packet_done();
                    351:
                    352:        debug("keyboard-interactive language %s devs %s", lang, devs);
1.28      markus    353:
1.34      markus    354:        if (options.challenge_reponse_authentication)
                    355:                authenticated = auth2_challenge(authctxt, devs);
1.28      markus    356:
1.18      markus    357:        xfree(lang);
                    358:        xfree(devs);
                    359:        return authenticated;
                    360: }
                    361:
1.1       markus    362: 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);
                    421:                                buffer_put_cstring(&b, key_ssh_name(key));
                    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.18      markus    464: /* get current user */
1.1       markus    465:
                    466: struct passwd*
                    467: auth_get_user(void)
                    468: {
1.18      markus    469:        return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
1.1       markus    470: }
                    471:
1.18      markus    472: #define        DELIM   ","
                    473:
                    474: char *
                    475: authmethods_get(void)
1.1       markus    476: {
1.18      markus    477:        Authmethod *method = NULL;
1.23      markus    478:        u_int size = 0;
1.18      markus    479:        char *list;
1.1       markus    480:
1.18      markus    481:        for (method = authmethods; method->name != NULL; method++) {
                    482:                if (strcmp(method->name, "none") == 0)
                    483:                        continue;
                    484:                if (method->enabled != NULL && *(method->enabled) != 0) {
                    485:                        if (size != 0)
                    486:                                size += strlen(DELIM);
                    487:                        size += strlen(method->name);
1.1       markus    488:                }
1.18      markus    489:        }
                    490:        size++;                 /* trailing '\0' */
                    491:        list = xmalloc(size);
                    492:        list[0] = '\0';
                    493:
                    494:        for (method = authmethods; method->name != NULL; method++) {
                    495:                if (strcmp(method->name, "none") == 0)
                    496:                        continue;
                    497:                if (method->enabled != NULL && *(method->enabled) != 0) {
                    498:                        if (list[0] != '\0')
                    499:                                strlcat(list, DELIM, size);
                    500:                        strlcat(list, method->name, size);
1.1       markus    501:                }
                    502:        }
1.18      markus    503:        return list;
                    504: }
                    505:
                    506: Authmethod *
                    507: authmethod_lookup(const char *name)
                    508: {
                    509:        Authmethod *method = NULL;
                    510:        if (name != NULL)
                    511:                for (method = authmethods; method->name != NULL; method++)
                    512:                        if (method->enabled != NULL &&
                    513:                            *(method->enabled) != 0 &&
                    514:                            strcmp(name, method->name) == 0)
                    515:                                return method;
                    516:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                    517:        return NULL;
1.1       markus    518: }
                    519:
                    520: /* return 1 if user allows given key */
                    521: int
1.21      markus    522: user_key_allowed(struct passwd *pw, Key *key)
1.1       markus    523: {
1.31      markus    524:        char line[8192], file[MAXPATHLEN];
1.1       markus    525:        int found_key = 0;
                    526:        FILE *f;
1.23      markus    527:        u_long linenum = 0;
1.1       markus    528:        struct stat st;
                    529:        Key *found;
                    530:
1.18      markus    531:        if (pw == NULL)
                    532:                return 0;
                    533:
1.1       markus    534:        /* Temporarily use the user's uid. */
1.51    ! markus    535:        temporarily_use_uid(pw);
1.1       markus    536:
                    537:        /* The authorized keys. */
                    538:        snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
1.29      markus    539:            _PATH_SSH_USER_PERMITTED_KEYS2);
1.1       markus    540:
                    541:        /* Fail quietly if file does not exist */
                    542:        if (stat(file, &st) < 0) {
                    543:                /* Restore the privileged uid. */
                    544:                restore_uid();
                    545:                return 0;
                    546:        }
                    547:        /* Open the file containing the authorized keys. */
                    548:        f = fopen(file, "r");
                    549:        if (!f) {
                    550:                /* Restore the privileged uid. */
                    551:                restore_uid();
                    552:                return 0;
                    553:        }
                    554:        if (options.strict_modes) {
                    555:                int fail = 0;
                    556:                char buf[1024];
                    557:                /* Check open file in order to avoid open/stat races */
                    558:                if (fstat(fileno(f), &st) < 0 ||
                    559:                    (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    560:                    (st.st_mode & 022) != 0) {
1.16      markus    561:                        snprintf(buf, sizeof buf,
                    562:                            "%s authentication refused for %.100s: "
                    563:                            "bad ownership or modes for '%s'.",
                    564:                            key_type(key), pw->pw_name, file);
1.1       markus    565:                        fail = 1;
                    566:                } else {
1.29      markus    567:                        /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
1.1       markus    568:                        int i;
                    569:                        static const char *check[] = {
1.29      markus    570:                                "", _PATH_SSH_USER_DIR, NULL
1.1       markus    571:                        };
                    572:                        for (i = 0; check[i]; i++) {
                    573:                                snprintf(line, sizeof line, "%.500s/%.100s",
                    574:                                    pw->pw_dir, check[i]);
                    575:                                if (stat(line, &st) < 0 ||
                    576:                                    (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    577:                                    (st.st_mode & 022) != 0) {
                    578:                                        snprintf(buf, sizeof buf,
1.16      markus    579:                                            "%s authentication refused for %.100s: "
1.1       markus    580:                                            "bad ownership or modes for '%s'.",
1.16      markus    581:                                            key_type(key), pw->pw_name, line);
1.1       markus    582:                                        fail = 1;
                    583:                                        break;
                    584:                                }
                    585:                        }
                    586:                }
                    587:                if (fail) {
                    588:                        fclose(f);
1.44      deraadt   589:                        log("%s", buf);
1.1       markus    590:                        restore_uid();
                    591:                        return 0;
                    592:                }
                    593:        }
                    594:        found_key = 0;
1.16      markus    595:        found = key_new(key->type);
1.1       markus    596:
                    597:        while (fgets(line, sizeof(line), f)) {
1.10      markus    598:                char *cp, *options = NULL;
1.1       markus    599:                linenum++;
                    600:                /* Skip leading whitespace, empty and comment lines. */
                    601:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    602:                        ;
                    603:                if (!*cp || *cp == '\n' || *cp == '#')
                    604:                        continue;
1.10      markus    605:
1.21      markus    606:                if (key_read(found, &cp) == -1) {
1.10      markus    607:                        /* no key?  check if there are options for this key */
                    608:                        int quoted = 0;
1.21      markus    609:                        debug2("user_key_allowed: check options: '%s'", cp);
1.10      markus    610:                        options = cp;
                    611:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    612:                                if (*cp == '\\' && cp[1] == '"')
                    613:                                        cp++;   /* Skip both */
                    614:                                else if (*cp == '"')
                    615:                                        quoted = !quoted;
                    616:                        }
                    617:                        /* Skip remaining whitespace. */
                    618:                        for (; *cp == ' ' || *cp == '\t'; cp++)
                    619:                                ;
1.21      markus    620:                        if (key_read(found, &cp) == -1) {
                    621:                                debug2("user_key_allowed: advance: '%s'", cp);
1.10      markus    622:                                /* still no key?  advance to next line*/
                    623:                                continue;
                    624:                        }
                    625:                }
                    626:                if (key_equal(found, key) &&
1.30      markus    627:                    auth_parse_options(pw, options, file, linenum) == 1) {
1.1       markus    628:                        found_key = 1;
                    629:                        debug("matching key found: file %s, line %ld",
                    630:                            file, linenum);
                    631:                        break;
                    632:                }
                    633:        }
                    634:        restore_uid();
                    635:        fclose(f);
                    636:        key_free(found);
1.46      markus    637:        if (!found_key)
                    638:                debug2("key not found");
1.1       markus    639:        return found_key;
                    640: }