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