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