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

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