xref: /netbsd-src/crypto/external/bsd/openssh/dist/auth2-pubkey.c (revision 7788a0781fe6ff2cce37368b4578a7ade0850cb1)
1 /*	$NetBSD: auth2-pubkey.c,v 1.8 2013/03/29 16:19:44 christos Exp $	*/
2 /* $OpenBSD: auth2-pubkey.c,v 1.34 2013/02/14 21:35:59 djm Exp $ */
3 /*
4  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "includes.h"
28 __RCSID("$NetBSD: auth2-pubkey.c,v 1.8 2013/03/29 16:19:44 christos Exp $");
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <paths.h>
36 #include <pwd.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <time.h>
42 #include <unistd.h>
43 
44 #include "xmalloc.h"
45 #include "ssh.h"
46 #include "ssh2.h"
47 #include "packet.h"
48 #include "buffer.h"
49 #include "log.h"
50 #include "servconf.h"
51 #include "compat.h"
52 #include "key.h"
53 #include "hostfile.h"
54 #include "auth.h"
55 #include "pathnames.h"
56 #include "uidswap.h"
57 #include "auth-options.h"
58 #include "canohost.h"
59 #ifdef GSSAPI
60 #include "ssh-gss.h"
61 #endif
62 #include "monitor_wrap.h"
63 #include "misc.h"
64 #include "authfile.h"
65 #include "match.h"
66 
67 #ifdef WITH_LDAP_PUBKEY
68 #include "ldapauth.h"
69 #endif
70 
71 /* import */
72 extern ServerOptions options;
73 extern u_char *session_id2;
74 extern u_int session_id2_len;
75 
76 static int
77 userauth_pubkey(Authctxt *authctxt)
78 {
79 	Buffer b;
80 	Key *key = NULL;
81 	char *pkalg;
82 	u_char *pkblob, *sig;
83 	u_int alen, blen, slen;
84 	int have_sig, pktype;
85 	int authenticated = 0;
86 
87 	if (!authctxt->valid) {
88 		debug2("userauth_pubkey: disabled because of invalid user");
89 		return 0;
90 	}
91 	have_sig = packet_get_char();
92 	if (datafellows & SSH_BUG_PKAUTH) {
93 		debug2("userauth_pubkey: SSH_BUG_PKAUTH");
94 		/* no explicit pkalg given */
95 		pkblob = packet_get_string(&blen);
96 		buffer_init(&b);
97 		buffer_append(&b, pkblob, blen);
98 		/* so we have to extract the pkalg from the pkblob */
99 		pkalg = buffer_get_string(&b, &alen);
100 		buffer_free(&b);
101 	} else {
102 		pkalg = packet_get_string(&alen);
103 		pkblob = packet_get_string(&blen);
104 	}
105 	pktype = key_type_from_name(pkalg);
106 	if (pktype == KEY_UNSPEC) {
107 		/* this is perfectly legal */
108 		logit("userauth_pubkey: unsupported public key algorithm: %s",
109 		    pkalg);
110 		goto done;
111 	}
112 	key = key_from_blob(pkblob, blen);
113 	if (key == NULL) {
114 		error("userauth_pubkey: cannot decode key: %s", pkalg);
115 		goto done;
116 	}
117 	if (key->type != pktype) {
118 		error("userauth_pubkey: type mismatch for decoded key "
119 		    "(received %d, expected %d)", key->type, pktype);
120 		goto done;
121 	}
122 	if (have_sig) {
123 		sig = packet_get_string(&slen);
124 		packet_check_eom();
125 		buffer_init(&b);
126 		if (datafellows & SSH_OLD_SESSIONID) {
127 			buffer_append(&b, session_id2, session_id2_len);
128 		} else {
129 			buffer_put_string(&b, session_id2, session_id2_len);
130 		}
131 		/* reconstruct packet */
132 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
133 		buffer_put_cstring(&b, authctxt->user);
134 		buffer_put_cstring(&b,
135 		    datafellows & SSH_BUG_PKSERVICE ?
136 		    "ssh-userauth" :
137 		    authctxt->service);
138 		if (datafellows & SSH_BUG_PKAUTH) {
139 			buffer_put_char(&b, have_sig);
140 		} else {
141 			buffer_put_cstring(&b, "publickey");
142 			buffer_put_char(&b, have_sig);
143 			buffer_put_cstring(&b, pkalg);
144 		}
145 		buffer_put_string(&b, pkblob, blen);
146 #ifdef DEBUG_PK
147 		buffer_dump(&b);
148 #endif
149 		/* test for correct signature */
150 		authenticated = 0;
151 		if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
152 		    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
153 		    buffer_len(&b))) == 1)
154 			authenticated = 1;
155 		buffer_free(&b);
156 		xfree(sig);
157 	} else {
158 		debug("test whether pkalg/pkblob are acceptable");
159 		packet_check_eom();
160 
161 		/* XXX fake reply and always send PK_OK ? */
162 		/*
163 		 * XXX this allows testing whether a user is allowed
164 		 * to login: if you happen to have a valid pubkey this
165 		 * message is sent. the message is NEVER sent at all
166 		 * if a user is not allowed to login. is this an
167 		 * issue? -markus
168 		 */
169 		if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
170 			packet_start(SSH2_MSG_USERAUTH_PK_OK);
171 			packet_put_string(pkalg, alen);
172 			packet_put_string(pkblob, blen);
173 			packet_send();
174 			packet_write_wait();
175 			authctxt->postponed = 1;
176 		}
177 	}
178 	if (authenticated != 1)
179 		auth_clear_options();
180 done:
181 	debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
182 	if (key != NULL)
183 		key_free(key);
184 	xfree(pkalg);
185 	xfree(pkblob);
186 	return authenticated;
187 }
188 
189 static int
190 match_principals_option(const char *principal_list, struct KeyCert *cert)
191 {
192 	char *result;
193 	u_int i;
194 
195 	/* XXX percent_expand() sequences for authorized_principals? */
196 
197 	for (i = 0; i < cert->nprincipals; i++) {
198 		if ((result = match_list(cert->principals[i],
199 		    principal_list, NULL)) != NULL) {
200 			debug3("matched principal from key options \"%.100s\"",
201 			    result);
202 			xfree(result);
203 			return 1;
204 		}
205 	}
206 	return 0;
207 }
208 
209 static int
210 match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert)
211 {
212 	FILE *f;
213 	char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
214 	u_long linenum = 0;
215 	u_int i;
216 
217 	temporarily_use_uid(pw);
218 	debug("trying authorized principals file %s", file);
219 	if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
220 		restore_uid();
221 		return 0;
222 	}
223 	while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
224 		/* Skip leading whitespace. */
225 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
226 			;
227 		/* Skip blank and comment lines. */
228 		if ((ep = strchr(cp, '#')) != NULL)
229 			*ep = '\0';
230 		if (!*cp || *cp == '\n')
231 			continue;
232 		/* Trim trailing whitespace. */
233 		ep = cp + strlen(cp) - 1;
234 		while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
235 			*ep-- = '\0';
236 		/*
237 		 * If the line has internal whitespace then assume it has
238 		 * key options.
239 		 */
240 		line_opts = NULL;
241 		if ((ep = strrchr(cp, ' ')) != NULL ||
242 		    (ep = strrchr(cp, '\t')) != NULL) {
243 			for (; *ep == ' ' || *ep == '\t'; ep++)
244 				;
245 			line_opts = cp;
246 			cp = ep;
247 		}
248 		for (i = 0; i < cert->nprincipals; i++) {
249 			if (strcmp(cp, cert->principals[i]) == 0) {
250 				debug3("matched principal \"%.100s\" "
251 				    "from file \"%s\" on line %lu",
252 				    cert->principals[i], file, linenum);
253 				if (auth_parse_options(pw, line_opts,
254 				    file, linenum) != 1)
255 					continue;
256 				fclose(f);
257 				restore_uid();
258 				return 1;
259 			}
260 		}
261 	}
262 	fclose(f);
263 	restore_uid();
264 	return 0;
265 }
266 
267 /*
268  * Checks whether key is allowed in authorized_keys-format file,
269  * returns 1 if the key is allowed or 0 otherwise.
270  */
271 static int
272 check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
273 {
274 	char line[SSH_MAX_PUBKEY_BYTES];
275 	const char *reason;
276 	int found_key = 0;
277 	u_long linenum = 0;
278 	Key *found;
279 	char *fp;
280 #ifdef WITH_LDAP_PUBKEY
281 	ldap_key_t * k;
282 	unsigned int i = 0;
283 #endif
284 
285 #ifdef WITH_LDAP_PUBKEY
286 	found_key = 0;
287 	/* allocate a new key type */
288 	found = key_new(key->type);
289 
290 	/* first check if the options is enabled, then try.. */
291 	if (options.lpk.on) {
292 	    debug("[LDAP] trying LDAP first uid=%s",pw->pw_name);
293 	    if (ldap_ismember(&options.lpk, pw->pw_name) > 0) {
294 		if ((k = ldap_getuserkey(&options.lpk, pw->pw_name)) != NULL) {
295 		    /* Skip leading whitespace, empty and comment lines. */
296 		    for (i = 0 ; i < k->num ; i++) {
297 			/* dont forget if multiple keys to reset options */
298 			char *cp, *xoptions = NULL;
299 
300 			for (cp = (char *)k->keys[i]->bv_val; *cp == ' ' || *cp == '\t'; cp++)
301 			    ;
302 			if (!*cp || *cp == '\n' || *cp == '#')
303 			    continue;
304 
305 			if (key_read(found, &cp) != 1) {
306 			    /* no key?  check if there are options for this key */
307 			    int quoted = 0;
308 			    debug2("[LDAP] user_key_allowed: check options: '%s'", cp);
309 			    xoptions = cp;
310 			    for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
311 				if (*cp == '\\' && cp[1] == '"')
312 				    cp++;	/* Skip both */
313 				else if (*cp == '"')
314 				    quoted = !quoted;
315 			    }
316 			    /* Skip remaining whitespace. */
317 			    for (; *cp == ' ' || *cp == '\t'; cp++)
318 				;
319 			    if (key_read(found, &cp) != 1) {
320 				debug2("[LDAP] user_key_allowed: advance: '%s'", cp);
321 				/* still no key?  advance to next line*/
322 				continue;
323 			    }
324 			}
325 
326 			if (key_equal(found, key) &&
327 				auth_parse_options(pw, xoptions, file, linenum) == 1) {
328 			    found_key = 1;
329 			    debug("[LDAP] matching key found");
330 			    fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
331 			    verbose("[LDAP] Found matching %s key: %s", key_type(found), fp);
332 
333 			    /* restoring memory */
334 			    ldap_keys_free(k);
335 			    xfree(fp);
336 			    restore_uid();
337 			    key_free(found);
338 			    return found_key;
339 			    break;
340 			}
341 		    }/* end of LDAP for() */
342 		} else {
343 		    logit("[LDAP] no keys found for '%s'!", pw->pw_name);
344 		}
345 	    } else {
346 		logit("[LDAP] '%s' is not in '%s'", pw->pw_name, options.lpk.sgroup);
347 	    }
348 	}
349 #endif
350 	debug("trying public key file %s", file);
351 	f = auth_openkeyfile(file, pw, options.strict_modes);
352 
353 	if (!f) {
354 		restore_uid();
355 		return 0;
356 	}
357 
358 	found_key = 0;
359 	found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
360 
361 	while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
362 		char *cp, *key_options = NULL;
363 
364 		auth_clear_options();
365 
366 		/* Skip leading whitespace, empty and comment lines. */
367 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
368 			;
369 		if (!*cp || *cp == '\n' || *cp == '#')
370 			continue;
371 
372 		if (key_read(found, &cp) != 1) {
373 			/* no key?  check if there are options for this key */
374 			int quoted = 0;
375 			debug2("user_key_allowed: check options: '%s'", cp);
376 			key_options = cp;
377 			for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
378 				if (*cp == '\\' && cp[1] == '"')
379 					cp++;	/* Skip both */
380 				else if (*cp == '"')
381 					quoted = !quoted;
382 			}
383 			/* Skip remaining whitespace. */
384 			for (; *cp == ' ' || *cp == '\t'; cp++)
385 				;
386 			if (key_read(found, &cp) != 1) {
387 				debug2("user_key_allowed: advance: '%s'", cp);
388 				/* still no key?  advance to next line*/
389 				continue;
390 			}
391 		}
392 		if (key_is_cert(key)) {
393 			if (!key_equal(found, key->cert->signature_key))
394 				continue;
395 			if (auth_parse_options(pw, key_options, file,
396 			    linenum) != 1)
397 				continue;
398 			if (!key_is_cert_authority)
399 				continue;
400 			fp = key_fingerprint(found, SSH_FP_MD5,
401 			    SSH_FP_HEX);
402 			debug("matching CA found: file %s, line %lu, %s %s",
403 			    file, linenum, key_type(found), fp);
404 			/*
405 			 * If the user has specified a list of principals as
406 			 * a key option, then prefer that list to matching
407 			 * their username in the certificate principals list.
408 			 */
409 			if (authorized_principals != NULL &&
410 			    !match_principals_option(authorized_principals,
411 			    key->cert)) {
412 				reason = "Certificate does not contain an "
413 				    "authorized principal";
414  fail_reason:
415 				xfree(fp);
416 				error("%s", reason);
417 				auth_debug_add("%s", reason);
418 				continue;
419 			}
420 			if (key_cert_check_authority(key, 0, 0,
421 			    authorized_principals == NULL ? pw->pw_name : NULL,
422 			    &reason) != 0)
423 				goto fail_reason;
424 			if (auth_cert_options(key, pw) != 0) {
425 				xfree(fp);
426 				continue;
427 			}
428 			verbose("Accepted certificate ID \"%s\" "
429 			    "signed by %s CA %s via %s", key->cert->key_id,
430 			    key_type(found), fp, file);
431 			xfree(fp);
432 			found_key = 1;
433 			break;
434 		} else if (key_equal(found, key)) {
435 			if (auth_parse_options(pw, key_options, file,
436 			    linenum) != 1)
437 				continue;
438 			if (key_is_cert_authority)
439 				continue;
440 			found_key = 1;
441 			debug("matching key found: file %s, line %lu",
442 			    file, linenum);
443 			fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
444 			verbose("Found matching %s key: %s",
445 			    key_type(found), fp);
446 			xfree(fp);
447 			break;
448 		}
449 	}
450 	key_free(found);
451 	if (!found_key)
452 		debug2("key not found");
453 	return found_key;
454 }
455 
456 /* Authenticate a certificate key against TrustedUserCAKeys */
457 static int
458 user_cert_trusted_ca(struct passwd *pw, Key *key)
459 {
460 	char *ca_fp, *principals_file = NULL;
461 	const char *reason;
462 	int ret = 0;
463 
464 	if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
465 		return 0;
466 
467 	ca_fp = key_fingerprint(key->cert->signature_key,
468 	    SSH_FP_MD5, SSH_FP_HEX);
469 
470 	if (key_in_file(key->cert->signature_key,
471 	    options.trusted_user_ca_keys, 1) != 1) {
472 		debug2("%s: CA %s %s is not listed in %s", __func__,
473 		    key_type(key->cert->signature_key), ca_fp,
474 		    options.trusted_user_ca_keys);
475 		goto out;
476 	}
477 	/*
478 	 * If AuthorizedPrincipals is in use, then compare the certificate
479 	 * principals against the names in that file rather than matching
480 	 * against the username.
481 	 */
482 	if ((principals_file = authorized_principals_file(pw)) != NULL) {
483 		if (!match_principals_file(principals_file, pw, key->cert)) {
484 			reason = "Certificate does not contain an "
485 			    "authorized principal";
486  fail_reason:
487 			error("%s", reason);
488 			auth_debug_add("%s", reason);
489 			goto out;
490 		}
491 	}
492 	if (key_cert_check_authority(key, 0, 1,
493 	    principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
494 		goto fail_reason;
495 	if (auth_cert_options(key, pw) != 0)
496 		goto out;
497 
498 	verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
499 	    key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
500 	    options.trusted_user_ca_keys);
501 	ret = 1;
502 
503  out:
504 	if (principals_file != NULL)
505 		xfree(principals_file);
506 	if (ca_fp != NULL)
507 		xfree(ca_fp);
508 	return ret;
509 }
510 
511 /*
512  * Checks whether key is allowed in file.
513  * returns 1 if the key is allowed or 0 otherwise.
514  */
515 static int
516 user_key_allowed2(struct passwd *pw, Key *key, char *file)
517 {
518 	FILE *f;
519 	int found_key = 0;
520 
521 	/* Temporarily use the user's uid. */
522 	temporarily_use_uid(pw);
523 
524 	debug("trying public key file %s", file);
525 	if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
526 		found_key = check_authkeys_file(f, file, key, pw);
527 		fclose(f);
528 	}
529 
530 	restore_uid();
531 	return found_key;
532 }
533 
534 /*
535  * Checks whether key is allowed in output of command.
536  * returns 1 if the key is allowed or 0 otherwise.
537  */
538 static int
539 user_key_command_allowed2(struct passwd *user_pw, Key *key)
540 {
541 	FILE *f;
542 	int ok, found_key = 0;
543 	struct passwd *pw;
544 	struct stat st;
545 	int status, devnull, p[2], i;
546 	pid_t pid;
547 	char *username, errmsg[512];
548 
549 	if (options.authorized_keys_command == NULL ||
550 	    options.authorized_keys_command[0] != '/')
551 		return 0;
552 
553 	if (options.authorized_keys_command_user == NULL) {
554 		error("No user for AuthorizedKeysCommand specified, skipping");
555 		return 0;
556 	}
557 
558 	username = percent_expand(options.authorized_keys_command_user,
559 	    "u", user_pw->pw_name, (char *)NULL);
560 	pw = getpwnam(username);
561 	if (pw == NULL) {
562 		error("AuthorizedKeysCommandUser \"%s\" not found: %s",
563 		    username, strerror(errno));
564 		free(username);
565 		return 0;
566 	}
567 	free(username);
568 
569 	temporarily_use_uid(pw);
570 
571 	if (stat(options.authorized_keys_command, &st) < 0) {
572 		error("Could not stat AuthorizedKeysCommand \"%s\": %s",
573 		    options.authorized_keys_command, strerror(errno));
574 		goto out;
575 	}
576 	if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
577 	    errmsg, sizeof(errmsg)) != 0) {
578 		error("Unsafe AuthorizedKeysCommand: %s", errmsg);
579 		goto out;
580 	}
581 
582 	if (pipe(p) != 0) {
583 		error("%s: pipe: %s", __func__, strerror(errno));
584 		goto out;
585 	}
586 
587 	debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
588 	    options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
589 
590 	/*
591 	 * Don't want to call this in the child, where it can fatal() and
592 	 * run cleanup_exit() code.
593 	 */
594 	restore_uid();
595 
596 	switch ((pid = fork())) {
597 	case -1: /* error */
598 		error("%s: fork: %s", __func__, strerror(errno));
599 		close(p[0]);
600 		close(p[1]);
601 		return 0;
602 	case 0: /* child */
603 		for (i = 0; i < NSIG; i++)
604 			signal(i, SIG_DFL);
605 
606 		if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
607 			error("%s: open %s: %s", __func__, _PATH_DEVNULL,
608 			    strerror(errno));
609 			_exit(1);
610 		}
611 		/* Keep stderr around a while longer to catch errors */
612 		if (dup2(devnull, STDIN_FILENO) == -1 ||
613 		    dup2(p[1], STDOUT_FILENO) == -1) {
614 			error("%s: dup2: %s", __func__, strerror(errno));
615 			_exit(1);
616 		}
617 		closefrom(STDERR_FILENO + 1);
618 
619 		/* Don't use permanently_set_uid() here to avoid fatal() */
620 		if (setgid(pw->pw_gid) != 0) {
621 			error("setgid %u: %s", (u_int)pw->pw_gid,
622 			    strerror(errno));
623 			_exit(1);
624 		}
625 		if (setuid(pw->pw_uid) != 0) {
626 			error("setuid %u: %s", (u_int)pw->pw_uid,
627 			    strerror(errno));
628 			_exit(1);
629 		}
630 		/* stdin is pointed to /dev/null at this point */
631 		if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
632 			error("%s: dup2: %s", __func__, strerror(errno));
633 			_exit(1);
634 		}
635 
636 		execl(options.authorized_keys_command,
637 		    options.authorized_keys_command, user_pw->pw_name, NULL);
638 
639 		error("AuthorizedKeysCommand %s exec failed: %s",
640 		    options.authorized_keys_command, strerror(errno));
641 		_exit(127);
642 	default: /* parent */
643 		break;
644 	}
645 
646 	temporarily_use_uid(pw);
647 
648 	close(p[1]);
649 	if ((f = fdopen(p[0], "r")) == NULL) {
650 		error("%s: fdopen: %s", __func__, strerror(errno));
651 		close(p[0]);
652 		/* Don't leave zombie child */
653 		kill(pid, SIGTERM);
654 		while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
655 			;
656 		goto out;
657 	}
658 	ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
659 	fclose(f);
660 
661 	while (waitpid(pid, &status, 0) == -1) {
662 		if (errno != EINTR) {
663 			error("%s: waitpid: %s", __func__, strerror(errno));
664 			goto out;
665 		}
666 	}
667 	if (WIFSIGNALED(status)) {
668 		error("AuthorizedKeysCommand %s exited on signal %d",
669 		    options.authorized_keys_command, WTERMSIG(status));
670 		goto out;
671 	} else if (WEXITSTATUS(status) != 0) {
672 		error("AuthorizedKeysCommand %s returned status %d",
673 		    options.authorized_keys_command, WEXITSTATUS(status));
674 		goto out;
675 	}
676 	found_key = ok;
677  out:
678 	restore_uid();
679 	return found_key;
680 }
681 
682 /*
683  * Check whether key authenticates and authorises the user.
684  */
685 int
686 user_key_allowed(struct passwd *pw, Key *key)
687 {
688 	u_int success, i;
689 	char *file;
690 
691 	if (auth_key_is_revoked(key))
692 		return 0;
693 	if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
694 		return 0;
695 
696 	success = user_cert_trusted_ca(pw, key);
697 	if (success)
698 		return success;
699 
700 	success = user_key_command_allowed2(pw, key);
701 	if (success > 0)
702 		return success;
703 
704 	for (i = 0; !success && i < options.num_authkeys_files; i++) {
705 
706 		if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
707 			continue;
708 		file = expand_authorized_keys(
709 		    options.authorized_keys_files[i], pw);
710 
711 		success = user_key_allowed2(pw, key, file);
712 		xfree(file);
713 	}
714 
715 	return success;
716 }
717 
718 Authmethod method_pubkey = {
719 	"publickey",
720 	userauth_pubkey,
721 	&options.pubkey_authentication
722 };
723