blob: 29efe5656a13d0a59061149e836b75b3c3f79455 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * hostfile.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Thu Jun 29 07:10:56 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Functions for manipulating the known hosts files.
Damien Miller4af51302000-04-16 11:18:38 +100013 *
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Miller4af51302000-04-16 11:18:38 +100017RCSID("$OpenBSD: hostfile.c,v 1.16 2000/04/14 10:30:31 markus Exp $");
Damien Miller450a7a12000-03-26 13:04:51 +100018
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019#include "packet.h"
Damien Miller450a7a12000-03-26 13:04:51 +100020#include "match.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021#include "ssh.h"
Damien Miller5f056372000-04-16 12:31:48 +100022#include <openssl/rsa.h>
23#include <openssl/dsa.h>
Damien Miller450a7a12000-03-26 13:04:51 +100024#include "key.h"
25#include "hostfile.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Miller5428f641999-11-25 11:54:57 +110027/*
Damien Miller450a7a12000-03-26 13:04:51 +100028 * 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.
Damien Miller5428f641999-11-25 11:54:57 +110030 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031
32int
Damien Miller450a7a12000-03-26 13:04:51 +100033hostfile_read_key(char **cpp, unsigned int *bitsp, Key *ret)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034{
Damien Miller95def091999-11-25 00:26:21 +110035 unsigned int bits;
36 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
Damien Miller95def091999-11-25 00:26:21 +110038 /* Skip leading whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +110039 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
40 ;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
Damien Miller95def091999-11-25 00:26:21 +110042 /* 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';
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
Damien Miller450a7a12000-03-26 13:04:51 +100048 if (!key_read(ret, bits, &cp))
Damien Miller95def091999-11-25 00:26:21 +110049 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Damien Miller95def091999-11-25 00:26:21 +110051 /* Skip trailing whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +110052 for (; *cp == ' ' || *cp == '\t'; cp++)
53 ;
Damien Miller95def091999-11-25 00:26:21 +110054
55 /* Return results. */
56 *cpp = cp;
57 *bitsp = bits;
58 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059}
60
Damien Miller450a7a12000-03-26 13:04:51 +100061int
62auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n)
63{
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}
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
72int
Damien Miller450a7a12000-03-26 13:04:51 +100073hostfile_check_key(int bits, Key *key, const char *host, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074{
Damien Miller450a7a12000-03-26 13:04:51 +100075 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);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083 }
Damien Miller450a7a12000-03-26 13:04:51 +100084 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085}
86
Damien Miller5428f641999-11-25 11:54:57 +110087/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94HostStatus
Damien Miller450a7a12000-03-26 13:04:51 +100095check_host_in_hostfile(const char *filename, const char *host, Key *key, Key *found)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096{
Damien Miller95def091999-11-25 00:26:21 +110097 FILE *f;
98 char line[8192];
99 int linenum = 0;
Damien Miller98c7ad62000-03-09 21:27:49 +1100100 unsigned int kbits, hostlen;
Damien Miller95def091999-11-25 00:26:21 +1100101 char *cp, *cp2;
102 HostStatus end_return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
Damien Miller450a7a12000-03-26 13:04:51 +1000104 if (key == NULL)
105 fatal("no key to look up");
Damien Miller95def091999-11-25 00:26:21 +1100106 /* Open the file containing the list of known hosts. */
107 f = fopen(filename, "r");
108 if (!f)
109 return HOST_NEW;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Miller95def091999-11-25 00:26:21 +1100111 /* Cache the length of the host name. */
112 hostlen = strlen(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller5428f641999-11-25 11:54:57 +1100114 /*
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 */
Damien Miller95def091999-11-25 00:26:21 +1100119 end_return = HOST_NEW;
Damien Miller7e8e8201999-11-16 13:37:16 +1100120
Damien Miller95def091999-11-25 00:26:21 +1100121 /* Go trough the file. */
122 while (fgets(line, sizeof(line), f)) {
123 cp = line;
124 linenum++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller5428f641999-11-25 11:54:57 +1100126 /* Skip any leading whitespace, comments and empty lines. */
127 for (; *cp == ' ' || *cp == '\t'; cp++)
128 ;
Damien Miller95def091999-11-25 00:26:21 +1100129 if (!*cp || *cp == '#' || *cp == '\n')
130 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Damien Miller95def091999-11-25 00:26:21 +1100132 /* Find the end of the host name portion. */
Damien Miller5428f641999-11-25 11:54:57 +1100133 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
134 ;
Damien Miller7e8e8201999-11-16 13:37:16 +1100135
Damien Miller95def091999-11-25 00:26:21 +1100136 /* 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
Damien Miller5428f641999-11-25 11:54:57 +1100143 /*
144 * Extract the key from the line. This will skip any leading
145 * whitespace. Ignore badly formatted lines.
146 */
Damien Miller450a7a12000-03-26 13:04:51 +1000147 if (!hostfile_read_key(&cp, &kbits, found))
148 continue;
149 if (!hostfile_check_key(kbits, found, host, filename, linenum))
Damien Miller95def091999-11-25 00:26:21 +1100150 continue;
151
Damien Miller95def091999-11-25 00:26:21 +1100152 /* Check if the current key is the same as the given key. */
Damien Miller450a7a12000-03-26 13:04:51 +1000153 if (key_equal(key, found)) {
Damien Miller95def091999-11-25 00:26:21 +1100154 /* Ok, they match. */
155 fclose(f);
156 return HOST_OK;
157 }
Damien Miller5428f641999-11-25 11:54:57 +1100158 /*
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 */
Damien Miller95def091999-11-25 00:26:21 +1100163 end_return = HOST_CHANGED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164 }
Damien Miller95def091999-11-25 00:26:21 +1100165 /* Clear variables and close the file. */
166 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
Damien Miller5428f641999-11-25 11:54:57 +1100168 /*
169 * Return either HOST_NEW or HOST_CHANGED, depending on whether we
170 * saw a different key for the host.
171 */
Damien Miller95def091999-11-25 00:26:21 +1100172 return end_return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Damien Miller5428f641999-11-25 11:54:57 +1100175/*
176 * Appends an entry to the host file. Returns false if the entry could not
177 * be appended.
178 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
180int
Damien Miller450a7a12000-03-26 13:04:51 +1000181add_host_to_hostfile(const char *filename, const char *host, Key *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182{
Damien Miller95def091999-11-25 00:26:21 +1100183 FILE *f;
Damien Miller450a7a12000-03-26 13:04:51 +1000184 int success = 0;
185
186 if (key == NULL)
187 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller95def091999-11-25 00:26:21 +1100189 /* Open the file for appending. */
190 f = fopen(filename, "a");
191 if (!f)
192 return 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100193
Damien Miller450a7a12000-03-26 13:04:51 +1000194 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");
Damien Miller95def091999-11-25 00:26:21 +1100200 }
Damien Miller95def091999-11-25 00:26:21 +1100201
202 /* Close the file. */
203 fclose(f);
Damien Miller450a7a12000-03-26 13:04:51 +1000204 return success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205}