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

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