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