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