1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3*0Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4*0Sstevel@tonic-gate  *                    All rights reserved
5*0Sstevel@tonic-gate  * Functions for manipulating the known hosts files.
6*0Sstevel@tonic-gate  *
7*0Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
8*0Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
9*0Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
10*0Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
11*0Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
15*0Sstevel@tonic-gate  * Copyright (c) 1999 Niels Provos.  All rights reserved.
16*0Sstevel@tonic-gate  *
17*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
18*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
19*0Sstevel@tonic-gate  * are met:
20*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
21*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
22*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
23*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
24*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27*0Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29*0Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30*0Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32*0Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33*0Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*0Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35*0Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "includes.h"
39*0Sstevel@tonic-gate RCSID("$OpenBSD: hostfile.c,v 1.30 2002/07/24 16:11:18 markus Exp $");
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include "packet.h"
44*0Sstevel@tonic-gate #include "match.h"
45*0Sstevel@tonic-gate #include "key.h"
46*0Sstevel@tonic-gate #include "hostfile.h"
47*0Sstevel@tonic-gate #include "log.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * Parses an RSA (number of bits, e, n) or DSA key from a string.  Moves the
51*0Sstevel@tonic-gate  * pointer over the key.  Skips any whitespace at the beginning and at end.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate int
55*0Sstevel@tonic-gate hostfile_read_key(char **cpp, u_int *bitsp, Key *ret)
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	char *cp;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	/* Skip leading whitespace. */
60*0Sstevel@tonic-gate 	for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
61*0Sstevel@tonic-gate 		;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	if (key_read(ret, &cp) != 1)
64*0Sstevel@tonic-gate 		return 0;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	/* Skip trailing whitespace. */
67*0Sstevel@tonic-gate 	for (; *cp == ' ' || *cp == '\t'; cp++)
68*0Sstevel@tonic-gate 		;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	/* Return results. */
71*0Sstevel@tonic-gate 	*cpp = cp;
72*0Sstevel@tonic-gate 	*bitsp = key_size(ret);
73*0Sstevel@tonic-gate 	return 1;
74*0Sstevel@tonic-gate }
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate static int
77*0Sstevel@tonic-gate hostfile_check_key(int bits, Key *key, const char *host, const char *filename, int linenum)
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
80*0Sstevel@tonic-gate 		return 1;
81*0Sstevel@tonic-gate 	if (bits != BN_num_bits(key->rsa->n)) {
82*0Sstevel@tonic-gate 		log("Warning: %s, line %d: keysize mismatch for host %s: "
83*0Sstevel@tonic-gate 		    "actual %d vs. announced %d.",
84*0Sstevel@tonic-gate 		    filename, linenum, host, BN_num_bits(key->rsa->n), bits);
85*0Sstevel@tonic-gate 		log("Warning: replace %d with %d in %s, line %d.",
86*0Sstevel@tonic-gate 		    bits, BN_num_bits(key->rsa->n), filename, linenum);
87*0Sstevel@tonic-gate 	}
88*0Sstevel@tonic-gate 	return 1;
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * Checks whether the given host (which must be in all lowercase) is already
93*0Sstevel@tonic-gate  * in the list of our known hosts. Returns HOST_OK if the host is known and
94*0Sstevel@tonic-gate  * has the specified key, HOST_NEW if the host is not known, and HOST_CHANGED
95*0Sstevel@tonic-gate  * if the host is known but used to have a different host key.
96*0Sstevel@tonic-gate  *
97*0Sstevel@tonic-gate  * If no 'key' has been specified and a key of type 'keytype' is known
98*0Sstevel@tonic-gate  * for the specified host, then HOST_FOUND is returned.
99*0Sstevel@tonic-gate  */
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate static HostStatus
102*0Sstevel@tonic-gate check_host_in_hostfile_by_key_or_type(const char *filename,
103*0Sstevel@tonic-gate     const char *host, Key *key, int keytype, Key *found, int *numret)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	FILE *f;
106*0Sstevel@tonic-gate 	char line[8192];
107*0Sstevel@tonic-gate 	int linenum = 0;
108*0Sstevel@tonic-gate 	u_int kbits;
109*0Sstevel@tonic-gate 	char *cp, *cp2;
110*0Sstevel@tonic-gate 	HostStatus end_return;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	debug3("check_host_in_hostfile: filename %s", filename);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/* Open the file containing the list of known hosts. */
115*0Sstevel@tonic-gate 	f = fopen(filename, "r");
116*0Sstevel@tonic-gate 	if (!f)
117*0Sstevel@tonic-gate 		return HOST_NEW;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/*
120*0Sstevel@tonic-gate 	 * Return value when the loop terminates.  This is set to
121*0Sstevel@tonic-gate 	 * HOST_CHANGED if we have seen a different key for the host and have
122*0Sstevel@tonic-gate 	 * not found the proper one.
123*0Sstevel@tonic-gate 	 */
124*0Sstevel@tonic-gate 	end_return = HOST_NEW;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/* Go through the file. */
127*0Sstevel@tonic-gate 	while (fgets(line, sizeof(line), f)) {
128*0Sstevel@tonic-gate 		cp = line;
129*0Sstevel@tonic-gate 		linenum++;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 		/* Skip any leading whitespace, comments and empty lines. */
132*0Sstevel@tonic-gate 		for (; *cp == ' ' || *cp == '\t'; cp++)
133*0Sstevel@tonic-gate 			;
134*0Sstevel@tonic-gate 		if (!*cp || *cp == '#' || *cp == '\n')
135*0Sstevel@tonic-gate 			continue;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 		/* Find the end of the host name portion. */
138*0Sstevel@tonic-gate 		for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
139*0Sstevel@tonic-gate 			;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 		/* Check if the host name matches. */
142*0Sstevel@tonic-gate 		if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1)
143*0Sstevel@tonic-gate 			continue;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 		/* Got a match.  Skip host name. */
146*0Sstevel@tonic-gate 		cp = cp2;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		/*
149*0Sstevel@tonic-gate 		 * Extract the key from the line.  This will skip any leading
150*0Sstevel@tonic-gate 		 * whitespace.  Ignore badly formatted lines.
151*0Sstevel@tonic-gate 		 */
152*0Sstevel@tonic-gate 		if (!hostfile_read_key(&cp, &kbits, found))
153*0Sstevel@tonic-gate 			continue;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 		if (numret != NULL)
156*0Sstevel@tonic-gate 			*numret = linenum;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		if (key == NULL) {
159*0Sstevel@tonic-gate 			/* we found a key of the requested type */
160*0Sstevel@tonic-gate 			if (found->type == keytype)
161*0Sstevel@tonic-gate 				return HOST_FOUND;
162*0Sstevel@tonic-gate 			continue;
163*0Sstevel@tonic-gate 		}
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 		if (!hostfile_check_key(kbits, found, host, filename, linenum))
166*0Sstevel@tonic-gate 			continue;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 		/* Check if the current key is the same as the given key. */
169*0Sstevel@tonic-gate 		if (key_equal(key, found)) {
170*0Sstevel@tonic-gate 			/* Ok, they match. */
171*0Sstevel@tonic-gate 			debug3("check_host_in_hostfile: match line %d", linenum);
172*0Sstevel@tonic-gate 			fclose(f);
173*0Sstevel@tonic-gate 			return HOST_OK;
174*0Sstevel@tonic-gate 		}
175*0Sstevel@tonic-gate 		/*
176*0Sstevel@tonic-gate 		 * They do not match.  We will continue to go through the
177*0Sstevel@tonic-gate 		 * file; however, we note that we will not return that it is
178*0Sstevel@tonic-gate 		 * new.
179*0Sstevel@tonic-gate 		 */
180*0Sstevel@tonic-gate 		end_return = HOST_CHANGED;
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 	/* Clear variables and close the file. */
183*0Sstevel@tonic-gate 	fclose(f);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	/*
186*0Sstevel@tonic-gate 	 * Return either HOST_NEW or HOST_CHANGED, depending on whether we
187*0Sstevel@tonic-gate 	 * saw a different key for the host.
188*0Sstevel@tonic-gate 	 */
189*0Sstevel@tonic-gate 	return end_return;
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate HostStatus
193*0Sstevel@tonic-gate check_host_in_hostfile(const char *filename, const char *host, Key *key,
194*0Sstevel@tonic-gate     Key *found, int *numret)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	if (key == NULL)
197*0Sstevel@tonic-gate 		fatal("no key to look up");
198*0Sstevel@tonic-gate 	return (check_host_in_hostfile_by_key_or_type(filename, host, key, 0,
199*0Sstevel@tonic-gate 	    found, numret));
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate int
203*0Sstevel@tonic-gate lookup_key_in_hostfile_by_type(const char *filename, const char *host,
204*0Sstevel@tonic-gate     int keytype, Key *found, int *numret)
205*0Sstevel@tonic-gate {
206*0Sstevel@tonic-gate 	return (check_host_in_hostfile_by_key_or_type(filename, host, NULL,
207*0Sstevel@tonic-gate 	    keytype, found, numret) == HOST_FOUND);
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate /*
211*0Sstevel@tonic-gate  * Appends an entry to the host file.  Returns false if the entry could not
212*0Sstevel@tonic-gate  * be appended.
213*0Sstevel@tonic-gate  */
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate int
216*0Sstevel@tonic-gate add_host_to_hostfile(const char *filename, const char *host, Key *key)
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 	FILE *f;
219*0Sstevel@tonic-gate 	int success = 0;
220*0Sstevel@tonic-gate 	if (key == NULL)
221*0Sstevel@tonic-gate 		return 1;	/* XXX ? */
222*0Sstevel@tonic-gate 	f = fopen(filename, "a");
223*0Sstevel@tonic-gate 	if (!f)
224*0Sstevel@tonic-gate 		return 0;
225*0Sstevel@tonic-gate 	fprintf(f, "%s ", host);
226*0Sstevel@tonic-gate 	if (key_write(key, f)) {
227*0Sstevel@tonic-gate 		success = 1;
228*0Sstevel@tonic-gate 	} else {
229*0Sstevel@tonic-gate 		error("add_host_to_hostfile: saving key in %s failed", filename);
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 	fprintf(f, "\n");
232*0Sstevel@tonic-gate 	fclose(f);
233*0Sstevel@tonic-gate 	return success;
234*0Sstevel@tonic-gate }
235