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