blob: f70609cb0a6b2290ae548577c16385984839a7da [file] [log] [blame]
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001/* $OpenBSD: auth2-hostbased.c,v 1.34 2018/07/03 11:39:54 djm Exp $ */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00002/*
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"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000027
Damien Miller9f2abc42006-07-10 20:53:08 +100028#include <sys/types.h>
29
30#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100031#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include <stdarg.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100033
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000034#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100035#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000036#include "packet.h"
37#include "buffer.h"
38#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100039#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000040#include "servconf.h"
41#include "compat.h"
markus@openbsd.orgeb766982017-05-30 14:25:42 +000042#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100043#include "hostfile.h"
44#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000045#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#ifdef GSSAPI
47#include "ssh-gss.h"
48#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000049#include "monitor_wrap.h"
50#include "pathnames.h"
markus@openbsd.orgeb766982017-05-30 14:25:42 +000051#include "ssherr.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +000052#include "match.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000053
54/* import */
55extern ServerOptions options;
56extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100057extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000058
59static int
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000060userauth_hostbased(struct ssh *ssh)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000061{
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000062 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgeb766982017-05-30 14:25:42 +000063 struct sshbuf *b;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000064 struct sshkey *key = NULL;
djm@openbsd.org14b5c632018-01-23 05:27:21 +000065 char *pkalg, *cuser, *chost;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000066 u_char *pkblob, *sig;
markus@openbsd.orgeb766982017-05-30 14:25:42 +000067 size_t alen, blen, slen;
68 int r, pktype, authenticated = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000069
70 if (!authctxt->valid) {
markus@openbsd.orgeb766982017-05-30 14:25:42 +000071 debug2("%s: disabled because of invalid user", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000072 return 0;
73 }
markus@openbsd.orgeb766982017-05-30 14:25:42 +000074 /* XXX use sshkey_froms() */
75 if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
76 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
77 (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
78 (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
79 (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
80 fatal("%s: packet parsing: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000081
markus@openbsd.orgeb766982017-05-30 14:25:42 +000082 debug("%s: cuser %s chost %s pkalg %s slen %zu", __func__,
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000083 cuser, chost, pkalg, slen);
84#ifdef DEBUG_PK
85 debug("signature:");
markus@openbsd.orgeb766982017-05-30 14:25:42 +000086 sshbuf_dump_data(sig, siglen, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000087#endif
markus@openbsd.orgeb766982017-05-30 14:25:42 +000088 pktype = sshkey_type_from_name(pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000089 if (pktype == KEY_UNSPEC) {
90 /* this is perfectly legal */
markus@openbsd.orgeb766982017-05-30 14:25:42 +000091 logit("%s: unsupported public key algorithm: %s",
92 __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000093 goto done;
94 }
markus@openbsd.orgeb766982017-05-30 14:25:42 +000095 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
96 error("%s: key_from_blob: %s", __func__, ssh_err(r));
97 goto done;
98 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000099 if (key == NULL) {
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000100 error("%s: cannot decode key: %s", __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000101 goto done;
102 }
103 if (key->type != pktype) {
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000104 error("%s: type mismatch for decoded key "
105 "(received %d, expected %d)", __func__, key->type, pktype);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000106 goto done;
107 }
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000108 if (sshkey_type_plain(key->type) == KEY_RSA &&
109 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
Damien Miller324541e2013-12-31 12:25:40 +1100110 error("Refusing RSA key because peer uses unsafe "
111 "signature format");
112 goto done;
113 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000114 if (match_pattern_list(pkalg, options.hostbased_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000115 logit("%s: key type %s not in HostbasedAcceptedKeyTypes",
116 __func__, sshkey_type(key));
117 goto done;
118 }
119
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000120 if ((b = sshbuf_new()) == NULL)
121 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000122 /* reconstruct packet */
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000123 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
124 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
125 (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000126 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000127 (r = sshbuf_put_cstring(b, "hostbased")) != 0 ||
128 (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
129 (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
130 (r = sshbuf_put_cstring(b, chost)) != 0 ||
131 (r = sshbuf_put_cstring(b, cuser)) != 0)
132 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000133#ifdef DEBUG_PK
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000134 sshbuf_dump(b, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000135#endif
Damien Miller20bdcd72013-07-18 16:10:09 +1000136
djm@openbsd.org8f574952017-06-24 06:34:38 +0000137 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +1000138 "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
139
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000140 /* test for allowed key and correct signature */
141 authenticated = 0;
142 if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000143 PRIVSEP(sshkey_verify(key, sig, slen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000144 sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat)) == 0)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000145 authenticated = 1;
146
djm@openbsd.org8f574952017-06-24 06:34:38 +0000147 auth2_record_key(authctxt, authenticated, key);
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000148 sshbuf_free(b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000149done:
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000150 debug2("%s: authenticated %d", __func__, authenticated);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000151 sshkey_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000152 free(pkalg);
153 free(pkblob);
154 free(cuser);
155 free(chost);
156 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000157 return authenticated;
158}
159
160/* return 1 if given hostkey is allowed */
161int
162hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000163 struct sshkey *key)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000164{
djm@openbsd.org95767262016-03-07 19:02:43 +0000165 struct ssh *ssh = active_state; /* XXX */
Damien Millerc1583312010-08-05 13:04:50 +1000166 const char *resolvedname, *ipaddr, *lookup, *reason;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000167 HostStatus host_status;
168 int len;
Damien Millerc1583312010-08-05 13:04:50 +1000169 char *fp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000170
Damien Miller1aed65e2010-03-04 21:53:35 +1100171 if (auth_key_is_revoked(key))
172 return 0;
173
djm@openbsd.org95767262016-03-07 19:02:43 +0000174 resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
175 ipaddr = ssh_remote_ipaddr(ssh);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000176
djm@openbsd.org5191df92014-12-23 22:42:48 +0000177 debug2("%s: chost %s resolvedname %s ipaddr %s", __func__,
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000178 chost, resolvedname, ipaddr);
179
Damien Millera1d03a52008-07-17 18:57:19 +1000180 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
181 debug2("stripping trailing dot from chost %s", chost);
182 chost[len - 1] = '\0';
183 }
184
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000185 if (options.hostbased_uses_name_from_packet_only) {
djm@openbsd.org5191df92014-12-23 22:42:48 +0000186 if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
187 debug2("%s: auth_rhosts2 refused "
188 "user \"%.100s\" host \"%.100s\" (from packet)",
189 __func__, cuser, chost);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000190 return 0;
djm@openbsd.org5191df92014-12-23 22:42:48 +0000191 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000192 lookup = chost;
193 } else {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000194 if (strcasecmp(resolvedname, chost) != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000195 logit("userauth_hostbased mismatch: "
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000196 "client sends %s, but we resolve %s to %s",
197 chost, ipaddr, resolvedname);
djm@openbsd.org5191df92014-12-23 22:42:48 +0000198 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
199 debug2("%s: auth_rhosts2 refused "
200 "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
201 __func__, cuser, resolvedname, ipaddr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000202 return 0;
djm@openbsd.org5191df92014-12-23 22:42:48 +0000203 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000204 lookup = resolvedname;
205 }
djm@openbsd.org5191df92014-12-23 22:42:48 +0000206 debug2("%s: access allowed by auth_rhosts2", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000207
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000208 if (sshkey_is_cert(key) &&
209 sshkey_cert_check_authority(key, 1, 0, lookup, &reason)) {
Damien Millerc1583312010-08-05 13:04:50 +1000210 error("%s", reason);
211 auth_debug_add("%s", reason);
212 return 0;
213 }
214
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000215 host_status = check_key_in_hostfiles(pw, key, lookup,
216 _PATH_SSH_SYSTEM_HOSTFILE,
217 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
218
219 /* backward compat if no key has been found. */
Damien Millerc1583312010-08-05 13:04:50 +1000220 if (host_status == HOST_NEW) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000221 host_status = check_key_in_hostfiles(pw, key, lookup,
222 _PATH_SSH_SYSTEM_HOSTFILE2,
223 options.ignore_user_known_hosts ? NULL :
224 _PATH_SSH_USER_HOSTFILE2);
Damien Millerc1583312010-08-05 13:04:50 +1000225 }
226
227 if (host_status == HOST_OK) {
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000228 if (sshkey_is_cert(key)) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000229 if ((fp = sshkey_fingerprint(key->cert->signature_key,
230 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
231 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Millerc1583312010-08-05 13:04:50 +1000232 verbose("Accepted certificate ID \"%s\" signed by "
233 "%s CA %s from %s@%s", key->cert->key_id,
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000234 sshkey_type(key->cert->signature_key), fp,
Damien Millerc1583312010-08-05 13:04:50 +1000235 cuser, lookup);
236 } else {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000237 if ((fp = sshkey_fingerprint(key,
238 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
239 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Millerc1583312010-08-05 13:04:50 +1000240 verbose("Accepted %s public key %s from %s@%s",
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000241 sshkey_type(key), fp, cuser, lookup);
Damien Millerc1583312010-08-05 13:04:50 +1000242 }
Darren Tuckera627d422013-06-02 07:31:17 +1000243 free(fp);
Damien Millerc1583312010-08-05 13:04:50 +1000244 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000245
246 return (host_status == HOST_OK);
247}
248
249Authmethod method_hostbased = {
250 "hostbased",
251 userauth_hostbased,
252 &options.hostbased_authentication
253};