1 /* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * Functions for manipulating the known hosts files. 6 * 7 * As far as I am concerned, the code I have written for this software 8 * can be used freely for any purpose. Any derived versions of this 9 * software must be clearly marked as such, and if the derived work is 10 * incompatible with the protocol description in the RFC file, it must be 11 * called by a name other than "ssh" or "Secure Shell". 12 * 13 * 14 * Copyright (c) 1999,2000 Markus Friedl. All rights reserved. 15 * Copyright (c) 1999 Niels Provos. All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include "includes.h" 39 RCSID("$OpenBSD: hostfile.c,v 1.20 2000/09/07 20:27:51 deraadt Exp $"); 40 RCSID("$FreeBSD$"); 41 42 #include "packet.h" 43 #include "match.h" 44 #include "ssh.h" 45 #include <openssl/rsa.h> 46 #include <openssl/dsa.h> 47 #include "key.h" 48 #include "hostfile.h" 49 50 /* 51 * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the 52 * pointer over the key. Skips any whitespace at the beginning and at end. 53 */ 54 55 int 56 hostfile_read_key(char **cpp, unsigned int *bitsp, Key *ret) 57 { 58 unsigned int bits; 59 char *cp; 60 61 /* Skip leading whitespace. */ 62 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) 63 ; 64 65 bits = key_read(ret, &cp); 66 if (bits == 0) 67 return 0; 68 69 /* Skip trailing whitespace. */ 70 for (; *cp == ' ' || *cp == '\t'; cp++) 71 ; 72 73 /* Return results. */ 74 *cpp = cp; 75 *bitsp = bits; 76 return 1; 77 } 78 79 int 80 auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n) 81 { 82 Key *k = key_new(KEY_RSA); 83 int ret = hostfile_read_key(cpp, bitsp, k); 84 BN_copy(e, k->rsa->e); 85 BN_copy(n, k->rsa->n); 86 key_free(k); 87 return ret; 88 } 89 90 int 91 hostfile_check_key(int bits, Key *key, const char *host, const char *filename, int linenum) 92 { 93 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) 94 return 1; 95 if (bits != BN_num_bits(key->rsa->n)) { 96 log("Warning: %s, line %d: keysize mismatch for host %s: " 97 "actual %d vs. announced %d.", 98 filename, linenum, host, BN_num_bits(key->rsa->n), bits); 99 log("Warning: replace %d with %d in %s, line %d.", 100 bits, BN_num_bits(key->rsa->n), filename, linenum); 101 } 102 return 1; 103 } 104 105 /* 106 * Checks whether the given host (which must be in all lowercase) is already 107 * in the list of our known hosts. Returns HOST_OK if the host is known and 108 * has the specified key, HOST_NEW if the host is not known, and HOST_CHANGED 109 * if the host is known but used to have a different host key. 110 */ 111 112 HostStatus 113 check_host_in_hostfile(const char *filename, const char *host, Key *key, Key *found) 114 { 115 FILE *f; 116 char line[8192]; 117 int linenum = 0; 118 unsigned int kbits, hostlen; 119 char *cp, *cp2; 120 HostStatus end_return; 121 122 if (key == NULL) 123 fatal("no key to look up"); 124 /* Open the file containing the list of known hosts. */ 125 f = fopen(filename, "r"); 126 if (!f) 127 return HOST_NEW; 128 129 /* Cache the length of the host name. */ 130 hostlen = strlen(host); 131 132 /* 133 * Return value when the loop terminates. This is set to 134 * HOST_CHANGED if we have seen a different key for the host and have 135 * not found the proper one. 136 */ 137 end_return = HOST_NEW; 138 139 /* Go trough the file. */ 140 while (fgets(line, sizeof(line), f)) { 141 cp = line; 142 linenum++; 143 144 /* Skip any leading whitespace, comments and empty lines. */ 145 for (; *cp == ' ' || *cp == '\t'; cp++) 146 ; 147 if (!*cp || *cp == '#' || *cp == '\n') 148 continue; 149 150 /* Find the end of the host name portion. */ 151 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) 152 ; 153 154 /* Check if the host name matches. */ 155 if (match_hostname(host, cp, (unsigned int) (cp2 - cp)) != 1) 156 continue; 157 158 /* Got a match. Skip host name. */ 159 cp = cp2; 160 161 /* 162 * Extract the key from the line. This will skip any leading 163 * whitespace. Ignore badly formatted lines. 164 */ 165 if (!hostfile_read_key(&cp, &kbits, found)) 166 continue; 167 if (!hostfile_check_key(kbits, found, host, filename, linenum)) 168 continue; 169 170 /* Check if the current key is the same as the given key. */ 171 if (key_equal(key, found)) { 172 /* Ok, they match. */ 173 fclose(f); 174 return HOST_OK; 175 } 176 /* 177 * They do not match. We will continue to go through the 178 * file; however, we note that we will not return that it is 179 * new. 180 */ 181 end_return = HOST_CHANGED; 182 } 183 /* Clear variables and close the file. */ 184 fclose(f); 185 186 /* 187 * Return either HOST_NEW or HOST_CHANGED, depending on whether we 188 * saw a different key for the host. 189 */ 190 return end_return; 191 } 192 193 /* 194 * Appends an entry to the host file. Returns false if the entry could not 195 * be appended. 196 */ 197 198 int 199 add_host_to_hostfile(const char *filename, const char *host, Key *key) 200 { 201 FILE *f; 202 int success = 0; 203 if (key == NULL) 204 return 1; /* XXX ? */ 205 f = fopen(filename, "a"); 206 if (!f) 207 return 0; 208 fprintf(f, "%s ", host); 209 if (key_write(key, f)) { 210 success = 1; 211 } else { 212 error("add_host_to_hostfile: saving key in %s failed", filename); 213 } 214 fprintf(f, "\n"); 215 fclose(f); 216 return success; 217 } 218