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.91 2002/05/13 02:37:39 itojun Exp $");
28 #include <openssl/evp.h>
48 #include "pathnames.h"
50 #include "auth-options.h"
54 #include "monitor_wrap.h"
61 #include "globus_gss_assist.h"
68 extern ServerOptions options;
69 extern u_char *session_id2;
70 extern int session_id2_len;
72 Authctxt *x_authctxt = NULL;
75 typedef struct Authmethod Authmethod;
78 int (*userauth)(Authctxt *authctxt);
84 static void input_service_request(int, u_int32_t, void *);
85 static void input_userauth_request(int, u_int32_t, void *);
88 static Authmethod *authmethod_lookup(const char *);
89 static char *authmethods_get(void);
90 int user_key_allowed(struct passwd *, Key *);
91 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
94 static void userauth_banner(void);
95 static int userauth_none(Authctxt *);
96 static int userauth_passwd(Authctxt *);
97 static int userauth_pubkey(Authctxt *);
98 static int userauth_hostbased(Authctxt *);
99 static int userauth_kbdint(Authctxt *);
102 int userauth_external(Authctxt *authctxt);
103 int userauth_gssapi(Authctxt *authctxt);
106 Authmethod authmethods[] = {
113 &options.gss_authentication},
116 &options.gss_authentication},
120 &options.pubkey_authentication},
123 &options.password_authentication},
124 {"keyboard-interactive",
126 &options.kbd_interactive_authentication},
129 &options.hostbased_authentication},
134 * loop until authctxt->success == TRUE
138 do_authentication2(void)
140 Authctxt *authctxt = authctxt_new();
142 x_authctxt = authctxt; /*XXX*/
144 /* challenge-response is implemented via keyboard interactive */
145 if (options.challenge_response_authentication)
146 options.kbd_interactive_authentication = 1;
147 if (options.pam_authentication_via_kbd_int)
148 options.kbd_interactive_authentication = 1;
150 options.pam_authentication_via_kbd_int = 0;
152 dispatch_init(&dispatch_protocol_error);
153 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
154 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
160 input_service_request(int type, u_int32_t seq, void *ctxt)
162 Authctxt *authctxt = ctxt;
165 char *service = packet_get_string(&len);
168 if (authctxt == NULL)
169 fatal("input_service_request: no authctxt");
171 if (strcmp(service, "ssh-userauth") == 0) {
172 if (!authctxt->success) {
174 /* now we can handle user-auth requests */
175 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
178 /* XXX all other service requests are denied */
181 packet_start(SSH2_MSG_SERVICE_ACCEPT);
182 packet_put_cstring(service);
186 debug("bad service request %s", service);
187 packet_disconnect("bad service request %s", service);
193 input_userauth_request(int type, u_int32_t seq, void *ctxt)
195 Authctxt *authctxt = ctxt;
196 Authmethod *m = NULL;
197 char *user, *service, *method, *style = NULL;
198 int authenticated = 0;
200 if (authctxt == NULL)
201 fatal("input_userauth_request: no authctxt");
203 user = packet_get_string(NULL);
204 service = packet_get_string(NULL);
205 method = packet_get_string(NULL);
209 if(changeuser == 0 && (strcmp(method,"external-keyx") == 0 || strcmp(method,"gssapi") ==0) && strcmp(user,"") == 0) {
210 char *gridmapped_name = NULL;
211 struct passwd *pw = NULL;
212 if(globus_gss_assist_gridmap(gssapi_client_name.value,
213 &gridmapped_name) == 0) {
214 user = gridmapped_name;
215 debug("I gridmapped and got %s", user);
217 if (pw && allowed_user(pw)) {
218 olduser = authctxt->user;
219 authctxt->user = user;
220 authctxt->pw = pwcopy(pw);
226 debug("I gridmapped and got null, reverting to %s",
229 user = xstrdup(authctxt->user);
232 else if(changeuser) {
233 struct passwd *pw = NULL;
235 if (pw && allowed_user(pw)) {
236 authctxt->user = olduser;
237 authctxt->pw = pwcopy(pw);
246 debug("userauth-request for user %s service %s method %s", user, service, method);
247 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
249 if ((style = strchr(user, ':')) != NULL)
252 if (authctxt->attempt++ == 0) {
253 /* setup auth context */
254 authctxt->pw = PRIVSEP(getpwnamallow(user));
255 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
257 debug2("input_userauth_request: setting up authctxt for %s", user);
259 PRIVSEP(start_pam(authctxt->pw->pw_name));
262 log("input_userauth_request: illegal user %s", user);
264 PRIVSEP(start_pam("NOUSER"));
267 setproctitle("%s%s", authctxt->pw ? user : "unknown",
268 use_privsep ? " [net]" : "");
269 authctxt->user = xstrdup(user);
270 authctxt->service = xstrdup(service);
271 authctxt->style = style ? xstrdup(style) : NULL;
273 mm_inform_authserv(service, style);
274 } else if (strcmp(user, authctxt->user) != 0 ||
275 strcmp(service, authctxt->service) != 0) {
276 packet_disconnect("Change of username or service not allowed: "
277 "(%s,%s) -> (%s,%s)",
278 authctxt->user, authctxt->service, user, service);
281 auth2_challenge_stop(authctxt);
284 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
285 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
288 authctxt->postponed = 0;
290 /* try to authenticate user */
291 m = authmethod_lookup(method);
293 debug2("input_userauth_request: try method %s", method);
294 authenticated = m->userauth(authctxt);
296 userauth_finish(authctxt, authenticated, method);
304 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
308 if (!authctxt->valid && authenticated)
309 fatal("INTERNAL ERROR: authenticated invalid user %s",
312 /* Special handling for root */
313 if (authenticated && authctxt->pw->pw_uid == 0 &&
314 !auth_root_allowed(method))
318 if (!use_privsep && authenticated && authctxt->user &&
319 !do_pam_account(authctxt->user, NULL))
323 /* Log before sending the reply */
325 auth_log(authctxt, authenticated, method, " ssh1");
327 auth_log(authctxt, authenticated, method, " ssh2");
329 if (authctxt->postponed)
332 /* XXX todo: check if multiple auth methods are needed */
333 if (authenticated == 1) {
334 /* turn off userauth */
335 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
337 packet_start(SSH_SMSG_SUCCESS);
339 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
342 /* now we can break out */
343 authctxt->success = 1;
345 if (authctxt->failures++ > AUTH_FAIL_MAX) {
346 #ifdef WITH_AIXAUTHENTICATE
347 loginfailed(authctxt->user,
348 get_canonical_hostname(options.verify_reverse_mapping),
350 #endif /* WITH_AIXAUTHENTICATE */
351 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
355 * Break out of the dispatch loop now and go back to
356 * SSH1 code. We need to set the 'success' flag to
357 * break out of the loop. Set the 'postponed' flag to
358 * tell the SSH1 code that authentication failed. The
359 * SSH1 code will handle sending SSH_SMSG_FAILURE.
361 authctxt->success = authctxt->postponed = 1;
363 methods = authmethods_get();
364 packet_start(SSH2_MSG_USERAUTH_FAILURE);
365 packet_put_cstring(methods);
366 packet_put_char(0); /* XXX partial success, unused */
375 auth2_read_banner(void)
382 if ((fd = open(options.banner, O_RDONLY)) == -1)
384 if (fstat(fd, &st) == -1) {
389 banner = xmalloc(len + 1);
390 n = atomicio(read, fd, banner, len);
403 userauth_banner(void)
407 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
410 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
413 packet_start(SSH2_MSG_USERAUTH_BANNER);
414 packet_put_cstring(banner);
415 packet_put_cstring(""); /* language, unused */
417 debug("userauth_banner: sent");
425 userauth_none(Authctxt *authctxt)
427 /* disable method "none", only allowed one time */
428 Authmethod *m = authmethod_lookup("none");
434 if (authctxt->valid == 0)
438 if (check_nt_auth(1, authctxt->pw) == 0)
441 return PRIVSEP(auth_password(authctxt, ""));
445 userauth_passwd(Authctxt *authctxt)
448 int authenticated = 0;
451 change = packet_get_char();
453 log("password change not supported");
454 password = packet_get_string(&len);
456 if (authctxt->valid &&
458 check_nt_auth(1, authctxt->pw) &&
460 PRIVSEP(auth_password(authctxt, password)) == 1)
462 memset(password, 0, len);
464 return authenticated;
468 userauth_kbdint(Authctxt *authctxt)
470 int authenticated = 0;
473 lang = packet_get_string(NULL);
474 devs = packet_get_string(NULL);
477 debug("keyboard-interactive devs %s", devs);
479 if (options.challenge_response_authentication)
480 authenticated = auth2_challenge(authctxt, devs);
483 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
484 authenticated = auth2_pam(authctxt);
489 if (check_nt_auth(0, authctxt->pw) == 0)
492 return authenticated;
496 userauth_pubkey(Authctxt *authctxt)
501 u_char *pkblob, *sig;
502 u_int alen, blen, slen;
503 int have_sig, pktype;
504 int authenticated = 0;
506 if (!authctxt->valid) {
507 debug2("userauth_pubkey: disabled because of invalid user");
510 have_sig = packet_get_char();
511 if (datafellows & SSH_BUG_PKAUTH) {
512 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
513 /* no explicit pkalg given */
514 pkblob = packet_get_string(&blen);
516 buffer_append(&b, pkblob, blen);
517 /* so we have to extract the pkalg from the pkblob */
518 pkalg = buffer_get_string(&b, &alen);
521 pkalg = packet_get_string(&alen);
522 pkblob = packet_get_string(&blen);
524 pktype = key_type_from_name(pkalg);
525 if (pktype == KEY_UNSPEC) {
526 /* this is perfectly legal */
527 log("userauth_pubkey: unsupported public key algorithm: %s",
531 key = key_from_blob(pkblob, blen);
533 error("userauth_pubkey: cannot decode key: %s", pkalg);
536 if (key->type != pktype) {
537 error("userauth_pubkey: type mismatch for decoded key "
538 "(received %d, expected %d)", key->type, pktype);
542 sig = packet_get_string(&slen);
545 if (datafellows & SSH_OLD_SESSIONID) {
546 buffer_append(&b, session_id2, session_id2_len);
548 buffer_put_string(&b, session_id2, session_id2_len);
550 /* reconstruct packet */
551 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
552 buffer_put_cstring(&b, authctxt->user);
553 buffer_put_cstring(&b,
554 datafellows & SSH_BUG_PKSERVICE ?
557 if (datafellows & SSH_BUG_PKAUTH) {
558 buffer_put_char(&b, have_sig);
560 buffer_put_cstring(&b, "publickey");
561 buffer_put_char(&b, have_sig);
562 buffer_put_cstring(&b, pkalg);
564 buffer_put_string(&b, pkblob, blen);
568 /* test for correct signature */
570 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
571 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
572 buffer_len(&b))) == 1)
577 debug("test whether pkalg/pkblob are acceptable");
580 /* XXX fake reply and always send PK_OK ? */
582 * XXX this allows testing whether a user is allowed
583 * to login: if you happen to have a valid pubkey this
584 * message is sent. the message is NEVER sent at all
585 * if a user is not allowed to login. is this an
588 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
589 packet_start(SSH2_MSG_USERAUTH_PK_OK);
590 packet_put_string(pkalg, alen);
591 packet_put_string(pkblob, blen);
594 authctxt->postponed = 1;
597 if (authenticated != 1)
598 auth_clear_options();
600 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
606 if (check_nt_auth(0, authctxt->pw) == 0)
609 return authenticated;
613 userauth_hostbased(Authctxt *authctxt)
617 char *pkalg, *cuser, *chost, *service;
618 u_char *pkblob, *sig;
619 u_int alen, blen, slen;
621 int authenticated = 0;
623 if (!authctxt->valid) {
624 debug2("userauth_hostbased: disabled because of invalid user");
627 pkalg = packet_get_string(&alen);
628 pkblob = packet_get_string(&blen);
629 chost = packet_get_string(NULL);
630 cuser = packet_get_string(NULL);
631 sig = packet_get_string(&slen);
633 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
634 cuser, chost, pkalg, slen);
638 buffer_append(&b, sig, slen);
642 pktype = key_type_from_name(pkalg);
643 if (pktype == KEY_UNSPEC) {
644 /* this is perfectly legal */
645 log("userauth_hostbased: unsupported "
646 "public key algorithm: %s", pkalg);
649 key = key_from_blob(pkblob, blen);
651 error("userauth_hostbased: cannot decode key: %s", pkalg);
654 if (key->type != pktype) {
655 error("userauth_hostbased: type mismatch for decoded key "
656 "(received %d, expected %d)", key->type, pktype);
659 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
662 buffer_put_string(&b, session_id2, session_id2_len);
663 /* reconstruct packet */
664 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
665 buffer_put_cstring(&b, authctxt->user);
666 buffer_put_cstring(&b, service);
667 buffer_put_cstring(&b, "hostbased");
668 buffer_put_string(&b, pkalg, alen);
669 buffer_put_string(&b, pkblob, blen);
670 buffer_put_cstring(&b, chost);
671 buffer_put_cstring(&b, cuser);
675 /* test for allowed key and correct signature */
677 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
678 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
679 buffer_len(&b))) == 1)
684 debug2("userauth_hostbased: authenticated %d", authenticated);
692 return authenticated;
695 /* get current user */
700 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
706 authmethods_get(void)
708 Authmethod *method = NULL;
713 for (method = authmethods; method->name != NULL; method++) {
714 if (strcmp(method->name, "none") == 0)
716 if (method->enabled != NULL && *(method->enabled) != 0) {
717 if (buffer_len(&b) > 0)
718 buffer_append(&b, ",", 1);
719 buffer_append(&b, method->name, strlen(method->name));
722 buffer_append(&b, "\0", 1);
723 list = xstrdup(buffer_ptr(&b));
729 authmethod_lookup(const char *name)
731 Authmethod *method = NULL;
733 for (method = authmethods; method->name != NULL; method++)
734 if (method->enabled != NULL &&
735 *(method->enabled) != 0 &&
736 strcmp(name, method->name) == 0)
738 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
742 /* return 1 if user allows given key */
744 user_key_allowed2(struct passwd *pw, Key *key, char *file)
757 /* Temporarily use the user's uid. */
758 temporarily_use_uid(pw);
760 debug("trying public key file %s", file);
762 /* Fail quietly if file does not exist */
763 if (stat(file, &st) < 0) {
764 /* Restore the privileged uid. */
768 /* Open the file containing the authorized keys. */
769 f = fopen(file, "r");
771 /* Restore the privileged uid. */
775 if (options.strict_modes &&
776 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
778 log("Authentication refused: %s", line);
784 found = key_new(key->type);
786 while (fgets(line, sizeof(line), f)) {
787 char *cp, *options = NULL;
789 /* Skip leading whitespace, empty and comment lines. */
790 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
792 if (!*cp || *cp == '\n' || *cp == '#')
795 if (key_read(found, &cp) != 1) {
796 /* no key? check if there are options for this key */
798 debug2("user_key_allowed: check options: '%s'", cp);
800 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
801 if (*cp == '\\' && cp[1] == '"')
802 cp++; /* Skip both */
806 /* Skip remaining whitespace. */
807 for (; *cp == ' ' || *cp == '\t'; cp++)
809 if (key_read(found, &cp) != 1) {
810 debug2("user_key_allowed: advance: '%s'", cp);
811 /* still no key? advance to next line*/
815 if (key_equal(found, key) &&
816 auth_parse_options(pw, options, file, linenum) == 1) {
818 debug("matching key found: file %s, line %lu",
820 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
821 verbose("Found matching %s key: %s",
822 key_type(found), fp);
831 debug2("key not found");
835 /* check whether given key is in .ssh/authorized_keys* */
837 user_key_allowed(struct passwd *pw, Key *key)
842 file = authorized_keys_file(pw);
843 success = user_key_allowed2(pw, key, file);
848 /* try suffix "2" for backward compat, too */
849 file = authorized_keys_file2(pw);
850 success = user_key_allowed2(pw, key, file);
855 /* return 1 if given hostkey is allowed */
857 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
860 const char *resolvedname, *ipaddr, *lookup;
861 HostStatus host_status;
864 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
865 ipaddr = get_remote_ipaddr();
867 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
868 chost, resolvedname, ipaddr);
870 if (options.hostbased_uses_name_from_packet_only) {
871 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
875 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
876 debug2("stripping trailing dot from chost %s", chost);
877 chost[len - 1] = '\0';
879 if (strcasecmp(resolvedname, chost) != 0)
880 log("userauth_hostbased mismatch: "
881 "client sends %s, but we resolve %s to %s",
882 chost, ipaddr, resolvedname);
883 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
885 lookup = resolvedname;
887 debug2("userauth_hostbased: access allowed by auth_rhosts2");
889 host_status = check_key_in_hostfiles(pw, key, lookup,
890 _PATH_SSH_SYSTEM_HOSTFILE,
891 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
893 /* backward compat if no key has been found. */
894 if (host_status == HOST_NEW)
895 host_status = check_key_in_hostfiles(pw, key, lookup,
896 _PATH_SSH_SYSTEM_HOSTFILE2,
897 options.ignore_user_known_hosts ? NULL :
898 _PATH_SSH_USER_HOSTFILE2);
900 return (host_status == HOST_OK);