xref: /titanic_50/usr/src/cmd/ssh/sshd/auth1.c (revision 9a8058b57457911fab0e3b4b6f0a97740e7a816d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
37c478bd9Sstevel@tonic-gate  *                    All rights reserved
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
67c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
77c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
87c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
97c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate /*
12*9a8058b5Sjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
137c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
147c478bd9Sstevel@tonic-gate  */
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include "includes.h"
177c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth1.c,v 1.44 2002/09/26 11:38:43 markus Exp $");
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #include "xmalloc.h"
227c478bd9Sstevel@tonic-gate #include "rsa.h"
237c478bd9Sstevel@tonic-gate #include "ssh1.h"
247c478bd9Sstevel@tonic-gate #include "packet.h"
257c478bd9Sstevel@tonic-gate #include "buffer.h"
267c478bd9Sstevel@tonic-gate #include "mpaux.h"
277c478bd9Sstevel@tonic-gate #include "log.h"
287c478bd9Sstevel@tonic-gate #include "servconf.h"
297c478bd9Sstevel@tonic-gate #include "compat.h"
307c478bd9Sstevel@tonic-gate #include "auth.h"
317c478bd9Sstevel@tonic-gate #include "channels.h"
327c478bd9Sstevel@tonic-gate #include "session.h"
337c478bd9Sstevel@tonic-gate #include "uidswap.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef HAVE_BSM
367c478bd9Sstevel@tonic-gate #include "bsmaudit.h"
377c478bd9Sstevel@tonic-gate extern adt_session_data_t *ah;
387c478bd9Sstevel@tonic-gate #endif /* HAVE_BSM */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* import */
417c478bd9Sstevel@tonic-gate extern ServerOptions options;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * convert ssh auth msg type into description
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate static char *
477c478bd9Sstevel@tonic-gate get_authname(int type)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	static char buf[1024];
507c478bd9Sstevel@tonic-gate 	switch (type) {
517c478bd9Sstevel@tonic-gate 	case SSH_CMSG_AUTH_PASSWORD:
527c478bd9Sstevel@tonic-gate 		return "password";
537c478bd9Sstevel@tonic-gate 	case SSH_CMSG_AUTH_RSA:
547c478bd9Sstevel@tonic-gate 		return "rsa";
557c478bd9Sstevel@tonic-gate 	case SSH_CMSG_AUTH_RHOSTS_RSA:
567c478bd9Sstevel@tonic-gate 		return "rhosts-rsa";
577c478bd9Sstevel@tonic-gate 	case SSH_CMSG_AUTH_RHOSTS:
587c478bd9Sstevel@tonic-gate 		return "rhosts";
597c478bd9Sstevel@tonic-gate 	case SSH_CMSG_AUTH_TIS:
607c478bd9Sstevel@tonic-gate 	case SSH_CMSG_AUTH_TIS_RESPONSE:
617c478bd9Sstevel@tonic-gate 		return "challenge-response";
627c478bd9Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
637c478bd9Sstevel@tonic-gate 	case SSH_CMSG_AUTH_KERBEROS:
647c478bd9Sstevel@tonic-gate 		return "kerberos";
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
687c478bd9Sstevel@tonic-gate 	return buf;
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * read packets, try to authenticate the user and
737c478bd9Sstevel@tonic-gate  * return only if authentication is successful
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static void
767c478bd9Sstevel@tonic-gate do_authloop(Authctxt *authctxt)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	int authenticated = 0;
797c478bd9Sstevel@tonic-gate 	u_int bits;
807c478bd9Sstevel@tonic-gate 	Key *client_host_key;
817c478bd9Sstevel@tonic-gate 	BIGNUM *n;
827c478bd9Sstevel@tonic-gate 	char *client_user, *password;
837c478bd9Sstevel@tonic-gate 	char info[1024];
847c478bd9Sstevel@tonic-gate 	u_int dlen;
857c478bd9Sstevel@tonic-gate 	u_int ulen;
867c478bd9Sstevel@tonic-gate 	int type = 0;
877c478bd9Sstevel@tonic-gate 	struct passwd *pw = authctxt->pw;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	debug("Attempting authentication for %s%.100s.",
907c478bd9Sstevel@tonic-gate 	    authctxt->valid ? "" : "illegal user ", authctxt->user);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/* If the user has no password, accept authentication immediately. */
937c478bd9Sstevel@tonic-gate 	if (options.password_authentication &&
947c478bd9Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
957c478bd9Sstevel@tonic-gate 	    (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
967c478bd9Sstevel@tonic-gate #endif
97*9a8058b5Sjp161948 	    auth_password(authctxt, "")) {
987c478bd9Sstevel@tonic-gate 		auth_log(authctxt, 1, "without authentication", "");
997c478bd9Sstevel@tonic-gate 		return;
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/* Indicate that authentication is needed. */
1037c478bd9Sstevel@tonic-gate 	packet_start(SSH_SMSG_FAILURE);
1047c478bd9Sstevel@tonic-gate 	packet_send();
1057c478bd9Sstevel@tonic-gate 	packet_write_wait();
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	client_user = NULL;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	for ( ;; ) {
1107c478bd9Sstevel@tonic-gate 		/* default to fail */
1117c478bd9Sstevel@tonic-gate 		authenticated = 0;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		info[0] = '\0';
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		/* Get a packet from the client. */
1167c478bd9Sstevel@tonic-gate 		authctxt->v1_auth_type = type = packet_read();
1177c478bd9Sstevel@tonic-gate 		authctxt->v1_auth_name = get_authname(type);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		authctxt->attempt++;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		/* Process the packet. */
1227c478bd9Sstevel@tonic-gate 		switch (type) {
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
1257c478bd9Sstevel@tonic-gate 		case SSH_CMSG_AUTH_KERBEROS:
1267c478bd9Sstevel@tonic-gate 			if (!options.kerberos_authentication) {
1277c478bd9Sstevel@tonic-gate 				verbose("Kerberos authentication disabled.");
1287c478bd9Sstevel@tonic-gate 			} else {
1297c478bd9Sstevel@tonic-gate 				char *kdata = packet_get_string(&dlen);
1307c478bd9Sstevel@tonic-gate 				packet_check_eom();
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 				if (kdata[0] == 4) { /* KRB_PROT_VERSION */
1337c478bd9Sstevel@tonic-gate #ifdef KRB4
1347c478bd9Sstevel@tonic-gate 					KTEXT_ST tkt, reply;
1357c478bd9Sstevel@tonic-gate 					tkt.length = dlen;
1367c478bd9Sstevel@tonic-gate 					if (tkt.length < MAX_KTXT_LEN)
1377c478bd9Sstevel@tonic-gate 						memcpy(tkt.dat, kdata, tkt.length);
1387c478bd9Sstevel@tonic-gate 
139*9a8058b5Sjp161948 					if (auth_krb4(authctxt, &tkt,
140*9a8058b5Sjp161948 					    &client_user, &reply)) {
1417c478bd9Sstevel@tonic-gate 						authenticated = 1;
1427c478bd9Sstevel@tonic-gate 						snprintf(info, sizeof(info),
1437c478bd9Sstevel@tonic-gate 						    " tktuser %.100s",
1447c478bd9Sstevel@tonic-gate 						    client_user);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 						packet_start(
1477c478bd9Sstevel@tonic-gate 						    SSH_SMSG_AUTH_KERBEROS_RESPONSE);
1487c478bd9Sstevel@tonic-gate 						packet_put_string((char *)
1497c478bd9Sstevel@tonic-gate 						    reply.dat, reply.length);
1507c478bd9Sstevel@tonic-gate 						packet_send();
1517c478bd9Sstevel@tonic-gate 						packet_write_wait();
1527c478bd9Sstevel@tonic-gate 					}
1537c478bd9Sstevel@tonic-gate #endif /* KRB4 */
1547c478bd9Sstevel@tonic-gate 				} else {
1557c478bd9Sstevel@tonic-gate #ifdef KRB5
1567c478bd9Sstevel@tonic-gate 					krb5_data tkt, reply;
1577c478bd9Sstevel@tonic-gate 					tkt.length = dlen;
1587c478bd9Sstevel@tonic-gate 					tkt.data = kdata;
1597c478bd9Sstevel@tonic-gate 
160*9a8058b5Sjp161948 					if (auth_krb5(authctxt, &tkt,
161*9a8058b5Sjp161948 					    &client_user, &reply)) {
1627c478bd9Sstevel@tonic-gate 						authenticated = 1;
1637c478bd9Sstevel@tonic-gate 						snprintf(info, sizeof(info),
1647c478bd9Sstevel@tonic-gate 						    " tktuser %.100s",
1657c478bd9Sstevel@tonic-gate 						    client_user);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate  						/* Send response to client */
1687c478bd9Sstevel@tonic-gate  						packet_start(
1697c478bd9Sstevel@tonic-gate 						    SSH_SMSG_AUTH_KERBEROS_RESPONSE);
1707c478bd9Sstevel@tonic-gate  						packet_put_string((char *)
1717c478bd9Sstevel@tonic-gate 						    reply.data, reply.length);
1727c478bd9Sstevel@tonic-gate  						packet_send();
1737c478bd9Sstevel@tonic-gate  						packet_write_wait();
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate  						if (reply.length)
1767c478bd9Sstevel@tonic-gate  							xfree(reply.data);
1777c478bd9Sstevel@tonic-gate 					}
1787c478bd9Sstevel@tonic-gate #endif /* KRB5 */
1797c478bd9Sstevel@tonic-gate 				}
1807c478bd9Sstevel@tonic-gate 				xfree(kdata);
1817c478bd9Sstevel@tonic-gate 			}
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate #endif /* KRB4 || KRB5 */
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
1867c478bd9Sstevel@tonic-gate 			/* XXX - punt on backward compatibility here. */
1877c478bd9Sstevel@tonic-gate 		case SSH_CMSG_HAVE_KERBEROS_TGT:
1887c478bd9Sstevel@tonic-gate 			packet_send_debug("Kerberos TGT passing disabled before authentication.");
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate #ifdef AFS
1917c478bd9Sstevel@tonic-gate 		case SSH_CMSG_HAVE_AFS_TOKEN:
1927c478bd9Sstevel@tonic-gate 			packet_send_debug("AFS token passing disabled before authentication.");
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate #endif /* AFS */
1957c478bd9Sstevel@tonic-gate #endif /* AFS || KRB5 */
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		case SSH_CMSG_AUTH_RHOSTS:
1987c478bd9Sstevel@tonic-gate 			if (!options.rhosts_authentication) {
1997c478bd9Sstevel@tonic-gate 				verbose("Rhosts authentication disabled.");
2007c478bd9Sstevel@tonic-gate 				break;
2017c478bd9Sstevel@tonic-gate 			}
2027c478bd9Sstevel@tonic-gate 			/*
2037c478bd9Sstevel@tonic-gate 			 * Get client user name.  Note that we just have to
2047c478bd9Sstevel@tonic-gate 			 * trust the client; this is one reason why rhosts
2057c478bd9Sstevel@tonic-gate 			 * authentication is insecure. (Another is
2067c478bd9Sstevel@tonic-gate 			 * IP-spoofing on a local network.)
2077c478bd9Sstevel@tonic-gate 			 */
2087c478bd9Sstevel@tonic-gate 			client_user = packet_get_string(&ulen);
2097c478bd9Sstevel@tonic-gate 			packet_check_eom();
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 			/* Try to authenticate using /etc/hosts.equiv and .rhosts. */
2127c478bd9Sstevel@tonic-gate 			authenticated = auth_rhosts(pw, client_user);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 			snprintf(info, sizeof info, " ruser %.100s", client_user);
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		case SSH_CMSG_AUTH_RHOSTS_RSA:
2187c478bd9Sstevel@tonic-gate 			if (!options.rhosts_rsa_authentication) {
2197c478bd9Sstevel@tonic-gate 				verbose("Rhosts with RSA authentication disabled.");
2207c478bd9Sstevel@tonic-gate 				break;
2217c478bd9Sstevel@tonic-gate 			}
2227c478bd9Sstevel@tonic-gate 			/*
2237c478bd9Sstevel@tonic-gate 			 * Get client user name.  Note that we just have to
2247c478bd9Sstevel@tonic-gate 			 * trust the client; root on the client machine can
2257c478bd9Sstevel@tonic-gate 			 * claim to be any user.
2267c478bd9Sstevel@tonic-gate 			 */
2277c478bd9Sstevel@tonic-gate 			client_user = packet_get_string(&ulen);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 			/* Get the client host key. */
2307c478bd9Sstevel@tonic-gate 			client_host_key = key_new(KEY_RSA1);
2317c478bd9Sstevel@tonic-gate 			bits = packet_get_int();
2327c478bd9Sstevel@tonic-gate 			packet_get_bignum(client_host_key->rsa->e);
2337c478bd9Sstevel@tonic-gate 			packet_get_bignum(client_host_key->rsa->n);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 			if (bits != BN_num_bits(client_host_key->rsa->n))
2367c478bd9Sstevel@tonic-gate 				verbose("Warning: keysize mismatch for client_host_key: "
2377c478bd9Sstevel@tonic-gate 				    "actual %d, announced %d",
2387c478bd9Sstevel@tonic-gate 				    BN_num_bits(client_host_key->rsa->n), bits);
2397c478bd9Sstevel@tonic-gate 			packet_check_eom();
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 			authenticated = auth_rhosts_rsa(pw, client_user,
2427c478bd9Sstevel@tonic-gate 			    client_host_key);
2437c478bd9Sstevel@tonic-gate 			key_free(client_host_key);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 			snprintf(info, sizeof info, " ruser %.100s", client_user);
2467c478bd9Sstevel@tonic-gate 			break;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		case SSH_CMSG_AUTH_RSA:
2497c478bd9Sstevel@tonic-gate 			if (!options.rsa_authentication) {
2507c478bd9Sstevel@tonic-gate 				verbose("RSA authentication disabled.");
2517c478bd9Sstevel@tonic-gate 				break;
2527c478bd9Sstevel@tonic-gate 			}
2537c478bd9Sstevel@tonic-gate 			/* RSA authentication requested. */
2547c478bd9Sstevel@tonic-gate 			if ((n = BN_new()) == NULL)
2557c478bd9Sstevel@tonic-gate 				fatal("do_authloop: BN_new failed");
2567c478bd9Sstevel@tonic-gate 			packet_get_bignum(n);
2577c478bd9Sstevel@tonic-gate 			packet_check_eom();
2587c478bd9Sstevel@tonic-gate 			authenticated = auth_rsa(pw, n);
2597c478bd9Sstevel@tonic-gate 			BN_clear_free(n);
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		case SSH_CMSG_AUTH_PASSWORD:
2637c478bd9Sstevel@tonic-gate 			authctxt->init_attempt++;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 			if (!options.password_authentication) {
2667c478bd9Sstevel@tonic-gate 				verbose("Password authentication disabled.");
2677c478bd9Sstevel@tonic-gate 				break;
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 			/*
2707c478bd9Sstevel@tonic-gate 			 * Read user password.  It is in plain text, but was
2717c478bd9Sstevel@tonic-gate 			 * transmitted over the encrypted channel so it is
2727c478bd9Sstevel@tonic-gate 			 * not visible to an outside observer.
2737c478bd9Sstevel@tonic-gate 			 */
2747c478bd9Sstevel@tonic-gate 			password = packet_get_string(&dlen);
2757c478bd9Sstevel@tonic-gate 			packet_check_eom();
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 			/* Try authentication with the password. */
2787c478bd9Sstevel@tonic-gate 			if (authctxt->init_failures <
2797c478bd9Sstevel@tonic-gate 				options.max_init_auth_tries)
2807c478bd9Sstevel@tonic-gate 				authenticated =
281*9a8058b5Sjp161948 				    auth_password(authctxt, password);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			memset(password, 0, strlen(password));
2847c478bd9Sstevel@tonic-gate 			xfree(password);
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		case SSH_CMSG_AUTH_TIS:
2887c478bd9Sstevel@tonic-gate 			debug("rcvd SSH_CMSG_AUTH_TIS");
2897c478bd9Sstevel@tonic-gate 			if (options.challenge_response_authentication == 1) {
2907c478bd9Sstevel@tonic-gate 				char *challenge = get_challenge(authctxt);
2917c478bd9Sstevel@tonic-gate 				if (challenge != NULL) {
2927c478bd9Sstevel@tonic-gate 					debug("sending challenge '%s'", challenge);
2937c478bd9Sstevel@tonic-gate 					packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
2947c478bd9Sstevel@tonic-gate 					packet_put_cstring(challenge);
2957c478bd9Sstevel@tonic-gate 					xfree(challenge);
2967c478bd9Sstevel@tonic-gate 					packet_send();
2977c478bd9Sstevel@tonic-gate 					packet_write_wait();
2987c478bd9Sstevel@tonic-gate 					continue;
2997c478bd9Sstevel@tonic-gate 				}
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 			break;
3027c478bd9Sstevel@tonic-gate 		case SSH_CMSG_AUTH_TIS_RESPONSE:
3037c478bd9Sstevel@tonic-gate 			debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
3047c478bd9Sstevel@tonic-gate 			if (options.challenge_response_authentication == 1) {
3057c478bd9Sstevel@tonic-gate 				char *response = packet_get_string(&dlen);
3067c478bd9Sstevel@tonic-gate 				debug("got response '%s'", response);
3077c478bd9Sstevel@tonic-gate 				packet_check_eom();
3087c478bd9Sstevel@tonic-gate 				authenticated = verify_response(authctxt, response);
3097c478bd9Sstevel@tonic-gate 				memset(response, 'r', dlen);
3107c478bd9Sstevel@tonic-gate 				xfree(response);
3117c478bd9Sstevel@tonic-gate 			}
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		default:
3157c478bd9Sstevel@tonic-gate 			/*
3167c478bd9Sstevel@tonic-gate 			 * Any unknown messages will be ignored (and failure
3177c478bd9Sstevel@tonic-gate 			 * returned) during authentication.
3187c478bd9Sstevel@tonic-gate 			 */
3197c478bd9Sstevel@tonic-gate 			log("Unknown message during authentication: type %d", type);
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate #ifdef BSD_AUTH
3237c478bd9Sstevel@tonic-gate 		if (authctxt->as) {
3247c478bd9Sstevel@tonic-gate 			auth_close(authctxt->as);
3257c478bd9Sstevel@tonic-gate 			authctxt->as = NULL;
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate #endif
3287c478bd9Sstevel@tonic-gate 		if (!authctxt->valid && authenticated) {
3297c478bd9Sstevel@tonic-gate 			authenticated = 0;
3307c478bd9Sstevel@tonic-gate 			log("Ignoring authenticated invalid user %s",
3317c478bd9Sstevel@tonic-gate 			    authctxt->user);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate #ifdef _UNICOS
3357c478bd9Sstevel@tonic-gate 		if (type == SSH_CMSG_AUTH_PASSWORD && !authenticated)
3367c478bd9Sstevel@tonic-gate 			cray_login_failure(authctxt->user, IA_UDBERR);
3377c478bd9Sstevel@tonic-gate 		if (authenticated && cray_access_denied(authctxt->user)) {
3387c478bd9Sstevel@tonic-gate 			authenticated = 0;
3397c478bd9Sstevel@tonic-gate 			fatal("Access denied for user %s.",authctxt->user);
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
3447c478bd9Sstevel@tonic-gate 		if (authenticated &&
3457c478bd9Sstevel@tonic-gate 		    !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
3467c478bd9Sstevel@tonic-gate 			packet_disconnect("Authentication rejected for uid %d.",
3477c478bd9Sstevel@tonic-gate 			pw == NULL ? -1 : pw->pw_uid);
3487c478bd9Sstevel@tonic-gate 			authenticated = 0;
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate #else
3517c478bd9Sstevel@tonic-gate 		/* Special handling for root */
352*9a8058b5Sjp161948 		if (authenticated && authctxt->pw->pw_uid == 0 &&
3537c478bd9Sstevel@tonic-gate 		    !auth_root_allowed(get_authname(type)))
3547c478bd9Sstevel@tonic-gate 			authenticated = 0;
3557c478bd9Sstevel@tonic-gate #endif
3567c478bd9Sstevel@tonic-gate #ifdef USE_PAM
3577c478bd9Sstevel@tonic-gate 		if (authenticated && type != SSH_CMSG_AUTH_PASSWORD)
3587c478bd9Sstevel@tonic-gate 			authenticated = do_pam_non_initial_userauth(authctxt);
3597c478bd9Sstevel@tonic-gate 		else if (authenticated && !AUTHPAM_DONE(authctxt))
3607c478bd9Sstevel@tonic-gate 			authenticated = 0;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		if (!authenticated)
3637c478bd9Sstevel@tonic-gate 			authctxt->pam_retval = AUTHPAM_ERROR(authctxt,
3647c478bd9Sstevel@tonic-gate 				PAM_PERM_DENIED);
3657c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		/* Log before sending the reply */
3687c478bd9Sstevel@tonic-gate 		auth_log(authctxt, authenticated, get_authname(type), info);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		if (client_user != NULL) {
3717c478bd9Sstevel@tonic-gate 			xfree(client_user);
3727c478bd9Sstevel@tonic-gate 			client_user = NULL;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		if (authenticated)
3767c478bd9Sstevel@tonic-gate 			return;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		if (type == SSH_CMSG_AUTH_PASSWORD)
3797c478bd9Sstevel@tonic-gate 			authctxt->init_failures++;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		if (authctxt->failures++ > options.max_auth_tries) {
3827c478bd9Sstevel@tonic-gate #ifdef HAVE_BSM
3837c478bd9Sstevel@tonic-gate 			fatal_remove_cleanup(audit_failed_login_cleanup,
3847c478bd9Sstevel@tonic-gate 				authctxt);
3857c478bd9Sstevel@tonic-gate 			audit_sshd_login_failure(&ah, PAM_MAXTRIES);
3867c478bd9Sstevel@tonic-gate #endif /* HAVE_BSM */
3877c478bd9Sstevel@tonic-gate 			packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		packet_start(SSH_SMSG_FAILURE);
3917c478bd9Sstevel@tonic-gate 		packet_send();
3927c478bd9Sstevel@tonic-gate 		packet_write_wait();
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Performs authentication of an incoming connection.  Session key has already
3987c478bd9Sstevel@tonic-gate  * been exchanged and encryption is enabled.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate Authctxt *
4017c478bd9Sstevel@tonic-gate do_authentication(void)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	Authctxt *authctxt;
4047c478bd9Sstevel@tonic-gate 	u_int ulen;
4057c478bd9Sstevel@tonic-gate 	char *user, *style = NULL;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/* Get the name of the user that we wish to log in as. */
4087c478bd9Sstevel@tonic-gate 	packet_read_expect(SSH_CMSG_USER);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/* Get the user name. */
4117c478bd9Sstevel@tonic-gate 	user = packet_get_string(&ulen);
4127c478bd9Sstevel@tonic-gate 	packet_check_eom();
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if ((style = strchr(user, ':')) != NULL)
4157c478bd9Sstevel@tonic-gate 		*style++ = '\0';
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate #ifdef KRB5
4187c478bd9Sstevel@tonic-gate 	/* XXX - SSH.com Kerberos v5 braindeath. */
4197c478bd9Sstevel@tonic-gate 	if ((datafellows & SSH_BUG_K5USER) &&
4207c478bd9Sstevel@tonic-gate 	    options.kerberos_authentication) {
4217c478bd9Sstevel@tonic-gate 		char *p;
4227c478bd9Sstevel@tonic-gate 		if ((p = strchr(user, '@')) != NULL)
4237c478bd9Sstevel@tonic-gate 			*p = '\0';
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate #endif
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	authctxt = authctxt_new();
4287c478bd9Sstevel@tonic-gate 	authctxt->user = user;
4297c478bd9Sstevel@tonic-gate 	authctxt->style = style;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate #ifdef HAVE_BSM
4327c478bd9Sstevel@tonic-gate 	fatal_add_cleanup(audit_failed_login_cleanup, authctxt);
4337c478bd9Sstevel@tonic-gate #endif /* HAVE_BSM */
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/* Verify that the user is a valid user. */
436*9a8058b5Sjp161948 	if ((authctxt->pw = getpwnamallow(user)) != NULL) {
4377c478bd9Sstevel@tonic-gate 		authctxt->valid = 1;
4387c478bd9Sstevel@tonic-gate 	} else {
4397c478bd9Sstevel@tonic-gate 		authctxt->valid = 0;
4407c478bd9Sstevel@tonic-gate 		debug("do_authentication: illegal user %s", user);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
443*9a8058b5Sjp161948 	setproctitle("%s", authctxt->pw ? user : "unknown");
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/*
4467c478bd9Sstevel@tonic-gate 	 * If we are not running as root, the user must have the same uid as
4477c478bd9Sstevel@tonic-gate 	 * the server. (Unless you are running Windows)
4487c478bd9Sstevel@tonic-gate 	 */
4497c478bd9Sstevel@tonic-gate #ifndef HAVE_CYGWIN
450*9a8058b5Sjp161948 	if (getuid() != 0 && authctxt->pw &&
4517c478bd9Sstevel@tonic-gate 	    authctxt->pw->pw_uid != getuid())
4527c478bd9Sstevel@tonic-gate 		packet_disconnect("Cannot change user when server not running as root.");
4537c478bd9Sstevel@tonic-gate #endif
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * Loop until the user has been authenticated or the connection is
4577c478bd9Sstevel@tonic-gate 	 * closed, do_authloop() returns only if authentication is successful
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 	do_authloop(authctxt);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/* The user has been authenticated and accepted. */
4627c478bd9Sstevel@tonic-gate 	packet_start(SSH_SMSG_SUCCESS);
4637c478bd9Sstevel@tonic-gate 	packet_send();
4647c478bd9Sstevel@tonic-gate 	packet_write_wait();
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	return (authctxt);
4677c478bd9Sstevel@tonic-gate }
468