10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
30Sstevel@tonic-gate * All rights reserved
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
60Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
70Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
80Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
90Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
100Sstevel@tonic-gate */
110Sstevel@tonic-gate /*
12*8064SBrent.Paulson@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
130Sstevel@tonic-gate * Use is subject to license terms.
140Sstevel@tonic-gate */
150Sstevel@tonic-gate
160Sstevel@tonic-gate #include "includes.h"
170Sstevel@tonic-gate RCSID("$OpenBSD: auth1.c,v 1.44 2002/09/26 11:38:43 markus Exp $");
180Sstevel@tonic-gate
190Sstevel@tonic-gate #include "xmalloc.h"
200Sstevel@tonic-gate #include "rsa.h"
210Sstevel@tonic-gate #include "ssh1.h"
220Sstevel@tonic-gate #include "packet.h"
230Sstevel@tonic-gate #include "buffer.h"
240Sstevel@tonic-gate #include "mpaux.h"
250Sstevel@tonic-gate #include "log.h"
260Sstevel@tonic-gate #include "servconf.h"
270Sstevel@tonic-gate #include "compat.h"
280Sstevel@tonic-gate #include "auth.h"
290Sstevel@tonic-gate #include "channels.h"
300Sstevel@tonic-gate #include "session.h"
310Sstevel@tonic-gate #include "uidswap.h"
320Sstevel@tonic-gate
330Sstevel@tonic-gate #ifdef HAVE_BSM
340Sstevel@tonic-gate #include "bsmaudit.h"
350Sstevel@tonic-gate extern adt_session_data_t *ah;
360Sstevel@tonic-gate #endif /* HAVE_BSM */
370Sstevel@tonic-gate
380Sstevel@tonic-gate /* import */
390Sstevel@tonic-gate extern ServerOptions options;
400Sstevel@tonic-gate
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate * convert ssh auth msg type into description
430Sstevel@tonic-gate */
440Sstevel@tonic-gate static char *
get_authname(int type)450Sstevel@tonic-gate get_authname(int type)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate static char buf[1024];
480Sstevel@tonic-gate switch (type) {
490Sstevel@tonic-gate case SSH_CMSG_AUTH_PASSWORD:
500Sstevel@tonic-gate return "password";
510Sstevel@tonic-gate case SSH_CMSG_AUTH_RSA:
520Sstevel@tonic-gate return "rsa";
530Sstevel@tonic-gate case SSH_CMSG_AUTH_RHOSTS_RSA:
540Sstevel@tonic-gate return "rhosts-rsa";
550Sstevel@tonic-gate case SSH_CMSG_AUTH_RHOSTS:
560Sstevel@tonic-gate return "rhosts";
570Sstevel@tonic-gate case SSH_CMSG_AUTH_TIS:
580Sstevel@tonic-gate case SSH_CMSG_AUTH_TIS_RESPONSE:
590Sstevel@tonic-gate return "challenge-response";
600Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
610Sstevel@tonic-gate case SSH_CMSG_AUTH_KERBEROS:
620Sstevel@tonic-gate return "kerberos";
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate }
650Sstevel@tonic-gate snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
660Sstevel@tonic-gate return buf;
670Sstevel@tonic-gate }
680Sstevel@tonic-gate
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate * read packets, try to authenticate the user and
710Sstevel@tonic-gate * return only if authentication is successful
720Sstevel@tonic-gate */
730Sstevel@tonic-gate static void
do_authloop(Authctxt * authctxt)740Sstevel@tonic-gate do_authloop(Authctxt *authctxt)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate int authenticated = 0;
770Sstevel@tonic-gate u_int bits;
780Sstevel@tonic-gate Key *client_host_key;
790Sstevel@tonic-gate BIGNUM *n;
800Sstevel@tonic-gate char *client_user, *password;
810Sstevel@tonic-gate char info[1024];
820Sstevel@tonic-gate u_int dlen;
830Sstevel@tonic-gate u_int ulen;
840Sstevel@tonic-gate int type = 0;
850Sstevel@tonic-gate struct passwd *pw = authctxt->pw;
860Sstevel@tonic-gate
870Sstevel@tonic-gate debug("Attempting authentication for %s%.100s.",
880Sstevel@tonic-gate authctxt->valid ? "" : "illegal user ", authctxt->user);
890Sstevel@tonic-gate
900Sstevel@tonic-gate /* If the user has no password, accept authentication immediately. */
910Sstevel@tonic-gate if (options.password_authentication &&
920Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
930Sstevel@tonic-gate (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
940Sstevel@tonic-gate #endif
955562Sjp161948 auth_password(authctxt, "")) {
960Sstevel@tonic-gate auth_log(authctxt, 1, "without authentication", "");
970Sstevel@tonic-gate return;
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate /* Indicate that authentication is needed. */
1010Sstevel@tonic-gate packet_start(SSH_SMSG_FAILURE);
1020Sstevel@tonic-gate packet_send();
1030Sstevel@tonic-gate packet_write_wait();
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate client_user = NULL;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate for ( ;; ) {
1080Sstevel@tonic-gate /* default to fail */
1090Sstevel@tonic-gate authenticated = 0;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate info[0] = '\0';
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /* Get a packet from the client. */
1140Sstevel@tonic-gate authctxt->v1_auth_type = type = packet_read();
1150Sstevel@tonic-gate authctxt->v1_auth_name = get_authname(type);
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate authctxt->attempt++;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /* Process the packet. */
1200Sstevel@tonic-gate switch (type) {
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate #if defined(KRB4) || defined(KRB5)
1230Sstevel@tonic-gate case SSH_CMSG_AUTH_KERBEROS:
1240Sstevel@tonic-gate if (!options.kerberos_authentication) {
1250Sstevel@tonic-gate verbose("Kerberos authentication disabled.");
1260Sstevel@tonic-gate } else {
1270Sstevel@tonic-gate char *kdata = packet_get_string(&dlen);
1280Sstevel@tonic-gate packet_check_eom();
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (kdata[0] == 4) { /* KRB_PROT_VERSION */
1310Sstevel@tonic-gate #ifdef KRB4
1320Sstevel@tonic-gate KTEXT_ST tkt, reply;
1330Sstevel@tonic-gate tkt.length = dlen;
1340Sstevel@tonic-gate if (tkt.length < MAX_KTXT_LEN)
1350Sstevel@tonic-gate memcpy(tkt.dat, kdata, tkt.length);
1360Sstevel@tonic-gate
1375562Sjp161948 if (auth_krb4(authctxt, &tkt,
1385562Sjp161948 &client_user, &reply)) {
1390Sstevel@tonic-gate authenticated = 1;
1400Sstevel@tonic-gate snprintf(info, sizeof(info),
1410Sstevel@tonic-gate " tktuser %.100s",
1420Sstevel@tonic-gate client_user);
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate packet_start(
1450Sstevel@tonic-gate SSH_SMSG_AUTH_KERBEROS_RESPONSE);
1460Sstevel@tonic-gate packet_put_string((char *)
1470Sstevel@tonic-gate reply.dat, reply.length);
1480Sstevel@tonic-gate packet_send();
1490Sstevel@tonic-gate packet_write_wait();
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate #endif /* KRB4 */
1520Sstevel@tonic-gate } else {
1530Sstevel@tonic-gate #ifdef KRB5
1540Sstevel@tonic-gate krb5_data tkt, reply;
1550Sstevel@tonic-gate tkt.length = dlen;
1560Sstevel@tonic-gate tkt.data = kdata;
1570Sstevel@tonic-gate
1585562Sjp161948 if (auth_krb5(authctxt, &tkt,
1595562Sjp161948 &client_user, &reply)) {
1600Sstevel@tonic-gate authenticated = 1;
1610Sstevel@tonic-gate snprintf(info, sizeof(info),
1620Sstevel@tonic-gate " tktuser %.100s",
1630Sstevel@tonic-gate client_user);
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /* Send response to client */
1660Sstevel@tonic-gate packet_start(
1670Sstevel@tonic-gate SSH_SMSG_AUTH_KERBEROS_RESPONSE);
1680Sstevel@tonic-gate packet_put_string((char *)
1690Sstevel@tonic-gate reply.data, reply.length);
1700Sstevel@tonic-gate packet_send();
1710Sstevel@tonic-gate packet_write_wait();
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if (reply.length)
1740Sstevel@tonic-gate xfree(reply.data);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate #endif /* KRB5 */
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate xfree(kdata);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate break;
1810Sstevel@tonic-gate #endif /* KRB4 || KRB5 */
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate #if defined(AFS) || defined(KRB5)
1840Sstevel@tonic-gate /* XXX - punt on backward compatibility here. */
1850Sstevel@tonic-gate case SSH_CMSG_HAVE_KERBEROS_TGT:
1860Sstevel@tonic-gate packet_send_debug("Kerberos TGT passing disabled before authentication.");
1870Sstevel@tonic-gate break;
1880Sstevel@tonic-gate #ifdef AFS
1890Sstevel@tonic-gate case SSH_CMSG_HAVE_AFS_TOKEN:
1900Sstevel@tonic-gate packet_send_debug("AFS token passing disabled before authentication.");
1910Sstevel@tonic-gate break;
1920Sstevel@tonic-gate #endif /* AFS */
1930Sstevel@tonic-gate #endif /* AFS || KRB5 */
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate case SSH_CMSG_AUTH_RHOSTS:
1960Sstevel@tonic-gate if (!options.rhosts_authentication) {
1970Sstevel@tonic-gate verbose("Rhosts authentication disabled.");
1980Sstevel@tonic-gate break;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate * Get client user name. Note that we just have to
2020Sstevel@tonic-gate * trust the client; this is one reason why rhosts
2030Sstevel@tonic-gate * authentication is insecure. (Another is
2040Sstevel@tonic-gate * IP-spoofing on a local network.)
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate client_user = packet_get_string(&ulen);
2070Sstevel@tonic-gate packet_check_eom();
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
2100Sstevel@tonic-gate authenticated = auth_rhosts(pw, client_user);
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate snprintf(info, sizeof info, " ruser %.100s", client_user);
2130Sstevel@tonic-gate break;
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate case SSH_CMSG_AUTH_RHOSTS_RSA:
2160Sstevel@tonic-gate if (!options.rhosts_rsa_authentication) {
2170Sstevel@tonic-gate verbose("Rhosts with RSA authentication disabled.");
2180Sstevel@tonic-gate break;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate * Get client user name. Note that we just have to
2220Sstevel@tonic-gate * trust the client; root on the client machine can
2230Sstevel@tonic-gate * claim to be any user.
2240Sstevel@tonic-gate */
2250Sstevel@tonic-gate client_user = packet_get_string(&ulen);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /* Get the client host key. */
2280Sstevel@tonic-gate client_host_key = key_new(KEY_RSA1);
2290Sstevel@tonic-gate bits = packet_get_int();
2300Sstevel@tonic-gate packet_get_bignum(client_host_key->rsa->e);
2310Sstevel@tonic-gate packet_get_bignum(client_host_key->rsa->n);
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (bits != BN_num_bits(client_host_key->rsa->n))
2340Sstevel@tonic-gate verbose("Warning: keysize mismatch for client_host_key: "
2350Sstevel@tonic-gate "actual %d, announced %d",
2360Sstevel@tonic-gate BN_num_bits(client_host_key->rsa->n), bits);
2370Sstevel@tonic-gate packet_check_eom();
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate authenticated = auth_rhosts_rsa(pw, client_user,
2400Sstevel@tonic-gate client_host_key);
2410Sstevel@tonic-gate key_free(client_host_key);
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate snprintf(info, sizeof info, " ruser %.100s", client_user);
2440Sstevel@tonic-gate break;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate case SSH_CMSG_AUTH_RSA:
2470Sstevel@tonic-gate if (!options.rsa_authentication) {
2480Sstevel@tonic-gate verbose("RSA authentication disabled.");
2490Sstevel@tonic-gate break;
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate /* RSA authentication requested. */
2520Sstevel@tonic-gate if ((n = BN_new()) == NULL)
2530Sstevel@tonic-gate fatal("do_authloop: BN_new failed");
2540Sstevel@tonic-gate packet_get_bignum(n);
2550Sstevel@tonic-gate packet_check_eom();
2560Sstevel@tonic-gate authenticated = auth_rsa(pw, n);
2570Sstevel@tonic-gate BN_clear_free(n);
2580Sstevel@tonic-gate break;
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate case SSH_CMSG_AUTH_PASSWORD:
2610Sstevel@tonic-gate authctxt->init_attempt++;
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate if (!options.password_authentication) {
2640Sstevel@tonic-gate verbose("Password authentication disabled.");
2650Sstevel@tonic-gate break;
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * Read user password. It is in plain text, but was
2690Sstevel@tonic-gate * transmitted over the encrypted channel so it is
2700Sstevel@tonic-gate * not visible to an outside observer.
2710Sstevel@tonic-gate */
2720Sstevel@tonic-gate password = packet_get_string(&dlen);
2730Sstevel@tonic-gate packet_check_eom();
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate /* Try authentication with the password. */
2760Sstevel@tonic-gate if (authctxt->init_failures <
2770Sstevel@tonic-gate options.max_init_auth_tries)
2780Sstevel@tonic-gate authenticated =
2795562Sjp161948 auth_password(authctxt, password);
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate memset(password, 0, strlen(password));
2820Sstevel@tonic-gate xfree(password);
2830Sstevel@tonic-gate break;
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate case SSH_CMSG_AUTH_TIS:
2860Sstevel@tonic-gate debug("rcvd SSH_CMSG_AUTH_TIS");
2870Sstevel@tonic-gate if (options.challenge_response_authentication == 1) {
2880Sstevel@tonic-gate char *challenge = get_challenge(authctxt);
2890Sstevel@tonic-gate if (challenge != NULL) {
2900Sstevel@tonic-gate debug("sending challenge '%s'", challenge);
2910Sstevel@tonic-gate packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
2920Sstevel@tonic-gate packet_put_cstring(challenge);
2930Sstevel@tonic-gate xfree(challenge);
2940Sstevel@tonic-gate packet_send();
2950Sstevel@tonic-gate packet_write_wait();
2960Sstevel@tonic-gate continue;
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate break;
3000Sstevel@tonic-gate case SSH_CMSG_AUTH_TIS_RESPONSE:
3010Sstevel@tonic-gate debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
3020Sstevel@tonic-gate if (options.challenge_response_authentication == 1) {
3030Sstevel@tonic-gate char *response = packet_get_string(&dlen);
3040Sstevel@tonic-gate debug("got response '%s'", response);
3050Sstevel@tonic-gate packet_check_eom();
3060Sstevel@tonic-gate authenticated = verify_response(authctxt, response);
3070Sstevel@tonic-gate memset(response, 'r', dlen);
3080Sstevel@tonic-gate xfree(response);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate break;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate default:
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate * Any unknown messages will be ignored (and failure
3150Sstevel@tonic-gate * returned) during authentication.
3160Sstevel@tonic-gate */
3170Sstevel@tonic-gate log("Unknown message during authentication: type %d", type);
3180Sstevel@tonic-gate break;
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate #ifdef BSD_AUTH
3210Sstevel@tonic-gate if (authctxt->as) {
3220Sstevel@tonic-gate auth_close(authctxt->as);
3230Sstevel@tonic-gate authctxt->as = NULL;
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate #endif
3260Sstevel@tonic-gate if (!authctxt->valid && authenticated) {
3270Sstevel@tonic-gate authenticated = 0;
3280Sstevel@tonic-gate log("Ignoring authenticated invalid user %s",
3290Sstevel@tonic-gate authctxt->user);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate #ifdef _UNICOS
3330Sstevel@tonic-gate if (type == SSH_CMSG_AUTH_PASSWORD && !authenticated)
3340Sstevel@tonic-gate cray_login_failure(authctxt->user, IA_UDBERR);
3350Sstevel@tonic-gate if (authenticated && cray_access_denied(authctxt->user)) {
3360Sstevel@tonic-gate authenticated = 0;
3370Sstevel@tonic-gate fatal("Access denied for user %s.",authctxt->user);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate #endif /* _UNICOS */
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate #ifdef HAVE_CYGWIN
3420Sstevel@tonic-gate if (authenticated &&
3430Sstevel@tonic-gate !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
3440Sstevel@tonic-gate packet_disconnect("Authentication rejected for uid %d.",
3450Sstevel@tonic-gate pw == NULL ? -1 : pw->pw_uid);
3460Sstevel@tonic-gate authenticated = 0;
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate #else
3490Sstevel@tonic-gate /* Special handling for root */
3505562Sjp161948 if (authenticated && authctxt->pw->pw_uid == 0 &&
3510Sstevel@tonic-gate !auth_root_allowed(get_authname(type)))
3520Sstevel@tonic-gate authenticated = 0;
3530Sstevel@tonic-gate #endif
3540Sstevel@tonic-gate #ifdef USE_PAM
3550Sstevel@tonic-gate if (authenticated && type != SSH_CMSG_AUTH_PASSWORD)
3560Sstevel@tonic-gate authenticated = do_pam_non_initial_userauth(authctxt);
3570Sstevel@tonic-gate else if (authenticated && !AUTHPAM_DONE(authctxt))
3580Sstevel@tonic-gate authenticated = 0;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate if (!authenticated)
3610Sstevel@tonic-gate authctxt->pam_retval = AUTHPAM_ERROR(authctxt,
3620Sstevel@tonic-gate PAM_PERM_DENIED);
3630Sstevel@tonic-gate #endif /* USE_PAM */
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate /* Log before sending the reply */
3660Sstevel@tonic-gate auth_log(authctxt, authenticated, get_authname(type), info);
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate if (client_user != NULL) {
3690Sstevel@tonic-gate xfree(client_user);
3700Sstevel@tonic-gate client_user = NULL;
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate if (authenticated)
3740Sstevel@tonic-gate return;
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate if (type == SSH_CMSG_AUTH_PASSWORD)
3770Sstevel@tonic-gate authctxt->init_failures++;
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate if (authctxt->failures++ > options.max_auth_tries) {
3800Sstevel@tonic-gate #ifdef HAVE_BSM
3810Sstevel@tonic-gate fatal_remove_cleanup(audit_failed_login_cleanup,
3820Sstevel@tonic-gate authctxt);
383*8064SBrent.Paulson@Sun.COM audit_sshd_login_failure(&ah, PAM_MAXTRIES,
384*8064SBrent.Paulson@Sun.COM authctxt->user);
3850Sstevel@tonic-gate #endif /* HAVE_BSM */
3860Sstevel@tonic-gate packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate packet_start(SSH_SMSG_FAILURE);
3900Sstevel@tonic-gate packet_send();
3910Sstevel@tonic-gate packet_write_wait();
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate * Performs authentication of an incoming connection. Session key has already
3970Sstevel@tonic-gate * been exchanged and encryption is enabled.
3980Sstevel@tonic-gate */
3990Sstevel@tonic-gate Authctxt *
do_authentication(void)4000Sstevel@tonic-gate do_authentication(void)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate Authctxt *authctxt;
4030Sstevel@tonic-gate u_int ulen;
4040Sstevel@tonic-gate char *user, *style = NULL;
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate /* Get the name of the user that we wish to log in as. */
4070Sstevel@tonic-gate packet_read_expect(SSH_CMSG_USER);
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate /* Get the user name. */
4100Sstevel@tonic-gate user = packet_get_string(&ulen);
4110Sstevel@tonic-gate packet_check_eom();
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate if ((style = strchr(user, ':')) != NULL)
4140Sstevel@tonic-gate *style++ = '\0';
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate #ifdef KRB5
4170Sstevel@tonic-gate /* XXX - SSH.com Kerberos v5 braindeath. */
4180Sstevel@tonic-gate if ((datafellows & SSH_BUG_K5USER) &&
4190Sstevel@tonic-gate options.kerberos_authentication) {
4200Sstevel@tonic-gate char *p;
4210Sstevel@tonic-gate if ((p = strchr(user, '@')) != NULL)
4220Sstevel@tonic-gate *p = '\0';
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate #endif
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate authctxt = authctxt_new();
4270Sstevel@tonic-gate authctxt->user = user;
4280Sstevel@tonic-gate authctxt->style = style;
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate #ifdef HAVE_BSM
4310Sstevel@tonic-gate fatal_add_cleanup(audit_failed_login_cleanup, authctxt);
4320Sstevel@tonic-gate #endif /* HAVE_BSM */
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate /* Verify that the user is a valid user. */
4355562Sjp161948 if ((authctxt->pw = getpwnamallow(user)) != NULL) {
4360Sstevel@tonic-gate authctxt->valid = 1;
4370Sstevel@tonic-gate } else {
4380Sstevel@tonic-gate authctxt->valid = 0;
4390Sstevel@tonic-gate debug("do_authentication: illegal user %s", user);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate
4425562Sjp161948 setproctitle("%s", authctxt->pw ? user : "unknown");
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate /*
4450Sstevel@tonic-gate * If we are not running as root, the user must have the same uid as
4460Sstevel@tonic-gate * the server. (Unless you are running Windows)
4470Sstevel@tonic-gate */
4480Sstevel@tonic-gate #ifndef HAVE_CYGWIN
4495562Sjp161948 if (getuid() != 0 && authctxt->pw &&
4500Sstevel@tonic-gate authctxt->pw->pw_uid != getuid())
4510Sstevel@tonic-gate packet_disconnect("Cannot change user when server not running as root.");
4520Sstevel@tonic-gate #endif
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate /*
4550Sstevel@tonic-gate * Loop until the user has been authenticated or the connection is
4560Sstevel@tonic-gate * closed, do_authloop() returns only if authentication is successful
4570Sstevel@tonic-gate */
4580Sstevel@tonic-gate do_authloop(authctxt);
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate /* The user has been authenticated and accepted. */
4610Sstevel@tonic-gate packet_start(SSH_SMSG_SUCCESS);
4620Sstevel@tonic-gate packet_send();
4630Sstevel@tonic-gate packet_write_wait();
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate return (authctxt);
4660Sstevel@tonic-gate }
467