xref: /onnv-gate/usr/src/cmd/ssh/sshd/auth2.c (revision 5562:0f12179b71ab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate  * are met:
70Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate /*
25*5562Sjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
260Sstevel@tonic-gate  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "includes.h"
300Sstevel@tonic-gate RCSID("$OpenBSD: auth2.c,v 1.95 2002/08/22 21:33:58 markus Exp $");
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "ssh2.h"
350Sstevel@tonic-gate #include "xmalloc.h"
360Sstevel@tonic-gate #include "packet.h"
370Sstevel@tonic-gate #include "log.h"
380Sstevel@tonic-gate #include "servconf.h"
390Sstevel@tonic-gate #include "compat.h"
400Sstevel@tonic-gate #include "misc.h"
410Sstevel@tonic-gate #include "auth.h"
420Sstevel@tonic-gate #include "dispatch.h"
430Sstevel@tonic-gate #include "sshlogin.h"
440Sstevel@tonic-gate #include "pathnames.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #ifdef HAVE_BSM
470Sstevel@tonic-gate #include "bsmaudit.h"
480Sstevel@tonic-gate extern adt_session_data_t *ah;
490Sstevel@tonic-gate #endif /* HAVE_BSM */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #ifdef GSSAPI
520Sstevel@tonic-gate #include "ssh-gss.h"
530Sstevel@tonic-gate #endif
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /* import */
560Sstevel@tonic-gate extern ServerOptions options;
570Sstevel@tonic-gate extern u_char *session_id2;
580Sstevel@tonic-gate extern int session_id2_len;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate Authctxt *x_authctxt = NULL;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /* methods */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate extern Authmethod method_none;
650Sstevel@tonic-gate extern Authmethod method_pubkey;
660Sstevel@tonic-gate extern Authmethod method_passwd;
670Sstevel@tonic-gate extern Authmethod method_kbdint;
680Sstevel@tonic-gate extern Authmethod method_hostbased;
690Sstevel@tonic-gate extern Authmethod method_external;
700Sstevel@tonic-gate extern Authmethod method_gssapi;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static Authmethod *authmethods[] = {
730Sstevel@tonic-gate 	&method_none,
740Sstevel@tonic-gate #ifdef GSSAPI
750Sstevel@tonic-gate 	&method_external,
760Sstevel@tonic-gate 	&method_gssapi,
770Sstevel@tonic-gate #endif
780Sstevel@tonic-gate 	&method_pubkey,
790Sstevel@tonic-gate 	&method_passwd,
800Sstevel@tonic-gate 	&method_kbdint,
810Sstevel@tonic-gate 	&method_hostbased,
820Sstevel@tonic-gate 	NULL
830Sstevel@tonic-gate };
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /* protocol */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static void input_service_request(int, u_int32_t, void *);
880Sstevel@tonic-gate static void input_userauth_request(int, u_int32_t, void *);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /* helper */
910Sstevel@tonic-gate static Authmethod *authmethod_lookup(const char *);
920Sstevel@tonic-gate static char *authmethods_get(void);
930Sstevel@tonic-gate static char *authmethods_check_abandonment(Authctxt *authctxt,
940Sstevel@tonic-gate 					  Authmethod *method);
950Sstevel@tonic-gate static void  authmethod_count_attempt(Authmethod *method);
960Sstevel@tonic-gate /*static char *authmethods_get_kbdint(void);*/
970Sstevel@tonic-gate int user_key_allowed(struct passwd *, Key *);
980Sstevel@tonic-gate int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
990Sstevel@tonic-gate static int   userauth_method_can_run(Authmethod *method);
1000Sstevel@tonic-gate static void  userauth_reset_methods(void);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * loop until authctxt->success == TRUE
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate Authctxt *
1070Sstevel@tonic-gate do_authentication2(void)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	Authctxt *authctxt = authctxt_new();
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	x_authctxt = authctxt;		/*XXX*/
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #ifdef HAVE_BSM
1140Sstevel@tonic-gate 	fatal_add_cleanup(audit_failed_login_cleanup, authctxt);
1150Sstevel@tonic-gate #endif /* HAVE_BSM */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/* challenge-response is implemented via keyboard interactive */
1180Sstevel@tonic-gate 	if (options.challenge_response_authentication)
1190Sstevel@tonic-gate 		options.kbd_interactive_authentication = 1;
1200Sstevel@tonic-gate 	if (options.pam_authentication_via_kbd_int)
1210Sstevel@tonic-gate 		options.kbd_interactive_authentication = 1;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	dispatch_init(&dispatch_protocol_error);
1240Sstevel@tonic-gate 	dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1250Sstevel@tonic-gate 	dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	return (authctxt);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static void
1310Sstevel@tonic-gate input_service_request(int type, u_int32_t seq, void *ctxt)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	Authctxt *authctxt = ctxt;
1340Sstevel@tonic-gate 	u_int len;
1350Sstevel@tonic-gate 	int acceptit = 0;
1360Sstevel@tonic-gate 	char *service = packet_get_string(&len);
1370Sstevel@tonic-gate 	packet_check_eom();
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if (authctxt == NULL)
1400Sstevel@tonic-gate 		fatal("input_service_request: no authctxt");
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if (strcmp(service, "ssh-userauth") == 0) {
1430Sstevel@tonic-gate 		if (!authctxt->success) {
1440Sstevel@tonic-gate 			acceptit = 1;
1450Sstevel@tonic-gate 			/* now we can handle user-auth requests */
1460Sstevel@tonic-gate 			dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	/* XXX all other service requests are denied */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (acceptit) {
1520Sstevel@tonic-gate 		packet_start(SSH2_MSG_SERVICE_ACCEPT);
1530Sstevel@tonic-gate 		packet_put_cstring(service);
1540Sstevel@tonic-gate 		packet_send();
1550Sstevel@tonic-gate 		packet_write_wait();
1560Sstevel@tonic-gate 	} else {
1570Sstevel@tonic-gate 		debug("bad service request %s", service);
1580Sstevel@tonic-gate 		packet_disconnect("bad service request %s", service);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	xfree(service);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static void
1640Sstevel@tonic-gate input_userauth_request(int type, u_int32_t seq, void *ctxt)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	Authctxt *authctxt = ctxt;
1670Sstevel@tonic-gate 	Authmethod *m = NULL;
1680Sstevel@tonic-gate 	char *user, *service, *method, *style = NULL;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (authctxt == NULL)
1710Sstevel@tonic-gate 		fatal("input_userauth_request: no authctxt");
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	user = packet_get_string(NULL);
1740Sstevel@tonic-gate 	service = packet_get_string(NULL);
1750Sstevel@tonic-gate 	method = packet_get_string(NULL);
1760Sstevel@tonic-gate 	debug("userauth-request for user %s service %s method %s", user,
1770Sstevel@tonic-gate 		service, method);
1780Sstevel@tonic-gate 	debug("attempt %d initial attempt %d failures %d initial failures %d",
1790Sstevel@tonic-gate 		authctxt->attempt, authctxt->init_attempt,
1800Sstevel@tonic-gate 		authctxt->failures, authctxt->init_failures);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	m = authmethod_lookup(method);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if ((style = strchr(user, ':')) != NULL)
1850Sstevel@tonic-gate 		*style++ = 0;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	authctxt->attempt++;
1880Sstevel@tonic-gate 	if (m != NULL && m->is_initial)
1890Sstevel@tonic-gate 		authctxt->init_attempt++;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (authctxt->attempt == 1) {
1920Sstevel@tonic-gate 		/* setup auth context */
193*5562Sjp161948 		authctxt->pw = getpwnamallow(user);
1940Sstevel@tonic-gate 		/* May want to abstract SSHv2 services someday */
1950Sstevel@tonic-gate 		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1960Sstevel@tonic-gate 			/* enforced in userauth_finish() below */
1970Sstevel@tonic-gate 			authctxt->valid = 1;
1980Sstevel@tonic-gate 			debug2("input_userauth_request: setting up authctxt for %s", user);
1990Sstevel@tonic-gate 		} else {
2000Sstevel@tonic-gate 			log("input_userauth_request: illegal user %s", user);
2010Sstevel@tonic-gate 		}
202*5562Sjp161948 		setproctitle("%s", authctxt->pw ? user : "unknown");
2030Sstevel@tonic-gate 		authctxt->user = xstrdup(user);
2040Sstevel@tonic-gate 		authctxt->service = xstrdup(service);
2050Sstevel@tonic-gate 		authctxt->style = style ? xstrdup(style) : NULL;
2060Sstevel@tonic-gate 		userauth_reset_methods();
2070Sstevel@tonic-gate 	} else {
2080Sstevel@tonic-gate 		char *abandoned;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		/*
2110Sstevel@tonic-gate 		 * Check for abandoned [multi-round-trip] userauths
2120Sstevel@tonic-gate 		 * methods (e.g., kbdint).  Userauth method abandonment
2130Sstevel@tonic-gate 		 * should be treated as userauth method failure and
2140Sstevel@tonic-gate 		 * counted against max_auth_tries.
2150Sstevel@tonic-gate 		 */
2160Sstevel@tonic-gate 		abandoned = authmethods_check_abandonment(authctxt, m);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		if (abandoned != NULL &&
2190Sstevel@tonic-gate 		    authctxt->failures > options.max_auth_tries) {
2200Sstevel@tonic-gate 			/* userauth_finish() will now packet_disconnect() */
2210Sstevel@tonic-gate 			userauth_finish(authctxt, abandoned);
2220Sstevel@tonic-gate 			/* NOTREACHED */
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		/* Handle user|service changes, possibly packet_disconnect() */
2260Sstevel@tonic-gate 		userauth_user_svc_change(authctxt, user, service);
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	authctxt->method = m;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/* run userauth method, try to authenticate user */
2320Sstevel@tonic-gate 	if (m != NULL && userauth_method_can_run(m)) {
2330Sstevel@tonic-gate 		debug2("input_userauth_request: try method %s", method);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		m->postponed = 0;
2360Sstevel@tonic-gate 		m->abandoned = 0;
2370Sstevel@tonic-gate 		m->authenticated = 0;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		if (!m->is_initial ||
2400Sstevel@tonic-gate 		    authctxt->init_failures < options.max_init_auth_tries)
2410Sstevel@tonic-gate 			m->userauth(authctxt);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		authmethod_count_attempt(m);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		if (authctxt->unwind_dispatch_loop) {
2460Sstevel@tonic-gate 			/*
2470Sstevel@tonic-gate 			 * Method ran nested dispatch loop but was
2480Sstevel@tonic-gate 			 * abandoned.  Cleanup and return without doing
2490Sstevel@tonic-gate 			 * anything else; we're just unwinding the stack.
2500Sstevel@tonic-gate 			 */
2510Sstevel@tonic-gate 			authctxt->unwind_dispatch_loop = 0;
2520Sstevel@tonic-gate 			goto done;
2530Sstevel@tonic-gate 		}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		if (m->postponed)
2560Sstevel@tonic-gate 			goto done; /* multi-round trip userauth not finished */
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		if (m->abandoned) {
2590Sstevel@tonic-gate 			/* multi-round trip userauth abandoned, log failure */
2600Sstevel@tonic-gate 			auth_log(authctxt, 0, method, " ssh2");
2610Sstevel@tonic-gate 			goto done;
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	userauth_finish(authctxt, method);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate done:
2680Sstevel@tonic-gate 	xfree(service);
2690Sstevel@tonic-gate 	xfree(user);
2700Sstevel@tonic-gate 	xfree(method);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate void
2740Sstevel@tonic-gate userauth_finish(Authctxt *authctxt, char *method)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	int authenticated, partial;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (authctxt == NULL)
2790Sstevel@tonic-gate 		fatal("%s: missing context", __func__);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/* unknown method handling -- must elicit userauth failure msg */
2820Sstevel@tonic-gate 	if (authctxt->method == NULL) {
2830Sstevel@tonic-gate 		authenticated = 0;
2840Sstevel@tonic-gate 		partial = 0;
2850Sstevel@tonic-gate 		goto done_checking;
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate #ifndef USE_PAM
2890Sstevel@tonic-gate 	/* Special handling for root (done elsewhere for PAM) */
290*5562Sjp161948 	if (authctxt->method->authenticated &&
2910Sstevel@tonic-gate 	    authctxt->pw != NULL && authctxt->pw->pw_uid == 0 &&
2920Sstevel@tonic-gate 	    !auth_root_allowed(method))
2930Sstevel@tonic-gate 		authctxt->method->authenticated = 0;
2940Sstevel@tonic-gate #endif /* USE_PAM */
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate #ifdef _UNICOS
2970Sstevel@tonic-gate 	if (authctxt->method->authenticated &&
2980Sstevel@tonic-gate 	    cray_access_denied(authctxt->user)) {
2990Sstevel@tonic-gate 		authctxt->method->authenticated = 0;
3000Sstevel@tonic-gate 		fatal("Access denied for user %s.",authctxt->user);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate #endif /* _UNICOS */
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	partial = userauth_check_partial_failure(authctxt);
3050Sstevel@tonic-gate 	authenticated = authctxt->method->authenticated;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate #ifdef USE_PAM
3080Sstevel@tonic-gate 	/*
3090Sstevel@tonic-gate 	 * If the userauth method failed to complete PAM work then force
3100Sstevel@tonic-gate 	 * partial failure.
3110Sstevel@tonic-gate 	 */
3120Sstevel@tonic-gate 	if (authenticated && !AUTHPAM_DONE(authctxt))
3130Sstevel@tonic-gate 		partial = 1;
3140Sstevel@tonic-gate #endif /* USE_PAM */
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/*
3170Sstevel@tonic-gate 	 * To properly support invalid userauth method names we set
3180Sstevel@tonic-gate 	 * authenticated=0, partial=0 above and know that
3190Sstevel@tonic-gate 	 * authctxt->method == NULL.
3200Sstevel@tonic-gate 	 *
3210Sstevel@tonic-gate 	 * No unguarded reference to authctxt->method allowed from here.
3220Sstevel@tonic-gate 	 * Checking authenticated != 0 is a valid guard; authctxt->method
3230Sstevel@tonic-gate 	 * MUST NOT be NULL if authenticated.
3240Sstevel@tonic-gate 	 */
3250Sstevel@tonic-gate done_checking:
3260Sstevel@tonic-gate 	if (!authctxt->valid && authenticated) {
3270Sstevel@tonic-gate 		/*
3280Sstevel@tonic-gate 		 * Should never happen -- if it does PAM's at fault
3290Sstevel@tonic-gate 		 * but we need not panic, just treat as a failure.
3300Sstevel@tonic-gate 		 */
3310Sstevel@tonic-gate 		authctxt->method->authenticated = 0;
3320Sstevel@tonic-gate 		authenticated = 0;
3330Sstevel@tonic-gate 		log("Ignoring authenticated invalid user %s",
3340Sstevel@tonic-gate 		    authctxt->user);
3350Sstevel@tonic-gate 		auth_log(authctxt, 0, method, " ssh2");
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	/* Log before sending the reply */
3390Sstevel@tonic-gate 	auth_log(authctxt, authenticated, method, " ssh2");
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if (authenticated && !partial) {
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		/* turn off userauth */
3440Sstevel@tonic-gate 		dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
3450Sstevel@tonic-gate 		packet_start(SSH2_MSG_USERAUTH_SUCCESS);
3460Sstevel@tonic-gate 		packet_send();
3470Sstevel@tonic-gate 		packet_write_wait();
3480Sstevel@tonic-gate 		/* now we can break out */
3490Sstevel@tonic-gate 		authctxt->success = 1;
3500Sstevel@tonic-gate 	} else {
3510Sstevel@tonic-gate 		char *methods;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		if (authctxt->method && authctxt->method->is_initial)
3540Sstevel@tonic-gate 			authctxt->init_failures++;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 		authctxt->method = NULL;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate #ifdef USE_PAM
3590Sstevel@tonic-gate 		/*
3600Sstevel@tonic-gate 		 * Keep track of last PAM error (or PERM_DENIED) for BSM
3610Sstevel@tonic-gate 		 * login failure auditing, which may run after the PAM
3620Sstevel@tonic-gate 		 * state has been cleaned up.
3630Sstevel@tonic-gate 		 */
3640Sstevel@tonic-gate 		authctxt->pam_retval = AUTHPAM_ERROR(authctxt, PAM_PERM_DENIED);
3650Sstevel@tonic-gate #endif /* USE_PAM */
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		if (authctxt->failures++ > options.max_auth_tries) {
3680Sstevel@tonic-gate #ifdef HAVE_BSM
3690Sstevel@tonic-gate 			fatal_remove_cleanup(audit_failed_login_cleanup,
3700Sstevel@tonic-gate 				authctxt);
3710Sstevel@tonic-gate 			audit_sshd_login_failure(&ah, PAM_MAXTRIES);
3720Sstevel@tonic-gate #endif /* HAVE_BSM */
3730Sstevel@tonic-gate 			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate #ifdef _UNICOS
3770Sstevel@tonic-gate 		if (strcmp(method, "password") == 0)
3780Sstevel@tonic-gate 			cray_login_failure(authctxt->user, IA_UDBERR);
3790Sstevel@tonic-gate #endif /* _UNICOS */
3800Sstevel@tonic-gate 		packet_start(SSH2_MSG_USERAUTH_FAILURE);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		/*
3830Sstevel@tonic-gate 		 * If (partial) then authmethods_get() will return only
3840Sstevel@tonic-gate 		 * required methods, likely only "keyboard-interactive;"
3850Sstevel@tonic-gate 		 * (methods == NULL) implies failure, even if (partial == 1)
3860Sstevel@tonic-gate 		 */
3870Sstevel@tonic-gate 		methods = authmethods_get();
3880Sstevel@tonic-gate 		packet_put_cstring(methods);
3890Sstevel@tonic-gate 		packet_put_char((authenticated && partial && methods) ? 1 : 0);
3900Sstevel@tonic-gate 		if (methods)
3910Sstevel@tonic-gate 			xfree(methods);
3920Sstevel@tonic-gate 		packet_send();
3930Sstevel@tonic-gate 		packet_write_wait();
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /* get current user */
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate struct passwd*
4000Sstevel@tonic-gate auth_get_user(void)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate #define	DELIM	","
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate #if 0
4080Sstevel@tonic-gate static char *
4090Sstevel@tonic-gate authmethods_get_kbdint(void)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	Buffer b;
4120Sstevel@tonic-gate 	int i;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
4150Sstevel@tonic-gate 		if (strcmp(authmethods[i]->name, "keyboard-interactive") != 0)
4160Sstevel@tonic-gate 			continue;
4170Sstevel@tonic-gate 		return xstrdup(authmethods[i]->name);
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 	return NULL;
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate #endif
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate void
4240Sstevel@tonic-gate userauth_user_svc_change(Authctxt *authctxt, char *user, char *service)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	/*
4270Sstevel@tonic-gate 	 * NOTE:
4280Sstevel@tonic-gate 	 *
4290Sstevel@tonic-gate 	 * SSHv2 services should be abstracted and service changes during
4300Sstevel@tonic-gate 	 * userauth should be supported as per the userauth draft.  In the PAM
4310Sstevel@tonic-gate 	 * case, support for multiple SSHv2 services means that we have to
4320Sstevel@tonic-gate 	 * format the PAM service name according to the SSHv2 service *and* the
4330Sstevel@tonic-gate 	 * SSHv2 userauth being attempted ("passwd", "kbdint" and "other").
4340Sstevel@tonic-gate 	 *
4350Sstevel@tonic-gate 	 * We'll cross that bridge when we come to it.  For now disallow service
4360Sstevel@tonic-gate 	 * changes during userauth if using PAM, but allow username changes.
4370Sstevel@tonic-gate 	 */
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/* authctxt->service must == ssh-connection here */
4400Sstevel@tonic-gate 	if (service != NULL && strcmp(service, authctxt->service) != 0) {
4410Sstevel@tonic-gate 		packet_disconnect("Change of service not "
4420Sstevel@tonic-gate 				  "allowed: %s and %s",
4430Sstevel@tonic-gate 				  authctxt->service, service);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 	if (user != NULL && authctxt->user != NULL &&
4460Sstevel@tonic-gate 	    strcmp(user, authctxt->user) == 0)
4470Sstevel@tonic-gate 		return;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/* All good; update authctxt */
4500Sstevel@tonic-gate 	xfree(authctxt->user);
4510Sstevel@tonic-gate 	authctxt->user = xstrdup(user);
4520Sstevel@tonic-gate 	pwfree(&authctxt->pw);
453*5562Sjp161948 	authctxt->pw = getpwnamallow(user);
4540Sstevel@tonic-gate 	authctxt->valid = (authctxt->pw != NULL);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/* Forget method state; abandon postponed userauths */
4570Sstevel@tonic-gate 	userauth_reset_methods();
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate int
4610Sstevel@tonic-gate userauth_check_partial_failure(Authctxt *authctxt)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	int i;
4640Sstevel@tonic-gate 	int required = 0;
4650Sstevel@tonic-gate 	int sufficient = 0;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	/*
4680Sstevel@tonic-gate 	 * v1 does not set authctxt->method
4690Sstevel@tonic-gate 	 * partial userauth failure is a v2 concept
4700Sstevel@tonic-gate 	 */
4710Sstevel@tonic-gate 	if (authctxt->method == NULL)
4720Sstevel@tonic-gate 		return 0;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
4750Sstevel@tonic-gate 		if (authmethods[i]->required)
4760Sstevel@tonic-gate 			required++;
4770Sstevel@tonic-gate 		if (authmethods[i]->sufficient)
4780Sstevel@tonic-gate 			sufficient++;
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	if (required == 0 && sufficient == 0)
4820Sstevel@tonic-gate 		return !authctxt->method->authenticated;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if (required == 1 && authctxt->method->required)
4850Sstevel@tonic-gate 		return !authctxt->method->authenticated;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (sufficient && authctxt->method->sufficient)
4880Sstevel@tonic-gate 		return !authctxt->method->authenticated;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	return 1;
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate int
4940Sstevel@tonic-gate userauth_method_can_run(Authmethod *method)
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate 	if (method->not_again)
4970Sstevel@tonic-gate 		return 0;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	return 1;
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate static
5030Sstevel@tonic-gate void
5040Sstevel@tonic-gate userauth_reset_methods(void)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	int i;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
5090Sstevel@tonic-gate 		/* note: counters not reset */
5100Sstevel@tonic-gate 		authmethods[i]->required = 0;
5110Sstevel@tonic-gate 		authmethods[i]->sufficient = 0;
5120Sstevel@tonic-gate 		authmethods[i]->authenticated = 0;
5130Sstevel@tonic-gate 		authmethods[i]->not_again = 0;
5140Sstevel@tonic-gate 		authmethods[i]->postponed = 0;
5150Sstevel@tonic-gate 		authmethods[i]->abandoned = 0;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate void
5200Sstevel@tonic-gate userauth_force_kbdint(void)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	int i;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
5250Sstevel@tonic-gate 		authmethods[i]->required = 0;
5260Sstevel@tonic-gate 		authmethods[i]->sufficient = 0;
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 	method_kbdint.required = 1;
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate /*
5320Sstevel@tonic-gate  * Check to see if a previously run multi-round trip userauth method has
5330Sstevel@tonic-gate  * been abandoned and call its cleanup function.
5340Sstevel@tonic-gate  *
5350Sstevel@tonic-gate  * Abandoned userauth method invocations are counted as userauth failures.
5360Sstevel@tonic-gate  */
5370Sstevel@tonic-gate static
5380Sstevel@tonic-gate char *
5390Sstevel@tonic-gate authmethods_check_abandonment(Authctxt *authctxt, Authmethod *method)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate 	int i;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	/* optimization: check current method first */
5440Sstevel@tonic-gate 	if (method && method->postponed) {
5450Sstevel@tonic-gate 		method->postponed = 0;
5460Sstevel@tonic-gate 		if (method->abandon)
5470Sstevel@tonic-gate 			method->abandon(authctxt, method);
5480Sstevel@tonic-gate 		else
5490Sstevel@tonic-gate 			method->abandons++;
5500Sstevel@tonic-gate 		authctxt->failures++; /* abandonment -> failure */
5510Sstevel@tonic-gate 		if (method->is_initial)
5520Sstevel@tonic-gate 			authctxt->init_failures++;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		/*
5550Sstevel@tonic-gate 		 * Since we check for abandonment whenever a userauth is
5560Sstevel@tonic-gate 		 * requested we know only one method could have been
5570Sstevel@tonic-gate 		 * in postponed state, so we can return now.
5580Sstevel@tonic-gate 		 */
5590Sstevel@tonic-gate 		return (method->name);
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
5620Sstevel@tonic-gate 		if (!authmethods[i]->postponed)
5630Sstevel@tonic-gate 			continue;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		/* some method was postponed and a diff one is being started */
5660Sstevel@tonic-gate 		if (method != authmethods[i]) {
5670Sstevel@tonic-gate 			authmethods[i]->postponed = 0;
5680Sstevel@tonic-gate 			if (authmethods[i]->abandon)
5690Sstevel@tonic-gate 				authmethods[i]->abandon(authctxt,
5700Sstevel@tonic-gate 							authmethods[i]);
5710Sstevel@tonic-gate 			else
5720Sstevel@tonic-gate 				authmethods[i]->abandons++;
5730Sstevel@tonic-gate 			authctxt->failures++;
5740Sstevel@tonic-gate 			if (authmethods[i]->is_initial)
5750Sstevel@tonic-gate 				authctxt->init_failures++;
5760Sstevel@tonic-gate 			return (authmethods[i]->name); /* see above */
5770Sstevel@tonic-gate 		}
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	return NULL;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate static char *
5840Sstevel@tonic-gate authmethods_get(void)
5850Sstevel@tonic-gate {
5860Sstevel@tonic-gate 	Buffer b;
5870Sstevel@tonic-gate 	char *list;
5880Sstevel@tonic-gate 	int i;
5890Sstevel@tonic-gate 	int sufficient = 0;
5900Sstevel@tonic-gate 	int required = 0;
5910Sstevel@tonic-gate 	int authenticated = 0;
5920Sstevel@tonic-gate 	int partial = 0;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/*
5950Sstevel@tonic-gate 	 * If at least one method succeeded partially then at least one
5960Sstevel@tonic-gate 	 * authmethod will be required and only required methods should
5970Sstevel@tonic-gate 	 * continue.
5980Sstevel@tonic-gate 	 */
5990Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
6000Sstevel@tonic-gate 		if (authmethods[i]->authenticated)
6010Sstevel@tonic-gate 			authenticated++;
6020Sstevel@tonic-gate 		if (authmethods[i]->required)
6030Sstevel@tonic-gate 			required++;
6040Sstevel@tonic-gate 		if (authmethods[i]->sufficient)
6050Sstevel@tonic-gate 			sufficient++;
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	partial = (required + sufficient) > 0;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	buffer_init(&b);
6110Sstevel@tonic-gate 	for (i = 0; authmethods[i] != NULL; i++) {
6120Sstevel@tonic-gate 		if (strcmp(authmethods[i]->name, "none") == 0)
6130Sstevel@tonic-gate 			continue;
6140Sstevel@tonic-gate 		if (required && !authmethods[i]->required)
6150Sstevel@tonic-gate 			continue;
6160Sstevel@tonic-gate 		if (sufficient && !required && !authmethods[i]->sufficient)
6170Sstevel@tonic-gate 			continue;
6180Sstevel@tonic-gate 		if (authmethods[i]->not_again)
6190Sstevel@tonic-gate 			continue;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		if (authmethods[i]->required) {
6220Sstevel@tonic-gate 			if (buffer_len(&b) > 0)
6230Sstevel@tonic-gate 				buffer_append(&b, ",", 1);
6240Sstevel@tonic-gate 			buffer_append(&b, authmethods[i]->name,
6250Sstevel@tonic-gate 			    strlen(authmethods[i]->name));
6260Sstevel@tonic-gate 			continue;
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 		/*
6300Sstevel@tonic-gate 		 * A method can be enabled (marked sufficient)
6310Sstevel@tonic-gate 		 * dynamically provided that at least one other method
6320Sstevel@tonic-gate 		 * has succeeded partially.
6330Sstevel@tonic-gate 		 */
6340Sstevel@tonic-gate 		if ((partial && authmethods[i]->sufficient) ||
6350Sstevel@tonic-gate 		    (authmethods[i]->enabled != NULL &&
6360Sstevel@tonic-gate 		    *(authmethods[i]->enabled) != 0)) {
6370Sstevel@tonic-gate 			if (buffer_len(&b) > 0)
6380Sstevel@tonic-gate 				buffer_append(&b, ",", 1);
6390Sstevel@tonic-gate 			buffer_append(&b, authmethods[i]->name,
6400Sstevel@tonic-gate 			    strlen(authmethods[i]->name));
6410Sstevel@tonic-gate 		}
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 	buffer_append(&b, "\0", 1);
6440Sstevel@tonic-gate 	list = xstrdup(buffer_ptr(&b));
6450Sstevel@tonic-gate 	buffer_free(&b);
6460Sstevel@tonic-gate 	return list;
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate static Authmethod *
6500Sstevel@tonic-gate authmethod_lookup(const char *name)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	int i;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/*
6550Sstevel@tonic-gate 	 * Method must be sufficient, required or enabled and must not
6560Sstevel@tonic-gate 	 * be marked as not able to run again
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	if (name != NULL)
6590Sstevel@tonic-gate 		for (i = 0; authmethods[i] != NULL; i++)
6600Sstevel@tonic-gate 			if (((authmethods[i]->sufficient ||
6610Sstevel@tonic-gate 			      authmethods[i]->required) ||
6620Sstevel@tonic-gate 			     (authmethods[i]->enabled != NULL &&
6630Sstevel@tonic-gate 			      *(authmethods[i]->enabled) != 0)) &&
6640Sstevel@tonic-gate 			    !authmethods[i]->not_again &&
6650Sstevel@tonic-gate 			    strcmp(name, authmethods[i]->name) == 0)
6660Sstevel@tonic-gate 				return authmethods[i];
6670Sstevel@tonic-gate 	debug2("Unrecognized authentication method name: %s",
6680Sstevel@tonic-gate 	    name ? name : "NULL");
6690Sstevel@tonic-gate 	return NULL;
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate static void
6730Sstevel@tonic-gate authmethod_count_attempt(Authmethod *method)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	if (!method)
6760Sstevel@tonic-gate 		fatal("Internal error in authmethod_count_attempt()");
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	if (method->postponed)
6790Sstevel@tonic-gate 		return;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	method->attempts++;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	if (method->abandoned)
6840Sstevel@tonic-gate 		method->abandons++;
6850Sstevel@tonic-gate 	else if (method->authenticated)
6860Sstevel@tonic-gate 		method->successes++;
6870Sstevel@tonic-gate 	else
6880Sstevel@tonic-gate 		method->failures++;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	return;
6910Sstevel@tonic-gate }
692