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.52 2001/04/12 19:15:24 markus Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
53 #include "tildexpand.h"
56 extern ServerOptions options;
57 extern u_char *session_id2;
58 extern int session_id2_len;
60 #ifdef WITH_AIXAUTHENTICATE
61 extern char *aixloginmsg;
64 static Authctxt *x_authctxt = NULL;
67 typedef struct Authmethod Authmethod;
70 int (*userauth)(Authctxt *authctxt);
76 void input_service_request(int type, int plen, void *ctxt);
77 void input_userauth_request(int type, int plen, void *ctxt);
78 void protocol_error(int type, int plen, void *ctxt);
81 Authmethod *authmethod_lookup(const char *name);
82 char *authmethods_get(void);
83 int user_key_allowed(struct passwd *pw, Key *key);
85 hostbased_key_allowed(struct passwd *pw, const char *cuser, const char *chost,
89 void userauth_banner(void);
90 void userauth_reply(Authctxt *authctxt, int authenticated);
91 int userauth_none(Authctxt *authctxt);
92 int userauth_passwd(Authctxt *authctxt);
93 int userauth_pubkey(Authctxt *authctxt);
94 int userauth_hostbased(Authctxt *authctxt);
95 int userauth_kbdint(Authctxt *authctxt);
97 Authmethod authmethods[] = {
103 &options.pubkey_authentication},
106 &options.password_authentication},
107 {"keyboard-interactive",
109 &options.kbd_interactive_authentication},
112 &options.hostbased_authentication},
117 * loop until authctxt->success == TRUE
123 Authctxt *authctxt = authctxt_new();
125 x_authctxt = authctxt; /*XXX*/
127 /* challenge-reponse is implemented via keyboard interactive */
128 if (options.challenge_reponse_authentication)
129 options.kbd_interactive_authentication = 1;
131 dispatch_init(&protocol_error);
132 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
133 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
134 do_authenticated(authctxt);
138 protocol_error(int type, int plen, void *ctxt)
140 log("auth: protocol error: type %d plen %d", type, plen);
141 packet_start(SSH2_MSG_UNIMPLEMENTED);
148 input_service_request(int type, int plen, void *ctxt)
150 Authctxt *authctxt = ctxt;
153 char *service = packet_get_string(&len);
156 if (authctxt == NULL)
157 fatal("input_service_request: no authctxt");
159 if (strcmp(service, "ssh-userauth") == 0) {
160 if (!authctxt->success) {
162 /* now we can handle user-auth requests */
163 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
166 /* XXX all other service requests are denied */
169 packet_start(SSH2_MSG_SERVICE_ACCEPT);
170 packet_put_cstring(service);
174 debug("bad service request %s", service);
175 packet_disconnect("bad service request %s", service);
181 input_userauth_request(int type, int plen, void *ctxt)
183 Authctxt *authctxt = ctxt;
184 Authmethod *m = NULL;
185 char *user, *service, *method, *style = NULL;
186 int authenticated = 0;
188 if (authctxt == NULL)
189 fatal("input_userauth_request: no authctxt");
191 user = packet_get_string(NULL);
192 service = packet_get_string(NULL);
193 method = packet_get_string(NULL);
194 debug("userauth-request for user %s service %s method %s", user, service, method);
195 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
197 if ((style = strchr(user, ':')) != NULL)
200 if (authctxt->attempt++ == 0) {
201 /* setup auth context */
202 struct passwd *pw = NULL;
204 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
205 authctxt->pw = pwcopy(pw);
207 debug2("input_userauth_request: setting up authctxt for %s", user);
209 start_pam(pw->pw_name);
212 log("input_userauth_request: illegal user %s", user);
217 setproctitle("%s", pw ? user : "unknown");
218 authctxt->user = xstrdup(user);
219 authctxt->service = xstrdup(service);
220 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
221 } else if (authctxt->valid) {
222 if (strcmp(user, authctxt->user) != 0 ||
223 strcmp(service, authctxt->service) != 0) {
224 log("input_userauth_request: mismatch: (%s,%s)!=(%s,%s)",
225 user, service, authctxt->user, authctxt->service);
230 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
231 authctxt->postponed = 0;
234 auth_close(authctxt->as);
239 /* try to authenticate user */
240 m = authmethod_lookup(method);
242 debug2("input_userauth_request: try method %s", method);
243 authenticated = m->userauth(authctxt);
245 userauth_finish(authctxt, authenticated, method);
253 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
255 if (!authctxt->valid && authenticated)
256 fatal("INTERNAL ERROR: authenticated invalid user %s",
259 /* Special handling for root */
260 if (authenticated && authctxt->pw->pw_uid == 0 &&
261 !auth_root_allowed(method))
265 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
270 /* Log before sending the reply */
271 auth_log(authctxt, authenticated, method, " ssh2");
273 if (!authctxt->postponed)
274 userauth_reply(authctxt, authenticated);
278 userauth_banner(void)
285 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
287 if ((fd = open(options.banner, O_RDONLY)) < 0)
289 if (fstat(fd, &st) < 0)
292 banner = xmalloc(len + 1);
293 if ((n = read(fd, banner, len)) < 0)
296 packet_start(SSH2_MSG_USERAUTH_BANNER);
297 packet_put_cstring(banner);
298 packet_put_cstring(""); /* language, unused */
300 debug("userauth_banner: sent");
309 userauth_reply(Authctxt *authctxt, int authenticated)
313 /* XXX todo: check if multiple auth methods are needed */
314 if (authenticated == 1) {
315 #ifdef WITH_AIXAUTHENTICATE
316 /* We don't have a pty yet, so just label the line as "ssh" */
317 if (loginsuccess(authctxt->user?authctxt->user:"NOUSER",
318 get_canonical_hostname(options.reverse_mapping_check),
319 "ssh", &aixloginmsg) < 0)
321 #endif /* WITH_AIXAUTHENTICATE */
322 /* turn off userauth */
323 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
324 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
327 /* now we can break out */
328 authctxt->success = 1;
330 if (authctxt->failures++ > AUTH_FAIL_MAX)
331 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
332 methods = authmethods_get();
333 packet_start(SSH2_MSG_USERAUTH_FAILURE);
334 packet_put_cstring(methods);
335 packet_put_char(0); /* XXX partial success, unused */
343 userauth_none(Authctxt *authctxt)
345 /* disable method "none", only allowed one time */
346 Authmethod *m = authmethod_lookup("none");
352 if (authctxt->valid == 0)
356 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
360 return auth_pam_password(authctxt->pw, "");
361 #elif defined(HAVE_OSF_SIA)
363 #else /* !HAVE_OSF_SIA && !USE_PAM */
364 return auth_password(authctxt, "");
369 userauth_passwd(Authctxt *authctxt)
372 int authenticated = 0;
375 change = packet_get_char();
377 log("password change not supported");
378 password = packet_get_string(&len);
380 if (authctxt->valid &&
382 check_nt_auth(1, authctxt->pw->pw_uid) &&
385 auth_pam_password(authctxt->pw, password) == 1)
386 #elif defined(HAVE_OSF_SIA)
387 auth_sia_password(authctxt->user, password) == 1)
388 #else /* !USE_PAM && !HAVE_OSF_SIA */
389 auth_password(authctxt, password) == 1)
392 memset(password, 0, len);
394 return authenticated;
398 userauth_kbdint(Authctxt *authctxt)
400 int authenticated = 0;
404 lang = packet_get_string(NULL);
405 devs = packet_get_string(NULL);
408 debug("keyboard-interactive language %s devs %s", lang, devs);
410 if (options.challenge_reponse_authentication)
411 authenticated = auth2_challenge(authctxt, devs);
414 if (authenticated == 0)
415 authenticated = auth2_pam(authctxt);
420 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
423 return authenticated;
427 userauth_pubkey(Authctxt *authctxt)
431 char *pkalg, *pkblob, *sig;
432 u_int alen, blen, slen;
433 int have_sig, pktype;
434 int authenticated = 0;
436 if (!authctxt->valid) {
437 debug2("userauth_pubkey: disabled because of invalid user");
440 have_sig = packet_get_char();
441 if (datafellows & SSH_BUG_PKAUTH) {
442 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
443 /* no explicit pkalg given */
444 pkblob = packet_get_string(&blen);
446 buffer_append(&b, pkblob, blen);
447 /* so we have to extract the pkalg from the pkblob */
448 pkalg = buffer_get_string(&b, &alen);
451 pkalg = packet_get_string(&alen);
452 pkblob = packet_get_string(&blen);
454 pktype = key_type_from_name(pkalg);
455 if (pktype == KEY_UNSPEC) {
456 /* this is perfectly legal */
457 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
462 key = key_from_blob(pkblob, blen);
465 sig = packet_get_string(&slen);
468 if (datafellows & SSH_OLD_SESSIONID) {
469 buffer_append(&b, session_id2, session_id2_len);
471 buffer_put_string(&b, session_id2, session_id2_len);
473 /* reconstruct packet */
474 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
475 buffer_put_cstring(&b, authctxt->user);
476 buffer_put_cstring(&b,
477 datafellows & SSH_BUG_PKSERVICE ?
480 if (datafellows & SSH_BUG_PKAUTH) {
481 buffer_put_char(&b, have_sig);
483 buffer_put_cstring(&b, "publickey");
484 buffer_put_char(&b, have_sig);
485 buffer_put_cstring(&b, key_ssh_name(key));
487 buffer_put_string(&b, pkblob, blen);
491 /* test for correct signature */
492 if (user_key_allowed(authctxt->pw, key) &&
493 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
498 debug("test whether pkalg/pkblob are acceptable");
501 /* XXX fake reply and always send PK_OK ? */
503 * XXX this allows testing whether a user is allowed
504 * to login: if you happen to have a valid pubkey this
505 * message is sent. the message is NEVER sent at all
506 * if a user is not allowed to login. is this an
509 if (user_key_allowed(authctxt->pw, key)) {
510 packet_start(SSH2_MSG_USERAUTH_PK_OK);
511 packet_put_string(pkalg, alen);
512 packet_put_string(pkblob, blen);
515 authctxt->postponed = 1;
518 if (authenticated != 1)
519 auth_clear_options();
522 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
526 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
529 return authenticated;
533 userauth_hostbased(Authctxt *authctxt)
537 char *pkalg, *pkblob, *sig;
539 u_int alen, blen, slen;
541 int authenticated = 0;
543 if (!authctxt->valid) {
544 debug2("userauth_hostbased: disabled because of invalid user");
547 pkalg = packet_get_string(&alen);
548 pkblob = packet_get_string(&blen);
549 chost = packet_get_string(NULL);
550 cuser = packet_get_string(NULL);
551 sig = packet_get_string(&slen);
553 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
554 cuser, chost, pkalg, slen);
558 buffer_append(&b, sig, slen);
562 pktype = key_type_from_name(pkalg);
563 if (pktype == KEY_UNSPEC) {
564 /* this is perfectly legal */
565 log("userauth_hostbased: unsupported "
566 "public key algorithm: %s", pkalg);
569 key = key_from_blob(pkblob, blen);
571 debug("userauth_hostbased: cannot decode key: %s", pkalg);
575 if (datafellows & SSH_OLD_SESSIONID) {
576 buffer_append(&b, session_id2, session_id2_len);
578 buffer_put_string(&b, session_id2, session_id2_len);
580 if (datafellows & SSH_BUG_HBSERVICE)
581 debug("SSH_BUG_HBSERVICE");
582 /* reconstruct packet */
583 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
584 buffer_put_cstring(&b, authctxt->user);
585 buffer_put_cstring(&b,
586 datafellows & SSH_BUG_HBSERVICE ?
589 buffer_put_cstring(&b, "hostbased");
590 buffer_put_string(&b, pkalg, alen);
591 buffer_put_string(&b, pkblob, blen);
592 buffer_put_cstring(&b, chost);
593 buffer_put_cstring(&b, cuser);
597 /* test for allowed key and correct signature */
598 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
599 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
606 debug2("userauth_hostbased: authenticated %d", authenticated);
612 return authenticated;
615 /* get current user */
620 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
626 authmethods_get(void)
628 Authmethod *method = NULL;
632 for (method = authmethods; method->name != NULL; method++) {
633 if (strcmp(method->name, "none") == 0)
635 if (method->enabled != NULL && *(method->enabled) != 0) {
637 size += strlen(DELIM);
638 size += strlen(method->name);
641 size++; /* trailing '\0' */
642 list = xmalloc(size);
645 for (method = authmethods; method->name != NULL; method++) {
646 if (strcmp(method->name, "none") == 0)
648 if (method->enabled != NULL && *(method->enabled) != 0) {
650 strlcat(list, DELIM, size);
651 strlcat(list, method->name, size);
658 authmethod_lookup(const char *name)
660 Authmethod *method = NULL;
662 for (method = authmethods; method->name != NULL; method++)
663 if (method->enabled != NULL &&
664 *(method->enabled) != 0 &&
665 strcmp(name, method->name) == 0)
667 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
671 /* return 1 if user allows given key */
673 user_key_allowed(struct passwd *pw, Key *key)
675 char line[8192], file[MAXPATHLEN];
685 /* Temporarily use the user's uid. */
686 temporarily_use_uid(pw);
688 /* The authorized keys. */
689 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
690 _PATH_SSH_USER_PERMITTED_KEYS2);
692 /* Fail quietly if file does not exist */
693 if (stat(file, &st) < 0) {
694 /* Restore the privileged uid. */
698 /* Open the file containing the authorized keys. */
699 f = fopen(file, "r");
701 /* Restore the privileged uid. */
705 if (options.strict_modes) {
708 /* Check open file in order to avoid open/stat races */
709 if (fstat(fileno(f), &st) < 0 ||
710 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
711 (st.st_mode & 022) != 0) {
712 snprintf(buf, sizeof buf,
713 "%s authentication refused for %.100s: "
714 "bad ownership or modes for '%s'.",
715 key_type(key), pw->pw_name, file);
718 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
720 static const char *check[] = {
721 "", _PATH_SSH_USER_DIR, NULL
723 for (i = 0; check[i]; i++) {
724 snprintf(line, sizeof line, "%.500s/%.100s",
725 pw->pw_dir, check[i]);
726 if (stat(line, &st) < 0 ||
727 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
728 (st.st_mode & 022) != 0) {
729 snprintf(buf, sizeof buf,
730 "%s authentication refused for %.100s: "
731 "bad ownership or modes for '%s'.",
732 key_type(key), pw->pw_name, line);
746 found = key_new(key->type);
748 while (fgets(line, sizeof(line), f)) {
749 char *cp, *options = NULL;
751 /* Skip leading whitespace, empty and comment lines. */
752 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
754 if (!*cp || *cp == '\n' || *cp == '#')
757 if (key_read(found, &cp) == -1) {
758 /* no key? check if there are options for this key */
760 debug2("user_key_allowed: check options: '%s'", cp);
762 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
763 if (*cp == '\\' && cp[1] == '"')
764 cp++; /* Skip both */
768 /* Skip remaining whitespace. */
769 for (; *cp == ' ' || *cp == '\t'; cp++)
771 if (key_read(found, &cp) == -1) {
772 debug2("user_key_allowed: advance: '%s'", cp);
773 /* still no key? advance to next line*/
777 if (key_equal(found, key) &&
778 auth_parse_options(pw, options, file, linenum) == 1) {
780 debug("matching key found: file %s, line %ld",
789 debug2("key not found");
793 /* return 1 if given hostkey is allowed */
795 hostbased_key_allowed(struct passwd *pw, const char *cuser, const char *chost,
799 const char *resolvedname, *ipaddr, *lookup;
804 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
805 ipaddr = get_remote_ipaddr();
807 debug2("userauth_hostbased: resolvedname %s ipaddr %s",
808 resolvedname, ipaddr);
810 if (options.hostbased_uses_name_from_packet_only) {
811 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
815 if (strcasecmp(resolvedname, chost) != 0)
816 log("userauth_hostbased mismatch: "
817 "client sends %s, but we resolve %s to %s",
818 chost, ipaddr, resolvedname);
819 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
821 lookup = resolvedname;
823 debug2("userauth_hostbased: access allowed by auth_rhosts2");
825 /* XXX this is copied from auth-rh-rsa.c and should be shared */
826 found = key_new(key->type);
827 host_status = check_host_in_hostfile(_PATH_SSH_SYSTEM_HOSTFILE2, lookup,
830 if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
831 user_hostfile = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE2,
833 if (options.strict_modes &&
834 (stat(user_hostfile, &st) == 0) &&
835 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
836 (st.st_mode & 022) != 0)) {
837 log("Hostbased authentication refused for %.100s: "
838 "bad owner or modes for %.200s",
839 pw->pw_name, user_hostfile);
841 temporarily_use_uid(pw);
842 host_status = check_host_in_hostfile(user_hostfile,
843 lookup, key, found, NULL);
846 xfree(user_hostfile);
850 debug2("userauth_hostbased: key %s for %s", host_status == HOST_OK ?
851 "ok" : "not found", lookup);
852 return (host_status == HOST_OK);