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