xref: /openssh-portable/hostfile.h (revision 05a65140)
1 /* $OpenBSD: hostfile.h,v 1.25 2020/05/13 09:52:41 djm Exp $ */
2 
3 /*
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  */
14 #ifndef HOSTFILE_H
15 #define HOSTFILE_H
16 
17 typedef enum {
18 	HOST_OK, HOST_NEW, HOST_CHANGED, HOST_REVOKED, HOST_FOUND
19 }       HostStatus;
20 
21 typedef enum {
22 	MRK_ERROR, MRK_NONE, MRK_REVOKE, MRK_CA
23 }	HostkeyMarker;
24 
25 struct hostkey_entry {
26 	char *host;
27 	char *file;
28 	u_long line;
29 	struct sshkey *key;
30 	HostkeyMarker marker;
31 };
32 struct hostkeys;
33 
34 struct hostkeys *init_hostkeys(void);
35 void	 load_hostkeys(struct hostkeys *, const char *, const char *);
36 void	 free_hostkeys(struct hostkeys *);
37 
38 HostStatus check_key_in_hostkeys(struct hostkeys *, struct sshkey *,
39     const struct hostkey_entry **);
40 int	 lookup_key_in_hostkeys_by_type(struct hostkeys *, int,
41     const struct hostkey_entry **);
42 int	 lookup_marker_in_hostkeys(struct hostkeys *, int);
43 
44 int	 hostfile_read_key(char **, u_int *, struct sshkey *);
45 int	 add_host_to_hostfile(const char *, const char *,
46     const struct sshkey *, int);
47 
48 int	 hostfile_replace_entries(const char *filename,
49     const char *host, const char *ip, struct sshkey **keys, size_t nkeys,
50     int store_hash, int quiet, int hash_alg);
51 
52 #define HASH_MAGIC	"|1|"
53 #define HASH_DELIM	'|'
54 
55 #define CA_MARKER	"@cert-authority"
56 #define REVOKE_MARKER	"@revoked"
57 
58 char	*host_hash(const char *, const char *, u_int);
59 
60 /*
61  * Iterate through a hostkeys file, optionally parsing keys and matching
62  * hostnames. Allows access to the raw keyfile lines to allow
63  * streaming edits to the file to take place.
64  */
65 #define HKF_WANT_MATCH		(1)	/* return only matching hosts/addrs */
66 #define HKF_WANT_PARSE_KEY	(1<<1)	/* need key parsed */
67 
68 #define HKF_STATUS_OK		0	/* Line parsed, didn't match host */
69 #define HKF_STATUS_INVALID	1	/* line had parse error */
70 #define HKF_STATUS_COMMENT	2	/* valid line contained no key */
71 #define HKF_STATUS_MATCHED	3	/* hostname or IP matched */
72 
73 #define HKF_MATCH_HOST		(1)	/* hostname matched */
74 #define HKF_MATCH_IP		(1<<1)	/* address matched */
75 #define HKF_MATCH_HOST_HASHED	(1<<2)	/* hostname was hashed */
76 #define HKF_MATCH_IP_HASHED	(1<<3)	/* address was hashed */
77 /* XXX HKF_MATCH_KEY_TYPE? */
78 
79 /*
80  * The callback function receives this as an argument for each matching
81  * hostkey line. The callback may "steal" the 'key' field by setting it to NULL.
82  * If a parse error occurred, then "hosts" and subsequent options may be NULL.
83  */
84 struct hostkey_foreach_line {
85 	const char *path; /* Path of file */
86 	u_long linenum;	/* Line number */
87 	u_int status;	/* One of HKF_STATUS_* */
88 	u_int match;	/* Zero or more of HKF_MATCH_* OR'd together */
89 	char *line;	/* Entire key line; mutable by callback */
90 	int marker;	/* CA/revocation markers; indicated by MRK_* value */
91 	const char *hosts; /* Raw hosts text, may be hashed or list multiple */
92 	const char *rawkey; /* Text of key and any comment following it */
93 	int keytype;	/* Type of key; KEY_UNSPEC for invalid/comment lines */
94 	struct sshkey *key; /* Key, if parsed ok and HKF_WANT_MATCH_HOST set */
95 	const char *comment; /* Any comment following the key */
96 };
97 
98 /*
99  * Callback fires for each line (or matching line if a HKF_WANT_* option
100  * is set). The foreach loop will terminate if the callback returns a non-
101  * zero exit status.
102  */
103 typedef int hostkeys_foreach_fn(struct hostkey_foreach_line *l, void *ctx);
104 
105 /* Iterate over a hostkeys file */
106 int hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
107     const char *host, const char *ip, u_int options);
108 
109 #endif
110