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