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.87 2002/03/18 01:12:14 provos 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);
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 = 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", pw ? user : "unknown");
203 authctxt->user = xstrdup(user);
204 authctxt->service = xstrdup(service);
205 authctxt->style = style ? xstrdup(style) : NULL;
206 } else if (strcmp(user, authctxt->user) != 0 ||
207 strcmp(service, authctxt->service) != 0) {
208 packet_disconnect("Change of username or service not allowed: "
209 "(%s,%s) -> (%s,%s)",
210 authctxt->user, authctxt->service, user, service);
213 auth2_challenge_stop(authctxt);
214 authctxt->postponed = 0;
216 /* try to authenticate user */
217 m = authmethod_lookup(method);
219 debug2("input_userauth_request: try method %s", method);
220 authenticated = m->userauth(authctxt);
222 userauth_finish(authctxt, authenticated, method);
230 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
234 if (!authctxt->valid && authenticated)
235 fatal("INTERNAL ERROR: authenticated invalid user %s",
238 /* Special handling for root */
239 if (authenticated && authctxt->pw->pw_uid == 0 &&
240 !auth_root_allowed(method))
244 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
249 /* Log before sending the reply */
250 auth_log(authctxt, authenticated, method, " ssh2");
252 if (authctxt->postponed)
255 /* XXX todo: check if multiple auth methods are needed */
256 if (authenticated == 1) {
257 /* turn off userauth */
258 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
259 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
262 /* now we can break out */
263 authctxt->success = 1;
265 if (authctxt->failures++ > AUTH_FAIL_MAX) {
266 #ifdef WITH_AIXAUTHENTICATE
267 loginfailed(authctxt->user,
268 get_canonical_hostname(options.verify_reverse_mapping),
270 #endif /* WITH_AIXAUTHENTICATE */
271 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
273 methods = authmethods_get();
274 packet_start(SSH2_MSG_USERAUTH_FAILURE);
275 packet_put_cstring(methods);
276 packet_put_char(0); /* XXX partial success, unused */
284 userauth_banner(void)
291 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
293 if ((fd = open(options.banner, O_RDONLY)) < 0)
295 if (fstat(fd, &st) < 0)
298 banner = xmalloc(len + 1);
299 if ((n = read(fd, banner, len)) < 0)
302 packet_start(SSH2_MSG_USERAUTH_BANNER);
303 packet_put_cstring(banner);
304 packet_put_cstring(""); /* language, unused */
306 debug("userauth_banner: sent");
315 userauth_none(Authctxt *authctxt)
317 /* disable method "none", only allowed one time */
318 Authmethod *m = authmethod_lookup("none");
324 if (authctxt->valid == 0)
328 if (check_nt_auth(1, authctxt->pw) == 0)
332 return auth_pam_password(authctxt->pw, "");
333 #elif defined(HAVE_OSF_SIA)
335 #else /* !HAVE_OSF_SIA && !USE_PAM */
336 return auth_password(authctxt, "");
341 userauth_passwd(Authctxt *authctxt)
344 int authenticated = 0;
347 change = packet_get_char();
349 log("password change not supported");
350 password = packet_get_string(&len);
352 if (authctxt->valid &&
354 check_nt_auth(1, authctxt->pw) &&
357 auth_pam_password(authctxt->pw, password) == 1)
358 #elif defined(HAVE_OSF_SIA)
359 auth_sia_password(authctxt->user, password) == 1)
360 #else /* !USE_PAM && !HAVE_OSF_SIA */
361 auth_password(authctxt, password) == 1)
364 memset(password, 0, len);
366 return authenticated;
370 userauth_kbdint(Authctxt *authctxt)
372 int authenticated = 0;
375 lang = packet_get_string(NULL);
376 devs = packet_get_string(NULL);
379 debug("keyboard-interactive devs %s", devs);
381 if (options.challenge_response_authentication)
382 authenticated = auth2_challenge(authctxt, devs);
385 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
386 authenticated = auth2_pam(authctxt);
391 if (check_nt_auth(0, authctxt->pw) == 0)
394 return authenticated;
398 userauth_pubkey(Authctxt *authctxt)
403 u_char *pkblob, *sig;
404 u_int alen, blen, slen;
405 int have_sig, pktype;
406 int authenticated = 0;
408 if (!authctxt->valid) {
409 debug2("userauth_pubkey: disabled because of invalid user");
412 have_sig = packet_get_char();
413 if (datafellows & SSH_BUG_PKAUTH) {
414 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
415 /* no explicit pkalg given */
416 pkblob = packet_get_string(&blen);
418 buffer_append(&b, pkblob, blen);
419 /* so we have to extract the pkalg from the pkblob */
420 pkalg = buffer_get_string(&b, &alen);
423 pkalg = packet_get_string(&alen);
424 pkblob = packet_get_string(&blen);
426 pktype = key_type_from_name(pkalg);
427 if (pktype == KEY_UNSPEC) {
428 /* this is perfectly legal */
429 log("userauth_pubkey: unsupported public key algorithm: %s",
433 key = key_from_blob(pkblob, blen);
435 error("userauth_pubkey: cannot decode key: %s", pkalg);
438 if (key->type != pktype) {
439 error("userauth_pubkey: type mismatch for decoded key "
440 "(received %d, expected %d)", key->type, pktype);
444 sig = packet_get_string(&slen);
447 if (datafellows & SSH_OLD_SESSIONID) {
448 buffer_append(&b, session_id2, session_id2_len);
450 buffer_put_string(&b, session_id2, session_id2_len);
452 /* reconstruct packet */
453 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
454 buffer_put_cstring(&b, authctxt->user);
455 buffer_put_cstring(&b,
456 datafellows & SSH_BUG_PKSERVICE ?
459 if (datafellows & SSH_BUG_PKAUTH) {
460 buffer_put_char(&b, have_sig);
462 buffer_put_cstring(&b, "publickey");
463 buffer_put_char(&b, have_sig);
464 buffer_put_cstring(&b, pkalg);
466 buffer_put_string(&b, pkblob, blen);
470 /* test for correct signature */
471 if (user_key_allowed(authctxt->pw, key) &&
472 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
477 debug("test whether pkalg/pkblob are acceptable");
480 /* XXX fake reply and always send PK_OK ? */
482 * XXX this allows testing whether a user is allowed
483 * to login: if you happen to have a valid pubkey this
484 * message is sent. the message is NEVER sent at all
485 * if a user is not allowed to login. is this an
488 if (user_key_allowed(authctxt->pw, key)) {
489 packet_start(SSH2_MSG_USERAUTH_PK_OK);
490 packet_put_string(pkalg, alen);
491 packet_put_string(pkblob, blen);
494 authctxt->postponed = 1;
497 if (authenticated != 1)
498 auth_clear_options();
500 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
506 if (check_nt_auth(0, authctxt->pw) == 0)
509 return authenticated;
513 userauth_hostbased(Authctxt *authctxt)
517 char *pkalg, *cuser, *chost, *service;
518 u_char *pkblob, *sig;
519 u_int alen, blen, slen;
521 int authenticated = 0;
523 if (!authctxt->valid) {
524 debug2("userauth_hostbased: disabled because of invalid user");
527 pkalg = packet_get_string(&alen);
528 pkblob = packet_get_string(&blen);
529 chost = packet_get_string(NULL);
530 cuser = packet_get_string(NULL);
531 sig = packet_get_string(&slen);
533 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
534 cuser, chost, pkalg, slen);
538 buffer_append(&b, sig, slen);
542 pktype = key_type_from_name(pkalg);
543 if (pktype == KEY_UNSPEC) {
544 /* this is perfectly legal */
545 log("userauth_hostbased: unsupported "
546 "public key algorithm: %s", pkalg);
549 key = key_from_blob(pkblob, blen);
551 error("userauth_hostbased: cannot decode key: %s", pkalg);
554 if (key->type != pktype) {
555 error("userauth_hostbased: type mismatch for decoded key "
556 "(received %d, expected %d)", key->type, pktype);
559 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
562 buffer_put_string(&b, session_id2, session_id2_len);
563 /* reconstruct packet */
564 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
565 buffer_put_cstring(&b, authctxt->user);
566 buffer_put_cstring(&b, service);
567 buffer_put_cstring(&b, "hostbased");
568 buffer_put_string(&b, pkalg, alen);
569 buffer_put_string(&b, pkblob, blen);
570 buffer_put_cstring(&b, chost);
571 buffer_put_cstring(&b, cuser);
575 /* test for allowed key and correct signature */
576 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
577 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
582 debug2("userauth_hostbased: authenticated %d", authenticated);
590 return authenticated;
593 /* get current user */
598 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
604 authmethods_get(void)
606 Authmethod *method = NULL;
611 for (method = authmethods; method->name != NULL; method++) {
612 if (strcmp(method->name, "none") == 0)
614 if (method->enabled != NULL && *(method->enabled) != 0) {
615 if (buffer_len(&b) > 0)
616 buffer_append(&b, ",", 1);
617 buffer_append(&b, method->name, strlen(method->name));
620 buffer_append(&b, "\0", 1);
621 list = xstrdup(buffer_ptr(&b));
627 authmethod_lookup(const char *name)
629 Authmethod *method = NULL;
631 for (method = authmethods; method->name != NULL; method++)
632 if (method->enabled != NULL &&
633 *(method->enabled) != 0 &&
634 strcmp(name, method->name) == 0)
636 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
640 /* return 1 if user allows given key */
642 user_key_allowed2(struct passwd *pw, Key *key, char *file)
655 /* Temporarily use the user's uid. */
656 temporarily_use_uid(pw);
658 debug("trying public key file %s", file);
660 /* Fail quietly if file does not exist */
661 if (stat(file, &st) < 0) {
662 /* Restore the privileged uid. */
666 /* Open the file containing the authorized keys. */
667 f = fopen(file, "r");
669 /* Restore the privileged uid. */
673 if (options.strict_modes &&
674 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
676 log("Authentication refused: %s", line);
682 found = key_new(key->type);
684 while (fgets(line, sizeof(line), f)) {
685 char *cp, *options = NULL;
687 /* Skip leading whitespace, empty and comment lines. */
688 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
690 if (!*cp || *cp == '\n' || *cp == '#')
693 if (key_read(found, &cp) != 1) {
694 /* no key? check if there are options for this key */
696 debug2("user_key_allowed: check options: '%s'", cp);
698 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
699 if (*cp == '\\' && cp[1] == '"')
700 cp++; /* Skip both */
704 /* Skip remaining whitespace. */
705 for (; *cp == ' ' || *cp == '\t'; cp++)
707 if (key_read(found, &cp) != 1) {
708 debug2("user_key_allowed: advance: '%s'", cp);
709 /* still no key? advance to next line*/
713 if (key_equal(found, key) &&
714 auth_parse_options(pw, options, file, linenum) == 1) {
716 debug("matching key found: file %s, line %lu",
718 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
719 verbose("Found matching %s key: %s",
720 key_type(found), fp);
729 debug2("key not found");
733 /* check whether given key is in .ssh/authorized_keys* */
735 user_key_allowed(struct passwd *pw, Key *key)
740 file = authorized_keys_file(pw);
741 success = user_key_allowed2(pw, key, file);
746 /* try suffix "2" for backward compat, too */
747 file = authorized_keys_file2(pw);
748 success = user_key_allowed2(pw, key, file);
753 /* return 1 if given hostkey is allowed */
755 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
758 const char *resolvedname, *ipaddr, *lookup;
759 HostStatus host_status;
762 resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
763 ipaddr = get_remote_ipaddr();
765 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
766 chost, resolvedname, ipaddr);
768 if (options.hostbased_uses_name_from_packet_only) {
769 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
773 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
774 debug2("stripping trailing dot from chost %s", chost);
775 chost[len - 1] = '\0';
777 if (strcasecmp(resolvedname, chost) != 0)
778 log("userauth_hostbased mismatch: "
779 "client sends %s, but we resolve %s to %s",
780 chost, ipaddr, resolvedname);
781 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
783 lookup = resolvedname;
785 debug2("userauth_hostbased: access allowed by auth_rhosts2");
787 host_status = check_key_in_hostfiles(pw, key, lookup,
788 _PATH_SSH_SYSTEM_HOSTFILE,
789 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
791 /* backward compat if no key has been found. */
792 if (host_status == HOST_NEW)
793 host_status = check_key_in_hostfiles(pw, key, lookup,
794 _PATH_SSH_SYSTEM_HOSTFILE2,
795 options.ignore_user_known_hosts ? NULL :
796 _PATH_SSH_USER_HOSTFILE2);
798 return (host_status == HOST_OK);