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

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