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