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