]>
Commit | Line | Data |
---|---|---|
a306f2dd | 1 | /* |
2 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
3 | * All rights reserved | |
bcbf86ec | 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". | |
a306f2dd | 10 | */ |
11 | ||
12 | #include "includes.h" | |
7203d6bb | 13 | RCSID("$OpenBSD: auth1.c,v 1.41 2002/06/19 00:27:55 deraadt Exp $"); |
94ec8c6b | 14 | |
a306f2dd | 15 | #include "xmalloc.h" |
16 | #include "rsa.h" | |
42f11eb2 | 17 | #include "ssh1.h" |
a306f2dd | 18 | #include "packet.h" |
19 | #include "buffer.h" | |
a306f2dd | 20 | #include "mpaux.h" |
42f11eb2 | 21 | #include "log.h" |
a306f2dd | 22 | #include "servconf.h" |
23 | #include "compat.h" | |
24 | #include "auth.h" | |
7899c98f | 25 | #include "channels.h" |
a306f2dd | 26 | #include "session.h" |
ced49be2 | 27 | #include "uidswap.h" |
1853d1ef | 28 | #include "monitor_wrap.h" |
a306f2dd | 29 | |
30 | /* import */ | |
31 | extern ServerOptions options; | |
94ec8c6b | 32 | |
a306f2dd | 33 | /* |
34 | * convert ssh auth msg type into description | |
35 | */ | |
396c147e | 36 | static char * |
a306f2dd | 37 | get_authname(int type) |
38 | { | |
39 | static char buf[1024]; | |
40 | switch (type) { | |
41 | case SSH_CMSG_AUTH_PASSWORD: | |
42 | return "password"; | |
43 | case SSH_CMSG_AUTH_RSA: | |
44 | return "rsa"; | |
45 | case SSH_CMSG_AUTH_RHOSTS_RSA: | |
46 | return "rhosts-rsa"; | |
47 | case SSH_CMSG_AUTH_RHOSTS: | |
48 | return "rhosts"; | |
59c97189 | 49 | case SSH_CMSG_AUTH_TIS: |
50 | case SSH_CMSG_AUTH_TIS_RESPONSE: | |
51 | return "challenge-response"; | |
ced49be2 | 52 | #if defined(KRB4) || defined(KRB5) |
a306f2dd | 53 | case SSH_CMSG_AUTH_KERBEROS: |
54 | return "kerberos"; | |
a306f2dd | 55 | #endif |
56 | } | |
57 | snprintf(buf, sizeof buf, "bad-auth-msg-%d", type); | |
58 | return buf; | |
59 | } | |
60 | ||
61 | /* | |
59c97189 | 62 | * read packets, try to authenticate the user and |
63 | * return only if authentication is successful | |
a306f2dd | 64 | */ |
396c147e | 65 | static void |
59c97189 | 66 | do_authloop(Authctxt *authctxt) |
a306f2dd | 67 | { |
94ec8c6b | 68 | int authenticated = 0; |
1e3b8b07 | 69 | u_int bits; |
b775c6f2 | 70 | Key *client_host_key; |
a306f2dd | 71 | BIGNUM *n; |
94ec8c6b | 72 | char *client_user, *password; |
59c97189 | 73 | char info[1024]; |
1e3b8b07 | 74 | u_int dlen; |
1e3b8b07 | 75 | u_int ulen; |
a306f2dd | 76 | int type = 0; |
59c97189 | 77 | struct passwd *pw = authctxt->pw; |
78 | ||
79 | debug("Attempting authentication for %s%.100s.", | |
184eed6a | 80 | authctxt->valid ? "" : "illegal user ", authctxt->user); |
59c97189 | 81 | |
82 | /* If the user has no password, accept authentication immediately. */ | |
83 | if (options.password_authentication && | |
ced49be2 | 84 | #if defined(KRB4) || defined(KRB5) |
59c97189 | 85 | (!options.kerberos_authentication || options.kerberos_or_local_passwd) && |
86 | #endif | |
1853d1ef | 87 | PRIVSEP(auth_password(authctxt, ""))) { |
59c97189 | 88 | auth_log(authctxt, 1, "without authentication", ""); |
89 | return; | |
90 | } | |
a306f2dd | 91 | |
92 | /* Indicate that authentication is needed. */ | |
93 | packet_start(SSH_SMSG_FAILURE); | |
94 | packet_send(); | |
95 | packet_write_wait(); | |
96 | ||
94ec8c6b | 97 | client_user = NULL; |
98 | ||
59c97189 | 99 | for (;;) { |
94ec8c6b | 100 | /* default to fail */ |
101 | authenticated = 0; | |
102 | ||
59c97189 | 103 | info[0] = '\0'; |
a306f2dd | 104 | |
105 | /* Get a packet from the client. */ | |
54a5250f | 106 | type = packet_read(); |
a306f2dd | 107 | |
108 | /* Process the packet. */ | |
109 | switch (type) { | |
a306f2dd | 110 | |
ced49be2 | 111 | #if defined(KRB4) || defined(KRB5) |
a306f2dd | 112 | case SSH_CMSG_AUTH_KERBEROS: |
113 | if (!options.kerberos_authentication) { | |
a306f2dd | 114 | verbose("Kerberos authentication disabled."); |
a306f2dd | 115 | } else { |
ced49be2 | 116 | char *kdata = packet_get_string(&dlen); |
95500969 | 117 | packet_check_eom(); |
184eed6a | 118 | |
ced49be2 | 119 | if (kdata[0] == 4) { /* KRB_PROT_VERSION */ |
120 | #ifdef KRB4 | |
121 | KTEXT_ST tkt; | |
184eed6a | 122 | |
ced49be2 | 123 | tkt.length = dlen; |
124 | if (tkt.length < MAX_KTXT_LEN) | |
125 | memcpy(tkt.dat, kdata, tkt.length); | |
184eed6a | 126 | |
ced49be2 | 127 | if (auth_krb4(authctxt, &tkt, &client_user)) { |
128 | authenticated = 1; | |
129 | snprintf(info, sizeof(info), | |
130 | " tktuser %.100s", | |
131 | client_user); | |
94ec8c6b | 132 | } |
ced49be2 | 133 | #endif /* KRB4 */ |
134 | } else { | |
135 | #ifdef KRB5 | |
136 | krb5_data tkt; | |
137 | tkt.length = dlen; | |
138 | tkt.data = kdata; | |
184eed6a | 139 | |
ced49be2 | 140 | if (auth_krb5(authctxt, &tkt, &client_user)) { |
141 | authenticated = 1; | |
142 | snprintf(info, sizeof(info), | |
143 | " tktuser %.100s", | |
144 | client_user); | |
ced49be2 | 145 | } |
146 | #endif /* KRB5 */ | |
a306f2dd | 147 | } |
59c97189 | 148 | xfree(kdata); |
a306f2dd | 149 | } |
150 | break; | |
ced49be2 | 151 | #endif /* KRB4 || KRB5 */ |
184eed6a | 152 | |
ced49be2 | 153 | #if defined(AFS) || defined(KRB5) |
154 | /* XXX - punt on backward compatibility here. */ | |
155 | case SSH_CMSG_HAVE_KERBEROS_TGT: | |
156 | packet_send_debug("Kerberos TGT passing disabled before authentication."); | |
157 | break; | |
158 | #ifdef AFS | |
159 | case SSH_CMSG_HAVE_AFS_TOKEN: | |
160 | packet_send_debug("AFS token passing disabled before authentication."); | |
161 | break; | |
162 | #endif /* AFS */ | |
163 | #endif /* AFS || KRB5 */ | |
184eed6a | 164 | |
a306f2dd | 165 | case SSH_CMSG_AUTH_RHOSTS: |
166 | if (!options.rhosts_authentication) { | |
167 | verbose("Rhosts authentication disabled."); | |
168 | break; | |
169 | } | |
170 | /* | |
171 | * Get client user name. Note that we just have to | |
172 | * trust the client; this is one reason why rhosts | |
173 | * authentication is insecure. (Another is | |
174 | * IP-spoofing on a local network.) | |
175 | */ | |
176 | client_user = packet_get_string(&ulen); | |
95500969 | 177 | packet_check_eom(); |
a306f2dd | 178 | |
94ec8c6b | 179 | /* Try to authenticate using /etc/hosts.equiv and .rhosts. */ |
a306f2dd | 180 | authenticated = auth_rhosts(pw, client_user); |
181 | ||
59c97189 | 182 | snprintf(info, sizeof info, " ruser %.100s", client_user); |
a306f2dd | 183 | break; |
184 | ||
185 | case SSH_CMSG_AUTH_RHOSTS_RSA: | |
186 | if (!options.rhosts_rsa_authentication) { | |
187 | verbose("Rhosts with RSA authentication disabled."); | |
188 | break; | |
189 | } | |
190 | /* | |
191 | * Get client user name. Note that we just have to | |
192 | * trust the client; root on the client machine can | |
193 | * claim to be any user. | |
194 | */ | |
195 | client_user = packet_get_string(&ulen); | |
196 | ||
197 | /* Get the client host key. */ | |
b775c6f2 | 198 | client_host_key = key_new(KEY_RSA1); |
a306f2dd | 199 | bits = packet_get_int(); |
20b279e6 | 200 | packet_get_bignum(client_host_key->rsa->e); |
201 | packet_get_bignum(client_host_key->rsa->n); | |
a306f2dd | 202 | |
b775c6f2 | 203 | if (bits != BN_num_bits(client_host_key->rsa->n)) |
94ec8c6b | 204 | verbose("Warning: keysize mismatch for client_host_key: " |
b775c6f2 | 205 | "actual %d, announced %d", |
7203d6bb | 206 | BN_num_bits(client_host_key->rsa->n), bits); |
95500969 | 207 | packet_check_eom(); |
a306f2dd | 208 | |
b775c6f2 | 209 | authenticated = auth_rhosts_rsa(pw, client_user, |
dc421aa3 | 210 | client_host_key); |
b775c6f2 | 211 | key_free(client_host_key); |
a306f2dd | 212 | |
59c97189 | 213 | snprintf(info, sizeof info, " ruser %.100s", client_user); |
a306f2dd | 214 | break; |
215 | ||
216 | case SSH_CMSG_AUTH_RSA: | |
217 | if (!options.rsa_authentication) { | |
218 | verbose("RSA authentication disabled."); | |
219 | break; | |
220 | } | |
221 | /* RSA authentication requested. */ | |
b775c6f2 | 222 | if ((n = BN_new()) == NULL) |
223 | fatal("do_authloop: BN_new failed"); | |
20b279e6 | 224 | packet_get_bignum(n); |
95500969 | 225 | packet_check_eom(); |
a306f2dd | 226 | authenticated = auth_rsa(pw, n); |
227 | BN_clear_free(n); | |
228 | break; | |
229 | ||
230 | case SSH_CMSG_AUTH_PASSWORD: | |
231 | if (!options.password_authentication) { | |
232 | verbose("Password authentication disabled."); | |
233 | break; | |
234 | } | |
235 | /* | |
236 | * Read user password. It is in plain text, but was | |
237 | * transmitted over the encrypted channel so it is | |
238 | * not visible to an outside observer. | |
239 | */ | |
240 | password = packet_get_string(&dlen); | |
95500969 | 241 | packet_check_eom(); |
a306f2dd | 242 | |
b4b5d644 | 243 | /* Try authentication with the password. */ |
1853d1ef | 244 | authenticated = PRIVSEP(auth_password(authctxt, password)); |
a306f2dd | 245 | |
246 | memset(password, 0, strlen(password)); | |
247 | xfree(password); | |
248 | break; | |
249 | ||
a306f2dd | 250 | case SSH_CMSG_AUTH_TIS: |
251 | debug("rcvd SSH_CMSG_AUTH_TIS"); | |
5ba55ada | 252 | if (options.challenge_response_authentication == 1) { |
253 | char *challenge = get_challenge(authctxt); | |
59c97189 | 254 | if (challenge != NULL) { |
255 | debug("sending challenge '%s'", challenge); | |
a306f2dd | 256 | packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); |
59c97189 | 257 | packet_put_cstring(challenge); |
5ba55ada | 258 | xfree(challenge); |
a306f2dd | 259 | packet_send(); |
260 | packet_write_wait(); | |
261 | continue; | |
262 | } | |
263 | } | |
264 | break; | |
265 | case SSH_CMSG_AUTH_TIS_RESPONSE: | |
266 | debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE"); | |
5ba55ada | 267 | if (options.challenge_response_authentication == 1) { |
a306f2dd | 268 | char *response = packet_get_string(&dlen); |
59c97189 | 269 | debug("got response '%s'", response); |
95500969 | 270 | packet_check_eom(); |
59c97189 | 271 | authenticated = verify_response(authctxt, response); |
272 | memset(response, 'r', dlen); | |
a306f2dd | 273 | xfree(response); |
274 | } | |
275 | break; | |
a306f2dd | 276 | |
277 | default: | |
278 | /* | |
279 | * Any unknown messages will be ignored (and failure | |
280 | * returned) during authentication. | |
281 | */ | |
282 | log("Unknown message during authentication: type %d", type); | |
283 | break; | |
284 | } | |
af774732 | 285 | #ifdef BSD_AUTH |
286 | if (authctxt->as) { | |
287 | auth_close(authctxt->as); | |
288 | authctxt->as = NULL; | |
289 | } | |
290 | #endif | |
59c97189 | 291 | if (!authctxt->valid && authenticated) |
292 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
293 | authctxt->user); | |
a306f2dd | 294 | |
2b87da3b | 295 | #ifdef HAVE_CYGWIN |
296 | if (authenticated && | |
e9571a2c | 297 | !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) { |
3c62e7eb | 298 | packet_disconnect("Authentication rejected for uid %d.", |
e9571a2c | 299 | pw == NULL ? -1 : pw->pw_uid); |
3c62e7eb | 300 | authenticated = 0; |
301 | } | |
3c418020 | 302 | #else |
59c97189 | 303 | /* Special handling for root */ |
15853e93 | 304 | if (authenticated && authctxt->pw->pw_uid == 0 && |
305 | !auth_root_allowed(get_authname(type))) | |
59c97189 | 306 | authenticated = 0; |
3c418020 | 307 | #endif |
2b87da3b | 308 | #ifdef USE_PAM |
ad200abb | 309 | if (!use_privsep && authenticated && |
310 | !do_pam_account(pw->pw_name, client_user)) | |
94ec8c6b | 311 | authenticated = 0; |
312 | #endif | |
a306f2dd | 313 | |
59c97189 | 314 | /* Log before sending the reply */ |
315 | auth_log(authctxt, authenticated, get_authname(type), info); | |
316 | ||
a306f2dd | 317 | if (client_user != NULL) { |
318 | xfree(client_user); | |
319 | client_user = NULL; | |
320 | } | |
321 | ||
94ec8c6b | 322 | if (authenticated) |
323 | return; | |
324 | ||
b4b5d644 | 325 | if (authctxt->failures++ > AUTH_FAIL_MAX) { |
2b87da3b | 326 | #ifdef WITH_AIXAUTHENTICATE |
327 | loginfailed(authctxt->user, | |
983784a1 | 328 | get_canonical_hostname(options.verify_reverse_mapping), |
61e96248 | 329 | "ssh"); |
c1ef8333 | 330 | #endif /* WITH_AIXAUTHENTICATE */ |
59c97189 | 331 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
c1ef8333 | 332 | } |
a306f2dd | 333 | |
a306f2dd | 334 | packet_start(SSH_SMSG_FAILURE); |
335 | packet_send(); | |
336 | packet_write_wait(); | |
337 | } | |
338 | } | |
339 | ||
340 | /* | |
341 | * Performs authentication of an incoming connection. Session key has already | |
342 | * been exchanged and encryption is enabled. | |
343 | */ | |
1b34c1b3 | 344 | Authctxt * |
d5bb9418 | 345 | do_authentication(void) |
a306f2dd | 346 | { |
59c97189 | 347 | Authctxt *authctxt; |
1e3b8b07 | 348 | u_int ulen; |
f8d99a32 | 349 | char *user, *style = NULL; |
a306f2dd | 350 | |
351 | /* Get the name of the user that we wish to log in as. */ | |
54a5250f | 352 | packet_read_expect(SSH_CMSG_USER); |
a306f2dd | 353 | |
354 | /* Get the user name. */ | |
355 | user = packet_get_string(&ulen); | |
95500969 | 356 | packet_check_eom(); |
a306f2dd | 357 | |
59c97189 | 358 | if ((style = strchr(user, ':')) != NULL) |
ced49be2 | 359 | *style++ = '\0'; |
59c97189 | 360 | |
f8d99a32 | 361 | #ifdef KRB5 |
ced49be2 | 362 | /* XXX - SSH.com Kerberos v5 braindeath. */ |
f8d99a32 | 363 | if ((datafellows & SSH_BUG_K5USER) && |
364 | options.kerberos_authentication) { | |
365 | char *p; | |
366 | if ((p = strchr(user, '@')) != NULL) | |
367 | *p = '\0'; | |
368 | } | |
369 | #endif | |
184eed6a | 370 | |
59c97189 | 371 | authctxt = authctxt_new(); |
372 | authctxt->user = user; | |
373 | authctxt->style = style; | |
374 | ||
a306f2dd | 375 | /* Verify that the user is a valid user. */ |
5f1f36b5 | 376 | if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL) |
59c97189 | 377 | authctxt->valid = 1; |
5f1f36b5 | 378 | else |
59c97189 | 379 | debug("do_authentication: illegal user %s", user); |
a306f2dd | 380 | |
5f1f36b5 | 381 | setproctitle("%s%s", authctxt->pw ? user : "unknown", |
1853d1ef | 382 | use_privsep ? " [net]" : ""); |
6172e290 | 383 | |
a306f2dd | 384 | #ifdef USE_PAM |
ad200abb | 385 | PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user)); |
a306f2dd | 386 | #endif |
387 | ||
388 | /* | |
389 | * If we are not running as root, the user must have the same uid as | |
94ec8c6b | 390 | * the server. (Unless you are running Windows) |
a306f2dd | 391 | */ |
94ec8c6b | 392 | #ifndef HAVE_CYGWIN |
5f1f36b5 | 393 | if (!use_privsep && getuid() != 0 && authctxt->pw && |
394 | authctxt->pw->pw_uid != getuid()) | |
a306f2dd | 395 | packet_disconnect("Cannot change user when server not running as root."); |
3c62e7eb | 396 | #endif |
a306f2dd | 397 | |
59c97189 | 398 | /* |
b4b5d644 | 399 | * Loop until the user has been authenticated or the connection is |
400 | * closed, do_authloop() returns only if authentication is successful | |
401 | */ | |
59c97189 | 402 | do_authloop(authctxt); |
a306f2dd | 403 | |
404 | /* The user has been authenticated and accepted. */ | |
94ec8c6b | 405 | packet_start(SSH_SMSG_SUCCESS); |
406 | packet_send(); | |
407 | packet_write_wait(); | |
59c97189 | 408 | |
1b34c1b3 | 409 | return (authctxt); |
a306f2dd | 410 | } |