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

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