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