1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved. 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 5*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions 6*7c478bd9Sstevel@tonic-gate * are met: 7*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 8*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 9*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 10*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 11*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 12*7c478bd9Sstevel@tonic-gate * 13*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16*7c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17*7c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18*7c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19*7c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20*7c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21*7c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23*7c478bd9Sstevel@tonic-gate */ 24*7c478bd9Sstevel@tonic-gate /* 25*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 26*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 27*7c478bd9Sstevel@tonic-gate */ 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include "includes.h" 30*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth2-hostbased.c,v 1.2 2002/05/31 11:35:15 markus Exp $"); 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include "ssh2.h" 35*7c478bd9Sstevel@tonic-gate #include "xmalloc.h" 36*7c478bd9Sstevel@tonic-gate #include "packet.h" 37*7c478bd9Sstevel@tonic-gate #include "buffer.h" 38*7c478bd9Sstevel@tonic-gate #include "log.h" 39*7c478bd9Sstevel@tonic-gate #include "servconf.h" 40*7c478bd9Sstevel@tonic-gate #include "compat.h" 41*7c478bd9Sstevel@tonic-gate #include "bufaux.h" 42*7c478bd9Sstevel@tonic-gate #include "auth.h" 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #ifdef USE_PAM 45*7c478bd9Sstevel@tonic-gate #include "auth-pam.h" 46*7c478bd9Sstevel@tonic-gate #endif /* USE_PAM */ 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate #include "key.h" 49*7c478bd9Sstevel@tonic-gate #include "canohost.h" 50*7c478bd9Sstevel@tonic-gate #include "monitor_wrap.h" 51*7c478bd9Sstevel@tonic-gate #include "pathnames.h" 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate /* import */ 54*7c478bd9Sstevel@tonic-gate extern ServerOptions options; 55*7c478bd9Sstevel@tonic-gate extern u_char *session_id2; 56*7c478bd9Sstevel@tonic-gate extern int session_id2_len; 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate static void 59*7c478bd9Sstevel@tonic-gate userauth_hostbased(Authctxt *authctxt) 60*7c478bd9Sstevel@tonic-gate { 61*7c478bd9Sstevel@tonic-gate Buffer b; 62*7c478bd9Sstevel@tonic-gate Key *key = NULL; 63*7c478bd9Sstevel@tonic-gate char *pkalg, *cuser, *chost, *service; 64*7c478bd9Sstevel@tonic-gate u_char *pkblob, *sig; 65*7c478bd9Sstevel@tonic-gate u_int alen, blen, slen; 66*7c478bd9Sstevel@tonic-gate int pktype; 67*7c478bd9Sstevel@tonic-gate int authenticated = 0; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate if (!authctxt || !authctxt->method) 70*7c478bd9Sstevel@tonic-gate fatal("%s: missing context", __func__); 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate pkalg = packet_get_string(&alen); 73*7c478bd9Sstevel@tonic-gate pkblob = packet_get_string(&blen); 74*7c478bd9Sstevel@tonic-gate chost = packet_get_string(NULL); 75*7c478bd9Sstevel@tonic-gate cuser = packet_get_string(NULL); 76*7c478bd9Sstevel@tonic-gate sig = packet_get_string(&slen); 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d", 79*7c478bd9Sstevel@tonic-gate cuser, chost, pkalg, slen); 80*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK 81*7c478bd9Sstevel@tonic-gate debug("signature:"); 82*7c478bd9Sstevel@tonic-gate buffer_init(&b); 83*7c478bd9Sstevel@tonic-gate buffer_append(&b, sig, slen); 84*7c478bd9Sstevel@tonic-gate buffer_dump(&b); 85*7c478bd9Sstevel@tonic-gate buffer_free(&b); 86*7c478bd9Sstevel@tonic-gate #endif 87*7c478bd9Sstevel@tonic-gate pktype = key_type_from_name(pkalg); 88*7c478bd9Sstevel@tonic-gate if (pktype == KEY_UNSPEC) { 89*7c478bd9Sstevel@tonic-gate /* this is perfectly legal */ 90*7c478bd9Sstevel@tonic-gate log("userauth_hostbased: unsupported " 91*7c478bd9Sstevel@tonic-gate "public key algorithm: %s", pkalg); 92*7c478bd9Sstevel@tonic-gate goto done; 93*7c478bd9Sstevel@tonic-gate } 94*7c478bd9Sstevel@tonic-gate key = key_from_blob(pkblob, blen); 95*7c478bd9Sstevel@tonic-gate if (key == NULL) { 96*7c478bd9Sstevel@tonic-gate error("userauth_hostbased: cannot decode key: %s", pkalg); 97*7c478bd9Sstevel@tonic-gate goto done; 98*7c478bd9Sstevel@tonic-gate } 99*7c478bd9Sstevel@tonic-gate if (key->type != pktype) { 100*7c478bd9Sstevel@tonic-gate error("userauth_hostbased: type mismatch for decoded key " 101*7c478bd9Sstevel@tonic-gate "(received %d, expected %d)", key->type, pktype); 102*7c478bd9Sstevel@tonic-gate goto done; 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : 105*7c478bd9Sstevel@tonic-gate authctxt->service; 106*7c478bd9Sstevel@tonic-gate buffer_init(&b); 107*7c478bd9Sstevel@tonic-gate buffer_put_string(&b, session_id2, session_id2_len); 108*7c478bd9Sstevel@tonic-gate /* reconstruct packet */ 109*7c478bd9Sstevel@tonic-gate buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); 110*7c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->user); 111*7c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, service); 112*7c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, "hostbased"); 113*7c478bd9Sstevel@tonic-gate buffer_put_string(&b, pkalg, alen); 114*7c478bd9Sstevel@tonic-gate buffer_put_string(&b, pkblob, blen); 115*7c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, chost); 116*7c478bd9Sstevel@tonic-gate buffer_put_cstring(&b, cuser); 117*7c478bd9Sstevel@tonic-gate #ifdef DEBUG_PK 118*7c478bd9Sstevel@tonic-gate buffer_dump(&b); 119*7c478bd9Sstevel@tonic-gate #endif 120*7c478bd9Sstevel@tonic-gate /* test for allowed key and correct signature */ 121*7c478bd9Sstevel@tonic-gate authenticated = 0; 122*7c478bd9Sstevel@tonic-gate if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) && 123*7c478bd9Sstevel@tonic-gate PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), 124*7c478bd9Sstevel@tonic-gate buffer_len(&b))) == 1) 125*7c478bd9Sstevel@tonic-gate authenticated = 1; 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate buffer_clear(&b); 128*7c478bd9Sstevel@tonic-gate done: 129*7c478bd9Sstevel@tonic-gate /* 130*7c478bd9Sstevel@tonic-gate * XXX TODO: Add config options for specifying users for whom 131*7c478bd9Sstevel@tonic-gate * this userauth is insufficient and what userauths 132*7c478bd9Sstevel@tonic-gate * may continue. 133*7c478bd9Sstevel@tonic-gate * 134*7c478bd9Sstevel@tonic-gate * NOTE: do_pam_non_initial_userauth() does this for 135*7c478bd9Sstevel@tonic-gate * users with expired passwords. 136*7c478bd9Sstevel@tonic-gate */ 137*7c478bd9Sstevel@tonic-gate #ifdef USE_PAM 138*7c478bd9Sstevel@tonic-gate if (authenticated) { 139*7c478bd9Sstevel@tonic-gate if (!do_pam_non_initial_userauth(authctxt)) 140*7c478bd9Sstevel@tonic-gate authenticated = 0; 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate #endif /* USE_PAM */ 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate if (authenticated) 145*7c478bd9Sstevel@tonic-gate authctxt->method->authenticated = 1; 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate debug2("userauth_hostbased: authenticated %d", authenticated); 148*7c478bd9Sstevel@tonic-gate if (key != NULL) 149*7c478bd9Sstevel@tonic-gate key_free(key); 150*7c478bd9Sstevel@tonic-gate xfree(pkalg); 151*7c478bd9Sstevel@tonic-gate xfree(pkblob); 152*7c478bd9Sstevel@tonic-gate xfree(cuser); 153*7c478bd9Sstevel@tonic-gate xfree(chost); 154*7c478bd9Sstevel@tonic-gate xfree(sig); 155*7c478bd9Sstevel@tonic-gate return; 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate /* return 1 if given hostkey is allowed */ 159*7c478bd9Sstevel@tonic-gate int 160*7c478bd9Sstevel@tonic-gate hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost, 161*7c478bd9Sstevel@tonic-gate Key *key) 162*7c478bd9Sstevel@tonic-gate { 163*7c478bd9Sstevel@tonic-gate const char *resolvedname, *ipaddr, *lookup; 164*7c478bd9Sstevel@tonic-gate HostStatus host_status; 165*7c478bd9Sstevel@tonic-gate int len; 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate resolvedname = get_canonical_hostname(options.verify_reverse_mapping); 168*7c478bd9Sstevel@tonic-gate ipaddr = get_remote_ipaddr(); 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate debug2("userauth_hostbased: chost %s resolvedname %s ipaddr %s", 171*7c478bd9Sstevel@tonic-gate chost, resolvedname, ipaddr); 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate if (pw == NULL) 174*7c478bd9Sstevel@tonic-gate return 0; 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate if (options.hostbased_uses_name_from_packet_only) { 177*7c478bd9Sstevel@tonic-gate if (auth_rhosts2(pw, cuser, chost, chost) == 0) 178*7c478bd9Sstevel@tonic-gate return 0; 179*7c478bd9Sstevel@tonic-gate lookup = chost; 180*7c478bd9Sstevel@tonic-gate } else { 181*7c478bd9Sstevel@tonic-gate if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') { 182*7c478bd9Sstevel@tonic-gate debug2("stripping trailing dot from chost %s", chost); 183*7c478bd9Sstevel@tonic-gate chost[len - 1] = '\0'; 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate if (strcasecmp(resolvedname, chost) != 0) 186*7c478bd9Sstevel@tonic-gate log("userauth_hostbased mismatch: " 187*7c478bd9Sstevel@tonic-gate "client sends %s, but we resolve %s to %s", 188*7c478bd9Sstevel@tonic-gate chost, ipaddr, resolvedname); 189*7c478bd9Sstevel@tonic-gate if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) 190*7c478bd9Sstevel@tonic-gate return 0; 191*7c478bd9Sstevel@tonic-gate lookup = resolvedname; 192*7c478bd9Sstevel@tonic-gate } 193*7c478bd9Sstevel@tonic-gate debug2("userauth_hostbased: access allowed by auth_rhosts2"); 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate host_status = check_key_in_hostfiles(pw, key, lookup, 196*7c478bd9Sstevel@tonic-gate _PATH_SSH_SYSTEM_HOSTFILE, 197*7c478bd9Sstevel@tonic-gate options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE); 198*7c478bd9Sstevel@tonic-gate 199*7c478bd9Sstevel@tonic-gate /* backward compat if no key has been found. */ 200*7c478bd9Sstevel@tonic-gate if (host_status == HOST_NEW) 201*7c478bd9Sstevel@tonic-gate host_status = check_key_in_hostfiles(pw, key, lookup, 202*7c478bd9Sstevel@tonic-gate _PATH_SSH_SYSTEM_HOSTFILE2, 203*7c478bd9Sstevel@tonic-gate options.ignore_user_known_hosts ? NULL : 204*7c478bd9Sstevel@tonic-gate _PATH_SSH_USER_HOSTFILE2); 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate return (host_status == HOST_OK); 207*7c478bd9Sstevel@tonic-gate } 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate Authmethod method_hostbased = { 210*7c478bd9Sstevel@tonic-gate "hostbased", 211*7c478bd9Sstevel@tonic-gate &options.hostbased_authentication, 212*7c478bd9Sstevel@tonic-gate userauth_hostbased, 213*7c478bd9Sstevel@tonic-gate NULL, /* no abandon function */ 214*7c478bd9Sstevel@tonic-gate NULL, NULL, /* method data and hist data */ 215*7c478bd9Sstevel@tonic-gate 0, /* is not initial userauth */ 216*7c478bd9Sstevel@tonic-gate 0, 0, 0, /* counters */ 217*7c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0, 0 /* state */ 218*7c478bd9Sstevel@tonic-gate }; 219