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 = xstrdup(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 xfree(authctxt->user);
237 authctxt->user = olduser;
238 authctxt->pw = pwcopy(pw);
247 debug("userauth-request for user %s service %s method %s", user, service, method);
248 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
250 if ((style = strchr(user, ':')) != NULL)
253 if (authctxt->attempt++ == 0) {
254 /* setup auth context */
255 authctxt->pw = PRIVSEP(getpwnamallow(user));
256 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
258 debug2("input_userauth_request: setting up authctxt for %s", user);
260 PRIVSEP(start_pam(authctxt->pw->pw_name));
263 log("input_userauth_request: illegal user %s", user);
265 PRIVSEP(start_pam("NOUSER"));
268 setproctitle("%s%s", authctxt->pw ? user : "unknown",
269 use_privsep ? " [net]" : "");
270 authctxt->user = xstrdup(user);
271 authctxt->service = xstrdup(service);
272 authctxt->style = style ? xstrdup(style) : NULL;
274 mm_inform_authserv(service, style);
275 } else if (strcmp(user, authctxt->user) != 0 ||
276 strcmp(service, authctxt->service) != 0) {
277 packet_disconnect("Change of username or service not allowed: "
278 "(%s,%s) -> (%s,%s)",
279 authctxt->user, authctxt->service, user, service);
282 auth2_challenge_stop(authctxt);
285 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
286 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
289 authctxt->postponed = 0;
291 /* try to authenticate user */
292 m = authmethod_lookup(method);
294 debug2("input_userauth_request: try method %s", method);
295 authenticated = m->userauth(authctxt);
297 userauth_finish(authctxt, authenticated, method);
305 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
309 if (!authctxt->valid && authenticated)
310 fatal("INTERNAL ERROR: authenticated invalid user %s",
313 /* Special handling for root */
314 if (authenticated && authctxt->pw->pw_uid == 0 &&
315 !auth_root_allowed(method))
319 if (!use_privsep && authenticated && authctxt->user &&
320 !do_pam_account(authctxt->user, NULL))
324 /* Log before sending the reply */
326 auth_log(authctxt, authenticated, method, " ssh1");
328 auth_log(authctxt, authenticated, method, " ssh2");
330 if (authctxt->postponed)
333 /* XXX todo: check if multiple auth methods are needed */
334 if (authenticated == 1) {
335 /* turn off userauth */
336 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
338 packet_start(SSH_SMSG_SUCCESS);
340 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
343 /* now we can break out */
344 authctxt->success = 1;
346 if (authctxt->failures++ > AUTH_FAIL_MAX) {
347 #ifdef WITH_AIXAUTHENTICATE
348 loginfailed(authctxt->user,
349 get_canonical_hostname(options.verify_reverse_mapping),
351 #endif /* WITH_AIXAUTHENTICATE */
352 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
356 * Break out of the dispatch loop now and go back to
357 * SSH1 code. We need to set the 'success' flag to
358 * break out of the loop. Set the 'postponed' flag to
359 * tell the SSH1 code that authentication failed. The
360 * SSH1 code will handle sending SSH_SMSG_FAILURE.
362 authctxt->success = authctxt->postponed = 1;
364 methods = authmethods_get();
366 packet_disconnect("GSSAPI authentication failed");
368 packet_start(SSH2_MSG_USERAUTH_FAILURE);
369 packet_put_cstring(methods);
370 packet_put_char(0); /* XXX partial success, unused */
379 auth2_read_banner(void)
386 if ((fd = open(options.banner, O_RDONLY)) == -1)
388 if (fstat(fd, &st) == -1) {
393 banner = xmalloc(len + 1);
394 n = atomicio(read, fd, banner, len);
407 userauth_banner(void)
411 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
414 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
417 packet_start(SSH2_MSG_USERAUTH_BANNER);
418 packet_put_cstring(banner);
419 packet_put_cstring(""); /* language, unused */
421 debug("userauth_banner: sent");
429 userauth_none(Authctxt *authctxt)
431 /* disable method "none", only allowed one time */
432 Authmethod *m = authmethod_lookup("none");
438 if (authctxt->valid == 0)
442 if (check_nt_auth(1, authctxt->pw) == 0)
445 return PRIVSEP(auth_password(authctxt, ""));
449 userauth_passwd(Authctxt *authctxt)
452 int authenticated = 0;
455 change = packet_get_char();
457 log("password change not supported");
458 password = packet_get_string(&len);
460 if (authctxt->valid &&
462 check_nt_auth(1, authctxt->pw) &&
464 PRIVSEP(auth_password(authctxt, password)) == 1)
466 memset(password, 0, len);
468 return authenticated;
472 userauth_kbdint(Authctxt *authctxt)
474 int authenticated = 0;
477 lang = packet_get_string(NULL);
478 devs = packet_get_string(NULL);
481 debug("keyboard-interactive devs %s", devs);
483 if (options.challenge_response_authentication)
484 authenticated = auth2_challenge(authctxt, devs);
487 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
488 authenticated = auth2_pam(authctxt);
493 if (check_nt_auth(0, authctxt->pw) == 0)
496 return authenticated;
500 userauth_pubkey(Authctxt *authctxt)
505 u_char *pkblob, *sig;
506 u_int alen, blen, slen;
507 int have_sig, pktype;
508 int authenticated = 0;
510 if (!authctxt->valid) {
511 debug2("userauth_pubkey: disabled because of invalid user");
514 have_sig = packet_get_char();
515 if (datafellows & SSH_BUG_PKAUTH) {
516 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
517 /* no explicit pkalg given */
518 pkblob = packet_get_string(&blen);
520 buffer_append(&b, pkblob, blen);
521 /* so we have to extract the pkalg from the pkblob */
522 pkalg = buffer_get_string(&b, &alen);
525 pkalg = packet_get_string(&alen);
526 pkblob = packet_get_string(&blen);
528 pktype = key_type_from_name(pkalg);
529 if (pktype == KEY_UNSPEC) {
530 /* this is perfectly legal */
531 log("userauth_pubkey: unsupported public key algorithm: %s",
535 key = key_from_blob(pkblob, blen);
537 error("userauth_pubkey: cannot decode key: %s", pkalg);
540 if (key->type != pktype) {
541 error("userauth_pubkey: type mismatch for decoded key "
542 "(received %d, expected %d)", key->type, pktype);
546 sig = packet_get_string(&slen);
549 if (datafellows & SSH_OLD_SESSIONID) {
550 buffer_append(&b, session_id2, session_id2_len);
552 buffer_put_string(&b, session_id2, session_id2_len);
554 /* reconstruct packet */
555 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
556 buffer_put_cstring(&b, authctxt->user);
557 buffer_put_cstring(&b,
558 datafellows & SSH_BUG_PKSERVICE ?
561 if (datafellows & SSH_BUG_PKAUTH) {
562 buffer_put_char(&b, have_sig);
564 buffer_put_cstring(&b, "publickey");
565 buffer_put_char(&b, have_sig);
566 buffer_put_cstring(&b, pkalg);
568 buffer_put_string(&b, pkblob, blen);
572 /* test for correct signature */
574 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
575 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
576 buffer_len(&b))) == 1)
581 debug("test whether pkalg/pkblob are acceptable");
584 /* XXX fake reply and always send PK_OK ? */
586 * XXX this allows testing whether a user is allowed
587 * to login: if you happen to have a valid pubkey this
588 * message is sent. the message is NEVER sent at all
589 * if a user is not allowed to login. is this an
592 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
593 packet_start(SSH2_MSG_USERAUTH_PK_OK);
594 packet_put_string(pkalg, alen);
595 packet_put_string(pkblob, blen);
598 authctxt->postponed = 1;
601 if (authenticated != 1)
602 auth_clear_options();
604 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
610 if (check_nt_auth(0, authctxt->pw) == 0)
613 return authenticated;
617 userauth_hostbased(Authctxt *authctxt)
621 char *pkalg, *cuser, *chost, *service;
622 u_char *pkblob, *sig;
623 u_int alen, blen, slen;
625 int authenticated = 0;
627 if (!authctxt->valid) {
628 debug2("userauth_hostbased: disabled because of invalid user");
631 pkalg = packet_get_string(&alen);
632 pkblob = packet_get_string(&blen);
633 chost = packet_get_string(NULL);
634 cuser = packet_get_string(NULL);
635 sig = packet_get_string(&slen);
637 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
638 cuser, chost, pkalg, slen);
642 buffer_append(&b, sig, slen);
646 pktype = key_type_from_name(pkalg);
647 if (pktype == KEY_UNSPEC) {
648 /* this is perfectly legal */
649 log("userauth_hostbased: unsupported "
650 "public key algorithm: %s", pkalg);
653 key = key_from_blob(pkblob, blen);
655 error("userauth_hostbased: cannot decode key: %s", pkalg);
658 if (key->type != pktype) {
659 error("userauth_hostbased: type mismatch for decoded key "
660 "(received %d, expected %d)", key->type, pktype);
663 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
666 buffer_put_string(&b, session_id2, session_id2_len);
667 /* reconstruct packet */
668 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
669 buffer_put_cstring(&b, authctxt->user);
670 buffer_put_cstring(&b, service);
671 buffer_put_cstring(&b, "hostbased");
672 buffer_put_string(&b, pkalg, alen);
673 buffer_put_string(&b, pkblob, blen);
674 buffer_put_cstring(&b, chost);
675 buffer_put_cstring(&b, cuser);
679 /* test for allowed key and correct signature */
681 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
682 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
683 buffer_len(&b))) == 1)
688 debug2("userauth_hostbased: authenticated %d", authenticated);
696 return authenticated;
699 /* get current user */
704 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
710 authmethods_get(void)
712 Authmethod *method = NULL;
717 for (method = authmethods; method->name != NULL; method++) {
718 if (strcmp(method->name, "none") == 0)
720 if (method->enabled != NULL && *(method->enabled) != 0) {
721 if (buffer_len(&b) > 0)
722 buffer_append(&b, ",", 1);
723 buffer_append(&b, method->name, strlen(method->name));
726 buffer_append(&b, "\0", 1);
727 list = xstrdup(buffer_ptr(&b));
733 authmethod_lookup(const char *name)
735 Authmethod *method = NULL;
737 for (method = authmethods; method->name != NULL; method++)
738 if (method->enabled != NULL &&
739 *(method->enabled) != 0 &&
740 strcmp(name, method->name) == 0)
742 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
746 /* return 1 if user allows given key */
748 user_key_allowed2(struct passwd *pw, Key *key, char *file)
761 /* Temporarily use the user's uid. */
762 temporarily_use_uid(pw);
764 debug("trying public key file %s", file);
766 /* Fail quietly if file does not exist */
767 if (stat(file, &st) < 0) {
768 /* Restore the privileged uid. */
772 /* Open the file containing the authorized keys. */
773 f = fopen(file, "r");
775 /* Restore the privileged uid. */
779 if (options.strict_modes &&
780 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
782 log("Authentication refused: %s", line);
788 found = key_new(key->type);
790 while (fgets(line, sizeof(line), f)) {
791 char *cp, *options = NULL;
793 /* Skip leading whitespace, empty and comment lines. */
794 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
796 if (!*cp || *cp == '\n' || *cp == '#')
799 if (key_read(found, &cp) != 1) {
800 /* no key? check if there are options for this key */
802 debug2("user_key_allowed: check options: '%s'", cp);
804 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
805 if (*cp == '\\' && cp[1] == '"')
806 cp++; /* Skip both */
810 /* Skip remaining whitespace. */
811 for (; *cp == ' ' || *cp == '\t'; cp++)
813 if (key_read(found, &cp) != 1) {
814 debug2("user_key_allowed: advance: '%s'", cp);
815 /* still no key? advance to next line*/
819 if (key_equal(found, key) &&
820 auth_parse_options(pw, options, file, linenum) == 1) {
822 debug("matching key found: file %s, line %lu",
824 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
825 verbose("Found matching %s key: %s",
826 key_type(found), fp);
835 debug2("key not found");
839 /* check whether given key is in .ssh/authorized_keys* */
841 user_key_allowed(struct passwd *pw, Key *key)
846 file = authorized_keys_file(pw);
847 success = user_key_allowed2(pw, key, file);
852 /* try suffix "2" for backward compat, too */
853 file = authorized_keys_file2(pw);
854 success = user_key_allowed2(pw, key, file);
859 /* return 1 if given hostkey is allowed */
861 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
864 const char *resolvedname, *ipaddr, *lookup;
865 HostStatus host_status;
868 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
869 ipaddr = get_remote_ipaddr();
871 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
872 chost, resolvedname, ipaddr);
874 if (options.hostbased_uses_name_from_packet_only) {
875 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
879 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
880 debug2("stripping trailing dot from chost %s", chost);
881 chost[len - 1] = '\0';
883 if (strcasecmp(resolvedname, chost) != 0)
884 log("userauth_hostbased mismatch: "
885 "client sends %s, but we resolve %s to %s",
886 chost, ipaddr, resolvedname);
887 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
889 lookup = resolvedname;
891 debug2("userauth_hostbased: access allowed by auth_rhosts2");
893 host_status = check_key_in_hostfiles(pw, key, lookup,
894 _PATH_SSH_SYSTEM_HOSTFILE,
895 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
897 /* backward compat if no key has been found. */
898 if (host_status == HOST_NEW)
899 host_status = check_key_in_hostfiles(pw, key, lookup,
900 _PATH_SSH_SYSTEM_HOSTFILE2,
901 options.ignore_user_known_hosts ? NULL :
902 _PATH_SSH_USER_HOSTFILE2);
904 return (host_status == HOST_OK);