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.88 2002/03/18 17:50:31 provos Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
54 #include "monitor_wrap.h"
57 extern ServerOptions options;
58 extern u_char *session_id2;
59 extern int session_id2_len;
61 Authctxt *x_authctxt = NULL;
64 typedef struct Authmethod Authmethod;
67 int (*userauth)(Authctxt *authctxt);
73 static void input_service_request(int, u_int32_t, void *);
74 static void input_userauth_request(int, u_int32_t, void *);
77 static Authmethod *authmethod_lookup(const char *);
78 static char *authmethods_get(void);
79 int user_key_allowed(struct passwd *, Key *);
80 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
83 static void userauth_banner(void);
84 static int userauth_none(Authctxt *);
85 static int userauth_passwd(Authctxt *);
86 static int userauth_pubkey(Authctxt *);
87 static int userauth_hostbased(Authctxt *);
88 static int userauth_kbdint(Authctxt *);
90 Authmethod authmethods[] = {
96 &options.pubkey_authentication},
99 &options.password_authentication},
100 {"keyboard-interactive",
102 &options.kbd_interactive_authentication},
105 &options.hostbased_authentication},
110 * loop until authctxt->success == TRUE
114 do_authentication2(void)
116 Authctxt *authctxt = authctxt_new();
118 x_authctxt = authctxt; /*XXX*/
120 /* challenge-response is implemented via keyboard interactive */
121 if (options.challenge_response_authentication)
122 options.kbd_interactive_authentication = 1;
123 if (options.pam_authentication_via_kbd_int)
124 options.kbd_interactive_authentication = 1;
126 dispatch_init(&dispatch_protocol_error);
127 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
128 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
134 input_service_request(int type, u_int32_t seq, void *ctxt)
136 Authctxt *authctxt = ctxt;
139 char *service = packet_get_string(&len);
142 if (authctxt == NULL)
143 fatal("input_service_request: no authctxt");
145 if (strcmp(service, "ssh-userauth") == 0) {
146 if (!authctxt->success) {
148 /* now we can handle user-auth requests */
149 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
152 /* XXX all other service requests are denied */
155 packet_start(SSH2_MSG_SERVICE_ACCEPT);
156 packet_put_cstring(service);
160 debug("bad service request %s", service);
161 packet_disconnect("bad service request %s", service);
167 input_userauth_request(int type, u_int32_t seq, void *ctxt)
169 Authctxt *authctxt = ctxt;
170 Authmethod *m = NULL;
171 char *user, *service, *method, *style = NULL;
172 int authenticated = 0;
174 if (authctxt == NULL)
175 fatal("input_userauth_request: no authctxt");
177 user = packet_get_string(NULL);
178 service = packet_get_string(NULL);
179 method = packet_get_string(NULL);
180 debug("userauth-request for user %s service %s method %s", user, service, method);
181 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
183 if ((style = strchr(user, ':')) != NULL)
186 if (authctxt->attempt++ == 0) {
187 /* setup auth context */
188 struct passwd *pw = NULL;
189 pw = PRIVSEP(getpwnamallow(user));
190 if (pw && strcmp(service, "ssh-connection")==0) {
191 authctxt->pw = pwcopy(pw);
193 debug2("input_userauth_request: setting up authctxt for %s", user);
195 start_pam(pw->pw_name);
198 log("input_userauth_request: illegal user %s", user);
204 if (use_privsep && pw != NULL)
207 setproctitle("%s%s", pw ? user : "unknown",
208 use_privsep ? " [net]" : "");
209 authctxt->user = xstrdup(user);
210 authctxt->service = xstrdup(service);
211 authctxt->style = style ? xstrdup(style) : NULL;
214 mm_inform_authserv(service, style);
215 } else if (strcmp(user, authctxt->user) != 0 ||
216 strcmp(service, authctxt->service) != 0) {
217 packet_disconnect("Change of username or service not allowed: "
218 "(%s,%s) -> (%s,%s)",
219 authctxt->user, authctxt->service, user, service);
222 auth2_challenge_stop(authctxt);
223 authctxt->postponed = 0;
225 /* try to authenticate user */
226 m = authmethod_lookup(method);
228 debug2("input_userauth_request: try method %s", method);
229 authenticated = m->userauth(authctxt);
231 userauth_finish(authctxt, authenticated, method);
239 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
243 if (!authctxt->valid && authenticated)
244 fatal("INTERNAL ERROR: authenticated invalid user %s",
247 /* Special handling for root */
248 if (authenticated && authctxt->pw->pw_uid == 0 &&
249 !auth_root_allowed(method))
253 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
258 /* Log before sending the reply */
259 auth_log(authctxt, authenticated, method, " ssh2");
261 if (authctxt->postponed)
264 /* XXX todo: check if multiple auth methods are needed */
265 if (authenticated == 1) {
266 /* turn off userauth */
267 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
268 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
271 /* now we can break out */
272 authctxt->success = 1;
274 if (authctxt->failures++ > AUTH_FAIL_MAX) {
275 #ifdef WITH_AIXAUTHENTICATE
276 loginfailed(authctxt->user,
277 get_canonical_hostname(options.verify_reverse_mapping),
279 #endif /* WITH_AIXAUTHENTICATE */
280 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
282 methods = authmethods_get();
283 packet_start(SSH2_MSG_USERAUTH_FAILURE);
284 packet_put_cstring(methods);
285 packet_put_char(0); /* XXX partial success, unused */
293 userauth_banner(void)
300 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
302 if ((fd = open(options.banner, O_RDONLY)) < 0)
304 if (fstat(fd, &st) < 0)
307 banner = xmalloc(len + 1);
308 if ((n = read(fd, banner, len)) < 0)
311 packet_start(SSH2_MSG_USERAUTH_BANNER);
312 packet_put_cstring(banner);
313 packet_put_cstring(""); /* language, unused */
315 debug("userauth_banner: sent");
324 userauth_none(Authctxt *authctxt)
326 /* disable method "none", only allowed one time */
327 Authmethod *m = authmethod_lookup("none");
333 if (authctxt->valid == 0)
337 if (check_nt_auth(1, authctxt->pw) == 0)
341 return auth_pam_password(authctxt->pw, "");
342 #elif defined(HAVE_OSF_SIA)
344 #else /* !HAVE_OSF_SIA && !USE_PAM */
345 return PRIVSEP(auth_password(authctxt, ""));
350 userauth_passwd(Authctxt *authctxt)
353 int authenticated = 0;
356 change = packet_get_char();
358 log("password change not supported");
359 password = packet_get_string(&len);
361 if (authctxt->valid &&
363 check_nt_auth(1, authctxt->pw) &&
366 auth_pam_password(authctxt->pw, password) == 1)
367 #elif defined(HAVE_OSF_SIA)
368 auth_sia_password(authctxt->user, password) == 1)
369 #else /* !USE_PAM && !HAVE_OSF_SIA */
370 PRIVSEP(auth_password(authctxt, password)) == 1)
373 memset(password, 0, len);
375 return authenticated;
379 userauth_kbdint(Authctxt *authctxt)
381 int authenticated = 0;
384 lang = packet_get_string(NULL);
385 devs = packet_get_string(NULL);
388 debug("keyboard-interactive devs %s", devs);
390 if (options.challenge_response_authentication)
391 authenticated = auth2_challenge(authctxt, devs);
394 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
395 authenticated = auth2_pam(authctxt);
400 if (check_nt_auth(0, authctxt->pw) == 0)
403 return authenticated;
407 userauth_pubkey(Authctxt *authctxt)
412 u_char *pkblob, *sig;
413 u_int alen, blen, slen;
414 int have_sig, pktype;
415 int authenticated = 0;
417 if (!authctxt->valid) {
418 debug2("userauth_pubkey: disabled because of invalid user");
421 have_sig = packet_get_char();
422 if (datafellows & SSH_BUG_PKAUTH) {
423 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
424 /* no explicit pkalg given */
425 pkblob = packet_get_string(&blen);
427 buffer_append(&b, pkblob, blen);
428 /* so we have to extract the pkalg from the pkblob */
429 pkalg = buffer_get_string(&b, &alen);
432 pkalg = packet_get_string(&alen);
433 pkblob = packet_get_string(&blen);
435 pktype = key_type_from_name(pkalg);
436 if (pktype == KEY_UNSPEC) {
437 /* this is perfectly legal */
438 log("userauth_pubkey: unsupported public key algorithm: %s",
442 key = key_from_blob(pkblob, blen);
444 error("userauth_pubkey: cannot decode key: %s", pkalg);
447 if (key->type != pktype) {
448 error("userauth_pubkey: type mismatch for decoded key "
449 "(received %d, expected %d)", key->type, pktype);
453 sig = packet_get_string(&slen);
456 if (datafellows & SSH_OLD_SESSIONID) {
457 buffer_append(&b, session_id2, session_id2_len);
459 buffer_put_string(&b, session_id2, session_id2_len);
461 /* reconstruct packet */
462 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
463 buffer_put_cstring(&b, authctxt->user);
464 buffer_put_cstring(&b,
465 datafellows & SSH_BUG_PKSERVICE ?
468 if (datafellows & SSH_BUG_PKAUTH) {
469 buffer_put_char(&b, have_sig);
471 buffer_put_cstring(&b, "publickey");
472 buffer_put_char(&b, have_sig);
473 buffer_put_cstring(&b, pkalg);
475 buffer_put_string(&b, pkblob, blen);
479 /* test for correct signature */
481 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
482 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
483 buffer_len(&b))) == 1)
488 debug("test whether pkalg/pkblob are acceptable");
491 /* XXX fake reply and always send PK_OK ? */
493 * XXX this allows testing whether a user is allowed
494 * to login: if you happen to have a valid pubkey this
495 * message is sent. the message is NEVER sent at all
496 * if a user is not allowed to login. is this an
499 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
500 packet_start(SSH2_MSG_USERAUTH_PK_OK);
501 packet_put_string(pkalg, alen);
502 packet_put_string(pkblob, blen);
505 authctxt->postponed = 1;
508 if (authenticated != 1)
509 auth_clear_options();
511 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
517 if (check_nt_auth(0, authctxt->pw) == 0)
520 return authenticated;
524 userauth_hostbased(Authctxt *authctxt)
528 char *pkalg, *cuser, *chost, *service;
529 u_char *pkblob, *sig;
530 u_int alen, blen, slen;
532 int authenticated = 0;
534 if (!authctxt->valid) {
535 debug2("userauth_hostbased: disabled because of invalid user");
538 pkalg = packet_get_string(&alen);
539 pkblob = packet_get_string(&blen);
540 chost = packet_get_string(NULL);
541 cuser = packet_get_string(NULL);
542 sig = packet_get_string(&slen);
544 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
545 cuser, chost, pkalg, slen);
549 buffer_append(&b, sig, slen);
553 pktype = key_type_from_name(pkalg);
554 if (pktype == KEY_UNSPEC) {
555 /* this is perfectly legal */
556 log("userauth_hostbased: unsupported "
557 "public key algorithm: %s", pkalg);
560 key = key_from_blob(pkblob, blen);
562 error("userauth_hostbased: cannot decode key: %s", pkalg);
565 if (key->type != pktype) {
566 error("userauth_hostbased: type mismatch for decoded key "
567 "(received %d, expected %d)", key->type, pktype);
570 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
573 buffer_put_string(&b, session_id2, session_id2_len);
574 /* reconstruct packet */
575 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
576 buffer_put_cstring(&b, authctxt->user);
577 buffer_put_cstring(&b, service);
578 buffer_put_cstring(&b, "hostbased");
579 buffer_put_string(&b, pkalg, alen);
580 buffer_put_string(&b, pkblob, blen);
581 buffer_put_cstring(&b, chost);
582 buffer_put_cstring(&b, cuser);
586 /* test for allowed key and correct signature */
588 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
589 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
590 buffer_len(&b))) == 1)
595 debug2("userauth_hostbased: authenticated %d", authenticated);
603 return authenticated;
606 /* get current user */
611 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
617 authmethods_get(void)
619 Authmethod *method = NULL;
624 for (method = authmethods; method->name != NULL; method++) {
625 if (strcmp(method->name, "none") == 0)
627 if (method->enabled != NULL && *(method->enabled) != 0) {
628 if (buffer_len(&b) > 0)
629 buffer_append(&b, ",", 1);
630 buffer_append(&b, method->name, strlen(method->name));
633 buffer_append(&b, "\0", 1);
634 list = xstrdup(buffer_ptr(&b));
640 authmethod_lookup(const char *name)
642 Authmethod *method = NULL;
644 for (method = authmethods; method->name != NULL; method++)
645 if (method->enabled != NULL &&
646 *(method->enabled) != 0 &&
647 strcmp(name, method->name) == 0)
649 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
653 /* return 1 if user allows given key */
655 user_key_allowed2(struct passwd *pw, Key *key, char *file)
668 /* Temporarily use the user's uid. */
669 temporarily_use_uid(pw);
671 debug("trying public key file %s", file);
673 /* Fail quietly if file does not exist */
674 if (stat(file, &st) < 0) {
675 /* Restore the privileged uid. */
679 /* Open the file containing the authorized keys. */
680 f = fopen(file, "r");
682 /* Restore the privileged uid. */
686 if (options.strict_modes &&
687 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
689 log("Authentication refused: %s", line);
695 found = key_new(key->type);
697 while (fgets(line, sizeof(line), f)) {
698 char *cp, *options = NULL;
700 /* Skip leading whitespace, empty and comment lines. */
701 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
703 if (!*cp || *cp == '\n' || *cp == '#')
706 if (key_read(found, &cp) != 1) {
707 /* no key? check if there are options for this key */
709 debug2("user_key_allowed: check options: '%s'", cp);
711 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
712 if (*cp == '\\' && cp[1] == '"')
713 cp++; /* Skip both */
717 /* Skip remaining whitespace. */
718 for (; *cp == ' ' || *cp == '\t'; cp++)
720 if (key_read(found, &cp) != 1) {
721 debug2("user_key_allowed: advance: '%s'", cp);
722 /* still no key? advance to next line*/
726 if (key_equal(found, key) &&
727 auth_parse_options(pw, options, file, linenum) == 1) {
729 debug("matching key found: file %s, line %lu",
731 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
732 verbose("Found matching %s key: %s",
733 key_type(found), fp);
742 debug2("key not found");
746 /* check whether given key is in .ssh/authorized_keys* */
748 user_key_allowed(struct passwd *pw, Key *key)
753 file = authorized_keys_file(pw);
754 success = user_key_allowed2(pw, key, file);
759 /* try suffix "2" for backward compat, too */
760 file = authorized_keys_file2(pw);
761 success = user_key_allowed2(pw, key, file);
766 /* return 1 if given hostkey is allowed */
768 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
771 const char *resolvedname, *ipaddr, *lookup;
772 HostStatus host_status;
775 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
776 ipaddr = get_remote_ipaddr();
778 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
779 chost, resolvedname, ipaddr);
781 if (options.hostbased_uses_name_from_packet_only) {
782 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
786 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
787 debug2("stripping trailing dot from chost %s", chost);
788 chost[len - 1] = '\0';
790 if (strcasecmp(resolvedname, chost) != 0)
791 log("userauth_hostbased mismatch: "
792 "client sends %s, but we resolve %s to %s",
793 chost, ipaddr, resolvedname);
794 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
796 lookup = resolvedname;
798 debug2("userauth_hostbased: access allowed by auth_rhosts2");
800 host_status = check_key_in_hostfiles(pw, key, lookup,
801 _PATH_SSH_SYSTEM_HOSTFILE,
802 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
804 /* backward compat if no key has been found. */
805 if (host_status == HOST_NEW)
806 host_status = check_key_in_hostfiles(pw, key, lookup,
807 _PATH_SSH_SYSTEM_HOSTFILE2,
808 options.ignore_user_known_hosts ? NULL :
809 _PATH_SSH_USER_HOSTFILE2);
811 return (host_status == HOST_OK);