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