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