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