10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved. 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 50Sstevel@tonic-gate * modification, are permitted provided that the following conditions 60Sstevel@tonic-gate * are met: 70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate /* 25*3908Sjp161948 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 260Sstevel@tonic-gate * Use is subject to license terms. 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include "includes.h" 300Sstevel@tonic-gate RCSID("$OpenBSD: auth2-hostbased.c,v 1.2 2002/05/31 11:35:15 markus Exp $"); 310Sstevel@tonic-gate 320Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include "ssh2.h" 350Sstevel@tonic-gate #include "xmalloc.h" 360Sstevel@tonic-gate #include "packet.h" 370Sstevel@tonic-gate #include "buffer.h" 380Sstevel@tonic-gate #include "log.h" 390Sstevel@tonic-gate #include "servconf.h" 400Sstevel@tonic-gate #include "compat.h" 410Sstevel@tonic-gate #include "bufaux.h" 420Sstevel@tonic-gate #include "auth.h" 430Sstevel@tonic-gate 440Sstevel@tonic-gate #ifdef USE_PAM 450Sstevel@tonic-gate #include "auth-pam.h" 460Sstevel@tonic-gate #endif /* USE_PAM */ 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include "key.h" 490Sstevel@tonic-gate #include "canohost.h" 500Sstevel@tonic-gate #include "monitor_wrap.h" 510Sstevel@tonic-gate #include "pathnames.h" 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* import */ 540Sstevel@tonic-gate extern ServerOptions options; 550Sstevel@tonic-gate extern u_char *session_id2; 560Sstevel@tonic-gate extern int session_id2_len; 570Sstevel@tonic-gate 580Sstevel@tonic-gate static void 590Sstevel@tonic-gate userauth_hostbased(Authctxt *authctxt) 600Sstevel@tonic-gate { 610Sstevel@tonic-gate Buffer b; 620Sstevel@tonic-gate Key *key = NULL; 630Sstevel@tonic-gate char *pkalg, *cuser, *chost, *service; 640Sstevel@tonic-gate u_char *pkblob, *sig; 650Sstevel@tonic-gate u_int alen, blen, slen; 660Sstevel@tonic-gate int pktype; 670Sstevel@tonic-gate int authenticated = 0; 680Sstevel@tonic-gate 690Sstevel@tonic-gate if (!authctxt || !authctxt->method) 700Sstevel@tonic-gate fatal("%s: missing context", __func__); 710Sstevel@tonic-gate 720Sstevel@tonic-gate pkalg = packet_get_string(&alen); 730Sstevel@tonic-gate pkblob = packet_get_string(&blen); 740Sstevel@tonic-gate chost = packet_get_string(NULL); 750Sstevel@tonic-gate cuser = packet_get_string(NULL); 760Sstevel@tonic-gate sig = packet_get_string(&slen); 770Sstevel@tonic-gate 780Sstevel@tonic-gate debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d", 790Sstevel@tonic-gate cuser, chost, pkalg, slen); 800Sstevel@tonic-gate #ifdef DEBUG_PK 810Sstevel@tonic-gate debug("signature:"); 820Sstevel@tonic-gate buffer_init(&b); 830Sstevel@tonic-gate buffer_append(&b, sig, slen); 840Sstevel@tonic-gate buffer_dump(&b); 850Sstevel@tonic-gate buffer_free(&b); 860Sstevel@tonic-gate #endif 870Sstevel@tonic-gate pktype = key_type_from_name(pkalg); 880Sstevel@tonic-gate if (pktype == KEY_UNSPEC) { 890Sstevel@tonic-gate /* this is perfectly legal */ 900Sstevel@tonic-gate log("userauth_hostbased: unsupported " 910Sstevel@tonic-gate "public key algorithm: %s", pkalg); 920Sstevel@tonic-gate goto done; 930Sstevel@tonic-gate } 940Sstevel@tonic-gate key = key_from_blob(pkblob, blen); 950Sstevel@tonic-gate if (key == NULL) { 960Sstevel@tonic-gate error("userauth_hostbased: cannot decode key: %s", pkalg); 970Sstevel@tonic-gate goto done; 980Sstevel@tonic-gate } 990Sstevel@tonic-gate if (key->type != pktype) { 1000Sstevel@tonic-gate error("userauth_hostbased: type mismatch for decoded key " 1010Sstevel@tonic-gate "(received %d, expected %d)", key->type, pktype); 1020Sstevel@tonic-gate goto done; 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : 1050Sstevel@tonic-gate authctxt->service; 1060Sstevel@tonic-gate buffer_init(&b); 1070Sstevel@tonic-gate buffer_put_string(&b, session_id2, session_id2_len); 1080Sstevel@tonic-gate /* reconstruct packet */ 1090Sstevel@tonic-gate buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); 1100Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->user); 1110Sstevel@tonic-gate buffer_put_cstring(&b, service); 1120Sstevel@tonic-gate buffer_put_cstring(&b, "hostbased"); 1130Sstevel@tonic-gate buffer_put_string(&b, pkalg, alen); 1140Sstevel@tonic-gate buffer_put_string(&b, pkblob, blen); 1150Sstevel@tonic-gate buffer_put_cstring(&b, chost); 1160Sstevel@tonic-gate buffer_put_cstring(&b, cuser); 1170Sstevel@tonic-gate #ifdef DEBUG_PK 1180Sstevel@tonic-gate buffer_dump(&b); 1190Sstevel@tonic-gate #endif 1200Sstevel@tonic-gate /* test for allowed key and correct signature */ 1210Sstevel@tonic-gate authenticated = 0; 1220Sstevel@tonic-gate if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) && 1230Sstevel@tonic-gate PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), 1240Sstevel@tonic-gate buffer_len(&b))) == 1) 1250Sstevel@tonic-gate authenticated = 1; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate buffer_clear(&b); 1280Sstevel@tonic-gate done: 1290Sstevel@tonic-gate /* 1300Sstevel@tonic-gate * XXX TODO: Add config options for specifying users for whom 1310Sstevel@tonic-gate * this userauth is insufficient and what userauths 1320Sstevel@tonic-gate * may continue. 1330Sstevel@tonic-gate * 1340Sstevel@tonic-gate * NOTE: do_pam_non_initial_userauth() does this for 1350Sstevel@tonic-gate * users with expired passwords. 1360Sstevel@tonic-gate */ 1370Sstevel@tonic-gate #ifdef USE_PAM 1380Sstevel@tonic-gate if (authenticated) { 139*3908Sjp161948 authctxt->cuser = cuser; 1400Sstevel@tonic-gate if (!do_pam_non_initial_userauth(authctxt)) 1410Sstevel@tonic-gate authenticated = 0; 142*3908Sjp161948 /* Make sure nobody else will use this pointer since we are 143*3908Sjp161948 * going to free that string. */ 144*3908Sjp161948 authctxt->cuser = NULL; 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate #endif /* USE_PAM */ 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate if (authenticated) 1490Sstevel@tonic-gate authctxt->method->authenticated = 1; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate debug2("userauth_hostbased: authenticated %d", authenticated); 1520Sstevel@tonic-gate if (key != NULL) 1530Sstevel@tonic-gate key_free(key); 1540Sstevel@tonic-gate xfree(pkalg); 1550Sstevel@tonic-gate xfree(pkblob); 1560Sstevel@tonic-gate xfree(cuser); 1570Sstevel@tonic-gate xfree(chost); 1580Sstevel@tonic-gate xfree(sig); 1590Sstevel@tonic-gate return; 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* return 1 if given hostkey is allowed */ 1630Sstevel@tonic-gate int 1640Sstevel@tonic-gate hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost, 1650Sstevel@tonic-gate Key *key) 1660Sstevel@tonic-gate { 1670Sstevel@tonic-gate const char *resolvedname, *ipaddr, *lookup; 1680Sstevel@tonic-gate HostStatus host_status; 1690Sstevel@tonic-gate int len; 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate resolvedname = get_canonical_hostname(options.verify_reverse_mapping); 1720Sstevel@tonic-gate ipaddr = get_remote_ipaddr(); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s", 1750Sstevel@tonic-gate chost, resolvedname, ipaddr); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate if (pw == NULL) 1780Sstevel@tonic-gate return 0; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if (options.hostbased_uses_name_from_packet_only) { 1810Sstevel@tonic-gate if (auth_rhosts2(pw, cuser, chost, chost) == 0) 1820Sstevel@tonic-gate return 0; 1830Sstevel@tonic-gate lookup = chost; 1840Sstevel@tonic-gate } else { 1850Sstevel@tonic-gate if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') { 1860Sstevel@tonic-gate debug2("stripping trailing dot from chost %s", chost); 1870Sstevel@tonic-gate chost[len - 1] = '\0'; 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate if (strcasecmp(resolvedname, chost) != 0) 1900Sstevel@tonic-gate log("userauth_hostbased mismatch: " 1910Sstevel@tonic-gate "client sends %s, but we resolve %s to %s", 1920Sstevel@tonic-gate chost, ipaddr, resolvedname); 1930Sstevel@tonic-gate if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) 1940Sstevel@tonic-gate return 0; 1950Sstevel@tonic-gate lookup = resolvedname; 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate debug2("userauth_hostbased: access allowed by auth_rhosts2"); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate host_status = check_key_in_hostfiles(pw, key, lookup, 2000Sstevel@tonic-gate _PATH_SSH_SYSTEM_HOSTFILE, 2010Sstevel@tonic-gate options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE); 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* backward compat if no key has been found. */ 2040Sstevel@tonic-gate if (host_status == HOST_NEW) 2050Sstevel@tonic-gate host_status = check_key_in_hostfiles(pw, key, lookup, 2060Sstevel@tonic-gate _PATH_SSH_SYSTEM_HOSTFILE2, 2070Sstevel@tonic-gate options.ignore_user_known_hosts ? NULL : 2080Sstevel@tonic-gate _PATH_SSH_USER_HOSTFILE2); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate return (host_status == HOST_OK); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate Authmethod method_hostbased = { 2140Sstevel@tonic-gate "hostbased", 2150Sstevel@tonic-gate &options.hostbased_authentication, 2160Sstevel@tonic-gate userauth_hostbased, 2170Sstevel@tonic-gate NULL, /* no abandon function */ 2180Sstevel@tonic-gate NULL, NULL, /* method data and hist data */ 2190Sstevel@tonic-gate 0, /* is not initial userauth */ 2200Sstevel@tonic-gate 0, 0, 0, /* counters */ 2210Sstevel@tonic-gate 0, 0, 0, 0, 0, 0 /* state */ 2220Sstevel@tonic-gate }; 223