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