]> andersk Git - gssapi-openssh.git/blob - openssh/auth2.c
merging in Simon Wilkinson's latest patch (openssh-3.1p1-gssapi-20020323.diff)
[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         auth2_challenge_stop(authctxt);
230
231 #ifdef GSSAPI
232         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
233         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
234 #endif
235
236         authctxt->postponed = 0;
237
238         /* try to authenticate user */
239         m = authmethod_lookup(method);
240         if (m != NULL) {
241                 debug2("input_userauth_request: try method %s", method);
242                 authenticated = m->userauth(authctxt);
243         }
244         userauth_finish(authctxt, authenticated, method);
245
246         xfree(service);
247         xfree(user);
248         xfree(method);
249 }
250
251 void
252 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
253 {
254         char *methods;
255
256         if (!authctxt->valid && authenticated)
257                 fatal("INTERNAL ERROR: authenticated invalid user %s",
258                     authctxt->user);
259
260         /* Special handling for root */
261         if (authenticated && authctxt->pw->pw_uid == 0 &&
262             !auth_root_allowed(method))
263                 authenticated = 0;
264
265 #ifdef USE_PAM
266         if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
267             NULL))
268                 authenticated = 0;
269 #endif /* USE_PAM */
270
271         /* Log before sending the reply */
272         auth_log(authctxt, authenticated, method, " ssh2");
273
274         if (authctxt->postponed)
275                 return;
276
277         /* XXX todo: check if multiple auth methods are needed */
278         if (authenticated == 1) {
279                 /* turn off userauth */
280                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
281                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
282                 packet_send();
283                 packet_write_wait();
284                 /* now we can break out */
285                 authctxt->success = 1;
286         } else {
287                 if (authctxt->failures++ > AUTH_FAIL_MAX) {
288 #ifdef WITH_AIXAUTHENTICATE
289                         loginfailed(authctxt->user,
290                             get_canonical_hostname(options.verify_reverse_mapping),
291                             "ssh");
292 #endif /* WITH_AIXAUTHENTICATE */
293                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
294                 }
295                 methods = authmethods_get();
296                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
297                 packet_put_cstring(methods);
298                 packet_put_char(0);     /* XXX partial success, unused */
299                 packet_send();
300                 packet_write_wait();
301                 xfree(methods);
302         }
303 }
304
305 static void
306 userauth_banner(void)
307 {
308         struct stat st;
309         char *banner = NULL;
310         off_t len, n;
311         int fd;
312
313         if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
314                 return;
315         if ((fd = open(options.banner, O_RDONLY)) < 0)
316                 return;
317         if (fstat(fd, &st) < 0)
318                 goto done;
319         len = st.st_size;
320         banner = xmalloc(len + 1);
321         if ((n = read(fd, banner, len)) < 0)
322                 goto done;
323         banner[n] = '\0';
324         packet_start(SSH2_MSG_USERAUTH_BANNER);
325         packet_put_cstring(banner);
326         packet_put_cstring("");         /* language, unused */
327         packet_send();
328         debug("userauth_banner: sent");
329 done:
330         if (banner)
331                 xfree(banner);
332         close(fd);
333         return;
334 }
335
336 static int
337 userauth_none(Authctxt *authctxt)
338 {
339         /* disable method "none", only allowed one time */
340         Authmethod *m = authmethod_lookup("none");
341         if (m != NULL)
342                 m->enabled = NULL;
343         packet_check_eom();
344         userauth_banner();
345
346         if (authctxt->valid == 0)
347                 return(0);
348
349 #ifdef HAVE_CYGWIN
350         if (check_nt_auth(1, authctxt->pw) == 0)
351                 return(0);
352 #endif
353 #ifdef USE_PAM
354         return auth_pam_password(authctxt->pw, "");
355 #elif defined(HAVE_OSF_SIA)
356         return 0;
357 #else /* !HAVE_OSF_SIA && !USE_PAM */
358         return auth_password(authctxt, "");
359 #endif /* USE_PAM */
360 }
361
362 static int
363 userauth_passwd(Authctxt *authctxt)
364 {
365         char *password;
366         int authenticated = 0;
367         int change;
368         u_int len;
369         change = packet_get_char();
370         if (change)
371                 log("password change not supported");
372         password = packet_get_string(&len);
373         packet_check_eom();
374         if (authctxt->valid &&
375 #ifdef HAVE_CYGWIN
376             check_nt_auth(1, authctxt->pw) &&
377 #endif
378 #ifdef USE_PAM
379             auth_pam_password(authctxt->pw, password) == 1)
380 #elif defined(HAVE_OSF_SIA)
381             auth_sia_password(authctxt->user, password) == 1)
382 #else /* !USE_PAM && !HAVE_OSF_SIA */
383             auth_password(authctxt, password) == 1)
384 #endif /* USE_PAM */
385                 authenticated = 1;
386         memset(password, 0, len);
387         xfree(password);
388         return authenticated;
389 }
390
391 static int
392 userauth_kbdint(Authctxt *authctxt)
393 {
394         int authenticated = 0;
395         char *lang, *devs;
396
397         lang = packet_get_string(NULL);
398         devs = packet_get_string(NULL);
399         packet_check_eom();
400
401         debug("keyboard-interactive devs %s", devs);
402
403         if (options.challenge_response_authentication)
404                 authenticated = auth2_challenge(authctxt, devs);
405
406 #ifdef USE_PAM
407         if (authenticated == 0 && options.pam_authentication_via_kbd_int)
408                 authenticated = auth2_pam(authctxt);
409 #endif
410         xfree(devs);
411         xfree(lang);
412 #ifdef HAVE_CYGWIN
413         if (check_nt_auth(0, authctxt->pw) == 0)
414                 return(0);
415 #endif
416         return authenticated;
417 }
418
419 static int
420 userauth_pubkey(Authctxt *authctxt)
421 {
422         Buffer b;
423         Key *key = NULL;
424         char *pkalg;
425         u_char *pkblob, *sig;
426         u_int alen, blen, slen;
427         int have_sig, pktype;
428         int authenticated = 0;
429
430         if (!authctxt->valid) {
431                 debug2("userauth_pubkey: disabled because of invalid user");
432                 return 0;
433         }
434         have_sig = packet_get_char();
435         if (datafellows & SSH_BUG_PKAUTH) {
436                 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
437                 /* no explicit pkalg given */
438                 pkblob = packet_get_string(&blen);
439                 buffer_init(&b);
440                 buffer_append(&b, pkblob, blen);
441                 /* so we have to extract the pkalg from the pkblob */
442                 pkalg = buffer_get_string(&b, &alen);
443                 buffer_free(&b);
444         } else {
445                 pkalg = packet_get_string(&alen);
446                 pkblob = packet_get_string(&blen);
447         }
448         pktype = key_type_from_name(pkalg);
449         if (pktype == KEY_UNSPEC) {
450                 /* this is perfectly legal */
451                 log("userauth_pubkey: unsupported public key algorithm: %s",
452                     pkalg);
453                 goto done;
454         }
455         key = key_from_blob(pkblob, blen);
456         if (key == NULL) {
457                 error("userauth_pubkey: cannot decode key: %s", pkalg);
458                 goto done;
459         }
460         if (key->type != pktype) {
461                 error("userauth_pubkey: type mismatch for decoded key "
462                     "(received %d, expected %d)", key->type, pktype);
463                 goto done;
464         }
465         if (have_sig) {
466                 sig = packet_get_string(&slen);
467                 packet_check_eom();
468                 buffer_init(&b);
469                 if (datafellows & SSH_OLD_SESSIONID) {
470                         buffer_append(&b, session_id2, session_id2_len);
471                 } else {
472                         buffer_put_string(&b, session_id2, session_id2_len);
473                 }
474                 /* reconstruct packet */
475                 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
476                 buffer_put_cstring(&b, authctxt->user);
477                 buffer_put_cstring(&b,
478                     datafellows & SSH_BUG_PKSERVICE ?
479                     "ssh-userauth" :
480                     authctxt->service);
481                 if (datafellows & SSH_BUG_PKAUTH) {
482                         buffer_put_char(&b, have_sig);
483                 } else {
484                         buffer_put_cstring(&b, "publickey");
485                         buffer_put_char(&b, have_sig);
486                         buffer_put_cstring(&b, pkalg);
487                 }
488                 buffer_put_string(&b, pkblob, blen);
489 #ifdef DEBUG_PK
490                 buffer_dump(&b);
491 #endif
492                 /* test for correct signature */
493                 if (user_key_allowed(authctxt->pw, key) &&
494                     key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
495                         authenticated = 1;
496                 buffer_clear(&b);
497                 xfree(sig);
498         } else {
499                 debug("test whether pkalg/pkblob are acceptable");
500                 packet_check_eom();
501
502                 /* XXX fake reply and always send PK_OK ? */
503                 /*
504                  * XXX this allows testing whether a user is allowed
505                  * to login: if you happen to have a valid pubkey this
506                  * message is sent. the message is NEVER sent at all
507                  * if a user is not allowed to login. is this an
508                  * issue? -markus
509                  */
510                 if (user_key_allowed(authctxt->pw, key)) {
511                         packet_start(SSH2_MSG_USERAUTH_PK_OK);
512                         packet_put_string(pkalg, alen);
513                         packet_put_string(pkblob, blen);
514                         packet_send();
515                         packet_write_wait();
516                         authctxt->postponed = 1;
517                 }
518         }
519         if (authenticated != 1)
520                 auth_clear_options();
521 done:
522         debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
523         if (key != NULL)
524                 key_free(key);
525         xfree(pkalg);
526         xfree(pkblob);
527 #ifdef HAVE_CYGWIN
528         if (check_nt_auth(0, authctxt->pw) == 0)
529                 return(0);
530 #endif
531         return authenticated;
532 }
533
534 static int
535 userauth_hostbased(Authctxt *authctxt)
536 {
537         Buffer b;
538         Key *key = NULL;
539         char *pkalg, *cuser, *chost, *service;
540         u_char *pkblob, *sig;
541         u_int alen, blen, slen;
542         int pktype;
543         int authenticated = 0;
544
545         if (!authctxt->valid) {
546                 debug2("userauth_hostbased: disabled because of invalid user");
547                 return 0;
548         }
549         pkalg = packet_get_string(&alen);
550         pkblob = packet_get_string(&blen);
551         chost = packet_get_string(NULL);
552         cuser = packet_get_string(NULL);
553         sig = packet_get_string(&slen);
554
555         debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
556             cuser, chost, pkalg, slen);
557 #ifdef DEBUG_PK
558         debug("signature:");
559         buffer_init(&b);
560         buffer_append(&b, sig, slen);
561         buffer_dump(&b);
562         buffer_free(&b);
563 #endif
564         pktype = key_type_from_name(pkalg);
565         if (pktype == KEY_UNSPEC) {
566                 /* this is perfectly legal */
567                 log("userauth_hostbased: unsupported "
568                     "public key algorithm: %s", pkalg);
569                 goto done;
570         }
571         key = key_from_blob(pkblob, blen);
572         if (key == NULL) {
573                 error("userauth_hostbased: cannot decode key: %s", pkalg);
574                 goto done;
575         }
576         if (key->type != pktype) {
577                 error("userauth_hostbased: type mismatch for decoded key "
578                     "(received %d, expected %d)", key->type, pktype);
579                 goto done;
580         }
581         service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
582             authctxt->service;
583         buffer_init(&b);
584         buffer_put_string(&b, session_id2, session_id2_len);
585         /* reconstruct packet */
586         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
587         buffer_put_cstring(&b, authctxt->user);
588         buffer_put_cstring(&b, service);
589         buffer_put_cstring(&b, "hostbased");
590         buffer_put_string(&b, pkalg, alen);
591         buffer_put_string(&b, pkblob, blen);
592         buffer_put_cstring(&b, chost);
593         buffer_put_cstring(&b, cuser);
594 #ifdef DEBUG_PK
595         buffer_dump(&b);
596 #endif
597         /* test for allowed key and correct signature */
598         if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
599             key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
600                 authenticated = 1;
601
602         buffer_clear(&b);
603 done:
604         debug2("userauth_hostbased: authenticated %d", authenticated);
605         if (key != NULL)
606                 key_free(key);
607         xfree(pkalg);
608         xfree(pkblob);
609         xfree(cuser);
610         xfree(chost);
611         xfree(sig);
612         return authenticated;
613 }
614
615 /* get current user */
616
617 struct passwd*
618 auth_get_user(void)
619 {
620         return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
621 }
622
623 #define DELIM   ","
624
625 static char *
626 authmethods_get(void)
627 {
628         Authmethod *method = NULL;
629         Buffer b;
630         char *list;
631
632         buffer_init(&b);
633         for (method = authmethods; method->name != NULL; method++) {
634                 if (strcmp(method->name, "none") == 0)
635                         continue;
636                 if (method->enabled != NULL && *(method->enabled) != 0) {
637                         if (buffer_len(&b) > 0)
638                                 buffer_append(&b, ",", 1);
639                         buffer_append(&b, method->name, strlen(method->name));
640                 }
641         }
642         buffer_append(&b, "\0", 1);
643         list = xstrdup(buffer_ptr(&b));
644         buffer_free(&b);
645         return list;
646 }
647
648 static Authmethod *
649 authmethod_lookup(const char *name)
650 {
651         Authmethod *method = NULL;
652         if (name != NULL)
653                 for (method = authmethods; method->name != NULL; method++)
654                         if (method->enabled != NULL &&
655                             *(method->enabled) != 0 &&
656                             strcmp(name, method->name) == 0)
657                                 return method;
658         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
659         return NULL;
660 }
661
662 /* return 1 if user allows given key */
663 static int
664 user_key_allowed2(struct passwd *pw, Key *key, char *file)
665 {
666         char line[8192];
667         int found_key = 0;
668         FILE *f;
669         u_long linenum = 0;
670         struct stat st;
671         Key *found;
672         char *fp;
673
674         if (pw == NULL)
675                 return 0;
676
677         /* Temporarily use the user's uid. */
678         temporarily_use_uid(pw);
679
680         debug("trying public key file %s", file);
681
682         /* Fail quietly if file does not exist */
683         if (stat(file, &st) < 0) {
684                 /* Restore the privileged uid. */
685                 restore_uid();
686                 return 0;
687         }
688         /* Open the file containing the authorized keys. */
689         f = fopen(file, "r");
690         if (!f) {
691                 /* Restore the privileged uid. */
692                 restore_uid();
693                 return 0;
694         }
695         if (options.strict_modes &&
696             secure_filename(f, file, pw, line, sizeof(line)) != 0) {
697                 fclose(f);
698                 log("Authentication refused: %s", line);
699                 restore_uid();
700                 return 0;
701         }
702
703         found_key = 0;
704         found = key_new(key->type);
705
706         while (fgets(line, sizeof(line), f)) {
707                 char *cp, *options = NULL;
708                 linenum++;
709                 /* Skip leading whitespace, empty and comment lines. */
710                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
711                         ;
712                 if (!*cp || *cp == '\n' || *cp == '#')
713                         continue;
714
715                 if (key_read(found, &cp) != 1) {
716                         /* no key?  check if there are options for this key */
717                         int quoted = 0;
718                         debug2("user_key_allowed: check options: '%s'", cp);
719                         options = cp;
720                         for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
721                                 if (*cp == '\\' && cp[1] == '"')
722                                         cp++;   /* Skip both */
723                                 else if (*cp == '"')
724                                         quoted = !quoted;
725                         }
726                         /* Skip remaining whitespace. */
727                         for (; *cp == ' ' || *cp == '\t'; cp++)
728                                 ;
729                         if (key_read(found, &cp) != 1) {
730                                 debug2("user_key_allowed: advance: '%s'", cp);
731                                 /* still no key?  advance to next line*/
732                                 continue;
733                         }
734                 }
735                 if (key_equal(found, key) &&
736                     auth_parse_options(pw, options, file, linenum) == 1) {
737                         found_key = 1;
738                         debug("matching key found: file %s, line %lu",
739                             file, linenum);
740                         fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
741                         verbose("Found matching %s key: %s",
742                             key_type(found), fp);
743                         xfree(fp);
744                         break;
745                 }
746         }
747         restore_uid();
748         fclose(f);
749         key_free(found);
750         if (!found_key)
751                 debug2("key not found");
752         return found_key;
753 }
754
755 /* check whether given key is in .ssh/authorized_keys* */
756 static int
757 user_key_allowed(struct passwd *pw, Key *key)
758 {
759         int success;
760         char *file;
761
762         file = authorized_keys_file(pw);
763         success = user_key_allowed2(pw, key, file);
764         xfree(file);
765         if (success)
766                 return success;
767
768         /* try suffix "2" for backward compat, too */
769         file = authorized_keys_file2(pw);
770         success = user_key_allowed2(pw, key, file);
771         xfree(file);
772         return success;
773 }
774
775 /* return 1 if given hostkey is allowed */
776 static int
777 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
778     Key *key)
779 {
780         const char *resolvedname, *ipaddr, *lookup;
781         HostStatus host_status;
782         int len;
783
784         resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
785         ipaddr = get_remote_ipaddr();
786
787         debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
788             chost, resolvedname, ipaddr);
789
790         if (options.hostbased_uses_name_from_packet_only) {
791                 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
792                         return 0;
793                 lookup = chost;
794         } else {
795                 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
796                         debug2("stripping trailing dot from chost %s", chost);
797                         chost[len - 1] = '\0';
798                 }
799                 if (strcasecmp(resolvedname, chost) != 0)
800                         log("userauth_hostbased mismatch: "
801                             "client sends %s, but we resolve %s to %s",
802                             chost, ipaddr, resolvedname);
803                 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
804                         return 0;
805                 lookup = resolvedname;
806         }
807         debug2("userauth_hostbased: access allowed by auth_rhosts2");
808
809         host_status = check_key_in_hostfiles(pw, key, lookup,
810             _PATH_SSH_SYSTEM_HOSTFILE,
811             options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
812
813         /* backward compat if no key has been found. */
814         if (host_status == HOST_NEW)
815                 host_status = check_key_in_hostfiles(pw, key, lookup,
816                     _PATH_SSH_SYSTEM_HOSTFILE2,
817                     options.ignore_user_known_hosts ? NULL :
818                     _PATH_SSH_USER_HOSTFILE2);
819
820         return (host_status == HOST_OK);
821 }
This page took 0.122208 seconds and 5 git commands to generate.