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

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