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