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