1 /* $OpenBSD: hostfile.c,v 1.95 2023/02/21 06:48:18 dtucker Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Functions for manipulating the known hosts files.
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".
15 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
16 * Copyright (c) 1999 Niels Provos. All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
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.
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.
41 #include <sys/types.h>
44 #include <netinet/in.h>
60 #include "pathnames.h"
66 /* XXX hmac is too easy to dictionary attack; use bcrypt? */
69 extract_salt(const char *s
, u_int l
, u_char
*salt
, size_t salt_len
)
75 if (l
< sizeof(HASH_MAGIC
) - 1) {
76 debug2("extract_salt: string too short");
79 if (strncmp(s
, HASH_MAGIC
, sizeof(HASH_MAGIC
) - 1) != 0) {
80 debug2("extract_salt: invalid magic identifier");
83 s
+= sizeof(HASH_MAGIC
) - 1;
84 l
-= sizeof(HASH_MAGIC
) - 1;
85 if ((p
= memchr(s
, HASH_DELIM
, l
)) == NULL
) {
86 debug2("extract_salt: missing salt termination character");
92 if (b64len
== 0 || b64len
> 1024) {
93 debug2("extract_salt: bad encoded salt length %u", b64len
);
96 b64salt
= xmalloc(1 + b64len
);
97 memcpy(b64salt
, s
, b64len
);
98 b64salt
[b64len
] = '\0';
100 ret
= __b64_pton(b64salt
, salt
, salt_len
);
103 debug2("extract_salt: salt decode error");
106 if (ret
!= (int)ssh_hmac_bytes(SSH_DIGEST_SHA1
)) {
107 debug2("extract_salt: expected salt len %zd, got %d",
108 ssh_hmac_bytes(SSH_DIGEST_SHA1
), ret
);
116 host_hash(const char *host
, const char *name_from_hostfile
, u_int src_len
)
118 struct ssh_hmac_ctx
*ctx
;
119 u_char salt
[256], result
[256];
120 char uu_salt
[512], uu_result
[512];
121 char *encoded
= NULL
;
124 len
= ssh_digest_bytes(SSH_DIGEST_SHA1
);
126 if (name_from_hostfile
== NULL
) {
127 /* Create new salt */
128 arc4random_buf(salt
, len
);
130 /* Extract salt from known host entry */
131 if (extract_salt(name_from_hostfile
, src_len
, salt
,
136 if ((ctx
= ssh_hmac_start(SSH_DIGEST_SHA1
)) == NULL
||
137 ssh_hmac_init(ctx
, salt
, len
) < 0 ||
138 ssh_hmac_update(ctx
, host
, strlen(host
)) < 0 ||
139 ssh_hmac_final(ctx
, result
, sizeof(result
)))
140 fatal_f("ssh_hmac failed");
143 if (__b64_ntop(salt
, len
, uu_salt
, sizeof(uu_salt
)) == -1 ||
144 __b64_ntop(result
, len
, uu_result
, sizeof(uu_result
)) == -1)
145 fatal_f("__b64_ntop failed");
146 xasprintf(&encoded
, "%s%s%c%s", HASH_MAGIC
, uu_salt
, HASH_DELIM
,
153 * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the
154 * pointer over the key. Skips any whitespace at the beginning and at end.
158 hostfile_read_key(char **cpp
, u_int
*bitsp
, struct sshkey
*ret
)
162 /* Skip leading whitespace. */
163 for (cp
= *cpp
; *cp
== ' ' || *cp
== '\t'; cp
++)
166 if (sshkey_read(ret
, &cp
) != 0)
169 /* Skip trailing whitespace. */
170 for (; *cp
== ' ' || *cp
== '\t'; cp
++)
173 /* Return results. */
176 *bitsp
= sshkey_size(ret
);
181 check_markers(char **cpp
)
183 char marker
[32], *sp
, *cp
= *cpp
;
187 /* Only one marker is allowed */
190 /* Markers are terminated by whitespace */
191 if ((sp
= strchr(cp
, ' ')) == NULL
&&
192 (sp
= strchr(cp
, '\t')) == NULL
)
194 /* Extract marker for comparison */
195 if (sp
<= cp
+ 1 || sp
>= cp
+ sizeof(marker
))
197 memcpy(marker
, cp
, sp
- cp
);
198 marker
[sp
- cp
] = '\0';
199 if (strcmp(marker
, CA_MARKER
) == 0)
201 else if (strcmp(marker
, REVOKE_MARKER
) == 0)
206 /* Skip past marker and any whitespace that follows it */
208 for (; *cp
== ' ' || *cp
== '\t'; cp
++)
218 struct hostkeys
*ret
= xcalloc(1, sizeof(*ret
));
224 struct load_callback_ctx
{
227 struct hostkeys
*hostkeys
;
231 record_hostkey(struct hostkey_foreach_line
*l
, void *_ctx
)
233 struct load_callback_ctx
*ctx
= (struct load_callback_ctx
*)_ctx
;
234 struct hostkeys
*hostkeys
= ctx
->hostkeys
;
235 struct hostkey_entry
*tmp
;
237 if (l
->status
== HKF_STATUS_INVALID
) {
238 /* XXX make this verbose() in the future */
239 debug("%s:%ld: parse error in hostkeys file",
240 l
->path
, l
->linenum
);
244 debug3_f("found %skey type %s in file %s:%lu",
245 l
->marker
== MRK_NONE
? "" :
246 (l
->marker
== MRK_CA
? "ca " : "revoked "),
247 sshkey_type(l
->key
), l
->path
, l
->linenum
);
248 if ((tmp
= recallocarray(hostkeys
->entries
, hostkeys
->num_entries
,
249 hostkeys
->num_entries
+ 1, sizeof(*hostkeys
->entries
))) == NULL
)
250 return SSH_ERR_ALLOC_FAIL
;
251 hostkeys
->entries
= tmp
;
252 hostkeys
->entries
[hostkeys
->num_entries
].host
= xstrdup(ctx
->host
);
253 hostkeys
->entries
[hostkeys
->num_entries
].file
= xstrdup(l
->path
);
254 hostkeys
->entries
[hostkeys
->num_entries
].line
= l
->linenum
;
255 hostkeys
->entries
[hostkeys
->num_entries
].key
= l
->key
;
256 l
->key
= NULL
; /* steal it */
257 hostkeys
->entries
[hostkeys
->num_entries
].marker
= l
->marker
;
258 hostkeys
->entries
[hostkeys
->num_entries
].note
= l
->note
;
259 hostkeys
->num_entries
++;
266 load_hostkeys_file(struct hostkeys
*hostkeys
, const char *host
,
267 const char *path
, FILE *f
, u_int note
)
270 struct load_callback_ctx ctx
;
274 ctx
.hostkeys
= hostkeys
;
276 if ((r
= hostkeys_foreach_file(path
, f
, record_hostkey
, &ctx
, host
,
277 NULL
, HKF_WANT_MATCH
|HKF_WANT_PARSE_KEY
, note
)) != 0) {
278 if (r
!= SSH_ERR_SYSTEM_ERROR
&& errno
!= ENOENT
)
279 debug_fr(r
, "hostkeys_foreach failed for %s", path
);
281 if (ctx
.num_loaded
!= 0)
282 debug3_f("loaded %lu keys from %s", ctx
.num_loaded
, host
);
286 load_hostkeys(struct hostkeys
*hostkeys
, const char *host
, const char *path
,
291 if ((f
= fopen(path
, "r")) == NULL
) {
292 debug_f("fopen %s: %s", path
, strerror(errno
));
296 load_hostkeys_file(hostkeys
, host
, path
, f
, note
);
301 free_hostkeys(struct hostkeys
*hostkeys
)
305 for (i
= 0; i
< hostkeys
->num_entries
; i
++) {
306 free(hostkeys
->entries
[i
].host
);
307 free(hostkeys
->entries
[i
].file
);
308 sshkey_free(hostkeys
->entries
[i
].key
);
309 explicit_bzero(hostkeys
->entries
+ i
, sizeof(*hostkeys
->entries
));
311 free(hostkeys
->entries
);
312 freezero(hostkeys
, sizeof(*hostkeys
));
316 check_key_not_revoked(struct hostkeys
*hostkeys
, struct sshkey
*k
)
318 int is_cert
= sshkey_is_cert(k
);
321 for (i
= 0; i
< hostkeys
->num_entries
; i
++) {
322 if (hostkeys
->entries
[i
].marker
!= MRK_REVOKE
)
324 if (sshkey_equal_public(k
, hostkeys
->entries
[i
].key
))
326 if (is_cert
&& k
!= NULL
&&
327 sshkey_equal_public(k
->cert
->signature_key
,
328 hostkeys
->entries
[i
].key
))
335 * Match keys against a specified key, or look one up by key type.
337 * If looking for a keytype (key == NULL) and one is found then return
338 * HOST_FOUND, otherwise HOST_NEW.
340 * If looking for a key (key != NULL):
341 * 1. If the key is a cert and a matching CA is found, return HOST_OK
342 * 2. If the key is not a cert and a matching key is found, return HOST_OK
343 * 3. If no key matches but a key with a different type is found, then
344 * return HOST_CHANGED
345 * 4. If no matching keys are found, then return HOST_NEW.
347 * Finally, check any found key is not revoked.
350 check_hostkeys_by_key_or_type(struct hostkeys
*hostkeys
,
351 struct sshkey
*k
, int keytype
, int nid
, const struct hostkey_entry
**found
)
354 HostStatus end_return
= HOST_NEW
;
355 int want_cert
= sshkey_is_cert(k
);
356 HostkeyMarker want_marker
= want_cert
? MRK_CA
: MRK_NONE
;
361 for (i
= 0; i
< hostkeys
->num_entries
; i
++) {
362 if (hostkeys
->entries
[i
].marker
!= want_marker
)
365 if (hostkeys
->entries
[i
].key
->type
!= keytype
)
368 sshkey_type_plain(keytype
) == KEY_ECDSA
&&
369 hostkeys
->entries
[i
].key
->ecdsa_nid
!= nid
)
371 end_return
= HOST_FOUND
;
373 *found
= hostkeys
->entries
+ i
;
374 k
= hostkeys
->entries
[i
].key
;
378 if (sshkey_equal_public(k
->cert
->signature_key
,
379 hostkeys
->entries
[i
].key
)) {
380 /* A matching CA exists */
381 end_return
= HOST_OK
;
383 *found
= hostkeys
->entries
+ i
;
387 if (sshkey_equal(k
, hostkeys
->entries
[i
].key
)) {
388 end_return
= HOST_OK
;
390 *found
= hostkeys
->entries
+ i
;
393 /* A non-matching key exists */
394 end_return
= HOST_CHANGED
;
396 *found
= hostkeys
->entries
+ i
;
399 if (check_key_not_revoked(hostkeys
, k
) != 0) {
400 end_return
= HOST_REVOKED
;
408 check_key_in_hostkeys(struct hostkeys
*hostkeys
, struct sshkey
*key
,
409 const struct hostkey_entry
**found
)
412 fatal("no key to look up");
413 return check_hostkeys_by_key_or_type(hostkeys
, key
, 0, -1, found
);
417 lookup_key_in_hostkeys_by_type(struct hostkeys
*hostkeys
, int keytype
, int nid
,
418 const struct hostkey_entry
**found
)
420 return (check_hostkeys_by_key_or_type(hostkeys
, NULL
, keytype
, nid
,
421 found
) == HOST_FOUND
);
425 lookup_marker_in_hostkeys(struct hostkeys
*hostkeys
, int want_marker
)
429 for (i
= 0; i
< hostkeys
->num_entries
; i
++) {
430 if (hostkeys
->entries
[i
].marker
== (HostkeyMarker
)want_marker
)
437 write_host_entry(FILE *f
, const char *host
, const char *ip
,
438 const struct sshkey
*key
, int store_hash
)
441 char *hashed_host
= NULL
, *lhost
;
443 lhost
= xstrdup(host
);
447 if ((hashed_host
= host_hash(lhost
, NULL
, 0)) == NULL
) {
448 error_f("host_hash failed");
452 fprintf(f
, "%s ", hashed_host
);
453 } else if (ip
!= NULL
)
454 fprintf(f
, "%s,%s ", lhost
, ip
);
456 fprintf(f
, "%s ", lhost
);
460 if ((r
= sshkey_write(key
, f
)) == 0)
463 error_fr(r
, "sshkey_write");
465 /* If hashing is enabled, the IP address needs to go on its own line */
466 if (success
&& store_hash
&& ip
!= NULL
)
467 success
= write_host_entry(f
, ip
, NULL
, key
, 1);
472 * Create user ~/.ssh directory if it doesn't exist and we want to write to it.
473 * If notify is set, a message will be emitted if the directory is created.
476 hostfile_create_user_ssh_dir(const char *filename
, int notify
)
478 char *dotsshdir
= NULL
, *p
;
482 if ((p
= strrchr(filename
, '/')) == NULL
)
485 dotsshdir
= tilde_expand_filename("~/" _PATH_SSH_USER_DIR
, getuid());
486 if (strlen(dotsshdir
) > len
|| strncmp(filename
, dotsshdir
, len
) != 0)
487 goto out
; /* not ~/.ssh prefixed */
488 if (stat(dotsshdir
, &st
) == 0)
489 goto out
; /* dir already exists */
490 else if (errno
!= ENOENT
)
491 error("Could not stat %s: %s", dotsshdir
, strerror(errno
));
494 ssh_selinux_setfscreatecon(dotsshdir
);
496 if (mkdir(dotsshdir
, 0700) == -1)
497 error("Could not create directory '%.200s' (%s).",
498 dotsshdir
, strerror(errno
));
500 logit("Created directory '%s'.", dotsshdir
);
502 ssh_selinux_setfscreatecon(NULL
);
510 * Appends an entry to the host file. Returns false if the entry could not
514 add_host_to_hostfile(const char *filename
, const char *host
,
515 const struct sshkey
*key
, int store_hash
)
518 int success
, addnl
= 0;
521 return 1; /* XXX ? */
522 hostfile_create_user_ssh_dir(filename
, 0);
523 f
= fopen(filename
, "a+");
526 /* Make sure we have a terminating newline. */
527 if (fseek(f
, -1L, SEEK_END
) == 0 && fgetc(f
) != '\n')
529 if (fseek(f
, 0L, SEEK_END
) != 0 || (addnl
&& fputc('\n', f
) != '\n')) {
530 error("Failed to add terminating newline to %s: %s",
531 filename
, strerror(errno
));
535 success
= write_host_entry(f
, host
, NULL
, key
, store_hash
);
540 struct host_delete_ctx
{
543 const char *host
, *ip
;
544 u_int
*match_keys
; /* mask of HKF_MATCH_* for this key */
545 struct sshkey
* const *keys
;
551 host_delete(struct hostkey_foreach_line
*l
, void *_ctx
)
553 struct host_delete_ctx
*ctx
= (struct host_delete_ctx
*)_ctx
;
554 int loglevel
= ctx
->quiet
? SYSLOG_LEVEL_DEBUG1
: SYSLOG_LEVEL_VERBOSE
;
557 /* Don't remove CA and revocation lines */
558 if (l
->status
== HKF_STATUS_MATCHED
&& l
->marker
== MRK_NONE
) {
560 * If this line contains one of the keys that we will be
561 * adding later, then don't change it and mark the key for
564 for (i
= 0; i
< ctx
->nkeys
; i
++) {
565 if (!sshkey_equal(ctx
->keys
[i
], l
->key
))
567 ctx
->match_keys
[i
] |= l
->match
;
568 fprintf(ctx
->out
, "%s\n", l
->line
);
569 debug3_f("%s key already at %s:%ld",
570 sshkey_type(l
->key
), l
->path
, l
->linenum
);
575 * Hostname matches and has no CA/revoke marker, delete it
576 * by *not* writing the line to ctx->out.
578 do_log2(loglevel
, "%s%s%s:%ld: Removed %s key for host %s",
579 ctx
->quiet
? __func__
: "", ctx
->quiet
? ": " : "",
580 l
->path
, l
->linenum
, sshkey_type(l
->key
), ctx
->host
);
584 /* Retain non-matching hosts and invalid lines when deleting */
585 if (l
->status
== HKF_STATUS_INVALID
) {
586 do_log2(loglevel
, "%s%s%s:%ld: invalid known_hosts entry",
587 ctx
->quiet
? __func__
: "", ctx
->quiet
? ": " : "",
588 l
->path
, l
->linenum
);
590 fprintf(ctx
->out
, "%s\n", l
->line
);
595 hostfile_replace_entries(const char *filename
, const char *host
, const char *ip
,
596 struct sshkey
**keys
, size_t nkeys
, int store_hash
, int quiet
, int hash_alg
)
598 int r
, fd
, oerrno
= 0;
599 int loglevel
= quiet
? SYSLOG_LEVEL_DEBUG1
: SYSLOG_LEVEL_VERBOSE
;
600 struct host_delete_ctx ctx
;
601 char *fp
, *temp
= NULL
, *back
= NULL
;
609 memset(&ctx
, 0, sizeof(ctx
));
614 if ((ctx
.match_keys
= calloc(nkeys
, sizeof(*ctx
.match_keys
))) == NULL
)
615 return SSH_ERR_ALLOC_FAIL
;
621 * Prepare temporary file for in-place deletion.
623 if ((r
= asprintf(&temp
, "%s.XXXXXXXXXXX", filename
)) == -1 ||
624 (r
= asprintf(&back
, "%s.old", filename
)) == -1) {
625 r
= SSH_ERR_ALLOC_FAIL
;
629 if ((fd
= mkstemp(temp
)) == -1) {
631 error_f("mkstemp: %s", strerror(oerrno
));
632 r
= SSH_ERR_SYSTEM_ERROR
;
635 if ((ctx
.out
= fdopen(fd
, "w")) == NULL
) {
638 error_f("fdopen: %s", strerror(oerrno
));
639 r
= SSH_ERR_SYSTEM_ERROR
;
643 /* Remove stale/mismatching entries for the specified host */
644 if ((r
= hostkeys_foreach(filename
, host_delete
, &ctx
, host
, ip
,
645 HKF_WANT_PARSE_KEY
, 0)) != 0) {
647 error_fr(r
, "hostkeys_foreach");
651 /* Re-add the requested keys */
652 want
= HKF_MATCH_HOST
| (ip
== NULL
? 0 : HKF_MATCH_IP
);
653 for (i
= 0; i
< nkeys
; i
++) {
654 if (keys
[i
] == NULL
|| (want
& ctx
.match_keys
[i
]) == want
)
656 if ((fp
= sshkey_fingerprint(keys
[i
], hash_alg
,
657 SSH_FP_DEFAULT
)) == NULL
) {
658 r
= SSH_ERR_ALLOC_FAIL
;
663 if (ctx
.match_keys
[i
] == 0) {
664 what
= "Adding new key";
665 if (!write_host_entry(ctx
.out
, host
, ip
,
666 keys
[i
], store_hash
)) {
667 r
= SSH_ERR_INTERNAL_ERROR
;
670 } else if ((want
& ~ctx
.match_keys
[i
]) == HKF_MATCH_HOST
) {
671 what
= "Fixing match (hostname)";
672 if (!write_host_entry(ctx
.out
, host
, NULL
,
673 keys
[i
], store_hash
)) {
674 r
= SSH_ERR_INTERNAL_ERROR
;
677 } else if ((want
& ~ctx
.match_keys
[i
]) == HKF_MATCH_IP
) {
678 what
= "Fixing match (address)";
679 if (!write_host_entry(ctx
.out
, ip
, NULL
,
680 keys
[i
], store_hash
)) {
681 r
= SSH_ERR_INTERNAL_ERROR
;
685 do_log2(loglevel
, "%s%s%s for %s%s%s to %s: %s %s",
686 quiet
? __func__
: "", quiet
? ": " : "", what
,
687 host
, ip
== NULL
? "" : ",", ip
== NULL
? "" : ip
, filename
,
688 sshkey_ssh_name(keys
[i
]), fp
);
696 /* Backup the original file and replace it with the temporary */
697 if (unlink(back
) == -1 && errno
!= ENOENT
) {
699 error_f("unlink %.100s: %s", back
, strerror(errno
));
700 r
= SSH_ERR_SYSTEM_ERROR
;
703 if (link(filename
, back
) == -1) {
705 error_f("link %.100s to %.100s: %s", filename
,
706 back
, strerror(errno
));
707 r
= SSH_ERR_SYSTEM_ERROR
;
710 if (rename(temp
, filename
) == -1) {
712 error_f("rename \"%s\" to \"%s\": %s", temp
,
713 filename
, strerror(errno
));
714 r
= SSH_ERR_SYSTEM_ERROR
;
718 /* No changes made; just delete the temporary file */
719 if (unlink(temp
) != 0)
720 error_f("unlink \"%s\": %s", temp
, strerror(errno
));
726 if (temp
!= NULL
&& r
!= 0)
732 free(ctx
.match_keys
);
734 if (r
== SSH_ERR_SYSTEM_ERROR
)
740 match_maybe_hashed(const char *host
, const char *names
, int *was_hashed
)
742 int hashed
= *names
== HASH_DELIM
, ret
;
743 char *hashed_host
= NULL
;
744 size_t nlen
= strlen(names
);
746 if (was_hashed
!= NULL
)
747 *was_hashed
= hashed
;
749 if ((hashed_host
= host_hash(host
, names
, nlen
)) == NULL
)
751 ret
= (nlen
== strlen(hashed_host
) &&
752 strncmp(hashed_host
, names
, nlen
) == 0);
756 return match_hostname(host
, names
) == 1;
760 hostkeys_foreach_file(const char *path
, FILE *f
, hostkeys_foreach_fn
*callback
,
761 void *ctx
, const char *host
, const char *ip
, u_int options
, u_int note
)
763 char *line
= NULL
, ktype
[128];
769 struct hostkey_foreach_line lineinfo
;
770 size_t linesize
= 0, l
;
772 memset(&lineinfo
, 0, sizeof(lineinfo
));
773 if (host
== NULL
&& (options
& HKF_WANT_MATCH
) != 0)
774 return SSH_ERR_INVALID_ARGUMENT
;
776 while (getline(&line
, &linesize
, f
) != -1) {
778 line
[strcspn(line
, "\n")] = '\0';
781 sshkey_free(lineinfo
.key
);
782 memset(&lineinfo
, 0, sizeof(lineinfo
));
783 lineinfo
.path
= path
;
784 lineinfo
.linenum
= linenum
;
785 lineinfo
.line
= xstrdup(line
);
786 lineinfo
.marker
= MRK_NONE
;
787 lineinfo
.status
= HKF_STATUS_OK
;
788 lineinfo
.keytype
= KEY_UNSPEC
;
789 lineinfo
.note
= note
;
791 /* Skip any leading whitespace, comments and empty lines. */
792 for (cp
= line
; *cp
== ' ' || *cp
== '\t'; cp
++)
794 if (!*cp
|| *cp
== '#' || *cp
== '\n') {
795 if ((options
& HKF_WANT_MATCH
) == 0) {
796 lineinfo
.status
= HKF_STATUS_COMMENT
;
797 if ((r
= callback(&lineinfo
, ctx
)) != 0)
803 if ((lineinfo
.marker
= check_markers(&cp
)) == MRK_ERROR
) {
804 verbose_f("invalid marker at %s:%lu", path
, linenum
);
805 if ((options
& HKF_WANT_MATCH
) == 0)
810 /* Find the end of the host name portion. */
811 for (cp2
= cp
; *cp2
&& *cp2
!= ' ' && *cp2
!= '\t'; cp2
++)
816 /* Check if the host name matches. */
818 if ((s
= match_maybe_hashed(host
, lineinfo
.hosts
,
820 debug2_f("%s:%ld: bad host hash \"%.32s\"",
821 path
, linenum
, lineinfo
.hosts
);
825 lineinfo
.status
= HKF_STATUS_MATCHED
;
826 lineinfo
.match
|= HKF_MATCH_HOST
|
827 (hashed
? HKF_MATCH_HOST_HASHED
: 0);
829 /* Try matching IP address if supplied */
831 if ((s
= match_maybe_hashed(ip
, lineinfo
.hosts
,
833 debug2_f("%s:%ld: bad ip hash "
834 "\"%.32s\"", path
, linenum
,
839 lineinfo
.status
= HKF_STATUS_MATCHED
;
840 lineinfo
.match
|= HKF_MATCH_IP
|
841 (hashed
? HKF_MATCH_IP_HASHED
: 0);
845 * Skip this line if host matching requested and
846 * neither host nor address matched.
848 if ((options
& HKF_WANT_MATCH
) != 0 &&
849 lineinfo
.status
!= HKF_STATUS_MATCHED
)
853 /* Got a match. Skip host name and any following whitespace */
854 for (; *cp2
== ' ' || *cp2
== '\t'; cp2
++)
856 if (*cp2
== '\0' || *cp2
== '#') {
857 debug2("%s:%ld: truncated before key type",
861 lineinfo
.rawkey
= cp
= cp2
;
863 if ((options
& HKF_WANT_PARSE_KEY
) != 0) {
865 * Extract the key from the line. This will skip
866 * any leading whitespace. Ignore badly formatted
869 if ((lineinfo
.key
= sshkey_new(KEY_UNSPEC
)) == NULL
) {
870 error_f("sshkey_new failed");
871 r
= SSH_ERR_ALLOC_FAIL
;
874 if (!hostfile_read_key(&cp
, &kbits
, lineinfo
.key
)) {
877 lineinfo
.keytype
= lineinfo
.key
->type
;
878 lineinfo
.comment
= cp
;
880 /* Extract and parse key type */
881 l
= strcspn(lineinfo
.rawkey
, " \t");
882 if (l
<= 1 || l
>= sizeof(ktype
) ||
883 lineinfo
.rawkey
[l
] == '\0')
885 memcpy(ktype
, lineinfo
.rawkey
, l
);
887 lineinfo
.keytype
= sshkey_type_from_name(ktype
);
890 * Assume legacy RSA1 if the first component is a short
893 if (lineinfo
.keytype
== KEY_UNSPEC
&& l
< 8 &&
894 strspn(ktype
, "0123456789") == l
)
898 * Check that something other than whitespace follows
899 * the key type. This won't catch all corruption, but
900 * it does catch trivial truncation.
902 cp2
+= l
; /* Skip past key type */
903 for (; *cp2
== ' ' || *cp2
== '\t'; cp2
++)
905 if (*cp2
== '\0' || *cp2
== '#') {
906 debug2("%s:%ld: truncated after key type",
908 lineinfo
.keytype
= KEY_UNSPEC
;
910 if (lineinfo
.keytype
== KEY_UNSPEC
) {
912 sshkey_free(lineinfo
.key
);
914 lineinfo
.status
= HKF_STATUS_INVALID
;
915 if ((r
= callback(&lineinfo
, ctx
)) != 0)
920 if ((r
= callback(&lineinfo
, ctx
)) != 0)
923 sshkey_free(lineinfo
.key
);
930 hostkeys_foreach(const char *path
, hostkeys_foreach_fn
*callback
, void *ctx
,
931 const char *host
, const char *ip
, u_int options
, u_int note
)
936 if ((f
= fopen(path
, "r")) == NULL
)
937 return SSH_ERR_SYSTEM_ERROR
;
939 debug3_f("reading file \"%s\"", path
);
940 r
= hostkeys_foreach_file(path
, f
, callback
, ctx
, host
, ip
,