1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 #ifndef NAMED_SERVER_H
13 #define NAMED_SERVER_H 1
14 
15 /*! \file */
16 
17 #include <inttypes.h>
18 #include <stdbool.h>
19 
20 #include <isc/log.h>
21 #include <isc/magic.h>
22 #include <isc/quota.h>
23 #include <isc/sockaddr.h>
24 #include <isc/types.h>
25 
26 #include <dns/acl.h>
27 #include <dns/dnstap.h>
28 #include <dns/stats.h>
29 #include <dns/types.h>
30 
31 #include <ns/interfacemgr.h>
32 #include <ns/server.h>
33 #include <ns/stats.h>
34 #include <ns/types.h>
35 
36 #include <named/types.h>
37 
38 #define NAMED_EVENTCLASS    ISC_EVENTCLASS(0x4E43)
39 #define NAMED_EVENT_RELOAD  (NAMED_EVENTCLASS + 0)
40 #define NAMED_EVENT_DELZONE (NAMED_EVENTCLASS + 1)
41 
42 /*%
43  * Name server state.  Better here than in lots of separate global variables.
44  */
45 struct named_server {
46 	unsigned int magic;
47 	isc_mem_t *  mctx;
48 
49 	ns_server_t *sctx;
50 
51 	isc_task_t *task;
52 
53 	char *statsfile;    /*%< Statistics file name */
54 	char *dumpfile;	    /*%< Dump file name */
55 	char *secrootsfile; /*%< Secroots file name */
56 	char *bindkeysfile; /*%< bind.keys file name
57 			     * */
58 	char *recfile;	    /*%< Recursive file name */
59 	bool  version_set;  /*%< User has set version
60 			     * */
61 	char *version;	    /*%< User-specified version */
62 	bool  hostname_set; /*%< User has set hostname
63 			     * */
64 	char *hostname;	    /*%< User-specified hostname
65 			     * */
66 
67 	/* Server data structures. */
68 	dns_loadmgr_t *	   loadmgr;
69 	dns_zonemgr_t *	   zonemgr;
70 	dns_viewlist_t	   viewlist;
71 	dns_kasplist_t	   kasplist;
72 	ns_interfacemgr_t *interfacemgr;
73 	dns_db_t *	   in_roothints;
74 
75 	isc_timer_t *interface_timer;
76 	isc_timer_t *heartbeat_timer;
77 	isc_timer_t *pps_timer;
78 	isc_timer_t *tat_timer;
79 
80 	uint32_t interface_interval;
81 	uint32_t heartbeat_interval;
82 
83 	isc_mutex_t    reload_event_lock;
84 	isc_event_t *  reload_event;
85 	named_reload_t reload_status;
86 
87 	bool flushonshutdown;
88 
89 	named_cachelist_t cachelist; /*%< Possibly shared caches
90 				      * */
91 	isc_stats_t *zonestats;	     /*% Zone management stats */
92 	isc_stats_t *resolverstats;  /*% Resolver stats */
93 	isc_stats_t *sockstats;	     /*%< Socket stats */
94 
95 	named_controls_t *   controls; /*%< Control channels */
96 	unsigned int	     dispatchgen;
97 	named_dispatchlist_t dispatches;
98 
99 	named_statschannellist_t statschannels;
100 
101 	dns_tsigkey_t *sessionkey;
102 	char *	       session_keyfile;
103 	dns_name_t *   session_keyname;
104 	unsigned int   session_keyalg;
105 	uint16_t       session_keybits;
106 	bool	       interface_auto;
107 	unsigned char  secret[32]; /*%< Server Cookie Secret */
108 	ns_cookiealg_t cookiealg;
109 
110 	dns_dtenv_t *dtenv; /*%< Dnstap environment */
111 
112 	char *lockfile;
113 };
114 
115 #define NAMED_SERVER_MAGIC    ISC_MAGIC('S', 'V', 'E', 'R')
116 #define NAMED_SERVER_VALID(s) ISC_MAGIC_VALID(s, NAMED_SERVER_MAGIC)
117 
118 void
119 named_server_create(isc_mem_t *mctx, named_server_t **serverp);
120 /*%<
121  * Create a server object with default settings.
122  * This function either succeeds or causes the program to exit
123  * with a fatal error.
124  */
125 
126 void
127 named_server_destroy(named_server_t **serverp);
128 /*%<
129  * Destroy a server object, freeing its memory.
130  */
131 
132 void
133 named_server_reloadwanted(named_server_t *server);
134 /*%<
135  * Inform a server that a reload is wanted.  This function
136  * may be called asynchronously, from outside the server's task.
137  * If a reload is already scheduled or in progress, the call
138  * is ignored.
139  */
140 
141 void
142 named_server_scan_interfaces(named_server_t *server);
143 /*%<
144  * Trigger a interface scan.
145  * Must only be called when running under server->task.
146  */
147 
148 void
149 named_server_flushonshutdown(named_server_t *server, bool flush);
150 /*%<
151  * Inform the server that the zones should be flushed to disk on shutdown.
152  */
153 
154 isc_result_t
155 named_server_reloadcommand(named_server_t *server, isc_lex_t *lex,
156 			   isc_buffer_t **text);
157 /*%<
158  * Act on a "reload" command from the command channel.
159  */
160 
161 isc_result_t
162 named_server_reconfigcommand(named_server_t *server);
163 /*%<
164  * Act on a "reconfig" command from the command channel.
165  */
166 
167 isc_result_t
168 named_server_notifycommand(named_server_t *server, isc_lex_t *lex,
169 			   isc_buffer_t **text);
170 /*%<
171  * Act on a "notify" command from the command channel.
172  */
173 
174 isc_result_t
175 named_server_refreshcommand(named_server_t *server, isc_lex_t *lex,
176 			    isc_buffer_t **text);
177 /*%<
178  * Act on a "refresh" command from the command channel.
179  */
180 
181 isc_result_t
182 named_server_retransfercommand(named_server_t *server, isc_lex_t *lex,
183 			       isc_buffer_t **text);
184 /*%<
185  * Act on a "retransfer" command from the command channel.
186  */
187 
188 isc_result_t
189 named_server_togglequerylog(named_server_t *server, isc_lex_t *lex);
190 /*%<
191  * Enable/disable logging of queries.  (Takes "yes" or "no" argument,
192  * but can also be used as a toggle for backward comptibility.)
193  */
194 
195 /*%
196  * Save the current NTAs for all views to files.
197  */
198 isc_result_t
199 named_server_saventa(named_server_t *server);
200 
201 /*%
202  * Load NTAs for all views from files.
203  */
204 isc_result_t
205 named_server_loadnta(named_server_t *server);
206 
207 /*%
208  * Dump the current statistics to the statistics file.
209  */
210 isc_result_t
211 named_server_dumpstats(named_server_t *server);
212 
213 /*%
214  * Dump the current cache to the dump file.
215  */
216 isc_result_t
217 named_server_dumpdb(named_server_t *server, isc_lex_t *lex,
218 		    isc_buffer_t **text);
219 
220 /*%
221  * Dump the current security roots to the secroots file.
222  */
223 isc_result_t
224 named_server_dumpsecroots(named_server_t *server, isc_lex_t *lex,
225 			  isc_buffer_t **text);
226 
227 /*%
228  * Change or increment the server debug level.
229  */
230 isc_result_t
231 named_server_setdebuglevel(named_server_t *server, isc_lex_t *lex);
232 
233 /*%
234  * Flush the server's cache(s)
235  */
236 isc_result_t
237 named_server_flushcache(named_server_t *server, isc_lex_t *lex);
238 
239 /*%
240  * Flush a particular name from the server's cache.  If 'tree' is false,
241  * also flush the name from the ADB and badcache.  If 'tree' is true, also
242  * flush all the names under the specified name.
243  */
244 isc_result_t
245 named_server_flushnode(named_server_t *server, isc_lex_t *lex, bool tree);
246 
247 /*%
248  * Report the server's status.
249  */
250 isc_result_t
251 named_server_status(named_server_t *server, isc_buffer_t **text);
252 
253 /*%
254  * Report a list of dynamic and static tsig keys, per view.
255  */
256 isc_result_t
257 named_server_tsiglist(named_server_t *server, isc_buffer_t **text);
258 
259 /*%
260  * Delete a specific key (with optional view).
261  */
262 isc_result_t
263 named_server_tsigdelete(named_server_t *server, isc_lex_t *lex,
264 			isc_buffer_t **text);
265 
266 /*%
267  * Enable or disable updates for a zone.
268  */
269 isc_result_t
270 named_server_freeze(named_server_t *server, bool freeze, isc_lex_t *lex,
271 		    isc_buffer_t **text);
272 
273 /*%
274  * Dump zone updates to disk, optionally removing the journal file
275  */
276 isc_result_t
277 named_server_sync(named_server_t *server, isc_lex_t *lex, isc_buffer_t **text);
278 
279 /*%
280  * Update a zone's DNSKEY set from the key repository.  If
281  * the command that triggered the call to this function was "sign",
282  * then force a full signing of the zone.  If it was "loadkeys",
283  * then don't sign the zone; any needed changes to signatures can
284  * take place incrementally.
285  */
286 isc_result_t
287 named_server_rekey(named_server_t *server, isc_lex_t *lex, isc_buffer_t **text);
288 
289 /*%
290  * Dump the current recursive queries.
291  */
292 isc_result_t
293 named_server_dumprecursing(named_server_t *server);
294 
295 /*%
296  * Maintain a list of dispatches that require reserved ports.
297  */
298 void
299 named_add_reserved_dispatch(named_server_t *server, const isc_sockaddr_t *addr);
300 
301 /*%
302  * Enable or disable dnssec validation.
303  */
304 isc_result_t
305 named_server_validation(named_server_t *server, isc_lex_t *lex,
306 			isc_buffer_t **text);
307 
308 /*%
309  * Add a zone to a running process, or modify an existing zone
310  */
311 isc_result_t
312 named_server_changezone(named_server_t *server, char *command,
313 			isc_buffer_t **text);
314 
315 /*%
316  * Deletes a zone from a running process
317  */
318 isc_result_t
319 named_server_delzone(named_server_t *server, isc_lex_t *lex,
320 		     isc_buffer_t **text);
321 
322 /*%
323  * Show current configuration for a given zone
324  */
325 isc_result_t
326 named_server_showzone(named_server_t *server, isc_lex_t *lex,
327 		      isc_buffer_t **text);
328 
329 /*%
330  * Lists the status of the signing records for a given zone.
331  */
332 isc_result_t
333 named_server_signing(named_server_t *server, isc_lex_t *lex,
334 		     isc_buffer_t **text);
335 
336 /*%
337  * Lists the DNSSEC status for a given zone.
338  */
339 isc_result_t
340 named_server_dnssec(named_server_t *server, isc_lex_t *lex,
341 		    isc_buffer_t **text);
342 
343 /*%
344  * Lists status information for a given zone (e.g., name, type, files,
345  * load time, expiry, etc).
346  */
347 isc_result_t
348 named_server_zonestatus(named_server_t *server, isc_lex_t *lex,
349 			isc_buffer_t **text);
350 
351 /*%
352  * Adds/updates a Negative Trust Anchor (NTA) for a specified name and
353  * duration, in a particular view if specified, or in all views.
354  */
355 isc_result_t
356 named_server_nta(named_server_t *server, isc_lex_t *lex, bool readonly,
357 		 isc_buffer_t **text);
358 
359 /*%
360  * Generates a test sequence that is only for use in system tests. The
361  * argument is the size of required output in bytes.
362  */
363 isc_result_t
364 named_server_testgen(isc_lex_t *lex, isc_buffer_t **text);
365 
366 /*%
367  * Force fefresh or print status for managed keys zones.
368  */
369 isc_result_t
370 named_server_mkeys(named_server_t *server, isc_lex_t *lex, isc_buffer_t **text);
371 
372 /*%
373  * Close and reopen DNSTAP output file.
374  */
375 isc_result_t
376 named_server_dnstap(named_server_t *server, isc_lex_t *lex,
377 		    isc_buffer_t **text);
378 
379 /*%
380  * Display or update tcp-{initial,idle,keepalive,advertised}-timeout options.
381  */
382 isc_result_t
383 named_server_tcptimeouts(isc_lex_t *lex, isc_buffer_t **text);
384 
385 /*%
386  * Control whether stale answers are served or not when configured in
387  * named.conf.
388  */
389 isc_result_t
390 named_server_servestale(named_server_t *server, isc_lex_t *lex,
391 			isc_buffer_t **text);
392 
393 #endif /* NAMED_SERVER_H */
394