1 1.15 christos /* $NetBSD: hostfile.c,v 1.25 2026/04/08 18:58:40 christos Exp $ */ 2 1.25 christos /* $OpenBSD: hostfile.c,v 1.100 2025/11/25 00:57:04 djm Exp $ */ 3 1.24 christos 4 1.1 christos /* 5 1.1 christos * Author: Tatu Ylonen <ylo (at) cs.hut.fi> 6 1.1 christos * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland 7 1.1 christos * All rights reserved 8 1.1 christos * Functions for manipulating the known hosts files. 9 1.1 christos * 10 1.1 christos * As far as I am concerned, the code I have written for this software 11 1.1 christos * can be used freely for any purpose. Any derived versions of this 12 1.1 christos * software must be clearly marked as such, and if the derived work is 13 1.1 christos * incompatible with the protocol description in the RFC file, it must be 14 1.1 christos * called by a name other than "ssh" or "Secure Shell". 15 1.1 christos * 16 1.1 christos * 17 1.1 christos * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved. 18 1.1 christos * Copyright (c) 1999 Niels Provos. All rights reserved. 19 1.1 christos * 20 1.1 christos * Redistribution and use in source and binary forms, with or without 21 1.1 christos * modification, are permitted provided that the following conditions 22 1.1 christos * are met: 23 1.1 christos * 1. Redistributions of source code must retain the above copyright 24 1.1 christos * notice, this list of conditions and the following disclaimer. 25 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 26 1.1 christos * notice, this list of conditions and the following disclaimer in the 27 1.1 christos * documentation and/or other materials provided with the distribution. 28 1.1 christos * 29 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 30 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 31 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 32 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 33 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 34 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 38 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 1.1 christos */ 40 1.1 christos 41 1.2 christos #include "includes.h" 42 1.15 christos __RCSID("$NetBSD: hostfile.c,v 1.25 2026/04/08 18:58:40 christos Exp $"); 43 1.1 christos #include <sys/types.h> 44 1.7 christos #include <sys/stat.h> 45 1.1 christos 46 1.1 christos #include <netinet/in.h> 47 1.1 christos 48 1.7 christos #include <errno.h> 49 1.1 christos #include <resolv.h> 50 1.21 christos #include <stdarg.h> 51 1.1 christos #include <stdio.h> 52 1.1 christos #include <stdlib.h> 53 1.1 christos #include <string.h> 54 1.7 christos #include <unistd.h> 55 1.1 christos 56 1.1 christos #include "xmalloc.h" 57 1.1 christos #include "match.h" 58 1.7 christos #include "sshkey.h" 59 1.1 christos #include "hostfile.h" 60 1.1 christos #include "log.h" 61 1.4 christos #include "misc.h" 62 1.18 christos #include "pathnames.h" 63 1.7 christos #include "ssherr.h" 64 1.6 christos #include "digest.h" 65 1.6 christos #include "hmac.h" 66 1.19 christos #include "sshbuf.h" 67 1.1 christos 68 1.7 christos /* XXX hmac is too easy to dictionary attack; use bcrypt? */ 69 1.7 christos 70 1.1 christos static int 71 1.5 christos extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len) 72 1.1 christos { 73 1.1 christos char *p, *b64salt; 74 1.1 christos u_int b64len; 75 1.1 christos int ret; 76 1.1 christos 77 1.1 christos if (l < sizeof(HASH_MAGIC) - 1) { 78 1.1 christos debug2("extract_salt: string too short"); 79 1.1 christos return (-1); 80 1.1 christos } 81 1.1 christos if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) { 82 1.1 christos debug2("extract_salt: invalid magic identifier"); 83 1.1 christos return (-1); 84 1.1 christos } 85 1.1 christos s += sizeof(HASH_MAGIC) - 1; 86 1.1 christos l -= sizeof(HASH_MAGIC) - 1; 87 1.1 christos if ((p = memchr(s, HASH_DELIM, l)) == NULL) { 88 1.1 christos debug2("extract_salt: missing salt termination character"); 89 1.1 christos return (-1); 90 1.1 christos } 91 1.1 christos 92 1.1 christos b64len = p - s; 93 1.1 christos /* Sanity check */ 94 1.1 christos if (b64len == 0 || b64len > 1024) { 95 1.1 christos debug2("extract_salt: bad encoded salt length %u", b64len); 96 1.1 christos return (-1); 97 1.1 christos } 98 1.1 christos b64salt = xmalloc(1 + b64len); 99 1.1 christos memcpy(b64salt, s, b64len); 100 1.1 christos b64salt[b64len] = '\0'; 101 1.1 christos 102 1.1 christos ret = __b64_pton(b64salt, salt, salt_len); 103 1.5 christos free(b64salt); 104 1.1 christos if (ret == -1) { 105 1.1 christos debug2("extract_salt: salt decode error"); 106 1.1 christos return (-1); 107 1.1 christos } 108 1.6 christos if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) { 109 1.6 christos debug2("extract_salt: expected salt len %zd, got %d", 110 1.6 christos ssh_hmac_bytes(SSH_DIGEST_SHA1), ret); 111 1.1 christos return (-1); 112 1.1 christos } 113 1.1 christos 114 1.1 christos return (0); 115 1.1 christos } 116 1.1 christos 117 1.1 christos char * 118 1.1 christos host_hash(const char *host, const char *name_from_hostfile, u_int src_len) 119 1.1 christos { 120 1.6 christos struct ssh_hmac_ctx *ctx; 121 1.5 christos u_char salt[256], result[256]; 122 1.5 christos char uu_salt[512], uu_result[512]; 123 1.22 christos char *encoded = NULL; 124 1.9 christos u_int len; 125 1.1 christos 126 1.6 christos len = ssh_digest_bytes(SSH_DIGEST_SHA1); 127 1.1 christos 128 1.1 christos if (name_from_hostfile == NULL) { 129 1.1 christos /* Create new salt */ 130 1.9 christos arc4random_buf(salt, len); 131 1.1 christos } else { 132 1.1 christos /* Extract salt from known host entry */ 133 1.1 christos if (extract_salt(name_from_hostfile, src_len, salt, 134 1.1 christos sizeof(salt)) == -1) 135 1.1 christos return (NULL); 136 1.1 christos } 137 1.1 christos 138 1.6 christos if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL || 139 1.6 christos ssh_hmac_init(ctx, salt, len) < 0 || 140 1.6 christos ssh_hmac_update(ctx, host, strlen(host)) < 0 || 141 1.6 christos ssh_hmac_final(ctx, result, sizeof(result))) 142 1.19 christos fatal_f("ssh_hmac failed"); 143 1.6 christos ssh_hmac_free(ctx); 144 1.1 christos 145 1.1 christos if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 || 146 1.1 christos __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1) 147 1.19 christos fatal_f("__b64_ntop failed"); 148 1.22 christos xasprintf(&encoded, "%s%s%c%s", HASH_MAGIC, uu_salt, HASH_DELIM, 149 1.22 christos uu_result); 150 1.1 christos 151 1.1 christos return (encoded); 152 1.1 christos } 153 1.1 christos 154 1.1 christos /* 155 1.24 christos * Parses an RSA key from a string. Moves the pointer over the key. 156 1.24 christos * Skips any whitespace at the beginning and at end. 157 1.1 christos */ 158 1.1 christos 159 1.1 christos int 160 1.7 christos hostfile_read_key(char **cpp, u_int *bitsp, struct sshkey *ret) 161 1.1 christos { 162 1.1 christos char *cp; 163 1.1 christos 164 1.1 christos /* Skip leading whitespace. */ 165 1.1 christos for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) 166 1.1 christos ; 167 1.1 christos 168 1.15 christos if (sshkey_read(ret, &cp) != 0) 169 1.1 christos return 0; 170 1.1 christos 171 1.1 christos /* Skip trailing whitespace. */ 172 1.1 christos for (; *cp == ' ' || *cp == '\t'; cp++) 173 1.1 christos ; 174 1.1 christos 175 1.1 christos /* Return results. */ 176 1.1 christos *cpp = cp; 177 1.7 christos if (bitsp != NULL) 178 1.7 christos *bitsp = sshkey_size(ret); 179 1.1 christos return 1; 180 1.1 christos } 181 1.1 christos 182 1.4 christos static HostkeyMarker 183 1.3 adam check_markers(char **cpp) 184 1.3 adam { 185 1.3 adam char marker[32], *sp, *cp = *cpp; 186 1.3 adam int ret = MRK_NONE; 187 1.3 adam 188 1.3 adam while (*cp == '@') { 189 1.3 adam /* Only one marker is allowed */ 190 1.3 adam if (ret != MRK_NONE) 191 1.3 adam return MRK_ERROR; 192 1.3 adam /* Markers are terminated by whitespace */ 193 1.3 adam if ((sp = strchr(cp, ' ')) == NULL && 194 1.3 adam (sp = strchr(cp, '\t')) == NULL) 195 1.3 adam return MRK_ERROR; 196 1.3 adam /* Extract marker for comparison */ 197 1.3 adam if (sp <= cp + 1 || sp >= cp + sizeof(marker)) 198 1.3 adam return MRK_ERROR; 199 1.3 adam memcpy(marker, cp, sp - cp); 200 1.3 adam marker[sp - cp] = '\0'; 201 1.3 adam if (strcmp(marker, CA_MARKER) == 0) 202 1.3 adam ret = MRK_CA; 203 1.3 adam else if (strcmp(marker, REVOKE_MARKER) == 0) 204 1.3 adam ret = MRK_REVOKE; 205 1.3 adam else 206 1.3 adam return MRK_ERROR; 207 1.3 adam 208 1.3 adam /* Skip past marker and any whitespace that follows it */ 209 1.3 adam cp = sp; 210 1.3 adam for (; *cp == ' ' || *cp == '\t'; cp++) 211 1.3 adam ; 212 1.3 adam } 213 1.3 adam *cpp = cp; 214 1.3 adam return ret; 215 1.3 adam } 216 1.3 adam 217 1.4 christos struct hostkeys * 218 1.4 christos init_hostkeys(void) 219 1.4 christos { 220 1.4 christos struct hostkeys *ret = xcalloc(1, sizeof(*ret)); 221 1.4 christos 222 1.4 christos ret->entries = NULL; 223 1.4 christos return ret; 224 1.4 christos } 225 1.1 christos 226 1.7 christos struct load_callback_ctx { 227 1.7 christos const char *host; 228 1.7 christos u_long num_loaded; 229 1.7 christos struct hostkeys *hostkeys; 230 1.7 christos }; 231 1.7 christos 232 1.7 christos static int 233 1.7 christos record_hostkey(struct hostkey_foreach_line *l, void *_ctx) 234 1.1 christos { 235 1.7 christos struct load_callback_ctx *ctx = (struct load_callback_ctx *)_ctx; 236 1.7 christos struct hostkeys *hostkeys = ctx->hostkeys; 237 1.7 christos struct hostkey_entry *tmp; 238 1.7 christos 239 1.7 christos if (l->status == HKF_STATUS_INVALID) { 240 1.8 christos /* XXX make this verbose() in the future */ 241 1.8 christos debug("%s:%ld: parse error in hostkeys file", 242 1.7 christos l->path, l->linenum); 243 1.7 christos return 0; 244 1.7 christos } 245 1.1 christos 246 1.19 christos debug3_f("found %skey type %s in file %s:%lu", 247 1.7 christos l->marker == MRK_NONE ? "" : 248 1.7 christos (l->marker == MRK_CA ? "ca " : "revoked "), 249 1.7 christos sshkey_type(l->key), l->path, l->linenum); 250 1.11 christos if ((tmp = recallocarray(hostkeys->entries, hostkeys->num_entries, 251 1.7 christos hostkeys->num_entries + 1, sizeof(*hostkeys->entries))) == NULL) 252 1.7 christos return SSH_ERR_ALLOC_FAIL; 253 1.7 christos hostkeys->entries = tmp; 254 1.7 christos hostkeys->entries[hostkeys->num_entries].host = xstrdup(ctx->host); 255 1.7 christos hostkeys->entries[hostkeys->num_entries].file = xstrdup(l->path); 256 1.7 christos hostkeys->entries[hostkeys->num_entries].line = l->linenum; 257 1.7 christos hostkeys->entries[hostkeys->num_entries].key = l->key; 258 1.7 christos l->key = NULL; /* steal it */ 259 1.7 christos hostkeys->entries[hostkeys->num_entries].marker = l->marker; 260 1.19 christos hostkeys->entries[hostkeys->num_entries].note = l->note; 261 1.7 christos hostkeys->num_entries++; 262 1.7 christos ctx->num_loaded++; 263 1.1 christos 264 1.7 christos return 0; 265 1.7 christos } 266 1.3 adam 267 1.7 christos void 268 1.19 christos load_hostkeys_file(struct hostkeys *hostkeys, const char *host, 269 1.19 christos const char *path, FILE *f, u_int note) 270 1.7 christos { 271 1.7 christos int r; 272 1.7 christos struct load_callback_ctx ctx; 273 1.1 christos 274 1.7 christos ctx.host = host; 275 1.7 christos ctx.num_loaded = 0; 276 1.7 christos ctx.hostkeys = hostkeys; 277 1.7 christos 278 1.19 christos if ((r = hostkeys_foreach_file(path, f, record_hostkey, &ctx, host, 279 1.19 christos NULL, HKF_WANT_MATCH|HKF_WANT_PARSE_KEY, note)) != 0) { 280 1.7 christos if (r != SSH_ERR_SYSTEM_ERROR && errno != ENOENT) 281 1.19 christos debug_fr(r, "hostkeys_foreach failed for %s", path); 282 1.7 christos } 283 1.7 christos if (ctx.num_loaded != 0) 284 1.19 christos debug3_f("loaded %lu keys from %s", ctx.num_loaded, host); 285 1.19 christos } 286 1.19 christos 287 1.19 christos void 288 1.19 christos load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path, 289 1.19 christos u_int note) 290 1.19 christos { 291 1.19 christos FILE *f; 292 1.19 christos 293 1.19 christos if ((f = fopen(path, "r")) == NULL) { 294 1.19 christos debug_f("fopen %s: %s", path, strerror(errno)); 295 1.19 christos return; 296 1.19 christos } 297 1.19 christos 298 1.19 christos load_hostkeys_file(hostkeys, host, path, f, note); 299 1.19 christos fclose(f); 300 1.7 christos } 301 1.4 christos 302 1.4 christos void 303 1.4 christos free_hostkeys(struct hostkeys *hostkeys) 304 1.4 christos { 305 1.4 christos u_int i; 306 1.1 christos 307 1.4 christos for (i = 0; i < hostkeys->num_entries; i++) { 308 1.5 christos free(hostkeys->entries[i].host); 309 1.5 christos free(hostkeys->entries[i].file); 310 1.7 christos sshkey_free(hostkeys->entries[i].key); 311 1.6 christos explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries)); 312 1.4 christos } 313 1.5 christos free(hostkeys->entries); 314 1.17 christos freezero(hostkeys, sizeof(*hostkeys)); 315 1.4 christos } 316 1.4 christos 317 1.4 christos static int 318 1.7 christos check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k) 319 1.4 christos { 320 1.7 christos int is_cert = sshkey_is_cert(k); 321 1.4 christos u_int i; 322 1.4 christos 323 1.4 christos for (i = 0; i < hostkeys->num_entries; i++) { 324 1.4 christos if (hostkeys->entries[i].marker != MRK_REVOKE) 325 1.1 christos continue; 326 1.7 christos if (sshkey_equal_public(k, hostkeys->entries[i].key)) 327 1.4 christos return -1; 328 1.17 christos if (is_cert && k != NULL && 329 1.7 christos sshkey_equal_public(k->cert->signature_key, 330 1.4 christos hostkeys->entries[i].key)) 331 1.4 christos return -1; 332 1.4 christos } 333 1.4 christos return 0; 334 1.4 christos } 335 1.4 christos 336 1.4 christos /* 337 1.4 christos * Match keys against a specified key, or look one up by key type. 338 1.4 christos * 339 1.4 christos * If looking for a keytype (key == NULL) and one is found then return 340 1.4 christos * HOST_FOUND, otherwise HOST_NEW. 341 1.4 christos * 342 1.4 christos * If looking for a key (key != NULL): 343 1.4 christos * 1. If the key is a cert and a matching CA is found, return HOST_OK 344 1.4 christos * 2. If the key is not a cert and a matching key is found, return HOST_OK 345 1.4 christos * 3. If no key matches but a key with a different type is found, then 346 1.4 christos * return HOST_CHANGED 347 1.4 christos * 4. If no matching keys are found, then return HOST_NEW. 348 1.4 christos * 349 1.4 christos * Finally, check any found key is not revoked. 350 1.4 christos */ 351 1.4 christos static HostStatus 352 1.4 christos check_hostkeys_by_key_or_type(struct hostkeys *hostkeys, 353 1.19 christos struct sshkey *k, int keytype, int nid, const struct hostkey_entry **found) 354 1.4 christos { 355 1.4 christos u_int i; 356 1.4 christos HostStatus end_return = HOST_NEW; 357 1.7 christos int want_cert = sshkey_is_cert(k); 358 1.4 christos HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE; 359 1.4 christos 360 1.4 christos if (found != NULL) 361 1.4 christos *found = NULL; 362 1.1 christos 363 1.4 christos for (i = 0; i < hostkeys->num_entries; i++) { 364 1.4 christos if (hostkeys->entries[i].marker != want_marker) 365 1.4 christos continue; 366 1.4 christos if (k == NULL) { 367 1.4 christos if (hostkeys->entries[i].key->type != keytype) 368 1.4 christos continue; 369 1.19 christos if (nid != -1 && 370 1.19 christos sshkey_type_plain(keytype) == KEY_ECDSA && 371 1.19 christos hostkeys->entries[i].key->ecdsa_nid != nid) 372 1.19 christos continue; 373 1.4 christos end_return = HOST_FOUND; 374 1.4 christos if (found != NULL) 375 1.4 christos *found = hostkeys->entries + i; 376 1.4 christos k = hostkeys->entries[i].key; 377 1.4 christos break; 378 1.4 christos } 379 1.4 christos if (want_cert) { 380 1.7 christos if (sshkey_equal_public(k->cert->signature_key, 381 1.4 christos hostkeys->entries[i].key)) { 382 1.4 christos /* A matching CA exists */ 383 1.4 christos end_return = HOST_OK; 384 1.4 christos if (found != NULL) 385 1.4 christos *found = hostkeys->entries + i; 386 1.4 christos break; 387 1.3 adam } 388 1.4 christos } else { 389 1.7 christos if (sshkey_equal(k, hostkeys->entries[i].key)) { 390 1.4 christos end_return = HOST_OK; 391 1.4 christos if (found != NULL) 392 1.4 christos *found = hostkeys->entries + i; 393 1.4 christos break; 394 1.3 adam } 395 1.20 christos /* A non-matching key exists */ 396 1.4 christos end_return = HOST_CHANGED; 397 1.4 christos if (found != NULL) 398 1.4 christos *found = hostkeys->entries + i; 399 1.3 adam } 400 1.1 christos } 401 1.4 christos if (check_key_not_revoked(hostkeys, k) != 0) { 402 1.4 christos end_return = HOST_REVOKED; 403 1.4 christos if (found != NULL) 404 1.4 christos *found = NULL; 405 1.4 christos } 406 1.1 christos return end_return; 407 1.1 christos } 408 1.7 christos 409 1.1 christos HostStatus 410 1.7 christos check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key, 411 1.4 christos const struct hostkey_entry **found) 412 1.1 christos { 413 1.1 christos if (key == NULL) 414 1.1 christos fatal("no key to look up"); 415 1.19 christos return check_hostkeys_by_key_or_type(hostkeys, key, 0, -1, found); 416 1.1 christos } 417 1.1 christos 418 1.1 christos int 419 1.19 christos lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype, int nid, 420 1.4 christos const struct hostkey_entry **found) 421 1.1 christos { 422 1.19 christos return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype, nid, 423 1.4 christos found) == HOST_FOUND); 424 1.1 christos } 425 1.1 christos 426 1.18 christos int 427 1.18 christos lookup_marker_in_hostkeys(struct hostkeys *hostkeys, int want_marker) 428 1.18 christos { 429 1.18 christos u_int i; 430 1.18 christos 431 1.18 christos for (i = 0; i < hostkeys->num_entries; i++) { 432 1.18 christos if (hostkeys->entries[i].marker == (HostkeyMarker)want_marker) 433 1.18 christos return 1; 434 1.18 christos } 435 1.18 christos return 0; 436 1.18 christos } 437 1.18 christos 438 1.7 christos static int 439 1.24 christos format_host_entry(struct sshbuf *entry, const char *host, const char *ip, 440 1.7 christos const struct sshkey *key, int store_hash) 441 1.7 christos { 442 1.7 christos int r, success = 0; 443 1.10 christos char *hashed_host = NULL, *lhost; 444 1.10 christos 445 1.10 christos lhost = xstrdup(host); 446 1.10 christos lowercase(lhost); 447 1.7 christos 448 1.7 christos if (store_hash) { 449 1.10 christos if ((hashed_host = host_hash(lhost, NULL, 0)) == NULL) { 450 1.19 christos error_f("host_hash failed"); 451 1.10 christos free(lhost); 452 1.7 christos return 0; 453 1.7 christos } 454 1.24 christos if ((r = sshbuf_putf(entry, "%s ", hashed_host)) != 0) 455 1.24 christos fatal_fr(r, "sshbuf_putf"); 456 1.24 christos } else if (ip != NULL) { 457 1.24 christos if ((r = sshbuf_putf(entry, "%s,%s ", lhost, ip)) != 0) 458 1.24 christos fatal_fr(r, "sshbuf_putf"); 459 1.24 christos } else { 460 1.24 christos if ((r = sshbuf_putf(entry, "%s ", lhost)) != 0) 461 1.24 christos fatal_fr(r, "sshbuf_putf"); 462 1.10 christos } 463 1.22 christos free(hashed_host); 464 1.10 christos free(lhost); 465 1.24 christos if ((r = sshkey_format_text(key, entry)) == 0) 466 1.7 christos success = 1; 467 1.7 christos else 468 1.19 christos error_fr(r, "sshkey_write"); 469 1.24 christos if ((r = sshbuf_putf(entry, "\n")) != 0) 470 1.24 christos fatal_fr(r, "sshbuf_putf"); 471 1.24 christos 472 1.19 christos /* If hashing is enabled, the IP address needs to go on its own line */ 473 1.19 christos if (success && store_hash && ip != NULL) 474 1.24 christos success = format_host_entry(entry, ip, NULL, key, 1); 475 1.24 christos return success; 476 1.24 christos } 477 1.24 christos 478 1.24 christos static int 479 1.24 christos write_host_entry(FILE *f, const char *host, const char *ip, 480 1.24 christos const struct sshkey *key, int store_hash) 481 1.24 christos { 482 1.24 christos int r, success = 0; 483 1.24 christos struct sshbuf *entry = NULL; 484 1.24 christos 485 1.24 christos if ((entry = sshbuf_new()) == NULL) 486 1.24 christos fatal_f("allocation failed"); 487 1.24 christos if ((r = format_host_entry(entry, host, ip, key, store_hash)) != 1) { 488 1.24 christos debug_f("failed to format host entry"); 489 1.24 christos goto out; 490 1.24 christos } 491 1.24 christos if ((r = fwrite(sshbuf_ptr(entry), sshbuf_len(entry), 1, f)) != 1) { 492 1.24 christos error_f("fwrite: %s", strerror(errno)); 493 1.24 christos goto out; 494 1.24 christos } 495 1.24 christos success = 1; 496 1.24 christos out: 497 1.24 christos sshbuf_free(entry); 498 1.7 christos return success; 499 1.7 christos } 500 1.7 christos 501 1.1 christos /* 502 1.18 christos * Create user ~/.ssh directory if it doesn't exist and we want to write to it. 503 1.18 christos * If notify is set, a message will be emitted if the directory is created. 504 1.18 christos */ 505 1.18 christos void 506 1.18 christos hostfile_create_user_ssh_dir(const char *filename, int notify) 507 1.18 christos { 508 1.18 christos char *dotsshdir = NULL, *p; 509 1.18 christos size_t len; 510 1.18 christos struct stat st; 511 1.18 christos 512 1.18 christos if ((p = strrchr(filename, '/')) == NULL) 513 1.18 christos return; 514 1.18 christos len = p - filename; 515 1.18 christos dotsshdir = tilde_expand_filename("~/" _PATH_SSH_USER_DIR, getuid()); 516 1.18 christos if (strlen(dotsshdir) > len || strncmp(filename, dotsshdir, len) != 0) 517 1.18 christos goto out; /* not ~/.ssh prefixed */ 518 1.18 christos if (stat(dotsshdir, &st) == 0) 519 1.18 christos goto out; /* dir already exists */ 520 1.18 christos else if (errno != ENOENT) 521 1.18 christos error("Could not stat %s: %s", dotsshdir, strerror(errno)); 522 1.18 christos else { 523 1.18 christos if (mkdir(dotsshdir, 0700) == -1) 524 1.18 christos error("Could not create directory '%.200s' (%s).", 525 1.18 christos dotsshdir, strerror(errno)); 526 1.18 christos else if (notify) 527 1.18 christos logit("Created directory '%s'.", dotsshdir); 528 1.18 christos } 529 1.18 christos out: 530 1.18 christos free(dotsshdir); 531 1.18 christos } 532 1.18 christos 533 1.18 christos 534 1.18 christos /* 535 1.1 christos * Appends an entry to the host file. Returns false if the entry could not 536 1.1 christos * be appended. 537 1.1 christos */ 538 1.1 christos int 539 1.7 christos add_host_to_hostfile(const char *filename, const char *host, 540 1.7 christos const struct sshkey *key, int store_hash) 541 1.1 christos { 542 1.1 christos FILE *f; 543 1.23 christos int success, addnl = 0; 544 1.1 christos 545 1.1 christos if (key == NULL) 546 1.1 christos return 1; /* XXX ? */ 547 1.18 christos hostfile_create_user_ssh_dir(filename, 0); 548 1.24 christos if ((f = fopen(filename, "a+")) == NULL) 549 1.1 christos return 0; 550 1.24 christos setvbuf(f, NULL, _IONBF, 0); 551 1.23 christos /* Make sure we have a terminating newline. */ 552 1.23 christos if (fseek(f, -1L, SEEK_END) == 0 && fgetc(f) != '\n') 553 1.23 christos addnl = 1; 554 1.23 christos if (fseek(f, 0L, SEEK_END) != 0 || (addnl && fputc('\n', f) != '\n')) { 555 1.23 christos error("Failed to add terminating newline to %s: %s", 556 1.23 christos filename, strerror(errno)); 557 1.23 christos fclose(f); 558 1.23 christos return 0; 559 1.23 christos } 560 1.7 christos success = write_host_entry(f, host, NULL, key, store_hash); 561 1.7 christos fclose(f); 562 1.7 christos return success; 563 1.7 christos } 564 1.1 christos 565 1.7 christos struct host_delete_ctx { 566 1.7 christos FILE *out; 567 1.7 christos int quiet; 568 1.19 christos const char *host, *ip; 569 1.19 christos u_int *match_keys; /* mask of HKF_MATCH_* for this key */ 570 1.7 christos struct sshkey * const *keys; 571 1.7 christos size_t nkeys; 572 1.7 christos int modified; 573 1.7 christos }; 574 1.7 christos 575 1.7 christos static int 576 1.7 christos host_delete(struct hostkey_foreach_line *l, void *_ctx) 577 1.7 christos { 578 1.7 christos struct host_delete_ctx *ctx = (struct host_delete_ctx *)_ctx; 579 1.7 christos int loglevel = ctx->quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_VERBOSE; 580 1.7 christos size_t i; 581 1.7 christos 582 1.19 christos /* Don't remove CA and revocation lines */ 583 1.19 christos if (l->status == HKF_STATUS_MATCHED && l->marker == MRK_NONE) { 584 1.7 christos /* 585 1.7 christos * If this line contains one of the keys that we will be 586 1.7 christos * adding later, then don't change it and mark the key for 587 1.7 christos * skipping. 588 1.7 christos */ 589 1.7 christos for (i = 0; i < ctx->nkeys; i++) { 590 1.19 christos if (!sshkey_equal(ctx->keys[i], l->key)) 591 1.19 christos continue; 592 1.19 christos ctx->match_keys[i] |= l->match; 593 1.19 christos fprintf(ctx->out, "%s\n", l->line); 594 1.19 christos debug3_f("%s key already at %s:%ld", 595 1.19 christos sshkey_type(l->key), l->path, l->linenum); 596 1.19 christos return 0; 597 1.7 christos } 598 1.7 christos 599 1.7 christos /* 600 1.7 christos * Hostname matches and has no CA/revoke marker, delete it 601 1.7 christos * by *not* writing the line to ctx->out. 602 1.7 christos */ 603 1.7 christos do_log2(loglevel, "%s%s%s:%ld: Removed %s key for host %s", 604 1.7 christos ctx->quiet ? __func__ : "", ctx->quiet ? ": " : "", 605 1.7 christos l->path, l->linenum, sshkey_type(l->key), ctx->host); 606 1.7 christos ctx->modified = 1; 607 1.7 christos return 0; 608 1.7 christos } 609 1.7 christos /* Retain non-matching hosts and invalid lines when deleting */ 610 1.7 christos if (l->status == HKF_STATUS_INVALID) { 611 1.7 christos do_log2(loglevel, "%s%s%s:%ld: invalid known_hosts entry", 612 1.7 christos ctx->quiet ? __func__ : "", ctx->quiet ? ": " : "", 613 1.7 christos l->path, l->linenum); 614 1.7 christos } 615 1.7 christos fprintf(ctx->out, "%s\n", l->line); 616 1.7 christos return 0; 617 1.7 christos } 618 1.7 christos 619 1.7 christos int 620 1.7 christos hostfile_replace_entries(const char *filename, const char *host, const char *ip, 621 1.7 christos struct sshkey **keys, size_t nkeys, int store_hash, int quiet, int hash_alg) 622 1.7 christos { 623 1.7 christos int r, fd, oerrno = 0; 624 1.7 christos int loglevel = quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_VERBOSE; 625 1.7 christos struct host_delete_ctx ctx; 626 1.25 christos char *fp = NULL, *temp = NULL, *back = NULL; 627 1.19 christos const char *what; 628 1.7 christos mode_t omask; 629 1.7 christos size_t i; 630 1.19 christos u_int want; 631 1.7 christos 632 1.7 christos omask = umask(077); 633 1.7 christos 634 1.7 christos memset(&ctx, 0, sizeof(ctx)); 635 1.7 christos ctx.host = host; 636 1.19 christos ctx.ip = ip; 637 1.7 christos ctx.quiet = quiet; 638 1.19 christos 639 1.19 christos if ((ctx.match_keys = calloc(nkeys, sizeof(*ctx.match_keys))) == NULL) 640 1.7 christos return SSH_ERR_ALLOC_FAIL; 641 1.7 christos ctx.keys = keys; 642 1.7 christos ctx.nkeys = nkeys; 643 1.7 christos ctx.modified = 0; 644 1.7 christos 645 1.7 christos /* 646 1.7 christos * Prepare temporary file for in-place deletion. 647 1.7 christos */ 648 1.15 christos if ((r = asprintf(&temp, "%s.XXXXXXXXXXX", filename)) == -1 || 649 1.15 christos (r = asprintf(&back, "%s.old", filename)) == -1) { 650 1.7 christos r = SSH_ERR_ALLOC_FAIL; 651 1.7 christos goto fail; 652 1.7 christos } 653 1.7 christos 654 1.7 christos if ((fd = mkstemp(temp)) == -1) { 655 1.7 christos oerrno = errno; 656 1.19 christos error_f("mkstemp: %s", strerror(oerrno)); 657 1.7 christos r = SSH_ERR_SYSTEM_ERROR; 658 1.7 christos goto fail; 659 1.7 christos } 660 1.7 christos if ((ctx.out = fdopen(fd, "w")) == NULL) { 661 1.7 christos oerrno = errno; 662 1.7 christos close(fd); 663 1.19 christos error_f("fdopen: %s", strerror(oerrno)); 664 1.7 christos r = SSH_ERR_SYSTEM_ERROR; 665 1.7 christos goto fail; 666 1.7 christos } 667 1.7 christos 668 1.19 christos /* Remove stale/mismatching entries for the specified host */ 669 1.7 christos if ((r = hostkeys_foreach(filename, host_delete, &ctx, host, ip, 670 1.19 christos HKF_WANT_PARSE_KEY, 0)) != 0) { 671 1.16 christos oerrno = errno; 672 1.19 christos error_fr(r, "hostkeys_foreach"); 673 1.7 christos goto fail; 674 1.7 christos } 675 1.7 christos 676 1.19 christos /* Re-add the requested keys */ 677 1.19 christos want = HKF_MATCH_HOST | (ip == NULL ? 0 : HKF_MATCH_IP); 678 1.7 christos for (i = 0; i < nkeys; i++) { 679 1.22 christos if (keys[i] == NULL || (want & ctx.match_keys[i]) == want) 680 1.7 christos continue; 681 1.7 christos if ((fp = sshkey_fingerprint(keys[i], hash_alg, 682 1.7 christos SSH_FP_DEFAULT)) == NULL) { 683 1.7 christos r = SSH_ERR_ALLOC_FAIL; 684 1.7 christos goto fail; 685 1.7 christos } 686 1.19 christos /* write host/ip */ 687 1.19 christos what = ""; 688 1.19 christos if (ctx.match_keys[i] == 0) { 689 1.19 christos what = "Adding new key"; 690 1.19 christos if (!write_host_entry(ctx.out, host, ip, 691 1.19 christos keys[i], store_hash)) { 692 1.19 christos r = SSH_ERR_INTERNAL_ERROR; 693 1.19 christos goto fail; 694 1.19 christos } 695 1.19 christos } else if ((want & ~ctx.match_keys[i]) == HKF_MATCH_HOST) { 696 1.19 christos what = "Fixing match (hostname)"; 697 1.19 christos if (!write_host_entry(ctx.out, host, NULL, 698 1.19 christos keys[i], store_hash)) { 699 1.19 christos r = SSH_ERR_INTERNAL_ERROR; 700 1.19 christos goto fail; 701 1.19 christos } 702 1.19 christos } else if ((want & ~ctx.match_keys[i]) == HKF_MATCH_IP) { 703 1.19 christos what = "Fixing match (address)"; 704 1.19 christos if (!write_host_entry(ctx.out, ip, NULL, 705 1.19 christos keys[i], store_hash)) { 706 1.19 christos r = SSH_ERR_INTERNAL_ERROR; 707 1.19 christos goto fail; 708 1.19 christos } 709 1.19 christos } 710 1.19 christos do_log2(loglevel, "%s%s%s for %s%s%s to %s: %s %s", 711 1.19 christos quiet ? __func__ : "", quiet ? ": " : "", what, 712 1.19 christos host, ip == NULL ? "" : ",", ip == NULL ? "" : ip, filename, 713 1.7 christos sshkey_ssh_name(keys[i]), fp); 714 1.7 christos free(fp); 715 1.25 christos fp = NULL; 716 1.7 christos ctx.modified = 1; 717 1.1 christos } 718 1.7 christos fclose(ctx.out); 719 1.7 christos ctx.out = NULL; 720 1.1 christos 721 1.7 christos if (ctx.modified) { 722 1.7 christos /* Backup the original file and replace it with the temporary */ 723 1.7 christos if (unlink(back) == -1 && errno != ENOENT) { 724 1.7 christos oerrno = errno; 725 1.19 christos error_f("unlink %.100s: %s", back, strerror(errno)); 726 1.7 christos r = SSH_ERR_SYSTEM_ERROR; 727 1.7 christos goto fail; 728 1.7 christos } 729 1.7 christos if (link(filename, back) == -1) { 730 1.7 christos oerrno = errno; 731 1.19 christos error_f("link %.100s to %.100s: %s", filename, 732 1.19 christos back, strerror(errno)); 733 1.7 christos r = SSH_ERR_SYSTEM_ERROR; 734 1.7 christos goto fail; 735 1.7 christos } 736 1.7 christos if (rename(temp, filename) == -1) { 737 1.7 christos oerrno = errno; 738 1.19 christos error_f("rename \"%s\" to \"%s\": %s", temp, 739 1.19 christos filename, strerror(errno)); 740 1.7 christos r = SSH_ERR_SYSTEM_ERROR; 741 1.7 christos goto fail; 742 1.7 christos } 743 1.1 christos } else { 744 1.7 christos /* No changes made; just delete the temporary file */ 745 1.7 christos if (unlink(temp) != 0) 746 1.19 christos error_f("unlink \"%s\": %s", temp, strerror(errno)); 747 1.7 christos } 748 1.7 christos 749 1.7 christos /* success */ 750 1.7 christos r = 0; 751 1.7 christos fail: 752 1.7 christos if (temp != NULL && r != 0) 753 1.7 christos unlink(temp); 754 1.7 christos free(temp); 755 1.7 christos free(back); 756 1.25 christos free(fp); 757 1.7 christos if (ctx.out != NULL) 758 1.7 christos fclose(ctx.out); 759 1.19 christos free(ctx.match_keys); 760 1.7 christos umask(omask); 761 1.7 christos if (r == SSH_ERR_SYSTEM_ERROR) 762 1.7 christos errno = oerrno; 763 1.7 christos return r; 764 1.7 christos } 765 1.7 christos 766 1.7 christos static int 767 1.7 christos match_maybe_hashed(const char *host, const char *names, int *was_hashed) 768 1.7 christos { 769 1.22 christos int hashed = *names == HASH_DELIM, ret; 770 1.22 christos char *hashed_host = NULL; 771 1.7 christos size_t nlen = strlen(names); 772 1.7 christos 773 1.7 christos if (was_hashed != NULL) 774 1.7 christos *was_hashed = hashed; 775 1.7 christos if (hashed) { 776 1.7 christos if ((hashed_host = host_hash(host, names, nlen)) == NULL) 777 1.7 christos return -1; 778 1.22 christos ret = (nlen == strlen(hashed_host) && 779 1.22 christos strncmp(hashed_host, names, nlen) == 0); 780 1.22 christos free(hashed_host); 781 1.22 christos return ret; 782 1.1 christos } 783 1.8 christos return match_hostname(host, names) == 1; 784 1.7 christos } 785 1.7 christos 786 1.7 christos int 787 1.19 christos hostkeys_foreach_file(const char *path, FILE *f, hostkeys_foreach_fn *callback, 788 1.19 christos void *ctx, const char *host, const char *ip, u_int options, u_int note) 789 1.7 christos { 790 1.12 christos char *line = NULL, ktype[128]; 791 1.7 christos u_long linenum = 0; 792 1.7 christos char *cp, *cp2; 793 1.7 christos u_int kbits; 794 1.7 christos int hashed; 795 1.7 christos int s, r = 0; 796 1.7 christos struct hostkey_foreach_line lineinfo; 797 1.12 christos size_t linesize = 0, l; 798 1.7 christos 799 1.7 christos memset(&lineinfo, 0, sizeof(lineinfo)); 800 1.7 christos if (host == NULL && (options & HKF_WANT_MATCH) != 0) 801 1.7 christos return SSH_ERR_INVALID_ARGUMENT; 802 1.7 christos 803 1.12 christos while (getline(&line, &linesize, f) != -1) { 804 1.12 christos linenum++; 805 1.7 christos line[strcspn(line, "\n")] = '\0'; 806 1.7 christos 807 1.12 christos free(lineinfo.line); 808 1.7 christos sshkey_free(lineinfo.key); 809 1.7 christos memset(&lineinfo, 0, sizeof(lineinfo)); 810 1.7 christos lineinfo.path = path; 811 1.7 christos lineinfo.linenum = linenum; 812 1.12 christos lineinfo.line = xstrdup(line); 813 1.7 christos lineinfo.marker = MRK_NONE; 814 1.7 christos lineinfo.status = HKF_STATUS_OK; 815 1.7 christos lineinfo.keytype = KEY_UNSPEC; 816 1.19 christos lineinfo.note = note; 817 1.7 christos 818 1.7 christos /* Skip any leading whitespace, comments and empty lines. */ 819 1.7 christos for (cp = line; *cp == ' ' || *cp == '\t'; cp++) 820 1.7 christos ; 821 1.7 christos if (!*cp || *cp == '#' || *cp == '\n') { 822 1.7 christos if ((options & HKF_WANT_MATCH) == 0) { 823 1.7 christos lineinfo.status = HKF_STATUS_COMMENT; 824 1.7 christos if ((r = callback(&lineinfo, ctx)) != 0) 825 1.7 christos break; 826 1.7 christos } 827 1.7 christos continue; 828 1.7 christos } 829 1.7 christos 830 1.7 christos if ((lineinfo.marker = check_markers(&cp)) == MRK_ERROR) { 831 1.19 christos verbose_f("invalid marker at %s:%lu", path, linenum); 832 1.7 christos if ((options & HKF_WANT_MATCH) == 0) 833 1.7 christos goto bad; 834 1.7 christos continue; 835 1.7 christos } 836 1.7 christos 837 1.7 christos /* Find the end of the host name portion. */ 838 1.7 christos for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) 839 1.7 christos ; 840 1.24 christos if (*cp2 == '\0') { 841 1.24 christos verbose_f("truncated line at %s:%lu", path, linenum); 842 1.24 christos if ((options & HKF_WANT_MATCH) == 0) 843 1.24 christos goto bad; 844 1.24 christos continue; 845 1.24 christos } 846 1.7 christos lineinfo.hosts = cp; 847 1.7 christos *cp2++ = '\0'; 848 1.7 christos 849 1.7 christos /* Check if the host name matches. */ 850 1.7 christos if (host != NULL) { 851 1.7 christos if ((s = match_maybe_hashed(host, lineinfo.hosts, 852 1.7 christos &hashed)) == -1) { 853 1.19 christos debug2_f("%s:%ld: bad host hash \"%.32s\"", 854 1.19 christos path, linenum, lineinfo.hosts); 855 1.7 christos goto bad; 856 1.7 christos } 857 1.7 christos if (s == 1) { 858 1.7 christos lineinfo.status = HKF_STATUS_MATCHED; 859 1.7 christos lineinfo.match |= HKF_MATCH_HOST | 860 1.7 christos (hashed ? HKF_MATCH_HOST_HASHED : 0); 861 1.7 christos } 862 1.7 christos /* Try matching IP address if supplied */ 863 1.7 christos if (ip != NULL) { 864 1.7 christos if ((s = match_maybe_hashed(ip, lineinfo.hosts, 865 1.7 christos &hashed)) == -1) { 866 1.19 christos debug2_f("%s:%ld: bad ip hash " 867 1.19 christos "\"%.32s\"", path, linenum, 868 1.19 christos lineinfo.hosts); 869 1.7 christos goto bad; 870 1.7 christos } 871 1.7 christos if (s == 1) { 872 1.7 christos lineinfo.status = HKF_STATUS_MATCHED; 873 1.7 christos lineinfo.match |= HKF_MATCH_IP | 874 1.7 christos (hashed ? HKF_MATCH_IP_HASHED : 0); 875 1.7 christos } 876 1.7 christos } 877 1.7 christos /* 878 1.7 christos * Skip this line if host matching requested and 879 1.7 christos * neither host nor address matched. 880 1.7 christos */ 881 1.7 christos if ((options & HKF_WANT_MATCH) != 0 && 882 1.7 christos lineinfo.status != HKF_STATUS_MATCHED) 883 1.7 christos continue; 884 1.7 christos } 885 1.7 christos 886 1.7 christos /* Got a match. Skip host name and any following whitespace */ 887 1.7 christos for (; *cp2 == ' ' || *cp2 == '\t'; cp2++) 888 1.7 christos ; 889 1.7 christos if (*cp2 == '\0' || *cp2 == '#') { 890 1.7 christos debug2("%s:%ld: truncated before key type", 891 1.7 christos path, linenum); 892 1.7 christos goto bad; 893 1.7 christos } 894 1.7 christos lineinfo.rawkey = cp = cp2; 895 1.7 christos 896 1.7 christos if ((options & HKF_WANT_PARSE_KEY) != 0) { 897 1.7 christos /* 898 1.7 christos * Extract the key from the line. This will skip 899 1.7 christos * any leading whitespace. Ignore badly formatted 900 1.7 christos * lines. 901 1.7 christos */ 902 1.7 christos if ((lineinfo.key = sshkey_new(KEY_UNSPEC)) == NULL) { 903 1.19 christos error_f("sshkey_new failed"); 904 1.7 christos r = SSH_ERR_ALLOC_FAIL; 905 1.7 christos break; 906 1.7 christos } 907 1.7 christos if (!hostfile_read_key(&cp, &kbits, lineinfo.key)) { 908 1.7 christos goto bad; 909 1.7 christos } 910 1.7 christos lineinfo.keytype = lineinfo.key->type; 911 1.7 christos lineinfo.comment = cp; 912 1.7 christos } else { 913 1.7 christos /* Extract and parse key type */ 914 1.7 christos l = strcspn(lineinfo.rawkey, " \t"); 915 1.7 christos if (l <= 1 || l >= sizeof(ktype) || 916 1.7 christos lineinfo.rawkey[l] == '\0') 917 1.7 christos goto bad; 918 1.7 christos memcpy(ktype, lineinfo.rawkey, l); 919 1.7 christos ktype[l] = '\0'; 920 1.7 christos lineinfo.keytype = sshkey_type_from_name(ktype); 921 1.8 christos 922 1.7 christos /* 923 1.11 christos * Assume legacy RSA1 if the first component is a short 924 1.7 christos * decimal number. 925 1.7 christos */ 926 1.7 christos if (lineinfo.keytype == KEY_UNSPEC && l < 8 && 927 1.7 christos strspn(ktype, "0123456789") == l) 928 1.11 christos goto bad; 929 1.8 christos 930 1.7 christos /* 931 1.7 christos * Check that something other than whitespace follows 932 1.7 christos * the key type. This won't catch all corruption, but 933 1.7 christos * it does catch trivial truncation. 934 1.7 christos */ 935 1.7 christos cp2 += l; /* Skip past key type */ 936 1.7 christos for (; *cp2 == ' ' || *cp2 == '\t'; cp2++) 937 1.7 christos ; 938 1.7 christos if (*cp2 == '\0' || *cp2 == '#') { 939 1.7 christos debug2("%s:%ld: truncated after key type", 940 1.7 christos path, linenum); 941 1.7 christos lineinfo.keytype = KEY_UNSPEC; 942 1.7 christos } 943 1.7 christos if (lineinfo.keytype == KEY_UNSPEC) { 944 1.7 christos bad: 945 1.7 christos sshkey_free(lineinfo.key); 946 1.7 christos lineinfo.key = NULL; 947 1.7 christos lineinfo.status = HKF_STATUS_INVALID; 948 1.7 christos if ((r = callback(&lineinfo, ctx)) != 0) 949 1.7 christos break; 950 1.7 christos continue; 951 1.7 christos } 952 1.7 christos } 953 1.7 christos if ((r = callback(&lineinfo, ctx)) != 0) 954 1.7 christos break; 955 1.7 christos } 956 1.7 christos sshkey_free(lineinfo.key); 957 1.12 christos free(lineinfo.line); 958 1.12 christos free(line); 959 1.19 christos return r; 960 1.19 christos } 961 1.19 christos 962 1.19 christos int 963 1.19 christos hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx, 964 1.19 christos const char *host, const char *ip, u_int options, u_int note) 965 1.19 christos { 966 1.19 christos FILE *f; 967 1.19 christos int r, oerrno; 968 1.19 christos 969 1.19 christos if ((f = fopen(path, "r")) == NULL) 970 1.19 christos return SSH_ERR_SYSTEM_ERROR; 971 1.19 christos 972 1.19 christos debug3_f("reading file \"%s\"", path); 973 1.19 christos r = hostkeys_foreach_file(path, f, callback, ctx, host, ip, 974 1.19 christos options, note); 975 1.19 christos oerrno = errno; 976 1.1 christos fclose(f); 977 1.19 christos errno = oerrno; 978 1.7 christos return r; 979 1.1 christos } 980