config_file.h revision 285206
1/*
2 * util/config_file.h - reads and stores the config file for unbound.
3 *
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/**
37 * \file
38 *
39 * This file contains functions for the config file.
40 */
41
42#ifndef UTIL_CONFIG_FILE_H
43#define UTIL_CONFIG_FILE_H
44struct config_stub;
45struct config_strlist;
46struct config_str2list;
47struct module_qstate;
48struct sock_list;
49struct ub_packed_rrset_key;
50
51/**
52 * The configuration options.
53 * Strings are malloced.
54 */
55struct config_file {
56	/** verbosity level as specified in the config file */
57	int verbosity;
58
59	/** statistics interval (in seconds) */
60	int stat_interval;
61	/** if false, statistics values are reset after printing them */
62	int stat_cumulative;
63	/** if true, the statistics are kept in greater detail */
64	int stat_extended;
65
66	/** number of threads to create */
67	int num_threads;
68
69	/** port on which queries are answered. */
70	int port;
71	/** do ip4 query support. */
72	int do_ip4;
73	/** do ip6 query support. */
74	int do_ip6;
75	/** do udp query support. */
76	int do_udp;
77	/** do tcp query support. */
78	int do_tcp;
79	/** tcp upstream queries (no UDP upstream queries) */
80	int tcp_upstream;
81
82	/** private key file for dnstcp-ssl service (enabled if not NULL) */
83	char* ssl_service_key;
84	/** public key file for dnstcp-ssl service */
85	char* ssl_service_pem;
86	/** port on which to provide ssl service */
87	int ssl_port;
88	/** if outgoing tcp connections use SSL */
89	int ssl_upstream;
90
91	/** outgoing port range number of ports (per thread) */
92	int outgoing_num_ports;
93	/** number of outgoing tcp buffers per (per thread) */
94	size_t outgoing_num_tcp;
95	/** number of incoming tcp buffers per (per thread) */
96	size_t incoming_num_tcp;
97	/** allowed udp port numbers, array with 0 if not allowed */
98	int* outgoing_avail_ports;
99
100	/** EDNS buffer size to use */
101	size_t edns_buffer_size;
102	/** number of bytes buffer size for DNS messages */
103	size_t msg_buffer_size;
104	/** size of the message cache */
105	size_t msg_cache_size;
106	/** slabs in the message cache. */
107	size_t msg_cache_slabs;
108	/** number of queries every thread can service */
109	size_t num_queries_per_thread;
110	/** number of msec to wait before items can be jostled out */
111	size_t jostle_time;
112	/** size of the rrset cache */
113	size_t rrset_cache_size;
114	/** slabs in the rrset cache */
115	size_t rrset_cache_slabs;
116	/** host cache ttl in seconds */
117	int host_ttl;
118	/** number of slabs in the infra host cache */
119	size_t infra_cache_slabs;
120	/** max number of hosts in the infra cache */
121	size_t infra_cache_numhosts;
122	/** min value for infra cache rtt */
123	int infra_cache_min_rtt;
124	/** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
125	int delay_close;
126
127	/** the target fetch policy for the iterator */
128	char* target_fetch_policy;
129
130	/** automatic interface for incoming messages. Uses ipv6 remapping,
131	 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
132	int if_automatic;
133	/** SO_RCVBUF size to set on port 53 UDP socket */
134	size_t so_rcvbuf;
135	/** SO_SNDBUF size to set on port 53 UDP socket */
136	size_t so_sndbuf;
137	/** SO_REUSEPORT requested on port 53 sockets */
138	int so_reuseport;
139
140	/** number of interfaces to open. If 0 default all interfaces. */
141	int num_ifs;
142	/** interface description strings (IP addresses) */
143	char **ifs;
144
145	/** number of outgoing interfaces to open.
146	 * If 0 default all interfaces. */
147	int num_out_ifs;
148	/** outgoing interface description strings (IP addresses) */
149	char **out_ifs;
150
151	/** the root hints */
152	struct config_strlist* root_hints;
153	/** the stub definitions, linked list */
154	struct config_stub* stubs;
155	/** the forward zone definitions, linked list */
156	struct config_stub* forwards;
157	/** list of donotquery addresses, linked list */
158	struct config_strlist* donotqueryaddrs;
159	/** list of access control entries, linked list */
160	struct config_str2list* acls;
161	/** use default localhost donotqueryaddr entries */
162	int donotquery_localhost;
163
164	/** harden against very small edns buffer sizes */
165	int harden_short_bufsize;
166	/** harden against very large query sizes */
167	int harden_large_queries;
168	/** harden against spoofed glue (out of zone data) */
169	int harden_glue;
170	/** harden against receiving no DNSSEC data for trust anchor */
171	int harden_dnssec_stripped;
172	/** harden against queries that fall under known nxdomain names */
173	int harden_below_nxdomain;
174	/** harden the referral path, query for NS,A,AAAA and validate */
175	int harden_referral_path;
176	/** use 0x20 bits in query as random ID bits */
177	int use_caps_bits_for_id;
178	/** strip away these private addrs from answers, no DNS Rebinding */
179	struct config_strlist* private_address;
180	/** allow domain (and subdomains) to use private address space */
181	struct config_strlist* private_domain;
182	/** what threshold for unwanted action. */
183	size_t unwanted_threshold;
184	/** the number of seconds maximal TTL used for RRsets and messages */
185	int max_ttl;
186	/** the number of seconds minimum TTL used for RRsets and messages */
187	int min_ttl;
188	/** if prefetching of messages should be performed. */
189	int prefetch;
190	/** if prefetching of DNSKEYs should be performed. */
191	int prefetch_key;
192
193	/** chrootdir, if not "" or chroot will be done */
194	char* chrootdir;
195	/** username to change to, if not "". */
196	char* username;
197	/** working directory */
198	char* directory;
199	/** filename to log to. */
200	char* logfile;
201	/** pidfile to write pid to. */
202	char* pidfile;
203
204	/** should log messages be sent to syslogd */
205	int use_syslog;
206	/** log timestamp in ascii UTC */
207	int log_time_ascii;
208	/** log queries with one line per query */
209	int log_queries;
210
211	/** do not report identity (id.server, hostname.bind) */
212	int hide_identity;
213	/** do not report version (version.server, version.bind) */
214	int hide_version;
215	/** identity, hostname is returned if "". */
216	char* identity;
217	/** version, package version returned if "". */
218	char* version;
219
220	/** the module configuration string */
221	char* module_conf;
222
223	/** files with trusted DS and DNSKEYs in zonefile format, list */
224	struct config_strlist* trust_anchor_file_list;
225	/** list of trustanchor keys, linked list */
226	struct config_strlist* trust_anchor_list;
227	/** files with 5011 autotrust tracked keys */
228	struct config_strlist* auto_trust_anchor_file_list;
229	/** files with trusted DNSKEYs in named.conf format, list */
230	struct config_strlist* trusted_keys_file_list;
231	/** DLV anchor file */
232	char* dlv_anchor_file;
233	/** DLV anchor inline */
234	struct config_strlist* dlv_anchor_list;
235	/** insecure domain list */
236	struct config_strlist* domain_insecure;
237
238	/** if not 0, this value is the validation date for RRSIGs */
239	int32_t val_date_override;
240	/** the minimum for signature clock skew */
241	int32_t val_sig_skew_min;
242	/** the maximum for signature clock skew */
243	int32_t val_sig_skew_max;
244	/** this value sets the number of seconds before revalidating bogus */
245	int bogus_ttl;
246	/** should validator clean additional section for secure msgs */
247	int val_clean_additional;
248	/** log bogus messages by the validator */
249	int val_log_level;
250	/** squelch val_log_level to log - this is library goes to callback */
251	int val_log_squelch;
252	/** should validator allow bogus messages to go through */
253	int val_permissive_mode;
254	/** ignore the CD flag in incoming queries and refuse them bogus data */
255	int ignore_cd;
256	/** nsec3 maximum iterations per key size, string */
257	char* val_nsec3_key_iterations;
258	/** autotrust add holddown time, in seconds */
259	unsigned int add_holddown;
260	/** autotrust del holddown time, in seconds */
261	unsigned int del_holddown;
262	/** autotrust keep_missing time, in seconds. 0 is forever. */
263	unsigned int keep_missing;
264
265	/** size of the key cache */
266	size_t key_cache_size;
267	/** slabs in the key cache. */
268	size_t key_cache_slabs;
269	/** size of the neg cache */
270	size_t neg_cache_size;
271
272	/** local zones config */
273	struct config_str2list* local_zones;
274	/** local zones nodefault list */
275	struct config_strlist* local_zones_nodefault;
276	/** local data RRs configged */
277	struct config_strlist* local_data;
278	/** unblock lan zones (reverse lookups for 10/8 and so on) */
279	int unblock_lan_zones;
280
281	/** remote control section. enable toggle. */
282	int remote_control_enable;
283	/** the interfaces the remote control should listen on */
284	struct config_strlist* control_ifs;
285	/** port number for the control port */
286	int control_port;
287	/** use certificates for remote control */
288	int remote_control_use_cert;
289	/** private key file for server */
290	char* server_key_file;
291	/** certificate file for server */
292	char* server_cert_file;
293	/** private key file for unbound-control */
294	char* control_key_file;
295	/** certificate file for unbound-control */
296	char* control_cert_file;
297
298	/** Python script file */
299	char* python_script;
300
301	/** daemonize, i.e. fork into the background. */
302	int do_daemonize;
303
304	/* minimal response when positive answer */
305	int minimal_responses;
306
307	/* RRSet roundrobin */
308	int rrset_roundrobin;
309
310	/* maximum UDP response size */
311	size_t max_udp_size;
312
313	/* DNS64 prefix */
314	char* dns64_prefix;
315
316	/* Synthetize all AAAA record despite the presence of an authoritative one */
317	int dns64_synthall;
318
319	/** true to enable dnstap support */
320	int dnstap;
321	/** dnstap socket path */
322	char* dnstap_socket_path;
323	/** true to send "identity" via dnstap */
324	int dnstap_send_identity;
325	/** true to send "version" via dnstap */
326	int dnstap_send_version;
327	/** dnstap "identity", hostname is used if "". */
328	char* dnstap_identity;
329	/** dnstap "version", package version is used if "". */
330	char* dnstap_version;
331
332	/** true to log dnstap RESOLVER_QUERY message events */
333	int dnstap_log_resolver_query_messages;
334	/** true to log dnstap RESOLVER_RESPONSE message events */
335	int dnstap_log_resolver_response_messages;
336	/** true to log dnstap CLIENT_QUERY message events */
337	int dnstap_log_client_query_messages;
338	/** true to log dnstap CLIENT_RESPONSE message events */
339	int dnstap_log_client_response_messages;
340	/** true to log dnstap FORWARDER_QUERY message events */
341	int dnstap_log_forwarder_query_messages;
342	/** true to log dnstap FORWARDER_RESPONSE message events */
343	int dnstap_log_forwarder_response_messages;
344};
345
346/** from cfg username, after daemonise setup performed */
347extern uid_t cfg_uid;
348/** from cfg username, after daemonise setup performed */
349extern gid_t cfg_gid;
350
351/**
352 * Stub config options
353 */
354struct config_stub {
355	/** next in list */
356	struct config_stub* next;
357	/** domain name (in text) of the stub apex domain */
358	char* name;
359	/** list of stub nameserver hosts (domain name) */
360	struct config_strlist* hosts;
361	/** list of stub nameserver addresses (IP address) */
362	struct config_strlist* addrs;
363	/** if stub-prime is set */
364	int isprime;
365	/** if forward-first is set (failover to without if fails) */
366	int isfirst;
367};
368
369/**
370 * List of strings for config options
371 */
372struct config_strlist {
373	/** next item in list */
374	struct config_strlist* next;
375	/** config option string */
376	char* str;
377};
378
379/**
380 * List of two strings for config options
381 */
382struct config_str2list {
383	/** next item in list */
384	struct config_str2list* next;
385	/** first string */
386	char* str;
387	/** second string */
388	char* str2;
389};
390
391/** List head for strlist processing, used for append operation. */
392struct config_strlist_head {
393	/** first in list of text items */
394	struct config_strlist* first;
395	/** last in list of text items */
396	struct config_strlist* last;
397};
398
399/**
400 * Create config file structure. Filled with default values.
401 * @return: the new structure or NULL on memory error.
402 */
403struct config_file* config_create(void);
404
405/**
406 * Create config file structure for library use. Filled with default values.
407 * @return: the new structure or NULL on memory error.
408 */
409struct config_file* config_create_forlib(void);
410
411/**
412 * Read the config file from the specified filename.
413 * @param config: where options are stored into, must be freshly created.
414 * @param filename: name of configfile. If NULL nothing is done.
415 * @param chroot: if not NULL, the chroot dir currently in use (for include).
416 * @return: false on error. In that case errno is set, ENOENT means
417 * 	file not found.
418 */
419int config_read(struct config_file* config, const char* filename,
420	const char* chroot);
421
422/**
423 * Destroy the config file structure.
424 * @param config: to delete.
425 */
426void config_delete(struct config_file* config);
427
428/**
429 * Apply config to global constants; this routine is called in single thread.
430 * @param config: to apply. Side effect: global constants change.
431 */
432void config_apply(struct config_file* config);
433
434/**
435 * Find username, sets cfg_uid and cfg_gid.
436 * @param config: the config structure.
437 */
438void config_lookup_uid(struct config_file* config);
439
440/**
441 * Set the given keyword to the given value.
442 * @param config: where to store config
443 * @param option: option name, including the ':' character.
444 * @param value: value, this string is copied if needed, or parsed.
445 * 	The caller owns the value string.
446 * @return 0 on error (malloc or syntax error).
447 */
448int config_set_option(struct config_file* config, const char* option,
449	const char* value);
450
451/**
452 * Call print routine for the given option.
453 * @param cfg: config.
454 * @param opt: option name without trailing :.
455 *	This is different from config_set_option.
456 * @param func: print func, called as (str, arg) for every data element.
457 * @param arg: user argument for print func.
458 * @return false if the option name is not supported (syntax error).
459 */
460int config_get_option(struct config_file* cfg, const char* opt,
461	void (*func)(char*,void*), void* arg);
462
463/**
464 * Get an option and return strlist
465 * @param cfg: config file
466 * @param opt: option name.
467 * @param list: list is returned here. malloced, caller must free it.
468 * @return 0=OK, 1=syntax error, 2=malloc failed.
469 */
470int config_get_option_list(struct config_file* cfg, const char* opt,
471	struct config_strlist** list);
472
473/**
474 * Get an option and collate results into string
475 * @param cfg: config file
476 * @param opt: option name.
477 * @param str: string. malloced, caller must free it.
478 * @return 0=OK, 1=syntax error, 2=malloc failed.
479 */
480int config_get_option_collate(struct config_file* cfg, const char* opt,
481	char** str);
482
483/**
484 * function to print to a file, use as func with config_get_option.
485 * @param line: text to print. \n appended.
486 * @param arg: pass a FILE*, like stdout.
487 */
488void config_print_func(char* line, void* arg);
489
490/**
491 * function to collate the text strings into a strlist_head.
492 * @param line: text to append.
493 * @param arg: pass a strlist_head structure. zeroed on start.
494 */
495void config_collate_func(char* line, void* arg);
496
497/**
498 * take a strlist_head list and return a malloc string. separated with newline.
499 * @param list: strlist first to collate. zeroes return "".
500 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
501 */
502char* config_collate_cat(struct config_strlist* list);
503
504/**
505 * Append text at end of list.
506 * @param list: list head. zeroed at start.
507 * @param item: new item. malloced by caller. if NULL the insertion fails.
508 * @return true on success.
509 */
510int cfg_strlist_append(struct config_strlist_head* list, char* item);
511
512/**
513 * Insert string into strlist.
514 * @param head: pointer to strlist head variable.
515 * @param item: new item. malloced by caller. If NULL the insertion fails.
516 * @return: true on success.
517 */
518int cfg_strlist_insert(struct config_strlist** head, char* item);
519
520/**
521 * Insert string into str2list.
522 * @param head: pointer to str2list head variable.
523 * @param item: new item. malloced by caller. If NULL the insertion fails.
524 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
525 * @return: true on success.
526 */
527int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
528
529/**
530 * Delete items in config string list.
531 * @param list: list.
532 */
533void config_delstrlist(struct config_strlist* list);
534
535/**
536 * Delete items in config double string list.
537 * @param list: list.
538 */
539void config_deldblstrlist(struct config_str2list* list);
540
541/**
542 * Delete items in config stub list.
543 * @param list: list.
544 */
545void config_delstubs(struct config_stub* list);
546
547/**
548 * Convert 14digit to time value
549 * @param str: string of 14 digits
550 * @return time value or 0 for error.
551 */
552time_t cfg_convert_timeval(const char* str);
553
554/**
555 * Count number of values in the string.
556 * format ::= (sp num)+ sp
557 * num ::= [-](0-9)+
558 * sp ::= (space|tab)*
559 *
560 * @param str: string
561 * @return: 0 on parse error, or empty string, else
562 *	number of integer values in the string.
563 */
564int cfg_count_numbers(const char* str);
565
566/**
567 * Convert a 'nice' memory or file size into a bytecount
568 * From '100k' to 102400. and so on. Understands kKmMgG.
569 * k=1024, m=1024*1024, g=1024*1024*1024.
570 * @param str: string
571 * @param res: result is stored here, size in bytes.
572 * @return: true if parsed correctly, or 0 on a parse error (and an error
573 * is logged).
574 */
575int cfg_parse_memsize(const char* str, size_t* res);
576
577/**
578 * Parse local-zone directive into two strings and register it in the config.
579 * @param cfg: to put it in.
580 * @param val: argument strings to local-zone, "example.com nodefault".
581 * @return: false on failure
582 */
583int cfg_parse_local_zone(struct config_file* cfg, const char* val);
584
585/**
586 * Mark "number" or "low-high" as available or not in ports array.
587 * @param str: string in input
588 * @param allow: give true if this range is permitted.
589 * @param avail: the array from cfg.
590 * @param num: size of the array (65536).
591 * @return: true if parsed correctly, or 0 on a parse error (and an error
592 * is logged).
593 */
594int cfg_mark_ports(const char* str, int allow, int* avail, int num);
595
596/**
597 * Get a condensed list of ports returned. allocated.
598 * @param cfg: config file.
599 * @param avail: the available ports array is returned here.
600 * @return: number of ports in array or 0 on error.
601 */
602int cfg_condense_ports(struct config_file* cfg, int** avail);
603
604/**
605 * Scan ports available
606 * @param avail: the array from cfg.
607 * @param num: size of the array (65536).
608 * @return the number of ports available for use.
609 */
610int cfg_scan_ports(int* avail, int num);
611
612/**
613 * Convert a filename to full pathname in original filesys
614 * @param fname: the path name to convert.
615 *      Must not be null or empty.
616 * @param cfg: config struct for chroot and chdir (if set).
617 * @param use_chdir: if false, only chroot is applied.
618 * @return pointer to malloced buffer which is: [chroot][chdir]fname
619 *      or NULL on malloc failure.
620 */
621char* fname_after_chroot(const char* fname, struct config_file* cfg,
622	int use_chdir);
623
624/**
625 * Convert a ptr shorthand into a full reverse-notation PTR record.
626 * @param str: input string, "IP name"
627 * @return: malloced string "reversed-ip-name PTR name"
628 */
629char* cfg_ptr_reverse(char* str);
630
631/**
632 * Append text to the error info for validation.
633 * @param qstate: query state.
634 * @param str: copied into query region and appended.
635 * Failures to allocate are logged.
636 */
637void errinf(struct module_qstate* qstate, const char* str);
638
639/**
640 * Append text to error info:  from 1.2.3.4
641 * @param qstate: query state.
642 * @param origin: sock list with origin of trouble.
643 *	Every element added.
644 *	If NULL: nothing is added.
645 *	if 0len element: 'from cache' is added.
646 */
647void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
648
649/**
650 * Append text to error info:  for RRset name type class
651 * @param qstate: query state.
652 * @param rr: rrset_key.
653 */
654void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
655
656/**
657 * Append text to error info:  str dname
658 * @param qstate: query state.
659 * @param str: explanation string
660 * @param dname: the dname.
661 */
662void errinf_dname(struct module_qstate* qstate, const char* str,
663	uint8_t* dname);
664
665/**
666 * Create error info in string
667 * @param qstate: query state.
668 * @return string or NULL on malloc failure (already logged).
669 *    This string is malloced and has to be freed by caller.
670 */
671char* errinf_to_str(struct module_qstate* qstate);
672
673/**
674 * Used during options parsing
675 */
676struct config_parser_state {
677	/** name of file being parser */
678	char* filename;
679	/** line number in the file, starts at 1 */
680	int line;
681	/** number of errors encountered */
682	int errors;
683	/** the result of parsing is stored here. */
684	struct config_file* cfg;
685	/** the current chroot dir (or NULL if none) */
686	const char* chroot;
687};
688
689/** global config parser object used during config parsing */
690extern struct config_parser_state* cfg_parser;
691/** init lex state */
692void init_cfg_parse(void);
693/** lex in file */
694extern FILE* ub_c_in;
695/** lex out file */
696extern FILE* ub_c_out;
697/** the yacc lex generated parse function */
698int ub_c_parse(void);
699/** the lexer function */
700int ub_c_lex(void);
701/** wrap function */
702int ub_c_wrap(void);
703/** parsing helpers: print error with file and line numbers */
704void ub_c_error(const char* msg);
705/** parsing helpers: print error with file and line numbers */
706void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
707
708#ifdef UB_ON_WINDOWS
709/**
710 * Obtain registry string (if it exists).
711 * @param key: key string
712 * @param name: name of value to fetch.
713 * @return malloced string with the result or NULL if it did not
714 * 	exist on an error (logged with log_err) was encountered.
715 */
716char* w_lookup_reg_str(const char* key, const char* name);
717#endif /* UB_ON_WINDOWS */
718
719#endif /* UTIL_CONFIG_FILE_H */
720