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