blob: a4a355972522f4cfbd36b06caba3be871bc85dae [file] [log] [blame]
markus@openbsd.orgfbff6052020-03-06 18:25:12 +00001/* $OpenBSD: hostfile.c,v 1.79 2020/03/06 18:25:12 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for manipulating the known hosts files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 * Copyright (c) 1999 Niels Provos. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Damien Millere1776152005-03-01 21:47:37 +110040
Damien Miller8ec8c3e2006-07-10 20:35:38 +100041#include <sys/types.h>
djm@openbsd.org8d4f8722015-01-26 03:04:45 +000042#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100043
44#include <netinet/in.h>
45
djm@openbsd.orgc29811c2015-01-18 21:40:23 +000046#include <errno.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100047#include <resolv.h>
Damien Millerded319c2006-09-01 15:38:36 +100048#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100049#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100050#include <stdlib.h>
51#include <string.h>
djm@openbsd.org8d4f8722015-01-26 03:04:45 +000052#include <unistd.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100053
Damien Millerd7834352006-08-05 12:39:39 +100054#include "xmalloc.h"
Damien Miller450a7a12000-03-26 13:04:51 +100055#include "match.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000056#include "sshkey.h"
Damien Miller450a7a12000-03-26 13:04:51 +100057#include "hostfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "log.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110059#include "misc.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000060#include "ssherr.h"
Damien Millerb3051d02014-01-10 10:58:53 +110061#include "digest.h"
Damien Miller4e8d9372014-02-04 11:02:42 +110062#include "hmac.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110063
64struct hostkeys {
65 struct hostkey_entry *entries;
66 u_int num_entries;
67};
Damien Millere1776152005-03-01 21:47:37 +110068
djm@openbsd.orgc29811c2015-01-18 21:40:23 +000069/* XXX hmac is too easy to dictionary attack; use bcrypt? */
70
Damien Millere1776152005-03-01 21:47:37 +110071static int
Damien Millerce986542013-07-18 16:12:44 +100072extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len)
Damien Millere1776152005-03-01 21:47:37 +110073{
74 char *p, *b64salt;
75 u_int b64len;
76 int ret;
77
78 if (l < sizeof(HASH_MAGIC) - 1) {
79 debug2("extract_salt: string too short");
80 return (-1);
81 }
82 if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
83 debug2("extract_salt: invalid magic identifier");
84 return (-1);
85 }
86 s += sizeof(HASH_MAGIC) - 1;
87 l -= sizeof(HASH_MAGIC) - 1;
88 if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
89 debug2("extract_salt: missing salt termination character");
90 return (-1);
91 }
92
93 b64len = p - s;
94 /* Sanity check */
95 if (b64len == 0 || b64len > 1024) {
96 debug2("extract_salt: bad encoded salt length %u", b64len);
97 return (-1);
98 }
99 b64salt = xmalloc(1 + b64len);
100 memcpy(b64salt, s, b64len);
101 b64salt[b64len] = '\0';
102
103 ret = __b64_pton(b64salt, salt, salt_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000104 free(b64salt);
Damien Millere1776152005-03-01 21:47:37 +1100105 if (ret == -1) {
106 debug2("extract_salt: salt decode error");
107 return (-1);
108 }
Damien Miller4e8d9372014-02-04 11:02:42 +1100109 if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) {
110 debug2("extract_salt: expected salt len %zd, got %d",
111 ssh_hmac_bytes(SSH_DIGEST_SHA1), ret);
Damien Millere1776152005-03-01 21:47:37 +1100112 return (-1);
113 }
Darren Tucker47eede72005-03-14 23:08:12 +1100114
Damien Millere1776152005-03-01 21:47:37 +1100115 return (0);
116}
117
118char *
119host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
120{
Damien Miller4e8d9372014-02-04 11:02:42 +1100121 struct ssh_hmac_ctx *ctx;
Damien Millerce986542013-07-18 16:12:44 +1000122 u_char salt[256], result[256];
123 char uu_salt[512], uu_result[512];
Damien Millere1776152005-03-01 21:47:37 +1100124 static char encoded[1024];
tedu@openbsd.org10363562016-09-17 18:00:27 +0000125 u_int len;
Damien Millere1776152005-03-01 21:47:37 +1100126
Damien Miller4e8d9372014-02-04 11:02:42 +1100127 len = ssh_digest_bytes(SSH_DIGEST_SHA1);
Damien Millere1776152005-03-01 21:47:37 +1100128
129 if (name_from_hostfile == NULL) {
130 /* Create new salt */
tedu@openbsd.org10363562016-09-17 18:00:27 +0000131 arc4random_buf(salt, len);
Damien Millere1776152005-03-01 21:47:37 +1100132 } else {
133 /* Extract salt from known host entry */
134 if (extract_salt(name_from_hostfile, src_len, salt,
135 sizeof(salt)) == -1)
136 return (NULL);
137 }
138
Damien Miller4e8d9372014-02-04 11:02:42 +1100139 if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL ||
140 ssh_hmac_init(ctx, salt, len) < 0 ||
141 ssh_hmac_update(ctx, host, strlen(host)) < 0 ||
142 ssh_hmac_final(ctx, result, sizeof(result)))
143 fatal("%s: ssh_hmac failed", __func__);
144 ssh_hmac_free(ctx);
Damien Millere1776152005-03-01 21:47:37 +1100145
Darren Tucker47eede72005-03-14 23:08:12 +1100146 if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
Damien Millere1776152005-03-01 21:47:37 +1100147 __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1)
Damien Miller4e8d9372014-02-04 11:02:42 +1100148 fatal("%s: __b64_ntop failed", __func__);
Damien Millere1776152005-03-01 21:47:37 +1100149
150 snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
151 HASH_DELIM, uu_result);
152
153 return (encoded);
154}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller5428f641999-11-25 11:54:57 +1100156/*
Damien Miller450a7a12000-03-26 13:04:51 +1000157 * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the
158 * pointer over the key. Skips any whitespace at the beginning and at end.
Damien Miller5428f641999-11-25 11:54:57 +1100159 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller5b2aea92001-12-21 12:47:09 +1100161int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000162hostfile_read_key(char **cpp, u_int *bitsp, struct sshkey *ret)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163{
Damien Miller95def091999-11-25 00:26:21 +1100164 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
Damien Miller95def091999-11-25 00:26:21 +1100166 /* Skip leading whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100167 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
168 ;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
dtucker@openbsd.org696fb422019-07-07 01:05:00 +0000170 if (sshkey_read(ret, &cp) != 0)
Damien Miller95def091999-11-25 00:26:21 +1100171 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
Damien Miller95def091999-11-25 00:26:21 +1100173 /* Skip trailing whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100174 for (; *cp == ' ' || *cp == '\t'; cp++)
175 ;
Damien Miller95def091999-11-25 00:26:21 +1100176
177 /* Return results. */
178 *cpp = cp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000179 if (bitsp != NULL)
180 *bitsp = sshkey_size(ret);
Damien Miller95def091999-11-25 00:26:21 +1100181 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182}
183
Damien Millerd925dcd2010-12-01 12:21:51 +1100184static HostkeyMarker
Damien Miller1aed65e2010-03-04 21:53:35 +1100185check_markers(char **cpp)
186{
187 char marker[32], *sp, *cp = *cpp;
188 int ret = MRK_NONE;
189
190 while (*cp == '@') {
191 /* Only one marker is allowed */
192 if (ret != MRK_NONE)
193 return MRK_ERROR;
194 /* Markers are terminated by whitespace */
195 if ((sp = strchr(cp, ' ')) == NULL &&
196 (sp = strchr(cp, '\t')) == NULL)
197 return MRK_ERROR;
198 /* Extract marker for comparison */
199 if (sp <= cp + 1 || sp >= cp + sizeof(marker))
200 return MRK_ERROR;
201 memcpy(marker, cp, sp - cp);
202 marker[sp - cp] = '\0';
203 if (strcmp(marker, CA_MARKER) == 0)
204 ret = MRK_CA;
205 else if (strcmp(marker, REVOKE_MARKER) == 0)
206 ret = MRK_REVOKE;
207 else
208 return MRK_ERROR;
209
210 /* Skip past marker and any whitespace that follows it */
211 cp = sp;
212 for (; *cp == ' ' || *cp == '\t'; cp++)
213 ;
214 }
215 *cpp = cp;
216 return ret;
217}
218
Damien Millerd925dcd2010-12-01 12:21:51 +1100219struct hostkeys *
220init_hostkeys(void)
221{
222 struct hostkeys *ret = xcalloc(1, sizeof(*ret));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
Damien Millerd925dcd2010-12-01 12:21:51 +1100224 ret->entries = NULL;
225 return ret;
226}
227
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000228struct load_callback_ctx {
229 const char *host;
230 u_long num_loaded;
231 struct hostkeys *hostkeys;
232};
233
234static int
235record_hostkey(struct hostkey_foreach_line *l, void *_ctx)
236{
237 struct load_callback_ctx *ctx = (struct load_callback_ctx *)_ctx;
238 struct hostkeys *hostkeys = ctx->hostkeys;
239 struct hostkey_entry *tmp;
240
241 if (l->status == HKF_STATUS_INVALID) {
djm@openbsd.org398f9ef2015-03-31 22:57:06 +0000242 /* XXX make this verbose() in the future */
243 debug("%s:%ld: parse error in hostkeys file",
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000244 l->path, l->linenum);
245 return 0;
246 }
247
248 debug3("%s: found %skey type %s in file %s:%lu", __func__,
249 l->marker == MRK_NONE ? "" :
250 (l->marker == MRK_CA ? "ca " : "revoked "),
251 sshkey_type(l->key), l->path, l->linenum);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +0000252 if ((tmp = recallocarray(hostkeys->entries, hostkeys->num_entries,
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000253 hostkeys->num_entries + 1, sizeof(*hostkeys->entries))) == NULL)
254 return SSH_ERR_ALLOC_FAIL;
255 hostkeys->entries = tmp;
256 hostkeys->entries[hostkeys->num_entries].host = xstrdup(ctx->host);
257 hostkeys->entries[hostkeys->num_entries].file = xstrdup(l->path);
258 hostkeys->entries[hostkeys->num_entries].line = l->linenum;
259 hostkeys->entries[hostkeys->num_entries].key = l->key;
260 l->key = NULL; /* steal it */
261 hostkeys->entries[hostkeys->num_entries].marker = l->marker;
262 hostkeys->num_entries++;
263 ctx->num_loaded++;
264
265 return 0;
266}
267
Damien Millerd925dcd2010-12-01 12:21:51 +1100268void
269load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270{
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000271 int r;
272 struct load_callback_ctx ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000274 ctx.host = host;
275 ctx.num_loaded = 0;
276 ctx.hostkeys = hostkeys;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000278 if ((r = hostkeys_foreach(path, record_hostkey, &ctx, host, NULL,
279 HKF_WANT_MATCH|HKF_WANT_PARSE_KEY)) != 0) {
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000280 if (r != SSH_ERR_SYSTEM_ERROR && errno != ENOENT)
281 debug("%s: hostkeys_foreach failed for %s: %s",
282 __func__, path, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283 }
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000284 if (ctx.num_loaded != 0)
285 debug3("%s: loaded %lu keys from %s", __func__,
286 ctx.num_loaded, host);
djm@openbsd.org6fdcaeb2014-10-20 03:43:01 +0000287}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288
Damien Millerd925dcd2010-12-01 12:21:51 +1100289void
290free_hostkeys(struct hostkeys *hostkeys)
291{
292 u_int i;
293
294 for (i = 0; i < hostkeys->num_entries; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000295 free(hostkeys->entries[i].host);
296 free(hostkeys->entries[i].file);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000297 sshkey_free(hostkeys->entries[i].key);
Damien Miller1d2c4562014-02-04 11:18:20 +1100298 explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
Damien Millerd925dcd2010-12-01 12:21:51 +1100299 }
Darren Tuckera627d422013-06-02 07:31:17 +1000300 free(hostkeys->entries);
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +0000301 freezero(hostkeys, sizeof(*hostkeys));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302}
303
Damien Millerd925dcd2010-12-01 12:21:51 +1100304static int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000305check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k)
Damien Millerd925dcd2010-12-01 12:21:51 +1100306{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000307 int is_cert = sshkey_is_cert(k);
Damien Millerd925dcd2010-12-01 12:21:51 +1100308 u_int i;
309
310 for (i = 0; i < hostkeys->num_entries; i++) {
311 if (hostkeys->entries[i].marker != MRK_REVOKE)
312 continue;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000313 if (sshkey_equal_public(k, hostkeys->entries[i].key))
Damien Millerd925dcd2010-12-01 12:21:51 +1100314 return -1;
markus@openbsd.orgfbff6052020-03-06 18:25:12 +0000315 if (is_cert && k != NULL &&
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000316 sshkey_equal_public(k->cert->signature_key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100317 hostkeys->entries[i].key))
318 return -1;
319 }
320 return 0;
321}
322
323/*
324 * Match keys against a specified key, or look one up by key type.
325 *
326 * If looking for a keytype (key == NULL) and one is found then return
327 * HOST_FOUND, otherwise HOST_NEW.
328 *
329 * If looking for a key (key != NULL):
330 * 1. If the key is a cert and a matching CA is found, return HOST_OK
331 * 2. If the key is not a cert and a matching key is found, return HOST_OK
332 * 3. If no key matches but a key with a different type is found, then
333 * return HOST_CHANGED
334 * 4. If no matching keys are found, then return HOST_NEW.
335 *
336 * Finally, check any found key is not revoked.
337 */
338static HostStatus
339check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000340 struct sshkey *k, int keytype, const struct hostkey_entry **found)
Damien Millerd925dcd2010-12-01 12:21:51 +1100341{
342 u_int i;
343 HostStatus end_return = HOST_NEW;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000344 int want_cert = sshkey_is_cert(k);
Damien Millerd925dcd2010-12-01 12:21:51 +1100345 HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
Damien Millerd925dcd2010-12-01 12:21:51 +1100346
347 if (found != NULL)
348 *found = NULL;
349
350 for (i = 0; i < hostkeys->num_entries; i++) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100351 if (hostkeys->entries[i].marker != want_marker)
352 continue;
353 if (k == NULL) {
354 if (hostkeys->entries[i].key->type != keytype)
355 continue;
356 end_return = HOST_FOUND;
357 if (found != NULL)
358 *found = hostkeys->entries + i;
359 k = hostkeys->entries[i].key;
360 break;
361 }
362 if (want_cert) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000363 if (sshkey_equal_public(k->cert->signature_key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100364 hostkeys->entries[i].key)) {
365 /* A matching CA exists */
366 end_return = HOST_OK;
367 if (found != NULL)
368 *found = hostkeys->entries + i;
369 break;
370 }
371 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000372 if (sshkey_equal(k, hostkeys->entries[i].key)) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100373 end_return = HOST_OK;
374 if (found != NULL)
375 *found = hostkeys->entries + i;
376 break;
377 }
378 /* A non-maching key exists */
379 end_return = HOST_CHANGED;
380 if (found != NULL)
381 *found = hostkeys->entries + i;
382 }
383 }
384 if (check_key_not_revoked(hostkeys, k) != 0) {
385 end_return = HOST_REVOKED;
386 if (found != NULL)
387 *found = NULL;
388 }
389 return end_return;
390}
djm@openbsd.org6fdcaeb2014-10-20 03:43:01 +0000391
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000392HostStatus
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000393check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100394 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000395{
396 if (key == NULL)
397 fatal("no key to look up");
Damien Millerd925dcd2010-12-01 12:21:51 +1100398 return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000399}
400
401int
Damien Millerd925dcd2010-12-01 12:21:51 +1100402lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
403 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000404{
Damien Millerd925dcd2010-12-01 12:21:51 +1100405 return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
406 found) == HOST_FOUND);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000407}
408
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000409static int
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000410write_host_entry(FILE *f, const char *host, const char *ip,
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000411 const struct sshkey *key, int store_hash)
412{
413 int r, success = 0;
djm@openbsd.orgdb259722017-03-10 04:26:06 +0000414 char *hashed_host = NULL, *lhost;
415
416 lhost = xstrdup(host);
417 lowercase(lhost);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000418
419 if (store_hash) {
djm@openbsd.orgdb259722017-03-10 04:26:06 +0000420 if ((hashed_host = host_hash(lhost, NULL, 0)) == NULL) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000421 error("%s: host_hash failed", __func__);
djm@openbsd.orgdb259722017-03-10 04:26:06 +0000422 free(lhost);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000423 return 0;
424 }
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000425 fprintf(f, "%s ", hashed_host);
426 } else if (ip != NULL)
djm@openbsd.orgdb259722017-03-10 04:26:06 +0000427 fprintf(f, "%s,%s ", lhost, ip);
428 else {
429 fprintf(f, "%s ", lhost);
430 }
431 free(lhost);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000432 if ((r = sshkey_write(key, f)) == 0)
433 success = 1;
434 else
435 error("%s: sshkey_write failed: %s", __func__, ssh_err(r));
436 fputc('\n', f);
437 return success;
438}
439
Damien Miller5428f641999-11-25 11:54:57 +1100440/*
441 * Appends an entry to the host file. Returns false if the entry could not
442 * be appended.
443 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000445add_host_to_hostfile(const char *filename, const char *host,
446 const struct sshkey *key, int store_hash)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447{
Damien Miller95def091999-11-25 00:26:21 +1100448 FILE *f;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000449 int success;
Damien Millere1776152005-03-01 21:47:37 +1100450
Damien Miller450a7a12000-03-26 13:04:51 +1000451 if (key == NULL)
Damien Millereba71ba2000-04-29 23:57:08 +1000452 return 1; /* XXX ? */
Damien Miller95def091999-11-25 00:26:21 +1100453 f = fopen(filename, "a");
454 if (!f)
455 return 0;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000456 success = write_host_entry(f, host, NULL, key, store_hash);
Damien Miller95def091999-11-25 00:26:21 +1100457 fclose(f);
Damien Miller450a7a12000-03-26 13:04:51 +1000458 return success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459}
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000460
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000461struct host_delete_ctx {
462 FILE *out;
463 int quiet;
464 const char *host;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000465 int *skip_keys; /* XXX split for host/ip? might want to ensure both */
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000466 struct sshkey * const *keys;
467 size_t nkeys;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000468 int modified;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000469};
470
471static int
472host_delete(struct hostkey_foreach_line *l, void *_ctx)
473{
474 struct host_delete_ctx *ctx = (struct host_delete_ctx *)_ctx;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000475 int loglevel = ctx->quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_VERBOSE;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000476 size_t i;
477
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000478 if (l->status == HKF_STATUS_MATCHED) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000479 if (l->marker != MRK_NONE) {
480 /* Don't remove CA and revocation lines */
481 fprintf(ctx->out, "%s\n", l->line);
482 return 0;
483 }
484
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000485 /*
486 * If this line contains one of the keys that we will be
487 * adding later, then don't change it and mark the key for
488 * skipping.
489 */
490 for (i = 0; i < ctx->nkeys; i++) {
491 if (sshkey_equal(ctx->keys[i], l->key)) {
492 ctx->skip_keys[i] = 1;
493 fprintf(ctx->out, "%s\n", l->line);
494 debug3("%s: %s key already at %s:%ld", __func__,
495 sshkey_type(l->key), l->path, l->linenum);
496 return 0;
497 }
498 }
499
500 /*
501 * Hostname matches and has no CA/revoke marker, delete it
502 * by *not* writing the line to ctx->out.
503 */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000504 do_log2(loglevel, "%s%s%s:%ld: Removed %s key for host %s",
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000505 ctx->quiet ? __func__ : "", ctx->quiet ? ": " : "",
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000506 l->path, l->linenum, sshkey_type(l->key), ctx->host);
507 ctx->modified = 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000508 return 0;
509 }
510 /* Retain non-matching hosts and invalid lines when deleting */
511 if (l->status == HKF_STATUS_INVALID) {
512 do_log2(loglevel, "%s%s%s:%ld: invalid known_hosts entry",
513 ctx->quiet ? __func__ : "", ctx->quiet ? ": " : "",
514 l->path, l->linenum);
515 }
516 fprintf(ctx->out, "%s\n", l->line);
517 return 0;
518}
519
520int
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000521hostfile_replace_entries(const char *filename, const char *host, const char *ip,
522 struct sshkey **keys, size_t nkeys, int store_hash, int quiet, int hash_alg)
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000523{
524 int r, fd, oerrno = 0;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000525 int loglevel = quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_VERBOSE;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000526 struct host_delete_ctx ctx;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000527 char *fp, *temp = NULL, *back = NULL;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000528 mode_t omask;
529 size_t i;
530
djm@openbsd.org3076ee72015-01-26 13:36:53 +0000531 omask = umask(077);
532
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000533 memset(&ctx, 0, sizeof(ctx));
534 ctx.host = host;
535 ctx.quiet = quiet;
536 if ((ctx.skip_keys = calloc(nkeys, sizeof(*ctx.skip_keys))) == NULL)
537 return SSH_ERR_ALLOC_FAIL;
538 ctx.keys = keys;
539 ctx.nkeys = nkeys;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000540 ctx.modified = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000541
542 /*
543 * Prepare temporary file for in-place deletion.
544 */
deraadt@openbsd.org1b2d55d2019-06-28 01:23:50 +0000545 if ((r = asprintf(&temp, "%s.XXXXXXXXXXX", filename)) == -1 ||
deraadt@openbsd.org5cdbaa72019-06-27 18:03:37 +0000546 (r = asprintf(&back, "%s.old", filename)) == -1) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000547 r = SSH_ERR_ALLOC_FAIL;
548 goto fail;
549 }
550
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000551 if ((fd = mkstemp(temp)) == -1) {
552 oerrno = errno;
553 error("%s: mkstemp: %s", __func__, strerror(oerrno));
554 r = SSH_ERR_SYSTEM_ERROR;
555 goto fail;
556 }
557 if ((ctx.out = fdopen(fd, "w")) == NULL) {
558 oerrno = errno;
559 close(fd);
560 error("%s: fdopen: %s", __func__, strerror(oerrno));
561 r = SSH_ERR_SYSTEM_ERROR;
562 goto fail;
563 }
564
565 /* Remove all entries for the specified host from the file */
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000566 if ((r = hostkeys_foreach(filename, host_delete, &ctx, host, ip,
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000567 HKF_WANT_PARSE_KEY)) != 0) {
djm@openbsd.org79556332020-01-25 00:21:08 +0000568 oerrno = errno;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000569 error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
570 goto fail;
571 }
572
573 /* Add the requested keys */
574 for (i = 0; i < nkeys; i++) {
575 if (ctx.skip_keys[i])
576 continue;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000577 if ((fp = sshkey_fingerprint(keys[i], hash_alg,
578 SSH_FP_DEFAULT)) == NULL) {
579 r = SSH_ERR_ALLOC_FAIL;
580 goto fail;
581 }
582 do_log2(loglevel, "%s%sAdding new key for %s to %s: %s %s",
583 quiet ? __func__ : "", quiet ? ": " : "", host, filename,
584 sshkey_ssh_name(keys[i]), fp);
585 free(fp);
586 if (!write_host_entry(ctx.out, host, ip, keys[i], store_hash)) {
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000587 r = SSH_ERR_INTERNAL_ERROR;
588 goto fail;
589 }
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000590 ctx.modified = 1;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000591 }
592 fclose(ctx.out);
593 ctx.out = NULL;
594
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000595 if (ctx.modified) {
596 /* Backup the original file and replace it with the temporary */
597 if (unlink(back) == -1 && errno != ENOENT) {
598 oerrno = errno;
599 error("%s: unlink %.100s: %s", __func__,
600 back, strerror(errno));
601 r = SSH_ERR_SYSTEM_ERROR;
602 goto fail;
603 }
604 if (link(filename, back) == -1) {
605 oerrno = errno;
606 error("%s: link %.100s to %.100s: %s", __func__,
607 filename, back, strerror(errno));
608 r = SSH_ERR_SYSTEM_ERROR;
609 goto fail;
610 }
611 if (rename(temp, filename) == -1) {
612 oerrno = errno;
613 error("%s: rename \"%s\" to \"%s\": %s", __func__,
614 temp, filename, strerror(errno));
615 r = SSH_ERR_SYSTEM_ERROR;
616 goto fail;
617 }
618 } else {
619 /* No changes made; just delete the temporary file */
620 if (unlink(temp) != 0)
621 error("%s: unlink \"%s\": %s", __func__,
622 temp, strerror(errno));
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000623 }
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000624
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000625 /* success */
626 r = 0;
627 fail:
628 if (temp != NULL && r != 0)
629 unlink(temp);
630 free(temp);
631 free(back);
632 if (ctx.out != NULL)
633 fclose(ctx.out);
634 free(ctx.skip_keys);
djm@openbsd.org3076ee72015-01-26 13:36:53 +0000635 umask(omask);
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000636 if (r == SSH_ERR_SYSTEM_ERROR)
637 errno = oerrno;
638 return r;
639}
640
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000641static int
642match_maybe_hashed(const char *host, const char *names, int *was_hashed)
643{
644 int hashed = *names == HASH_DELIM;
645 const char *hashed_host;
646 size_t nlen = strlen(names);
647
648 if (was_hashed != NULL)
649 *was_hashed = hashed;
650 if (hashed) {
651 if ((hashed_host = host_hash(host, names, nlen)) == NULL)
652 return -1;
653 return nlen == strlen(hashed_host) &&
654 strncmp(hashed_host, names, nlen) == 0;
655 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000656 return match_hostname(host, names) == 1;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000657}
658
659int
660hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000661 const char *host, const char *ip, u_int options)
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000662{
663 FILE *f;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000664 char *line = NULL, ktype[128];
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000665 u_long linenum = 0;
666 char *cp, *cp2;
667 u_int kbits;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000668 int hashed;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000669 int s, r = 0;
670 struct hostkey_foreach_line lineinfo;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000671 size_t linesize = 0, l;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000672
673 memset(&lineinfo, 0, sizeof(lineinfo));
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000674 if (host == NULL && (options & HKF_WANT_MATCH) != 0)
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000675 return SSH_ERR_INVALID_ARGUMENT;
676 if ((f = fopen(path, "r")) == NULL)
677 return SSH_ERR_SYSTEM_ERROR;
678
679 debug3("%s: reading file \"%s\"", __func__, path);
markus@openbsd.org7f906352018-06-06 18:29:18 +0000680 while (getline(&line, &linesize, f) != -1) {
681 linenum++;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000682 line[strcspn(line, "\n")] = '\0';
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000683
djm@openbsd.orgde2997a2018-07-16 03:09:13 +0000684 free(lineinfo.line);
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000685 sshkey_free(lineinfo.key);
686 memset(&lineinfo, 0, sizeof(lineinfo));
687 lineinfo.path = path;
688 lineinfo.linenum = linenum;
markus@openbsd.org7f906352018-06-06 18:29:18 +0000689 lineinfo.line = xstrdup(line);
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000690 lineinfo.marker = MRK_NONE;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000691 lineinfo.status = HKF_STATUS_OK;
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000692 lineinfo.keytype = KEY_UNSPEC;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000693
694 /* Skip any leading whitespace, comments and empty lines. */
695 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
696 ;
697 if (!*cp || *cp == '#' || *cp == '\n') {
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000698 if ((options & HKF_WANT_MATCH) == 0) {
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000699 lineinfo.status = HKF_STATUS_COMMENT;
700 if ((r = callback(&lineinfo, ctx)) != 0)
701 break;
702 }
703 continue;
704 }
705
706 if ((lineinfo.marker = check_markers(&cp)) == MRK_ERROR) {
707 verbose("%s: invalid marker at %s:%lu",
708 __func__, path, linenum);
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000709 if ((options & HKF_WANT_MATCH) == 0)
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000710 goto bad;
711 continue;
712 }
713
714 /* Find the end of the host name portion. */
715 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
716 ;
717 lineinfo.hosts = cp;
718 *cp2++ = '\0';
719
720 /* Check if the host name matches. */
721 if (host != NULL) {
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000722 if ((s = match_maybe_hashed(host, lineinfo.hosts,
723 &hashed)) == -1) {
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000724 debug2("%s: %s:%ld: bad host hash \"%.32s\"",
725 __func__, path, linenum, lineinfo.hosts);
726 goto bad;
727 }
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000728 if (s == 1) {
729 lineinfo.status = HKF_STATUS_MATCHED;
730 lineinfo.match |= HKF_MATCH_HOST |
731 (hashed ? HKF_MATCH_HOST_HASHED : 0);
732 }
733 /* Try matching IP address if supplied */
734 if (ip != NULL) {
735 if ((s = match_maybe_hashed(ip, lineinfo.hosts,
736 &hashed)) == -1) {
737 debug2("%s: %s:%ld: bad ip hash "
738 "\"%.32s\"", __func__, path,
739 linenum, lineinfo.hosts);
740 goto bad;
741 }
742 if (s == 1) {
743 lineinfo.status = HKF_STATUS_MATCHED;
744 lineinfo.match |= HKF_MATCH_IP |
745 (hashed ? HKF_MATCH_IP_HASHED : 0);
746 }
747 }
748 /*
749 * Skip this line if host matching requested and
750 * neither host nor address matched.
751 */
752 if ((options & HKF_WANT_MATCH) != 0 &&
753 lineinfo.status != HKF_STATUS_MATCHED)
754 continue;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000755 }
756
757 /* Got a match. Skip host name and any following whitespace */
758 for (; *cp2 == ' ' || *cp2 == '\t'; cp2++)
759 ;
760 if (*cp2 == '\0' || *cp2 == '#') {
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000761 debug2("%s:%ld: truncated before key type",
762 path, linenum);
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000763 goto bad;
764 }
765 lineinfo.rawkey = cp = cp2;
766
767 if ((options & HKF_WANT_PARSE_KEY) != 0) {
768 /*
769 * Extract the key from the line. This will skip
770 * any leading whitespace. Ignore badly formatted
771 * lines.
772 */
773 if ((lineinfo.key = sshkey_new(KEY_UNSPEC)) == NULL) {
774 error("%s: sshkey_new failed", __func__);
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000775 r = SSH_ERR_ALLOC_FAIL;
776 break;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000777 }
778 if (!hostfile_read_key(&cp, &kbits, lineinfo.key)) {
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000779 goto bad;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000780 }
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000781 lineinfo.keytype = lineinfo.key->type;
782 lineinfo.comment = cp;
783 } else {
784 /* Extract and parse key type */
785 l = strcspn(lineinfo.rawkey, " \t");
786 if (l <= 1 || l >= sizeof(ktype) ||
787 lineinfo.rawkey[l] == '\0')
788 goto bad;
789 memcpy(ktype, lineinfo.rawkey, l);
790 ktype[l] = '\0';
791 lineinfo.keytype = sshkey_type_from_name(ktype);
djm@openbsd.org398f9ef2015-03-31 22:57:06 +0000792
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000793 /*
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000794 * Assume legacy RSA1 if the first component is a short
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000795 * decimal number.
796 */
797 if (lineinfo.keytype == KEY_UNSPEC && l < 8 &&
798 strspn(ktype, "0123456789") == l)
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000799 goto bad;
djm@openbsd.org398f9ef2015-03-31 22:57:06 +0000800
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000801 /*
802 * Check that something other than whitespace follows
803 * the key type. This won't catch all corruption, but
804 * it does catch trivial truncation.
805 */
806 cp2 += l; /* Skip past key type */
807 for (; *cp2 == ' ' || *cp2 == '\t'; cp2++)
808 ;
809 if (*cp2 == '\0' || *cp2 == '#') {
810 debug2("%s:%ld: truncated after key type",
811 path, linenum);
812 lineinfo.keytype = KEY_UNSPEC;
813 }
814 if (lineinfo.keytype == KEY_UNSPEC) {
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000815 bad:
djm@openbsd.org6c5c9492015-02-16 22:08:57 +0000816 sshkey_free(lineinfo.key);
817 lineinfo.key = NULL;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000818 lineinfo.status = HKF_STATUS_INVALID;
819 if ((r = callback(&lineinfo, ctx)) != 0)
820 break;
821 continue;
822 }
823 }
824 if ((r = callback(&lineinfo, ctx)) != 0)
825 break;
826 }
827 sshkey_free(lineinfo.key);
markus@openbsd.org7f906352018-06-06 18:29:18 +0000828 free(lineinfo.line);
829 free(line);
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000830 fclose(f);
831 return r;
832}