2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: auth2.c,v 1.89 2002/03/19 14:27:39 markus Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
53 #include "monitor_wrap.h"
56 extern ServerOptions options;
57 extern u_char *session_id2;
58 extern int session_id2_len;
60 Authctxt *x_authctxt = NULL;
63 typedef struct Authmethod Authmethod;
66 int (*userauth)(Authctxt *authctxt);
72 static void input_service_request(int, u_int32_t, void *);
73 static void input_userauth_request(int, u_int32_t, void *);
76 static Authmethod *authmethod_lookup(const char *);
77 static char *authmethods_get(void);
78 int user_key_allowed(struct passwd *, Key *);
79 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
82 static void userauth_banner(void);
83 static int userauth_none(Authctxt *);
84 static int userauth_passwd(Authctxt *);
85 static int userauth_pubkey(Authctxt *);
86 static int userauth_hostbased(Authctxt *);
87 static int userauth_kbdint(Authctxt *);
89 Authmethod authmethods[] = {
95 &options.pubkey_authentication},
98 &options.password_authentication},
99 {"keyboard-interactive",
101 &options.kbd_interactive_authentication},
104 &options.hostbased_authentication},
109 * loop until authctxt->success == TRUE
113 do_authentication2(void)
115 Authctxt *authctxt = authctxt_new();
117 x_authctxt = authctxt; /*XXX*/
119 /* challenge-response is implemented via keyboard interactive */
120 if (options.challenge_response_authentication)
121 options.kbd_interactive_authentication = 1;
122 if (options.pam_authentication_via_kbd_int)
123 options.kbd_interactive_authentication = 1;
125 dispatch_init(&dispatch_protocol_error);
126 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
127 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
133 input_service_request(int type, u_int32_t seq, void *ctxt)
135 Authctxt *authctxt = ctxt;
138 char *service = packet_get_string(&len);
141 if (authctxt == NULL)
142 fatal("input_service_request: no authctxt");
144 if (strcmp(service, "ssh-userauth") == 0) {
145 if (!authctxt->success) {
147 /* now we can handle user-auth requests */
148 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
151 /* XXX all other service requests are denied */
154 packet_start(SSH2_MSG_SERVICE_ACCEPT);
155 packet_put_cstring(service);
159 debug("bad service request %s", service);
160 packet_disconnect("bad service request %s", service);
166 input_userauth_request(int type, u_int32_t seq, void *ctxt)
168 Authctxt *authctxt = ctxt;
169 Authmethod *m = NULL;
170 char *user, *service, *method, *style = NULL;
171 int authenticated = 0;
173 if (authctxt == NULL)
174 fatal("input_userauth_request: no authctxt");
176 user = packet_get_string(NULL);
177 service = packet_get_string(NULL);
178 method = packet_get_string(NULL);
179 debug("userauth-request for user %s service %s method %s", user, service, method);
180 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
182 if ((style = strchr(user, ':')) != NULL)
185 if (authctxt->attempt++ == 0) {
186 /* setup auth context */
187 authctxt->pw = PRIVSEP(getpwnamallow(user));
188 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
190 debug2("input_userauth_request: setting up authctxt for %s", user);
192 start_pam(authctxt->pw->pw_name);
195 log("input_userauth_request: illegal user %s", user);
200 setproctitle("%s%s", authctxt->pw ? user : "unknown",
201 use_privsep ? " [net]" : "");
202 authctxt->user = xstrdup(user);
203 authctxt->service = xstrdup(service);
204 authctxt->style = style ? xstrdup(style) : NULL;
206 mm_inform_authserv(service, style);
207 } else if (strcmp(user, authctxt->user) != 0 ||
208 strcmp(service, authctxt->service) != 0) {
209 packet_disconnect("Change of username or service not allowed: "
210 "(%s,%s) -> (%s,%s)",
211 authctxt->user, authctxt->service, user, service);
214 auth2_challenge_stop(authctxt);
215 authctxt->postponed = 0;
217 /* try to authenticate user */
218 m = authmethod_lookup(method);
220 debug2("input_userauth_request: try method %s", method);
221 authenticated = m->userauth(authctxt);
223 userauth_finish(authctxt, authenticated, method);
231 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
235 if (!authctxt->valid && authenticated)
236 fatal("INTERNAL ERROR: authenticated invalid user %s",
239 /* Special handling for root */
240 if (authenticated && authctxt->pw->pw_uid == 0 &&
241 !auth_root_allowed(method))
245 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
250 /* Log before sending the reply */
251 auth_log(authctxt, authenticated, method, " ssh2");
253 if (authctxt->postponed)
256 /* XXX todo: check if multiple auth methods are needed */
257 if (authenticated == 1) {
258 /* turn off userauth */
259 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
260 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
263 /* now we can break out */
264 authctxt->success = 1;
266 if (authctxt->failures++ > AUTH_FAIL_MAX) {
267 #ifdef WITH_AIXAUTHENTICATE
268 loginfailed(authctxt->user,
269 get_canonical_hostname(options.verify_reverse_mapping),
271 #endif /* WITH_AIXAUTHENTICATE */
272 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
274 methods = authmethods_get();
275 packet_start(SSH2_MSG_USERAUTH_FAILURE);
276 packet_put_cstring(methods);
277 packet_put_char(0); /* XXX partial success, unused */
285 userauth_banner(void)
292 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
294 if ((fd = open(options.banner, O_RDONLY)) < 0)
296 if (fstat(fd, &st) < 0)
299 banner = xmalloc(len + 1);
300 if ((n = read(fd, banner, len)) < 0)
303 packet_start(SSH2_MSG_USERAUTH_BANNER);
304 packet_put_cstring(banner);
305 packet_put_cstring(""); /* language, unused */
307 debug("userauth_banner: sent");
316 userauth_none(Authctxt *authctxt)
318 /* disable method "none", only allowed one time */
319 Authmethod *m = authmethod_lookup("none");
325 if (authctxt->valid == 0)
329 if (check_nt_auth(1, authctxt->pw) == 0)
333 return auth_pam_password(authctxt->pw, "");
334 #elif defined(HAVE_OSF_SIA)
336 #else /* !HAVE_OSF_SIA && !USE_PAM */
337 return PRIVSEP(auth_password(authctxt, ""));
342 userauth_passwd(Authctxt *authctxt)
345 int authenticated = 0;
348 change = packet_get_char();
350 log("password change not supported");
351 password = packet_get_string(&len);
353 if (authctxt->valid &&
355 check_nt_auth(1, authctxt->pw) &&
358 auth_pam_password(authctxt->pw, password) == 1)
359 #elif defined(HAVE_OSF_SIA)
360 auth_sia_password(authctxt->user, password) == 1)
361 #else /* !USE_PAM && !HAVE_OSF_SIA */
362 PRIVSEP(auth_password(authctxt, password)) == 1)
365 memset(password, 0, len);
367 return authenticated;
371 userauth_kbdint(Authctxt *authctxt)
373 int authenticated = 0;
376 lang = packet_get_string(NULL);
377 devs = packet_get_string(NULL);
380 debug("keyboard-interactive devs %s", devs);
382 if (options.challenge_response_authentication)
383 authenticated = auth2_challenge(authctxt, devs);
386 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
387 authenticated = auth2_pam(authctxt);
392 if (check_nt_auth(0, authctxt->pw) == 0)
395 return authenticated;
399 userauth_pubkey(Authctxt *authctxt)
404 u_char *pkblob, *sig;
405 u_int alen, blen, slen;
406 int have_sig, pktype;
407 int authenticated = 0;
409 if (!authctxt->valid) {
410 debug2("userauth_pubkey: disabled because of invalid user");
413 have_sig = packet_get_char();
414 if (datafellows & SSH_BUG_PKAUTH) {
415 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
416 /* no explicit pkalg given */
417 pkblob = packet_get_string(&blen);
419 buffer_append(&b, pkblob, blen);
420 /* so we have to extract the pkalg from the pkblob */
421 pkalg = buffer_get_string(&b, &alen);
424 pkalg = packet_get_string(&alen);
425 pkblob = packet_get_string(&blen);
427 pktype = key_type_from_name(pkalg);
428 if (pktype == KEY_UNSPEC) {
429 /* this is perfectly legal */
430 log("userauth_pubkey: unsupported public key algorithm: %s",
434 key = key_from_blob(pkblob, blen);
436 error("userauth_pubkey: cannot decode key: %s", pkalg);
439 if (key->type != pktype) {
440 error("userauth_pubkey: type mismatch for decoded key "
441 "(received %d, expected %d)", key->type, pktype);
445 sig = packet_get_string(&slen);
448 if (datafellows & SSH_OLD_SESSIONID) {
449 buffer_append(&b, session_id2, session_id2_len);
451 buffer_put_string(&b, session_id2, session_id2_len);
453 /* reconstruct packet */
454 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
455 buffer_put_cstring(&b, authctxt->user);
456 buffer_put_cstring(&b,
457 datafellows & SSH_BUG_PKSERVICE ?
460 if (datafellows & SSH_BUG_PKAUTH) {
461 buffer_put_char(&b, have_sig);
463 buffer_put_cstring(&b, "publickey");
464 buffer_put_char(&b, have_sig);
465 buffer_put_cstring(&b, pkalg);
467 buffer_put_string(&b, pkblob, blen);
471 /* test for correct signature */
473 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
474 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
475 buffer_len(&b))) == 1)
480 debug("test whether pkalg/pkblob are acceptable");
483 /* XXX fake reply and always send PK_OK ? */
485 * XXX this allows testing whether a user is allowed
486 * to login: if you happen to have a valid pubkey this
487 * message is sent. the message is NEVER sent at all
488 * if a user is not allowed to login. is this an
491 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
492 packet_start(SSH2_MSG_USERAUTH_PK_OK);
493 packet_put_string(pkalg, alen);
494 packet_put_string(pkblob, blen);
497 authctxt->postponed = 1;
500 if (authenticated != 1)
501 auth_clear_options();
503 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
509 if (check_nt_auth(0, authctxt->pw) == 0)
512 return authenticated;
516 userauth_hostbased(Authctxt *authctxt)
520 char *pkalg, *cuser, *chost, *service;
521 u_char *pkblob, *sig;
522 u_int alen, blen, slen;
524 int authenticated = 0;
526 if (!authctxt->valid) {
527 debug2("userauth_hostbased: disabled because of invalid user");
530 pkalg = packet_get_string(&alen);
531 pkblob = packet_get_string(&blen);
532 chost = packet_get_string(NULL);
533 cuser = packet_get_string(NULL);
534 sig = packet_get_string(&slen);
536 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
537 cuser, chost, pkalg, slen);
541 buffer_append(&b, sig, slen);
545 pktype = key_type_from_name(pkalg);
546 if (pktype == KEY_UNSPEC) {
547 /* this is perfectly legal */
548 log("userauth_hostbased: unsupported "
549 "public key algorithm: %s", pkalg);
552 key = key_from_blob(pkblob, blen);
554 error("userauth_hostbased: cannot decode key: %s", pkalg);
557 if (key->type != pktype) {
558 error("userauth_hostbased: type mismatch for decoded key "
559 "(received %d, expected %d)", key->type, pktype);
562 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
565 buffer_put_string(&b, session_id2, session_id2_len);
566 /* reconstruct packet */
567 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
568 buffer_put_cstring(&b, authctxt->user);
569 buffer_put_cstring(&b, service);
570 buffer_put_cstring(&b, "hostbased");
571 buffer_put_string(&b, pkalg, alen);
572 buffer_put_string(&b, pkblob, blen);
573 buffer_put_cstring(&b, chost);
574 buffer_put_cstring(&b, cuser);
578 /* test for allowed key and correct signature */
580 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
581 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
582 buffer_len(&b))) == 1)
587 debug2("userauth_hostbased: authenticated %d", authenticated);
595 return authenticated;
598 /* get current user */
603 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
609 authmethods_get(void)
611 Authmethod *method = NULL;
616 for (method = authmethods; method->name != NULL; method++) {
617 if (strcmp(method->name, "none") == 0)
619 if (method->enabled != NULL && *(method->enabled) != 0) {
620 if (buffer_len(&b) > 0)
621 buffer_append(&b, ",", 1);
622 buffer_append(&b, method->name, strlen(method->name));
625 buffer_append(&b, "\0", 1);
626 list = xstrdup(buffer_ptr(&b));
632 authmethod_lookup(const char *name)
634 Authmethod *method = NULL;
636 for (method = authmethods; method->name != NULL; method++)
637 if (method->enabled != NULL &&
638 *(method->enabled) != 0 &&
639 strcmp(name, method->name) == 0)
641 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
645 /* return 1 if user allows given key */
647 user_key_allowed2(struct passwd *pw, Key *key, char *file)
660 /* Temporarily use the user's uid. */
661 temporarily_use_uid(pw);
663 debug("trying public key file %s", file);
665 /* Fail quietly if file does not exist */
666 if (stat(file, &st) < 0) {
667 /* Restore the privileged uid. */
671 /* Open the file containing the authorized keys. */
672 f = fopen(file, "r");
674 /* Restore the privileged uid. */
678 if (options.strict_modes &&
679 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
681 log("Authentication refused: %s", line);
687 found = key_new(key->type);
689 while (fgets(line, sizeof(line), f)) {
690 char *cp, *options = NULL;
692 /* Skip leading whitespace, empty and comment lines. */
693 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
695 if (!*cp || *cp == '\n' || *cp == '#')
698 if (key_read(found, &cp) != 1) {
699 /* no key? check if there are options for this key */
701 debug2("user_key_allowed: check options: '%s'", cp);
703 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
704 if (*cp == '\\' && cp[1] == '"')
705 cp++; /* Skip both */
709 /* Skip remaining whitespace. */
710 for (; *cp == ' ' || *cp == '\t'; cp++)
712 if (key_read(found, &cp) != 1) {
713 debug2("user_key_allowed: advance: '%s'", cp);
714 /* still no key? advance to next line*/
718 if (key_equal(found, key) &&
719 auth_parse_options(pw, options, file, linenum) == 1) {
721 debug("matching key found: file %s, line %lu",
723 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
724 verbose("Found matching %s key: %s",
725 key_type(found), fp);
734 debug2("key not found");
738 /* check whether given key is in .ssh/authorized_keys* */
740 user_key_allowed(struct passwd *pw, Key *key)
745 file = authorized_keys_file(pw);
746 success = user_key_allowed2(pw, key, file);
751 /* try suffix "2" for backward compat, too */
752 file = authorized_keys_file2(pw);
753 success = user_key_allowed2(pw, key, file);
758 /* return 1 if given hostkey is allowed */
760 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
763 const char *resolvedname, *ipaddr, *lookup;
764 HostStatus host_status;
767 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
768 ipaddr = get_remote_ipaddr();
770 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
771 chost, resolvedname, ipaddr);
773 if (options.hostbased_uses_name_from_packet_only) {
774 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
778 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
779 debug2("stripping trailing dot from chost %s", chost);
780 chost[len - 1] = '\0';
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)
788 lookup = resolvedname;
790 debug2("userauth_hostbased: access allowed by auth_rhosts2");
792 host_status = check_key_in_hostfiles(pw, key, lookup,
793 _PATH_SSH_SYSTEM_HOSTFILE,
794 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
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,
800 options.ignore_user_known_hosts ? NULL :
801 _PATH_SSH_USER_HOSTFILE2);
803 return (host_status == HOST_OK);