blob: 9080b5edb6a69ee8659085e5d57b2f92796fce0f [file] [log] [blame]
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001/* $OpenBSD: hostfile.h,v 1.23 2015/01/26 03:04:45 djm Exp $ */
Ben Lindstrom36579d32001-01-29 07:39:26 +00002
Damien Millere4340be2000-09-16 13:29:08 +11003/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
7 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 */
Damien Miller450a7a12000-03-26 13:04:51 +100014#ifndef HOSTFILE_H
15#define HOSTFILE_H
16
Damien Miller450a7a12000-03-26 13:04:51 +100017typedef enum {
Damien Miller1aed65e2010-03-04 21:53:35 +110018 HOST_OK, HOST_NEW, HOST_CHANGED, HOST_REVOKED, HOST_FOUND
Damien Miller450a7a12000-03-26 13:04:51 +100019} HostStatus;
Ben Lindstrom46c16222000-12-22 01:43:59 +000020
Damien Millerd925dcd2010-12-01 12:21:51 +110021typedef enum {
22 MRK_ERROR, MRK_NONE, MRK_REVOKE, MRK_CA
23} HostkeyMarker;
24
25struct hostkey_entry {
26 char *host;
27 char *file;
28 u_long line;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000029 struct sshkey *key;
Damien Millerd925dcd2010-12-01 12:21:51 +110030 HostkeyMarker marker;
31};
32struct hostkeys;
33
34struct hostkeys *init_hostkeys(void);
35void load_hostkeys(struct hostkeys *, const char *, const char *);
36void free_hostkeys(struct hostkeys *);
37
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000038HostStatus check_key_in_hostkeys(struct hostkeys *, struct sshkey *,
Damien Millerd925dcd2010-12-01 12:21:51 +110039 const struct hostkey_entry **);
40int lookup_key_in_hostkeys_by_type(struct hostkeys *, int,
41 const struct hostkey_entry **);
42
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000043int hostfile_read_key(char **, u_int *, struct sshkey *);
44int add_host_to_hostfile(const char *, const char *,
45 const struct sshkey *, int);
Damien Miller450a7a12000-03-26 13:04:51 +100046
djm@openbsd.org8d4f8722015-01-26 03:04:45 +000047int hostfile_replace_entries(const char *filename, const char *host,
48 struct sshkey **keys, size_t nkeys, int store_hash, int quiet);
49
Damien Millere1776152005-03-01 21:47:37 +110050#define HASH_MAGIC "|1|"
51#define HASH_DELIM '|'
52
Damien Miller0a80ca12010-02-27 07:55:05 +110053#define CA_MARKER "@cert-authority"
Damien Miller1aed65e2010-03-04 21:53:35 +110054#define REVOKE_MARKER "@revoked"
Damien Miller0a80ca12010-02-27 07:55:05 +110055
Damien Millere1776152005-03-01 21:47:37 +110056char *host_hash(const char *, const char *, u_int);
57
djm@openbsd.orgc29811c2015-01-18 21:40:23 +000058/*
59 * Iterate through a hostkeys file, optionally parsing keys and matching
60 * hostnames. Allows access to the raw keyfile lines to allow
61 * streaming edits to the file to take place.
62 */
63#define HKF_WANT_MATCH_HOST (1) /* return only matching hosts */
64#define HKF_WANT_PARSE_KEY (1<<1) /* need key parsed */
65
66#define HKF_STATUS_OK 1 /* Line parsed, didn't match host */
67#define HKF_STATUS_INVALID 2 /* line had parse error */
68#define HKF_STATUS_COMMENT 3 /* valid line contained no key */
69#define HKF_STATUS_HOST_MATCHED 4 /* hostname matched */
70
71/*
72 * The callback function receives this as an argument for each matching
73 * hostkey line. The callback may "steal" the 'key' field by setting it to NULL.
74 * If a parse error occurred, then "hosts" and subsequent options may be NULL.
75 */
76struct hostkey_foreach_line {
77 const char *path; /* Path of file */
78 u_long linenum; /* Line number */
79 int status; /* One of HKF_STATUS_* */
80 char *line; /* Entire key line; mutable by callback */
81 int marker; /* CA/revocation markers; indicated by MRK_* value */
82 const char *hosts; /* Raw hosts text, may be hashed or list multiple */
83 int was_hashed; /* Non-zero if hostname was hashed */
84 const char *rawkey; /* Text of key and any comment following it */
85 struct sshkey *key; /* Key, if parsed ok and HKF_WANT_MATCH_HOST set */
86 const char *comment; /* Any comment following the key */
87};
88
89/*
90 * Callback fires for each line (or matching line if a HKF_WANT_* option
91 * is set). The foreach loop will terminate if the callback returns a non-
92 * zero exit status.
93 */
94typedef int hostkeys_foreach_fn(struct hostkey_foreach_line *l, void *ctx);
95
96int hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
97 const char *host, u_int options);
98
Damien Miller450a7a12000-03-26 13:04:51 +100099#endif