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

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