djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 1 | /* $OpenBSD: hostfile.c,v 1.66 2015/05/04 06:10:48 djm 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]; |
| 126 | u_int i, len; |
| 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 */ |
| 132 | for (i = 0; i < len; i++) |
| 133 | salt[i] = arc4random(); |
| 134 | } else { |
| 135 | /* Extract salt from known host entry */ |
| 136 | if (extract_salt(name_from_hostfile, src_len, salt, |
| 137 | sizeof(salt)) == -1) |
| 138 | return (NULL); |
| 139 | } |
| 140 | |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 141 | if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL || |
| 142 | ssh_hmac_init(ctx, salt, len) < 0 || |
| 143 | ssh_hmac_update(ctx, host, strlen(host)) < 0 || |
| 144 | ssh_hmac_final(ctx, result, sizeof(result))) |
| 145 | fatal("%s: ssh_hmac failed", __func__); |
| 146 | ssh_hmac_free(ctx); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 147 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 148 | if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 || |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 149 | __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1) |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 150 | fatal("%s: __b64_ntop failed", __func__); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 151 | |
| 152 | snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt, |
| 153 | HASH_DELIM, uu_result); |
| 154 | |
| 155 | return (encoded); |
| 156 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 158 | /* |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 159 | * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the |
| 160 | * 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] | 161 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | |
Damien Miller | 5b2aea9 | 2001-12-21 12:47:09 +1100 | [diff] [blame] | 163 | int |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 164 | hostfile_read_key(char **cpp, u_int *bitsp, struct sshkey *ret) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 165 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 166 | char *cp; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 167 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 168 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | /* Skip leading whitespace. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 170 | for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) |
| 171 | ; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 172 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 173 | if ((r = sshkey_read(ret, &cp)) != 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 174 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 175 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 176 | /* Skip trailing whitespace. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 177 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 178 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 179 | |
| 180 | /* Return results. */ |
| 181 | *cpp = cp; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 182 | if (bitsp != NULL) |
| 183 | *bitsp = sshkey_size(ret); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 184 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 185 | } |
| 186 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 187 | static HostkeyMarker |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 188 | check_markers(char **cpp) |
| 189 | { |
| 190 | char marker[32], *sp, *cp = *cpp; |
| 191 | int ret = MRK_NONE; |
| 192 | |
| 193 | while (*cp == '@') { |
| 194 | /* Only one marker is allowed */ |
| 195 | if (ret != MRK_NONE) |
| 196 | return MRK_ERROR; |
| 197 | /* Markers are terminated by whitespace */ |
| 198 | if ((sp = strchr(cp, ' ')) == NULL && |
| 199 | (sp = strchr(cp, '\t')) == NULL) |
| 200 | return MRK_ERROR; |
| 201 | /* Extract marker for comparison */ |
| 202 | if (sp <= cp + 1 || sp >= cp + sizeof(marker)) |
| 203 | return MRK_ERROR; |
| 204 | memcpy(marker, cp, sp - cp); |
| 205 | marker[sp - cp] = '\0'; |
| 206 | if (strcmp(marker, CA_MARKER) == 0) |
| 207 | ret = MRK_CA; |
| 208 | else if (strcmp(marker, REVOKE_MARKER) == 0) |
| 209 | ret = MRK_REVOKE; |
| 210 | else |
| 211 | return MRK_ERROR; |
| 212 | |
| 213 | /* Skip past marker and any whitespace that follows it */ |
| 214 | cp = sp; |
| 215 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 216 | ; |
| 217 | } |
| 218 | *cpp = cp; |
| 219 | return ret; |
| 220 | } |
| 221 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 222 | struct hostkeys * |
| 223 | init_hostkeys(void) |
| 224 | { |
| 225 | struct hostkeys *ret = xcalloc(1, sizeof(*ret)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 226 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 227 | ret->entries = NULL; |
| 228 | return ret; |
| 229 | } |
| 230 | |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 231 | struct load_callback_ctx { |
| 232 | const char *host; |
| 233 | u_long num_loaded; |
| 234 | struct hostkeys *hostkeys; |
| 235 | }; |
| 236 | |
| 237 | static int |
| 238 | record_hostkey(struct hostkey_foreach_line *l, void *_ctx) |
| 239 | { |
| 240 | struct load_callback_ctx *ctx = (struct load_callback_ctx *)_ctx; |
| 241 | struct hostkeys *hostkeys = ctx->hostkeys; |
| 242 | struct hostkey_entry *tmp; |
| 243 | |
| 244 | if (l->status == HKF_STATUS_INVALID) { |
djm@openbsd.org | 398f9ef | 2015-03-31 22:57:06 +0000 | [diff] [blame] | 245 | /* XXX make this verbose() in the future */ |
| 246 | debug("%s:%ld: parse error in hostkeys file", |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 247 | l->path, l->linenum); |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | debug3("%s: found %skey type %s in file %s:%lu", __func__, |
| 252 | l->marker == MRK_NONE ? "" : |
| 253 | (l->marker == MRK_CA ? "ca " : "revoked "), |
| 254 | sshkey_type(l->key), l->path, l->linenum); |
| 255 | if ((tmp = reallocarray(hostkeys->entries, |
| 256 | hostkeys->num_entries + 1, sizeof(*hostkeys->entries))) == NULL) |
| 257 | return SSH_ERR_ALLOC_FAIL; |
| 258 | hostkeys->entries = tmp; |
| 259 | hostkeys->entries[hostkeys->num_entries].host = xstrdup(ctx->host); |
| 260 | hostkeys->entries[hostkeys->num_entries].file = xstrdup(l->path); |
| 261 | hostkeys->entries[hostkeys->num_entries].line = l->linenum; |
| 262 | hostkeys->entries[hostkeys->num_entries].key = l->key; |
| 263 | l->key = NULL; /* steal it */ |
| 264 | hostkeys->entries[hostkeys->num_entries].marker = l->marker; |
| 265 | hostkeys->num_entries++; |
| 266 | ctx->num_loaded++; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 271 | void |
| 272 | load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 273 | { |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 274 | int r; |
| 275 | struct load_callback_ctx ctx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 276 | |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 277 | ctx.host = host; |
| 278 | ctx.num_loaded = 0; |
| 279 | ctx.hostkeys = hostkeys; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 280 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 281 | if ((r = hostkeys_foreach(path, record_hostkey, &ctx, host, NULL, |
| 282 | HKF_WANT_MATCH|HKF_WANT_PARSE_KEY)) != 0) { |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 283 | if (r != SSH_ERR_SYSTEM_ERROR && errno != ENOENT) |
| 284 | debug("%s: hostkeys_foreach failed for %s: %s", |
| 285 | __func__, path, ssh_err(r)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 286 | } |
djm@openbsd.org | ec3d065 | 2015-01-18 21:48:09 +0000 | [diff] [blame] | 287 | if (ctx.num_loaded != 0) |
| 288 | debug3("%s: loaded %lu keys from %s", __func__, |
| 289 | ctx.num_loaded, host); |
djm@openbsd.org | 6fdcaeb | 2014-10-20 03:43:01 +0000 | [diff] [blame] | 290 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 291 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 292 | void |
| 293 | free_hostkeys(struct hostkeys *hostkeys) |
| 294 | { |
| 295 | u_int i; |
| 296 | |
| 297 | for (i = 0; i < hostkeys->num_entries; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 298 | free(hostkeys->entries[i].host); |
| 299 | free(hostkeys->entries[i].file); |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 300 | sshkey_free(hostkeys->entries[i].key); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 301 | explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries)); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 302 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 303 | free(hostkeys->entries); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 304 | explicit_bzero(hostkeys, sizeof(*hostkeys)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 305 | free(hostkeys); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 306 | } |
| 307 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 308 | static int |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 309 | check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 310 | { |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 311 | int is_cert = sshkey_is_cert(k); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 312 | u_int i; |
| 313 | |
| 314 | for (i = 0; i < hostkeys->num_entries; i++) { |
| 315 | if (hostkeys->entries[i].marker != MRK_REVOKE) |
| 316 | continue; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 317 | if (sshkey_equal_public(k, hostkeys->entries[i].key)) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 318 | return -1; |
| 319 | if (is_cert && |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 320 | sshkey_equal_public(k->cert->signature_key, |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 321 | hostkeys->entries[i].key)) |
| 322 | return -1; |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Match keys against a specified key, or look one up by key type. |
| 329 | * |
| 330 | * If looking for a keytype (key == NULL) and one is found then return |
| 331 | * HOST_FOUND, otherwise HOST_NEW. |
| 332 | * |
| 333 | * If looking for a key (key != NULL): |
| 334 | * 1. If the key is a cert and a matching CA is found, return HOST_OK |
| 335 | * 2. If the key is not a cert and a matching key is found, return HOST_OK |
| 336 | * 3. If no key matches but a key with a different type is found, then |
| 337 | * return HOST_CHANGED |
| 338 | * 4. If no matching keys are found, then return HOST_NEW. |
| 339 | * |
| 340 | * Finally, check any found key is not revoked. |
| 341 | */ |
| 342 | static HostStatus |
| 343 | check_hostkeys_by_key_or_type(struct hostkeys *hostkeys, |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 344 | struct sshkey *k, int keytype, const struct hostkey_entry **found) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 345 | { |
| 346 | u_int i; |
| 347 | HostStatus end_return = HOST_NEW; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 348 | int want_cert = sshkey_is_cert(k); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 349 | HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE; |
| 350 | int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2; |
| 351 | |
| 352 | if (found != NULL) |
| 353 | *found = NULL; |
| 354 | |
| 355 | for (i = 0; i < hostkeys->num_entries; i++) { |
| 356 | if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1) |
| 357 | continue; |
| 358 | if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1) |
| 359 | continue; |
| 360 | if (hostkeys->entries[i].marker != want_marker) |
| 361 | continue; |
| 362 | if (k == NULL) { |
| 363 | if (hostkeys->entries[i].key->type != keytype) |
| 364 | continue; |
| 365 | end_return = HOST_FOUND; |
| 366 | if (found != NULL) |
| 367 | *found = hostkeys->entries + i; |
| 368 | k = hostkeys->entries[i].key; |
| 369 | break; |
| 370 | } |
| 371 | if (want_cert) { |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 372 | if (sshkey_equal_public(k->cert->signature_key, |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 373 | hostkeys->entries[i].key)) { |
| 374 | /* A matching CA exists */ |
| 375 | end_return = HOST_OK; |
| 376 | if (found != NULL) |
| 377 | *found = hostkeys->entries + i; |
| 378 | break; |
| 379 | } |
| 380 | } else { |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 381 | if (sshkey_equal(k, hostkeys->entries[i].key)) { |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 382 | end_return = HOST_OK; |
| 383 | if (found != NULL) |
| 384 | *found = hostkeys->entries + i; |
| 385 | break; |
| 386 | } |
| 387 | /* A non-maching key exists */ |
| 388 | end_return = HOST_CHANGED; |
| 389 | if (found != NULL) |
| 390 | *found = hostkeys->entries + i; |
| 391 | } |
| 392 | } |
| 393 | if (check_key_not_revoked(hostkeys, k) != 0) { |
| 394 | end_return = HOST_REVOKED; |
| 395 | if (found != NULL) |
| 396 | *found = NULL; |
| 397 | } |
| 398 | return end_return; |
| 399 | } |
djm@openbsd.org | 6fdcaeb | 2014-10-20 03:43:01 +0000 | [diff] [blame] | 400 | |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 401 | HostStatus |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 402 | check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key, |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 403 | const struct hostkey_entry **found) |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 404 | { |
| 405 | if (key == NULL) |
| 406 | fatal("no key to look up"); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 407 | return check_hostkeys_by_key_or_type(hostkeys, key, 0, found); |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | int |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 411 | lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype, |
| 412 | const struct hostkey_entry **found) |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 413 | { |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 414 | return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype, |
| 415 | found) == HOST_FOUND); |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 416 | } |
| 417 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 418 | static int |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 419 | write_host_entry(FILE *f, const char *host, const char *ip, |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 420 | const struct sshkey *key, int store_hash) |
| 421 | { |
| 422 | int r, success = 0; |
| 423 | char *hashed_host = NULL; |
| 424 | |
| 425 | if (store_hash) { |
| 426 | if ((hashed_host = host_hash(host, NULL, 0)) == NULL) { |
| 427 | error("%s: host_hash failed", __func__); |
| 428 | return 0; |
| 429 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 430 | fprintf(f, "%s ", hashed_host); |
| 431 | } else if (ip != NULL) |
| 432 | fprintf(f, "%s,%s ", host, ip); |
| 433 | else |
| 434 | fprintf(f, "%s ", host); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 435 | |
| 436 | if ((r = sshkey_write(key, f)) == 0) |
| 437 | success = 1; |
| 438 | else |
| 439 | error("%s: sshkey_write failed: %s", __func__, ssh_err(r)); |
| 440 | fputc('\n', f); |
| 441 | return success; |
| 442 | } |
| 443 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 444 | /* |
| 445 | * Appends an entry to the host file. Returns false if the entry could not |
| 446 | * be appended. |
| 447 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 448 | int |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 449 | add_host_to_hostfile(const char *filename, const char *host, |
| 450 | const struct sshkey *key, int store_hash) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 451 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 452 | FILE *f; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 453 | int success; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 454 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 455 | if (key == NULL) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 456 | return 1; /* XXX ? */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 457 | f = fopen(filename, "a"); |
| 458 | if (!f) |
| 459 | return 0; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 460 | success = write_host_entry(f, host, NULL, key, store_hash); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 461 | fclose(f); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 462 | return success; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 463 | } |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 464 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 465 | struct host_delete_ctx { |
| 466 | FILE *out; |
| 467 | int quiet; |
| 468 | const char *host; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 469 | 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] | 470 | struct sshkey * const *keys; |
| 471 | size_t nkeys; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 472 | int modified; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | static int |
| 476 | host_delete(struct hostkey_foreach_line *l, void *_ctx) |
| 477 | { |
| 478 | struct host_delete_ctx *ctx = (struct host_delete_ctx *)_ctx; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 479 | int loglevel = ctx->quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_VERBOSE; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 480 | size_t i; |
| 481 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 482 | if (l->status == HKF_STATUS_MATCHED) { |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 483 | if (l->marker != MRK_NONE) { |
| 484 | /* Don't remove CA and revocation lines */ |
| 485 | fprintf(ctx->out, "%s\n", l->line); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | /* XXX might need a knob for this later */ |
| 490 | /* Don't remove RSA1 keys */ |
| 491 | if (l->key->type == KEY_RSA1) { |
| 492 | fprintf(ctx->out, "%s\n", l->line); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * If this line contains one of the keys that we will be |
| 498 | * adding later, then don't change it and mark the key for |
| 499 | * skipping. |
| 500 | */ |
| 501 | for (i = 0; i < ctx->nkeys; i++) { |
| 502 | if (sshkey_equal(ctx->keys[i], l->key)) { |
| 503 | ctx->skip_keys[i] = 1; |
| 504 | fprintf(ctx->out, "%s\n", l->line); |
| 505 | debug3("%s: %s key already at %s:%ld", __func__, |
| 506 | sshkey_type(l->key), l->path, l->linenum); |
| 507 | return 0; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Hostname matches and has no CA/revoke marker, delete it |
| 513 | * by *not* writing the line to ctx->out. |
| 514 | */ |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 515 | 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] | 516 | ctx->quiet ? __func__ : "", ctx->quiet ? ": " : "", |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 517 | l->path, l->linenum, sshkey_type(l->key), ctx->host); |
| 518 | ctx->modified = 1; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | /* Retain non-matching hosts and invalid lines when deleting */ |
| 522 | if (l->status == HKF_STATUS_INVALID) { |
| 523 | do_log2(loglevel, "%s%s%s:%ld: invalid known_hosts entry", |
| 524 | ctx->quiet ? __func__ : "", ctx->quiet ? ": " : "", |
| 525 | l->path, l->linenum); |
| 526 | } |
| 527 | fprintf(ctx->out, "%s\n", l->line); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 532 | hostfile_replace_entries(const char *filename, const char *host, const char *ip, |
| 533 | 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] | 534 | { |
| 535 | int r, fd, oerrno = 0; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 536 | int loglevel = quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_VERBOSE; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 537 | struct host_delete_ctx ctx; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 538 | char *fp, *temp = NULL, *back = NULL; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 539 | mode_t omask; |
| 540 | size_t i; |
| 541 | |
djm@openbsd.org | 3076ee7 | 2015-01-26 13:36:53 +0000 | [diff] [blame] | 542 | omask = umask(077); |
| 543 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 544 | memset(&ctx, 0, sizeof(ctx)); |
| 545 | ctx.host = host; |
| 546 | ctx.quiet = quiet; |
| 547 | if ((ctx.skip_keys = calloc(nkeys, sizeof(*ctx.skip_keys))) == NULL) |
| 548 | return SSH_ERR_ALLOC_FAIL; |
| 549 | ctx.keys = keys; |
| 550 | ctx.nkeys = nkeys; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 551 | ctx.modified = 0; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 552 | |
| 553 | /* |
| 554 | * Prepare temporary file for in-place deletion. |
| 555 | */ |
| 556 | if ((r = asprintf(&temp, "%s.XXXXXXXXXXX", filename)) < 0 || |
| 557 | (r = asprintf(&back, "%s.old", filename)) < 0) { |
| 558 | r = SSH_ERR_ALLOC_FAIL; |
| 559 | goto fail; |
| 560 | } |
| 561 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 562 | if ((fd = mkstemp(temp)) == -1) { |
| 563 | oerrno = errno; |
| 564 | error("%s: mkstemp: %s", __func__, strerror(oerrno)); |
| 565 | r = SSH_ERR_SYSTEM_ERROR; |
| 566 | goto fail; |
| 567 | } |
| 568 | if ((ctx.out = fdopen(fd, "w")) == NULL) { |
| 569 | oerrno = errno; |
| 570 | close(fd); |
| 571 | error("%s: fdopen: %s", __func__, strerror(oerrno)); |
| 572 | r = SSH_ERR_SYSTEM_ERROR; |
| 573 | goto fail; |
| 574 | } |
| 575 | |
| 576 | /* Remove all entries for the specified host from the file */ |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 577 | if ((r = hostkeys_foreach(filename, host_delete, &ctx, host, ip, |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 578 | HKF_WANT_PARSE_KEY)) != 0) { |
| 579 | error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r)); |
| 580 | goto fail; |
| 581 | } |
| 582 | |
| 583 | /* Add the requested keys */ |
| 584 | for (i = 0; i < nkeys; i++) { |
| 585 | if (ctx.skip_keys[i]) |
| 586 | continue; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 587 | if ((fp = sshkey_fingerprint(keys[i], hash_alg, |
| 588 | SSH_FP_DEFAULT)) == NULL) { |
| 589 | r = SSH_ERR_ALLOC_FAIL; |
| 590 | goto fail; |
| 591 | } |
| 592 | do_log2(loglevel, "%s%sAdding new key for %s to %s: %s %s", |
| 593 | quiet ? __func__ : "", quiet ? ": " : "", host, filename, |
| 594 | sshkey_ssh_name(keys[i]), fp); |
| 595 | free(fp); |
| 596 | 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] | 597 | r = SSH_ERR_INTERNAL_ERROR; |
| 598 | goto fail; |
| 599 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 600 | ctx.modified = 1; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 601 | } |
| 602 | fclose(ctx.out); |
| 603 | ctx.out = NULL; |
| 604 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 605 | if (ctx.modified) { |
| 606 | /* Backup the original file and replace it with the temporary */ |
| 607 | if (unlink(back) == -1 && errno != ENOENT) { |
| 608 | oerrno = errno; |
| 609 | error("%s: unlink %.100s: %s", __func__, |
| 610 | back, strerror(errno)); |
| 611 | r = SSH_ERR_SYSTEM_ERROR; |
| 612 | goto fail; |
| 613 | } |
| 614 | if (link(filename, back) == -1) { |
| 615 | oerrno = errno; |
| 616 | error("%s: link %.100s to %.100s: %s", __func__, |
| 617 | filename, back, strerror(errno)); |
| 618 | r = SSH_ERR_SYSTEM_ERROR; |
| 619 | goto fail; |
| 620 | } |
| 621 | if (rename(temp, filename) == -1) { |
| 622 | oerrno = errno; |
| 623 | error("%s: rename \"%s\" to \"%s\": %s", __func__, |
| 624 | temp, filename, strerror(errno)); |
| 625 | r = SSH_ERR_SYSTEM_ERROR; |
| 626 | goto fail; |
| 627 | } |
| 628 | } else { |
| 629 | /* No changes made; just delete the temporary file */ |
| 630 | if (unlink(temp) != 0) |
| 631 | error("%s: unlink \"%s\": %s", __func__, |
| 632 | temp, strerror(errno)); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 633 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 634 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 635 | /* success */ |
| 636 | r = 0; |
| 637 | fail: |
| 638 | if (temp != NULL && r != 0) |
| 639 | unlink(temp); |
| 640 | free(temp); |
| 641 | free(back); |
| 642 | if (ctx.out != NULL) |
| 643 | fclose(ctx.out); |
| 644 | free(ctx.skip_keys); |
djm@openbsd.org | 3076ee7 | 2015-01-26 13:36:53 +0000 | [diff] [blame] | 645 | umask(omask); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 646 | if (r == SSH_ERR_SYSTEM_ERROR) |
| 647 | errno = oerrno; |
| 648 | return r; |
| 649 | } |
| 650 | |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 651 | static int |
| 652 | match_maybe_hashed(const char *host, const char *names, int *was_hashed) |
| 653 | { |
| 654 | int hashed = *names == HASH_DELIM; |
| 655 | const char *hashed_host; |
| 656 | size_t nlen = strlen(names); |
| 657 | |
| 658 | if (was_hashed != NULL) |
| 659 | *was_hashed = hashed; |
| 660 | if (hashed) { |
| 661 | if ((hashed_host = host_hash(host, names, nlen)) == NULL) |
| 662 | return -1; |
| 663 | return nlen == strlen(hashed_host) && |
| 664 | strncmp(hashed_host, names, nlen) == 0; |
| 665 | } |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 666 | return match_hostname(host, names) == 1; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | int |
| 670 | hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx, |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 671 | const char *host, const char *ip, u_int options) |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 672 | { |
| 673 | FILE *f; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 674 | char line[8192], oline[8192], ktype[128]; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 675 | u_long linenum = 0; |
| 676 | char *cp, *cp2; |
| 677 | u_int kbits; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 678 | int hashed; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 679 | int s, r = 0; |
| 680 | struct hostkey_foreach_line lineinfo; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 681 | size_t l; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 682 | |
| 683 | memset(&lineinfo, 0, sizeof(lineinfo)); |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 684 | if (host == NULL && (options & HKF_WANT_MATCH) != 0) |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 685 | return SSH_ERR_INVALID_ARGUMENT; |
| 686 | if ((f = fopen(path, "r")) == NULL) |
| 687 | return SSH_ERR_SYSTEM_ERROR; |
| 688 | |
| 689 | debug3("%s: reading file \"%s\"", __func__, path); |
| 690 | while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) { |
| 691 | line[strcspn(line, "\n")] = '\0'; |
| 692 | strlcpy(oline, line, sizeof(oline)); |
| 693 | |
| 694 | sshkey_free(lineinfo.key); |
| 695 | memset(&lineinfo, 0, sizeof(lineinfo)); |
| 696 | lineinfo.path = path; |
| 697 | lineinfo.linenum = linenum; |
| 698 | lineinfo.line = oline; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 699 | lineinfo.marker = MRK_NONE; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 700 | lineinfo.status = HKF_STATUS_OK; |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 701 | lineinfo.keytype = KEY_UNSPEC; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 702 | |
| 703 | /* Skip any leading whitespace, comments and empty lines. */ |
| 704 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 705 | ; |
| 706 | if (!*cp || *cp == '#' || *cp == '\n') { |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 707 | if ((options & HKF_WANT_MATCH) == 0) { |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 708 | lineinfo.status = HKF_STATUS_COMMENT; |
| 709 | if ((r = callback(&lineinfo, ctx)) != 0) |
| 710 | break; |
| 711 | } |
| 712 | continue; |
| 713 | } |
| 714 | |
| 715 | if ((lineinfo.marker = check_markers(&cp)) == MRK_ERROR) { |
| 716 | verbose("%s: invalid marker at %s:%lu", |
| 717 | __func__, path, linenum); |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 718 | if ((options & HKF_WANT_MATCH) == 0) |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 719 | goto bad; |
| 720 | continue; |
| 721 | } |
| 722 | |
| 723 | /* Find the end of the host name portion. */ |
| 724 | for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) |
| 725 | ; |
| 726 | lineinfo.hosts = cp; |
| 727 | *cp2++ = '\0'; |
| 728 | |
| 729 | /* Check if the host name matches. */ |
| 730 | if (host != NULL) { |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 731 | if ((s = match_maybe_hashed(host, lineinfo.hosts, |
| 732 | &hashed)) == -1) { |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 733 | debug2("%s: %s:%ld: bad host hash \"%.32s\"", |
| 734 | __func__, path, linenum, lineinfo.hosts); |
| 735 | goto bad; |
| 736 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 737 | if (s == 1) { |
| 738 | lineinfo.status = HKF_STATUS_MATCHED; |
| 739 | lineinfo.match |= HKF_MATCH_HOST | |
| 740 | (hashed ? HKF_MATCH_HOST_HASHED : 0); |
| 741 | } |
| 742 | /* Try matching IP address if supplied */ |
| 743 | if (ip != NULL) { |
| 744 | if ((s = match_maybe_hashed(ip, lineinfo.hosts, |
| 745 | &hashed)) == -1) { |
| 746 | debug2("%s: %s:%ld: bad ip hash " |
| 747 | "\"%.32s\"", __func__, path, |
| 748 | linenum, lineinfo.hosts); |
| 749 | goto bad; |
| 750 | } |
| 751 | if (s == 1) { |
| 752 | lineinfo.status = HKF_STATUS_MATCHED; |
| 753 | lineinfo.match |= HKF_MATCH_IP | |
| 754 | (hashed ? HKF_MATCH_IP_HASHED : 0); |
| 755 | } |
| 756 | } |
| 757 | /* |
| 758 | * Skip this line if host matching requested and |
| 759 | * neither host nor address matched. |
| 760 | */ |
| 761 | if ((options & HKF_WANT_MATCH) != 0 && |
| 762 | lineinfo.status != HKF_STATUS_MATCHED) |
| 763 | continue; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /* Got a match. Skip host name and any following whitespace */ |
| 767 | for (; *cp2 == ' ' || *cp2 == '\t'; cp2++) |
| 768 | ; |
| 769 | if (*cp2 == '\0' || *cp2 == '#') { |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 770 | debug2("%s:%ld: truncated before key type", |
| 771 | path, linenum); |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 772 | goto bad; |
| 773 | } |
| 774 | lineinfo.rawkey = cp = cp2; |
| 775 | |
| 776 | if ((options & HKF_WANT_PARSE_KEY) != 0) { |
| 777 | /* |
| 778 | * Extract the key from the line. This will skip |
| 779 | * any leading whitespace. Ignore badly formatted |
| 780 | * lines. |
| 781 | */ |
| 782 | if ((lineinfo.key = sshkey_new(KEY_UNSPEC)) == NULL) { |
| 783 | error("%s: sshkey_new failed", __func__); |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 784 | r = SSH_ERR_ALLOC_FAIL; |
| 785 | break; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 786 | } |
| 787 | if (!hostfile_read_key(&cp, &kbits, lineinfo.key)) { |
| 788 | #ifdef WITH_SSH1 |
| 789 | sshkey_free(lineinfo.key); |
| 790 | lineinfo.key = sshkey_new(KEY_RSA1); |
| 791 | if (lineinfo.key == NULL) { |
| 792 | error("%s: sshkey_new fail", __func__); |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 793 | r = SSH_ERR_ALLOC_FAIL; |
| 794 | break; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 795 | } |
| 796 | if (!hostfile_read_key(&cp, &kbits, |
| 797 | lineinfo.key)) |
| 798 | goto bad; |
| 799 | #else |
| 800 | goto bad; |
| 801 | #endif |
| 802 | } |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 803 | lineinfo.keytype = lineinfo.key->type; |
| 804 | lineinfo.comment = cp; |
| 805 | } else { |
| 806 | /* Extract and parse key type */ |
| 807 | l = strcspn(lineinfo.rawkey, " \t"); |
| 808 | if (l <= 1 || l >= sizeof(ktype) || |
| 809 | lineinfo.rawkey[l] == '\0') |
| 810 | goto bad; |
| 811 | memcpy(ktype, lineinfo.rawkey, l); |
| 812 | ktype[l] = '\0'; |
| 813 | lineinfo.keytype = sshkey_type_from_name(ktype); |
djm@openbsd.org | 398f9ef | 2015-03-31 22:57:06 +0000 | [diff] [blame] | 814 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 815 | /* |
| 816 | * Assume RSA1 if the first component is a short |
| 817 | * decimal number. |
| 818 | */ |
| 819 | if (lineinfo.keytype == KEY_UNSPEC && l < 8 && |
| 820 | strspn(ktype, "0123456789") == l) |
| 821 | lineinfo.keytype = KEY_RSA1; |
djm@openbsd.org | 398f9ef | 2015-03-31 22:57:06 +0000 | [diff] [blame] | 822 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 823 | /* |
| 824 | * Check that something other than whitespace follows |
| 825 | * the key type. This won't catch all corruption, but |
| 826 | * it does catch trivial truncation. |
| 827 | */ |
| 828 | cp2 += l; /* Skip past key type */ |
| 829 | for (; *cp2 == ' ' || *cp2 == '\t'; cp2++) |
| 830 | ; |
| 831 | if (*cp2 == '\0' || *cp2 == '#') { |
| 832 | debug2("%s:%ld: truncated after key type", |
| 833 | path, linenum); |
| 834 | lineinfo.keytype = KEY_UNSPEC; |
| 835 | } |
| 836 | if (lineinfo.keytype == KEY_UNSPEC) { |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 837 | bad: |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 838 | sshkey_free(lineinfo.key); |
| 839 | lineinfo.key = NULL; |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 840 | lineinfo.status = HKF_STATUS_INVALID; |
| 841 | if ((r = callback(&lineinfo, ctx)) != 0) |
| 842 | break; |
| 843 | continue; |
| 844 | } |
| 845 | } |
| 846 | if ((r = callback(&lineinfo, ctx)) != 0) |
| 847 | break; |
| 848 | } |
| 849 | sshkey_free(lineinfo.key); |
| 850 | fclose(f); |
| 851 | return r; |
| 852 | } |