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