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