blob: 5e9b7c65d32561e1ae523b5557938a24850e5a90 [file] [log] [blame]
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00001/* $OpenBSD: auth2-hostbased.c,v 1.42 2019/11/25 00:51:37 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
djm@openbsd.orgbe02d7c2019-09-06 04:53:27 +000030#include <stdlib.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100031#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100032#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include <stdarg.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100034
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000035#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000037#include "packet.h"
markus@openbsd.orgc7d39ac2018-07-09 21:35:50 +000038#include "sshbuf.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000039#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100040#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000041#include "servconf.h"
42#include "compat.h"
markus@openbsd.orgeb766982017-05-30 14:25:42 +000043#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100044#include "hostfile.h"
45#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000046#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100047#ifdef GSSAPI
48#include "ssh-gss.h"
49#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000050#include "monitor_wrap.h"
51#include "pathnames.h"
markus@openbsd.orgeb766982017-05-30 14:25:42 +000052#include "ssherr.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +000053#include "match.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000054
55/* import */
56extern ServerOptions options;
57extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100058extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000059
60static int
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000061userauth_hostbased(struct ssh *ssh)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000062{
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000063 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgeb766982017-05-30 14:25:42 +000064 struct sshbuf *b;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000065 struct sshkey *key = NULL;
djm@openbsd.org14b5c632018-01-23 05:27:21 +000066 char *pkalg, *cuser, *chost;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000067 u_char *pkblob, *sig;
markus@openbsd.orgeb766982017-05-30 14:25:42 +000068 size_t alen, blen, slen;
69 int r, pktype, authenticated = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000070
markus@openbsd.orgeb766982017-05-30 14:25:42 +000071 /* 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("%s: packet parsing: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000078
markus@openbsd.orgeb766982017-05-30 14:25:42 +000079 debug("%s: cuser %s chost %s pkalg %s slen %zu", __func__,
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000080 cuser, chost, pkalg, slen);
81#ifdef DEBUG_PK
82 debug("signature:");
mestre@openbsd.org086cc612018-08-28 12:17:45 +000083 sshbuf_dump_data(sig, slen, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000084#endif
markus@openbsd.orgeb766982017-05-30 14:25:42 +000085 pktype = sshkey_type_from_name(pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000086 if (pktype == KEY_UNSPEC) {
87 /* this is perfectly legal */
markus@openbsd.orgeb766982017-05-30 14:25:42 +000088 logit("%s: unsupported public key algorithm: %s",
89 __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000090 goto done;
91 }
markus@openbsd.orgeb766982017-05-30 14:25:42 +000092 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
93 error("%s: key_from_blob: %s", __func__, ssh_err(r));
94 goto done;
95 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000096 if (key == NULL) {
markus@openbsd.orgeb766982017-05-30 14:25:42 +000097 error("%s: cannot decode key: %s", __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000098 goto done;
99 }
100 if (key->type != pktype) {
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000101 error("%s: type mismatch for decoded key "
102 "(received %d, expected %d)", __func__, key->type, pktype);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000103 goto done;
104 }
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000105 if (sshkey_type_plain(key->type) == KEY_RSA &&
106 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
Damien Miller324541e2013-12-31 12:25:40 +1100107 error("Refusing RSA key because peer uses unsafe "
108 "signature format");
109 goto done;
110 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000111 if (match_pattern_list(pkalg, options.hostbased_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000112 logit("%s: key type %s not in HostbasedAcceptedKeyTypes",
113 __func__, sshkey_type(key));
114 goto done;
115 }
djm@openbsd.org86e57372018-09-20 03:28:06 +0000116 if ((r = sshkey_check_cert_sigtype(key,
117 options.ca_sign_algorithms)) != 0) {
118 logit("%s: certificate signature algorithm %s: %s", __func__,
119 (key->cert == NULL || key->cert->signature_type == NULL) ?
120 "(null)" : key->cert->signature_type, ssh_err(r));
121 goto done;
122 }
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000123
djm@openbsd.org74287f52018-07-31 03:10:27 +0000124 if (!authctxt->valid || authctxt->user == NULL) {
125 debug2("%s: disabled because of invalid user", __func__);
126 goto done;
127 }
128
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000129 if ((b = sshbuf_new()) == NULL)
130 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000131 /* reconstruct packet */
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000132 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 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000135 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000136 (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("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000142#ifdef DEBUG_PK
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000143 sshbuf_dump(b, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000144#endif
Damien Miller20bdcd72013-07-18 16:10:09 +1000145
djm@openbsd.org8f574952017-06-24 06:34:38 +0000146 auth2_record_info(authctxt,
Damien Miller20bdcd72013-07-18 16:10:09 +1000147 "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
148
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000149 /* test for allowed key and correct signature */
150 authenticated = 0;
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000151 if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
152 chost, key)) &&
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000153 PRIVSEP(sshkey_verify(key, sig, slen,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000154 sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL)) == 0)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000155 authenticated = 1;
156
djm@openbsd.org8f574952017-06-24 06:34:38 +0000157 auth2_record_key(authctxt, authenticated, key);
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000158 sshbuf_free(b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000159done:
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000160 debug2("%s: authenticated %d", __func__, authenticated);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000161 sshkey_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000162 free(pkalg);
163 free(pkblob);
164 free(cuser);
165 free(chost);
166 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000167 return authenticated;
168}
169
170/* return 1 if given hostkey is allowed */
171int
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000172hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
173 const char *cuser, char *chost, struct sshkey *key)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000174{
Damien Millerc1583312010-08-05 13:04:50 +1000175 const char *resolvedname, *ipaddr, *lookup, *reason;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000176 HostStatus host_status;
177 int len;
Damien Millerc1583312010-08-05 13:04:50 +1000178 char *fp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000179
Damien Miller1aed65e2010-03-04 21:53:35 +1100180 if (auth_key_is_revoked(key))
181 return 0;
182
djm@openbsd.org95767262016-03-07 19:02:43 +0000183 resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
184 ipaddr = ssh_remote_ipaddr(ssh);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000185
djm@openbsd.org5191df92014-12-23 22:42:48 +0000186 debug2("%s: chost %s resolvedname %s ipaddr %s", __func__,
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000187 chost, resolvedname, ipaddr);
188
Damien Millera1d03a52008-07-17 18:57:19 +1000189 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
190 debug2("stripping trailing dot from chost %s", chost);
191 chost[len - 1] = '\0';
192 }
193
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000194 if (options.hostbased_uses_name_from_packet_only) {
djm@openbsd.org5191df92014-12-23 22:42:48 +0000195 if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
196 debug2("%s: auth_rhosts2 refused "
197 "user \"%.100s\" host \"%.100s\" (from packet)",
198 __func__, cuser, chost);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000199 return 0;
djm@openbsd.org5191df92014-12-23 22:42:48 +0000200 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000201 lookup = chost;
202 } else {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000203 if (strcasecmp(resolvedname, chost) != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000204 logit("userauth_hostbased mismatch: "
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000205 "client sends %s, but we resolve %s to %s",
206 chost, ipaddr, resolvedname);
djm@openbsd.org5191df92014-12-23 22:42:48 +0000207 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
208 debug2("%s: auth_rhosts2 refused "
209 "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
210 __func__, cuser, resolvedname, ipaddr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000211 return 0;
djm@openbsd.org5191df92014-12-23 22:42:48 +0000212 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000213 lookup = resolvedname;
214 }
djm@openbsd.org5191df92014-12-23 22:42:48 +0000215 debug2("%s: access allowed by auth_rhosts2", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000216
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000217 if (sshkey_is_cert(key) &&
218 sshkey_cert_check_authority(key, 1, 0, lookup, &reason)) {
Damien Millerc1583312010-08-05 13:04:50 +1000219 error("%s", reason);
220 auth_debug_add("%s", reason);
221 return 0;
222 }
223
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000224 host_status = check_key_in_hostfiles(pw, key, lookup,
225 _PATH_SSH_SYSTEM_HOSTFILE,
226 options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
227
228 /* backward compat if no key has been found. */
Damien Millerc1583312010-08-05 13:04:50 +1000229 if (host_status == HOST_NEW) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000230 host_status = check_key_in_hostfiles(pw, key, lookup,
231 _PATH_SSH_SYSTEM_HOSTFILE2,
232 options.ignore_user_known_hosts ? NULL :
233 _PATH_SSH_USER_HOSTFILE2);
Damien Millerc1583312010-08-05 13:04:50 +1000234 }
235
236 if (host_status == HOST_OK) {
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000237 if (sshkey_is_cert(key)) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000238 if ((fp = sshkey_fingerprint(key->cert->signature_key,
239 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
240 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Millerc1583312010-08-05 13:04:50 +1000241 verbose("Accepted certificate ID \"%s\" signed by "
242 "%s CA %s from %s@%s", key->cert->key_id,
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000243 sshkey_type(key->cert->signature_key), fp,
Damien Millerc1583312010-08-05 13:04:50 +1000244 cuser, lookup);
245 } else {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000246 if ((fp = sshkey_fingerprint(key,
247 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
248 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Millerc1583312010-08-05 13:04:50 +1000249 verbose("Accepted %s public key %s from %s@%s",
markus@openbsd.orgeb766982017-05-30 14:25:42 +0000250 sshkey_type(key), fp, cuser, lookup);
Damien Millerc1583312010-08-05 13:04:50 +1000251 }
Darren Tuckera627d422013-06-02 07:31:17 +1000252 free(fp);
Damien Millerc1583312010-08-05 13:04:50 +1000253 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000254
255 return (host_status == HOST_OK);
256}
257
258Authmethod method_hostbased = {
259 "hostbased",
260 userauth_hostbased,
261 &options.hostbased_authentication
262};