config_file.h revision 269257
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	/** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
123	int delay_close;
124
125	/** the target fetch policy for the iterator */
126	char* target_fetch_policy;
127
128	/** automatic interface for incoming messages. Uses ipv6 remapping,
129	 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
130	int if_automatic;
131	/** SO_RCVBUF size to set on port 53 UDP socket */
132	size_t so_rcvbuf;
133	/** SO_SNDBUF size to set on port 53 UDP socket */
134	size_t so_sndbuf;
135	/** SO_REUSEPORT requested on port 53 sockets */
136	int so_reuseport;
137
138	/** number of interfaces to open. If 0 default all interfaces. */
139	int num_ifs;
140	/** interface description strings (IP addresses) */
141	char **ifs;
142
143	/** number of outgoing interfaces to open.
144	 * If 0 default all interfaces. */
145	int num_out_ifs;
146	/** outgoing interface description strings (IP addresses) */
147	char **out_ifs;
148
149	/** the root hints */
150	struct config_strlist* root_hints;
151	/** the stub definitions, linked list */
152	struct config_stub* stubs;
153	/** the forward zone definitions, linked list */
154	struct config_stub* forwards;
155	/** list of donotquery addresses, linked list */
156	struct config_strlist* donotqueryaddrs;
157	/** list of access control entries, linked list */
158	struct config_str2list* acls;
159	/** use default localhost donotqueryaddr entries */
160	int donotquery_localhost;
161
162	/** harden against very small edns buffer sizes */
163	int harden_short_bufsize;
164	/** harden against very large query sizes */
165	int harden_large_queries;
166	/** harden against spoofed glue (out of zone data) */
167	int harden_glue;
168	/** harden against receiving no DNSSEC data for trust anchor */
169	int harden_dnssec_stripped;
170	/** harden against queries that fall under known nxdomain names */
171	int harden_below_nxdomain;
172	/** harden the referral path, query for NS,A,AAAA and validate */
173	int harden_referral_path;
174	/** use 0x20 bits in query as random ID bits */
175	int use_caps_bits_for_id;
176	/** strip away these private addrs from answers, no DNS Rebinding */
177	struct config_strlist* private_address;
178	/** allow domain (and subdomains) to use private address space */
179	struct config_strlist* private_domain;
180	/** what threshold for unwanted action. */
181	size_t unwanted_threshold;
182	/** the number of seconds maximal TTL used for RRsets and messages */
183	int max_ttl;
184	/** the number of seconds minimum TTL used for RRsets and messages */
185	int min_ttl;
186	/** if prefetching of messages should be performed. */
187	int prefetch;
188	/** if prefetching of DNSKEYs should be performed. */
189	int prefetch_key;
190
191	/** chrootdir, if not "" or chroot will be done */
192	char* chrootdir;
193	/** username to change to, if not "". */
194	char* username;
195	/** working directory */
196	char* directory;
197	/** filename to log to. */
198	char* logfile;
199	/** pidfile to write pid to. */
200	char* pidfile;
201
202	/** should log messages be sent to syslogd */
203	int use_syslog;
204	/** log timestamp in ascii UTC */
205	int log_time_ascii;
206	/** log queries with one line per query */
207	int log_queries;
208
209	/** do not report identity (id.server, hostname.bind) */
210	int hide_identity;
211	/** do not report version (version.server, version.bind) */
212	int hide_version;
213	/** identity, hostname is returned if "". */
214	char* identity;
215	/** version, package version returned if "". */
216	char* version;
217
218	/** the module configuration string */
219	char* module_conf;
220
221	/** files with trusted DS and DNSKEYs in zonefile format, list */
222	struct config_strlist* trust_anchor_file_list;
223	/** list of trustanchor keys, linked list */
224	struct config_strlist* trust_anchor_list;
225	/** files with 5011 autotrust tracked keys */
226	struct config_strlist* auto_trust_anchor_file_list;
227	/** files with trusted DNSKEYs in named.conf format, list */
228	struct config_strlist* trusted_keys_file_list;
229	/** DLV anchor file */
230	char* dlv_anchor_file;
231	/** DLV anchor inline */
232	struct config_strlist* dlv_anchor_list;
233	/** insecure domain list */
234	struct config_strlist* domain_insecure;
235
236	/** if not 0, this value is the validation date for RRSIGs */
237	int32_t val_date_override;
238	/** the minimum for signature clock skew */
239	int32_t val_sig_skew_min;
240	/** the maximum for signature clock skew */
241	int32_t val_sig_skew_max;
242	/** this value sets the number of seconds before revalidating bogus */
243	int bogus_ttl;
244	/** should validator clean additional section for secure msgs */
245	int val_clean_additional;
246	/** log bogus messages by the validator */
247	int val_log_level;
248	/** squelch val_log_level to log - this is library goes to callback */
249	int val_log_squelch;
250	/** should validator allow bogus messages to go through */
251	int val_permissive_mode;
252	/** ignore the CD flag in incoming queries and refuse them bogus data */
253	int ignore_cd;
254	/** nsec3 maximum iterations per key size, string */
255	char* val_nsec3_key_iterations;
256	/** autotrust add holddown time, in seconds */
257	unsigned int add_holddown;
258	/** autotrust del holddown time, in seconds */
259	unsigned int del_holddown;
260	/** autotrust keep_missing time, in seconds. 0 is forever. */
261	unsigned int keep_missing;
262
263	/** size of the key cache */
264	size_t key_cache_size;
265	/** slabs in the key cache. */
266	size_t key_cache_slabs;
267	/** size of the neg cache */
268	size_t neg_cache_size;
269
270	/** local zones config */
271	struct config_str2list* local_zones;
272	/** local zones nodefault list */
273	struct config_strlist* local_zones_nodefault;
274	/** local data RRs configged */
275	struct config_strlist* local_data;
276	/** unblock lan zones (reverse lookups for 10/8 and so on) */
277	int unblock_lan_zones;
278
279	/** remote control section. enable toggle. */
280	int remote_control_enable;
281	/** the interfaces the remote control should listen on */
282	struct config_strlist* control_ifs;
283	/** port number for the control port */
284	int control_port;
285	/** private key file for server */
286	char* server_key_file;
287	/** certificate file for server */
288	char* server_cert_file;
289	/** private key file for unbound-control */
290	char* control_key_file;
291	/** certificate file for unbound-control */
292	char* control_cert_file;
293
294	/** Python script file */
295	char* python_script;
296
297	/** daemonize, i.e. fork into the background. */
298	int do_daemonize;
299
300	/* minimal response when positive answer */
301	int minimal_responses;
302
303	/* RRSet roundrobin */
304	int rrset_roundrobin;
305
306	/* maximum UDP response size */
307	size_t max_udp_size;
308};
309
310/**
311 * Stub config options
312 */
313struct config_stub {
314	/** next in list */
315	struct config_stub* next;
316	/** domain name (in text) of the stub apex domain */
317	char* name;
318	/** list of stub nameserver hosts (domain name) */
319	struct config_strlist* hosts;
320	/** list of stub nameserver addresses (IP address) */
321	struct config_strlist* addrs;
322	/** if stub-prime is set */
323	int isprime;
324	/** if forward-first is set (failover to without if fails) */
325	int isfirst;
326};
327
328/**
329 * List of strings for config options
330 */
331struct config_strlist {
332	/** next item in list */
333	struct config_strlist* next;
334	/** config option string */
335	char* str;
336};
337
338/**
339 * List of two strings for config options
340 */
341struct config_str2list {
342	/** next item in list */
343	struct config_str2list* next;
344	/** first string */
345	char* str;
346	/** second string */
347	char* str2;
348};
349
350/** List head for strlist processing, used for append operation. */
351struct config_strlist_head {
352	/** first in list of text items */
353	struct config_strlist* first;
354	/** last in list of text items */
355	struct config_strlist* last;
356};
357
358/**
359 * Create config file structure. Filled with default values.
360 * @return: the new structure or NULL on memory error.
361 */
362struct config_file* config_create(void);
363
364/**
365 * Create config file structure for library use. Filled with default values.
366 * @return: the new structure or NULL on memory error.
367 */
368struct config_file* config_create_forlib(void);
369
370/**
371 * Read the config file from the specified filename.
372 * @param config: where options are stored into, must be freshly created.
373 * @param filename: name of configfile. If NULL nothing is done.
374 * @param chroot: if not NULL, the chroot dir currently in use (for include).
375 * @return: false on error. In that case errno is set, ENOENT means
376 * 	file not found.
377 */
378int config_read(struct config_file* config, const char* filename,
379	const char* chroot);
380
381/**
382 * Destroy the config file structure.
383 * @param config: to delete.
384 */
385void config_delete(struct config_file* config);
386
387/**
388 * Apply config to global constants; this routine is called in single thread.
389 * @param config: to apply. Side effect: global constants change.
390 */
391void config_apply(struct config_file* config);
392
393/**
394 * Set the given keyword to the given value.
395 * @param config: where to store config
396 * @param option: option name, including the ':' character.
397 * @param value: value, this string is copied if needed, or parsed.
398 * 	The caller owns the value string.
399 * @return 0 on error (malloc or syntax error).
400 */
401int config_set_option(struct config_file* config, const char* option,
402	const char* value);
403
404/**
405 * Call print routine for the given option.
406 * @param cfg: config.
407 * @param opt: option name without trailing :.
408 *	This is different from config_set_option.
409 * @param func: print func, called as (str, arg) for every data element.
410 * @param arg: user argument for print func.
411 * @return false if the option name is not supported (syntax error).
412 */
413int config_get_option(struct config_file* cfg, const char* opt,
414	void (*func)(char*,void*), void* arg);
415
416/**
417 * Get an option and return strlist
418 * @param cfg: config file
419 * @param opt: option name.
420 * @param list: list is returned here. malloced, caller must free it.
421 * @return 0=OK, 1=syntax error, 2=malloc failed.
422 */
423int config_get_option_list(struct config_file* cfg, const char* opt,
424	struct config_strlist** list);
425
426/**
427 * Get an option and collate results into string
428 * @param cfg: config file
429 * @param opt: option name.
430 * @param str: string. malloced, caller must free it.
431 * @return 0=OK, 1=syntax error, 2=malloc failed.
432 */
433int config_get_option_collate(struct config_file* cfg, const char* opt,
434	char** str);
435
436/**
437 * function to print to a file, use as func with config_get_option.
438 * @param line: text to print. \n appended.
439 * @param arg: pass a FILE*, like stdout.
440 */
441void config_print_func(char* line, void* arg);
442
443/**
444 * function to collate the text strings into a strlist_head.
445 * @param line: text to append.
446 * @param arg: pass a strlist_head structure. zeroed on start.
447 */
448void config_collate_func(char* line, void* arg);
449
450/**
451 * take a strlist_head list and return a malloc string. separated with newline.
452 * @param list: strlist first to collate. zeroes return "".
453 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
454 */
455char* config_collate_cat(struct config_strlist* list);
456
457/**
458 * Append text at end of list.
459 * @param list: list head. zeroed at start.
460 * @param item: new item. malloced by caller. if NULL the insertion fails.
461 * @return true on success.
462 */
463int cfg_strlist_append(struct config_strlist_head* list, char* item);
464
465/**
466 * Insert string into strlist.
467 * @param head: pointer to strlist head variable.
468 * @param item: new item. malloced by caller. If NULL the insertion fails.
469 * @return: true on success.
470 */
471int cfg_strlist_insert(struct config_strlist** head, char* item);
472
473/**
474 * Insert string into str2list.
475 * @param head: pointer to str2list head variable.
476 * @param item: new item. malloced by caller. If NULL the insertion fails.
477 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
478 * @return: true on success.
479 */
480int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
481
482/**
483 * Delete items in config string list.
484 * @param list: list.
485 */
486void config_delstrlist(struct config_strlist* list);
487
488/**
489 * Delete items in config double string list.
490 * @param list: list.
491 */
492void config_deldblstrlist(struct config_str2list* list);
493
494/**
495 * Delete items in config stub list.
496 * @param list: list.
497 */
498void config_delstubs(struct config_stub* list);
499
500/**
501 * Convert 14digit to time value
502 * @param str: string of 14 digits
503 * @return time value or 0 for error.
504 */
505time_t cfg_convert_timeval(const char* str);
506
507/**
508 * Count number of values in the string.
509 * format ::= (sp num)+ sp
510 * num ::= [-](0-9)+
511 * sp ::= (space|tab)*
512 *
513 * @param str: string
514 * @return: 0 on parse error, or empty string, else
515 *	number of integer values in the string.
516 */
517int cfg_count_numbers(const char* str);
518
519/**
520 * Convert a 'nice' memory or file size into a bytecount
521 * From '100k' to 102400. and so on. Understands kKmMgG.
522 * k=1024, m=1024*1024, g=1024*1024*1024.
523 * @param str: string
524 * @param res: result is stored here, size in bytes.
525 * @return: true if parsed correctly, or 0 on a parse error (and an error
526 * is logged).
527 */
528int cfg_parse_memsize(const char* str, size_t* res);
529
530/**
531 * Parse local-zone directive into two strings and register it in the config.
532 * @param cfg: to put it in.
533 * @param val: argument strings to local-zone, "example.com nodefault".
534 * @return: false on failure
535 */
536int cfg_parse_local_zone(struct config_file* cfg, const char* val);
537
538/**
539 * Mark "number" or "low-high" as available or not in ports array.
540 * @param str: string in input
541 * @param allow: give true if this range is permitted.
542 * @param avail: the array from cfg.
543 * @param num: size of the array (65536).
544 * @return: true if parsed correctly, or 0 on a parse error (and an error
545 * is logged).
546 */
547int cfg_mark_ports(const char* str, int allow, int* avail, int num);
548
549/**
550 * Get a condensed list of ports returned. allocated.
551 * @param cfg: config file.
552 * @param avail: the available ports array is returned here.
553 * @return: number of ports in array or 0 on error.
554 */
555int cfg_condense_ports(struct config_file* cfg, int** avail);
556
557/**
558 * Scan ports available
559 * @param avail: the array from cfg.
560 * @param num: size of the array (65536).
561 * @return the number of ports available for use.
562 */
563int cfg_scan_ports(int* avail, int num);
564
565/**
566 * Convert a filename to full pathname in original filesys
567 * @param fname: the path name to convert.
568 *      Must not be null or empty.
569 * @param cfg: config struct for chroot and chdir (if set).
570 * @param use_chdir: if false, only chroot is applied.
571 * @return pointer to malloced buffer which is: [chroot][chdir]fname
572 *      or NULL on malloc failure.
573 */
574char* fname_after_chroot(const char* fname, struct config_file* cfg,
575	int use_chdir);
576
577/**
578 * Convert a ptr shorthand into a full reverse-notation PTR record.
579 * @param str: input string, "IP name"
580 * @return: malloced string "reversed-ip-name PTR name"
581 */
582char* cfg_ptr_reverse(char* str);
583
584/**
585 * Append text to the error info for validation.
586 * @param qstate: query state.
587 * @param str: copied into query region and appended.
588 * Failures to allocate are logged.
589 */
590void errinf(struct module_qstate* qstate, const char* str);
591
592/**
593 * Append text to error info:  from 1.2.3.4
594 * @param qstate: query state.
595 * @param origin: sock list with origin of trouble.
596 *	Every element added.
597 *	If NULL: nothing is added.
598 *	if 0len element: 'from cache' is added.
599 */
600void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
601
602/**
603 * Append text to error info:  for RRset name type class
604 * @param qstate: query state.
605 * @param rr: rrset_key.
606 */
607void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
608
609/**
610 * Append text to error info:  str dname
611 * @param qstate: query state.
612 * @param str: explanation string
613 * @param dname: the dname.
614 */
615void errinf_dname(struct module_qstate* qstate, const char* str,
616	uint8_t* dname);
617
618/**
619 * Create error info in string
620 * @param qstate: query state.
621 * @return string or NULL on malloc failure (already logged).
622 *    This string is malloced and has to be freed by caller.
623 */
624char* errinf_to_str(struct module_qstate* qstate);
625
626/**
627 * Used during options parsing
628 */
629struct config_parser_state {
630	/** name of file being parser */
631	char* filename;
632	/** line number in the file, starts at 1 */
633	int line;
634	/** number of errors encountered */
635	int errors;
636	/** the result of parsing is stored here. */
637	struct config_file* cfg;
638	/** the current chroot dir (or NULL if none) */
639	const char* chroot;
640};
641
642/** global config parser object used during config parsing */
643extern struct config_parser_state* cfg_parser;
644/** init lex state */
645void init_cfg_parse(void);
646/** lex in file */
647extern FILE* ub_c_in;
648/** lex out file */
649extern FILE* ub_c_out;
650/** the yacc lex generated parse function */
651int ub_c_parse(void);
652/** the lexer function */
653int ub_c_lex(void);
654/** wrap function */
655int ub_c_wrap(void);
656/** parsing helpers: print error with file and line numbers */
657void ub_c_error(const char* msg);
658/** parsing helpers: print error with file and line numbers */
659void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
660
661#ifdef UB_ON_WINDOWS
662/**
663 * Obtain registry string (if it exists).
664 * @param key: key string
665 * @param name: name of value to fetch.
666 * @return malloced string with the result or NULL if it did not
667 * 	exist on an error (logged with log_err) was encountered.
668 */
669char* w_lookup_reg_str(const char* key, const char* name);
670#endif /* UB_ON_WINDOWS */
671
672#endif /* UTIL_CONFIG_FILE_H */
673