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"
66 extern ServerOptions options;
67 extern u_char *session_id2;
68 extern int session_id2_len;
70 static Authctxt *x_authctxt = NULL;
73 typedef struct Authmethod Authmethod;
76 int (*userauth)(Authctxt *authctxt);
82 static void input_service_request(int, u_int32_t, void *);
83 static void input_userauth_request(int, u_int32_t, void *);
86 static Authmethod *authmethod_lookup(const char *);
87 static char *authmethods_get(void);
88 static int user_key_allowed(struct passwd *, Key *);
89 static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
92 static void userauth_banner(void);
93 static int userauth_none(Authctxt *);
94 static int userauth_passwd(Authctxt *);
95 static int userauth_pubkey(Authctxt *);
96 static int userauth_hostbased(Authctxt *);
97 static int userauth_kbdint(Authctxt *);
100 int userauth_external(Authctxt *authctxt);
101 int userauth_gssapi(Authctxt *authctxt);
104 Authmethod authmethods[] = {
111 &options.gss_authentication},
114 &options.gss_authentication},
118 &options.pubkey_authentication},
121 &options.password_authentication},
122 {"keyboard-interactive",
124 &options.kbd_interactive_authentication},
127 &options.hostbased_authentication},
132 * loop until authctxt->success == TRUE
136 do_authentication2(void)
138 Authctxt *authctxt = authctxt_new();
140 x_authctxt = authctxt; /*XXX*/
142 /* challenge-response is implemented via keyboard interactive */
143 if (options.challenge_response_authentication)
144 options.kbd_interactive_authentication = 1;
145 if (options.pam_authentication_via_kbd_int)
146 options.kbd_interactive_authentication = 1;
148 dispatch_init(&dispatch_protocol_error);
149 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
150 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
151 do_authenticated(authctxt);
155 input_service_request(int type, u_int32_t seq, void *ctxt)
157 Authctxt *authctxt = ctxt;
160 char *service = packet_get_string(&len);
163 if (authctxt == NULL)
164 fatal("input_service_request: no authctxt");
166 if (strcmp(service, "ssh-userauth") == 0) {
167 if (!authctxt->success) {
169 /* now we can handle user-auth requests */
170 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
173 /* XXX all other service requests are denied */
176 packet_start(SSH2_MSG_SERVICE_ACCEPT);
177 packet_put_cstring(service);
181 debug("bad service request %s", service);
182 packet_disconnect("bad service request %s", service);
188 input_userauth_request(int type, u_int32_t seq, void *ctxt)
190 Authctxt *authctxt = ctxt;
191 Authmethod *m = NULL;
192 char *user, *service, *method, *style = NULL;
193 int authenticated = 0;
195 if (authctxt == NULL)
196 fatal("input_userauth_request: no authctxt");
198 user = packet_get_string(NULL);
199 service = packet_get_string(NULL);
200 method = packet_get_string(NULL);
204 if(changeuser == 0 && (strcmp(method,"external-keyx") == 0 || strcmp(method,"gssapi") ==0) && strcmp(user,"") == 0) {
205 char *gridmapped_name = NULL;
206 struct passwd *pw = NULL;
208 if(globus_gss_assist_gridmap(gssapi_client_name.value,
209 &gridmapped_name) == 0) {
210 user = gridmapped_name;
211 debug("I gridmapped and got %s", user);
213 if (pw && allowed_user(pw)) {
214 olduser = authctxt->user;
215 authctxt->user = user;
216 authctxt->pw = pwcopy(pw);
222 debug("I gridmapped and got null, reverting to %s", authctxt->user);
223 user = authctxt->user;
226 else if(changeuser) {
227 struct passwd *pw = NULL;
229 if (pw && allowed_user(pw)) {
230 authctxt->user = olduser;
231 authctxt->pw = pwcopy(pw);
240 debug("userauth-request for user %s service %s method %s", user, service, method);
241 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
243 if ((style = strchr(user, ':')) != NULL)
246 if (authctxt->attempt++ == 0) {
247 /* setup auth context */
248 struct passwd *pw = NULL;
250 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
251 authctxt->pw = pwcopy(pw);
253 debug2("input_userauth_request: setting up authctxt for %s", user);
255 start_pam(pw->pw_name);
258 log("input_userauth_request: illegal user %s", user);
263 setproctitle("%s", pw ? user : "unknown");
264 authctxt->user = xstrdup(user);
265 authctxt->service = xstrdup(service);
266 authctxt->style = style ? xstrdup(style) : NULL;
267 } else if (strcmp(user, authctxt->user) != 0 ||
268 strcmp(service, authctxt->service) != 0) {
269 packet_disconnect("Change of username or service not allowed: "
270 "(%s,%s) -> (%s,%s)",
271 authctxt->user, authctxt->service, user, service);
274 auth2_challenge_stop(authctxt);
277 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
278 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
281 authctxt->postponed = 0;
283 /* try to authenticate user */
284 m = authmethod_lookup(method);
286 debug2("input_userauth_request: try method %s", method);
287 authenticated = m->userauth(authctxt);
289 userauth_finish(authctxt, authenticated, method);
297 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
301 if (!authctxt->valid && authenticated)
302 fatal("INTERNAL ERROR: authenticated invalid user %s",
305 /* Special handling for root */
306 if (authenticated && authctxt->pw->pw_uid == 0 &&
307 !auth_root_allowed(method))
311 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
316 /* Log before sending the reply */
318 auth_log(authctxt, authenticated, method, " ssh1");
320 auth_log(authctxt, authenticated, method, " ssh2");
322 if (authctxt->postponed)
325 /* XXX todo: check if multiple auth methods are needed */
326 if (authenticated == 1) {
327 /* turn off userauth */
328 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
330 packet_start(SSH_SMSG_SUCCESS);
332 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
335 /* now we can break out */
336 authctxt->success = 1;
338 if (authctxt->failures++ > AUTH_FAIL_MAX) {
339 #ifdef WITH_AIXAUTHENTICATE
340 loginfailed(authctxt->user,
341 get_canonical_hostname(options.verify_reverse_mapping),
343 #endif /* WITH_AIXAUTHENTICATE */
344 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
348 * Break out of the dispatch loop now and go back to
349 * SSH1 code. We need to set the 'success' flag to
350 * break out of the loop. Set the 'postponed' flag to
351 * tell the SSH1 code that authentication failed. The
352 * SSH1 code will handle sending SSH_SMSG_FAILURE.
354 authctxt->success = authctxt->postponed = 1;
356 methods = authmethods_get();
357 packet_start(SSH2_MSG_USERAUTH_FAILURE);
358 packet_put_cstring(methods);
359 packet_put_char(0); /* XXX partial success, unused */
368 userauth_banner(void)
375 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
377 if ((fd = open(options.banner, O_RDONLY)) < 0)
379 if (fstat(fd, &st) < 0)
382 banner = xmalloc(len + 1);
383 if ((n = read(fd, banner, len)) < 0)
386 packet_start(SSH2_MSG_USERAUTH_BANNER);
387 packet_put_cstring(banner);
388 packet_put_cstring(""); /* language, unused */
390 debug("userauth_banner: sent");
399 userauth_none(Authctxt *authctxt)
401 /* disable method "none", only allowed one time */
402 Authmethod *m = authmethod_lookup("none");
408 if (authctxt->valid == 0)
412 if (check_nt_auth(1, authctxt->pw) == 0)
416 return auth_pam_password(authctxt->pw, "");
417 #elif defined(HAVE_OSF_SIA)
419 #else /* !HAVE_OSF_SIA && !USE_PAM */
420 return auth_password(authctxt, "");
425 userauth_passwd(Authctxt *authctxt)
428 int authenticated = 0;
431 change = packet_get_char();
433 log("password change not supported");
434 password = packet_get_string(&len);
436 if (authctxt->valid &&
438 check_nt_auth(1, authctxt->pw) &&
441 auth_pam_password(authctxt->pw, password) == 1)
442 #elif defined(HAVE_OSF_SIA)
443 auth_sia_password(authctxt->user, password) == 1)
444 #else /* !USE_PAM && !HAVE_OSF_SIA */
445 auth_password(authctxt, password) == 1)
448 memset(password, 0, len);
450 return authenticated;
454 userauth_kbdint(Authctxt *authctxt)
456 int authenticated = 0;
459 lang = packet_get_string(NULL);
460 devs = packet_get_string(NULL);
463 debug("keyboard-interactive devs %s", devs);
465 if (options.challenge_response_authentication)
466 authenticated = auth2_challenge(authctxt, devs);
469 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
470 authenticated = auth2_pam(authctxt);
475 if (check_nt_auth(0, authctxt->pw) == 0)
478 return authenticated;
482 userauth_pubkey(Authctxt *authctxt)
487 u_char *pkblob, *sig;
488 u_int alen, blen, slen;
489 int have_sig, pktype;
490 int authenticated = 0;
492 if (!authctxt->valid) {
493 debug2("userauth_pubkey: disabled because of invalid user");
496 have_sig = packet_get_char();
497 if (datafellows & SSH_BUG_PKAUTH) {
498 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
499 /* no explicit pkalg given */
500 pkblob = packet_get_string(&blen);
502 buffer_append(&b, pkblob, blen);
503 /* so we have to extract the pkalg from the pkblob */
504 pkalg = buffer_get_string(&b, &alen);
507 pkalg = packet_get_string(&alen);
508 pkblob = packet_get_string(&blen);
510 pktype = key_type_from_name(pkalg);
511 if (pktype == KEY_UNSPEC) {
512 /* this is perfectly legal */
513 log("userauth_pubkey: unsupported public key algorithm: %s",
517 key = key_from_blob(pkblob, blen);
519 error("userauth_pubkey: cannot decode key: %s", pkalg);
522 if (key->type != pktype) {
523 error("userauth_pubkey: type mismatch for decoded key "
524 "(received %d, expected %d)", key->type, pktype);
528 sig = packet_get_string(&slen);
531 if (datafellows & SSH_OLD_SESSIONID) {
532 buffer_append(&b, session_id2, session_id2_len);
534 buffer_put_string(&b, session_id2, session_id2_len);
536 /* reconstruct packet */
537 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
538 buffer_put_cstring(&b, authctxt->user);
539 buffer_put_cstring(&b,
540 datafellows & SSH_BUG_PKSERVICE ?
543 if (datafellows & SSH_BUG_PKAUTH) {
544 buffer_put_char(&b, have_sig);
546 buffer_put_cstring(&b, "publickey");
547 buffer_put_char(&b, have_sig);
548 buffer_put_cstring(&b, pkalg);
550 buffer_put_string(&b, pkblob, blen);
554 /* test for correct signature */
555 if (user_key_allowed(authctxt->pw, key) &&
556 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
561 debug("test whether pkalg/pkblob are acceptable");
564 /* XXX fake reply and always send PK_OK ? */
566 * XXX this allows testing whether a user is allowed
567 * to login: if you happen to have a valid pubkey this
568 * message is sent. the message is NEVER sent at all
569 * if a user is not allowed to login. is this an
572 if (user_key_allowed(authctxt->pw, key)) {
573 packet_start(SSH2_MSG_USERAUTH_PK_OK);
574 packet_put_string(pkalg, alen);
575 packet_put_string(pkblob, blen);
578 authctxt->postponed = 1;
581 if (authenticated != 1)
582 auth_clear_options();
584 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
590 if (check_nt_auth(0, authctxt->pw) == 0)
593 return authenticated;
597 userauth_hostbased(Authctxt *authctxt)
601 char *pkalg, *cuser, *chost, *service;
602 u_char *pkblob, *sig;
603 u_int alen, blen, slen;
605 int authenticated = 0;
607 if (!authctxt->valid) {
608 debug2("userauth_hostbased: disabled because of invalid user");
611 pkalg = packet_get_string(&alen);
612 pkblob = packet_get_string(&blen);
613 chost = packet_get_string(NULL);
614 cuser = packet_get_string(NULL);
615 sig = packet_get_string(&slen);
617 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
618 cuser, chost, pkalg, slen);
622 buffer_append(&b, sig, slen);
626 pktype = key_type_from_name(pkalg);
627 if (pktype == KEY_UNSPEC) {
628 /* this is perfectly legal */
629 log("userauth_hostbased: unsupported "
630 "public key algorithm: %s", pkalg);
633 key = key_from_blob(pkblob, blen);
635 error("userauth_hostbased: cannot decode key: %s", pkalg);
638 if (key->type != pktype) {
639 error("userauth_hostbased: type mismatch for decoded key "
640 "(received %d, expected %d)", key->type, pktype);
643 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
646 buffer_put_string(&b, session_id2, session_id2_len);
647 /* reconstruct packet */
648 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
649 buffer_put_cstring(&b, authctxt->user);
650 buffer_put_cstring(&b, service);
651 buffer_put_cstring(&b, "hostbased");
652 buffer_put_string(&b, pkalg, alen);
653 buffer_put_string(&b, pkblob, blen);
654 buffer_put_cstring(&b, chost);
655 buffer_put_cstring(&b, cuser);
659 /* test for allowed key and correct signature */
660 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
661 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
666 debug2("userauth_hostbased: authenticated %d", authenticated);
674 return authenticated;
677 /* get current user */
682 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
688 authmethods_get(void)
690 Authmethod *method = NULL;
695 for (method = authmethods; method->name != NULL; method++) {
696 if (strcmp(method->name, "none") == 0)
698 if (method->enabled != NULL && *(method->enabled) != 0) {
699 if (buffer_len(&b) > 0)
700 buffer_append(&b, ",", 1);
701 buffer_append(&b, method->name, strlen(method->name));
704 buffer_append(&b, "\0", 1);
705 list = xstrdup(buffer_ptr(&b));
711 authmethod_lookup(const char *name)
713 Authmethod *method = NULL;
715 for (method = authmethods; method->name != NULL; method++)
716 if (method->enabled != NULL &&
717 *(method->enabled) != 0 &&
718 strcmp(name, method->name) == 0)
720 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
724 /* return 1 if user allows given key */
726 user_key_allowed2(struct passwd *pw, Key *key, char *file)
739 /* Temporarily use the user's uid. */
740 temporarily_use_uid(pw);
742 debug("trying public key file %s", file);
744 /* Fail quietly if file does not exist */
745 if (stat(file, &st) < 0) {
746 /* Restore the privileged uid. */
750 /* Open the file containing the authorized keys. */
751 f = fopen(file, "r");
753 /* Restore the privileged uid. */
757 if (options.strict_modes &&
758 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
760 log("Authentication refused: %s", line);
766 found = key_new(key->type);
768 while (fgets(line, sizeof(line), f)) {
769 char *cp, *options = NULL;
771 /* Skip leading whitespace, empty and comment lines. */
772 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
774 if (!*cp || *cp == '\n' || *cp == '#')
777 if (key_read(found, &cp) != 1) {
778 /* no key? check if there are options for this key */
780 debug2("user_key_allowed: check options: '%s'", cp);
782 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
783 if (*cp == '\\' && cp[1] == '"')
784 cp++; /* Skip both */
788 /* Skip remaining whitespace. */
789 for (; *cp == ' ' || *cp == '\t'; cp++)
791 if (key_read(found, &cp) != 1) {
792 debug2("user_key_allowed: advance: '%s'", cp);
793 /* still no key? advance to next line*/
797 if (key_equal(found, key) &&
798 auth_parse_options(pw, options, file, linenum) == 1) {
800 debug("matching key found: file %s, line %lu",
802 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
803 verbose("Found matching %s key: %s",
804 key_type(found), fp);
813 debug2("key not found");
817 /* check whether given key is in .ssh/authorized_keys* */
819 user_key_allowed(struct passwd *pw, Key *key)
824 file = authorized_keys_file(pw);
825 success = user_key_allowed2(pw, key, file);
830 /* try suffix "2" for backward compat, too */
831 file = authorized_keys_file2(pw);
832 success = user_key_allowed2(pw, key, file);
837 /* return 1 if given hostkey is allowed */
839 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
842 const char *resolvedname, *ipaddr, *lookup;
843 HostStatus host_status;
846 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
847 ipaddr = get_remote_ipaddr();
849 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
850 chost, resolvedname, ipaddr);
852 if (options.hostbased_uses_name_from_packet_only) {
853 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
857 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
858 debug2("stripping trailing dot from chost %s", chost);
859 chost[len - 1] = '\0';
861 if (strcasecmp(resolvedname, chost) != 0)
862 log("userauth_hostbased mismatch: "
863 "client sends %s, but we resolve %s to %s",
864 chost, ipaddr, resolvedname);
865 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
867 lookup = resolvedname;
869 debug2("userauth_hostbased: access allowed by auth_rhosts2");
871 host_status = check_key_in_hostfiles(pw, key, lookup,
872 _PATH_SSH_SYSTEM_HOSTFILE,
873 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
875 /* backward compat if no key has been found. */
876 if (host_status == HOST_NEW)
877 host_status = check_key_in_hostfiles(pw, key, lookup,
878 _PATH_SSH_SYSTEM_HOSTFILE2,
879 options.ignore_user_known_hosts ? NULL :
880 _PATH_SSH_USER_HOSTFILE2);
882 return (host_status == HOST_OK);