[BACK]Return to auth2-pubkey.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/auth2-pubkey.c, Revision 1.36

1.36    ! djm         1: /* $OpenBSD: auth2-pubkey.c,v 1.35 2013/03/07 00:19:59 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
                     25:
1.10      stevesk    26:
                     27: #include <sys/types.h>
                     28: #include <sys/stat.h>
1.31      djm        29: #include <sys/wait.h>
1.13      stevesk    30:
1.31      djm        31: #include <errno.h>
1.16      djm        32: #include <fcntl.h>
1.31      djm        33: #include <paths.h>
1.13      stevesk    34: #include <pwd.h>
1.31      djm        35: #include <signal.h>
1.14      stevesk    36: #include <stdio.h>
1.15      deraadt    37: #include <stdarg.h>
1.20      djm        38: #include <string.h>
                     39: #include <time.h>
1.17      dtucker    40: #include <unistd.h>
1.1       markus     41:
1.15      deraadt    42: #include "xmalloc.h"
1.8       dtucker    43: #include "ssh.h"
1.1       markus     44: #include "ssh2.h"
                     45: #include "packet.h"
                     46: #include "buffer.h"
                     47: #include "log.h"
                     48: #include "servconf.h"
                     49: #include "compat.h"
1.15      deraadt    50: #include "key.h"
                     51: #include "hostfile.h"
1.1       markus     52: #include "auth.h"
                     53: #include "pathnames.h"
                     54: #include "uidswap.h"
                     55: #include "auth-options.h"
                     56: #include "canohost.h"
1.15      deraadt    57: #ifdef GSSAPI
                     58: #include "ssh-gss.h"
                     59: #endif
1.1       markus     60: #include "monitor_wrap.h"
1.9       dtucker    61: #include "misc.h"
1.21      djm        62: #include "authfile.h"
1.24      djm        63: #include "match.h"
1.1       markus     64:
                     65: /* import */
                     66: extern ServerOptions options;
                     67: extern u_char *session_id2;
1.4       markus     68: extern u_int session_id2_len;
1.1       markus     69:
1.2       markus     70: static int
1.1       markus     71: userauth_pubkey(Authctxt *authctxt)
                     72: {
                     73:        Buffer b;
                     74:        Key *key = NULL;
1.35      djm        75:        char *pkalg, *userstyle;
1.1       markus     76:        u_char *pkblob, *sig;
                     77:        u_int alen, blen, slen;
                     78:        int have_sig, pktype;
                     79:        int authenticated = 0;
                     80:
                     81:        if (!authctxt->valid) {
                     82:                debug2("userauth_pubkey: disabled because of invalid user");
                     83:                return 0;
                     84:        }
                     85:        have_sig = packet_get_char();
                     86:        if (datafellows & SSH_BUG_PKAUTH) {
                     87:                debug2("userauth_pubkey: SSH_BUG_PKAUTH");
                     88:                /* no explicit pkalg given */
                     89:                pkblob = packet_get_string(&blen);
                     90:                buffer_init(&b);
                     91:                buffer_append(&b, pkblob, blen);
                     92:                /* so we have to extract the pkalg from the pkblob */
                     93:                pkalg = buffer_get_string(&b, &alen);
                     94:                buffer_free(&b);
                     95:        } else {
                     96:                pkalg = packet_get_string(&alen);
                     97:                pkblob = packet_get_string(&blen);
                     98:        }
                     99:        pktype = key_type_from_name(pkalg);
                    100:        if (pktype == KEY_UNSPEC) {
                    101:                /* this is perfectly legal */
1.3       itojun    102:                logit("userauth_pubkey: unsupported public key algorithm: %s",
1.1       markus    103:                    pkalg);
                    104:                goto done;
                    105:        }
                    106:        key = key_from_blob(pkblob, blen);
                    107:        if (key == NULL) {
                    108:                error("userauth_pubkey: cannot decode key: %s", pkalg);
                    109:                goto done;
                    110:        }
                    111:        if (key->type != pktype) {
                    112:                error("userauth_pubkey: type mismatch for decoded key "
                    113:                    "(received %d, expected %d)", key->type, pktype);
                    114:                goto done;
                    115:        }
                    116:        if (have_sig) {
                    117:                sig = packet_get_string(&slen);
                    118:                packet_check_eom();
                    119:                buffer_init(&b);
                    120:                if (datafellows & SSH_OLD_SESSIONID) {
                    121:                        buffer_append(&b, session_id2, session_id2_len);
                    122:                } else {
                    123:                        buffer_put_string(&b, session_id2, session_id2_len);
                    124:                }
                    125:                /* reconstruct packet */
                    126:                buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.35      djm       127:                xasprintf(&userstyle, "%s%s%s", authctxt->user,
                    128:                    authctxt->style ? ":" : "",
                    129:                    authctxt->style ? authctxt->style : "");
                    130:                buffer_put_cstring(&b, userstyle);
                    131:                free(userstyle);
1.1       markus    132:                buffer_put_cstring(&b,
                    133:                    datafellows & SSH_BUG_PKSERVICE ?
                    134:                    "ssh-userauth" :
                    135:                    authctxt->service);
                    136:                if (datafellows & SSH_BUG_PKAUTH) {
                    137:                        buffer_put_char(&b, have_sig);
                    138:                } else {
                    139:                        buffer_put_cstring(&b, "publickey");
                    140:                        buffer_put_char(&b, have_sig);
                    141:                        buffer_put_cstring(&b, pkalg);
                    142:                }
                    143:                buffer_put_string(&b, pkblob, blen);
                    144: #ifdef DEBUG_PK
                    145:                buffer_dump(&b);
                    146: #endif
                    147:                /* test for correct signature */
                    148:                authenticated = 0;
                    149:                if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
                    150:                    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
1.6       markus    151:                    buffer_len(&b))) == 1)
1.1       markus    152:                        authenticated = 1;
1.6       markus    153:                buffer_free(&b);
1.36    ! djm       154:                free(sig);
1.1       markus    155:        } else {
                    156:                debug("test whether pkalg/pkblob are acceptable");
                    157:                packet_check_eom();
                    158:
                    159:                /* XXX fake reply and always send PK_OK ? */
                    160:                /*
                    161:                 * XXX this allows testing whether a user is allowed
                    162:                 * to login: if you happen to have a valid pubkey this
                    163:                 * message is sent. the message is NEVER sent at all
                    164:                 * if a user is not allowed to login. is this an
                    165:                 * issue? -markus
                    166:                 */
                    167:                if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
                    168:                        packet_start(SSH2_MSG_USERAUTH_PK_OK);
                    169:                        packet_put_string(pkalg, alen);
                    170:                        packet_put_string(pkblob, blen);
                    171:                        packet_send();
                    172:                        packet_write_wait();
                    173:                        authctxt->postponed = 1;
                    174:                }
                    175:        }
                    176:        if (authenticated != 1)
                    177:                auth_clear_options();
                    178: done:
                    179:        debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
                    180:        if (key != NULL)
                    181:                key_free(key);
1.36    ! djm       182:        free(pkalg);
        !           183:        free(pkblob);
1.1       markus    184:        return authenticated;
                    185: }
                    186:
1.24      djm       187: static int
                    188: match_principals_option(const char *principal_list, struct KeyCert *cert)
                    189: {
                    190:        char *result;
                    191:        u_int i;
                    192:
                    193:        /* XXX percent_expand() sequences for authorized_principals? */
                    194:
                    195:        for (i = 0; i < cert->nprincipals; i++) {
                    196:                if ((result = match_list(cert->principals[i],
                    197:                    principal_list, NULL)) != NULL) {
                    198:                        debug3("matched principal from key options \"%.100s\"",
                    199:                            result);
1.36    ! djm       200:                        free(result);
1.24      djm       201:                        return 1;
                    202:                }
                    203:        }
                    204:        return 0;
                    205: }
                    206:
                    207: static int
1.26      djm       208: match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert)
1.24      djm       209: {
                    210:        FILE *f;
1.26      djm       211:        char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
1.24      djm       212:        u_long linenum = 0;
                    213:        u_int i;
                    214:
                    215:        temporarily_use_uid(pw);
                    216:        debug("trying authorized principals file %s", file);
                    217:        if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
                    218:                restore_uid();
                    219:                return 0;
                    220:        }
                    221:        while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.26      djm       222:                /* Skip leading whitespace. */
1.24      djm       223:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    224:                        ;
1.26      djm       225:                /* Skip blank and comment lines. */
                    226:                if ((ep = strchr(cp, '#')) != NULL)
                    227:                        *ep = '\0';
                    228:                if (!*cp || *cp == '\n')
1.24      djm       229:                        continue;
1.26      djm       230:                /* Trim trailing whitespace. */
                    231:                ep = cp + strlen(cp) - 1;
                    232:                while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
                    233:                        *ep-- = '\0';
                    234:                /*
                    235:                 * If the line has internal whitespace then assume it has
                    236:                 * key options.
                    237:                 */
                    238:                line_opts = NULL;
                    239:                if ((ep = strrchr(cp, ' ')) != NULL ||
                    240:                    (ep = strrchr(cp, '\t')) != NULL) {
                    241:                        for (; *ep == ' ' || *ep == '\t'; ep++)
1.27      deraadt   242:                                ;
1.26      djm       243:                        line_opts = cp;
                    244:                        cp = ep;
                    245:                }
1.24      djm       246:                for (i = 0; i < cert->nprincipals; i++) {
                    247:                        if (strcmp(cp, cert->principals[i]) == 0) {
1.30      djm       248:                                debug3("matched principal \"%.100s\" "
                    249:                                    "from file \"%s\" on line %lu",
1.31      djm       250:                                    cert->principals[i], file, linenum);
1.26      djm       251:                                if (auth_parse_options(pw, line_opts,
                    252:                                    file, linenum) != 1)
                    253:                                        continue;
1.24      djm       254:                                fclose(f);
                    255:                                restore_uid();
                    256:                                return 1;
                    257:                        }
                    258:                }
                    259:        }
                    260:        fclose(f);
                    261:        restore_uid();
                    262:        return 0;
1.31      djm       263: }
1.24      djm       264:
1.31      djm       265: /*
                    266:  * Checks whether key is allowed in authorized_keys-format file,
                    267:  * returns 1 if the key is allowed or 0 otherwise.
                    268:  */
1.1       markus    269: static int
1.31      djm       270: check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
1.1       markus    271: {
1.8       dtucker   272:        char line[SSH_MAX_PUBKEY_BYTES];
1.20      djm       273:        const char *reason;
1.18      dtucker   274:        int found_key = 0;
1.1       markus    275:        u_long linenum = 0;
                    276:        Key *found;
                    277:        char *fp;
                    278:
                    279:        found_key = 0;
1.20      djm       280:        found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
1.1       markus    281:
1.8       dtucker   282:        while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.7       avsm      283:                char *cp, *key_options = NULL;
1.8       dtucker   284:
1.20      djm       285:                auth_clear_options();
                    286:
1.1       markus    287:                /* Skip leading whitespace, empty and comment lines. */
                    288:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    289:                        ;
                    290:                if (!*cp || *cp == '\n' || *cp == '#')
                    291:                        continue;
                    292:
                    293:                if (key_read(found, &cp) != 1) {
                    294:                        /* no key?  check if there are options for this key */
                    295:                        int quoted = 0;
                    296:                        debug2("user_key_allowed: check options: '%s'", cp);
1.7       avsm      297:                        key_options = cp;
1.1       markus    298:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    299:                                if (*cp == '\\' && cp[1] == '"')
                    300:                                        cp++;   /* Skip both */
                    301:                                else if (*cp == '"')
                    302:                                        quoted = !quoted;
                    303:                        }
                    304:                        /* Skip remaining whitespace. */
                    305:                        for (; *cp == ' ' || *cp == '\t'; cp++)
                    306:                                ;
                    307:                        if (key_read(found, &cp) != 1) {
                    308:                                debug2("user_key_allowed: advance: '%s'", cp);
                    309:                                /* still no key?  advance to next line*/
                    310:                                continue;
                    311:                        }
                    312:                }
1.23      djm       313:                if (key_is_cert(key)) {
1.25      djm       314:                        if (!key_equal(found, key->cert->signature_key))
                    315:                                continue;
                    316:                        if (auth_parse_options(pw, key_options, file,
                    317:                            linenum) != 1)
                    318:                                continue;
1.20      djm       319:                        if (!key_is_cert_authority)
                    320:                                continue;
                    321:                        fp = key_fingerprint(found, SSH_FP_MD5,
                    322:                            SSH_FP_HEX);
1.22      djm       323:                        debug("matching CA found: file %s, line %lu, %s %s",
                    324:                            file, linenum, key_type(found), fp);
1.24      djm       325:                        /*
                    326:                         * If the user has specified a list of principals as
                    327:                         * a key option, then prefer that list to matching
                    328:                         * their username in the certificate principals list.
                    329:                         */
                    330:                        if (authorized_principals != NULL &&
                    331:                            !match_principals_option(authorized_principals,
                    332:                            key->cert)) {
                    333:                                reason = "Certificate does not contain an "
                    334:                                    "authorized principal";
                    335:  fail_reason:
1.36    ! djm       336:                                free(fp);
1.20      djm       337:                                error("%s", reason);
                    338:                                auth_debug_add("%s", reason);
                    339:                                continue;
                    340:                        }
1.24      djm       341:                        if (key_cert_check_authority(key, 0, 0,
                    342:                            authorized_principals == NULL ? pw->pw_name : NULL,
                    343:                            &reason) != 0)
                    344:                                goto fail_reason;
1.23      djm       345:                        if (auth_cert_options(key, pw) != 0) {
1.36    ! djm       346:                                free(fp);
1.20      djm       347:                                continue;
1.22      djm       348:                        }
                    349:                        verbose("Accepted certificate ID \"%s\" "
                    350:                            "signed by %s CA %s via %s", key->cert->key_id,
                    351:                            key_type(found), fp, file);
1.36    ! djm       352:                        free(fp);
1.20      djm       353:                        found_key = 1;
                    354:                        break;
1.25      djm       355:                } else if (key_equal(found, key)) {
                    356:                        if (auth_parse_options(pw, key_options, file,
                    357:                            linenum) != 1)
                    358:                                continue;
                    359:                        if (key_is_cert_authority)
                    360:                                continue;
1.1       markus    361:                        found_key = 1;
                    362:                        debug("matching key found: file %s, line %lu",
                    363:                            file, linenum);
                    364:                        fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
                    365:                        verbose("Found matching %s key: %s",
                    366:                            key_type(found), fp);
1.36    ! djm       367:                        free(fp);
1.1       markus    368:                        break;
                    369:                }
                    370:        }
                    371:        key_free(found);
                    372:        if (!found_key)
                    373:                debug2("key not found");
                    374:        return found_key;
                    375: }
                    376:
1.21      djm       377: /* Authenticate a certificate key against TrustedUserCAKeys */
                    378: static int
                    379: user_cert_trusted_ca(struct passwd *pw, Key *key)
                    380: {
1.24      djm       381:        char *ca_fp, *principals_file = NULL;
1.21      djm       382:        const char *reason;
                    383:        int ret = 0;
                    384:
                    385:        if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
                    386:                return 0;
                    387:
1.22      djm       388:        ca_fp = key_fingerprint(key->cert->signature_key,
                    389:            SSH_FP_MD5, SSH_FP_HEX);
1.21      djm       390:
                    391:        if (key_in_file(key->cert->signature_key,
                    392:            options.trusted_user_ca_keys, 1) != 1) {
                    393:                debug2("%s: CA %s %s is not listed in %s", __func__,
                    394:                    key_type(key->cert->signature_key), ca_fp,
                    395:                    options.trusted_user_ca_keys);
                    396:                goto out;
                    397:        }
1.24      djm       398:        /*
                    399:         * If AuthorizedPrincipals is in use, then compare the certificate
                    400:         * principals against the names in that file rather than matching
                    401:         * against the username.
                    402:         */
                    403:        if ((principals_file = authorized_principals_file(pw)) != NULL) {
                    404:                if (!match_principals_file(principals_file, pw, key->cert)) {
                    405:                        reason = "Certificate does not contain an "
                    406:                            "authorized principal";
                    407:  fail_reason:
                    408:                        error("%s", reason);
                    409:                        auth_debug_add("%s", reason);
                    410:                        goto out;
                    411:                }
1.21      djm       412:        }
1.24      djm       413:        if (key_cert_check_authority(key, 0, 1,
                    414:            principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
                    415:                goto fail_reason;
1.23      djm       416:        if (auth_cert_options(key, pw) != 0)
1.21      djm       417:                goto out;
                    418:
1.22      djm       419:        verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
                    420:            key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
                    421:            options.trusted_user_ca_keys);
1.21      djm       422:        ret = 1;
                    423:
                    424:  out:
1.36    ! djm       425:        free(principals_file);
        !           426:        free(ca_fp);
1.21      djm       427:        return ret;
                    428: }
                    429:
1.31      djm       430: /*
                    431:  * Checks whether key is allowed in file.
                    432:  * returns 1 if the key is allowed or 0 otherwise.
                    433:  */
                    434: static int
                    435: user_key_allowed2(struct passwd *pw, Key *key, char *file)
                    436: {
                    437:        FILE *f;
                    438:        int found_key = 0;
                    439:
                    440:        /* Temporarily use the user's uid. */
                    441:        temporarily_use_uid(pw);
                    442:
                    443:        debug("trying public key file %s", file);
                    444:        if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
                    445:                found_key = check_authkeys_file(f, file, key, pw);
                    446:                fclose(f);
                    447:        }
                    448:
                    449:        restore_uid();
                    450:        return found_key;
                    451: }
                    452:
                    453: /*
                    454:  * Checks whether key is allowed in output of command.
                    455:  * returns 1 if the key is allowed or 0 otherwise.
                    456:  */
                    457: static int
                    458: user_key_command_allowed2(struct passwd *user_pw, Key *key)
                    459: {
                    460:        FILE *f;
                    461:        int ok, found_key = 0;
                    462:        struct passwd *pw;
                    463:        struct stat st;
                    464:        int status, devnull, p[2], i;
                    465:        pid_t pid;
1.32      djm       466:        char *username, errmsg[512];
1.31      djm       467:
                    468:        if (options.authorized_keys_command == NULL ||
                    469:            options.authorized_keys_command[0] != '/')
                    470:                return 0;
                    471:
1.32      djm       472:        if (options.authorized_keys_command_user == NULL) {
                    473:                error("No user for AuthorizedKeysCommand specified, skipping");
                    474:                return 0;
                    475:        }
                    476:
                    477:        username = percent_expand(options.authorized_keys_command_user,
                    478:            "u", user_pw->pw_name, (char *)NULL);
                    479:        pw = getpwnam(username);
                    480:        if (pw == NULL) {
1.34      djm       481:                error("AuthorizedKeysCommandUser \"%s\" not found: %s",
                    482:                    username, strerror(errno));
1.32      djm       483:                free(username);
                    484:                return 0;
1.31      djm       485:        }
1.32      djm       486:        free(username);
1.31      djm       487:
                    488:        temporarily_use_uid(pw);
                    489:
                    490:        if (stat(options.authorized_keys_command, &st) < 0) {
                    491:                error("Could not stat AuthorizedKeysCommand \"%s\": %s",
                    492:                    options.authorized_keys_command, strerror(errno));
                    493:                goto out;
                    494:        }
                    495:        if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
                    496:            errmsg, sizeof(errmsg)) != 0) {
                    497:                error("Unsafe AuthorizedKeysCommand: %s", errmsg);
                    498:                goto out;
                    499:        }
                    500:
                    501:        if (pipe(p) != 0) {
                    502:                error("%s: pipe: %s", __func__, strerror(errno));
                    503:                goto out;
                    504:        }
                    505:
1.33      djm       506:        debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
                    507:            options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
1.31      djm       508:
                    509:        /*
                    510:         * Don't want to call this in the child, where it can fatal() and
                    511:         * run cleanup_exit() code.
                    512:         */
                    513:        restore_uid();
                    514:
                    515:        switch ((pid = fork())) {
                    516:        case -1: /* error */
                    517:                error("%s: fork: %s", __func__, strerror(errno));
                    518:                close(p[0]);
                    519:                close(p[1]);
                    520:                return 0;
                    521:        case 0: /* child */
                    522:                for (i = 0; i < NSIG; i++)
                    523:                        signal(i, SIG_DFL);
                    524:
1.33      djm       525:                if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
                    526:                        error("%s: open %s: %s", __func__, _PATH_DEVNULL,
                    527:                            strerror(errno));
                    528:                        _exit(1);
                    529:                }
                    530:                /* Keep stderr around a while longer to catch errors */
                    531:                if (dup2(devnull, STDIN_FILENO) == -1 ||
                    532:                    dup2(p[1], STDOUT_FILENO) == -1) {
                    533:                        error("%s: dup2: %s", __func__, strerror(errno));
                    534:                        _exit(1);
                    535:                }
1.32      djm       536:                closefrom(STDERR_FILENO + 1);
1.33      djm       537:
1.31      djm       538:                /* Don't use permanently_set_uid() here to avoid fatal() */
                    539:                if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
                    540:                        error("setresgid %u: %s", (u_int)pw->pw_gid,
                    541:                            strerror(errno));
                    542:                        _exit(1);
                    543:                }
                    544:                if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
                    545:                        error("setresuid %u: %s", (u_int)pw->pw_uid,
                    546:                            strerror(errno));
                    547:                        _exit(1);
                    548:                }
1.33      djm       549:                /* stdin is pointed to /dev/null at this point */
                    550:                if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
1.31      djm       551:                        error("%s: dup2: %s", __func__, strerror(errno));
                    552:                        _exit(1);
                    553:                }
                    554:
                    555:                execl(options.authorized_keys_command,
1.33      djm       556:                    options.authorized_keys_command, user_pw->pw_name, NULL);
1.31      djm       557:
                    558:                error("AuthorizedKeysCommand %s exec failed: %s",
                    559:                    options.authorized_keys_command, strerror(errno));
                    560:                _exit(127);
                    561:        default: /* parent */
                    562:                break;
                    563:        }
                    564:
                    565:        temporarily_use_uid(pw);
                    566:
                    567:        close(p[1]);
                    568:        if ((f = fdopen(p[0], "r")) == NULL) {
                    569:                error("%s: fdopen: %s", __func__, strerror(errno));
                    570:                close(p[0]);
                    571:                /* Don't leave zombie child */
                    572:                kill(pid, SIGTERM);
                    573:                while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
                    574:                        ;
                    575:                goto out;
                    576:        }
                    577:        ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
                    578:        fclose(f);
                    579:
                    580:        while (waitpid(pid, &status, 0) == -1) {
                    581:                if (errno != EINTR) {
                    582:                        error("%s: waitpid: %s", __func__, strerror(errno));
                    583:                        goto out;
                    584:                }
                    585:        }
                    586:        if (WIFSIGNALED(status)) {
                    587:                error("AuthorizedKeysCommand %s exited on signal %d",
                    588:                    options.authorized_keys_command, WTERMSIG(status));
                    589:                goto out;
                    590:        } else if (WEXITSTATUS(status) != 0) {
                    591:                error("AuthorizedKeysCommand %s returned status %d",
                    592:                    options.authorized_keys_command, WEXITSTATUS(status));
                    593:                goto out;
                    594:        }
                    595:        found_key = ok;
                    596:  out:
                    597:        restore_uid();
                    598:        return found_key;
                    599: }
                    600:
                    601: /*
                    602:  * Check whether key authenticates and authorises the user.
                    603:  */
1.1       markus    604: int
                    605: user_key_allowed(struct passwd *pw, Key *key)
                    606: {
1.29      djm       607:        u_int success, i;
1.1       markus    608:        char *file;
1.21      djm       609:
                    610:        if (auth_key_is_revoked(key))
                    611:                return 0;
                    612:        if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
                    613:                return 0;
                    614:
                    615:        success = user_cert_trusted_ca(pw, key);
                    616:        if (success)
                    617:                return success;
1.1       markus    618:
1.31      djm       619:        success = user_key_command_allowed2(pw, key);
                    620:        if (success > 0)
                    621:                return success;
                    622:
1.29      djm       623:        for (i = 0; !success && i < options.num_authkeys_files; i++) {
1.31      djm       624:
                    625:                if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
                    626:                        continue;
1.29      djm       627:                file = expand_authorized_keys(
                    628:                    options.authorized_keys_files[i], pw);
1.31      djm       629:
1.29      djm       630:                success = user_key_allowed2(pw, key, file);
1.36    ! djm       631:                free(file);
1.29      djm       632:        }
1.1       markus    633:
                    634:        return success;
                    635: }
1.2       markus    636:
                    637: Authmethod method_pubkey = {
                    638:        "publickey",
                    639:        userauth_pubkey,
                    640:        &options.pubkey_authentication
                    641: };