1 /* $OpenBSD: auth2-hostbased.c,v 1.36 2018/07/31 03:10:27 djm Exp $ */
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
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.
28 #include <sys/types.h>
49 #include "monitor_wrap.h"
50 #include "pathnames.h"
55 extern ServerOptions options;
56 extern u_char *session_id2;
57 extern u_int session_id2_len;
60 userauth_hostbased(struct ssh *ssh)
62 Authctxt *authctxt = ssh->authctxt;
64 struct sshkey *key = NULL;
65 char *pkalg, *cuser, *chost;
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("%s: packet parsing: %s", __func__, ssh_err(r));
78 debug("%s: cuser %s chost %s pkalg %s slen %zu", __func__,
79 cuser, chost, pkalg, slen);
82 sshbuf_dump_data(sig, siglen, stderr);
84 pktype = sshkey_type_from_name(pkalg);
85 if (pktype == KEY_UNSPEC) {
86 /* this is perfectly legal */
87 logit("%s: unsupported public key algorithm: %s",
91 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
92 error("%s: key_from_blob: %s", __func__, ssh_err(r));
96 error("%s: cannot decode key: %s", __func__, pkalg);
99 if (key->type != pktype) {
100 error("%s: type mismatch for decoded key "
101 "(received %d, expected %d)", __func__, key->type, pktype);
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 "
110 if (match_pattern_list(pkalg, options.hostbased_key_types, 0) != 1) {
111 logit("%s: key type %s not in HostbasedAcceptedKeyTypes",
112 __func__, sshkey_type(key));
116 if (!authctxt->valid || authctxt->user == NULL) {
117 debug2("%s: disabled because of invalid user", __func__);
121 if ((b = sshbuf_new()) == NULL)
122 fatal("%s: sshbuf_new failed", __func__);
123 /* reconstruct packet */
124 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
125 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
126 (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
127 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
128 (r = sshbuf_put_cstring(b, "hostbased")) != 0 ||
129 (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
130 (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
131 (r = sshbuf_put_cstring(b, chost)) != 0 ||
132 (r = sshbuf_put_cstring(b, cuser)) != 0)
133 fatal("%s: buffer error: %s", __func__, ssh_err(r));
135 sshbuf_dump(b, stderr);
138 auth2_record_info(authctxt,
139 "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
141 /* test for allowed key and correct signature */
143 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
144 PRIVSEP(sshkey_verify(key, sig, slen,
145 sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat)) == 0)
148 auth2_record_key(authctxt, authenticated, key);
151 debug2("%s: authenticated %d", __func__, authenticated);
158 return authenticated;
161 /* return 1 if given hostkey is allowed */
163 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
166 struct ssh *ssh = active_state; /* XXX */
167 const char *resolvedname, *ipaddr, *lookup, *reason;
168 HostStatus host_status;
172 if (auth_key_is_revoked(key))
175 resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
176 ipaddr = ssh_remote_ipaddr(ssh);
178 debug2("%s: chost %s resolvedname %s ipaddr %s", __func__,
179 chost, resolvedname, ipaddr);
181 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
182 debug2("stripping trailing dot from chost %s", chost);
183 chost[len - 1] = '\0';
186 if (options.hostbased_uses_name_from_packet_only) {
187 if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
188 debug2("%s: auth_rhosts2 refused "
189 "user \"%.100s\" host \"%.100s\" (from packet)",
190 __func__, cuser, chost);
195 if (strcasecmp(resolvedname, chost) != 0)
196 logit("userauth_hostbased mismatch: "
197 "client sends %s, but we resolve %s to %s",
198 chost, ipaddr, resolvedname);
199 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
200 debug2("%s: auth_rhosts2 refused "
201 "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
202 __func__, cuser, resolvedname, ipaddr);
205 lookup = resolvedname;
207 debug2("%s: access allowed by auth_rhosts2", __func__);
209 if (sshkey_is_cert(key) &&
210 sshkey_cert_check_authority(key, 1, 0, lookup, &reason)) {
212 auth_debug_add("%s", reason);
216 host_status = check_key_in_hostfiles(pw, key, lookup,
217 _PATH_SSH_SYSTEM_HOSTFILE,
218 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
220 /* backward compat if no key has been found. */
221 if (host_status == HOST_NEW) {
222 host_status = check_key_in_hostfiles(pw, key, lookup,
223 _PATH_SSH_SYSTEM_HOSTFILE2,
224 options.ignore_user_known_hosts ? NULL :
225 _PATH_SSH_USER_HOSTFILE2);
228 if (host_status == HOST_OK) {
229 if (sshkey_is_cert(key)) {
230 if ((fp = sshkey_fingerprint(key->cert->signature_key,
231 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
232 fatal("%s: sshkey_fingerprint fail", __func__);
233 verbose("Accepted certificate ID \"%s\" signed by "
234 "%s CA %s from %s@%s", key->cert->key_id,
235 sshkey_type(key->cert->signature_key), fp,
238 if ((fp = sshkey_fingerprint(key,
239 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
240 fatal("%s: sshkey_fingerprint fail", __func__);
241 verbose("Accepted %s public key %s from %s@%s",
242 sshkey_type(key), fp, cuser, lookup);
247 return (host_status == HOST_OK);
250 Authmethod method_hostbased = {
253 &options.hostbased_authentication