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

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