xref: /freebsd/contrib/unbound/daemon/daemon.h (revision e27b1cae848219d07f0a12a48990af0558b4cced)
1 /*
2  * daemon/daemon.h - collection of workers that handles requests.
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  * The daemon consists of global settings and a number of workers.
40  */
41 
42 #ifndef DAEMON_H
43 #define DAEMON_H
44 
45 #include "util/locks.h"
46 #include "util/alloc.h"
47 #include "services/modstack.h"
48 struct config_file;
49 struct worker;
50 struct listen_port;
51 struct slabhash;
52 struct module_env;
53 struct rrset_cache;
54 struct acl_list;
55 struct local_zones;
56 struct views;
57 struct ub_randstate;
58 struct daemon_remote;
59 struct respip_set;
60 struct shm_main_info;
61 struct doq_table;
62 struct cookie_secrets;
63 struct fast_reload_thread;
64 struct fast_reload_printq;
65 struct shared_ports;
66 
67 #include "dnstap/dnstap_config.h"
68 #ifdef USE_DNSTAP
69 struct dt_env;
70 #endif
71 
72 #include "dnscrypt/dnscrypt_config.h"
73 #ifdef USE_DNSCRYPT
74 struct dnsc_env;
75 #endif
76 
77 /**
78  * Structure holding worker list.
79  * Holds globally visible information.
80  */
81 struct daemon {
82 	/** The config settings */
83 	struct config_file* cfg;
84 	/** the chroot dir in use, NULL if none */
85 	char* chroot;
86 	/** pidfile that is used */
87 	char* pidfile;
88 	/** port number that has ports opened. */
89 	int listening_port;
90 	/** array of listening ports, opened.  Listening ports per worker,
91 	 * or just one element[0] shared by the worker threads. */
92 	struct listen_port** ports;
93 	/** size of ports array */
94 	size_t num_ports;
95 	/** reuseport is enabled if true */
96 	int reuseport;
97 	/** port number for remote that has ports opened. */
98 	int rc_port;
99 	/** listening ports for remote control */
100 	struct listen_port* rc_ports;
101 	/** the shared ports structure, with random ports numbers. */
102 	struct shared_ports* shared_ports;
103 	/** remote control connections management (for first worker) */
104 	struct daemon_remote* rc;
105 	/** ssl context for listening to dnstcp over ssl */
106 	void* listen_dot_sslctx;
107 	/** ssl context for connecting to dnstcp over ssl */
108 	void* connect_dot_sslctx;
109 	/** ssl context for listening to DoH */
110 	void* listen_doh_sslctx;
111 	/** ssl context for listening to quic */
112 	void* listen_quic_sslctx;
113 	/** the file name that the ssl context is made with, private key. */
114 	char* ssl_service_key;
115 	/** the file name that the ssl context is made with, certificate. */
116 	char* ssl_service_pem;
117 	/** modification time for ssl_service_key, in sec and ns. Like
118 	 * in a struct timespec, but without that for portability. */
119 	time_t mtime_ssl_service_key;
120 	long mtime_ns_ssl_service_key;
121 	/** modification time for ssl_service_pem, in sec and ns. Like
122 	 * in a struct timespec, but without that for portability. */
123 	time_t mtime_ssl_service_pem;
124 	long mtime_ns_ssl_service_pem;
125 	/** num threads allocated */
126 	int num;
127 	/** num threads allocated in the previous config or 0 at first */
128 	int old_num;
129 	/** the worker entries */
130 	struct worker** workers;
131 	/** per-worker allocation cache */
132 	struct alloc_cache **worker_allocs;
133 	/** do we need to exit unbound (or is it only a reload?) */
134 	int need_to_exit;
135 	/** master random table ; used for port div between threads on reload*/
136 	struct ub_randstate* rand;
137 	/** master allocation cache */
138 	struct alloc_cache superalloc;
139 	/** the module environment master value, copied and changed by threads*/
140 	struct module_env* env;
141 	/** stack of module callbacks */
142 	struct module_stack mods;
143 	/** The module stack has been inited */
144 	int mods_inited;
145 	/** access control, which client IPs are allowed to connect */
146 	struct acl_list* acl;
147 	/** access control, which interfaces are allowed to connect */
148 	struct acl_list* acl_interface;
149 	/** TCP connection limit, limit connections from client IPs */
150 	struct tcl_list* tcl;
151 	/** local authority zones */
152 	struct local_zones* local_zones;
153 	/** last time of statistics printout */
154 	struct timeval time_last_stat;
155 	/** time when daemon started */
156 	struct timeval time_boot;
157 #ifdef USE_DNSTAP
158 	/** the dnstap environment master value, copied and changed by threads*/
159 	struct dt_env* dtenv;
160 #endif
161 	/** The SHM info for shared memory stats. */
162 	struct shm_main_info* shm_info;
163 	/** if the timeout for statistics is attempted at specific offset.
164 	 * If it is true, the stat timeout is the interval+offset, and that
165 	 * picks (roughly) the same time offset every time period. */
166 	int stat_time_specific;
167 	/** if the timeout is specific, what offset in the period. */
168 	int stat_time_offset;
169 	/** some response-ip tags or actions are configured if true */
170 	int use_response_ip;
171 	/** some RPZ policies are configured */
172 	int use_rpz;
173 #ifdef USE_DNSCRYPT
174 	/** the dnscrypt environment */
175 	struct dnsc_env* dnscenv;
176 #endif
177 	/** the doq connection table */
178 	struct doq_table* doq_table;
179 	/** reuse existing cache on reload if other conditions allow it. */
180 	int reuse_cache;
181 	/** the EDNS cookie secrets from the cookie-secret-file */
182 	struct cookie_secrets* cookie_secrets;
183 	/** the fast reload thread, or NULL */
184 	struct fast_reload_thread* fast_reload_thread;
185 	/** the fast reload printq list */
186 	struct fast_reload_printq* fast_reload_printq_list;
187 	/** the fast reload option to drop mesh queries, true if so. */
188 	int fast_reload_drop_mesh;
189 	/** for fast reload, if the tcl, tcp connection limits, has
190 	 * changes for workers */
191 	int fast_reload_tcl_has_changes;
192 	/** config file name */
193 	char* cfgfile;
194 };
195 
196 /**
197  * Initialize daemon structure.
198  * @return: The daemon structure, or NULL on error.
199  */
200 struct daemon* daemon_init(void);
201 
202 /**
203  * Open shared listening ports (if needed).
204  * The cfg member pointer must have been set for the daemon.
205  * @param daemon: the daemon.
206  * @return: false on error.
207  */
208 int daemon_open_shared_ports(struct daemon* daemon);
209 
210 /**
211  * Do daemon setup that needs privileges
212  * like opening privileged ports or opening device files.
213  * The cfg member pointer must have been set for the daemon.
214  * @param daemon: the daemon.
215  * @return: false on error.
216  */
217 int daemon_privileged(struct daemon* daemon);
218 
219 /**
220  * Fork workers and start service.
221  * When the routine exits, it is no longer forked.
222  * @param daemon: the daemon.
223  */
224 void daemon_fork(struct daemon* daemon);
225 
226 /**
227  * Close off the worker thread information.
228  * Bring the daemon back into state ready for daemon_fork again.
229  * @param daemon: the daemon.
230  */
231 void daemon_cleanup(struct daemon* daemon);
232 
233 /**
234  * Delete workers, close listening ports.
235  * @param daemon: the daemon.
236  */
237 void daemon_delete(struct daemon* daemon);
238 
239 /**
240  * Apply config settings.
241  * @param daemon: the daemon.
242  * @param cfg: new config settings.
243  */
244 void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg);
245 
246 /**
247  * Setup acl list to have entries for the port list.
248  * @param list: the acl interface
249  * @param port_list: list of open ports, or none.
250  * @return false on failure
251  */
252 int setup_acl_for_ports(struct acl_list* list, struct listen_port* port_list);
253 
254 /* setups the needed ssl contexts, fatal_exit() on any failure */
255 void daemon_setup_sslctxs(struct daemon* daemon, struct config_file* cfg);
256 
257 /** See if the SSL cert files have changed */
258 int ssl_cert_changed(struct daemon* daemon, struct config_file* cfg);
259 
260 /** Setup the listening DoT SSL_CTX, returns the ssl ctx. */
261 void* daemon_setup_listen_dot_sslctx(struct daemon* daemon,
262 	struct config_file* cfg);
263 
264 /** Setup the listening DoH SSL_CTX, returns the ssl ctx. */
265 void* daemon_setup_listen_doh_sslctx(struct daemon* daemon,
266 	struct config_file* cfg);
267 
268 /** Setup the listening Quic SSL_CTX, returns the ssl ctx */
269 void* daemon_setup_listen_quic_sslctx(struct daemon* daemon,
270 	struct config_file* cfg);
271 
272 /** Setup the connect DoT SSL_CTX, returns the ssl ctx */
273 void* daemon_setup_connect_dot_sslctx(struct daemon* daemon,
274 	struct config_file* cfg);
275 
276 #endif /* DAEMON_H */
277