Annotation of src/usr.bin/ssh/auth2-pubkey.c, Revision 1.76
1.76 ! dtucker 1: /* $OpenBSD: auth2-pubkey.c,v 1.75 2018/01/23 05:27:21 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.64 markus 88: struct sshbuf *b;
1.63 markus 89: struct sshkey *key = NULL;
1.73 djm 90: char *pkalg, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
1.64 markus 91: u_char *pkblob, *sig, have_sig;
92: size_t blen, slen;
93: int r, pktype;
1.1 markus 94: int authenticated = 0;
95:
96: if (!authctxt->valid) {
1.55 djm 97: debug2("%s: disabled because of invalid user", __func__);
1.1 markus 98: return 0;
99: }
1.75 djm 100: if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
101: (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
102: (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
103: fatal("%s: parse request failed: %s", __func__, ssh_err(r));
1.64 markus 104: pktype = sshkey_type_from_name(pkalg);
1.1 markus 105: if (pktype == KEY_UNSPEC) {
106: /* this is perfectly legal */
1.55 djm 107: logit("%s: unsupported public key algorithm: %s",
108: __func__, pkalg);
1.1 markus 109: goto done;
110: }
1.64 markus 111: if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
112: error("%s: could not parse key: %s", __func__, ssh_err(r));
113: goto done;
114: }
1.1 markus 115: if (key == NULL) {
1.55 djm 116: error("%s: cannot decode key: %s", __func__, pkalg);
1.1 markus 117: goto done;
118: }
119: if (key->type != pktype) {
1.55 djm 120: error("%s: type mismatch for decoded key "
121: "(received %d, expected %d)", __func__, key->type, pktype);
1.39 djm 122: goto done;
123: }
1.64 markus 124: if (sshkey_type_plain(key->type) == KEY_RSA &&
125: (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
1.39 djm 126: logit("Refusing RSA key because client uses unsafe "
127: "signature scheme");
1.1 markus 128: goto done;
129: }
1.68 djm 130: if (auth2_key_already_used(authctxt, key)) {
1.64 markus 131: logit("refusing previously-used %s key", sshkey_type(key));
1.44 djm 132: goto done;
133: }
1.49 djm 134: if (match_pattern_list(sshkey_ssh_name(key),
135: options.pubkey_key_types, 0) != 1) {
1.45 djm 136: logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
137: __func__, sshkey_ssh_name(key));
138: goto done;
139: }
140:
1.73 djm 141: key_s = format_key(key);
142: if (sshkey_is_cert(key))
143: ca_s = format_key(key->cert->signature_key);
144:
1.1 markus 145: if (have_sig) {
1.73 djm 146: debug3("%s: have %s signature for %s%s%s",
147: __func__, pkalg, key_s,
148: ca_s == NULL ? "" : " CA ",
149: ca_s == NULL ? "" : ca_s);
1.64 markus 150: if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
151: (r = sshpkt_get_end(ssh)) != 0)
152: fatal("%s: %s", __func__, ssh_err(r));
153: if ((b = sshbuf_new()) == NULL)
154: fatal("%s: sshbuf_new failed", __func__);
155: if (ssh->compat & SSH_OLD_SESSIONID) {
156: if ((r = sshbuf_put(b, session_id2,
157: session_id2_len)) != 0)
158: fatal("%s: sshbuf_put session id: %s",
159: __func__, ssh_err(r));
1.1 markus 160: } else {
1.64 markus 161: if ((r = sshbuf_put_string(b, session_id2,
162: session_id2_len)) != 0)
163: fatal("%s: sshbuf_put_string session id: %s",
164: __func__, ssh_err(r));
1.1 markus 165: }
166: /* reconstruct packet */
1.35 djm 167: xasprintf(&userstyle, "%s%s%s", authctxt->user,
168: authctxt->style ? ":" : "",
169: authctxt->style ? authctxt->style : "");
1.64 markus 170: if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
171: (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
1.75 djm 172: (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
173: (r = sshbuf_put_cstring(b, "publickey")) != 0 ||
174: (r = sshbuf_put_u8(b, have_sig)) != 0 ||
175: (r = sshbuf_put_cstring(b, pkalg) != 0) ||
176: (r = sshbuf_put_string(b, pkblob, blen)) != 0)
1.64 markus 177: fatal("%s: build packet failed: %s",
178: __func__, ssh_err(r));
1.1 markus 179: #ifdef DEBUG_PK
1.64 markus 180: sshbuf_dump(b, stderr);
1.1 markus 181: #endif
1.37 djm 182:
1.1 markus 183: /* test for correct signature */
184: authenticated = 0;
1.48 djm 185: if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
1.64 markus 186: PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b),
1.74 djm 187: sshbuf_len(b), NULL, ssh->compat)) == 0) {
1.1 markus 188: authenticated = 1;
1.44 djm 189: }
1.64 markus 190: sshbuf_free(b);
1.36 djm 191: free(sig);
1.68 djm 192: auth2_record_key(authctxt, authenticated, key);
1.1 markus 193: } else {
1.73 djm 194: debug("%s: test pkalg %s pkblob %s%s%s",
195: __func__, pkalg, key_s,
196: ca_s == NULL ? "" : " CA ",
197: ca_s == NULL ? "" : ca_s);
198:
1.64 markus 199: if ((r = sshpkt_get_end(ssh)) != 0)
200: fatal("%s: %s", __func__, ssh_err(r));
1.1 markus 201:
202: /* XXX fake reply and always send PK_OK ? */
203: /*
204: * XXX this allows testing whether a user is allowed
205: * to login: if you happen to have a valid pubkey this
206: * message is sent. the message is NEVER sent at all
207: * if a user is not allowed to login. is this an
208: * issue? -markus
209: */
1.48 djm 210: if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) {
1.64 markus 211: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
212: != 0 ||
213: (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
214: (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
215: (r = sshpkt_send(ssh)) != 0)
216: fatal("%s: %s", __func__, ssh_err(r));
217: ssh_packet_write_wait(ssh);
1.1 markus 218: authctxt->postponed = 1;
219: }
220: }
221: if (authenticated != 1)
222: auth_clear_options();
223: done:
1.55 djm 224: debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
1.68 djm 225: sshkey_free(key);
1.64 markus 226: free(userstyle);
1.36 djm 227: free(pkalg);
228: free(pkblob);
1.73 djm 229: free(key_s);
230: free(ca_s);
1.1 markus 231: return authenticated;
232: }
233:
1.24 djm 234: static int
1.40 djm 235: match_principals_option(const char *principal_list, struct sshkey_cert *cert)
1.24 djm 236: {
237: char *result;
238: u_int i;
239:
240: /* XXX percent_expand() sequences for authorized_principals? */
241:
242: for (i = 0; i < cert->nprincipals; i++) {
243: if ((result = match_list(cert->principals[i],
244: principal_list, NULL)) != NULL) {
245: debug3("matched principal from key options \"%.100s\"",
246: result);
1.36 djm 247: free(result);
1.24 djm 248: return 1;
249: }
250: }
251: return 0;
252: }
253:
254: static int
1.67 markus 255: process_principals(FILE *f, const char *file, struct passwd *pw,
1.56 djm 256: const struct sshkey_cert *cert)
1.24 djm 257: {
1.26 djm 258: char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
1.24 djm 259: u_long linenum = 0;
1.62 djm 260: u_int i, found_principal = 0;
1.24 djm 261:
262: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.62 djm 263: /* Always consume entire input */
264: if (found_principal)
265: continue;
1.26 djm 266: /* Skip leading whitespace. */
1.24 djm 267: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
268: ;
1.26 djm 269: /* Skip blank and comment lines. */
270: if ((ep = strchr(cp, '#')) != NULL)
271: *ep = '\0';
272: if (!*cp || *cp == '\n')
1.24 djm 273: continue;
1.26 djm 274: /* Trim trailing whitespace. */
275: ep = cp + strlen(cp) - 1;
276: while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
277: *ep-- = '\0';
278: /*
279: * If the line has internal whitespace then assume it has
280: * key options.
281: */
282: line_opts = NULL;
283: if ((ep = strrchr(cp, ' ')) != NULL ||
284: (ep = strrchr(cp, '\t')) != NULL) {
285: for (; *ep == ' ' || *ep == '\t'; ep++)
1.27 deraadt 286: ;
1.26 djm 287: line_opts = cp;
288: cp = ep;
289: }
1.24 djm 290: for (i = 0; i < cert->nprincipals; i++) {
291: if (strcmp(cp, cert->principals[i]) == 0) {
1.51 djm 292: debug3("%s:%lu: matched principal \"%.100s\"",
1.67 markus 293: file, linenum, cert->principals[i]);
1.26 djm 294: if (auth_parse_options(pw, line_opts,
295: file, linenum) != 1)
296: continue;
1.62 djm 297: found_principal = 1;
298: continue;
1.24 djm 299: }
300: }
301: }
1.62 djm 302: return found_principal;
1.51 djm 303: }
304:
305: static int
306: match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
307: {
308: FILE *f;
309: int success;
310:
311: temporarily_use_uid(pw);
312: debug("trying authorized principals file %s", file);
313: if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
314: restore_uid();
315: return 0;
316: }
317: success = process_principals(f, file, pw, cert);
1.24 djm 318: fclose(f);
319: restore_uid();
1.51 djm 320: return success;
1.31 djm 321: }
1.24 djm 322:
1.31 djm 323: /*
1.51 djm 324: * Checks whether principal is allowed in output of command.
325: * returns 1 if the principal is allowed or 0 otherwise.
326: */
327: static int
1.56 djm 328: match_principals_command(struct passwd *user_pw, const struct sshkey *key)
1.51 djm 329: {
1.56 djm 330: const struct sshkey_cert *cert = key->cert;
1.51 djm 331: FILE *f = NULL;
1.56 djm 332: int r, ok, found_principal = 0;
1.51 djm 333: struct passwd *pw;
334: int i, ac = 0, uid_swapped = 0;
335: pid_t pid;
336: char *tmp, *username = NULL, *command = NULL, **av = NULL;
1.56 djm 337: char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
1.58 djm 338: char serial_s[16];
1.51 djm 339: void (*osigchld)(int);
340:
341: if (options.authorized_principals_command == NULL)
342: return 0;
343: if (options.authorized_principals_command_user == NULL) {
344: error("No user for AuthorizedPrincipalsCommand specified, "
345: "skipping");
346: return 0;
347: }
348:
349: /*
350: * NB. all returns later this function should go via "out" to
351: * ensure the original SIGCHLD handler is restored properly.
352: */
353: osigchld = signal(SIGCHLD, SIG_DFL);
354:
355: /* Prepare and verify the user for the command */
356: username = percent_expand(options.authorized_principals_command_user,
357: "u", user_pw->pw_name, (char *)NULL);
358: pw = getpwnam(username);
359: if (pw == NULL) {
360: error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
361: username, strerror(errno));
362: goto out;
363: }
364:
365: /* Turn the command into an argument vector */
1.69 djm 366: if (argv_split(options.authorized_principals_command, &ac, &av) != 0) {
1.51 djm 367: error("AuthorizedPrincipalsCommand \"%s\" contains "
368: "invalid quotes", command);
369: goto out;
370: }
371: if (ac == 0) {
372: error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
373: command);
374: goto out;
375: }
1.56 djm 376: if ((ca_fp = sshkey_fingerprint(cert->signature_key,
377: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
378: error("%s: sshkey_fingerprint failed", __func__);
379: goto out;
380: }
1.57 djm 381: if ((key_fp = sshkey_fingerprint(key,
1.56 djm 382: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
383: error("%s: sshkey_fingerprint failed", __func__);
384: goto out;
385: }
386: if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
387: error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
388: goto out;
389: }
390: if ((r = sshkey_to_base64(key, &keytext)) != 0) {
391: error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
392: goto out;
393: }
1.59 djm 394: snprintf(serial_s, sizeof(serial_s), "%llu",
395: (unsigned long long)cert->serial);
1.51 djm 396: for (i = 1; i < ac; i++) {
397: tmp = percent_expand(av[i],
398: "u", user_pw->pw_name,
399: "h", user_pw->pw_dir,
1.56 djm 400: "t", sshkey_ssh_name(key),
401: "T", sshkey_ssh_name(cert->signature_key),
402: "f", key_fp,
403: "F", ca_fp,
404: "k", keytext,
405: "K", catext,
1.58 djm 406: "i", cert->key_id,
407: "s", serial_s,
1.51 djm 408: (char *)NULL);
409: if (tmp == NULL)
410: fatal("%s: percent_expand failed", __func__);
411: free(av[i]);
412: av[i] = tmp;
413: }
414: /* Prepare a printable command for logs, etc. */
1.69 djm 415: command = argv_assemble(ac, av);
1.51 djm 416:
417: if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command,
1.69 djm 418: ac, av, &f,
419: SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
1.51 djm 420: goto out;
421:
422: uid_swapped = 1;
423: temporarily_use_uid(pw);
424:
1.67 markus 425: ok = process_principals(f, "(command)", pw, cert);
1.51 djm 426:
1.61 djm 427: fclose(f);
428: f = NULL;
429:
1.70 djm 430: if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
1.51 djm 431: goto out;
432:
433: /* Read completed successfully */
434: found_principal = ok;
435: out:
436: if (f != NULL)
437: fclose(f);
438: signal(SIGCHLD, osigchld);
439: for (i = 0; i < ac; i++)
440: free(av[i]);
441: free(av);
442: if (uid_swapped)
443: restore_uid();
444: free(command);
445: free(username);
1.56 djm 446: free(ca_fp);
447: free(key_fp);
448: free(catext);
449: free(keytext);
1.51 djm 450: return found_principal;
451: }
452: /*
1.31 djm 453: * Checks whether key is allowed in authorized_keys-format file,
454: * returns 1 if the key is allowed or 0 otherwise.
455: */
1.1 markus 456: static int
1.64 markus 457: check_authkeys_file(FILE *f, char *file, struct sshkey *key, struct passwd *pw)
1.1 markus 458: {
1.8 dtucker 459: char line[SSH_MAX_PUBKEY_BYTES];
1.18 dtucker 460: int found_key = 0;
1.1 markus 461: u_long linenum = 0;
1.64 markus 462: struct sshkey *found = NULL;
1.1 markus 463:
1.8 dtucker 464: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.60 djm 465: char *cp, *key_options = NULL, *fp = NULL;
466: const char *reason = NULL;
467:
1.71 djm 468: /* Always consume entire file */
1.62 djm 469: if (found_key)
470: continue;
1.76 ! dtucker 471: sshkey_free(found);
1.64 markus 472: found = sshkey_new(sshkey_is_cert(key) ? KEY_UNSPEC : key->type);
473: if (found == NULL)
474: goto done;
1.20 djm 475: auth_clear_options();
476:
1.1 markus 477: /* Skip leading whitespace, empty and comment lines. */
478: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
479: ;
480: if (!*cp || *cp == '\n' || *cp == '#')
481: continue;
482:
1.64 markus 483: if (sshkey_read(found, &cp) != 0) {
1.1 markus 484: /* no key? check if there are options for this key */
485: int quoted = 0;
486: debug2("user_key_allowed: check options: '%s'", cp);
1.7 avsm 487: key_options = cp;
1.1 markus 488: for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
489: if (*cp == '\\' && cp[1] == '"')
490: cp++; /* Skip both */
491: else if (*cp == '"')
492: quoted = !quoted;
493: }
494: /* Skip remaining whitespace. */
495: for (; *cp == ' ' || *cp == '\t'; cp++)
496: ;
1.64 markus 497: if (sshkey_read(found, &cp) != 0) {
1.1 markus 498: debug2("user_key_allowed: advance: '%s'", cp);
499: /* still no key? advance to next line*/
500: continue;
501: }
502: }
1.64 markus 503: if (sshkey_is_cert(key)) {
504: if (!sshkey_equal(found, key->cert->signature_key))
1.25 djm 505: continue;
506: if (auth_parse_options(pw, key_options, file,
507: linenum) != 1)
508: continue;
1.20 djm 509: if (!key_is_cert_authority)
510: continue;
1.46 djm 511: if ((fp = sshkey_fingerprint(found,
512: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
513: continue;
1.22 djm 514: debug("matching CA found: file %s, line %lu, %s %s",
1.64 markus 515: file, linenum, sshkey_type(found), fp);
1.24 djm 516: /*
517: * If the user has specified a list of principals as
518: * a key option, then prefer that list to matching
519: * their username in the certificate principals list.
520: */
521: if (authorized_principals != NULL &&
522: !match_principals_option(authorized_principals,
523: key->cert)) {
524: reason = "Certificate does not contain an "
525: "authorized principal";
526: fail_reason:
1.36 djm 527: free(fp);
1.20 djm 528: error("%s", reason);
529: auth_debug_add("%s", reason);
530: continue;
531: }
1.64 markus 532: if (sshkey_cert_check_authority(key, 0, 0,
1.24 djm 533: authorized_principals == NULL ? pw->pw_name : NULL,
534: &reason) != 0)
535: goto fail_reason;
1.60 djm 536: if (auth_cert_options(key, pw, &reason) != 0)
537: goto fail_reason;
1.54 djm 538: verbose("Accepted certificate ID \"%s\" (serial %llu) "
1.22 djm 539: "signed by %s CA %s via %s", key->cert->key_id,
1.54 djm 540: (unsigned long long)key->cert->serial,
1.64 markus 541: sshkey_type(found), fp, file);
1.36 djm 542: free(fp);
1.20 djm 543: found_key = 1;
544: break;
1.64 markus 545: } else if (sshkey_equal(found, key)) {
1.25 djm 546: if (auth_parse_options(pw, key_options, file,
547: linenum) != 1)
548: continue;
549: if (key_is_cert_authority)
550: continue;
1.46 djm 551: if ((fp = sshkey_fingerprint(found,
552: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
553: continue;
1.37 djm 554: debug("matching key found: file %s, line %lu %s %s",
1.64 markus 555: file, linenum, sshkey_type(found), fp);
1.36 djm 556: free(fp);
1.46 djm 557: found_key = 1;
1.62 djm 558: continue;
1.1 markus 559: }
560: }
1.64 markus 561: done:
1.76 ! dtucker 562: sshkey_free(found);
1.1 markus 563: if (!found_key)
564: debug2("key not found");
565: return found_key;
566: }
567:
1.21 djm 568: /* Authenticate a certificate key against TrustedUserCAKeys */
569: static int
1.63 markus 570: user_cert_trusted_ca(struct passwd *pw, struct sshkey *key)
1.21 djm 571: {
1.24 djm 572: char *ca_fp, *principals_file = NULL;
1.21 djm 573: const char *reason;
1.64 markus 574: int r, ret = 0, found_principal = 0, use_authorized_principals;
1.21 djm 575:
1.64 markus 576: if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
1.21 djm 577: return 0;
578:
1.46 djm 579: if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
580: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
581: return 0;
1.21 djm 582:
1.64 markus 583: if ((r = sshkey_in_file(key->cert->signature_key,
584: options.trusted_user_ca_keys, 1, 0)) != 0) {
585: debug2("%s: CA %s %s is not listed in %s: %s", __func__,
586: sshkey_type(key->cert->signature_key), ca_fp,
587: options.trusted_user_ca_keys, ssh_err(r));
1.21 djm 588: goto out;
589: }
1.24 djm 590: /*
591: * If AuthorizedPrincipals is in use, then compare the certificate
592: * principals against the names in that file rather than matching
593: * against the username.
594: */
595: if ((principals_file = authorized_principals_file(pw)) != NULL) {
1.51 djm 596: if (match_principals_file(principals_file, pw, key->cert))
597: found_principal = 1;
598: }
599: /* Try querying command if specified */
1.56 djm 600: if (!found_principal && match_principals_command(pw, key))
1.51 djm 601: found_principal = 1;
1.53 jsing 602: /* If principals file or command is specified, then require a match */
603: use_authorized_principals = principals_file != NULL ||
604: options.authorized_principals_command != NULL;
605: if (!found_principal && use_authorized_principals) {
1.51 djm 606: reason = "Certificate does not contain an authorized principal";
1.24 djm 607: fail_reason:
1.51 djm 608: error("%s", reason);
609: auth_debug_add("%s", reason);
610: goto out;
1.21 djm 611: }
1.64 markus 612: if (sshkey_cert_check_authority(key, 0, 1,
1.53 jsing 613: use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
1.24 djm 614: goto fail_reason;
1.60 djm 615: if (auth_cert_options(key, pw, &reason) != 0)
616: goto fail_reason;
1.21 djm 617:
1.54 djm 618: verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
619: "%s CA %s via %s", key->cert->key_id,
620: (unsigned long long)key->cert->serial,
1.64 markus 621: sshkey_type(key->cert->signature_key), ca_fp,
1.22 djm 622: options.trusted_user_ca_keys);
1.21 djm 623: ret = 1;
624:
625: out:
1.36 djm 626: free(principals_file);
627: free(ca_fp);
1.21 djm 628: return ret;
629: }
630:
1.31 djm 631: /*
632: * Checks whether key is allowed in file.
633: * returns 1 if the key is allowed or 0 otherwise.
634: */
635: static int
1.63 markus 636: user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file)
1.31 djm 637: {
638: FILE *f;
639: int found_key = 0;
640:
641: /* Temporarily use the user's uid. */
642: temporarily_use_uid(pw);
643:
644: debug("trying public key file %s", file);
645: if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
646: found_key = check_authkeys_file(f, file, key, pw);
647: fclose(f);
648: }
649:
650: restore_uid();
651: return found_key;
652: }
653:
654: /*
655: * Checks whether key is allowed in output of command.
656: * returns 1 if the key is allowed or 0 otherwise.
657: */
658: static int
1.63 markus 659: user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key)
1.31 djm 660: {
1.50 djm 661: FILE *f = NULL;
662: int r, ok, found_key = 0;
1.31 djm 663: struct passwd *pw;
1.50 djm 664: int i, uid_swapped = 0, ac = 0;
1.31 djm 665: pid_t pid;
1.50 djm 666: char *username = NULL, *key_fp = NULL, *keytext = NULL;
667: char *tmp, *command = NULL, **av = NULL;
668: void (*osigchld)(int);
1.31 djm 669:
1.50 djm 670: if (options.authorized_keys_command == NULL)
1.31 djm 671: return 0;
1.32 djm 672: if (options.authorized_keys_command_user == NULL) {
673: error("No user for AuthorizedKeysCommand specified, skipping");
674: return 0;
675: }
676:
1.50 djm 677: /*
678: * NB. all returns later this function should go via "out" to
679: * ensure the original SIGCHLD handler is restored properly.
680: */
681: osigchld = signal(SIGCHLD, SIG_DFL);
682:
683: /* Prepare and verify the user for the command */
1.32 djm 684: username = percent_expand(options.authorized_keys_command_user,
685: "u", user_pw->pw_name, (char *)NULL);
686: pw = getpwnam(username);
687: if (pw == NULL) {
1.34 djm 688: error("AuthorizedKeysCommandUser \"%s\" not found: %s",
689: username, strerror(errno));
1.50 djm 690: goto out;
1.31 djm 691: }
692:
1.50 djm 693: /* Prepare AuthorizedKeysCommand */
694: if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
695: SSH_FP_DEFAULT)) == NULL) {
696: error("%s: sshkey_fingerprint failed", __func__);
1.31 djm 697: goto out;
698: }
1.50 djm 699: if ((r = sshkey_to_base64(key, &keytext)) != 0) {
700: error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
1.31 djm 701: goto out;
702: }
703:
1.50 djm 704: /* Turn the command into an argument vector */
1.69 djm 705: if (argv_split(options.authorized_keys_command, &ac, &av) != 0) {
1.50 djm 706: error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
707: command);
708: goto out;
709: }
710: if (ac == 0) {
711: error("AuthorizedKeysCommand \"%s\" yielded no arguments",
712: command);
1.31 djm 713: goto out;
714: }
1.50 djm 715: for (i = 1; i < ac; i++) {
716: tmp = percent_expand(av[i],
717: "u", user_pw->pw_name,
718: "h", user_pw->pw_dir,
719: "t", sshkey_ssh_name(key),
720: "f", key_fp,
721: "k", keytext,
722: (char *)NULL);
723: if (tmp == NULL)
724: fatal("%s: percent_expand failed", __func__);
725: free(av[i]);
726: av[i] = tmp;
727: }
728: /* Prepare a printable command for logs, etc. */
1.69 djm 729: command = argv_assemble(ac, av);
1.31 djm 730:
731: /*
1.50 djm 732: * If AuthorizedKeysCommand was run without arguments
733: * then fall back to the old behaviour of passing the
734: * target username as a single argument.
1.31 djm 735: */
1.50 djm 736: if (ac == 1) {
737: av = xreallocarray(av, ac + 2, sizeof(*av));
738: av[1] = xstrdup(user_pw->pw_name);
739: av[2] = NULL;
740: /* Fix up command too, since it is used in log messages */
741: free(command);
742: xasprintf(&command, "%s %s", av[0], av[1]);
743: }
1.31 djm 744:
1.50 djm 745: if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
1.69 djm 746: ac, av, &f,
747: SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
1.50 djm 748: goto out;
1.31 djm 749:
1.50 djm 750: uid_swapped = 1;
1.31 djm 751: temporarily_use_uid(pw);
752:
753: ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
1.61 djm 754:
755: fclose(f);
756: f = NULL;
1.31 djm 757:
1.70 djm 758: if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
1.31 djm 759: goto out;
1.50 djm 760:
761: /* Read completed successfully */
1.31 djm 762: found_key = ok;
763: out:
1.50 djm 764: if (f != NULL)
765: fclose(f);
766: signal(SIGCHLD, osigchld);
767: for (i = 0; i < ac; i++)
768: free(av[i]);
769: free(av);
770: if (uid_swapped)
771: restore_uid();
772: free(command);
773: free(username);
774: free(key_fp);
775: free(keytext);
1.31 djm 776: return found_key;
777: }
778:
779: /*
780: * Check whether key authenticates and authorises the user.
781: */
1.1 markus 782: int
1.63 markus 783: user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt)
1.1 markus 784: {
1.29 djm 785: u_int success, i;
1.1 markus 786: char *file;
1.21 djm 787:
788: if (auth_key_is_revoked(key))
789: return 0;
1.64 markus 790: if (sshkey_is_cert(key) &&
791: auth_key_is_revoked(key->cert->signature_key))
1.21 djm 792: return 0;
793:
794: success = user_cert_trusted_ca(pw, key);
795: if (success)
796: return success;
1.1 markus 797:
1.31 djm 798: success = user_key_command_allowed2(pw, key);
799: if (success > 0)
800: return success;
801:
1.29 djm 802: for (i = 0; !success && i < options.num_authkeys_files; i++) {
1.31 djm 803:
804: if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
805: continue;
1.29 djm 806: file = expand_authorized_keys(
807: options.authorized_keys_files[i], pw);
1.31 djm 808:
1.29 djm 809: success = user_key_allowed2(pw, key, file);
1.36 djm 810: free(file);
1.29 djm 811: }
1.1 markus 812:
813: return success;
814: }
1.2 markus 815:
816: Authmethod method_pubkey = {
817: "publickey",
818: userauth_pubkey,
819: &options.pubkey_authentication
820: };