blob: ccb2af9200c49d59f3726f778954335bad6814fa [file] [log] [blame]
djm@openbsd.orgec3d0652015-01-18 21:48:09 +00001/* $OpenBSD: hostfile.c,v 1.61 2015/01/18 21:48:09 djm 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>
42
43#include <netinet/in.h>
44
djm@openbsd.orgc29811c2015-01-18 21:40:23 +000045#include <errno.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100046#include <resolv.h>
Damien Millerded319c2006-09-01 15:38:36 +100047#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100048#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100049#include <stdlib.h>
50#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100051#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100052
Damien Millerd7834352006-08-05 12:39:39 +100053#include "xmalloc.h"
Damien Miller450a7a12000-03-26 13:04:51 +100054#include "match.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000055#include "sshkey.h"
Damien Miller450a7a12000-03-26 13:04:51 +100056#include "hostfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "log.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110058#include "misc.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000059#include "ssherr.h"
Damien Millerb3051d02014-01-10 10:58:53 +110060#include "digest.h"
Damien Miller4e8d9372014-02-04 11:02:42 +110061#include "hmac.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110062
63struct hostkeys {
64 struct hostkey_entry *entries;
65 u_int num_entries;
66};
Damien Millere1776152005-03-01 21:47:37 +110067
djm@openbsd.orgc29811c2015-01-18 21:40:23 +000068/* XXX hmac is too easy to dictionary attack; use bcrypt? */
69
Damien Millere1776152005-03-01 21:47:37 +110070static int
Damien Millerce986542013-07-18 16:12:44 +100071extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len)
Damien Millere1776152005-03-01 21:47:37 +110072{
73 char *p, *b64salt;
74 u_int b64len;
75 int ret;
76
77 if (l < sizeof(HASH_MAGIC) - 1) {
78 debug2("extract_salt: string too short");
79 return (-1);
80 }
81 if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
82 debug2("extract_salt: invalid magic identifier");
83 return (-1);
84 }
85 s += sizeof(HASH_MAGIC) - 1;
86 l -= sizeof(HASH_MAGIC) - 1;
87 if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
88 debug2("extract_salt: missing salt termination character");
89 return (-1);
90 }
91
92 b64len = p - s;
93 /* Sanity check */
94 if (b64len == 0 || b64len > 1024) {
95 debug2("extract_salt: bad encoded salt length %u", b64len);
96 return (-1);
97 }
98 b64salt = xmalloc(1 + b64len);
99 memcpy(b64salt, s, b64len);
100 b64salt[b64len] = '\0';
101
102 ret = __b64_pton(b64salt, salt, salt_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000103 free(b64salt);
Damien Millere1776152005-03-01 21:47:37 +1100104 if (ret == -1) {
105 debug2("extract_salt: salt decode error");
106 return (-1);
107 }
Damien Miller4e8d9372014-02-04 11:02:42 +1100108 if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) {
109 debug2("extract_salt: expected salt len %zd, got %d",
110 ssh_hmac_bytes(SSH_DIGEST_SHA1), ret);
Damien Millere1776152005-03-01 21:47:37 +1100111 return (-1);
112 }
Darren Tucker47eede72005-03-14 23:08:12 +1100113
Damien Millere1776152005-03-01 21:47:37 +1100114 return (0);
115}
116
117char *
118host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
119{
Damien Miller4e8d9372014-02-04 11:02:42 +1100120 struct ssh_hmac_ctx *ctx;
Damien Millerce986542013-07-18 16:12:44 +1000121 u_char salt[256], result[256];
122 char uu_salt[512], uu_result[512];
Damien Millere1776152005-03-01 21:47:37 +1100123 static char encoded[1024];
124 u_int i, len;
125
Damien Miller4e8d9372014-02-04 11:02:42 +1100126 len = ssh_digest_bytes(SSH_DIGEST_SHA1);
Damien Millere1776152005-03-01 21:47:37 +1100127
128 if (name_from_hostfile == NULL) {
129 /* Create new salt */
130 for (i = 0; i < len; i++)
131 salt[i] = arc4random();
132 } 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;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000165 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166
Damien Miller95def091999-11-25 00:26:21 +1100167 /* Skip leading whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100168 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
169 ;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000171 if ((r = sshkey_read(ret, &cp)) != 0)
Damien Miller95def091999-11-25 00:26:21 +1100172 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173
Damien Miller95def091999-11-25 00:26:21 +1100174 /* Skip trailing whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100175 for (; *cp == ' ' || *cp == '\t'; cp++)
176 ;
Damien Miller95def091999-11-25 00:26:21 +1100177
178 /* Return results. */
179 *cpp = cp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000180 if (bitsp != NULL)
181 *bitsp = sshkey_size(ret);
Damien Miller95def091999-11-25 00:26:21 +1100182 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183}
184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000186hostfile_check_key(int bits, const struct sshkey *key, const char *host,
Damien Millerd925dcd2010-12-01 12:21:51 +1100187 const char *filename, u_long linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188{
Damien Miller1f0311c2014-05-15 14:24:09 +1000189#ifdef WITH_SSH1
Damien Miller0bc1bd82000-11-13 22:57:25 +1100190 if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
Damien Miller450a7a12000-03-26 13:04:51 +1000191 return 1;
192 if (bits != BN_num_bits(key->rsa->n)) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100193 logit("Warning: %s, line %lu: keysize mismatch for host %s: "
Damien Miller450a7a12000-03-26 13:04:51 +1000194 "actual %d vs. announced %d.",
195 filename, linenum, host, BN_num_bits(key->rsa->n), bits);
Damien Millerd925dcd2010-12-01 12:21:51 +1100196 logit("Warning: replace %d with %d in %s, line %lu.",
Damien Miller450a7a12000-03-26 13:04:51 +1000197 bits, BN_num_bits(key->rsa->n), filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000199#endif
Damien Miller450a7a12000-03-26 13:04:51 +1000200 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201}
202
Damien Millerd925dcd2010-12-01 12:21:51 +1100203static HostkeyMarker
Damien Miller1aed65e2010-03-04 21:53:35 +1100204check_markers(char **cpp)
205{
206 char marker[32], *sp, *cp = *cpp;
207 int ret = MRK_NONE;
208
209 while (*cp == '@') {
210 /* Only one marker is allowed */
211 if (ret != MRK_NONE)
212 return MRK_ERROR;
213 /* Markers are terminated by whitespace */
214 if ((sp = strchr(cp, ' ')) == NULL &&
215 (sp = strchr(cp, '\t')) == NULL)
216 return MRK_ERROR;
217 /* Extract marker for comparison */
218 if (sp <= cp + 1 || sp >= cp + sizeof(marker))
219 return MRK_ERROR;
220 memcpy(marker, cp, sp - cp);
221 marker[sp - cp] = '\0';
222 if (strcmp(marker, CA_MARKER) == 0)
223 ret = MRK_CA;
224 else if (strcmp(marker, REVOKE_MARKER) == 0)
225 ret = MRK_REVOKE;
226 else
227 return MRK_ERROR;
228
229 /* Skip past marker and any whitespace that follows it */
230 cp = sp;
231 for (; *cp == ' ' || *cp == '\t'; cp++)
232 ;
233 }
234 *cpp = cp;
235 return ret;
236}
237
Damien Millerd925dcd2010-12-01 12:21:51 +1100238struct hostkeys *
239init_hostkeys(void)
240{
241 struct hostkeys *ret = xcalloc(1, sizeof(*ret));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Millerd925dcd2010-12-01 12:21:51 +1100243 ret->entries = NULL;
244 return ret;
245}
246
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000247struct load_callback_ctx {
248 const char *host;
249 u_long num_loaded;
250 struct hostkeys *hostkeys;
251};
252
253static int
254record_hostkey(struct hostkey_foreach_line *l, void *_ctx)
255{
256 struct load_callback_ctx *ctx = (struct load_callback_ctx *)_ctx;
257 struct hostkeys *hostkeys = ctx->hostkeys;
258 struct hostkey_entry *tmp;
259
260 if (l->status == HKF_STATUS_INVALID) {
261 error("%s:%ld: parse error in hostkeys file",
262 l->path, l->linenum);
263 return 0;
264 }
265
266 debug3("%s: found %skey type %s in file %s:%lu", __func__,
267 l->marker == MRK_NONE ? "" :
268 (l->marker == MRK_CA ? "ca " : "revoked "),
269 sshkey_type(l->key), l->path, l->linenum);
270 if ((tmp = reallocarray(hostkeys->entries,
271 hostkeys->num_entries + 1, sizeof(*hostkeys->entries))) == NULL)
272 return SSH_ERR_ALLOC_FAIL;
273 hostkeys->entries = tmp;
274 hostkeys->entries[hostkeys->num_entries].host = xstrdup(ctx->host);
275 hostkeys->entries[hostkeys->num_entries].file = xstrdup(l->path);
276 hostkeys->entries[hostkeys->num_entries].line = l->linenum;
277 hostkeys->entries[hostkeys->num_entries].key = l->key;
278 l->key = NULL; /* steal it */
279 hostkeys->entries[hostkeys->num_entries].marker = l->marker;
280 hostkeys->num_entries++;
281 ctx->num_loaded++;
282
283 return 0;
284}
285
Damien Millerd925dcd2010-12-01 12:21:51 +1100286void
287load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288{
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000289 int r;
290 struct load_callback_ctx ctx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000292 ctx.host = host;
293 ctx.num_loaded = 0;
294 ctx.hostkeys = hostkeys;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000296 if ((r = hostkeys_foreach(path, record_hostkey, &ctx, host,
297 HKF_WANT_MATCH_HOST|HKF_WANT_PARSE_KEY)) != 0) {
298 if (r != SSH_ERR_SYSTEM_ERROR && errno != ENOENT)
299 debug("%s: hostkeys_foreach failed for %s: %s",
300 __func__, path, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301 }
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000302 if (ctx.num_loaded != 0)
303 debug3("%s: loaded %lu keys from %s", __func__,
304 ctx.num_loaded, host);
djm@openbsd.org6fdcaeb2014-10-20 03:43:01 +0000305}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306
Damien Millerd925dcd2010-12-01 12:21:51 +1100307void
308free_hostkeys(struct hostkeys *hostkeys)
309{
310 u_int i;
311
312 for (i = 0; i < hostkeys->num_entries; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000313 free(hostkeys->entries[i].host);
314 free(hostkeys->entries[i].file);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000315 sshkey_free(hostkeys->entries[i].key);
Damien Miller1d2c4562014-02-04 11:18:20 +1100316 explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
Damien Millerd925dcd2010-12-01 12:21:51 +1100317 }
Darren Tuckera627d422013-06-02 07:31:17 +1000318 free(hostkeys->entries);
Damien Miller1d2c4562014-02-04 11:18:20 +1100319 explicit_bzero(hostkeys, sizeof(*hostkeys));
Darren Tuckera627d422013-06-02 07:31:17 +1000320 free(hostkeys);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321}
322
Damien Millerd925dcd2010-12-01 12:21:51 +1100323static int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000324check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k)
Damien Millerd925dcd2010-12-01 12:21:51 +1100325{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000326 int is_cert = sshkey_is_cert(k);
Damien Millerd925dcd2010-12-01 12:21:51 +1100327 u_int i;
328
329 for (i = 0; i < hostkeys->num_entries; i++) {
330 if (hostkeys->entries[i].marker != MRK_REVOKE)
331 continue;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000332 if (sshkey_equal_public(k, hostkeys->entries[i].key))
Damien Millerd925dcd2010-12-01 12:21:51 +1100333 return -1;
334 if (is_cert &&
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000335 sshkey_equal_public(k->cert->signature_key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100336 hostkeys->entries[i].key))
337 return -1;
338 }
339 return 0;
340}
341
342/*
343 * Match keys against a specified key, or look one up by key type.
344 *
345 * If looking for a keytype (key == NULL) and one is found then return
346 * HOST_FOUND, otherwise HOST_NEW.
347 *
348 * If looking for a key (key != NULL):
349 * 1. If the key is a cert and a matching CA is found, return HOST_OK
350 * 2. If the key is not a cert and a matching key is found, return HOST_OK
351 * 3. If no key matches but a key with a different type is found, then
352 * return HOST_CHANGED
353 * 4. If no matching keys are found, then return HOST_NEW.
354 *
355 * Finally, check any found key is not revoked.
356 */
357static HostStatus
358check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000359 struct sshkey *k, int keytype, const struct hostkey_entry **found)
Damien Millerd925dcd2010-12-01 12:21:51 +1100360{
361 u_int i;
362 HostStatus end_return = HOST_NEW;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000363 int want_cert = sshkey_is_cert(k);
Damien Millerd925dcd2010-12-01 12:21:51 +1100364 HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
365 int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
366
367 if (found != NULL)
368 *found = NULL;
369
370 for (i = 0; i < hostkeys->num_entries; i++) {
371 if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1)
372 continue;
373 if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1)
374 continue;
375 if (hostkeys->entries[i].marker != want_marker)
376 continue;
377 if (k == NULL) {
378 if (hostkeys->entries[i].key->type != keytype)
379 continue;
380 end_return = HOST_FOUND;
381 if (found != NULL)
382 *found = hostkeys->entries + i;
383 k = hostkeys->entries[i].key;
384 break;
385 }
386 if (want_cert) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000387 if (sshkey_equal_public(k->cert->signature_key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100388 hostkeys->entries[i].key)) {
389 /* A matching CA exists */
390 end_return = HOST_OK;
391 if (found != NULL)
392 *found = hostkeys->entries + i;
393 break;
394 }
395 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000396 if (sshkey_equal(k, hostkeys->entries[i].key)) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100397 end_return = HOST_OK;
398 if (found != NULL)
399 *found = hostkeys->entries + i;
400 break;
401 }
402 /* A non-maching key exists */
403 end_return = HOST_CHANGED;
404 if (found != NULL)
405 *found = hostkeys->entries + i;
406 }
407 }
408 if (check_key_not_revoked(hostkeys, k) != 0) {
409 end_return = HOST_REVOKED;
410 if (found != NULL)
411 *found = NULL;
412 }
413 return end_return;
414}
djm@openbsd.org6fdcaeb2014-10-20 03:43:01 +0000415
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000416HostStatus
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000417check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100418 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000419{
420 if (key == NULL)
421 fatal("no key to look up");
Damien Millerd925dcd2010-12-01 12:21:51 +1100422 return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000423}
424
425int
Damien Millerd925dcd2010-12-01 12:21:51 +1100426lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
427 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000428{
Damien Millerd925dcd2010-12-01 12:21:51 +1100429 return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
430 found) == HOST_FOUND);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000431}
432
Damien Miller5428f641999-11-25 11:54:57 +1100433/*
434 * Appends an entry to the host file. Returns false if the entry could not
435 * be appended.
436 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000438add_host_to_hostfile(const char *filename, const char *host,
439 const struct sshkey *key, int store_hash)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440{
Damien Miller95def091999-11-25 00:26:21 +1100441 FILE *f;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000442 int r, success = 0;
Darren Tucker40858532005-08-02 17:07:07 +1000443 char *hashed_host = NULL;
Damien Millere1776152005-03-01 21:47:37 +1100444
Damien Miller450a7a12000-03-26 13:04:51 +1000445 if (key == NULL)
Damien Millereba71ba2000-04-29 23:57:08 +1000446 return 1; /* XXX ? */
Damien Miller95def091999-11-25 00:26:21 +1100447 f = fopen(filename, "a");
448 if (!f)
449 return 0;
Damien Millere1776152005-03-01 21:47:37 +1100450
451 if (store_hash) {
452 if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
djm@openbsd.orgec3d0652015-01-18 21:48:09 +0000453 error("%s: host_hash failed", __func__);
Damien Millere1776152005-03-01 21:47:37 +1100454 fclose(f);
455 return 0;
456 }
457 }
458 fprintf(f, "%s ", store_hash ? hashed_host : host);
459
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000460 if ((r = sshkey_write(key, f)) != 0) {
461 error("%s: saving key in %s failed: %s",
462 __func__, filename, ssh_err(r));
463 } else
Damien Miller450a7a12000-03-26 13:04:51 +1000464 success = 1;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000465 fputc('\n', f);
Damien Miller95def091999-11-25 00:26:21 +1100466 fclose(f);
Damien Miller450a7a12000-03-26 13:04:51 +1000467 return success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468}
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000469
470static int
471match_maybe_hashed(const char *host, const char *names, int *was_hashed)
472{
473 int hashed = *names == HASH_DELIM;
474 const char *hashed_host;
475 size_t nlen = strlen(names);
476
477 if (was_hashed != NULL)
478 *was_hashed = hashed;
479 if (hashed) {
480 if ((hashed_host = host_hash(host, names, nlen)) == NULL)
481 return -1;
482 return nlen == strlen(hashed_host) &&
483 strncmp(hashed_host, names, nlen) == 0;
484 }
485 return match_hostname(host, names, nlen) == 1;
486}
487
488int
489hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
490 const char *host, u_int options)
491{
492 FILE *f;
493 char line[8192], oline[8192];
494 u_long linenum = 0;
495 char *cp, *cp2;
496 u_int kbits;
497 int s, r = 0;
498 struct hostkey_foreach_line lineinfo;
499
500 memset(&lineinfo, 0, sizeof(lineinfo));
501 if (host == NULL && (options & HKF_WANT_MATCH_HOST) != 0)
502 return SSH_ERR_INVALID_ARGUMENT;
503 if ((f = fopen(path, "r")) == NULL)
504 return SSH_ERR_SYSTEM_ERROR;
505
506 debug3("%s: reading file \"%s\"", __func__, path);
507 while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
508 line[strcspn(line, "\n")] = '\0';
509 strlcpy(oline, line, sizeof(oline));
510
511 sshkey_free(lineinfo.key);
512 memset(&lineinfo, 0, sizeof(lineinfo));
513 lineinfo.path = path;
514 lineinfo.linenum = linenum;
515 lineinfo.line = oline;
516 lineinfo.status = HKF_STATUS_OK;
517
518 /* Skip any leading whitespace, comments and empty lines. */
519 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
520 ;
521 if (!*cp || *cp == '#' || *cp == '\n') {
522 if ((options & HKF_WANT_MATCH_HOST) == 0) {
523 lineinfo.status = HKF_STATUS_COMMENT;
524 if ((r = callback(&lineinfo, ctx)) != 0)
525 break;
526 }
527 continue;
528 }
529
530 if ((lineinfo.marker = check_markers(&cp)) == MRK_ERROR) {
531 verbose("%s: invalid marker at %s:%lu",
532 __func__, path, linenum);
533 if ((options & HKF_WANT_MATCH_HOST) == 0)
534 goto bad;
535 continue;
536 }
537
538 /* Find the end of the host name portion. */
539 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
540 ;
541 lineinfo.hosts = cp;
542 *cp2++ = '\0';
543
544 /* Check if the host name matches. */
545 if (host != NULL) {
546 s = match_maybe_hashed(host, lineinfo.hosts,
547 &lineinfo.was_hashed);
548 if (s == 1)
549 lineinfo.status = HKF_STATUS_HOST_MATCHED;
550 else if ((options & HKF_WANT_MATCH_HOST) != 0)
551 continue;
552 else if (s == -1) {
553 debug2("%s: %s:%ld: bad host hash \"%.32s\"",
554 __func__, path, linenum, lineinfo.hosts);
555 goto bad;
556 }
557 }
558
559 /* Got a match. Skip host name and any following whitespace */
560 for (; *cp2 == ' ' || *cp2 == '\t'; cp2++)
561 ;
562 if (*cp2 == '\0' || *cp2 == '#') {
563 debug2("%s:%ld: truncated before key", path, linenum);
564 goto bad;
565 }
566 lineinfo.rawkey = cp = cp2;
567
568 if ((options & HKF_WANT_PARSE_KEY) != 0) {
569 /*
570 * Extract the key from the line. This will skip
571 * any leading whitespace. Ignore badly formatted
572 * lines.
573 */
574 if ((lineinfo.key = sshkey_new(KEY_UNSPEC)) == NULL) {
575 error("%s: sshkey_new failed", __func__);
576 return SSH_ERR_ALLOC_FAIL;
577 }
578 if (!hostfile_read_key(&cp, &kbits, lineinfo.key)) {
579#ifdef WITH_SSH1
580 sshkey_free(lineinfo.key);
581 lineinfo.key = sshkey_new(KEY_RSA1);
582 if (lineinfo.key == NULL) {
583 error("%s: sshkey_new fail", __func__);
584 return SSH_ERR_ALLOC_FAIL;
585 }
586 if (!hostfile_read_key(&cp, &kbits,
587 lineinfo.key))
588 goto bad;
589#else
590 goto bad;
591#endif
592 }
593 if (!hostfile_check_key(kbits, lineinfo.key, host,
594 path, linenum)) {
595 bad:
596 lineinfo.status = HKF_STATUS_INVALID;
597 if ((r = callback(&lineinfo, ctx)) != 0)
598 break;
599 continue;
600 }
601 }
602 if ((r = callback(&lineinfo, ctx)) != 0)
603 break;
604 }
605 sshkey_free(lineinfo.key);
606 fclose(f);
607 return r;
608}