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.85 2002/02/24 19:14:59 markus Exp $");
28 #include <openssl/evp.h>
48 #include "pathnames.h"
50 #include "auth-options.h"
59 #include "globus_gss_assist.h"
64 extern ServerOptions options;
65 extern u_char *session_id2;
66 extern int session_id2_len;
68 static Authctxt *x_authctxt = NULL;
71 typedef struct Authmethod Authmethod;
74 int (*userauth)(Authctxt *authctxt);
80 static void input_service_request(int, u_int32_t, void *);
81 static void input_userauth_request(int, u_int32_t, void *);
84 static Authmethod *authmethod_lookup(const char *);
85 static char *authmethods_get(void);
86 static int user_key_allowed(struct passwd *, Key *);
87 static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
90 static void userauth_banner(void);
91 static int userauth_none(Authctxt *);
92 static int userauth_passwd(Authctxt *);
93 static int userauth_pubkey(Authctxt *);
94 static int userauth_hostbased(Authctxt *);
95 static int userauth_kbdint(Authctxt *);
98 int userauth_external(Authctxt *authctxt);
99 int userauth_gssapi(Authctxt *authctxt);
102 Authmethod authmethods[] = {
109 &options.gss_authentication},
112 &options.gss_authentication},
116 &options.pubkey_authentication},
119 &options.password_authentication},
120 {"keyboard-interactive",
122 &options.kbd_interactive_authentication},
125 &options.hostbased_authentication},
130 * loop until authctxt->success == TRUE
134 do_authentication2(void)
136 Authctxt *authctxt = authctxt_new();
138 x_authctxt = authctxt; /*XXX*/
140 /* challenge-response is implemented via keyboard interactive */
141 if (options.challenge_response_authentication)
142 options.kbd_interactive_authentication = 1;
143 if (options.pam_authentication_via_kbd_int)
144 options.kbd_interactive_authentication = 1;
146 dispatch_init(&dispatch_protocol_error);
147 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
148 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
149 do_authenticated(authctxt);
153 input_service_request(int type, u_int32_t seq, void *ctxt)
155 Authctxt *authctxt = ctxt;
158 char *service = packet_get_string(&len);
161 if (authctxt == NULL)
162 fatal("input_service_request: no authctxt");
164 if (strcmp(service, "ssh-userauth") == 0) {
165 if (!authctxt->success) {
167 /* now we can handle user-auth requests */
168 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
171 /* XXX all other service requests are denied */
174 packet_start(SSH2_MSG_SERVICE_ACCEPT);
175 packet_put_cstring(service);
179 debug("bad service request %s", service);
180 packet_disconnect("bad service request %s", service);
186 input_userauth_request(int type, u_int32_t seq, void *ctxt)
188 Authctxt *authctxt = ctxt;
189 Authmethod *m = NULL;
190 char *user, *service, *method, *style = NULL;
191 int authenticated = 0;
193 if (authctxt == NULL)
194 fatal("input_userauth_request: no authctxt");
196 user = packet_get_string(NULL);
197 service = packet_get_string(NULL);
198 method = packet_get_string(NULL);
200 if(strcmp(method,"external-keyx") == 0 && strcmp(user,"") == 0) {
201 char *gridmapped_name = NULL;
202 struct passwd *pw = NULL;
204 if(globus_gss_assist_gridmap(gssapi_client_name.value,
205 &gridmapped_name) == 0) {
206 user = gridmapped_name;
207 debug("I gridmapped and got %s", user);
209 if (pw && allowed_user(pw)) {
210 authctxt->user = user;
211 authctxt->pw = pwcopy(pw);
215 debug("I gridmapped and got null, reverting to %s", authctxt->user);
216 user = authctxt->user;
220 debug("userauth-request for user %s service %s method %s", user, service, method);
221 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
223 if ((style = strchr(user, ':')) != NULL)
226 if (authctxt->attempt++ == 0) {
227 /* setup auth context */
228 struct passwd *pw = NULL;
230 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
231 authctxt->pw = pwcopy(pw);
233 debug2("input_userauth_request: setting up authctxt for %s", user);
235 start_pam(pw->pw_name);
238 log("input_userauth_request: illegal user %s", user);
243 setproctitle("%s", pw ? user : "unknown");
244 authctxt->user = xstrdup(user);
245 authctxt->service = xstrdup(service);
246 authctxt->style = style ? xstrdup(style) : NULL;
247 } else if (strcmp(user, authctxt->user) != 0 ||
248 strcmp(service, authctxt->service) != 0) {
249 packet_disconnect("Change of username or service not allowed: "
250 "(%s,%s) -> (%s,%s)",
251 authctxt->user, authctxt->service, user, service);
254 auth2_challenge_stop(authctxt);
257 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
258 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
261 authctxt->postponed = 0;
263 /* try to authenticate user */
264 m = authmethod_lookup(method);
266 debug2("input_userauth_request: try method %s", method);
267 authenticated = m->userauth(authctxt);
269 userauth_finish(authctxt, authenticated, method);
277 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
281 if (!authctxt->valid && authenticated)
282 fatal("INTERNAL ERROR: authenticated invalid user %s",
285 /* Special handling for root */
286 if (authenticated && authctxt->pw->pw_uid == 0 &&
287 !auth_root_allowed(method))
291 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
296 /* Log before sending the reply */
298 auth_log(authctxt, authenticated, method, " ssh1");
300 auth_log(authctxt, authenticated, method, " ssh2");
302 if (authctxt->postponed)
305 /* XXX todo: check if multiple auth methods are needed */
306 if (authenticated == 1) {
307 /* turn off userauth */
308 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
310 packet_start(SSH_SMSG_SUCCESS);
312 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
315 /* now we can break out */
316 authctxt->success = 1;
318 if (authctxt->failures++ > AUTH_FAIL_MAX) {
319 #ifdef WITH_AIXAUTHENTICATE
320 loginfailed(authctxt->user,
321 get_canonical_hostname(options.verify_reverse_mapping),
323 #endif /* WITH_AIXAUTHENTICATE */
324 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
328 * Break out of the dispatch loop now and go back to
329 * SSH1 code. We need to set the 'success' flag to
330 * break out of the loop. Set the 'postponed' flag to
331 * tell the SSH1 code that authentication failed. The
332 * SSH1 code will handle sending SSH_SMSG_FAILURE.
334 authctxt->success = authctxt->postponed = 1;
336 methods = authmethods_get();
337 packet_start(SSH2_MSG_USERAUTH_FAILURE);
338 packet_put_cstring(methods);
339 packet_put_char(0); /* XXX partial success, unused */
348 userauth_banner(void)
355 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
357 if ((fd = open(options.banner, O_RDONLY)) < 0)
359 if (fstat(fd, &st) < 0)
362 banner = xmalloc(len + 1);
363 if ((n = read(fd, banner, len)) < 0)
366 packet_start(SSH2_MSG_USERAUTH_BANNER);
367 packet_put_cstring(banner);
368 packet_put_cstring(""); /* language, unused */
370 debug("userauth_banner: sent");
379 userauth_none(Authctxt *authctxt)
381 /* disable method "none", only allowed one time */
382 Authmethod *m = authmethod_lookup("none");
388 if (authctxt->valid == 0)
392 if (check_nt_auth(1, authctxt->pw) == 0)
396 return auth_pam_password(authctxt->pw, "");
397 #elif defined(HAVE_OSF_SIA)
399 #else /* !HAVE_OSF_SIA && !USE_PAM */
400 return auth_password(authctxt, "");
405 userauth_passwd(Authctxt *authctxt)
408 int authenticated = 0;
411 change = packet_get_char();
413 log("password change not supported");
414 password = packet_get_string(&len);
416 if (authctxt->valid &&
418 check_nt_auth(1, authctxt->pw) &&
421 auth_pam_password(authctxt->pw, password) == 1)
422 #elif defined(HAVE_OSF_SIA)
423 auth_sia_password(authctxt->user, password) == 1)
424 #else /* !USE_PAM && !HAVE_OSF_SIA */
425 auth_password(authctxt, password) == 1)
428 memset(password, 0, len);
430 return authenticated;
434 userauth_kbdint(Authctxt *authctxt)
436 int authenticated = 0;
439 lang = packet_get_string(NULL);
440 devs = packet_get_string(NULL);
443 debug("keyboard-interactive devs %s", devs);
445 if (options.challenge_response_authentication)
446 authenticated = auth2_challenge(authctxt, devs);
449 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
450 authenticated = auth2_pam(authctxt);
455 if (check_nt_auth(0, authctxt->pw) == 0)
458 return authenticated;
462 userauth_pubkey(Authctxt *authctxt)
467 u_char *pkblob, *sig;
468 u_int alen, blen, slen;
469 int have_sig, pktype;
470 int authenticated = 0;
472 if (!authctxt->valid) {
473 debug2("userauth_pubkey: disabled because of invalid user");
476 have_sig = packet_get_char();
477 if (datafellows & SSH_BUG_PKAUTH) {
478 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
479 /* no explicit pkalg given */
480 pkblob = packet_get_string(&blen);
482 buffer_append(&b, pkblob, blen);
483 /* so we have to extract the pkalg from the pkblob */
484 pkalg = buffer_get_string(&b, &alen);
487 pkalg = packet_get_string(&alen);
488 pkblob = packet_get_string(&blen);
490 pktype = key_type_from_name(pkalg);
491 if (pktype == KEY_UNSPEC) {
492 /* this is perfectly legal */
493 log("userauth_pubkey: unsupported public key algorithm: %s",
497 key = key_from_blob(pkblob, blen);
499 error("userauth_pubkey: cannot decode key: %s", pkalg);
502 if (key->type != pktype) {
503 error("userauth_pubkey: type mismatch for decoded key "
504 "(received %d, expected %d)", key->type, pktype);
508 sig = packet_get_string(&slen);
511 if (datafellows & SSH_OLD_SESSIONID) {
512 buffer_append(&b, session_id2, session_id2_len);
514 buffer_put_string(&b, session_id2, session_id2_len);
516 /* reconstruct packet */
517 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
518 buffer_put_cstring(&b, authctxt->user);
519 buffer_put_cstring(&b,
520 datafellows & SSH_BUG_PKSERVICE ?
523 if (datafellows & SSH_BUG_PKAUTH) {
524 buffer_put_char(&b, have_sig);
526 buffer_put_cstring(&b, "publickey");
527 buffer_put_char(&b, have_sig);
528 buffer_put_cstring(&b, pkalg);
530 buffer_put_string(&b, pkblob, blen);
534 /* test for correct signature */
535 if (user_key_allowed(authctxt->pw, key) &&
536 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
541 debug("test whether pkalg/pkblob are acceptable");
544 /* XXX fake reply and always send PK_OK ? */
546 * XXX this allows testing whether a user is allowed
547 * to login: if you happen to have a valid pubkey this
548 * message is sent. the message is NEVER sent at all
549 * if a user is not allowed to login. is this an
552 if (user_key_allowed(authctxt->pw, key)) {
553 packet_start(SSH2_MSG_USERAUTH_PK_OK);
554 packet_put_string(pkalg, alen);
555 packet_put_string(pkblob, blen);
558 authctxt->postponed = 1;
561 if (authenticated != 1)
562 auth_clear_options();
564 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
570 if (check_nt_auth(0, authctxt->pw) == 0)
573 return authenticated;
577 userauth_hostbased(Authctxt *authctxt)
581 char *pkalg, *cuser, *chost, *service;
582 u_char *pkblob, *sig;
583 u_int alen, blen, slen;
585 int authenticated = 0;
587 if (!authctxt->valid) {
588 debug2("userauth_hostbased: disabled because of invalid user");
591 pkalg = packet_get_string(&alen);
592 pkblob = packet_get_string(&blen);
593 chost = packet_get_string(NULL);
594 cuser = packet_get_string(NULL);
595 sig = packet_get_string(&slen);
597 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
598 cuser, chost, pkalg, slen);
602 buffer_append(&b, sig, slen);
606 pktype = key_type_from_name(pkalg);
607 if (pktype == KEY_UNSPEC) {
608 /* this is perfectly legal */
609 log("userauth_hostbased: unsupported "
610 "public key algorithm: %s", pkalg);
613 key = key_from_blob(pkblob, blen);
615 error("userauth_hostbased: cannot decode key: %s", pkalg);
618 if (key->type != pktype) {
619 error("userauth_hostbased: type mismatch for decoded key "
620 "(received %d, expected %d)", key->type, pktype);
623 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
626 buffer_put_string(&b, session_id2, session_id2_len);
627 /* reconstruct packet */
628 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
629 buffer_put_cstring(&b, authctxt->user);
630 buffer_put_cstring(&b, service);
631 buffer_put_cstring(&b, "hostbased");
632 buffer_put_string(&b, pkalg, alen);
633 buffer_put_string(&b, pkblob, blen);
634 buffer_put_cstring(&b, chost);
635 buffer_put_cstring(&b, cuser);
639 /* test for allowed key and correct signature */
640 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
641 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
646 debug2("userauth_hostbased: authenticated %d", authenticated);
654 return authenticated;
657 /* get current user */
662 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
668 authmethods_get(void)
670 Authmethod *method = NULL;
675 for (method = authmethods; method->name != NULL; method++) {
676 if (strcmp(method->name, "none") == 0)
678 if (method->enabled != NULL && *(method->enabled) != 0) {
679 if (buffer_len(&b) > 0)
680 buffer_append(&b, ",", 1);
681 buffer_append(&b, method->name, strlen(method->name));
684 buffer_append(&b, "\0", 1);
685 list = xstrdup(buffer_ptr(&b));
691 authmethod_lookup(const char *name)
693 Authmethod *method = NULL;
695 for (method = authmethods; method->name != NULL; method++)
696 if (method->enabled != NULL &&
697 *(method->enabled) != 0 &&
698 strcmp(name, method->name) == 0)
700 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
704 /* return 1 if user allows given key */
706 user_key_allowed2(struct passwd *pw, Key *key, char *file)
719 /* Temporarily use the user's uid. */
720 temporarily_use_uid(pw);
722 debug("trying public key file %s", file);
724 /* Fail quietly if file does not exist */
725 if (stat(file, &st) < 0) {
726 /* Restore the privileged uid. */
730 /* Open the file containing the authorized keys. */
731 f = fopen(file, "r");
733 /* Restore the privileged uid. */
737 if (options.strict_modes &&
738 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
740 log("Authentication refused: %s", line);
746 found = key_new(key->type);
748 while (fgets(line, sizeof(line), f)) {
749 char *cp, *options = NULL;
751 /* Skip leading whitespace, empty and comment lines. */
752 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
754 if (!*cp || *cp == '\n' || *cp == '#')
757 if (key_read(found, &cp) != 1) {
758 /* no key? check if there are options for this key */
760 debug2("user_key_allowed: check options: '%s'", cp);
762 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
763 if (*cp == '\\' && cp[1] == '"')
764 cp++; /* Skip both */
768 /* Skip remaining whitespace. */
769 for (; *cp == ' ' || *cp == '\t'; cp++)
771 if (key_read(found, &cp) != 1) {
772 debug2("user_key_allowed: advance: '%s'", cp);
773 /* still no key? advance to next line*/
777 if (key_equal(found, key) &&
778 auth_parse_options(pw, options, file, linenum) == 1) {
780 debug("matching key found: file %s, line %lu",
782 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
783 verbose("Found matching %s key: %s",
784 key_type(found), fp);
793 debug2("key not found");
797 /* check whether given key is in .ssh/authorized_keys* */
799 user_key_allowed(struct passwd *pw, Key *key)
804 file = authorized_keys_file(pw);
805 success = user_key_allowed2(pw, key, file);
810 /* try suffix "2" for backward compat, too */
811 file = authorized_keys_file2(pw);
812 success = user_key_allowed2(pw, key, file);
817 /* return 1 if given hostkey is allowed */
819 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
822 const char *resolvedname, *ipaddr, *lookup;
823 HostStatus host_status;
826 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
827 ipaddr = get_remote_ipaddr();
829 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
830 chost, resolvedname, ipaddr);
832 if (options.hostbased_uses_name_from_packet_only) {
833 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
837 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
838 debug2("stripping trailing dot from chost %s", chost);
839 chost[len - 1] = '\0';
841 if (strcasecmp(resolvedname, chost) != 0)
842 log("userauth_hostbased mismatch: "
843 "client sends %s, but we resolve %s to %s",
844 chost, ipaddr, resolvedname);
845 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
847 lookup = resolvedname;
849 debug2("userauth_hostbased: access allowed by auth_rhosts2");
851 host_status = check_key_in_hostfiles(pw, key, lookup,
852 _PATH_SSH_SYSTEM_HOSTFILE,
853 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
855 /* backward compat if no key has been found. */
856 if (host_status == HOST_NEW)
857 host_status = check_key_in_hostfiles(pw, key, lookup,
858 _PATH_SSH_SYSTEM_HOSTFILE2,
859 options.ignore_user_known_hosts ? NULL :
860 _PATH_SSH_USER_HOSTFILE2);
862 return (host_status == HOST_OK);