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