blob: 2ff4c48b4805bcdc7f881c03af26a7a637111dbf [file] [log] [blame]
Damien Millerce986542013-07-18 16:12:44 +10001/* $OpenBSD: hostfile.c,v 1.52 2013/07/12 00:19:58 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
Damien Millere1776152005-03-01 21:47:37 +110045#include <openssl/hmac.h>
46#include <openssl/sha.h>
Damien Miller450a7a12000-03-26 13:04:51 +100047
Damien Millere7a1e5c2006-08-05 11:34:19 +100048#include <resolv.h>
Damien Millerded319c2006-09-01 15:38:36 +100049#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100050#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100051#include <stdlib.h>
52#include <string.h>
53
Damien Millerd7834352006-08-05 12:39:39 +100054#include "xmalloc.h"
Damien Miller450a7a12000-03-26 13:04:51 +100055#include "match.h"
Damien Miller450a7a12000-03-26 13:04:51 +100056#include "key.h"
57#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"
60
61struct hostkeys {
62 struct hostkey_entry *entries;
63 u_int num_entries;
64};
Damien Millere1776152005-03-01 21:47:37 +110065
66static int
Damien Millerce986542013-07-18 16:12:44 +100067extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len)
Damien Millere1776152005-03-01 21:47:37 +110068{
69 char *p, *b64salt;
70 u_int b64len;
71 int ret;
72
73 if (l < sizeof(HASH_MAGIC) - 1) {
74 debug2("extract_salt: string too short");
75 return (-1);
76 }
77 if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
78 debug2("extract_salt: invalid magic identifier");
79 return (-1);
80 }
81 s += sizeof(HASH_MAGIC) - 1;
82 l -= sizeof(HASH_MAGIC) - 1;
83 if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
84 debug2("extract_salt: missing salt termination character");
85 return (-1);
86 }
87
88 b64len = p - s;
89 /* Sanity check */
90 if (b64len == 0 || b64len > 1024) {
91 debug2("extract_salt: bad encoded salt length %u", b64len);
92 return (-1);
93 }
94 b64salt = xmalloc(1 + b64len);
95 memcpy(b64salt, s, b64len);
96 b64salt[b64len] = '\0';
97
98 ret = __b64_pton(b64salt, salt, salt_len);
Darren Tuckera627d422013-06-02 07:31:17 +100099 free(b64salt);
Damien Millere1776152005-03-01 21:47:37 +1100100 if (ret == -1) {
101 debug2("extract_salt: salt decode error");
102 return (-1);
103 }
104 if (ret != SHA_DIGEST_LENGTH) {
Darren Tucker593bae72005-11-22 19:43:26 +1100105 debug2("extract_salt: expected salt len %d, got %d",
106 SHA_DIGEST_LENGTH, ret);
Damien Millere1776152005-03-01 21:47:37 +1100107 return (-1);
108 }
Darren Tucker47eede72005-03-14 23:08:12 +1100109
Damien Millere1776152005-03-01 21:47:37 +1100110 return (0);
111}
112
113char *
114host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
115{
116 const EVP_MD *md = EVP_sha1();
117 HMAC_CTX mac_ctx;
Damien Millerce986542013-07-18 16:12:44 +1000118 u_char salt[256], result[256];
119 char uu_salt[512], uu_result[512];
Damien Millere1776152005-03-01 21:47:37 +1100120 static char encoded[1024];
121 u_int i, len;
122
123 len = EVP_MD_size(md);
124
125 if (name_from_hostfile == NULL) {
126 /* Create new salt */
127 for (i = 0; i < len; i++)
128 salt[i] = arc4random();
129 } else {
130 /* Extract salt from known host entry */
131 if (extract_salt(name_from_hostfile, src_len, salt,
132 sizeof(salt)) == -1)
133 return (NULL);
134 }
135
136 HMAC_Init(&mac_ctx, salt, len, md);
Damien Millerce986542013-07-18 16:12:44 +1000137 HMAC_Update(&mac_ctx, (u_char *)host, strlen(host));
Damien Millere1776152005-03-01 21:47:37 +1100138 HMAC_Final(&mac_ctx, result, NULL);
139 HMAC_cleanup(&mac_ctx);
140
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)
143 fatal("host_hash: __b64_ntop failed");
144
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 Miller0bc1bd82000-11-13 22:57:25 +1100185 if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
Damien Miller450a7a12000-03-26 13:04:51 +1000186 return 1;
187 if (bits != BN_num_bits(key->rsa->n)) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100188 logit("Warning: %s, line %lu: keysize mismatch for host %s: "
Damien Miller450a7a12000-03-26 13:04:51 +1000189 "actual %d vs. announced %d.",
190 filename, linenum, host, BN_num_bits(key->rsa->n), bits);
Damien Millerd925dcd2010-12-01 12:21:51 +1100191 logit("Warning: replace %d with %d in %s, line %lu.",
Damien Miller450a7a12000-03-26 13:04:51 +1000192 bits, BN_num_bits(key->rsa->n), filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193 }
Damien Miller450a7a12000-03-26 13:04:51 +1000194 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195}
196
Damien Millerd925dcd2010-12-01 12:21:51 +1100197static HostkeyMarker
Damien Miller1aed65e2010-03-04 21:53:35 +1100198check_markers(char **cpp)
199{
200 char marker[32], *sp, *cp = *cpp;
201 int ret = MRK_NONE;
202
203 while (*cp == '@') {
204 /* Only one marker is allowed */
205 if (ret != MRK_NONE)
206 return MRK_ERROR;
207 /* Markers are terminated by whitespace */
208 if ((sp = strchr(cp, ' ')) == NULL &&
209 (sp = strchr(cp, '\t')) == NULL)
210 return MRK_ERROR;
211 /* Extract marker for comparison */
212 if (sp <= cp + 1 || sp >= cp + sizeof(marker))
213 return MRK_ERROR;
214 memcpy(marker, cp, sp - cp);
215 marker[sp - cp] = '\0';
216 if (strcmp(marker, CA_MARKER) == 0)
217 ret = MRK_CA;
218 else if (strcmp(marker, REVOKE_MARKER) == 0)
219 ret = MRK_REVOKE;
220 else
221 return MRK_ERROR;
222
223 /* Skip past marker and any whitespace that follows it */
224 cp = sp;
225 for (; *cp == ' ' || *cp == '\t'; cp++)
226 ;
227 }
228 *cpp = cp;
229 return ret;
230}
231
Damien Millerd925dcd2010-12-01 12:21:51 +1100232struct hostkeys *
233init_hostkeys(void)
234{
235 struct hostkeys *ret = xcalloc(1, sizeof(*ret));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Damien Millerd925dcd2010-12-01 12:21:51 +1100237 ret->entries = NULL;
238 return ret;
239}
240
241void
242load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243{
Damien Miller95def091999-11-25 00:26:21 +1100244 FILE *f;
245 char line[8192];
Damien Millerd925dcd2010-12-01 12:21:51 +1100246 u_long linenum = 0, num_loaded = 0;
Damien Millere1776152005-03-01 21:47:37 +1100247 char *cp, *cp2, *hashed_host;
Damien Millerd925dcd2010-12-01 12:21:51 +1100248 HostkeyMarker marker;
249 Key *key;
250 int kbits;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Millerd925dcd2010-12-01 12:21:51 +1100252 if ((f = fopen(path, "r")) == NULL)
253 return;
254 debug3("%s: loading entries for host \"%.100s\" from file \"%s\"",
255 __func__, host, path);
256 while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100257 cp = line;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller5428f641999-11-25 11:54:57 +1100259 /* Skip any leading whitespace, comments and empty lines. */
260 for (; *cp == ' ' || *cp == '\t'; cp++)
261 ;
Damien Miller95def091999-11-25 00:26:21 +1100262 if (!*cp || *cp == '#' || *cp == '\n')
263 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Damien Millerd925dcd2010-12-01 12:21:51 +1100265 if ((marker = check_markers(&cp)) == MRK_ERROR) {
266 verbose("%s: invalid marker at %s:%lu",
267 __func__, path, linenum);
Damien Miller1aed65e2010-03-04 21:53:35 +1100268 continue;
Damien Millerd925dcd2010-12-01 12:21:51 +1100269 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100270
Damien Miller95def091999-11-25 00:26:21 +1100271 /* Find the end of the host name portion. */
Damien Miller5428f641999-11-25 11:54:57 +1100272 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
273 ;
Damien Miller7e8e8201999-11-16 13:37:16 +1100274
Damien Miller95def091999-11-25 00:26:21 +1100275 /* Check if the host name matches. */
Damien Millere1776152005-03-01 21:47:37 +1100276 if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) {
277 if (*cp != HASH_DELIM)
278 continue;
279 hashed_host = host_hash(host, cp, (u_int) (cp2 - cp));
280 if (hashed_host == NULL) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100281 debug("Invalid hashed host line %lu of %s",
282 linenum, path);
Damien Millere1776152005-03-01 21:47:37 +1100283 continue;
284 }
285 if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0)
286 continue;
287 }
Damien Miller95def091999-11-25 00:26:21 +1100288
289 /* Got a match. Skip host name. */
290 cp = cp2;
291
Damien Miller5428f641999-11-25 11:54:57 +1100292 /*
293 * Extract the key from the line. This will skip any leading
294 * whitespace. Ignore badly formatted lines.
295 */
Damien Millerd925dcd2010-12-01 12:21:51 +1100296 key = key_new(KEY_UNSPEC);
297 if (!hostfile_read_key(&cp, &kbits, key)) {
298 key_free(key);
299 key = key_new(KEY_RSA1);
300 if (!hostfile_read_key(&cp, &kbits, key)) {
301 key_free(key);
302 continue;
Damien Miller6db780e2006-03-26 13:52:20 +1100303 }
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000304 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100305 if (!hostfile_check_key(kbits, key, host, path, linenum))
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000306 continue;
307
Damien Millerd925dcd2010-12-01 12:21:51 +1100308 debug3("%s: found %skey type %s in file %s:%lu", __func__,
309 marker == MRK_NONE ? "" :
310 (marker == MRK_CA ? "ca " : "revoked "),
311 key_type(key), path, linenum);
312 hostkeys->entries = xrealloc(hostkeys->entries,
313 hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
314 hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
315 hostkeys->entries[hostkeys->num_entries].file = xstrdup(path);
316 hostkeys->entries[hostkeys->num_entries].line = linenum;
317 hostkeys->entries[hostkeys->num_entries].key = key;
318 hostkeys->entries[hostkeys->num_entries].marker = marker;
319 hostkeys->num_entries++;
320 num_loaded++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100322 debug3("%s: loaded %lu keys", __func__, num_loaded);
Darren Tucker094f1e92010-12-05 09:03:31 +1100323 fclose(f);
Damien Millerd925dcd2010-12-01 12:21:51 +1100324 return;
325}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Damien Millerd925dcd2010-12-01 12:21:51 +1100327void
328free_hostkeys(struct hostkeys *hostkeys)
329{
330 u_int i;
331
332 for (i = 0; i < hostkeys->num_entries; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000333 free(hostkeys->entries[i].host);
334 free(hostkeys->entries[i].file);
Damien Millerd925dcd2010-12-01 12:21:51 +1100335 key_free(hostkeys->entries[i].key);
336 bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
337 }
Darren Tuckera627d422013-06-02 07:31:17 +1000338 free(hostkeys->entries);
339 bzero(hostkeys, sizeof(*hostkeys));
340 free(hostkeys);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341}
342
Damien Millerd925dcd2010-12-01 12:21:51 +1100343static int
344check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
345{
346 int is_cert = key_is_cert(k);
347 u_int i;
348
349 for (i = 0; i < hostkeys->num_entries; i++) {
350 if (hostkeys->entries[i].marker != MRK_REVOKE)
351 continue;
352 if (key_equal_public(k, hostkeys->entries[i].key))
353 return -1;
354 if (is_cert &&
355 key_equal_public(k->cert->signature_key,
356 hostkeys->entries[i].key))
357 return -1;
358 }
359 return 0;
360}
361
362/*
363 * Match keys against a specified key, or look one up by key type.
364 *
365 * If looking for a keytype (key == NULL) and one is found then return
366 * HOST_FOUND, otherwise HOST_NEW.
367 *
368 * If looking for a key (key != NULL):
369 * 1. If the key is a cert and a matching CA is found, return HOST_OK
370 * 2. If the key is not a cert and a matching key is found, return HOST_OK
371 * 3. If no key matches but a key with a different type is found, then
372 * return HOST_CHANGED
373 * 4. If no matching keys are found, then return HOST_NEW.
374 *
375 * Finally, check any found key is not revoked.
376 */
377static HostStatus
378check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
379 Key *k, int keytype, const struct hostkey_entry **found)
380{
381 u_int i;
382 HostStatus end_return = HOST_NEW;
383 int want_cert = key_is_cert(k);
384 HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
385 int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
386
387 if (found != NULL)
388 *found = NULL;
389
390 for (i = 0; i < hostkeys->num_entries; i++) {
391 if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1)
392 continue;
393 if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1)
394 continue;
395 if (hostkeys->entries[i].marker != want_marker)
396 continue;
397 if (k == NULL) {
398 if (hostkeys->entries[i].key->type != keytype)
399 continue;
400 end_return = HOST_FOUND;
401 if (found != NULL)
402 *found = hostkeys->entries + i;
403 k = hostkeys->entries[i].key;
404 break;
405 }
406 if (want_cert) {
407 if (key_equal_public(k->cert->signature_key,
408 hostkeys->entries[i].key)) {
409 /* A matching CA exists */
410 end_return = HOST_OK;
411 if (found != NULL)
412 *found = hostkeys->entries + i;
413 break;
414 }
415 } else {
416 if (key_equal(k, hostkeys->entries[i].key)) {
417 end_return = HOST_OK;
418 if (found != NULL)
419 *found = hostkeys->entries + i;
420 break;
421 }
422 /* A non-maching key exists */
423 end_return = HOST_CHANGED;
424 if (found != NULL)
425 *found = hostkeys->entries + i;
426 }
427 }
428 if (check_key_not_revoked(hostkeys, k) != 0) {
429 end_return = HOST_REVOKED;
430 if (found != NULL)
431 *found = NULL;
432 }
433 return end_return;
434}
435
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000436HostStatus
Damien Millerd925dcd2010-12-01 12:21:51 +1100437check_key_in_hostkeys(struct hostkeys *hostkeys, Key *key,
438 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000439{
440 if (key == NULL)
441 fatal("no key to look up");
Damien Millerd925dcd2010-12-01 12:21:51 +1100442 return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000443}
444
445int
Damien Millerd925dcd2010-12-01 12:21:51 +1100446lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
447 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000448{
Damien Millerd925dcd2010-12-01 12:21:51 +1100449 return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
450 found) == HOST_FOUND);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000451}
452
Damien Miller5428f641999-11-25 11:54:57 +1100453/*
454 * Appends an entry to the host file. Returns false if the entry could not
455 * be appended.
456 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
458int
Darren Tucker47eede72005-03-14 23:08:12 +1100459add_host_to_hostfile(const char *filename, const char *host, const Key *key,
Damien Millere1776152005-03-01 21:47:37 +1100460 int store_hash)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461{
Damien Miller95def091999-11-25 00:26:21 +1100462 FILE *f;
Damien Miller450a7a12000-03-26 13:04:51 +1000463 int success = 0;
Darren Tucker40858532005-08-02 17:07:07 +1000464 char *hashed_host = NULL;
Damien Millere1776152005-03-01 21:47:37 +1100465
Damien Miller450a7a12000-03-26 13:04:51 +1000466 if (key == NULL)
Damien Millereba71ba2000-04-29 23:57:08 +1000467 return 1; /* XXX ? */
Damien Miller95def091999-11-25 00:26:21 +1100468 f = fopen(filename, "a");
469 if (!f)
470 return 0;
Damien Millere1776152005-03-01 21:47:37 +1100471
472 if (store_hash) {
473 if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
474 error("add_host_to_hostfile: host_hash failed");
475 fclose(f);
476 return 0;
477 }
478 }
479 fprintf(f, "%s ", store_hash ? hashed_host : host);
480
Damien Miller450a7a12000-03-26 13:04:51 +1000481 if (key_write(key, f)) {
Damien Miller450a7a12000-03-26 13:04:51 +1000482 success = 1;
483 } else {
Damien Millereba71ba2000-04-29 23:57:08 +1000484 error("add_host_to_hostfile: saving key in %s failed", filename);
Damien Miller95def091999-11-25 00:26:21 +1100485 }
Damien Millereba71ba2000-04-29 23:57:08 +1000486 fprintf(f, "\n");
Damien Miller95def091999-11-25 00:26:21 +1100487 fclose(f);
Damien Miller450a7a12000-03-26 13:04:51 +1000488 return success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489}