]> andersk Git - openssh.git/blob - auth2.c
- markus@cvs.openbsd.org 2002/01/13 17:57:37
[openssh.git] / auth2.c
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.82 2002/01/13 17:57:37 markus 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 void
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         do_authenticated(authctxt);
129 }
130
131 static void
132 input_service_request(int type, u_int32_t seq, void *ctxt)
133 {
134         Authctxt *authctxt = ctxt;
135         u_int len;
136         int accept = 0;
137         char *service = packet_get_string(&len);
138         packet_check_eom();
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
164 static void
165 input_userauth_request(int type, u_int32_t seq, void *ctxt)
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 */
212         auth2_challenge_stop(authctxt);
213         authctxt->postponed = 0;
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
228 void
229 userauth_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 */
257                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
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,
267                             get_canonical_hostname(options.reverse_mapping_check),
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
282 static void
283 userauth_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");
306 done:
307         if (banner)
308                 xfree(banner);
309         close(fd);
310         return;
311 }
312
313 static int
314 userauth_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;
320         packet_check_eom();
321         userauth_banner();
322
323         if (authctxt->valid == 0)
324                 return(0);
325
326 #ifdef HAVE_CYGWIN
327         if (check_nt_auth(1, authctxt->pw) == 0)
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
339 static int
340 userauth_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);
350         packet_check_eom();
351         if (authctxt->valid &&
352 #ifdef HAVE_CYGWIN
353             check_nt_auth(1, authctxt->pw) &&
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
368 static int
369 userauth_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);
376         packet_check_eom();
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
390         if (check_nt_auth(0, authctxt->pw) == 0)
391                 return(0);
392 #endif
393         return authenticated;
394 }
395
396 static int
397 userauth_pubkey(Authctxt *authctxt)
398 {
399         Buffer b;
400         Key *key;
401         char *pkalg, *pkblob, *sig;
402         u_int alen, blen, slen;
403         int have_sig, pktype;
404         int authenticated = 0;
405
406         if (!authctxt->valid) {
407                 debug2("userauth_pubkey: disabled because of invalid user");
408                 return 0;
409         }
410         have_sig = packet_get_char();
411         if (datafellows & SSH_BUG_PKAUTH) {
412                 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
413                 /* no explicit pkalg given */
414                 pkblob = packet_get_string(&blen);
415                 buffer_init(&b);
416                 buffer_append(&b, pkblob, blen);
417                 /* so we have to extract the pkalg from the pkblob */
418                 pkalg = buffer_get_string(&b, &alen);
419                 buffer_free(&b);
420         } else {
421                 pkalg = packet_get_string(&alen);
422                 pkblob = packet_get_string(&blen);
423         }
424         pktype = key_type_from_name(pkalg);
425         if (pktype == KEY_UNSPEC) {
426                 /* this is perfectly legal */
427                 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
428                 xfree(pkalg);
429                 xfree(pkblob);
430                 return 0;
431         }
432         key = key_from_blob(pkblob, blen);
433         if (key != NULL) {
434                 if (have_sig) {
435                         sig = packet_get_string(&slen);
436                         packet_check_eom();
437                         buffer_init(&b);
438                         if (datafellows & SSH_OLD_SESSIONID) {
439                                 buffer_append(&b, session_id2, session_id2_len);
440                         } else {
441                                 buffer_put_string(&b, session_id2, session_id2_len);
442                         }
443                         /* reconstruct packet */
444                         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
445                         buffer_put_cstring(&b, authctxt->user);
446                         buffer_put_cstring(&b,
447                             datafellows & SSH_BUG_PKSERVICE ?
448                             "ssh-userauth" :
449                             authctxt->service);
450                         if (datafellows & SSH_BUG_PKAUTH) {
451                                 buffer_put_char(&b, have_sig);
452                         } else {
453                                 buffer_put_cstring(&b, "publickey");
454                                 buffer_put_char(&b, have_sig);
455                                 buffer_put_cstring(&b, pkalg);
456                         }
457                         buffer_put_string(&b, pkblob, blen);
458 #ifdef DEBUG_PK
459                         buffer_dump(&b);
460 #endif
461                         /* test for correct signature */
462                         if (user_key_allowed(authctxt->pw, key) &&
463                             key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
464                                 authenticated = 1;
465                         buffer_clear(&b);
466                         xfree(sig);
467                 } else {
468                         debug("test whether pkalg/pkblob are acceptable");
469                         packet_check_eom();
470
471                         /* XXX fake reply and always send PK_OK ? */
472                         /*
473                          * XXX this allows testing whether a user is allowed
474                          * to login: if you happen to have a valid pubkey this
475                          * message is sent. the message is NEVER sent at all
476                          * if a user is not allowed to login. is this an
477                          * issue? -markus
478                          */
479                         if (user_key_allowed(authctxt->pw, key)) {
480                                 packet_start(SSH2_MSG_USERAUTH_PK_OK);
481                                 packet_put_string(pkalg, alen);
482                                 packet_put_string(pkblob, blen);
483                                 packet_send();
484                                 packet_write_wait();
485                                 authctxt->postponed = 1;
486                         }
487                 }
488                 if (authenticated != 1)
489                         auth_clear_options();
490                 key_free(key);
491         }
492         debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
493         xfree(pkalg);
494         xfree(pkblob);
495 #ifdef HAVE_CYGWIN
496         if (check_nt_auth(0, authctxt->pw) == 0)
497                 return(0);
498 #endif
499         return authenticated;
500 }
501
502 static int
503 userauth_hostbased(Authctxt *authctxt)
504 {
505         Buffer b;
506         Key *key;
507         char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
508         u_int alen, blen, slen;
509         int pktype;
510         int authenticated = 0;
511
512         if (!authctxt->valid) {
513                 debug2("userauth_hostbased: disabled because of invalid user");
514                 return 0;
515         }
516         pkalg = packet_get_string(&alen);
517         pkblob = packet_get_string(&blen);
518         chost = packet_get_string(NULL);
519         cuser = packet_get_string(NULL);
520         sig = packet_get_string(&slen);
521
522         debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
523             cuser, chost, pkalg, slen);
524 #ifdef DEBUG_PK
525         debug("signature:");
526         buffer_init(&b);
527         buffer_append(&b, sig, slen);
528         buffer_dump(&b);
529         buffer_free(&b);
530 #endif
531         pktype = key_type_from_name(pkalg);
532         if (pktype == KEY_UNSPEC) {
533                 /* this is perfectly legal */
534                 log("userauth_hostbased: unsupported "
535                     "public key algorithm: %s", pkalg);
536                 goto done;
537         }
538         key = key_from_blob(pkblob, blen);
539         if (key == NULL) {
540                 debug("userauth_hostbased: cannot decode key: %s", pkalg);
541                 goto done;
542         }
543         service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
544             authctxt->service;
545         buffer_init(&b);
546         buffer_put_string(&b, session_id2, session_id2_len);
547         /* reconstruct packet */
548         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
549         buffer_put_cstring(&b, authctxt->user);
550         buffer_put_cstring(&b, service);
551         buffer_put_cstring(&b, "hostbased");
552         buffer_put_string(&b, pkalg, alen);
553         buffer_put_string(&b, pkblob, blen);
554         buffer_put_cstring(&b, chost);
555         buffer_put_cstring(&b, cuser);
556 #ifdef DEBUG_PK
557         buffer_dump(&b);
558 #endif
559         /* test for allowed key and correct signature */
560         if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
561             key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
562                 authenticated = 1;
563
564         buffer_clear(&b);
565         key_free(key);
566
567 done:
568         debug2("userauth_hostbased: authenticated %d", authenticated);
569         xfree(pkalg);
570         xfree(pkblob);
571         xfree(cuser);
572         xfree(chost);
573         xfree(sig);
574         return authenticated;
575 }
576
577 /* get current user */
578
579 struct passwd*
580 auth_get_user(void)
581 {
582         return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
583 }
584
585 #define DELIM   ","
586
587 static char *
588 authmethods_get(void)
589 {
590         Authmethod *method = NULL;
591         Buffer b;
592         char *list;
593
594         buffer_init(&b);
595         for (method = authmethods; method->name != NULL; method++) {
596                 if (strcmp(method->name, "none") == 0)
597                         continue;
598                 if (method->enabled != NULL && *(method->enabled) != 0) {
599                         if (buffer_len(&b) > 0)
600                                 buffer_append(&b, ",", 1);
601                         buffer_append(&b, method->name, strlen(method->name));
602                 }
603         }
604         buffer_append(&b, "\0", 1);
605         list = xstrdup(buffer_ptr(&b));
606         buffer_free(&b);
607         return list;
608 }
609
610 static Authmethod *
611 authmethod_lookup(const char *name)
612 {
613         Authmethod *method = NULL;
614         if (name != NULL)
615                 for (method = authmethods; method->name != NULL; method++)
616                         if (method->enabled != NULL &&
617                             *(method->enabled) != 0 &&
618                             strcmp(name, method->name) == 0)
619                                 return method;
620         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
621         return NULL;
622 }
623
624 /* return 1 if user allows given key */
625 static int
626 user_key_allowed2(struct passwd *pw, Key *key, char *file)
627 {
628         char line[8192];
629         int found_key = 0;
630         FILE *f;
631         u_long linenum = 0;
632         struct stat st;
633         Key *found;
634         char *fp;
635
636         if (pw == NULL)
637                 return 0;
638
639         /* Temporarily use the user's uid. */
640         temporarily_use_uid(pw);
641
642         debug("trying public key file %s", file);
643
644         /* Fail quietly if file does not exist */
645         if (stat(file, &st) < 0) {
646                 /* Restore the privileged uid. */
647                 restore_uid();
648                 return 0;
649         }
650         /* Open the file containing the authorized keys. */
651         f = fopen(file, "r");
652         if (!f) {
653                 /* Restore the privileged uid. */
654                 restore_uid();
655                 return 0;
656         }
657         if (options.strict_modes &&
658             secure_filename(f, file, pw, line, sizeof(line)) != 0) {
659                 fclose(f);
660                 log("Authentication refused: %s", line);
661                 restore_uid();
662                 return 0;
663         }
664
665         found_key = 0;
666         found = key_new(key->type);
667
668         while (fgets(line, sizeof(line), f)) {
669                 char *cp, *options = NULL;
670                 linenum++;
671                 /* Skip leading whitespace, empty and comment lines. */
672                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
673                         ;
674                 if (!*cp || *cp == '\n' || *cp == '#')
675                         continue;
676
677                 if (key_read(found, &cp) != 1) {
678                         /* no key?  check if there are options for this key */
679                         int quoted = 0;
680                         debug2("user_key_allowed: check options: '%s'", cp);
681                         options = cp;
682                         for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
683                                 if (*cp == '\\' && cp[1] == '"')
684                                         cp++;   /* Skip both */
685                                 else if (*cp == '"')
686                                         quoted = !quoted;
687                         }
688                         /* Skip remaining whitespace. */
689                         for (; *cp == ' ' || *cp == '\t'; cp++)
690                                 ;
691                         if (key_read(found, &cp) != 1) {
692                                 debug2("user_key_allowed: advance: '%s'", cp);
693                                 /* still no key?  advance to next line*/
694                                 continue;
695                         }
696                 }
697                 if (key_equal(found, key) &&
698                     auth_parse_options(pw, options, file, linenum) == 1) {
699                         found_key = 1;
700                         debug("matching key found: file %s, line %lu",
701                             file, linenum);
702                         fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
703                         verbose("Found matching %s key: %s",
704                             key_type(found), fp);
705                         xfree(fp);
706                         break;
707                 }
708         }
709         restore_uid();
710         fclose(f);
711         key_free(found);
712         if (!found_key)
713                 debug2("key not found");
714         return found_key;
715 }
716
717 /* check whether given key is in .ssh/authorized_keys* */
718 static int
719 user_key_allowed(struct passwd *pw, Key *key)
720 {
721         int success;
722         char *file;
723
724         file = authorized_keys_file(pw);
725         success = user_key_allowed2(pw, key, file);
726         xfree(file);
727         if (success)
728                 return success;
729
730         /* try suffix "2" for backward compat, too */
731         file = authorized_keys_file2(pw);
732         success = user_key_allowed2(pw, key, file);
733         xfree(file);
734         return success;
735 }
736
737 /* return 1 if given hostkey is allowed */
738 static int
739 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
740     Key *key)
741 {
742         const char *resolvedname, *ipaddr, *lookup;
743         HostStatus host_status;
744         int len;
745
746         resolvedname = get_canonical_hostname(options.reverse_mapping_check);
747         ipaddr = get_remote_ipaddr();
748
749         debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
750             chost, resolvedname, ipaddr);
751
752         if (options.hostbased_uses_name_from_packet_only) {
753                 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
754                         return 0;
755                 lookup = chost;
756         } else {
757                 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
758                         debug2("stripping trailing dot from chost %s", chost);
759                         chost[len - 1] = '\0';
760                 }
761                 if (strcasecmp(resolvedname, chost) != 0)
762                         log("userauth_hostbased mismatch: "
763                             "client sends %s, but we resolve %s to %s",
764                             chost, ipaddr, resolvedname);
765                 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
766                         return 0;
767                 lookup = resolvedname;
768         }
769         debug2("userauth_hostbased: access allowed by auth_rhosts2");
770
771         host_status = check_key_in_hostfiles(pw, key, lookup,
772             _PATH_SSH_SYSTEM_HOSTFILE,
773             options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
774
775         /* backward compat if no key has been found. */
776         if (host_status == HOST_NEW)
777                 host_status = check_key_in_hostfiles(pw, key, lookup,
778                     _PATH_SSH_SYSTEM_HOSTFILE2,
779                     options.ignore_user_known_hosts ? NULL :
780                     _PATH_SSH_USER_HOSTFILE2);
781
782         return (host_status == HOST_OK);
783 }
784
This page took 1.069479 seconds and 5 git commands to generate.