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

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.47    ! markus     26: RCSID("$OpenBSD: auth2.c,v 1.46 2001/03/11 13:25: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"
                     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.18      markus     80: int    userauth_none(Authctxt *authctxt);
                     81: int    userauth_passwd(Authctxt *authctxt);
                     82: int    userauth_pubkey(Authctxt *authctxt);
                     83: int    userauth_kbdint(Authctxt *authctxt);
                     84:
                     85: Authmethod authmethods[] = {
                     86:        {"none",
                     87:                userauth_none,
                     88:                &one},
                     89:        {"publickey",
                     90:                userauth_pubkey,
1.21      markus     91:                &options.pubkey_authentication},
1.40      markus     92:        {"password",
                     93:                userauth_passwd,
                     94:                &options.password_authentication},
1.18      markus     95:        {"keyboard-interactive",
                     96:                userauth_kbdint,
                     97:                &options.kbd_interactive_authentication},
                     98:        {NULL, NULL, NULL}
1.1       markus     99: };
                    100:
                    101: /*
1.18      markus    102:  * loop until authctxt->success == TRUE
1.1       markus    103:  */
                    104:
                    105: void
                    106: do_authentication2()
                    107: {
1.28      markus    108:        Authctxt *authctxt = authctxt_new();
                    109:
1.18      markus    110:        x_authctxt = authctxt;          /*XXX*/
                    111:
1.34      markus    112:        /* challenge-reponse is implemented via keyboard interactive */
                    113:        if (options.challenge_reponse_authentication)
                    114:                options.kbd_interactive_authentication = 1;
                    115:
1.1       markus    116:        dispatch_init(&protocol_error);
                    117:        dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18      markus    118:        dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.28      markus    119:        do_authenticated2(authctxt);
1.1       markus    120: }
                    121:
                    122: void
1.15      markus    123: protocol_error(int type, int plen, void *ctxt)
1.1       markus    124: {
                    125:        log("auth: protocol error: type %d plen %d", type, plen);
                    126:        packet_start(SSH2_MSG_UNIMPLEMENTED);
                    127:        packet_put_int(0);
                    128:        packet_send();
                    129:        packet_write_wait();
                    130: }
                    131:
                    132: void
1.15      markus    133: input_service_request(int type, int plen, void *ctxt)
1.1       markus    134: {
1.18      markus    135:        Authctxt *authctxt = ctxt;
1.23      markus    136:        u_int len;
1.1       markus    137:        int accept = 0;
                    138:        char *service = packet_get_string(&len);
                    139:        packet_done();
                    140:
1.18      markus    141:        if (authctxt == NULL)
                    142:                fatal("input_service_request: no authctxt");
                    143:
1.1       markus    144:        if (strcmp(service, "ssh-userauth") == 0) {
1.18      markus    145:                if (!authctxt->success) {
1.1       markus    146:                        accept = 1;
                    147:                        /* now we can handle user-auth requests */
                    148:                        dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
                    149:                }
                    150:        }
                    151:        /* XXX all other service requests are denied */
                    152:
                    153:        if (accept) {
                    154:                packet_start(SSH2_MSG_SERVICE_ACCEPT);
                    155:                packet_put_cstring(service);
                    156:                packet_send();
                    157:                packet_write_wait();
                    158:        } else {
                    159:                debug("bad service request %s", service);
                    160:                packet_disconnect("bad service request %s", service);
                    161:        }
                    162:        xfree(service);
                    163: }
                    164:
                    165: void
1.15      markus    166: input_userauth_request(int type, int plen, void *ctxt)
1.1       markus    167: {
1.18      markus    168:        Authctxt *authctxt = ctxt;
                    169:        Authmethod *m = NULL;
1.28      markus    170:        char *user, *service, *method, *style = NULL;
1.1       markus    171:        int authenticated = 0;
                    172:
1.18      markus    173:        if (authctxt == NULL)
                    174:                fatal("input_userauth_request: no authctxt");
1.1       markus    175:
1.18      markus    176:        user = packet_get_string(NULL);
                    177:        service = packet_get_string(NULL);
                    178:        method = packet_get_string(NULL);
1.1       markus    179:        debug("userauth-request for user %s service %s method %s", user, service, method);
1.24      markus    180:        debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1       markus    181:
1.28      markus    182:        if ((style = strchr(user, ':')) != NULL)
                    183:                *style++ = 0;
                    184:
1.36      stevesk   185:        if (authctxt->attempt++ == 0) {
1.18      markus    186:                /* setup auth context */
                    187:                struct passwd *pw = NULL;
                    188:                pw = getpwnam(user);
                    189:                if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
                    190:                        authctxt->pw = pwcopy(pw);
                    191:                        authctxt->valid = 1;
                    192:                        debug2("input_userauth_request: setting up authctxt for %s", user);
                    193:                } else {
                    194:                        log("input_userauth_request: illegal user %s", user);
                    195:                }
1.42      markus    196:                setproctitle("%s", pw ? user : "unknown");
1.18      markus    197:                authctxt->user = xstrdup(user);
                    198:                authctxt->service = xstrdup(service);
1.28      markus    199:                authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
1.18      markus    200:        } else if (authctxt->valid) {
                    201:                if (strcmp(user, authctxt->user) != 0 ||
                    202:                    strcmp(service, authctxt->service) != 0) {
                    203:                        log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)",
                    204:                            user, service, authctxt->user, authctxt->service);
                    205:                        authctxt->valid = 0;
1.1       markus    206:                }
                    207:        }
1.28      markus    208:        /* reset state */
                    209:        dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
                    210:        authctxt->postponed = 0;
1.47    ! markus    211: #ifdef BSD_AUTH
        !           212:        if (authctxt->as) {
        !           213:                auth_close(authctxt->as);
        !           214:                authctxt->as = NULL;
        !           215:        }
        !           216: #endif
1.18      markus    217:
1.28      markus    218:        /* try to authenticate user */
1.18      markus    219:        m = authmethod_lookup(method);
                    220:        if (m != NULL) {
                    221:                debug2("input_userauth_request: try method %s", method);
                    222:                authenticated = m->userauth(authctxt);
                    223:        }
1.28      markus    224:        if (!authctxt->valid && authenticated)
                    225:                fatal("INTERNAL ERROR: authenticated invalid user %s",
                    226:                    authctxt->user);
1.18      markus    227:
                    228:        /* Special handling for root */
1.41      markus    229:        if (authenticated && authctxt->pw->pw_uid == 0 &&
                    230:            !auth_root_allowed(method))
1.3       markus    231:                authenticated = 0;
                    232:
1.18      markus    233:        /* Log before sending the reply */
1.28      markus    234:        auth_log(authctxt, authenticated, method, " ssh2");
                    235:
                    236:        if (!authctxt->postponed)
                    237:                userauth_reply(authctxt, authenticated);
1.18      markus    238:
                    239:        xfree(service);
                    240:        xfree(user);
                    241:        xfree(method);
                    242: }
                    243:
1.25      markus    244: void
                    245: userauth_banner(void)
                    246: {
                    247:        struct stat st;
                    248:        char *banner = NULL;
                    249:        off_t len, n;
                    250:        int fd;
                    251:
                    252:        if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
                    253:                return;
                    254:        if ((fd = open(options.banner, O_RDONLY)) < 0) {
                    255:                error("userauth_banner: open %s failed: %s",
                    256:                    options.banner, strerror(errno));
                    257:                return;
                    258:        }
                    259:        if (fstat(fd, &st) < 0)
                    260:                goto done;
                    261:        len = st.st_size;
                    262:        banner = xmalloc(len + 1);
                    263:        if ((n = read(fd, banner, len)) < 0)
                    264:                goto done;
                    265:        banner[n] = '\0';
                    266:        packet_start(SSH2_MSG_USERAUTH_BANNER);
                    267:        packet_put_cstring(banner);
                    268:        packet_put_cstring("");         /* language, unused */
                    269:        packet_send();
                    270:        debug("userauth_banner: sent");
                    271: done:
                    272:        if (banner)
                    273:                xfree(banner);
                    274:        close(fd);
                    275:        return;
                    276: }
1.18      markus    277:
1.36      stevesk   278: void
1.18      markus    279: userauth_reply(Authctxt *authctxt, int authenticated)
                    280: {
1.24      markus    281:        char *methods;
1.28      markus    282:
1.3       markus    283:        /* XXX todo: check if multiple auth methods are needed */
1.39      markus    284:        if (authenticated == 1) {
1.1       markus    285:                /* turn off userauth */
                    286:                dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
                    287:                packet_start(SSH2_MSG_USERAUTH_SUCCESS);
                    288:                packet_send();
                    289:                packet_write_wait();
                    290:                /* now we can break out */
1.18      markus    291:                authctxt->success = 1;
1.28      markus    292:        } else {
                    293:                if (authctxt->failures++ > AUTH_FAIL_MAX)
1.36      stevesk   294:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.24      markus    295:                methods = authmethods_get();
1.1       markus    296:                packet_start(SSH2_MSG_USERAUTH_FAILURE);
1.18      markus    297:                packet_put_cstring(methods);
                    298:                packet_put_char(0);     /* XXX partial success, unused */
1.1       markus    299:                packet_send();
                    300:                packet_write_wait();
1.18      markus    301:                xfree(methods);
1.1       markus    302:        }
                    303: }
                    304:
                    305: int
1.18      markus    306: userauth_none(Authctxt *authctxt)
1.1       markus    307: {
1.18      markus    308:        /* disable method "none", only allowed one time */
                    309:        Authmethod *m = authmethod_lookup("none");
                    310:        if (m != NULL)
                    311:                m->enabled = NULL;
1.1       markus    312:        packet_done();
1.25      markus    313:        userauth_banner();
1.47    ! markus    314:        return authctxt->valid ? auth_password(authctxt, "") : 0;
1.1       markus    315: }
1.18      markus    316:
1.1       markus    317: int
1.18      markus    318: userauth_passwd(Authctxt *authctxt)
1.1       markus    319: {
                    320:        char *password;
                    321:        int authenticated = 0;
                    322:        int change;
1.23      markus    323:        u_int len;
1.1       markus    324:        change = packet_get_char();
                    325:        if (change)
                    326:                log("password change not supported");
                    327:        password = packet_get_string(&len);
                    328:        packet_done();
1.18      markus    329:        if (authctxt->valid &&
1.47    ! markus    330:            auth_password(authctxt, password) == 1)
1.1       markus    331:                authenticated = 1;
                    332:        memset(password, 0, len);
                    333:        xfree(password);
                    334:        return authenticated;
                    335: }
1.18      markus    336:
                    337: int
                    338: userauth_kbdint(Authctxt *authctxt)
                    339: {
                    340:        int authenticated = 0;
                    341:        char *lang = NULL;
                    342:        char *devs = NULL;
                    343:
                    344:        lang = packet_get_string(NULL);
                    345:        devs = packet_get_string(NULL);
                    346:        packet_done();
                    347:
                    348:        debug("keyboard-interactive language %s devs %s", lang, devs);
1.28      markus    349:
1.34      markus    350:        if (options.challenge_reponse_authentication)
                    351:                authenticated = auth2_challenge(authctxt, devs);
1.28      markus    352:
1.18      markus    353:        xfree(lang);
                    354:        xfree(devs);
                    355:        return authenticated;
                    356: }
                    357:
1.1       markus    358: int
1.18      markus    359: userauth_pubkey(Authctxt *authctxt)
1.1       markus    360: {
                    361:        Buffer b;
                    362:        Key *key;
                    363:        char *pkalg, *pkblob, *sig;
1.23      markus    364:        u_int alen, blen, slen;
1.21      markus    365:        int have_sig, pktype;
1.1       markus    366:        int authenticated = 0;
                    367:
1.18      markus    368:        if (!authctxt->valid) {
                    369:                debug2("userauth_pubkey: disabled because of invalid user");
1.8       markus    370:                return 0;
                    371:        }
1.1       markus    372:        have_sig = packet_get_char();
1.22      markus    373:        if (datafellows & SSH_BUG_PKAUTH) {
                    374:                debug2("userauth_pubkey: SSH_BUG_PKAUTH");
                    375:                /* no explicit pkalg given */
                    376:                pkblob = packet_get_string(&blen);
                    377:                buffer_init(&b);
                    378:                buffer_append(&b, pkblob, blen);
                    379:                /* so we have to extract the pkalg from the pkblob */
                    380:                pkalg = buffer_get_string(&b, &alen);
                    381:                buffer_free(&b);
                    382:        } else {
                    383:                pkalg = packet_get_string(&alen);
                    384:                pkblob = packet_get_string(&blen);
                    385:        }
1.21      markus    386:        pktype = key_type_from_name(pkalg);
                    387:        if (pktype == KEY_UNSPEC) {
1.22      markus    388:                /* this is perfectly legal */
                    389:                log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
1.1       markus    390:                xfree(pkalg);
1.22      markus    391:                xfree(pkblob);
1.1       markus    392:                return 0;
                    393:        }
1.21      markus    394:        key = key_from_blob(pkblob, blen);
1.2       markus    395:        if (key != NULL) {
                    396:                if (have_sig) {
                    397:                        sig = packet_get_string(&slen);
                    398:                        packet_done();
                    399:                        buffer_init(&b);
1.20      markus    400:                        if (datafellows & SSH_OLD_SESSIONID) {
                    401:                                buffer_append(&b, session_id2, session_id2_len);
                    402:                        } else {
1.11      markus    403:                                buffer_put_string(&b, session_id2, session_id2_len);
                    404:                        }
1.9       markus    405:                        /* reconstruct packet */
1.2       markus    406:                        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.18      markus    407:                        buffer_put_cstring(&b, authctxt->user);
1.9       markus    408:                        buffer_put_cstring(&b,
1.22      markus    409:                            datafellows & SSH_BUG_PKSERVICE ?
1.9       markus    410:                            "ssh-userauth" :
1.18      markus    411:                            authctxt->service);
1.22      markus    412:                        if (datafellows & SSH_BUG_PKAUTH) {
                    413:                                buffer_put_char(&b, have_sig);
                    414:                        } else {
                    415:                                buffer_put_cstring(&b, "publickey");
                    416:                                buffer_put_char(&b, have_sig);
                    417:                                buffer_put_cstring(&b, key_ssh_name(key));
                    418:                        }
1.9       markus    419:                        buffer_put_string(&b, pkblob, blen);
1.21      markus    420: #ifdef DEBUG_PK
1.2       markus    421:                        buffer_dump(&b);
1.1       markus    422: #endif
1.2       markus    423:                        /* test for correct signature */
1.21      markus    424:                        if (user_key_allowed(authctxt->pw, key) &&
                    425:                            key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
1.2       markus    426:                                authenticated = 1;
                    427:                        buffer_clear(&b);
                    428:                        xfree(sig);
                    429:                } else {
1.18      markus    430:                        debug("test whether pkalg/pkblob are acceptable");
1.2       markus    431:                        packet_done();
1.18      markus    432:
1.2       markus    433:                        /* XXX fake reply and always send PK_OK ? */
1.6       markus    434:                        /*
                    435:                         * XXX this allows testing whether a user is allowed
                    436:                         * to login: if you happen to have a valid pubkey this
                    437:                         * message is sent. the message is NEVER sent at all
                    438:                         * if a user is not allowed to login. is this an
                    439:                         * issue? -markus
                    440:                         */
1.21      markus    441:                        if (user_key_allowed(authctxt->pw, key)) {
1.2       markus    442:                                packet_start(SSH2_MSG_USERAUTH_PK_OK);
                    443:                                packet_put_string(pkalg, alen);
                    444:                                packet_put_string(pkblob, blen);
                    445:                                packet_send();
                    446:                                packet_write_wait();
1.38      markus    447:                                authctxt->postponed = 1;
1.2       markus    448:                        }
1.1       markus    449:                }
1.17      markus    450:                if (authenticated != 1)
                    451:                        auth_clear_options();
1.2       markus    452:                key_free(key);
1.1       markus    453:        }
1.21      markus    454:        debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
1.1       markus    455:        xfree(pkalg);
                    456:        xfree(pkblob);
                    457:        return authenticated;
                    458: }
                    459:
1.18      markus    460: /* get current user */
1.1       markus    461:
                    462: struct passwd*
                    463: auth_get_user(void)
                    464: {
1.18      markus    465:        return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
1.1       markus    466: }
                    467:
1.18      markus    468: #define        DELIM   ","
                    469:
                    470: char *
                    471: authmethods_get(void)
1.1       markus    472: {
1.18      markus    473:        Authmethod *method = NULL;
1.23      markus    474:        u_int size = 0;
1.18      markus    475:        char *list;
1.1       markus    476:
1.18      markus    477:        for (method = authmethods; method->name != NULL; method++) {
                    478:                if (strcmp(method->name, "none") == 0)
                    479:                        continue;
                    480:                if (method->enabled != NULL && *(method->enabled) != 0) {
                    481:                        if (size != 0)
                    482:                                size += strlen(DELIM);
                    483:                        size += strlen(method->name);
1.1       markus    484:                }
1.18      markus    485:        }
                    486:        size++;                 /* trailing '\0' */
                    487:        list = xmalloc(size);
                    488:        list[0] = '\0';
                    489:
                    490:        for (method = authmethods; method->name != NULL; method++) {
                    491:                if (strcmp(method->name, "none") == 0)
                    492:                        continue;
                    493:                if (method->enabled != NULL && *(method->enabled) != 0) {
                    494:                        if (list[0] != '\0')
                    495:                                strlcat(list, DELIM, size);
                    496:                        strlcat(list, method->name, size);
1.1       markus    497:                }
                    498:        }
1.18      markus    499:        return list;
                    500: }
                    501:
                    502: Authmethod *
                    503: authmethod_lookup(const char *name)
                    504: {
                    505:        Authmethod *method = NULL;
                    506:        if (name != NULL)
                    507:                for (method = authmethods; method->name != NULL; method++)
                    508:                        if (method->enabled != NULL &&
                    509:                            *(method->enabled) != 0 &&
                    510:                            strcmp(name, method->name) == 0)
                    511:                                return method;
                    512:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                    513:        return NULL;
1.1       markus    514: }
                    515:
                    516: /* return 1 if user allows given key */
                    517: int
1.21      markus    518: user_key_allowed(struct passwd *pw, Key *key)
1.1       markus    519: {
1.31      markus    520:        char line[8192], file[MAXPATHLEN];
1.1       markus    521:        int found_key = 0;
                    522:        FILE *f;
1.23      markus    523:        u_long linenum = 0;
1.1       markus    524:        struct stat st;
                    525:        Key *found;
                    526:
1.18      markus    527:        if (pw == NULL)
                    528:                return 0;
                    529:
1.1       markus    530:        /* Temporarily use the user's uid. */
                    531:        temporarily_use_uid(pw->pw_uid);
                    532:
                    533:        /* The authorized keys. */
                    534:        snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
1.29      markus    535:            _PATH_SSH_USER_PERMITTED_KEYS2);
1.1       markus    536:
                    537:        /* Fail quietly if file does not exist */
                    538:        if (stat(file, &st) < 0) {
                    539:                /* Restore the privileged uid. */
                    540:                restore_uid();
                    541:                return 0;
                    542:        }
                    543:        /* Open the file containing the authorized keys. */
                    544:        f = fopen(file, "r");
                    545:        if (!f) {
                    546:                /* Restore the privileged uid. */
                    547:                restore_uid();
                    548:                return 0;
                    549:        }
                    550:        if (options.strict_modes) {
                    551:                int fail = 0;
                    552:                char buf[1024];
                    553:                /* Check open file in order to avoid open/stat races */
                    554:                if (fstat(fileno(f), &st) < 0 ||
                    555:                    (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    556:                    (st.st_mode & 022) != 0) {
1.16      markus    557:                        snprintf(buf, sizeof buf,
                    558:                            "%s authentication refused for %.100s: "
                    559:                            "bad ownership or modes for '%s'.",
                    560:                            key_type(key), pw->pw_name, file);
1.1       markus    561:                        fail = 1;
                    562:                } else {
1.29      markus    563:                        /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
1.1       markus    564:                        int i;
                    565:                        static const char *check[] = {
1.29      markus    566:                                "", _PATH_SSH_USER_DIR, NULL
1.1       markus    567:                        };
                    568:                        for (i = 0; check[i]; i++) {
                    569:                                snprintf(line, sizeof line, "%.500s/%.100s",
                    570:                                    pw->pw_dir, check[i]);
                    571:                                if (stat(line, &st) < 0 ||
                    572:                                    (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    573:                                    (st.st_mode & 022) != 0) {
                    574:                                        snprintf(buf, sizeof buf,
1.16      markus    575:                                            "%s authentication refused for %.100s: "
1.1       markus    576:                                            "bad ownership or modes for '%s'.",
1.16      markus    577:                                            key_type(key), pw->pw_name, line);
1.1       markus    578:                                        fail = 1;
                    579:                                        break;
                    580:                                }
                    581:                        }
                    582:                }
                    583:                if (fail) {
                    584:                        fclose(f);
1.44      deraadt   585:                        log("%s", buf);
1.1       markus    586:                        restore_uid();
                    587:                        return 0;
                    588:                }
                    589:        }
                    590:        found_key = 0;
1.16      markus    591:        found = key_new(key->type);
1.1       markus    592:
                    593:        while (fgets(line, sizeof(line), f)) {
1.10      markus    594:                char *cp, *options = NULL;
1.1       markus    595:                linenum++;
                    596:                /* Skip leading whitespace, empty and comment lines. */
                    597:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    598:                        ;
                    599:                if (!*cp || *cp == '\n' || *cp == '#')
                    600:                        continue;
1.10      markus    601:
1.21      markus    602:                if (key_read(found, &cp) == -1) {
1.10      markus    603:                        /* no key?  check if there are options for this key */
                    604:                        int quoted = 0;
1.21      markus    605:                        debug2("user_key_allowed: check options: '%s'", cp);
1.10      markus    606:                        options = cp;
                    607:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    608:                                if (*cp == '\\' && cp[1] == '"')
                    609:                                        cp++;   /* Skip both */
                    610:                                else if (*cp == '"')
                    611:                                        quoted = !quoted;
                    612:                        }
                    613:                        /* Skip remaining whitespace. */
                    614:                        for (; *cp == ' ' || *cp == '\t'; cp++)
                    615:                                ;
1.21      markus    616:                        if (key_read(found, &cp) == -1) {
                    617:                                debug2("user_key_allowed: advance: '%s'", cp);
1.10      markus    618:                                /* still no key?  advance to next line*/
                    619:                                continue;
                    620:                        }
                    621:                }
                    622:                if (key_equal(found, key) &&
1.30      markus    623:                    auth_parse_options(pw, options, file, linenum) == 1) {
1.1       markus    624:                        found_key = 1;
                    625:                        debug("matching key found: file %s, line %ld",
                    626:                            file, linenum);
                    627:                        break;
                    628:                }
                    629:        }
                    630:        restore_uid();
                    631:        fclose(f);
                    632:        key_free(found);
1.46      markus    633:        if (!found_key)
                    634:                debug2("key not found");
1.1       markus    635:        return found_key;
                    636: }