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