blob: f58e1d67d04878e7c6278a7c7ea0d0a5ca5d3937 [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 Millerd3a18572000-06-07 19:55:44 +100017RCSID("$OpenBSD: hostfile.c,v 1.19 2000/06/06 19:32:13 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 Millereba71ba2000-04-29 23:57:08 +100042 bits = key_read(ret, &cp);
43 if (bits == 0)
Damien Miller95def091999-11-25 00:26:21 +110044 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Damien Miller95def091999-11-25 00:26:21 +110046 /* Skip trailing whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +110047 for (; *cp == ' ' || *cp == '\t'; cp++)
48 ;
Damien Miller95def091999-11-25 00:26:21 +110049
50 /* Return results. */
51 *cpp = cp;
52 *bitsp = bits;
53 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054}
55
Damien Miller450a7a12000-03-26 13:04:51 +100056int
57auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n)
58{
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}
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
67int
Damien Miller450a7a12000-03-26 13:04:51 +100068hostfile_check_key(int bits, Key *key, const char *host, const char *filename, int linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069{
Damien Miller450a7a12000-03-26 13:04:51 +100070 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL)
71 return 1;
72 if (bits != BN_num_bits(key->rsa->n)) {
Damien Millerbd483e72000-04-30 10:00:53 +100073 log("Warning: %s, line %d: keysize mismatch for host %s: "
Damien Miller450a7a12000-03-26 13:04:51 +100074 "actual %d vs. announced %d.",
75 filename, linenum, host, BN_num_bits(key->rsa->n), bits);
Damien Millerbd483e72000-04-30 10:00:53 +100076 log("Warning: replace %d with %d in %s, line %d.",
Damien Miller450a7a12000-03-26 13:04:51 +100077 bits, BN_num_bits(key->rsa->n), filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078 }
Damien Miller450a7a12000-03-26 13:04:51 +100079 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080}
81
Damien Miller5428f641999-11-25 11:54:57 +110082/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89HostStatus
Damien Miller450a7a12000-03-26 13:04:51 +100090check_host_in_hostfile(const char *filename, const char *host, Key *key, Key *found)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091{
Damien Miller95def091999-11-25 00:26:21 +110092 FILE *f;
93 char line[8192];
94 int linenum = 0;
Damien Miller98c7ad62000-03-09 21:27:49 +110095 unsigned int kbits, hostlen;
Damien Miller95def091999-11-25 00:26:21 +110096 char *cp, *cp2;
97 HostStatus end_return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller450a7a12000-03-26 13:04:51 +100099 if (key == NULL)
100 fatal("no key to look up");
Damien Miller95def091999-11-25 00:26:21 +1100101 /* Open the file containing the list of known hosts. */
102 f = fopen(filename, "r");
103 if (!f)
104 return HOST_NEW;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller95def091999-11-25 00:26:21 +1100106 /* Cache the length of the host name. */
107 hostlen = strlen(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller5428f641999-11-25 11:54:57 +1100109 /*
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 */
Damien Miller95def091999-11-25 00:26:21 +1100114 end_return = HOST_NEW;
Damien Miller7e8e8201999-11-16 13:37:16 +1100115
Damien Miller95def091999-11-25 00:26:21 +1100116 /* Go trough the file. */
117 while (fgets(line, sizeof(line), f)) {
118 cp = line;
119 linenum++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Damien Miller5428f641999-11-25 11:54:57 +1100121 /* Skip any leading whitespace, comments and empty lines. */
122 for (; *cp == ' ' || *cp == '\t'; cp++)
123 ;
Damien Miller95def091999-11-25 00:26:21 +1100124 if (!*cp || *cp == '#' || *cp == '\n')
125 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127 /* Find the end of the host name portion. */
Damien Miller5428f641999-11-25 11:54:57 +1100128 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
129 ;
Damien Miller7e8e8201999-11-16 13:37:16 +1100130
Damien Miller95def091999-11-25 00:26:21 +1100131 /* Check if the host name matches. */
Damien Millerd3a18572000-06-07 19:55:44 +1000132 if (match_hostname(host, cp, (unsigned int) (cp2 - cp)) != 1)
Damien Miller95def091999-11-25 00:26:21 +1100133 continue;
134
135 /* Got a match. Skip host name. */
136 cp = cp2;
137
Damien Miller5428f641999-11-25 11:54:57 +1100138 /*
139 * Extract the key from the line. This will skip any leading
140 * whitespace. Ignore badly formatted lines.
141 */
Damien Miller450a7a12000-03-26 13:04:51 +1000142 if (!hostfile_read_key(&cp, &kbits, found))
143 continue;
144 if (!hostfile_check_key(kbits, found, host, filename, linenum))
Damien Miller95def091999-11-25 00:26:21 +1100145 continue;
146
Damien Miller95def091999-11-25 00:26:21 +1100147 /* Check if the current key is the same as the given key. */
Damien Miller450a7a12000-03-26 13:04:51 +1000148 if (key_equal(key, found)) {
Damien Miller95def091999-11-25 00:26:21 +1100149 /* Ok, they match. */
150 fclose(f);
151 return HOST_OK;
152 }
Damien Miller5428f641999-11-25 11:54:57 +1100153 /*
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 */
Damien Miller95def091999-11-25 00:26:21 +1100158 end_return = HOST_CHANGED;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159 }
Damien Miller95def091999-11-25 00:26:21 +1100160 /* Clear variables and close the file. */
161 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162
Damien Miller5428f641999-11-25 11:54:57 +1100163 /*
164 * Return either HOST_NEW or HOST_CHANGED, depending on whether we
165 * saw a different key for the host.
166 */
Damien Miller95def091999-11-25 00:26:21 +1100167 return end_return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168}
169
Damien Miller5428f641999-11-25 11:54:57 +1100170/*
171 * Appends an entry to the host file. Returns false if the entry could not
172 * be appended.
173 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174
175int
Damien Miller450a7a12000-03-26 13:04:51 +1000176add_host_to_hostfile(const char *filename, const char *host, Key *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177{
Damien Miller95def091999-11-25 00:26:21 +1100178 FILE *f;
Damien Miller450a7a12000-03-26 13:04:51 +1000179 int success = 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000180 if (key == NULL)
Damien Millereba71ba2000-04-29 23:57:08 +1000181 return 1; /* XXX ? */
Damien Miller95def091999-11-25 00:26:21 +1100182 f = fopen(filename, "a");
183 if (!f)
184 return 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000185 fprintf(f, "%s ", host);
186 if (key_write(key, f)) {
Damien Miller450a7a12000-03-26 13:04:51 +1000187 success = 1;
188 } else {
Damien Millereba71ba2000-04-29 23:57:08 +1000189 error("add_host_to_hostfile: saving key in %s failed", filename);
Damien Miller95def091999-11-25 00:26:21 +1100190 }
Damien Millereba71ba2000-04-29 23:57:08 +1000191 fprintf(f, "\n");
Damien Miller95def091999-11-25 00:26:21 +1100192 fclose(f);
Damien Miller450a7a12000-03-26 13:04:51 +1000193 return success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194}