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

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