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