]> andersk Git - gssapi-openssh.git/blame - openssh/auth2.c
Initial revision
[gssapi-openssh.git] / openssh / auth2.c
CommitLineData
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"
510132b6 26RCSID("$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"
510132b6 54#include "monitor_wrap.h"
55#include "atomicio.h"
cf4d5580 56#include "misc.h"
3c0ef626 57
5598e598 58#ifdef GSSAPI
59#include "ssh-gss.h"
7d1d5d37 60#ifdef GSI
61#include "globus_gss_assist.h"
305d07c0 62char* olduser;
63int changeuser = 0;
7d1d5d37 64#endif
5598e598 65#endif
66
3c0ef626 67/* import */
68extern ServerOptions options;
69extern u_char *session_id2;
70extern int session_id2_len;
71
510132b6 72Authctxt *x_authctxt = NULL;
3c0ef626 73static int one = 1;
74
75typedef struct Authmethod Authmethod;
76struct Authmethod {
77 char *name;
78 int (*userauth)(Authctxt *authctxt);
79 int *enabled;
80};
81
82/* protocol */
83
1e608e42 84static void input_service_request(int, u_int32_t, void *);
85static void input_userauth_request(int, u_int32_t, void *);
3c0ef626 86
87/* helper */
88static Authmethod *authmethod_lookup(const char *);
89static char *authmethods_get(void);
510132b6 90int user_key_allowed(struct passwd *, Key *);
91int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
3c0ef626 92
93/* auth */
94static void userauth_banner(void);
95static int userauth_none(Authctxt *);
96static int userauth_passwd(Authctxt *);
97static int userauth_pubkey(Authctxt *);
98static int userauth_hostbased(Authctxt *);
99static int userauth_kbdint(Authctxt *);
100
5598e598 101#ifdef GSSAPI
102int userauth_external(Authctxt *authctxt);
103int userauth_gssapi(Authctxt *authctxt);
104#endif
105
3c0ef626 106Authmethod 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
510132b6 137Authctxt *
1e608e42 138do_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;
510132b6 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);
510132b6 155
156 return (authctxt);
3c0ef626 157}
158
159static void
1e608e42 160input_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
192static void
1e608e42 193input_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);
73a68d83 206
305d07c0 207#ifdef GSSAPI
208#ifdef GSI
209 if(changeuser == 0 && (strcmp(method,"external-keyx") == 0 || strcmp(method,"gssapi") ==0) && strcmp(user,"") == 0) {
73a68d83 210 char *gridmapped_name = NULL;
211 struct passwd *pw = NULL;
73a68d83 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)) {
305d07c0 218 olduser = authctxt->user;
a28539f3 219 authctxt->user = xstrdup(user);
73a68d83 220 authctxt->pw = pwcopy(pw);
221 authctxt->valid = 1;
305d07c0 222 changeuser = 1;
73a68d83 223 }
305d07c0 224
73a68d83 225 } else {
0dadc8a9 226 debug("I gridmapped and got null, reverting to %s",
227 authctxt->user);
228 xfree(user);
229 user = xstrdup(authctxt->user);
73a68d83 230 }
231 }
305d07c0 232 else if(changeuser) {
233 struct passwd *pw = NULL;
234 pw = getpwnam(user);
235 if (pw && allowed_user(pw)) {
a28539f3 236 xfree(authctxt->user);
305d07c0 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 */
73a68d83 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 */
510132b6 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
510132b6 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
510132b6 265 PRIVSEP(start_pam("NOUSER"));
3c0ef626 266#endif
267 }
510132b6 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;
510132b6 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
304void
305userauth_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
510132b6 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 }
411da244 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();
365 packet_start(SSH2_MSG_USERAUTH_FAILURE);
366 packet_put_cstring(methods);
367 packet_put_char(0); /* XXX partial success, unused */
368 packet_send();
369 packet_write_wait();
370 xfree(methods);
411da244 371 }
3c0ef626 372 }
373}
374
510132b6 375char *
376auth2_read_banner(void)
3c0ef626 377{
378 struct stat st;
379 char *banner = NULL;
380 off_t len, n;
381 int fd;
382
510132b6 383 if ((fd = open(options.banner, O_RDONLY)) == -1)
384 return (NULL);
385 if (fstat(fd, &st) == -1) {
386 close(fd);
387 return (NULL);
388 }
3c0ef626 389 len = st.st_size;
390 banner = xmalloc(len + 1);
510132b6 391 n = atomicio(read, fd, banner, len);
392 close(fd);
393
394 if (n != len) {
395 free(banner);
396 return (NULL);
397 }
3c0ef626 398 banner[n] = '\0';
510132b6 399
400 return (banner);
401}
402
403static void
404userauth_banner(void)
405{
406 char *banner = NULL;
407
408 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
409 return;
410
411 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
412 goto done;
413
3c0ef626 414 packet_start(SSH2_MSG_USERAUTH_BANNER);
415 packet_put_cstring(banner);
416 packet_put_cstring(""); /* language, unused */
417 packet_send();
418 debug("userauth_banner: sent");
419done:
420 if (banner)
421 xfree(banner);
3c0ef626 422 return;
423}
424
425static int
426userauth_none(Authctxt *authctxt)
427{
428 /* disable method "none", only allowed one time */
429 Authmethod *m = authmethod_lookup("none");
430 if (m != NULL)
431 m->enabled = NULL;
1e608e42 432 packet_check_eom();
3c0ef626 433 userauth_banner();
434
435 if (authctxt->valid == 0)
436 return(0);
437
438#ifdef HAVE_CYGWIN
1e608e42 439 if (check_nt_auth(1, authctxt->pw) == 0)
3c0ef626 440 return(0);
441#endif
510132b6 442 return PRIVSEP(auth_password(authctxt, ""));
3c0ef626 443}
444
445static int
446userauth_passwd(Authctxt *authctxt)
447{
448 char *password;
449 int authenticated = 0;
450 int change;
451 u_int len;
452 change = packet_get_char();
453 if (change)
454 log("password change not supported");
455 password = packet_get_string(&len);
1e608e42 456 packet_check_eom();
3c0ef626 457 if (authctxt->valid &&
458#ifdef HAVE_CYGWIN
1e608e42 459 check_nt_auth(1, authctxt->pw) &&
3c0ef626 460#endif
510132b6 461 PRIVSEP(auth_password(authctxt, password)) == 1)
3c0ef626 462 authenticated = 1;
463 memset(password, 0, len);
464 xfree(password);
465 return authenticated;
466}
467
468static int
469userauth_kbdint(Authctxt *authctxt)
470{
471 int authenticated = 0;
472 char *lang, *devs;
473
474 lang = packet_get_string(NULL);
475 devs = packet_get_string(NULL);
1e608e42 476 packet_check_eom();
3c0ef626 477
478 debug("keyboard-interactive devs %s", devs);
479
480 if (options.challenge_response_authentication)
481 authenticated = auth2_challenge(authctxt, devs);
482
483#ifdef USE_PAM
484 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
485 authenticated = auth2_pam(authctxt);
486#endif
487 xfree(devs);
488 xfree(lang);
489#ifdef HAVE_CYGWIN
1e608e42 490 if (check_nt_auth(0, authctxt->pw) == 0)
3c0ef626 491 return(0);
492#endif
493 return authenticated;
494}
495
496static int
497userauth_pubkey(Authctxt *authctxt)
498{
499 Buffer b;
1e608e42 500 Key *key = NULL;
501 char *pkalg;
502 u_char *pkblob, *sig;
3c0ef626 503 u_int alen, blen, slen;
504 int have_sig, pktype;
505 int authenticated = 0;
506
507 if (!authctxt->valid) {
508 debug2("userauth_pubkey: disabled because of invalid user");
509 return 0;
510 }
511 have_sig = packet_get_char();
512 if (datafellows & SSH_BUG_PKAUTH) {
513 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
514 /* no explicit pkalg given */
515 pkblob = packet_get_string(&blen);
516 buffer_init(&b);
517 buffer_append(&b, pkblob, blen);
518 /* so we have to extract the pkalg from the pkblob */
519 pkalg = buffer_get_string(&b, &alen);
520 buffer_free(&b);
521 } else {
522 pkalg = packet_get_string(&alen);
523 pkblob = packet_get_string(&blen);
524 }
525 pktype = key_type_from_name(pkalg);
526 if (pktype == KEY_UNSPEC) {
527 /* this is perfectly legal */
1e608e42 528 log("userauth_pubkey: unsupported public key algorithm: %s",
529 pkalg);
530 goto done;
3c0ef626 531 }
532 key = key_from_blob(pkblob, blen);
1e608e42 533 if (key == NULL) {
534 error("userauth_pubkey: cannot decode key: %s", pkalg);
535 goto done;
536 }
537 if (key->type != pktype) {
538 error("userauth_pubkey: type mismatch for decoded key "
539 "(received %d, expected %d)", key->type, pktype);
540 goto done;
541 }
542 if (have_sig) {
543 sig = packet_get_string(&slen);
544 packet_check_eom();
545 buffer_init(&b);
546 if (datafellows & SSH_OLD_SESSIONID) {
547 buffer_append(&b, session_id2, session_id2_len);
548 } else {
549 buffer_put_string(&b, session_id2, session_id2_len);
550 }
551 /* reconstruct packet */
552 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
553 buffer_put_cstring(&b, authctxt->user);
554 buffer_put_cstring(&b,
555 datafellows & SSH_BUG_PKSERVICE ?
556 "ssh-userauth" :
557 authctxt->service);
558 if (datafellows & SSH_BUG_PKAUTH) {
559 buffer_put_char(&b, have_sig);
560 } else {
561 buffer_put_cstring(&b, "publickey");
562 buffer_put_char(&b, have_sig);
563 buffer_put_cstring(&b, pkalg);
564 }
565 buffer_put_string(&b, pkblob, blen);
3c0ef626 566#ifdef DEBUG_PK
1e608e42 567 buffer_dump(&b);
3c0ef626 568#endif
1e608e42 569 /* test for correct signature */
510132b6 570 authenticated = 0;
571 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
572 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
573 buffer_len(&b))) == 1)
1e608e42 574 authenticated = 1;
575 buffer_clear(&b);
576 xfree(sig);
577 } else {
578 debug("test whether pkalg/pkblob are acceptable");
579 packet_check_eom();
580
581 /* XXX fake reply and always send PK_OK ? */
582 /*
583 * XXX this allows testing whether a user is allowed
584 * to login: if you happen to have a valid pubkey this
585 * message is sent. the message is NEVER sent at all
586 * if a user is not allowed to login. is this an
587 * issue? -markus
588 */
510132b6 589 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
1e608e42 590 packet_start(SSH2_MSG_USERAUTH_PK_OK);
591 packet_put_string(pkalg, alen);
592 packet_put_string(pkblob, blen);
593 packet_send();
594 packet_write_wait();
595 authctxt->postponed = 1;
3c0ef626 596 }
3c0ef626 597 }
1e608e42 598 if (authenticated != 1)
599 auth_clear_options();
600done:
3c0ef626 601 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
1e608e42 602 if (key != NULL)
603 key_free(key);
3c0ef626 604 xfree(pkalg);
605 xfree(pkblob);
606#ifdef HAVE_CYGWIN
1e608e42 607 if (check_nt_auth(0, authctxt->pw) == 0)
3c0ef626 608 return(0);
609#endif
610 return authenticated;
611}
612
613static int
614userauth_hostbased(Authctxt *authctxt)
615{
616 Buffer b;
1e608e42 617 Key *key = NULL;
618 char *pkalg, *cuser, *chost, *service;
619 u_char *pkblob, *sig;
3c0ef626 620 u_int alen, blen, slen;
621 int pktype;
622 int authenticated = 0;
623
624 if (!authctxt->valid) {
625 debug2("userauth_hostbased: disabled because of invalid user");
626 return 0;
627 }
628 pkalg = packet_get_string(&alen);
629 pkblob = packet_get_string(&blen);
630 chost = packet_get_string(NULL);
631 cuser = packet_get_string(NULL);
632 sig = packet_get_string(&slen);
633
634 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
635 cuser, chost, pkalg, slen);
636#ifdef DEBUG_PK
637 debug("signature:");
638 buffer_init(&b);
639 buffer_append(&b, sig, slen);
640 buffer_dump(&b);
641 buffer_free(&b);
642#endif
643 pktype = key_type_from_name(pkalg);
644 if (pktype == KEY_UNSPEC) {
645 /* this is perfectly legal */
646 log("userauth_hostbased: unsupported "
647 "public key algorithm: %s", pkalg);
648 goto done;
649 }
650 key = key_from_blob(pkblob, blen);
651 if (key == NULL) {
1e608e42 652 error("userauth_hostbased: cannot decode key: %s", pkalg);
653 goto done;
654 }
655 if (key->type != pktype) {
656 error("userauth_hostbased: type mismatch for decoded key "
657 "(received %d, expected %d)", key->type, pktype);
3c0ef626 658 goto done;
659 }
660 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
661 authctxt->service;
662 buffer_init(&b);
663 buffer_put_string(&b, session_id2, session_id2_len);
664 /* reconstruct packet */
665 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
666 buffer_put_cstring(&b, authctxt->user);
667 buffer_put_cstring(&b, service);
668 buffer_put_cstring(&b, "hostbased");
669 buffer_put_string(&b, pkalg, alen);
670 buffer_put_string(&b, pkblob, blen);
671 buffer_put_cstring(&b, chost);
672 buffer_put_cstring(&b, cuser);
673#ifdef DEBUG_PK
674 buffer_dump(&b);
675#endif
676 /* test for allowed key and correct signature */
510132b6 677 authenticated = 0;
678 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
679 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
680 buffer_len(&b))) == 1)
3c0ef626 681 authenticated = 1;
682
683 buffer_clear(&b);
3c0ef626 684done:
685 debug2("userauth_hostbased: authenticated %d", authenticated);
1e608e42 686 if (key != NULL)
687 key_free(key);
3c0ef626 688 xfree(pkalg);
689 xfree(pkblob);
690 xfree(cuser);
691 xfree(chost);
692 xfree(sig);
693 return authenticated;
694}
695
696/* get current user */
697
698struct passwd*
699auth_get_user(void)
700{
701 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
702}
703
704#define DELIM ","
705
706static char *
707authmethods_get(void)
708{
709 Authmethod *method = NULL;
1e608e42 710 Buffer b;
3c0ef626 711 char *list;
712
1e608e42 713 buffer_init(&b);
3c0ef626 714 for (method = authmethods; method->name != NULL; method++) {
715 if (strcmp(method->name, "none") == 0)
716 continue;
717 if (method->enabled != NULL && *(method->enabled) != 0) {
1e608e42 718 if (buffer_len(&b) > 0)
719 buffer_append(&b, ",", 1);
720 buffer_append(&b, method->name, strlen(method->name));
3c0ef626 721 }
722 }
1e608e42 723 buffer_append(&b, "\0", 1);
724 list = xstrdup(buffer_ptr(&b));
725 buffer_free(&b);
3c0ef626 726 return list;
727}
728
729static Authmethod *
730authmethod_lookup(const char *name)
731{
732 Authmethod *method = NULL;
733 if (name != NULL)
734 for (method = authmethods; method->name != NULL; method++)
735 if (method->enabled != NULL &&
736 *(method->enabled) != 0 &&
737 strcmp(name, method->name) == 0)
738 return method;
739 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
740 return NULL;
741}
742
743/* return 1 if user allows given key */
744static int
745user_key_allowed2(struct passwd *pw, Key *key, char *file)
746{
747 char line[8192];
748 int found_key = 0;
749 FILE *f;
750 u_long linenum = 0;
751 struct stat st;
752 Key *found;
1e608e42 753 char *fp;
3c0ef626 754
755 if (pw == NULL)
756 return 0;
757
758 /* Temporarily use the user's uid. */
759 temporarily_use_uid(pw);
760
761 debug("trying public key file %s", file);
762
763 /* Fail quietly if file does not exist */
764 if (stat(file, &st) < 0) {
765 /* Restore the privileged uid. */
766 restore_uid();
767 return 0;
768 }
769 /* Open the file containing the authorized keys. */
770 f = fopen(file, "r");
771 if (!f) {
772 /* Restore the privileged uid. */
773 restore_uid();
774 return 0;
775 }
776 if (options.strict_modes &&
777 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
778 fclose(f);
779 log("Authentication refused: %s", line);
780 restore_uid();
781 return 0;
782 }
783
784 found_key = 0;
785 found = key_new(key->type);
786
787 while (fgets(line, sizeof(line), f)) {
788 char *cp, *options = NULL;
789 linenum++;
790 /* Skip leading whitespace, empty and comment lines. */
791 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
792 ;
793 if (!*cp || *cp == '\n' || *cp == '#')
794 continue;
795
796 if (key_read(found, &cp) != 1) {
797 /* no key? check if there are options for this key */
798 int quoted = 0;
799 debug2("user_key_allowed: check options: '%s'", cp);
800 options = cp;
801 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
802 if (*cp == '\\' && cp[1] == '"')
803 cp++; /* Skip both */
804 else if (*cp == '"')
805 quoted = !quoted;
806 }
807 /* Skip remaining whitespace. */
808 for (; *cp == ' ' || *cp == '\t'; cp++)
809 ;
810 if (key_read(found, &cp) != 1) {
811 debug2("user_key_allowed: advance: '%s'", cp);
812 /* still no key? advance to next line*/
813 continue;
814 }
815 }
816 if (key_equal(found, key) &&
817 auth_parse_options(pw, options, file, linenum) == 1) {
818 found_key = 1;
819 debug("matching key found: file %s, line %lu",
820 file, linenum);
1e608e42 821 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
822 verbose("Found matching %s key: %s",
823 key_type(found), fp);
824 xfree(fp);
3c0ef626 825 break;
826 }
827 }
828 restore_uid();
829 fclose(f);
830 key_free(found);
831 if (!found_key)
832 debug2("key not found");
833 return found_key;
834}
835
836/* check whether given key is in .ssh/authorized_keys* */
510132b6 837int
3c0ef626 838user_key_allowed(struct passwd *pw, Key *key)
839{
840 int success;
841 char *file;
842
843 file = authorized_keys_file(pw);
844 success = user_key_allowed2(pw, key, file);
845 xfree(file);
846 if (success)
847 return success;
848
849 /* try suffix "2" for backward compat, too */
850 file = authorized_keys_file2(pw);
851 success = user_key_allowed2(pw, key, file);
852 xfree(file);
853 return success;
854}
855
856/* return 1 if given hostkey is allowed */
510132b6 857int
3c0ef626 858hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
859 Key *key)
860{
861 const char *resolvedname, *ipaddr, *lookup;
1e608e42 862 HostStatus host_status;
863 int len;
3c0ef626 864
1e608e42 865 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
3c0ef626 866 ipaddr = get_remote_ipaddr();
867
868 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
869 chost, resolvedname, ipaddr);
870
871 if (options.hostbased_uses_name_from_packet_only) {
872 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
873 return 0;
874 lookup = chost;
875 } else {
876 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
877 debug2("stripping trailing dot from chost %s", chost);
878 chost[len - 1] = '\0';
879 }
880 if (strcasecmp(resolvedname, chost) != 0)
881 log("userauth_hostbased mismatch: "
882 "client sends %s, but we resolve %s to %s",
883 chost, ipaddr, resolvedname);
884 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
885 return 0;
886 lookup = resolvedname;
887 }
888 debug2("userauth_hostbased: access allowed by auth_rhosts2");
889
890 host_status = check_key_in_hostfiles(pw, key, lookup,
891 _PATH_SSH_SYSTEM_HOSTFILE,
892 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
893
894 /* backward compat if no key has been found. */
895 if (host_status == HOST_NEW)
896 host_status = check_key_in_hostfiles(pw, key, lookup,
897 _PATH_SSH_SYSTEM_HOSTFILE2,
898 options.ignore_user_known_hosts ? NULL :
899 _PATH_SSH_USER_HOSTFILE2);
900
901 return (host_status == HOST_OK);
902}
This page took 0.27093 seconds and 5 git commands to generate.