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