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