Home | History | Annotate | Line # | Download | only in dist
auth2-hostbased.c revision 1.15
      1 /*	$NetBSD: auth2-hostbased.c,v 1.15 2019/01/27 02:08:33 pgoyette Exp $	*/
      2 /* $OpenBSD: auth2-hostbased.c,v 1.36 2018/07/31 03:10:27 djm Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "includes.h"
     29 __RCSID("$NetBSD: auth2-hostbased.c,v 1.15 2019/01/27 02:08:33 pgoyette Exp $");
     30 #include <sys/types.h>
     31 
     32 #include <pwd.h>
     33 #include <string.h>
     34 #include <stdarg.h>
     35 
     36 #include "xmalloc.h"
     37 #include "ssh2.h"
     38 #include "packet.h"
     39 #include "sshbuf.h"
     40 #include "log.h"
     41 #include "misc.h"
     42 #include "servconf.h"
     43 #include "compat.h"
     44 #include "sshkey.h"
     45 #include "hostfile.h"
     46 #include "auth.h"
     47 #include "canohost.h"
     48 #ifdef GSSAPI
     49 #include "ssh-gss.h"
     50 #endif
     51 #include "monitor_wrap.h"
     52 #include "pathnames.h"
     53 #include "ssherr.h"
     54 #include "match.h"
     55 
     56 /* import */
     57 extern ServerOptions options;
     58 extern u_char *session_id2;
     59 extern u_int session_id2_len;
     60 
     61 static int
     62 userauth_hostbased(struct ssh *ssh)
     63 {
     64 	Authctxt *authctxt = ssh->authctxt;
     65 	struct sshbuf *b;
     66 	struct sshkey *key = NULL;
     67 	char *pkalg, *cuser, *chost;
     68 	u_char *pkblob, *sig;
     69 	size_t alen, blen, slen;
     70 	int r, pktype, authenticated = 0;
     71 
     72 	/* XXX use sshkey_froms() */
     73 	if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
     74 	    (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
     75 	    (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
     76 	    (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
     77 	    (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
     78 		fatal("%s: packet parsing: %s", __func__, ssh_err(r));
     79 
     80 	debug("%s: cuser %s chost %s pkalg %s slen %zu", __func__,
     81 	    cuser, chost, pkalg, slen);
     82 #ifdef DEBUG_PK
     83 	debug("signature:");
     84 	sshbuf_dump_data(sig, siglen, stderr);
     85 #endif
     86 	pktype = sshkey_type_from_name(pkalg);
     87 	if (pktype == KEY_UNSPEC) {
     88 		/* this is perfectly legal */
     89 		logit("%s: unsupported public key algorithm: %s",
     90 		    __func__, pkalg);
     91 		goto done;
     92 	}
     93 	if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
     94 		error("%s: key_from_blob: %s", __func__, ssh_err(r));
     95 		goto done;
     96 	}
     97 	if (key == NULL) {
     98 		error("%s: cannot decode key: %s", __func__, pkalg);
     99 		goto done;
    100 	}
    101 	if (key->type != pktype) {
    102 		error("%s: type mismatch for decoded key "
    103 		    "(received %d, expected %d)", __func__, key->type, pktype);
    104 		goto done;
    105 	}
    106 	if (sshkey_type_plain(key->type) == KEY_RSA &&
    107 	    (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
    108 		error("Refusing RSA key because peer uses unsafe "
    109 		    "signature format");
    110 		goto done;
    111 	}
    112 	if (match_pattern_list(pkalg, options.hostbased_key_types, 0) != 1) {
    113 		logit("%s: key type %s not in HostbasedAcceptedKeyTypes",
    114 		    __func__, sshkey_type(key));
    115 		goto done;
    116 	}
    117 
    118 	if (!authctxt->valid || authctxt->user == NULL) {
    119 		debug2("%s: disabled because of invalid user", __func__);
    120 		goto done;
    121 	}
    122 
    123 	if ((b = sshbuf_new()) == NULL)
    124 		fatal("%s: sshbuf_new failed", __func__);
    125 	/* reconstruct packet */
    126 	if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
    127 	    (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
    128 	    (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
    129 	    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
    130 	    (r = sshbuf_put_cstring(b, "hostbased")) != 0 ||
    131 	    (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
    132 	    (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
    133 	    (r = sshbuf_put_cstring(b, chost)) != 0 ||
    134 	    (r = sshbuf_put_cstring(b, cuser)) != 0)
    135 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
    136 #ifdef DEBUG_PK
    137 	sshbuf_dump(b, stderr);
    138 #endif
    139 
    140 	auth2_record_info(authctxt,
    141 	    "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
    142 
    143 	/* test for allowed key and correct signature */
    144 	authenticated = 0;
    145 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
    146 	    PRIVSEP(sshkey_verify(key, sig, slen,
    147 	    sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat)) == 0)
    148 		authenticated = 1;
    149 
    150 	auth2_record_key(authctxt, authenticated, key);
    151 	sshbuf_free(b);
    152 done:
    153 	debug2("%s: authenticated %d", __func__, authenticated);
    154 	sshkey_free(key);
    155 	free(pkalg);
    156 	free(pkblob);
    157 	free(cuser);
    158 	free(chost);
    159 	free(sig);
    160 	return authenticated;
    161 }
    162 
    163 /* return 1 if given hostkey is allowed */
    164 int
    165 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
    166     struct sshkey *key)
    167 {
    168 	struct ssh *ssh = active_state; /* XXX */
    169 	const char *resolvedname, *ipaddr, *lookup, *reason;
    170 	HostStatus host_status;
    171 	int len;
    172 	char *fp;
    173 
    174 	if (auth_key_is_revoked(key))
    175 		return 0;
    176 
    177 	resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
    178 	ipaddr = ssh_remote_ipaddr(ssh);
    179 
    180 	debug2("%s: chost %s resolvedname %s ipaddr %s", __func__,
    181 	    chost, resolvedname, ipaddr);
    182 
    183 	if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
    184 		debug2("stripping trailing dot from chost %s", chost);
    185 		chost[len - 1] = '\0';
    186 	}
    187 
    188 	if (options.hostbased_uses_name_from_packet_only) {
    189 		if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
    190 			debug2("%s: auth_rhosts2 refused "
    191 			    "user \"%.100s\" host \"%.100s\" (from packet)",
    192 			    __func__, cuser, chost);
    193 			return 0;
    194 		}
    195 		lookup = chost;
    196 	} else {
    197 		if (strcasecmp(resolvedname, chost) != 0)
    198 			logit("userauth_hostbased mismatch: "
    199 			    "client sends %s, but we resolve %s to %s",
    200 			    chost, ipaddr, resolvedname);
    201 		if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
    202 			debug2("%s: auth_rhosts2 refused "
    203 			    "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
    204 			    __func__, cuser, resolvedname, ipaddr);
    205 			return 0;
    206 		}
    207 		lookup = resolvedname;
    208 	}
    209 	debug2("%s: access allowed by auth_rhosts2", __func__);
    210 
    211 	if (sshkey_is_cert(key) &&
    212 	    sshkey_cert_check_authority(key, 1, 0, lookup, &reason)) {
    213 		error("%s", reason);
    214 		auth_debug_add("%s", reason);
    215 		return 0;
    216 	}
    217 
    218 	host_status = check_key_in_hostfiles(pw, key, lookup,
    219 	    _PATH_SSH_SYSTEM_HOSTFILE,
    220 	    options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
    221 
    222 	/* backward compat if no key has been found. */
    223 	if (host_status == HOST_NEW) {
    224 		host_status = check_key_in_hostfiles(pw, key, lookup,
    225 		    _PATH_SSH_SYSTEM_HOSTFILE2,
    226 		    options.ignore_user_known_hosts ? NULL :
    227 		    _PATH_SSH_USER_HOSTFILE2);
    228 	}
    229 
    230 	if (host_status == HOST_OK) {
    231 		if (sshkey_is_cert(key)) {
    232 			if ((fp = sshkey_fingerprint(key->cert->signature_key,
    233 			    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
    234 				fatal("%s: sshkey_fingerprint fail", __func__);
    235 			verbose("Accepted certificate ID \"%s\" signed by "
    236 			    "%s CA %s from %s@%s", key->cert->key_id,
    237 			    sshkey_type(key->cert->signature_key), fp,
    238 			    cuser, lookup);
    239 		} else {
    240 			if ((fp = sshkey_fingerprint(key,
    241 			    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
    242 				fatal("%s: sshkey_fingerprint fail", __func__);
    243 			verbose("Accepted %s public key %s from %s@%s",
    244 			    sshkey_type(key), fp, cuser, lookup);
    245 		}
    246 		free(fp);
    247 	}
    248 
    249 	return (host_status == HOST_OK);
    250 }
    251 
    252 Authmethod method_hostbased = {
    253 	"hostbased",
    254 	userauth_hostbased,
    255 	&options.hostbased_authentication
    256 };
    257