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

1.37    ! djm         1: /* $OpenBSD: auth2-pubkey.c,v 1.36 2013/05/17 00:13:13 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
1.37    ! djm       147:                pubkey_auth_info(authctxt, key);
        !           148:
1.1       markus    149:                /* test for correct signature */
                    150:                authenticated = 0;
                    151:                if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
                    152:                    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
1.6       markus    153:                    buffer_len(&b))) == 1)
1.1       markus    154:                        authenticated = 1;
1.6       markus    155:                buffer_free(&b);
1.36      djm       156:                free(sig);
1.1       markus    157:        } else {
                    158:                debug("test whether pkalg/pkblob are acceptable");
                    159:                packet_check_eom();
                    160:
                    161:                /* XXX fake reply and always send PK_OK ? */
                    162:                /*
                    163:                 * XXX this allows testing whether a user is allowed
                    164:                 * to login: if you happen to have a valid pubkey this
                    165:                 * message is sent. the message is NEVER sent at all
                    166:                 * if a user is not allowed to login. is this an
                    167:                 * issue? -markus
                    168:                 */
                    169:                if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
                    170:                        packet_start(SSH2_MSG_USERAUTH_PK_OK);
                    171:                        packet_put_string(pkalg, alen);
                    172:                        packet_put_string(pkblob, blen);
                    173:                        packet_send();
                    174:                        packet_write_wait();
                    175:                        authctxt->postponed = 1;
                    176:                }
                    177:        }
                    178:        if (authenticated != 1)
                    179:                auth_clear_options();
                    180: done:
                    181:        debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
                    182:        if (key != NULL)
                    183:                key_free(key);
1.36      djm       184:        free(pkalg);
                    185:        free(pkblob);
1.1       markus    186:        return authenticated;
                    187: }
                    188:
1.37    ! djm       189: void
        !           190: pubkey_auth_info(Authctxt *authctxt, const Key *key)
        !           191: {
        !           192:        char *fp;
        !           193:
        !           194:        if (key_is_cert(key)) {
        !           195:                fp = key_fingerprint(key->cert->signature_key,
        !           196:                    SSH_FP_MD5, SSH_FP_HEX);
        !           197:                auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s",
        !           198:                    key_type(key), key->cert->key_id,
        !           199:                    (unsigned long long)key->cert->serial,
        !           200:                    key_type(key->cert->signature_key), fp);
        !           201:                free(fp);
        !           202:        } else {
        !           203:                fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
        !           204:                auth_info(authctxt, "%s %s", key_type(key), fp);
        !           205:                free(fp);
        !           206:        }
        !           207: }
        !           208:
1.24      djm       209: static int
                    210: match_principals_option(const char *principal_list, struct KeyCert *cert)
                    211: {
                    212:        char *result;
                    213:        u_int i;
                    214:
                    215:        /* XXX percent_expand() sequences for authorized_principals? */
                    216:
                    217:        for (i = 0; i < cert->nprincipals; i++) {
                    218:                if ((result = match_list(cert->principals[i],
                    219:                    principal_list, NULL)) != NULL) {
                    220:                        debug3("matched principal from key options \"%.100s\"",
                    221:                            result);
1.36      djm       222:                        free(result);
1.24      djm       223:                        return 1;
                    224:                }
                    225:        }
                    226:        return 0;
                    227: }
                    228:
                    229: static int
1.26      djm       230: match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert)
1.24      djm       231: {
                    232:        FILE *f;
1.26      djm       233:        char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
1.24      djm       234:        u_long linenum = 0;
                    235:        u_int i;
                    236:
                    237:        temporarily_use_uid(pw);
                    238:        debug("trying authorized principals file %s", file);
                    239:        if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
                    240:                restore_uid();
                    241:                return 0;
                    242:        }
                    243:        while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.26      djm       244:                /* Skip leading whitespace. */
1.24      djm       245:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    246:                        ;
1.26      djm       247:                /* Skip blank and comment lines. */
                    248:                if ((ep = strchr(cp, '#')) != NULL)
                    249:                        *ep = '\0';
                    250:                if (!*cp || *cp == '\n')
1.24      djm       251:                        continue;
1.26      djm       252:                /* Trim trailing whitespace. */
                    253:                ep = cp + strlen(cp) - 1;
                    254:                while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
                    255:                        *ep-- = '\0';
                    256:                /*
                    257:                 * If the line has internal whitespace then assume it has
                    258:                 * key options.
                    259:                 */
                    260:                line_opts = NULL;
                    261:                if ((ep = strrchr(cp, ' ')) != NULL ||
                    262:                    (ep = strrchr(cp, '\t')) != NULL) {
                    263:                        for (; *ep == ' ' || *ep == '\t'; ep++)
1.27      deraadt   264:                                ;
1.26      djm       265:                        line_opts = cp;
                    266:                        cp = ep;
                    267:                }
1.24      djm       268:                for (i = 0; i < cert->nprincipals; i++) {
                    269:                        if (strcmp(cp, cert->principals[i]) == 0) {
1.30      djm       270:                                debug3("matched principal \"%.100s\" "
                    271:                                    "from file \"%s\" on line %lu",
1.31      djm       272:                                    cert->principals[i], file, linenum);
1.26      djm       273:                                if (auth_parse_options(pw, line_opts,
                    274:                                    file, linenum) != 1)
                    275:                                        continue;
1.24      djm       276:                                fclose(f);
                    277:                                restore_uid();
                    278:                                return 1;
                    279:                        }
                    280:                }
                    281:        }
                    282:        fclose(f);
                    283:        restore_uid();
                    284:        return 0;
1.31      djm       285: }
1.24      djm       286:
1.31      djm       287: /*
                    288:  * Checks whether key is allowed in authorized_keys-format file,
                    289:  * returns 1 if the key is allowed or 0 otherwise.
                    290:  */
1.1       markus    291: static int
1.31      djm       292: check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
1.1       markus    293: {
1.8       dtucker   294:        char line[SSH_MAX_PUBKEY_BYTES];
1.20      djm       295:        const char *reason;
1.18      dtucker   296:        int found_key = 0;
1.1       markus    297:        u_long linenum = 0;
                    298:        Key *found;
                    299:        char *fp;
                    300:
                    301:        found_key = 0;
                    302:
1.37    ! djm       303:        found = NULL;
1.8       dtucker   304:        while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.7       avsm      305:                char *cp, *key_options = NULL;
1.37    ! djm       306:                if (found != NULL)
        !           307:                        key_free(found);
        !           308:                found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
1.20      djm       309:                auth_clear_options();
                    310:
1.1       markus    311:                /* Skip leading whitespace, empty and comment lines. */
                    312:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    313:                        ;
                    314:                if (!*cp || *cp == '\n' || *cp == '#')
                    315:                        continue;
                    316:
                    317:                if (key_read(found, &cp) != 1) {
                    318:                        /* no key?  check if there are options for this key */
                    319:                        int quoted = 0;
                    320:                        debug2("user_key_allowed: check options: '%s'", cp);
1.7       avsm      321:                        key_options = cp;
1.1       markus    322:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    323:                                if (*cp == '\\' && cp[1] == '"')
                    324:                                        cp++;   /* Skip both */
                    325:                                else if (*cp == '"')
                    326:                                        quoted = !quoted;
                    327:                        }
                    328:                        /* Skip remaining whitespace. */
                    329:                        for (; *cp == ' ' || *cp == '\t'; cp++)
                    330:                                ;
                    331:                        if (key_read(found, &cp) != 1) {
                    332:                                debug2("user_key_allowed: advance: '%s'", cp);
                    333:                                /* still no key?  advance to next line*/
                    334:                                continue;
                    335:                        }
                    336:                }
1.23      djm       337:                if (key_is_cert(key)) {
1.25      djm       338:                        if (!key_equal(found, key->cert->signature_key))
                    339:                                continue;
                    340:                        if (auth_parse_options(pw, key_options, file,
                    341:                            linenum) != 1)
                    342:                                continue;
1.20      djm       343:                        if (!key_is_cert_authority)
                    344:                                continue;
                    345:                        fp = key_fingerprint(found, SSH_FP_MD5,
                    346:                            SSH_FP_HEX);
1.22      djm       347:                        debug("matching CA found: file %s, line %lu, %s %s",
                    348:                            file, linenum, key_type(found), fp);
1.24      djm       349:                        /*
                    350:                         * If the user has specified a list of principals as
                    351:                         * a key option, then prefer that list to matching
                    352:                         * their username in the certificate principals list.
                    353:                         */
                    354:                        if (authorized_principals != NULL &&
                    355:                            !match_principals_option(authorized_principals,
                    356:                            key->cert)) {
                    357:                                reason = "Certificate does not contain an "
                    358:                                    "authorized principal";
                    359:  fail_reason:
1.36      djm       360:                                free(fp);
1.20      djm       361:                                error("%s", reason);
                    362:                                auth_debug_add("%s", reason);
                    363:                                continue;
                    364:                        }
1.24      djm       365:                        if (key_cert_check_authority(key, 0, 0,
                    366:                            authorized_principals == NULL ? pw->pw_name : NULL,
                    367:                            &reason) != 0)
                    368:                                goto fail_reason;
1.23      djm       369:                        if (auth_cert_options(key, pw) != 0) {
1.36      djm       370:                                free(fp);
1.20      djm       371:                                continue;
1.22      djm       372:                        }
                    373:                        verbose("Accepted certificate ID \"%s\" "
                    374:                            "signed by %s CA %s via %s", key->cert->key_id,
                    375:                            key_type(found), fp, file);
1.36      djm       376:                        free(fp);
1.20      djm       377:                        found_key = 1;
                    378:                        break;
1.25      djm       379:                } else if (key_equal(found, key)) {
                    380:                        if (auth_parse_options(pw, key_options, file,
                    381:                            linenum) != 1)
                    382:                                continue;
                    383:                        if (key_is_cert_authority)
                    384:                                continue;
1.1       markus    385:                        found_key = 1;
                    386:                        fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1.37    ! djm       387:                        debug("matching key found: file %s, line %lu %s %s",
        !           388:                            file, linenum, key_type(found), fp);
1.36      djm       389:                        free(fp);
1.1       markus    390:                        break;
                    391:                }
                    392:        }
1.37    ! djm       393:        if (found != NULL)
        !           394:                key_free(found);
1.1       markus    395:        if (!found_key)
                    396:                debug2("key not found");
                    397:        return found_key;
                    398: }
                    399:
1.21      djm       400: /* Authenticate a certificate key against TrustedUserCAKeys */
                    401: static int
                    402: user_cert_trusted_ca(struct passwd *pw, Key *key)
                    403: {
1.24      djm       404:        char *ca_fp, *principals_file = NULL;
1.21      djm       405:        const char *reason;
                    406:        int ret = 0;
                    407:
                    408:        if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
                    409:                return 0;
                    410:
1.22      djm       411:        ca_fp = key_fingerprint(key->cert->signature_key,
                    412:            SSH_FP_MD5, SSH_FP_HEX);
1.21      djm       413:
                    414:        if (key_in_file(key->cert->signature_key,
                    415:            options.trusted_user_ca_keys, 1) != 1) {
                    416:                debug2("%s: CA %s %s is not listed in %s", __func__,
                    417:                    key_type(key->cert->signature_key), ca_fp,
                    418:                    options.trusted_user_ca_keys);
                    419:                goto out;
                    420:        }
1.24      djm       421:        /*
                    422:         * If AuthorizedPrincipals is in use, then compare the certificate
                    423:         * principals against the names in that file rather than matching
                    424:         * against the username.
                    425:         */
                    426:        if ((principals_file = authorized_principals_file(pw)) != NULL) {
                    427:                if (!match_principals_file(principals_file, pw, key->cert)) {
                    428:                        reason = "Certificate does not contain an "
                    429:                            "authorized principal";
                    430:  fail_reason:
                    431:                        error("%s", reason);
                    432:                        auth_debug_add("%s", reason);
                    433:                        goto out;
                    434:                }
1.21      djm       435:        }
1.24      djm       436:        if (key_cert_check_authority(key, 0, 1,
                    437:            principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
                    438:                goto fail_reason;
1.23      djm       439:        if (auth_cert_options(key, pw) != 0)
1.21      djm       440:                goto out;
                    441:
1.22      djm       442:        verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
                    443:            key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
                    444:            options.trusted_user_ca_keys);
1.21      djm       445:        ret = 1;
                    446:
                    447:  out:
1.36      djm       448:        free(principals_file);
                    449:        free(ca_fp);
1.21      djm       450:        return ret;
                    451: }
                    452:
1.31      djm       453: /*
                    454:  * Checks whether key is allowed in file.
                    455:  * returns 1 if the key is allowed or 0 otherwise.
                    456:  */
                    457: static int
                    458: user_key_allowed2(struct passwd *pw, Key *key, char *file)
                    459: {
                    460:        FILE *f;
                    461:        int found_key = 0;
                    462:
                    463:        /* Temporarily use the user's uid. */
                    464:        temporarily_use_uid(pw);
                    465:
                    466:        debug("trying public key file %s", file);
                    467:        if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
                    468:                found_key = check_authkeys_file(f, file, key, pw);
                    469:                fclose(f);
                    470:        }
                    471:
                    472:        restore_uid();
                    473:        return found_key;
                    474: }
                    475:
                    476: /*
                    477:  * Checks whether key is allowed in output of command.
                    478:  * returns 1 if the key is allowed or 0 otherwise.
                    479:  */
                    480: static int
                    481: user_key_command_allowed2(struct passwd *user_pw, Key *key)
                    482: {
                    483:        FILE *f;
                    484:        int ok, found_key = 0;
                    485:        struct passwd *pw;
                    486:        struct stat st;
                    487:        int status, devnull, p[2], i;
                    488:        pid_t pid;
1.32      djm       489:        char *username, errmsg[512];
1.31      djm       490:
                    491:        if (options.authorized_keys_command == NULL ||
                    492:            options.authorized_keys_command[0] != '/')
                    493:                return 0;
                    494:
1.32      djm       495:        if (options.authorized_keys_command_user == NULL) {
                    496:                error("No user for AuthorizedKeysCommand specified, skipping");
                    497:                return 0;
                    498:        }
                    499:
                    500:        username = percent_expand(options.authorized_keys_command_user,
                    501:            "u", user_pw->pw_name, (char *)NULL);
                    502:        pw = getpwnam(username);
                    503:        if (pw == NULL) {
1.34      djm       504:                error("AuthorizedKeysCommandUser \"%s\" not found: %s",
                    505:                    username, strerror(errno));
1.32      djm       506:                free(username);
                    507:                return 0;
1.31      djm       508:        }
1.32      djm       509:        free(username);
1.31      djm       510:
                    511:        temporarily_use_uid(pw);
                    512:
                    513:        if (stat(options.authorized_keys_command, &st) < 0) {
                    514:                error("Could not stat AuthorizedKeysCommand \"%s\": %s",
                    515:                    options.authorized_keys_command, strerror(errno));
                    516:                goto out;
                    517:        }
                    518:        if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
                    519:            errmsg, sizeof(errmsg)) != 0) {
                    520:                error("Unsafe AuthorizedKeysCommand: %s", errmsg);
                    521:                goto out;
                    522:        }
                    523:
                    524:        if (pipe(p) != 0) {
                    525:                error("%s: pipe: %s", __func__, strerror(errno));
                    526:                goto out;
                    527:        }
                    528:
1.33      djm       529:        debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
                    530:            options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
1.31      djm       531:
                    532:        /*
                    533:         * Don't want to call this in the child, where it can fatal() and
                    534:         * run cleanup_exit() code.
                    535:         */
                    536:        restore_uid();
                    537:
                    538:        switch ((pid = fork())) {
                    539:        case -1: /* error */
                    540:                error("%s: fork: %s", __func__, strerror(errno));
                    541:                close(p[0]);
                    542:                close(p[1]);
                    543:                return 0;
                    544:        case 0: /* child */
                    545:                for (i = 0; i < NSIG; i++)
                    546:                        signal(i, SIG_DFL);
                    547:
1.33      djm       548:                if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
                    549:                        error("%s: open %s: %s", __func__, _PATH_DEVNULL,
                    550:                            strerror(errno));
                    551:                        _exit(1);
                    552:                }
                    553:                /* Keep stderr around a while longer to catch errors */
                    554:                if (dup2(devnull, STDIN_FILENO) == -1 ||
                    555:                    dup2(p[1], STDOUT_FILENO) == -1) {
                    556:                        error("%s: dup2: %s", __func__, strerror(errno));
                    557:                        _exit(1);
                    558:                }
1.32      djm       559:                closefrom(STDERR_FILENO + 1);
1.33      djm       560:
1.31      djm       561:                /* Don't use permanently_set_uid() here to avoid fatal() */
                    562:                if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
                    563:                        error("setresgid %u: %s", (u_int)pw->pw_gid,
                    564:                            strerror(errno));
                    565:                        _exit(1);
                    566:                }
                    567:                if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
                    568:                        error("setresuid %u: %s", (u_int)pw->pw_uid,
                    569:                            strerror(errno));
                    570:                        _exit(1);
                    571:                }
1.33      djm       572:                /* stdin is pointed to /dev/null at this point */
                    573:                if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
1.31      djm       574:                        error("%s: dup2: %s", __func__, strerror(errno));
                    575:                        _exit(1);
                    576:                }
                    577:
                    578:                execl(options.authorized_keys_command,
1.33      djm       579:                    options.authorized_keys_command, user_pw->pw_name, NULL);
1.31      djm       580:
                    581:                error("AuthorizedKeysCommand %s exec failed: %s",
                    582:                    options.authorized_keys_command, strerror(errno));
                    583:                _exit(127);
                    584:        default: /* parent */
                    585:                break;
                    586:        }
                    587:
                    588:        temporarily_use_uid(pw);
                    589:
                    590:        close(p[1]);
                    591:        if ((f = fdopen(p[0], "r")) == NULL) {
                    592:                error("%s: fdopen: %s", __func__, strerror(errno));
                    593:                close(p[0]);
                    594:                /* Don't leave zombie child */
                    595:                kill(pid, SIGTERM);
                    596:                while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
                    597:                        ;
                    598:                goto out;
                    599:        }
                    600:        ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
                    601:        fclose(f);
                    602:
                    603:        while (waitpid(pid, &status, 0) == -1) {
                    604:                if (errno != EINTR) {
                    605:                        error("%s: waitpid: %s", __func__, strerror(errno));
                    606:                        goto out;
                    607:                }
                    608:        }
                    609:        if (WIFSIGNALED(status)) {
                    610:                error("AuthorizedKeysCommand %s exited on signal %d",
                    611:                    options.authorized_keys_command, WTERMSIG(status));
                    612:                goto out;
                    613:        } else if (WEXITSTATUS(status) != 0) {
                    614:                error("AuthorizedKeysCommand %s returned status %d",
                    615:                    options.authorized_keys_command, WEXITSTATUS(status));
                    616:                goto out;
                    617:        }
                    618:        found_key = ok;
                    619:  out:
                    620:        restore_uid();
                    621:        return found_key;
                    622: }
                    623:
                    624: /*
                    625:  * Check whether key authenticates and authorises the user.
                    626:  */
1.1       markus    627: int
                    628: user_key_allowed(struct passwd *pw, Key *key)
                    629: {
1.29      djm       630:        u_int success, i;
1.1       markus    631:        char *file;
1.21      djm       632:
                    633:        if (auth_key_is_revoked(key))
                    634:                return 0;
                    635:        if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
                    636:                return 0;
                    637:
                    638:        success = user_cert_trusted_ca(pw, key);
                    639:        if (success)
                    640:                return success;
1.1       markus    641:
1.31      djm       642:        success = user_key_command_allowed2(pw, key);
                    643:        if (success > 0)
                    644:                return success;
                    645:
1.29      djm       646:        for (i = 0; !success && i < options.num_authkeys_files; i++) {
1.31      djm       647:
                    648:                if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
                    649:                        continue;
1.29      djm       650:                file = expand_authorized_keys(
                    651:                    options.authorized_keys_files[i], pw);
1.31      djm       652:
1.29      djm       653:                success = user_key_allowed2(pw, key, file);
1.36      djm       654:                free(file);
1.29      djm       655:        }
1.1       markus    656:
                    657:        return success;
                    658: }
1.2       markus    659:
                    660: Authmethod method_pubkey = {
                    661:        "publickey",
                    662:        userauth_pubkey,
                    663:        &options.pubkey_authentication
                    664: };