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

1.115   ! djm         1: /* $OpenBSD: auth2-pubkey.c,v 1.114 2022/05/27 05:01:25 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.115   ! djm         4:  * Copyright (c) 2010 Damien Miller.  All rights reserved.
1.1       markus      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     17:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     18:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     19:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     20:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     21:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     22:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     23:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     24:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
1.10      stevesk    27:
                     28: #include <sys/types.h>
1.13      stevesk    29:
1.94      djm        30: #include <stdlib.h>
1.31      djm        31: #include <errno.h>
                     32: #include <paths.h>
1.13      stevesk    33: #include <pwd.h>
1.31      djm        34: #include <signal.h>
1.14      stevesk    35: #include <stdio.h>
1.15      deraadt    36: #include <stdarg.h>
1.20      djm        37: #include <string.h>
                     38: #include <time.h>
1.17      dtucker    39: #include <unistd.h>
1.44      djm        40: #include <limits.h>
1.1       markus     41:
1.15      deraadt    42: #include "xmalloc.h"
1.8       dtucker    43: #include "ssh.h"
1.1       markus     44: #include "ssh2.h"
                     45: #include "packet.h"
1.106     djm        46: #include "kex.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:
1.73      djm        73: static char *
                     74: format_key(const struct sshkey *key)
                     75: {
                     76:        char *ret, *fp = sshkey_fingerprint(key,
                     77:            options.fingerprint_hash, SSH_FP_DEFAULT);
                     78:
                     79:        xasprintf(&ret, "%s %s", sshkey_type(key), fp);
                     80:        free(fp);
                     81:        return ret;
                     82: }
                     83:
1.2       markus     84: static int
1.111     djm        85: userauth_pubkey(struct ssh *ssh, const char *method)
1.1       markus     86: {
1.65      markus     87:        Authctxt *authctxt = ssh->authctxt;
1.77      djm        88:        struct passwd *pw = authctxt->pw;
1.83      djm        89:        struct sshbuf *b = NULL;
1.112     djm        90:        struct sshkey *key = NULL, *hostkey = NULL;
1.83      djm        91:        char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
                     92:        u_char *pkblob = NULL, *sig = NULL, have_sig;
1.64      markus     93:        size_t blen, slen;
1.112     djm        94:        int hostbound, r, pktype;
1.100     djm        95:        int req_presence = 0, req_verify = 0, authenticated = 0;
1.77      djm        96:        struct sshauthopt *authopts = NULL;
1.95      djm        97:        struct sshkey_sig_details *sig_details = NULL;
1.114     djm        98:        const char *remote_ip = ssh_remote_ipaddr(ssh);
                     99:        const char *remote_host = auth_get_canonical_hostname(ssh,
                    100:            options.use_dns);
1.1       markus    101:
1.112     djm       102:        hostbound = strcmp(method, "publickey-hostbound-v00@openssh.com") == 0;
                    103:
1.75      djm       104:        if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
                    105:            (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
                    106:            (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
1.112     djm       107:                fatal_fr(r, "parse %s packet", method);
                    108:
                    109:        /* hostbound auth includes the hostkey offered at initial KEX */
                    110:        if (hostbound) {
                    111:                if ((r = sshpkt_getb_froms(ssh, &b)) != 0 ||
                    112:                    (r = sshkey_fromb(b, &hostkey)) != 0)
                    113:                        fatal_fr(r, "parse %s hostkey", method);
                    114:                if (ssh->kex->initial_hostkey == NULL)
                    115:                        fatal_f("internal error: initial hostkey not recorded");
                    116:                if (!sshkey_equal(hostkey, ssh->kex->initial_hostkey))
                    117:                        fatal_f("%s packet contained wrong host key", method);
                    118:                sshbuf_free(b);
                    119:                b = NULL;
                    120:        }
1.87      djm       121:
                    122:        if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) {
                    123:                char *keystring;
                    124:                struct sshbuf *pkbuf;
                    125:
                    126:                if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL)
1.101     djm       127:                        fatal_f("sshbuf_from failed");
1.91      djm       128:                if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL)
1.101     djm       129:                        fatal_f("sshbuf_dtob64 failed");
                    130:                debug2_f("%s user %s %s public key %s %s",
1.87      djm       131:                    authctxt->valid ? "valid" : "invalid", authctxt->user,
                    132:                    have_sig ? "attempting" : "querying", pkalg, keystring);
                    133:                sshbuf_free(pkbuf);
                    134:                free(keystring);
                    135:        }
                    136:
1.64      markus    137:        pktype = sshkey_type_from_name(pkalg);
1.1       markus    138:        if (pktype == KEY_UNSPEC) {
                    139:                /* this is perfectly legal */
1.101     djm       140:                verbose_f("unsupported public key algorithm: %s", pkalg);
1.1       markus    141:                goto done;
                    142:        }
1.64      markus    143:        if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
1.101     djm       144:                error_fr(r, "parse key");
1.64      markus    145:                goto done;
                    146:        }
1.1       markus    147:        if (key == NULL) {
1.101     djm       148:                error_f("cannot decode key: %s", pkalg);
1.1       markus    149:                goto done;
                    150:        }
                    151:        if (key->type != pktype) {
1.101     djm       152:                error_f("type mismatch for decoded key "
                    153:                    "(received %d, expected %d)", key->type, pktype);
1.39      djm       154:                goto done;
                    155:        }
1.64      markus    156:        if (sshkey_type_plain(key->type) == KEY_RSA &&
                    157:            (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
1.39      djm       158:                logit("Refusing RSA key because client uses unsafe "
                    159:                    "signature scheme");
1.1       markus    160:                goto done;
                    161:        }
1.68      djm       162:        if (auth2_key_already_used(authctxt, key)) {
1.64      markus    163:                logit("refusing previously-used %s key", sshkey_type(key));
1.44      djm       164:                goto done;
                    165:        }
1.104     dtucker   166:        if (match_pattern_list(pkalg, options.pubkey_accepted_algos, 0) != 1) {
1.113     naddy     167:                logit_f("signature algorithm %s not in "
                    168:                    "PubkeyAcceptedAlgorithms", pkalg);
1.45      djm       169:                goto done;
                    170:        }
1.86      djm       171:        if ((r = sshkey_check_cert_sigtype(key,
                    172:            options.ca_sign_algorithms)) != 0) {
1.101     djm       173:                logit_fr(r, "certificate signature algorithm %s",
1.86      djm       174:                    (key->cert == NULL || key->cert->signature_type == NULL) ?
1.101     djm       175:                    "(null)" : key->cert->signature_type);
1.86      djm       176:                goto done;
                    177:        }
1.73      djm       178:        key_s = format_key(key);
                    179:        if (sshkey_is_cert(key))
                    180:                ca_s = format_key(key->cert->signature_key);
                    181:
1.1       markus    182:        if (have_sig) {
1.112     djm       183:                debug3_f("%s have %s signature for %s%s%s",
                    184:                    method, pkalg, key_s,
1.101     djm       185:                    ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
1.64      markus    186:                if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
                    187:                    (r = sshpkt_get_end(ssh)) != 0)
1.101     djm       188:                        fatal_fr(r, "parse signature packet");
1.64      markus    189:                if ((b = sshbuf_new()) == NULL)
1.101     djm       190:                        fatal_f("sshbuf_new failed");
1.64      markus    191:                if (ssh->compat & SSH_OLD_SESSIONID) {
1.106     djm       192:                        if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
1.101     djm       193:                                fatal_fr(r, "put old session id");
1.1       markus    194:                } else {
1.106     djm       195:                        if ((r = sshbuf_put_stringb(b,
                    196:                            ssh->kex->session_id)) != 0)
1.101     djm       197:                                fatal_fr(r, "put session id");
1.1       markus    198:                }
1.83      djm       199:                if (!authctxt->valid || authctxt->user == NULL) {
1.101     djm       200:                        debug2_f("disabled because of invalid user");
1.83      djm       201:                        goto done;
                    202:                }
1.1       markus    203:                /* reconstruct packet */
1.35      djm       204:                xasprintf(&userstyle, "%s%s%s", authctxt->user,
                    205:                    authctxt->style ? ":" : "",
                    206:                    authctxt->style ? authctxt->style : "");
1.64      markus    207:                if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                    208:                    (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
1.75      djm       209:                    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.111     djm       210:                    (r = sshbuf_put_cstring(b, method)) != 0 ||
1.75      djm       211:                    (r = sshbuf_put_u8(b, have_sig)) != 0 ||
1.85      mestre    212:                    (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
1.75      djm       213:                    (r = sshbuf_put_string(b, pkblob, blen)) != 0)
1.112     djm       214:                        fatal_fr(r, "reconstruct %s packet", method);
                    215:                if (hostbound &&
                    216:                    (r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0)
                    217:                        fatal_fr(r, "reconstruct %s packet", method);
1.1       markus    218: #ifdef DEBUG_PK
1.64      markus    219:                sshbuf_dump(b, stderr);
1.1       markus    220: #endif
                    221:                /* test for correct signature */
                    222:                authenticated = 0;
1.114     djm       223:                if (PRIVSEP(user_key_allowed(pw, key, 1, remote_ip,
                    224:                    remote_host, &authopts)) &&
1.80      djm       225:                    PRIVSEP(sshkey_verify(key, sig, slen,
                    226:                    sshbuf_ptr(b), sshbuf_len(b),
                    227:                    (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
1.95      djm       228:                    ssh->compat, &sig_details)) == 0) {
1.1       markus    229:                        authenticated = 1;
1.44      djm       230:                }
1.96      djm       231:                if (authenticated == 1 && sig_details != NULL) {
                    232:                        auth2_record_info(authctxt, "signature count = %u",
                    233:                            sig_details->sk_counter);
1.101     djm       234:                        debug_f("sk_counter = %u, sk_flags = 0x%02x",
                    235:                            sig_details->sk_counter, sig_details->sk_flags);
1.96      djm       236:                        req_presence = (options.pubkey_auth_options &
1.97      djm       237:                            PUBKEYAUTH_TOUCH_REQUIRED) ||
                    238:                            !authopts->no_require_user_presence;
1.96      djm       239:                        if (req_presence && (sig_details->sk_flags &
                    240:                            SSH_SK_USER_PRESENCE_REQD) == 0) {
                    241:                                error("public key %s signature for %s%s from "
                    242:                                    "%.128s port %d rejected: user presence "
1.99      naddy     243:                                    "(authenticator touch) requirement "
                    244:                                    "not met ", key_s,
1.100     djm       245:                                    authctxt->valid ? "" : "invalid user ",
                    246:                                    authctxt->user, ssh_remote_ipaddr(ssh),
                    247:                                    ssh_remote_port(ssh));
                    248:                                authenticated = 0;
                    249:                                goto done;
                    250:                        }
                    251:                        req_verify = (options.pubkey_auth_options &
                    252:                            PUBKEYAUTH_VERIFY_REQUIRED) ||
                    253:                            authopts->require_verify;
                    254:                        if (req_verify && (sig_details->sk_flags &
                    255:                            SSH_SK_USER_VERIFICATION_REQD) == 0) {
                    256:                                error("public key %s signature for %s%s from "
                    257:                                    "%.128s port %d rejected: user "
                    258:                                    "verification requirement not met ", key_s,
1.96      djm       259:                                    authctxt->valid ? "" : "invalid user ",
                    260:                                    authctxt->user, ssh_remote_ipaddr(ssh),
                    261:                                    ssh_remote_port(ssh));
                    262:                                authenticated = 0;
                    263:                                goto done;
                    264:                        }
1.95      djm       265:                }
1.68      djm       266:                auth2_record_key(authctxt, authenticated, key);
1.1       markus    267:        } else {
1.112     djm       268:                debug_f("%s test pkalg %s pkblob %s%s%s", method, pkalg, key_s,
1.101     djm       269:                    ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
1.73      djm       270:
1.64      markus    271:                if ((r = sshpkt_get_end(ssh)) != 0)
1.101     djm       272:                        fatal_fr(r, "parse packet");
1.1       markus    273:
1.83      djm       274:                if (!authctxt->valid || authctxt->user == NULL) {
1.101     djm       275:                        debug2_f("disabled because of invalid user");
1.83      djm       276:                        goto done;
                    277:                }
1.1       markus    278:                /* XXX fake reply and always send PK_OK ? */
                    279:                /*
                    280:                 * XXX this allows testing whether a user is allowed
                    281:                 * to login: if you happen to have a valid pubkey this
                    282:                 * message is sent. the message is NEVER sent at all
                    283:                 * if a user is not allowed to login. is this an
                    284:                 * issue? -markus
                    285:                 */
1.114     djm       286:                if (PRIVSEP(user_key_allowed(pw, key, 0, remote_ip,
                    287:                    remote_host, NULL))) {
1.64      markus    288:                        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
                    289:                            != 0 ||
                    290:                            (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
                    291:                            (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
1.82      markus    292:                            (r = sshpkt_send(ssh)) != 0 ||
                    293:                            (r = ssh_packet_write_wait(ssh)) != 0)
1.101     djm       294:                                fatal_fr(r, "send packet");
1.1       markus    295:                        authctxt->postponed = 1;
                    296:                }
                    297:        }
                    298: done:
1.77      djm       299:        if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
1.101     djm       300:                debug_f("key options inconsistent with existing");
1.77      djm       301:                authenticated = 0;
                    302:        }
1.101     djm       303:        debug2_f("authenticated %d pkalg %s", authenticated, pkalg);
1.77      djm       304:
1.84      djm       305:        sshbuf_free(b);
1.77      djm       306:        sshauthopt_free(authopts);
1.68      djm       307:        sshkey_free(key);
1.112     djm       308:        sshkey_free(hostkey);
1.64      markus    309:        free(userstyle);
1.36      djm       310:        free(pkalg);
                    311:        free(pkblob);
1.73      djm       312:        free(key_s);
                    313:        free(ca_s);
1.83      djm       314:        free(sig);
1.95      djm       315:        sshkey_sig_details_free(sig_details);
1.1       markus    316:        return authenticated;
                    317: }
                    318:
1.24      djm       319: static int
1.114     djm       320: match_principals_file(struct passwd *pw, char *file,
1.77      djm       321:     struct sshkey_cert *cert, struct sshauthopt **authoptsp)
1.51      djm       322: {
                    323:        FILE *f;
                    324:        int success;
                    325:
1.77      djm       326:        if (authoptsp != NULL)
                    327:                *authoptsp = NULL;
                    328:
1.51      djm       329:        temporarily_use_uid(pw);
                    330:        debug("trying authorized principals file %s", file);
                    331:        if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
                    332:                restore_uid();
                    333:                return 0;
                    334:        }
1.115   ! djm       335:        success = auth_process_principals(f, file, cert, authoptsp);
1.24      djm       336:        fclose(f);
                    337:        restore_uid();
1.51      djm       338:        return success;
1.31      djm       339: }
1.24      djm       340:
1.31      djm       341: /*
1.51      djm       342:  * Checks whether principal is allowed in output of command.
                    343:  * returns 1 if the principal is allowed or 0 otherwise.
                    344:  */
                    345: static int
1.114     djm       346: match_principals_command(struct passwd *user_pw,
1.77      djm       347:     const struct sshkey *key, struct sshauthopt **authoptsp)
1.51      djm       348: {
1.77      djm       349:        struct passwd *runas_pw = NULL;
1.56      djm       350:        const struct sshkey_cert *cert = key->cert;
1.51      djm       351:        FILE *f = NULL;
1.56      djm       352:        int r, ok, found_principal = 0;
1.51      djm       353:        int i, ac = 0, uid_swapped = 0;
                    354:        pid_t pid;
                    355:        char *tmp, *username = NULL, *command = NULL, **av = NULL;
1.56      djm       356:        char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
1.88      djm       357:        char serial_s[32], uidstr[32];
1.51      djm       358:        void (*osigchld)(int);
                    359:
1.77      djm       360:        if (authoptsp != NULL)
                    361:                *authoptsp = NULL;
1.51      djm       362:        if (options.authorized_principals_command == NULL)
                    363:                return 0;
                    364:        if (options.authorized_principals_command_user == NULL) {
                    365:                error("No user for AuthorizedPrincipalsCommand specified, "
                    366:                    "skipping");
                    367:                return 0;
                    368:        }
                    369:
                    370:        /*
                    371:         * NB. all returns later this function should go via "out" to
                    372:         * ensure the original SIGCHLD handler is restored properly.
                    373:         */
1.98      dtucker   374:        osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.51      djm       375:
                    376:        /* Prepare and verify the user for the command */
                    377:        username = percent_expand(options.authorized_principals_command_user,
                    378:            "u", user_pw->pw_name, (char *)NULL);
1.77      djm       379:        runas_pw = getpwnam(username);
                    380:        if (runas_pw == NULL) {
1.51      djm       381:                error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
                    382:                    username, strerror(errno));
                    383:                goto out;
                    384:        }
                    385:
                    386:        /* Turn the command into an argument vector */
1.108     djm       387:        if (argv_split(options.authorized_principals_command,
                    388:            &ac, &av, 0) != 0) {
1.51      djm       389:                error("AuthorizedPrincipalsCommand \"%s\" contains "
1.90      djm       390:                    "invalid quotes", options.authorized_principals_command);
1.51      djm       391:                goto out;
                    392:        }
                    393:        if (ac == 0) {
                    394:                error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
1.90      djm       395:                    options.authorized_principals_command);
1.51      djm       396:                goto out;
                    397:        }
1.56      djm       398:        if ((ca_fp = sshkey_fingerprint(cert->signature_key,
                    399:            options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
1.101     djm       400:                error_f("sshkey_fingerprint failed");
1.56      djm       401:                goto out;
                    402:        }
1.57      djm       403:        if ((key_fp = sshkey_fingerprint(key,
1.56      djm       404:            options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
1.101     djm       405:                error_f("sshkey_fingerprint failed");
1.56      djm       406:                goto out;
                    407:        }
                    408:        if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
1.101     djm       409:                error_fr(r, "sshkey_to_base64 failed");
1.56      djm       410:                goto out;
                    411:        }
                    412:        if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1.101     djm       413:                error_fr(r, "sshkey_to_base64 failed");
1.56      djm       414:                goto out;
                    415:        }
1.59      djm       416:        snprintf(serial_s, sizeof(serial_s), "%llu",
                    417:            (unsigned long long)cert->serial);
1.78      djm       418:        snprintf(uidstr, sizeof(uidstr), "%llu",
                    419:            (unsigned long long)user_pw->pw_uid);
1.51      djm       420:        for (i = 1; i < ac; i++) {
                    421:                tmp = percent_expand(av[i],
1.78      djm       422:                    "U", uidstr,
1.51      djm       423:                    "u", user_pw->pw_name,
                    424:                    "h", user_pw->pw_dir,
1.56      djm       425:                    "t", sshkey_ssh_name(key),
                    426:                    "T", sshkey_ssh_name(cert->signature_key),
                    427:                    "f", key_fp,
                    428:                    "F", ca_fp,
                    429:                    "k", keytext,
                    430:                    "K", catext,
1.58      djm       431:                    "i", cert->key_id,
                    432:                    "s", serial_s,
1.51      djm       433:                    (char *)NULL);
                    434:                if (tmp == NULL)
1.101     djm       435:                        fatal_f("percent_expand failed");
1.51      djm       436:                free(av[i]);
                    437:                av[i] = tmp;
                    438:        }
                    439:        /* Prepare a printable command for logs, etc. */
1.69      djm       440:        command = argv_assemble(ac, av);
1.51      djm       441:
1.103     djm       442:        if ((pid = subprocess("AuthorizedPrincipalsCommand", command,
1.69      djm       443:            ac, av, &f,
1.103     djm       444:            SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
                    445:            runas_pw, temporarily_use_uid, restore_uid)) == 0)
1.51      djm       446:                goto out;
                    447:
                    448:        uid_swapped = 1;
1.77      djm       449:        temporarily_use_uid(runas_pw);
1.51      djm       450:
1.115   ! djm       451:        ok = auth_process_principals(f, "(command)", cert, authoptsp);
1.51      djm       452:
1.61      djm       453:        fclose(f);
                    454:        f = NULL;
                    455:
1.70      djm       456:        if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
1.51      djm       457:                goto out;
                    458:
                    459:        /* Read completed successfully */
                    460:        found_principal = ok;
                    461:  out:
                    462:        if (f != NULL)
                    463:                fclose(f);
1.98      dtucker   464:        ssh_signal(SIGCHLD, osigchld);
1.51      djm       465:        for (i = 0; i < ac; i++)
                    466:                free(av[i]);
                    467:        free(av);
                    468:        if (uid_swapped)
                    469:                restore_uid();
                    470:        free(command);
                    471:        free(username);
1.56      djm       472:        free(ca_fp);
                    473:        free(key_fp);
                    474:        free(catext);
                    475:        free(keytext);
1.51      djm       476:        return found_principal;
1.77      djm       477: }
                    478:
1.21      djm       479: /* Authenticate a certificate key against TrustedUserCAKeys */
                    480: static int
1.114     djm       481: user_cert_trusted_ca(struct passwd *pw, struct sshkey *key,
                    482:     const char *remote_ip, const char *remote_host,
1.77      djm       483:     struct sshauthopt **authoptsp)
1.21      djm       484: {
1.24      djm       485:        char *ca_fp, *principals_file = NULL;
1.21      djm       486:        const char *reason;
1.77      djm       487:        struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
                    488:        struct sshauthopt *final_opts = NULL;
1.64      markus    489:        int r, ret = 0, found_principal = 0, use_authorized_principals;
1.21      djm       490:
1.77      djm       491:        if (authoptsp != NULL)
                    492:                *authoptsp = NULL;
                    493:
1.64      markus    494:        if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
1.21      djm       495:                return 0;
                    496:
1.46      djm       497:        if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
                    498:            options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
                    499:                return 0;
1.21      djm       500:
1.64      markus    501:        if ((r = sshkey_in_file(key->cert->signature_key,
                    502:            options.trusted_user_ca_keys, 1, 0)) != 0) {
1.101     djm       503:                debug2_fr(r, "CA %s %s is not listed in %s",
1.64      markus    504:                    sshkey_type(key->cert->signature_key), ca_fp,
1.101     djm       505:                    options.trusted_user_ca_keys);
1.21      djm       506:                goto out;
                    507:        }
1.24      djm       508:        /*
                    509:         * If AuthorizedPrincipals is in use, then compare the certificate
                    510:         * principals against the names in that file rather than matching
                    511:         * against the username.
                    512:         */
                    513:        if ((principals_file = authorized_principals_file(pw)) != NULL) {
1.114     djm       514:                if (match_principals_file(pw, principals_file,
1.77      djm       515:                    key->cert, &principals_opts))
1.51      djm       516:                        found_principal = 1;
                    517:        }
                    518:        /* Try querying command if specified */
1.114     djm       519:        if (!found_principal && match_principals_command(pw, key,
1.77      djm       520:            &principals_opts))
1.51      djm       521:                found_principal = 1;
1.53      jsing     522:        /* If principals file or command is specified, then require a match */
                    523:        use_authorized_principals = principals_file != NULL ||
1.107     djm       524:            options.authorized_principals_command != NULL;
1.53      jsing     525:        if (!found_principal && use_authorized_principals) {
1.51      djm       526:                reason = "Certificate does not contain an authorized principal";
1.77      djm       527:                goto fail_reason;
1.21      djm       528:        }
1.77      djm       529:        if (use_authorized_principals && principals_opts == NULL)
1.101     djm       530:                fatal_f("internal error: missing principals_opts");
1.109     djm       531:        if (sshkey_cert_check_authority_now(key, 0, 1, 0,
1.53      jsing     532:            use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
1.24      djm       533:                goto fail_reason;
1.77      djm       534:
                    535:        /* Check authority from options in key and from principals file/cmd */
                    536:        if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
                    537:                reason = "Invalid certificate options";
                    538:                goto fail_reason;
                    539:        }
1.114     djm       540:        if (auth_authorise_keyopts(pw, cert_opts, 0,
                    541:            remote_ip, remote_host, "cert") != 0) {
1.77      djm       542:                reason = "Refused by certificate options";
1.60      djm       543:                goto fail_reason;
1.77      djm       544:        }
                    545:        if (principals_opts == NULL) {
                    546:                final_opts = cert_opts;
                    547:                cert_opts = NULL;
                    548:        } else {
1.114     djm       549:                if (auth_authorise_keyopts(pw, principals_opts, 0,
                    550:                    remote_ip, remote_host, "principals") != 0) {
1.77      djm       551:                        reason = "Refused by certificate principals options";
                    552:                        goto fail_reason;
                    553:                }
                    554:                if ((final_opts = sshauthopt_merge(principals_opts,
                    555:                    cert_opts, &reason)) == NULL) {
                    556:  fail_reason:
                    557:                        error("%s", reason);
                    558:                        auth_debug_add("%s", reason);
                    559:                        goto out;
                    560:                }
                    561:        }
1.21      djm       562:
1.77      djm       563:        /* Success */
1.54      djm       564:        verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
                    565:            "%s CA %s via %s", key->cert->key_id,
                    566:            (unsigned long long)key->cert->serial,
1.64      markus    567:            sshkey_type(key->cert->signature_key), ca_fp,
1.22      djm       568:            options.trusted_user_ca_keys);
1.77      djm       569:        if (authoptsp != NULL) {
                    570:                *authoptsp = final_opts;
                    571:                final_opts = NULL;
                    572:        }
1.21      djm       573:        ret = 1;
                    574:  out:
1.77      djm       575:        sshauthopt_free(principals_opts);
                    576:        sshauthopt_free(cert_opts);
                    577:        sshauthopt_free(final_opts);
1.36      djm       578:        free(principals_file);
                    579:        free(ca_fp);
1.21      djm       580:        return ret;
                    581: }
                    582:
1.31      djm       583: /*
                    584:  * Checks whether key is allowed in file.
                    585:  * returns 1 if the key is allowed or 0 otherwise.
                    586:  */
                    587: static int
1.114     djm       588: user_key_allowed2(struct passwd *pw, struct sshkey *key,
                    589:     char *file, const char *remote_ip, const char *remote_host,
                    590:     struct sshauthopt **authoptsp)
1.31      djm       591: {
                    592:        FILE *f;
                    593:        int found_key = 0;
                    594:
1.77      djm       595:        if (authoptsp != NULL)
                    596:                *authoptsp = NULL;
                    597:
1.31      djm       598:        /* Temporarily use the user's uid. */
                    599:        temporarily_use_uid(pw);
                    600:
                    601:        debug("trying public key file %s", file);
                    602:        if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
1.115   ! djm       603:                found_key = auth_check_authkeys_file(pw, f, file,
1.114     djm       604:                    key, remote_ip, remote_host, authoptsp);
1.31      djm       605:                fclose(f);
                    606:        }
                    607:
                    608:        restore_uid();
                    609:        return found_key;
                    610: }
                    611:
                    612: /*
                    613:  * Checks whether key is allowed in output of command.
                    614:  * returns 1 if the key is allowed or 0 otherwise.
                    615:  */
                    616: static int
1.114     djm       617: user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key,
                    618:     const char *remote_ip, const char *remote_host,
                    619:     struct sshauthopt **authoptsp)
1.31      djm       620: {
1.77      djm       621:        struct passwd *runas_pw = NULL;
1.50      djm       622:        FILE *f = NULL;
                    623:        int r, ok, found_key = 0;
                    624:        int i, uid_swapped = 0, ac = 0;
1.31      djm       625:        pid_t pid;
1.50      djm       626:        char *username = NULL, *key_fp = NULL, *keytext = NULL;
1.78      djm       627:        char uidstr[32], *tmp, *command = NULL, **av = NULL;
1.50      djm       628:        void (*osigchld)(int);
1.31      djm       629:
1.77      djm       630:        if (authoptsp != NULL)
                    631:                *authoptsp = NULL;
1.50      djm       632:        if (options.authorized_keys_command == NULL)
1.31      djm       633:                return 0;
1.32      djm       634:        if (options.authorized_keys_command_user == NULL) {
                    635:                error("No user for AuthorizedKeysCommand specified, skipping");
                    636:                return 0;
                    637:        }
                    638:
1.50      djm       639:        /*
                    640:         * NB. all returns later this function should go via "out" to
                    641:         * ensure the original SIGCHLD handler is restored properly.
                    642:         */
1.98      dtucker   643:        osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.50      djm       644:
                    645:        /* Prepare and verify the user for the command */
1.32      djm       646:        username = percent_expand(options.authorized_keys_command_user,
                    647:            "u", user_pw->pw_name, (char *)NULL);
1.77      djm       648:        runas_pw = getpwnam(username);
                    649:        if (runas_pw == NULL) {
1.34      djm       650:                error("AuthorizedKeysCommandUser \"%s\" not found: %s",
                    651:                    username, strerror(errno));
1.50      djm       652:                goto out;
1.31      djm       653:        }
                    654:
1.50      djm       655:        /* Prepare AuthorizedKeysCommand */
                    656:        if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    657:            SSH_FP_DEFAULT)) == NULL) {
1.101     djm       658:                error_f("sshkey_fingerprint failed");
1.31      djm       659:                goto out;
                    660:        }
1.50      djm       661:        if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1.101     djm       662:                error_fr(r, "sshkey_to_base64 failed");
1.31      djm       663:                goto out;
                    664:        }
                    665:
1.50      djm       666:        /* Turn the command into an argument vector */
1.108     djm       667:        if (argv_split(options.authorized_keys_command, &ac, &av, 0) != 0) {
1.50      djm       668:                error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
1.102     djm       669:                    options.authorized_keys_command);
1.50      djm       670:                goto out;
                    671:        }
                    672:        if (ac == 0) {
                    673:                error("AuthorizedKeysCommand \"%s\" yielded no arguments",
1.102     djm       674:                    options.authorized_keys_command);
1.31      djm       675:                goto out;
                    676:        }
1.78      djm       677:        snprintf(uidstr, sizeof(uidstr), "%llu",
                    678:            (unsigned long long)user_pw->pw_uid);
1.50      djm       679:        for (i = 1; i < ac; i++) {
                    680:                tmp = percent_expand(av[i],
1.78      djm       681:                    "U", uidstr,
1.50      djm       682:                    "u", user_pw->pw_name,
                    683:                    "h", user_pw->pw_dir,
                    684:                    "t", sshkey_ssh_name(key),
                    685:                    "f", key_fp,
                    686:                    "k", keytext,
                    687:                    (char *)NULL);
                    688:                if (tmp == NULL)
1.101     djm       689:                        fatal_f("percent_expand failed");
1.50      djm       690:                free(av[i]);
                    691:                av[i] = tmp;
                    692:        }
                    693:        /* Prepare a printable command for logs, etc. */
1.69      djm       694:        command = argv_assemble(ac, av);
1.31      djm       695:
                    696:        /*
1.50      djm       697:         * If AuthorizedKeysCommand was run without arguments
                    698:         * then fall back to the old behaviour of passing the
                    699:         * target username as a single argument.
1.31      djm       700:         */
1.50      djm       701:        if (ac == 1) {
                    702:                av = xreallocarray(av, ac + 2, sizeof(*av));
                    703:                av[1] = xstrdup(user_pw->pw_name);
                    704:                av[2] = NULL;
                    705:                /* Fix up command too, since it is used in log messages */
                    706:                free(command);
                    707:                xasprintf(&command, "%s %s", av[0], av[1]);
                    708:        }
1.31      djm       709:
1.103     djm       710:        if ((pid = subprocess("AuthorizedKeysCommand", command,
1.69      djm       711:            ac, av, &f,
1.103     djm       712:            SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
                    713:            runas_pw, temporarily_use_uid, restore_uid)) == 0)
1.50      djm       714:                goto out;
1.31      djm       715:
1.50      djm       716:        uid_swapped = 1;
1.77      djm       717:        temporarily_use_uid(runas_pw);
1.31      djm       718:
1.115   ! djm       719:        ok = auth_check_authkeys_file(user_pw, f,
1.114     djm       720:            options.authorized_keys_command, key, remote_ip,
                    721:            remote_host, authoptsp);
1.61      djm       722:
                    723:        fclose(f);
                    724:        f = NULL;
1.31      djm       725:
1.70      djm       726:        if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
1.31      djm       727:                goto out;
1.50      djm       728:
                    729:        /* Read completed successfully */
1.31      djm       730:        found_key = ok;
                    731:  out:
1.50      djm       732:        if (f != NULL)
                    733:                fclose(f);
1.98      dtucker   734:        ssh_signal(SIGCHLD, osigchld);
1.50      djm       735:        for (i = 0; i < ac; i++)
                    736:                free(av[i]);
                    737:        free(av);
                    738:        if (uid_swapped)
                    739:                restore_uid();
                    740:        free(command);
                    741:        free(username);
                    742:        free(key_fp);
                    743:        free(keytext);
1.31      djm       744:        return found_key;
                    745: }
                    746:
                    747: /*
                    748:  * Check whether key authenticates and authorises the user.
                    749:  */
1.1       markus    750: int
1.114     djm       751: user_key_allowed(struct passwd *pw, struct sshkey *key,
                    752:     int auth_attempt, const char *remote_ip, const char *remote_host,
                    753:     struct sshauthopt **authoptsp)
1.1       markus    754: {
1.89      djm       755:        u_int success = 0, i;
1.1       markus    756:        char *file;
1.77      djm       757:        struct sshauthopt *opts = NULL;
1.89      djm       758:
1.77      djm       759:        if (authoptsp != NULL)
                    760:                *authoptsp = NULL;
1.21      djm       761:
                    762:        if (auth_key_is_revoked(key))
                    763:                return 0;
1.64      markus    764:        if (sshkey_is_cert(key) &&
                    765:            auth_key_is_revoked(key->cert->signature_key))
1.21      djm       766:                return 0;
                    767:
1.89      djm       768:        for (i = 0; !success && i < options.num_authkeys_files; i++) {
                    769:                if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
                    770:                        continue;
                    771:                file = expand_authorized_keys(
                    772:                    options.authorized_keys_files[i], pw);
1.114     djm       773:                success = user_key_allowed2(pw, key, file,
                    774:                    remote_ip, remote_host, &opts);
1.89      djm       775:                free(file);
                    776:                if (!success) {
                    777:                        sshauthopt_free(opts);
                    778:                        opts = NULL;
                    779:                }
                    780:        }
                    781:        if (success)
                    782:                goto out;
                    783:
1.114     djm       784:        if ((success = user_cert_trusted_ca(pw, key, remote_ip, remote_host,
                    785:            &opts)) != 0)
1.77      djm       786:                goto out;
                    787:        sshauthopt_free(opts);
                    788:        opts = NULL;
                    789:
1.114     djm       790:        if ((success = user_key_command_allowed2(pw, key, remote_ip,
                    791:            remote_host, &opts)) != 0)
1.77      djm       792:                goto out;
                    793:        sshauthopt_free(opts);
                    794:        opts = NULL;
1.1       markus    795:
1.77      djm       796:  out:
                    797:        if (success && authoptsp != NULL) {
                    798:                *authoptsp = opts;
                    799:                opts = NULL;
                    800:        }
                    801:        sshauthopt_free(opts);
1.1       markus    802:        return success;
                    803: }
1.2       markus    804:
                    805: Authmethod method_pubkey = {
                    806:        "publickey",
1.112     djm       807:        "publickey-hostbound-v00@openssh.com",
1.2       markus    808:        userauth_pubkey,
                    809:        &options.pubkey_authentication
                    810: };