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