xref: /titanic_41/usr/src/cmd/ssh/sshd/auth2.c (revision bca3984e29c8c75c0e473a3f1134d686db15e937)
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*bca3984eSBrent Paulson  * 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.c,v 1.95 2002/08/22 21:33:58 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 "log.h"
367c478bd9Sstevel@tonic-gate #include "servconf.h"
377c478bd9Sstevel@tonic-gate #include "compat.h"
387c478bd9Sstevel@tonic-gate #include "misc.h"
397c478bd9Sstevel@tonic-gate #include "auth.h"
407c478bd9Sstevel@tonic-gate #include "dispatch.h"
417c478bd9Sstevel@tonic-gate #include "sshlogin.h"
427c478bd9Sstevel@tonic-gate #include "pathnames.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #ifdef HAVE_BSM
457c478bd9Sstevel@tonic-gate #include "bsmaudit.h"
467c478bd9Sstevel@tonic-gate extern adt_session_data_t *ah;
477c478bd9Sstevel@tonic-gate #endif /* HAVE_BSM */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #ifdef GSSAPI
507c478bd9Sstevel@tonic-gate #include "ssh-gss.h"
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* import */
547c478bd9Sstevel@tonic-gate extern ServerOptions options;
557c478bd9Sstevel@tonic-gate extern u_char *session_id2;
567c478bd9Sstevel@tonic-gate extern int session_id2_len;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate Authctxt *x_authctxt = NULL;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* methods */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate extern Authmethod method_none;
637c478bd9Sstevel@tonic-gate extern Authmethod method_pubkey;
647c478bd9Sstevel@tonic-gate extern Authmethod method_passwd;
657c478bd9Sstevel@tonic-gate extern Authmethod method_kbdint;
667c478bd9Sstevel@tonic-gate extern Authmethod method_hostbased;
677c478bd9Sstevel@tonic-gate extern Authmethod method_external;
687c478bd9Sstevel@tonic-gate extern Authmethod method_gssapi;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static Authmethod *authmethods[] = {
717c478bd9Sstevel@tonic-gate 	&method_none,
727c478bd9Sstevel@tonic-gate #ifdef GSSAPI
737c478bd9Sstevel@tonic-gate 	&method_external,
747c478bd9Sstevel@tonic-gate 	&method_gssapi,
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate 	&method_pubkey,
777c478bd9Sstevel@tonic-gate 	&method_passwd,
787c478bd9Sstevel@tonic-gate 	&method_kbdint,
797c478bd9Sstevel@tonic-gate 	&method_hostbased,
807c478bd9Sstevel@tonic-gate 	NULL
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /* protocol */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static void input_service_request(int, u_int32_t, void *);
867c478bd9Sstevel@tonic-gate static void input_userauth_request(int, u_int32_t, void *);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* helper */
897c478bd9Sstevel@tonic-gate static Authmethod *authmethod_lookup(const char *);
907c478bd9Sstevel@tonic-gate static char *authmethods_get(void);
917c478bd9Sstevel@tonic-gate static char *authmethods_check_abandonment(Authctxt *authctxt,
927c478bd9Sstevel@tonic-gate 					  Authmethod *method);
937c478bd9Sstevel@tonic-gate static void  authmethod_count_attempt(Authmethod *method);
947c478bd9Sstevel@tonic-gate /*static char *authmethods_get_kbdint(void);*/
957c478bd9Sstevel@tonic-gate int user_key_allowed(struct passwd *, Key *);
967c478bd9Sstevel@tonic-gate int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
977c478bd9Sstevel@tonic-gate static int   userauth_method_can_run(Authmethod *method);
987c478bd9Sstevel@tonic-gate static void  userauth_reset_methods(void);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * loop until authctxt->success == TRUE
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate Authctxt *
1057c478bd9Sstevel@tonic-gate do_authentication2(void)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	Authctxt *authctxt = authctxt_new();
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	x_authctxt = authctxt;		/*XXX*/
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #ifdef HAVE_BSM
1127c478bd9Sstevel@tonic-gate 	fatal_add_cleanup(audit_failed_login_cleanup, authctxt);
1137c478bd9Sstevel@tonic-gate #endif /* HAVE_BSM */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/* challenge-response is implemented via keyboard interactive */
1167c478bd9Sstevel@tonic-gate 	if (options.challenge_response_authentication)
1177c478bd9Sstevel@tonic-gate 		options.kbd_interactive_authentication = 1;
1187c478bd9Sstevel@tonic-gate 	if (options.pam_authentication_via_kbd_int)
1197c478bd9Sstevel@tonic-gate 		options.kbd_interactive_authentication = 1;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	dispatch_init(&dispatch_protocol_error);
1227c478bd9Sstevel@tonic-gate 	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1237c478bd9Sstevel@tonic-gate 	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	return (authctxt);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static void
1297c478bd9Sstevel@tonic-gate input_service_request(int type, u_int32_t seq, void *ctxt)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	Authctxt *authctxt = ctxt;
1327c478bd9Sstevel@tonic-gate 	u_int len;
1337c478bd9Sstevel@tonic-gate 	int acceptit = 0;
1347c478bd9Sstevel@tonic-gate 	char *service = packet_get_string(&len);
1357c478bd9Sstevel@tonic-gate 	packet_check_eom();
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (authctxt == NULL)
1387c478bd9Sstevel@tonic-gate 		fatal("input_service_request: no authctxt");
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (strcmp(service, "ssh-userauth") == 0) {
1417c478bd9Sstevel@tonic-gate 		if (!authctxt->success) {
1427c478bd9Sstevel@tonic-gate 			acceptit = 1;
1437c478bd9Sstevel@tonic-gate 			/* now we can handle user-auth requests */
1447c478bd9Sstevel@tonic-gate 			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	/* XXX all other service requests are denied */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (acceptit) {
1507c478bd9Sstevel@tonic-gate 		packet_start(SSH2_MSG_SERVICE_ACCEPT);
1517c478bd9Sstevel@tonic-gate 		packet_put_cstring(service);
1527c478bd9Sstevel@tonic-gate 		packet_send();
1537c478bd9Sstevel@tonic-gate 		packet_write_wait();
1547c478bd9Sstevel@tonic-gate 	} else {
1557c478bd9Sstevel@tonic-gate 		debug("bad service request %s", service);
1567c478bd9Sstevel@tonic-gate 		packet_disconnect("bad service request %s", service);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 	xfree(service);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static void
1627c478bd9Sstevel@tonic-gate input_userauth_request(int type, u_int32_t seq, void *ctxt)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	Authctxt *authctxt = ctxt;
1657c478bd9Sstevel@tonic-gate 	Authmethod *m = NULL;
1667c478bd9Sstevel@tonic-gate 	char *user, *service, *method, *style = NULL;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (authctxt == NULL)
1697c478bd9Sstevel@tonic-gate 		fatal("input_userauth_request: no authctxt");
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	user = packet_get_string(NULL);
1727c478bd9Sstevel@tonic-gate 	service = packet_get_string(NULL);
1737c478bd9Sstevel@tonic-gate 	method = packet_get_string(NULL);
1747c478bd9Sstevel@tonic-gate 	debug("userauth-request for user %s service %s method %s", user,
1757c478bd9Sstevel@tonic-gate 		service, method);
1767c478bd9Sstevel@tonic-gate 	debug("attempt %d initial attempt %d failures %d initial failures %d",
1777c478bd9Sstevel@tonic-gate 		authctxt->attempt, authctxt->init_attempt,
1787c478bd9Sstevel@tonic-gate 		authctxt->failures, authctxt->init_failures);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	m = authmethod_lookup(method);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if ((style = strchr(user, ':')) != NULL)
1837c478bd9Sstevel@tonic-gate 		*style++ = 0;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	authctxt->attempt++;
1867c478bd9Sstevel@tonic-gate 	if (m != NULL && m->is_initial)
1877c478bd9Sstevel@tonic-gate 		authctxt->init_attempt++;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (authctxt->attempt == 1) {
1907c478bd9Sstevel@tonic-gate 		/* setup auth context */
1919a8058b5Sjp161948 		authctxt->pw = getpwnamallow(user);
1927c478bd9Sstevel@tonic-gate 		/* May want to abstract SSHv2 services someday */
1937c478bd9Sstevel@tonic-gate 		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1947c478bd9Sstevel@tonic-gate 			/* enforced in userauth_finish() below */
1957c478bd9Sstevel@tonic-gate 			authctxt->valid = 1;
1967c478bd9Sstevel@tonic-gate 			debug2("input_userauth_request: setting up authctxt for %s", user);
1977c478bd9Sstevel@tonic-gate 		} else {
1987c478bd9Sstevel@tonic-gate 			log("input_userauth_request: illegal user %s", user);
1997c478bd9Sstevel@tonic-gate 		}
2009a8058b5Sjp161948 		setproctitle("%s", authctxt->pw ? user : "unknown");
2017c478bd9Sstevel@tonic-gate 		authctxt->user = xstrdup(user);
2027c478bd9Sstevel@tonic-gate 		authctxt->service = xstrdup(service);
2037c478bd9Sstevel@tonic-gate 		authctxt->style = style ? xstrdup(style) : NULL;
2047c478bd9Sstevel@tonic-gate 		userauth_reset_methods();
2057c478bd9Sstevel@tonic-gate 	} else {
2067c478bd9Sstevel@tonic-gate 		char *abandoned;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		/*
2097c478bd9Sstevel@tonic-gate 		 * Check for abandoned [multi-round-trip] userauths
2107c478bd9Sstevel@tonic-gate 		 * methods (e.g., kbdint).  Userauth method abandonment
2117c478bd9Sstevel@tonic-gate 		 * should be treated as userauth method failure and
2127c478bd9Sstevel@tonic-gate 		 * counted against max_auth_tries.
2137c478bd9Sstevel@tonic-gate 		 */
2147c478bd9Sstevel@tonic-gate 		abandoned = authmethods_check_abandonment(authctxt, m);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		if (abandoned != NULL &&
2177c478bd9Sstevel@tonic-gate 		    authctxt->failures > options.max_auth_tries) {
2187c478bd9Sstevel@tonic-gate 			/* userauth_finish() will now packet_disconnect() */
2197c478bd9Sstevel@tonic-gate 			userauth_finish(authctxt, abandoned);
2207c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		/* Handle user|service changes, possibly packet_disconnect() */
2247c478bd9Sstevel@tonic-gate 		userauth_user_svc_change(authctxt, user, service);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	authctxt->method = m;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/* run userauth method, try to authenticate user */
2307c478bd9Sstevel@tonic-gate 	if (m != NULL && userauth_method_can_run(m)) {
2317c478bd9Sstevel@tonic-gate 		debug2("input_userauth_request: try method %s", method);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		m->postponed = 0;
2347c478bd9Sstevel@tonic-gate 		m->abandoned = 0;
2357c478bd9Sstevel@tonic-gate 		m->authenticated = 0;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		if (!m->is_initial ||
2387c478bd9Sstevel@tonic-gate 		    authctxt->init_failures < options.max_init_auth_tries)
2397c478bd9Sstevel@tonic-gate 			m->userauth(authctxt);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		authmethod_count_attempt(m);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		if (authctxt->unwind_dispatch_loop) {
2447c478bd9Sstevel@tonic-gate 			/*
2457c478bd9Sstevel@tonic-gate 			 * Method ran nested dispatch loop but was
2467c478bd9Sstevel@tonic-gate 			 * abandoned.  Cleanup and return without doing
2477c478bd9Sstevel@tonic-gate 			 * anything else; we're just unwinding the stack.
2487c478bd9Sstevel@tonic-gate 			 */
2497c478bd9Sstevel@tonic-gate 			authctxt->unwind_dispatch_loop = 0;
2507c478bd9Sstevel@tonic-gate 			goto done;
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		if (m->postponed)
2547c478bd9Sstevel@tonic-gate 			goto done; /* multi-round trip userauth not finished */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		if (m->abandoned) {
2577c478bd9Sstevel@tonic-gate 			/* multi-round trip userauth abandoned, log failure */
2587c478bd9Sstevel@tonic-gate 			auth_log(authctxt, 0, method, " ssh2");
2597c478bd9Sstevel@tonic-gate 			goto done;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	userauth_finish(authctxt, method);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate done:
2667c478bd9Sstevel@tonic-gate 	xfree(service);
2677c478bd9Sstevel@tonic-gate 	xfree(user);
2687c478bd9Sstevel@tonic-gate 	xfree(method);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate void
2727c478bd9Sstevel@tonic-gate userauth_finish(Authctxt *authctxt, char *method)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	int authenticated, partial;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (authctxt == NULL)
2777c478bd9Sstevel@tonic-gate 		fatal("%s: missing context", __func__);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/* unknown method handling -- must elicit userauth failure msg */
2807c478bd9Sstevel@tonic-gate 	if (authctxt->method == NULL) {
2817c478bd9Sstevel@tonic-gate 		authenticated = 0;
2827c478bd9Sstevel@tonic-gate 		partial = 0;
2837c478bd9Sstevel@tonic-gate 		goto done_checking;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate #ifndef USE_PAM
2877c478bd9Sstevel@tonic-gate 	/* Special handling for root (done elsewhere for PAM) */
2889a8058b5Sjp161948 	if (authctxt->method->authenticated &&
2897c478bd9Sstevel@tonic-gate 	    authctxt->pw != NULL && authctxt->pw->pw_uid == 0 &&
2907c478bd9Sstevel@tonic-gate 	    !auth_root_allowed(method))
2917c478bd9Sstevel@tonic-gate 		authctxt->method->authenticated = 0;
2927c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate #ifdef _UNICOS
2957c478bd9Sstevel@tonic-gate 	if (authctxt->method->authenticated &&
2967c478bd9Sstevel@tonic-gate 	    cray_access_denied(authctxt->user)) {
2977c478bd9Sstevel@tonic-gate 		authctxt->method->authenticated = 0;
2987c478bd9Sstevel@tonic-gate 		fatal("Access denied for user %s.",authctxt->user);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	partial = userauth_check_partial_failure(authctxt);
3037c478bd9Sstevel@tonic-gate 	authenticated = authctxt->method->authenticated;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate #ifdef USE_PAM
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * If the userauth method failed to complete PAM work then force
3087c478bd9Sstevel@tonic-gate 	 * partial failure.
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 	if (authenticated && !AUTHPAM_DONE(authctxt))
3117c478bd9Sstevel@tonic-gate 		partial = 1;
3127c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * To properly support invalid userauth method names we set
3167c478bd9Sstevel@tonic-gate 	 * authenticated=0, partial=0 above and know that
3177c478bd9Sstevel@tonic-gate 	 * authctxt->method == NULL.
3187c478bd9Sstevel@tonic-gate 	 *
3197c478bd9Sstevel@tonic-gate 	 * No unguarded reference to authctxt->method allowed from here.
3207c478bd9Sstevel@tonic-gate 	 * Checking authenticated != 0 is a valid guard; authctxt->method
3217c478bd9Sstevel@tonic-gate 	 * MUST NOT be NULL if authenticated.
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate done_checking:
3247c478bd9Sstevel@tonic-gate 	if (!authctxt->valid && authenticated) {
3257c478bd9Sstevel@tonic-gate 		/*
3267c478bd9Sstevel@tonic-gate 		 * Should never happen -- if it does PAM's at fault
3277c478bd9Sstevel@tonic-gate 		 * but we need not panic, just treat as a failure.
3287c478bd9Sstevel@tonic-gate 		 */
3297c478bd9Sstevel@tonic-gate 		authctxt->method->authenticated = 0;
3307c478bd9Sstevel@tonic-gate 		authenticated = 0;
3317c478bd9Sstevel@tonic-gate 		log("Ignoring authenticated invalid user %s",
3327c478bd9Sstevel@tonic-gate 		    authctxt->user);
3337c478bd9Sstevel@tonic-gate 		auth_log(authctxt, 0, method, " ssh2");
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/* Log before sending the reply */
3377c478bd9Sstevel@tonic-gate 	auth_log(authctxt, authenticated, method, " ssh2");
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	if (authenticated && !partial) {
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		/* turn off userauth */
3427c478bd9Sstevel@tonic-gate 		dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
3437c478bd9Sstevel@tonic-gate 		packet_start(SSH2_MSG_USERAUTH_SUCCESS);
3447c478bd9Sstevel@tonic-gate 		packet_send();
3457c478bd9Sstevel@tonic-gate 		packet_write_wait();
3467c478bd9Sstevel@tonic-gate 		/* now we can break out */
3477c478bd9Sstevel@tonic-gate 		authctxt->success = 1;
3487c478bd9Sstevel@tonic-gate 	} else {
3497c478bd9Sstevel@tonic-gate 		char *methods;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		if (authctxt->method && authctxt->method->is_initial)
3527c478bd9Sstevel@tonic-gate 			authctxt->init_failures++;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		authctxt->method = NULL;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate #ifdef USE_PAM
3577c478bd9Sstevel@tonic-gate 		/*
3587c478bd9Sstevel@tonic-gate 		 * Keep track of last PAM error (or PERM_DENIED) for BSM
3597c478bd9Sstevel@tonic-gate 		 * login failure auditing, which may run after the PAM
3607c478bd9Sstevel@tonic-gate 		 * state has been cleaned up.
3617c478bd9Sstevel@tonic-gate 		 */
3627c478bd9Sstevel@tonic-gate 		authctxt->pam_retval = AUTHPAM_ERROR(authctxt, PAM_PERM_DENIED);
3637c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		if (authctxt->failures++ > options.max_auth_tries) {
3667c478bd9Sstevel@tonic-gate #ifdef HAVE_BSM
3677c478bd9Sstevel@tonic-gate 			fatal_remove_cleanup(audit_failed_login_cleanup,
3687c478bd9Sstevel@tonic-gate 				authctxt);
369*bca3984eSBrent Paulson 			audit_sshd_login_failure(&ah, PAM_MAXTRIES,
370*bca3984eSBrent Paulson 			    authctxt->user);
3717c478bd9Sstevel@tonic-gate #endif /* HAVE_BSM */
3727c478bd9Sstevel@tonic-gate 			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate #ifdef _UNICOS
3767c478bd9Sstevel@tonic-gate 		if (strcmp(method, "password") == 0)
3777c478bd9Sstevel@tonic-gate 			cray_login_failure(authctxt->user, IA_UDBERR);
3787c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
3797c478bd9Sstevel@tonic-gate 		packet_start(SSH2_MSG_USERAUTH_FAILURE);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		/*
3827c478bd9Sstevel@tonic-gate 		 * If (partial) then authmethods_get() will return only
3837c478bd9Sstevel@tonic-gate 		 * required methods, likely only "keyboard-interactive;"
3847c478bd9Sstevel@tonic-gate 		 * (methods == NULL) implies failure, even if (partial == 1)
3857c478bd9Sstevel@tonic-gate 		 */
3867c478bd9Sstevel@tonic-gate 		methods = authmethods_get();
3877c478bd9Sstevel@tonic-gate 		packet_put_cstring(methods);
3887c478bd9Sstevel@tonic-gate 		packet_put_char((authenticated && partial && methods) ? 1 : 0);
3897c478bd9Sstevel@tonic-gate 		if (methods)
3907c478bd9Sstevel@tonic-gate 			xfree(methods);
3917c478bd9Sstevel@tonic-gate 		packet_send();
3927c478bd9Sstevel@tonic-gate 		packet_write_wait();
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /* get current user */
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate struct passwd*
3997c478bd9Sstevel@tonic-gate auth_get_user(void)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate #define	DELIM	","
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate #if 0
4077c478bd9Sstevel@tonic-gate static char *
4087c478bd9Sstevel@tonic-gate authmethods_get_kbdint(void)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	Buffer b;
4117c478bd9Sstevel@tonic-gate 	int i;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
4147c478bd9Sstevel@tonic-gate 		if (strcmp(authmethods[i]->name, "keyboard-interactive") != 0)
4157c478bd9Sstevel@tonic-gate 			continue;
4167c478bd9Sstevel@tonic-gate 		return xstrdup(authmethods[i]->name);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 	return NULL;
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate #endif
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate void
4237c478bd9Sstevel@tonic-gate userauth_user_svc_change(Authctxt *authctxt, char *user, char *service)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * NOTE:
4277c478bd9Sstevel@tonic-gate 	 *
4287c478bd9Sstevel@tonic-gate 	 * SSHv2 services should be abstracted and service changes during
4297c478bd9Sstevel@tonic-gate 	 * userauth should be supported as per the userauth draft.  In the PAM
4307c478bd9Sstevel@tonic-gate 	 * case, support for multiple SSHv2 services means that we have to
4317c478bd9Sstevel@tonic-gate 	 * format the PAM service name according to the SSHv2 service *and* the
4327c478bd9Sstevel@tonic-gate 	 * SSHv2 userauth being attempted ("passwd", "kbdint" and "other").
4337c478bd9Sstevel@tonic-gate 	 *
4347c478bd9Sstevel@tonic-gate 	 * We'll cross that bridge when we come to it.  For now disallow service
4357c478bd9Sstevel@tonic-gate 	 * changes during userauth if using PAM, but allow username changes.
4367c478bd9Sstevel@tonic-gate 	 */
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/* authctxt->service must == ssh-connection here */
4397c478bd9Sstevel@tonic-gate 	if (service != NULL && strcmp(service, authctxt->service) != 0) {
4407c478bd9Sstevel@tonic-gate 		packet_disconnect("Change of service not "
4417c478bd9Sstevel@tonic-gate 				  "allowed: %s and %s",
4427c478bd9Sstevel@tonic-gate 				  authctxt->service, service);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	if (user != NULL && authctxt->user != NULL &&
4457c478bd9Sstevel@tonic-gate 	    strcmp(user, authctxt->user) == 0)
4467c478bd9Sstevel@tonic-gate 		return;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/* All good; update authctxt */
4497c478bd9Sstevel@tonic-gate 	xfree(authctxt->user);
4507c478bd9Sstevel@tonic-gate 	authctxt->user = xstrdup(user);
4517c478bd9Sstevel@tonic-gate 	pwfree(&authctxt->pw);
4529a8058b5Sjp161948 	authctxt->pw = getpwnamallow(user);
4537c478bd9Sstevel@tonic-gate 	authctxt->valid = (authctxt->pw != NULL);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/* Forget method state; abandon postponed userauths */
4567c478bd9Sstevel@tonic-gate 	userauth_reset_methods();
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate int
4607c478bd9Sstevel@tonic-gate userauth_check_partial_failure(Authctxt *authctxt)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	int i;
4637c478bd9Sstevel@tonic-gate 	int required = 0;
4647c478bd9Sstevel@tonic-gate 	int sufficient = 0;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
4677c478bd9Sstevel@tonic-gate 	 * v1 does not set authctxt->method
4687c478bd9Sstevel@tonic-gate 	 * partial userauth failure is a v2 concept
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	if (authctxt->method == NULL)
4717c478bd9Sstevel@tonic-gate 		return 0;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
4747c478bd9Sstevel@tonic-gate 		if (authmethods[i]->required)
4757c478bd9Sstevel@tonic-gate 			required++;
4767c478bd9Sstevel@tonic-gate 		if (authmethods[i]->sufficient)
4777c478bd9Sstevel@tonic-gate 			sufficient++;
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (required == 0 && sufficient == 0)
4817c478bd9Sstevel@tonic-gate 		return !authctxt->method->authenticated;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (required == 1 && authctxt->method->required)
4847c478bd9Sstevel@tonic-gate 		return !authctxt->method->authenticated;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if (sufficient && authctxt->method->sufficient)
4877c478bd9Sstevel@tonic-gate 		return !authctxt->method->authenticated;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	return 1;
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate int
4937c478bd9Sstevel@tonic-gate userauth_method_can_run(Authmethod *method)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	if (method->not_again)
4967c478bd9Sstevel@tonic-gate 		return 0;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	return 1;
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate static
5027c478bd9Sstevel@tonic-gate void
5037c478bd9Sstevel@tonic-gate userauth_reset_methods(void)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	int i;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
5087c478bd9Sstevel@tonic-gate 		/* note: counters not reset */
5097c478bd9Sstevel@tonic-gate 		authmethods[i]->required = 0;
5107c478bd9Sstevel@tonic-gate 		authmethods[i]->sufficient = 0;
5117c478bd9Sstevel@tonic-gate 		authmethods[i]->authenticated = 0;
5127c478bd9Sstevel@tonic-gate 		authmethods[i]->not_again = 0;
5137c478bd9Sstevel@tonic-gate 		authmethods[i]->postponed = 0;
5147c478bd9Sstevel@tonic-gate 		authmethods[i]->abandoned = 0;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate void
5197c478bd9Sstevel@tonic-gate userauth_force_kbdint(void)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	int i;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
5247c478bd9Sstevel@tonic-gate 		authmethods[i]->required = 0;
5257c478bd9Sstevel@tonic-gate 		authmethods[i]->sufficient = 0;
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 	method_kbdint.required = 1;
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  * Check to see if a previously run multi-round trip userauth method has
5327c478bd9Sstevel@tonic-gate  * been abandoned and call its cleanup function.
5337c478bd9Sstevel@tonic-gate  *
5347c478bd9Sstevel@tonic-gate  * Abandoned userauth method invocations are counted as userauth failures.
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate static
5377c478bd9Sstevel@tonic-gate char *
5387c478bd9Sstevel@tonic-gate authmethods_check_abandonment(Authctxt *authctxt, Authmethod *method)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	int i;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/* optimization: check current method first */
5437c478bd9Sstevel@tonic-gate 	if (method && method->postponed) {
5447c478bd9Sstevel@tonic-gate 		method->postponed = 0;
5457c478bd9Sstevel@tonic-gate 		if (method->abandon)
5467c478bd9Sstevel@tonic-gate 			method->abandon(authctxt, method);
5477c478bd9Sstevel@tonic-gate 		else
5487c478bd9Sstevel@tonic-gate 			method->abandons++;
5497c478bd9Sstevel@tonic-gate 		authctxt->failures++; /* abandonment -> failure */
5507c478bd9Sstevel@tonic-gate 		if (method->is_initial)
5517c478bd9Sstevel@tonic-gate 			authctxt->init_failures++;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 		/*
5547c478bd9Sstevel@tonic-gate 		 * Since we check for abandonment whenever a userauth is
5557c478bd9Sstevel@tonic-gate 		 * requested we know only one method could have been
5567c478bd9Sstevel@tonic-gate 		 * in postponed state, so we can return now.
5577c478bd9Sstevel@tonic-gate 		 */
5587c478bd9Sstevel@tonic-gate 		return (method->name);
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
5617c478bd9Sstevel@tonic-gate 		if (!authmethods[i]->postponed)
5627c478bd9Sstevel@tonic-gate 			continue;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		/* some method was postponed and a diff one is being started */
5657c478bd9Sstevel@tonic-gate 		if (method != authmethods[i]) {
5667c478bd9Sstevel@tonic-gate 			authmethods[i]->postponed = 0;
5677c478bd9Sstevel@tonic-gate 			if (authmethods[i]->abandon)
5687c478bd9Sstevel@tonic-gate 				authmethods[i]->abandon(authctxt,
5697c478bd9Sstevel@tonic-gate 							authmethods[i]);
5707c478bd9Sstevel@tonic-gate 			else
5717c478bd9Sstevel@tonic-gate 				authmethods[i]->abandons++;
5727c478bd9Sstevel@tonic-gate 			authctxt->failures++;
5737c478bd9Sstevel@tonic-gate 			if (authmethods[i]->is_initial)
5747c478bd9Sstevel@tonic-gate 				authctxt->init_failures++;
5757c478bd9Sstevel@tonic-gate 			return (authmethods[i]->name); /* see above */
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	return NULL;
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate static char *
5837c478bd9Sstevel@tonic-gate authmethods_get(void)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	Buffer b;
5867c478bd9Sstevel@tonic-gate 	char *list;
5877c478bd9Sstevel@tonic-gate 	int i;
5887c478bd9Sstevel@tonic-gate 	int sufficient = 0;
5897c478bd9Sstevel@tonic-gate 	int required = 0;
5907c478bd9Sstevel@tonic-gate 	int authenticated = 0;
5917c478bd9Sstevel@tonic-gate 	int partial = 0;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * If at least one method succeeded partially then at least one
5957c478bd9Sstevel@tonic-gate 	 * authmethod will be required and only required methods should
5967c478bd9Sstevel@tonic-gate 	 * continue.
5977c478bd9Sstevel@tonic-gate 	 */
5987c478bd9Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
5997c478bd9Sstevel@tonic-gate 		if (authmethods[i]->authenticated)
6007c478bd9Sstevel@tonic-gate 			authenticated++;
6017c478bd9Sstevel@tonic-gate 		if (authmethods[i]->required)
6027c478bd9Sstevel@tonic-gate 			required++;
6037c478bd9Sstevel@tonic-gate 		if (authmethods[i]->sufficient)
6047c478bd9Sstevel@tonic-gate 			sufficient++;
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	partial = (required + sufficient) > 0;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	buffer_init(&b);
6107c478bd9Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
6117c478bd9Sstevel@tonic-gate 		if (strcmp(authmethods[i]->name, "none") == 0)
6127c478bd9Sstevel@tonic-gate 			continue;
6137c478bd9Sstevel@tonic-gate 		if (required && !authmethods[i]->required)
6147c478bd9Sstevel@tonic-gate 			continue;
6157c478bd9Sstevel@tonic-gate 		if (sufficient && !required && !authmethods[i]->sufficient)
6167c478bd9Sstevel@tonic-gate 			continue;
6177c478bd9Sstevel@tonic-gate 		if (authmethods[i]->not_again)
6187c478bd9Sstevel@tonic-gate 			continue;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		if (authmethods[i]->required) {
6217c478bd9Sstevel@tonic-gate 			if (buffer_len(&b) > 0)
6227c478bd9Sstevel@tonic-gate 				buffer_append(&b, ",", 1);
6237c478bd9Sstevel@tonic-gate 			buffer_append(&b, authmethods[i]->name,
6247c478bd9Sstevel@tonic-gate 			    strlen(authmethods[i]->name));
6257c478bd9Sstevel@tonic-gate 			continue;
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		/*
6297c478bd9Sstevel@tonic-gate 		 * A method can be enabled (marked sufficient)
6307c478bd9Sstevel@tonic-gate 		 * dynamically provided that at least one other method
6317c478bd9Sstevel@tonic-gate 		 * has succeeded partially.
6327c478bd9Sstevel@tonic-gate 		 */
6337c478bd9Sstevel@tonic-gate 		if ((partial && authmethods[i]->sufficient) ||
6347c478bd9Sstevel@tonic-gate 		    (authmethods[i]->enabled != NULL &&
6357c478bd9Sstevel@tonic-gate 		    *(authmethods[i]->enabled) != 0)) {
6367c478bd9Sstevel@tonic-gate 			if (buffer_len(&b) > 0)
6377c478bd9Sstevel@tonic-gate 				buffer_append(&b, ",", 1);
6387c478bd9Sstevel@tonic-gate 			buffer_append(&b, authmethods[i]->name,
6397c478bd9Sstevel@tonic-gate 			    strlen(authmethods[i]->name));
6407c478bd9Sstevel@tonic-gate 		}
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 	buffer_append(&b, "\0", 1);
6437c478bd9Sstevel@tonic-gate 	list = xstrdup(buffer_ptr(&b));
6447c478bd9Sstevel@tonic-gate 	buffer_free(&b);
6457c478bd9Sstevel@tonic-gate 	return list;
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate static Authmethod *
6497c478bd9Sstevel@tonic-gate authmethod_lookup(const char *name)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	int i;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/*
6547c478bd9Sstevel@tonic-gate 	 * Method must be sufficient, required or enabled and must not
6557c478bd9Sstevel@tonic-gate 	 * be marked as not able to run again
6567c478bd9Sstevel@tonic-gate 	 */
6577c478bd9Sstevel@tonic-gate 	if (name != NULL)
6587c478bd9Sstevel@tonic-gate 		for (i = 0; authmethods[i] != NULL; i++)
6597c478bd9Sstevel@tonic-gate 			if (((authmethods[i]->sufficient ||
6607c478bd9Sstevel@tonic-gate 			      authmethods[i]->required) ||
6617c478bd9Sstevel@tonic-gate 			     (authmethods[i]->enabled != NULL &&
6627c478bd9Sstevel@tonic-gate 			      *(authmethods[i]->enabled) != 0)) &&
6637c478bd9Sstevel@tonic-gate 			    !authmethods[i]->not_again &&
6647c478bd9Sstevel@tonic-gate 			    strcmp(name, authmethods[i]->name) == 0)
6657c478bd9Sstevel@tonic-gate 				return authmethods[i];
6667c478bd9Sstevel@tonic-gate 	debug2("Unrecognized authentication method name: %s",
6677c478bd9Sstevel@tonic-gate 	    name ? name : "NULL");
6687c478bd9Sstevel@tonic-gate 	return NULL;
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate static void
6727c478bd9Sstevel@tonic-gate authmethod_count_attempt(Authmethod *method)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	if (!method)
6757c478bd9Sstevel@tonic-gate 		fatal("Internal error in authmethod_count_attempt()");
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (method->postponed)
6787c478bd9Sstevel@tonic-gate 		return;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	method->attempts++;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	if (method->abandoned)
6837c478bd9Sstevel@tonic-gate 		method->abandons++;
6847c478bd9Sstevel@tonic-gate 	else if (method->authenticated)
6857c478bd9Sstevel@tonic-gate 		method->successes++;
6867c478bd9Sstevel@tonic-gate 	else
6877c478bd9Sstevel@tonic-gate 		method->failures++;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	return;
6907c478bd9Sstevel@tonic-gate }
691