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

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