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