vendor/OPENSSH: upgrade from 8.8p1 top 9.1p1
[dragonfly.git] / crypto / openssh / auth2-hostbased.c
blob6b517db411dff7c9f873593c25ec7c877ae488ca
1 /* $OpenBSD: auth2-hostbased.c,v 1.50 2022/09/17 10:34:29 djm Exp $ */
2 /*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
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.
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.
26 #include "includes.h"
28 #include <sys/types.h>
30 #include <stdlib.h>
31 #include <pwd.h>
32 #include <string.h>
33 #include <stdarg.h>
35 #include "xmalloc.h"
36 #include "ssh2.h"
37 #include "packet.h"
38 #include "kex.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"
56 /* import */
57 extern ServerOptions options;
59 static int
60 userauth_hostbased(struct ssh *ssh, const char *method)
62 Authctxt *authctxt = ssh->authctxt;
63 struct sshbuf *b;
64 struct sshkey *key = NULL;
65 char *pkalg, *cuser, *chost;
66 u_char *pkblob, *sig;
67 size_t alen, blen, slen;
68 int r, pktype, authenticated = 0;
70 /* XXX use sshkey_froms() */
71 if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
72 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
73 (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
74 (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
75 (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
76 fatal_fr(r, "parse packet");
78 debug_f("cuser %s chost %s pkalg %s slen %zu",
79 cuser, chost, pkalg, slen);
80 #ifdef DEBUG_PK
81 debug("signature:");
82 sshbuf_dump_data(sig, slen, stderr);
83 #endif
84 pktype = sshkey_type_from_name(pkalg);
85 if (pktype == KEY_UNSPEC) {
86 /* this is perfectly legal */
87 logit_f("unsupported public key algorithm: %s",
88 pkalg);
89 goto done;
91 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
92 error_fr(r, "key_from_blob");
93 goto done;
95 if (key == NULL) {
96 error_f("cannot decode key: %s", pkalg);
97 goto done;
99 if (key->type != pktype) {
100 error_f("type mismatch for decoded key "
101 "(received %d, expected %d)", key->type, pktype);
102 goto done;
104 if (sshkey_type_plain(key->type) == KEY_RSA &&
105 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
106 error("Refusing RSA key because peer uses unsafe "
107 "signature format");
108 goto done;
110 if (match_pattern_list(pkalg, options.hostbased_accepted_algos, 0) != 1) {
111 logit_f("signature algorithm %s not in "
112 "HostbasedAcceptedAlgorithms", pkalg);
113 goto done;
115 if ((r = sshkey_check_cert_sigtype(key,
116 options.ca_sign_algorithms)) != 0) {
117 logit_fr(r, "certificate signature algorithm %s",
118 (key->cert == NULL || key->cert->signature_type == NULL) ?
119 "(null)" : key->cert->signature_type);
120 goto done;
122 if ((r = sshkey_check_rsa_length(key,
123 options.required_rsa_size)) != 0) {
124 logit_r(r, "refusing %s key", sshkey_type(key));
125 goto done;
128 if (!authctxt->valid || authctxt->user == NULL) {
129 debug2_f("disabled because of invalid user");
130 goto done;
133 if ((b = sshbuf_new()) == NULL)
134 fatal_f("sshbuf_new failed");
135 /* reconstruct packet */
136 if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
137 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
138 (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
139 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
140 (r = sshbuf_put_cstring(b, method)) != 0 ||
141 (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
142 (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
143 (r = sshbuf_put_cstring(b, chost)) != 0 ||
144 (r = sshbuf_put_cstring(b, cuser)) != 0)
145 fatal_fr(r, "reconstruct packet");
146 #ifdef DEBUG_PK
147 sshbuf_dump(b, stderr);
148 #endif
150 auth2_record_info(authctxt,
151 "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
153 /* test for allowed key and correct signature */
154 authenticated = 0;
155 if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
156 chost, key)) &&
157 PRIVSEP(sshkey_verify(key, sig, slen,
158 sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL)) == 0)
159 authenticated = 1;
161 auth2_record_key(authctxt, authenticated, key);
162 sshbuf_free(b);
163 done:
164 debug2_f("authenticated %d", authenticated);
165 sshkey_free(key);
166 free(pkalg);
167 free(pkblob);
168 free(cuser);
169 free(chost);
170 free(sig);
171 return authenticated;
174 /* return 1 if given hostkey is allowed */
176 hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
177 const char *cuser, char *chost, struct sshkey *key)
179 const char *resolvedname, *ipaddr, *lookup, *reason;
180 HostStatus host_status;
181 int len;
182 char *fp;
184 if (auth_key_is_revoked(key))
185 return 0;
187 resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
188 ipaddr = ssh_remote_ipaddr(ssh);
190 debug2_f("chost %s resolvedname %s ipaddr %s",
191 chost, resolvedname, ipaddr);
193 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
194 debug2("stripping trailing dot from chost %s", chost);
195 chost[len - 1] = '\0';
198 if (options.hostbased_uses_name_from_packet_only) {
199 if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
200 debug2_f("auth_rhosts2 refused user \"%.100s\" "
201 "host \"%.100s\" (from packet)", cuser, chost);
202 return 0;
204 lookup = chost;
205 } else {
206 if (strcasecmp(resolvedname, chost) != 0)
207 logit("userauth_hostbased mismatch: "
208 "client sends %s, but we resolve %s to %s",
209 chost, ipaddr, resolvedname);
210 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
211 debug2_f("auth_rhosts2 refused "
212 "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
213 cuser, resolvedname, ipaddr);
214 return 0;
216 lookup = resolvedname;
218 debug2_f("access allowed by auth_rhosts2");
220 if (sshkey_is_cert(key) &&
221 sshkey_cert_check_authority_now(key, 1, 0, 0, lookup, &reason)) {
222 error("%s", reason);
223 auth_debug_add("%s", reason);
224 return 0;
227 host_status = check_key_in_hostfiles(pw, key, lookup,
228 _PATH_SSH_SYSTEM_HOSTFILE,
229 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
231 /* backward compat if no key has been found. */
232 if (host_status == HOST_NEW) {
233 host_status = check_key_in_hostfiles(pw, key, lookup,
234 _PATH_SSH_SYSTEM_HOSTFILE2,
235 options.ignore_user_known_hosts ? NULL :
236 _PATH_SSH_USER_HOSTFILE2);
239 if (host_status == HOST_OK) {
240 if (sshkey_is_cert(key)) {
241 if ((fp = sshkey_fingerprint(key->cert->signature_key,
242 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
243 fatal_f("sshkey_fingerprint fail");
244 verbose("Accepted certificate ID \"%s\" signed by "
245 "%s CA %s from %s@%s", key->cert->key_id,
246 sshkey_type(key->cert->signature_key), fp,
247 cuser, lookup);
248 } else {
249 if ((fp = sshkey_fingerprint(key,
250 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
251 fatal_f("sshkey_fingerprint fail");
252 verbose("Accepted %s public key %s from %s@%s",
253 sshkey_type(key), fp, cuser, lookup);
255 free(fp);
258 return (host_status == HOST_OK);
261 Authmethod method_hostbased = {
262 "hostbased",
263 NULL,
264 userauth_hostbased,
265 &options.hostbased_authentication