]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
6ae2364d | 2 | * |
5260325f | 3 | * hostfile.c |
6ae2364d | 4 | * |
5260325f | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
6ae2364d | 6 | * |
5260325f | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
8 | * All rights reserved | |
6ae2364d | 9 | * |
5260325f | 10 | * Created: Thu Jun 29 07:10:56 1995 ylo |
6ae2364d | 11 | * |
5260325f | 12 | * Functions for manipulating the known hosts files. |
6ae2364d | 13 | * |
5260325f | 14 | */ |
8efc0c15 | 15 | |
16 | #include "includes.h" | |
6ae2364d | 17 | RCSID("$OpenBSD: hostfile.c,v 1.16 2000/04/14 10:30:31 markus Exp $"); |
4fe2af09 | 18 | |
19 | #ifdef HAVE_OPENSSL | |
20 | #include <openssl/bn.h> | |
21 | #include <openssl/rsa.h> | |
22 | #include <openssl/dsa.h> | |
23 | #endif | |
24 | #ifdef HAVE_SSL | |
25 | #include <ssl/bn.h> | |
26 | #include <ssl/rsa.h> | |
27 | #include <ssl/dsa.h> | |
28 | #endif | |
8efc0c15 | 29 | |
30 | #include "packet.h" | |
4fe2af09 | 31 | #include "match.h" |
8efc0c15 | 32 | #include "ssh.h" |
4fe2af09 | 33 | #include "key.h" |
34 | #include "hostfile.h" | |
8efc0c15 | 35 | |
aa3378df | 36 | /* |
4fe2af09 | 37 | * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the |
38 | * pointer over the key. Skips any whitespace at the beginning and at end. | |
aa3378df | 39 | */ |
8efc0c15 | 40 | |
41 | int | |
4fe2af09 | 42 | hostfile_read_key(char **cpp, unsigned int *bitsp, Key *ret) |
8efc0c15 | 43 | { |
5260325f | 44 | unsigned int bits; |
45 | char *cp; | |
46 | ||
47 | /* Skip leading whitespace. */ | |
aa3378df | 48 | for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) |
49 | ; | |
5260325f | 50 | |
51 | /* Get number of bits. */ | |
52 | if (*cp < '0' || *cp > '9') | |
53 | return 0; /* Bad bit count... */ | |
54 | for (bits = 0; *cp >= '0' && *cp <= '9'; cp++) | |
55 | bits = 10 * bits + *cp - '0'; | |
56 | ||
4fe2af09 | 57 | if (!key_read(ret, bits, &cp)) |
5260325f | 58 | return 0; |
59 | ||
60 | /* Skip trailing whitespace. */ | |
aa3378df | 61 | for (; *cp == ' ' || *cp == '\t'; cp++) |
62 | ; | |
5260325f | 63 | |
64 | /* Return results. */ | |
65 | *cpp = cp; | |
66 | *bitsp = bits; | |
67 | return 1; | |
8efc0c15 | 68 | } |
69 | ||
8efc0c15 | 70 | int |
4fe2af09 | 71 | auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n) |
8efc0c15 | 72 | { |
4fe2af09 | 73 | Key *k = key_new(KEY_RSA); |
74 | int ret = hostfile_read_key(cpp, bitsp, k); | |
75 | BN_copy(e, k->rsa->e); | |
76 | BN_copy(n, k->rsa->n); | |
77 | key_free(k); | |
78 | return ret; | |
79 | } | |
5260325f | 80 | |
4fe2af09 | 81 | int |
82 | hostfile_check_key(int bits, Key *key, const char *host, const char *filename, int linenum) | |
83 | { | |
84 | if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) | |
85 | return 1; | |
86 | if (bits != BN_num_bits(key->rsa->n)) { | |
87 | error("Warning: %s, line %d: keysize mismatch for host %s: " | |
88 | "actual %d vs. announced %d.", | |
89 | filename, linenum, host, BN_num_bits(key->rsa->n), bits); | |
90 | error("Warning: replace %d with %d in %s, line %d.", | |
91 | bits, BN_num_bits(key->rsa->n), filename, linenum); | |
8efc0c15 | 92 | } |
4fe2af09 | 93 | return 1; |
8efc0c15 | 94 | } |
95 | ||
aa3378df | 96 | /* |
97 | * Checks whether the given host (which must be in all lowercase) is already | |
98 | * in the list of our known hosts. Returns HOST_OK if the host is known and | |
99 | * has the specified key, HOST_NEW if the host is not known, and HOST_CHANGED | |
100 | * if the host is known but used to have a different host key. | |
101 | */ | |
8efc0c15 | 102 | |
103 | HostStatus | |
4fe2af09 | 104 | check_host_in_hostfile(const char *filename, const char *host, Key *key, Key *found) |
8efc0c15 | 105 | { |
5260325f | 106 | FILE *f; |
107 | char line[8192]; | |
108 | int linenum = 0; | |
c8d54615 | 109 | unsigned int kbits, hostlen; |
5260325f | 110 | char *cp, *cp2; |
111 | HostStatus end_return; | |
112 | ||
4fe2af09 | 113 | if (key == NULL) |
114 | fatal("no key to look up"); | |
5260325f | 115 | /* Open the file containing the list of known hosts. */ |
116 | f = fopen(filename, "r"); | |
117 | if (!f) | |
118 | return HOST_NEW; | |
119 | ||
120 | /* Cache the length of the host name. */ | |
121 | hostlen = strlen(host); | |
122 | ||
aa3378df | 123 | /* |
124 | * Return value when the loop terminates. This is set to | |
125 | * HOST_CHANGED if we have seen a different key for the host and have | |
126 | * not found the proper one. | |
127 | */ | |
5260325f | 128 | end_return = HOST_NEW; |
129 | ||
5260325f | 130 | /* Go trough the file. */ |
131 | while (fgets(line, sizeof(line), f)) { | |
132 | cp = line; | |
133 | linenum++; | |
134 | ||
aa3378df | 135 | /* Skip any leading whitespace, comments and empty lines. */ |
136 | for (; *cp == ' ' || *cp == '\t'; cp++) | |
137 | ; | |
5260325f | 138 | if (!*cp || *cp == '#' || *cp == '\n') |
139 | continue; | |
140 | ||
141 | /* Find the end of the host name portion. */ | |
aa3378df | 142 | for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) |
143 | ; | |
5260325f | 144 | |
145 | /* Check if the host name matches. */ | |
146 | if (!match_hostname(host, cp, (unsigned int) (cp2 - cp))) | |
147 | continue; | |
148 | ||
149 | /* Got a match. Skip host name. */ | |
150 | cp = cp2; | |
151 | ||
aa3378df | 152 | /* |
153 | * Extract the key from the line. This will skip any leading | |
154 | * whitespace. Ignore badly formatted lines. | |
155 | */ | |
4fe2af09 | 156 | if (!hostfile_read_key(&cp, &kbits, found)) |
157 | continue; | |
158 | if (!hostfile_check_key(kbits, found, host, filename, linenum)) | |
5260325f | 159 | continue; |
160 | ||
5260325f | 161 | /* Check if the current key is the same as the given key. */ |
4fe2af09 | 162 | if (key_equal(key, found)) { |
5260325f | 163 | /* Ok, they match. */ |
164 | fclose(f); | |
165 | return HOST_OK; | |
166 | } | |
aa3378df | 167 | /* |
168 | * They do not match. We will continue to go through the | |
169 | * file; however, we note that we will not return that it is | |
170 | * new. | |
171 | */ | |
5260325f | 172 | end_return = HOST_CHANGED; |
8efc0c15 | 173 | } |
5260325f | 174 | /* Clear variables and close the file. */ |
175 | fclose(f); | |
176 | ||
aa3378df | 177 | /* |
178 | * Return either HOST_NEW or HOST_CHANGED, depending on whether we | |
179 | * saw a different key for the host. | |
180 | */ | |
5260325f | 181 | return end_return; |
8efc0c15 | 182 | } |
183 | ||
aa3378df | 184 | /* |
185 | * Appends an entry to the host file. Returns false if the entry could not | |
186 | * be appended. | |
187 | */ | |
8efc0c15 | 188 | |
189 | int | |
4fe2af09 | 190 | add_host_to_hostfile(const char *filename, const char *host, Key *key) |
8efc0c15 | 191 | { |
5260325f | 192 | FILE *f; |
4fe2af09 | 193 | int success = 0; |
194 | ||
195 | if (key == NULL) | |
196 | return 1; | |
5260325f | 197 | |
198 | /* Open the file for appending. */ | |
199 | f = fopen(filename, "a"); | |
200 | if (!f) | |
201 | return 0; | |
202 | ||
4fe2af09 | 203 | fprintf(f, "%s ", host); |
204 | if (key_write(key, f)) { | |
205 | fprintf(f, "\n"); | |
206 | success = 1; | |
207 | } else { | |
208 | error("add_host_to_hostfile: saving key failed"); | |
5260325f | 209 | } |
5260325f | 210 | |
211 | /* Close the file. */ | |
212 | fclose(f); | |
4fe2af09 | 213 | return success; |
8efc0c15 | 214 | } |