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.89 2002/03/19 14:27:39 markus Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
53 #include "monitor_wrap.h"
56 extern ServerOptions options;
57 extern u_char *session_id2;
58 extern int session_id2_len;
60 Authctxt *x_authctxt = NULL;
63 typedef struct Authmethod Authmethod;
66 int (*userauth)(Authctxt *authctxt);
72 static void input_service_request(int, u_int32_t, void *);
73 static void input_userauth_request(int, u_int32_t, void *);
76 static Authmethod *authmethod_lookup(const char *);
77 static char *authmethods_get(void);
78 int user_key_allowed(struct passwd *, Key *);
79 int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
82 static void userauth_banner(void);
83 static int userauth_none(Authctxt *);
84 static int userauth_passwd(Authctxt *);
85 static int userauth_pubkey(Authctxt *);
86 static int userauth_hostbased(Authctxt *);
87 static int userauth_kbdint(Authctxt *);
89 Authmethod authmethods[] = {
95 &options.pubkey_authentication},
98 &options.password_authentication},
99 {"keyboard-interactive",
101 &options.kbd_interactive_authentication},
104 &options.hostbased_authentication},
109 * loop until authctxt->success == TRUE
113 do_authentication2(void)
115 Authctxt *authctxt = authctxt_new();
117 x_authctxt = authctxt; /*XXX*/
119 /* challenge-response is implemented via keyboard interactive */
120 if (options.challenge_response_authentication)
121 options.kbd_interactive_authentication = 1;
122 if (options.pam_authentication_via_kbd_int)
123 options.kbd_interactive_authentication = 1;
125 dispatch_init(&dispatch_protocol_error);
126 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
127 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
133 input_service_request(int type, u_int32_t seq, void *ctxt)
135 Authctxt *authctxt = ctxt;
138 char *service = packet_get_string(&len);
141 if (authctxt == NULL)
142 fatal("input_service_request: no authctxt");
144 if (strcmp(service, "ssh-userauth") == 0) {
145 if (!authctxt->success) {
147 /* now we can handle user-auth requests */
148 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
151 /* XXX all other service requests are denied */
154 packet_start(SSH2_MSG_SERVICE_ACCEPT);
155 packet_put_cstring(service);
159 debug("bad service request %s", service);
160 packet_disconnect("bad service request %s", service);
166 input_userauth_request(int type, u_int32_t seq, void *ctxt)
168 Authctxt *authctxt = ctxt;
169 Authmethod *m = NULL;
170 char *user, *service, *method, *style = NULL;
171 int authenticated = 0;
173 if (authctxt == NULL)
174 fatal("input_userauth_request: no authctxt");
176 user = packet_get_string(NULL);
177 service = packet_get_string(NULL);
178 method = packet_get_string(NULL);
179 debug("userauth-request for user %s service %s method %s", user, service, method);
180 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
182 if ((style = strchr(user, ':')) != NULL)
185 if (authctxt->attempt++ == 0) {
186 /* setup auth context */
187 struct passwd *pw = NULL;
188 pw = PRIVSEP(getpwnamallow(user));
189 if (pw && strcmp(service, "ssh-connection")==0) {
190 authctxt->pw = pwcopy(pw);
192 debug2("input_userauth_request: setting up authctxt for %s", user);
194 start_pam(pw->pw_name);
197 log("input_userauth_request: illegal user %s", user);
202 setproctitle("%s%s", authctxt->pw ? user : "unknown",
203 use_privsep ? " [net]" : "");
204 authctxt->user = xstrdup(user);
205 authctxt->service = xstrdup(service);
206 authctxt->style = style ? xstrdup(style) : NULL;
208 mm_inform_authserv(service, style);
209 } else if (strcmp(user, authctxt->user) != 0 ||
210 strcmp(service, authctxt->service) != 0) {
211 packet_disconnect("Change of username or service not allowed: "
212 "(%s,%s) -> (%s,%s)",
213 authctxt->user, authctxt->service, user, service);
216 auth2_challenge_stop(authctxt);
217 authctxt->postponed = 0;
219 /* try to authenticate user */
220 m = authmethod_lookup(method);
222 debug2("input_userauth_request: try method %s", method);
223 authenticated = m->userauth(authctxt);
225 userauth_finish(authctxt, authenticated, method);
233 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
237 if (!authctxt->valid && authenticated)
238 fatal("INTERNAL ERROR: authenticated invalid user %s",
241 /* Special handling for root */
242 if (authenticated && authctxt->pw->pw_uid == 0 &&
243 !auth_root_allowed(method))
247 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
252 /* Log before sending the reply */
253 auth_log(authctxt, authenticated, method, " ssh2");
255 if (authctxt->postponed)
258 /* XXX todo: check if multiple auth methods are needed */
259 if (authenticated == 1) {
260 /* turn off userauth */
261 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
262 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
265 /* now we can break out */
266 authctxt->success = 1;
268 if (authctxt->failures++ > AUTH_FAIL_MAX) {
269 #ifdef WITH_AIXAUTHENTICATE
270 loginfailed(authctxt->user,
271 get_canonical_hostname(options.verify_reverse_mapping),
273 #endif /* WITH_AIXAUTHENTICATE */
274 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
276 methods = authmethods_get();
277 packet_start(SSH2_MSG_USERAUTH_FAILURE);
278 packet_put_cstring(methods);
279 packet_put_char(0); /* XXX partial success, unused */
287 userauth_banner(void)
294 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
296 if ((fd = open(options.banner, O_RDONLY)) < 0)
298 if (fstat(fd, &st) < 0)
301 banner = xmalloc(len + 1);
302 if ((n = read(fd, banner, len)) < 0)
305 packet_start(SSH2_MSG_USERAUTH_BANNER);
306 packet_put_cstring(banner);
307 packet_put_cstring(""); /* language, unused */
309 debug("userauth_banner: sent");
318 userauth_none(Authctxt *authctxt)
320 /* disable method "none", only allowed one time */
321 Authmethod *m = authmethod_lookup("none");
327 if (authctxt->valid == 0)
331 if (check_nt_auth(1, authctxt->pw) == 0)
335 return auth_pam_password(authctxt->pw, "");
336 #elif defined(HAVE_OSF_SIA)
338 #else /* !HAVE_OSF_SIA && !USE_PAM */
339 return PRIVSEP(auth_password(authctxt, ""));
344 userauth_passwd(Authctxt *authctxt)
347 int authenticated = 0;
350 change = packet_get_char();
352 log("password change not supported");
353 password = packet_get_string(&len);
355 if (authctxt->valid &&
357 check_nt_auth(1, authctxt->pw) &&
360 auth_pam_password(authctxt->pw, password) == 1)
361 #elif defined(HAVE_OSF_SIA)
362 auth_sia_password(authctxt->user, password) == 1)
363 #else /* !USE_PAM && !HAVE_OSF_SIA */
364 PRIVSEP(auth_password(authctxt, password)) == 1)
367 memset(password, 0, len);
369 return authenticated;
373 userauth_kbdint(Authctxt *authctxt)
375 int authenticated = 0;
378 lang = packet_get_string(NULL);
379 devs = packet_get_string(NULL);
382 debug("keyboard-interactive devs %s", devs);
384 if (options.challenge_response_authentication)
385 authenticated = auth2_challenge(authctxt, devs);
388 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
389 authenticated = auth2_pam(authctxt);
394 if (check_nt_auth(0, authctxt->pw) == 0)
397 return authenticated;
401 userauth_pubkey(Authctxt *authctxt)
406 u_char *pkblob, *sig;
407 u_int alen, blen, slen;
408 int have_sig, pktype;
409 int authenticated = 0;
411 if (!authctxt->valid) {
412 debug2("userauth_pubkey: disabled because of invalid user");
415 have_sig = packet_get_char();
416 if (datafellows & SSH_BUG_PKAUTH) {
417 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
418 /* no explicit pkalg given */
419 pkblob = packet_get_string(&blen);
421 buffer_append(&b, pkblob, blen);
422 /* so we have to extract the pkalg from the pkblob */
423 pkalg = buffer_get_string(&b, &alen);
426 pkalg = packet_get_string(&alen);
427 pkblob = packet_get_string(&blen);
429 pktype = key_type_from_name(pkalg);
430 if (pktype == KEY_UNSPEC) {
431 /* this is perfectly legal */
432 log("userauth_pubkey: unsupported public key algorithm: %s",
436 key = key_from_blob(pkblob, blen);
438 error("userauth_pubkey: cannot decode key: %s", pkalg);
441 if (key->type != pktype) {
442 error("userauth_pubkey: type mismatch for decoded key "
443 "(received %d, expected %d)", key->type, pktype);
447 sig = packet_get_string(&slen);
450 if (datafellows & SSH_OLD_SESSIONID) {
451 buffer_append(&b, session_id2, session_id2_len);
453 buffer_put_string(&b, session_id2, session_id2_len);
455 /* reconstruct packet */
456 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
457 buffer_put_cstring(&b, authctxt->user);
458 buffer_put_cstring(&b,
459 datafellows & SSH_BUG_PKSERVICE ?
462 if (datafellows & SSH_BUG_PKAUTH) {
463 buffer_put_char(&b, have_sig);
465 buffer_put_cstring(&b, "publickey");
466 buffer_put_char(&b, have_sig);
467 buffer_put_cstring(&b, pkalg);
469 buffer_put_string(&b, pkblob, blen);
473 /* test for correct signature */
475 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
476 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
477 buffer_len(&b))) == 1)
482 debug("test whether pkalg/pkblob are acceptable");
485 /* XXX fake reply and always send PK_OK ? */
487 * XXX this allows testing whether a user is allowed
488 * to login: if you happen to have a valid pubkey this
489 * message is sent. the message is NEVER sent at all
490 * if a user is not allowed to login. is this an
493 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
494 packet_start(SSH2_MSG_USERAUTH_PK_OK);
495 packet_put_string(pkalg, alen);
496 packet_put_string(pkblob, blen);
499 authctxt->postponed = 1;
502 if (authenticated != 1)
503 auth_clear_options();
505 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
511 if (check_nt_auth(0, authctxt->pw) == 0)
514 return authenticated;
518 userauth_hostbased(Authctxt *authctxt)
522 char *pkalg, *cuser, *chost, *service;
523 u_char *pkblob, *sig;
524 u_int alen, blen, slen;
526 int authenticated = 0;
528 if (!authctxt->valid) {
529 debug2("userauth_hostbased: disabled because of invalid user");
532 pkalg = packet_get_string(&alen);
533 pkblob = packet_get_string(&blen);
534 chost = packet_get_string(NULL);
535 cuser = packet_get_string(NULL);
536 sig = packet_get_string(&slen);
538 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
539 cuser, chost, pkalg, slen);
543 buffer_append(&b, sig, slen);
547 pktype = key_type_from_name(pkalg);
548 if (pktype == KEY_UNSPEC) {
549 /* this is perfectly legal */
550 log("userauth_hostbased: unsupported "
551 "public key algorithm: %s", pkalg);
554 key = key_from_blob(pkblob, blen);
556 error("userauth_hostbased: cannot decode key: %s", pkalg);
559 if (key->type != pktype) {
560 error("userauth_hostbased: type mismatch for decoded key "
561 "(received %d, expected %d)", key->type, pktype);
564 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
567 buffer_put_string(&b, session_id2, session_id2_len);
568 /* reconstruct packet */
569 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
570 buffer_put_cstring(&b, authctxt->user);
571 buffer_put_cstring(&b, service);
572 buffer_put_cstring(&b, "hostbased");
573 buffer_put_string(&b, pkalg, alen);
574 buffer_put_string(&b, pkblob, blen);
575 buffer_put_cstring(&b, chost);
576 buffer_put_cstring(&b, cuser);
580 /* test for allowed key and correct signature */
582 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
583 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
584 buffer_len(&b))) == 1)
589 debug2("userauth_hostbased: authenticated %d", authenticated);
597 return authenticated;
600 /* get current user */
605 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
611 authmethods_get(void)
613 Authmethod *method = NULL;
618 for (method = authmethods; method->name != NULL; method++) {
619 if (strcmp(method->name, "none") == 0)
621 if (method->enabled != NULL && *(method->enabled) != 0) {
622 if (buffer_len(&b) > 0)
623 buffer_append(&b, ",", 1);
624 buffer_append(&b, method->name, strlen(method->name));
627 buffer_append(&b, "\0", 1);
628 list = xstrdup(buffer_ptr(&b));
634 authmethod_lookup(const char *name)
636 Authmethod *method = NULL;
638 for (method = authmethods; method->name != NULL; method++)
639 if (method->enabled != NULL &&
640 *(method->enabled) != 0 &&
641 strcmp(name, method->name) == 0)
643 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
647 /* return 1 if user allows given key */
649 user_key_allowed2(struct passwd *pw, Key *key, char *file)
662 /* Temporarily use the user's uid. */
663 temporarily_use_uid(pw);
665 debug("trying public key file %s", file);
667 /* Fail quietly if file does not exist */
668 if (stat(file, &st) < 0) {
669 /* Restore the privileged uid. */
673 /* Open the file containing the authorized keys. */
674 f = fopen(file, "r");
676 /* Restore the privileged uid. */
680 if (options.strict_modes &&
681 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
683 log("Authentication refused: %s", line);
689 found = key_new(key->type);
691 while (fgets(line, sizeof(line), f)) {
692 char *cp, *options = NULL;
694 /* Skip leading whitespace, empty and comment lines. */
695 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
697 if (!*cp || *cp == '\n' || *cp == '#')
700 if (key_read(found, &cp) != 1) {
701 /* no key? check if there are options for this key */
703 debug2("user_key_allowed: check options: '%s'", cp);
705 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
706 if (*cp == '\\' && cp[1] == '"')
707 cp++; /* Skip both */
711 /* Skip remaining whitespace. */
712 for (; *cp == ' ' || *cp == '\t'; cp++)
714 if (key_read(found, &cp) != 1) {
715 debug2("user_key_allowed: advance: '%s'", cp);
716 /* still no key? advance to next line*/
720 if (key_equal(found, key) &&
721 auth_parse_options(pw, options, file, linenum) == 1) {
723 debug("matching key found: file %s, line %lu",
725 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
726 verbose("Found matching %s key: %s",
727 key_type(found), fp);
736 debug2("key not found");
740 /* check whether given key is in .ssh/authorized_keys* */
742 user_key_allowed(struct passwd *pw, Key *key)
747 file = authorized_keys_file(pw);
748 success = user_key_allowed2(pw, key, file);
753 /* try suffix "2" for backward compat, too */
754 file = authorized_keys_file2(pw);
755 success = user_key_allowed2(pw, key, file);
760 /* return 1 if given hostkey is allowed */
762 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
765 const char *resolvedname, *ipaddr, *lookup;
766 HostStatus host_status;
769 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
770 ipaddr = get_remote_ipaddr();
772 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
773 chost, resolvedname, ipaddr);
775 if (options.hostbased_uses_name_from_packet_only) {
776 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
780 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
781 debug2("stripping trailing dot from chost %s", chost);
782 chost[len - 1] = '\0';
784 if (strcasecmp(resolvedname, chost) != 0)
785 log("userauth_hostbased mismatch: "
786 "client sends %s, but we resolve %s to %s",
787 chost, ipaddr, resolvedname);
788 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
790 lookup = resolvedname;
792 debug2("userauth_hostbased: access allowed by auth_rhosts2");
794 host_status = check_key_in_hostfiles(pw, key, lookup,
795 _PATH_SSH_SYSTEM_HOSTFILE,
796 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
798 /* backward compat if no key has been found. */
799 if (host_status == HOST_NEW)
800 host_status = check_key_in_hostfiles(pw, key, lookup,
801 _PATH_SSH_SYSTEM_HOSTFILE2,
802 options.ignore_user_known_hosts ? NULL :
803 _PATH_SSH_USER_HOSTFILE2);
805 return (host_status == HOST_OK);