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