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.61 2001/05/31 10:30:12 markus Exp $");
28 #include <openssl/evp.h>
47 #include "pathnames.h"
49 #include "auth-options.h"
53 #include "tildexpand.h"
57 extern ServerOptions options;
58 extern u_char *session_id2;
59 extern int session_id2_len;
61 #ifdef WITH_AIXAUTHENTICATE
62 extern char *aixloginmsg;
65 static Authctxt *x_authctxt = NULL;
68 typedef struct Authmethod Authmethod;
71 int (*userauth)(Authctxt *authctxt);
77 void input_service_request(int type, int plen, void *ctxt);
78 void input_userauth_request(int type, int plen, void *ctxt);
79 void protocol_error(int type, int plen, void *ctxt);
82 Authmethod *authmethod_lookup(const char *name);
83 char *authmethods_get(void);
84 int user_key_allowed(struct passwd *pw, Key *key);
86 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
90 void userauth_banner(void);
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_response_authentication)
129 options.kbd_interactive_authentication = 1;
130 if (options.pam_authentication_via_kbd_int)
131 options.kbd_interactive_authentication = 1;
133 dispatch_init(&protocol_error);
134 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
135 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
136 do_authenticated(authctxt);
140 protocol_error(int type, int plen, void *ctxt)
142 log("auth: protocol error: type %d plen %d", type, plen);
143 packet_start(SSH2_MSG_UNIMPLEMENTED);
150 input_service_request(int type, int plen, void *ctxt)
152 Authctxt *authctxt = ctxt;
155 char *service = packet_get_string(&len);
158 if (authctxt == NULL)
159 fatal("input_service_request: no authctxt");
161 if (strcmp(service, "ssh-userauth") == 0) {
162 if (!authctxt->success) {
164 /* now we can handle user-auth requests */
165 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
168 /* XXX all other service requests are denied */
171 packet_start(SSH2_MSG_SERVICE_ACCEPT);
172 packet_put_cstring(service);
176 debug("bad service request %s", service);
177 packet_disconnect("bad service request %s", service);
183 input_userauth_request(int type, int plen, void *ctxt)
185 Authctxt *authctxt = ctxt;
186 Authmethod *m = NULL;
187 char *user, *service, *method, *style = NULL;
188 int authenticated = 0;
190 if (authctxt == NULL)
191 fatal("input_userauth_request: no authctxt");
193 user = packet_get_string(NULL);
194 service = packet_get_string(NULL);
195 method = packet_get_string(NULL);
196 debug("userauth-request for user %s service %s method %s", user, service, method);
197 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
199 if ((style = strchr(user, ':')) != NULL)
202 if (authctxt->attempt++ == 0) {
203 /* setup auth context */
204 struct passwd *pw = NULL;
206 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
207 authctxt->pw = pwcopy(pw);
209 debug2("input_userauth_request: setting up authctxt for %s", user);
211 start_pam(pw->pw_name);
214 log("input_userauth_request: illegal user %s", user);
219 setproctitle("%s", pw ? user : "unknown");
220 authctxt->user = xstrdup(user);
221 authctxt->service = xstrdup(service);
222 authctxt->style = style ? xstrdup(style) : NULL; /* currently unused */
223 } else if (authctxt->valid) {
224 if (strcmp(user, authctxt->user) != 0 ||
225 strcmp(service, authctxt->service) != 0) {
226 log("input_userauth_request: mismatch: (%s,%s)!=(%s,%s)",
227 user, service, authctxt->user, authctxt->service);
232 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
233 authctxt->postponed = 0;
236 auth_close(authctxt->as);
241 /* try to authenticate user */
242 m = authmethod_lookup(method);
244 debug2("input_userauth_request: try method %s", method);
245 authenticated = m->userauth(authctxt);
247 userauth_finish(authctxt, authenticated, method);
255 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
259 if (!authctxt->valid && authenticated)
260 fatal("INTERNAL ERROR: authenticated invalid user %s",
263 /* Special handling for root */
264 if (authenticated && authctxt->pw->pw_uid == 0 &&
265 !auth_root_allowed(method))
269 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
274 /* Log before sending the reply */
275 auth_log(authctxt, authenticated, method, " ssh2");
277 if (authctxt->postponed)
280 /* XXX todo: check if multiple auth methods are needed */
281 if (authenticated == 1) {
282 /* turn off userauth */
283 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
284 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
287 /* now we can break out */
288 authctxt->success = 1;
290 if (authctxt->failures++ > AUTH_FAIL_MAX)
291 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
292 methods = authmethods_get();
293 packet_start(SSH2_MSG_USERAUTH_FAILURE);
294 packet_put_cstring(methods);
295 packet_put_char(0); /* XXX partial success, unused */
303 userauth_banner(void)
310 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
312 if ((fd = open(options.banner, O_RDONLY)) < 0)
314 if (fstat(fd, &st) < 0)
317 banner = xmalloc(len + 1);
318 if ((n = read(fd, banner, len)) < 0)
321 packet_start(SSH2_MSG_USERAUTH_BANNER);
322 packet_put_cstring(banner);
323 packet_put_cstring(""); /* language, unused */
325 debug("userauth_banner: sent");
334 userauth_none(Authctxt *authctxt)
336 /* disable method "none", only allowed one time */
337 Authmethod *m = authmethod_lookup("none");
343 if (authctxt->valid == 0)
347 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
351 return auth_pam_password(authctxt->pw, "");
352 #elif defined(HAVE_OSF_SIA)
354 #else /* !HAVE_OSF_SIA && !USE_PAM */
355 return auth_password(authctxt, "");
360 userauth_passwd(Authctxt *authctxt)
363 int authenticated = 0;
366 change = packet_get_char();
368 log("password change not supported");
369 password = packet_get_string(&len);
371 if (authctxt->valid &&
373 check_nt_auth(1, authctxt->pw->pw_uid) &&
376 auth_pam_password(authctxt->pw, password) == 1)
377 #elif defined(HAVE_OSF_SIA)
378 auth_sia_password(authctxt->user, password) == 1)
379 #else /* !USE_PAM && !HAVE_OSF_SIA */
380 auth_password(authctxt, password) == 1)
383 memset(password, 0, len);
385 return authenticated;
389 userauth_kbdint(Authctxt *authctxt)
391 int authenticated = 0;
394 lang = packet_get_string(NULL);
395 devs = packet_get_string(NULL);
398 debug("keyboard-interactive devs %s", devs);
400 if (options.challenge_response_authentication)
401 authenticated = auth2_challenge(authctxt, devs);
404 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
405 authenticated = auth2_pam(authctxt);
410 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
413 return authenticated;
417 userauth_pubkey(Authctxt *authctxt)
421 char *pkalg, *pkblob, *sig;
422 u_int alen, blen, slen;
423 int have_sig, pktype;
424 int authenticated = 0;
426 if (!authctxt->valid) {
427 debug2("userauth_pubkey: disabled because of invalid user");
430 have_sig = packet_get_char();
431 if (datafellows & SSH_BUG_PKAUTH) {
432 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
433 /* no explicit pkalg given */
434 pkblob = packet_get_string(&blen);
436 buffer_append(&b, pkblob, blen);
437 /* so we have to extract the pkalg from the pkblob */
438 pkalg = buffer_get_string(&b, &alen);
441 pkalg = packet_get_string(&alen);
442 pkblob = packet_get_string(&blen);
444 pktype = key_type_from_name(pkalg);
445 if (pktype == KEY_UNSPEC) {
446 /* this is perfectly legal */
447 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
452 key = key_from_blob(pkblob, blen);
455 sig = packet_get_string(&slen);
458 if (datafellows & SSH_OLD_SESSIONID) {
459 buffer_append(&b, session_id2, session_id2_len);
461 buffer_put_string(&b, session_id2, session_id2_len);
463 /* reconstruct packet */
464 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
465 buffer_put_cstring(&b, authctxt->user);
466 buffer_put_cstring(&b,
467 datafellows & SSH_BUG_PKSERVICE ?
470 if (datafellows & SSH_BUG_PKAUTH) {
471 buffer_put_char(&b, have_sig);
473 buffer_put_cstring(&b, "publickey");
474 buffer_put_char(&b, have_sig);
475 buffer_put_cstring(&b, pkalg);
477 buffer_put_string(&b, pkblob, blen);
481 /* test for correct signature */
482 if (user_key_allowed(authctxt->pw, key) &&
483 key_verify(key, sig, slen, buffer_ptr(&b), 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 (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();
512 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
516 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
519 return authenticated;
523 userauth_hostbased(Authctxt *authctxt)
527 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
528 u_int alen, blen, slen;
530 int authenticated = 0;
532 if (!authctxt->valid) {
533 debug2("userauth_hostbased: disabled because of invalid user");
536 pkalg = packet_get_string(&alen);
537 pkblob = packet_get_string(&blen);
538 chost = packet_get_string(NULL);
539 cuser = packet_get_string(NULL);
540 sig = packet_get_string(&slen);
542 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
543 cuser, chost, pkalg, slen);
547 buffer_append(&b, sig, slen);
551 pktype = key_type_from_name(pkalg);
552 if (pktype == KEY_UNSPEC) {
553 /* this is perfectly legal */
554 log("userauth_hostbased: unsupported "
555 "public key algorithm: %s", pkalg);
558 key = key_from_blob(pkblob, blen);
560 debug("userauth_hostbased: cannot decode key: %s", pkalg);
563 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
566 buffer_put_string(&b, session_id2, session_id2_len);
567 /* reconstruct packet */
568 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
569 buffer_put_cstring(&b, authctxt->user);
570 buffer_put_cstring(&b, service);
571 buffer_put_cstring(&b, "hostbased");
572 buffer_put_string(&b, pkalg, alen);
573 buffer_put_string(&b, pkblob, blen);
574 buffer_put_cstring(&b, chost);
575 buffer_put_cstring(&b, cuser);
579 /* test for allowed key and correct signature */
580 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
581 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
588 debug2("userauth_hostbased: authenticated %d", authenticated);
594 return authenticated;
597 /* get current user */
602 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
608 authmethods_get(void)
610 Authmethod *method = NULL;
614 for (method = authmethods; method->name != NULL; method++) {
615 if (strcmp(method->name, "none") == 0)
617 if (method->enabled != NULL && *(method->enabled) != 0) {
619 size += strlen(DELIM);
620 size += strlen(method->name);
623 size++; /* trailing '\0' */
624 list = xmalloc(size);
627 for (method = authmethods; method->name != NULL; method++) {
628 if (strcmp(method->name, "none") == 0)
630 if (method->enabled != NULL && *(method->enabled) != 0) {
632 strlcat(list, DELIM, size);
633 strlcat(list, method->name, size);
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_allowed(struct passwd *pw, Key *key)
657 char line[8192], *file;
667 /* Temporarily use the user's uid. */
668 temporarily_use_uid(pw);
670 /* The authorized keys. */
671 file = authorized_keys_file2(pw);
672 debug("trying public key file %s", file);
674 /* Fail quietly if file does not exist */
675 if (stat(file, &st) < 0) {
676 /* Restore the privileged uid. */
681 /* Open the file containing the authorized keys. */
682 f = fopen(file, "r");
684 /* Restore the privileged uid. */
689 if (options.strict_modes &&
690 secure_filename(f, file, pw->pw_uid, line, sizeof(line)) != 0) {
693 log("Authentication refused: %s", line);
699 found = key_new(key->type);
701 while (fgets(line, sizeof(line), f)) {
702 char *cp, *options = NULL;
704 /* Skip leading whitespace, empty and comment lines. */
705 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
707 if (!*cp || *cp == '\n' || *cp == '#')
710 if (key_read(found, &cp) == -1) {
711 /* no key? check if there are options for this key */
713 debug2("user_key_allowed: check options: '%s'", cp);
715 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
716 if (*cp == '\\' && cp[1] == '"')
717 cp++; /* Skip both */
721 /* Skip remaining whitespace. */
722 for (; *cp == ' ' || *cp == '\t'; cp++)
724 if (key_read(found, &cp) == -1) {
725 debug2("user_key_allowed: advance: '%s'", cp);
726 /* still no key? advance to next line*/
730 if (key_equal(found, key) &&
731 auth_parse_options(pw, options, file, linenum) == 1) {
733 debug("matching key found: file %s, line %ld",
743 debug2("key not found");
747 /* return 1 if given hostkey is allowed */
749 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
753 const char *resolvedname, *ipaddr, *lookup;
756 int host_status, len;
758 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
759 ipaddr = get_remote_ipaddr();
761 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
762 chost, resolvedname, ipaddr);
764 if (options.hostbased_uses_name_from_packet_only) {
765 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
769 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
770 debug2("stripping trailing dot from chost %s", chost);
771 chost[len - 1] = '\0';
773 if (strcasecmp(resolvedname, chost) != 0)
774 log("userauth_hostbased mismatch: "
775 "client sends %s, but we resolve %s to %s",
776 chost, ipaddr, resolvedname);
777 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
779 lookup = resolvedname;
781 debug2("userauth_hostbased: access allowed by auth_rhosts2");
783 /* XXX this is copied from auth-rh-rsa.c and should be shared */
784 found = key_new(key->type);
785 host_status = check_host_in_hostfile(_PATH_SSH_SYSTEM_HOSTFILE2, lookup,
788 if (host_status != HOST_OK && !options.ignore_user_known_hosts) {
789 user_hostfile = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE2,
791 if (options.strict_modes &&
792 (stat(user_hostfile, &st) == 0) &&
793 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
794 (st.st_mode & 022) != 0)) {
795 log("Hostbased authentication refused for %.100s: "
796 "bad owner or modes for %.200s",
797 pw->pw_name, user_hostfile);
799 temporarily_use_uid(pw);
800 host_status = check_host_in_hostfile(user_hostfile,
801 lookup, key, found, NULL);
804 xfree(user_hostfile);
808 debug2("userauth_hostbased: key %s for %s", host_status == HOST_OK ?
809 "ok" : "not found", lookup);
810 return (host_status == HOST_OK);