]> andersk Git - gssapi-openssh.git/blob - openssh/auth2.c
another bug fix from OPENSSH_3_2_3P1_GSI_3 (the tag moved...)
[gssapi-openssh.git] / openssh / auth2.c
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "includes.h"
26 RCSID("$OpenBSD: auth2.c,v 1.91 2002/05/13 02:37:39 itojun Exp $");
27
28 #include <openssl/evp.h>
29
30 #include "ssh2.h"
31 #include "ssh1.h"
32 #include "xmalloc.h"
33 #include "rsa.h"
34 #include "sshpty.h"
35 #include "packet.h"
36 #include "buffer.h"
37 #include "log.h"
38 #include "servconf.h"
39 #include "compat.h"
40 #include "channels.h"
41 #include "bufaux.h"
42 #include "auth.h"
43 #include "session.h"
44 #include "dispatch.h"
45 #include "key.h"
46 #include "cipher.h"
47 #include "kex.h"
48 #include "pathnames.h"
49 #include "uidswap.h"
50 #include "auth-options.h"
51 #include "hostfile.h"
52 #include "canohost.h"
53 #include "match.h"
54 #include "monitor_wrap.h"
55 #include "atomicio.h"
56 #include "misc.h"
57
58 #ifdef GSSAPI
59 #include "ssh-gss.h"
60 #ifdef GSI
61 #include "globus_gss_assist.h"
62 char* olduser;
63 int  changeuser = 0;
64 #endif
65 #endif
66
67 /* import */
68 extern ServerOptions options;
69 extern u_char *session_id2;
70 extern int session_id2_len;
71
72 Authctxt *x_authctxt = NULL;
73 static int one = 1;
74
75 typedef struct Authmethod Authmethod;
76 struct Authmethod {
77         char    *name;
78         int     (*userauth)(Authctxt *authctxt);
79         int     *enabled;
80 };
81
82 /* protocol */
83
84 static void input_service_request(int, u_int32_t, void *);
85 static void input_userauth_request(int, u_int32_t, void *);
86
87 /* helper */
88 static Authmethod *authmethod_lookup(const char *);
89 static char *authmethods_get(void);
90 int user_key_allowed(struct passwd *, Key *);
91 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
92
93 /* auth */
94 static void userauth_banner(void);
95 static int userauth_none(Authctxt *);
96 static int userauth_passwd(Authctxt *);
97 static int userauth_pubkey(Authctxt *);
98 static int userauth_hostbased(Authctxt *);
99 static int userauth_kbdint(Authctxt *);
100
101 #ifdef GSSAPI
102 int     userauth_external(Authctxt *authctxt);
103 int     userauth_gssapi(Authctxt *authctxt);
104 #endif
105
106 Authmethod authmethods[] = {
107         {"none",
108                 userauth_none,
109                 &one},
110 #ifdef GSSAPI
111         {"external-keyx",
112                 userauth_external,
113                 &options.gss_authentication},
114         {"gssapi",
115                 userauth_gssapi,
116                 &options.gss_authentication},
117 #endif
118         {"publickey",
119                 userauth_pubkey,
120                 &options.pubkey_authentication},
121         {"password",
122                 userauth_passwd,
123                 &options.password_authentication},
124         {"keyboard-interactive",
125                 userauth_kbdint,
126                 &options.kbd_interactive_authentication},
127         {"hostbased",
128                 userauth_hostbased,
129                 &options.hostbased_authentication},
130         {NULL, NULL, NULL}
131 };
132
133 /*
134  * loop until authctxt->success == TRUE
135  */
136
137 Authctxt *
138 do_authentication2(void)
139 {
140         Authctxt *authctxt = authctxt_new();
141
142         x_authctxt = authctxt;          /*XXX*/
143
144         /* challenge-response is implemented via keyboard interactive */
145         if (options.challenge_response_authentication)
146                 options.kbd_interactive_authentication = 1;
147         if (options.pam_authentication_via_kbd_int)
148                 options.kbd_interactive_authentication = 1;
149         if (use_privsep)
150                 options.pam_authentication_via_kbd_int = 0;
151
152         dispatch_init(&dispatch_protocol_error);
153         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
154         dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
155
156         return (authctxt);
157 }
158
159 static void
160 input_service_request(int type, u_int32_t seq, void *ctxt)
161 {
162         Authctxt *authctxt = ctxt;
163         u_int len;
164         int accept = 0;
165         char *service = packet_get_string(&len);
166         packet_check_eom();
167
168         if (authctxt == NULL)
169                 fatal("input_service_request: no authctxt");
170
171         if (strcmp(service, "ssh-userauth") == 0) {
172                 if (!authctxt->success) {
173                         accept = 1;
174                         /* now we can handle user-auth requests */
175                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
176                 }
177         }
178         /* XXX all other service requests are denied */
179
180         if (accept) {
181                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
182                 packet_put_cstring(service);
183                 packet_send();
184                 packet_write_wait();
185         } else {
186                 debug("bad service request %s", service);
187                 packet_disconnect("bad service request %s", service);
188         }
189         xfree(service);
190 }
191
192 static void
193 input_userauth_request(int type, u_int32_t seq, void *ctxt)
194 {
195         Authctxt *authctxt = ctxt;
196         Authmethod *m = NULL;
197         char *user, *service, *method, *style = NULL;
198         int authenticated = 0;
199
200         if (authctxt == NULL)
201                 fatal("input_userauth_request: no authctxt");
202
203         user = packet_get_string(NULL);
204         service = packet_get_string(NULL);
205         method = packet_get_string(NULL);
206
207 #ifdef GSSAPI
208 #ifdef GSI
209         if(changeuser == 0 && (strcmp(method,"external-keyx") == 0 || strcmp(method,"gssapi") ==0) && strcmp(user,"") == 0) {
210                 char *gridmapped_name = NULL;
211                 struct passwd *pw = NULL;
212                 if(globus_gss_assist_gridmap(gssapi_client_name.value,
213                                      &gridmapped_name) == 0) {
214                         user = gridmapped_name;
215                         debug("I gridmapped and got %s", user);
216                         pw = getpwnam(user);
217                         if (pw && allowed_user(pw)) {
218                                 olduser = authctxt->user;
219                                 authctxt->user = xstrdup(user);
220                                 authctxt->pw = pwcopy(pw);
221                                 authctxt->valid = 1;
222                                 changeuser = 1;
223                         }
224
225                 } else {
226                     debug("I gridmapped and got null, reverting to %s",
227                           authctxt->user);
228                     xfree(user);
229                     user = xstrdup(authctxt->user);
230                 }
231         }
232         else if(changeuser) {
233                 struct passwd *pw = NULL;
234                 pw = getpwnam(user);
235                 if (pw && allowed_user(pw)) {
236                         xfree(authctxt->user);
237                         authctxt->user = olduser;
238                         authctxt->pw = pwcopy(pw);
239                         authctxt->valid = 1;
240                         changeuser = 0;
241                 }
242         }
243
244 #endif  /* GSI */
245 #endif /* GSSAPI */
246
247         debug("userauth-request for user %s service %s method %s", user, service, method);
248         debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
249
250         if ((style = strchr(user, ':')) != NULL)
251                 *style++ = 0;
252
253         if (authctxt->attempt++ == 0) {
254                 /* setup auth context */
255                 authctxt->pw = PRIVSEP(getpwnamallow(user));
256                 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
257                         authctxt->valid = 1;
258                         debug2("input_userauth_request: setting up authctxt for %s", user);
259 #ifdef USE_PAM
260                         PRIVSEP(start_pam(authctxt->pw->pw_name));
261 #endif
262                 } else {
263                         log("input_userauth_request: illegal user %s", user);
264 #ifdef USE_PAM
265                         PRIVSEP(start_pam("NOUSER"));
266 #endif
267                 }
268                 setproctitle("%s%s", authctxt->pw ? user : "unknown",
269                     use_privsep ? " [net]" : "");
270                 authctxt->user = xstrdup(user);
271                 authctxt->service = xstrdup(service);
272                 authctxt->style = style ? xstrdup(style) : NULL;
273                 if (use_privsep)
274                         mm_inform_authserv(service, style);
275         } else if (strcmp(user, authctxt->user) != 0 ||
276             strcmp(service, authctxt->service) != 0) {
277                 packet_disconnect("Change of username or service not allowed: "
278                     "(%s,%s) -> (%s,%s)",
279                     authctxt->user, authctxt->service, user, service);
280         }
281         /* reset state */
282         auth2_challenge_stop(authctxt);
283
284 #ifdef GSSAPI
285         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
286         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
287 #endif
288
289         authctxt->postponed = 0;
290
291         /* try to authenticate user */
292         m = authmethod_lookup(method);
293         if (m != NULL) {
294                 debug2("input_userauth_request: try method %s", method);
295                 authenticated = m->userauth(authctxt);
296         }
297         userauth_finish(authctxt, authenticated, method);
298
299         xfree(service);
300         xfree(user);
301         xfree(method);
302 }
303
304 void
305 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
306 {
307         char *methods;
308
309         if (!authctxt->valid && authenticated)
310                 fatal("INTERNAL ERROR: authenticated invalid user %s",
311                     authctxt->user);
312
313         /* Special handling for root */
314         if (authenticated && authctxt->pw->pw_uid == 0 &&
315             !auth_root_allowed(method))
316                 authenticated = 0;
317
318 #ifdef USE_PAM
319         if (!use_privsep && authenticated && authctxt->user && 
320             !do_pam_account(authctxt->user, NULL))
321                 authenticated = 0;
322 #endif /* USE_PAM */
323
324         /* Log before sending the reply */
325         if (!compat20)
326         auth_log(authctxt, authenticated, method, " ssh1");
327         else
328         auth_log(authctxt, authenticated, method, " ssh2");
329
330         if (authctxt->postponed)
331                 return;
332
333         /* XXX todo: check if multiple auth methods are needed */
334         if (authenticated == 1) {
335                 /* turn off userauth */
336                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
337                 if (!compat20)
338                 packet_start(SSH_SMSG_SUCCESS);
339                 else
340                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
341                 packet_send();
342                 packet_write_wait();
343                 /* now we can break out */
344                 authctxt->success = 1;
345         } else {
346                 if (authctxt->failures++ > AUTH_FAIL_MAX) {
347 #ifdef WITH_AIXAUTHENTICATE
348                         loginfailed(authctxt->user,
349                             get_canonical_hostname(options.verify_reverse_mapping),
350                             "ssh");
351 #endif /* WITH_AIXAUTHENTICATE */
352                         packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
353                 }
354                 if (!compat20) {
355                 /*
356                  * Break out of the dispatch loop now and go back to
357                  * SSH1 code.  We need to set the 'success' flag to
358                  * break out of the loop.  Set the 'postponed' flag to
359                  * tell the SSH1 code that authentication failed.  The
360                  * SSH1 code will handle sending SSH_SMSG_FAILURE.
361                 */
362                 authctxt->success = authctxt->postponed = 1;
363                 } else {
364                 methods = authmethods_get();
365                 if (!compat20)
366                 packet_disconnect("GSSAPI authentication failed");
367                 else
368                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
369                 packet_put_cstring(methods);
370                 packet_put_char(0);     /* XXX partial success, unused */
371                 packet_send();
372                 packet_write_wait();
373                 xfree(methods);
374                 }
375         }
376 }
377
378 char *
379 auth2_read_banner(void)
380 {
381         struct stat st;
382         char *banner = NULL;
383         off_t len, n;
384         int fd;
385
386         if ((fd = open(options.banner, O_RDONLY)) == -1)
387                 return (NULL);
388         if (fstat(fd, &st) == -1) {
389                 close(fd);
390                 return (NULL);
391         }
392         len = st.st_size;
393         banner = xmalloc(len + 1);
394         n = atomicio(read, fd, banner, len);
395         close(fd);
396
397         if (n != len) {
398                 free(banner);
399                 return (NULL);
400         }
401         banner[n] = '\0';
402         
403         return (banner);
404 }
405
406 static void
407 userauth_banner(void)
408 {
409         char *banner = NULL;
410
411         if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
412                 return;
413
414         if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
415                 goto done;
416
417         packet_start(SSH2_MSG_USERAUTH_BANNER);
418         packet_put_cstring(banner);
419         packet_put_cstring("");         /* language, unused */
420         packet_send();
421         debug("userauth_banner: sent");
422 done:
423         if (banner)
424                 xfree(banner);
425         return;
426 }
427
428 static int
429 userauth_none(Authctxt *authctxt)
430 {
431         /* disable method "none", only allowed one time */
432         Authmethod *m = authmethod_lookup("none");
433         if (m != NULL)
434                 m->enabled = NULL;
435         packet_check_eom();
436         userauth_banner();
437
438         if (authctxt->valid == 0)
439                 return(0);
440
441 #ifdef HAVE_CYGWIN
442         if (check_nt_auth(1, authctxt->pw) == 0)
443                 return(0);
444 #endif
445         return PRIVSEP(auth_password(authctxt, ""));
446 }
447
448 static int
449 userauth_passwd(Authctxt *authctxt)
450 {
451         char *password;
452         int authenticated = 0;
453         int change;
454         u_int len;
455         change = packet_get_char();
456         if (change)
457                 log("password change not supported");
458         password = packet_get_string(&len);
459         packet_check_eom();
460         if (authctxt->valid &&
461 #ifdef HAVE_CYGWIN
462             check_nt_auth(1, authctxt->pw) &&
463 #endif
464             PRIVSEP(auth_password(authctxt, password)) == 1)
465                 authenticated = 1;
466         memset(password, 0, len);
467         xfree(password);
468         return authenticated;
469 }
470
471 static int
472 userauth_kbdint(Authctxt *authctxt)
473 {
474         int authenticated = 0;
475         char *lang, *devs;
476
477         lang = packet_get_string(NULL);
478         devs = packet_get_string(NULL);
479         packet_check_eom();
480
481         debug("keyboard-interactive devs %s", devs);
482
483         if (options.challenge_response_authentication)
484                 authenticated = auth2_challenge(authctxt, devs);
485
486 #ifdef USE_PAM
487         if (authenticated == 0 && options.pam_authentication_via_kbd_int)
488                 authenticated = auth2_pam(authctxt);
489 #endif
490         xfree(devs);
491         xfree(lang);
492 #ifdef HAVE_CYGWIN
493         if (check_nt_auth(0, authctxt->pw) == 0)
494                 return(0);
495 #endif
496         return authenticated;
497 }
498
499 static int
500 userauth_pubkey(Authctxt *authctxt)
501 {
502         Buffer b;
503         Key *key = NULL;
504         char *pkalg;
505         u_char *pkblob, *sig;
506         u_int alen, blen, slen;
507         int have_sig, pktype;
508         int authenticated = 0;
509
510         if (!authctxt->valid) {
511                 debug2("userauth_pubkey: disabled because of invalid user");
512                 return 0;
513         }
514         have_sig = packet_get_char();
515         if (datafellows & SSH_BUG_PKAUTH) {
516                 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
517                 /* no explicit pkalg given */
518                 pkblob = packet_get_string(&blen);
519                 buffer_init(&b);
520                 buffer_append(&b, pkblob, blen);
521                 /* so we have to extract the pkalg from the pkblob */
522                 pkalg = buffer_get_string(&b, &alen);
523                 buffer_free(&b);
524         } else {
525                 pkalg = packet_get_string(&alen);
526                 pkblob = packet_get_string(&blen);
527         }
528         pktype = key_type_from_name(pkalg);
529         if (pktype == KEY_UNSPEC) {
530                 /* this is perfectly legal */
531                 log("userauth_pubkey: unsupported public key algorithm: %s",
532                     pkalg);
533                 goto done;
534         }
535         key = key_from_blob(pkblob, blen);
536         if (key == NULL) {
537                 error("userauth_pubkey: cannot decode key: %s", pkalg);
538                 goto done;
539         }
540         if (key->type != pktype) {
541                 error("userauth_pubkey: type mismatch for decoded key "
542                     "(received %d, expected %d)", key->type, pktype);
543                 goto done;
544         }
545         if (have_sig) {
546                 sig = packet_get_string(&slen);
547                 packet_check_eom();
548                 buffer_init(&b);
549                 if (datafellows & SSH_OLD_SESSIONID) {
550                         buffer_append(&b, session_id2, session_id2_len);
551                 } else {
552                         buffer_put_string(&b, session_id2, session_id2_len);
553                 }
554                 /* reconstruct packet */
555                 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
556                 buffer_put_cstring(&b, authctxt->user);
557                 buffer_put_cstring(&b,
558                     datafellows & SSH_BUG_PKSERVICE ?
559                     "ssh-userauth" :
560                     authctxt->service);
561                 if (datafellows & SSH_BUG_PKAUTH) {
562                         buffer_put_char(&b, have_sig);
563                 } else {
564                         buffer_put_cstring(&b, "publickey");
565                         buffer_put_char(&b, have_sig);
566                         buffer_put_cstring(&b, pkalg);
567                 }
568                 buffer_put_string(&b, pkblob, blen);
569 #ifdef DEBUG_PK
570                 buffer_dump(&b);
571 #endif
572                 /* test for correct signature */
573                 authenticated = 0;
574                 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
575                     PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
576                                 buffer_len(&b))) == 1)
577                         authenticated = 1;
578                 buffer_clear(&b);
579                 xfree(sig);
580         } else {
581                 debug("test whether pkalg/pkblob are acceptable");
582                 packet_check_eom();
583
584                 /* XXX fake reply and always send PK_OK ? */
585                 /*
586                  * XXX this allows testing whether a user is allowed
587                  * to login: if you happen to have a valid pubkey this
588                  * message is sent. the message is NEVER sent at all
589                  * if a user is not allowed to login. is this an
590                  * issue? -markus
591                  */
592                 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
593                         packet_start(SSH2_MSG_USERAUTH_PK_OK);
594                         packet_put_string(pkalg, alen);
595                         packet_put_string(pkblob, blen);
596                         packet_send();
597                         packet_write_wait();
598                         authctxt->postponed = 1;
599                 }
600         }
601         if (authenticated != 1)
602                 auth_clear_options();
603 done:
604         debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
605         if (key != NULL)
606                 key_free(key);
607         xfree(pkalg);
608         xfree(pkblob);
609 #ifdef HAVE_CYGWIN
610         if (check_nt_auth(0, authctxt->pw) == 0)
611                 return(0);
612 #endif
613         return authenticated;
614 }
615
616 static int
617 userauth_hostbased(Authctxt *authctxt)
618 {
619         Buffer b;
620         Key *key = NULL;
621         char *pkalg, *cuser, *chost, *service;
622         u_char *pkblob, *sig;
623         u_int alen, blen, slen;
624         int pktype;
625         int authenticated = 0;
626
627         if (!authctxt->valid) {
628                 debug2("userauth_hostbased: disabled because of invalid user");
629                 return 0;
630         }
631         pkalg = packet_get_string(&alen);
632         pkblob = packet_get_string(&blen);
633         chost = packet_get_string(NULL);
634         cuser = packet_get_string(NULL);
635         sig = packet_get_string(&slen);
636
637         debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
638             cuser, chost, pkalg, slen);
639 #ifdef DEBUG_PK
640         debug("signature:");
641         buffer_init(&b);
642         buffer_append(&b, sig, slen);
643         buffer_dump(&b);
644         buffer_free(&b);
645 #endif
646         pktype = key_type_from_name(pkalg);
647         if (pktype == KEY_UNSPEC) {
648                 /* this is perfectly legal */
649                 log("userauth_hostbased: unsupported "
650                     "public key algorithm: %s", pkalg);
651                 goto done;
652         }
653         key = key_from_blob(pkblob, blen);
654         if (key == NULL) {
655                 error("userauth_hostbased: cannot decode key: %s", pkalg);
656                 goto done;
657         }
658         if (key->type != pktype) {
659                 error("userauth_hostbased: type mismatch for decoded key "
660                     "(received %d, expected %d)", key->type, pktype);
661                 goto done;
662         }
663         service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
664             authctxt->service;
665         buffer_init(&b);
666         buffer_put_string(&b, session_id2, session_id2_len);
667         /* reconstruct packet */
668         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
669         buffer_put_cstring(&b, authctxt->user);
670         buffer_put_cstring(&b, service);
671         buffer_put_cstring(&b, "hostbased");
672         buffer_put_string(&b, pkalg, alen);
673         buffer_put_string(&b, pkblob, blen);
674         buffer_put_cstring(&b, chost);
675         buffer_put_cstring(&b, cuser);
676 #ifdef DEBUG_PK
677         buffer_dump(&b);
678 #endif
679         /* test for allowed key and correct signature */
680         authenticated = 0;
681         if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
682             PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
683                         buffer_len(&b))) == 1)
684                 authenticated = 1;
685
686         buffer_clear(&b);
687 done:
688         debug2("userauth_hostbased: authenticated %d", authenticated);
689         if (key != NULL)
690                 key_free(key);
691         xfree(pkalg);
692         xfree(pkblob);
693         xfree(cuser);
694         xfree(chost);
695         xfree(sig);
696         return authenticated;
697 }
698
699 /* get current user */
700
701 struct passwd*
702 auth_get_user(void)
703 {
704         return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
705 }
706
707 #define DELIM   ","
708
709 static char *
710 authmethods_get(void)
711 {
712         Authmethod *method = NULL;
713         Buffer b;
714         char *list;
715
716         buffer_init(&b);
717         for (method = authmethods; method->name != NULL; method++) {
718                 if (strcmp(method->name, "none") == 0)
719                         continue;
720                 if (method->enabled != NULL && *(method->enabled) != 0) {
721                         if (buffer_len(&b) > 0)
722                                 buffer_append(&b, ",", 1);
723                         buffer_append(&b, method->name, strlen(method->name));
724                 }
725         }
726         buffer_append(&b, "\0", 1);
727         list = xstrdup(buffer_ptr(&b));
728         buffer_free(&b);
729         return list;
730 }
731
732 static Authmethod *
733 authmethod_lookup(const char *name)
734 {
735         Authmethod *method = NULL;
736         if (name != NULL)
737                 for (method = authmethods; method->name != NULL; method++)
738                         if (method->enabled != NULL &&
739                             *(method->enabled) != 0 &&
740                             strcmp(name, method->name) == 0)
741                                 return method;
742         debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
743         return NULL;
744 }
745
746 /* return 1 if user allows given key */
747 static int
748 user_key_allowed2(struct passwd *pw, Key *key, char *file)
749 {
750         char line[8192];
751         int found_key = 0;
752         FILE *f;
753         u_long linenum = 0;
754         struct stat st;
755         Key *found;
756         char *fp;
757
758         if (pw == NULL)
759                 return 0;
760
761         /* Temporarily use the user's uid. */
762         temporarily_use_uid(pw);
763
764         debug("trying public key file %s", file);
765
766         /* Fail quietly if file does not exist */
767         if (stat(file, &st) < 0) {
768                 /* Restore the privileged uid. */
769                 restore_uid();
770                 return 0;
771         }
772         /* Open the file containing the authorized keys. */
773         f = fopen(file, "r");
774         if (!f) {
775                 /* Restore the privileged uid. */
776                 restore_uid();
777                 return 0;
778         }
779         if (options.strict_modes &&
780             secure_filename(f, file, pw, line, sizeof(line)) != 0) {
781                 fclose(f);
782                 log("Authentication refused: %s", line);
783                 restore_uid();
784                 return 0;
785         }
786
787         found_key = 0;
788         found = key_new(key->type);
789
790         while (fgets(line, sizeof(line), f)) {
791                 char *cp, *options = NULL;
792                 linenum++;
793                 /* Skip leading whitespace, empty and comment lines. */
794                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
795                         ;
796                 if (!*cp || *cp == '\n' || *cp == '#')
797                         continue;
798
799                 if (key_read(found, &cp) != 1) {
800                         /* no key?  check if there are options for this key */
801                         int quoted = 0;
802                         debug2("user_key_allowed: check options: '%s'", cp);
803                         options = cp;
804                         for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
805                                 if (*cp == '\\' && cp[1] == '"')
806                                         cp++;   /* Skip both */
807                                 else if (*cp == '"')
808                                         quoted = !quoted;
809                         }
810                         /* Skip remaining whitespace. */
811                         for (; *cp == ' ' || *cp == '\t'; cp++)
812                                 ;
813                         if (key_read(found, &cp) != 1) {
814                                 debug2("user_key_allowed: advance: '%s'", cp);
815                                 /* still no key?  advance to next line*/
816                                 continue;
817                         }
818                 }
819                 if (key_equal(found, key) &&
820                     auth_parse_options(pw, options, file, linenum) == 1) {
821                         found_key = 1;
822                         debug("matching key found: file %s, line %lu",
823                             file, linenum);
824                         fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
825                         verbose("Found matching %s key: %s",
826                             key_type(found), fp);
827                         xfree(fp);
828                         break;
829                 }
830         }
831         restore_uid();
832         fclose(f);
833         key_free(found);
834         if (!found_key)
835                 debug2("key not found");
836         return found_key;
837 }
838
839 /* check whether given key is in .ssh/authorized_keys* */
840 int
841 user_key_allowed(struct passwd *pw, Key *key)
842 {
843         int success;
844         char *file;
845
846         file = authorized_keys_file(pw);
847         success = user_key_allowed2(pw, key, file);
848         xfree(file);
849         if (success)
850                 return success;
851
852         /* try suffix "2" for backward compat, too */
853         file = authorized_keys_file2(pw);
854         success = user_key_allowed2(pw, key, file);
855         xfree(file);
856         return success;
857 }
858
859 /* return 1 if given hostkey is allowed */
860 int
861 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
862     Key *key)
863 {
864         const char *resolvedname, *ipaddr, *lookup;
865         HostStatus host_status;
866         int len;
867
868         resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
869         ipaddr = get_remote_ipaddr();
870
871         debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
872             chost, resolvedname, ipaddr);
873
874         if (options.hostbased_uses_name_from_packet_only) {
875                 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
876                         return 0;
877                 lookup = chost;
878         } else {
879                 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
880                         debug2("stripping trailing dot from chost %s", chost);
881                         chost[len - 1] = '\0';
882                 }
883                 if (strcasecmp(resolvedname, chost) != 0)
884                         log("userauth_hostbased mismatch: "
885                             "client sends %s, but we resolve %s to %s",
886                             chost, ipaddr, resolvedname);
887                 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
888                         return 0;
889                 lookup = resolvedname;
890         }
891         debug2("userauth_hostbased: access allowed by auth_rhosts2");
892
893         host_status = check_key_in_hostfiles(pw, key, lookup,
894             _PATH_SSH_SYSTEM_HOSTFILE,
895             options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
896
897         /* backward compat if no key has been found. */
898         if (host_status == HOST_NEW)
899                 host_status = check_key_in_hostfiles(pw, key, lookup,
900                     _PATH_SSH_SYSTEM_HOSTFILE2,
901                     options.ignore_user_known_hosts ? NULL :
902                     _PATH_SSH_USER_HOSTFILE2);
903
904         return (host_status == HOST_OK);
905 }
This page took 0.138193 seconds and 5 git commands to generate.