1 /*	$NetBSD: ntp_config.h,v 1.14 2022/10/09 21:41:03 christos Exp $	*/
2 
3 #ifndef NTP_CONFIG_H
4 #define NTP_CONFIG_H
5 
6 #ifdef HAVE_SYS_RESOURCE_H
7 # include <sys/resource.h>
8 #endif /* HAVE_SYS_RESOURCE_H */
9 
10 #include "ntp_machine.h"
11 #include "ntp_psl.h"
12 #include "ntpsim.h"
13 
14 
15 /*
16  * Configuration file name
17  */
18 #ifndef CONFIG_FILE
19 # ifndef SYS_WINNT
20 #  define	CONFIG_FILE "/etc/ntp.conf"
21 # else /* SYS_WINNT */
22 #  define	CONFIG_FILE	"%windir%\\system32\\drivers\\etc\\ntp.conf"
23 #  define	ALT_CONFIG_FILE "%windir%\\ntp.conf"
24 #  define	NTP_KEYSDIR	"%windir%\\system32\\drivers\\etc"
25 # endif /* SYS_WINNT */
26 #endif /* not CONFIG_FILE */
27 
28 
29 /*
30  * We keep config trees around for possible saveconfig use.  When
31  * built with configure --disable-saveconfig, and when built with
32  * debugging enabled, include the free_config_*() routines.  In the
33  * DEBUG case, they are used in an atexit() cleanup routine to make
34  * postmortem leak check reports more interesting.
35  */
36 #if !defined(FREE_CFG_T) && (!defined(SAVECONFIG) || defined(DEBUG))
37 #define FREE_CFG_T
38 #endif
39 
40 /* Limits */
41 #define MAXLINE 1024
42 
43 /* Configuration sources */
44 
45 #define CONF_SOURCE_FILE		0
46 #define CONF_SOURCE_NTPQ		1
47 
48 /* list of servers from command line for config_peers() */
49 extern	int	cmdline_server_count;
50 extern	char **	cmdline_servers;
51 
52 /* set to zero if we're not locking memory */
53 extern	int	cur_memlock;
54 
55 typedef struct int_range_tag {
56 	int	first;
57 	int	last;
58 } int_range;
59 
60 /* generic list node */
61 typedef struct any_node_tag any_node;
62 struct any_node_tag {
63 	any_node *	link;
64 };
65 
66 typedef DECL_FIFO_ANCHOR(any_node) any_node_fifo;
67 
68 /* Structure for storing an attribute-value pair */
69 typedef struct attr_val_tag attr_val;
70 struct attr_val_tag {
71 	attr_val *	link;
72 	int		attr;
73 	int		type;	/* T_String, T_Integer, ... */
74 	int		flag;	/* auxiliary flags */
75 	union val {
76 		double		d;	/* T_Double */
77 		int		i;	/* T_Integer */
78 		int_range	r;	/* T_Intrange */
79 		char *		s;	/* T_String */
80 		u_int		u;	/* T_U_int */
81 	} value;
82 };
83 
84 typedef DECL_FIFO_ANCHOR(attr_val) attr_val_fifo;
85 
86 /* Structure for nodes on the syntax tree */
87 typedef struct address_node_tag address_node;
88 struct address_node_tag {
89 	address_node *	link;
90 	char *		address;
91 	u_short		type;	/* family, AF_UNSPEC (0), AF_INET[6] */
92 };
93 
94 typedef DECL_FIFO_ANCHOR(address_node) address_fifo;
95 
96 typedef struct int_node_tag int_node;
97 struct int_node_tag {
98 	int_node *	link;
99 	int		i;
100 };
101 
102 typedef DECL_FIFO_ANCHOR(int_node) int_fifo;
103 
104 typedef struct string_node_tag string_node;
105 struct string_node_tag {
106 	string_node *	link;
107 	char *		s;
108 };
109 
110 typedef DECL_FIFO_ANCHOR(string_node) string_fifo;
111 
112 typedef struct restrict_node_tag restrict_node;
113 struct restrict_node_tag {
114 	restrict_node *	link;
115 	address_node *	addr;
116 	address_node *	mask;
117 	attr_val_fifo *	flag_tok_fifo;
118 	int		line_no;
119 	short		ippeerlimit;
120 	short		srvfuzrft;
121 };
122 
123 typedef DECL_FIFO_ANCHOR(restrict_node) restrict_fifo;
124 
125 typedef struct peer_node_tag peer_node;
126 struct peer_node_tag {
127 	peer_node *	link;
128 	int		host_mode;
129 	address_node *	addr;
130 	attr_val_fifo *	peerflags;
131 	u_char		minpoll;
132 	u_char		maxpoll;
133 	u_int32		ttl;
134 	u_char		peerversion;
135 	keyid_t		peerkey;
136 	char *		group;
137 };
138 
139 typedef DECL_FIFO_ANCHOR(peer_node) peer_fifo;
140 
141 typedef struct unpeer_node_tag unpeer_node;
142 struct unpeer_node_tag {
143 	unpeer_node *	link;
144 	associd_t	assocID;
145 	address_node *	addr;
146 };
147 
148 typedef DECL_FIFO_ANCHOR(unpeer_node) unpeer_fifo;
149 
150 typedef struct auth_node_tag auth_node;
151 struct auth_node_tag {
152 	int		control_key;
153 	int		cryptosw;
154 	attr_val_fifo *	crypto_cmd_list;
155 	char *		keys;
156 	char *		keysdir;
157 	int		request_key;
158 	int		revoke;
159 	attr_val_fifo *	trusted_key_list;
160 	char *		ntp_signd_socket;
161 };
162 
163 typedef struct filegen_node_tag filegen_node;
164 struct filegen_node_tag {
165 	filegen_node *	link;
166 	int		filegen_token;
167 	attr_val_fifo *	options;
168 };
169 
170 typedef DECL_FIFO_ANCHOR(filegen_node) filegen_fifo;
171 
172 typedef struct setvar_node_tag setvar_node;
173 struct setvar_node_tag {
174 	setvar_node *	link;
175 	char *		var;
176 	char *		val;
177 	int		isdefault;
178 };
179 
180 typedef DECL_FIFO_ANCHOR(setvar_node) setvar_fifo;
181 
182 typedef struct nic_rule_node_tag nic_rule_node;
183 struct nic_rule_node_tag {
184 	nic_rule_node *	link;
185 	int		match_class;
186 	char *		if_name;	/* or numeric address */
187 	int		action;
188 };
189 
190 typedef DECL_FIFO_ANCHOR(nic_rule_node) nic_rule_fifo;
191 
192 typedef struct addr_opts_node_tag addr_opts_node;
193 struct addr_opts_node_tag {
194 	addr_opts_node *link;
195 	address_node *	addr;
196 	attr_val_fifo *	options;
197 };
198 
199 typedef DECL_FIFO_ANCHOR(addr_opts_node) addr_opts_fifo;
200 
201 typedef struct sim_node_tag sim_node;
202 struct sim_node_tag {
203 	sim_node *		link;
204 	attr_val_fifo *		init_opts;
205 	server_info_fifo *	servers;
206 };
207 
208 typedef DECL_FIFO_ANCHOR(sim_node) sim_fifo;
209 
210 /* The syntax tree */
211 typedef struct config_tree_tag config_tree;
212 struct config_tree_tag {
213 	config_tree *	link;
214 
215 	attr_val	source;
216 	time_t		timestamp;
217 
218 	peer_fifo *	peers;
219 	unpeer_fifo *	unpeers;
220 
221 	/* Other Modes */
222 	int		broadcastclient;
223 	address_fifo *	manycastserver;
224 	address_fifo *	multicastclient;
225 
226 	attr_val_fifo *	orphan_cmds;	/* s/b renamed tos_options */
227 
228 	/* Monitoring Configuration */
229 	int_fifo *	stats_list;
230 	char *		stats_dir;
231 	filegen_fifo *	filegen_opts;
232 
233 	/* Access Control Configuration */
234 	attr_val_fifo *	discard_opts;
235 	attr_val_fifo *	mru_opts;
236 	restrict_fifo *	restrict_opts;
237 
238 	addr_opts_fifo *fudge;
239 	attr_val_fifo *	rlimit;
240 	attr_val_fifo *	tinker;
241 	attr_val_fifo *	enable_opts;
242 	attr_val_fifo *	disable_opts;
243 
244 	auth_node	auth;
245 
246 	attr_val_fifo *	logconfig;
247 	string_fifo *	phone;
248 	setvar_fifo *	setvar;
249 	int_fifo *	ttl;
250 	addr_opts_fifo *trap;
251 	attr_val_fifo *	vars;
252 	nic_rule_fifo *	nic_rules;
253 	int_fifo *	reset_counters;
254 	attr_val_fifo *	pollskewlist;
255 
256 	sim_fifo *	sim_details;
257 	int		mdnstries;
258 };
259 
260 
261 /* Structure for holding a remote configuration command */
262 struct REMOTE_CONFIG_INFO {
263 	char buffer[MAXLINE];
264 	char err_msg[MAXLINE];
265 	int pos;
266 	int err_pos;
267 	int no_errors;
268 };
269 
270 
271 /*
272  * context for trap_name_resolved() to call ctlsettrap() once the
273  * name->address resolution completes.
274  */
275 typedef struct settrap_parms_tag {
276 	sockaddr_u	ifaddr;
277 	int		ifaddr_nonnull;
278 } settrap_parms;
279 
280 
281 /*
282 ** Data Minimization Items
283 */
284 
285 /* Serverresponse fuzz reftime: stored in 'restrict' fifos */
286 
287 
288 /* get text from T_ tokens */
289 const char * token_name(int token);
290 
291 /* generic fifo routines for structs linked by 1st member */
292 typedef void (*fifo_deleter)(void*);
293 void *	destroy_gen_fifo(void *fifo, fifo_deleter func);
294 void *	append_gen_fifo(void *fifo, void *entry);
295 void *	concat_gen_fifos(void *first, void *second);
296 #define DESTROY_G_FIFO(pf, func)	\
297 	((pf) = destroy_gen_fifo((pf), (fifo_deleter)(func)))
298 #define APPEND_G_FIFO(pf, pe)		\
299 	((pf) = append_gen_fifo((pf), (pe)))
300 #define CONCAT_G_FIFOS(first, second)	\
301 	((first) = concat_gen_fifos((first), (second)))
302 #define HEAD_PFIFO(pf)			\
303 	(((pf) != NULL)			\
304 	      ? HEAD_FIFO(*(pf))	\
305 	      : NULL)
306 
307 peer_node *create_peer_node(int hmode, address_node *addr,
308 			    attr_val_fifo *options);
309 unpeer_node *create_unpeer_node(address_node *addr);
310 address_node *create_address_node(char *addr, int type);
311 void destroy_address_node(address_node *my_node);
312 attr_val *create_attr_dval(int attr, double value);
313 attr_val *create_attr_ival(int attr, int value);
314 attr_val *create_attr_rval(int attr, int first, int last);
315 attr_val *create_attr_sval(int attr, const char *s);
316 attr_val *create_attr_uval(int attr, u_int value);
317 void	  destroy_attr_val(attr_val *node);
318 filegen_node *create_filegen_node(int filegen_token,
319 				  attr_val_fifo *options);
320 string_node *create_string_node(char *str);
321 restrict_node *create_restrict_node(address_node *addr,
322 				    address_node *mask,
323 				    short ippeerlimit,
324 				    attr_val_fifo *flags, int line_no);
325 int_node *create_int_node(int val);
326 addr_opts_node *create_addr_opts_node(address_node *addr,
327 				      attr_val_fifo *options);
328 sim_node *create_sim_node(attr_val_fifo *init_opts,
329 			  server_info_fifo *servers);
330 setvar_node *create_setvar_node(char *var, char *val, int isdefault);
331 nic_rule_node *create_nic_rule_node(int match_class, char *if_name,
332 				    int action);
333 
334 script_info *create_sim_script_info(double duration,
335 				    attr_val_fifo *script_queue);
336 server_info *create_sim_server(address_node *addr, double server_offset,
337 			       script_info_fifo *script);
338 
339 extern struct REMOTE_CONFIG_INFO remote_config;
340 void config_remotely(sockaddr_u *);
341 
342 #ifdef SAVECONFIG
343 int dump_config_tree(config_tree *ptree, FILE *df, int comment);
344 int dump_all_config_trees(FILE *df, int comment);
345 #endif
346 
347 #if defined(HAVE_SETRLIMIT)
348 void ntp_rlimit(int, rlim_t, int, const char *);
349 #endif
350 
351 #endif	/* !defined(NTP_CONFIG_H) */
352