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