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