blob: 91741cab847bba897514d38f6a0bd13f61f9fb28 [file] [log] [blame]
Damien Miller1f0311c2014-05-15 14:24:09 +10001/* $OpenBSD: hostfile.c,v 1.56 2014/04/29 18:01:49 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>
42
43#include <netinet/in.h>
44
Damien Millere7a1e5c2006-08-05 11:34:19 +100045#include <resolv.h>
Damien Millerded319c2006-09-01 15:38:36 +100046#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100047#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100048#include <stdlib.h>
49#include <string.h>
50
Damien Millerd7834352006-08-05 12:39:39 +100051#include "xmalloc.h"
Damien Miller450a7a12000-03-26 13:04:51 +100052#include "match.h"
Damien Miller450a7a12000-03-26 13:04:51 +100053#include "key.h"
54#include "hostfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "log.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110056#include "misc.h"
Damien Millerb3051d02014-01-10 10:58:53 +110057#include "digest.h"
Damien Miller4e8d9372014-02-04 11:02:42 +110058#include "hmac.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110059
60struct hostkeys {
61 struct hostkey_entry *entries;
62 u_int num_entries;
63};
Damien Millere1776152005-03-01 21:47:37 +110064
65static int
Damien Millerce986542013-07-18 16:12:44 +100066extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len)
Damien Millere1776152005-03-01 21:47:37 +110067{
68 char *p, *b64salt;
69 u_int b64len;
70 int ret;
71
72 if (l < sizeof(HASH_MAGIC) - 1) {
73 debug2("extract_salt: string too short");
74 return (-1);
75 }
76 if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
77 debug2("extract_salt: invalid magic identifier");
78 return (-1);
79 }
80 s += sizeof(HASH_MAGIC) - 1;
81 l -= sizeof(HASH_MAGIC) - 1;
82 if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
83 debug2("extract_salt: missing salt termination character");
84 return (-1);
85 }
86
87 b64len = p - s;
88 /* Sanity check */
89 if (b64len == 0 || b64len > 1024) {
90 debug2("extract_salt: bad encoded salt length %u", b64len);
91 return (-1);
92 }
93 b64salt = xmalloc(1 + b64len);
94 memcpy(b64salt, s, b64len);
95 b64salt[b64len] = '\0';
96
97 ret = __b64_pton(b64salt, salt, salt_len);
Darren Tuckera627d422013-06-02 07:31:17 +100098 free(b64salt);
Damien Millere1776152005-03-01 21:47:37 +110099 if (ret == -1) {
100 debug2("extract_salt: salt decode error");
101 return (-1);
102 }
Damien Miller4e8d9372014-02-04 11:02:42 +1100103 if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) {
104 debug2("extract_salt: expected salt len %zd, got %d",
105 ssh_hmac_bytes(SSH_DIGEST_SHA1), ret);
Damien Millere1776152005-03-01 21:47:37 +1100106 return (-1);
107 }
Darren Tucker47eede72005-03-14 23:08:12 +1100108
Damien Millere1776152005-03-01 21:47:37 +1100109 return (0);
110}
111
112char *
113host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
114{
Damien Miller4e8d9372014-02-04 11:02:42 +1100115 struct ssh_hmac_ctx *ctx;
Damien Millerce986542013-07-18 16:12:44 +1000116 u_char salt[256], result[256];
117 char uu_salt[512], uu_result[512];
Damien Millere1776152005-03-01 21:47:37 +1100118 static char encoded[1024];
119 u_int i, len;
120
Damien Miller4e8d9372014-02-04 11:02:42 +1100121 len = ssh_digest_bytes(SSH_DIGEST_SHA1);
Damien Millere1776152005-03-01 21:47:37 +1100122
123 if (name_from_hostfile == NULL) {
124 /* Create new salt */
125 for (i = 0; i < len; i++)
126 salt[i] = arc4random();
127 } else {
128 /* Extract salt from known host entry */
129 if (extract_salt(name_from_hostfile, src_len, salt,
130 sizeof(salt)) == -1)
131 return (NULL);
132 }
133
Damien Miller4e8d9372014-02-04 11:02:42 +1100134 if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL ||
135 ssh_hmac_init(ctx, salt, len) < 0 ||
136 ssh_hmac_update(ctx, host, strlen(host)) < 0 ||
137 ssh_hmac_final(ctx, result, sizeof(result)))
138 fatal("%s: ssh_hmac failed", __func__);
139 ssh_hmac_free(ctx);
Damien Millere1776152005-03-01 21:47:37 +1100140
Darren Tucker47eede72005-03-14 23:08:12 +1100141 if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
Damien Millere1776152005-03-01 21:47:37 +1100142 __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1)
Damien Miller4e8d9372014-02-04 11:02:42 +1100143 fatal("%s: __b64_ntop failed", __func__);
Damien Millere1776152005-03-01 21:47:37 +1100144
145 snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
146 HASH_DELIM, uu_result);
147
148 return (encoded);
149}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller5428f641999-11-25 11:54:57 +1100151/*
Damien Miller450a7a12000-03-26 13:04:51 +1000152 * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the
153 * pointer over the key. Skips any whitespace at the beginning and at end.
Damien Miller5428f641999-11-25 11:54:57 +1100154 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller5b2aea92001-12-21 12:47:09 +1100156int
Damien Millerce986542013-07-18 16:12:44 +1000157hostfile_read_key(char **cpp, int *bitsp, Key *ret)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158{
Damien Miller95def091999-11-25 00:26:21 +1100159 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 /* Skip leading whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100162 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
163 ;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Damien Miller0bc1bd82000-11-13 22:57:25 +1100165 if (key_read(ret, &cp) != 1)
Damien Miller95def091999-11-25 00:26:21 +1100166 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
Damien Miller95def091999-11-25 00:26:21 +1100168 /* Skip trailing whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100169 for (; *cp == ' ' || *cp == '\t'; cp++)
170 ;
Damien Miller95def091999-11-25 00:26:21 +1100171
172 /* Return results. */
173 *cpp = cp;
Damien Millerce986542013-07-18 16:12:44 +1000174 if (bitsp != NULL) {
175 if ((*bitsp = key_size(ret)) <= 0)
176 return 0;
177 }
Damien Miller95def091999-11-25 00:26:21 +1100178 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179}
180
Ben Lindstrombba81212001-06-25 05:01:22 +0000181static int
Damien Millerd925dcd2010-12-01 12:21:51 +1100182hostfile_check_key(int bits, const Key *key, const char *host,
183 const char *filename, u_long linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184{
Damien Miller1f0311c2014-05-15 14:24:09 +1000185#ifdef WITH_SSH1
Damien Miller0bc1bd82000-11-13 22:57:25 +1100186 if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
Damien Miller450a7a12000-03-26 13:04:51 +1000187 return 1;
188 if (bits != BN_num_bits(key->rsa->n)) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100189 logit("Warning: %s, line %lu: keysize mismatch for host %s: "
Damien Miller450a7a12000-03-26 13:04:51 +1000190 "actual %d vs. announced %d.",
191 filename, linenum, host, BN_num_bits(key->rsa->n), bits);
Damien Millerd925dcd2010-12-01 12:21:51 +1100192 logit("Warning: replace %d with %d in %s, line %lu.",
Damien Miller450a7a12000-03-26 13:04:51 +1000193 bits, BN_num_bits(key->rsa->n), filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000195#endif
Damien Miller450a7a12000-03-26 13:04:51 +1000196 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197}
198
Damien Millerd925dcd2010-12-01 12:21:51 +1100199static HostkeyMarker
Damien Miller1aed65e2010-03-04 21:53:35 +1100200check_markers(char **cpp)
201{
202 char marker[32], *sp, *cp = *cpp;
203 int ret = MRK_NONE;
204
205 while (*cp == '@') {
206 /* Only one marker is allowed */
207 if (ret != MRK_NONE)
208 return MRK_ERROR;
209 /* Markers are terminated by whitespace */
210 if ((sp = strchr(cp, ' ')) == NULL &&
211 (sp = strchr(cp, '\t')) == NULL)
212 return MRK_ERROR;
213 /* Extract marker for comparison */
214 if (sp <= cp + 1 || sp >= cp + sizeof(marker))
215 return MRK_ERROR;
216 memcpy(marker, cp, sp - cp);
217 marker[sp - cp] = '\0';
218 if (strcmp(marker, CA_MARKER) == 0)
219 ret = MRK_CA;
220 else if (strcmp(marker, REVOKE_MARKER) == 0)
221 ret = MRK_REVOKE;
222 else
223 return MRK_ERROR;
224
225 /* Skip past marker and any whitespace that follows it */
226 cp = sp;
227 for (; *cp == ' ' || *cp == '\t'; cp++)
228 ;
229 }
230 *cpp = cp;
231 return ret;
232}
233
Damien Millerd925dcd2010-12-01 12:21:51 +1100234struct hostkeys *
235init_hostkeys(void)
236{
237 struct hostkeys *ret = xcalloc(1, sizeof(*ret));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238
Damien Millerd925dcd2010-12-01 12:21:51 +1100239 ret->entries = NULL;
240 return ret;
241}
242
243void
244load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245{
Damien Miller95def091999-11-25 00:26:21 +1100246 FILE *f;
247 char line[8192];
Damien Millerd925dcd2010-12-01 12:21:51 +1100248 u_long linenum = 0, num_loaded = 0;
Damien Millere1776152005-03-01 21:47:37 +1100249 char *cp, *cp2, *hashed_host;
Damien Millerd925dcd2010-12-01 12:21:51 +1100250 HostkeyMarker marker;
251 Key *key;
252 int kbits;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Millerd925dcd2010-12-01 12:21:51 +1100254 if ((f = fopen(path, "r")) == NULL)
255 return;
256 debug3("%s: loading entries for host \"%.100s\" from file \"%s\"",
257 __func__, host, path);
258 while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100259 cp = line;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Miller5428f641999-11-25 11:54:57 +1100261 /* Skip any leading whitespace, comments and empty lines. */
262 for (; *cp == ' ' || *cp == '\t'; cp++)
263 ;
Damien Miller95def091999-11-25 00:26:21 +1100264 if (!*cp || *cp == '#' || *cp == '\n')
265 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Damien Millerd925dcd2010-12-01 12:21:51 +1100267 if ((marker = check_markers(&cp)) == MRK_ERROR) {
268 verbose("%s: invalid marker at %s:%lu",
269 __func__, path, linenum);
Damien Miller1aed65e2010-03-04 21:53:35 +1100270 continue;
Damien Millerd925dcd2010-12-01 12:21:51 +1100271 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100272
Damien Miller95def091999-11-25 00:26:21 +1100273 /* Find the end of the host name portion. */
Damien Miller5428f641999-11-25 11:54:57 +1100274 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
275 ;
Damien Miller7e8e8201999-11-16 13:37:16 +1100276
Damien Miller95def091999-11-25 00:26:21 +1100277 /* Check if the host name matches. */
Damien Millere1776152005-03-01 21:47:37 +1100278 if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) {
279 if (*cp != HASH_DELIM)
280 continue;
281 hashed_host = host_hash(host, cp, (u_int) (cp2 - cp));
282 if (hashed_host == NULL) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100283 debug("Invalid hashed host line %lu of %s",
284 linenum, path);
Damien Millere1776152005-03-01 21:47:37 +1100285 continue;
286 }
287 if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0)
288 continue;
289 }
Damien Miller95def091999-11-25 00:26:21 +1100290
291 /* Got a match. Skip host name. */
292 cp = cp2;
293
Damien Miller5428f641999-11-25 11:54:57 +1100294 /*
295 * Extract the key from the line. This will skip any leading
296 * whitespace. Ignore badly formatted lines.
297 */
Damien Millerd925dcd2010-12-01 12:21:51 +1100298 key = key_new(KEY_UNSPEC);
299 if (!hostfile_read_key(&cp, &kbits, key)) {
300 key_free(key);
Damien Miller1f0311c2014-05-15 14:24:09 +1000301#ifdef WITH_SSH1
Damien Millerd925dcd2010-12-01 12:21:51 +1100302 key = key_new(KEY_RSA1);
303 if (!hostfile_read_key(&cp, &kbits, key)) {
304 key_free(key);
305 continue;
Damien Miller6db780e2006-03-26 13:52:20 +1100306 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000307#else
308 continue;
309#endif
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000310 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100311 if (!hostfile_check_key(kbits, key, host, path, linenum))
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000312 continue;
313
Damien Millerd925dcd2010-12-01 12:21:51 +1100314 debug3("%s: found %skey type %s in file %s:%lu", __func__,
315 marker == MRK_NONE ? "" :
316 (marker == MRK_CA ? "ca " : "revoked "),
317 key_type(key), path, linenum);
318 hostkeys->entries = xrealloc(hostkeys->entries,
319 hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
320 hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
321 hostkeys->entries[hostkeys->num_entries].file = xstrdup(path);
322 hostkeys->entries[hostkeys->num_entries].line = linenum;
323 hostkeys->entries[hostkeys->num_entries].key = key;
324 hostkeys->entries[hostkeys->num_entries].marker = marker;
325 hostkeys->num_entries++;
326 num_loaded++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100328 debug3("%s: loaded %lu keys", __func__, num_loaded);
Darren Tucker094f1e92010-12-05 09:03:31 +1100329 fclose(f);
Damien Millerd925dcd2010-12-01 12:21:51 +1100330 return;
331}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Millerd925dcd2010-12-01 12:21:51 +1100333void
334free_hostkeys(struct hostkeys *hostkeys)
335{
336 u_int i;
337
338 for (i = 0; i < hostkeys->num_entries; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000339 free(hostkeys->entries[i].host);
340 free(hostkeys->entries[i].file);
Damien Millerd925dcd2010-12-01 12:21:51 +1100341 key_free(hostkeys->entries[i].key);
Damien Miller1d2c4562014-02-04 11:18:20 +1100342 explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
Damien Millerd925dcd2010-12-01 12:21:51 +1100343 }
Darren Tuckera627d422013-06-02 07:31:17 +1000344 free(hostkeys->entries);
Damien Miller1d2c4562014-02-04 11:18:20 +1100345 explicit_bzero(hostkeys, sizeof(*hostkeys));
Darren Tuckera627d422013-06-02 07:31:17 +1000346 free(hostkeys);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347}
348
Damien Millerd925dcd2010-12-01 12:21:51 +1100349static int
350check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
351{
352 int is_cert = key_is_cert(k);
353 u_int i;
354
355 for (i = 0; i < hostkeys->num_entries; i++) {
356 if (hostkeys->entries[i].marker != MRK_REVOKE)
357 continue;
358 if (key_equal_public(k, hostkeys->entries[i].key))
359 return -1;
360 if (is_cert &&
361 key_equal_public(k->cert->signature_key,
362 hostkeys->entries[i].key))
363 return -1;
364 }
365 return 0;
366}
367
368/*
369 * Match keys against a specified key, or look one up by key type.
370 *
371 * If looking for a keytype (key == NULL) and one is found then return
372 * HOST_FOUND, otherwise HOST_NEW.
373 *
374 * If looking for a key (key != NULL):
375 * 1. If the key is a cert and a matching CA is found, return HOST_OK
376 * 2. If the key is not a cert and a matching key is found, return HOST_OK
377 * 3. If no key matches but a key with a different type is found, then
378 * return HOST_CHANGED
379 * 4. If no matching keys are found, then return HOST_NEW.
380 *
381 * Finally, check any found key is not revoked.
382 */
383static HostStatus
384check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
385 Key *k, int keytype, const struct hostkey_entry **found)
386{
387 u_int i;
388 HostStatus end_return = HOST_NEW;
389 int want_cert = key_is_cert(k);
390 HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
391 int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
392
393 if (found != NULL)
394 *found = NULL;
395
396 for (i = 0; i < hostkeys->num_entries; i++) {
397 if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1)
398 continue;
399 if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1)
400 continue;
401 if (hostkeys->entries[i].marker != want_marker)
402 continue;
403 if (k == NULL) {
404 if (hostkeys->entries[i].key->type != keytype)
405 continue;
406 end_return = HOST_FOUND;
407 if (found != NULL)
408 *found = hostkeys->entries + i;
409 k = hostkeys->entries[i].key;
410 break;
411 }
412 if (want_cert) {
413 if (key_equal_public(k->cert->signature_key,
414 hostkeys->entries[i].key)) {
415 /* A matching CA exists */
416 end_return = HOST_OK;
417 if (found != NULL)
418 *found = hostkeys->entries + i;
419 break;
420 }
421 } else {
422 if (key_equal(k, hostkeys->entries[i].key)) {
423 end_return = HOST_OK;
424 if (found != NULL)
425 *found = hostkeys->entries + i;
426 break;
427 }
428 /* A non-maching key exists */
429 end_return = HOST_CHANGED;
430 if (found != NULL)
431 *found = hostkeys->entries + i;
432 }
433 }
434 if (check_key_not_revoked(hostkeys, k) != 0) {
435 end_return = HOST_REVOKED;
436 if (found != NULL)
437 *found = NULL;
438 }
439 return end_return;
440}
441
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000442HostStatus
Damien Millerd925dcd2010-12-01 12:21:51 +1100443check_key_in_hostkeys(struct hostkeys *hostkeys, Key *key,
444 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000445{
446 if (key == NULL)
447 fatal("no key to look up");
Damien Millerd925dcd2010-12-01 12:21:51 +1100448 return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000449}
450
451int
Damien Millerd925dcd2010-12-01 12:21:51 +1100452lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
453 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000454{
Damien Millerd925dcd2010-12-01 12:21:51 +1100455 return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
456 found) == HOST_FOUND);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000457}
458
Damien Miller5428f641999-11-25 11:54:57 +1100459/*
460 * Appends an entry to the host file. Returns false if the entry could not
461 * be appended.
462 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
464int
Darren Tucker47eede72005-03-14 23:08:12 +1100465add_host_to_hostfile(const char *filename, const char *host, const Key *key,
Damien Millere1776152005-03-01 21:47:37 +1100466 int store_hash)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467{
Damien Miller95def091999-11-25 00:26:21 +1100468 FILE *f;
Damien Miller450a7a12000-03-26 13:04:51 +1000469 int success = 0;
Darren Tucker40858532005-08-02 17:07:07 +1000470 char *hashed_host = NULL;
Damien Millere1776152005-03-01 21:47:37 +1100471
Damien Miller450a7a12000-03-26 13:04:51 +1000472 if (key == NULL)
Damien Millereba71ba2000-04-29 23:57:08 +1000473 return 1; /* XXX ? */
Damien Miller95def091999-11-25 00:26:21 +1100474 f = fopen(filename, "a");
475 if (!f)
476 return 0;
Damien Millere1776152005-03-01 21:47:37 +1100477
478 if (store_hash) {
479 if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
480 error("add_host_to_hostfile: host_hash failed");
481 fclose(f);
482 return 0;
483 }
484 }
485 fprintf(f, "%s ", store_hash ? hashed_host : host);
486
Damien Miller450a7a12000-03-26 13:04:51 +1000487 if (key_write(key, f)) {
Damien Miller450a7a12000-03-26 13:04:51 +1000488 success = 1;
489 } else {
Damien Millereba71ba2000-04-29 23:57:08 +1000490 error("add_host_to_hostfile: saving key in %s failed", filename);
Damien Miller95def091999-11-25 00:26:21 +1100491 }
Damien Millereba71ba2000-04-29 23:57:08 +1000492 fprintf(f, "\n");
Damien Miller95def091999-11-25 00:26:21 +1100493 fclose(f);
Damien Miller450a7a12000-03-26 13:04:51 +1000494 return success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495}