xref: /titanic_50/usr/src/cmd/ssh/libssh/common/hostfile.c (revision 442d23f49355a5d0694c758975be57af39f91a61)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate  *                    All rights reserved
57c478bd9Sstevel@tonic-gate  * Functions for manipulating the known hosts files.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
87c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
97c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
107c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
117c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
157c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Niels Provos.  All rights reserved.
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
187c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
197c478bd9Sstevel@tonic-gate  * are met:
207c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
217c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
227c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
237c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
247c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
277c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
287c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
297c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
307c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
317c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
327c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
337c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
347c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
357c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
38*442d23f4Sjp161948 /* $OpenBSD: hostfile.c,v 1.45 2006/08/03 03:34:42 deraadt Exp $ */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
42*442d23f4Sjp161948 #include "includes.h"
43*442d23f4Sjp161948 
44*442d23f4Sjp161948 #include <openssl/hmac.h>
45*442d23f4Sjp161948 #include <openssl/sha.h>
46*442d23f4Sjp161948 
477c478bd9Sstevel@tonic-gate #include "packet.h"
48*442d23f4Sjp161948 #include "xmalloc.h"
497c478bd9Sstevel@tonic-gate #include "match.h"
507c478bd9Sstevel@tonic-gate #include "key.h"
517c478bd9Sstevel@tonic-gate #include "hostfile.h"
527c478bd9Sstevel@tonic-gate #include "log.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
55*442d23f4Sjp161948  * Format of a hashed hostname is <MAGIC><SALT>|<HASHED_HOSTNAME>. <MAGIC> is
56*442d23f4Sjp161948  * "|1|". As in non-hashed hostnames this whole string is then followed by a
57*442d23f4Sjp161948  * space, a key type and the key (which is out of scope of this function).
58*442d23f4Sjp161948  *
59*442d23f4Sjp161948  * Example what can be in 's':
60*442d23f4Sjp161948  *
61*442d23f4Sjp161948  * |1|t17NtsuXSLwP0H0eYdd8vJeNakM=|9XFVPh3jZUrfY6YCWn8Ua5eGZtA=
62*442d23f4Sjp161948  */
63*442d23f4Sjp161948 static int
extract_salt(const char * s,u_int l,char * salt,size_t salt_len)64*442d23f4Sjp161948 extract_salt(const char *s, u_int l, char *salt, size_t salt_len)
65*442d23f4Sjp161948 {
66*442d23f4Sjp161948 	char *p;
67*442d23f4Sjp161948 	u_char *b64salt;
68*442d23f4Sjp161948 	u_int b64len;
69*442d23f4Sjp161948 	int ret;
70*442d23f4Sjp161948 
71*442d23f4Sjp161948 	if (l < sizeof(HASH_MAGIC) - 1) {
72*442d23f4Sjp161948 		debug2("extract_salt: string too short");
73*442d23f4Sjp161948 		return (-1);
74*442d23f4Sjp161948 	}
75*442d23f4Sjp161948 	if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
76*442d23f4Sjp161948 		debug2("extract_salt: invalid magic identifier");
77*442d23f4Sjp161948 		return (-1);
78*442d23f4Sjp161948 	}
79*442d23f4Sjp161948 	s += sizeof(HASH_MAGIC) - 1;
80*442d23f4Sjp161948 	l -= sizeof(HASH_MAGIC) - 1;
81*442d23f4Sjp161948 	if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
82*442d23f4Sjp161948 		debug2("extract_salt: missing salt termination character");
83*442d23f4Sjp161948 		return (-1);
84*442d23f4Sjp161948 	}
85*442d23f4Sjp161948 
86*442d23f4Sjp161948 	b64len = p - s;
87*442d23f4Sjp161948 	/* Sanity check */
88*442d23f4Sjp161948 	if (b64len == 0 || b64len > 1024) {
89*442d23f4Sjp161948 		debug2("extract_salt: bad encoded salt length %u", b64len);
90*442d23f4Sjp161948 		return (-1);
91*442d23f4Sjp161948 	}
92*442d23f4Sjp161948 	b64salt = xmalloc(1 + b64len);
93*442d23f4Sjp161948 	memcpy(b64salt, s, b64len);
94*442d23f4Sjp161948 	b64salt[b64len] = '\0';
95*442d23f4Sjp161948 
96*442d23f4Sjp161948 	ret = __b64_pton(b64salt, (u_char *) salt, salt_len);
97*442d23f4Sjp161948 	xfree(b64salt);
98*442d23f4Sjp161948 	if (ret == -1) {
99*442d23f4Sjp161948 		debug2("extract_salt: salt decode error");
100*442d23f4Sjp161948 		return (-1);
101*442d23f4Sjp161948 	}
102*442d23f4Sjp161948 	if (ret != SHA_DIGEST_LENGTH) {
103*442d23f4Sjp161948 		debug2("extract_salt: expected salt len %d, got %d",
104*442d23f4Sjp161948 		    SHA_DIGEST_LENGTH, ret);
105*442d23f4Sjp161948 		return (-1);
106*442d23f4Sjp161948 	}
107*442d23f4Sjp161948 
108*442d23f4Sjp161948 	return (0);
109*442d23f4Sjp161948 }
110*442d23f4Sjp161948 
111*442d23f4Sjp161948 char *
host_hash(const char * host,const char * name_from_hostfile,u_int src_len)112*442d23f4Sjp161948 host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
113*442d23f4Sjp161948 {
114*442d23f4Sjp161948 	const EVP_MD *md = EVP_sha1();
115*442d23f4Sjp161948 	HMAC_CTX mac_ctx;
116*442d23f4Sjp161948 	char salt[256], result[256], uu_salt[512], uu_result[512];
117*442d23f4Sjp161948 	static char encoded[1024];
118*442d23f4Sjp161948 	u_int i, len;
119*442d23f4Sjp161948 
120*442d23f4Sjp161948 	len = EVP_MD_size(md);
121*442d23f4Sjp161948 
122*442d23f4Sjp161948 	if (name_from_hostfile == NULL) {
123*442d23f4Sjp161948 		/* Create new salt */
124*442d23f4Sjp161948 		for (i = 0; i < len; i++)
125*442d23f4Sjp161948 			salt[i] = arc4random();
126*442d23f4Sjp161948 	} else {
127*442d23f4Sjp161948 		/* Extract salt from known host entry */
128*442d23f4Sjp161948 		if (extract_salt(name_from_hostfile, src_len, salt,
129*442d23f4Sjp161948 		    sizeof(salt)) == -1)
130*442d23f4Sjp161948 			return (NULL);
131*442d23f4Sjp161948 	}
132*442d23f4Sjp161948 
133*442d23f4Sjp161948 	HMAC_Init(&mac_ctx, salt, len, md);
134*442d23f4Sjp161948 	HMAC_Update(&mac_ctx, (u_char *) host, strlen(host));
135*442d23f4Sjp161948 	HMAC_Final(&mac_ctx, (u_char *) result, NULL);
136*442d23f4Sjp161948 	HMAC_cleanup(&mac_ctx);
137*442d23f4Sjp161948 
138*442d23f4Sjp161948 	if (__b64_ntop((u_char *) salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
139*442d23f4Sjp161948 	    __b64_ntop((u_char *) result, len, uu_result, sizeof(uu_result)) == -1)
140*442d23f4Sjp161948 		fatal("host_hash: __b64_ntop failed");
141*442d23f4Sjp161948 
142*442d23f4Sjp161948 	snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
143*442d23f4Sjp161948 	    HASH_DELIM, uu_result);
144*442d23f4Sjp161948 
145*442d23f4Sjp161948 	return (encoded);
146*442d23f4Sjp161948 }
147*442d23f4Sjp161948 
148*442d23f4Sjp161948 /*
1497c478bd9Sstevel@tonic-gate  * Parses an RSA (number of bits, e, n) or DSA key from a string.  Moves the
1507c478bd9Sstevel@tonic-gate  * pointer over the key.  Skips any whitespace at the beginning and at end.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate int
hostfile_read_key(char ** cpp,u_int * bitsp,Key * ret)1547c478bd9Sstevel@tonic-gate hostfile_read_key(char **cpp, u_int *bitsp, Key *ret)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	char *cp;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* Skip leading whitespace. */
1597c478bd9Sstevel@tonic-gate 	for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
1607c478bd9Sstevel@tonic-gate 		;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (key_read(ret, &cp) != 1)
1637c478bd9Sstevel@tonic-gate 		return 0;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/* Skip trailing whitespace. */
1667c478bd9Sstevel@tonic-gate 	for (; *cp == ' ' || *cp == '\t'; cp++)
1677c478bd9Sstevel@tonic-gate 		;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* Return results. */
1707c478bd9Sstevel@tonic-gate 	*cpp = cp;
1717c478bd9Sstevel@tonic-gate 	*bitsp = key_size(ret);
1727c478bd9Sstevel@tonic-gate 	return 1;
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static int
hostfile_check_key(int bits,const Key * key,const char * host,const char * filename,int linenum)176*442d23f4Sjp161948 hostfile_check_key(int bits, const Key *key, const char *host, const char *filename, int linenum)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
1797c478bd9Sstevel@tonic-gate 		return 1;
1807c478bd9Sstevel@tonic-gate 	if (bits != BN_num_bits(key->rsa->n)) {
1817c478bd9Sstevel@tonic-gate 		log("Warning: %s, line %d: keysize mismatch for host %s: "
1827c478bd9Sstevel@tonic-gate 		    "actual %d vs. announced %d.",
1837c478bd9Sstevel@tonic-gate 		    filename, linenum, host, BN_num_bits(key->rsa->n), bits);
1847c478bd9Sstevel@tonic-gate 		log("Warning: replace %d with %d in %s, line %d.",
1857c478bd9Sstevel@tonic-gate 		    bits, BN_num_bits(key->rsa->n), filename, linenum);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	return 1;
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Checks whether the given host (which must be in all lowercase) is already
1927c478bd9Sstevel@tonic-gate  * in the list of our known hosts. Returns HOST_OK if the host is known and
1937c478bd9Sstevel@tonic-gate  * has the specified key, HOST_NEW if the host is not known, and HOST_CHANGED
1947c478bd9Sstevel@tonic-gate  * if the host is known but used to have a different host key.
1957c478bd9Sstevel@tonic-gate  *
1967c478bd9Sstevel@tonic-gate  * If no 'key' has been specified and a key of type 'keytype' is known
1977c478bd9Sstevel@tonic-gate  * for the specified host, then HOST_FOUND is returned.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static HostStatus
check_host_in_hostfile_by_key_or_type(const char * filename,const char * host,const Key * key,int keytype,Key * found,int * numret)2017c478bd9Sstevel@tonic-gate check_host_in_hostfile_by_key_or_type(const char *filename,
202*442d23f4Sjp161948     const char *host, const Key *key, int keytype, Key *found, int *numret)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	FILE *f;
2057c478bd9Sstevel@tonic-gate 	char line[8192];
2067c478bd9Sstevel@tonic-gate 	int linenum = 0;
2077c478bd9Sstevel@tonic-gate 	u_int kbits;
208*442d23f4Sjp161948 	char *cp, *cp2, *hashed_host;
2097c478bd9Sstevel@tonic-gate 	HostStatus end_return;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	debug3("check_host_in_hostfile: filename %s", filename);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	/* Open the file containing the list of known hosts. */
2147c478bd9Sstevel@tonic-gate 	f = fopen(filename, "r");
2157c478bd9Sstevel@tonic-gate 	if (!f)
2167c478bd9Sstevel@tonic-gate 		return HOST_NEW;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Return value when the loop terminates.  This is set to
2207c478bd9Sstevel@tonic-gate 	 * HOST_CHANGED if we have seen a different key for the host and have
2217c478bd9Sstevel@tonic-gate 	 * not found the proper one.
2227c478bd9Sstevel@tonic-gate 	 */
2237c478bd9Sstevel@tonic-gate 	end_return = HOST_NEW;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* Go through the file. */
2267c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof(line), f)) {
2277c478bd9Sstevel@tonic-gate 		cp = line;
2287c478bd9Sstevel@tonic-gate 		linenum++;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		/* Skip any leading whitespace, comments and empty lines. */
2317c478bd9Sstevel@tonic-gate 		for (; *cp == ' ' || *cp == '\t'; cp++)
2327c478bd9Sstevel@tonic-gate 			;
2337c478bd9Sstevel@tonic-gate 		if (!*cp || *cp == '#' || *cp == '\n')
2347c478bd9Sstevel@tonic-gate 			continue;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		/* Find the end of the host name portion. */
2377c478bd9Sstevel@tonic-gate 		for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
2387c478bd9Sstevel@tonic-gate 			;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		/* Check if the host name matches. */
241*442d23f4Sjp161948 		if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) {
242*442d23f4Sjp161948 			if (*cp != HASH_DELIM)
2437c478bd9Sstevel@tonic-gate 				continue;
244*442d23f4Sjp161948 			hashed_host = host_hash(host, cp, (u_int) (cp2 - cp));
245*442d23f4Sjp161948 			if (hashed_host == NULL) {
246*442d23f4Sjp161948 				debug("Invalid hashed host line %d of %s",
247*442d23f4Sjp161948 				    linenum, filename);
248*442d23f4Sjp161948 				continue;
249*442d23f4Sjp161948 			}
250*442d23f4Sjp161948 			if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0)
251*442d23f4Sjp161948 				continue;
252*442d23f4Sjp161948 		}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		/* Got a match.  Skip host name. */
2557c478bd9Sstevel@tonic-gate 		cp = cp2;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		/*
2587c478bd9Sstevel@tonic-gate 		 * Extract the key from the line.  This will skip any leading
2597c478bd9Sstevel@tonic-gate 		 * whitespace.  Ignore badly formatted lines.
2607c478bd9Sstevel@tonic-gate 		 */
2617c478bd9Sstevel@tonic-gate 		if (!hostfile_read_key(&cp, &kbits, found))
2627c478bd9Sstevel@tonic-gate 			continue;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		if (numret != NULL)
2657c478bd9Sstevel@tonic-gate 			*numret = linenum;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		if (key == NULL) {
2687c478bd9Sstevel@tonic-gate 			/* we found a key of the requested type */
269*442d23f4Sjp161948 			if (found->type == keytype) {
270*442d23f4Sjp161948 				fclose(f);
2717c478bd9Sstevel@tonic-gate 				return HOST_FOUND;
272*442d23f4Sjp161948 			}
2737c478bd9Sstevel@tonic-gate 			continue;
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		if (!hostfile_check_key(kbits, found, host, filename, linenum))
2777c478bd9Sstevel@tonic-gate 			continue;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		/* Check if the current key is the same as the given key. */
2807c478bd9Sstevel@tonic-gate 		if (key_equal(key, found)) {
2817c478bd9Sstevel@tonic-gate 			/* Ok, they match. */
2827c478bd9Sstevel@tonic-gate 			debug3("check_host_in_hostfile: match line %d", linenum);
2837c478bd9Sstevel@tonic-gate 			fclose(f);
2847c478bd9Sstevel@tonic-gate 			return HOST_OK;
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		/*
2877c478bd9Sstevel@tonic-gate 		 * They do not match.  We will continue to go through the
2887c478bd9Sstevel@tonic-gate 		 * file; however, we note that we will not return that it is
2897c478bd9Sstevel@tonic-gate 		 * new.
2907c478bd9Sstevel@tonic-gate 		 */
2917c478bd9Sstevel@tonic-gate 		end_return = HOST_CHANGED;
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	/* Clear variables and close the file. */
2947c478bd9Sstevel@tonic-gate 	fclose(f);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * Return either HOST_NEW or HOST_CHANGED, depending on whether we
2987c478bd9Sstevel@tonic-gate 	 * saw a different key for the host.
2997c478bd9Sstevel@tonic-gate 	 */
3007c478bd9Sstevel@tonic-gate 	return end_return;
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate HostStatus
check_host_in_hostfile(const char * filename,const char * host,const Key * key,Key * found,int * numret)304*442d23f4Sjp161948 check_host_in_hostfile(const char *filename, const char *host, const Key *key,
3057c478bd9Sstevel@tonic-gate     Key *found, int *numret)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	if (key == NULL)
3087c478bd9Sstevel@tonic-gate 		fatal("no key to look up");
3097c478bd9Sstevel@tonic-gate 	return (check_host_in_hostfile_by_key_or_type(filename, host, key, 0,
3107c478bd9Sstevel@tonic-gate 	    found, numret));
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate int
lookup_key_in_hostfile_by_type(const char * filename,const char * host,int keytype,Key * found,int * numret)3147c478bd9Sstevel@tonic-gate lookup_key_in_hostfile_by_type(const char *filename, const char *host,
3157c478bd9Sstevel@tonic-gate     int keytype, Key *found, int *numret)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	return (check_host_in_hostfile_by_key_or_type(filename, host, NULL,
3187c478bd9Sstevel@tonic-gate 	    keytype, found, numret) == HOST_FOUND);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * Appends an entry to the host file.  Returns false if the entry could not
3237c478bd9Sstevel@tonic-gate  * be appended.
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate int
add_host_to_hostfile(const char * filename,const char * host,const Key * key,int store_hash)327*442d23f4Sjp161948 add_host_to_hostfile(const char *filename, const char *host, const Key *key,
328*442d23f4Sjp161948     int store_hash)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	FILE *f;
3317c478bd9Sstevel@tonic-gate 	int success = 0;
332*442d23f4Sjp161948 	char *hashed_host = NULL;
333*442d23f4Sjp161948 
3347c478bd9Sstevel@tonic-gate 	if (key == NULL)
3357c478bd9Sstevel@tonic-gate 		return 1;	/* XXX ? */
3367c478bd9Sstevel@tonic-gate 	f = fopen(filename, "a");
3377c478bd9Sstevel@tonic-gate 	if (!f)
3387c478bd9Sstevel@tonic-gate 		return 0;
339*442d23f4Sjp161948 
340*442d23f4Sjp161948 	if (store_hash) {
341*442d23f4Sjp161948 		if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
342*442d23f4Sjp161948 			error("add_host_to_hostfile: host_hash failed");
343*442d23f4Sjp161948 			fclose(f);
344*442d23f4Sjp161948 			return 0;
345*442d23f4Sjp161948 		}
346*442d23f4Sjp161948 	}
347*442d23f4Sjp161948 	fprintf(f, "%s ", store_hash ? hashed_host : host);
348*442d23f4Sjp161948 
3497c478bd9Sstevel@tonic-gate 	if (key_write(key, f)) {
3507c478bd9Sstevel@tonic-gate 		success = 1;
3517c478bd9Sstevel@tonic-gate 	} else {
3527c478bd9Sstevel@tonic-gate 		error("add_host_to_hostfile: saving key in %s failed", filename);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	fprintf(f, "\n");
3557c478bd9Sstevel@tonic-gate 	fclose(f);
3567c478bd9Sstevel@tonic-gate 	return success;
3577c478bd9Sstevel@tonic-gate }
358