xref: /dflybsd-src/crypto/openssh/hostfile.h (revision 50a69bb51183a7916e776f2c9f5fa64c999f1a2f)
1*50a69bb5SSascha Wildner /* $OpenBSD: hostfile.h,v 1.29 2021/01/26 00:51:30 djm Exp $ */
218de8d7fSPeter Avalos 
318de8d7fSPeter Avalos /*
418de8d7fSPeter Avalos  * Author: Tatu Ylonen <ylo@cs.hut.fi>
518de8d7fSPeter Avalos  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
618de8d7fSPeter Avalos  *                    All rights reserved
718de8d7fSPeter Avalos  *
818de8d7fSPeter Avalos  * As far as I am concerned, the code I have written for this software
918de8d7fSPeter Avalos  * can be used freely for any purpose.  Any derived versions of this
1018de8d7fSPeter Avalos  * software must be clearly marked as such, and if the derived work is
1118de8d7fSPeter Avalos  * incompatible with the protocol description in the RFC file, it must be
1218de8d7fSPeter Avalos  * called by a name other than "ssh" or "Secure Shell".
1318de8d7fSPeter Avalos  */
1418de8d7fSPeter Avalos #ifndef HOSTFILE_H
1518de8d7fSPeter Avalos #define HOSTFILE_H
1618de8d7fSPeter Avalos 
1718de8d7fSPeter Avalos typedef enum {
18856ea928SPeter Avalos 	HOST_OK, HOST_NEW, HOST_CHANGED, HOST_REVOKED, HOST_FOUND
1918de8d7fSPeter Avalos }       HostStatus;
2018de8d7fSPeter Avalos 
219f304aafSPeter Avalos typedef enum {
229f304aafSPeter Avalos 	MRK_ERROR, MRK_NONE, MRK_REVOKE, MRK_CA
239f304aafSPeter Avalos }	HostkeyMarker;
249f304aafSPeter Avalos 
259f304aafSPeter Avalos struct hostkey_entry {
269f304aafSPeter Avalos 	char *host;
279f304aafSPeter Avalos 	char *file;
289f304aafSPeter Avalos 	u_long line;
29e9778795SPeter Avalos 	struct sshkey *key;
309f304aafSPeter Avalos 	HostkeyMarker marker;
31*50a69bb5SSascha Wildner 	u_int note; /* caller-specific note/flag */
329f304aafSPeter Avalos };
33*50a69bb5SSascha Wildner struct hostkeys {
34*50a69bb5SSascha Wildner 	struct hostkey_entry *entries;
35*50a69bb5SSascha Wildner 	u_int num_entries;
36*50a69bb5SSascha Wildner };
379f304aafSPeter Avalos 
389f304aafSPeter Avalos struct hostkeys *init_hostkeys(void);
39*50a69bb5SSascha Wildner void	 load_hostkeys(struct hostkeys *, const char *,
40*50a69bb5SSascha Wildner     const char *, u_int);
41*50a69bb5SSascha Wildner void	 load_hostkeys_file(struct hostkeys *, const char *,
42*50a69bb5SSascha Wildner     const char *, FILE *, u_int note);
439f304aafSPeter Avalos void	 free_hostkeys(struct hostkeys *);
449f304aafSPeter Avalos 
45e9778795SPeter Avalos HostStatus check_key_in_hostkeys(struct hostkeys *, struct sshkey *,
469f304aafSPeter Avalos     const struct hostkey_entry **);
47*50a69bb5SSascha Wildner int	 lookup_key_in_hostkeys_by_type(struct hostkeys *, int, int,
489f304aafSPeter Avalos     const struct hostkey_entry **);
49*50a69bb5SSascha Wildner int	 lookup_marker_in_hostkeys(struct hostkeys *, int);
509f304aafSPeter Avalos 
51e9778795SPeter Avalos int	 hostfile_read_key(char **, u_int *, struct sshkey *);
52e9778795SPeter Avalos int	 add_host_to_hostfile(const char *, const char *,
53e9778795SPeter Avalos     const struct sshkey *, int);
54e9778795SPeter Avalos 
55e9778795SPeter Avalos int	 hostfile_replace_entries(const char *filename,
56e9778795SPeter Avalos     const char *host, const char *ip, struct sshkey **keys, size_t nkeys,
57e9778795SPeter Avalos     int store_hash, int quiet, int hash_alg);
5818de8d7fSPeter Avalos 
5918de8d7fSPeter Avalos #define HASH_MAGIC	"|1|"
6018de8d7fSPeter Avalos #define HASH_DELIM	'|'
6118de8d7fSPeter Avalos 
62856ea928SPeter Avalos #define CA_MARKER	"@cert-authority"
63856ea928SPeter Avalos #define REVOKE_MARKER	"@revoked"
64856ea928SPeter Avalos 
6518de8d7fSPeter Avalos char	*host_hash(const char *, const char *, u_int);
6618de8d7fSPeter Avalos 
67e9778795SPeter Avalos /*
68e9778795SPeter Avalos  * Iterate through a hostkeys file, optionally parsing keys and matching
69e9778795SPeter Avalos  * hostnames. Allows access to the raw keyfile lines to allow
70e9778795SPeter Avalos  * streaming edits to the file to take place.
71e9778795SPeter Avalos  */
72e9778795SPeter Avalos #define HKF_WANT_MATCH		(1)	/* return only matching hosts/addrs */
73e9778795SPeter Avalos #define HKF_WANT_PARSE_KEY	(1<<1)	/* need key parsed */
74e9778795SPeter Avalos 
75e9778795SPeter Avalos #define HKF_STATUS_OK		0	/* Line parsed, didn't match host */
76e9778795SPeter Avalos #define HKF_STATUS_INVALID	1	/* line had parse error */
77e9778795SPeter Avalos #define HKF_STATUS_COMMENT	2	/* valid line contained no key */
78e9778795SPeter Avalos #define HKF_STATUS_MATCHED	3	/* hostname or IP matched */
79e9778795SPeter Avalos 
80e9778795SPeter Avalos #define HKF_MATCH_HOST		(1)	/* hostname matched */
81e9778795SPeter Avalos #define HKF_MATCH_IP		(1<<1)	/* address matched */
82e9778795SPeter Avalos #define HKF_MATCH_HOST_HASHED	(1<<2)	/* hostname was hashed */
83e9778795SPeter Avalos #define HKF_MATCH_IP_HASHED	(1<<3)	/* address was hashed */
84e9778795SPeter Avalos /* XXX HKF_MATCH_KEY_TYPE? */
85e9778795SPeter Avalos 
86e9778795SPeter Avalos /*
87e9778795SPeter Avalos  * The callback function receives this as an argument for each matching
88e9778795SPeter Avalos  * hostkey line. The callback may "steal" the 'key' field by setting it to NULL.
89e9778795SPeter Avalos  * If a parse error occurred, then "hosts" and subsequent options may be NULL.
90e9778795SPeter Avalos  */
91e9778795SPeter Avalos struct hostkey_foreach_line {
92e9778795SPeter Avalos 	const char *path; /* Path of file */
93e9778795SPeter Avalos 	u_long linenum;	/* Line number */
94e9778795SPeter Avalos 	u_int status;	/* One of HKF_STATUS_* */
95e9778795SPeter Avalos 	u_int match;	/* Zero or more of HKF_MATCH_* OR'd together */
96e9778795SPeter Avalos 	char *line;	/* Entire key line; mutable by callback */
97e9778795SPeter Avalos 	int marker;	/* CA/revocation markers; indicated by MRK_* value */
98e9778795SPeter Avalos 	const char *hosts; /* Raw hosts text, may be hashed or list multiple */
99e9778795SPeter Avalos 	const char *rawkey; /* Text of key and any comment following it */
100e9778795SPeter Avalos 	int keytype;	/* Type of key; KEY_UNSPEC for invalid/comment lines */
101e9778795SPeter Avalos 	struct sshkey *key; /* Key, if parsed ok and HKF_WANT_MATCH_HOST set */
102e9778795SPeter Avalos 	const char *comment; /* Any comment following the key */
103*50a69bb5SSascha Wildner 	u_int note;	/* caller-specified note copied from arguments */
104e9778795SPeter Avalos };
105e9778795SPeter Avalos 
106e9778795SPeter Avalos /*
107e9778795SPeter Avalos  * Callback fires for each line (or matching line if a HKF_WANT_* option
108e9778795SPeter Avalos  * is set). The foreach loop will terminate if the callback returns a non-
109e9778795SPeter Avalos  * zero exit status.
110e9778795SPeter Avalos  */
111e9778795SPeter Avalos typedef int hostkeys_foreach_fn(struct hostkey_foreach_line *l, void *ctx);
112e9778795SPeter Avalos 
113e9778795SPeter Avalos /* Iterate over a hostkeys file */
114*50a69bb5SSascha Wildner int hostkeys_foreach(const char *path,
115*50a69bb5SSascha Wildner     hostkeys_foreach_fn *callback, void *ctx,
116*50a69bb5SSascha Wildner     const char *host, const char *ip, u_int options, u_int note);
117*50a69bb5SSascha Wildner int hostkeys_foreach_file(const char *path, FILE *f,
118*50a69bb5SSascha Wildner     hostkeys_foreach_fn *callback, void *ctx,
119*50a69bb5SSascha Wildner     const char *host, const char *ip, u_int options, u_int note);
120*50a69bb5SSascha Wildner 
121*50a69bb5SSascha Wildner void hostfile_create_user_ssh_dir(const char *, int);
122e9778795SPeter Avalos 
12318de8d7fSPeter Avalos #endif
124