1295367Sdes/* $OpenBSD: hostfile.h,v 1.24 2015/02/16 22:08:57 djm Exp $ */
276259Sgreen
365668Skris/*
465668Skris * Author: Tatu Ylonen <ylo@cs.hut.fi>
565668Skris * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
665668Skris *                    All rights reserved
765668Skris *
865668Skris * As far as I am concerned, the code I have written for this software
965668Skris * can be used freely for any purpose.  Any derived versions of this
1065668Skris * software must be clearly marked as such, and if the derived work is
1165668Skris * incompatible with the protocol description in the RFC file, it must be
1265668Skris * called by a name other than "ssh" or "Secure Shell".
1365668Skris */
1458582Skris#ifndef HOSTFILE_H
1558582Skris#define HOSTFILE_H
1658582Skris
1758582Skristypedef enum {
18204917Sdes	HOST_OK, HOST_NEW, HOST_CHANGED, HOST_REVOKED, HOST_FOUND
1958582Skris}       HostStatus;
2076259Sgreen
21221420Sdestypedef enum {
22221420Sdes	MRK_ERROR, MRK_NONE, MRK_REVOKE, MRK_CA
23221420Sdes}	HostkeyMarker;
24221420Sdes
25221420Sdesstruct hostkey_entry {
26221420Sdes	char *host;
27221420Sdes	char *file;
28221420Sdes	u_long line;
29295367Sdes	struct sshkey *key;
30221420Sdes	HostkeyMarker marker;
31221420Sdes};
32221420Sdesstruct hostkeys;
33221420Sdes
34221420Sdesstruct hostkeys *init_hostkeys(void);
35221420Sdesvoid	 load_hostkeys(struct hostkeys *, const char *, const char *);
36221420Sdesvoid	 free_hostkeys(struct hostkeys *);
37221420Sdes
38295367SdesHostStatus check_key_in_hostkeys(struct hostkeys *, struct sshkey *,
39221420Sdes    const struct hostkey_entry **);
40221420Sdesint	 lookup_key_in_hostkeys_by_type(struct hostkeys *, int,
41221420Sdes    const struct hostkey_entry **);
42221420Sdes
43295367Sdesint	 hostfile_read_key(char **, u_int *, struct sshkey *);
44295367Sdesint	 add_host_to_hostfile(const char *, const char *,
45295367Sdes    const struct sshkey *, int);
4658582Skris
47295367Sdesint	 hostfile_replace_entries(const char *filename,
48295367Sdes    const char *host, const char *ip, struct sshkey **keys, size_t nkeys,
49295367Sdes    int store_hash, int quiet, int hash_alg);
50295367Sdes
51146998Sdes#define HASH_MAGIC	"|1|"
52146998Sdes#define HASH_DELIM	'|'
53146998Sdes
54204917Sdes#define CA_MARKER	"@cert-authority"
55204917Sdes#define REVOKE_MARKER	"@revoked"
56204917Sdes
57146998Sdeschar	*host_hash(const char *, const char *, u_int);
58146998Sdes
59295367Sdes/*
60295367Sdes * Iterate through a hostkeys file, optionally parsing keys and matching
61295367Sdes * hostnames. Allows access to the raw keyfile lines to allow
62295367Sdes * streaming edits to the file to take place.
63295367Sdes */
64295367Sdes#define HKF_WANT_MATCH		(1)	/* return only matching hosts/addrs */
65295367Sdes#define HKF_WANT_PARSE_KEY	(1<<1)	/* need key parsed */
66295367Sdes
67295367Sdes#define HKF_STATUS_OK		0	/* Line parsed, didn't match host */
68295367Sdes#define HKF_STATUS_INVALID	1	/* line had parse error */
69295367Sdes#define HKF_STATUS_COMMENT	2	/* valid line contained no key */
70295367Sdes#define HKF_STATUS_MATCHED	3	/* hostname or IP matched */
71295367Sdes
72295367Sdes#define HKF_MATCH_HOST		(1)	/* hostname matched */
73295367Sdes#define HKF_MATCH_IP		(1<<1)	/* address matched */
74295367Sdes#define HKF_MATCH_HOST_HASHED	(1<<2)	/* hostname was hashed */
75295367Sdes#define HKF_MATCH_IP_HASHED	(1<<3)	/* address was hashed */
76295367Sdes/* XXX HKF_MATCH_KEY_TYPE? */
77295367Sdes
78295367Sdes/*
79295367Sdes * The callback function receives this as an argument for each matching
80295367Sdes * hostkey line. The callback may "steal" the 'key' field by setting it to NULL.
81295367Sdes * If a parse error occurred, then "hosts" and subsequent options may be NULL.
82295367Sdes */
83295367Sdesstruct hostkey_foreach_line {
84295367Sdes	const char *path; /* Path of file */
85295367Sdes	u_long linenum;	/* Line number */
86295367Sdes	u_int status;	/* One of HKF_STATUS_* */
87295367Sdes	u_int match;	/* Zero or more of HKF_MATCH_* OR'd together */
88295367Sdes	char *line;	/* Entire key line; mutable by callback */
89295367Sdes	int marker;	/* CA/revocation markers; indicated by MRK_* value */
90295367Sdes	const char *hosts; /* Raw hosts text, may be hashed or list multiple */
91295367Sdes	const char *rawkey; /* Text of key and any comment following it */
92295367Sdes	int keytype;	/* Type of key; KEY_UNSPEC for invalid/comment lines */
93295367Sdes	struct sshkey *key; /* Key, if parsed ok and HKF_WANT_MATCH_HOST set */
94295367Sdes	const char *comment; /* Any comment following the key */
95295367Sdes};
96295367Sdes
97295367Sdes/*
98295367Sdes * Callback fires for each line (or matching line if a HKF_WANT_* option
99295367Sdes * is set). The foreach loop will terminate if the callback returns a non-
100295367Sdes * zero exit status.
101295367Sdes */
102295367Sdestypedef int hostkeys_foreach_fn(struct hostkey_foreach_line *l, void *ctx);
103295367Sdes
104295367Sdes/* Iterate over a hostkeys file */
105295367Sdesint hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
106295367Sdes    const char *host, const char *ip, u_int options);
107295367Sdes
10858582Skris#endif
109