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.72 2001/11/07 22:41:51 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 #ifdef WITH_AIXAUTHENTICATE
61 extern char *aixloginmsg;
64 static Authctxt *x_authctxt = NULL;
67 typedef struct Authmethod Authmethod;
70 int (*userauth)(Authctxt *authctxt);
76 static void input_service_request(int, int, void *);
77 static void input_userauth_request(int, int, void *);
78 static void protocol_error(int, int, void *);
81 static Authmethod *authmethod_lookup(const char *);
82 static char *authmethods_get(void);
83 static int user_key_allowed(struct passwd *, Key *);
84 static int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
87 static void userauth_banner(void);
88 static int userauth_none(Authctxt *);
89 static int userauth_passwd(Authctxt *);
90 static int userauth_pubkey(Authctxt *);
91 static int userauth_hostbased(Authctxt *);
92 static int userauth_kbdint(Authctxt *);
94 Authmethod authmethods[] = {
100 &options.pubkey_authentication},
103 &options.password_authentication},
104 {"keyboard-interactive",
106 &options.kbd_interactive_authentication},
109 &options.hostbased_authentication},
114 * loop until authctxt->success == TRUE
120 Authctxt *authctxt = authctxt_new();
122 x_authctxt = authctxt; /*XXX*/
124 /* challenge-response is implemented via keyboard interactive */
125 if (options.challenge_response_authentication)
126 options.kbd_interactive_authentication = 1;
127 if (options.pam_authentication_via_kbd_int)
128 options.kbd_interactive_authentication = 1;
130 dispatch_init(&protocol_error);
131 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
132 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
133 do_authenticated(authctxt);
137 protocol_error(int type, int plen, void *ctxt)
139 log("auth: protocol error: type %d plen %d", type, plen);
140 packet_start(SSH2_MSG_UNIMPLEMENTED);
147 input_service_request(int type, int plen, void *ctxt)
149 Authctxt *authctxt = ctxt;
152 char *service = packet_get_string(&len);
155 if (authctxt == NULL)
156 fatal("input_service_request: no authctxt");
158 if (strcmp(service, "ssh-userauth") == 0) {
159 if (!authctxt->success) {
161 /* now we can handle user-auth requests */
162 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
165 /* XXX all other service requests are denied */
168 packet_start(SSH2_MSG_SERVICE_ACCEPT);
169 packet_put_cstring(service);
173 debug("bad service request %s", service);
174 packet_disconnect("bad service request %s", service);
180 input_userauth_request(int type, int plen, void *ctxt)
182 Authctxt *authctxt = ctxt;
183 Authmethod *m = NULL;
184 char *user, *service, *method, *style = NULL;
185 int authenticated = 0;
187 if (authctxt == NULL)
188 fatal("input_userauth_request: no authctxt");
190 user = packet_get_string(NULL);
191 service = packet_get_string(NULL);
192 method = packet_get_string(NULL);
193 debug("userauth-request for user %s service %s method %s", user, service, method);
194 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
196 if ((style = strchr(user, ':')) != NULL)
199 if (authctxt->attempt++ == 0) {
200 /* setup auth context */
201 struct passwd *pw = NULL;
203 if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) {
204 authctxt->pw = pwcopy(pw);
206 debug2("input_userauth_request: setting up authctxt for %s", user);
208 start_pam(pw->pw_name);
211 log("input_userauth_request: illegal user %s", user);
216 setproctitle("%s", pw ? user : "unknown");
217 authctxt->user = xstrdup(user);
218 authctxt->service = xstrdup(service);
219 authctxt->style = style ? xstrdup(style) : NULL;
220 } else if (strcmp(user, authctxt->user) != 0 ||
221 strcmp(service, authctxt->service) != 0) {
222 packet_disconnect("Change of username or service not allowed: "
223 "(%s,%s) -> (%s,%s)",
224 authctxt->user, authctxt->service, user, service);
227 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, &protocol_error);
228 authctxt->postponed = 0;
231 auth_close(authctxt->as);
236 /* try to authenticate user */
237 m = authmethod_lookup(method);
239 debug2("input_userauth_request: try method %s", method);
240 authenticated = m->userauth(authctxt);
242 userauth_finish(authctxt, authenticated, method);
250 userauth_finish(Authctxt *authctxt, int authenticated, char *method)
254 if (!authctxt->valid && authenticated)
255 fatal("INTERNAL ERROR: authenticated invalid user %s",
258 /* Special handling for root */
259 if (authenticated && authctxt->pw->pw_uid == 0 &&
260 !auth_root_allowed(method))
264 if (authenticated && authctxt->user && !do_pam_account(authctxt->user,
269 /* Log before sending the reply */
270 auth_log(authctxt, authenticated, method, " ssh2");
272 if (authctxt->postponed)
275 /* XXX todo: check if multiple auth methods are needed */
276 if (authenticated == 1) {
277 /* turn off userauth */
278 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
279 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
282 /* now we can break out */
283 authctxt->success = 1;
285 if (authctxt->failures++ > AUTH_FAIL_MAX)
286 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
287 methods = authmethods_get();
288 packet_start(SSH2_MSG_USERAUTH_FAILURE);
289 packet_put_cstring(methods);
290 packet_put_char(0); /* XXX partial success, unused */
298 userauth_banner(void)
305 if (options.banner == NULL || (datafellows & SSH_BUG_BANNER))
307 if ((fd = open(options.banner, O_RDONLY)) < 0)
309 if (fstat(fd, &st) < 0)
312 banner = xmalloc(len + 1);
313 if ((n = read(fd, banner, len)) < 0)
316 packet_start(SSH2_MSG_USERAUTH_BANNER);
317 packet_put_cstring(banner);
318 packet_put_cstring(""); /* language, unused */
320 debug("userauth_banner: sent");
329 userauth_none(Authctxt *authctxt)
331 /* disable method "none", only allowed one time */
332 Authmethod *m = authmethod_lookup("none");
338 if (authctxt->valid == 0)
342 if (check_nt_auth(1, authctxt->pw->pw_uid) == 0)
346 return auth_pam_password(authctxt->pw, "");
347 #elif defined(HAVE_OSF_SIA)
349 #else /* !HAVE_OSF_SIA && !USE_PAM */
350 return auth_password(authctxt, "");
355 userauth_passwd(Authctxt *authctxt)
358 int authenticated = 0;
361 change = packet_get_char();
363 log("password change not supported");
364 password = packet_get_string(&len);
366 if (authctxt->valid &&
368 check_nt_auth(1, authctxt->pw->pw_uid) &&
371 auth_pam_password(authctxt->pw, password) == 1)
372 #elif defined(HAVE_OSF_SIA)
373 auth_sia_password(authctxt->user, password) == 1)
374 #else /* !USE_PAM && !HAVE_OSF_SIA */
375 auth_password(authctxt, password) == 1)
378 memset(password, 0, len);
380 return authenticated;
384 userauth_kbdint(Authctxt *authctxt)
386 int authenticated = 0;
389 lang = packet_get_string(NULL);
390 devs = packet_get_string(NULL);
393 debug("keyboard-interactive devs %s", devs);
395 if (options.challenge_response_authentication)
396 authenticated = auth2_challenge(authctxt, devs);
399 if (authenticated == 0 && options.pam_authentication_via_kbd_int)
400 authenticated = auth2_pam(authctxt);
405 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
408 return authenticated;
412 userauth_pubkey(Authctxt *authctxt)
416 char *pkalg, *pkblob, *sig;
417 u_int alen, blen, slen;
418 int have_sig, pktype;
419 int authenticated = 0;
421 if (!authctxt->valid) {
422 debug2("userauth_pubkey: disabled because of invalid user");
425 have_sig = packet_get_char();
426 if (datafellows & SSH_BUG_PKAUTH) {
427 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
428 /* no explicit pkalg given */
429 pkblob = packet_get_string(&blen);
431 buffer_append(&b, pkblob, blen);
432 /* so we have to extract the pkalg from the pkblob */
433 pkalg = buffer_get_string(&b, &alen);
436 pkalg = packet_get_string(&alen);
437 pkblob = packet_get_string(&blen);
439 pktype = key_type_from_name(pkalg);
440 if (pktype == KEY_UNSPEC) {
441 /* this is perfectly legal */
442 log("userauth_pubkey: unsupported public key algorithm: %s", pkalg);
447 key = key_from_blob(pkblob, blen);
450 sig = packet_get_string(&slen);
453 if (datafellows & SSH_OLD_SESSIONID) {
454 buffer_append(&b, session_id2, session_id2_len);
456 buffer_put_string(&b, session_id2, session_id2_len);
458 /* reconstruct packet */
459 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
460 buffer_put_cstring(&b, authctxt->user);
461 buffer_put_cstring(&b,
462 datafellows & SSH_BUG_PKSERVICE ?
465 if (datafellows & SSH_BUG_PKAUTH) {
466 buffer_put_char(&b, have_sig);
468 buffer_put_cstring(&b, "publickey");
469 buffer_put_char(&b, have_sig);
470 buffer_put_cstring(&b, pkalg);
472 buffer_put_string(&b, pkblob, blen);
476 /* test for correct signature */
477 if (user_key_allowed(authctxt->pw, key) &&
478 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
483 debug("test whether pkalg/pkblob are acceptable");
486 /* XXX fake reply and always send PK_OK ? */
488 * XXX this allows testing whether a user is allowed
489 * to login: if you happen to have a valid pubkey this
490 * message is sent. the message is NEVER sent at all
491 * if a user is not allowed to login. is this an
494 if (user_key_allowed(authctxt->pw, key)) {
495 packet_start(SSH2_MSG_USERAUTH_PK_OK);
496 packet_put_string(pkalg, alen);
497 packet_put_string(pkblob, blen);
500 authctxt->postponed = 1;
503 if (authenticated != 1)
504 auth_clear_options();
507 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
511 if (check_nt_auth(0, authctxt->pw->pw_uid) == 0)
514 return authenticated;
518 userauth_hostbased(Authctxt *authctxt)
522 char *pkalg, *pkblob, *sig, *cuser, *chost, *service;
523 u_int alen, blen, slen;
525 int authenticated = 0;
527 if (!authctxt->valid) {
528 debug2("userauth_hostbased: disabled because of invalid user");
531 pkalg = packet_get_string(&alen);
532 pkblob = packet_get_string(&blen);
533 chost = packet_get_string(NULL);
534 cuser = packet_get_string(NULL);
535 sig = packet_get_string(&slen);
537 debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
538 cuser, chost, pkalg, slen);
542 buffer_append(&b, sig, slen);
546 pktype = key_type_from_name(pkalg);
547 if (pktype == KEY_UNSPEC) {
548 /* this is perfectly legal */
549 log("userauth_hostbased: unsupported "
550 "public key algorithm: %s", pkalg);
553 key = key_from_blob(pkblob, blen);
555 debug("userauth_hostbased: cannot decode key: %s", pkalg);
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)
583 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;
609 for (method = authmethods; method->name != NULL; method++) {
610 if (strcmp(method->name, "none") == 0)
612 if (method->enabled != NULL && *(method->enabled) != 0) {
614 size += strlen(DELIM);
615 size += strlen(method->name);
618 size++; /* trailing '\0' */
619 list = xmalloc(size);
622 for (method = authmethods; method->name != NULL; method++) {
623 if (strcmp(method->name, "none") == 0)
625 if (method->enabled != NULL && *(method->enabled) != 0) {
627 strlcat(list, DELIM, size);
628 strlcat(list, method->name, size);
635 authmethod_lookup(const char *name)
637 Authmethod *method = NULL;
639 for (method = authmethods; method->name != NULL; method++)
640 if (method->enabled != NULL &&
641 *(method->enabled) != 0 &&
642 strcmp(name, method->name) == 0)
644 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
648 /* return 1 if user allows given key */
650 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",
732 debug2("key not found");
736 /* check whether given key is in .ssh/authorized_keys* */
738 user_key_allowed(struct passwd *pw, Key *key)
743 file = authorized_keys_file(pw);
744 success = user_key_allowed2(pw, key, file);
749 /* try suffix "2" for backward compat, too */
750 file = authorized_keys_file2(pw);
751 success = user_key_allowed2(pw, key, file);
756 /* return 1 if given hostkey is allowed */
758 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
761 const char *resolvedname, *ipaddr, *lookup;
762 int host_status, len;
764 resolvedname = get_canonical_hostname(options.reverse_mapping_check);
765 ipaddr = get_remote_ipaddr();
767 debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
768 chost, resolvedname, ipaddr);
770 if (options.hostbased_uses_name_from_packet_only) {
771 if (auth_rhosts2(pw, cuser, chost, chost) == 0)
775 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
776 debug2("stripping trailing dot from chost %s", chost);
777 chost[len - 1] = '\0';
779 if (strcasecmp(resolvedname, chost) != 0)
780 log("userauth_hostbased mismatch: "
781 "client sends %s, but we resolve %s to %s",
782 chost, ipaddr, resolvedname);
783 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
785 lookup = resolvedname;
787 debug2("userauth_hostbased: access allowed by auth_rhosts2");
789 host_status = check_key_in_hostfiles(pw, key, lookup,
790 _PATH_SSH_SYSTEM_HOSTFILE,
791 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
793 /* backward compat if no key has been found. */
794 if (host_status == HOST_NEW)
795 host_status = check_key_in_hostfiles(pw, key, lookup,
796 _PATH_SSH_SYSTEM_HOSTFILE2,
797 options.ignore_user_known_hosts ? NULL :
798 _PATH_SSH_USER_HOSTFILE2);
800 return (host_status == HOST_OK);