xref: /openbsd-src/usr.sbin/nsd/options.h (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
1 /*
2  * options.h -- nsd.conf options definitions and prototypes
3  *
4  * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
5  *
6  * See LICENSE for the license.
7  *
8  */
9 
10 #ifndef OPTIONS_H
11 #define OPTIONS_H
12 
13 #include "config.h"
14 #include <stdarg.h>
15 #include "region-allocator.h"
16 #include "rbtree.h"
17 struct query;
18 struct dname;
19 struct tsig_key;
20 struct buffer;
21 struct nsd;
22 
23 typedef struct nsd_options nsd_options_type;
24 typedef struct pattern_options pattern_options_type;
25 typedef struct zone_options zone_options_type;
26 typedef struct ip_address_option ip_address_option_type;
27 typedef struct acl_options acl_options_type;
28 typedef struct key_options key_options_type;
29 typedef struct config_parser_state config_parser_state_type;
30 /*
31  * Options global for nsd.
32  */
33 struct nsd_options {
34 	/* config file name */
35 	char* configfile;
36 	/* options for zones, by apex, contains zone_options */
37 	rbtree_type* zone_options;
38 	/* patterns, by name, contains pattern_options */
39 	rbtree_type* patterns;
40 
41 	/* free space in zonelist file, contains zonelist_bucket */
42 	rbtree_type* zonefree;
43 	/* number of free space lines in zonelist file */
44 	size_t zonefree_number;
45 	/* zonelist file if open */
46 	FILE* zonelist;
47 	/* last offset in file (or 0 if none) */
48 	off_t zonelist_off;
49 
50 	/* tree of zonestat names and their id values, entries are struct
51 	 * zonestatname with malloced key=stringname. The number of items
52 	 * is the max statnameid, no items are freed from this.
53 	 * kept correct in the xfrd process, and on startup. */
54 	rbtree_type* zonestatnames;
55 
56 	/* rbtree of keys defined, by name */
57 	rbtree_type* keys;
58 
59 	/* list of ip addresses to bind to (or NULL for all) */
60 	struct ip_address_option* ip_addresses;
61 
62 	int ip_transparent;
63 	int ip_freebind;
64 	int debug_mode;
65 	int verbosity;
66 	int hide_version;
67 	int do_ip4;
68 	int do_ip6;
69 	const char* database;
70 	const char* identity;
71 	const char* version;
72 	const char* logfile;
73 	int server_count;
74 	int tcp_count;
75 	int tcp_query_count;
76 	int tcp_timeout;
77 	int tcp_mss;
78 	int outgoing_tcp_mss;
79 	size_t ipv4_edns_size;
80 	size_t ipv6_edns_size;
81 	const char* pidfile;
82 	const char* port;
83 	int statistics;
84 	const char* chroot;
85 	const char* username;
86 	const char* zonesdir;
87 	const char* xfrdfile;
88 	const char* xfrdir;
89 	const char* zonelistfile;
90 	const char* nsid;
91 	int xfrd_reload_timeout;
92 	int zonefiles_check;
93 	int zonefiles_write;
94 	int log_time_ascii;
95 	int round_robin;
96 	int minimal_responses;
97 	int refuse_any;
98 	int reuseport;
99 
100         /** remote control section. enable toggle. */
101 	int control_enable;
102 	/** the interfaces the remote control should listen on */
103 	struct ip_address_option* control_interface;
104 	/** port number for the control port */
105 	int control_port;
106 	/** private key file for server */
107 	char* server_key_file;
108 	/** certificate file for server */
109 	char* server_cert_file;
110 	/** private key file for nsd-control */
111 	char* control_key_file;
112 	/** certificate file for nsd-control */
113 	char* control_cert_file;
114 
115 #ifdef RATELIMIT
116 	/** number of buckets in rrl hashtable */
117 	size_t rrl_size;
118 	/** max qps for queries, 0 is nolimit */
119 	size_t rrl_ratelimit;
120 	/** ratio of slipped responses, 0 is noslip */
121 	size_t rrl_slip;
122 	/** ip prefix length */
123 	size_t rrl_ipv4_prefix_length;
124 	size_t rrl_ipv6_prefix_length;
125 	/** max qps for whitelisted queries, 0 is nolimit */
126 	size_t rrl_whitelist_ratelimit;
127 #endif
128 	/** if dnstap is enabled */
129 	int dnstap_enable;
130 	/** dnstap socket path */
131 	char* dnstap_socket_path;
132 	/** true to send "identity" via dnstap */
133 	int dnstap_send_identity;
134 	/** true to send "version" via dnstap */
135 	int dnstap_send_version;
136 	/** dnstap "identity", hostname is used if "". */
137 	char* dnstap_identity;
138 	/** dnstap "version", package version is used if "". */
139 	char* dnstap_version;
140 	/** true to log dnstap AUTH_QUERY message events */
141 	int dnstap_log_auth_query_messages;
142 	/** true to log dnstap AUTH_RESPONSE message events */
143 	int dnstap_log_auth_response_messages;
144 
145 	region_type* region;
146 };
147 
148 struct ip_address_option {
149 	struct ip_address_option* next;
150 	char* address;
151 };
152 
153 /*
154  * Pattern of zone options, used to contain options for zone(s).
155  */
156 struct pattern_options {
157 	rbnode_type node;
158 	const char* pname; /* name of the pattern, key of rbtree */
159 	const char* zonefile;
160 	struct acl_options* allow_notify;
161 	struct acl_options* request_xfr;
162 	struct acl_options* notify;
163 	struct acl_options* provide_xfr;
164 	struct acl_options* outgoing_interface;
165 	const char* zonestats;
166 #ifdef RATELIMIT
167 	uint16_t rrl_whitelist; /* bitmap with rrl types */
168 #endif
169 	uint8_t allow_axfr_fallback;
170 	uint8_t allow_axfr_fallback_is_default;
171 	uint8_t notify_retry;
172 	uint8_t notify_retry_is_default;
173 	uint8_t implicit; /* pattern is implicit, part_of_config zone used */
174 	uint8_t xfrd_flags;
175 	uint32_t max_refresh_time;
176 	uint8_t max_refresh_time_is_default;
177 	uint32_t min_refresh_time;
178 	uint8_t min_refresh_time_is_default;
179 	uint32_t max_retry_time;
180 	uint8_t max_retry_time_is_default;
181 	uint32_t min_retry_time;
182 	uint8_t min_retry_time_is_default;
183 	uint64_t size_limit_xfr;
184 	uint8_t multi_master_check;
185 } ATTR_PACKED;
186 
187 #define PATTERN_IMPLICIT_MARKER "_implicit_"
188 
189 /*
190  * Options for a zone
191  */
192 struct zone_options {
193 	/* key is dname of apex */
194 	rbnode_type node;
195 
196 	/* is apex of the zone */
197 	const char* name;
198 	/* if not part of config, the offset and linesize of zonelist entry */
199 	off_t off;
200 	int linesize;
201 	/* pattern for the zone options, if zone is part_of_config, this is
202 	 * a anonymous pattern created in-place */
203 	struct pattern_options* pattern;
204 	/* zone is fixed into the main config, not in zonelist, cannot delete */
205 	uint8_t part_of_config;
206 } ATTR_PACKED;
207 
208 union acl_addr_storage {
209 #ifdef INET6
210 	struct in_addr addr;
211 	struct in6_addr addr6;
212 #else
213 	struct in_addr addr;
214 #endif
215 };
216 
217 /*
218  * Access control list element
219  */
220 struct acl_options {
221 	struct acl_options* next;
222 
223 	/* options */
224 	time_t ixfr_disabled;
225 	int bad_xfr_count;
226 	uint8_t use_axfr_only;
227 	uint8_t allow_udp;
228 
229 	/* ip address range */
230 	const char* ip_address_spec;
231 	uint8_t is_ipv6;
232 	unsigned int port;	/* is 0(no port) or suffix @port value */
233 	union acl_addr_storage addr;
234 	union acl_addr_storage range_mask;
235 	enum {
236 		acl_range_single = 0,	/* single address */
237 		acl_range_mask = 1,	/* 10.20.30.40&255.255.255.0 */
238 		acl_range_subnet = 2,	/* 10.20.30.40/28 */
239 		acl_range_minmax = 3	/* 10.20.30.40-10.20.30.60 (mask=max) */
240 	} rangetype;
241 
242 	/* key */
243 	uint8_t nokey;
244 	uint8_t blocked;
245 	const char* key_name;
246 	struct key_options* key_options;
247 } ATTR_PACKED;
248 
249 /*
250  * Key definition
251  */
252 struct key_options {
253 	rbnode_type node; /* key of tree is name */
254 	char* name;
255 	char* algorithm;
256 	char* secret;
257 	struct tsig_key* tsig_key;
258 } ATTR_PACKED;
259 
260 /** zone list free space */
261 struct zonelist_free {
262 	struct zonelist_free* next;
263 	off_t off;
264 };
265 /** zonelist free bucket for a particular line length */
266 struct zonelist_bucket {
267 	rbnode_type node; /* key is ptr to linesize */
268 	int linesize;
269 	struct zonelist_free* list;
270 };
271 
272 /* default zonefile write interval if database is "", in seconds */
273 #define ZONEFILES_WRITE_INTERVAL 3600
274 
275 struct zonestatname {
276 	rbnode_type node; /* key is malloced string with cooked zonestat name */
277 	unsigned id; /* index in nsd.zonestat array */
278 };
279 
280 /*
281  * Used during options parsing
282  */
283 struct config_parser_state {
284 	char* filename;
285 	const char* chroot;
286 	int line;
287 	int errors;
288 	int server_settings_seen;
289 	struct nsd_options* opt;
290 	struct pattern_options* current_pattern;
291 	struct zone_options* current_zone;
292 	struct key_options* current_key;
293 	struct ip_address_option* current_ip_address_option;
294 	struct acl_options* current_allow_notify;
295 	struct acl_options* current_request_xfr;
296 	struct acl_options* current_notify;
297 	struct acl_options* current_provide_xfr;
298 	struct acl_options* current_outgoing_interface;
299 	void (*err)(void*,const char*);
300 	void* err_arg;
301 };
302 
303 extern config_parser_state_type* cfg_parser;
304 
305 /* region will be put in nsd_options struct. Returns empty options struct. */
306 struct nsd_options* nsd_options_create(region_type* region);
307 /* the number of zones that are configured */
308 static inline size_t nsd_options_num_zones(struct nsd_options* opt)
309 { return opt->zone_options->count; }
310 /* insert a zone into the main options tree, returns 0 on error */
311 int nsd_options_insert_zone(struct nsd_options* opt, struct zone_options* zone);
312 /* insert a pattern into the main options tree, returns 0 on error */
313 int nsd_options_insert_pattern(struct nsd_options* opt,
314 	struct pattern_options* pat);
315 
316 /* parses options file. Returns false on failure. callback, if nonNULL,
317  * gets called with error strings, default prints. */
318 int parse_options_file(struct nsd_options* opt, const char* file,
319 	void (*err)(void*,const char*), void* err_arg);
320 struct zone_options* zone_options_create(region_type* region);
321 void zone_options_delete(struct nsd_options* opt, struct zone_options* zone);
322 /* find a zone by apex domain name, or NULL if not found. */
323 struct zone_options* zone_options_find(struct nsd_options* opt,
324 	const struct dname* apex);
325 struct pattern_options* pattern_options_create(region_type* region);
326 struct pattern_options* pattern_options_find(struct nsd_options* opt, const char* name);
327 int pattern_options_equal(struct pattern_options* p, struct pattern_options* q);
328 void pattern_options_remove(struct nsd_options* opt, const char* name);
329 void pattern_options_add_modify(struct nsd_options* opt,
330 	struct pattern_options* p);
331 void pattern_options_marshal(struct buffer* buffer, struct pattern_options* p);
332 struct pattern_options* pattern_options_unmarshal(region_type* r,
333 	struct buffer* b);
334 struct key_options* key_options_create(region_type* region);
335 void key_options_insert(struct nsd_options* opt, struct key_options* key);
336 struct key_options* key_options_find(struct nsd_options* opt, const char* name);
337 void key_options_remove(struct nsd_options* opt, const char* name);
338 int key_options_equal(struct key_options* p, struct key_options* q);
339 void key_options_add_modify(struct nsd_options* opt, struct key_options* key);
340 void key_options_setup(region_type* region, struct key_options* key);
341 void key_options_desetup(region_type* region, struct key_options* key);
342 /* read in zone list file. Returns false on failure */
343 int parse_zone_list_file(struct nsd_options* opt);
344 /* create zone entry and add to the zonelist file */
345 struct zone_options* zone_list_add(struct nsd_options* opt, const char* zname,
346 	const char* pname);
347 /* create zonelist entry, do not insert in file (called by _add) */
348 struct zone_options* zone_list_zone_insert(struct nsd_options* opt,
349 	const char* nm, const char* patnm, int linesize, off_t off);
350 void zone_list_del(struct nsd_options* opt, struct zone_options* zone);
351 void zone_list_compact(struct nsd_options* opt);
352 void zone_list_close(struct nsd_options* opt);
353 
354 /* create zonestat name tree , for initially created zones */
355 void options_zonestatnames_create(struct nsd_options* opt);
356 /* Get zonestat id for zone options, add new entry if necessary.
357  * instantiates the pattern's zonestat string */
358 unsigned getzonestatid(struct nsd_options* opt, struct zone_options* zopt);
359 /* create string, same options as zonefile but no chroot changes */
360 const char* config_cook_string(struct zone_options* zone, const char* input);
361 
362 /** check if config for remote control turns on IP-address interface
363  * with certificates or a named pipe without certificates. */
364 int options_remote_is_address(struct nsd_options* cfg);
365 
366 #if defined(HAVE_SSL)
367 /* tsig must be inited, adds all keys in options to tsig. */
368 void key_options_tsig_add(struct nsd_options* opt);
369 #endif
370 
371 /* check acl list, acl number that matches if passed(0..),
372  * or failure (-1) if dropped */
373 /* the reason why (the acl) is returned too (or NULL) */
374 int acl_check_incoming(struct acl_options* acl, struct query* q,
375 	struct acl_options** reason);
376 int acl_addr_matches_host(struct acl_options* acl, struct acl_options* host);
377 int acl_addr_matches(struct acl_options* acl, struct query* q);
378 int acl_key_matches(struct acl_options* acl, struct query* q);
379 int acl_addr_match_mask(uint32_t* a, uint32_t* b, uint32_t* mask, size_t sz);
380 int acl_addr_match_range(uint32_t* minval, uint32_t* x, uint32_t* maxval, size_t sz);
381 
382 /* returns true if acls are both from the same host */
383 int acl_same_host(struct acl_options* a, struct acl_options* b);
384 /* find acl by number in the list */
385 struct acl_options* acl_find_num(struct acl_options* acl, int num);
386 
387 /* see if two acl lists are the same (same elements in same order, or empty) */
388 int acl_list_equal(struct acl_options* p, struct acl_options* q);
389 /* see if two acl are the same */
390 int acl_equal(struct acl_options* p, struct acl_options* q);
391 
392 /* see if a zone is a slave or a master zone */
393 int zone_is_slave(struct zone_options* opt);
394 /* create zonefile name, returns static pointer (perhaps to options data) */
395 const char* config_make_zonefile(struct zone_options* zone, struct nsd* nsd);
396 
397 #define ZONEC_PCT_TIME 5 /* seconds, then it starts to print pcts */
398 #define ZONEC_PCT_COUNT 100000 /* elements before pct check is done */
399 
400 /* parsing helpers */
401 void c_error(const char* msg);
402 void c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
403 struct acl_options* parse_acl_info(region_type* region, char* ip,
404 	const char* key);
405 /* true if ipv6 address, false if ipv4 */
406 int parse_acl_is_ipv6(const char* p);
407 /* returns range type. mask is the 2nd part of the range */
408 int parse_acl_range_type(char* ip, char** mask);
409 /* parses subnet mask, fills 0 mask as well */
410 void parse_acl_range_subnet(char* p, void* addr, int maxbits);
411 /* clean up options */
412 void nsd_options_destroy(struct nsd_options* opt);
413 /* replace occurrences of one with two in buf, pass length of buffer */
414 void replace_str(char* buf, size_t len, const char* one, const char* two);
415 /* apply pattern to the existing pattern in the parser */
416 void config_apply_pattern(const char* name);
417 
418 #endif /* OPTIONS_H */
419