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>
47 #include "pathnames.h"
49 #include "auth-options.h"
56 extern ServerOptions options;
57 extern u_char *session_id2;
58 extern int session_id2_len;
60 static 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 static int user_key_allowed(struct passwd *, Key *);
79 static 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);
128 do_authenticated(authctxt);
132 input_service_request(int type, u_int32_t seq, void *ctxt)
134 Authctxt *authctxt = ctxt;
137 char *service = packet_get_string(&len);
140 if (authctxt == NULL)
141 fatal("input_service_request: no authctxt");
143 if (strcmp(service, "ssh-userauth") == 0) {
144 if (!authctxt->success) {
146 /* now we can handle user-auth requests */
147 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
150 /* XXX all other service requests are denied */
153 packet_start(SSH2_MSG_SERVICE_ACCEPT);
154 packet_put_cstring(service);
158 debug("bad service request %s", service);
159 packet_disconnect("bad service request %s", service);
165 input_userauth_request(int type, u_int32_t seq, void *ctxt)
167 Authctxt *authctxt = ctxt;
168 Authmethod *m = NULL;
169 char *user, *service, *method, *style = NULL;
170 int authenticated = 0;
172 if (authctxt == NULL)
173 fatal("input_userauth_request: no authctxt");
175 user = packet_get_string(NULL);
176 service = packet_get_string(NULL);
177 method = packet_get_string(NULL);
178 debug("userauth-request for user %s service %s method %s", user, service, method);
179 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
181 if ((style = strchr(user, ':')) != NULL)
184 if (authctxt->attempt++ == 0) {
185 /* setup auth context */
186 struct passwd *pw = NULL;
188 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
189 authctxt->pw = pwcopy(pw);
191 debug2("input_userauth_request: setting up authctxt for %s", user);
193 start_pam(pw->pw_name);
196 log("input_userauth_request: illegal user %s", user);
201 setproctitle("%s", pw ? user : "unknown");
202 authctxt->user = xstrdup(user);
203 authctxt->service = xstrdup(service);
204 authctxt->style = style ? xstrdup(style) : NULL;
205 } else if (strcmp(user, authctxt->user) != 0 ||
206 strcmp(service, authctxt->service) != 0) {
207 packet_disconnect("Change of username or service not allowed: "
208 "(%s,%s) -> (%s,%s)",
209 authctxt->user, authctxt->service, user, service);
212 auth2_challenge_stop(authctxt);
213 authctxt->postponed = 0;
215 /* try to authenticate user */
216 m = authmethod_lookup(method);
218 debug2("input_userauth_request: try method %s", method);
219 authenticated = m->userauth(authctxt);
221 userauth_finish(authctxt, authenticated, method);
229 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
233 if (!authctxt->valid && authenticated)
234 fatal("INTERNAL ERROR: authenticated invalid user %s",
237 /* Special handling for root */
238 if (authenticated && authctxt->pw->pw_uid == 0 &&
239 !auth_root_allowed(method))
243 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
248 /* Log before sending the reply */
249 auth_log(authctxt, authenticated, method, " ssh2");
251 if (authctxt->postponed)
254 /* XXX todo: check if multiple auth methods are needed */
255 if (authenticated == 1) {
256 /* turn off userauth */
257 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
258 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
261 /* now we can break out */
262 authctxt->success = 1;
264 if (authctxt->failures++ > AUTH_FAIL_MAX) {
265 #ifdef WITH_AIXAUTHENTICATE
266 loginfailed(authctxt->user,
267 get_canonical_hostname(options.verify_reverse_mapping),
269 #endif /* WITH_AIXAUTHENTICATE */
270 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
272 methods = authmethods_get();
273 packet_start(SSH2_MSG_USERAUTH_FAILURE);
274 packet_put_cstring(methods);
275 packet_put_char(0); /* XXX partial success, unused */
283 userauth_banner(void)
290 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
292 if ((fd = open(options.banner, O_RDONLY)) < 0)
294 if (fstat(fd, &st) < 0)
297 banner = xmalloc(len + 1);
298 if ((n = read(fd, banner, len)) < 0)
301 packet_start(SSH2_MSG_USERAUTH_BANNER);
302 packet_put_cstring(banner);
303 packet_put_cstring(""); /* language, unused */
305 debug("userauth_banner: sent");
314 userauth_none(Authctxt *authctxt)
316 /* disable method "none", only allowed one time */
317 Authmethod *m = authmethod_lookup("none");
323 if (authctxt->valid == 0)
327 if (check_nt_auth(1, authctxt->pw) == 0)
331 return auth_pam_password(authctxt->pw, "");
332 #elif defined(HAVE_OSF_SIA)
334 #else /* !HAVE_OSF_SIA && !USE_PAM */
335 return auth_password(authctxt, "");
340 userauth_passwd(Authctxt *authctxt)
343 int authenticated = 0;
346 change = packet_get_char();
348 log("password change not supported");
349 password = packet_get_string(&len);
351 if (authctxt->valid &&
353 check_nt_auth(1, authctxt->pw) &&
356 auth_pam_password(authctxt->pw, password) == 1)
357 #elif defined(HAVE_OSF_SIA)
358 auth_sia_password(authctxt->user, password) == 1)
359 #else /* !USE_PAM && !HAVE_OSF_SIA */
360 auth_password(authctxt, password) == 1)
363 memset(password, 0, len);
365 return authenticated;
369 userauth_kbdint(Authctxt *authctxt)
371 int authenticated = 0;
374 lang = packet_get_string(NULL);
375 devs = packet_get_string(NULL);
378 debug("keyboard-interactive devs %s", devs);
380 if (options.challenge_response_authentication)
381 authenticated = auth2_challenge(authctxt, devs);
384 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
385 authenticated = auth2_pam(authctxt);
390 if (check_nt_auth(0, authctxt->pw) == 0)
393 return authenticated;
397 userauth_pubkey(Authctxt *authctxt)
402 u_char *pkblob, *sig;
403 u_int alen, blen, slen;
404 int have_sig, pktype;
405 int authenticated = 0;
407 if (!authctxt->valid) {
408 debug2("userauth_pubkey: disabled because of invalid user");
411 have_sig = packet_get_char();
412 if (datafellows & SSH_BUG_PKAUTH) {
413 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
414 /* no explicit pkalg given */
415 pkblob = packet_get_string(&blen);
417 buffer_append(&b, pkblob, blen);
418 /* so we have to extract the pkalg from the pkblob */
419 pkalg = buffer_get_string(&b, &alen);
422 pkalg = packet_get_string(&alen);
423 pkblob = packet_get_string(&blen);
425 pktype = key_type_from_name(pkalg);
426 if (pktype == KEY_UNSPEC) {
427 /* this is perfectly legal */
428 log("userauth_pubkey: unsupported public key algorithm: %s",
432 key = key_from_blob(pkblob, blen);
434 error("userauth_pubkey: cannot decode key: %s", pkalg);
437 if (key->type != pktype) {
438 error("userauth_pubkey: type mismatch for decoded key "
439 "(received %d, expected %d)", key->type, pktype);
443 sig = packet_get_string(&slen);
446 if (datafellows & SSH_OLD_SESSIONID) {
447 buffer_append(&b, session_id2, session_id2_len);
449 buffer_put_string(&b, session_id2, session_id2_len);
451 /* reconstruct packet */
452 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
453 buffer_put_cstring(&b, authctxt->user);
454 buffer_put_cstring(&b,
455 datafellows & SSH_BUG_PKSERVICE ?
458 if (datafellows & SSH_BUG_PKAUTH) {
459 buffer_put_char(&b, have_sig);
461 buffer_put_cstring(&b, "publickey");
462 buffer_put_char(&b, have_sig);
463 buffer_put_cstring(&b, pkalg);
465 buffer_put_string(&b, pkblob, blen);
469 /* test for correct signature */
470 if (user_key_allowed(authctxt->pw, key) &&
471 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
476 debug("test whether pkalg/pkblob are acceptable");
479 /* XXX fake reply and always send PK_OK ? */
481 * XXX this allows testing whether a user is allowed
482 * to login: if you happen to have a valid pubkey this
483 * message is sent. the message is NEVER sent at all
484 * if a user is not allowed to login. is this an
487 if (user_key_allowed(authctxt->pw, key)) {
488 packet_start(SSH2_MSG_USERAUTH_PK_OK);
489 packet_put_string(pkalg, alen);
490 packet_put_string(pkblob, blen);
493 authctxt->postponed = 1;
496 if (authenticated != 1)
497 auth_clear_options();
499 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
505 if (check_nt_auth(0, authctxt->pw) == 0)
508 return authenticated;
512 userauth_hostbased(Authctxt *authctxt)
516 char *pkalg, *cuser, *chost, *service;
517 u_char *pkblob, *sig;
518 u_int alen, blen, slen;
520 int authenticated = 0;
522 if (!authctxt->valid) {
523 debug2("userauth_hostbased: disabled because of invalid user");
526 pkalg = packet_get_string(&alen);
527 pkblob = packet_get_string(&blen);
528 chost = packet_get_string(NULL);
529 cuser = packet_get_string(NULL);
530 sig = packet_get_string(&slen);
532 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
533 cuser, chost, pkalg, slen);
537 buffer_append(&b, sig, slen);
541 pktype = key_type_from_name(pkalg);
542 if (pktype == KEY_UNSPEC) {
543 /* this is perfectly legal */
544 log("userauth_hostbased: unsupported "
545 "public key algorithm: %s", pkalg);
548 key = key_from_blob(pkblob, blen);
550 error("userauth_hostbased: cannot decode key: %s", pkalg);
553 if (key->type != pktype) {
554 error("userauth_hostbased: type mismatch for decoded key "
555 "(received %d, expected %d)", key->type, pktype);
558 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
561 buffer_put_string(&b, session_id2, session_id2_len);
562 /* reconstruct packet */
563 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
564 buffer_put_cstring(&b, authctxt->user);
565 buffer_put_cstring(&b, service);
566 buffer_put_cstring(&b, "hostbased");
567 buffer_put_string(&b, pkalg, alen);
568 buffer_put_string(&b, pkblob, blen);
569 buffer_put_cstring(&b, chost);
570 buffer_put_cstring(&b, cuser);
574 /* test for allowed key and correct signature */
575 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
576 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
581 debug2("userauth_hostbased: authenticated %d", authenticated);
589 return authenticated;
592 /* get current user */
597 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
603 authmethods_get(void)
605 Authmethod *method = NULL;
610 for (method = authmethods; method->name != NULL; method++) {
611 if (strcmp(method->name, "none") == 0)
613 if (method->enabled != NULL && *(method->enabled) != 0) {
614 if (buffer_len(&b) > 0)
615 buffer_append(&b, ",", 1);
616 buffer_append(&b, method->name, strlen(method->name));
619 buffer_append(&b, "\0", 1);
620 list = xstrdup(buffer_ptr(&b));
626 authmethod_lookup(const char *name)
628 Authmethod *method = NULL;
630 for (method = authmethods; method->name != NULL; method++)
631 if (method->enabled != NULL &&
632 *(method->enabled) != 0 &&
633 strcmp(name, method->name) == 0)
635 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
639 /* return 1 if user allows given key */
641 user_key_allowed2(struct passwd *pw, Key *key, char *file)
654 /* Temporarily use the user's uid. */
655 temporarily_use_uid(pw);
657 debug("trying public key file %s", file);
659 /* Fail quietly if file does not exist */
660 if (stat(file, &st) < 0) {
661 /* Restore the privileged uid. */
665 /* Open the file containing the authorized keys. */
666 f = fopen(file, "r");
668 /* Restore the privileged uid. */
672 if (options.strict_modes &&
673 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
675 log("Authentication refused: %s", line);
681 found = key_new(key->type);
683 while (fgets(line, sizeof(line), f)) {
684 char *cp, *options = NULL;
686 /* Skip leading whitespace, empty and comment lines. */
687 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
689 if (!*cp || *cp == '\n' || *cp == '#')
692 if (key_read(found, &cp) != 1) {
693 /* no key? check if there are options for this key */
695 debug2("user_key_allowed: check options: '%s'", cp);
697 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
698 if (*cp == '\\' && cp[1] == '"')
699 cp++; /* Skip both */
703 /* Skip remaining whitespace. */
704 for (; *cp == ' ' || *cp == '\t'; cp++)
706 if (key_read(found, &cp) != 1) {
707 debug2("user_key_allowed: advance: '%s'", cp);
708 /* still no key? advance to next line*/
712 if (key_equal(found, key) &&
713 auth_parse_options(pw, options, file, linenum) == 1) {
715 debug("matching key found: file %s, line %lu",
717 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
718 verbose("Found matching %s key: %s",
719 key_type(found), fp);
728 debug2("key not found");
732 /* check whether given key is in .ssh/authorized_keys* */
734 user_key_allowed(struct passwd *pw, Key *key)
739 file = authorized_keys_file(pw);
740 success = user_key_allowed2(pw, key, file);
745 /* try suffix "2" for backward compat, too */
746 file = authorized_keys_file2(pw);
747 success = user_key_allowed2(pw, key, file);
752 /* return 1 if given hostkey is allowed */
754 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
757 const char *resolvedname, *ipaddr, *lookup;
758 HostStatus host_status;
761 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
762 ipaddr = get_remote_ipaddr();
764 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
765 chost, resolvedname, ipaddr);
767 if (options.hostbased_uses_name_from_packet_only) {
768 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
772 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
773 debug2("stripping trailing dot from chost %s", chost);
774 chost[len - 1] = '\0';
776 if (strcasecmp(resolvedname, chost) != 0)
777 log("userauth_hostbased mismatch: "
778 "client sends %s, but we resolve %s to %s",
779 chost, ipaddr, resolvedname);
780 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
782 lookup = resolvedname;
784 debug2("userauth_hostbased: access allowed by auth_rhosts2");
786 host_status = check_key_in_hostfiles(pw, key, lookup,
787 _PATH_SSH_SYSTEM_HOSTFILE,
788 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
790 /* backward compat if no key has been found. */
791 if (host_status == HOST_NEW)
792 host_status = check_key_in_hostfiles(pw, key, lookup,
793 _PATH_SSH_SYSTEM_HOSTFILE2,
794 options.ignore_user_known_hosts ? NULL :
795 _PATH_SSH_USER_HOSTFILE2);
797 return (host_status == HOST_OK);