Annotation of src/usr.bin/ssh/auth2-pubkey.c, Revision 1.45
1.45 ! djm 1: /* $OpenBSD: auth2-pubkey.c,v 1.44 2014/12/22 07:51:30 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.1 markus 65:
66: /* import */
67: extern ServerOptions options;
68: extern u_char *session_id2;
1.4 markus 69: extern u_int session_id2_len;
1.1 markus 70:
1.2 markus 71: static int
1.1 markus 72: userauth_pubkey(Authctxt *authctxt)
73: {
74: Buffer b;
75: Key *key = NULL;
1.35 djm 76: char *pkalg, *userstyle;
1.1 markus 77: u_char *pkblob, *sig;
78: u_int alen, blen, slen;
79: int have_sig, pktype;
80: int authenticated = 0;
81:
82: if (!authctxt->valid) {
83: debug2("userauth_pubkey: disabled because of invalid user");
84: return 0;
85: }
86: have_sig = packet_get_char();
87: if (datafellows & SSH_BUG_PKAUTH) {
88: debug2("userauth_pubkey: SSH_BUG_PKAUTH");
89: /* no explicit pkalg given */
90: pkblob = packet_get_string(&blen);
91: buffer_init(&b);
92: buffer_append(&b, pkblob, blen);
93: /* so we have to extract the pkalg from the pkblob */
94: pkalg = buffer_get_string(&b, &alen);
95: buffer_free(&b);
96: } else {
97: pkalg = packet_get_string(&alen);
98: pkblob = packet_get_string(&blen);
99: }
100: pktype = key_type_from_name(pkalg);
101: if (pktype == KEY_UNSPEC) {
102: /* this is perfectly legal */
1.3 itojun 103: logit("userauth_pubkey: unsupported public key algorithm: %s",
1.1 markus 104: pkalg);
105: goto done;
106: }
107: key = key_from_blob(pkblob, blen);
108: if (key == NULL) {
109: error("userauth_pubkey: cannot decode key: %s", pkalg);
110: goto done;
111: }
112: if (key->type != pktype) {
113: error("userauth_pubkey: type mismatch for decoded key "
114: "(received %d, expected %d)", key->type, pktype);
1.39 djm 115: goto done;
116: }
117: if (key_type_plain(key->type) == KEY_RSA &&
118: (datafellows & SSH_BUG_RSASIGMD5) != 0) {
119: logit("Refusing RSA key because client uses unsafe "
120: "signature scheme");
1.1 markus 121: goto done;
122: }
1.44 djm 123: if (auth2_userkey_already_used(authctxt, key)) {
124: logit("refusing previously-used %s key", key_type(key));
125: goto done;
126: }
1.45 ! djm 127: if (match_pattern_list(sshkey_ssh_name(key), options.pubkey_key_types,
! 128: strlen(options.pubkey_key_types), 0) != 1) {
! 129: logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
! 130: __func__, sshkey_ssh_name(key));
! 131: goto done;
! 132: }
! 133:
1.1 markus 134: if (have_sig) {
135: sig = packet_get_string(&slen);
136: packet_check_eom();
137: buffer_init(&b);
138: if (datafellows & SSH_OLD_SESSIONID) {
139: buffer_append(&b, session_id2, session_id2_len);
140: } else {
141: buffer_put_string(&b, session_id2, session_id2_len);
142: }
143: /* reconstruct packet */
144: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.35 djm 145: xasprintf(&userstyle, "%s%s%s", authctxt->user,
146: authctxt->style ? ":" : "",
147: authctxt->style ? authctxt->style : "");
148: buffer_put_cstring(&b, userstyle);
149: free(userstyle);
1.1 markus 150: buffer_put_cstring(&b,
151: datafellows & SSH_BUG_PKSERVICE ?
152: "ssh-userauth" :
153: authctxt->service);
154: if (datafellows & SSH_BUG_PKAUTH) {
155: buffer_put_char(&b, have_sig);
156: } else {
157: buffer_put_cstring(&b, "publickey");
158: buffer_put_char(&b, have_sig);
159: buffer_put_cstring(&b, pkalg);
160: }
161: buffer_put_string(&b, pkblob, blen);
162: #ifdef DEBUG_PK
163: buffer_dump(&b);
164: #endif
1.38 djm 165: pubkey_auth_info(authctxt, key, NULL);
1.37 djm 166:
1.1 markus 167: /* test for correct signature */
168: authenticated = 0;
169: if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
170: PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
1.44 djm 171: buffer_len(&b))) == 1) {
1.1 markus 172: authenticated = 1;
1.44 djm 173: /* Record the successful key to prevent reuse */
174: auth2_record_userkey(authctxt, key);
175: key = NULL; /* Don't free below */
176: }
1.6 markus 177: buffer_free(&b);
1.36 djm 178: free(sig);
1.1 markus 179: } else {
180: debug("test whether pkalg/pkblob are acceptable");
181: packet_check_eom();
182:
183: /* XXX fake reply and always send PK_OK ? */
184: /*
185: * XXX this allows testing whether a user is allowed
186: * to login: if you happen to have a valid pubkey this
187: * message is sent. the message is NEVER sent at all
188: * if a user is not allowed to login. is this an
189: * issue? -markus
190: */
191: if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
192: packet_start(SSH2_MSG_USERAUTH_PK_OK);
193: packet_put_string(pkalg, alen);
194: packet_put_string(pkblob, blen);
195: packet_send();
196: packet_write_wait();
197: authctxt->postponed = 1;
198: }
199: }
200: if (authenticated != 1)
201: auth_clear_options();
202: done:
203: debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
204: if (key != NULL)
205: key_free(key);
1.36 djm 206: free(pkalg);
207: free(pkblob);
1.1 markus 208: return authenticated;
209: }
210:
1.37 djm 211: void
1.38 djm 212: pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
1.37 djm 213: {
1.38 djm 214: char *fp, *extra;
215: va_list ap;
216: int i;
217:
218: extra = NULL;
219: if (fmt != NULL) {
220: va_start(ap, fmt);
221: i = vasprintf(&extra, fmt, ap);
222: va_end(ap);
223: if (i < 0 || extra == NULL)
224: fatal("%s: vasprintf failed", __func__);
225: }
1.37 djm 226:
227: if (key_is_cert(key)) {
228: fp = key_fingerprint(key->cert->signature_key,
1.43 djm 229: options.fingerprint_hash, SSH_FP_DEFAULT);
1.38 djm 230: auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s",
1.37 djm 231: key_type(key), key->cert->key_id,
232: (unsigned long long)key->cert->serial,
1.38 djm 233: key_type(key->cert->signature_key), fp,
234: extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
1.37 djm 235: free(fp);
236: } else {
1.43 djm 237: fp = key_fingerprint(key, options.fingerprint_hash,
238: SSH_FP_DEFAULT);
1.38 djm 239: auth_info(authctxt, "%s %s%s%s", key_type(key), fp,
240: extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
1.37 djm 241: free(fp);
242: }
1.38 djm 243: free(extra);
1.37 djm 244: }
245:
1.24 djm 246: static int
1.40 djm 247: match_principals_option(const char *principal_list, struct sshkey_cert *cert)
1.24 djm 248: {
249: char *result;
250: u_int i;
251:
252: /* XXX percent_expand() sequences for authorized_principals? */
253:
254: for (i = 0; i < cert->nprincipals; i++) {
255: if ((result = match_list(cert->principals[i],
256: principal_list, NULL)) != NULL) {
257: debug3("matched principal from key options \"%.100s\"",
258: result);
1.36 djm 259: free(result);
1.24 djm 260: return 1;
261: }
262: }
263: return 0;
264: }
265:
266: static int
1.40 djm 267: match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
1.24 djm 268: {
269: FILE *f;
1.26 djm 270: char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
1.24 djm 271: u_long linenum = 0;
272: u_int i;
273:
274: temporarily_use_uid(pw);
275: debug("trying authorized principals file %s", file);
276: if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
277: restore_uid();
278: return 0;
279: }
280: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.26 djm 281: /* Skip leading whitespace. */
1.24 djm 282: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
283: ;
1.26 djm 284: /* Skip blank and comment lines. */
285: if ((ep = strchr(cp, '#')) != NULL)
286: *ep = '\0';
287: if (!*cp || *cp == '\n')
1.24 djm 288: continue;
1.26 djm 289: /* Trim trailing whitespace. */
290: ep = cp + strlen(cp) - 1;
291: while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
292: *ep-- = '\0';
293: /*
294: * If the line has internal whitespace then assume it has
295: * key options.
296: */
297: line_opts = NULL;
298: if ((ep = strrchr(cp, ' ')) != NULL ||
299: (ep = strrchr(cp, '\t')) != NULL) {
300: for (; *ep == ' ' || *ep == '\t'; ep++)
1.27 deraadt 301: ;
1.26 djm 302: line_opts = cp;
303: cp = ep;
304: }
1.24 djm 305: for (i = 0; i < cert->nprincipals; i++) {
306: if (strcmp(cp, cert->principals[i]) == 0) {
1.30 djm 307: debug3("matched principal \"%.100s\" "
308: "from file \"%s\" on line %lu",
1.31 djm 309: cert->principals[i], file, linenum);
1.26 djm 310: if (auth_parse_options(pw, line_opts,
311: file, linenum) != 1)
312: continue;
1.24 djm 313: fclose(f);
314: restore_uid();
315: return 1;
316: }
317: }
318: }
319: fclose(f);
320: restore_uid();
321: return 0;
1.31 djm 322: }
1.24 djm 323:
1.31 djm 324: /*
325: * Checks whether key is allowed in authorized_keys-format file,
326: * returns 1 if the key is allowed or 0 otherwise.
327: */
1.1 markus 328: static int
1.31 djm 329: check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
1.1 markus 330: {
1.8 dtucker 331: char line[SSH_MAX_PUBKEY_BYTES];
1.20 djm 332: const char *reason;
1.18 dtucker 333: int found_key = 0;
1.1 markus 334: u_long linenum = 0;
335: Key *found;
336: char *fp;
337:
338: found_key = 0;
339:
1.37 djm 340: found = NULL;
1.8 dtucker 341: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.7 avsm 342: char *cp, *key_options = NULL;
1.37 djm 343: if (found != NULL)
344: key_free(found);
345: found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
1.20 djm 346: auth_clear_options();
347:
1.1 markus 348: /* Skip leading whitespace, empty and comment lines. */
349: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
350: ;
351: if (!*cp || *cp == '\n' || *cp == '#')
352: continue;
353:
354: if (key_read(found, &cp) != 1) {
355: /* no key? check if there are options for this key */
356: int quoted = 0;
357: debug2("user_key_allowed: check options: '%s'", cp);
1.7 avsm 358: key_options = cp;
1.1 markus 359: for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
360: if (*cp == '\\' && cp[1] == '"')
361: cp++; /* Skip both */
362: else if (*cp == '"')
363: quoted = !quoted;
364: }
365: /* Skip remaining whitespace. */
366: for (; *cp == ' ' || *cp == '\t'; cp++)
367: ;
368: if (key_read(found, &cp) != 1) {
369: debug2("user_key_allowed: advance: '%s'", cp);
370: /* still no key? advance to next line*/
371: continue;
372: }
373: }
1.23 djm 374: if (key_is_cert(key)) {
1.25 djm 375: if (!key_equal(found, key->cert->signature_key))
376: continue;
377: if (auth_parse_options(pw, key_options, file,
378: linenum) != 1)
379: continue;
1.20 djm 380: if (!key_is_cert_authority)
381: continue;
1.43 djm 382: fp = key_fingerprint(found, options.fingerprint_hash,
383: SSH_FP_DEFAULT);
1.22 djm 384: debug("matching CA found: file %s, line %lu, %s %s",
385: file, linenum, key_type(found), fp);
1.24 djm 386: /*
387: * If the user has specified a list of principals as
388: * a key option, then prefer that list to matching
389: * their username in the certificate principals list.
390: */
391: if (authorized_principals != NULL &&
392: !match_principals_option(authorized_principals,
393: key->cert)) {
394: reason = "Certificate does not contain an "
395: "authorized principal";
396: fail_reason:
1.36 djm 397: free(fp);
1.20 djm 398: error("%s", reason);
399: auth_debug_add("%s", reason);
400: continue;
401: }
1.24 djm 402: if (key_cert_check_authority(key, 0, 0,
403: authorized_principals == NULL ? pw->pw_name : NULL,
404: &reason) != 0)
405: goto fail_reason;
1.23 djm 406: if (auth_cert_options(key, pw) != 0) {
1.36 djm 407: free(fp);
1.20 djm 408: continue;
1.22 djm 409: }
410: verbose("Accepted certificate ID \"%s\" "
411: "signed by %s CA %s via %s", key->cert->key_id,
412: key_type(found), fp, file);
1.36 djm 413: free(fp);
1.20 djm 414: found_key = 1;
415: break;
1.25 djm 416: } else if (key_equal(found, key)) {
417: if (auth_parse_options(pw, key_options, file,
418: linenum) != 1)
419: continue;
420: if (key_is_cert_authority)
421: continue;
1.1 markus 422: found_key = 1;
1.43 djm 423: fp = key_fingerprint(found, options.fingerprint_hash,
424: SSH_FP_DEFAULT);
1.37 djm 425: debug("matching key found: file %s, line %lu %s %s",
426: file, linenum, key_type(found), fp);
1.36 djm 427: free(fp);
1.1 markus 428: break;
429: }
430: }
1.37 djm 431: if (found != NULL)
432: key_free(found);
1.1 markus 433: if (!found_key)
434: debug2("key not found");
435: return found_key;
436: }
437:
1.21 djm 438: /* Authenticate a certificate key against TrustedUserCAKeys */
439: static int
440: user_cert_trusted_ca(struct passwd *pw, Key *key)
441: {
1.24 djm 442: char *ca_fp, *principals_file = NULL;
1.21 djm 443: const char *reason;
444: int ret = 0;
445:
446: if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
447: return 0;
448:
1.22 djm 449: ca_fp = key_fingerprint(key->cert->signature_key,
1.43 djm 450: options.fingerprint_hash, SSH_FP_DEFAULT);
1.21 djm 451:
1.42 djm 452: if (sshkey_in_file(key->cert->signature_key,
453: options.trusted_user_ca_keys, 1, 0) != 0) {
1.21 djm 454: debug2("%s: CA %s %s is not listed in %s", __func__,
455: key_type(key->cert->signature_key), ca_fp,
456: options.trusted_user_ca_keys);
457: goto out;
458: }
1.24 djm 459: /*
460: * If AuthorizedPrincipals is in use, then compare the certificate
461: * principals against the names in that file rather than matching
462: * against the username.
463: */
464: if ((principals_file = authorized_principals_file(pw)) != NULL) {
465: if (!match_principals_file(principals_file, pw, key->cert)) {
466: reason = "Certificate does not contain an "
467: "authorized principal";
468: fail_reason:
469: error("%s", reason);
470: auth_debug_add("%s", reason);
471: goto out;
472: }
1.21 djm 473: }
1.24 djm 474: if (key_cert_check_authority(key, 0, 1,
475: principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
476: goto fail_reason;
1.23 djm 477: if (auth_cert_options(key, pw) != 0)
1.21 djm 478: goto out;
479:
1.22 djm 480: verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
481: key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
482: options.trusted_user_ca_keys);
1.21 djm 483: ret = 1;
484:
485: out:
1.36 djm 486: free(principals_file);
487: free(ca_fp);
1.21 djm 488: return ret;
489: }
490:
1.31 djm 491: /*
492: * Checks whether key is allowed in file.
493: * returns 1 if the key is allowed or 0 otherwise.
494: */
495: static int
496: user_key_allowed2(struct passwd *pw, Key *key, char *file)
497: {
498: FILE *f;
499: int found_key = 0;
500:
501: /* Temporarily use the user's uid. */
502: temporarily_use_uid(pw);
503:
504: debug("trying public key file %s", file);
505: if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
506: found_key = check_authkeys_file(f, file, key, pw);
507: fclose(f);
508: }
509:
510: restore_uid();
511: return found_key;
512: }
513:
514: /*
515: * Checks whether key is allowed in output of command.
516: * returns 1 if the key is allowed or 0 otherwise.
517: */
518: static int
519: user_key_command_allowed2(struct passwd *user_pw, Key *key)
520: {
521: FILE *f;
522: int ok, found_key = 0;
523: struct passwd *pw;
524: struct stat st;
525: int status, devnull, p[2], i;
526: pid_t pid;
1.32 djm 527: char *username, errmsg[512];
1.31 djm 528:
529: if (options.authorized_keys_command == NULL ||
530: options.authorized_keys_command[0] != '/')
531: return 0;
532:
1.32 djm 533: if (options.authorized_keys_command_user == NULL) {
534: error("No user for AuthorizedKeysCommand specified, skipping");
535: return 0;
536: }
537:
538: username = percent_expand(options.authorized_keys_command_user,
539: "u", user_pw->pw_name, (char *)NULL);
540: pw = getpwnam(username);
541: if (pw == NULL) {
1.34 djm 542: error("AuthorizedKeysCommandUser \"%s\" not found: %s",
543: username, strerror(errno));
1.32 djm 544: free(username);
545: return 0;
1.31 djm 546: }
1.32 djm 547: free(username);
1.31 djm 548:
549: temporarily_use_uid(pw);
550:
551: if (stat(options.authorized_keys_command, &st) < 0) {
552: error("Could not stat AuthorizedKeysCommand \"%s\": %s",
553: options.authorized_keys_command, strerror(errno));
554: goto out;
555: }
556: if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
557: errmsg, sizeof(errmsg)) != 0) {
558: error("Unsafe AuthorizedKeysCommand: %s", errmsg);
559: goto out;
560: }
561:
562: if (pipe(p) != 0) {
563: error("%s: pipe: %s", __func__, strerror(errno));
564: goto out;
565: }
566:
1.33 djm 567: debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
568: options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
1.31 djm 569:
570: /*
571: * Don't want to call this in the child, where it can fatal() and
572: * run cleanup_exit() code.
573: */
574: restore_uid();
575:
576: switch ((pid = fork())) {
577: case -1: /* error */
578: error("%s: fork: %s", __func__, strerror(errno));
579: close(p[0]);
580: close(p[1]);
581: return 0;
582: case 0: /* child */
583: for (i = 0; i < NSIG; i++)
584: signal(i, SIG_DFL);
585:
1.33 djm 586: if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
587: error("%s: open %s: %s", __func__, _PATH_DEVNULL,
588: strerror(errno));
589: _exit(1);
590: }
591: /* Keep stderr around a while longer to catch errors */
592: if (dup2(devnull, STDIN_FILENO) == -1 ||
593: dup2(p[1], STDOUT_FILENO) == -1) {
594: error("%s: dup2: %s", __func__, strerror(errno));
595: _exit(1);
596: }
1.32 djm 597: closefrom(STDERR_FILENO + 1);
1.33 djm 598:
1.31 djm 599: /* Don't use permanently_set_uid() here to avoid fatal() */
600: if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
601: error("setresgid %u: %s", (u_int)pw->pw_gid,
602: strerror(errno));
603: _exit(1);
604: }
605: if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
606: error("setresuid %u: %s", (u_int)pw->pw_uid,
607: strerror(errno));
608: _exit(1);
609: }
1.33 djm 610: /* stdin is pointed to /dev/null at this point */
611: if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
1.31 djm 612: error("%s: dup2: %s", __func__, strerror(errno));
613: _exit(1);
614: }
615:
616: execl(options.authorized_keys_command,
1.33 djm 617: options.authorized_keys_command, user_pw->pw_name, NULL);
1.31 djm 618:
619: error("AuthorizedKeysCommand %s exec failed: %s",
620: options.authorized_keys_command, strerror(errno));
621: _exit(127);
622: default: /* parent */
623: break;
624: }
625:
626: temporarily_use_uid(pw);
627:
628: close(p[1]);
629: if ((f = fdopen(p[0], "r")) == NULL) {
630: error("%s: fdopen: %s", __func__, strerror(errno));
631: close(p[0]);
632: /* Don't leave zombie child */
633: kill(pid, SIGTERM);
634: while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
635: ;
636: goto out;
637: }
638: ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
639: fclose(f);
640:
641: while (waitpid(pid, &status, 0) == -1) {
642: if (errno != EINTR) {
643: error("%s: waitpid: %s", __func__, strerror(errno));
644: goto out;
645: }
646: }
647: if (WIFSIGNALED(status)) {
648: error("AuthorizedKeysCommand %s exited on signal %d",
649: options.authorized_keys_command, WTERMSIG(status));
650: goto out;
651: } else if (WEXITSTATUS(status) != 0) {
652: error("AuthorizedKeysCommand %s returned status %d",
653: options.authorized_keys_command, WEXITSTATUS(status));
654: goto out;
655: }
656: found_key = ok;
657: out:
658: restore_uid();
659: return found_key;
660: }
661:
662: /*
663: * Check whether key authenticates and authorises the user.
664: */
1.1 markus 665: int
666: user_key_allowed(struct passwd *pw, Key *key)
667: {
1.29 djm 668: u_int success, i;
1.1 markus 669: char *file;
1.21 djm 670:
671: if (auth_key_is_revoked(key))
672: return 0;
673: if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
674: return 0;
675:
676: success = user_cert_trusted_ca(pw, key);
677: if (success)
678: return success;
1.1 markus 679:
1.31 djm 680: success = user_key_command_allowed2(pw, key);
681: if (success > 0)
682: return success;
683:
1.29 djm 684: for (i = 0; !success && i < options.num_authkeys_files; i++) {
1.31 djm 685:
686: if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
687: continue;
1.29 djm 688: file = expand_authorized_keys(
689: options.authorized_keys_files[i], pw);
1.31 djm 690:
1.29 djm 691: success = user_key_allowed2(pw, key, file);
1.36 djm 692: free(file);
1.29 djm 693: }
1.1 markus 694:
695: return success;
1.44 djm 696: }
697:
698: /* Records a public key in the list of previously-successful keys */
699: void
700: auth2_record_userkey(Authctxt *authctxt, struct sshkey *key)
701: {
702: struct sshkey **tmp;
703:
704: if (authctxt->nprev_userkeys >= INT_MAX ||
705: (tmp = reallocarray(authctxt->prev_userkeys,
706: authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL)
707: fatal("%s: reallocarray failed", __func__);
708: authctxt->prev_userkeys = tmp;
709: authctxt->prev_userkeys[authctxt->nprev_userkeys] = key;
710: authctxt->nprev_userkeys++;
711: }
712:
713: /* Checks whether a key has already been used successfully for authentication */
714: int
715: auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key)
716: {
717: u_int i;
718:
719: for (i = 0; i < authctxt->nprev_userkeys; i++) {
720: if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) {
721: return 1;
722: }
723: }
724: return 0;
1.1 markus 725: }
1.2 markus 726:
727: Authmethod method_pubkey = {
728: "publickey",
729: userauth_pubkey,
730: &options.pubkey_authentication
731: };