]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/util/config_file.h
Upgrade Unbound to 1.6.5. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / util / config_file.h
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
44 struct config_stub;
45 struct config_view;
46 struct config_strlist;
47 struct config_str2list;
48 struct config_str3list;
49 struct config_strbytelist;
50 struct module_qstate;
51 struct sock_list;
52 struct ub_packed_rrset_key;
53 struct regional;
54
55 /**
56  * The configuration options.
57  * Strings are malloced.
58  */
59 struct config_file {
60         /** verbosity level as specified in the config file */
61         int verbosity;
62
63         /** statistics interval (in seconds) */
64         int stat_interval;
65         /** if false, statistics values are reset after printing them */
66         int stat_cumulative;
67         /** if true, the statistics are kept in greater detail */
68         int stat_extended;
69
70         /** number of threads to create */
71         int num_threads;
72
73         /** port on which queries are answered. */
74         int port;
75         /** do ip4 query support. */
76         int do_ip4;
77         /** do ip6 query support. */
78         int do_ip6;
79         /** prefer ip6 upstream queries. */
80         int prefer_ip6;
81         /** do udp query support. */
82         int do_udp;
83         /** do tcp query support. */
84         int do_tcp;
85         /** tcp upstream queries (no UDP upstream queries) */
86         int tcp_upstream;
87         /** maximum segment size of tcp socket which queries are answered */
88         int tcp_mss;
89         /** maximum segment size of tcp socket for outgoing queries */
90         int outgoing_tcp_mss;
91
92         /** private key file for dnstcp-ssl service (enabled if not NULL) */
93         char* ssl_service_key;
94         /** public key file for dnstcp-ssl service */
95         char* ssl_service_pem;
96         /** port on which to provide ssl service */
97         int ssl_port;
98         /** if outgoing tcp connections use SSL */
99         int ssl_upstream;
100
101         /** outgoing port range number of ports (per thread) */
102         int outgoing_num_ports;
103         /** number of outgoing tcp buffers per (per thread) */
104         size_t outgoing_num_tcp;
105         /** number of incoming tcp buffers per (per thread) */
106         size_t incoming_num_tcp;
107         /** allowed udp port numbers, array with 0 if not allowed */
108         int* outgoing_avail_ports;
109
110         /** EDNS buffer size to use */
111         size_t edns_buffer_size;
112         /** number of bytes buffer size for DNS messages */
113         size_t msg_buffer_size;
114         /** size of the message cache */
115         size_t msg_cache_size;
116         /** slabs in the message cache. */
117         size_t msg_cache_slabs;
118         /** number of queries every thread can service */
119         size_t num_queries_per_thread;
120         /** number of msec to wait before items can be jostled out */
121         size_t jostle_time;
122         /** size of the rrset cache */
123         size_t rrset_cache_size;
124         /** slabs in the rrset cache */
125         size_t rrset_cache_slabs;
126         /** host cache ttl in seconds */
127         int host_ttl;
128         /** number of slabs in the infra host cache */
129         size_t infra_cache_slabs;
130         /** max number of hosts in the infra cache */
131         size_t infra_cache_numhosts;
132         /** min value for infra cache rtt */
133         int infra_cache_min_rtt;
134         /** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
135         int delay_close;
136
137         /** the target fetch policy for the iterator */
138         char* target_fetch_policy;
139
140         /** automatic interface for incoming messages. Uses ipv6 remapping,
141          * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
142         int if_automatic;
143         /** SO_RCVBUF size to set on port 53 UDP socket */
144         size_t so_rcvbuf;
145         /** SO_SNDBUF size to set on port 53 UDP socket */
146         size_t so_sndbuf;
147         /** SO_REUSEPORT requested on port 53 sockets */
148         int so_reuseport;
149         /** IP_TRANSPARENT socket option requested on port 53 sockets */
150         int ip_transparent;
151         /** IP_FREEBIND socket option request on port 53 sockets */
152         int ip_freebind;
153
154         /** number of interfaces to open. If 0 default all interfaces. */
155         int num_ifs;
156         /** interface description strings (IP addresses) */
157         char **ifs;
158
159         /** number of outgoing interfaces to open. 
160          * If 0 default all interfaces. */
161         int num_out_ifs;
162         /** outgoing interface description strings (IP addresses) */
163         char **out_ifs;
164
165         /** the root hints */
166         struct config_strlist* root_hints;
167         /** the stub definitions, linked list */
168         struct config_stub* stubs;
169         /** the forward zone definitions, linked list */
170         struct config_stub* forwards;
171         /** the views definitions, linked list */
172         struct config_view* views;
173         /** list of donotquery addresses, linked list */
174         struct config_strlist* donotqueryaddrs;
175 #ifdef CLIENT_SUBNET
176         /** list of servers we send edns-client-subnet option to and 
177          * accept option from, linked list */
178         struct config_strlist* client_subnet;
179         /** list of zones we send edns-client-subnet option for */
180         struct config_strlist* client_subnet_zone;
181         /** opcode assigned by IANA for edns0-client-subnet option */
182         uint16_t client_subnet_opcode;
183         /** Do not check whitelist if incoming query contains an ECS record */
184         int client_subnet_always_forward;
185         /** Subnet length we are willing to give up privacy for */
186         uint8_t max_client_subnet_ipv4;
187         uint8_t max_client_subnet_ipv6;
188 #endif
189         /** list of access control entries, linked list */
190         struct config_str2list* acls;
191         /** use default localhost donotqueryaddr entries */
192         int donotquery_localhost;
193
194         /** harden against very small edns buffer sizes */
195         int harden_short_bufsize;
196         /** harden against very large query sizes */
197         int harden_large_queries;
198         /** harden against spoofed glue (out of zone data) */
199         int harden_glue;
200         /** harden against receiving no DNSSEC data for trust anchor */
201         int harden_dnssec_stripped;
202         /** harden against queries that fall under known nxdomain names */
203         int harden_below_nxdomain;
204         /** harden the referral path, query for NS,A,AAAA and validate */
205         int harden_referral_path;
206         /** harden against algorithm downgrade */
207         int harden_algo_downgrade;
208         /** use 0x20 bits in query as random ID bits */
209         int use_caps_bits_for_id;
210         /** 0x20 whitelist, domains that do not use capsforid */
211         struct config_strlist* caps_whitelist;
212         /** strip away these private addrs from answers, no DNS Rebinding */
213         struct config_strlist* private_address;
214         /** allow domain (and subdomains) to use private address space */
215         struct config_strlist* private_domain;
216         /** what threshold for unwanted action. */
217         size_t unwanted_threshold;
218         /** the number of seconds maximal TTL used for RRsets and messages */
219         int max_ttl;
220         /** the number of seconds minimum TTL used for RRsets and messages */
221         int min_ttl;
222         /** the number of seconds maximal negative TTL for SOA in auth */
223         int max_negative_ttl;
224         /** if prefetching of messages should be performed. */
225         int prefetch;
226         /** if prefetching of DNSKEYs should be performed. */
227         int prefetch_key;
228
229         /** chrootdir, if not "" or chroot will be done */
230         char* chrootdir;
231         /** username to change to, if not "". */
232         char* username;
233         /** working directory */
234         char* directory;
235         /** filename to log to. */
236         char* logfile;
237         /** pidfile to write pid to. */
238         char* pidfile;
239
240         /** should log messages be sent to syslogd */
241         int use_syslog;
242         /** log timestamp in ascii UTC */
243         int log_time_ascii;
244         /** log queries with one line per query */
245         int log_queries;
246         /** log replies with one line per reply */
247         int log_replies;
248         /** log identity to report */
249         char* log_identity;
250
251         /** do not report identity (id.server, hostname.bind) */
252         int hide_identity;
253         /** do not report version (version.server, version.bind) */
254         int hide_version;
255         /** do not report trustanchor (trustanchor.unbound) */
256         int hide_trustanchor;
257         /** identity, hostname is returned if "". */
258         char* identity;
259         /** version, package version returned if "". */
260         char* version;
261
262         /** the module configuration string */
263         char* module_conf;
264         
265         /** files with trusted DS and DNSKEYs in zonefile format, list */
266         struct config_strlist* trust_anchor_file_list;
267         /** list of trustanchor keys, linked list */
268         struct config_strlist* trust_anchor_list;
269         /** files with 5011 autotrust tracked keys */
270         struct config_strlist* auto_trust_anchor_file_list;
271         /** files with trusted DNSKEYs in named.conf format, list */
272         struct config_strlist* trusted_keys_file_list;
273         /** DLV anchor file */
274         char* dlv_anchor_file;
275         /** DLV anchor inline */
276         struct config_strlist* dlv_anchor_list;
277         /** insecure domain list */
278         struct config_strlist* domain_insecure;
279         /** send key tag query */
280         int trust_anchor_signaling;
281
282         /** if not 0, this value is the validation date for RRSIGs */
283         int32_t val_date_override;
284         /** the minimum for signature clock skew */
285         int32_t val_sig_skew_min;
286         /** the maximum for signature clock skew */
287         int32_t val_sig_skew_max;
288         /** this value sets the number of seconds before revalidating bogus */
289         int bogus_ttl; 
290         /** should validator clean additional section for secure msgs */
291         int val_clean_additional;
292         /** log bogus messages by the validator */
293         int val_log_level;
294         /** squelch val_log_level to log - this is library goes to callback */
295         int val_log_squelch;
296         /** should validator allow bogus messages to go through */
297         int val_permissive_mode;
298         /** ignore the CD flag in incoming queries and refuse them bogus data */
299         int ignore_cd;
300         /** serve expired entries and prefetch them */
301         int serve_expired;
302         /** nsec3 maximum iterations per key size, string */
303         char* val_nsec3_key_iterations;
304         /** autotrust add holddown time, in seconds */
305         unsigned int add_holddown;
306         /** autotrust del holddown time, in seconds */
307         unsigned int del_holddown;
308         /** autotrust keep_missing time, in seconds. 0 is forever. */
309         unsigned int keep_missing;
310         /** permit small holddown values, allowing 5011 rollover very fast */
311         int permit_small_holddown;
312
313         /** size of the key cache */
314         size_t key_cache_size;
315         /** slabs in the key cache. */
316         size_t key_cache_slabs;
317         /** size of the neg cache */
318         size_t neg_cache_size;
319
320         /** local zones config */
321         struct config_str2list* local_zones;
322         /** local zones nodefault list */
323         struct config_strlist* local_zones_nodefault;
324         /** do not add any default local zone */
325         int local_zones_disable_default;
326         /** local data RRs configured */
327         struct config_strlist* local_data;
328         /** local zone override types per netblock */
329         struct config_str3list* local_zone_overrides;
330         /** unblock lan zones (reverse lookups for AS112 zones) */
331         int unblock_lan_zones;
332         /** insecure lan zones (don't validate AS112 zones) */
333         int insecure_lan_zones;
334         /** list of zonename, tagbitlist */
335         struct config_strbytelist* local_zone_tags;
336         /** list of aclname, tagbitlist */
337         struct config_strbytelist* acl_tags;
338         /** list of aclname, tagname, localzonetype */
339         struct config_str3list* acl_tag_actions;
340         /** list of aclname, tagname, redirectdata */
341         struct config_str3list* acl_tag_datas;
342         /** list of aclname, view*/
343         struct config_str2list* acl_view;
344         /** list of IP-netblock, tagbitlist */
345         struct config_strbytelist* respip_tags;
346         /** list of response-driven access control entries, linked list */
347         struct config_str2list* respip_actions;
348         /** RRs configured for response-driven access controls */
349         struct config_str2list* respip_data;
350         /** tag list, array with tagname[i] is malloced string */
351         char** tagname;
352         /** number of items in the taglist */
353         int num_tags;
354
355         /** remote control section. enable toggle. */
356         int remote_control_enable;
357         /** the interfaces the remote control should listen on */
358         struct config_strlist* control_ifs;
359         /** port number for the control port */
360         int control_port;
361         /** use certificates for remote control */
362         int remote_control_use_cert;
363         /** private key file for server */
364         char* server_key_file;
365         /** certificate file for server */
366         char* server_cert_file;
367         /** private key file for unbound-control */
368         char* control_key_file;
369         /** certificate file for unbound-control */
370         char* control_cert_file;
371
372         /** Python script file */
373         char* python_script;
374
375         /** Use systemd socket activation. */
376         int use_systemd;
377
378         /** daemonize, i.e. fork into the background. */
379         int do_daemonize;
380
381         /* minimal response when positive answer */
382         int minimal_responses;
383
384         /* RRSet roundrobin */
385         int rrset_roundrobin;
386
387         /* maximum UDP response size */
388         size_t max_udp_size;
389
390         /* DNS64 prefix */
391         char* dns64_prefix;
392
393         /* Synthetize all AAAA record despite the presence of an authoritative one */
394         int dns64_synthall;
395
396         /** true to enable dnstap support */
397         int dnstap;
398         /** dnstap socket path */
399         char* dnstap_socket_path;
400         /** true to send "identity" via dnstap */
401         int dnstap_send_identity;
402         /** true to send "version" via dnstap */
403         int dnstap_send_version;
404         /** dnstap "identity", hostname is used if "". */
405         char* dnstap_identity;
406         /** dnstap "version", package version is used if "". */
407         char* dnstap_version;
408
409         /** true to log dnstap RESOLVER_QUERY message events */
410         int dnstap_log_resolver_query_messages;
411         /** true to log dnstap RESOLVER_RESPONSE message events */
412         int dnstap_log_resolver_response_messages;
413         /** true to log dnstap CLIENT_QUERY message events */
414         int dnstap_log_client_query_messages;
415         /** true to log dnstap CLIENT_RESPONSE message events */
416         int dnstap_log_client_response_messages;
417         /** true to log dnstap FORWARDER_QUERY message events */
418         int dnstap_log_forwarder_query_messages;
419         /** true to log dnstap FORWARDER_RESPONSE message events */
420         int dnstap_log_forwarder_response_messages;
421
422         /** true to disable DNSSEC lameness check in iterator */
423         int disable_dnssec_lame_check;
424
425         /** ratelimit for ip addresses. 0 is off, otherwise qps (unless overridden) */
426         int ip_ratelimit;
427         /** number of slabs for ip_ratelimit cache */
428         size_t ip_ratelimit_slabs;
429         /** memory size in bytes for ip_ratelimit cache */
430         size_t ip_ratelimit_size;
431         /** ip_ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
432         int ip_ratelimit_factor;
433
434         /** ratelimit for domains. 0 is off, otherwise qps (unless overridden) */
435         int ratelimit;
436         /** number of slabs for ratelimit cache */
437         size_t ratelimit_slabs;
438         /** memory size in bytes for ratelimit cache */
439         size_t ratelimit_size;
440         /** ratelimits for domain (exact match) */
441         struct config_str2list* ratelimit_for_domain;
442         /** ratelimits below domain */
443         struct config_str2list* ratelimit_below_domain;
444         /** ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
445         int ratelimit_factor;
446         /** minimise outgoing QNAME and hide original QTYPE if possible */
447         int qname_minimisation;
448         /** minimise QNAME in strict mode, minimise according to RFC.
449          *  Do not apply fallback */
450         int qname_minimisation_strict;
451         /** SHM data - true if shm is enabled */
452         int shm_enable;
453         /** SHM data - key for the shm */
454         int shm_key;
455
456         /** DNSCrypt */
457         /** true to enable dnscrypt */
458         int dnscrypt;
459         /** port on which to provide dnscrypt service */
460         int dnscrypt_port;
461         /** provider name 2.dnscrypt-cert.example.com */
462         char* dnscrypt_provider;
463         /** dnscrypt secret keys 1.key */
464         struct config_strlist* dnscrypt_secret_key;
465         /** dnscrypt provider certs 1.cert */
466         struct config_strlist* dnscrypt_provider_cert;
467
468         /** IPsec module */
469 #ifdef USE_IPSECMOD
470         /** false to bypass the IPsec module */
471         int ipsecmod_enabled;
472         /** whitelisted domains for ipsecmod */
473         struct config_strlist* ipsecmod_whitelist;
474         /** path to external hook */
475         char* ipsecmod_hook;
476         /** true to proceed even with a bogus IPSECKEY */
477         int ipsecmod_ignore_bogus;
478         /** max TTL for the A/AAAA records that call the hook */
479         int ipsecmod_max_ttl;
480         /** false to proceed even when ipsecmod_hook fails */
481         int ipsecmod_strict;
482 #endif
483 };
484
485 /** from cfg username, after daemonise setup performed */
486 extern uid_t cfg_uid;
487 /** from cfg username, after daemonise setup performed */
488 extern gid_t cfg_gid;
489 /** debug and enable small timeouts */
490 extern int autr_permit_small_holddown;
491
492 /**
493  * Stub config options
494  */
495 struct config_stub {
496         /** next in list */
497         struct config_stub* next;
498         /** domain name (in text) of the stub apex domain */
499         char* name;
500         /** list of stub nameserver hosts (domain name) */
501         struct config_strlist* hosts;
502         /** list of stub nameserver addresses (IP address) */
503         struct config_strlist* addrs;
504         /** if stub-prime is set */
505         int isprime;
506         /** if forward-first is set (failover to without if fails) */
507         int isfirst;
508         /** use SSL for queries to this stub */
509         int ssl_upstream;
510 };
511
512 /**
513  * View config options
514  */
515 struct config_view {
516         /** next in list */
517         struct config_view* next;
518         /** view name */
519         char* name;
520         /** local zones */
521         struct config_str2list* local_zones;
522         /** local data RRs */
523         struct config_strlist* local_data;
524         /** local zones nodefault list */
525         struct config_strlist* local_zones_nodefault;
526         /** Fallback to global local_zones when there is no match in the view
527          * view specific tree. 1 for yes, 0 for no */   
528         int isfirst;
529         /** predefined actions for particular IP address responses */
530         struct config_str2list* respip_actions;
531         /** data complementing the 'redirect' response IP actions */
532         struct config_str2list* respip_data;
533 };
534
535 /**
536  * List of strings for config options
537  */
538 struct config_strlist {
539         /** next item in list */
540         struct config_strlist* next;
541         /** config option string */
542         char* str;
543 };
544
545 /**
546  * List of two strings for config options
547  */
548 struct config_str2list {
549         /** next item in list */
550         struct config_str2list* next;
551         /** first string */
552         char* str;
553         /** second string */
554         char* str2;
555 };
556
557 /**
558  * List of three strings for config options
559  */
560 struct config_str3list {
561         /** next item in list */
562         struct config_str3list* next;
563         /** first string */
564         char* str;
565         /** second string */
566         char* str2;
567         /** third string */
568         char* str3;
569 };
570
571
572 /**
573  * List of string, bytestring for config options
574  */
575 struct config_strbytelist {
576         /** next item in list */
577         struct config_strbytelist* next;
578         /** first string */
579         char* str;
580         /** second bytestring */
581         uint8_t* str2;
582         size_t str2len;
583 };
584
585 /** List head for strlist processing, used for append operation. */
586 struct config_strlist_head {
587         /** first in list of text items */
588         struct config_strlist* first;
589         /** last in list of text items */
590         struct config_strlist* last;
591 };
592
593 /**
594  * Create config file structure. Filled with default values.
595  * @return: the new structure or NULL on memory error.
596  */
597 struct config_file* config_create(void);
598
599 /**
600  * Create config file structure for library use. Filled with default values.
601  * @return: the new structure or NULL on memory error.
602  */
603 struct config_file* config_create_forlib(void);
604
605 /**
606  * Read the config file from the specified filename.
607  * @param config: where options are stored into, must be freshly created.
608  * @param filename: name of configfile. If NULL nothing is done.
609  * @param chroot: if not NULL, the chroot dir currently in use (for include).
610  * @return: false on error. In that case errno is set, ENOENT means 
611  *      file not found.
612  */
613 int config_read(struct config_file* config, const char* filename,
614         const char* chroot);
615
616 /**
617  * Destroy the config file structure.
618  * @param config: to delete.
619  */
620 void config_delete(struct config_file* config);
621
622 /**
623  * Apply config to global constants; this routine is called in single thread.
624  * @param config: to apply. Side effect: global constants change.
625  */
626 void config_apply(struct config_file* config);
627
628 /**
629  * Find username, sets cfg_uid and cfg_gid.
630  * @param config: the config structure.
631  */
632 void config_lookup_uid(struct config_file* config);
633
634 /**
635  * Set the given keyword to the given value.
636  * @param config: where to store config
637  * @param option: option name, including the ':' character.
638  * @param value: value, this string is copied if needed, or parsed.
639  *      The caller owns the value string.
640  * @return 0 on error (malloc or syntax error).
641  */
642 int config_set_option(struct config_file* config, const char* option,
643         const char* value);
644
645 /** 
646  * Call print routine for the given option.
647  * @param cfg: config.
648  * @param opt: option name without trailing :. 
649  *      This is different from config_set_option.
650  * @param func: print func, called as (str, arg) for every data element.
651  * @param arg: user argument for print func.
652  * @return false if the option name is not supported (syntax error).
653  */
654 int config_get_option(struct config_file* cfg, const char* opt, 
655         void (*func)(char*,void*), void* arg);
656
657 /**
658  * Get an option and return strlist
659  * @param cfg: config file
660  * @param opt: option name.
661  * @param list: list is returned here. malloced, caller must free it.
662  * @return 0=OK, 1=syntax error, 2=malloc failed.
663  */
664 int config_get_option_list(struct config_file* cfg, const char* opt,
665         struct config_strlist** list);
666
667 /**
668  * Get an option and collate results into string
669  * @param cfg: config file
670  * @param opt: option name.
671  * @param str: string. malloced, caller must free it.
672  * @return 0=OK, 1=syntax error, 2=malloc failed.
673  */
674 int config_get_option_collate(struct config_file* cfg, const char* opt, 
675         char** str);
676
677 /**
678  * function to print to a file, use as func with config_get_option.
679  * @param line: text to print. \n appended.
680  * @param arg: pass a FILE*, like stdout.
681  */
682 void config_print_func(char* line, void* arg);
683
684 /**
685  * function to collate the text strings into a strlist_head.
686  * @param line: text to append.
687  * @param arg: pass a strlist_head structure. zeroed on start.
688  */
689 void config_collate_func(char* line, void* arg);
690
691 /**
692  * take a strlist_head list and return a malloc string. separated with newline.
693  * @param list: strlist first to collate. zeroes return "".
694  * @return NULL on malloc failure. Or if malloc failure happened in strlist.
695  */
696 char* config_collate_cat(struct config_strlist* list);
697
698 /**
699  * Append text at end of list.
700  * @param list: list head. zeroed at start.
701  * @param item: new item. malloced by caller. if NULL the insertion fails.
702  * @return true on success.
703  */
704 int cfg_strlist_append(struct config_strlist_head* list, char* item);
705
706 /**
707  * Insert string into strlist.
708  * @param head: pointer to strlist head variable.
709  * @param item: new item. malloced by caller. If NULL the insertion fails.
710  * @return: true on success.
711  */
712 int cfg_strlist_insert(struct config_strlist** head, char* item);
713
714 /** insert with region for allocation. */
715 int cfg_region_strlist_insert(struct regional* region,
716         struct config_strlist** head, char* item);
717
718 /**
719  * Insert string into str2list.
720  * @param head: pointer to str2list head variable.
721  * @param item: new item. malloced by caller. If NULL the insertion fails.
722  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
723  * @return: true on success.
724  */
725 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
726
727 /**
728  * Insert string into str3list.
729  * @param head: pointer to str3list head variable.
730  * @param item: new item. malloced by caller. If NULL the insertion fails.
731  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
732  * @param i3: 3rd string, malloced by caller. If NULL the insertion fails.
733  * @return: true on success.
734  */
735 int cfg_str3list_insert(struct config_str3list** head, char* item, char* i2,
736         char* i3);
737
738 /**
739  * Insert string into strbytelist.
740  * @param head: pointer to strbytelist head variable.
741  * @param item: new item. malloced by caller. If NULL the insertion fails.
742  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
743  * @param i2len: length of the i2 bytestring.
744  * @return: true on success.
745  */
746 int cfg_strbytelist_insert(struct config_strbytelist** head, char* item,
747         uint8_t* i2, size_t i2len);
748
749 /**
750  * Find stub in config list, also returns prevptr (for deletion).
751  * @param pp: call routine with pointer to a pointer to the start of the list,
752  *      if the stub is found, on exit, the value contains a pointer to the
753  *      next pointer that points to the found element (or to the list start
754  *      pointer if it is the first element).
755  * @param nm: name of stub to find.
756  * @return: pointer to config_stub if found, or NULL if not found.
757  */
758 struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm);
759
760 /**
761  * Delete items in config string list.
762  * @param list: list.
763  */
764 void config_delstrlist(struct config_strlist* list);
765
766 /**
767  * Delete items in config double string list.
768  * @param list: list.
769  */
770 void config_deldblstrlist(struct config_str2list* list);
771
772 /**
773  * Delete items in config triple string list.
774  * @param list: list.
775  */
776 void config_deltrplstrlist(struct config_str3list* list);
777
778 /** delete stringbytelist */
779 void config_del_strbytelist(struct config_strbytelist* list);
780
781 /**
782  * Delete a stub item
783  * @param p: stub item
784  */
785 void config_delstub(struct config_stub* p);
786
787 /**
788  * Delete items in config stub list.
789  * @param list: list.
790  */
791 void config_delstubs(struct config_stub* list);
792
793 /**
794  * Delete a view item
795  * @param p: view item
796  */
797 void config_delview(struct config_view* p);
798
799 /**
800  * Delete items in config view list.
801  * @param list: list.
802  */
803 void config_delviews(struct config_view* list);
804
805 /**
806  * Convert 14digit to time value
807  * @param str: string of 14 digits
808  * @return time value or 0 for error.
809  */
810 time_t cfg_convert_timeval(const char* str);
811
812 /**
813  * Count number of values in the string.
814  * format ::= (sp num)+ sp
815  * num ::= [-](0-9)+
816  * sp ::= (space|tab)*
817  *
818  * @param str: string
819  * @return: 0 on parse error, or empty string, else
820  *      number of integer values in the string.
821  */
822 int cfg_count_numbers(const char* str);
823
824 /**
825  * Convert a 'nice' memory or file size into a bytecount
826  * From '100k' to 102400. and so on. Understands kKmMgG.
827  * k=1024, m=1024*1024, g=1024*1024*1024.
828  * @param str: string
829  * @param res: result is stored here, size in bytes.
830  * @return: true if parsed correctly, or 0 on a parse error (and an error 
831  * is logged).
832  */
833 int cfg_parse_memsize(const char* str, size_t* res);
834
835 /**
836  * Add a tag name to the config.  It is added at the end with a new ID value.
837  * @param cfg: the config structure.
838  * @param tag: string (which is copied) with the name.
839  * @return: false on alloc failure.
840  */
841 int config_add_tag(struct config_file* cfg, const char* tag);
842
843 /**
844  * Find tag ID in the tag list.
845  * @param cfg: the config structure.
846  * @param tag: string with tag name to search for.
847  * @return: 0..(num_tags-1) with tag ID, or -1 if tagname is not found.
848  */
849 int find_tag_id(struct config_file* cfg, const char* tag);
850
851 /**
852  * parse taglist from string into bytestring with bitlist.
853  * @param cfg: the config structure (with tagnames)
854  * @param str: the string to parse.  Parse puts 0 bytes in string. 
855  * @param listlen: returns length of in bytes.
856  * @return malloced bytes with a bitlist of the tags.  or NULL on parse error
857  * or malloc failure.
858  */
859 uint8_t* config_parse_taglist(struct config_file* cfg, char* str,
860         size_t* listlen);
861
862 /**
863  * convert tag bitlist to a malloced string with tag names.  For debug output.
864  * @param cfg: the config structure (with tagnames)
865  * @param taglist: the tag bitlist.
866  * @param len: length of the tag bitlist.
867  * @return malloced string or NULL.
868  */
869 char* config_taglist2str(struct config_file* cfg, uint8_t* taglist,
870         size_t len);
871
872 /**
873  * see if two taglists intersect (have tags in common).
874  * @param list1: first tag bitlist.
875  * @param list1len: length in bytes of first list.
876  * @param list2: second tag bitlist.
877  * @param list2len: length in bytes of second list.
878  * @return true if there are tags in common, 0 if not.
879  */
880 int taglist_intersect(uint8_t* list1, size_t list1len, uint8_t* list2,
881         size_t list2len);
882
883 /**
884  * Parse local-zone directive into two strings and register it in the config.
885  * @param cfg: to put it in.
886  * @param val: argument strings to local-zone, "example.com nodefault".
887  * @return: false on failure
888  */
889 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
890
891 /**
892  * Mark "number" or "low-high" as available or not in ports array.
893  * @param str: string in input
894  * @param allow: give true if this range is permitted.
895  * @param avail: the array from cfg.
896  * @param num: size of the array (65536).
897  * @return: true if parsed correctly, or 0 on a parse error (and an error 
898  * is logged).
899  */
900 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
901
902 /**
903  * Get a condensed list of ports returned. allocated.
904  * @param cfg: config file.
905  * @param avail: the available ports array is returned here.
906  * @return: number of ports in array or 0 on error.
907  */
908 int cfg_condense_ports(struct config_file* cfg, int** avail);
909
910 /**
911  * Scan ports available
912  * @param avail: the array from cfg.
913  * @param num: size of the array (65536).
914  * @return the number of ports available for use.
915  */
916 int cfg_scan_ports(int* avail, int num);
917
918 /** 
919  * Convert a filename to full pathname in original filesys
920  * @param fname: the path name to convert.
921  *      Must not be null or empty.
922  * @param cfg: config struct for chroot and chdir (if set).
923  * @param use_chdir: if false, only chroot is applied.
924  * @return pointer to malloced buffer which is: [chroot][chdir]fname
925  *      or NULL on malloc failure.
926  */
927 char* fname_after_chroot(const char* fname, struct config_file* cfg, 
928         int use_chdir);
929
930 /**
931  * Convert a ptr shorthand into a full reverse-notation PTR record.
932  * @param str: input string, "IP name"
933  * @return: malloced string "reversed-ip-name PTR name"
934  */
935 char* cfg_ptr_reverse(char* str);
936
937 /**
938  * Append text to the error info for validation.
939  * @param qstate: query state.
940  * @param str: copied into query region and appended.
941  * Failures to allocate are logged.
942  */
943 void errinf(struct module_qstate* qstate, const char* str);
944
945 /**
946  * Append text to error info:  from 1.2.3.4
947  * @param qstate: query state.
948  * @param origin: sock list with origin of trouble. 
949  *      Every element added.
950  *      If NULL: nothing is added.
951  *      if 0len element: 'from cache' is added.
952  */
953 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
954
955 /**
956  * Append text to error info:  for RRset name type class
957  * @param qstate: query state.
958  * @param rr: rrset_key.
959  */
960 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
961
962 /**
963  * Append text to error info:  str dname
964  * @param qstate: query state.
965  * @param str: explanation string
966  * @param dname: the dname.
967  */
968 void errinf_dname(struct module_qstate* qstate, const char* str, 
969         uint8_t* dname);
970
971 /**
972  * Create error info in string
973  * @param qstate: query state.
974  * @return string or NULL on malloc failure (already logged).
975  *    This string is malloced and has to be freed by caller.
976  */
977 char* errinf_to_str(struct module_qstate* qstate);
978
979 /**
980  * Used during options parsing
981  */
982 struct config_parser_state {
983         /** name of file being parser */
984         char* filename;
985         /** line number in the file, starts at 1 */
986         int line;
987         /** number of errors encountered */
988         int errors;
989         /** the result of parsing is stored here. */
990         struct config_file* cfg;
991         /** the current chroot dir (or NULL if none) */
992         const char* chroot;
993 };
994
995 /** global config parser object used during config parsing */
996 extern struct config_parser_state* cfg_parser;
997 /** init lex state */
998 void init_cfg_parse(void);
999 /** lex in file */
1000 extern FILE* ub_c_in;
1001 /** lex out file */
1002 extern FILE* ub_c_out;
1003 /** the yacc lex generated parse function */
1004 int ub_c_parse(void);
1005 /** the lexer function */
1006 int ub_c_lex(void);
1007 /** wrap function */
1008 int ub_c_wrap(void);
1009 /** parsing helpers: print error with file and line numbers */
1010 void ub_c_error(const char* msg);
1011 /** parsing helpers: print error with file and line numbers */
1012 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
1013
1014 #ifdef UB_ON_WINDOWS
1015 /**
1016  * Obtain registry string (if it exists).
1017  * @param key: key string
1018  * @param name: name of value to fetch.
1019  * @return malloced string with the result or NULL if it did not
1020  *      exist on an error (logged with log_err) was encountered.
1021  */
1022 char* w_lookup_reg_str(const char* key, const char* name);
1023
1024 /** Modify directory in options for module file name */
1025 void w_config_adjust_directory(struct config_file* cfg);
1026 #endif /* UB_ON_WINDOWS */
1027
1028 /** debug option for unit tests. */
1029 extern int fake_dsa, fake_sha1;
1030
1031 #endif /* UTIL_CONFIG_FILE_H */