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