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