Annotation of src/usr.bin/ssh/auth2-pubkey.c, Revision 1.46
1.46 ! djm 1: /* $OpenBSD: auth2-pubkey.c,v 1.45 2015/01/13 07:39:19 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)) {
1.46 ! djm 228: fp = sshkey_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.46 ! djm 233: key_type(key->cert->signature_key),
! 234: fp == NULL ? "(null)" : "",
1.38 djm 235: extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
1.37 djm 236: free(fp);
237: } else {
1.46 ! djm 238: fp = sshkey_fingerprint(key, options.fingerprint_hash,
1.43 djm 239: SSH_FP_DEFAULT);
1.46 ! djm 240: auth_info(authctxt, "%s %s%s%s", key_type(key),
! 241: fp == NULL ? "(null)" : "",
1.38 djm 242: extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
1.37 djm 243: free(fp);
244: }
1.38 djm 245: free(extra);
1.37 djm 246: }
247:
1.24 djm 248: static int
1.40 djm 249: match_principals_option(const char *principal_list, struct sshkey_cert *cert)
1.24 djm 250: {
251: char *result;
252: u_int i;
253:
254: /* XXX percent_expand() sequences for authorized_principals? */
255:
256: for (i = 0; i < cert->nprincipals; i++) {
257: if ((result = match_list(cert->principals[i],
258: principal_list, NULL)) != NULL) {
259: debug3("matched principal from key options \"%.100s\"",
260: result);
1.36 djm 261: free(result);
1.24 djm 262: return 1;
263: }
264: }
265: return 0;
266: }
267:
268: static int
1.40 djm 269: match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
1.24 djm 270: {
271: FILE *f;
1.26 djm 272: char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
1.24 djm 273: u_long linenum = 0;
274: u_int i;
275:
276: temporarily_use_uid(pw);
277: debug("trying authorized principals file %s", file);
278: if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
279: restore_uid();
280: return 0;
281: }
282: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.26 djm 283: /* Skip leading whitespace. */
1.24 djm 284: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
285: ;
1.26 djm 286: /* Skip blank and comment lines. */
287: if ((ep = strchr(cp, '#')) != NULL)
288: *ep = '\0';
289: if (!*cp || *cp == '\n')
1.24 djm 290: continue;
1.26 djm 291: /* Trim trailing whitespace. */
292: ep = cp + strlen(cp) - 1;
293: while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
294: *ep-- = '\0';
295: /*
296: * If the line has internal whitespace then assume it has
297: * key options.
298: */
299: line_opts = NULL;
300: if ((ep = strrchr(cp, ' ')) != NULL ||
301: (ep = strrchr(cp, '\t')) != NULL) {
302: for (; *ep == ' ' || *ep == '\t'; ep++)
1.27 deraadt 303: ;
1.26 djm 304: line_opts = cp;
305: cp = ep;
306: }
1.24 djm 307: for (i = 0; i < cert->nprincipals; i++) {
308: if (strcmp(cp, cert->principals[i]) == 0) {
1.30 djm 309: debug3("matched principal \"%.100s\" "
310: "from file \"%s\" on line %lu",
1.31 djm 311: cert->principals[i], file, linenum);
1.26 djm 312: if (auth_parse_options(pw, line_opts,
313: file, linenum) != 1)
314: continue;
1.24 djm 315: fclose(f);
316: restore_uid();
317: return 1;
318: }
319: }
320: }
321: fclose(f);
322: restore_uid();
323: return 0;
1.31 djm 324: }
1.24 djm 325:
1.31 djm 326: /*
327: * Checks whether key is allowed in authorized_keys-format file,
328: * returns 1 if the key is allowed or 0 otherwise.
329: */
1.1 markus 330: static int
1.31 djm 331: check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
1.1 markus 332: {
1.8 dtucker 333: char line[SSH_MAX_PUBKEY_BYTES];
1.20 djm 334: const char *reason;
1.18 dtucker 335: int found_key = 0;
1.1 markus 336: u_long linenum = 0;
337: Key *found;
338: char *fp;
339:
340: found_key = 0;
341:
1.37 djm 342: found = NULL;
1.8 dtucker 343: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.7 avsm 344: char *cp, *key_options = NULL;
1.37 djm 345: if (found != NULL)
346: key_free(found);
347: found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
1.20 djm 348: auth_clear_options();
349:
1.1 markus 350: /* Skip leading whitespace, empty and comment lines. */
351: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
352: ;
353: if (!*cp || *cp == '\n' || *cp == '#')
354: continue;
355:
356: if (key_read(found, &cp) != 1) {
357: /* no key? check if there are options for this key */
358: int quoted = 0;
359: debug2("user_key_allowed: check options: '%s'", cp);
1.7 avsm 360: key_options = cp;
1.1 markus 361: for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
362: if (*cp == '\\' && cp[1] == '"')
363: cp++; /* Skip both */
364: else if (*cp == '"')
365: quoted = !quoted;
366: }
367: /* Skip remaining whitespace. */
368: for (; *cp == ' ' || *cp == '\t'; cp++)
369: ;
370: if (key_read(found, &cp) != 1) {
371: debug2("user_key_allowed: advance: '%s'", cp);
372: /* still no key? advance to next line*/
373: continue;
374: }
375: }
1.23 djm 376: if (key_is_cert(key)) {
1.25 djm 377: if (!key_equal(found, key->cert->signature_key))
378: continue;
379: if (auth_parse_options(pw, key_options, file,
380: linenum) != 1)
381: continue;
1.20 djm 382: if (!key_is_cert_authority)
383: continue;
1.46 ! djm 384: if ((fp = sshkey_fingerprint(found,
! 385: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
! 386: continue;
1.22 djm 387: debug("matching CA found: file %s, line %lu, %s %s",
388: file, linenum, key_type(found), fp);
1.24 djm 389: /*
390: * If the user has specified a list of principals as
391: * a key option, then prefer that list to matching
392: * their username in the certificate principals list.
393: */
394: if (authorized_principals != NULL &&
395: !match_principals_option(authorized_principals,
396: key->cert)) {
397: reason = "Certificate does not contain an "
398: "authorized principal";
399: fail_reason:
1.36 djm 400: free(fp);
1.20 djm 401: error("%s", reason);
402: auth_debug_add("%s", reason);
403: continue;
404: }
1.24 djm 405: if (key_cert_check_authority(key, 0, 0,
406: authorized_principals == NULL ? pw->pw_name : NULL,
407: &reason) != 0)
408: goto fail_reason;
1.23 djm 409: if (auth_cert_options(key, pw) != 0) {
1.36 djm 410: free(fp);
1.20 djm 411: continue;
1.22 djm 412: }
413: verbose("Accepted certificate ID \"%s\" "
414: "signed by %s CA %s via %s", key->cert->key_id,
415: key_type(found), fp, file);
1.36 djm 416: free(fp);
1.20 djm 417: found_key = 1;
418: break;
1.25 djm 419: } else if (key_equal(found, key)) {
420: if (auth_parse_options(pw, key_options, file,
421: linenum) != 1)
422: continue;
423: if (key_is_cert_authority)
424: continue;
1.46 ! djm 425: if ((fp = sshkey_fingerprint(found,
! 426: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
! 427: continue;
1.37 djm 428: debug("matching key found: file %s, line %lu %s %s",
429: file, linenum, key_type(found), fp);
1.36 djm 430: free(fp);
1.46 ! djm 431: found_key = 1;
1.1 markus 432: break;
433: }
434: }
1.37 djm 435: if (found != NULL)
436: key_free(found);
1.1 markus 437: if (!found_key)
438: debug2("key not found");
439: return found_key;
440: }
441:
1.21 djm 442: /* Authenticate a certificate key against TrustedUserCAKeys */
443: static int
444: user_cert_trusted_ca(struct passwd *pw, Key *key)
445: {
1.24 djm 446: char *ca_fp, *principals_file = NULL;
1.21 djm 447: const char *reason;
448: int ret = 0;
449:
450: if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
451: return 0;
452:
1.46 ! djm 453: if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
! 454: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
! 455: return 0;
1.21 djm 456:
1.42 djm 457: if (sshkey_in_file(key->cert->signature_key,
458: options.trusted_user_ca_keys, 1, 0) != 0) {
1.21 djm 459: debug2("%s: CA %s %s is not listed in %s", __func__,
460: key_type(key->cert->signature_key), ca_fp,
461: options.trusted_user_ca_keys);
462: goto out;
463: }
1.24 djm 464: /*
465: * If AuthorizedPrincipals is in use, then compare the certificate
466: * principals against the names in that file rather than matching
467: * against the username.
468: */
469: if ((principals_file = authorized_principals_file(pw)) != NULL) {
470: if (!match_principals_file(principals_file, pw, key->cert)) {
471: reason = "Certificate does not contain an "
472: "authorized principal";
473: fail_reason:
474: error("%s", reason);
475: auth_debug_add("%s", reason);
476: goto out;
477: }
1.21 djm 478: }
1.24 djm 479: if (key_cert_check_authority(key, 0, 1,
480: principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
481: goto fail_reason;
1.23 djm 482: if (auth_cert_options(key, pw) != 0)
1.21 djm 483: goto out;
484:
1.22 djm 485: verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
486: key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
487: options.trusted_user_ca_keys);
1.21 djm 488: ret = 1;
489:
490: out:
1.36 djm 491: free(principals_file);
492: free(ca_fp);
1.21 djm 493: return ret;
494: }
495:
1.31 djm 496: /*
497: * Checks whether key is allowed in file.
498: * returns 1 if the key is allowed or 0 otherwise.
499: */
500: static int
501: user_key_allowed2(struct passwd *pw, Key *key, char *file)
502: {
503: FILE *f;
504: int found_key = 0;
505:
506: /* Temporarily use the user's uid. */
507: temporarily_use_uid(pw);
508:
509: debug("trying public key file %s", file);
510: if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
511: found_key = check_authkeys_file(f, file, key, pw);
512: fclose(f);
513: }
514:
515: restore_uid();
516: return found_key;
517: }
518:
519: /*
520: * Checks whether key is allowed in output of command.
521: * returns 1 if the key is allowed or 0 otherwise.
522: */
523: static int
524: user_key_command_allowed2(struct passwd *user_pw, Key *key)
525: {
526: FILE *f;
527: int ok, found_key = 0;
528: struct passwd *pw;
529: struct stat st;
530: int status, devnull, p[2], i;
531: pid_t pid;
1.32 djm 532: char *username, errmsg[512];
1.31 djm 533:
534: if (options.authorized_keys_command == NULL ||
535: options.authorized_keys_command[0] != '/')
536: return 0;
537:
1.32 djm 538: if (options.authorized_keys_command_user == NULL) {
539: error("No user for AuthorizedKeysCommand specified, skipping");
540: return 0;
541: }
542:
543: username = percent_expand(options.authorized_keys_command_user,
544: "u", user_pw->pw_name, (char *)NULL);
545: pw = getpwnam(username);
546: if (pw == NULL) {
1.34 djm 547: error("AuthorizedKeysCommandUser \"%s\" not found: %s",
548: username, strerror(errno));
1.32 djm 549: free(username);
550: return 0;
1.31 djm 551: }
1.32 djm 552: free(username);
1.31 djm 553:
554: temporarily_use_uid(pw);
555:
556: if (stat(options.authorized_keys_command, &st) < 0) {
557: error("Could not stat AuthorizedKeysCommand \"%s\": %s",
558: options.authorized_keys_command, strerror(errno));
559: goto out;
560: }
561: if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
562: errmsg, sizeof(errmsg)) != 0) {
563: error("Unsafe AuthorizedKeysCommand: %s", errmsg);
564: goto out;
565: }
566:
567: if (pipe(p) != 0) {
568: error("%s: pipe: %s", __func__, strerror(errno));
569: goto out;
570: }
571:
1.33 djm 572: debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
573: options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
1.31 djm 574:
575: /*
576: * Don't want to call this in the child, where it can fatal() and
577: * run cleanup_exit() code.
578: */
579: restore_uid();
580:
581: switch ((pid = fork())) {
582: case -1: /* error */
583: error("%s: fork: %s", __func__, strerror(errno));
584: close(p[0]);
585: close(p[1]);
586: return 0;
587: case 0: /* child */
588: for (i = 0; i < NSIG; i++)
589: signal(i, SIG_DFL);
590:
1.33 djm 591: if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
592: error("%s: open %s: %s", __func__, _PATH_DEVNULL,
593: strerror(errno));
594: _exit(1);
595: }
596: /* Keep stderr around a while longer to catch errors */
597: if (dup2(devnull, STDIN_FILENO) == -1 ||
598: dup2(p[1], STDOUT_FILENO) == -1) {
599: error("%s: dup2: %s", __func__, strerror(errno));
600: _exit(1);
601: }
1.32 djm 602: closefrom(STDERR_FILENO + 1);
1.33 djm 603:
1.31 djm 604: /* Don't use permanently_set_uid() here to avoid fatal() */
605: if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
606: error("setresgid %u: %s", (u_int)pw->pw_gid,
607: strerror(errno));
608: _exit(1);
609: }
610: if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
611: error("setresuid %u: %s", (u_int)pw->pw_uid,
612: strerror(errno));
613: _exit(1);
614: }
1.33 djm 615: /* stdin is pointed to /dev/null at this point */
616: if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
1.31 djm 617: error("%s: dup2: %s", __func__, strerror(errno));
618: _exit(1);
619: }
620:
621: execl(options.authorized_keys_command,
1.33 djm 622: options.authorized_keys_command, user_pw->pw_name, NULL);
1.31 djm 623:
624: error("AuthorizedKeysCommand %s exec failed: %s",
625: options.authorized_keys_command, strerror(errno));
626: _exit(127);
627: default: /* parent */
628: break;
629: }
630:
631: temporarily_use_uid(pw);
632:
633: close(p[1]);
634: if ((f = fdopen(p[0], "r")) == NULL) {
635: error("%s: fdopen: %s", __func__, strerror(errno));
636: close(p[0]);
637: /* Don't leave zombie child */
638: kill(pid, SIGTERM);
639: while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
640: ;
641: goto out;
642: }
643: ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
644: fclose(f);
645:
646: while (waitpid(pid, &status, 0) == -1) {
647: if (errno != EINTR) {
648: error("%s: waitpid: %s", __func__, strerror(errno));
649: goto out;
650: }
651: }
652: if (WIFSIGNALED(status)) {
653: error("AuthorizedKeysCommand %s exited on signal %d",
654: options.authorized_keys_command, WTERMSIG(status));
655: goto out;
656: } else if (WEXITSTATUS(status) != 0) {
657: error("AuthorizedKeysCommand %s returned status %d",
658: options.authorized_keys_command, WEXITSTATUS(status));
659: goto out;
660: }
661: found_key = ok;
662: out:
663: restore_uid();
664: return found_key;
665: }
666:
667: /*
668: * Check whether key authenticates and authorises the user.
669: */
1.1 markus 670: int
671: user_key_allowed(struct passwd *pw, Key *key)
672: {
1.29 djm 673: u_int success, i;
1.1 markus 674: char *file;
1.21 djm 675:
676: if (auth_key_is_revoked(key))
677: return 0;
678: if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
679: return 0;
680:
681: success = user_cert_trusted_ca(pw, key);
682: if (success)
683: return success;
1.1 markus 684:
1.31 djm 685: success = user_key_command_allowed2(pw, key);
686: if (success > 0)
687: return success;
688:
1.29 djm 689: for (i = 0; !success && i < options.num_authkeys_files; i++) {
1.31 djm 690:
691: if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
692: continue;
1.29 djm 693: file = expand_authorized_keys(
694: options.authorized_keys_files[i], pw);
1.31 djm 695:
1.29 djm 696: success = user_key_allowed2(pw, key, file);
1.36 djm 697: free(file);
1.29 djm 698: }
1.1 markus 699:
700: return success;
1.44 djm 701: }
702:
703: /* Records a public key in the list of previously-successful keys */
704: void
705: auth2_record_userkey(Authctxt *authctxt, struct sshkey *key)
706: {
707: struct sshkey **tmp;
708:
709: if (authctxt->nprev_userkeys >= INT_MAX ||
710: (tmp = reallocarray(authctxt->prev_userkeys,
711: authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL)
712: fatal("%s: reallocarray failed", __func__);
713: authctxt->prev_userkeys = tmp;
714: authctxt->prev_userkeys[authctxt->nprev_userkeys] = key;
715: authctxt->nprev_userkeys++;
716: }
717:
718: /* Checks whether a key has already been used successfully for authentication */
719: int
720: auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key)
721: {
722: u_int i;
723:
724: for (i = 0; i < authctxt->nprev_userkeys; i++) {
725: if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) {
726: return 1;
727: }
728: }
729: return 0;
1.1 markus 730: }
1.2 markus 731:
732: Authmethod method_pubkey = {
733: "publickey",
734: userauth_pubkey,
735: &options.pubkey_authentication
736: };