]>
Commit | Line | Data |
---|---|---|
00146caa | 1 | /* $OpenBSD: auth1.c,v 1.68 2006/07/22 20:48:22 stevesk Exp $ */ |
a306f2dd | 2 | /* |
3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
4 | * All rights reserved | |
bcbf86ec | 5 | * |
6 | * As far as I am concerned, the code I have written for this software | |
7 | * can be used freely for any purpose. Any derived versions of this | |
8 | * software must be clearly marked as such, and if the derived work is | |
9 | * incompatible with the protocol description in the RFC file, it must be | |
10 | * called by a name other than "ssh" or "Secure Shell". | |
a306f2dd | 11 | */ |
12 | ||
13 | #include "includes.h" | |
94ec8c6b | 14 | |
67514848 | 15 | #include <sys/types.h> |
16 | ||
00146caa | 17 | #include <string.h> |
67514848 | 18 | #include <unistd.h> |
19 | ||
a306f2dd | 20 | #include "xmalloc.h" |
21 | #include "rsa.h" | |
42f11eb2 | 22 | #include "ssh1.h" |
a306f2dd | 23 | #include "packet.h" |
24 | #include "buffer.h" | |
42f11eb2 | 25 | #include "log.h" |
a306f2dd | 26 | #include "servconf.h" |
27 | #include "compat.h" | |
28 | #include "auth.h" | |
7899c98f | 29 | #include "channels.h" |
a306f2dd | 30 | #include "session.h" |
ced49be2 | 31 | #include "uidswap.h" |
1853d1ef | 32 | #include "monitor_wrap.h" |
9aab0af7 | 33 | #include "buffer.h" |
a306f2dd | 34 | |
35 | /* import */ | |
36 | extern ServerOptions options; | |
9aab0af7 | 37 | extern Buffer loginmsg; |
94ec8c6b | 38 | |
8485ce56 | 39 | static int auth1_process_password(Authctxt *, char *, size_t); |
40 | static int auth1_process_rsa(Authctxt *, char *, size_t); | |
41 | static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t); | |
42 | static int auth1_process_tis_challenge(Authctxt *, char *, size_t); | |
43 | static int auth1_process_tis_response(Authctxt *, char *, size_t); | |
44 | ||
45 | static char *client_user = NULL; /* Used to fill in remote user for PAM */ | |
46 | ||
47 | struct AuthMethod1 { | |
48 | int type; | |
49 | char *name; | |
50 | int *enabled; | |
51 | int (*method)(Authctxt *, char *, size_t); | |
52 | }; | |
53 | ||
54 | const struct AuthMethod1 auth1_methods[] = { | |
55 | { | |
56 | SSH_CMSG_AUTH_PASSWORD, "password", | |
57 | &options.password_authentication, auth1_process_password | |
58 | }, | |
59 | { | |
60 | SSH_CMSG_AUTH_RSA, "rsa", | |
61 | &options.rsa_authentication, auth1_process_rsa | |
62 | }, | |
63 | { | |
64 | SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa", | |
65 | &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa | |
66 | }, | |
67 | { | |
68 | SSH_CMSG_AUTH_TIS, "challenge-response", | |
69 | &options.challenge_response_authentication, | |
70 | auth1_process_tis_challenge | |
71 | }, | |
72 | { | |
73 | SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response", | |
74 | &options.challenge_response_authentication, | |
75 | auth1_process_tis_response | |
76 | }, | |
77 | { -1, NULL, NULL, NULL} | |
78 | }; | |
79 | ||
80 | static const struct AuthMethod1 | |
81 | *lookup_authmethod1(int type) | |
82 | { | |
83 | int i; | |
84 | ||
c8e9c167 | 85 | for (i = 0; auth1_methods[i].name != NULL; i++) |
8485ce56 | 86 | if (auth1_methods[i].type == type) |
87 | return (&(auth1_methods[i])); | |
88 | ||
89 | return (NULL); | |
90 | } | |
91 | ||
396c147e | 92 | static char * |
a306f2dd | 93 | get_authname(int type) |
94 | { | |
8485ce56 | 95 | const struct AuthMethod1 *a; |
96 | static char buf[64]; | |
97 | ||
98 | if ((a = lookup_authmethod1(type)) != NULL) | |
99 | return (a->name); | |
100 | snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type); | |
101 | return (buf); | |
102 | } | |
103 | ||
fe8c3af1 | 104 | /*ARGSUSED*/ |
8485ce56 | 105 | static int |
106 | auth1_process_password(Authctxt *authctxt, char *info, size_t infolen) | |
107 | { | |
108 | int authenticated = 0; | |
109 | char *password; | |
110 | u_int dlen; | |
111 | ||
112 | /* | |
113 | * Read user password. It is in plain text, but was | |
114 | * transmitted over the encrypted channel so it is | |
115 | * not visible to an outside observer. | |
116 | */ | |
117 | password = packet_get_string(&dlen); | |
118 | packet_check_eom(); | |
119 | ||
120 | /* Try authentication with the password. */ | |
121 | authenticated = PRIVSEP(auth_password(authctxt, password)); | |
122 | ||
123 | memset(password, 0, dlen); | |
124 | xfree(password); | |
125 | ||
126 | return (authenticated); | |
127 | } | |
128 | ||
fe8c3af1 | 129 | /*ARGSUSED*/ |
8485ce56 | 130 | static int |
131 | auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen) | |
132 | { | |
133 | int authenticated = 0; | |
134 | BIGNUM *n; | |
135 | ||
136 | /* RSA authentication requested. */ | |
137 | if ((n = BN_new()) == NULL) | |
138 | fatal("do_authloop: BN_new failed"); | |
139 | packet_get_bignum(n); | |
140 | packet_check_eom(); | |
141 | authenticated = auth_rsa(authctxt, n); | |
142 | BN_clear_free(n); | |
143 | ||
144 | return (authenticated); | |
145 | } | |
146 | ||
fe8c3af1 | 147 | /*ARGSUSED*/ |
8485ce56 | 148 | static int |
149 | auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen) | |
150 | { | |
a375df46 | 151 | int keybits, authenticated = 0; |
8485ce56 | 152 | u_int bits; |
153 | Key *client_host_key; | |
154 | u_int ulen; | |
155 | ||
156 | /* | |
157 | * Get client user name. Note that we just have to | |
158 | * trust the client; root on the client machine can | |
159 | * claim to be any user. | |
160 | */ | |
161 | client_user = packet_get_string(&ulen); | |
162 | ||
163 | /* Get the client host key. */ | |
164 | client_host_key = key_new(KEY_RSA1); | |
165 | bits = packet_get_int(); | |
166 | packet_get_bignum(client_host_key->rsa->e); | |
167 | packet_get_bignum(client_host_key->rsa->n); | |
168 | ||
a375df46 | 169 | keybits = BN_num_bits(client_host_key->rsa->n); |
170 | if (keybits < 0 || bits != (u_int)keybits) { | |
8485ce56 | 171 | verbose("Warning: keysize mismatch for client_host_key: " |
172 | "actual %d, announced %d", | |
173 | BN_num_bits(client_host_key->rsa->n), bits); | |
a306f2dd | 174 | } |
8485ce56 | 175 | packet_check_eom(); |
176 | ||
177 | authenticated = auth_rhosts_rsa(authctxt, client_user, | |
178 | client_host_key); | |
179 | key_free(client_host_key); | |
180 | ||
181 | snprintf(info, infolen, " ruser %.100s", client_user); | |
d1cf9a87 | 182 | |
8485ce56 | 183 | return (authenticated); |
184 | } | |
185 | ||
fe8c3af1 | 186 | /*ARGSUSED*/ |
8485ce56 | 187 | static int |
188 | auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen) | |
189 | { | |
190 | char *challenge; | |
56964485 | 191 | |
8485ce56 | 192 | if ((challenge = get_challenge(authctxt)) == NULL) |
193 | return (0); | |
194 | ||
195 | debug("sending challenge '%s'", challenge); | |
196 | packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); | |
197 | packet_put_cstring(challenge); | |
198 | xfree(challenge); | |
199 | packet_send(); | |
200 | packet_write_wait(); | |
201 | ||
202 | return (-1); | |
203 | } | |
204 | ||
fe8c3af1 | 205 | /*ARGSUSED*/ |
8485ce56 | 206 | static int |
207 | auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen) | |
208 | { | |
209 | int authenticated = 0; | |
210 | char *response; | |
211 | u_int dlen; | |
212 | ||
213 | response = packet_get_string(&dlen); | |
214 | packet_check_eom(); | |
215 | authenticated = verify_response(authctxt, response); | |
216 | memset(response, 'r', dlen); | |
217 | xfree(response); | |
218 | ||
219 | return (authenticated); | |
a306f2dd | 220 | } |
221 | ||
222 | /* | |
59c97189 | 223 | * read packets, try to authenticate the user and |
224 | * return only if authentication is successful | |
a306f2dd | 225 | */ |
396c147e | 226 | static void |
59c97189 | 227 | do_authloop(Authctxt *authctxt) |
a306f2dd | 228 | { |
94ec8c6b | 229 | int authenticated = 0; |
59c97189 | 230 | char info[1024]; |
8485ce56 | 231 | int prev = 0, type = 0; |
232 | const struct AuthMethod1 *meth; | |
59c97189 | 233 | |
234 | debug("Attempting authentication for %s%.100s.", | |
d77347cc | 235 | authctxt->valid ? "" : "invalid user ", authctxt->user); |
59c97189 | 236 | |
237 | /* If the user has no password, accept authentication immediately. */ | |
238 | if (options.password_authentication && | |
1c590258 | 239 | #ifdef KRB5 |
59c97189 | 240 | (!options.kerberos_authentication || options.kerberos_or_local_passwd) && |
241 | #endif | |
1853d1ef | 242 | PRIVSEP(auth_password(authctxt, ""))) { |
ece30983 | 243 | #ifdef USE_PAM |
244 | if (options.use_pam && (PRIVSEP(do_pam_account()))) | |
245 | #endif | |
246 | { | |
247 | auth_log(authctxt, 1, "without authentication", ""); | |
248 | return; | |
249 | } | |
59c97189 | 250 | } |
a306f2dd | 251 | |
252 | /* Indicate that authentication is needed. */ | |
253 | packet_start(SSH_SMSG_FAILURE); | |
254 | packet_send(); | |
255 | packet_write_wait(); | |
256 | ||
59c97189 | 257 | for (;;) { |
94ec8c6b | 258 | /* default to fail */ |
259 | authenticated = 0; | |
260 | ||
59c97189 | 261 | info[0] = '\0'; |
a306f2dd | 262 | |
263 | /* Get a packet from the client. */ | |
05114c74 | 264 | prev = type; |
54a5250f | 265 | type = packet_read(); |
a306f2dd | 266 | |
05114c74 | 267 | /* |
268 | * If we started challenge-response authentication but the | |
269 | * next packet is not a response to our challenge, release | |
270 | * the resources allocated by get_challenge() (which would | |
271 | * normally have been released by verify_response() had we | |
272 | * received such a response) | |
273 | */ | |
274 | if (prev == SSH_CMSG_AUTH_TIS && | |
275 | type != SSH_CMSG_AUTH_TIS_RESPONSE) | |
276 | abandon_challenge_response(authctxt); | |
277 | ||
8485ce56 | 278 | if ((meth = lookup_authmethod1(type)) == NULL) { |
279 | logit("Unknown message during authentication: " | |
280 | "type %d", type); | |
281 | goto skip; | |
282 | } | |
283 | ||
284 | if (!*(meth->enabled)) { | |
285 | verbose("%s authentication disabled.", meth->name); | |
286 | goto skip; | |
a306f2dd | 287 | } |
8485ce56 | 288 | |
289 | authenticated = meth->method(authctxt, info, sizeof(info)); | |
290 | if (authenticated == -1) | |
291 | continue; /* "postponed" */ | |
292 | ||
af774732 | 293 | #ifdef BSD_AUTH |
294 | if (authctxt->as) { | |
295 | auth_close(authctxt->as); | |
296 | authctxt->as = NULL; | |
297 | } | |
298 | #endif | |
59c97189 | 299 | if (!authctxt->valid && authenticated) |
300 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
301 | authctxt->user); | |
a306f2dd | 302 | |
ef51930f | 303 | #ifdef _UNICOS |
ef51930f | 304 | if (authenticated && cray_access_denied(authctxt->user)) { |
305 | authenticated = 0; | |
306 | fatal("Access denied for user %s.",authctxt->user); | |
307 | } | |
308 | #endif /* _UNICOS */ | |
309 | ||
2b87da3b | 310 | #ifdef HAVE_CYGWIN |
311 | if (authenticated && | |
d1cf9a87 | 312 | !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, |
02a57950 | 313 | authctxt->pw)) { |
3c62e7eb | 314 | packet_disconnect("Authentication rejected for uid %d.", |
02a57950 | 315 | authctxt->pw == NULL ? -1 : authctxt->pw->pw_uid); |
3c62e7eb | 316 | authenticated = 0; |
317 | } | |
3c418020 | 318 | #else |
59c97189 | 319 | /* Special handling for root */ |
5d47f1d4 | 320 | if (authenticated && authctxt->pw->pw_uid == 0 && |
8485ce56 | 321 | !auth_root_allowed(meth->name)) { |
322 | authenticated = 0; | |
6039eeef | 323 | # ifdef SSH_AUDIT_EVENTS |
324 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); | |
c00e4d75 | 325 | # endif |
326 | } | |
3c418020 | 327 | #endif |
a306f2dd | 328 | |
5b9e2464 | 329 | #ifdef USE_PAM |
aff51935 | 330 | if (options.use_pam && authenticated && |
9aab0af7 | 331 | !PRIVSEP(do_pam_account())) { |
332 | char *msg; | |
333 | size_t len; | |
334 | ||
335 | error("Access denied for user %s by PAM account " | |
98c044d0 | 336 | "configuration", authctxt->user); |
9aab0af7 | 337 | len = buffer_len(&loginmsg); |
338 | buffer_append(&loginmsg, "\0", 1); | |
339 | msg = buffer_ptr(&loginmsg); | |
340 | /* strip trailing newlines */ | |
341 | if (len > 0) | |
342 | while (len > 0 && msg[--len] == '\n') | |
343 | msg[len] = '\0'; | |
344 | else | |
345 | msg = "Access denied."; | |
346 | packet_disconnect(msg); | |
347 | } | |
5b9e2464 | 348 | #endif |
349 | ||
8485ce56 | 350 | skip: |
59c97189 | 351 | /* Log before sending the reply */ |
352 | auth_log(authctxt, authenticated, get_authname(type), info); | |
353 | ||
a306f2dd | 354 | if (client_user != NULL) { |
355 | xfree(client_user); | |
356 | client_user = NULL; | |
357 | } | |
358 | ||
94ec8c6b | 359 | if (authenticated) |
360 | return; | |
361 | ||
c00e4d75 | 362 | if (authctxt->failures++ > options.max_authtries) { |
6039eeef | 363 | #ifdef SSH_AUDIT_EVENTS |
364 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); | |
c00e4d75 | 365 | #endif |
59c97189 | 366 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
c00e4d75 | 367 | } |
a306f2dd | 368 | |
a306f2dd | 369 | packet_start(SSH_SMSG_FAILURE); |
370 | packet_send(); | |
371 | packet_write_wait(); | |
372 | } | |
373 | } | |
374 | ||
375 | /* | |
376 | * Performs authentication of an incoming connection. Session key has already | |
377 | * been exchanged and encryption is enabled. | |
378 | */ | |
2362db19 | 379 | void |
380 | do_authentication(Authctxt *authctxt) | |
a306f2dd | 381 | { |
1e3b8b07 | 382 | u_int ulen; |
f8d99a32 | 383 | char *user, *style = NULL; |
a306f2dd | 384 | |
385 | /* Get the name of the user that we wish to log in as. */ | |
54a5250f | 386 | packet_read_expect(SSH_CMSG_USER); |
a306f2dd | 387 | |
388 | /* Get the user name. */ | |
389 | user = packet_get_string(&ulen); | |
95500969 | 390 | packet_check_eom(); |
a306f2dd | 391 | |
59c97189 | 392 | if ((style = strchr(user, ':')) != NULL) |
ced49be2 | 393 | *style++ = '\0'; |
59c97189 | 394 | |
59c97189 | 395 | authctxt->user = user; |
396 | authctxt->style = style; | |
397 | ||
a306f2dd | 398 | /* Verify that the user is a valid user. */ |
5f1f36b5 | 399 | if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL) |
59c97189 | 400 | authctxt->valid = 1; |
96d0bf74 | 401 | else { |
d77347cc | 402 | debug("do_authentication: invalid user %s", user); |
96d0bf74 | 403 | authctxt->pw = fakepw(); |
404 | } | |
a306f2dd | 405 | |
bd5c0694 | 406 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
1853d1ef | 407 | use_privsep ? " [net]" : ""); |
6172e290 | 408 | |
a306f2dd | 409 | #ifdef USE_PAM |
7fceb20d | 410 | if (options.use_pam) |
529d73ab | 411 | PRIVSEP(start_pam(authctxt)); |
a306f2dd | 412 | #endif |
413 | ||
414 | /* | |
415 | * If we are not running as root, the user must have the same uid as | |
8485ce56 | 416 | * the server. |
a306f2dd | 417 | */ |
94ec8c6b | 418 | #ifndef HAVE_CYGWIN |
5f1f36b5 | 419 | if (!use_privsep && getuid() != 0 && authctxt->pw && |
420 | authctxt->pw->pw_uid != getuid()) | |
a306f2dd | 421 | packet_disconnect("Cannot change user when server not running as root."); |
3c62e7eb | 422 | #endif |
a306f2dd | 423 | |
59c97189 | 424 | /* |
b4b5d644 | 425 | * Loop until the user has been authenticated or the connection is |
426 | * closed, do_authloop() returns only if authentication is successful | |
427 | */ | |
59c97189 | 428 | do_authloop(authctxt); |
a306f2dd | 429 | |
430 | /* The user has been authenticated and accepted. */ | |
94ec8c6b | 431 | packet_start(SSH_SMSG_SUCCESS); |
432 | packet_send(); | |
433 | packet_write_wait(); | |
a306f2dd | 434 | } |