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