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

1.107   ! djm         1: /* $OpenBSD: auth2-pubkey.c,v 1.106 2021/01/27 10:05:28 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.13      stevesk    29:
1.94      djm        30: #include <stdlib.h>
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"
1.106     djm        47: #include "kex.h"
1.81      markus     48: #include "sshbuf.h"
1.1       markus     49: #include "log.h"
1.41      millert    50: #include "misc.h"
1.1       markus     51: #include "servconf.h"
                     52: #include "compat.h"
1.64      markus     53: #include "sshkey.h"
1.15      deraadt    54: #include "hostfile.h"
1.1       markus     55: #include "auth.h"
                     56: #include "pathnames.h"
                     57: #include "uidswap.h"
                     58: #include "auth-options.h"
                     59: #include "canohost.h"
1.15      deraadt    60: #ifdef GSSAPI
                     61: #include "ssh-gss.h"
                     62: #endif
1.1       markus     63: #include "monitor_wrap.h"
1.21      djm        64: #include "authfile.h"
1.24      djm        65: #include "match.h"
1.50      djm        66: #include "ssherr.h"
                     67: #include "channels.h" /* XXX for session.h */
                     68: #include "session.h" /* XXX for child_set_env(); refactor? */
1.96      djm        69: #include "sk-api.h"
1.1       markus     70:
                     71: /* import */
                     72: extern ServerOptions options;
                     73:
1.73      djm        74: static char *
                     75: format_key(const struct sshkey *key)
                     76: {
                     77:        char *ret, *fp = sshkey_fingerprint(key,
                     78:            options.fingerprint_hash, SSH_FP_DEFAULT);
                     79:
                     80:        xasprintf(&ret, "%s %s", sshkey_type(key), fp);
                     81:        free(fp);
                     82:        return ret;
                     83: }
                     84:
1.2       markus     85: static int
1.65      markus     86: userauth_pubkey(struct ssh *ssh)
1.1       markus     87: {
1.65      markus     88:        Authctxt *authctxt = ssh->authctxt;
1.77      djm        89:        struct passwd *pw = authctxt->pw;
1.83      djm        90:        struct sshbuf *b = NULL;
1.63      markus     91:        struct sshkey *key = NULL;
1.83      djm        92:        char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
                     93:        u_char *pkblob = NULL, *sig = NULL, have_sig;
1.64      markus     94:        size_t blen, slen;
                     95:        int r, pktype;
1.100     djm        96:        int req_presence = 0, req_verify = 0, authenticated = 0;
1.77      djm        97:        struct sshauthopt *authopts = NULL;
1.95      djm        98:        struct sshkey_sig_details *sig_details = NULL;
1.1       markus     99:
1.75      djm       100:        if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
                    101:            (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
                    102:            (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
1.101     djm       103:                fatal_fr(r, "parse packet");
1.87      djm       104:
                    105:        if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) {
                    106:                char *keystring;
                    107:                struct sshbuf *pkbuf;
                    108:
                    109:                if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL)
1.101     djm       110:                        fatal_f("sshbuf_from failed");
1.91      djm       111:                if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL)
1.101     djm       112:                        fatal_f("sshbuf_dtob64 failed");
                    113:                debug2_f("%s user %s %s public key %s %s",
1.87      djm       114:                    authctxt->valid ? "valid" : "invalid", authctxt->user,
                    115:                    have_sig ? "attempting" : "querying", pkalg, keystring);
                    116:                sshbuf_free(pkbuf);
                    117:                free(keystring);
                    118:        }
                    119:
1.64      markus    120:        pktype = sshkey_type_from_name(pkalg);
1.1       markus    121:        if (pktype == KEY_UNSPEC) {
                    122:                /* this is perfectly legal */
1.101     djm       123:                verbose_f("unsupported public key algorithm: %s", pkalg);
1.1       markus    124:                goto done;
                    125:        }
1.64      markus    126:        if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
1.101     djm       127:                error_fr(r, "parse key");
1.64      markus    128:                goto done;
                    129:        }
1.1       markus    130:        if (key == NULL) {
1.101     djm       131:                error_f("cannot decode key: %s", pkalg);
1.1       markus    132:                goto done;
                    133:        }
                    134:        if (key->type != pktype) {
1.101     djm       135:                error_f("type mismatch for decoded key "
                    136:                    "(received %d, expected %d)", key->type, pktype);
1.39      djm       137:                goto done;
                    138:        }
1.64      markus    139:        if (sshkey_type_plain(key->type) == KEY_RSA &&
                    140:            (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
1.39      djm       141:                logit("Refusing RSA key because client uses unsafe "
                    142:                    "signature scheme");
1.1       markus    143:                goto done;
                    144:        }
1.68      djm       145:        if (auth2_key_already_used(authctxt, key)) {
1.64      markus    146:                logit("refusing previously-used %s key", sshkey_type(key));
1.44      djm       147:                goto done;
                    148:        }
1.104     dtucker   149:        if (match_pattern_list(pkalg, options.pubkey_accepted_algos, 0) != 1) {
                    150:                logit_f("key type %s not in PubkeyAcceptedAlgorithms",
1.101     djm       151:                    sshkey_ssh_name(key));
1.45      djm       152:                goto done;
                    153:        }
1.86      djm       154:        if ((r = sshkey_check_cert_sigtype(key,
                    155:            options.ca_sign_algorithms)) != 0) {
1.101     djm       156:                logit_fr(r, "certificate signature algorithm %s",
1.86      djm       157:                    (key->cert == NULL || key->cert->signature_type == NULL) ?
1.101     djm       158:                    "(null)" : key->cert->signature_type);
1.86      djm       159:                goto done;
                    160:        }
1.73      djm       161:        key_s = format_key(key);
                    162:        if (sshkey_is_cert(key))
                    163:                ca_s = format_key(key->cert->signature_key);
                    164:
1.1       markus    165:        if (have_sig) {
1.101     djm       166:                debug3_f("have %s signature for %s%s%s", pkalg, key_s,
                    167:                    ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
1.64      markus    168:                if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
                    169:                    (r = sshpkt_get_end(ssh)) != 0)
1.101     djm       170:                        fatal_fr(r, "parse signature packet");
1.64      markus    171:                if ((b = sshbuf_new()) == NULL)
1.101     djm       172:                        fatal_f("sshbuf_new failed");
1.64      markus    173:                if (ssh->compat & SSH_OLD_SESSIONID) {
1.106     djm       174:                        if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
1.101     djm       175:                                fatal_fr(r, "put old session id");
1.1       markus    176:                } else {
1.106     djm       177:                        if ((r = sshbuf_put_stringb(b,
                    178:                            ssh->kex->session_id)) != 0)
1.101     djm       179:                                fatal_fr(r, "put session id");
1.1       markus    180:                }
1.83      djm       181:                if (!authctxt->valid || authctxt->user == NULL) {
1.101     djm       182:                        debug2_f("disabled because of invalid user");
1.83      djm       183:                        goto done;
                    184:                }
1.1       markus    185:                /* reconstruct packet */
1.35      djm       186:                xasprintf(&userstyle, "%s%s%s", authctxt->user,
                    187:                    authctxt->style ? ":" : "",
                    188:                    authctxt->style ? authctxt->style : "");
1.64      markus    189:                if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                    190:                    (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
1.75      djm       191:                    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
                    192:                    (r = sshbuf_put_cstring(b, "publickey")) != 0 ||
                    193:                    (r = sshbuf_put_u8(b, have_sig)) != 0 ||
1.85      mestre    194:                    (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
1.75      djm       195:                    (r = sshbuf_put_string(b, pkblob, blen)) != 0)
1.101     djm       196:                        fatal_fr(r, "reconstruct packet");
1.1       markus    197: #ifdef DEBUG_PK
1.64      markus    198:                sshbuf_dump(b, stderr);
1.1       markus    199: #endif
                    200:                /* test for correct signature */
                    201:                authenticated = 0;
1.77      djm       202:                if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
1.80      djm       203:                    PRIVSEP(sshkey_verify(key, sig, slen,
                    204:                    sshbuf_ptr(b), sshbuf_len(b),
                    205:                    (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
1.95      djm       206:                    ssh->compat, &sig_details)) == 0) {
1.1       markus    207:                        authenticated = 1;
1.44      djm       208:                }
1.96      djm       209:                if (authenticated == 1 && sig_details != NULL) {
                    210:                        auth2_record_info(authctxt, "signature count = %u",
                    211:                            sig_details->sk_counter);
1.101     djm       212:                        debug_f("sk_counter = %u, sk_flags = 0x%02x",
                    213:                            sig_details->sk_counter, sig_details->sk_flags);
1.96      djm       214:                        req_presence = (options.pubkey_auth_options &
1.97      djm       215:                            PUBKEYAUTH_TOUCH_REQUIRED) ||
                    216:                            !authopts->no_require_user_presence;
1.96      djm       217:                        if (req_presence && (sig_details->sk_flags &
                    218:                            SSH_SK_USER_PRESENCE_REQD) == 0) {
                    219:                                error("public key %s signature for %s%s from "
                    220:                                    "%.128s port %d rejected: user presence "
1.99      naddy     221:                                    "(authenticator touch) requirement "
                    222:                                    "not met ", key_s,
1.100     djm       223:                                    authctxt->valid ? "" : "invalid user ",
                    224:                                    authctxt->user, ssh_remote_ipaddr(ssh),
                    225:                                    ssh_remote_port(ssh));
                    226:                                authenticated = 0;
                    227:                                goto done;
                    228:                        }
                    229:                        req_verify = (options.pubkey_auth_options &
                    230:                            PUBKEYAUTH_VERIFY_REQUIRED) ||
                    231:                            authopts->require_verify;
                    232:                        if (req_verify && (sig_details->sk_flags &
                    233:                            SSH_SK_USER_VERIFICATION_REQD) == 0) {
                    234:                                error("public key %s signature for %s%s from "
                    235:                                    "%.128s port %d rejected: user "
                    236:                                    "verification requirement not met ", key_s,
1.96      djm       237:                                    authctxt->valid ? "" : "invalid user ",
                    238:                                    authctxt->user, ssh_remote_ipaddr(ssh),
                    239:                                    ssh_remote_port(ssh));
                    240:                                authenticated = 0;
                    241:                                goto done;
                    242:                        }
1.95      djm       243:                }
1.68      djm       244:                auth2_record_key(authctxt, authenticated, key);
1.1       markus    245:        } else {
1.101     djm       246:                debug_f("test pkalg %s pkblob %s%s%s", pkalg, key_s,
                    247:                    ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
1.73      djm       248:
1.64      markus    249:                if ((r = sshpkt_get_end(ssh)) != 0)
1.101     djm       250:                        fatal_fr(r, "parse packet");
1.1       markus    251:
1.83      djm       252:                if (!authctxt->valid || authctxt->user == NULL) {
1.101     djm       253:                        debug2_f("disabled because of invalid user");
1.83      djm       254:                        goto done;
                    255:                }
1.1       markus    256:                /* XXX fake reply and always send PK_OK ? */
                    257:                /*
                    258:                 * XXX this allows testing whether a user is allowed
                    259:                 * to login: if you happen to have a valid pubkey this
                    260:                 * message is sent. the message is NEVER sent at all
                    261:                 * if a user is not allowed to login. is this an
                    262:                 * issue? -markus
                    263:                 */
1.77      djm       264:                if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) {
1.64      markus    265:                        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
                    266:                            != 0 ||
                    267:                            (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
                    268:                            (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
1.82      markus    269:                            (r = sshpkt_send(ssh)) != 0 ||
                    270:                            (r = ssh_packet_write_wait(ssh)) != 0)
1.101     djm       271:                                fatal_fr(r, "send packet");
1.1       markus    272:                        authctxt->postponed = 1;
                    273:                }
                    274:        }
                    275: done:
1.77      djm       276:        if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
1.101     djm       277:                debug_f("key options inconsistent with existing");
1.77      djm       278:                authenticated = 0;
                    279:        }
1.101     djm       280:        debug2_f("authenticated %d pkalg %s", authenticated, pkalg);
1.77      djm       281:
1.84      djm       282:        sshbuf_free(b);
1.77      djm       283:        sshauthopt_free(authopts);
1.68      djm       284:        sshkey_free(key);
1.64      markus    285:        free(userstyle);
1.36      djm       286:        free(pkalg);
                    287:        free(pkblob);
1.73      djm       288:        free(key_s);
                    289:        free(ca_s);
1.83      djm       290:        free(sig);
1.95      djm       291:        sshkey_sig_details_free(sig_details);
1.1       markus    292:        return authenticated;
                    293: }
                    294:
1.24      djm       295: static int
1.40      djm       296: match_principals_option(const char *principal_list, struct sshkey_cert *cert)
1.24      djm       297: {
                    298:        char *result;
                    299:        u_int i;
                    300:
                    301:        /* XXX percent_expand() sequences for authorized_principals? */
                    302:
                    303:        for (i = 0; i < cert->nprincipals; i++) {
                    304:                if ((result = match_list(cert->principals[i],
                    305:                    principal_list, NULL)) != NULL) {
                    306:                        debug3("matched principal from key options \"%.100s\"",
                    307:                            result);
1.36      djm       308:                        free(result);
1.24      djm       309:                        return 1;
                    310:                }
                    311:        }
                    312:        return 0;
                    313: }
                    314:
1.77      djm       315: /*
                    316:  * Process a single authorized_principals format line. Returns 0 and sets
                    317:  * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a
                    318:  * log preamble for file/line information.
                    319:  */
1.24      djm       320: static int
1.77      djm       321: check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert,
                    322:     const char *loc, struct sshauthopt **authoptsp)
1.24      djm       323: {
1.77      djm       324:        u_int i, found = 0;
                    325:        char *ep, *line_opts;
                    326:        const char *reason = NULL;
                    327:        struct sshauthopt *opts = NULL;
                    328:
                    329:        if (authoptsp != NULL)
                    330:                *authoptsp = NULL;
                    331:
                    332:        /* Trim trailing whitespace. */
                    333:        ep = cp + strlen(cp) - 1;
                    334:        while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
                    335:                *ep-- = '\0';
                    336:
                    337:        /*
                    338:         * If the line has internal whitespace then assume it has
                    339:         * key options.
                    340:         */
                    341:        line_opts = NULL;
                    342:        if ((ep = strrchr(cp, ' ')) != NULL ||
                    343:            (ep = strrchr(cp, '\t')) != NULL) {
                    344:                for (; *ep == ' ' || *ep == '\t'; ep++)
                    345:                        ;
                    346:                line_opts = cp;
                    347:                cp = ep;
                    348:        }
                    349:        if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) {
                    350:                debug("%s: bad principals options: %s", loc, reason);
                    351:                auth_debug_add("%s: bad principals options: %s", loc, reason);
                    352:                return -1;
                    353:        }
                    354:        /* Check principals in cert against those on line */
                    355:        for (i = 0; i < cert->nprincipals; i++) {
                    356:                if (strcmp(cp, cert->principals[i]) != 0)
                    357:                        continue;
                    358:                debug3("%s: matched principal \"%.100s\"",
                    359:                    loc, cert->principals[i]);
                    360:                found = 1;
                    361:        }
                    362:        if (found && authoptsp != NULL) {
                    363:                *authoptsp = opts;
                    364:                opts = NULL;
                    365:        }
                    366:        sshauthopt_free(opts);
                    367:        return found ? 0 : -1;
                    368: }
                    369:
                    370: static int
                    371: process_principals(struct ssh *ssh, FILE *f, const char *file,
                    372:     const struct sshkey_cert *cert, struct sshauthopt **authoptsp)
                    373: {
1.79      markus    374:        char loc[256], *line = NULL, *cp, *ep;
                    375:        size_t linesize = 0;
1.24      djm       376:        u_long linenum = 0;
1.77      djm       377:        u_int found_principal = 0;
                    378:
                    379:        if (authoptsp != NULL)
                    380:                *authoptsp = NULL;
1.24      djm       381:
1.79      markus    382:        while (getline(&line, &linesize, f) != -1) {
                    383:                linenum++;
1.62      djm       384:                /* Always consume entire input */
                    385:                if (found_principal)
                    386:                        continue;
1.77      djm       387:
1.26      djm       388:                /* Skip leading whitespace. */
1.24      djm       389:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    390:                        ;
1.26      djm       391:                /* Skip blank and comment lines. */
                    392:                if ((ep = strchr(cp, '#')) != NULL)
                    393:                        *ep = '\0';
                    394:                if (!*cp || *cp == '\n')
1.24      djm       395:                        continue;
1.77      djm       396:
                    397:                snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
                    398:                if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0)
                    399:                        found_principal = 1;
1.24      djm       400:        }
1.79      markus    401:        free(line);
1.62      djm       402:        return found_principal;
1.51      djm       403: }
                    404:
1.77      djm       405: /* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */
                    406:
1.51      djm       407: static int
1.77      djm       408: match_principals_file(struct ssh *ssh, struct passwd *pw, char *file,
                    409:     struct sshkey_cert *cert, struct sshauthopt **authoptsp)
1.51      djm       410: {
                    411:        FILE *f;
                    412:        int success;
                    413:
1.77      djm       414:        if (authoptsp != NULL)
                    415:                *authoptsp = NULL;
                    416:
1.51      djm       417:        temporarily_use_uid(pw);
                    418:        debug("trying authorized principals file %s", file);
                    419:        if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
                    420:                restore_uid();
                    421:                return 0;
                    422:        }
1.77      djm       423:        success = process_principals(ssh, f, file, cert, authoptsp);
1.24      djm       424:        fclose(f);
                    425:        restore_uid();
1.51      djm       426:        return success;
1.31      djm       427: }
1.24      djm       428:
1.31      djm       429: /*
1.51      djm       430:  * Checks whether principal is allowed in output of command.
                    431:  * returns 1 if the principal is allowed or 0 otherwise.
                    432:  */
                    433: static int
1.77      djm       434: match_principals_command(struct ssh *ssh, struct passwd *user_pw,
                    435:     const struct sshkey *key, struct sshauthopt **authoptsp)
1.51      djm       436: {
1.77      djm       437:        struct passwd *runas_pw = NULL;
1.56      djm       438:        const struct sshkey_cert *cert = key->cert;
1.51      djm       439:        FILE *f = NULL;
1.56      djm       440:        int r, ok, found_principal = 0;
1.51      djm       441:        int i, ac = 0, uid_swapped = 0;
                    442:        pid_t pid;
                    443:        char *tmp, *username = NULL, *command = NULL, **av = NULL;
1.56      djm       444:        char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
1.88      djm       445:        char serial_s[32], uidstr[32];
1.51      djm       446:        void (*osigchld)(int);
                    447:
1.77      djm       448:        if (authoptsp != NULL)
                    449:                *authoptsp = NULL;
1.51      djm       450:        if (options.authorized_principals_command == NULL)
                    451:                return 0;
                    452:        if (options.authorized_principals_command_user == NULL) {
                    453:                error("No user for AuthorizedPrincipalsCommand specified, "
                    454:                    "skipping");
                    455:                return 0;
                    456:        }
                    457:
                    458:        /*
                    459:         * NB. all returns later this function should go via "out" to
                    460:         * ensure the original SIGCHLD handler is restored properly.
                    461:         */
1.98      dtucker   462:        osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.51      djm       463:
                    464:        /* Prepare and verify the user for the command */
                    465:        username = percent_expand(options.authorized_principals_command_user,
                    466:            "u", user_pw->pw_name, (char *)NULL);
1.77      djm       467:        runas_pw = getpwnam(username);
                    468:        if (runas_pw == NULL) {
1.51      djm       469:                error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
                    470:                    username, strerror(errno));
                    471:                goto out;
                    472:        }
                    473:
                    474:        /* Turn the command into an argument vector */
1.69      djm       475:        if (argv_split(options.authorized_principals_command, &ac, &av) != 0) {
1.51      djm       476:                error("AuthorizedPrincipalsCommand \"%s\" contains "
1.90      djm       477:                    "invalid quotes", options.authorized_principals_command);
1.51      djm       478:                goto out;
                    479:        }
                    480:        if (ac == 0) {
                    481:                error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
1.90      djm       482:                    options.authorized_principals_command);
1.51      djm       483:                goto out;
                    484:        }
1.56      djm       485:        if ((ca_fp = sshkey_fingerprint(cert->signature_key,
                    486:            options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
1.101     djm       487:                error_f("sshkey_fingerprint failed");
1.56      djm       488:                goto out;
                    489:        }
1.57      djm       490:        if ((key_fp = sshkey_fingerprint(key,
1.56      djm       491:            options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
1.101     djm       492:                error_f("sshkey_fingerprint failed");
1.56      djm       493:                goto out;
                    494:        }
                    495:        if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
1.101     djm       496:                error_fr(r, "sshkey_to_base64 failed");
1.56      djm       497:                goto out;
                    498:        }
                    499:        if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1.101     djm       500:                error_fr(r, "sshkey_to_base64 failed");
1.56      djm       501:                goto out;
                    502:        }
1.59      djm       503:        snprintf(serial_s, sizeof(serial_s), "%llu",
                    504:            (unsigned long long)cert->serial);
1.78      djm       505:        snprintf(uidstr, sizeof(uidstr), "%llu",
                    506:            (unsigned long long)user_pw->pw_uid);
1.51      djm       507:        for (i = 1; i < ac; i++) {
                    508:                tmp = percent_expand(av[i],
1.78      djm       509:                    "U", uidstr,
1.51      djm       510:                    "u", user_pw->pw_name,
                    511:                    "h", user_pw->pw_dir,
1.56      djm       512:                    "t", sshkey_ssh_name(key),
                    513:                    "T", sshkey_ssh_name(cert->signature_key),
                    514:                    "f", key_fp,
                    515:                    "F", ca_fp,
                    516:                    "k", keytext,
                    517:                    "K", catext,
1.58      djm       518:                    "i", cert->key_id,
                    519:                    "s", serial_s,
1.51      djm       520:                    (char *)NULL);
                    521:                if (tmp == NULL)
1.101     djm       522:                        fatal_f("percent_expand failed");
1.51      djm       523:                free(av[i]);
                    524:                av[i] = tmp;
                    525:        }
                    526:        /* Prepare a printable command for logs, etc. */
1.69      djm       527:        command = argv_assemble(ac, av);
1.51      djm       528:
1.103     djm       529:        if ((pid = subprocess("AuthorizedPrincipalsCommand", command,
1.69      djm       530:            ac, av, &f,
1.103     djm       531:            SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
                    532:            runas_pw, temporarily_use_uid, restore_uid)) == 0)
1.51      djm       533:                goto out;
                    534:
                    535:        uid_swapped = 1;
1.77      djm       536:        temporarily_use_uid(runas_pw);
1.51      djm       537:
1.77      djm       538:        ok = process_principals(ssh, f, "(command)", cert, authoptsp);
1.51      djm       539:
1.61      djm       540:        fclose(f);
                    541:        f = NULL;
                    542:
1.70      djm       543:        if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
1.51      djm       544:                goto out;
                    545:
                    546:        /* Read completed successfully */
                    547:        found_principal = ok;
                    548:  out:
                    549:        if (f != NULL)
                    550:                fclose(f);
1.98      dtucker   551:        ssh_signal(SIGCHLD, osigchld);
1.51      djm       552:        for (i = 0; i < ac; i++)
                    553:                free(av[i]);
                    554:        free(av);
                    555:        if (uid_swapped)
                    556:                restore_uid();
                    557:        free(command);
                    558:        free(username);
1.56      djm       559:        free(ca_fp);
                    560:        free(key_fp);
                    561:        free(catext);
                    562:        free(keytext);
1.51      djm       563:        return found_principal;
1.77      djm       564: }
                    565:
                    566: /*
                    567:  * Check a single line of an authorized_keys-format file. Returns 0 if key
                    568:  * matches, -1 otherwise. Will return key/cert options via *authoptsp
                    569:  * on success. "loc" is used as file/line location in log messages.
                    570:  */
                    571: static int
                    572: check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
                    573:     char *cp, const char *loc, struct sshauthopt **authoptsp)
                    574: {
                    575:        int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type;
                    576:        struct sshkey *found = NULL;
                    577:        struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL;
                    578:        char *key_options = NULL, *fp = NULL;
                    579:        const char *reason = NULL;
                    580:        int ret = -1;
                    581:
                    582:        if (authoptsp != NULL)
                    583:                *authoptsp = NULL;
                    584:
                    585:        if ((found = sshkey_new(want_keytype)) == NULL) {
1.101     djm       586:                debug3_f("keytype %d failed", want_keytype);
1.77      djm       587:                goto out;
                    588:        }
                    589:
                    590:        /* XXX djm: peek at key type in line and skip if unwanted */
                    591:
                    592:        if (sshkey_read(found, &cp) != 0) {
                    593:                /* no key?  check for options */
                    594:                debug2("%s: check options: '%s'", loc, cp);
                    595:                key_options = cp;
1.93      djm       596:                if (sshkey_advance_past_options(&cp) != 0) {
1.77      djm       597:                        reason = "invalid key option string";
                    598:                        goto fail_reason;
                    599:                }
                    600:                skip_space(&cp);
                    601:                if (sshkey_read(found, &cp) != 0) {
                    602:                        /* still no key?  advance to next line*/
                    603:                        debug2("%s: advance: '%s'", loc, cp);
                    604:                        goto out;
                    605:                }
                    606:        }
                    607:        /* Parse key options now; we need to know if this is a CA key */
                    608:        if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) {
                    609:                debug("%s: bad key options: %s", loc, reason);
                    610:                auth_debug_add("%s: bad key options: %s", loc, reason);
                    611:                goto out;
                    612:        }
                    613:        /* Ignore keys that don't match or incorrectly marked as CAs */
                    614:        if (sshkey_is_cert(key)) {
                    615:                /* Certificate; check signature key against CA */
                    616:                if (!sshkey_equal(found, key->cert->signature_key) ||
                    617:                    !keyopts->cert_authority)
                    618:                        goto out;
                    619:        } else {
                    620:                /* Plain key: check it against key found in file */
                    621:                if (!sshkey_equal(found, key) || keyopts->cert_authority)
                    622:                        goto out;
                    623:        }
                    624:
                    625:        /* We have a candidate key, perform authorisation checks */
                    626:        if ((fp = sshkey_fingerprint(found,
                    627:            options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1.101     djm       628:                fatal_f("fingerprint failed");
1.77      djm       629:
                    630:        debug("%s: matching %s found: %s %s", loc,
                    631:            sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp);
                    632:
                    633:        if (auth_authorise_keyopts(ssh, pw, keyopts,
                    634:            sshkey_is_cert(key), loc) != 0) {
                    635:                reason = "Refused by key options";
                    636:                goto fail_reason;
                    637:        }
                    638:        /* That's all we need for plain keys. */
                    639:        if (!sshkey_is_cert(key)) {
                    640:                verbose("Accepted key %s %s found at %s",
                    641:                    sshkey_type(found), fp, loc);
                    642:                finalopts = keyopts;
                    643:                keyopts = NULL;
                    644:                goto success;
                    645:        }
                    646:
                    647:        /*
                    648:         * Additional authorisation for certificates.
                    649:         */
                    650:
                    651:        /* Parse and check options present in certificate */
                    652:        if ((certopts = sshauthopt_from_cert(key)) == NULL) {
                    653:                reason = "Invalid certificate options";
                    654:                goto fail_reason;
                    655:        }
                    656:        if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) {
                    657:                reason = "Refused by certificate options";
                    658:                goto fail_reason;
                    659:        }
                    660:        if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL)
                    661:                goto fail_reason;
                    662:
                    663:        /*
                    664:         * If the user has specified a list of principals as
                    665:         * a key option, then prefer that list to matching
                    666:         * their username in the certificate principals list.
                    667:         */
                    668:        if (keyopts->cert_principals != NULL &&
                    669:            !match_principals_option(keyopts->cert_principals, key->cert)) {
                    670:                reason = "Certificate does not contain an authorized principal";
                    671:                goto fail_reason;
                    672:        }
1.105     djm       673:        if (sshkey_cert_check_authority(key, 0, 0, 0,
1.107   ! djm       674:            keyopts->cert_principals == NULL ? pw->pw_name : NULL,
        !           675:            &reason) != 0)
1.77      djm       676:                goto fail_reason;
                    677:
                    678:        verbose("Accepted certificate ID \"%s\" (serial %llu) "
                    679:            "signed by CA %s %s found at %s",
                    680:            key->cert->key_id,
                    681:            (unsigned long long)key->cert->serial,
                    682:            sshkey_type(found), fp, loc);
                    683:
                    684:  success:
                    685:        if (finalopts == NULL)
1.101     djm       686:                fatal_f("internal error: missing options");
1.77      djm       687:        if (authoptsp != NULL) {
                    688:                *authoptsp = finalopts;
                    689:                finalopts = NULL;
                    690:        }
                    691:        /* success */
                    692:        ret = 0;
                    693:        goto out;
                    694:
                    695:  fail_reason:
                    696:        error("%s", reason);
                    697:        auth_debug_add("%s", reason);
                    698:  out:
                    699:        free(fp);
                    700:        sshauthopt_free(keyopts);
                    701:        sshauthopt_free(certopts);
                    702:        sshauthopt_free(finalopts);
                    703:        sshkey_free(found);
                    704:        return ret;
                    705: }
                    706:
1.51      djm       707: /*
1.31      djm       708:  * Checks whether key is allowed in authorized_keys-format file,
                    709:  * returns 1 if the key is allowed or 0 otherwise.
                    710:  */
1.1       markus    711: static int
1.77      djm       712: check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f,
                    713:     char *file, struct sshkey *key, struct sshauthopt **authoptsp)
1.1       markus    714: {
1.79      markus    715:        char *cp, *line = NULL, loc[256];
                    716:        size_t linesize = 0;
1.18      dtucker   717:        int found_key = 0;
1.1       markus    718:        u_long linenum = 0;
1.77      djm       719:
                    720:        if (authoptsp != NULL)
                    721:                *authoptsp = NULL;
1.1       markus    722:
1.79      markus    723:        while (getline(&line, &linesize, f) != -1) {
                    724:                linenum++;
1.71      djm       725:                /* Always consume entire file */
1.62      djm       726:                if (found_key)
                    727:                        continue;
1.20      djm       728:
1.1       markus    729:                /* Skip leading whitespace, empty and comment lines. */
1.77      djm       730:                cp = line;
                    731:                skip_space(&cp);
1.1       markus    732:                if (!*cp || *cp == '\n' || *cp == '#')
                    733:                        continue;
1.77      djm       734:                snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
                    735:                if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0)
1.20      djm       736:                        found_key = 1;
1.1       markus    737:        }
1.79      markus    738:        free(line);
1.1       markus    739:        return found_key;
                    740: }
                    741:
1.21      djm       742: /* Authenticate a certificate key against TrustedUserCAKeys */
                    743: static int
1.77      djm       744: user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
                    745:     struct sshauthopt **authoptsp)
1.21      djm       746: {
1.24      djm       747:        char *ca_fp, *principals_file = NULL;
1.21      djm       748:        const char *reason;
1.77      djm       749:        struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
                    750:        struct sshauthopt *final_opts = NULL;
1.64      markus    751:        int r, ret = 0, found_principal = 0, use_authorized_principals;
1.21      djm       752:
1.77      djm       753:        if (authoptsp != NULL)
                    754:                *authoptsp = NULL;
                    755:
1.64      markus    756:        if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
1.21      djm       757:                return 0;
                    758:
1.46      djm       759:        if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
                    760:            options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
                    761:                return 0;
1.21      djm       762:
1.64      markus    763:        if ((r = sshkey_in_file(key->cert->signature_key,
                    764:            options.trusted_user_ca_keys, 1, 0)) != 0) {
1.101     djm       765:                debug2_fr(r, "CA %s %s is not listed in %s",
1.64      markus    766:                    sshkey_type(key->cert->signature_key), ca_fp,
1.101     djm       767:                    options.trusted_user_ca_keys);
1.21      djm       768:                goto out;
                    769:        }
1.24      djm       770:        /*
                    771:         * If AuthorizedPrincipals is in use, then compare the certificate
                    772:         * principals against the names in that file rather than matching
                    773:         * against the username.
                    774:         */
                    775:        if ((principals_file = authorized_principals_file(pw)) != NULL) {
1.77      djm       776:                if (match_principals_file(ssh, pw, principals_file,
                    777:                    key->cert, &principals_opts))
1.51      djm       778:                        found_principal = 1;
                    779:        }
                    780:        /* Try querying command if specified */
1.77      djm       781:        if (!found_principal && match_principals_command(ssh, pw, key,
                    782:            &principals_opts))
1.51      djm       783:                found_principal = 1;
1.53      jsing     784:        /* If principals file or command is specified, then require a match */
                    785:        use_authorized_principals = principals_file != NULL ||
1.107   ! djm       786:            options.authorized_principals_command != NULL;
1.53      jsing     787:        if (!found_principal && use_authorized_principals) {
1.51      djm       788:                reason = "Certificate does not contain an authorized principal";
1.77      djm       789:                goto fail_reason;
1.21      djm       790:        }
1.77      djm       791:        if (use_authorized_principals && principals_opts == NULL)
1.101     djm       792:                fatal_f("internal error: missing principals_opts");
1.105     djm       793:        if (sshkey_cert_check_authority(key, 0, 1, 0,
1.53      jsing     794:            use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
1.24      djm       795:                goto fail_reason;
1.77      djm       796:
                    797:        /* Check authority from options in key and from principals file/cmd */
                    798:        if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
                    799:                reason = "Invalid certificate options";
                    800:                goto fail_reason;
                    801:        }
                    802:        if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) {
                    803:                reason = "Refused by certificate options";
1.60      djm       804:                goto fail_reason;
1.77      djm       805:        }
                    806:        if (principals_opts == NULL) {
                    807:                final_opts = cert_opts;
                    808:                cert_opts = NULL;
                    809:        } else {
                    810:                if (auth_authorise_keyopts(ssh, pw, principals_opts, 0,
                    811:                    "principals") != 0) {
                    812:                        reason = "Refused by certificate principals options";
                    813:                        goto fail_reason;
                    814:                }
                    815:                if ((final_opts = sshauthopt_merge(principals_opts,
                    816:                    cert_opts, &reason)) == NULL) {
                    817:  fail_reason:
                    818:                        error("%s", reason);
                    819:                        auth_debug_add("%s", reason);
                    820:                        goto out;
                    821:                }
                    822:        }
1.21      djm       823:
1.77      djm       824:        /* Success */
1.54      djm       825:        verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
                    826:            "%s CA %s via %s", key->cert->key_id,
                    827:            (unsigned long long)key->cert->serial,
1.64      markus    828:            sshkey_type(key->cert->signature_key), ca_fp,
1.22      djm       829:            options.trusted_user_ca_keys);
1.77      djm       830:        if (authoptsp != NULL) {
                    831:                *authoptsp = final_opts;
                    832:                final_opts = NULL;
                    833:        }
1.21      djm       834:        ret = 1;
                    835:  out:
1.77      djm       836:        sshauthopt_free(principals_opts);
                    837:        sshauthopt_free(cert_opts);
                    838:        sshauthopt_free(final_opts);
1.36      djm       839:        free(principals_file);
                    840:        free(ca_fp);
1.21      djm       841:        return ret;
                    842: }
                    843:
1.31      djm       844: /*
                    845:  * Checks whether key is allowed in file.
                    846:  * returns 1 if the key is allowed or 0 otherwise.
                    847:  */
                    848: static int
1.77      djm       849: user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
                    850:     char *file, struct sshauthopt **authoptsp)
1.31      djm       851: {
                    852:        FILE *f;
                    853:        int found_key = 0;
                    854:
1.77      djm       855:        if (authoptsp != NULL)
                    856:                *authoptsp = NULL;
                    857:
1.31      djm       858:        /* Temporarily use the user's uid. */
                    859:        temporarily_use_uid(pw);
                    860:
                    861:        debug("trying public key file %s", file);
                    862:        if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
1.77      djm       863:                found_key = check_authkeys_file(ssh, pw, f, file,
                    864:                    key, authoptsp);
1.31      djm       865:                fclose(f);
                    866:        }
                    867:
                    868:        restore_uid();
                    869:        return found_key;
                    870: }
                    871:
                    872: /*
                    873:  * Checks whether key is allowed in output of command.
                    874:  * returns 1 if the key is allowed or 0 otherwise.
                    875:  */
                    876: static int
1.77      djm       877: user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw,
                    878:     struct sshkey *key, struct sshauthopt **authoptsp)
1.31      djm       879: {
1.77      djm       880:        struct passwd *runas_pw = NULL;
1.50      djm       881:        FILE *f = NULL;
                    882:        int r, ok, found_key = 0;
                    883:        int i, uid_swapped = 0, ac = 0;
1.31      djm       884:        pid_t pid;
1.50      djm       885:        char *username = NULL, *key_fp = NULL, *keytext = NULL;
1.78      djm       886:        char uidstr[32], *tmp, *command = NULL, **av = NULL;
1.50      djm       887:        void (*osigchld)(int);
1.31      djm       888:
1.77      djm       889:        if (authoptsp != NULL)
                    890:                *authoptsp = NULL;
1.50      djm       891:        if (options.authorized_keys_command == NULL)
1.31      djm       892:                return 0;
1.32      djm       893:        if (options.authorized_keys_command_user == NULL) {
                    894:                error("No user for AuthorizedKeysCommand specified, skipping");
                    895:                return 0;
                    896:        }
                    897:
1.50      djm       898:        /*
                    899:         * NB. all returns later this function should go via "out" to
                    900:         * ensure the original SIGCHLD handler is restored properly.
                    901:         */
1.98      dtucker   902:        osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.50      djm       903:
                    904:        /* Prepare and verify the user for the command */
1.32      djm       905:        username = percent_expand(options.authorized_keys_command_user,
                    906:            "u", user_pw->pw_name, (char *)NULL);
1.77      djm       907:        runas_pw = getpwnam(username);
                    908:        if (runas_pw == NULL) {
1.34      djm       909:                error("AuthorizedKeysCommandUser \"%s\" not found: %s",
                    910:                    username, strerror(errno));
1.50      djm       911:                goto out;
1.31      djm       912:        }
                    913:
1.50      djm       914:        /* Prepare AuthorizedKeysCommand */
                    915:        if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    916:            SSH_FP_DEFAULT)) == NULL) {
1.101     djm       917:                error_f("sshkey_fingerprint failed");
1.31      djm       918:                goto out;
                    919:        }
1.50      djm       920:        if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1.101     djm       921:                error_fr(r, "sshkey_to_base64 failed");
1.31      djm       922:                goto out;
                    923:        }
                    924:
1.50      djm       925:        /* Turn the command into an argument vector */
1.69      djm       926:        if (argv_split(options.authorized_keys_command, &ac, &av) != 0) {
1.50      djm       927:                error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
1.102     djm       928:                    options.authorized_keys_command);
1.50      djm       929:                goto out;
                    930:        }
                    931:        if (ac == 0) {
                    932:                error("AuthorizedKeysCommand \"%s\" yielded no arguments",
1.102     djm       933:                    options.authorized_keys_command);
1.31      djm       934:                goto out;
                    935:        }
1.78      djm       936:        snprintf(uidstr, sizeof(uidstr), "%llu",
                    937:            (unsigned long long)user_pw->pw_uid);
1.50      djm       938:        for (i = 1; i < ac; i++) {
                    939:                tmp = percent_expand(av[i],
1.78      djm       940:                    "U", uidstr,
1.50      djm       941:                    "u", user_pw->pw_name,
                    942:                    "h", user_pw->pw_dir,
                    943:                    "t", sshkey_ssh_name(key),
                    944:                    "f", key_fp,
                    945:                    "k", keytext,
                    946:                    (char *)NULL);
                    947:                if (tmp == NULL)
1.101     djm       948:                        fatal_f("percent_expand failed");
1.50      djm       949:                free(av[i]);
                    950:                av[i] = tmp;
                    951:        }
                    952:        /* Prepare a printable command for logs, etc. */
1.69      djm       953:        command = argv_assemble(ac, av);
1.31      djm       954:
                    955:        /*
1.50      djm       956:         * If AuthorizedKeysCommand was run without arguments
                    957:         * then fall back to the old behaviour of passing the
                    958:         * target username as a single argument.
1.31      djm       959:         */
1.50      djm       960:        if (ac == 1) {
                    961:                av = xreallocarray(av, ac + 2, sizeof(*av));
                    962:                av[1] = xstrdup(user_pw->pw_name);
                    963:                av[2] = NULL;
                    964:                /* Fix up command too, since it is used in log messages */
                    965:                free(command);
                    966:                xasprintf(&command, "%s %s", av[0], av[1]);
                    967:        }
1.31      djm       968:
1.103     djm       969:        if ((pid = subprocess("AuthorizedKeysCommand", command,
1.69      djm       970:            ac, av, &f,
1.103     djm       971:            SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
                    972:            runas_pw, temporarily_use_uid, restore_uid)) == 0)
1.50      djm       973:                goto out;
1.31      djm       974:
1.50      djm       975:        uid_swapped = 1;
1.77      djm       976:        temporarily_use_uid(runas_pw);
1.31      djm       977:
1.77      djm       978:        ok = check_authkeys_file(ssh, user_pw, f,
                    979:            options.authorized_keys_command, key, authoptsp);
1.61      djm       980:
                    981:        fclose(f);
                    982:        f = NULL;
1.31      djm       983:
1.70      djm       984:        if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
1.31      djm       985:                goto out;
1.50      djm       986:
                    987:        /* Read completed successfully */
1.31      djm       988:        found_key = ok;
                    989:  out:
1.50      djm       990:        if (f != NULL)
                    991:                fclose(f);
1.98      dtucker   992:        ssh_signal(SIGCHLD, osigchld);
1.50      djm       993:        for (i = 0; i < ac; i++)
                    994:                free(av[i]);
                    995:        free(av);
                    996:        if (uid_swapped)
                    997:                restore_uid();
                    998:        free(command);
                    999:        free(username);
                   1000:        free(key_fp);
                   1001:        free(keytext);
1.31      djm      1002:        return found_key;
                   1003: }
                   1004:
                   1005: /*
                   1006:  * Check whether key authenticates and authorises the user.
                   1007:  */
1.1       markus   1008: int
1.77      djm      1009: user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
                   1010:     int auth_attempt, struct sshauthopt **authoptsp)
1.1       markus   1011: {
1.89      djm      1012:        u_int success = 0, i;
1.1       markus   1013:        char *file;
1.77      djm      1014:        struct sshauthopt *opts = NULL;
1.89      djm      1015:
1.77      djm      1016:        if (authoptsp != NULL)
                   1017:                *authoptsp = NULL;
1.21      djm      1018:
                   1019:        if (auth_key_is_revoked(key))
                   1020:                return 0;
1.64      markus   1021:        if (sshkey_is_cert(key) &&
                   1022:            auth_key_is_revoked(key->cert->signature_key))
1.21      djm      1023:                return 0;
                   1024:
1.89      djm      1025:        for (i = 0; !success && i < options.num_authkeys_files; i++) {
                   1026:                if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
                   1027:                        continue;
                   1028:                file = expand_authorized_keys(
                   1029:                    options.authorized_keys_files[i], pw);
                   1030:                success = user_key_allowed2(ssh, pw, key, file, &opts);
                   1031:                free(file);
                   1032:                if (!success) {
                   1033:                        sshauthopt_free(opts);
                   1034:                        opts = NULL;
                   1035:                }
                   1036:        }
                   1037:        if (success)
                   1038:                goto out;
                   1039:
1.77      djm      1040:        if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0)
                   1041:                goto out;
                   1042:        sshauthopt_free(opts);
                   1043:        opts = NULL;
                   1044:
                   1045:        if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0)
                   1046:                goto out;
                   1047:        sshauthopt_free(opts);
                   1048:        opts = NULL;
1.1       markus   1049:
1.77      djm      1050:  out:
                   1051:        if (success && authoptsp != NULL) {
                   1052:                *authoptsp = opts;
                   1053:                opts = NULL;
                   1054:        }
                   1055:        sshauthopt_free(opts);
1.1       markus   1056:        return success;
                   1057: }
1.2       markus   1058:
                   1059: Authmethod method_pubkey = {
                   1060:        "publickey",
                   1061:        userauth_pubkey,
                   1062:        &options.pubkey_authentication
                   1063: };