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

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