Annotation of src/usr.bin/ssh/auth2-pubkey.c, Revision 1.63
1.63 ! markus 1: /* $OpenBSD: auth2-pubkey.c,v 1.62 2017/01/30 01:03:00 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.31 djm 29: #include <sys/wait.h>
1.13 stevesk 30:
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"
47: #include "buffer.h"
48: #include "log.h"
1.41 millert 49: #include "misc.h"
1.1 markus 50: #include "servconf.h"
51: #include "compat.h"
1.15 deraadt 52: #include "key.h"
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.2 markus 74: static int
1.1 markus 75: userauth_pubkey(Authctxt *authctxt)
76: {
77: Buffer b;
1.63 ! markus 78: struct sshkey *key = NULL;
1.55 djm 79: char *pkalg, *userstyle, *fp = NULL;
1.1 markus 80: u_char *pkblob, *sig;
81: u_int alen, blen, slen;
82: int have_sig, pktype;
83: int authenticated = 0;
84:
85: if (!authctxt->valid) {
1.55 djm 86: debug2("%s: disabled because of invalid user", __func__);
1.1 markus 87: return 0;
88: }
89: have_sig = packet_get_char();
90: if (datafellows & SSH_BUG_PKAUTH) {
1.55 djm 91: debug2("%s: SSH_BUG_PKAUTH", __func__);
1.1 markus 92: /* no explicit pkalg given */
93: pkblob = packet_get_string(&blen);
94: buffer_init(&b);
95: buffer_append(&b, pkblob, blen);
96: /* so we have to extract the pkalg from the pkblob */
97: pkalg = buffer_get_string(&b, &alen);
98: buffer_free(&b);
99: } else {
100: pkalg = packet_get_string(&alen);
101: pkblob = packet_get_string(&blen);
102: }
103: pktype = key_type_from_name(pkalg);
104: if (pktype == KEY_UNSPEC) {
105: /* this is perfectly legal */
1.55 djm 106: logit("%s: unsupported public key algorithm: %s",
107: __func__, pkalg);
1.1 markus 108: goto done;
109: }
110: key = key_from_blob(pkblob, blen);
111: if (key == NULL) {
1.55 djm 112: error("%s: cannot decode key: %s", __func__, pkalg);
1.1 markus 113: goto done;
114: }
115: if (key->type != pktype) {
1.55 djm 116: error("%s: type mismatch for decoded key "
117: "(received %d, expected %d)", __func__, key->type, pktype);
1.39 djm 118: goto done;
119: }
120: if (key_type_plain(key->type) == KEY_RSA &&
121: (datafellows & SSH_BUG_RSASIGMD5) != 0) {
122: logit("Refusing RSA key because client uses unsafe "
123: "signature scheme");
1.1 markus 124: goto done;
125: }
1.55 djm 126: fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT);
1.44 djm 127: if (auth2_userkey_already_used(authctxt, key)) {
128: logit("refusing previously-used %s key", key_type(key));
129: goto done;
130: }
1.49 djm 131: if (match_pattern_list(sshkey_ssh_name(key),
132: options.pubkey_key_types, 0) != 1) {
1.45 djm 133: logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
134: __func__, sshkey_ssh_name(key));
135: goto done;
136: }
137:
1.1 markus 138: if (have_sig) {
1.55 djm 139: debug3("%s: have signature for %s %s",
140: __func__, sshkey_type(key), fp);
1.1 markus 141: sig = packet_get_string(&slen);
142: packet_check_eom();
143: buffer_init(&b);
144: if (datafellows & SSH_OLD_SESSIONID) {
145: buffer_append(&b, session_id2, session_id2_len);
146: } else {
147: buffer_put_string(&b, session_id2, session_id2_len);
148: }
149: /* reconstruct packet */
150: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.35 djm 151: xasprintf(&userstyle, "%s%s%s", authctxt->user,
152: authctxt->style ? ":" : "",
153: authctxt->style ? authctxt->style : "");
154: buffer_put_cstring(&b, userstyle);
155: free(userstyle);
1.1 markus 156: buffer_put_cstring(&b,
157: datafellows & SSH_BUG_PKSERVICE ?
158: "ssh-userauth" :
159: authctxt->service);
160: if (datafellows & SSH_BUG_PKAUTH) {
161: buffer_put_char(&b, have_sig);
162: } else {
163: buffer_put_cstring(&b, "publickey");
164: buffer_put_char(&b, have_sig);
165: buffer_put_cstring(&b, pkalg);
166: }
167: buffer_put_string(&b, pkblob, blen);
168: #ifdef DEBUG_PK
169: buffer_dump(&b);
170: #endif
1.38 djm 171: pubkey_auth_info(authctxt, key, NULL);
1.37 djm 172:
1.1 markus 173: /* test for correct signature */
174: authenticated = 0;
1.48 djm 175: if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
1.1 markus 176: PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
1.44 djm 177: buffer_len(&b))) == 1) {
1.1 markus 178: authenticated = 1;
1.44 djm 179: /* Record the successful key to prevent reuse */
180: auth2_record_userkey(authctxt, key);
181: key = NULL; /* Don't free below */
182: }
1.6 markus 183: buffer_free(&b);
1.36 djm 184: free(sig);
1.1 markus 185: } else {
1.55 djm 186: debug("%s: test whether pkalg/pkblob are acceptable for %s %s",
187: __func__, sshkey_type(key), fp);
1.1 markus 188: packet_check_eom();
189:
190: /* XXX fake reply and always send PK_OK ? */
191: /*
192: * XXX this allows testing whether a user is allowed
193: * to login: if you happen to have a valid pubkey this
194: * message is sent. the message is NEVER sent at all
195: * if a user is not allowed to login. is this an
196: * issue? -markus
197: */
1.48 djm 198: if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) {
1.1 markus 199: packet_start(SSH2_MSG_USERAUTH_PK_OK);
200: packet_put_string(pkalg, alen);
201: packet_put_string(pkblob, blen);
202: packet_send();
203: packet_write_wait();
204: authctxt->postponed = 1;
205: }
206: }
207: if (authenticated != 1)
208: auth_clear_options();
209: done:
1.55 djm 210: debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
1.1 markus 211: if (key != NULL)
212: key_free(key);
1.36 djm 213: free(pkalg);
214: free(pkblob);
1.55 djm 215: free(fp);
1.1 markus 216: return authenticated;
217: }
218:
1.37 djm 219: void
1.63 ! markus 220: pubkey_auth_info(Authctxt *authctxt, const struct sshkey *key,
! 221: const char *fmt, ...)
1.37 djm 222: {
1.38 djm 223: char *fp, *extra;
224: va_list ap;
225: int i;
226:
227: extra = NULL;
228: if (fmt != NULL) {
229: va_start(ap, fmt);
230: i = vasprintf(&extra, fmt, ap);
231: va_end(ap);
232: if (i < 0 || extra == NULL)
233: fatal("%s: vasprintf failed", __func__);
234: }
1.37 djm 235:
236: if (key_is_cert(key)) {
1.46 djm 237: fp = sshkey_fingerprint(key->cert->signature_key,
1.43 djm 238: options.fingerprint_hash, SSH_FP_DEFAULT);
1.38 djm 239: auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s",
1.37 djm 240: key_type(key), key->cert->key_id,
241: (unsigned long long)key->cert->serial,
1.46 djm 242: key_type(key->cert->signature_key),
1.47 djm 243: fp == NULL ? "(null)" : fp,
1.38 djm 244: extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
1.37 djm 245: free(fp);
246: } else {
1.46 djm 247: fp = sshkey_fingerprint(key, options.fingerprint_hash,
1.43 djm 248: SSH_FP_DEFAULT);
1.46 djm 249: auth_info(authctxt, "%s %s%s%s", key_type(key),
1.47 djm 250: fp == NULL ? "(null)" : fp,
1.38 djm 251: extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
1.37 djm 252: free(fp);
253: }
1.38 djm 254: free(extra);
1.37 djm 255: }
256:
1.50 djm 257: /*
258: * Splits 's' into an argument vector. Handles quoted string and basic
259: * escape characters (\\, \", \'). Caller must free the argument vector
260: * and its members.
261: */
262: static int
263: split_argv(const char *s, int *argcp, char ***argvp)
264: {
265: int r = SSH_ERR_INTERNAL_ERROR;
266: int argc = 0, quote, i, j;
267: char *arg, **argv = xcalloc(1, sizeof(*argv));
268:
269: *argvp = NULL;
270: *argcp = 0;
271:
272: for (i = 0; s[i] != '\0'; i++) {
273: /* Skip leading whitespace */
274: if (s[i] == ' ' || s[i] == '\t')
275: continue;
276:
277: /* Start of a token */
278: quote = 0;
279: if (s[i] == '\\' &&
280: (s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\'))
281: i++;
282: else if (s[i] == '\'' || s[i] == '"')
283: quote = s[i++];
284:
285: argv = xreallocarray(argv, (argc + 2), sizeof(*argv));
286: arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1);
287: argv[argc] = NULL;
288:
289: /* Copy the token in, removing escapes */
290: for (j = 0; s[i] != '\0'; i++) {
291: if (s[i] == '\\') {
292: if (s[i + 1] == '\'' ||
293: s[i + 1] == '\"' ||
294: s[i + 1] == '\\') {
295: i++; /* Skip '\' */
296: arg[j++] = s[i];
297: } else {
298: /* Unrecognised escape */
299: arg[j++] = s[i];
300: }
301: } else if (quote == 0 && (s[i] == ' ' || s[i] == '\t'))
302: break; /* done */
303: else if (quote != 0 && s[i] == quote)
304: break; /* done */
305: else
306: arg[j++] = s[i];
307: }
308: if (s[i] == '\0') {
309: if (quote != 0) {
310: /* Ran out of string looking for close quote */
311: r = SSH_ERR_INVALID_FORMAT;
312: goto out;
313: }
314: break;
315: }
316: }
317: /* Success */
318: *argcp = argc;
319: *argvp = argv;
320: argc = 0;
321: argv = NULL;
322: r = 0;
323: out:
324: if (argc != 0 && argv != NULL) {
325: for (i = 0; i < argc; i++)
326: free(argv[i]);
327: free(argv);
328: }
329: return r;
330: }
331:
332: /*
333: * Reassemble an argument vector into a string, quoting and escaping as
334: * necessary. Caller must free returned string.
335: */
336: static char *
337: assemble_argv(int argc, char **argv)
338: {
339: int i, j, ws, r;
340: char c, *ret;
341: struct sshbuf *buf, *arg;
342:
343: if ((buf = sshbuf_new()) == NULL || (arg = sshbuf_new()) == NULL)
344: fatal("%s: sshbuf_new failed", __func__);
345:
346: for (i = 0; i < argc; i++) {
347: ws = 0;
348: sshbuf_reset(arg);
349: for (j = 0; argv[i][j] != '\0'; j++) {
350: r = 0;
351: c = argv[i][j];
352: switch (c) {
353: case ' ':
354: case '\t':
355: ws = 1;
356: r = sshbuf_put_u8(arg, c);
357: break;
358: case '\\':
359: case '\'':
360: case '"':
361: if ((r = sshbuf_put_u8(arg, '\\')) != 0)
362: break;
363: /* FALLTHROUGH */
364: default:
365: r = sshbuf_put_u8(arg, c);
366: break;
367: }
368: if (r != 0)
369: fatal("%s: sshbuf_put_u8: %s",
370: __func__, ssh_err(r));
371: }
372: if ((i != 0 && (r = sshbuf_put_u8(buf, ' ')) != 0) ||
373: (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0) ||
374: (r = sshbuf_putb(buf, arg)) != 0 ||
375: (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0))
376: fatal("%s: buffer error: %s", __func__, ssh_err(r));
377: }
378: if ((ret = malloc(sshbuf_len(buf) + 1)) == NULL)
379: fatal("%s: malloc failed", __func__);
380: memcpy(ret, sshbuf_ptr(buf), sshbuf_len(buf));
381: ret[sshbuf_len(buf)] = '\0';
382: sshbuf_free(buf);
383: sshbuf_free(arg);
384: return ret;
385: }
386:
387: /*
388: * Runs command in a subprocess. Returns pid on success and a FILE* to the
389: * subprocess' stdout or 0 on failure.
390: * NB. "command" is only used for logging.
391: */
392: static pid_t
393: subprocess(const char *tag, struct passwd *pw, const char *command,
394: int ac, char **av, FILE **child)
395: {
396: FILE *f;
397: struct stat st;
398: int devnull, p[2], i;
399: pid_t pid;
400: char *cp, errmsg[512];
401: u_int envsize;
402: char **child_env;
403:
404: *child = NULL;
405:
406: debug3("%s: %s command \"%s\" running as %s", __func__,
407: tag, command, pw->pw_name);
408:
409: /* Verify the path exists and is safe-ish to execute */
410: if (*av[0] != '/') {
411: error("%s path is not absolute", tag);
412: return 0;
413: }
414: temporarily_use_uid(pw);
415: if (stat(av[0], &st) < 0) {
416: error("Could not stat %s \"%s\": %s", tag,
417: av[0], strerror(errno));
418: restore_uid();
419: return 0;
420: }
421: if (auth_secure_path(av[0], &st, NULL, 0,
422: errmsg, sizeof(errmsg)) != 0) {
423: error("Unsafe %s \"%s\": %s", tag, av[0], errmsg);
424: restore_uid();
425: return 0;
426: }
427:
428: /*
429: * Run the command; stderr is left in place, stdout is the
430: * authorized_keys output.
431: */
432: if (pipe(p) != 0) {
433: error("%s: pipe: %s", tag, strerror(errno));
434: restore_uid();
435: return 0;
436: }
437:
438: /*
439: * Don't want to call this in the child, where it can fatal() and
440: * run cleanup_exit() code.
441: */
442: restore_uid();
443:
444: switch ((pid = fork())) {
445: case -1: /* error */
446: error("%s: fork: %s", tag, strerror(errno));
447: close(p[0]);
448: close(p[1]);
449: return 0;
450: case 0: /* child */
451: /* Prepare a minimal environment for the child. */
452: envsize = 5;
453: child_env = xcalloc(sizeof(*child_env), envsize);
454: child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH);
455: child_set_env(&child_env, &envsize, "USER", pw->pw_name);
456: child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name);
457: child_set_env(&child_env, &envsize, "HOME", pw->pw_dir);
458: if ((cp = getenv("LANG")) != NULL)
459: child_set_env(&child_env, &envsize, "LANG", cp);
460:
461: for (i = 0; i < NSIG; i++)
462: signal(i, SIG_DFL);
463:
464: if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
465: error("%s: open %s: %s", tag, _PATH_DEVNULL,
466: strerror(errno));
467: _exit(1);
468: }
469: /* Keep stderr around a while longer to catch errors */
470: if (dup2(devnull, STDIN_FILENO) == -1 ||
471: dup2(p[1], STDOUT_FILENO) == -1) {
472: error("%s: dup2: %s", tag, strerror(errno));
473: _exit(1);
474: }
475: closefrom(STDERR_FILENO + 1);
476:
477: /* Don't use permanently_set_uid() here to avoid fatal() */
478: if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
479: error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid,
480: strerror(errno));
481: _exit(1);
482: }
483: if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
484: error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid,
485: strerror(errno));
486: _exit(1);
487: }
488: /* stdin is pointed to /dev/null at this point */
489: if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
490: error("%s: dup2: %s", tag, strerror(errno));
491: _exit(1);
492: }
493:
494: execve(av[0], av, child_env);
495: error("%s exec \"%s\": %s", tag, command, strerror(errno));
496: _exit(127);
497: default: /* parent */
498: break;
499: }
500:
501: close(p[1]);
502: if ((f = fdopen(p[0], "r")) == NULL) {
503: error("%s: fdopen: %s", tag, strerror(errno));
504: close(p[0]);
505: /* Don't leave zombie child */
506: kill(pid, SIGTERM);
507: while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
508: ;
509: return 0;
510: }
511: /* Success */
512: debug3("%s: %s pid %ld", __func__, tag, (long)pid);
513: *child = f;
514: return pid;
515: }
516:
517: /* Returns 0 if pid exited cleanly, non-zero otherwise */
518: static int
519: exited_cleanly(pid_t pid, const char *tag, const char *cmd)
520: {
521: int status;
522:
523: while (waitpid(pid, &status, 0) == -1) {
524: if (errno != EINTR) {
525: error("%s: waitpid: %s", tag, strerror(errno));
526: return -1;
527: }
528: }
529: if (WIFSIGNALED(status)) {
530: error("%s %s exited on signal %d", tag, cmd, WTERMSIG(status));
531: return -1;
532: } else if (WEXITSTATUS(status) != 0) {
533: error("%s %s failed, status %d", tag, cmd, WEXITSTATUS(status));
534: return -1;
535: }
536: return 0;
537: }
538:
1.24 djm 539: static int
1.40 djm 540: match_principals_option(const char *principal_list, struct sshkey_cert *cert)
1.24 djm 541: {
542: char *result;
543: u_int i;
544:
545: /* XXX percent_expand() sequences for authorized_principals? */
546:
547: for (i = 0; i < cert->nprincipals; i++) {
548: if ((result = match_list(cert->principals[i],
549: principal_list, NULL)) != NULL) {
550: debug3("matched principal from key options \"%.100s\"",
551: result);
1.36 djm 552: free(result);
1.24 djm 553: return 1;
554: }
555: }
556: return 0;
557: }
558:
559: static int
1.51 djm 560: process_principals(FILE *f, char *file, struct passwd *pw,
1.56 djm 561: const struct sshkey_cert *cert)
1.24 djm 562: {
1.26 djm 563: char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
1.24 djm 564: u_long linenum = 0;
1.62 djm 565: u_int i, found_principal = 0;
1.24 djm 566:
567: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.62 djm 568: /* Always consume entire input */
569: if (found_principal)
570: continue;
1.26 djm 571: /* Skip leading whitespace. */
1.24 djm 572: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
573: ;
1.26 djm 574: /* Skip blank and comment lines. */
575: if ((ep = strchr(cp, '#')) != NULL)
576: *ep = '\0';
577: if (!*cp || *cp == '\n')
1.24 djm 578: continue;
1.26 djm 579: /* Trim trailing whitespace. */
580: ep = cp + strlen(cp) - 1;
581: while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
582: *ep-- = '\0';
583: /*
584: * If the line has internal whitespace then assume it has
585: * key options.
586: */
587: line_opts = NULL;
588: if ((ep = strrchr(cp, ' ')) != NULL ||
589: (ep = strrchr(cp, '\t')) != NULL) {
590: for (; *ep == ' ' || *ep == '\t'; ep++)
1.27 deraadt 591: ;
1.26 djm 592: line_opts = cp;
593: cp = ep;
594: }
1.24 djm 595: for (i = 0; i < cert->nprincipals; i++) {
596: if (strcmp(cp, cert->principals[i]) == 0) {
1.51 djm 597: debug3("%s:%lu: matched principal \"%.100s\"",
598: file == NULL ? "(command)" : file,
599: linenum, cert->principals[i]);
1.26 djm 600: if (auth_parse_options(pw, line_opts,
601: file, linenum) != 1)
602: continue;
1.62 djm 603: found_principal = 1;
604: continue;
1.24 djm 605: }
606: }
607: }
1.62 djm 608: return found_principal;
1.51 djm 609: }
610:
611: static int
612: match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
613: {
614: FILE *f;
615: int success;
616:
617: temporarily_use_uid(pw);
618: debug("trying authorized principals file %s", file);
619: if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
620: restore_uid();
621: return 0;
622: }
623: success = process_principals(f, file, pw, cert);
1.24 djm 624: fclose(f);
625: restore_uid();
1.51 djm 626: return success;
1.31 djm 627: }
1.24 djm 628:
1.31 djm 629: /*
1.51 djm 630: * Checks whether principal is allowed in output of command.
631: * returns 1 if the principal is allowed or 0 otherwise.
632: */
633: static int
1.56 djm 634: match_principals_command(struct passwd *user_pw, const struct sshkey *key)
1.51 djm 635: {
1.56 djm 636: const struct sshkey_cert *cert = key->cert;
1.51 djm 637: FILE *f = NULL;
1.56 djm 638: int r, ok, found_principal = 0;
1.51 djm 639: struct passwd *pw;
640: int i, ac = 0, uid_swapped = 0;
641: pid_t pid;
642: char *tmp, *username = NULL, *command = NULL, **av = NULL;
1.56 djm 643: char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
1.58 djm 644: char serial_s[16];
1.51 djm 645: void (*osigchld)(int);
646:
647: if (options.authorized_principals_command == NULL)
648: return 0;
649: if (options.authorized_principals_command_user == NULL) {
650: error("No user for AuthorizedPrincipalsCommand specified, "
651: "skipping");
652: return 0;
653: }
654:
655: /*
656: * NB. all returns later this function should go via "out" to
657: * ensure the original SIGCHLD handler is restored properly.
658: */
659: osigchld = signal(SIGCHLD, SIG_DFL);
660:
661: /* Prepare and verify the user for the command */
662: username = percent_expand(options.authorized_principals_command_user,
663: "u", user_pw->pw_name, (char *)NULL);
664: pw = getpwnam(username);
665: if (pw == NULL) {
666: error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
667: username, strerror(errno));
668: goto out;
669: }
670:
671: /* Turn the command into an argument vector */
672: if (split_argv(options.authorized_principals_command, &ac, &av) != 0) {
673: error("AuthorizedPrincipalsCommand \"%s\" contains "
674: "invalid quotes", command);
675: goto out;
676: }
677: if (ac == 0) {
678: error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
679: command);
680: goto out;
681: }
1.56 djm 682: if ((ca_fp = sshkey_fingerprint(cert->signature_key,
683: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
684: error("%s: sshkey_fingerprint failed", __func__);
685: goto out;
686: }
1.57 djm 687: if ((key_fp = sshkey_fingerprint(key,
1.56 djm 688: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
689: error("%s: sshkey_fingerprint failed", __func__);
690: goto out;
691: }
692: if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
693: error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
694: goto out;
695: }
696: if ((r = sshkey_to_base64(key, &keytext)) != 0) {
697: error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
698: goto out;
699: }
1.59 djm 700: snprintf(serial_s, sizeof(serial_s), "%llu",
701: (unsigned long long)cert->serial);
1.51 djm 702: for (i = 1; i < ac; i++) {
703: tmp = percent_expand(av[i],
704: "u", user_pw->pw_name,
705: "h", user_pw->pw_dir,
1.56 djm 706: "t", sshkey_ssh_name(key),
707: "T", sshkey_ssh_name(cert->signature_key),
708: "f", key_fp,
709: "F", ca_fp,
710: "k", keytext,
711: "K", catext,
1.58 djm 712: "i", cert->key_id,
713: "s", serial_s,
1.51 djm 714: (char *)NULL);
715: if (tmp == NULL)
716: fatal("%s: percent_expand failed", __func__);
717: free(av[i]);
718: av[i] = tmp;
719: }
720: /* Prepare a printable command for logs, etc. */
721: command = assemble_argv(ac, av);
722:
723: if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command,
724: ac, av, &f)) == 0)
725: goto out;
726:
727: uid_swapped = 1;
728: temporarily_use_uid(pw);
729:
1.52 jsing 730: ok = process_principals(f, NULL, pw, cert);
1.51 djm 731:
1.61 djm 732: fclose(f);
733: f = NULL;
734:
1.51 djm 735: if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command) != 0)
736: goto out;
737:
738: /* Read completed successfully */
739: found_principal = ok;
740: out:
741: if (f != NULL)
742: fclose(f);
743: signal(SIGCHLD, osigchld);
744: for (i = 0; i < ac; i++)
745: free(av[i]);
746: free(av);
747: if (uid_swapped)
748: restore_uid();
749: free(command);
750: free(username);
1.56 djm 751: free(ca_fp);
752: free(key_fp);
753: free(catext);
754: free(keytext);
1.51 djm 755: return found_principal;
756: }
757: /*
1.31 djm 758: * Checks whether key is allowed in authorized_keys-format file,
759: * returns 1 if the key is allowed or 0 otherwise.
760: */
1.1 markus 761: static int
1.63 ! markus 762: check_authkeys_file(FILE *f, char *file, struct sshkey* key, struct passwd *pw)
1.1 markus 763: {
1.8 dtucker 764: char line[SSH_MAX_PUBKEY_BYTES];
1.18 dtucker 765: int found_key = 0;
1.1 markus 766: u_long linenum = 0;
1.63 ! markus 767: struct sshkey *found;
1.1 markus 768:
769: found_key = 0;
770:
1.37 djm 771: found = NULL;
1.8 dtucker 772: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.60 djm 773: char *cp, *key_options = NULL, *fp = NULL;
774: const char *reason = NULL;
775:
1.62 djm 776: /* Always consume entrire file */
777: if (found_key)
778: continue;
1.37 djm 779: if (found != NULL)
780: key_free(found);
781: found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
1.20 djm 782: auth_clear_options();
783:
1.1 markus 784: /* Skip leading whitespace, empty and comment lines. */
785: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
786: ;
787: if (!*cp || *cp == '\n' || *cp == '#')
788: continue;
789:
790: if (key_read(found, &cp) != 1) {
791: /* no key? check if there are options for this key */
792: int quoted = 0;
793: debug2("user_key_allowed: check options: '%s'", cp);
1.7 avsm 794: key_options = cp;
1.1 markus 795: for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
796: if (*cp == '\\' && cp[1] == '"')
797: cp++; /* Skip both */
798: else if (*cp == '"')
799: quoted = !quoted;
800: }
801: /* Skip remaining whitespace. */
802: for (; *cp == ' ' || *cp == '\t'; cp++)
803: ;
804: if (key_read(found, &cp) != 1) {
805: debug2("user_key_allowed: advance: '%s'", cp);
806: /* still no key? advance to next line*/
807: continue;
808: }
809: }
1.23 djm 810: if (key_is_cert(key)) {
1.25 djm 811: if (!key_equal(found, key->cert->signature_key))
812: continue;
813: if (auth_parse_options(pw, key_options, file,
814: linenum) != 1)
815: continue;
1.20 djm 816: if (!key_is_cert_authority)
817: continue;
1.46 djm 818: if ((fp = sshkey_fingerprint(found,
819: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
820: continue;
1.22 djm 821: debug("matching CA found: file %s, line %lu, %s %s",
822: file, linenum, key_type(found), fp);
1.24 djm 823: /*
824: * If the user has specified a list of principals as
825: * a key option, then prefer that list to matching
826: * their username in the certificate principals list.
827: */
828: if (authorized_principals != NULL &&
829: !match_principals_option(authorized_principals,
830: key->cert)) {
831: reason = "Certificate does not contain an "
832: "authorized principal";
833: fail_reason:
1.36 djm 834: free(fp);
1.20 djm 835: error("%s", reason);
836: auth_debug_add("%s", reason);
837: continue;
838: }
1.24 djm 839: if (key_cert_check_authority(key, 0, 0,
840: authorized_principals == NULL ? pw->pw_name : NULL,
841: &reason) != 0)
842: goto fail_reason;
1.60 djm 843: if (auth_cert_options(key, pw, &reason) != 0)
844: goto fail_reason;
1.54 djm 845: verbose("Accepted certificate ID \"%s\" (serial %llu) "
1.22 djm 846: "signed by %s CA %s via %s", key->cert->key_id,
1.54 djm 847: (unsigned long long)key->cert->serial,
1.22 djm 848: key_type(found), fp, file);
1.36 djm 849: free(fp);
1.20 djm 850: found_key = 1;
851: break;
1.25 djm 852: } else if (key_equal(found, key)) {
853: if (auth_parse_options(pw, key_options, file,
854: linenum) != 1)
855: continue;
856: if (key_is_cert_authority)
857: continue;
1.46 djm 858: if ((fp = sshkey_fingerprint(found,
859: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
860: continue;
1.37 djm 861: debug("matching key found: file %s, line %lu %s %s",
862: file, linenum, key_type(found), fp);
1.36 djm 863: free(fp);
1.46 djm 864: found_key = 1;
1.62 djm 865: continue;
1.1 markus 866: }
867: }
1.37 djm 868: if (found != NULL)
869: key_free(found);
1.1 markus 870: if (!found_key)
871: debug2("key not found");
872: return found_key;
873: }
874:
1.21 djm 875: /* Authenticate a certificate key against TrustedUserCAKeys */
876: static int
1.63 ! markus 877: user_cert_trusted_ca(struct passwd *pw, struct sshkey *key)
1.21 djm 878: {
1.24 djm 879: char *ca_fp, *principals_file = NULL;
1.21 djm 880: const char *reason;
1.53 jsing 881: int ret = 0, found_principal = 0, use_authorized_principals;
1.21 djm 882:
883: if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
884: return 0;
885:
1.46 djm 886: if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
887: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
888: return 0;
1.21 djm 889:
1.42 djm 890: if (sshkey_in_file(key->cert->signature_key,
891: options.trusted_user_ca_keys, 1, 0) != 0) {
1.21 djm 892: debug2("%s: CA %s %s is not listed in %s", __func__,
893: key_type(key->cert->signature_key), ca_fp,
894: options.trusted_user_ca_keys);
895: goto out;
896: }
1.24 djm 897: /*
898: * If AuthorizedPrincipals is in use, then compare the certificate
899: * principals against the names in that file rather than matching
900: * against the username.
901: */
902: if ((principals_file = authorized_principals_file(pw)) != NULL) {
1.51 djm 903: if (match_principals_file(principals_file, pw, key->cert))
904: found_principal = 1;
905: }
906: /* Try querying command if specified */
1.56 djm 907: if (!found_principal && match_principals_command(pw, key))
1.51 djm 908: found_principal = 1;
1.53 jsing 909: /* If principals file or command is specified, then require a match */
910: use_authorized_principals = principals_file != NULL ||
911: options.authorized_principals_command != NULL;
912: if (!found_principal && use_authorized_principals) {
1.51 djm 913: reason = "Certificate does not contain an authorized principal";
1.24 djm 914: fail_reason:
1.51 djm 915: error("%s", reason);
916: auth_debug_add("%s", reason);
917: goto out;
1.21 djm 918: }
1.24 djm 919: if (key_cert_check_authority(key, 0, 1,
1.53 jsing 920: use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
1.24 djm 921: goto fail_reason;
1.60 djm 922: if (auth_cert_options(key, pw, &reason) != 0)
923: goto fail_reason;
1.21 djm 924:
1.54 djm 925: verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
926: "%s CA %s via %s", key->cert->key_id,
927: (unsigned long long)key->cert->serial,
928: key_type(key->cert->signature_key), ca_fp,
1.22 djm 929: options.trusted_user_ca_keys);
1.21 djm 930: ret = 1;
931:
932: out:
1.36 djm 933: free(principals_file);
934: free(ca_fp);
1.21 djm 935: return ret;
936: }
937:
1.31 djm 938: /*
939: * Checks whether key is allowed in file.
940: * returns 1 if the key is allowed or 0 otherwise.
941: */
942: static int
1.63 ! markus 943: user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file)
1.31 djm 944: {
945: FILE *f;
946: int found_key = 0;
947:
948: /* Temporarily use the user's uid. */
949: temporarily_use_uid(pw);
950:
951: debug("trying public key file %s", file);
952: if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
953: found_key = check_authkeys_file(f, file, key, pw);
954: fclose(f);
955: }
956:
957: restore_uid();
958: return found_key;
959: }
960:
961: /*
962: * Checks whether key is allowed in output of command.
963: * returns 1 if the key is allowed or 0 otherwise.
964: */
965: static int
1.63 ! markus 966: user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key)
1.31 djm 967: {
1.50 djm 968: FILE *f = NULL;
969: int r, ok, found_key = 0;
1.31 djm 970: struct passwd *pw;
1.50 djm 971: int i, uid_swapped = 0, ac = 0;
1.31 djm 972: pid_t pid;
1.50 djm 973: char *username = NULL, *key_fp = NULL, *keytext = NULL;
974: char *tmp, *command = NULL, **av = NULL;
975: void (*osigchld)(int);
1.31 djm 976:
1.50 djm 977: if (options.authorized_keys_command == NULL)
1.31 djm 978: return 0;
1.32 djm 979: if (options.authorized_keys_command_user == NULL) {
980: error("No user for AuthorizedKeysCommand specified, skipping");
981: return 0;
982: }
983:
1.50 djm 984: /*
985: * NB. all returns later this function should go via "out" to
986: * ensure the original SIGCHLD handler is restored properly.
987: */
988: osigchld = signal(SIGCHLD, SIG_DFL);
989:
990: /* Prepare and verify the user for the command */
1.32 djm 991: username = percent_expand(options.authorized_keys_command_user,
992: "u", user_pw->pw_name, (char *)NULL);
993: pw = getpwnam(username);
994: if (pw == NULL) {
1.34 djm 995: error("AuthorizedKeysCommandUser \"%s\" not found: %s",
996: username, strerror(errno));
1.50 djm 997: goto out;
1.31 djm 998: }
999:
1.50 djm 1000: /* Prepare AuthorizedKeysCommand */
1001: if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
1002: SSH_FP_DEFAULT)) == NULL) {
1003: error("%s: sshkey_fingerprint failed", __func__);
1.31 djm 1004: goto out;
1005: }
1.50 djm 1006: if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1007: error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
1.31 djm 1008: goto out;
1009: }
1010:
1.50 djm 1011: /* Turn the command into an argument vector */
1012: if (split_argv(options.authorized_keys_command, &ac, &av) != 0) {
1013: error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
1014: command);
1015: goto out;
1016: }
1017: if (ac == 0) {
1018: error("AuthorizedKeysCommand \"%s\" yielded no arguments",
1019: command);
1.31 djm 1020: goto out;
1021: }
1.50 djm 1022: for (i = 1; i < ac; i++) {
1023: tmp = percent_expand(av[i],
1024: "u", user_pw->pw_name,
1025: "h", user_pw->pw_dir,
1026: "t", sshkey_ssh_name(key),
1027: "f", key_fp,
1028: "k", keytext,
1029: (char *)NULL);
1030: if (tmp == NULL)
1031: fatal("%s: percent_expand failed", __func__);
1032: free(av[i]);
1033: av[i] = tmp;
1034: }
1035: /* Prepare a printable command for logs, etc. */
1036: command = assemble_argv(ac, av);
1.31 djm 1037:
1038: /*
1.50 djm 1039: * If AuthorizedKeysCommand was run without arguments
1040: * then fall back to the old behaviour of passing the
1041: * target username as a single argument.
1.31 djm 1042: */
1.50 djm 1043: if (ac == 1) {
1044: av = xreallocarray(av, ac + 2, sizeof(*av));
1045: av[1] = xstrdup(user_pw->pw_name);
1046: av[2] = NULL;
1047: /* Fix up command too, since it is used in log messages */
1048: free(command);
1049: xasprintf(&command, "%s %s", av[0], av[1]);
1050: }
1.31 djm 1051:
1.50 djm 1052: if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
1053: ac, av, &f)) == 0)
1054: goto out;
1.31 djm 1055:
1.50 djm 1056: uid_swapped = 1;
1.31 djm 1057: temporarily_use_uid(pw);
1058:
1059: ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
1.61 djm 1060:
1061: fclose(f);
1062: f = NULL;
1.31 djm 1063:
1.50 djm 1064: if (exited_cleanly(pid, "AuthorizedKeysCommand", command) != 0)
1.31 djm 1065: goto out;
1.50 djm 1066:
1067: /* Read completed successfully */
1.31 djm 1068: found_key = ok;
1069: out:
1.50 djm 1070: if (f != NULL)
1071: fclose(f);
1072: signal(SIGCHLD, osigchld);
1073: for (i = 0; i < ac; i++)
1074: free(av[i]);
1075: free(av);
1076: if (uid_swapped)
1077: restore_uid();
1078: free(command);
1079: free(username);
1080: free(key_fp);
1081: free(keytext);
1.31 djm 1082: return found_key;
1083: }
1084:
1085: /*
1086: * Check whether key authenticates and authorises the user.
1087: */
1.1 markus 1088: int
1.63 ! markus 1089: user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt)
1.1 markus 1090: {
1.29 djm 1091: u_int success, i;
1.1 markus 1092: char *file;
1.21 djm 1093:
1094: if (auth_key_is_revoked(key))
1095: return 0;
1096: if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
1097: return 0;
1098:
1099: success = user_cert_trusted_ca(pw, key);
1100: if (success)
1101: return success;
1.1 markus 1102:
1.31 djm 1103: success = user_key_command_allowed2(pw, key);
1104: if (success > 0)
1105: return success;
1106:
1.29 djm 1107: for (i = 0; !success && i < options.num_authkeys_files; i++) {
1.31 djm 1108:
1109: if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1110: continue;
1.29 djm 1111: file = expand_authorized_keys(
1112: options.authorized_keys_files[i], pw);
1.31 djm 1113:
1.29 djm 1114: success = user_key_allowed2(pw, key, file);
1.36 djm 1115: free(file);
1.29 djm 1116: }
1.1 markus 1117:
1118: return success;
1.44 djm 1119: }
1120:
1121: /* Records a public key in the list of previously-successful keys */
1122: void
1123: auth2_record_userkey(Authctxt *authctxt, struct sshkey *key)
1124: {
1125: struct sshkey **tmp;
1126:
1127: if (authctxt->nprev_userkeys >= INT_MAX ||
1128: (tmp = reallocarray(authctxt->prev_userkeys,
1129: authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL)
1130: fatal("%s: reallocarray failed", __func__);
1131: authctxt->prev_userkeys = tmp;
1132: authctxt->prev_userkeys[authctxt->nprev_userkeys] = key;
1133: authctxt->nprev_userkeys++;
1134: }
1135:
1136: /* Checks whether a key has already been used successfully for authentication */
1137: int
1138: auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key)
1139: {
1140: u_int i;
1141:
1142: for (i = 0; i < authctxt->nprev_userkeys; i++) {
1143: if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) {
1144: return 1;
1145: }
1146: }
1147: return 0;
1.1 markus 1148: }
1.2 markus 1149:
1150: Authmethod method_pubkey = {
1151: "publickey",
1152: userauth_pubkey,
1153: &options.pubkey_authentication
1154: };