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