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

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