Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 3 | * All rights reserved |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 4 | * |
| 5 | * As far as I am concerned, the code I have written for this software |
| 6 | * can be used freely for any purpose. Any derived versions of this |
| 7 | * software must be clearly marked as such, and if the derived work is |
| 8 | * incompatible with the protocol description in the RFC file, it must be |
| 9 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "includes.h" |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 13 | RCSID("$OpenBSD: auth1.c,v 1.14 2001/01/22 23:06:39 markus Exp $"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 14 | |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 15 | #ifdef HAVE_OSF_SIA |
| 16 | # include <sia.h> |
| 17 | # include <siad.h> |
| 18 | #endif |
| 19 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 20 | #include "xmalloc.h" |
| 21 | #include "rsa.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 22 | #include "ssh1.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 23 | #include "packet.h" |
| 24 | #include "buffer.h" |
| 25 | #include "mpaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 26 | #include "log.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 27 | #include "servconf.h" |
| 28 | #include "compat.h" |
| 29 | #include "auth.h" |
| 30 | #include "session.h" |
| 31 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 32 | /* import */ |
| 33 | extern ServerOptions options; |
| 34 | extern char *forced_command; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 35 | |
| 36 | #ifdef WITH_AIXAUTHENTICATE |
| 37 | extern char *aixloginmsg; |
| 38 | #endif /* WITH_AIXAUTHENTICATE */ |
Damien Miller | fe668e4 | 2000-07-08 10:44:13 +1000 | [diff] [blame] | 39 | #ifdef HAVE_OSF_SIA |
| 40 | extern int saved_argc; |
| 41 | extern char **saved_argv; |
| 42 | #endif /* HAVE_OSF_SIA */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * convert ssh auth msg type into description |
| 46 | */ |
| 47 | char * |
| 48 | get_authname(int type) |
| 49 | { |
| 50 | static char buf[1024]; |
| 51 | switch (type) { |
| 52 | case SSH_CMSG_AUTH_PASSWORD: |
| 53 | return "password"; |
| 54 | case SSH_CMSG_AUTH_RSA: |
| 55 | return "rsa"; |
| 56 | case SSH_CMSG_AUTH_RHOSTS_RSA: |
| 57 | return "rhosts-rsa"; |
| 58 | case SSH_CMSG_AUTH_RHOSTS: |
| 59 | return "rhosts"; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 60 | case SSH_CMSG_AUTH_TIS: |
| 61 | case SSH_CMSG_AUTH_TIS_RESPONSE: |
| 62 | return "challenge-response"; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 63 | #ifdef KRB4 |
| 64 | case SSH_CMSG_AUTH_KERBEROS: |
| 65 | return "kerberos"; |
| 66 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 67 | } |
| 68 | snprintf(buf, sizeof buf, "bad-auth-msg-%d", type); |
| 69 | return buf; |
| 70 | } |
| 71 | |
| 72 | /* |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 73 | * read packets, try to authenticate the user and |
| 74 | * return only if authentication is successful |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 75 | */ |
| 76 | void |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 77 | do_authloop(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 78 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 79 | int authenticated = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 80 | u_int bits; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 81 | RSA *client_host_key; |
| 82 | BIGNUM *n; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 83 | char *client_user, *password; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 84 | char info[1024]; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 85 | u_int dlen; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 86 | int plen, nlen, elen; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 87 | u_int ulen; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 88 | int type = 0; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 89 | struct passwd *pw = authctxt->pw; |
| 90 | |
| 91 | debug("Attempting authentication for %s%.100s.", |
| 92 | authctxt->valid ? "" : "illegal user ", authctxt->user); |
| 93 | |
| 94 | /* If the user has no password, accept authentication immediately. */ |
| 95 | if (options.password_authentication && |
| 96 | #ifdef KRB4 |
| 97 | (!options.kerberos_authentication || options.kerberos_or_local_passwd) && |
| 98 | #endif |
Ben Lindstrom | b100ec9 | 2001-01-19 05:37:32 +0000 | [diff] [blame] | 99 | #ifdef USE_PAM |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 100 | auth_pam_password(pw, password)) { |
| 101 | #else |
| 102 | auth_password(pw, "")) { |
| 103 | #endif |
| 104 | auth_log(authctxt, 1, "without authentication", ""); |
| 105 | return; |
| 106 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 107 | |
| 108 | /* Indicate that authentication is needed. */ |
| 109 | packet_start(SSH_SMSG_FAILURE); |
| 110 | packet_send(); |
| 111 | packet_write_wait(); |
| 112 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 113 | client_user = NULL; |
| 114 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 115 | for (;;) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 116 | /* default to fail */ |
| 117 | authenticated = 0; |
| 118 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 119 | info[0] = '\0'; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 120 | |
| 121 | /* Get a packet from the client. */ |
| 122 | type = packet_read(&plen); |
| 123 | |
| 124 | /* Process the packet. */ |
| 125 | switch (type) { |
| 126 | #ifdef AFS |
| 127 | case SSH_CMSG_HAVE_KERBEROS_TGT: |
| 128 | if (!options.kerberos_tgt_passing) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 129 | verbose("Kerberos tgt passing disabled."); |
| 130 | break; |
| 131 | } else { |
| 132 | /* Accept Kerberos tgt. */ |
| 133 | char *tgt = packet_get_string(&dlen); |
| 134 | packet_integrity_check(plen, 4 + dlen, type); |
| 135 | if (!auth_kerberos_tgt(pw, tgt)) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 136 | verbose("Kerberos tgt REFUSED for %.100s", authctxt->user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 137 | xfree(tgt); |
| 138 | } |
| 139 | continue; |
| 140 | |
| 141 | case SSH_CMSG_HAVE_AFS_TOKEN: |
| 142 | if (!options.afs_token_passing || !k_hasafs()) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 143 | verbose("AFS token passing disabled."); |
| 144 | break; |
| 145 | } else { |
| 146 | /* Accept AFS token. */ |
| 147 | char *token_string = packet_get_string(&dlen); |
| 148 | packet_integrity_check(plen, 4 + dlen, type); |
| 149 | if (!auth_afs_token(pw, token_string)) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 150 | verbose("AFS token REFUSED for %.100s", authctxt->user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 151 | xfree(token_string); |
| 152 | } |
| 153 | continue; |
| 154 | #endif /* AFS */ |
| 155 | #ifdef KRB4 |
| 156 | case SSH_CMSG_AUTH_KERBEROS: |
| 157 | if (!options.kerberos_authentication) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 158 | verbose("Kerberos authentication disabled."); |
| 159 | break; |
| 160 | } else { |
| 161 | /* Try Kerberos v4 authentication. */ |
| 162 | KTEXT_ST auth; |
| 163 | char *tkt_user = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 164 | char *kdata = packet_get_string((u_int *) &auth.length); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 165 | packet_integrity_check(plen, 4 + auth.length, type); |
| 166 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 167 | if (authctxt->valid) { |
| 168 | if (auth.length < MAX_KTXT_LEN) |
| 169 | memcpy(auth.dat, kdata, auth.length); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 170 | authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user); |
| 171 | if (authenticated) { |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 172 | snprintf(info, sizeof info, |
| 173 | " tktuser %.100s", tkt_user); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 174 | xfree(tkt_user); |
| 175 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 176 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 177 | xfree(kdata); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 178 | } |
| 179 | break; |
| 180 | #endif /* KRB4 */ |
| 181 | |
| 182 | case SSH_CMSG_AUTH_RHOSTS: |
| 183 | if (!options.rhosts_authentication) { |
| 184 | verbose("Rhosts authentication disabled."); |
| 185 | break; |
| 186 | } |
| 187 | /* |
| 188 | * Get client user name. Note that we just have to |
| 189 | * trust the client; this is one reason why rhosts |
| 190 | * authentication is insecure. (Another is |
| 191 | * IP-spoofing on a local network.) |
| 192 | */ |
| 193 | client_user = packet_get_string(&ulen); |
| 194 | packet_integrity_check(plen, 4 + ulen, type); |
| 195 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 196 | /* Try to authenticate using /etc/hosts.equiv and .rhosts. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 197 | authenticated = auth_rhosts(pw, client_user); |
| 198 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 199 | snprintf(info, sizeof info, " ruser %.100s", client_user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 200 | break; |
| 201 | |
| 202 | case SSH_CMSG_AUTH_RHOSTS_RSA: |
| 203 | if (!options.rhosts_rsa_authentication) { |
| 204 | verbose("Rhosts with RSA authentication disabled."); |
| 205 | break; |
| 206 | } |
| 207 | /* |
| 208 | * Get client user name. Note that we just have to |
| 209 | * trust the client; root on the client machine can |
| 210 | * claim to be any user. |
| 211 | */ |
| 212 | client_user = packet_get_string(&ulen); |
| 213 | |
| 214 | /* Get the client host key. */ |
| 215 | client_host_key = RSA_new(); |
| 216 | if (client_host_key == NULL) |
| 217 | fatal("RSA_new failed"); |
| 218 | client_host_key->e = BN_new(); |
| 219 | client_host_key->n = BN_new(); |
| 220 | if (client_host_key->e == NULL || client_host_key->n == NULL) |
| 221 | fatal("BN_new failed"); |
| 222 | bits = packet_get_int(); |
| 223 | packet_get_bignum(client_host_key->e, &elen); |
| 224 | packet_get_bignum(client_host_key->n, &nlen); |
| 225 | |
| 226 | if (bits != BN_num_bits(client_host_key->n)) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 227 | verbose("Warning: keysize mismatch for client_host_key: " |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 228 | "actual %d, announced %d", BN_num_bits(client_host_key->n), bits); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 229 | packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type); |
| 230 | |
| 231 | authenticated = auth_rhosts_rsa(pw, client_user, client_host_key); |
| 232 | RSA_free(client_host_key); |
| 233 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 234 | snprintf(info, sizeof info, " ruser %.100s", client_user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 235 | break; |
| 236 | |
| 237 | case SSH_CMSG_AUTH_RSA: |
| 238 | if (!options.rsa_authentication) { |
| 239 | verbose("RSA authentication disabled."); |
| 240 | break; |
| 241 | } |
| 242 | /* RSA authentication requested. */ |
| 243 | n = BN_new(); |
| 244 | packet_get_bignum(n, &nlen); |
| 245 | packet_integrity_check(plen, nlen, type); |
| 246 | authenticated = auth_rsa(pw, n); |
| 247 | BN_clear_free(n); |
| 248 | break; |
| 249 | |
| 250 | case SSH_CMSG_AUTH_PASSWORD: |
| 251 | if (!options.password_authentication) { |
| 252 | verbose("Password authentication disabled."); |
| 253 | break; |
| 254 | } |
| 255 | /* |
| 256 | * Read user password. It is in plain text, but was |
| 257 | * transmitted over the encrypted channel so it is |
| 258 | * not visible to an outside observer. |
| 259 | */ |
| 260 | password = packet_get_string(&dlen); |
| 261 | packet_integrity_check(plen, 4 + dlen, type); |
| 262 | |
| 263 | #ifdef USE_PAM |
| 264 | /* Do PAM auth with password */ |
| 265 | authenticated = auth_pam_password(pw, password); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 266 | #elif defined(HAVE_OSF_SIA) |
| 267 | /* Do SIA auth with password */ |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 268 | if (sia_validate_user(NULL, saved_argc, saved_argv, |
| 269 | get_canonical_hostname(options.reverse_mapping_check), |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 270 | pw->pw_name, NULL, 0, NULL, password) == SIASUCCESS) { |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 271 | authenticated = 1; |
| 272 | } |
| 273 | #else /* !USE_PAM && !HAVE_OSF_SIA */ |
Kevin Steves | 12aaa04 | 2001-01-24 21:23:39 +0000 | [diff] [blame] | 274 | /* Try authentication with the password. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 275 | authenticated = auth_password(pw, password); |
| 276 | #endif /* USE_PAM */ |
| 277 | |
| 278 | memset(password, 0, strlen(password)); |
| 279 | xfree(password); |
| 280 | break; |
| 281 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 282 | case SSH_CMSG_AUTH_TIS: |
| 283 | debug("rcvd SSH_CMSG_AUTH_TIS"); |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 284 | if (options.challenge_reponse_authentication == 1) { |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 285 | char *challenge = get_challenge(authctxt, authctxt->style); |
| 286 | if (challenge != NULL) { |
| 287 | debug("sending challenge '%s'", challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 288 | packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 289 | packet_put_cstring(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 290 | packet_send(); |
| 291 | packet_write_wait(); |
| 292 | continue; |
| 293 | } |
| 294 | } |
| 295 | break; |
| 296 | case SSH_CMSG_AUTH_TIS_RESPONSE: |
| 297 | debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE"); |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 298 | if (options.challenge_reponse_authentication == 1) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 299 | char *response = packet_get_string(&dlen); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 300 | debug("got response '%s'", response); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 301 | packet_integrity_check(plen, 4 + dlen, type); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 302 | authenticated = verify_response(authctxt, response); |
| 303 | memset(response, 'r', dlen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 304 | xfree(response); |
| 305 | } |
| 306 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 307 | |
| 308 | default: |
| 309 | /* |
| 310 | * Any unknown messages will be ignored (and failure |
| 311 | * returned) during authentication. |
| 312 | */ |
| 313 | log("Unknown message during authentication: type %d", type); |
| 314 | break; |
| 315 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 316 | if (!authctxt->valid && authenticated) |
| 317 | fatal("INTERNAL ERROR: authenticated invalid user %s", |
| 318 | authctxt->user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 319 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 320 | #ifdef HAVE_CYGWIN |
| 321 | if (authenticated && |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 322 | !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD,pw->pw_uid)) { |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 323 | packet_disconnect("Authentication rejected for uid %d.", |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 324 | (int)pw->pw_uid); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 325 | authenticated = 0; |
| 326 | } |
Ben Lindstrom | 5dc8150 | 2001-01-19 06:10:29 +0000 | [diff] [blame] | 327 | #else |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 328 | /* Special handling for root */ |
| 329 | if (authenticated && authctxt->pw->pw_uid == 0 && !auth_root_allowed()) |
| 330 | authenticated = 0; |
Ben Lindstrom | 5dc8150 | 2001-01-19 06:10:29 +0000 | [diff] [blame] | 331 | #endif |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 332 | #ifdef USE_PAM |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 333 | if (authenticated && !do_pam_account(pw->pw_name, client_user)) |
| 334 | authenticated = 0; |
| 335 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 336 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 337 | /* Log before sending the reply */ |
| 338 | auth_log(authctxt, authenticated, get_authname(type), info); |
| 339 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 340 | if (client_user != NULL) { |
| 341 | xfree(client_user); |
| 342 | client_user = NULL; |
| 343 | } |
| 344 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 345 | if (authenticated) |
| 346 | return; |
| 347 | |
Kevin Steves | 12aaa04 | 2001-01-24 21:23:39 +0000 | [diff] [blame] | 348 | if (authctxt->failures++ > AUTH_FAIL_MAX) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 349 | #ifdef WITH_AIXAUTHENTICATE |
| 350 | loginfailed(authctxt->user, |
| 351 | get_canonical_hostname(options.reverse_mapping_check), |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 352 | "ssh"); |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 353 | #endif /* WITH_AIXAUTHENTICATE */ |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 354 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 355 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 356 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 357 | packet_start(SSH_SMSG_FAILURE); |
| 358 | packet_send(); |
| 359 | packet_write_wait(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Performs authentication of an incoming connection. Session key has already |
| 365 | * been exchanged and encryption is enabled. |
| 366 | */ |
| 367 | void |
| 368 | do_authentication() |
| 369 | { |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 370 | Authctxt *authctxt; |
| 371 | struct passwd *pw; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 372 | int plen; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 373 | u_int ulen; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 374 | char *user, *style = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 375 | |
| 376 | /* Get the name of the user that we wish to log in as. */ |
| 377 | packet_read_expect(&plen, SSH_CMSG_USER); |
| 378 | |
| 379 | /* Get the user name. */ |
| 380 | user = packet_get_string(&ulen); |
| 381 | packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER); |
| 382 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 383 | if ((style = strchr(user, ':')) != NULL) |
| 384 | *style++ = 0; |
| 385 | |
| 386 | authctxt = authctxt_new(); |
| 387 | authctxt->user = user; |
| 388 | authctxt->style = style; |
| 389 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 390 | setproctitle("%s", user); |
| 391 | |
| 392 | #ifdef AFS |
| 393 | /* If machine has AFS, set process authentication group. */ |
| 394 | if (k_hasafs()) { |
| 395 | k_setpag(); |
| 396 | k_unlog(); |
| 397 | } |
| 398 | #endif /* AFS */ |
| 399 | |
| 400 | /* Verify that the user is a valid user. */ |
| 401 | pw = getpwnam(user); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 402 | if (pw && allowed_user(pw)) { |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 403 | authctxt->valid = 1; |
| 404 | pw = pwcopy(pw); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 405 | } else { |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 406 | debug("do_authentication: illegal user %s", user); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 407 | pw = NULL; |
| 408 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 409 | authctxt->pw = pw; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 410 | |
| 411 | #ifdef USE_PAM |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 412 | if (pw) |
Damien Miller | 22e22bf | 2001-01-19 15:46:38 +1100 | [diff] [blame] | 413 | start_pam(user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 414 | #endif |
| 415 | |
| 416 | /* |
| 417 | * If we are not running as root, the user must have the same uid as |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 418 | * the server. (Unless you are running Windows) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 419 | */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 420 | #ifndef HAVE_CYGWIN |
| 421 | if (getuid() != 0 && pw && pw->pw_uid != getuid()) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 422 | packet_disconnect("Cannot change user when server not running as root."); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 423 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 424 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 425 | /* |
Kevin Steves | 12aaa04 | 2001-01-24 21:23:39 +0000 | [diff] [blame] | 426 | * Loop until the user has been authenticated or the connection is |
| 427 | * closed, do_authloop() returns only if authentication is successful |
| 428 | */ |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 429 | do_authloop(authctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 430 | |
| 431 | /* The user has been authenticated and accepted. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 432 | packet_start(SSH_SMSG_SUCCESS); |
| 433 | packet_send(); |
| 434 | packet_write_wait(); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 435 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 436 | #ifdef WITH_AIXAUTHENTICATE |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 437 | /* We don't have a pty yet, so just label the line as "ssh" */ |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 438 | if (loginsuccess(authctxt->user, |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 439 | get_canonical_hostname(options.reverse_mapping_check), |
| 440 | "ssh", &aixloginmsg) < 0) |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 441 | aixloginmsg = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 442 | #endif /* WITH_AIXAUTHENTICATE */ |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 443 | |
| 444 | xfree(authctxt->user); |
| 445 | xfree(authctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 446 | |
| 447 | /* Perform session preparation. */ |
| 448 | do_authenticated(pw); |
| 449 | } |