dtucker@openbsd.org | 696fb42 | 2019-07-07 01:05:00 +0000 | [diff] [blame] | 1 | /* $OpenBSD: hostfile.c,v 1.76 2019/07/07 01:05:00 dtucker Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Functions for manipulating the known hosts files. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
| 13 | * |
| 14 | * |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 15 | * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 16 | * Copyright (c) 1999 Niels Provos. All rights reserved. |
| 17 | * |
| 18 | * Redistribution and use in source and binary forms, with or without |
| 19 | * modification, are permitted provided that the following conditions |
| 20 | * are met: |
| 21 | * 1. Redistributions of source code must retain the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer. |
| 23 | * 2. Redistributions in binary form must reproduce the above copyright |
| 24 | * notice, this list of conditions and the following disclaimer in the |
| 25 | * documentation and/or other materials provided with the distribution. |
| 26 | * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 28 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 29 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 30 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 31 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 32 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 34 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 35 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 36 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | |
| 39 | #include "includes.h" |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 40 | |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 41 | #include <sys/types.h> |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 42 | #include <sys/stat.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 43 | |
| 44 | #include <netinet/in.h> |
| 45 | |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 46 | #include <errno.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 47 | #include <resolv.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 48 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 49 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 52 | #include <stdarg.h> |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 53 | #include <unistd.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 54 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 55 | #include "xmalloc.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 56 | #include "match.h" |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 57 | #include "sshkey.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 58 | #include "hostfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 59 | #include "log.h" |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 60 | #include "misc.h" |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 61 | #include "ssherr.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 62 | #include "digest.h" |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 63 | #include "hmac.h" |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 64 | |
| 65 | struct hostkeys { |
| 66 | struct hostkey_entry *entries; |
| 67 | u_int num_entries; |
| 68 | }; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 69 | |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 70 | /* XXX hmac is too easy to dictionary attack; use bcrypt? */ |
| 71 | |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 72 | static int |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 73 | extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len) |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 74 | { |
| 75 | char *p, *b64salt; |
| 76 | u_int b64len; |
| 77 | int ret; |
| 78 | |
| 79 | if (l < sizeof(HASH_MAGIC) - 1) { |
| 80 | debug2("extract_salt: string too short"); |
| 81 | return (-1); |
| 82 | } |
| 83 | if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) { |
| 84 | debug2("extract_salt: invalid magic identifier"); |
| 85 | return (-1); |
| 86 | } |
| 87 | s += sizeof(HASH_MAGIC) - 1; |
| 88 | l -= sizeof(HASH_MAGIC) - 1; |
| 89 | if ((p = memchr(s, HASH_DELIM, l)) == NULL) { |
| 90 | debug2("extract_salt: missing salt termination character"); |
| 91 | return (-1); |
| 92 | } |
| 93 | |
| 94 | b64len = p - s; |
| 95 | /* Sanity check */ |
| 96 | if (b64len == 0 || b64len > 1024) { |
| 97 | debug2("extract_salt: bad encoded salt length %u", b64len); |
| 98 | return (-1); |
| 99 | } |
| 100 | b64salt = xmalloc(1 + b64len); |
| 101 | memcpy(b64salt, s, b64len); |
| 102 | b64salt[b64len] = '\0'; |
| 103 | |
| 104 | ret = __b64_pton(b64salt, salt, salt_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 105 | free(b64salt); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 106 | if (ret == -1) { |
| 107 | debug2("extract_salt: salt decode error"); |
| 108 | return (-1); |
| 109 | } |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 110 | if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) { |
| 111 | debug2("extract_salt: expected salt len %zd, got %d", |
| 112 | ssh_hmac_bytes(SSH_DIGEST_SHA1), ret); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 113 | return (-1); |
| 114 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 115 | |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 116 | return (0); |
| 117 | } |
| 118 | |
| 119 | char * |
| 120 | host_hash(const char *host, const char *name_from_hostfile, u_int src_len) |
| 121 | { |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 122 | struct ssh_hmac_ctx *ctx; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 123 | u_char salt[256], result[256]; |
| 124 | char uu_salt[512], uu_result[512]; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 125 | static char encoded[1024]; |
tedu@openbsd.org | 1036356 | 2016-09-17 18:00:27 +0000 | [diff] [blame] | 126 | u_int len; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 127 | |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 128 | len = ssh_digest_bytes(SSH_DIGEST_SHA1); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 129 | |
| 130 | if (name_from_hostfile == NULL) { |
| 131 | /* Create new salt */ |
tedu@openbsd.org | 1036356 | 2016-09-17 18:00:27 +0000 | [diff] [blame] | 132 | arc4random_buf(salt, len); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 133 | } else { |
| 134 | /* Extract salt from known host entry */ |
| 135 | if (extract_salt(name_from_hostfile, src_len, salt, |
| 136 | sizeof(salt)) == -1) |
| 137 | return (NULL); |
| 138 | } |
| 139 | |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 140 | if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL || |
| 141 | ssh_hmac_init(ctx, salt, len) < 0 || |
| 142 | ssh_hmac_update(ctx, host, strlen(host)) < 0 || |
| 143 | ssh_hmac_final(ctx, result, sizeof(result))) |
| 144 | fatal("%s: ssh_hmac failed", __func__); |
| 145 | ssh_hmac_free(ctx); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 146 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 147 | if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 || |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 148 | __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1) |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 149 | fatal("%s: __b64_ntop failed", __func__); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 150 | |
| 151 | snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt, |
| 152 | HASH_DELIM, uu_result); |
| 153 | |
| 154 | return (encoded); |
| 155 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 156 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 157 | /* |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 158 | * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the |
| 159 | * 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] | 160 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | |
Damien Miller | 5b2aea9 | 2001-12-21 12:47:09 +1100 | [diff] [blame] | 162 | int |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 163 | hostfile_read_key(char **cpp, u_int *bitsp, struct sshkey *ret) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 164 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 165 | char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 166 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | /* Skip leading whitespace. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 168 | for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) |
| 169 | ; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 170 | |
dtucker@openbsd.org | 696fb42 | 2019-07-07 01:05:00 +0000 | [diff] [blame] | 171 | if (sshkey_read(ret, &cp) != 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 172 | return 0; |
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 | /* Skip trailing whitespace. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 175 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 176 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 177 | |
| 178 | /* Return results. */ |
| 179 | *cpp = cp; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 180 | if (bitsp != NULL) |
| 181 | *bitsp = sshkey_size(ret); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 183 | } |
| 184 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 185 | static HostkeyMarker |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 186 | check_markers(char **cpp) |
| 187 | { |
| 188 | char marker[32], *sp, *cp = *cpp; |
| 189 | int ret = MRK_NONE; |
| 190 | |
| 191 | while (*cp == '@') { |
| 192 | /* Only one marker is allowed */ |
| 193 | if (ret != MRK_NONE) |
| 194 | return MRK_ERROR; |
| 195 | /* Markers are terminated by whitespace */ |
| 196 | if ((sp = strchr(cp, ' ')) == NULL && |
| 197 | (sp = strchr(cp, '\t')) == NULL) |
| 198 | return MRK_ERROR; |
| 199 | /* Extract marker for comparison */ |
| 200 | if (sp <= cp + 1 || sp >= cp + sizeof(marker)) |
| 201 | return MRK_ERROR; |
| 202 | memcpy(marker, cp, sp - cp); |
| 203 | marker[sp - cp] = '\0'; |
| 204 | if (strcmp(marker, CA_MARKER) == 0) |
| 205 | ret = MRK_CA; |
| 206 | else if (strcmp(marker, REVOKE_MARKER) == 0) |
| 207 | ret = MRK_REVOKE; |
| 208 | else |
| 209 | return MRK_ERROR; |
| 210 | |
| 211 | /* Skip past marker and any whitespace that follows it */ |
| 212 | cp = sp; |
| 213 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 214 | ; |
| 215 | } |
| 216 | *cpp = cp; |
| 217 | return ret; |
| 218 | } |
| 219 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 220 | struct hostkeys * |
| 221 | init_hostkeys(void) |
| 222 | { |
| 223 | struct hostkeys *ret = xcalloc(1, sizeof(*ret)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 224 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 225 | ret->entries = NULL; |
| 226 | return ret; |
| 227 | } |
| 228 | |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 229 | struct load_callback_ctx { |
| 230 | const char *host; |
| 231 | u_long num_loaded; |
| 232 | struct hostkeys *hostkeys; |
| 233 | }; |
| 234 | |
| 235 | static int |
| 236 | record_hostkey(struct hostkey_foreach_line *l, void *_ctx) |
| 237 | { |
| 238 | struct load_callback_ctx *ctx = (struct load_callback_ctx *)_ctx; |
| 239 | struct hostkeys *hostkeys = ctx->hostkeys; |
| 240 | struct hostkey_entry *tmp; |
| 241 | |
| 242 | if (l->status == HKF_STATUS_INVALID) { |
djm@openbsd.org | 398f9ef | 2015-03-31 22:57:06 +0000 | [diff] [blame] | 243 | /* XXX make this verbose() in the future */ |
| 244 | debug("%s:%ld: parse error in hostkeys file", |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 245 | l->path, l->linenum); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | debug3("%s: found %skey type %s in file %s:%lu", __func__, |
| 250 | l->marker == MRK_NONE ? "" : |
| 251 | (l->marker == MRK_CA ? "ca " : "revoked "), |
| 252 | sshkey_type(l->key), l->path, l->linenum); |
deraadt@openbsd.org | 9e509d4 | 2017-05-31 09:15:42 +0000 | [diff] [blame] | 253 | if ((tmp = recallocarray(hostkeys->entries, hostkeys->num_entries, |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 254 | hostkeys->num_entries + 1, sizeof(*hostkeys->entries))) == NULL) |
| 255 | return SSH_ERR_ALLOC_FAIL; |
| 256 | hostkeys->entries = tmp; |
| 257 | hostkeys->entries[hostkeys->num_entries].host = xstrdup(ctx->host); |
| 258 | hostkeys->entries[hostkeys->num_entries].file = xstrdup(l->path); |
| 259 | hostkeys->entries[hostkeys->num_entries].line = l->linenum; |
| 260 | hostkeys->entries[hostkeys->num_entries].key = l->key; |
| 261 | l->key = NULL; /* steal it */ |
| 262 | hostkeys->entries[hostkeys->num_entries].marker = l->marker; |
| 263 | hostkeys->num_entries++; |
| 264 | ctx->num_loaded++; |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 269 | void |
| 270 | load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 271 | { |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 272 | int r; |
| 273 | struct load_callback_ctx ctx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 274 | |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 275 | ctx.host = host; |
| 276 | ctx.num_loaded = 0; |
| 277 | ctx.hostkeys = hostkeys; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 278 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 279 | if ((r = hostkeys_foreach(path, record_hostkey, &ctx, host, NULL, |
| 280 | HKF_WANT_MATCH|HKF_WANT_PARSE_KEY)) != 0) { |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 281 | if (r != SSH_ERR_SYSTEM_ERROR && errno != ENOENT) |
| 282 | debug("%s: hostkeys_foreach failed for %s: %s", |
| 283 | __func__, path, ssh_err(r)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 284 | } |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 285 | if (ctx.num_loaded != 0) |
| 286 | debug3("%s: loaded %lu keys from %s", __func__, |
| 287 | ctx.num_loaded, host); |
djm@openbsd.org | 6fdcaeb | 2014-10-20 03:43:01 +0000 | [diff] [blame] | 288 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 289 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 290 | void |
| 291 | free_hostkeys(struct hostkeys *hostkeys) |
| 292 | { |
| 293 | u_int i; |
| 294 | |
| 295 | for (i = 0; i < hostkeys->num_entries; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 296 | free(hostkeys->entries[i].host); |
| 297 | free(hostkeys->entries[i].file); |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 298 | sshkey_free(hostkeys->entries[i].key); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 299 | explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries)); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 300 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 301 | free(hostkeys->entries); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 302 | explicit_bzero(hostkeys, sizeof(*hostkeys)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 303 | free(hostkeys); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 304 | } |
| 305 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 306 | static int |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 307 | check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 308 | { |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 309 | int is_cert = sshkey_is_cert(k); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 310 | u_int i; |
| 311 | |
| 312 | for (i = 0; i < hostkeys->num_entries; i++) { |
| 313 | if (hostkeys->entries[i].marker != MRK_REVOKE) |
| 314 | continue; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 315 | if (sshkey_equal_public(k, hostkeys->entries[i].key)) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 316 | return -1; |
| 317 | if (is_cert && |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 318 | sshkey_equal_public(k->cert->signature_key, |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 319 | hostkeys->entries[i].key)) |
| 320 | return -1; |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Match keys against a specified key, or look one up by key type. |
| 327 | * |
| 328 | * If looking for a keytype (key == NULL) and one is found then return |
| 329 | * HOST_FOUND, otherwise HOST_NEW. |
| 330 | * |
| 331 | * If looking for a key (key != NULL): |
| 332 | * 1. If the key is a cert and a matching CA is found, return HOST_OK |
| 333 | * 2. If the key is not a cert and a matching key is found, return HOST_OK |
| 334 | * 3. If no key matches but a key with a different type is found, then |
| 335 | * return HOST_CHANGED |
| 336 | * 4. If no matching keys are found, then return HOST_NEW. |
| 337 | * |
| 338 | * Finally, check any found key is not revoked. |
| 339 | */ |
| 340 | static HostStatus |
| 341 | check_hostkeys_by_key_or_type(struct hostkeys *hostkeys, |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 342 | struct sshkey *k, int keytype, const struct hostkey_entry **found) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 343 | { |
| 344 | u_int i; |
| 345 | HostStatus end_return = HOST_NEW; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 346 | int want_cert = sshkey_is_cert(k); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 347 | HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 348 | |
| 349 | if (found != NULL) |
| 350 | *found = NULL; |
| 351 | |
| 352 | for (i = 0; i < hostkeys->num_entries; i++) { |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 353 | if (hostkeys->entries[i].marker != want_marker) |
| 354 | continue; |
| 355 | if (k == NULL) { |
| 356 | if (hostkeys->entries[i].key->type != keytype) |
| 357 | continue; |
| 358 | end_return = HOST_FOUND; |
| 359 | if (found != NULL) |
| 360 | *found = hostkeys->entries + i; |
| 361 | k = hostkeys->entries[i].key; |
| 362 | break; |
| 363 | } |
| 364 | if (want_cert) { |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 365 | if (sshkey_equal_public(k->cert->signature_key, |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 366 | hostkeys->entries[i].key)) { |
| 367 | /* A matching CA exists */ |
| 368 | end_return = HOST_OK; |
| 369 | if (found != NULL) |
| 370 | *found = hostkeys->entries + i; |
| 371 | break; |
| 372 | } |
| 373 | } else { |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 374 | if (sshkey_equal(k, hostkeys->entries[i].key)) { |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 375 | end_return = HOST_OK; |
| 376 | if (found != NULL) |
| 377 | *found = hostkeys->entries + i; |
| 378 | break; |
| 379 | } |
| 380 | /* A non-maching key exists */ |
| 381 | end_return = HOST_CHANGED; |
| 382 | if (found != NULL) |
| 383 | *found = hostkeys->entries + i; |
| 384 | } |
| 385 | } |
| 386 | if (check_key_not_revoked(hostkeys, k) != 0) { |
| 387 | end_return = HOST_REVOKED; |
| 388 | if (found != NULL) |
| 389 | *found = NULL; |
| 390 | } |
| 391 | return end_return; |
| 392 | } |
djm@openbsd.org | 6fdcaeb | 2014-10-20 03:43:01 +0000 | [diff] [blame] | 393 | |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 394 | HostStatus |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 395 | check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key, |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 396 | const struct hostkey_entry **found) |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 397 | { |
| 398 | if (key == NULL) |
| 399 | fatal("no key to look up"); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 400 | return check_hostkeys_by_key_or_type(hostkeys, key, 0, found); |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | int |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 404 | lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype, |
| 405 | const struct hostkey_entry **found) |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 406 | { |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 407 | return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype, |
| 408 | found) == HOST_FOUND); |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 409 | } |
| 410 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 411 | static int |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 412 | write_host_entry(FILE *f, const char *host, const char *ip, |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 413 | const struct sshkey *key, int store_hash) |
| 414 | { |
| 415 | int r, success = 0; |
djm@openbsd.org | db25972 | 2017-03-10 04:26:06 +0000 | [diff] [blame] | 416 | char *hashed_host = NULL, *lhost; |
| 417 | |
| 418 | lhost = xstrdup(host); |
| 419 | lowercase(lhost); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 420 | |
| 421 | if (store_hash) { |
djm@openbsd.org | db25972 | 2017-03-10 04:26:06 +0000 | [diff] [blame] | 422 | if ((hashed_host = host_hash(lhost, NULL, 0)) == NULL) { |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 423 | error("%s: host_hash failed", __func__); |
djm@openbsd.org | db25972 | 2017-03-10 04:26:06 +0000 | [diff] [blame] | 424 | free(lhost); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 425 | return 0; |
| 426 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 427 | fprintf(f, "%s ", hashed_host); |
| 428 | } else if (ip != NULL) |
djm@openbsd.org | db25972 | 2017-03-10 04:26:06 +0000 | [diff] [blame] | 429 | fprintf(f, "%s,%s ", lhost, ip); |
| 430 | else { |
| 431 | fprintf(f, "%s ", lhost); |
| 432 | } |
| 433 | free(lhost); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 434 | if ((r = sshkey_write(key, f)) == 0) |
| 435 | success = 1; |
| 436 | else |
| 437 | error("%s: sshkey_write failed: %s", __func__, ssh_err(r)); |
| 438 | fputc('\n', f); |
| 439 | return success; |
| 440 | } |
| 441 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 442 | /* |
| 443 | * Appends an entry to the host file. Returns false if the entry could not |
| 444 | * be appended. |
| 445 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 446 | int |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 447 | add_host_to_hostfile(const char *filename, const char *host, |
| 448 | const struct sshkey *key, int store_hash) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 449 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 450 | FILE *f; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 451 | int success; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 452 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 453 | if (key == NULL) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 454 | return 1; /* XXX ? */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 455 | f = fopen(filename, "a"); |
| 456 | if (!f) |
| 457 | return 0; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 458 | success = write_host_entry(f, host, NULL, key, store_hash); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 459 | fclose(f); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 460 | return success; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 461 | } |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 462 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 463 | struct host_delete_ctx { |
| 464 | FILE *out; |
| 465 | int quiet; |
| 466 | const char *host; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 467 | int *skip_keys; /* XXX split for host/ip? might want to ensure both */ |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 468 | struct sshkey * const *keys; |
| 469 | size_t nkeys; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 470 | int modified; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 471 | }; |
| 472 | |
| 473 | static int |
| 474 | host_delete(struct hostkey_foreach_line *l, void *_ctx) |
| 475 | { |
| 476 | struct host_delete_ctx *ctx = (struct host_delete_ctx *)_ctx; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 477 | int loglevel = ctx->quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_VERBOSE; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 478 | size_t i; |
| 479 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 480 | if (l->status == HKF_STATUS_MATCHED) { |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 481 | if (l->marker != MRK_NONE) { |
| 482 | /* Don't remove CA and revocation lines */ |
| 483 | fprintf(ctx->out, "%s\n", l->line); |
| 484 | return 0; |
| 485 | } |
| 486 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 487 | /* |
| 488 | * If this line contains one of the keys that we will be |
| 489 | * adding later, then don't change it and mark the key for |
| 490 | * skipping. |
| 491 | */ |
| 492 | for (i = 0; i < ctx->nkeys; i++) { |
| 493 | if (sshkey_equal(ctx->keys[i], l->key)) { |
| 494 | ctx->skip_keys[i] = 1; |
| 495 | fprintf(ctx->out, "%s\n", l->line); |
| 496 | debug3("%s: %s key already at %s:%ld", __func__, |
| 497 | sshkey_type(l->key), l->path, l->linenum); |
| 498 | return 0; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Hostname matches and has no CA/revoke marker, delete it |
| 504 | * by *not* writing the line to ctx->out. |
| 505 | */ |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 506 | do_log2(loglevel, "%s%s%s:%ld: Removed %s key for host %s", |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 507 | ctx->quiet ? __func__ : "", ctx->quiet ? ": " : "", |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 508 | l->path, l->linenum, sshkey_type(l->key), ctx->host); |
| 509 | ctx->modified = 1; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | /* Retain non-matching hosts and invalid lines when deleting */ |
| 513 | if (l->status == HKF_STATUS_INVALID) { |
| 514 | do_log2(loglevel, "%s%s%s:%ld: invalid known_hosts entry", |
| 515 | ctx->quiet ? __func__ : "", ctx->quiet ? ": " : "", |
| 516 | l->path, l->linenum); |
| 517 | } |
| 518 | fprintf(ctx->out, "%s\n", l->line); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | int |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 523 | hostfile_replace_entries(const char *filename, const char *host, const char *ip, |
| 524 | struct sshkey **keys, size_t nkeys, int store_hash, int quiet, int hash_alg) |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 525 | { |
| 526 | int r, fd, oerrno = 0; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 527 | int loglevel = quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_VERBOSE; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 528 | struct host_delete_ctx ctx; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 529 | char *fp, *temp = NULL, *back = NULL; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 530 | mode_t omask; |
| 531 | size_t i; |
| 532 | |
djm@openbsd.org | 3076ee7 | 2015-01-26 13:36:53 +0000 | [diff] [blame] | 533 | omask = umask(077); |
| 534 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 535 | memset(&ctx, 0, sizeof(ctx)); |
| 536 | ctx.host = host; |
| 537 | ctx.quiet = quiet; |
| 538 | if ((ctx.skip_keys = calloc(nkeys, sizeof(*ctx.skip_keys))) == NULL) |
| 539 | return SSH_ERR_ALLOC_FAIL; |
| 540 | ctx.keys = keys; |
| 541 | ctx.nkeys = nkeys; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 542 | ctx.modified = 0; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 543 | |
| 544 | /* |
| 545 | * Prepare temporary file for in-place deletion. |
| 546 | */ |
deraadt@openbsd.org | 1b2d55d | 2019-06-28 01:23:50 +0000 | [diff] [blame] | 547 | if ((r = asprintf(&temp, "%s.XXXXXXXXXXX", filename)) == -1 || |
deraadt@openbsd.org | 5cdbaa7 | 2019-06-27 18:03:37 +0000 | [diff] [blame] | 548 | (r = asprintf(&back, "%s.old", filename)) == -1) { |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 549 | r = SSH_ERR_ALLOC_FAIL; |
| 550 | goto fail; |
| 551 | } |
| 552 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 553 | if ((fd = mkstemp(temp)) == -1) { |
| 554 | oerrno = errno; |
| 555 | error("%s: mkstemp: %s", __func__, strerror(oerrno)); |
| 556 | r = SSH_ERR_SYSTEM_ERROR; |
| 557 | goto fail; |
| 558 | } |
| 559 | if ((ctx.out = fdopen(fd, "w")) == NULL) { |
| 560 | oerrno = errno; |
| 561 | close(fd); |
| 562 | error("%s: fdopen: %s", __func__, strerror(oerrno)); |
| 563 | r = SSH_ERR_SYSTEM_ERROR; |
| 564 | goto fail; |
| 565 | } |
| 566 | |
| 567 | /* Remove all entries for the specified host from the file */ |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 568 | if ((r = hostkeys_foreach(filename, host_delete, &ctx, host, ip, |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 569 | HKF_WANT_PARSE_KEY)) != 0) { |
| 570 | error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r)); |
| 571 | goto fail; |
| 572 | } |
| 573 | |
| 574 | /* Add the requested keys */ |
| 575 | for (i = 0; i < nkeys; i++) { |
| 576 | if (ctx.skip_keys[i]) |
| 577 | continue; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 578 | if ((fp = sshkey_fingerprint(keys[i], hash_alg, |
| 579 | SSH_FP_DEFAULT)) == NULL) { |
| 580 | r = SSH_ERR_ALLOC_FAIL; |
| 581 | goto fail; |
| 582 | } |
| 583 | do_log2(loglevel, "%s%sAdding new key for %s to %s: %s %s", |
| 584 | quiet ? __func__ : "", quiet ? ": " : "", host, filename, |
| 585 | sshkey_ssh_name(keys[i]), fp); |
| 586 | free(fp); |
| 587 | if (!write_host_entry(ctx.out, host, ip, keys[i], store_hash)) { |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 588 | r = SSH_ERR_INTERNAL_ERROR; |
| 589 | goto fail; |
| 590 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 591 | ctx.modified = 1; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 592 | } |
| 593 | fclose(ctx.out); |
| 594 | ctx.out = NULL; |
| 595 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 596 | if (ctx.modified) { |
| 597 | /* Backup the original file and replace it with the temporary */ |
| 598 | if (unlink(back) == -1 && errno != ENOENT) { |
| 599 | oerrno = errno; |
| 600 | error("%s: unlink %.100s: %s", __func__, |
| 601 | back, strerror(errno)); |
| 602 | r = SSH_ERR_SYSTEM_ERROR; |
| 603 | goto fail; |
| 604 | } |
| 605 | if (link(filename, back) == -1) { |
| 606 | oerrno = errno; |
| 607 | error("%s: link %.100s to %.100s: %s", __func__, |
| 608 | filename, back, strerror(errno)); |
| 609 | r = SSH_ERR_SYSTEM_ERROR; |
| 610 | goto fail; |
| 611 | } |
| 612 | if (rename(temp, filename) == -1) { |
| 613 | oerrno = errno; |
| 614 | error("%s: rename \"%s\" to \"%s\": %s", __func__, |
| 615 | temp, filename, strerror(errno)); |
| 616 | r = SSH_ERR_SYSTEM_ERROR; |
| 617 | goto fail; |
| 618 | } |
| 619 | } else { |
| 620 | /* No changes made; just delete the temporary file */ |
| 621 | if (unlink(temp) != 0) |
| 622 | error("%s: unlink \"%s\": %s", __func__, |
| 623 | temp, strerror(errno)); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 624 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 625 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 626 | /* success */ |
| 627 | r = 0; |
| 628 | fail: |
| 629 | if (temp != NULL && r != 0) |
| 630 | unlink(temp); |
| 631 | free(temp); |
| 632 | free(back); |
| 633 | if (ctx.out != NULL) |
| 634 | fclose(ctx.out); |
| 635 | free(ctx.skip_keys); |
djm@openbsd.org | 3076ee7 | 2015-01-26 13:36:53 +0000 | [diff] [blame] | 636 | umask(omask); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 637 | if (r == SSH_ERR_SYSTEM_ERROR) |
| 638 | errno = oerrno; |
| 639 | return r; |
| 640 | } |
| 641 | |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 642 | static int |
| 643 | match_maybe_hashed(const char *host, const char *names, int *was_hashed) |
| 644 | { |
| 645 | int hashed = *names == HASH_DELIM; |
| 646 | const char *hashed_host; |
| 647 | size_t nlen = strlen(names); |
| 648 | |
| 649 | if (was_hashed != NULL) |
| 650 | *was_hashed = hashed; |
| 651 | if (hashed) { |
| 652 | if ((hashed_host = host_hash(host, names, nlen)) == NULL) |
| 653 | return -1; |
| 654 | return nlen == strlen(hashed_host) && |
| 655 | strncmp(hashed_host, names, nlen) == 0; |
| 656 | } |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 657 | return match_hostname(host, names) == 1; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | int |
| 661 | hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx, |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 662 | const char *host, const char *ip, u_int options) |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 663 | { |
| 664 | FILE *f; |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 665 | char *line = NULL, ktype[128]; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 666 | u_long linenum = 0; |
| 667 | char *cp, *cp2; |
| 668 | u_int kbits; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 669 | int hashed; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 670 | int s, r = 0; |
| 671 | struct hostkey_foreach_line lineinfo; |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 672 | size_t linesize = 0, l; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 673 | |
| 674 | memset(&lineinfo, 0, sizeof(lineinfo)); |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 675 | if (host == NULL && (options & HKF_WANT_MATCH) != 0) |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 676 | return SSH_ERR_INVALID_ARGUMENT; |
| 677 | if ((f = fopen(path, "r")) == NULL) |
| 678 | return SSH_ERR_SYSTEM_ERROR; |
| 679 | |
| 680 | debug3("%s: reading file \"%s\"", __func__, path); |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 681 | while (getline(&line, &linesize, f) != -1) { |
| 682 | linenum++; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 683 | line[strcspn(line, "\n")] = '\0'; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 684 | |
djm@openbsd.org | de2997a | 2018-07-16 03:09:13 +0000 | [diff] [blame] | 685 | free(lineinfo.line); |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 686 | sshkey_free(lineinfo.key); |
| 687 | memset(&lineinfo, 0, sizeof(lineinfo)); |
| 688 | lineinfo.path = path; |
| 689 | lineinfo.linenum = linenum; |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 690 | lineinfo.line = xstrdup(line); |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 691 | lineinfo.marker = MRK_NONE; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 692 | lineinfo.status = HKF_STATUS_OK; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 693 | lineinfo.keytype = KEY_UNSPEC; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 694 | |
| 695 | /* Skip any leading whitespace, comments and empty lines. */ |
| 696 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 697 | ; |
| 698 | if (!*cp || *cp == '#' || *cp == '\n') { |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 699 | if ((options & HKF_WANT_MATCH) == 0) { |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 700 | lineinfo.status = HKF_STATUS_COMMENT; |
| 701 | if ((r = callback(&lineinfo, ctx)) != 0) |
| 702 | break; |
| 703 | } |
| 704 | continue; |
| 705 | } |
| 706 | |
| 707 | if ((lineinfo.marker = check_markers(&cp)) == MRK_ERROR) { |
| 708 | verbose("%s: invalid marker at %s:%lu", |
| 709 | __func__, path, linenum); |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 710 | if ((options & HKF_WANT_MATCH) == 0) |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 711 | goto bad; |
| 712 | continue; |
| 713 | } |
| 714 | |
| 715 | /* Find the end of the host name portion. */ |
| 716 | for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) |
| 717 | ; |
| 718 | lineinfo.hosts = cp; |
| 719 | *cp2++ = '\0'; |
| 720 | |
| 721 | /* Check if the host name matches. */ |
| 722 | if (host != NULL) { |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 723 | if ((s = match_maybe_hashed(host, lineinfo.hosts, |
| 724 | &hashed)) == -1) { |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 725 | debug2("%s: %s:%ld: bad host hash \"%.32s\"", |
| 726 | __func__, path, linenum, lineinfo.hosts); |
| 727 | goto bad; |
| 728 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 729 | if (s == 1) { |
| 730 | lineinfo.status = HKF_STATUS_MATCHED; |
| 731 | lineinfo.match |= HKF_MATCH_HOST | |
| 732 | (hashed ? HKF_MATCH_HOST_HASHED : 0); |
| 733 | } |
| 734 | /* Try matching IP address if supplied */ |
| 735 | if (ip != NULL) { |
| 736 | if ((s = match_maybe_hashed(ip, lineinfo.hosts, |
| 737 | &hashed)) == -1) { |
| 738 | debug2("%s: %s:%ld: bad ip hash " |
| 739 | "\"%.32s\"", __func__, path, |
| 740 | linenum, lineinfo.hosts); |
| 741 | goto bad; |
| 742 | } |
| 743 | if (s == 1) { |
| 744 | lineinfo.status = HKF_STATUS_MATCHED; |
| 745 | lineinfo.match |= HKF_MATCH_IP | |
| 746 | (hashed ? HKF_MATCH_IP_HASHED : 0); |
| 747 | } |
| 748 | } |
| 749 | /* |
| 750 | * Skip this line if host matching requested and |
| 751 | * neither host nor address matched. |
| 752 | */ |
| 753 | if ((options & HKF_WANT_MATCH) != 0 && |
| 754 | lineinfo.status != HKF_STATUS_MATCHED) |
| 755 | continue; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /* Got a match. Skip host name and any following whitespace */ |
| 759 | for (; *cp2 == ' ' || *cp2 == '\t'; cp2++) |
| 760 | ; |
| 761 | if (*cp2 == '\0' || *cp2 == '#') { |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 762 | debug2("%s:%ld: truncated before key type", |
| 763 | path, linenum); |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 764 | goto bad; |
| 765 | } |
| 766 | lineinfo.rawkey = cp = cp2; |
| 767 | |
| 768 | if ((options & HKF_WANT_PARSE_KEY) != 0) { |
| 769 | /* |
| 770 | * Extract the key from the line. This will skip |
| 771 | * any leading whitespace. Ignore badly formatted |
| 772 | * lines. |
| 773 | */ |
| 774 | if ((lineinfo.key = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 775 | error("%s: sshkey_new failed", __func__); |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 776 | r = SSH_ERR_ALLOC_FAIL; |
| 777 | break; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 778 | } |
| 779 | if (!hostfile_read_key(&cp, &kbits, lineinfo.key)) { |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 780 | goto bad; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 781 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 782 | lineinfo.keytype = lineinfo.key->type; |
| 783 | lineinfo.comment = cp; |
| 784 | } else { |
| 785 | /* Extract and parse key type */ |
| 786 | l = strcspn(lineinfo.rawkey, " \t"); |
| 787 | if (l <= 1 || l >= sizeof(ktype) || |
| 788 | lineinfo.rawkey[l] == '\0') |
| 789 | goto bad; |
| 790 | memcpy(ktype, lineinfo.rawkey, l); |
| 791 | ktype[l] = '\0'; |
| 792 | lineinfo.keytype = sshkey_type_from_name(ktype); |
djm@openbsd.org | 398f9ef | 2015-03-31 22:57:06 +0000 | [diff] [blame] | 793 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 794 | /* |
djm@openbsd.org | 873d3e7 | 2017-04-30 23:18:44 +0000 | [diff] [blame] | 795 | * Assume legacy RSA1 if the first component is a short |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 796 | * decimal number. |
| 797 | */ |
| 798 | if (lineinfo.keytype == KEY_UNSPEC && l < 8 && |
| 799 | strspn(ktype, "0123456789") == l) |
djm@openbsd.org | 873d3e7 | 2017-04-30 23:18:44 +0000 | [diff] [blame] | 800 | goto bad; |
djm@openbsd.org | 398f9ef | 2015-03-31 22:57:06 +0000 | [diff] [blame] | 801 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 802 | /* |
| 803 | * Check that something other than whitespace follows |
| 804 | * the key type. This won't catch all corruption, but |
| 805 | * it does catch trivial truncation. |
| 806 | */ |
| 807 | cp2 += l; /* Skip past key type */ |
| 808 | for (; *cp2 == ' ' || *cp2 == '\t'; cp2++) |
| 809 | ; |
| 810 | if (*cp2 == '\0' || *cp2 == '#') { |
| 811 | debug2("%s:%ld: truncated after key type", |
| 812 | path, linenum); |
| 813 | lineinfo.keytype = KEY_UNSPEC; |
| 814 | } |
| 815 | if (lineinfo.keytype == KEY_UNSPEC) { |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 816 | bad: |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 817 | sshkey_free(lineinfo.key); |
| 818 | lineinfo.key = NULL; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 819 | lineinfo.status = HKF_STATUS_INVALID; |
| 820 | if ((r = callback(&lineinfo, ctx)) != 0) |
| 821 | break; |
| 822 | continue; |
| 823 | } |
| 824 | } |
| 825 | if ((r = callback(&lineinfo, ctx)) != 0) |
| 826 | break; |
| 827 | } |
| 828 | sshkey_free(lineinfo.key); |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 829 | free(lineinfo.line); |
| 830 | free(line); |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 831 | fclose(f); |
| 832 | return r; |
| 833 | } |