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