5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Thu Jun 29 07:10:56 1995 ylo
12 * Functions for manipulating the known hosts files.
17 RCSID("$OpenBSD: hostfile.c,v 1.13 2000/02/18 10:20:20 markus Exp $");
23 * Reads a multiple-precision integer in decimal from the buffer, and advances
24 * the pointer. The integer must already be initialized. This function is
25 * permitted to modify the buffer. This leaves *cpp to point just beyond the
26 * last processed (and maybe modified) character. Note that this may modify
27 * the buffer containing the number.
31 auth_rsa_read_bignum(char **cpp, BIGNUM * value)
36 /* Skip any leading whitespace. */
37 for (; *cp == ' ' || *cp == '\t'; cp++)
40 /* Check that it begins with a decimal digit. */
41 if (*cp < '0' || *cp > '9')
44 /* Save starting position. */
47 /* Move forward until all decimal digits skipped. */
48 for (; *cp >= '0' && *cp <= '9'; cp++)
51 /* Save the old terminating character, and replace it by \0. */
55 /* Parse the number. */
56 if (BN_dec2bn(&value, *cpp) == 0)
59 /* Restore old terminating character. */
62 /* Move beyond the number and return success. */
68 * Parses an RSA key (number of bits, e, n) from a string. Moves the pointer
69 * over the key. Skips any whitespace at the beginning and at end.
73 auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n)
78 /* Skip leading whitespace. */
79 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
82 /* Get number of bits. */
83 if (*cp < '0' || *cp > '9')
84 return 0; /* Bad bit count... */
85 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
86 bits = 10 * bits + *cp - '0';
88 /* Get public exponent. */
89 if (!auth_rsa_read_bignum(&cp, e))
92 /* Get public modulus. */
93 if (!auth_rsa_read_bignum(&cp, n))
96 /* Skip trailing whitespace. */
97 for (; *cp == ' ' || *cp == '\t'; cp++)
100 /* Return results. */
107 * Tries to match the host name (which must be in all lowercase) against the
108 * comma-separated sequence of subpatterns (each possibly preceded by ! to
109 * indicate negation). Returns true if there is a positive match; zero
114 match_hostname(const char *host, const char *pattern, unsigned int len)
119 unsigned int i, subi;
122 for (i = 0; i < len;) {
123 /* Check if the subpattern is negated. */
124 if (pattern[i] == '!') {
131 * Extract the subpattern up to a comma or end. Convert the
132 * subpattern to lowercase.
135 i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
137 sub[subi] = isupper(pattern[i]) ? tolower(pattern[i]) : pattern[i];
138 /* If subpattern too long, return failure (no match). */
139 if (subi >= sizeof(sub) - 1)
142 /* If the subpattern was terminated by a comma, skip the comma. */
143 if (i < len && pattern[i] == ',')
146 /* Null-terminate the subpattern. */
149 /* Try to match the subpattern against the host name. */
150 if (match_pattern(host, sub)) {
159 * Return success if got a positive match. If there was a negative
160 * match, we have already returned zero and never get here.
166 * Checks whether the given host (which must be in all lowercase) is already
167 * in the list of our known hosts. Returns HOST_OK if the host is known and
168 * has the specified key, HOST_NEW if the host is not known, and HOST_CHANGED
169 * if the host is known but used to have a different host key.
173 check_host_in_hostfile(const char *filename, const char *host,
174 BIGNUM * e, BIGNUM * n, BIGNUM * ke, BIGNUM * kn)
179 unsigned int kbits, hostlen;
181 HostStatus end_return;
183 /* Open the file containing the list of known hosts. */
184 f = fopen(filename, "r");
188 /* Cache the length of the host name. */
189 hostlen = strlen(host);
192 * Return value when the loop terminates. This is set to
193 * HOST_CHANGED if we have seen a different key for the host and have
194 * not found the proper one.
196 end_return = HOST_NEW;
198 /* Go trough the file. */
199 while (fgets(line, sizeof(line), f)) {
203 /* Skip any leading whitespace, comments and empty lines. */
204 for (; *cp == ' ' || *cp == '\t'; cp++)
206 if (!*cp || *cp == '#' || *cp == '\n')
209 /* Find the end of the host name portion. */
210 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
213 /* Check if the host name matches. */
214 if (!match_hostname(host, cp, (unsigned int) (cp2 - cp)))
217 /* Got a match. Skip host name. */
221 * Extract the key from the line. This will skip any leading
222 * whitespace. Ignore badly formatted lines.
224 if (!auth_rsa_read_key(&cp, &kbits, ke, kn))
227 if (kbits != BN_num_bits(kn)) {
228 error("Warning: %s, line %d: keysize mismatch for host %s: "
229 "actual %d vs. announced %d.",
230 filename, linenum, host, BN_num_bits(kn), kbits);
231 error("Warning: replace %d with %d in %s, line %d.",
232 kbits, BN_num_bits(kn), filename, linenum);
234 /* Check if the current key is the same as the given key. */
235 if (BN_cmp(ke, e) == 0 && BN_cmp(kn, n) == 0) {
236 /* Ok, they match. */
241 * They do not match. We will continue to go through the
242 * file; however, we note that we will not return that it is
245 end_return = HOST_CHANGED;
247 /* Clear variables and close the file. */
251 * Return either HOST_NEW or HOST_CHANGED, depending on whether we
252 * saw a different key for the host.
258 * Appends an entry to the host file. Returns false if the entry could not
263 add_host_to_hostfile(const char *filename, const char *host,
264 BIGNUM * e, BIGNUM * n)
270 /* Open the file for appending. */
271 f = fopen(filename, "a");
275 /* size of modulus 'n' */
276 bits = BN_num_bits(n);
278 /* Print the host name and key to the file. */
279 fprintf(f, "%s %u ", host, bits);
282 error("add_host_to_hostfile: BN_bn2dec(e) failed");
286 fprintf(f, "%s ", buf);
290 error("add_host_to_hostfile: BN_bn2dec(n) failed");
294 fprintf(f, "%s\n", buf);
297 /* Close the file. */