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