Home | History | Annotate | Line # | Download | only in dist
      1 /*	$NetBSD: auth2-hostbased.c,v 1.26 2026/04/08 18:58:40 christos Exp $	*/
      2 /* $OpenBSD: auth2-hostbased.c,v 1.57 2026/04/02 07:48:13 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.26 2026/04/08 18:58:40 christos Exp $");
     30 #include <sys/types.h>
     31 
     32 #include <stdlib.h>
     33 #include <pwd.h>
     34 #include <string.h>
     35 #include <stdarg.h>
     36 
     37 #include "xmalloc.h"
     38 #include "ssh2.h"
     39 #include "packet.h"
     40 #include "kex.h"
     41 #include "sshbuf.h"
     42 #include "log.h"
     43 #include "misc.h"
     44 #include "servconf.h"
     45 #include "sshkey.h"
     46 #include "hostfile.h"
     47 #include "auth.h"
     48 #include "canohost.h"
     49 #ifdef GSSAPI
     50 #include "ssh-gss.h"
     51 #endif
     52 #include "monitor_wrap.h"
     53 #include "pathnames.h"
     54 #include "ssherr.h"
     55 #include "match.h"
     56 
     57 /* import */
     58 extern ServerOptions options;
     59 extern struct authmethod_cfg methodcfg_hostbased;
     60 
     61 static int
     62 userauth_hostbased(struct ssh *ssh, const char *method)
     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_fr(r, "parse packet");
     79 
     80 	debug_f("cuser %s chost %s pkalg %s slen %zu",
     81 	    cuser, chost, pkalg, slen);
     82 #ifdef DEBUG_PK
     83 	debug("signature:");
     84 	sshbuf_dump_data(sig, slen, stderr);
     85 #endif
     86 	pktype = sshkey_type_from_name(pkalg);
     87 	if (pktype == KEY_UNSPEC) {
     88 		/* this is perfectly legal */
     89 		logit_f("unsupported public key algorithm: %s",
     90 		    pkalg);
     91 		goto done;
     92 	}
     93 	if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
     94 		error_fr(r, "key_from_blob");
     95 		goto done;
     96 	}
     97 	if (key == NULL) {
     98 		error_f("cannot decode key: %s", pkalg);
     99 		goto done;
    100 	}
    101 	if (key->type != pktype || (sshkey_type_plain(pktype) == KEY_ECDSA &&
    102 	    sshkey_ecdsa_nid_from_name(pkalg) != key->ecdsa_nid)) {
    103 		error_f("key type mismatch for decoded key "
    104 		    "(received %s, expected %s)", sshkey_ssh_name(key), pkalg);
    105 		goto done;
    106 	}
    107 	if (match_pattern_list(pkalg, options.hostbased_accepted_algos, 0) != 1) {
    108 		logit_f("signature algorithm %s not in "
    109 		    "HostbasedAcceptedAlgorithms", pkalg);
    110 		goto done;
    111 	}
    112 	if ((r = sshkey_check_cert_sigtype(key,
    113 	    options.ca_sign_algorithms)) != 0) {
    114 		logit_fr(r, "certificate signature algorithm %s",
    115 		    (key->cert == NULL || key->cert->signature_type == NULL) ?
    116 		    "(null)" : key->cert->signature_type);
    117 		goto done;
    118 	}
    119 	if ((r = sshkey_check_rsa_length(key,
    120 	    options.required_rsa_size)) != 0) {
    121 		logit_r(r, "refusing %s key", sshkey_type(key));
    122 		goto done;
    123 	}
    124 
    125 	if (!authctxt->valid || authctxt->user == NULL) {
    126 		debug2_f("disabled because of invalid user");
    127 		goto done;
    128 	}
    129 
    130 	if ((b = sshbuf_new()) == NULL)
    131 		fatal_f("sshbuf_new failed");
    132 	/* reconstruct packet */
    133 	if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
    134 	    (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
    135 	    (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
    136 	    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
    137 	    (r = sshbuf_put_cstring(b, method)) != 0 ||
    138 	    (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
    139 	    (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
    140 	    (r = sshbuf_put_cstring(b, chost)) != 0 ||
    141 	    (r = sshbuf_put_cstring(b, cuser)) != 0)
    142 		fatal_fr(r, "reconstruct packet");
    143 #ifdef DEBUG_PK
    144 	sshbuf_dump(b, stderr);
    145 #endif
    146 
    147 	auth2_record_info(authctxt,
    148 	    "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
    149 
    150 	/* test for allowed key and correct signature */
    151 	authenticated = 0;
    152 	if (mm_hostbased_key_allowed(ssh, authctxt->pw, cuser,
    153 	    chost, key) &&
    154 	    mm_sshkey_verify(key, sig, slen,
    155 	    sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL) == 0)
    156 		authenticated = 1;
    157 
    158 	auth2_record_key(authctxt, authenticated, key);
    159 	sshbuf_free(b);
    160 done:
    161 	debug2_f("authenticated %d", authenticated);
    162 	sshkey_free(key);
    163 	free(pkalg);
    164 	free(pkblob);
    165 	free(cuser);
    166 	free(chost);
    167 	free(sig);
    168 	return authenticated;
    169 }
    170 
    171 /* return 1 if given hostkey is allowed */
    172 int
    173 hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
    174     const char *cuser, char *chost, struct sshkey *key)
    175 {
    176 	const char *resolvedname, *ipaddr, *lookup, *reason;
    177 	HostStatus host_status;
    178 	int len;
    179 	char *fp;
    180 
    181 	if (auth_key_is_revoked(key))
    182 		return 0;
    183 
    184 	resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
    185 	ipaddr = ssh_remote_ipaddr(ssh);
    186 
    187 	debug2_f("chost %s resolvedname %s ipaddr %s",
    188 	    chost, resolvedname, ipaddr);
    189 
    190 	if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
    191 		debug2("stripping trailing dot from chost %s", chost);
    192 		chost[len - 1] = '\0';
    193 	}
    194 
    195 	if (options.hostbased_uses_name_from_packet_only) {
    196 		if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
    197 			debug2_f("auth_rhosts2 refused user \"%.100s\" "
    198 			    "host \"%.100s\" (from packet)", cuser, chost);
    199 			return 0;
    200 		}
    201 		lookup = chost;
    202 	} else {
    203 		if (strcasecmp(resolvedname, chost) != 0)
    204 			logit("userauth_hostbased mismatch: "
    205 			    "client sends %s, but we resolve %s to %s",
    206 			    chost, ipaddr, resolvedname);
    207 		if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
    208 			debug2_f("auth_rhosts2 refused "
    209 			    "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
    210 			    cuser, resolvedname, ipaddr);
    211 			return 0;
    212 		}
    213 		lookup = resolvedname;
    214 	}
    215 	debug2_f("access allowed by auth_rhosts2");
    216 
    217 	if (sshkey_is_cert(key) && sshkey_cert_check_host(key, lookup,
    218 	     options.ca_sign_algorithms, &reason) != 0) {
    219 		if ((fp = sshkey_fingerprint(key->cert->signature_key,
    220 		    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
    221 			fatal_f("sshkey_fingerprint fail");
    222 		error("Refusing certificate ID \"%s\" serial=%llu signed by "
    223 		    "%s CA %s: %s", key->cert->key_id,
    224 		    (unsigned long long)key->cert->serial,
    225 		    sshkey_type(key->cert->signature_key), fp, reason);
    226 		auth_debug_add("Refused Certificate ID \"%s\" serial=%llu: %s",
    227 		    key->cert->key_id, (unsigned long long)key->cert->serial,
    228 		    reason);
    229 		free(fp);
    230 		return 0;
    231 	}
    232 
    233 	host_status = check_key_in_hostfiles(pw, key, lookup,
    234 	    _PATH_SSH_SYSTEM_HOSTFILE,
    235 	    options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
    236 
    237 	/* backward compat if no key has been found. */
    238 	if (host_status == HOST_NEW) {
    239 		host_status = check_key_in_hostfiles(pw, key, lookup,
    240 		    _PATH_SSH_SYSTEM_HOSTFILE2,
    241 		    options.ignore_user_known_hosts ? NULL :
    242 		    _PATH_SSH_USER_HOSTFILE2);
    243 	}
    244 
    245 	if (host_status == HOST_OK) {
    246 		if (sshkey_is_cert(key)) {
    247 			if ((fp = sshkey_fingerprint(key->cert->signature_key,
    248 			    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
    249 				fatal_f("sshkey_fingerprint fail");
    250 			verbose("Accepted certificate ID \"%s\" signed by "
    251 			    "%s CA %s from %s@%s", key->cert->key_id,
    252 			    sshkey_type(key->cert->signature_key), fp,
    253 			    cuser, lookup);
    254 		} else {
    255 			if ((fp = sshkey_fingerprint(key,
    256 			    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
    257 				fatal_f("sshkey_fingerprint fail");
    258 			verbose("Accepted %s public key %s from %s@%s",
    259 			    sshkey_type(key), fp, cuser, lookup);
    260 		}
    261 		free(fp);
    262 	}
    263 
    264 	return (host_status == HOST_OK);
    265 }
    266 
    267 Authmethod method_hostbased = {
    268 	&methodcfg_hostbased,
    269 	userauth_hostbased,
    270 };
    271