17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved.
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
57c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
67c478bd9Sstevel@tonic-gate * are met:
77c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
87c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
97c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
107c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
117c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate /*
25f44ef466Sjp161948 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include "includes.h"
307c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth2-hostbased.c,v 1.2 2002/05/31 11:35:15 markus Exp $");
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include "ssh2.h"
357c478bd9Sstevel@tonic-gate #include "xmalloc.h"
367c478bd9Sstevel@tonic-gate #include "packet.h"
377c478bd9Sstevel@tonic-gate #include "buffer.h"
387c478bd9Sstevel@tonic-gate #include "log.h"
397c478bd9Sstevel@tonic-gate #include "servconf.h"
407c478bd9Sstevel@tonic-gate #include "compat.h"
417c478bd9Sstevel@tonic-gate #include "bufaux.h"
427c478bd9Sstevel@tonic-gate #include "auth.h"
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #ifdef USE_PAM
457c478bd9Sstevel@tonic-gate #include "auth-pam.h"
467c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #include "key.h"
497c478bd9Sstevel@tonic-gate #include "canohost.h"
507c478bd9Sstevel@tonic-gate #include "pathnames.h"
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate /* import */
537c478bd9Sstevel@tonic-gate extern ServerOptions options;
547c478bd9Sstevel@tonic-gate extern u_char *session_id2;
557c478bd9Sstevel@tonic-gate extern int session_id2_len;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate static void
userauth_hostbased(Authctxt * authctxt)587c478bd9Sstevel@tonic-gate userauth_hostbased(Authctxt *authctxt)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate Buffer b;
617c478bd9Sstevel@tonic-gate Key *key = NULL;
627c478bd9Sstevel@tonic-gate char *pkalg, *cuser, *chost, *service;
637c478bd9Sstevel@tonic-gate u_char *pkblob, *sig;
647c478bd9Sstevel@tonic-gate u_int alen, blen, slen;
657c478bd9Sstevel@tonic-gate int pktype;
667c478bd9Sstevel@tonic-gate int authenticated = 0;
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate if (!authctxt || !authctxt->method)
697c478bd9Sstevel@tonic-gate fatal("%s: missing context", __func__);
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate pkalg = packet_get_string(&alen);
727c478bd9Sstevel@tonic-gate pkblob = packet_get_string(&blen);
737c478bd9Sstevel@tonic-gate chost = packet_get_string(NULL);
747c478bd9Sstevel@tonic-gate cuser = packet_get_string(NULL);
757c478bd9Sstevel@tonic-gate sig = packet_get_string(&slen);
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
787c478bd9Sstevel@tonic-gate cuser, chost, pkalg, slen);
797c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
807c478bd9Sstevel@tonic-gate debug("signature:");
817c478bd9Sstevel@tonic-gate buffer_init(&b);
827c478bd9Sstevel@tonic-gate buffer_append(&b, sig, slen);
837c478bd9Sstevel@tonic-gate buffer_dump(&b);
847c478bd9Sstevel@tonic-gate buffer_free(&b);
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate pktype = key_type_from_name(pkalg);
877c478bd9Sstevel@tonic-gate if (pktype == KEY_UNSPEC) {
887c478bd9Sstevel@tonic-gate /* this is perfectly legal */
897c478bd9Sstevel@tonic-gate log("userauth_hostbased: unsupported "
907c478bd9Sstevel@tonic-gate "public key algorithm: %s", pkalg);
917c478bd9Sstevel@tonic-gate goto done;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate key = key_from_blob(pkblob, blen);
947c478bd9Sstevel@tonic-gate if (key == NULL) {
957c478bd9Sstevel@tonic-gate error("userauth_hostbased: cannot decode key: %s", pkalg);
967c478bd9Sstevel@tonic-gate goto done;
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate if (key->type != pktype) {
997c478bd9Sstevel@tonic-gate error("userauth_hostbased: type mismatch for decoded key "
1007c478bd9Sstevel@tonic-gate "(received %d, expected %d)", key->type, pktype);
1017c478bd9Sstevel@tonic-gate goto done;
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1047c478bd9Sstevel@tonic-gate authctxt->service;
1057c478bd9Sstevel@tonic-gate buffer_init(&b);
1067c478bd9Sstevel@tonic-gate buffer_put_string(&b, session_id2, session_id2_len);
1077c478bd9Sstevel@tonic-gate /* reconstruct packet */
1087c478bd9Sstevel@tonic-gate buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1097c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->user);
1107c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, service);
1117c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, "hostbased");
1127c478bd9Sstevel@tonic-gate buffer_put_string(&b, pkalg, alen);
1137c478bd9Sstevel@tonic-gate buffer_put_string(&b, pkblob, blen);
1147c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, chost);
1157c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, cuser);
1167c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK
1177c478bd9Sstevel@tonic-gate buffer_dump(&b);
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate /* test for allowed key and correct signature */
1207c478bd9Sstevel@tonic-gate authenticated = 0;
121*9a8058b5Sjp161948 if (hostbased_key_allowed(authctxt->pw, cuser, chost, key) &&
122*9a8058b5Sjp161948 key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
1237c478bd9Sstevel@tonic-gate authenticated = 1;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate buffer_clear(&b);
1267c478bd9Sstevel@tonic-gate done:
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate * XXX TODO: Add config options for specifying users for whom
1297c478bd9Sstevel@tonic-gate * this userauth is insufficient and what userauths
1307c478bd9Sstevel@tonic-gate * may continue.
1317c478bd9Sstevel@tonic-gate *
1327c478bd9Sstevel@tonic-gate * NOTE: do_pam_non_initial_userauth() does this for
1337c478bd9Sstevel@tonic-gate * users with expired passwords.
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate #ifdef USE_PAM
1367c478bd9Sstevel@tonic-gate if (authenticated) {
137f44ef466Sjp161948 authctxt->cuser = cuser;
1387c478bd9Sstevel@tonic-gate if (!do_pam_non_initial_userauth(authctxt))
1397c478bd9Sstevel@tonic-gate authenticated = 0;
140f44ef466Sjp161948 /* Make sure nobody else will use this pointer since we are
141f44ef466Sjp161948 * going to free that string. */
142f44ef466Sjp161948 authctxt->cuser = NULL;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate #endif /* USE_PAM */
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate if (authenticated)
1477c478bd9Sstevel@tonic-gate authctxt->method->authenticated = 1;
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate debug2("userauth_hostbased: authenticated %d", authenticated);
1507c478bd9Sstevel@tonic-gate if (key != NULL)
1517c478bd9Sstevel@tonic-gate key_free(key);
1527c478bd9Sstevel@tonic-gate xfree(pkalg);
1537c478bd9Sstevel@tonic-gate xfree(pkblob);
1547c478bd9Sstevel@tonic-gate xfree(cuser);
1557c478bd9Sstevel@tonic-gate xfree(chost);
1567c478bd9Sstevel@tonic-gate xfree(sig);
1577c478bd9Sstevel@tonic-gate return;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /* return 1 if given hostkey is allowed */
1617c478bd9Sstevel@tonic-gate int
hostbased_key_allowed(struct passwd * pw,const char * cuser,char * chost,Key * key)1627c478bd9Sstevel@tonic-gate hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1637c478bd9Sstevel@tonic-gate Key *key)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate const char *resolvedname, *ipaddr, *lookup;
1667c478bd9Sstevel@tonic-gate HostStatus host_status;
1677c478bd9Sstevel@tonic-gate int len;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate resolvedname = get_canonical_hostname(options.verify_reverse_mapping);
1707c478bd9Sstevel@tonic-gate ipaddr = get_remote_ipaddr();
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s",
1737c478bd9Sstevel@tonic-gate chost, resolvedname, ipaddr);
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate if (pw == NULL)
1767c478bd9Sstevel@tonic-gate return 0;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate if (options.hostbased_uses_name_from_packet_only) {
1797c478bd9Sstevel@tonic-gate if (auth_rhosts2(pw, cuser, chost, chost) == 0)
1807c478bd9Sstevel@tonic-gate return 0;
1817c478bd9Sstevel@tonic-gate lookup = chost;
1827c478bd9Sstevel@tonic-gate } else {
1837c478bd9Sstevel@tonic-gate if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
1847c478bd9Sstevel@tonic-gate debug2("stripping trailing dot from chost %s", chost);
1857c478bd9Sstevel@tonic-gate chost[len - 1] = '\0';
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate if (strcasecmp(resolvedname, chost) != 0)
1887c478bd9Sstevel@tonic-gate log("userauth_hostbased mismatch: "
1897c478bd9Sstevel@tonic-gate "client sends %s, but we resolve %s to %s",
1907c478bd9Sstevel@tonic-gate chost, ipaddr, resolvedname);
1917c478bd9Sstevel@tonic-gate if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0)
1927c478bd9Sstevel@tonic-gate return 0;
1937c478bd9Sstevel@tonic-gate lookup = resolvedname;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate debug2("userauth_hostbased: access allowed by auth_rhosts2");
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate host_status = check_key_in_hostfiles(pw, key, lookup,
1987c478bd9Sstevel@tonic-gate _PATH_SSH_SYSTEM_HOSTFILE,
1997c478bd9Sstevel@tonic-gate options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate /* backward compat if no key has been found. */
2027c478bd9Sstevel@tonic-gate if (host_status == HOST_NEW)
2037c478bd9Sstevel@tonic-gate host_status = check_key_in_hostfiles(pw, key, lookup,
2047c478bd9Sstevel@tonic-gate _PATH_SSH_SYSTEM_HOSTFILE2,
2057c478bd9Sstevel@tonic-gate options.ignore_user_known_hosts ? NULL :
2067c478bd9Sstevel@tonic-gate _PATH_SSH_USER_HOSTFILE2);
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate return (host_status == HOST_OK);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate Authmethod method_hostbased = {
2127c478bd9Sstevel@tonic-gate "hostbased",
2137c478bd9Sstevel@tonic-gate &options.hostbased_authentication,
2147c478bd9Sstevel@tonic-gate userauth_hostbased,
2157c478bd9Sstevel@tonic-gate NULL, /* no abandon function */
2167c478bd9Sstevel@tonic-gate NULL, NULL, /* method data and hist data */
2177c478bd9Sstevel@tonic-gate 0, /* is not initial userauth */
2187c478bd9Sstevel@tonic-gate 0, 0, 0, /* counters */
2197c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0, 0 /* state */
2207c478bd9Sstevel@tonic-gate };
221