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