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