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