]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | #include "includes.h" | |
2980ea68 | 26 | RCSID("$OpenBSD: auth2.c,v 1.91 2002/05/13 02:37:39 itojun Exp $"); |
3c0ef626 | 27 | |
28 | #include <openssl/evp.h> | |
29 | ||
30 | #include "ssh2.h" | |
a2d82e42 | 31 | #include "ssh1.h" |
3c0ef626 | 32 | #include "xmalloc.h" |
33 | #include "rsa.h" | |
34 | #include "sshpty.h" | |
35 | #include "packet.h" | |
36 | #include "buffer.h" | |
37 | #include "log.h" | |
38 | #include "servconf.h" | |
39 | #include "compat.h" | |
40 | #include "channels.h" | |
41 | #include "bufaux.h" | |
42 | #include "auth.h" | |
43 | #include "session.h" | |
44 | #include "dispatch.h" | |
45 | #include "key.h" | |
46 | #include "cipher.h" | |
47 | #include "kex.h" | |
48 | #include "pathnames.h" | |
49 | #include "uidswap.h" | |
50 | #include "auth-options.h" | |
3c0ef626 | 51 | #include "hostfile.h" |
52 | #include "canohost.h" | |
53 | #include "match.h" | |
2980ea68 | 54 | #include "monitor_wrap.h" |
55 | #include "atomicio.h" | |
078f71c2 | 56 | #include "misc.h" |
3c0ef626 | 57 | |
5598e598 | 58 | #ifdef GSSAPI |
59 | #include "ssh-gss.h" | |
2980ea68 | 60 | #ifdef GSI |
61 | #include "globus_gss_assist.h" | |
62 | char* olduser; | |
63 | int changeuser = 0; | |
64 | #endif | |
5598e598 | 65 | #endif |
66 | ||
3c0ef626 | 67 | /* import */ |
68 | extern ServerOptions options; | |
69 | extern u_char *session_id2; | |
70 | extern int session_id2_len; | |
71 | ||
2980ea68 | 72 | Authctxt *x_authctxt = NULL; |
3c0ef626 | 73 | static int one = 1; |
74 | ||
75 | typedef struct Authmethod Authmethod; | |
76 | struct Authmethod { | |
77 | char *name; | |
78 | int (*userauth)(Authctxt *authctxt); | |
79 | int *enabled; | |
80 | }; | |
81 | ||
82 | /* protocol */ | |
83 | ||
1e608e42 | 84 | static void input_service_request(int, u_int32_t, void *); |
85 | static void input_userauth_request(int, u_int32_t, void *); | |
3c0ef626 | 86 | |
87 | /* helper */ | |
88 | static Authmethod *authmethod_lookup(const char *); | |
89 | static char *authmethods_get(void); | |
2980ea68 | 90 | int user_key_allowed(struct passwd *, Key *); |
91 | int hostbased_key_allowed(struct passwd *, const char *, char *, Key *); | |
3c0ef626 | 92 | |
93 | /* auth */ | |
94 | static void userauth_banner(void); | |
95 | static int userauth_none(Authctxt *); | |
96 | static int userauth_passwd(Authctxt *); | |
97 | static int userauth_pubkey(Authctxt *); | |
98 | static int userauth_hostbased(Authctxt *); | |
99 | static int userauth_kbdint(Authctxt *); | |
100 | ||
5598e598 | 101 | #ifdef GSSAPI |
102 | int userauth_external(Authctxt *authctxt); | |
103 | int userauth_gssapi(Authctxt *authctxt); | |
104 | #endif | |
105 | ||
3c0ef626 | 106 | Authmethod authmethods[] = { |
107 | {"none", | |
108 | userauth_none, | |
109 | &one}, | |
5598e598 | 110 | #ifdef GSSAPI |
111 | {"external-keyx", | |
112 | userauth_external, | |
113 | &options.gss_authentication}, | |
114 | {"gssapi", | |
115 | userauth_gssapi, | |
116 | &options.gss_authentication}, | |
117 | #endif | |
3c0ef626 | 118 | {"publickey", |
119 | userauth_pubkey, | |
120 | &options.pubkey_authentication}, | |
121 | {"password", | |
122 | userauth_passwd, | |
123 | &options.password_authentication}, | |
124 | {"keyboard-interactive", | |
125 | userauth_kbdint, | |
126 | &options.kbd_interactive_authentication}, | |
127 | {"hostbased", | |
128 | userauth_hostbased, | |
129 | &options.hostbased_authentication}, | |
130 | {NULL, NULL, NULL} | |
131 | }; | |
132 | ||
133 | /* | |
134 | * loop until authctxt->success == TRUE | |
135 | */ | |
136 | ||
2980ea68 | 137 | Authctxt * |
1e608e42 | 138 | do_authentication2(void) |
3c0ef626 | 139 | { |
140 | Authctxt *authctxt = authctxt_new(); | |
141 | ||
142 | x_authctxt = authctxt; /*XXX*/ | |
143 | ||
144 | /* challenge-response is implemented via keyboard interactive */ | |
145 | if (options.challenge_response_authentication) | |
146 | options.kbd_interactive_authentication = 1; | |
147 | if (options.pam_authentication_via_kbd_int) | |
148 | options.kbd_interactive_authentication = 1; | |
2980ea68 | 149 | if (use_privsep) |
150 | options.pam_authentication_via_kbd_int = 0; | |
3c0ef626 | 151 | |
1e608e42 | 152 | dispatch_init(&dispatch_protocol_error); |
3c0ef626 | 153 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
154 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); | |
2980ea68 | 155 | |
156 | return (authctxt); | |
3c0ef626 | 157 | } |
158 | ||
159 | static void | |
1e608e42 | 160 | input_service_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 161 | { |
162 | Authctxt *authctxt = ctxt; | |
163 | u_int len; | |
164 | int accept = 0; | |
165 | char *service = packet_get_string(&len); | |
1e608e42 | 166 | packet_check_eom(); |
3c0ef626 | 167 | |
168 | if (authctxt == NULL) | |
169 | fatal("input_service_request: no authctxt"); | |
170 | ||
171 | if (strcmp(service, "ssh-userauth") == 0) { | |
172 | if (!authctxt->success) { | |
173 | accept = 1; | |
174 | /* now we can handle user-auth requests */ | |
175 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
176 | } | |
177 | } | |
178 | /* XXX all other service requests are denied */ | |
179 | ||
180 | if (accept) { | |
181 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
182 | packet_put_cstring(service); | |
183 | packet_send(); | |
184 | packet_write_wait(); | |
185 | } else { | |
186 | debug("bad service request %s", service); | |
187 | packet_disconnect("bad service request %s", service); | |
188 | } | |
189 | xfree(service); | |
190 | } | |
191 | ||
192 | static void | |
1e608e42 | 193 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 194 | { |
195 | Authctxt *authctxt = ctxt; | |
196 | Authmethod *m = NULL; | |
197 | char *user, *service, *method, *style = NULL; | |
198 | int authenticated = 0; | |
199 | ||
200 | if (authctxt == NULL) | |
201 | fatal("input_userauth_request: no authctxt"); | |
202 | ||
203 | user = packet_get_string(NULL); | |
204 | service = packet_get_string(NULL); | |
205 | method = packet_get_string(NULL); | |
2980ea68 | 206 | |
207 | #ifdef GSSAPI | |
208 | #ifdef GSI | |
209 | if(changeuser == 0 && (strcmp(method,"external-keyx") == 0 || strcmp(method,"gssapi") ==0) && strcmp(user,"") == 0) { | |
210 | char *gridmapped_name = NULL; | |
211 | struct passwd *pw = NULL; | |
212 | if(globus_gss_assist_gridmap(gssapi_client_name.value, | |
213 | &gridmapped_name) == 0) { | |
214 | user = gridmapped_name; | |
215 | debug("I gridmapped and got %s", user); | |
216 | pw = getpwnam(user); | |
217 | if (pw && allowed_user(pw)) { | |
218 | olduser = authctxt->user; | |
6ff6813c | 219 | authctxt->user = xstrdup(user); |
2980ea68 | 220 | authctxt->pw = pwcopy(pw); |
221 | authctxt->valid = 1; | |
222 | changeuser = 1; | |
223 | } | |
224 | ||
225 | } else { | |
3f6bb687 | 226 | debug("I gridmapped and got null, reverting to %s", |
227 | authctxt->user); | |
228 | xfree(user); | |
229 | user = xstrdup(authctxt->user); | |
2980ea68 | 230 | } |
231 | } | |
232 | else if(changeuser) { | |
233 | struct passwd *pw = NULL; | |
234 | pw = getpwnam(user); | |
235 | if (pw && allowed_user(pw)) { | |
6ff6813c | 236 | xfree(authctxt->user); |
2980ea68 | 237 | authctxt->user = olduser; |
238 | authctxt->pw = pwcopy(pw); | |
239 | authctxt->valid = 1; | |
240 | changeuser = 0; | |
241 | } | |
242 | } | |
243 | ||
244 | #endif /* GSI */ | |
245 | #endif /* GSSAPI */ | |
246 | ||
3c0ef626 | 247 | debug("userauth-request for user %s service %s method %s", user, service, method); |
248 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); | |
249 | ||
250 | if ((style = strchr(user, ':')) != NULL) | |
251 | *style++ = 0; | |
252 | ||
253 | if (authctxt->attempt++ == 0) { | |
254 | /* setup auth context */ | |
2980ea68 | 255 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
256 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { | |
3c0ef626 | 257 | authctxt->valid = 1; |
258 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
259 | #ifdef USE_PAM | |
2980ea68 | 260 | PRIVSEP(start_pam(authctxt->pw->pw_name)); |
3c0ef626 | 261 | #endif |
262 | } else { | |
263 | log("input_userauth_request: illegal user %s", user); | |
264 | #ifdef USE_PAM | |
2980ea68 | 265 | PRIVSEP(start_pam("NOUSER")); |
3c0ef626 | 266 | #endif |
267 | } | |
2980ea68 | 268 | setproctitle("%s%s", authctxt->pw ? user : "unknown", |
269 | use_privsep ? " [net]" : ""); | |
3c0ef626 | 270 | authctxt->user = xstrdup(user); |
271 | authctxt->service = xstrdup(service); | |
272 | authctxt->style = style ? xstrdup(style) : NULL; | |
2980ea68 | 273 | if (use_privsep) |
274 | mm_inform_authserv(service, style); | |
3c0ef626 | 275 | } else if (strcmp(user, authctxt->user) != 0 || |
276 | strcmp(service, authctxt->service) != 0) { | |
277 | packet_disconnect("Change of username or service not allowed: " | |
278 | "(%s,%s) -> (%s,%s)", | |
279 | authctxt->user, authctxt->service, user, service); | |
280 | } | |
281 | /* reset state */ | |
1e608e42 | 282 | auth2_challenge_stop(authctxt); |
5598e598 | 283 | |
284 | #ifdef GSSAPI | |
1e608e42 | 285 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
286 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
5598e598 | 287 | #endif |
288 | ||
3c0ef626 | 289 | authctxt->postponed = 0; |
3c0ef626 | 290 | |
291 | /* try to authenticate user */ | |
292 | m = authmethod_lookup(method); | |
293 | if (m != NULL) { | |
294 | debug2("input_userauth_request: try method %s", method); | |
295 | authenticated = m->userauth(authctxt); | |
296 | } | |
297 | userauth_finish(authctxt, authenticated, method); | |
298 | ||
299 | xfree(service); | |
300 | xfree(user); | |
301 | xfree(method); | |
302 | } | |
303 | ||
304 | void | |
305 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
306 | { | |
307 | char *methods; | |
308 | ||
309 | if (!authctxt->valid && authenticated) | |
310 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
311 | authctxt->user); | |
312 | ||
313 | /* Special handling for root */ | |
314 | if (authenticated && authctxt->pw->pw_uid == 0 && | |
315 | !auth_root_allowed(method)) | |
316 | authenticated = 0; | |
317 | ||
318 | #ifdef USE_PAM | |
2980ea68 | 319 | if (!use_privsep && authenticated && authctxt->user && |
320 | !do_pam_account(authctxt->user, NULL)) | |
3c0ef626 | 321 | authenticated = 0; |
322 | #endif /* USE_PAM */ | |
323 | ||
324 | /* Log before sending the reply */ | |
a2d82e42 | 325 | if (!compat20) |
326 | auth_log(authctxt, authenticated, method, " ssh1"); | |
327 | else | |
3c0ef626 | 328 | auth_log(authctxt, authenticated, method, " ssh2"); |
329 | ||
330 | if (authctxt->postponed) | |
331 | return; | |
332 | ||
333 | /* XXX todo: check if multiple auth methods are needed */ | |
334 | if (authenticated == 1) { | |
335 | /* turn off userauth */ | |
1e608e42 | 336 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
a2d82e42 | 337 | if (!compat20) |
338 | packet_start(SSH_SMSG_SUCCESS); | |
339 | else | |
3c0ef626 | 340 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
341 | packet_send(); | |
342 | packet_write_wait(); | |
343 | /* now we can break out */ | |
344 | authctxt->success = 1; | |
345 | } else { | |
346 | if (authctxt->failures++ > AUTH_FAIL_MAX) { | |
347 | #ifdef WITH_AIXAUTHENTICATE | |
348 | loginfailed(authctxt->user, | |
1e608e42 | 349 | get_canonical_hostname(options.verify_reverse_mapping), |
3c0ef626 | 350 | "ssh"); |
351 | #endif /* WITH_AIXAUTHENTICATE */ | |
352 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); | |
353 | } | |
2980ea68 | 354 | if (!compat20) { |
355 | /* | |
356 | * Break out of the dispatch loop now and go back to | |
357 | * SSH1 code. We need to set the 'success' flag to | |
358 | * break out of the loop. Set the 'postponed' flag to | |
359 | * tell the SSH1 code that authentication failed. The | |
360 | * SSH1 code will handle sending SSH_SMSG_FAILURE. | |
361 | */ | |
362 | authctxt->success = authctxt->postponed = 1; | |
363 | } else { | |
3c0ef626 | 364 | methods = authmethods_get(); |
a2d82e42 | 365 | if (!compat20) |
366 | packet_disconnect("GSSAPI authentication failed"); | |
367 | else | |
3c0ef626 | 368 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
369 | packet_put_cstring(methods); | |
370 | packet_put_char(0); /* XXX partial success, unused */ | |
371 | packet_send(); | |
372 | packet_write_wait(); | |
373 | xfree(methods); | |
2980ea68 | 374 | } |
3c0ef626 | 375 | } |
376 | } | |
377 | ||
2980ea68 | 378 | char * |
379 | auth2_read_banner(void) | |
3c0ef626 | 380 | { |
381 | struct stat st; | |
382 | char *banner = NULL; | |
383 | off_t len, n; | |
384 | int fd; | |
385 | ||
2980ea68 | 386 | if ((fd = open(options.banner, O_RDONLY)) == -1) |
387 | return (NULL); | |
388 | if (fstat(fd, &st) == -1) { | |
389 | close(fd); | |
390 | return (NULL); | |
391 | } | |
3c0ef626 | 392 | len = st.st_size; |
393 | banner = xmalloc(len + 1); | |
2980ea68 | 394 | n = atomicio(read, fd, banner, len); |
395 | close(fd); | |
396 | ||
397 | if (n != len) { | |
398 | free(banner); | |
399 | return (NULL); | |
400 | } | |
3c0ef626 | 401 | banner[n] = '\0'; |
2980ea68 | 402 | |
403 | return (banner); | |
404 | } | |
405 | ||
406 | static void | |
407 | userauth_banner(void) | |
408 | { | |
409 | char *banner = NULL; | |
410 | ||
411 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) | |
412 | return; | |
413 | ||
414 | if ((banner = PRIVSEP(auth2_read_banner())) == NULL) | |
415 | goto done; | |
416 | ||
3c0ef626 | 417 | packet_start(SSH2_MSG_USERAUTH_BANNER); |
418 | packet_put_cstring(banner); | |
419 | packet_put_cstring(""); /* language, unused */ | |
420 | packet_send(); | |
421 | debug("userauth_banner: sent"); | |
422 | done: | |
423 | if (banner) | |
424 | xfree(banner); | |
3c0ef626 | 425 | return; |
426 | } | |
427 | ||
428 | static int | |
429 | userauth_none(Authctxt *authctxt) | |
430 | { | |
431 | /* disable method "none", only allowed one time */ | |
432 | Authmethod *m = authmethod_lookup("none"); | |
433 | if (m != NULL) | |
434 | m->enabled = NULL; | |
1e608e42 | 435 | packet_check_eom(); |
3c0ef626 | 436 | userauth_banner(); |
437 | ||
438 | if (authctxt->valid == 0) | |
439 | return(0); | |
440 | ||
441 | #ifdef HAVE_CYGWIN | |
1e608e42 | 442 | if (check_nt_auth(1, authctxt->pw) == 0) |
3c0ef626 | 443 | return(0); |
444 | #endif | |
2980ea68 | 445 | return PRIVSEP(auth_password(authctxt, "")); |
3c0ef626 | 446 | } |
447 | ||
448 | static int | |
449 | userauth_passwd(Authctxt *authctxt) | |
450 | { | |
451 | char *password; | |
452 | int authenticated = 0; | |
453 | int change; | |
454 | u_int len; | |
455 | change = packet_get_char(); | |
456 | if (change) | |
457 | log("password change not supported"); | |
458 | password = packet_get_string(&len); | |
1e608e42 | 459 | packet_check_eom(); |
3c0ef626 | 460 | if (authctxt->valid && |
461 | #ifdef HAVE_CYGWIN | |
1e608e42 | 462 | check_nt_auth(1, authctxt->pw) && |
3c0ef626 | 463 | #endif |
2980ea68 | 464 | PRIVSEP(auth_password(authctxt, password)) == 1) |
3c0ef626 | 465 | authenticated = 1; |
466 | memset(password, 0, len); | |
467 | xfree(password); | |
468 | return authenticated; | |
469 | } | |
470 | ||
471 | static int | |
472 | userauth_kbdint(Authctxt *authctxt) | |
473 | { | |
474 | int authenticated = 0; | |
475 | char *lang, *devs; | |
476 | ||
477 | lang = packet_get_string(NULL); | |
478 | devs = packet_get_string(NULL); | |
1e608e42 | 479 | packet_check_eom(); |
3c0ef626 | 480 | |
481 | debug("keyboard-interactive devs %s", devs); | |
482 | ||
483 | if (options.challenge_response_authentication) | |
484 | authenticated = auth2_challenge(authctxt, devs); | |
485 | ||
486 | #ifdef USE_PAM | |
487 | if (authenticated == 0 && options.pam_authentication_via_kbd_int) | |
488 | authenticated = auth2_pam(authctxt); | |
489 | #endif | |
490 | xfree(devs); | |
491 | xfree(lang); | |
492 | #ifdef HAVE_CYGWIN | |
1e608e42 | 493 | if (check_nt_auth(0, authctxt->pw) == 0) |
3c0ef626 | 494 | return(0); |
495 | #endif | |
496 | return authenticated; | |
497 | } | |
498 | ||
499 | static int | |
500 | userauth_pubkey(Authctxt *authctxt) | |
501 | { | |
502 | Buffer b; | |
1e608e42 | 503 | Key *key = NULL; |
504 | char *pkalg; | |
505 | u_char *pkblob, *sig; | |
3c0ef626 | 506 | u_int alen, blen, slen; |
507 | int have_sig, pktype; | |
508 | int authenticated = 0; | |
509 | ||
510 | if (!authctxt->valid) { | |
511 | debug2("userauth_pubkey: disabled because of invalid user"); | |
512 | return 0; | |
513 | } | |
514 | have_sig = packet_get_char(); | |
515 | if (datafellows & SSH_BUG_PKAUTH) { | |
516 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); | |
517 | /* no explicit pkalg given */ | |
518 | pkblob = packet_get_string(&blen); | |
519 | buffer_init(&b); | |
520 | buffer_append(&b, pkblob, blen); | |
521 | /* so we have to extract the pkalg from the pkblob */ | |
522 | pkalg = buffer_get_string(&b, &alen); | |
523 | buffer_free(&b); | |
524 | } else { | |
525 | pkalg = packet_get_string(&alen); | |
526 | pkblob = packet_get_string(&blen); | |
527 | } | |
528 | pktype = key_type_from_name(pkalg); | |
529 | if (pktype == KEY_UNSPEC) { | |
530 | /* this is perfectly legal */ | |
1e608e42 | 531 | log("userauth_pubkey: unsupported public key algorithm: %s", |
532 | pkalg); | |
533 | goto done; | |
3c0ef626 | 534 | } |
535 | key = key_from_blob(pkblob, blen); | |
1e608e42 | 536 | if (key == NULL) { |
537 | error("userauth_pubkey: cannot decode key: %s", pkalg); | |
538 | goto done; | |
539 | } | |
540 | if (key->type != pktype) { | |
541 | error("userauth_pubkey: type mismatch for decoded key " | |
542 | "(received %d, expected %d)", key->type, pktype); | |
543 | goto done; | |
544 | } | |
545 | if (have_sig) { | |
546 | sig = packet_get_string(&slen); | |
547 | packet_check_eom(); | |
548 | buffer_init(&b); | |
549 | if (datafellows & SSH_OLD_SESSIONID) { | |
550 | buffer_append(&b, session_id2, session_id2_len); | |
551 | } else { | |
552 | buffer_put_string(&b, session_id2, session_id2_len); | |
553 | } | |
554 | /* reconstruct packet */ | |
555 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
556 | buffer_put_cstring(&b, authctxt->user); | |
557 | buffer_put_cstring(&b, | |
558 | datafellows & SSH_BUG_PKSERVICE ? | |
559 | "ssh-userauth" : | |
560 | authctxt->service); | |
561 | if (datafellows & SSH_BUG_PKAUTH) { | |
562 | buffer_put_char(&b, have_sig); | |
563 | } else { | |
564 | buffer_put_cstring(&b, "publickey"); | |
565 | buffer_put_char(&b, have_sig); | |
566 | buffer_put_cstring(&b, pkalg); | |
567 | } | |
568 | buffer_put_string(&b, pkblob, blen); | |
3c0ef626 | 569 | #ifdef DEBUG_PK |
1e608e42 | 570 | buffer_dump(&b); |
3c0ef626 | 571 | #endif |
1e608e42 | 572 | /* test for correct signature */ |
2980ea68 | 573 | authenticated = 0; |
574 | if (PRIVSEP(user_key_allowed(authctxt->pw, key)) && | |
575 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), | |
576 | buffer_len(&b))) == 1) | |
1e608e42 | 577 | authenticated = 1; |
578 | buffer_clear(&b); | |
579 | xfree(sig); | |
580 | } else { | |
581 | debug("test whether pkalg/pkblob are acceptable"); | |
582 | packet_check_eom(); | |
583 | ||
584 | /* XXX fake reply and always send PK_OK ? */ | |
585 | /* | |
586 | * XXX this allows testing whether a user is allowed | |
587 | * to login: if you happen to have a valid pubkey this | |
588 | * message is sent. the message is NEVER sent at all | |
589 | * if a user is not allowed to login. is this an | |
590 | * issue? -markus | |
591 | */ | |
2980ea68 | 592 | if (PRIVSEP(user_key_allowed(authctxt->pw, key))) { |
1e608e42 | 593 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
594 | packet_put_string(pkalg, alen); | |
595 | packet_put_string(pkblob, blen); | |
596 | packet_send(); | |
597 | packet_write_wait(); | |
598 | authctxt->postponed = 1; | |
3c0ef626 | 599 | } |
3c0ef626 | 600 | } |
1e608e42 | 601 | if (authenticated != 1) |
602 | auth_clear_options(); | |
603 | done: | |
3c0ef626 | 604 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
1e608e42 | 605 | if (key != NULL) |
606 | key_free(key); | |
3c0ef626 | 607 | xfree(pkalg); |
608 | xfree(pkblob); | |
609 | #ifdef HAVE_CYGWIN | |
1e608e42 | 610 | if (check_nt_auth(0, authctxt->pw) == 0) |
3c0ef626 | 611 | return(0); |
612 | #endif | |
613 | return authenticated; | |
614 | } | |
615 | ||
616 | static int | |
617 | userauth_hostbased(Authctxt *authctxt) | |
618 | { | |
619 | Buffer b; | |
1e608e42 | 620 | Key *key = NULL; |
621 | char *pkalg, *cuser, *chost, *service; | |
622 | u_char *pkblob, *sig; | |
3c0ef626 | 623 | u_int alen, blen, slen; |
624 | int pktype; | |
625 | int authenticated = 0; | |
626 | ||
627 | if (!authctxt->valid) { | |
628 | debug2("userauth_hostbased: disabled because of invalid user"); | |
629 | return 0; | |
630 | } | |
631 | pkalg = packet_get_string(&alen); | |
632 | pkblob = packet_get_string(&blen); | |
633 | chost = packet_get_string(NULL); | |
634 | cuser = packet_get_string(NULL); | |
635 | sig = packet_get_string(&slen); | |
636 | ||
637 | debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d", | |
638 | cuser, chost, pkalg, slen); | |
639 | #ifdef DEBUG_PK | |
640 | debug("signature:"); | |
641 | buffer_init(&b); | |
642 | buffer_append(&b, sig, slen); | |
643 | buffer_dump(&b); | |
644 | buffer_free(&b); | |
645 | #endif | |
646 | pktype = key_type_from_name(pkalg); | |
647 | if (pktype == KEY_UNSPEC) { | |
648 | /* this is perfectly legal */ | |
649 | log("userauth_hostbased: unsupported " | |
650 | "public key algorithm: %s", pkalg); | |
651 | goto done; | |
652 | } | |
653 | key = key_from_blob(pkblob, blen); | |
654 | if (key == NULL) { | |
1e608e42 | 655 | error("userauth_hostbased: cannot decode key: %s", pkalg); |
656 | goto done; | |
657 | } | |
658 | if (key->type != pktype) { | |
659 | error("userauth_hostbased: type mismatch for decoded key " | |
660 | "(received %d, expected %d)", key->type, pktype); | |
3c0ef626 | 661 | goto done; |
662 | } | |
663 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : | |
664 | authctxt->service; | |
665 | buffer_init(&b); | |
666 | buffer_put_string(&b, session_id2, session_id2_len); | |
667 | /* reconstruct packet */ | |
668 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
669 | buffer_put_cstring(&b, authctxt->user); | |
670 | buffer_put_cstring(&b, service); | |
671 | buffer_put_cstring(&b, "hostbased"); | |
672 | buffer_put_string(&b, pkalg, alen); | |
673 | buffer_put_string(&b, pkblob, blen); | |
674 | buffer_put_cstring(&b, chost); | |
675 | buffer_put_cstring(&b, cuser); | |
676 | #ifdef DEBUG_PK | |
677 | buffer_dump(&b); | |
678 | #endif | |
679 | /* test for allowed key and correct signature */ | |
2980ea68 | 680 | authenticated = 0; |
681 | if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) && | |
682 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), | |
683 | buffer_len(&b))) == 1) | |
3c0ef626 | 684 | authenticated = 1; |
685 | ||
686 | buffer_clear(&b); | |
3c0ef626 | 687 | done: |
688 | debug2("userauth_hostbased: authenticated %d", authenticated); | |
1e608e42 | 689 | if (key != NULL) |
690 | key_free(key); | |
3c0ef626 | 691 | xfree(pkalg); |
692 | xfree(pkblob); | |
693 | xfree(cuser); | |
694 | xfree(chost); | |
695 | xfree(sig); | |
696 | return authenticated; | |
697 | } | |
698 | ||
699 | /* get current user */ | |
700 | ||
701 | struct passwd* | |
702 | auth_get_user(void) | |
703 | { | |
704 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; | |
705 | } | |
706 | ||
707 | #define DELIM "," | |
708 | ||
709 | static char * | |
710 | authmethods_get(void) | |
711 | { | |
712 | Authmethod *method = NULL; | |
1e608e42 | 713 | Buffer b; |
3c0ef626 | 714 | char *list; |
715 | ||
1e608e42 | 716 | buffer_init(&b); |
3c0ef626 | 717 | for (method = authmethods; method->name != NULL; method++) { |
718 | if (strcmp(method->name, "none") == 0) | |
719 | continue; | |
720 | if (method->enabled != NULL && *(method->enabled) != 0) { | |
1e608e42 | 721 | if (buffer_len(&b) > 0) |
722 | buffer_append(&b, ",", 1); | |
723 | buffer_append(&b, method->name, strlen(method->name)); | |
3c0ef626 | 724 | } |
725 | } | |
1e608e42 | 726 | buffer_append(&b, "\0", 1); |
727 | list = xstrdup(buffer_ptr(&b)); | |
728 | buffer_free(&b); | |
3c0ef626 | 729 | return list; |
730 | } | |
731 | ||
732 | static Authmethod * | |
733 | authmethod_lookup(const char *name) | |
734 | { | |
735 | Authmethod *method = NULL; | |
736 | if (name != NULL) | |
737 | for (method = authmethods; method->name != NULL; method++) | |
738 | if (method->enabled != NULL && | |
739 | *(method->enabled) != 0 && | |
740 | strcmp(name, method->name) == 0) | |
741 | return method; | |
742 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
743 | return NULL; | |
744 | } | |
745 | ||
746 | /* return 1 if user allows given key */ | |
747 | static int | |
748 | user_key_allowed2(struct passwd *pw, Key *key, char *file) | |
749 | { | |
750 | char line[8192]; | |
751 | int found_key = 0; | |
752 | FILE *f; | |
753 | u_long linenum = 0; | |
754 | struct stat st; | |
755 | Key *found; | |
1e608e42 | 756 | char *fp; |
3c0ef626 | 757 | |
758 | if (pw == NULL) | |
759 | return 0; | |
760 | ||
761 | /* Temporarily use the user's uid. */ | |
762 | temporarily_use_uid(pw); | |
763 | ||
764 | debug("trying public key file %s", file); | |
765 | ||
766 | /* Fail quietly if file does not exist */ | |
767 | if (stat(file, &st) < 0) { | |
768 | /* Restore the privileged uid. */ | |
769 | restore_uid(); | |
770 | return 0; | |
771 | } | |
772 | /* Open the file containing the authorized keys. */ | |
773 | f = fopen(file, "r"); | |
774 | if (!f) { | |
775 | /* Restore the privileged uid. */ | |
776 | restore_uid(); | |
777 | return 0; | |
778 | } | |
779 | if (options.strict_modes && | |
780 | secure_filename(f, file, pw, line, sizeof(line)) != 0) { | |
781 | fclose(f); | |
782 | log("Authentication refused: %s", line); | |
783 | restore_uid(); | |
784 | return 0; | |
785 | } | |
786 | ||
787 | found_key = 0; | |
788 | found = key_new(key->type); | |
789 | ||
790 | while (fgets(line, sizeof(line), f)) { | |
791 | char *cp, *options = NULL; | |
792 | linenum++; | |
793 | /* Skip leading whitespace, empty and comment lines. */ | |
794 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
795 | ; | |
796 | if (!*cp || *cp == '\n' || *cp == '#') | |
797 | continue; | |
798 | ||
799 | if (key_read(found, &cp) != 1) { | |
800 | /* no key? check if there are options for this key */ | |
801 | int quoted = 0; | |
802 | debug2("user_key_allowed: check options: '%s'", cp); | |
803 | options = cp; | |
804 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { | |
805 | if (*cp == '\\' && cp[1] == '"') | |
806 | cp++; /* Skip both */ | |
807 | else if (*cp == '"') | |
808 | quoted = !quoted; | |
809 | } | |
810 | /* Skip remaining whitespace. */ | |
811 | for (; *cp == ' ' || *cp == '\t'; cp++) | |
812 | ; | |
813 | if (key_read(found, &cp) != 1) { | |
814 | debug2("user_key_allowed: advance: '%s'", cp); | |
815 | /* still no key? advance to next line*/ | |
816 | continue; | |
817 | } | |
818 | } | |
819 | if (key_equal(found, key) && | |
820 | auth_parse_options(pw, options, file, linenum) == 1) { | |
821 | found_key = 1; | |
822 | debug("matching key found: file %s, line %lu", | |
823 | file, linenum); | |
1e608e42 | 824 | fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); |
825 | verbose("Found matching %s key: %s", | |
826 | key_type(found), fp); | |
827 | xfree(fp); | |
3c0ef626 | 828 | break; |
829 | } | |
830 | } | |
831 | restore_uid(); | |
832 | fclose(f); | |
833 | key_free(found); | |
834 | if (!found_key) | |
835 | debug2("key not found"); | |
836 | return found_key; | |
837 | } | |
838 | ||
839 | /* check whether given key is in .ssh/authorized_keys* */ | |
2980ea68 | 840 | int |
3c0ef626 | 841 | user_key_allowed(struct passwd *pw, Key *key) |
842 | { | |
843 | int success; | |
844 | char *file; | |
845 | ||
846 | file = authorized_keys_file(pw); | |
847 | success = user_key_allowed2(pw, key, file); | |
848 | xfree(file); | |
849 | if (success) | |
850 | return success; | |
851 | ||
852 | /* try suffix "2" for backward compat, too */ | |
853 | file = authorized_keys_file2(pw); | |
854 | success = user_key_allowed2(pw, key, file); | |
855 | xfree(file); | |
856 | return success; | |
857 | } | |
858 | ||
859 | /* return 1 if given hostkey is allowed */ | |
2980ea68 | 860 | int |
3c0ef626 | 861 | hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost, |
862 | Key *key) | |
863 | { | |
864 | const char *resolvedname, *ipaddr, *lookup; | |
1e608e42 | 865 | HostStatus host_status; |
866 | int len; | |
3c0ef626 | 867 | |
1e608e42 | 868 | resolvedname = get_canonical_hostname(options.verify_reverse_mapping); |
3c0ef626 | 869 | ipaddr = get_remote_ipaddr(); |
870 | ||
871 | debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s", | |
872 | chost, resolvedname, ipaddr); | |
873 | ||
874 | if (options.hostbased_uses_name_from_packet_only) { | |
875 | if (auth_rhosts2(pw, cuser, chost, chost) == 0) | |
876 | return 0; | |
877 | lookup = chost; | |
878 | } else { | |
879 | if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') { | |
880 | debug2("stripping trailing dot from chost %s", chost); | |
881 | chost[len - 1] = '\0'; | |
882 | } | |
883 | if (strcasecmp(resolvedname, chost) != 0) | |
884 | log("userauth_hostbased mismatch: " | |
885 | "client sends %s, but we resolve %s to %s", | |
886 | chost, ipaddr, resolvedname); | |
887 | if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) | |
888 | return 0; | |
889 | lookup = resolvedname; | |
890 | } | |
891 | debug2("userauth_hostbased: access allowed by auth_rhosts2"); | |
892 | ||
893 | host_status = check_key_in_hostfiles(pw, key, lookup, | |
894 | _PATH_SSH_SYSTEM_HOSTFILE, | |
895 | options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE); | |
896 | ||
897 | /* backward compat if no key has been found. */ | |
898 | if (host_status == HOST_NEW) | |
899 | host_status = check_key_in_hostfiles(pw, key, lookup, | |
900 | _PATH_SSH_SYSTEM_HOSTFILE2, | |
901 | options.ignore_user_known_hosts ? NULL : | |
902 | _PATH_SSH_USER_HOSTFILE2); | |
903 | ||
904 | return (host_status == HOST_OK); | |
905 | } |