xref: /titanic_44/usr/src/cmd/ssh/sshd/auth2-pubkey.c (revision cd7d5faf5bbb52336a6f85578a90b31a648ac3fa)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
57c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
67c478bd9Sstevel@tonic-gate  * are met:
77c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
87c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
97c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
107c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
117c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate /*
25*cd7d5fafSJan Pechanec  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "includes.h"
307c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth2-pubkey.c,v 1.2 2002/05/31 11:35:15 markus Exp $");
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "ssh2.h"
337c478bd9Sstevel@tonic-gate #include "xmalloc.h"
347c478bd9Sstevel@tonic-gate #include "packet.h"
357c478bd9Sstevel@tonic-gate #include "buffer.h"
367c478bd9Sstevel@tonic-gate #include "log.h"
377c478bd9Sstevel@tonic-gate #include "servconf.h"
387c478bd9Sstevel@tonic-gate #include "compat.h"
397c478bd9Sstevel@tonic-gate #include "bufaux.h"
407c478bd9Sstevel@tonic-gate #include "auth.h"
417c478bd9Sstevel@tonic-gate #include "key.h"
427c478bd9Sstevel@tonic-gate #include "pathnames.h"
437c478bd9Sstevel@tonic-gate #include "uidswap.h"
447c478bd9Sstevel@tonic-gate #include "auth-options.h"
457c478bd9Sstevel@tonic-gate #include "canohost.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #ifdef USE_PAM
487c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
497c478bd9Sstevel@tonic-gate #include "auth-pam.h"
507c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* import */
537c478bd9Sstevel@tonic-gate extern ServerOptions options;
547c478bd9Sstevel@tonic-gate extern u_char *session_id2;
557c478bd9Sstevel@tonic-gate extern int session_id2_len;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static void
userauth_pubkey(Authctxt * authctxt)587c478bd9Sstevel@tonic-gate userauth_pubkey(Authctxt *authctxt)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	Buffer b;
617c478bd9Sstevel@tonic-gate 	Key *key = NULL;
627c478bd9Sstevel@tonic-gate 	char *pkalg;
637c478bd9Sstevel@tonic-gate 	u_char *pkblob, *sig;
647c478bd9Sstevel@tonic-gate 	u_int alen, blen, slen;
657c478bd9Sstevel@tonic-gate 	int have_sig, pktype;
667c478bd9Sstevel@tonic-gate 	int authenticated = 0;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (!authctxt || !authctxt->method)
697c478bd9Sstevel@tonic-gate 		fatal("%s: missing context", __func__);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	have_sig = packet_get_char();
727c478bd9Sstevel@tonic-gate 	if (datafellows & SSH_BUG_PKAUTH) {
737c478bd9Sstevel@tonic-gate 		debug2("userauth_pubkey: SSH_BUG_PKAUTH");
747c478bd9Sstevel@tonic-gate 		/* no explicit pkalg given */
757c478bd9Sstevel@tonic-gate 		pkblob = packet_get_string(&blen);
767c478bd9Sstevel@tonic-gate 		buffer_init(&b);
777c478bd9Sstevel@tonic-gate 		buffer_append(&b, pkblob, blen);
787c478bd9Sstevel@tonic-gate 		/* so we have to extract the pkalg from the pkblob */
797c478bd9Sstevel@tonic-gate 		pkalg = buffer_get_string(&b, &alen);
807c478bd9Sstevel@tonic-gate 		buffer_free(&b);
817c478bd9Sstevel@tonic-gate 	} else {
827c478bd9Sstevel@tonic-gate 		pkalg = packet_get_string(&alen);
837c478bd9Sstevel@tonic-gate 		pkblob = packet_get_string(&blen);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	pktype = key_type_from_name(pkalg);
867c478bd9Sstevel@tonic-gate 	if (pktype == KEY_UNSPEC) {
877c478bd9Sstevel@tonic-gate 		/* this is perfectly legal */
887c478bd9Sstevel@tonic-gate 		log("userauth_pubkey: unsupported public key algorithm: %s",
897c478bd9Sstevel@tonic-gate 		    pkalg);
907c478bd9Sstevel@tonic-gate 		goto done;
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	key = key_from_blob(pkblob, blen);
937c478bd9Sstevel@tonic-gate 	if (key == NULL) {
947c478bd9Sstevel@tonic-gate 		error("userauth_pubkey: cannot decode key: %s", pkalg);
957c478bd9Sstevel@tonic-gate 		goto done;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	if (key->type != pktype) {
987c478bd9Sstevel@tonic-gate 		error("userauth_pubkey: type mismatch for decoded key "
997c478bd9Sstevel@tonic-gate 		    "(received %d, expected %d)", key->type, pktype);
1007c478bd9Sstevel@tonic-gate 		goto done;
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/* Detect and count abandonment */
1047c478bd9Sstevel@tonic-gate 	if (authctxt->method->method_data) {
1057c478bd9Sstevel@tonic-gate 		Key	*prev_key;
1067c478bd9Sstevel@tonic-gate 		unsigned char	*prev_pkblob;
1077c478bd9Sstevel@tonic-gate 		int	 prev_blen;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		/*
1107c478bd9Sstevel@tonic-gate 		 * Check for earlier test of a key that was allowed but
1117c478bd9Sstevel@tonic-gate 		 * not followed up with a pubkey req for the same pubkey
1127c478bd9Sstevel@tonic-gate 		 * and with a signature.
1137c478bd9Sstevel@tonic-gate 		 */
1147c478bd9Sstevel@tonic-gate 		prev_key = authctxt->method->method_data;
1157c478bd9Sstevel@tonic-gate 		if ((prev_blen = key_to_blob(prev_key,
1167c478bd9Sstevel@tonic-gate 			    &prev_pkblob, NULL))) {
1177c478bd9Sstevel@tonic-gate 			if (prev_blen != blen ||
1187c478bd9Sstevel@tonic-gate 			    memcmp(prev_pkblob, pkblob, blen) != 0) {
1197c478bd9Sstevel@tonic-gate 				authctxt->method->abandons++;
1207c478bd9Sstevel@tonic-gate 				authctxt->method->attempts++;
1217c478bd9Sstevel@tonic-gate 			}
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 		key_free(prev_key);
1247c478bd9Sstevel@tonic-gate 		authctxt->method->method_data = NULL;
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (have_sig) {
1287c478bd9Sstevel@tonic-gate 		sig = packet_get_string(&slen);
1297c478bd9Sstevel@tonic-gate 		packet_check_eom();
1307c478bd9Sstevel@tonic-gate 		buffer_init(&b);
1317c478bd9Sstevel@tonic-gate 		if (datafellows & SSH_OLD_SESSIONID) {
1327c478bd9Sstevel@tonic-gate 			buffer_append(&b, session_id2, session_id2_len);
1337c478bd9Sstevel@tonic-gate 		} else {
1347c478bd9Sstevel@tonic-gate 			buffer_put_string(&b, session_id2, session_id2_len);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		/* reconstruct packet */
1377c478bd9Sstevel@tonic-gate 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1387c478bd9Sstevel@tonic-gate 		buffer_put_cstring(&b, authctxt->user);
1397c478bd9Sstevel@tonic-gate 		buffer_put_cstring(&b,
1407c478bd9Sstevel@tonic-gate 		    datafellows & SSH_BUG_PKSERVICE ?
1417c478bd9Sstevel@tonic-gate 		    "ssh-userauth" :
1427c478bd9Sstevel@tonic-gate 		    authctxt->service);
1437c478bd9Sstevel@tonic-gate 		if (datafellows & SSH_BUG_PKAUTH) {
1447c478bd9Sstevel@tonic-gate 			buffer_put_char(&b, have_sig);
1457c478bd9Sstevel@tonic-gate 		} else {
1467c478bd9Sstevel@tonic-gate 			buffer_put_cstring(&b, "publickey");
1477c478bd9Sstevel@tonic-gate 			buffer_put_char(&b, have_sig);
1487c478bd9Sstevel@tonic-gate 			buffer_put_cstring(&b, pkalg);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		buffer_put_string(&b, pkblob, blen);
1517c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
1527c478bd9Sstevel@tonic-gate 		buffer_dump(&b);
1537c478bd9Sstevel@tonic-gate #endif
1547c478bd9Sstevel@tonic-gate 		/* test for correct signature */
1559a8058b5Sjp161948 		if (user_key_allowed(authctxt->pw, key) &&
1569a8058b5Sjp161948 		    key_verify(key, sig, slen, buffer_ptr(&b),
1579a8058b5Sjp161948 		    buffer_len(&b)) == 1) {
1587c478bd9Sstevel@tonic-gate 			authenticated = 1;
1599a8058b5Sjp161948 		}
1607c478bd9Sstevel@tonic-gate 		authctxt->method->postponed = 0;
161*cd7d5fafSJan Pechanec 		buffer_free(&b);
1627c478bd9Sstevel@tonic-gate 		xfree(sig);
1637c478bd9Sstevel@tonic-gate 	} else {
1647c478bd9Sstevel@tonic-gate 		debug("test whether pkalg/pkblob are acceptable");
1657c478bd9Sstevel@tonic-gate 		packet_check_eom();
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		/* XXX fake reply and always send PK_OK ? */
1687c478bd9Sstevel@tonic-gate 		/*
1697c478bd9Sstevel@tonic-gate 		 * XXX this allows testing whether a user is allowed
1707c478bd9Sstevel@tonic-gate 		 * to login: if you happen to have a valid pubkey this
1717c478bd9Sstevel@tonic-gate 		 * message is sent. the message is NEVER sent at all
1727c478bd9Sstevel@tonic-gate 		 * if a user is not allowed to login. is this an
1737c478bd9Sstevel@tonic-gate 		 * issue? -markus
1747c478bd9Sstevel@tonic-gate 		 */
1759a8058b5Sjp161948 		if (user_key_allowed(authctxt->pw, key)) {
1767c478bd9Sstevel@tonic-gate 			packet_start(SSH2_MSG_USERAUTH_PK_OK);
1777c478bd9Sstevel@tonic-gate 			packet_put_string(pkalg, alen);
1787c478bd9Sstevel@tonic-gate 			packet_put_string(pkblob, blen);
1797c478bd9Sstevel@tonic-gate 			packet_send();
1807c478bd9Sstevel@tonic-gate 			packet_write_wait();
1817c478bd9Sstevel@tonic-gate 			authctxt->method->postponed = 1;
1827c478bd9Sstevel@tonic-gate 			/*
1837c478bd9Sstevel@tonic-gate 			 * Remember key that was tried so we can
1847c478bd9Sstevel@tonic-gate 			 * correctly detect abandonment.  See above.
1857c478bd9Sstevel@tonic-gate 			 */
1867c478bd9Sstevel@tonic-gate 			authctxt->method->method_data = (void *) key;
1877c478bd9Sstevel@tonic-gate 			key = NULL;
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	if (authenticated != 1)
1917c478bd9Sstevel@tonic-gate 		auth_clear_options();
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate done:
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * XXX TODO: add config options for specifying users for whom
1967c478bd9Sstevel@tonic-gate 	 * this userauth is insufficient and what userauths may
1977c478bd9Sstevel@tonic-gate 	 * continue.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate #ifdef USE_PAM
2007c478bd9Sstevel@tonic-gate 	if (authenticated) {
2017c478bd9Sstevel@tonic-gate 		if (!do_pam_non_initial_userauth(authctxt))
2027c478bd9Sstevel@tonic-gate 			authenticated = 0;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
2077c478bd9Sstevel@tonic-gate 	if (key != NULL)
2087c478bd9Sstevel@tonic-gate 		key_free(key);
2097c478bd9Sstevel@tonic-gate 	xfree(pkalg);
2107c478bd9Sstevel@tonic-gate 	xfree(pkblob);
2117c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
2127c478bd9Sstevel@tonic-gate 	if (check_nt_auth(0, authctxt->pw) == 0)
2137c478bd9Sstevel@tonic-gate 		return;
2147c478bd9Sstevel@tonic-gate #endif
2157c478bd9Sstevel@tonic-gate 	if (authenticated)
2167c478bd9Sstevel@tonic-gate 		authctxt->method->authenticated = 1;
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /* return 1 if user allows given key */
2207c478bd9Sstevel@tonic-gate static int
user_key_allowed2(struct passwd * pw,Key * key,char * file)2217c478bd9Sstevel@tonic-gate user_key_allowed2(struct passwd *pw, Key *key, char *file)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	char line[8192];
2247c478bd9Sstevel@tonic-gate 	int found_key = 0;
2257c478bd9Sstevel@tonic-gate 	FILE *f;
2267c478bd9Sstevel@tonic-gate 	u_long linenum = 0;
2277c478bd9Sstevel@tonic-gate 	struct stat st;
2287c478bd9Sstevel@tonic-gate 	Key *found;
2297c478bd9Sstevel@tonic-gate 	char *fp;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (pw == NULL)
2327c478bd9Sstevel@tonic-gate 		return 0;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/* Temporarily use the user's uid. */
2357c478bd9Sstevel@tonic-gate 	temporarily_use_uid(pw);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	debug("trying public key file %s", file);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/* Fail quietly if file does not exist */
2407c478bd9Sstevel@tonic-gate 	if (stat(file, &st) < 0) {
2417c478bd9Sstevel@tonic-gate 		/* Restore the privileged uid. */
2427c478bd9Sstevel@tonic-gate 		restore_uid();
2437c478bd9Sstevel@tonic-gate 		return 0;
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	/* Open the file containing the authorized keys. */
2467c478bd9Sstevel@tonic-gate 	f = fopen(file, "r");
2477c478bd9Sstevel@tonic-gate 	if (!f) {
2487c478bd9Sstevel@tonic-gate 		/* Restore the privileged uid. */
2497c478bd9Sstevel@tonic-gate 		restore_uid();
2507c478bd9Sstevel@tonic-gate 		return 0;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	if (options.strict_modes &&
2537c478bd9Sstevel@tonic-gate 	    secure_filename(f, file, pw, line, sizeof(line)) != 0) {
2547c478bd9Sstevel@tonic-gate 		(void) fclose(f);
2557c478bd9Sstevel@tonic-gate 		log("Authentication refused: %s", line);
2567c478bd9Sstevel@tonic-gate 		restore_uid();
2577c478bd9Sstevel@tonic-gate 		return 0;
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	found_key = 0;
2617c478bd9Sstevel@tonic-gate 	found = key_new(key->type);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof(line), f)) {
2647c478bd9Sstevel@tonic-gate 		char *cp, *options = NULL;
2657c478bd9Sstevel@tonic-gate 		linenum++;
2667c478bd9Sstevel@tonic-gate 		/* Skip leading whitespace, empty and comment lines. */
2677c478bd9Sstevel@tonic-gate 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
2687c478bd9Sstevel@tonic-gate 			;
2697c478bd9Sstevel@tonic-gate 		if (!*cp || *cp == '\n' || *cp == '#')
2707c478bd9Sstevel@tonic-gate 			continue;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		if (key_read(found, &cp) != 1) {
2737c478bd9Sstevel@tonic-gate 			/* no key?  check if there are options for this key */
2747c478bd9Sstevel@tonic-gate 			int quoted = 0;
2757c478bd9Sstevel@tonic-gate 			debug2("user_key_allowed: check options: '%s'", cp);
2767c478bd9Sstevel@tonic-gate 			options = cp;
2777c478bd9Sstevel@tonic-gate 			for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
2787c478bd9Sstevel@tonic-gate 				if (*cp == '\\' && cp[1] == '"')
2797c478bd9Sstevel@tonic-gate 					cp++;	/* Skip both */
2807c478bd9Sstevel@tonic-gate 				else if (*cp == '"')
2817c478bd9Sstevel@tonic-gate 					quoted = !quoted;
2827c478bd9Sstevel@tonic-gate 			}
2837c478bd9Sstevel@tonic-gate 			/* Skip remaining whitespace. */
2847c478bd9Sstevel@tonic-gate 			for (; *cp == ' ' || *cp == '\t'; cp++)
2857c478bd9Sstevel@tonic-gate 				;
2867c478bd9Sstevel@tonic-gate 			if (key_read(found, &cp) != 1) {
2877c478bd9Sstevel@tonic-gate 				debug2("user_key_allowed: advance: '%s'", cp);
2887c478bd9Sstevel@tonic-gate 				/* still no key?  advance to next line*/
2897c478bd9Sstevel@tonic-gate 				continue;
2907c478bd9Sstevel@tonic-gate 			}
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		if (key_equal(found, key) &&
2937c478bd9Sstevel@tonic-gate 		    auth_parse_options(pw, options, file, linenum) == 1) {
2947c478bd9Sstevel@tonic-gate 			found_key = 1;
2957c478bd9Sstevel@tonic-gate 			debug("matching key found: file %s, line %lu",
2967c478bd9Sstevel@tonic-gate 			    file, linenum);
2977c478bd9Sstevel@tonic-gate 			fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
2987c478bd9Sstevel@tonic-gate 			verbose("Found matching %s key: %s",
2997c478bd9Sstevel@tonic-gate 			    key_type(found), fp);
3007c478bd9Sstevel@tonic-gate 			xfree(fp);
3017c478bd9Sstevel@tonic-gate 			break;
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 	restore_uid();
3057c478bd9Sstevel@tonic-gate 	(void) fclose(f);
3067c478bd9Sstevel@tonic-gate 	key_free(found);
3077c478bd9Sstevel@tonic-gate 	if (!found_key)
3087c478bd9Sstevel@tonic-gate 		debug2("key not found");
3097c478bd9Sstevel@tonic-gate 	return found_key;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /* check whether given key is in .ssh/authorized_keys* */
3137c478bd9Sstevel@tonic-gate int
user_key_allowed(struct passwd * pw,Key * key)3147c478bd9Sstevel@tonic-gate user_key_allowed(struct passwd *pw, Key *key)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	int success;
3177c478bd9Sstevel@tonic-gate 	char *file;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (pw == NULL)
3207c478bd9Sstevel@tonic-gate 		return 0;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	file = authorized_keys_file(pw);
3237c478bd9Sstevel@tonic-gate 	success = user_key_allowed2(pw, key, file);
3247c478bd9Sstevel@tonic-gate 	xfree(file);
3257c478bd9Sstevel@tonic-gate 	if (success)
3267c478bd9Sstevel@tonic-gate 		return success;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/* try suffix "2" for backward compat, too */
3297c478bd9Sstevel@tonic-gate 	file = authorized_keys_file2(pw);
3307c478bd9Sstevel@tonic-gate 	success = user_key_allowed2(pw, key, file);
3317c478bd9Sstevel@tonic-gate 	xfree(file);
3327c478bd9Sstevel@tonic-gate 	return success;
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate static
3367c478bd9Sstevel@tonic-gate void
userauth_pubkey_abandon(Authctxt * authctxt,Authmethod * method)3377c478bd9Sstevel@tonic-gate userauth_pubkey_abandon(Authctxt *authctxt, Authmethod *method)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	if (!authctxt || !method)
3407c478bd9Sstevel@tonic-gate 		return;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (method->method_data) {
3437c478bd9Sstevel@tonic-gate 		method->abandons++;
3447c478bd9Sstevel@tonic-gate 		method->attempts++;
3457c478bd9Sstevel@tonic-gate 		key_free((Key *) method->method_data);
3467c478bd9Sstevel@tonic-gate 		method->method_data = NULL;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate Authmethod method_pubkey = {
3517c478bd9Sstevel@tonic-gate 	"publickey",
3527c478bd9Sstevel@tonic-gate 	&options.pubkey_authentication,
3537c478bd9Sstevel@tonic-gate 	userauth_pubkey,
3547c478bd9Sstevel@tonic-gate 	userauth_pubkey_abandon,
3557c478bd9Sstevel@tonic-gate 	NULL, NULL,	    /* method data and hist data */
3567c478bd9Sstevel@tonic-gate 	0,		    /* not initial userauth */
3577c478bd9Sstevel@tonic-gate 	0, 0, 0,	    /* counters */
3587c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0    /* state */
3597c478bd9Sstevel@tonic-gate };
360