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