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