Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * hostfile.c |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 10 | * Created: Thu Jun 29 07:10:56 1995 ylo |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 11 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | * Functions for manipulating the known hosts files. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 13 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 14 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "includes.h" |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 17 | RCSID("$OpenBSD: hostfile.c,v 1.16 2000/04/14 10:30:31 markus Exp $"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 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 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 29 | |
| 30 | #include "packet.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 31 | #include "match.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | #include "ssh.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 33 | #include "key.h" |
| 34 | #include "hostfile.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 35 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 36 | /* |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 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. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 39 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | |
| 41 | int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 42 | hostfile_read_key(char **cpp, unsigned int *bitsp, Key *ret) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 43 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 44 | unsigned int bits; |
| 45 | char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 46 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 47 | /* Skip leading whitespace. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 48 | for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) |
| 49 | ; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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'; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 56 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 57 | if (!key_read(ret, bits, &cp)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 60 | /* Skip trailing whitespace. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 61 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 62 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 63 | |
| 64 | /* Return results. */ |
| 65 | *cpp = cp; |
| 66 | *bitsp = bits; |
| 67 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | } |
| 69 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 70 | int |
| 71 | auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n) |
| 72 | { |
| 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 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | |
| 81 | int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 82 | hostfile_check_key(int bits, Key *key, const char *host, const char *filename, int linenum) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | { |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 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); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 93 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | } |
| 95 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 102 | |
| 103 | HostStatus |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 104 | check_host_in_hostfile(const char *filename, const char *host, Key *key, Key *found) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 106 | FILE *f; |
| 107 | char line[8192]; |
| 108 | int linenum = 0; |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 109 | unsigned int kbits, hostlen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | char *cp, *cp2; |
| 111 | HostStatus end_return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 112 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 113 | if (key == NULL) |
| 114 | fatal("no key to look up"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | /* Open the file containing the list of known hosts. */ |
| 116 | f = fopen(filename, "r"); |
| 117 | if (!f) |
| 118 | return HOST_NEW; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 119 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 120 | /* Cache the length of the host name. */ |
| 121 | hostlen = strlen(host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 128 | end_return = HOST_NEW; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 129 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | /* Go trough the file. */ |
| 131 | while (fgets(line, sizeof(line), f)) { |
| 132 | cp = line; |
| 133 | linenum++; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 134 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 135 | /* Skip any leading whitespace, comments and empty lines. */ |
| 136 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 137 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | if (!*cp || *cp == '#' || *cp == '\n') |
| 139 | continue; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | /* Find the end of the host name portion. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 142 | for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) |
| 143 | ; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 144 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 152 | /* |
| 153 | * Extract the key from the line. This will skip any leading |
| 154 | * whitespace. Ignore badly formatted lines. |
| 155 | */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 156 | if (!hostfile_read_key(&cp, &kbits, found)) |
| 157 | continue; |
| 158 | if (!hostfile_check_key(kbits, found, host, filename, linenum)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 159 | continue; |
| 160 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | /* Check if the current key is the same as the given key. */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 162 | if (key_equal(key, found)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | /* Ok, they match. */ |
| 164 | fclose(f); |
| 165 | return HOST_OK; |
| 166 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 172 | end_return = HOST_CHANGED; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 173 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 174 | /* Clear variables and close the file. */ |
| 175 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 176 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 177 | /* |
| 178 | * Return either HOST_NEW or HOST_CHANGED, depending on whether we |
| 179 | * saw a different key for the host. |
| 180 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 181 | return end_return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 182 | } |
| 183 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 184 | /* |
| 185 | * Appends an entry to the host file. Returns false if the entry could not |
| 186 | * be appended. |
| 187 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 188 | |
| 189 | int |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 190 | add_host_to_hostfile(const char *filename, const char *host, Key *key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 191 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 192 | FILE *f; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 193 | int success = 0; |
| 194 | |
| 195 | if (key == NULL) |
| 196 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 197 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 198 | /* Open the file for appending. */ |
| 199 | f = fopen(filename, "a"); |
| 200 | if (!f) |
| 201 | return 0; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 202 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 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"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 209 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | |
| 211 | /* Close the file. */ |
| 212 | fclose(f); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 213 | return success; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 214 | } |