No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / named / server.c
blob1a7f639bd0d26c13bbc6e33f9ae2f57dd6d331ae
1 /* $NetBSD: server.c,v 1.1.1.3 2009/12/26 22:19:21 christos Exp $ */
3 /*
4 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: server.c,v 1.556 2009/11/28 15:57:36 vjs Exp */
22 /*! \file */
24 #include <config.h>
26 #include <stdlib.h>
27 #include <unistd.h>
29 #include <isc/app.h>
30 #include <isc/base64.h>
31 #include <isc/dir.h>
32 #include <isc/entropy.h>
33 #include <isc/file.h>
34 #include <isc/hash.h>
35 #include <isc/httpd.h>
36 #include <isc/lex.h>
37 #include <isc/parseint.h>
38 #include <isc/portset.h>
39 #include <isc/print.h>
40 #include <isc/resource.h>
41 #include <isc/socket.h>
42 #include <isc/stat.h>
43 #include <isc/stats.h>
44 #include <isc/stdio.h>
45 #include <isc/string.h>
46 #include <isc/task.h>
47 #include <isc/timer.h>
48 #include <isc/util.h>
49 #include <isc/xml.h>
51 #include <isccfg/namedconf.h>
53 #include <bind9/check.h>
55 #include <dns/acache.h>
56 #include <dns/adb.h>
57 #include <dns/cache.h>
58 #include <dns/db.h>
59 #include <dns/dispatch.h>
60 #ifdef DLZ
61 #include <dns/dlz.h>
62 #endif
63 #include <dns/forward.h>
64 #include <dns/journal.h>
65 #include <dns/keytable.h>
66 #include <dns/keyvalues.h>
67 #include <dns/lib.h>
68 #include <dns/master.h>
69 #include <dns/masterdump.h>
70 #include <dns/order.h>
71 #include <dns/peer.h>
72 #include <dns/portlist.h>
73 #include <dns/rbt.h>
74 #include <dns/rdataclass.h>
75 #include <dns/rdataset.h>
76 #include <dns/rdatastruct.h>
77 #include <dns/resolver.h>
78 #include <dns/rootns.h>
79 #include <dns/secalg.h>
80 #include <dns/stats.h>
81 #include <dns/tkey.h>
82 #include <dns/tsig.h>
83 #include <dns/view.h>
84 #include <dns/zone.h>
85 #include <dns/zt.h>
87 #include <dst/dst.h>
88 #include <dst/result.h>
90 #include <named/client.h>
91 #include <named/config.h>
92 #include <named/control.h>
93 #include <named/interfacemgr.h>
94 #include <named/log.h>
95 #include <named/logconf.h>
96 #include <named/lwresd.h>
97 #include <named/main.h>
98 #include <named/os.h>
99 #include <named/server.h>
100 #include <named/statschannel.h>
101 #include <named/tkeyconf.h>
102 #include <named/tsigconf.h>
103 #include <named/zoneconf.h>
104 #ifdef HAVE_LIBSCF
105 #include <named/ns_smf_globals.h>
106 #include <stdlib.h>
107 #endif
110 * Check an operation for failure. Assumes that the function
111 * using it has a 'result' variable and a 'cleanup' label.
113 #define CHECK(op) \
114 do { result = (op); \
115 if (result != ISC_R_SUCCESS) goto cleanup; \
116 } while (0)
118 #define CHECKM(op, msg) \
119 do { result = (op); \
120 if (result != ISC_R_SUCCESS) { \
121 isc_log_write(ns_g_lctx, \
122 NS_LOGCATEGORY_GENERAL, \
123 NS_LOGMODULE_SERVER, \
124 ISC_LOG_ERROR, \
125 "%s: %s", msg, \
126 isc_result_totext(result)); \
127 goto cleanup; \
129 } while (0) \
131 #define CHECKMF(op, msg, file) \
132 do { result = (op); \
133 if (result != ISC_R_SUCCESS) { \
134 isc_log_write(ns_g_lctx, \
135 NS_LOGCATEGORY_GENERAL, \
136 NS_LOGMODULE_SERVER, \
137 ISC_LOG_ERROR, \
138 "%s '%s': %s", msg, file, \
139 isc_result_totext(result)); \
140 goto cleanup; \
142 } while (0) \
144 #define CHECKFATAL(op, msg) \
145 do { result = (op); \
146 if (result != ISC_R_SUCCESS) \
147 fatal(msg, result); \
148 } while (0) \
151 * Maximum ADB size for views that share a cache. Use this limit to suppress
152 * the total of memory footprint, which should be the main reason for sharing
153 * a cache. Only effective when a finite max-cache-size is specified.
154 * This is currently defined to be 8MB.
156 #define MAX_ADB_SIZE_FOR_CACHESHARE 8388608
158 struct ns_dispatch {
159 isc_sockaddr_t addr;
160 unsigned int dispatchgen;
161 dns_dispatch_t *dispatch;
162 ISC_LINK(struct ns_dispatch) link;
165 struct ns_cache {
166 dns_cache_t *cache;
167 dns_view_t *primaryview;
168 isc_boolean_t needflush;
169 isc_boolean_t adbsizeadjusted;
170 ISC_LINK(ns_cache_t) link;
173 struct dumpcontext {
174 isc_mem_t *mctx;
175 isc_boolean_t dumpcache;
176 isc_boolean_t dumpzones;
177 FILE *fp;
178 ISC_LIST(struct viewlistentry) viewlist;
179 struct viewlistentry *view;
180 struct zonelistentry *zone;
181 dns_dumpctx_t *mdctx;
182 dns_db_t *db;
183 dns_db_t *cache;
184 isc_task_t *task;
185 dns_dbversion_t *version;
188 struct viewlistentry {
189 dns_view_t *view;
190 ISC_LINK(struct viewlistentry) link;
191 ISC_LIST(struct zonelistentry) zonelist;
194 struct zonelistentry {
195 dns_zone_t *zone;
196 ISC_LINK(struct zonelistentry) link;
200 * These zones should not leak onto the Internet.
202 static const struct {
203 const char *zone;
204 isc_boolean_t rfc1918;
205 } empty_zones[] = {
206 #ifdef notyet
207 /* RFC 1918 */
208 { "10.IN-ADDR.ARPA", ISC_TRUE },
209 { "16.172.IN-ADDR.ARPA", ISC_TRUE },
210 { "17.172.IN-ADDR.ARPA", ISC_TRUE },
211 { "18.172.IN-ADDR.ARPA", ISC_TRUE },
212 { "19.172.IN-ADDR.ARPA", ISC_TRUE },
213 { "20.172.IN-ADDR.ARPA", ISC_TRUE },
214 { "21.172.IN-ADDR.ARPA", ISC_TRUE },
215 { "22.172.IN-ADDR.ARPA", ISC_TRUE },
216 { "23.172.IN-ADDR.ARPA", ISC_TRUE },
217 { "24.172.IN-ADDR.ARPA", ISC_TRUE },
218 { "25.172.IN-ADDR.ARPA", ISC_TRUE },
219 { "26.172.IN-ADDR.ARPA", ISC_TRUE },
220 { "27.172.IN-ADDR.ARPA", ISC_TRUE },
221 { "28.172.IN-ADDR.ARPA", ISC_TRUE },
222 { "29.172.IN-ADDR.ARPA", ISC_TRUE },
223 { "30.172.IN-ADDR.ARPA", ISC_TRUE },
224 { "31.172.IN-ADDR.ARPA", ISC_TRUE },
225 { "168.192.IN-ADDR.ARPA", ISC_TRUE },
226 #endif
228 /* RFC 3330 */
229 { "0.IN-ADDR.ARPA", ISC_FALSE }, /* THIS NETWORK */
230 { "127.IN-ADDR.ARPA", ISC_FALSE }, /* LOOPBACK */
231 { "254.169.IN-ADDR.ARPA", ISC_FALSE }, /* LINK LOCAL */
232 { "2.0.192.IN-ADDR.ARPA", ISC_FALSE }, /* TEST NET */
233 { "255.255.255.255.IN-ADDR.ARPA", ISC_FALSE }, /* BROADCAST */
235 /* Local IPv6 Unicast Addresses */
236 { "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA", ISC_FALSE },
237 { "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.ARPA", ISC_FALSE },
238 /* LOCALLY ASSIGNED LOCAL ADDRESS SCOPE */
239 { "D.F.IP6.ARPA", ISC_FALSE },
240 { "8.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
241 { "9.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
242 { "A.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
243 { "B.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
245 { NULL, ISC_FALSE }
248 ISC_PLATFORM_NORETURN_PRE static void
249 fatal(const char *msg, isc_result_t result) ISC_PLATFORM_NORETURN_POST;
251 static void
252 ns_server_reload(isc_task_t *task, isc_event_t *event);
254 static isc_result_t
255 ns_listenelt_fromconfig(const cfg_obj_t *listener, const cfg_obj_t *config,
256 cfg_aclconfctx_t *actx,
257 isc_mem_t *mctx, ns_listenelt_t **target);
258 static isc_result_t
259 ns_listenlist_fromconfig(const cfg_obj_t *listenlist, const cfg_obj_t *config,
260 cfg_aclconfctx_t *actx,
261 isc_mem_t *mctx, ns_listenlist_t **target);
263 static isc_result_t
264 configure_forward(const cfg_obj_t *config, dns_view_t *view, dns_name_t *origin,
265 const cfg_obj_t *forwarders, const cfg_obj_t *forwardtype);
267 static isc_result_t
268 configure_alternates(const cfg_obj_t *config, dns_view_t *view,
269 const cfg_obj_t *alternates);
271 static isc_result_t
272 configure_zone(const cfg_obj_t *config, const cfg_obj_t *zconfig,
273 const cfg_obj_t *vconfig, isc_mem_t *mctx, dns_view_t *view,
274 cfg_aclconfctx_t *aclconf);
276 static isc_result_t
277 add_keydata_zone(dns_view_t *view, isc_mem_t *mctx);
279 static void
280 end_reserved_dispatches(ns_server_t *server, isc_boolean_t all);
283 * Configure a single view ACL at '*aclp'. Get its configuration from
284 * 'vconfig' (for per-view configuration) and maybe from 'config'
286 static isc_result_t
287 configure_view_acl(const cfg_obj_t *vconfig, const cfg_obj_t *config,
288 const char *aclname, const char *acltuplename,
289 cfg_aclconfctx_t *actx, isc_mem_t *mctx, dns_acl_t **aclp)
291 isc_result_t result;
292 const cfg_obj_t *maps[3];
293 const cfg_obj_t *aclobj = NULL;
294 int i = 0;
296 if (*aclp != NULL)
297 dns_acl_detach(aclp);
298 if (vconfig != NULL)
299 maps[i++] = cfg_tuple_get(vconfig, "options");
300 if (config != NULL) {
301 const cfg_obj_t *options = NULL;
302 (void)cfg_map_get(config, "options", &options);
303 if (options != NULL)
304 maps[i++] = options;
306 maps[i] = NULL;
308 (void)ns_config_get(maps, aclname, &aclobj);
309 if (aclobj == NULL)
311 * No value available. *aclp == NULL.
313 return (ISC_R_SUCCESS);
315 if (acltuplename != NULL) {
317 * If the ACL is given in an optional tuple, retrieve it.
318 * The parser should have ensured that a valid object be
319 * returned.
321 aclobj = cfg_tuple_get(aclobj, acltuplename);
324 result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
325 actx, mctx, 0, aclp);
327 return (result);
331 * Configure a sortlist at '*aclp'. Essentially the same as
332 * configure_view_acl() except it calls cfg_acl_fromconfig with a
333 * nest_level value of 2.
335 static isc_result_t
336 configure_view_sortlist(const cfg_obj_t *vconfig, const cfg_obj_t *config,
337 cfg_aclconfctx_t *actx, isc_mem_t *mctx,
338 dns_acl_t **aclp)
340 isc_result_t result;
341 const cfg_obj_t *maps[3];
342 const cfg_obj_t *aclobj = NULL;
343 int i = 0;
345 if (*aclp != NULL)
346 dns_acl_detach(aclp);
347 if (vconfig != NULL)
348 maps[i++] = cfg_tuple_get(vconfig, "options");
349 if (config != NULL) {
350 const cfg_obj_t *options = NULL;
351 (void)cfg_map_get(config, "options", &options);
352 if (options != NULL)
353 maps[i++] = options;
355 maps[i] = NULL;
357 (void)ns_config_get(maps, "sortlist", &aclobj);
358 if (aclobj == NULL)
359 return (ISC_R_SUCCESS);
362 * Use a nest level of 3 for the "top level" of the sortlist;
363 * this means each entry in the top three levels will be stored
364 * as lists of separate, nested ACLs, rather than merged together
365 * into IP tables as is usually done with ACLs.
367 result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
368 actx, mctx, 3, aclp);
370 return (result);
373 static isc_result_t
374 configure_view_nametable(const cfg_obj_t *vconfig, const cfg_obj_t *config,
375 const char *confname, const char *conftuplename,
376 isc_mem_t *mctx, dns_rbt_t **rbtp)
378 isc_result_t result;
379 const cfg_obj_t *maps[3];
380 const cfg_obj_t *obj = NULL;
381 const cfg_listelt_t *element;
382 int i = 0;
383 dns_fixedname_t fixed;
384 dns_name_t *name;
385 isc_buffer_t b;
386 const char *str;
387 const cfg_obj_t *nameobj;
389 if (*rbtp != NULL)
390 dns_rbt_destroy(rbtp);
391 if (vconfig != NULL)
392 maps[i++] = cfg_tuple_get(vconfig, "options");
393 if (config != NULL) {
394 const cfg_obj_t *options = NULL;
395 (void)cfg_map_get(config, "options", &options);
396 if (options != NULL)
397 maps[i++] = options;
399 maps[i] = NULL;
401 (void)ns_config_get(maps, confname, &obj);
402 if (obj == NULL)
404 * No value available. *rbtp == NULL.
406 return (ISC_R_SUCCESS);
408 if (conftuplename != NULL) {
409 obj = cfg_tuple_get(obj, conftuplename);
410 if (cfg_obj_isvoid(obj))
411 return (ISC_R_SUCCESS);
414 result = dns_rbt_create(mctx, NULL, NULL, rbtp);
415 if (result != ISC_R_SUCCESS)
416 return (result);
418 dns_fixedname_init(&fixed);
419 name = dns_fixedname_name(&fixed);
420 for (element = cfg_list_first(obj);
421 element != NULL;
422 element = cfg_list_next(element)) {
423 nameobj = cfg_listelt_value(element);
424 str = cfg_obj_asstring(nameobj);
425 isc_buffer_init(&b, str, strlen(str));
426 isc_buffer_add(&b, strlen(str));
427 CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
429 * We don't need the node data, but need to set dummy data to
430 * avoid a partial match with an empty node. For example, if
431 * we have foo.example.com and bar.example.com, we'd get a match
432 * for baz.example.com, which is not the expected result.
433 * We simply use (void *)1 as the dummy data.
435 result = dns_rbt_addname(*rbtp, name, (void *)1);
436 if (result != ISC_R_SUCCESS) {
437 cfg_obj_log(nameobj, ns_g_lctx, ISC_LOG_ERROR,
438 "failed to add %s for %s: %s",
439 str, confname, isc_result_totext(result));
440 goto cleanup;
445 return (result);
447 cleanup:
448 dns_rbt_destroy(rbtp);
449 return (result);
453 static isc_result_t
454 dstkey_fromconfig(const cfg_obj_t *vconfig, const cfg_obj_t *key,
455 isc_boolean_t managed, dst_key_t **target, isc_mem_t *mctx)
457 dns_rdataclass_t viewclass;
458 dns_rdata_dnskey_t keystruct;
459 isc_uint32_t flags, proto, alg;
460 const char *keystr, *keynamestr;
461 unsigned char keydata[4096];
462 isc_buffer_t keydatabuf;
463 unsigned char rrdata[4096];
464 isc_buffer_t rrdatabuf;
465 isc_region_t r;
466 dns_fixedname_t fkeyname;
467 dns_name_t *keyname;
468 isc_buffer_t namebuf;
469 isc_result_t result;
470 dst_key_t *dstkey = NULL;
472 INSIST(target != NULL && *target == NULL);
474 flags = cfg_obj_asuint32(cfg_tuple_get(key, "flags"));
475 proto = cfg_obj_asuint32(cfg_tuple_get(key, "protocol"));
476 alg = cfg_obj_asuint32(cfg_tuple_get(key, "algorithm"));
477 keyname = dns_fixedname_name(&fkeyname);
478 keynamestr = cfg_obj_asstring(cfg_tuple_get(key, "name"));
480 if (managed) {
481 const char *initmethod;
482 initmethod = cfg_obj_asstring(cfg_tuple_get(key, "init"));
484 if (strcmp(initmethod, "initial-key") != 0) {
485 cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
486 "managed key '%s': "
487 "invalid initialization method '%s'",
488 keynamestr, initmethod);
489 result = ISC_R_FAILURE;
490 goto cleanup;
494 if (vconfig == NULL)
495 viewclass = dns_rdataclass_in;
496 else {
497 const cfg_obj_t *classobj = cfg_tuple_get(vconfig, "class");
498 CHECK(ns_config_getclass(classobj, dns_rdataclass_in,
499 &viewclass));
501 keystruct.common.rdclass = viewclass;
502 keystruct.common.rdtype = dns_rdatatype_dnskey;
504 * The key data in keystruct is not dynamically allocated.
506 keystruct.mctx = NULL;
508 ISC_LINK_INIT(&keystruct.common, link);
510 if (flags > 0xffff)
511 CHECKM(ISC_R_RANGE, "key flags");
512 if (proto > 0xff)
513 CHECKM(ISC_R_RANGE, "key protocol");
514 if (alg > 0xff)
515 CHECKM(ISC_R_RANGE, "key algorithm");
516 keystruct.flags = (isc_uint16_t)flags;
517 keystruct.protocol = (isc_uint8_t)proto;
518 keystruct.algorithm = (isc_uint8_t)alg;
520 isc_buffer_init(&keydatabuf, keydata, sizeof(keydata));
521 isc_buffer_init(&rrdatabuf, rrdata, sizeof(rrdata));
523 keystr = cfg_obj_asstring(cfg_tuple_get(key, "key"));
524 CHECK(isc_base64_decodestring(keystr, &keydatabuf));
525 isc_buffer_usedregion(&keydatabuf, &r);
526 keystruct.datalen = r.length;
527 keystruct.data = r.base;
529 if ((keystruct.algorithm == DST_ALG_RSASHA1 ||
530 keystruct.algorithm == DST_ALG_RSAMD5) &&
531 r.length > 1 && r.base[0] == 1 && r.base[1] == 3)
532 cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
533 "%s key '%s' has a weak exponent",
534 managed ? "managed" : "trusted",
535 keynamestr);
537 CHECK(dns_rdata_fromstruct(NULL,
538 keystruct.common.rdclass,
539 keystruct.common.rdtype,
540 &keystruct, &rrdatabuf));
541 dns_fixedname_init(&fkeyname);
542 isc_buffer_init(&namebuf, keynamestr, strlen(keynamestr));
543 isc_buffer_add(&namebuf, strlen(keynamestr));
544 CHECK(dns_name_fromtext(keyname, &namebuf, dns_rootname, 0, NULL));
545 CHECK(dst_key_fromdns(keyname, viewclass, &rrdatabuf,
546 mctx, &dstkey));
548 *target = dstkey;
549 return (ISC_R_SUCCESS);
551 cleanup:
552 if (result == DST_R_NOCRYPTO) {
553 cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
554 "ignoring %s key for '%s': no crypto support",
555 managed ? "managed" : "trusted",
556 keynamestr);
557 } else if (result == DST_R_UNSUPPORTEDALG) {
558 cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
559 "skipping %s key for '%s': %s",
560 managed ? "managed" : "trusted",
561 keynamestr, isc_result_totext(result));
562 } else {
563 cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
564 "configuring %s key for '%s': %s",
565 managed ? "managed" : "trusted",
566 keynamestr, isc_result_totext(result));
567 result = ISC_R_FAILURE;
570 if (dstkey != NULL)
571 dst_key_free(&dstkey);
573 return (result);
576 static isc_result_t
577 load_view_keys(const cfg_obj_t *keys, const cfg_obj_t *vconfig,
578 dns_view_t *view, isc_boolean_t managed, isc_mem_t *mctx)
580 const cfg_listelt_t *elt, *elt2;
581 const cfg_obj_t *key, *keylist;
582 dst_key_t *dstkey = NULL;
583 isc_result_t result;
584 dns_keytable_t *secroots = NULL;
586 CHECK(dns_view_getsecroots(view, &secroots));
588 for (elt = cfg_list_first(keys);
589 elt != NULL;
590 elt = cfg_list_next(elt)) {
591 keylist = cfg_listelt_value(elt);
593 for (elt2 = cfg_list_first(keylist);
594 elt2 != NULL;
595 elt2 = cfg_list_next(elt2)) {
596 key = cfg_listelt_value(elt2);
597 result = dstkey_fromconfig(vconfig, key, managed,
598 &dstkey, mctx);
599 if (result == DST_R_UNSUPPORTEDALG) {
600 result = ISC_R_SUCCESS;
601 continue;
603 if (result != ISC_R_SUCCESS)
604 goto cleanup;
606 CHECK(dns_keytable_add(secroots, managed, &dstkey));
610 cleanup:
611 if (secroots != NULL)
612 dns_keytable_detach(&secroots);
613 if (result == DST_R_NOCRYPTO)
614 result = ISC_R_SUCCESS;
615 return (result);
619 * Configure DNSSEC keys for a view.
621 * The per-view configuration values and the server-global defaults are read
622 * from 'vconfig' and 'config'.
624 static isc_result_t
625 configure_view_dnsseckeys(dns_view_t *view, const cfg_obj_t *vconfig,
626 const cfg_obj_t *config, const cfg_obj_t *bindkeys,
627 isc_boolean_t auto_dlv, isc_mem_t *mctx)
629 isc_result_t result = ISC_R_SUCCESS;
630 const cfg_obj_t *view_keys = NULL;
631 const cfg_obj_t *global_keys = NULL;
632 const cfg_obj_t *global_managed_keys = NULL;
633 const cfg_obj_t *builtin_keys = NULL;
634 const cfg_obj_t *builtin_managed_keys = NULL;
635 const cfg_obj_t *maps[4];
636 const cfg_obj_t *voptions = NULL;
637 const cfg_obj_t *options = NULL;
638 isc_boolean_t meta;
639 int i = 0;
641 /* We don't need trust anchors for the _bind view */
642 if (strcmp(view->name, "_bind") == 0 &&
643 view->rdclass == dns_rdataclass_chaos) {
644 return (ISC_R_SUCCESS);
647 meta = ISC_TF(strcmp(view->name, "_meta") == 0 &&
648 view->rdclass == dns_rdataclass_in);
650 if (vconfig != NULL) {
651 voptions = cfg_tuple_get(vconfig, "options");
652 if (voptions != NULL) {
653 (void) cfg_map_get(voptions, "trusted-keys",
654 &view_keys);
655 maps[i++] = voptions;
659 if (config != NULL) {
660 (void)cfg_map_get(config, "trusted-keys", &global_keys);
661 (void)cfg_map_get(config, "managed-keys", &global_managed_keys);
662 (void)cfg_map_get(config, "options", &options);
663 if (options != NULL) {
664 maps[i++] = options;
668 maps[i++] = ns_g_defaults;
669 maps[i] = NULL;
671 result = dns_view_initsecroots(view, mctx);
672 if (result != ISC_R_SUCCESS) {
673 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
674 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
675 "couldn't create keytable");
676 return (ISC_R_UNEXPECTED);
679 if (global_managed_keys != NULL)
680 ns_g_server->managedkeys = ISC_TRUE;
682 if (auto_dlv) {
683 isc_log_write(ns_g_lctx, DNS_LOGCATEGORY_SECURITY,
684 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
685 "using built-in trusted-keys for view %s",
686 view->name);
689 * If bind.keys exists, it overrides the managed-keys
690 * clause hard-coded in ns_g_config.
692 if (bindkeys != NULL) {
693 (void)cfg_map_get(bindkeys, "trusted-keys",
694 &builtin_keys);
695 (void)cfg_map_get(bindkeys, "managed-keys",
696 &builtin_managed_keys);
697 } else {
698 (void)cfg_map_get(ns_g_config, "trusted-keys",
699 &builtin_keys);
700 (void)cfg_map_get(ns_g_config, "managed-keys",
701 &builtin_managed_keys);
704 if (builtin_managed_keys != NULL)
705 ns_g_server->managedkeys = ISC_TRUE;
706 CHECK(load_view_keys(builtin_keys, vconfig, view,
707 ISC_FALSE, mctx));
709 if (meta)
710 CHECK(load_view_keys(builtin_managed_keys, vconfig,
711 view, ISC_TRUE, mctx));
714 CHECK(load_view_keys(view_keys, vconfig, view, ISC_FALSE, mctx));
715 CHECK(load_view_keys(global_keys, vconfig, view, ISC_FALSE, mctx));
717 if (meta)
718 CHECK(load_view_keys(global_managed_keys, vconfig, view,
719 ISC_TRUE, mctx));
721 cleanup:
722 return (result);
725 static isc_result_t
726 mustbesecure(const cfg_obj_t *mbs, dns_resolver_t *resolver) {
727 const cfg_listelt_t *element;
728 const cfg_obj_t *obj;
729 const char *str;
730 dns_fixedname_t fixed;
731 dns_name_t *name;
732 isc_boolean_t value;
733 isc_result_t result;
734 isc_buffer_t b;
736 dns_fixedname_init(&fixed);
737 name = dns_fixedname_name(&fixed);
738 for (element = cfg_list_first(mbs);
739 element != NULL;
740 element = cfg_list_next(element))
742 obj = cfg_listelt_value(element);
743 str = cfg_obj_asstring(cfg_tuple_get(obj, "name"));
744 isc_buffer_init(&b, str, strlen(str));
745 isc_buffer_add(&b, strlen(str));
746 CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
747 value = cfg_obj_asboolean(cfg_tuple_get(obj, "value"));
748 CHECK(dns_resolver_setmustbesecure(resolver, name, value));
751 result = ISC_R_SUCCESS;
753 cleanup:
754 return (result);
758 * Get a dispatch appropriate for the resolver of a given view.
760 static isc_result_t
761 get_view_querysource_dispatch(const cfg_obj_t **maps,
762 int af, dns_dispatch_t **dispatchp,
763 isc_boolean_t is_firstview)
765 isc_result_t result;
766 dns_dispatch_t *disp;
767 isc_sockaddr_t sa;
768 unsigned int attrs, attrmask;
769 const cfg_obj_t *obj = NULL;
770 unsigned int maxdispatchbuffers;
773 * Make compiler happy.
775 result = ISC_R_FAILURE;
777 switch (af) {
778 case AF_INET:
779 result = ns_config_get(maps, "query-source", &obj);
780 INSIST(result == ISC_R_SUCCESS);
781 break;
782 case AF_INET6:
783 result = ns_config_get(maps, "query-source-v6", &obj);
784 INSIST(result == ISC_R_SUCCESS);
785 break;
786 default:
787 INSIST(0);
790 sa = *(cfg_obj_assockaddr(obj));
791 INSIST(isc_sockaddr_pf(&sa) == af);
794 * If we don't support this address family, we're done!
796 switch (af) {
797 case AF_INET:
798 result = isc_net_probeipv4();
799 break;
800 case AF_INET6:
801 result = isc_net_probeipv6();
802 break;
803 default:
804 INSIST(0);
806 if (result != ISC_R_SUCCESS)
807 return (ISC_R_SUCCESS);
810 * Try to find a dispatcher that we can share.
812 attrs = 0;
813 attrs |= DNS_DISPATCHATTR_UDP;
814 switch (af) {
815 case AF_INET:
816 attrs |= DNS_DISPATCHATTR_IPV4;
817 break;
818 case AF_INET6:
819 attrs |= DNS_DISPATCHATTR_IPV6;
820 break;
822 if (isc_sockaddr_getport(&sa) == 0) {
823 attrs |= DNS_DISPATCHATTR_EXCLUSIVE;
824 maxdispatchbuffers = 4096;
825 } else {
826 INSIST(obj != NULL);
827 if (is_firstview) {
828 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_INFO,
829 "using specific query-source port "
830 "suppresses port randomization and can be "
831 "insecure.");
833 maxdispatchbuffers = 1000;
836 attrmask = 0;
837 attrmask |= DNS_DISPATCHATTR_UDP;
838 attrmask |= DNS_DISPATCHATTR_TCP;
839 attrmask |= DNS_DISPATCHATTR_IPV4;
840 attrmask |= DNS_DISPATCHATTR_IPV6;
842 disp = NULL;
843 result = dns_dispatch_getudp(ns_g_dispatchmgr, ns_g_socketmgr,
844 ns_g_taskmgr, &sa, 4096,
845 maxdispatchbuffers, 32768, 16411, 16433,
846 attrs, attrmask, &disp);
847 if (result != ISC_R_SUCCESS) {
848 isc_sockaddr_t any;
849 char buf[ISC_SOCKADDR_FORMATSIZE];
851 switch (af) {
852 case AF_INET:
853 isc_sockaddr_any(&any);
854 break;
855 case AF_INET6:
856 isc_sockaddr_any6(&any);
857 break;
859 if (isc_sockaddr_equal(&sa, &any))
860 return (ISC_R_SUCCESS);
861 isc_sockaddr_format(&sa, buf, sizeof(buf));
862 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
863 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
864 "could not get query source dispatcher (%s)",
865 buf);
866 return (result);
869 *dispatchp = disp;
871 return (ISC_R_SUCCESS);
874 static isc_result_t
875 configure_order(dns_order_t *order, const cfg_obj_t *ent) {
876 dns_rdataclass_t rdclass;
877 dns_rdatatype_t rdtype;
878 const cfg_obj_t *obj;
879 dns_fixedname_t fixed;
880 unsigned int mode = 0;
881 const char *str;
882 isc_buffer_t b;
883 isc_result_t result;
884 isc_boolean_t addroot;
886 result = ns_config_getclass(cfg_tuple_get(ent, "class"),
887 dns_rdataclass_any, &rdclass);
888 if (result != ISC_R_SUCCESS)
889 return (result);
891 result = ns_config_gettype(cfg_tuple_get(ent, "type"),
892 dns_rdatatype_any, &rdtype);
893 if (result != ISC_R_SUCCESS)
894 return (result);
896 obj = cfg_tuple_get(ent, "name");
897 if (cfg_obj_isstring(obj))
898 str = cfg_obj_asstring(obj);
899 else
900 str = "*";
901 addroot = ISC_TF(strcmp(str, "*") == 0);
902 isc_buffer_init(&b, str, strlen(str));
903 isc_buffer_add(&b, strlen(str));
904 dns_fixedname_init(&fixed);
905 result = dns_name_fromtext(dns_fixedname_name(&fixed), &b,
906 dns_rootname, 0, NULL);
907 if (result != ISC_R_SUCCESS)
908 return (result);
910 obj = cfg_tuple_get(ent, "ordering");
911 INSIST(cfg_obj_isstring(obj));
912 str = cfg_obj_asstring(obj);
913 if (!strcasecmp(str, "fixed"))
914 mode = DNS_RDATASETATTR_FIXEDORDER;
915 else if (!strcasecmp(str, "random"))
916 mode = DNS_RDATASETATTR_RANDOMIZE;
917 else if (!strcasecmp(str, "cyclic"))
918 mode = 0;
919 else
920 INSIST(0);
923 * "*" should match everything including the root (BIND 8 compat).
924 * As dns_name_matcheswildcard(".", "*.") returns FALSE add a
925 * explicit entry for "." when the name is "*".
927 if (addroot) {
928 result = dns_order_add(order, dns_rootname,
929 rdtype, rdclass, mode);
930 if (result != ISC_R_SUCCESS)
931 return (result);
934 return (dns_order_add(order, dns_fixedname_name(&fixed),
935 rdtype, rdclass, mode));
938 static isc_result_t
939 configure_peer(const cfg_obj_t *cpeer, isc_mem_t *mctx, dns_peer_t **peerp) {
940 isc_netaddr_t na;
941 dns_peer_t *peer;
942 const cfg_obj_t *obj;
943 const char *str;
944 isc_result_t result;
945 unsigned int prefixlen;
947 cfg_obj_asnetprefix(cfg_map_getname(cpeer), &na, &prefixlen);
949 peer = NULL;
950 result = dns_peer_newprefix(mctx, &na, prefixlen, &peer);
951 if (result != ISC_R_SUCCESS)
952 return (result);
954 obj = NULL;
955 (void)cfg_map_get(cpeer, "bogus", &obj);
956 if (obj != NULL)
957 CHECK(dns_peer_setbogus(peer, cfg_obj_asboolean(obj)));
959 obj = NULL;
960 (void)cfg_map_get(cpeer, "provide-ixfr", &obj);
961 if (obj != NULL)
962 CHECK(dns_peer_setprovideixfr(peer, cfg_obj_asboolean(obj)));
964 obj = NULL;
965 (void)cfg_map_get(cpeer, "request-ixfr", &obj);
966 if (obj != NULL)
967 CHECK(dns_peer_setrequestixfr(peer, cfg_obj_asboolean(obj)));
969 obj = NULL;
970 (void)cfg_map_get(cpeer, "request-nsid", &obj);
971 if (obj != NULL)
972 CHECK(dns_peer_setrequestnsid(peer, cfg_obj_asboolean(obj)));
974 obj = NULL;
975 (void)cfg_map_get(cpeer, "edns", &obj);
976 if (obj != NULL)
977 CHECK(dns_peer_setsupportedns(peer, cfg_obj_asboolean(obj)));
979 obj = NULL;
980 (void)cfg_map_get(cpeer, "edns-udp-size", &obj);
981 if (obj != NULL) {
982 isc_uint32_t udpsize = cfg_obj_asuint32(obj);
983 if (udpsize < 512)
984 udpsize = 512;
985 if (udpsize > 4096)
986 udpsize = 4096;
987 CHECK(dns_peer_setudpsize(peer, (isc_uint16_t)udpsize));
990 obj = NULL;
991 (void)cfg_map_get(cpeer, "max-udp-size", &obj);
992 if (obj != NULL) {
993 isc_uint32_t udpsize = cfg_obj_asuint32(obj);
994 if (udpsize < 512)
995 udpsize = 512;
996 if (udpsize > 4096)
997 udpsize = 4096;
998 CHECK(dns_peer_setmaxudp(peer, (isc_uint16_t)udpsize));
1001 obj = NULL;
1002 (void)cfg_map_get(cpeer, "transfers", &obj);
1003 if (obj != NULL)
1004 CHECK(dns_peer_settransfers(peer, cfg_obj_asuint32(obj)));
1006 obj = NULL;
1007 (void)cfg_map_get(cpeer, "transfer-format", &obj);
1008 if (obj != NULL) {
1009 str = cfg_obj_asstring(obj);
1010 if (strcasecmp(str, "many-answers") == 0)
1011 CHECK(dns_peer_settransferformat(peer,
1012 dns_many_answers));
1013 else if (strcasecmp(str, "one-answer") == 0)
1014 CHECK(dns_peer_settransferformat(peer,
1015 dns_one_answer));
1016 else
1017 INSIST(0);
1020 obj = NULL;
1021 (void)cfg_map_get(cpeer, "keys", &obj);
1022 if (obj != NULL) {
1023 result = dns_peer_setkeybycharp(peer, cfg_obj_asstring(obj));
1024 if (result != ISC_R_SUCCESS)
1025 goto cleanup;
1028 obj = NULL;
1029 if (na.family == AF_INET)
1030 (void)cfg_map_get(cpeer, "transfer-source", &obj);
1031 else
1032 (void)cfg_map_get(cpeer, "transfer-source-v6", &obj);
1033 if (obj != NULL) {
1034 result = dns_peer_settransfersource(peer,
1035 cfg_obj_assockaddr(obj));
1036 if (result != ISC_R_SUCCESS)
1037 goto cleanup;
1038 ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
1041 obj = NULL;
1042 if (na.family == AF_INET)
1043 (void)cfg_map_get(cpeer, "notify-source", &obj);
1044 else
1045 (void)cfg_map_get(cpeer, "notify-source-v6", &obj);
1046 if (obj != NULL) {
1047 result = dns_peer_setnotifysource(peer,
1048 cfg_obj_assockaddr(obj));
1049 if (result != ISC_R_SUCCESS)
1050 goto cleanup;
1051 ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
1054 obj = NULL;
1055 if (na.family == AF_INET)
1056 (void)cfg_map_get(cpeer, "query-source", &obj);
1057 else
1058 (void)cfg_map_get(cpeer, "query-source-v6", &obj);
1059 if (obj != NULL) {
1060 result = dns_peer_setquerysource(peer,
1061 cfg_obj_assockaddr(obj));
1062 if (result != ISC_R_SUCCESS)
1063 goto cleanup;
1064 ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
1067 *peerp = peer;
1068 return (ISC_R_SUCCESS);
1070 cleanup:
1071 dns_peer_detach(&peer);
1072 return (result);
1075 static isc_result_t
1076 disable_algorithms(const cfg_obj_t *disabled, dns_resolver_t *resolver) {
1077 isc_result_t result;
1078 const cfg_obj_t *algorithms;
1079 const cfg_listelt_t *element;
1080 const char *str;
1081 dns_fixedname_t fixed;
1082 dns_name_t *name;
1083 isc_buffer_t b;
1085 dns_fixedname_init(&fixed);
1086 name = dns_fixedname_name(&fixed);
1087 str = cfg_obj_asstring(cfg_tuple_get(disabled, "name"));
1088 isc_buffer_init(&b, str, strlen(str));
1089 isc_buffer_add(&b, strlen(str));
1090 CHECK(dns_name_fromtext(name, &b, dns_rootname, 0, NULL));
1092 algorithms = cfg_tuple_get(disabled, "algorithms");
1093 for (element = cfg_list_first(algorithms);
1094 element != NULL;
1095 element = cfg_list_next(element))
1097 isc_textregion_t r;
1098 dns_secalg_t alg;
1100 DE_CONST(cfg_obj_asstring(cfg_listelt_value(element)), r.base);
1101 r.length = strlen(r.base);
1103 result = dns_secalg_fromtext(&alg, &r);
1104 if (result != ISC_R_SUCCESS) {
1105 isc_uint8_t ui;
1106 result = isc_parse_uint8(&ui, r.base, 10);
1107 alg = ui;
1109 if (result != ISC_R_SUCCESS) {
1110 cfg_obj_log(cfg_listelt_value(element),
1111 ns_g_lctx, ISC_LOG_ERROR,
1112 "invalid algorithm");
1113 CHECK(result);
1115 CHECK(dns_resolver_disable_algorithm(resolver, name, alg));
1117 cleanup:
1118 return (result);
1121 static isc_boolean_t
1122 on_disable_list(const cfg_obj_t *disablelist, dns_name_t *zonename) {
1123 const cfg_listelt_t *element;
1124 dns_fixedname_t fixed;
1125 dns_name_t *name;
1126 isc_result_t result;
1127 const cfg_obj_t *value;
1128 const char *str;
1129 isc_buffer_t b;
1131 dns_fixedname_init(&fixed);
1132 name = dns_fixedname_name(&fixed);
1134 for (element = cfg_list_first(disablelist);
1135 element != NULL;
1136 element = cfg_list_next(element))
1138 value = cfg_listelt_value(element);
1139 str = cfg_obj_asstring(value);
1140 isc_buffer_init(&b, str, strlen(str));
1141 isc_buffer_add(&b, strlen(str));
1142 result = dns_name_fromtext(name, &b, dns_rootname,
1143 0, NULL);
1144 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1145 if (dns_name_equal(name, zonename))
1146 return (ISC_TRUE);
1148 return (ISC_FALSE);
1151 static void
1152 check_dbtype(dns_zone_t **zonep, unsigned int dbtypec, const char **dbargv,
1153 isc_mem_t *mctx)
1155 char **argv = NULL;
1156 unsigned int i;
1157 isc_result_t result;
1159 result = dns_zone_getdbtype(*zonep, &argv, mctx);
1160 if (result != ISC_R_SUCCESS) {
1161 dns_zone_detach(zonep);
1162 return;
1166 * Check that all the arguments match.
1168 for (i = 0; i < dbtypec; i++)
1169 if (argv[i] == NULL || strcmp(argv[i], dbargv[i]) != 0) {
1170 dns_zone_detach(zonep);
1171 break;
1175 * Check that there are not extra arguments.
1177 if (i == dbtypec && argv[i] != NULL)
1178 dns_zone_detach(zonep);
1179 isc_mem_free(mctx, argv);
1182 static isc_result_t
1183 setquerystats(dns_zone_t *zone, isc_mem_t *mctx, isc_boolean_t on) {
1184 isc_result_t result;
1185 isc_stats_t *zoneqrystats;
1187 zoneqrystats = NULL;
1188 if (on) {
1189 result = isc_stats_create(mctx, &zoneqrystats,
1190 dns_nsstatscounter_max);
1191 if (result != ISC_R_SUCCESS)
1192 return (result);
1194 dns_zone_setrequeststats(zone, zoneqrystats);
1195 if (zoneqrystats != NULL)
1196 isc_stats_detach(&zoneqrystats);
1198 return (ISC_R_SUCCESS);
1201 static ns_cache_t *
1202 cachelist_find(ns_cachelist_t *cachelist, const char *cachename) {
1203 ns_cache_t *nsc;
1205 for (nsc = ISC_LIST_HEAD(*cachelist);
1206 nsc != NULL;
1207 nsc = ISC_LIST_NEXT(nsc, link)) {
1208 if (strcmp(dns_cache_getname(nsc->cache), cachename) == 0)
1209 return (nsc);
1212 return (NULL);
1215 static isc_boolean_t
1216 cache_reusable(dns_view_t *originview, dns_view_t *view,
1217 isc_boolean_t new_zero_no_soattl)
1219 if (originview->checknames != view->checknames ||
1220 dns_resolver_getzeronosoattl(originview->resolver) !=
1221 new_zero_no_soattl ||
1222 originview->acceptexpired != view->acceptexpired ||
1223 originview->enablevalidation != view->enablevalidation ||
1224 originview->maxcachettl != view->maxcachettl ||
1225 originview->maxncachettl != view->maxncachettl) {
1226 return (ISC_FALSE);
1229 return (ISC_TRUE);
1232 static isc_boolean_t
1233 cache_sharable(dns_view_t *originview, dns_view_t *view,
1234 isc_boolean_t new_zero_no_soattl,
1235 unsigned int new_cleaning_interval,
1236 isc_uint32_t new_max_cache_size)
1239 * If the cache cannot even reused for the same view, it cannot be
1240 * shared with other views.
1242 if (!cache_reusable(originview, view, new_zero_no_soattl))
1243 return (ISC_FALSE);
1246 * Check other cache related parameters that must be consistent among
1247 * the sharing views.
1249 if (dns_cache_getcleaninginterval(originview->cache) !=
1250 new_cleaning_interval ||
1251 dns_cache_getcachesize(originview->cache) != new_max_cache_size) {
1252 return (ISC_FALSE);
1255 return (ISC_TRUE);
1259 * Configure 'view' according to 'vconfig', taking defaults from 'config'
1260 * where values are missing in 'vconfig'.
1262 * When configuring the default view, 'vconfig' will be NULL and the
1263 * global defaults in 'config' used exclusively.
1265 static isc_result_t
1266 configure_view(dns_view_t *view, const cfg_obj_t *config,
1267 const cfg_obj_t *vconfig, ns_cachelist_t *cachelist,
1268 const cfg_obj_t *bindkeys, isc_mem_t *mctx,
1269 cfg_aclconfctx_t *actx, isc_boolean_t need_hints)
1271 const cfg_obj_t *maps[4];
1272 const cfg_obj_t *cfgmaps[3];
1273 const cfg_obj_t *optionmaps[3];
1274 const cfg_obj_t *options = NULL;
1275 const cfg_obj_t *voptions = NULL;
1276 const cfg_obj_t *forwardtype;
1277 const cfg_obj_t *forwarders;
1278 const cfg_obj_t *alternates;
1279 const cfg_obj_t *zonelist;
1280 #ifdef DLZ
1281 const cfg_obj_t *dlz;
1282 unsigned int dlzargc;
1283 char **dlzargv;
1284 #endif
1285 const cfg_obj_t *disabled;
1286 const cfg_obj_t *obj;
1287 const cfg_listelt_t *element;
1288 in_port_t port;
1289 dns_cache_t *cache = NULL;
1290 isc_result_t result;
1291 isc_uint32_t max_adb_size;
1292 unsigned int cleaning_interval;
1293 isc_uint32_t max_cache_size;
1294 isc_uint32_t max_acache_size;
1295 isc_uint32_t lame_ttl;
1296 dns_tsig_keyring_t *ring = NULL;
1297 dns_view_t *pview = NULL; /* Production view */
1298 isc_mem_t *cmctx;
1299 dns_dispatch_t *dispatch4 = NULL;
1300 dns_dispatch_t *dispatch6 = NULL;
1301 isc_boolean_t reused_cache = ISC_FALSE;
1302 isc_boolean_t shared_cache = ISC_FALSE;
1303 int i = 0, j = 0, k = 0;
1304 const char *str;
1305 const char *cachename = NULL;
1306 dns_order_t *order = NULL;
1307 isc_uint32_t udpsize;
1308 unsigned int resopts = 0;
1309 dns_zone_t *zone = NULL;
1310 isc_uint32_t max_clients_per_query;
1311 const char *sep = ": view ";
1312 const char *viewname = view->name;
1313 const char *forview = " for view ";
1314 isc_boolean_t rfc1918;
1315 isc_boolean_t empty_zones_enable;
1316 const cfg_obj_t *disablelist = NULL;
1317 isc_stats_t *resstats = NULL;
1318 dns_stats_t *resquerystats = NULL;
1319 isc_boolean_t auto_dlv = ISC_FALSE;
1320 ns_cache_t *nsc;
1321 isc_boolean_t zero_no_soattl;
1323 REQUIRE(DNS_VIEW_VALID(view));
1325 cmctx = NULL;
1327 if (config != NULL)
1328 (void)cfg_map_get(config, "options", &options);
1331 * maps: view options, options, defaults
1332 * cfgmaps: view options, config
1333 * optionmaps: view options, options
1335 if (vconfig != NULL) {
1336 voptions = cfg_tuple_get(vconfig, "options");
1337 maps[i++] = voptions;
1338 optionmaps[j++] = voptions;
1339 cfgmaps[k++] = voptions;
1341 if (options != NULL) {
1342 maps[i++] = options;
1343 optionmaps[j++] = options;
1346 maps[i++] = ns_g_defaults;
1347 maps[i] = NULL;
1348 optionmaps[j] = NULL;
1349 if (config != NULL)
1350 cfgmaps[k++] = config;
1351 cfgmaps[k] = NULL;
1353 if (!strcmp(viewname, "_default")) {
1354 sep = "";
1355 viewname = "";
1356 forview = "";
1360 * Set the view's port number for outgoing queries.
1362 CHECKM(ns_config_getport(config, &port), "port");
1363 dns_view_setdstport(view, port);
1366 * Create additional cache for this view and zones under the view
1367 * if explicitly enabled.
1368 * XXX950 default to on.
1370 obj = NULL;
1371 (void)ns_config_get(maps, "acache-enable", &obj);
1372 if (obj != NULL && cfg_obj_asboolean(obj)) {
1373 cmctx = NULL;
1374 CHECK(isc_mem_create(0, 0, &cmctx));
1375 CHECK(dns_acache_create(&view->acache, cmctx, ns_g_taskmgr,
1376 ns_g_timermgr));
1377 isc_mem_setname(cmctx, "acache", NULL);
1378 isc_mem_detach(&cmctx);
1380 if (view->acache != NULL) {
1381 obj = NULL;
1382 result = ns_config_get(maps, "acache-cleaning-interval", &obj);
1383 INSIST(result == ISC_R_SUCCESS);
1384 dns_acache_setcleaninginterval(view->acache,
1385 cfg_obj_asuint32(obj) * 60);
1387 obj = NULL;
1388 result = ns_config_get(maps, "max-acache-size", &obj);
1389 INSIST(result == ISC_R_SUCCESS);
1390 if (cfg_obj_isstring(obj)) {
1391 str = cfg_obj_asstring(obj);
1392 INSIST(strcasecmp(str, "unlimited") == 0);
1393 max_acache_size = ISC_UINT32_MAX;
1394 } else {
1395 isc_resourcevalue_t value;
1397 value = cfg_obj_asuint64(obj);
1398 if (value > ISC_UINT32_MAX) {
1399 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
1400 "'max-acache-size "
1401 "%" ISC_PRINT_QUADFORMAT
1402 "d' is too large",
1403 value);
1404 result = ISC_R_RANGE;
1405 goto cleanup;
1407 max_acache_size = (isc_uint32_t)value;
1409 dns_acache_setcachesize(view->acache, max_acache_size);
1413 * Configure the zones.
1415 zonelist = NULL;
1416 if (voptions != NULL)
1417 (void)cfg_map_get(voptions, "zone", &zonelist);
1418 else
1419 (void)cfg_map_get(config, "zone", &zonelist);
1420 for (element = cfg_list_first(zonelist);
1421 element != NULL;
1422 element = cfg_list_next(element))
1424 const cfg_obj_t *zconfig = cfg_listelt_value(element);
1425 CHECK(configure_zone(config, zconfig, vconfig, mctx, view,
1426 actx));
1429 #ifdef DLZ
1431 * Create Dynamically Loadable Zone driver.
1433 dlz = NULL;
1434 if (voptions != NULL)
1435 (void)cfg_map_get(voptions, "dlz", &dlz);
1436 else
1437 (void)cfg_map_get(config, "dlz", &dlz);
1439 obj = NULL;
1440 if (dlz != NULL) {
1441 (void)cfg_map_get(cfg_tuple_get(dlz, "options"),
1442 "database", &obj);
1443 if (obj != NULL) {
1444 char *s = isc_mem_strdup(mctx, cfg_obj_asstring(obj));
1445 if (s == NULL) {
1446 result = ISC_R_NOMEMORY;
1447 goto cleanup;
1450 result = dns_dlzstrtoargv(mctx, s, &dlzargc, &dlzargv);
1451 if (result != ISC_R_SUCCESS) {
1452 isc_mem_free(mctx, s);
1453 goto cleanup;
1456 obj = cfg_tuple_get(dlz, "name");
1457 result = dns_dlzcreate(mctx, cfg_obj_asstring(obj),
1458 dlzargv[0], dlzargc, dlzargv,
1459 &view->dlzdatabase);
1460 isc_mem_free(mctx, s);
1461 isc_mem_put(mctx, dlzargv, dlzargc * sizeof(*dlzargv));
1462 if (result != ISC_R_SUCCESS)
1463 goto cleanup;
1466 #endif
1469 * Obtain configuration parameters that affect the decision of whether
1470 * we can reuse/share an existing cache.
1472 obj = NULL;
1473 result = ns_config_get(maps, "cleaning-interval", &obj);
1474 INSIST(result == ISC_R_SUCCESS);
1475 cleaning_interval = cfg_obj_asuint32(obj) * 60;
1477 obj = NULL;
1478 result = ns_config_get(maps, "max-cache-size", &obj);
1479 INSIST(result == ISC_R_SUCCESS);
1480 if (cfg_obj_isstring(obj)) {
1481 str = cfg_obj_asstring(obj);
1482 INSIST(strcasecmp(str, "unlimited") == 0);
1483 max_cache_size = ISC_UINT32_MAX;
1484 } else {
1485 isc_resourcevalue_t value;
1486 value = cfg_obj_asuint64(obj);
1487 if (value > ISC_UINT32_MAX) {
1488 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
1489 "'max-cache-size "
1490 "%" ISC_PRINT_QUADFORMAT "d' is too large",
1491 value);
1492 result = ISC_R_RANGE;
1493 goto cleanup;
1495 max_cache_size = (isc_uint32_t)value;
1498 /* Check-names. */
1499 obj = NULL;
1500 result = ns_checknames_get(maps, "response", &obj);
1501 INSIST(result == ISC_R_SUCCESS);
1503 str = cfg_obj_asstring(obj);
1504 if (strcasecmp(str, "fail") == 0) {
1505 resopts |= DNS_RESOLVER_CHECKNAMES |
1506 DNS_RESOLVER_CHECKNAMESFAIL;
1507 view->checknames = ISC_TRUE;
1508 } else if (strcasecmp(str, "warn") == 0) {
1509 resopts |= DNS_RESOLVER_CHECKNAMES;
1510 view->checknames = ISC_FALSE;
1511 } else if (strcasecmp(str, "ignore") == 0) {
1512 view->checknames = ISC_FALSE;
1513 } else
1514 INSIST(0);
1516 obj = NULL;
1517 result = ns_config_get(maps, "zero-no-soa-ttl-cache", &obj);
1518 INSIST(result == ISC_R_SUCCESS);
1519 zero_no_soattl = cfg_obj_asboolean(obj);
1521 obj = NULL;
1522 result = ns_config_get(maps, "dnssec-accept-expired", &obj);
1523 INSIST(result == ISC_R_SUCCESS);
1524 view->acceptexpired = cfg_obj_asboolean(obj);
1526 obj = NULL;
1527 result = ns_config_get(maps, "dnssec-validation", &obj);
1528 INSIST(result == ISC_R_SUCCESS);
1529 view->enablevalidation = cfg_obj_asboolean(obj);
1531 obj = NULL;
1532 result = ns_config_get(maps, "max-cache-ttl", &obj);
1533 INSIST(result == ISC_R_SUCCESS);
1534 view->maxcachettl = cfg_obj_asuint32(obj);
1536 obj = NULL;
1537 result = ns_config_get(maps, "max-ncache-ttl", &obj);
1538 INSIST(result == ISC_R_SUCCESS);
1539 view->maxncachettl = cfg_obj_asuint32(obj);
1540 if (view->maxncachettl > 7 * 24 * 3600)
1541 view->maxncachettl = 7 * 24 * 3600;
1544 * Configure the view's cache.
1546 * First, check to see if there are any attach-cache options. If yes,
1547 * attempt to lookup an existing cache at attach it to the view. If
1548 * there is not one, then try to reuse an existing cache if possible;
1549 * otherwise create a new cache.
1551 * Note that the ADB is not preserved or shared in either case.
1553 * When a matching view is found, the associated statistics are also
1554 * retrieved and reused.
1556 * XXX Determining when it is safe to reuse or share a cache is tricky.
1557 * When the view's configuration changes, the cached data may become
1558 * invalid because it reflects our old view of the world. We check
1559 * some of the configuration parameters that could invalidate the cache
1560 * or otherwise make it unsharable, but there are other configuration
1561 * options that should be checked. For example, if a view uses a
1562 * forwarder, changes in the forwarder configuration may invalidate
1563 * the cache. At the moment, it's the administrator's responsibility to
1564 * ensure these configuration options don't invalidate reusing/sharing.
1566 obj = NULL;
1567 result = ns_config_get(maps, "attach-cache", &obj);
1568 if (result == ISC_R_SUCCESS)
1569 cachename = cfg_obj_asstring(obj);
1570 else
1571 cachename = view->name;
1572 cache = NULL;
1573 nsc = cachelist_find(cachelist, cachename);
1574 if (nsc != NULL) {
1575 if (!cache_sharable(nsc->primaryview, view, zero_no_soattl,
1576 cleaning_interval, max_cache_size)) {
1577 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1578 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
1579 "views %s and %s can't share the cache "
1580 "due to configuration parameter mismatch",
1581 nsc->primaryview->name, view->name);
1582 result = ISC_R_FAILURE;
1583 goto cleanup;
1585 dns_cache_attach(nsc->cache, &cache);
1586 shared_cache = ISC_TRUE;
1587 } else {
1588 if (strcmp(cachename, view->name) == 0) {
1589 result = dns_viewlist_find(&ns_g_server->viewlist,
1590 cachename, view->rdclass,
1591 &pview);
1592 if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
1593 goto cleanup;
1594 if (pview != NULL) {
1595 if (!cache_reusable(pview, view,
1596 zero_no_soattl)) {
1597 isc_log_write(ns_g_lctx,
1598 NS_LOGCATEGORY_GENERAL,
1599 NS_LOGMODULE_SERVER,
1600 ISC_LOG_DEBUG(1),
1601 "cache cannot be reused "
1602 "for view %s due to "
1603 "configuration parameter "
1604 "mismatch", view->name);
1605 } else {
1606 INSIST(pview->cache != NULL);
1607 isc_log_write(ns_g_lctx,
1608 NS_LOGCATEGORY_GENERAL,
1609 NS_LOGMODULE_SERVER,
1610 ISC_LOG_DEBUG(3),
1611 "reusing existing cache");
1612 reused_cache = ISC_TRUE;
1613 dns_cache_attach(pview->cache, &cache);
1615 dns_view_getresstats(pview, &resstats);
1616 dns_view_getresquerystats(pview,
1617 &resquerystats);
1618 dns_view_detach(&pview);
1621 if (cache == NULL) {
1623 * Create a cache with the desired name. This normally
1624 * equals the view name, but may also be a forward
1625 * reference to a view that share the cache with this
1626 * view but is not yet configured. If it is not the
1627 * view name but not a forward reference either, then it
1628 * is simply a named cache that is not shared.
1630 CHECK(isc_mem_create(0, 0, &cmctx));
1631 isc_mem_setname(cmctx, "cache", NULL);
1632 CHECK(dns_cache_create2(cmctx, ns_g_taskmgr,
1633 ns_g_timermgr, view->rdclass,
1634 cachename, "rbt", 0, NULL,
1635 &cache));
1637 nsc = isc_mem_get(mctx, sizeof(*nsc));
1638 if (nsc == NULL) {
1639 result = ISC_R_NOMEMORY;
1640 goto cleanup;
1642 nsc->cache = NULL;
1643 dns_cache_attach(cache, &nsc->cache);
1644 nsc->primaryview = view;
1645 nsc->needflush = ISC_FALSE;
1646 nsc->adbsizeadjusted = ISC_FALSE;
1647 ISC_LINK_INIT(nsc, link);
1648 ISC_LIST_APPEND(*cachelist, nsc, link);
1650 dns_view_setcache2(view, cache, shared_cache);
1653 * cache-file cannot be inherited if views are present, but this
1654 * should be caught by the configuration checking stage.
1656 obj = NULL;
1657 result = ns_config_get(maps, "cache-file", &obj);
1658 if (result == ISC_R_SUCCESS && strcmp(view->name, "_bind") != 0) {
1659 CHECK(dns_cache_setfilename(cache, cfg_obj_asstring(obj)));
1660 if (!reused_cache && !shared_cache)
1661 CHECK(dns_cache_load(cache));
1664 dns_cache_setcleaninginterval(cache, cleaning_interval);
1665 dns_cache_setcachesize(cache, max_cache_size);
1667 dns_cache_detach(&cache);
1670 * Resolver.
1672 * XXXRTH Hardwired number of tasks.
1674 CHECK(get_view_querysource_dispatch(maps, AF_INET, &dispatch4,
1675 ISC_TF(ISC_LIST_PREV(view, link)
1676 == NULL)));
1677 CHECK(get_view_querysource_dispatch(maps, AF_INET6, &dispatch6,
1678 ISC_TF(ISC_LIST_PREV(view, link)
1679 == NULL)));
1680 if (dispatch4 == NULL && dispatch6 == NULL) {
1681 UNEXPECTED_ERROR(__FILE__, __LINE__,
1682 "unable to obtain neither an IPv4 nor"
1683 " an IPv6 dispatch");
1684 result = ISC_R_UNEXPECTED;
1685 goto cleanup;
1687 CHECK(dns_view_createresolver(view, ns_g_taskmgr, 31,
1688 ns_g_socketmgr, ns_g_timermgr,
1689 resopts, ns_g_dispatchmgr,
1690 dispatch4, dispatch6));
1692 if (resstats == NULL) {
1693 CHECK(isc_stats_create(mctx, &resstats,
1694 dns_resstatscounter_max));
1696 dns_view_setresstats(view, resstats);
1697 if (resquerystats == NULL)
1698 CHECK(dns_rdatatypestats_create(mctx, &resquerystats));
1699 dns_view_setresquerystats(view, resquerystats);
1702 * Set the ADB cache size to 1/8th of the max-cache-size or
1703 * MAX_ADB_SIZE_FOR_CACHESHARE when the cache is shared.
1705 max_adb_size = 0;
1706 if (max_cache_size != 0) {
1707 max_adb_size = max_cache_size / 8;
1708 if (max_adb_size == 0)
1709 max_adb_size = 1; /* Force minimum. */
1710 if (view != nsc->primaryview &&
1711 max_adb_size > MAX_ADB_SIZE_FOR_CACHESHARE) {
1712 max_adb_size = MAX_ADB_SIZE_FOR_CACHESHARE;
1713 if (!nsc->adbsizeadjusted) {
1714 dns_adb_setadbsize(nsc->primaryview->adb,
1715 MAX_ADB_SIZE_FOR_CACHESHARE);
1716 nsc->adbsizeadjusted = ISC_TRUE;
1720 dns_adb_setadbsize(view->adb, max_adb_size);
1723 * Set resolver's lame-ttl.
1725 obj = NULL;
1726 result = ns_config_get(maps, "lame-ttl", &obj);
1727 INSIST(result == ISC_R_SUCCESS);
1728 lame_ttl = cfg_obj_asuint32(obj);
1729 if (lame_ttl > 1800)
1730 lame_ttl = 1800;
1731 dns_resolver_setlamettl(view->resolver, lame_ttl);
1733 /* Specify whether to use 0-TTL for negative response for SOA query */
1734 dns_resolver_setzeronosoattl(view->resolver, zero_no_soattl);
1737 * Set the resolver's EDNS UDP size.
1739 obj = NULL;
1740 result = ns_config_get(maps, "edns-udp-size", &obj);
1741 INSIST(result == ISC_R_SUCCESS);
1742 udpsize = cfg_obj_asuint32(obj);
1743 if (udpsize < 512)
1744 udpsize = 512;
1745 if (udpsize > 4096)
1746 udpsize = 4096;
1747 dns_resolver_setudpsize(view->resolver, (isc_uint16_t)udpsize);
1750 * Set the maximum UDP response size.
1752 obj = NULL;
1753 result = ns_config_get(maps, "max-udp-size", &obj);
1754 INSIST(result == ISC_R_SUCCESS);
1755 udpsize = cfg_obj_asuint32(obj);
1756 if (udpsize < 512)
1757 udpsize = 512;
1758 if (udpsize > 4096)
1759 udpsize = 4096;
1760 view->maxudp = udpsize;
1763 * Set supported DNSSEC algorithms.
1765 dns_resolver_reset_algorithms(view->resolver);
1766 disabled = NULL;
1767 (void)ns_config_get(maps, "disable-algorithms", &disabled);
1768 if (disabled != NULL) {
1769 for (element = cfg_list_first(disabled);
1770 element != NULL;
1771 element = cfg_list_next(element))
1772 CHECK(disable_algorithms(cfg_listelt_value(element),
1773 view->resolver));
1777 * A global or view "forwarders" option, if present,
1778 * creates an entry for "." in the forwarding table.
1780 forwardtype = NULL;
1781 forwarders = NULL;
1782 (void)ns_config_get(maps, "forward", &forwardtype);
1783 (void)ns_config_get(maps, "forwarders", &forwarders);
1784 if (forwarders != NULL)
1785 CHECK(configure_forward(config, view, dns_rootname,
1786 forwarders, forwardtype));
1789 * Dual Stack Servers.
1791 alternates = NULL;
1792 (void)ns_config_get(maps, "dual-stack-servers", &alternates);
1793 if (alternates != NULL)
1794 CHECK(configure_alternates(config, view, alternates));
1797 * We have default hints for class IN if we need them.
1799 if (view->rdclass == dns_rdataclass_in && view->hints == NULL)
1800 dns_view_sethints(view, ns_g_server->in_roothints);
1803 * If we still have no hints, this is a non-IN view with no
1804 * "hints zone" configured. Issue a warning, except if this
1805 * is a root server. Root servers never need to consult
1806 * their hints, so it's no point requiring users to configure
1807 * them.
1809 if (view->hints == NULL) {
1810 dns_zone_t *rootzone = NULL;
1811 (void)dns_view_findzone(view, dns_rootname, &rootzone);
1812 if (rootzone != NULL) {
1813 dns_zone_detach(&rootzone);
1814 need_hints = ISC_FALSE;
1816 if (need_hints)
1817 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1818 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
1819 "no root hints for view '%s'",
1820 view->name);
1824 * Configure the view's TSIG keys.
1826 CHECK(ns_tsigkeyring_fromconfig(config, vconfig, view->mctx, &ring));
1827 if (ns_g_server->sessionkey != NULL) {
1828 CHECK(dns_tsigkeyring_add(ring, ns_g_server->session_keyname,
1829 ns_g_server->sessionkey));
1831 dns_view_setkeyring(view, ring);
1832 ring = NULL; /* ownership transferred */
1835 * Configure the view's peer list.
1838 const cfg_obj_t *peers = NULL;
1839 const cfg_listelt_t *element;
1840 dns_peerlist_t *newpeers = NULL;
1842 (void)ns_config_get(cfgmaps, "server", &peers);
1843 CHECK(dns_peerlist_new(mctx, &newpeers));
1844 for (element = cfg_list_first(peers);
1845 element != NULL;
1846 element = cfg_list_next(element))
1848 const cfg_obj_t *cpeer = cfg_listelt_value(element);
1849 dns_peer_t *peer;
1851 CHECK(configure_peer(cpeer, mctx, &peer));
1852 dns_peerlist_addpeer(newpeers, peer);
1853 dns_peer_detach(&peer);
1855 dns_peerlist_detach(&view->peers);
1856 view->peers = newpeers; /* Transfer ownership. */
1860 * Configure the views rrset-order.
1863 const cfg_obj_t *rrsetorder = NULL;
1864 const cfg_listelt_t *element;
1866 (void)ns_config_get(maps, "rrset-order", &rrsetorder);
1867 CHECK(dns_order_create(mctx, &order));
1868 for (element = cfg_list_first(rrsetorder);
1869 element != NULL;
1870 element = cfg_list_next(element))
1872 const cfg_obj_t *ent = cfg_listelt_value(element);
1874 CHECK(configure_order(order, ent));
1876 if (view->order != NULL)
1877 dns_order_detach(&view->order);
1878 dns_order_attach(order, &view->order);
1879 dns_order_detach(&order);
1882 * Copy the aclenv object.
1884 dns_aclenv_copy(&view->aclenv, &ns_g_server->aclenv);
1887 * Configure the "match-clients" and "match-destinations" ACL.
1889 CHECK(configure_view_acl(vconfig, config, "match-clients", NULL, actx,
1890 ns_g_mctx, &view->matchclients));
1891 CHECK(configure_view_acl(vconfig, config, "match-destinations", NULL,
1892 actx, ns_g_mctx, &view->matchdestinations));
1895 * Configure the "match-recursive-only" option.
1897 obj = NULL;
1898 (void)ns_config_get(maps, "match-recursive-only", &obj);
1899 if (obj != NULL && cfg_obj_asboolean(obj))
1900 view->matchrecursiveonly = ISC_TRUE;
1901 else
1902 view->matchrecursiveonly = ISC_FALSE;
1905 * Configure other configurable data.
1907 obj = NULL;
1908 result = ns_config_get(maps, "recursion", &obj);
1909 INSIST(result == ISC_R_SUCCESS);
1910 view->recursion = cfg_obj_asboolean(obj);
1912 obj = NULL;
1913 result = ns_config_get(maps, "auth-nxdomain", &obj);
1914 INSIST(result == ISC_R_SUCCESS);
1915 view->auth_nxdomain = cfg_obj_asboolean(obj);
1917 obj = NULL;
1918 result = ns_config_get(maps, "minimal-responses", &obj);
1919 INSIST(result == ISC_R_SUCCESS);
1920 view->minimalresponses = cfg_obj_asboolean(obj);
1922 obj = NULL;
1923 result = ns_config_get(maps, "transfer-format", &obj);
1924 INSIST(result == ISC_R_SUCCESS);
1925 str = cfg_obj_asstring(obj);
1926 if (strcasecmp(str, "many-answers") == 0)
1927 view->transfer_format = dns_many_answers;
1928 else if (strcasecmp(str, "one-answer") == 0)
1929 view->transfer_format = dns_one_answer;
1930 else
1931 INSIST(0);
1934 * Set sources where additional data and CNAME/DNAME
1935 * targets for authoritative answers may be found.
1937 obj = NULL;
1938 result = ns_config_get(maps, "additional-from-auth", &obj);
1939 INSIST(result == ISC_R_SUCCESS);
1940 view->additionalfromauth = cfg_obj_asboolean(obj);
1941 if (view->recursion && ! view->additionalfromauth) {
1942 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_WARNING,
1943 "'additional-from-auth no' is only supported "
1944 "with 'recursion no'");
1945 view->additionalfromauth = ISC_TRUE;
1948 obj = NULL;
1949 result = ns_config_get(maps, "additional-from-cache", &obj);
1950 INSIST(result == ISC_R_SUCCESS);
1951 view->additionalfromcache = cfg_obj_asboolean(obj);
1952 if (view->recursion && ! view->additionalfromcache) {
1953 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_WARNING,
1954 "'additional-from-cache no' is only supported "
1955 "with 'recursion no'");
1956 view->additionalfromcache = ISC_TRUE;
1960 * Set "allow-query-cache", "allow-query-cache-on",
1961 * "allow-recursion", and "allow-recursion-on" acls if
1962 * configured in named.conf.
1964 CHECK(configure_view_acl(vconfig, config, "allow-query-cache", NULL,
1965 actx, ns_g_mctx, &view->queryacl));
1966 CHECK(configure_view_acl(vconfig, config, "allow-query-cache-on", NULL,
1967 actx, ns_g_mctx, &view->queryonacl));
1968 if (view->queryonacl == NULL)
1969 CHECK(configure_view_acl(NULL, ns_g_config,
1970 "allow-query-cache-on", NULL, actx,
1971 ns_g_mctx, &view->queryonacl));
1972 if (strcmp(view->name, "_bind") != 0) {
1973 CHECK(configure_view_acl(vconfig, config, "allow-recursion",
1974 NULL, actx, ns_g_mctx,
1975 &view->recursionacl));
1976 CHECK(configure_view_acl(vconfig, config, "allow-recursion-on",
1977 NULL, actx, ns_g_mctx,
1978 &view->recursiononacl));
1982 * "allow-query-cache" inherits from "allow-recursion" if set,
1983 * otherwise from "allow-query" if set.
1984 * "allow-recursion" inherits from "allow-query-cache" if set,
1985 * otherwise from "allow-query" if set.
1987 if (view->queryacl == NULL && view->recursionacl != NULL)
1988 dns_acl_attach(view->recursionacl, &view->queryacl);
1989 if (view->queryacl == NULL && view->recursion)
1990 CHECK(configure_view_acl(vconfig, config, "allow-query", NULL,
1991 actx, ns_g_mctx, &view->queryacl));
1992 if (view->recursion &&
1993 view->recursionacl == NULL && view->queryacl != NULL)
1994 dns_acl_attach(view->queryacl, &view->recursionacl);
1997 * Set default "allow-recursion", "allow-recursion-on" and
1998 * "allow-query-cache" acls.
2000 if (view->recursionacl == NULL && view->recursion)
2001 CHECK(configure_view_acl(NULL, ns_g_config,
2002 "allow-recursion", NULL,
2003 actx, ns_g_mctx,
2004 &view->recursionacl));
2005 if (view->recursiononacl == NULL && view->recursion)
2006 CHECK(configure_view_acl(NULL, ns_g_config,
2007 "allow-recursion-on", NULL,
2008 actx, ns_g_mctx,
2009 &view->recursiononacl));
2010 if (view->queryacl == NULL) {
2011 if (view->recursion)
2012 CHECK(configure_view_acl(NULL, ns_g_config,
2013 "allow-query-cache", NULL,
2014 actx, ns_g_mctx,
2015 &view->queryacl));
2016 else {
2017 if (view->queryacl != NULL)
2018 dns_acl_detach(&view->queryacl);
2019 CHECK(dns_acl_none(ns_g_mctx, &view->queryacl));
2024 * Filter setting on addresses in the answer section.
2026 CHECK(configure_view_acl(vconfig, config, "deny-answer-addresses",
2027 "acl", actx, ns_g_mctx, &view->denyansweracl));
2028 CHECK(configure_view_nametable(vconfig, config, "deny-answer-addresses",
2029 "except-from", ns_g_mctx,
2030 &view->answeracl_exclude));
2033 * Filter setting on names (CNAME/DNAME targets) in the answer section.
2035 CHECK(configure_view_nametable(vconfig, config, "deny-answer-aliases",
2036 "name", ns_g_mctx,
2037 &view->denyanswernames));
2038 CHECK(configure_view_nametable(vconfig, config, "deny-answer-aliases",
2039 "except-from", ns_g_mctx,
2040 &view->answernames_exclude));
2043 * Configure sortlist, if set
2045 CHECK(configure_view_sortlist(vconfig, config, actx, ns_g_mctx,
2046 &view->sortlist));
2049 * Configure default allow-transfer, allow-notify, allow-update
2050 * and allow-update-forwarding ACLs, if set, so they can be
2051 * inherited by zones.
2053 if (view->notifyacl == NULL)
2054 CHECK(configure_view_acl(NULL, ns_g_config,
2055 "allow-notify", NULL, actx,
2056 ns_g_mctx, &view->notifyacl));
2057 if (view->transferacl == NULL)
2058 CHECK(configure_view_acl(NULL, ns_g_config,
2059 "allow-transfer", NULL, actx,
2060 ns_g_mctx, &view->transferacl));
2061 if (view->updateacl == NULL)
2062 CHECK(configure_view_acl(NULL, ns_g_config,
2063 "allow-update", NULL, actx,
2064 ns_g_mctx, &view->updateacl));
2065 if (view->upfwdacl == NULL)
2066 CHECK(configure_view_acl(NULL, ns_g_config,
2067 "allow-update-forwarding", NULL, actx,
2068 ns_g_mctx, &view->upfwdacl));
2070 obj = NULL;
2071 result = ns_config_get(maps, "request-ixfr", &obj);
2072 INSIST(result == ISC_R_SUCCESS);
2073 view->requestixfr = cfg_obj_asboolean(obj);
2075 obj = NULL;
2076 result = ns_config_get(maps, "provide-ixfr", &obj);
2077 INSIST(result == ISC_R_SUCCESS);
2078 view->provideixfr = cfg_obj_asboolean(obj);
2080 obj = NULL;
2081 result = ns_config_get(maps, "request-nsid", &obj);
2082 INSIST(result == ISC_R_SUCCESS);
2083 view->requestnsid = cfg_obj_asboolean(obj);
2085 obj = NULL;
2086 result = ns_config_get(maps, "max-clients-per-query", &obj);
2087 INSIST(result == ISC_R_SUCCESS);
2088 max_clients_per_query = cfg_obj_asuint32(obj);
2090 obj = NULL;
2091 result = ns_config_get(maps, "clients-per-query", &obj);
2092 INSIST(result == ISC_R_SUCCESS);
2093 dns_resolver_setclientsperquery(view->resolver,
2094 cfg_obj_asuint32(obj),
2095 max_clients_per_query);
2097 #ifdef ALLOW_FILTER_AAAA_ON_V4
2098 obj = NULL;
2099 result = ns_config_get(maps, "filter-aaaa-on-v4", &obj);
2100 INSIST(result == ISC_R_SUCCESS);
2101 if (cfg_obj_isboolean(obj)) {
2102 if (cfg_obj_asboolean(obj))
2103 view->v4_aaaa = dns_v4_aaaa_filter;
2104 else
2105 view->v4_aaaa = dns_v4_aaaa_ok;
2106 } else {
2107 const char *v4_aaaastr = cfg_obj_asstring(obj);
2108 if (strcasecmp(v4_aaaastr, "break-dnssec") == 0)
2109 view->v4_aaaa = dns_v4_aaaa_break_dnssec;
2110 else
2111 INSIST(0);
2114 #endif
2115 obj = NULL;
2116 result = ns_config_get(maps, "dnssec-enable", &obj);
2117 INSIST(result == ISC_R_SUCCESS);
2118 view->enablednssec = cfg_obj_asboolean(obj);
2120 obj = NULL;
2121 result = ns_config_get(optionmaps, "dnssec-lookaside", &obj);
2122 if (result == ISC_R_SUCCESS) {
2123 /* If set to "auto", use the version from the defaults */
2124 const cfg_obj_t *dlvobj;
2125 dlvobj = cfg_listelt_value(cfg_list_first(obj));
2126 if (!strcmp(cfg_obj_asstring(cfg_tuple_get(dlvobj, "domain")),
2127 "auto") &&
2128 cfg_obj_isvoid(cfg_tuple_get(dlvobj, "trust-anchor"))) {
2129 auto_dlv = ISC_TRUE;
2130 obj = NULL;
2131 result = cfg_map_get(ns_g_defaults,
2132 "dnssec-lookaside", &obj);
2136 if (result == ISC_R_SUCCESS) {
2137 for (element = cfg_list_first(obj);
2138 element != NULL;
2139 element = cfg_list_next(element))
2141 const char *str;
2142 isc_buffer_t b;
2143 dns_name_t *dlv;
2145 obj = cfg_listelt_value(element);
2146 #if 0
2147 dns_fixedname_t fixed;
2148 dns_name_t *name;
2151 * When we support multiple dnssec-lookaside
2152 * entries this is how to find the domain to be
2153 * checked. XXXMPA
2155 dns_fixedname_init(&fixed);
2156 name = dns_fixedname_name(&fixed);
2157 str = cfg_obj_asstring(cfg_tuple_get(obj,
2158 "domain"));
2159 isc_buffer_init(&b, str, strlen(str));
2160 isc_buffer_add(&b, strlen(str));
2161 CHECK(dns_name_fromtext(name, &b, dns_rootname,
2162 0, NULL));
2163 #endif
2164 str = cfg_obj_asstring(cfg_tuple_get(obj,
2165 "trust-anchor"));
2166 isc_buffer_init(&b, str, strlen(str));
2167 isc_buffer_add(&b, strlen(str));
2168 dlv = dns_fixedname_name(&view->dlv_fixed);
2169 CHECK(dns_name_fromtext(dlv, &b, dns_rootname,
2170 DNS_NAME_DOWNCASE, NULL));
2171 view->dlv = dns_fixedname_name(&view->dlv_fixed);
2173 } else
2174 view->dlv = NULL;
2177 * For now, there is only one kind of trusted keys, the
2178 * "security roots".
2180 CHECK(configure_view_dnsseckeys(view, vconfig, config, bindkeys,
2181 auto_dlv, mctx));
2182 dns_resolver_resetmustbesecure(view->resolver);
2183 obj = NULL;
2184 result = ns_config_get(maps, "dnssec-must-be-secure", &obj);
2185 if (result == ISC_R_SUCCESS)
2186 CHECK(mustbesecure(obj, view->resolver));
2188 obj = NULL;
2189 result = ns_config_get(maps, "preferred-glue", &obj);
2190 if (result == ISC_R_SUCCESS) {
2191 str = cfg_obj_asstring(obj);
2192 if (strcasecmp(str, "a") == 0)
2193 view->preferred_glue = dns_rdatatype_a;
2194 else if (strcasecmp(str, "aaaa") == 0)
2195 view->preferred_glue = dns_rdatatype_aaaa;
2196 else
2197 view->preferred_glue = 0;
2198 } else
2199 view->preferred_glue = 0;
2201 obj = NULL;
2202 result = ns_config_get(maps, "root-delegation-only", &obj);
2203 if (result == ISC_R_SUCCESS) {
2204 dns_view_setrootdelonly(view, ISC_TRUE);
2205 if (!cfg_obj_isvoid(obj)) {
2206 dns_fixedname_t fixed;
2207 dns_name_t *name;
2208 isc_buffer_t b;
2209 const char *str;
2210 const cfg_obj_t *exclude;
2212 dns_fixedname_init(&fixed);
2213 name = dns_fixedname_name(&fixed);
2214 for (element = cfg_list_first(obj);
2215 element != NULL;
2216 element = cfg_list_next(element)) {
2217 exclude = cfg_listelt_value(element);
2218 str = cfg_obj_asstring(exclude);
2219 isc_buffer_init(&b, str, strlen(str));
2220 isc_buffer_add(&b, strlen(str));
2221 CHECK(dns_name_fromtext(name, &b, dns_rootname,
2222 0, NULL));
2223 CHECK(dns_view_excludedelegationonly(view,
2224 name));
2227 } else
2228 dns_view_setrootdelonly(view, ISC_FALSE);
2231 * Setup automatic empty zones. If recursion is off then
2232 * they are disabled by default.
2234 obj = NULL;
2235 (void)ns_config_get(maps, "empty-zones-enable", &obj);
2236 (void)ns_config_get(maps, "disable-empty-zone", &disablelist);
2237 if (obj == NULL && disablelist == NULL &&
2238 view->rdclass == dns_rdataclass_in) {
2239 rfc1918 = ISC_FALSE;
2240 empty_zones_enable = view->recursion;
2241 } else if (view->rdclass == dns_rdataclass_in) {
2242 rfc1918 = ISC_TRUE;
2243 if (obj != NULL)
2244 empty_zones_enable = cfg_obj_asboolean(obj);
2245 else
2246 empty_zones_enable = view->recursion;
2247 } else {
2248 rfc1918 = ISC_FALSE;
2249 empty_zones_enable = ISC_FALSE;
2251 if (empty_zones_enable) {
2252 const char *empty;
2253 int empty_zone = 0;
2254 dns_fixedname_t fixed;
2255 dns_name_t *name;
2256 isc_buffer_t buffer;
2257 const char *str;
2258 char server[DNS_NAME_FORMATSIZE + 1];
2259 char contact[DNS_NAME_FORMATSIZE + 1];
2260 isc_boolean_t logit;
2261 const char *empty_dbtype[4] =
2262 { "_builtin", "empty", NULL, NULL };
2263 int empty_dbtypec = 4;
2264 isc_boolean_t zonestats_on;
2266 dns_fixedname_init(&fixed);
2267 name = dns_fixedname_name(&fixed);
2269 obj = NULL;
2270 result = ns_config_get(maps, "empty-server", &obj);
2271 if (result == ISC_R_SUCCESS) {
2272 str = cfg_obj_asstring(obj);
2273 isc_buffer_init(&buffer, str, strlen(str));
2274 isc_buffer_add(&buffer, strlen(str));
2275 CHECK(dns_name_fromtext(name, &buffer, dns_rootname, 0,
2276 NULL));
2277 isc_buffer_init(&buffer, server, sizeof(server) - 1);
2278 CHECK(dns_name_totext(name, ISC_FALSE, &buffer));
2279 server[isc_buffer_usedlength(&buffer)] = 0;
2280 empty_dbtype[2] = server;
2281 } else
2282 empty_dbtype[2] = "@";
2284 obj = NULL;
2285 result = ns_config_get(maps, "empty-contact", &obj);
2286 if (result == ISC_R_SUCCESS) {
2287 str = cfg_obj_asstring(obj);
2288 isc_buffer_init(&buffer, str, strlen(str));
2289 isc_buffer_add(&buffer, strlen(str));
2290 CHECK(dns_name_fromtext(name, &buffer, dns_rootname, 0,
2291 NULL));
2292 isc_buffer_init(&buffer, contact, sizeof(contact) - 1);
2293 CHECK(dns_name_totext(name, ISC_FALSE, &buffer));
2294 contact[isc_buffer_usedlength(&buffer)] = 0;
2295 empty_dbtype[3] = contact;
2296 } else
2297 empty_dbtype[3] = ".";
2299 obj = NULL;
2300 result = ns_config_get(maps, "zone-statistics", &obj);
2301 INSIST(result == ISC_R_SUCCESS);
2302 zonestats_on = cfg_obj_asboolean(obj);
2304 logit = ISC_TRUE;
2305 for (empty = empty_zones[empty_zone].zone;
2306 empty != NULL;
2307 empty = empty_zones[++empty_zone].zone)
2309 dns_forwarders_t *forwarders = NULL;
2310 dns_view_t *pview = NULL;
2312 isc_buffer_init(&buffer, empty, strlen(empty));
2313 isc_buffer_add(&buffer, strlen(empty));
2315 * Look for zone on drop list.
2317 CHECK(dns_name_fromtext(name, &buffer, dns_rootname, 0,
2318 NULL));
2319 if (disablelist != NULL &&
2320 on_disable_list(disablelist, name))
2321 continue;
2324 * This zone already exists.
2326 (void)dns_view_findzone(view, name, &zone);
2327 if (zone != NULL) {
2328 CHECK(setquerystats(zone, mctx, zonestats_on));
2329 dns_zone_detach(&zone);
2330 continue;
2334 * If we would forward this name don't add a
2335 * empty zone for it.
2337 result = dns_fwdtable_find(view->fwdtable, name,
2338 &forwarders);
2339 if (result == ISC_R_SUCCESS &&
2340 forwarders->fwdpolicy == dns_fwdpolicy_only)
2341 continue;
2343 if (!rfc1918 && empty_zones[empty_zone].rfc1918) {
2344 if (logit) {
2345 isc_log_write(ns_g_lctx,
2346 NS_LOGCATEGORY_GENERAL,
2347 NS_LOGMODULE_SERVER,
2348 ISC_LOG_WARNING,
2349 "Warning%s%s: "
2350 "'empty-zones-enable/"
2351 "disable-empty-zone' "
2352 "not set: disabling "
2353 "RFC 1918 empty zones",
2354 sep, viewname);
2355 logit = ISC_FALSE;
2357 continue;
2361 * See if we can re-use a existing zone.
2363 result = dns_viewlist_find(&ns_g_server->viewlist,
2364 view->name, view->rdclass,
2365 &pview);
2366 if (result != ISC_R_NOTFOUND &&
2367 result != ISC_R_SUCCESS)
2368 goto cleanup;
2370 if (pview != NULL) {
2371 (void)dns_view_findzone(pview, name, &zone);
2372 dns_view_detach(&pview);
2373 if (zone != NULL)
2374 check_dbtype(&zone, empty_dbtypec,
2375 empty_dbtype, mctx);
2376 if (zone != NULL) {
2377 dns_zone_setview(zone, view);
2378 CHECK(dns_view_addzone(view, zone));
2379 CHECK(setquerystats(zone, mctx,
2380 zonestats_on));
2381 dns_zone_detach(&zone);
2382 continue;
2386 CHECK(dns_zone_create(&zone, mctx));
2387 CHECK(dns_zone_setorigin(zone, name));
2388 dns_zone_setview(zone, view);
2389 CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr, zone));
2390 dns_zone_setclass(zone, view->rdclass);
2391 dns_zone_settype(zone, dns_zone_master);
2392 dns_zone_setstats(zone, ns_g_server->zonestats);
2393 CHECK(dns_zone_setdbtype(zone, empty_dbtypec,
2394 empty_dbtype));
2395 if (view->queryacl != NULL)
2396 dns_zone_setqueryacl(zone, view->queryacl);
2397 if (view->queryonacl != NULL)
2398 dns_zone_setqueryonacl(zone, view->queryonacl);
2399 dns_zone_setdialup(zone, dns_dialuptype_no);
2400 dns_zone_setnotifytype(zone, dns_notifytype_no);
2401 dns_zone_setoption(zone, DNS_ZONEOPT_NOCHECKNS,
2402 ISC_TRUE);
2403 CHECK(setquerystats(zone, mctx, zonestats_on));
2404 CHECK(dns_view_addzone(view, zone));
2405 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2406 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
2407 "automatic empty zone%s%s: %s",
2408 sep, viewname, empty);
2409 dns_zone_detach(&zone);
2413 result = ISC_R_SUCCESS;
2415 cleanup:
2416 if (ring != NULL)
2417 dns_tsigkeyring_destroy(&ring);
2418 if (zone != NULL)
2419 dns_zone_detach(&zone);
2420 if (dispatch4 != NULL)
2421 dns_dispatch_detach(&dispatch4);
2422 if (dispatch6 != NULL)
2423 dns_dispatch_detach(&dispatch6);
2424 if (resstats != NULL)
2425 isc_stats_detach(&resstats);
2426 if (resquerystats != NULL)
2427 dns_stats_detach(&resquerystats);
2428 if (order != NULL)
2429 dns_order_detach(&order);
2430 if (cmctx != NULL)
2431 isc_mem_detach(&cmctx);
2433 if (cache != NULL)
2434 dns_cache_detach(&cache);
2436 return (result);
2439 static isc_result_t
2440 configure_hints(dns_view_t *view, const char *filename) {
2441 isc_result_t result;
2442 dns_db_t *db;
2444 db = NULL;
2445 result = dns_rootns_create(view->mctx, view->rdclass, filename, &db);
2446 if (result == ISC_R_SUCCESS) {
2447 dns_view_sethints(view, db);
2448 dns_db_detach(&db);
2451 return (result);
2454 static isc_result_t
2455 configure_alternates(const cfg_obj_t *config, dns_view_t *view,
2456 const cfg_obj_t *alternates)
2458 const cfg_obj_t *portobj;
2459 const cfg_obj_t *addresses;
2460 const cfg_listelt_t *element;
2461 isc_result_t result = ISC_R_SUCCESS;
2462 in_port_t port;
2465 * Determine which port to send requests to.
2467 if (ns_g_lwresdonly && ns_g_port != 0)
2468 port = ns_g_port;
2469 else
2470 CHECKM(ns_config_getport(config, &port), "port");
2472 if (alternates != NULL) {
2473 portobj = cfg_tuple_get(alternates, "port");
2474 if (cfg_obj_isuint32(portobj)) {
2475 isc_uint32_t val = cfg_obj_asuint32(portobj);
2476 if (val > ISC_UINT16_MAX) {
2477 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
2478 "port '%u' out of range", val);
2479 return (ISC_R_RANGE);
2481 port = (in_port_t) val;
2485 addresses = NULL;
2486 if (alternates != NULL)
2487 addresses = cfg_tuple_get(alternates, "addresses");
2489 for (element = cfg_list_first(addresses);
2490 element != NULL;
2491 element = cfg_list_next(element))
2493 const cfg_obj_t *alternate = cfg_listelt_value(element);
2494 isc_sockaddr_t sa;
2496 if (!cfg_obj_issockaddr(alternate)) {
2497 dns_fixedname_t fixed;
2498 dns_name_t *name;
2499 const char *str = cfg_obj_asstring(cfg_tuple_get(
2500 alternate, "name"));
2501 isc_buffer_t buffer;
2502 in_port_t myport = port;
2504 isc_buffer_init(&buffer, str, strlen(str));
2505 isc_buffer_add(&buffer, strlen(str));
2506 dns_fixedname_init(&fixed);
2507 name = dns_fixedname_name(&fixed);
2508 CHECK(dns_name_fromtext(name, &buffer, dns_rootname, 0,
2509 NULL));
2511 portobj = cfg_tuple_get(alternate, "port");
2512 if (cfg_obj_isuint32(portobj)) {
2513 isc_uint32_t val = cfg_obj_asuint32(portobj);
2514 if (val > ISC_UINT16_MAX) {
2515 cfg_obj_log(portobj, ns_g_lctx,
2516 ISC_LOG_ERROR,
2517 "port '%u' out of range",
2518 val);
2519 return (ISC_R_RANGE);
2521 myport = (in_port_t) val;
2523 CHECK(dns_resolver_addalternate(view->resolver, NULL,
2524 name, myport));
2525 continue;
2528 sa = *cfg_obj_assockaddr(alternate);
2529 if (isc_sockaddr_getport(&sa) == 0)
2530 isc_sockaddr_setport(&sa, port);
2531 CHECK(dns_resolver_addalternate(view->resolver, &sa,
2532 NULL, 0));
2535 cleanup:
2536 return (result);
2539 static isc_result_t
2540 configure_forward(const cfg_obj_t *config, dns_view_t *view, dns_name_t *origin,
2541 const cfg_obj_t *forwarders, const cfg_obj_t *forwardtype)
2543 const cfg_obj_t *portobj;
2544 const cfg_obj_t *faddresses;
2545 const cfg_listelt_t *element;
2546 dns_fwdpolicy_t fwdpolicy = dns_fwdpolicy_none;
2547 isc_sockaddrlist_t addresses;
2548 isc_sockaddr_t *sa;
2549 isc_result_t result;
2550 in_port_t port;
2552 ISC_LIST_INIT(addresses);
2555 * Determine which port to send forwarded requests to.
2557 if (ns_g_lwresdonly && ns_g_port != 0)
2558 port = ns_g_port;
2559 else
2560 CHECKM(ns_config_getport(config, &port), "port");
2562 if (forwarders != NULL) {
2563 portobj = cfg_tuple_get(forwarders, "port");
2564 if (cfg_obj_isuint32(portobj)) {
2565 isc_uint32_t val = cfg_obj_asuint32(portobj);
2566 if (val > ISC_UINT16_MAX) {
2567 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
2568 "port '%u' out of range", val);
2569 return (ISC_R_RANGE);
2571 port = (in_port_t) val;
2575 faddresses = NULL;
2576 if (forwarders != NULL)
2577 faddresses = cfg_tuple_get(forwarders, "addresses");
2579 for (element = cfg_list_first(faddresses);
2580 element != NULL;
2581 element = cfg_list_next(element))
2583 const cfg_obj_t *forwarder = cfg_listelt_value(element);
2584 sa = isc_mem_get(view->mctx, sizeof(isc_sockaddr_t));
2585 if (sa == NULL) {
2586 result = ISC_R_NOMEMORY;
2587 goto cleanup;
2589 *sa = *cfg_obj_assockaddr(forwarder);
2590 if (isc_sockaddr_getport(sa) == 0)
2591 isc_sockaddr_setport(sa, port);
2592 ISC_LINK_INIT(sa, link);
2593 ISC_LIST_APPEND(addresses, sa, link);
2596 if (ISC_LIST_EMPTY(addresses)) {
2597 if (forwardtype != NULL)
2598 cfg_obj_log(forwarders, ns_g_lctx, ISC_LOG_WARNING,
2599 "no forwarders seen; disabling "
2600 "forwarding");
2601 fwdpolicy = dns_fwdpolicy_none;
2602 } else {
2603 if (forwardtype == NULL)
2604 fwdpolicy = dns_fwdpolicy_first;
2605 else {
2606 const char *forwardstr = cfg_obj_asstring(forwardtype);
2607 if (strcasecmp(forwardstr, "first") == 0)
2608 fwdpolicy = dns_fwdpolicy_first;
2609 else if (strcasecmp(forwardstr, "only") == 0)
2610 fwdpolicy = dns_fwdpolicy_only;
2611 else
2612 INSIST(0);
2616 result = dns_fwdtable_add(view->fwdtable, origin, &addresses,
2617 fwdpolicy);
2618 if (result != ISC_R_SUCCESS) {
2619 char namebuf[DNS_NAME_FORMATSIZE];
2620 dns_name_format(origin, namebuf, sizeof(namebuf));
2621 cfg_obj_log(forwarders, ns_g_lctx, ISC_LOG_WARNING,
2622 "could not set up forwarding for domain '%s': %s",
2623 namebuf, isc_result_totext(result));
2624 goto cleanup;
2627 result = ISC_R_SUCCESS;
2629 cleanup:
2631 while (!ISC_LIST_EMPTY(addresses)) {
2632 sa = ISC_LIST_HEAD(addresses);
2633 ISC_LIST_UNLINK(addresses, sa, link);
2634 isc_mem_put(view->mctx, sa, sizeof(isc_sockaddr_t));
2637 return (result);
2641 * Create a new view and add it to the list.
2643 * If 'vconfig' is NULL, create the default view.
2645 * The view created is attached to '*viewp'.
2647 static isc_result_t
2648 create_view(const cfg_obj_t *vconfig, dns_viewlist_t *viewlist,
2649 dns_view_t **viewp)
2651 isc_result_t result;
2652 const char *viewname;
2653 dns_rdataclass_t viewclass;
2654 dns_view_t *view = NULL;
2656 if (vconfig != NULL) {
2657 const cfg_obj_t *classobj = NULL;
2659 viewname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
2660 classobj = cfg_tuple_get(vconfig, "class");
2661 result = ns_config_getclass(classobj, dns_rdataclass_in,
2662 &viewclass);
2663 } else {
2664 viewname = "_default";
2665 viewclass = dns_rdataclass_in;
2667 result = dns_viewlist_find(viewlist, viewname, viewclass, &view);
2668 if (result == ISC_R_SUCCESS)
2669 return (ISC_R_EXISTS);
2670 if (result != ISC_R_NOTFOUND)
2671 return (result);
2672 INSIST(view == NULL);
2674 result = dns_view_create(ns_g_mctx, viewclass, viewname, &view);
2675 if (result != ISC_R_SUCCESS)
2676 return (result);
2678 ISC_LIST_APPEND(*viewlist, view, link);
2679 dns_view_attach(view, viewp);
2680 return (ISC_R_SUCCESS);
2684 * Configure or reconfigure a zone.
2686 static isc_result_t
2687 configure_zone(const cfg_obj_t *config, const cfg_obj_t *zconfig,
2688 const cfg_obj_t *vconfig, isc_mem_t *mctx, dns_view_t *view,
2689 cfg_aclconfctx_t *aclconf)
2691 dns_view_t *pview = NULL; /* Production view */
2692 dns_zone_t *zone = NULL; /* New or reused zone */
2693 dns_zone_t *dupzone = NULL;
2694 const cfg_obj_t *options = NULL;
2695 const cfg_obj_t *zoptions = NULL;
2696 const cfg_obj_t *typeobj = NULL;
2697 const cfg_obj_t *forwarders = NULL;
2698 const cfg_obj_t *forwardtype = NULL;
2699 const cfg_obj_t *only = NULL;
2700 isc_result_t result;
2701 isc_result_t tresult;
2702 isc_buffer_t buffer;
2703 dns_fixedname_t fixorigin;
2704 dns_name_t *origin;
2705 const char *zname;
2706 dns_rdataclass_t zclass;
2707 const char *ztypestr;
2709 options = NULL;
2710 (void)cfg_map_get(config, "options", &options);
2712 zoptions = cfg_tuple_get(zconfig, "options");
2715 * Get the zone origin as a dns_name_t.
2717 zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
2718 isc_buffer_init(&buffer, zname, strlen(zname));
2719 isc_buffer_add(&buffer, strlen(zname));
2720 dns_fixedname_init(&fixorigin);
2721 CHECK(dns_name_fromtext(dns_fixedname_name(&fixorigin),
2722 &buffer, dns_rootname, 0, NULL));
2723 origin = dns_fixedname_name(&fixorigin);
2725 CHECK(ns_config_getclass(cfg_tuple_get(zconfig, "class"),
2726 view->rdclass, &zclass));
2727 if (zclass != view->rdclass) {
2728 const char *vname = NULL;
2729 if (vconfig != NULL)
2730 vname = cfg_obj_asstring(cfg_tuple_get(vconfig,
2731 "name"));
2732 else
2733 vname = "<default view>";
2735 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2736 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
2737 "zone '%s': wrong class for view '%s'",
2738 zname, vname);
2739 result = ISC_R_FAILURE;
2740 goto cleanup;
2743 (void)cfg_map_get(zoptions, "type", &typeobj);
2744 if (typeobj == NULL) {
2745 cfg_obj_log(zconfig, ns_g_lctx, ISC_LOG_ERROR,
2746 "zone '%s' 'type' not specified", zname);
2747 return (ISC_R_FAILURE);
2749 ztypestr = cfg_obj_asstring(typeobj);
2752 * "hints zones" aren't zones. If we've got one,
2753 * configure it and return.
2755 if (strcasecmp(ztypestr, "hint") == 0) {
2756 const cfg_obj_t *fileobj = NULL;
2757 if (cfg_map_get(zoptions, "file", &fileobj) != ISC_R_SUCCESS) {
2758 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2759 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
2760 "zone '%s': 'file' not specified",
2761 zname);
2762 result = ISC_R_FAILURE;
2763 goto cleanup;
2765 if (dns_name_equal(origin, dns_rootname)) {
2766 const char *hintsfile = cfg_obj_asstring(fileobj);
2768 result = configure_hints(view, hintsfile);
2769 if (result != ISC_R_SUCCESS) {
2770 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2771 NS_LOGMODULE_SERVER,
2772 ISC_LOG_ERROR,
2773 "could not configure root hints "
2774 "from '%s': %s", hintsfile,
2775 isc_result_totext(result));
2776 goto cleanup;
2779 * Hint zones may also refer to delegation only points.
2781 only = NULL;
2782 tresult = cfg_map_get(zoptions, "delegation-only",
2783 &only);
2784 if (tresult == ISC_R_SUCCESS && cfg_obj_asboolean(only))
2785 CHECK(dns_view_adddelegationonly(view, origin));
2786 } else {
2787 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2788 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
2789 "ignoring non-root hint zone '%s'",
2790 zname);
2791 result = ISC_R_SUCCESS;
2793 /* Skip ordinary zone processing. */
2794 goto cleanup;
2798 * "forward zones" aren't zones either. Translate this syntax into
2799 * the appropriate selective forwarding configuration and return.
2801 if (strcasecmp(ztypestr, "forward") == 0) {
2802 forwardtype = NULL;
2803 forwarders = NULL;
2805 (void)cfg_map_get(zoptions, "forward", &forwardtype);
2806 (void)cfg_map_get(zoptions, "forwarders", &forwarders);
2807 result = configure_forward(config, view, origin, forwarders,
2808 forwardtype);
2809 goto cleanup;
2813 * "delegation-only zones" aren't zones either.
2815 if (strcasecmp(ztypestr, "delegation-only") == 0) {
2816 result = dns_view_adddelegationonly(view, origin);
2817 goto cleanup;
2821 * Check for duplicates in the new zone table.
2823 result = dns_view_findzone(view, origin, &dupzone);
2824 if (result == ISC_R_SUCCESS) {
2826 * We already have this zone!
2828 cfg_obj_log(zconfig, ns_g_lctx, ISC_LOG_ERROR,
2829 "zone '%s' already exists", zname);
2830 dns_zone_detach(&dupzone);
2831 result = ISC_R_EXISTS;
2832 goto cleanup;
2834 INSIST(dupzone == NULL);
2837 * See if we can reuse an existing zone. This is
2838 * only possible if all of these are true:
2839 * - The zone's view exists
2840 * - A zone with the right name exists in the view
2841 * - The zone is compatible with the config
2842 * options (e.g., an existing master zone cannot
2843 * be reused if the options specify a slave zone)
2845 result = dns_viewlist_find(&ns_g_server->viewlist,
2846 view->name, view->rdclass,
2847 &pview);
2848 if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
2849 goto cleanup;
2850 if (pview != NULL)
2851 result = dns_view_findzone(pview, origin, &zone);
2852 if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
2853 goto cleanup;
2854 if (zone != NULL && !ns_zone_reusable(zone, zconfig))
2855 dns_zone_detach(&zone);
2857 if (zone != NULL) {
2859 * We found a reusable zone. Make it use the
2860 * new view.
2862 dns_zone_setview(zone, view);
2863 if (view->acache != NULL)
2864 dns_zone_setacache(zone, view->acache);
2865 } else {
2867 * We cannot reuse an existing zone, we have
2868 * to create a new one.
2870 CHECK(dns_zone_create(&zone, mctx));
2871 CHECK(dns_zone_setorigin(zone, origin));
2872 dns_zone_setview(zone, view);
2873 if (view->acache != NULL)
2874 dns_zone_setacache(zone, view->acache);
2875 CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr, zone));
2876 dns_zone_setstats(zone, ns_g_server->zonestats);
2880 * If the zone contains a 'forwarders' statement, configure
2881 * selective forwarding.
2883 forwarders = NULL;
2884 if (cfg_map_get(zoptions, "forwarders", &forwarders) == ISC_R_SUCCESS)
2886 forwardtype = NULL;
2887 (void)cfg_map_get(zoptions, "forward", &forwardtype);
2888 CHECK(configure_forward(config, view, origin, forwarders,
2889 forwardtype));
2893 * Stub and forward zones may also refer to delegation only points.
2895 only = NULL;
2896 if (cfg_map_get(zoptions, "delegation-only", &only) == ISC_R_SUCCESS)
2898 if (cfg_obj_asboolean(only))
2899 CHECK(dns_view_adddelegationonly(view, origin));
2903 * Configure the zone.
2905 CHECK(ns_zone_configure(config, vconfig, zconfig, aclconf, zone));
2908 * Add the zone to its view in the new view list.
2910 CHECK(dns_view_addzone(view, zone));
2912 cleanup:
2913 if (zone != NULL)
2914 dns_zone_detach(&zone);
2915 if (pview != NULL)
2916 dns_view_detach(&pview);
2918 return (result);
2922 * Configure built-in zone for storing managed-key data.
2925 #define KEYZONE "managed-keys.bind"
2927 static isc_result_t
2928 add_keydata_zone(dns_view_t *view, isc_mem_t *mctx) {
2929 isc_result_t result;
2930 dns_zone_t *zone = NULL;
2931 dns_acl_t *none = NULL;
2932 dns_name_t zname;
2934 if (!ns_g_server->managedkeys)
2935 return (ISC_R_SUCCESS);
2937 REQUIRE(view != NULL);
2939 CHECK(dns_zone_create(&zone, mctx));
2941 dns_name_init(&zname, NULL);
2942 CHECK(dns_name_fromstring(&zname, KEYZONE, 0, mctx));
2943 CHECK(dns_zone_setorigin(zone, &zname));
2944 dns_name_free(&zname, mctx);
2946 CHECK(dns_zone_setfile(zone, KEYZONE));
2948 if (view->hints == NULL)
2949 dns_view_sethints(view, ns_g_server->in_roothints);
2951 dns_zone_setview(zone, view);
2952 dns_zone_settype(zone, dns_zone_key);
2953 dns_zone_setclass(zone, view->rdclass);
2955 CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr, zone));
2957 if (view->acache != NULL)
2958 dns_zone_setacache(zone, view->acache);
2960 CHECK(dns_acl_none(mctx, &none));
2961 dns_zone_setqueryacl(zone, none);
2962 dns_zone_setqueryonacl(zone, none);
2963 dns_acl_detach(&none);
2965 dns_zone_setdialup(zone, dns_dialuptype_no);
2966 dns_zone_setnotifytype(zone, dns_notifytype_no);
2967 dns_zone_setoption(zone, DNS_ZONEOPT_NOCHECKNS, ISC_TRUE);
2968 dns_zone_setjournalsize(zone, 0);
2970 dns_zone_setstats(zone, ns_g_server->zonestats);
2971 CHECK(setquerystats(zone, mctx, ISC_FALSE));
2973 CHECK(dns_view_addzone(view, zone));
2975 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2976 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
2977 "set up %s meta-zone", KEYZONE);
2979 cleanup:
2980 if (zone != NULL)
2981 dns_zone_detach(&zone);
2982 if (none != NULL)
2983 dns_acl_detach(&none);
2985 return (result);
2989 * Configure a single server quota.
2991 static void
2992 configure_server_quota(const cfg_obj_t **maps, const char *name,
2993 isc_quota_t *quota)
2995 const cfg_obj_t *obj = NULL;
2996 isc_result_t result;
2998 result = ns_config_get(maps, name, &obj);
2999 INSIST(result == ISC_R_SUCCESS);
3000 isc_quota_max(quota, cfg_obj_asuint32(obj));
3004 * This function is called as soon as the 'directory' statement has been
3005 * parsed. This can be extended to support other options if necessary.
3007 static isc_result_t
3008 directory_callback(const char *clausename, const cfg_obj_t *obj, void *arg) {
3009 isc_result_t result;
3010 const char *directory;
3012 REQUIRE(strcasecmp("directory", clausename) == 0);
3014 UNUSED(arg);
3015 UNUSED(clausename);
3018 * Change directory.
3020 directory = cfg_obj_asstring(obj);
3022 if (! isc_file_ischdiridempotent(directory))
3023 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_WARNING,
3024 "option 'directory' contains relative path '%s'",
3025 directory);
3027 result = isc_dir_chdir(directory);
3028 if (result != ISC_R_SUCCESS) {
3029 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
3030 "change directory to '%s' failed: %s",
3031 directory, isc_result_totext(result));
3032 return (result);
3035 return (ISC_R_SUCCESS);
3038 static void
3039 scan_interfaces(ns_server_t *server, isc_boolean_t verbose) {
3040 isc_boolean_t match_mapped = server->aclenv.match_mapped;
3042 ns_interfacemgr_scan(server->interfacemgr, verbose);
3044 * Update the "localhost" and "localnets" ACLs to match the
3045 * current set of network interfaces.
3047 dns_aclenv_copy(&server->aclenv,
3048 ns_interfacemgr_getaclenv(server->interfacemgr));
3050 server->aclenv.match_mapped = match_mapped;
3053 static isc_result_t
3054 add_listenelt(isc_mem_t *mctx, ns_listenlist_t *list, isc_sockaddr_t *addr,
3055 isc_boolean_t wcardport_ok)
3057 ns_listenelt_t *lelt = NULL;
3058 dns_acl_t *src_acl = NULL;
3059 isc_result_t result;
3060 isc_sockaddr_t any_sa6;
3061 isc_netaddr_t netaddr;
3063 REQUIRE(isc_sockaddr_pf(addr) == AF_INET6);
3065 isc_sockaddr_any6(&any_sa6);
3066 if (!isc_sockaddr_equal(&any_sa6, addr) &&
3067 (wcardport_ok || isc_sockaddr_getport(addr) != 0)) {
3068 isc_netaddr_fromin6(&netaddr, &addr->type.sin6.sin6_addr);
3070 result = dns_acl_create(mctx, 0, &src_acl);
3071 if (result != ISC_R_SUCCESS)
3072 return (result);
3074 result = dns_iptable_addprefix(src_acl->iptable,
3075 &netaddr, 128, ISC_TRUE);
3076 if (result != ISC_R_SUCCESS)
3077 goto clean;
3079 result = ns_listenelt_create(mctx, isc_sockaddr_getport(addr),
3080 src_acl, &lelt);
3081 if (result != ISC_R_SUCCESS)
3082 goto clean;
3083 ISC_LIST_APPEND(list->elts, lelt, link);
3086 return (ISC_R_SUCCESS);
3088 clean:
3089 INSIST(lelt == NULL);
3090 dns_acl_detach(&src_acl);
3092 return (result);
3096 * Make a list of xxx-source addresses and call ns_interfacemgr_adjust()
3097 * to update the listening interfaces accordingly.
3098 * We currently only consider IPv6, because this only affects IPv6 wildcard
3099 * sockets.
3101 static void
3102 adjust_interfaces(ns_server_t *server, isc_mem_t *mctx) {
3103 isc_result_t result;
3104 ns_listenlist_t *list = NULL;
3105 dns_view_t *view;
3106 dns_zone_t *zone, *next;
3107 isc_sockaddr_t addr, *addrp;
3109 result = ns_listenlist_create(mctx, &list);
3110 if (result != ISC_R_SUCCESS)
3111 return;
3113 for (view = ISC_LIST_HEAD(server->viewlist);
3114 view != NULL;
3115 view = ISC_LIST_NEXT(view, link)) {
3116 dns_dispatch_t *dispatch6;
3118 dispatch6 = dns_resolver_dispatchv6(view->resolver);
3119 if (dispatch6 == NULL)
3120 continue;
3121 result = dns_dispatch_getlocaladdress(dispatch6, &addr);
3122 if (result != ISC_R_SUCCESS)
3123 goto fail;
3126 * We always add non-wildcard address regardless of whether
3127 * the port is 'any' (the fourth arg is TRUE): if the port is
3128 * specific, we need to add it since it may conflict with a
3129 * listening interface; if it's zero, we'll dynamically open
3130 * query ports, and some of them may override an existing
3131 * wildcard IPv6 port.
3133 result = add_listenelt(mctx, list, &addr, ISC_TRUE);
3134 if (result != ISC_R_SUCCESS)
3135 goto fail;
3138 zone = NULL;
3139 for (result = dns_zone_first(server->zonemgr, &zone);
3140 result == ISC_R_SUCCESS;
3141 next = NULL, result = dns_zone_next(zone, &next), zone = next) {
3142 dns_view_t *zoneview;
3145 * At this point the zone list may contain a stale zone
3146 * just removed from the configuration. To see the validity,
3147 * check if the corresponding view is in our current view list.
3148 * There may also be old zones that are still in the process
3149 * of shutting down and have detached from their old view
3150 * (zoneview == NULL).
3152 zoneview = dns_zone_getview(zone);
3153 if (zoneview == NULL)
3154 continue;
3155 for (view = ISC_LIST_HEAD(server->viewlist);
3156 view != NULL && view != zoneview;
3157 view = ISC_LIST_NEXT(view, link))
3159 if (view == NULL)
3160 continue;
3162 addrp = dns_zone_getnotifysrc6(zone);
3163 result = add_listenelt(mctx, list, addrp, ISC_FALSE);
3164 if (result != ISC_R_SUCCESS)
3165 goto fail;
3167 addrp = dns_zone_getxfrsource6(zone);
3168 result = add_listenelt(mctx, list, addrp, ISC_FALSE);
3169 if (result != ISC_R_SUCCESS)
3170 goto fail;
3173 ns_interfacemgr_adjust(server->interfacemgr, list, ISC_TRUE);
3175 clean:
3176 ns_listenlist_detach(&list);
3177 return;
3179 fail:
3181 * Even when we failed the procedure, most of other interfaces
3182 * should work correctly. We therefore just warn it.
3184 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3185 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
3186 "could not adjust the listen-on list; "
3187 "some interfaces may not work");
3188 goto clean;
3192 * This event callback is invoked to do periodic network
3193 * interface scanning.
3195 static void
3196 interface_timer_tick(isc_task_t *task, isc_event_t *event) {
3197 isc_result_t result;
3198 ns_server_t *server = (ns_server_t *) event->ev_arg;
3199 INSIST(task == server->task);
3200 UNUSED(task);
3201 isc_event_free(&event);
3203 * XXX should scan interfaces unlocked and get exclusive access
3204 * only to replace ACLs.
3206 result = isc_task_beginexclusive(server->task);
3207 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3208 scan_interfaces(server, ISC_FALSE);
3209 isc_task_endexclusive(server->task);
3212 static void
3213 heartbeat_timer_tick(isc_task_t *task, isc_event_t *event) {
3214 ns_server_t *server = (ns_server_t *) event->ev_arg;
3215 dns_view_t *view;
3217 UNUSED(task);
3218 isc_event_free(&event);
3219 view = ISC_LIST_HEAD(server->viewlist);
3220 while (view != NULL) {
3221 dns_view_dialup(view);
3222 view = ISC_LIST_NEXT(view, link);
3226 static void
3227 pps_timer_tick(isc_task_t *task, isc_event_t *event) {
3228 static unsigned int oldrequests = 0;
3229 unsigned int requests = ns_client_requests;
3231 UNUSED(task);
3232 isc_event_free(&event);
3235 * Don't worry about wrapping as the overflow result will be right.
3237 dns_pps = (requests - oldrequests) / 1200;
3238 oldrequests = requests;
3242 * Replace the current value of '*field', a dynamically allocated
3243 * string or NULL, with a dynamically allocated copy of the
3244 * null-terminated string pointed to by 'value', or NULL.
3246 static isc_result_t
3247 setstring(ns_server_t *server, char **field, const char *value) {
3248 char *copy;
3250 if (value != NULL) {
3251 copy = isc_mem_strdup(server->mctx, value);
3252 if (copy == NULL)
3253 return (ISC_R_NOMEMORY);
3254 } else {
3255 copy = NULL;
3258 if (*field != NULL)
3259 isc_mem_free(server->mctx, *field);
3261 *field = copy;
3262 return (ISC_R_SUCCESS);
3266 * Replace the current value of '*field', a dynamically allocated
3267 * string or NULL, with another dynamically allocated string
3268 * or NULL if whether 'obj' is a string or void value, respectively.
3270 static isc_result_t
3271 setoptstring(ns_server_t *server, char **field, const cfg_obj_t *obj) {
3272 if (cfg_obj_isvoid(obj))
3273 return (setstring(server, field, NULL));
3274 else
3275 return (setstring(server, field, cfg_obj_asstring(obj)));
3278 static void
3279 set_limit(const cfg_obj_t **maps, const char *configname,
3280 const char *description, isc_resource_t resourceid,
3281 isc_resourcevalue_t defaultvalue)
3283 const cfg_obj_t *obj = NULL;
3284 const char *resource;
3285 isc_resourcevalue_t value;
3286 isc_result_t result;
3288 if (ns_config_get(maps, configname, &obj) != ISC_R_SUCCESS)
3289 return;
3291 if (cfg_obj_isstring(obj)) {
3292 resource = cfg_obj_asstring(obj);
3293 if (strcasecmp(resource, "unlimited") == 0)
3294 value = ISC_RESOURCE_UNLIMITED;
3295 else {
3296 INSIST(strcasecmp(resource, "default") == 0);
3297 value = defaultvalue;
3299 } else
3300 value = cfg_obj_asuint64(obj);
3302 result = isc_resource_setlimit(resourceid, value);
3303 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
3304 result == ISC_R_SUCCESS ?
3305 ISC_LOG_DEBUG(3) : ISC_LOG_WARNING,
3306 "set maximum %s to %" ISC_PRINT_QUADFORMAT "u: %s",
3307 description, value, isc_result_totext(result));
3310 #define SETLIMIT(cfgvar, resource, description) \
3311 set_limit(maps, cfgvar, description, isc_resource_ ## resource, \
3312 ns_g_init ## resource)
3314 static void
3315 set_limits(const cfg_obj_t **maps) {
3316 SETLIMIT("stacksize", stacksize, "stack size");
3317 SETLIMIT("datasize", datasize, "data size");
3318 SETLIMIT("coresize", coresize, "core size");
3319 SETLIMIT("files", openfiles, "open files");
3322 static void
3323 portset_fromconf(isc_portset_t *portset, const cfg_obj_t *ports,
3324 isc_boolean_t positive)
3326 const cfg_listelt_t *element;
3328 for (element = cfg_list_first(ports);
3329 element != NULL;
3330 element = cfg_list_next(element)) {
3331 const cfg_obj_t *obj = cfg_listelt_value(element);
3333 if (cfg_obj_isuint32(obj)) {
3334 in_port_t port = (in_port_t)cfg_obj_asuint32(obj);
3336 if (positive)
3337 isc_portset_add(portset, port);
3338 else
3339 isc_portset_remove(portset, port);
3340 } else {
3341 const cfg_obj_t *obj_loport, *obj_hiport;
3342 in_port_t loport, hiport;
3344 obj_loport = cfg_tuple_get(obj, "loport");
3345 loport = (in_port_t)cfg_obj_asuint32(obj_loport);
3346 obj_hiport = cfg_tuple_get(obj, "hiport");
3347 hiport = (in_port_t)cfg_obj_asuint32(obj_hiport);
3349 if (positive)
3350 isc_portset_addrange(portset, loport, hiport);
3351 else {
3352 isc_portset_removerange(portset, loport,
3353 hiport);
3359 static isc_result_t
3360 removed(dns_zone_t *zone, void *uap) {
3361 const char *type;
3363 if (dns_zone_getview(zone) != uap)
3364 return (ISC_R_SUCCESS);
3366 switch (dns_zone_gettype(zone)) {
3367 case dns_zone_master:
3368 type = "master";
3369 break;
3370 case dns_zone_slave:
3371 type = "slave";
3372 break;
3373 case dns_zone_stub:
3374 type = "stub";
3375 break;
3376 default:
3377 type = "other";
3378 break;
3380 dns_zone_log(zone, ISC_LOG_INFO, "(%s) removed", type);
3381 return (ISC_R_SUCCESS);
3384 static void
3385 cleanup_session_key(ns_server_t *server, isc_mem_t *mctx) {
3386 if (server->session_keyfile != NULL) {
3387 isc_file_remove(server->session_keyfile);
3388 isc_mem_free(mctx, server->session_keyfile);
3389 server->session_keyfile = NULL;
3392 if (server->session_keyname != NULL) {
3393 if (dns_name_dynamic(server->session_keyname))
3394 dns_name_free(server->session_keyname, mctx);
3395 isc_mem_put(mctx, server->session_keyname, sizeof(dns_name_t));
3396 server->session_keyname = NULL;
3399 if (server->sessionkey != NULL)
3400 dns_tsigkey_detach(&server->sessionkey);
3402 server->session_keyalg = DST_ALG_UNKNOWN;
3403 server->session_keybits = 0;
3406 static isc_result_t
3407 generate_session_key(const char *filename, const char *keynamestr,
3408 dns_name_t *keyname, const char *algstr,
3409 dns_name_t *algname, unsigned int algtype,
3410 isc_uint16_t bits, isc_mem_t *mctx,
3411 dns_tsigkey_t **tsigkeyp)
3413 isc_result_t result = ISC_R_SUCCESS;
3414 dst_key_t *key = NULL;
3415 isc_buffer_t key_txtbuffer;
3416 isc_buffer_t key_rawbuffer;
3417 char key_txtsecret[256];
3418 char key_rawsecret[64];
3419 isc_region_t key_rawregion;
3420 isc_stdtime_t now;
3421 dns_tsigkey_t *tsigkey = NULL;
3422 FILE *fp = NULL;
3424 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3425 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
3426 "generating session key for dynamic DNS");
3428 /* generate key */
3429 result = dst_key_generate(keyname, algtype, bits, 1, 0,
3430 DNS_KEYPROTO_ANY, dns_rdataclass_in,
3431 mctx, &key);
3432 if (result != ISC_R_SUCCESS)
3433 return (result);
3436 * Dump the key to the buffer for later use. Should be done before
3437 * we transfer the ownership of key to tsigkey.
3439 isc_buffer_init(&key_rawbuffer, &key_rawsecret, sizeof(key_rawsecret));
3440 CHECK(dst_key_tobuffer(key, &key_rawbuffer));
3442 isc_buffer_usedregion(&key_rawbuffer, &key_rawregion);
3443 isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
3444 CHECK(isc_base64_totext(&key_rawregion, -1, "", &key_txtbuffer));
3446 /* Store the key in tsigkey. */
3447 isc_stdtime_get(&now);
3448 CHECK(dns_tsigkey_createfromkey(dst_key_name(key), algname, key,
3449 ISC_FALSE, NULL, now, now, mctx, NULL,
3450 &tsigkey));
3451 key = NULL; /* ownership of key has been transferred */
3453 /* Dump the key to the key file. */
3454 fp = ns_os_openfile(filename, S_IRUSR|S_IWUSR, ISC_TRUE);
3455 if (fp == NULL) {
3456 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3457 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
3458 "could not create %s", filename);
3459 result = ISC_R_NOPERM;
3460 goto cleanup;
3463 fprintf(fp, "key \"%s\" {\n"
3464 "\talgorithm %s;\n"
3465 "\tsecret \"%.*s\";\n};\n", keynamestr, algstr,
3466 (int) isc_buffer_usedlength(&key_txtbuffer),
3467 (char*) isc_buffer_base(&key_txtbuffer));
3469 RUNTIME_CHECK(isc_stdio_flush(fp) == ISC_R_SUCCESS);
3470 RUNTIME_CHECK(isc_stdio_close(fp) == ISC_R_SUCCESS);
3472 *tsigkeyp = tsigkey;
3474 return (ISC_R_SUCCESS);
3476 cleanup:
3477 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3478 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
3479 "failed to generate session key "
3480 "for dynamic DNS: %s", isc_result_totext(result));
3481 if (tsigkey != NULL)
3482 dns_tsigkey_detach(&tsigkey);
3483 if (key != NULL)
3484 dst_key_free(&key);
3486 return (result);
3489 static isc_result_t
3490 configure_session_key(const cfg_obj_t **maps, ns_server_t *server,
3491 isc_mem_t *mctx)
3493 const char *keyfile, *keynamestr, *algstr;
3494 unsigned int algtype;
3495 dns_fixedname_t fname;
3496 dns_name_t *keyname, *algname;
3497 isc_buffer_t buffer;
3498 isc_uint16_t bits;
3499 const cfg_obj_t *obj;
3500 isc_boolean_t need_deleteold = ISC_FALSE;
3501 isc_boolean_t need_createnew = ISC_FALSE;
3502 isc_result_t result;
3504 obj = NULL;
3505 result = ns_config_get(maps, "session-keyfile", &obj);
3506 if (result == ISC_R_SUCCESS) {
3507 if (cfg_obj_isvoid(obj))
3508 keyfile = NULL; /* disable it */
3509 else
3510 keyfile = cfg_obj_asstring(obj);
3511 } else
3512 keyfile = ns_g_defaultsessionkeyfile;
3514 obj = NULL;
3515 result = ns_config_get(maps, "session-keyname", &obj);
3516 INSIST(result == ISC_R_SUCCESS);
3517 keynamestr = cfg_obj_asstring(obj);
3518 dns_fixedname_init(&fname);
3519 isc_buffer_init(&buffer, keynamestr, strlen(keynamestr));
3520 isc_buffer_add(&buffer, strlen(keynamestr));
3521 keyname = dns_fixedname_name(&fname);
3522 result = dns_name_fromtext(keyname, &buffer, dns_rootname, 0, NULL);
3523 if (result != ISC_R_SUCCESS)
3524 return (result);
3526 obj = NULL;
3527 result = ns_config_get(maps, "session-keyalg", &obj);
3528 INSIST(result == ISC_R_SUCCESS);
3529 algstr = cfg_obj_asstring(obj);
3530 algname = NULL;
3531 result = ns_config_getkeyalgorithm2(algstr, &algname, &algtype, &bits);
3532 if (result != ISC_R_SUCCESS) {
3533 const char *s = " (keeping current key)";
3535 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR, "session-keyalg: "
3536 "unsupported or unknown algorithm '%s'%s",
3537 algstr,
3538 server->session_keyfile != NULL ? s : "");
3539 return (result);
3542 /* See if we need to (re)generate a new key. */
3543 if (keyfile == NULL) {
3544 if (server->session_keyfile != NULL)
3545 need_deleteold = ISC_TRUE;
3546 } else if (server->session_keyfile == NULL)
3547 need_createnew = ISC_TRUE;
3548 else if (strcmp(keyfile, server->session_keyfile) != 0 ||
3549 !dns_name_equal(server->session_keyname, keyname) ||
3550 server->session_keyalg != algtype ||
3551 server->session_keybits != bits) {
3552 need_deleteold = ISC_TRUE;
3553 need_createnew = ISC_TRUE;
3556 if (need_deleteold) {
3557 INSIST(server->session_keyfile != NULL);
3558 INSIST(server->session_keyname != NULL);
3559 INSIST(server->sessionkey != NULL);
3561 cleanup_session_key(server, mctx);
3564 if (need_createnew) {
3565 INSIST(server->sessionkey == NULL);
3566 INSIST(server->session_keyfile == NULL);
3567 INSIST(server->session_keyname == NULL);
3568 INSIST(server->session_keyalg == DST_ALG_UNKNOWN);
3569 INSIST(server->session_keybits == 0);
3571 server->session_keyname = isc_mem_get(mctx, sizeof(dns_name_t));
3572 if (server->session_keyname == NULL)
3573 goto cleanup;
3574 dns_name_init(server->session_keyname, NULL);
3575 CHECK(dns_name_dup(keyname, mctx, server->session_keyname));
3577 server->session_keyfile = isc_mem_strdup(mctx, keyfile);
3578 if (server->session_keyfile == NULL)
3579 goto cleanup;
3581 server->session_keyalg = algtype;
3582 server->session_keybits = bits;
3584 CHECK(generate_session_key(keyfile, keynamestr, keyname, algstr,
3585 algname, algtype, bits, mctx,
3586 &server->sessionkey));
3589 return (result);
3591 cleanup:
3592 cleanup_session_key(server, mctx);
3593 return (result);
3596 static isc_result_t
3597 load_configuration(const char *filename, ns_server_t *server,
3598 isc_boolean_t first_time)
3600 cfg_aclconfctx_t aclconfctx;
3601 cfg_obj_t *config = NULL, *bindkeys = NULL;
3602 cfg_parser_t *conf_parser = NULL, *bindkeys_parser = NULL;
3603 const cfg_listelt_t *element;
3604 const cfg_obj_t *builtin_views;
3605 const cfg_obj_t *maps[3];
3606 const cfg_obj_t *obj;
3607 const cfg_obj_t *options;
3608 const cfg_obj_t *usev4ports, *avoidv4ports, *usev6ports, *avoidv6ports;
3609 const cfg_obj_t *views;
3610 dns_view_t *view = NULL;
3611 dns_view_t *view_next;
3612 dns_viewlist_t tmpviewlist;
3613 dns_viewlist_t viewlist, builtin_viewlist;
3614 in_port_t listen_port, udpport_low, udpport_high;
3615 int i;
3616 isc_interval_t interval;
3617 isc_portset_t *v4portset = NULL;
3618 isc_portset_t *v6portset = NULL;
3619 isc_resourcevalue_t nfiles;
3620 isc_result_t result;
3621 isc_uint32_t heartbeat_interval;
3622 isc_uint32_t interface_interval;
3623 isc_uint32_t reserved;
3624 isc_uint32_t udpsize;
3625 ns_cachelist_t cachelist, tmpcachelist;
3626 unsigned int maxsocks;
3627 ns_cache_t *nsc;
3629 cfg_aclconfctx_init(&aclconfctx);
3630 ISC_LIST_INIT(viewlist);
3631 ISC_LIST_INIT(builtin_viewlist);
3632 ISC_LIST_INIT(cachelist);
3634 /* Ensure exclusive access to configuration data. */
3635 result = isc_task_beginexclusive(server->task);
3636 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3639 * Parse the global default pseudo-config file.
3641 if (first_time) {
3642 CHECK(ns_config_parsedefaults(ns_g_parser, &ns_g_config));
3643 RUNTIME_CHECK(cfg_map_get(ns_g_config, "options",
3644 &ns_g_defaults) == ISC_R_SUCCESS);
3648 * Parse the configuration file using the new config code.
3650 result = ISC_R_FAILURE;
3651 config = NULL;
3654 * Unless this is lwresd with the -C option, parse the config file.
3656 if (!(ns_g_lwresdonly && lwresd_g_useresolvconf)) {
3657 isc_log_write(ns_g_lctx,
3658 NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
3659 ISC_LOG_INFO, "loading configuration from '%s'",
3660 filename);
3661 CHECK(cfg_parser_create(ns_g_mctx, ns_g_lctx, &conf_parser));
3662 cfg_parser_setcallback(conf_parser, directory_callback, NULL);
3663 result = cfg_parse_file(conf_parser, filename,
3664 &cfg_type_namedconf, &config);
3668 * If this is lwresd with the -C option, or lwresd with no -C or -c
3669 * option where the above parsing failed, parse resolv.conf.
3671 if (ns_g_lwresdonly &&
3672 (lwresd_g_useresolvconf ||
3673 (!ns_g_conffileset && result == ISC_R_FILENOTFOUND)))
3675 isc_log_write(ns_g_lctx,
3676 NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
3677 ISC_LOG_INFO, "loading configuration from '%s'",
3678 lwresd_g_resolvconffile);
3679 if (conf_parser != NULL)
3680 cfg_parser_destroy(&conf_parser);
3681 CHECK(cfg_parser_create(ns_g_mctx, ns_g_lctx, &conf_parser));
3682 result = ns_lwresd_parseeresolvconf(ns_g_mctx, conf_parser,
3683 &config);
3685 CHECK(result);
3688 * Check the validity of the configuration.
3690 CHECK(bind9_check_namedconf(config, ns_g_lctx, ns_g_mctx));
3693 * Fill in the maps array, used for resolving defaults.
3695 i = 0;
3696 options = NULL;
3697 result = cfg_map_get(config, "options", &options);
3698 if (result == ISC_R_SUCCESS)
3699 maps[i++] = options;
3700 maps[i++] = ns_g_defaults;
3701 maps[i++] = NULL;
3704 * If bind.keys exists, load it. If "dnssec-lookaside auto"
3705 * is turned on, the keys found there will be used as default
3706 * trust anchors.
3708 obj = NULL;
3709 result = ns_config_get(maps, "bindkeys-file", &obj);
3710 INSIST(result == ISC_R_SUCCESS);
3711 CHECKM(setstring(server, &server->bindkeysfile,
3712 cfg_obj_asstring(obj)), "strdup");
3714 if (access(server->bindkeysfile, R_OK) == 0) {
3715 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3716 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
3717 "reading built-in trusted "
3718 "keys from file '%s'", server->bindkeysfile);
3720 CHECK(cfg_parser_create(ns_g_mctx, ns_g_lctx,
3721 &bindkeys_parser));
3723 result = cfg_parse_file(bindkeys_parser, server->bindkeysfile,
3724 &cfg_type_bindkeys, &bindkeys);
3725 CHECK(result);
3729 * Set process limits, which (usually) needs to be done as root.
3731 set_limits(maps);
3734 * Check if max number of open sockets that the system allows is
3735 * sufficiently large. Failing this condition is not necessarily fatal,
3736 * but may cause subsequent runtime failures for a busy recursive
3737 * server.
3739 result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &maxsocks);
3740 if (result != ISC_R_SUCCESS)
3741 maxsocks = 0;
3742 result = isc_resource_getcurlimit(isc_resource_openfiles, &nfiles);
3743 if (result == ISC_R_SUCCESS && (isc_resourcevalue_t)maxsocks > nfiles) {
3744 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3745 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
3746 "max open files (%" ISC_PRINT_QUADFORMAT "u)"
3747 " is smaller than max sockets (%u)",
3748 nfiles, maxsocks);
3752 * Set the number of socket reserved for TCP, stdio etc.
3754 obj = NULL;
3755 result = ns_config_get(maps, "reserved-sockets", &obj);
3756 INSIST(result == ISC_R_SUCCESS);
3757 reserved = cfg_obj_asuint32(obj);
3758 if (maxsocks != 0) {
3759 if (maxsocks < 128U) /* Prevent underflow. */
3760 reserved = 0;
3761 else if (reserved > maxsocks - 128U) /* Minimum UDP space. */
3762 reserved = maxsocks - 128;
3764 /* Minimum TCP/stdio space. */
3765 if (reserved < 128U)
3766 reserved = 128;
3767 if (reserved + 128U > maxsocks && maxsocks != 0) {
3768 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3769 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
3770 "less than 128 UDP sockets available after "
3771 "applying 'reserved-sockets' and 'maxsockets'");
3773 isc__socketmgr_setreserved(ns_g_socketmgr, reserved);
3776 * Configure various server options.
3778 configure_server_quota(maps, "transfers-out", &server->xfroutquota);
3779 configure_server_quota(maps, "tcp-clients", &server->tcpquota);
3780 configure_server_quota(maps, "recursive-clients",
3781 &server->recursionquota);
3782 if (server->recursionquota.max > 1000)
3783 isc_quota_soft(&server->recursionquota,
3784 server->recursionquota.max - 100);
3785 else
3786 isc_quota_soft(&server->recursionquota, 0);
3788 CHECK(configure_view_acl(NULL, config, "blackhole", NULL, &aclconfctx,
3789 ns_g_mctx, &server->blackholeacl));
3790 if (server->blackholeacl != NULL)
3791 dns_dispatchmgr_setblackhole(ns_g_dispatchmgr,
3792 server->blackholeacl);
3794 obj = NULL;
3795 result = ns_config_get(maps, "match-mapped-addresses", &obj);
3796 INSIST(result == ISC_R_SUCCESS);
3797 server->aclenv.match_mapped = cfg_obj_asboolean(obj);
3799 CHECKM(ns_statschannels_configure(ns_g_server, config, &aclconfctx),
3800 "configuring statistics server(s)");
3803 * Configure sets of UDP query source ports.
3805 CHECKM(isc_portset_create(ns_g_mctx, &v4portset),
3806 "creating UDP port set");
3807 CHECKM(isc_portset_create(ns_g_mctx, &v6portset),
3808 "creating UDP port set");
3810 usev4ports = NULL;
3811 usev6ports = NULL;
3812 avoidv4ports = NULL;
3813 avoidv6ports = NULL;
3815 (void)ns_config_get(maps, "use-v4-udp-ports", &usev4ports);
3816 if (usev4ports != NULL)
3817 portset_fromconf(v4portset, usev4ports, ISC_TRUE);
3818 else {
3819 CHECKM(isc_net_getudpportrange(AF_INET, &udpport_low,
3820 &udpport_high),
3821 "get the default UDP/IPv4 port range");
3822 if (udpport_low == udpport_high)
3823 isc_portset_add(v4portset, udpport_low);
3824 else {
3825 isc_portset_addrange(v4portset, udpport_low,
3826 udpport_high);
3828 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3829 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
3830 "using default UDP/IPv4 port range: [%d, %d]",
3831 udpport_low, udpport_high);
3833 (void)ns_config_get(maps, "avoid-v4-udp-ports", &avoidv4ports);
3834 if (avoidv4ports != NULL)
3835 portset_fromconf(v4portset, avoidv4ports, ISC_FALSE);
3837 (void)ns_config_get(maps, "use-v6-udp-ports", &usev6ports);
3838 if (usev6ports != NULL)
3839 portset_fromconf(v6portset, usev6ports, ISC_TRUE);
3840 else {
3841 CHECKM(isc_net_getudpportrange(AF_INET6, &udpport_low,
3842 &udpport_high),
3843 "get the default UDP/IPv6 port range");
3844 if (udpport_low == udpport_high)
3845 isc_portset_add(v6portset, udpport_low);
3846 else {
3847 isc_portset_addrange(v6portset, udpport_low,
3848 udpport_high);
3850 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3851 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
3852 "using default UDP/IPv6 port range: [%d, %d]",
3853 udpport_low, udpport_high);
3855 (void)ns_config_get(maps, "avoid-v6-udp-ports", &avoidv6ports);
3856 if (avoidv6ports != NULL)
3857 portset_fromconf(v6portset, avoidv6ports, ISC_FALSE);
3859 dns_dispatchmgr_setavailports(ns_g_dispatchmgr, v4portset, v6portset);
3862 * Set the EDNS UDP size when we don't match a view.
3864 obj = NULL;
3865 result = ns_config_get(maps, "edns-udp-size", &obj);
3866 INSIST(result == ISC_R_SUCCESS);
3867 udpsize = cfg_obj_asuint32(obj);
3868 if (udpsize < 512)
3869 udpsize = 512;
3870 if (udpsize > 4096)
3871 udpsize = 4096;
3872 ns_g_udpsize = (isc_uint16_t)udpsize;
3875 * Configure the zone manager.
3877 obj = NULL;
3878 result = ns_config_get(maps, "transfers-in", &obj);
3879 INSIST(result == ISC_R_SUCCESS);
3880 dns_zonemgr_settransfersin(server->zonemgr, cfg_obj_asuint32(obj));
3882 obj = NULL;
3883 result = ns_config_get(maps, "transfers-per-ns", &obj);
3884 INSIST(result == ISC_R_SUCCESS);
3885 dns_zonemgr_settransfersperns(server->zonemgr, cfg_obj_asuint32(obj));
3887 obj = NULL;
3888 result = ns_config_get(maps, "serial-query-rate", &obj);
3889 INSIST(result == ISC_R_SUCCESS);
3890 dns_zonemgr_setserialqueryrate(server->zonemgr, cfg_obj_asuint32(obj));
3893 * Determine which port to use for listening for incoming connections.
3895 if (ns_g_port != 0)
3896 listen_port = ns_g_port;
3897 else
3898 CHECKM(ns_config_getport(config, &listen_port), "port");
3901 * Find the listen queue depth.
3903 obj = NULL;
3904 result = ns_config_get(maps, "tcp-listen-queue", &obj);
3905 INSIST(result == ISC_R_SUCCESS);
3906 ns_g_listen = cfg_obj_asuint32(obj);
3907 if (ns_g_listen < 3)
3908 ns_g_listen = 3;
3911 * Configure the interface manager according to the "listen-on"
3912 * statement.
3915 const cfg_obj_t *clistenon = NULL;
3916 ns_listenlist_t *listenon = NULL;
3918 clistenon = NULL;
3920 * Even though listen-on is present in the default
3921 * configuration, we can't use it here, since it isn't
3922 * used if we're in lwresd mode. This way is easier.
3924 if (options != NULL)
3925 (void)cfg_map_get(options, "listen-on", &clistenon);
3926 if (clistenon != NULL) {
3927 result = ns_listenlist_fromconfig(clistenon,
3928 config,
3929 &aclconfctx,
3930 ns_g_mctx,
3931 &listenon);
3932 } else if (!ns_g_lwresdonly) {
3934 * Not specified, use default.
3936 CHECK(ns_listenlist_default(ns_g_mctx, listen_port,
3937 ISC_TRUE, &listenon));
3939 if (listenon != NULL) {
3940 ns_interfacemgr_setlistenon4(server->interfacemgr,
3941 listenon);
3942 ns_listenlist_detach(&listenon);
3946 * Ditto for IPv6.
3949 const cfg_obj_t *clistenon = NULL;
3950 ns_listenlist_t *listenon = NULL;
3952 if (options != NULL)
3953 (void)cfg_map_get(options, "listen-on-v6", &clistenon);
3954 if (clistenon != NULL) {
3955 result = ns_listenlist_fromconfig(clistenon,
3956 config,
3957 &aclconfctx,
3958 ns_g_mctx,
3959 &listenon);
3960 } else if (!ns_g_lwresdonly) {
3961 isc_boolean_t enable;
3963 * Not specified, use default.
3965 enable = ISC_TF(isc_net_probeipv4() != ISC_R_SUCCESS);
3966 CHECK(ns_listenlist_default(ns_g_mctx, listen_port,
3967 enable, &listenon));
3969 if (listenon != NULL) {
3970 ns_interfacemgr_setlistenon6(server->interfacemgr,
3971 listenon);
3972 ns_listenlist_detach(&listenon);
3977 * Rescan the interface list to pick up changes in the
3978 * listen-on option. It's important that we do this before we try
3979 * to configure the query source, since the dispatcher we use might
3980 * be shared with an interface.
3982 scan_interfaces(server, ISC_TRUE);
3985 * Arrange for further interface scanning to occur periodically
3986 * as specified by the "interface-interval" option.
3988 obj = NULL;
3989 result = ns_config_get(maps, "interface-interval", &obj);
3990 INSIST(result == ISC_R_SUCCESS);
3991 interface_interval = cfg_obj_asuint32(obj) * 60;
3992 if (interface_interval == 0) {
3993 CHECK(isc_timer_reset(server->interface_timer,
3994 isc_timertype_inactive,
3995 NULL, NULL, ISC_TRUE));
3996 } else if (server->interface_interval != interface_interval) {
3997 isc_interval_set(&interval, interface_interval, 0);
3998 CHECK(isc_timer_reset(server->interface_timer,
3999 isc_timertype_ticker,
4000 NULL, &interval, ISC_FALSE));
4002 server->interface_interval = interface_interval;
4005 * Configure the dialup heartbeat timer.
4007 obj = NULL;
4008 result = ns_config_get(maps, "heartbeat-interval", &obj);
4009 INSIST(result == ISC_R_SUCCESS);
4010 heartbeat_interval = cfg_obj_asuint32(obj) * 60;
4011 if (heartbeat_interval == 0) {
4012 CHECK(isc_timer_reset(server->heartbeat_timer,
4013 isc_timertype_inactive,
4014 NULL, NULL, ISC_TRUE));
4015 } else if (server->heartbeat_interval != heartbeat_interval) {
4016 isc_interval_set(&interval, heartbeat_interval, 0);
4017 CHECK(isc_timer_reset(server->heartbeat_timer,
4018 isc_timertype_ticker,
4019 NULL, &interval, ISC_FALSE));
4021 server->heartbeat_interval = heartbeat_interval;
4023 isc_interval_set(&interval, 1200, 0);
4024 CHECK(isc_timer_reset(server->pps_timer, isc_timertype_ticker, NULL,
4025 &interval, ISC_FALSE));
4028 * Write the PID file.
4030 obj = NULL;
4031 if (ns_config_get(maps, "pid-file", &obj) == ISC_R_SUCCESS)
4032 if (cfg_obj_isvoid(obj))
4033 ns_os_writepidfile(NULL, first_time);
4034 else
4035 ns_os_writepidfile(cfg_obj_asstring(obj), first_time);
4036 else if (ns_g_lwresdonly)
4037 ns_os_writepidfile(lwresd_g_defaultpidfile, first_time);
4038 else
4039 ns_os_writepidfile(ns_g_defaultpidfile, first_time);
4042 * Configure the server-wide session key. This must be done before
4043 * configure views because zone configuration may need to know
4044 * session-keyname.
4046 * Failure of session key generation isn't fatal at this time; if it
4047 * turns out that a session key is really needed but doesn't exist,
4048 * we'll treat it as a fatal error then.
4050 (void)configure_session_key(maps, server, ns_g_mctx);
4053 * Configure and freeze all explicit views. Explicit
4054 * views that have zones were already created at parsing
4055 * time, but views with no zones must be created here.
4057 views = NULL;
4058 (void)cfg_map_get(config, "view", &views);
4059 for (element = cfg_list_first(views);
4060 element != NULL;
4061 element = cfg_list_next(element))
4063 const cfg_obj_t *vconfig = cfg_listelt_value(element);
4064 view = NULL;
4066 CHECK(create_view(vconfig, &viewlist, &view));
4067 INSIST(view != NULL);
4068 CHECK(configure_view(view, config, vconfig,
4069 &cachelist, bindkeys,
4070 ns_g_mctx, &aclconfctx, ISC_TRUE));
4071 dns_view_freeze(view);
4072 dns_view_detach(&view);
4076 * Make sure we have a default view if and only if there
4077 * were no explicit views.
4079 if (views == NULL) {
4081 * No explicit views; there ought to be a default view.
4082 * There may already be one created as a side effect
4083 * of zone statements, or we may have to create one.
4084 * In either case, we need to configure and freeze it.
4086 CHECK(create_view(NULL, &viewlist, &view));
4087 CHECK(configure_view(view, config, NULL,
4088 &cachelist, bindkeys,
4089 ns_g_mctx, &aclconfctx, ISC_TRUE));
4090 dns_view_freeze(view);
4091 dns_view_detach(&view);
4095 * Create (or recreate) the built-in views. Currently
4096 * there is only one, the _bind view, but allow for others.
4098 builtin_views = NULL;
4099 RUNTIME_CHECK(cfg_map_get(ns_g_config, "view",
4100 &builtin_views) == ISC_R_SUCCESS);
4101 for (element = cfg_list_first(builtin_views);
4102 element != NULL;
4103 element = cfg_list_next(element))
4105 const cfg_obj_t *vconfig = cfg_listelt_value(element);
4107 CHECK(create_view(vconfig, &builtin_viewlist, &view));
4108 CHECK(configure_view(view, config, vconfig,
4109 &cachelist, bindkeys,
4110 ns_g_mctx, &aclconfctx, ISC_FALSE));
4112 if (!strcmp(view->name, "_meta")) {
4113 result = add_keydata_zone(view, ns_g_mctx);
4114 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4117 dns_view_freeze(view);
4118 dns_view_detach(&view);
4119 view = NULL;
4122 /* Now combine the two viewlists into one */
4123 ISC_LIST_APPENDLIST(viewlist, builtin_viewlist, link);
4125 /* Swap our new view list with the production one. */
4126 tmpviewlist = server->viewlist;
4127 server->viewlist = viewlist;
4128 viewlist = tmpviewlist;
4130 /* Make the view list available to each of the views */
4131 view = ISC_LIST_HEAD(server->viewlist);
4132 while (view != NULL) {
4133 view->viewlist = &server->viewlist;
4134 view = ISC_LIST_NEXT(view, link);
4137 /* Swap our new cache list with the production one. */
4138 tmpcachelist = server->cachelist;
4139 server->cachelist = cachelist;
4140 cachelist = tmpcachelist;
4142 /* Load the TKEY information from the configuration. */
4143 if (options != NULL) {
4144 dns_tkeyctx_t *t = NULL;
4145 CHECKM(ns_tkeyctx_fromconfig(options, ns_g_mctx, ns_g_entropy,
4146 &t),
4147 "configuring TKEY");
4148 if (server->tkeyctx != NULL)
4149 dns_tkeyctx_destroy(&server->tkeyctx);
4150 server->tkeyctx = t;
4154 * Bind the control port(s).
4156 CHECKM(ns_controls_configure(ns_g_server->controls, config,
4157 &aclconfctx),
4158 "binding control channel(s)");
4161 * Bind the lwresd port(s).
4163 CHECKM(ns_lwresd_configure(ns_g_mctx, config),
4164 "binding lightweight resolver ports");
4167 * Open the source of entropy.
4169 if (first_time) {
4170 obj = NULL;
4171 result = ns_config_get(maps, "random-device", &obj);
4172 if (result != ISC_R_SUCCESS) {
4173 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4174 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4175 "no source of entropy found");
4176 } else {
4177 const char *randomdev = cfg_obj_asstring(obj);
4178 result = isc_entropy_createfilesource(ns_g_entropy,
4179 randomdev);
4180 if (result != ISC_R_SUCCESS)
4181 isc_log_write(ns_g_lctx,
4182 NS_LOGCATEGORY_GENERAL,
4183 NS_LOGMODULE_SERVER,
4184 ISC_LOG_INFO,
4185 "could not open entropy source "
4186 "%s: %s",
4187 randomdev,
4188 isc_result_totext(result));
4189 #ifdef PATH_RANDOMDEV
4190 if (ns_g_fallbackentropy != NULL) {
4191 if (result != ISC_R_SUCCESS) {
4192 isc_log_write(ns_g_lctx,
4193 NS_LOGCATEGORY_GENERAL,
4194 NS_LOGMODULE_SERVER,
4195 ISC_LOG_INFO,
4196 "using pre-chroot entropy source "
4197 "%s",
4198 PATH_RANDOMDEV);
4199 isc_entropy_detach(&ns_g_entropy);
4200 isc_entropy_attach(ns_g_fallbackentropy,
4201 &ns_g_entropy);
4203 isc_entropy_detach(&ns_g_fallbackentropy);
4205 #endif
4210 * Relinquish root privileges.
4212 if (first_time)
4213 ns_os_changeuser();
4215 #ifdef notdef
4217 * Check that the working directory is writable.
4219 if (access(".", W_OK) != 0) {
4220 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4221 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
4222 "the working directory is not writable");
4224 #endif
4227 * Configure the logging system.
4229 * Do this after changing UID to make sure that any log
4230 * files specified in named.conf get created by the
4231 * unprivileged user, not root.
4233 if (ns_g_logstderr) {
4234 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4235 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4236 "ignoring config file logging "
4237 "statement due to -g option");
4238 } else {
4239 const cfg_obj_t *logobj = NULL;
4240 isc_logconfig_t *logc = NULL;
4242 CHECKM(isc_logconfig_create(ns_g_lctx, &logc),
4243 "creating new logging configuration");
4245 logobj = NULL;
4246 (void)cfg_map_get(config, "logging", &logobj);
4247 if (logobj != NULL) {
4248 CHECKM(ns_log_configure(logc, logobj),
4249 "configuring logging");
4250 } else {
4251 CHECKM(ns_log_setdefaultchannels(logc),
4252 "setting up default logging channels");
4253 CHECKM(ns_log_setunmatchedcategory(logc),
4254 "setting up default 'category unmatched'");
4255 CHECKM(ns_log_setdefaultcategory(logc),
4256 "setting up default 'category default'");
4259 result = isc_logconfig_use(ns_g_lctx, logc);
4260 if (result != ISC_R_SUCCESS) {
4261 isc_logconfig_destroy(&logc);
4262 CHECKM(result, "installing logging configuration");
4265 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4266 NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(1),
4267 "now using logging configuration from "
4268 "config file");
4272 * Set the default value of the query logging flag depending
4273 * whether a "queries" category has been defined. This is
4274 * a disgusting hack, but we need to do this for BIND 8
4275 * compatibility.
4277 if (first_time) {
4278 const cfg_obj_t *logobj = NULL;
4279 const cfg_obj_t *categories = NULL;
4281 obj = NULL;
4282 if (ns_config_get(maps, "querylog", &obj) == ISC_R_SUCCESS) {
4283 server->log_queries = cfg_obj_asboolean(obj);
4284 } else {
4286 (void)cfg_map_get(config, "logging", &logobj);
4287 if (logobj != NULL)
4288 (void)cfg_map_get(logobj, "category",
4289 &categories);
4290 if (categories != NULL) {
4291 const cfg_listelt_t *element;
4292 for (element = cfg_list_first(categories);
4293 element != NULL;
4294 element = cfg_list_next(element))
4296 const cfg_obj_t *catobj;
4297 const char *str;
4299 obj = cfg_listelt_value(element);
4300 catobj = cfg_tuple_get(obj, "name");
4301 str = cfg_obj_asstring(catobj);
4302 if (strcasecmp(str, "queries") == 0)
4303 server->log_queries = ISC_TRUE;
4310 obj = NULL;
4311 if (options != NULL &&
4312 cfg_map_get(options, "memstatistics", &obj) == ISC_R_SUCCESS)
4313 ns_g_memstatistics = cfg_obj_asboolean(obj);
4314 else
4315 ns_g_memstatistics =
4316 ISC_TF((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0);
4318 obj = NULL;
4319 if (ns_config_get(maps, "memstatistics-file", &obj) == ISC_R_SUCCESS)
4320 ns_main_setmemstats(cfg_obj_asstring(obj));
4321 else if (ns_g_memstatistics)
4322 ns_main_setmemstats("named.memstats");
4323 else
4324 ns_main_setmemstats(NULL);
4326 obj = NULL;
4327 result = ns_config_get(maps, "statistics-file", &obj);
4328 INSIST(result == ISC_R_SUCCESS);
4329 CHECKM(setstring(server, &server->statsfile, cfg_obj_asstring(obj)),
4330 "strdup");
4332 obj = NULL;
4333 result = ns_config_get(maps, "dump-file", &obj);
4334 INSIST(result == ISC_R_SUCCESS);
4335 CHECKM(setstring(server, &server->dumpfile, cfg_obj_asstring(obj)),
4336 "strdup");
4338 obj = NULL;
4339 result = ns_config_get(maps, "recursing-file", &obj);
4340 INSIST(result == ISC_R_SUCCESS);
4341 CHECKM(setstring(server, &server->recfile, cfg_obj_asstring(obj)),
4342 "strdup");
4344 obj = NULL;
4345 result = ns_config_get(maps, "version", &obj);
4346 if (result == ISC_R_SUCCESS) {
4347 CHECKM(setoptstring(server, &server->version, obj), "strdup");
4348 server->version_set = ISC_TRUE;
4349 } else {
4350 server->version_set = ISC_FALSE;
4353 obj = NULL;
4354 result = ns_config_get(maps, "hostname", &obj);
4355 if (result == ISC_R_SUCCESS) {
4356 CHECKM(setoptstring(server, &server->hostname, obj), "strdup");
4357 server->hostname_set = ISC_TRUE;
4358 } else {
4359 server->hostname_set = ISC_FALSE;
4362 obj = NULL;
4363 result = ns_config_get(maps, "server-id", &obj);
4364 server->server_usehostname = ISC_FALSE;
4365 if (result == ISC_R_SUCCESS && cfg_obj_isboolean(obj)) {
4366 /* The parser translates "hostname" to ISC_TRUE */
4367 server->server_usehostname = cfg_obj_asboolean(obj);
4368 result = setstring(server, &server->server_id, NULL);
4369 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4370 } else if (result == ISC_R_SUCCESS) {
4371 /* Found a quoted string */
4372 CHECKM(setoptstring(server, &server->server_id, obj), "strdup");
4373 } else {
4374 result = setstring(server, &server->server_id, NULL);
4375 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4378 obj = NULL;
4379 result = ns_config_get(maps, "flush-zones-on-shutdown", &obj);
4380 if (result == ISC_R_SUCCESS) {
4381 server->flushonshutdown = cfg_obj_asboolean(obj);
4382 } else {
4383 server->flushonshutdown = ISC_FALSE;
4386 result = ISC_R_SUCCESS;
4388 cleanup:
4389 if (v4portset != NULL)
4390 isc_portset_destroy(ns_g_mctx, &v4portset);
4392 if (v6portset != NULL)
4393 isc_portset_destroy(ns_g_mctx, &v6portset);
4395 cfg_aclconfctx_destroy(&aclconfctx);
4397 if (conf_parser != NULL) {
4398 if (config != NULL)
4399 cfg_obj_destroy(conf_parser, &config);
4400 cfg_parser_destroy(&conf_parser);
4403 if (bindkeys_parser != NULL) {
4404 if (bindkeys != NULL)
4405 cfg_obj_destroy(bindkeys_parser, &bindkeys);
4406 cfg_parser_destroy(&bindkeys_parser);
4409 if (view != NULL)
4410 dns_view_detach(&view);
4413 * This cleans up either the old production view list
4414 * or our temporary list depending on whether they
4415 * were swapped above or not.
4417 for (view = ISC_LIST_HEAD(viewlist);
4418 view != NULL;
4419 view = view_next) {
4420 view_next = ISC_LIST_NEXT(view, link);
4421 ISC_LIST_UNLINK(viewlist, view, link);
4422 if (result == ISC_R_SUCCESS &&
4423 strcmp(view->name, "_bind") != 0)
4424 (void)dns_zt_apply(view->zonetable, ISC_FALSE,
4425 removed, view);
4426 dns_view_detach(&view);
4429 /* Same cleanup for cache list. */
4430 while ((nsc = ISC_LIST_HEAD(cachelist)) != NULL) {
4431 ISC_LIST_UNLINK(cachelist, nsc, link);
4432 dns_cache_detach(&nsc->cache);
4433 isc_mem_put(server->mctx, nsc, sizeof(*nsc));
4437 * Adjust the listening interfaces in accordance with the source
4438 * addresses specified in views and zones.
4440 if (isc_net_probeipv6() == ISC_R_SUCCESS)
4441 adjust_interfaces(server, ns_g_mctx);
4443 /* Relinquish exclusive access to configuration data. */
4444 isc_task_endexclusive(server->task);
4446 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
4447 ISC_LOG_DEBUG(1), "load_configuration: %s",
4448 isc_result_totext(result));
4450 return (result);
4453 static isc_result_t
4454 load_zones(ns_server_t *server, isc_boolean_t stop) {
4455 isc_result_t result;
4456 dns_view_t *view;
4458 result = isc_task_beginexclusive(server->task);
4459 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4462 * Load zone data from disk.
4464 for (view = ISC_LIST_HEAD(server->viewlist);
4465 view != NULL;
4466 view = ISC_LIST_NEXT(view, link))
4468 CHECK(dns_view_load(view, stop));
4472 * Force zone maintenance. Do this after loading
4473 * so that we know when we need to force AXFR of
4474 * slave zones whose master files are missing.
4476 CHECK(dns_zonemgr_forcemaint(server->zonemgr));
4477 cleanup:
4478 isc_task_endexclusive(server->task);
4479 return (result);
4482 static isc_result_t
4483 load_new_zones(ns_server_t *server, isc_boolean_t stop) {
4484 isc_result_t result;
4485 dns_view_t *view;
4487 result = isc_task_beginexclusive(server->task);
4488 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4491 * Load zone data from disk.
4493 for (view = ISC_LIST_HEAD(server->viewlist);
4494 view != NULL;
4495 view = ISC_LIST_NEXT(view, link))
4497 CHECK(dns_view_loadnew(view, stop));
4500 * Force zone maintenance. Do this after loading
4501 * so that we know when we need to force AXFR of
4502 * slave zones whose master files are missing.
4504 dns_zonemgr_resumexfrs(server->zonemgr);
4505 cleanup:
4506 isc_task_endexclusive(server->task);
4507 return (result);
4510 static void
4511 run_server(isc_task_t *task, isc_event_t *event) {
4512 isc_result_t result;
4513 ns_server_t *server = (ns_server_t *)event->ev_arg;
4515 INSIST(task == server->task);
4517 isc_event_free(&event);
4519 CHECKFATAL(dns_dispatchmgr_create(ns_g_mctx, ns_g_entropy,
4520 &ns_g_dispatchmgr),
4521 "creating dispatch manager");
4523 dns_dispatchmgr_setstats(ns_g_dispatchmgr, server->resolverstats);
4525 CHECKFATAL(ns_interfacemgr_create(ns_g_mctx, ns_g_taskmgr,
4526 ns_g_socketmgr, ns_g_dispatchmgr,
4527 &server->interfacemgr),
4528 "creating interface manager");
4530 CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
4531 NULL, NULL, server->task,
4532 interface_timer_tick,
4533 server, &server->interface_timer),
4534 "creating interface timer");
4536 CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
4537 NULL, NULL, server->task,
4538 heartbeat_timer_tick,
4539 server, &server->heartbeat_timer),
4540 "creating heartbeat timer");
4542 CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
4543 NULL, NULL, server->task, pps_timer_tick,
4544 server, &server->pps_timer),
4545 "creating pps timer");
4547 CHECKFATAL(cfg_parser_create(ns_g_mctx, NULL, &ns_g_parser),
4548 "creating default configuration parser");
4550 if (ns_g_lwresdonly)
4551 CHECKFATAL(load_configuration(lwresd_g_conffile, server,
4552 ISC_TRUE),
4553 "loading configuration");
4554 else
4555 CHECKFATAL(load_configuration(ns_g_conffile, server, ISC_TRUE),
4556 "loading configuration");
4558 isc_hash_init();
4560 CHECKFATAL(load_zones(server, ISC_FALSE), "loading zones");
4562 ns_os_started();
4563 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
4564 ISC_LOG_NOTICE, "running");
4567 void
4568 ns_server_flushonshutdown(ns_server_t *server, isc_boolean_t flush) {
4570 REQUIRE(NS_SERVER_VALID(server));
4572 server->flushonshutdown = flush;
4575 static void
4576 shutdown_server(isc_task_t *task, isc_event_t *event) {
4577 isc_result_t result;
4578 dns_view_t *view, *view_next;
4579 ns_server_t *server = (ns_server_t *)event->ev_arg;
4580 isc_boolean_t flush = server->flushonshutdown;
4581 ns_cache_t *nsc;
4583 UNUSED(task);
4584 INSIST(task == server->task);
4586 result = isc_task_beginexclusive(server->task);
4587 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4589 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
4590 ISC_LOG_INFO, "shutting down%s",
4591 flush ? ": flushing changes" : "");
4593 ns_statschannels_shutdown(server);
4594 ns_controls_shutdown(server->controls);
4595 end_reserved_dispatches(server, ISC_TRUE);
4596 cleanup_session_key(server, server->mctx);
4598 cfg_obj_destroy(ns_g_parser, &ns_g_config);
4599 cfg_parser_destroy(&ns_g_parser);
4601 for (view = ISC_LIST_HEAD(server->viewlist);
4602 view != NULL;
4603 view = view_next) {
4604 view_next = ISC_LIST_NEXT(view, link);
4605 ISC_LIST_UNLINK(server->viewlist, view, link);
4606 if (flush)
4607 dns_view_flushanddetach(&view);
4608 else
4609 dns_view_detach(&view);
4612 while ((nsc = ISC_LIST_HEAD(server->cachelist)) != NULL) {
4613 ISC_LIST_UNLINK(server->cachelist, nsc, link);
4614 dns_cache_detach(&nsc->cache);
4615 isc_mem_put(server->mctx, nsc, sizeof(*nsc));
4618 isc_timer_detach(&server->interface_timer);
4619 isc_timer_detach(&server->heartbeat_timer);
4620 isc_timer_detach(&server->pps_timer);
4622 ns_interfacemgr_shutdown(server->interfacemgr);
4623 ns_interfacemgr_detach(&server->interfacemgr);
4625 dns_dispatchmgr_destroy(&ns_g_dispatchmgr);
4627 dns_zonemgr_shutdown(server->zonemgr);
4629 if (ns_g_sessionkey != NULL) {
4630 dns_tsigkey_detach(&ns_g_sessionkey);
4631 dns_name_free(&ns_g_sessionkeyname, server->mctx);
4634 if (server->blackholeacl != NULL)
4635 dns_acl_detach(&server->blackholeacl);
4637 dns_db_detach(&server->in_roothints);
4639 isc_task_endexclusive(server->task);
4641 isc_task_detach(&server->task);
4643 isc_event_free(&event);
4646 void
4647 ns_server_create(isc_mem_t *mctx, ns_server_t **serverp) {
4648 isc_result_t result;
4650 ns_server_t *server = isc_mem_get(mctx, sizeof(*server));
4651 if (server == NULL)
4652 fatal("allocating server object", ISC_R_NOMEMORY);
4654 server->mctx = mctx;
4655 server->task = NULL;
4657 /* Initialize configuration data with default values. */
4659 result = isc_quota_init(&server->xfroutquota, 10);
4660 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4661 result = isc_quota_init(&server->tcpquota, 10);
4662 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4663 result = isc_quota_init(&server->recursionquota, 100);
4664 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4666 result = dns_aclenv_init(mctx, &server->aclenv);
4667 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4669 /* Initialize server data structures. */
4670 server->zonemgr = NULL;
4671 server->interfacemgr = NULL;
4672 ISC_LIST_INIT(server->viewlist);
4673 server->in_roothints = NULL;
4674 server->blackholeacl = NULL;
4676 CHECKFATAL(dns_rootns_create(mctx, dns_rdataclass_in, NULL,
4677 &server->in_roothints),
4678 "setting up root hints");
4680 CHECKFATAL(isc_mutex_init(&server->reload_event_lock),
4681 "initializing reload event lock");
4682 server->reload_event =
4683 isc_event_allocate(ns_g_mctx, server,
4684 NS_EVENT_RELOAD,
4685 ns_server_reload,
4686 server,
4687 sizeof(isc_event_t));
4688 CHECKFATAL(server->reload_event == NULL ?
4689 ISC_R_NOMEMORY : ISC_R_SUCCESS,
4690 "allocating reload event");
4692 CHECKFATAL(dst_lib_init2(ns_g_mctx, ns_g_entropy,
4693 ns_g_engine, ISC_ENTROPY_GOODONLY),
4694 "initializing DST");
4696 server->tkeyctx = NULL;
4697 CHECKFATAL(dns_tkeyctx_create(ns_g_mctx, ns_g_entropy,
4698 &server->tkeyctx),
4699 "creating TKEY context");
4702 * Setup the server task, which is responsible for coordinating
4703 * startup and shutdown of the server.
4705 CHECKFATAL(isc_task_create(ns_g_taskmgr, 0, &server->task),
4706 "creating server task");
4707 isc_task_setname(server->task, "server", server);
4708 CHECKFATAL(isc_task_onshutdown(server->task, shutdown_server, server),
4709 "isc_task_onshutdown");
4710 CHECKFATAL(isc_app_onrun(ns_g_mctx, server->task, run_server, server),
4711 "isc_app_onrun");
4713 server->interface_timer = NULL;
4714 server->heartbeat_timer = NULL;
4715 server->pps_timer = NULL;
4717 server->interface_interval = 0;
4718 server->heartbeat_interval = 0;
4720 CHECKFATAL(dns_zonemgr_create(ns_g_mctx, ns_g_taskmgr, ns_g_timermgr,
4721 ns_g_socketmgr, &server->zonemgr),
4722 "dns_zonemgr_create");
4724 server->statsfile = isc_mem_strdup(server->mctx, "named.stats");
4725 CHECKFATAL(server->statsfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
4726 "isc_mem_strdup");
4727 server->nsstats = NULL;
4728 server->rcvquerystats = NULL;
4729 server->opcodestats = NULL;
4730 server->zonestats = NULL;
4731 server->resolverstats = NULL;
4732 server->sockstats = NULL;
4733 CHECKFATAL(isc_stats_create(server->mctx, &server->sockstats,
4734 isc_sockstatscounter_max),
4735 "isc_stats_create");
4736 isc_socketmgr_setstats(ns_g_socketmgr, server->sockstats);
4738 server->bindkeysfile = isc_mem_strdup(server->mctx, "bind.keys");
4739 CHECKFATAL(server->bindkeysfile == NULL ? ISC_R_NOMEMORY :
4740 ISC_R_SUCCESS,
4741 "isc_mem_strdup");
4743 server->managedkeys = ISC_FALSE;
4745 server->dumpfile = isc_mem_strdup(server->mctx, "named_dump.db");
4746 CHECKFATAL(server->dumpfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
4747 "isc_mem_strdup");
4749 server->recfile = isc_mem_strdup(server->mctx, "named.recursing");
4750 CHECKFATAL(server->recfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
4751 "isc_mem_strdup");
4753 server->hostname_set = ISC_FALSE;
4754 server->hostname = NULL;
4755 server->version_set = ISC_FALSE;
4756 server->version = NULL;
4757 server->server_usehostname = ISC_FALSE;
4758 server->server_id = NULL;
4760 CHECKFATAL(isc_stats_create(ns_g_mctx, &server->nsstats,
4761 dns_nsstatscounter_max),
4762 "dns_stats_create (server)");
4764 CHECKFATAL(dns_rdatatypestats_create(ns_g_mctx,
4765 &server->rcvquerystats),
4766 "dns_stats_create (rcvquery)");
4768 CHECKFATAL(dns_opcodestats_create(ns_g_mctx, &server->opcodestats),
4769 "dns_stats_create (opcode)");
4771 CHECKFATAL(isc_stats_create(ns_g_mctx, &server->zonestats,
4772 dns_zonestatscounter_max),
4773 "dns_stats_create (zone)");
4775 CHECKFATAL(isc_stats_create(ns_g_mctx, &server->resolverstats,
4776 dns_resstatscounter_max),
4777 "dns_stats_create (resolver)");
4779 server->flushonshutdown = ISC_FALSE;
4780 server->log_queries = ISC_FALSE;
4782 server->controls = NULL;
4783 CHECKFATAL(ns_controls_create(server, &server->controls),
4784 "ns_controls_create");
4785 server->dispatchgen = 0;
4786 ISC_LIST_INIT(server->dispatches);
4788 ISC_LIST_INIT(server->statschannels);
4790 ISC_LIST_INIT(server->cachelist);
4792 server->sessionkey = NULL;
4793 server->session_keyfile = NULL;
4794 server->session_keyname = NULL;
4795 server->session_keyalg = DST_ALG_UNKNOWN;
4796 server->session_keybits = 0;
4798 server->magic = NS_SERVER_MAGIC;
4799 *serverp = server;
4802 void
4803 ns_server_destroy(ns_server_t **serverp) {
4804 ns_server_t *server = *serverp;
4805 REQUIRE(NS_SERVER_VALID(server));
4807 ns_controls_destroy(&server->controls);
4809 isc_stats_detach(&server->nsstats);
4810 dns_stats_detach(&server->rcvquerystats);
4811 dns_stats_detach(&server->opcodestats);
4812 isc_stats_detach(&server->zonestats);
4813 isc_stats_detach(&server->resolverstats);
4814 isc_stats_detach(&server->sockstats);
4816 isc_mem_free(server->mctx, server->statsfile);
4817 isc_mem_free(server->mctx, server->bindkeysfile);
4818 isc_mem_free(server->mctx, server->dumpfile);
4819 isc_mem_free(server->mctx, server->recfile);
4821 if (server->version != NULL)
4822 isc_mem_free(server->mctx, server->version);
4823 if (server->hostname != NULL)
4824 isc_mem_free(server->mctx, server->hostname);
4825 if (server->server_id != NULL)
4826 isc_mem_free(server->mctx, server->server_id);
4828 dns_zonemgr_detach(&server->zonemgr);
4830 if (server->tkeyctx != NULL)
4831 dns_tkeyctx_destroy(&server->tkeyctx);
4833 dst_lib_destroy();
4835 isc_event_free(&server->reload_event);
4837 INSIST(ISC_LIST_EMPTY(server->viewlist));
4838 INSIST(ISC_LIST_EMPTY(server->cachelist));
4840 dns_aclenv_destroy(&server->aclenv);
4842 isc_quota_destroy(&server->recursionquota);
4843 isc_quota_destroy(&server->tcpquota);
4844 isc_quota_destroy(&server->xfroutquota);
4846 server->magic = 0;
4847 isc_mem_put(server->mctx, server, sizeof(*server));
4848 *serverp = NULL;
4851 static void
4852 fatal(const char *msg, isc_result_t result) {
4853 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
4854 ISC_LOG_CRITICAL, "%s: %s", msg,
4855 isc_result_totext(result));
4856 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
4857 ISC_LOG_CRITICAL, "exiting (due to fatal error)");
4858 exit(1);
4861 static void
4862 start_reserved_dispatches(ns_server_t *server) {
4864 REQUIRE(NS_SERVER_VALID(server));
4866 server->dispatchgen++;
4869 static void
4870 end_reserved_dispatches(ns_server_t *server, isc_boolean_t all) {
4871 ns_dispatch_t *dispatch, *nextdispatch;
4873 REQUIRE(NS_SERVER_VALID(server));
4875 for (dispatch = ISC_LIST_HEAD(server->dispatches);
4876 dispatch != NULL;
4877 dispatch = nextdispatch) {
4878 nextdispatch = ISC_LIST_NEXT(dispatch, link);
4879 if (!all && server->dispatchgen == dispatch-> dispatchgen)
4880 continue;
4881 ISC_LIST_UNLINK(server->dispatches, dispatch, link);
4882 dns_dispatch_detach(&dispatch->dispatch);
4883 isc_mem_put(server->mctx, dispatch, sizeof(*dispatch));
4887 void
4888 ns_add_reserved_dispatch(ns_server_t *server, const isc_sockaddr_t *addr) {
4889 ns_dispatch_t *dispatch;
4890 in_port_t port;
4891 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
4892 isc_result_t result;
4893 unsigned int attrs, attrmask;
4895 REQUIRE(NS_SERVER_VALID(server));
4897 port = isc_sockaddr_getport(addr);
4898 if (port == 0 || port >= 1024)
4899 return;
4901 for (dispatch = ISC_LIST_HEAD(server->dispatches);
4902 dispatch != NULL;
4903 dispatch = ISC_LIST_NEXT(dispatch, link)) {
4904 if (isc_sockaddr_equal(&dispatch->addr, addr))
4905 break;
4907 if (dispatch != NULL) {
4908 dispatch->dispatchgen = server->dispatchgen;
4909 return;
4912 dispatch = isc_mem_get(server->mctx, sizeof(*dispatch));
4913 if (dispatch == NULL) {
4914 result = ISC_R_NOMEMORY;
4915 goto cleanup;
4918 dispatch->addr = *addr;
4919 dispatch->dispatchgen = server->dispatchgen;
4920 dispatch->dispatch = NULL;
4922 attrs = 0;
4923 attrs |= DNS_DISPATCHATTR_UDP;
4924 switch (isc_sockaddr_pf(addr)) {
4925 case AF_INET:
4926 attrs |= DNS_DISPATCHATTR_IPV4;
4927 break;
4928 case AF_INET6:
4929 attrs |= DNS_DISPATCHATTR_IPV6;
4930 break;
4931 default:
4932 result = ISC_R_NOTIMPLEMENTED;
4933 goto cleanup;
4935 attrmask = 0;
4936 attrmask |= DNS_DISPATCHATTR_UDP;
4937 attrmask |= DNS_DISPATCHATTR_TCP;
4938 attrmask |= DNS_DISPATCHATTR_IPV4;
4939 attrmask |= DNS_DISPATCHATTR_IPV6;
4941 result = dns_dispatch_getudp(ns_g_dispatchmgr, ns_g_socketmgr,
4942 ns_g_taskmgr, &dispatch->addr, 4096,
4943 1000, 32768, 16411, 16433,
4944 attrs, attrmask, &dispatch->dispatch);
4945 if (result != ISC_R_SUCCESS)
4946 goto cleanup;
4948 ISC_LIST_INITANDPREPEND(server->dispatches, dispatch, link);
4950 return;
4952 cleanup:
4953 if (dispatch != NULL)
4954 isc_mem_put(server->mctx, dispatch, sizeof(*dispatch));
4955 isc_sockaddr_format(addr, addrbuf, sizeof(addrbuf));
4956 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4957 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
4958 "unable to create dispatch for reserved port %s: %s",
4959 addrbuf, isc_result_totext(result));
4963 static isc_result_t
4964 loadconfig(ns_server_t *server) {
4965 isc_result_t result;
4966 start_reserved_dispatches(server);
4967 result = load_configuration(ns_g_lwresdonly ?
4968 lwresd_g_conffile : ns_g_conffile,
4969 server, ISC_FALSE);
4970 if (result == ISC_R_SUCCESS) {
4971 end_reserved_dispatches(server, ISC_FALSE);
4972 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4973 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4974 "reloading configuration succeeded");
4975 } else {
4976 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4977 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
4978 "reloading configuration failed: %s",
4979 isc_result_totext(result));
4981 return (result);
4984 static isc_result_t
4985 reload(ns_server_t *server) {
4986 isc_result_t result;
4987 CHECK(loadconfig(server));
4989 result = load_zones(server, ISC_FALSE);
4990 if (result == ISC_R_SUCCESS)
4991 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4992 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4993 "reloading zones succeeded");
4994 else
4995 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4996 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
4997 "reloading zones failed: %s",
4998 isc_result_totext(result));
5000 cleanup:
5001 return (result);
5004 static void
5005 reconfig(ns_server_t *server) {
5006 isc_result_t result;
5007 CHECK(loadconfig(server));
5009 result = load_new_zones(server, ISC_FALSE);
5010 if (result == ISC_R_SUCCESS)
5011 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5012 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5013 "any newly configured zones are now loaded");
5014 else
5015 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5016 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5017 "loading new zones failed: %s",
5018 isc_result_totext(result));
5020 cleanup: ;
5024 * Handle a reload event (from SIGHUP).
5026 static void
5027 ns_server_reload(isc_task_t *task, isc_event_t *event) {
5028 ns_server_t *server = (ns_server_t *)event->ev_arg;
5030 INSIST(task = server->task);
5031 UNUSED(task);
5033 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5034 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5035 "received SIGHUP signal to reload zones");
5036 (void)reload(server);
5038 LOCK(&server->reload_event_lock);
5039 INSIST(server->reload_event == NULL);
5040 server->reload_event = event;
5041 UNLOCK(&server->reload_event_lock);
5044 void
5045 ns_server_reloadwanted(ns_server_t *server) {
5046 LOCK(&server->reload_event_lock);
5047 if (server->reload_event != NULL)
5048 isc_task_send(server->task, &server->reload_event);
5049 UNLOCK(&server->reload_event_lock);
5052 static char *
5053 next_token(char **stringp, const char *delim) {
5054 char *res;
5056 do {
5057 res = strsep(stringp, delim);
5058 if (res == NULL)
5059 break;
5060 } while (*res == '\0');
5061 return (res);
5065 * Find the zone specified in the control channel command 'args',
5066 * if any. If a zone is specified, point '*zonep' at it, otherwise
5067 * set '*zonep' to NULL.
5069 static isc_result_t
5070 zone_from_args(ns_server_t *server, char *args, dns_zone_t **zonep) {
5071 char *input, *ptr;
5072 const char *zonetxt;
5073 char *classtxt;
5074 const char *viewtxt = NULL;
5075 dns_fixedname_t name;
5076 isc_result_t result;
5077 isc_buffer_t buf;
5078 dns_view_t *view = NULL;
5079 dns_rdataclass_t rdclass;
5081 REQUIRE(zonep != NULL && *zonep == NULL);
5083 input = args;
5085 /* Skip the command name. */
5086 ptr = next_token(&input, " \t");
5087 if (ptr == NULL)
5088 return (ISC_R_UNEXPECTEDEND);
5090 /* Look for the zone name. */
5091 zonetxt = next_token(&input, " \t");
5092 if (zonetxt == NULL)
5093 return (ISC_R_SUCCESS);
5095 /* Look for the optional class name. */
5096 classtxt = next_token(&input, " \t");
5097 if (classtxt != NULL) {
5098 /* Look for the optional view name. */
5099 viewtxt = next_token(&input, " \t");
5102 isc_buffer_init(&buf, zonetxt, strlen(zonetxt));
5103 isc_buffer_add(&buf, strlen(zonetxt));
5104 dns_fixedname_init(&name);
5105 result = dns_name_fromtext(dns_fixedname_name(&name),
5106 &buf, dns_rootname, 0, NULL);
5107 if (result != ISC_R_SUCCESS)
5108 goto fail1;
5110 if (classtxt != NULL) {
5111 isc_textregion_t r;
5112 r.base = classtxt;
5113 r.length = strlen(classtxt);
5114 result = dns_rdataclass_fromtext(&rdclass, &r);
5115 if (result != ISC_R_SUCCESS)
5116 goto fail1;
5117 } else
5118 rdclass = dns_rdataclass_in;
5120 if (viewtxt == NULL) {
5121 result = dns_viewlist_findzone(&server->viewlist,
5122 dns_fixedname_name(&name),
5123 ISC_TF(classtxt == NULL),
5124 rdclass, zonep);
5125 } else {
5126 result = dns_viewlist_find(&server->viewlist, viewtxt,
5127 rdclass, &view);
5128 if (result != ISC_R_SUCCESS)
5129 goto fail1;
5131 result = dns_zt_find(view->zonetable, dns_fixedname_name(&name),
5132 0, NULL, zonep);
5133 dns_view_detach(&view);
5136 /* Partial match? */
5137 if (result != ISC_R_SUCCESS && *zonep != NULL)
5138 dns_zone_detach(zonep);
5139 fail1:
5140 return (result);
5144 * Act on a "retransfer" command from the command channel.
5146 isc_result_t
5147 ns_server_retransfercommand(ns_server_t *server, char *args) {
5148 isc_result_t result;
5149 dns_zone_t *zone = NULL;
5150 dns_zonetype_t type;
5152 result = zone_from_args(server, args, &zone);
5153 if (result != ISC_R_SUCCESS)
5154 return (result);
5155 if (zone == NULL)
5156 return (ISC_R_UNEXPECTEDEND);
5157 type = dns_zone_gettype(zone);
5158 if (type == dns_zone_slave || type == dns_zone_stub)
5159 dns_zone_forcereload(zone);
5160 else
5161 result = ISC_R_NOTFOUND;
5162 dns_zone_detach(&zone);
5163 return (result);
5167 * Act on a "reload" command from the command channel.
5169 isc_result_t
5170 ns_server_reloadcommand(ns_server_t *server, char *args, isc_buffer_t *text) {
5171 isc_result_t result;
5172 dns_zone_t *zone = NULL;
5173 dns_zonetype_t type;
5174 const char *msg = NULL;
5176 result = zone_from_args(server, args, &zone);
5177 if (result != ISC_R_SUCCESS)
5178 return (result);
5179 if (zone == NULL) {
5180 result = reload(server);
5181 if (result == ISC_R_SUCCESS)
5182 msg = "server reload successful";
5183 } else {
5184 type = dns_zone_gettype(zone);
5185 if (type == dns_zone_slave || type == dns_zone_stub) {
5186 dns_zone_refresh(zone);
5187 dns_zone_detach(&zone);
5188 msg = "zone refresh queued";
5189 } else {
5190 result = dns_zone_load(zone);
5191 dns_zone_detach(&zone);
5192 switch (result) {
5193 case ISC_R_SUCCESS:
5194 msg = "zone reload successful";
5195 break;
5196 case DNS_R_CONTINUE:
5197 msg = "zone reload queued";
5198 result = ISC_R_SUCCESS;
5199 break;
5200 case DNS_R_UPTODATE:
5201 msg = "zone reload up-to-date";
5202 result = ISC_R_SUCCESS;
5203 break;
5204 default:
5205 /* failure message will be generated by rndc */
5206 break;
5210 if (msg != NULL && strlen(msg) < isc_buffer_availablelength(text))
5211 isc_buffer_putmem(text, (const unsigned char *)msg,
5212 strlen(msg) + 1);
5213 return (result);
5217 * Act on a "reconfig" command from the command channel.
5219 isc_result_t
5220 ns_server_reconfigcommand(ns_server_t *server, char *args) {
5221 UNUSED(args);
5223 reconfig(server);
5224 return (ISC_R_SUCCESS);
5228 * Act on a "notify" command from the command channel.
5230 isc_result_t
5231 ns_server_notifycommand(ns_server_t *server, char *args, isc_buffer_t *text) {
5232 isc_result_t result;
5233 dns_zone_t *zone = NULL;
5234 const unsigned char msg[] = "zone notify queued";
5236 result = zone_from_args(server, args, &zone);
5237 if (result != ISC_R_SUCCESS)
5238 return (result);
5239 if (zone == NULL)
5240 return (ISC_R_UNEXPECTEDEND);
5242 dns_zone_notify(zone);
5243 dns_zone_detach(&zone);
5244 if (sizeof(msg) <= isc_buffer_availablelength(text))
5245 isc_buffer_putmem(text, msg, sizeof(msg));
5247 return (ISC_R_SUCCESS);
5251 * Act on a "refresh" command from the command channel.
5253 isc_result_t
5254 ns_server_refreshcommand(ns_server_t *server, char *args, isc_buffer_t *text) {
5255 isc_result_t result;
5256 dns_zone_t *zone = NULL;
5257 const unsigned char msg1[] = "zone refresh queued";
5258 const unsigned char msg2[] = "not a slave or stub zone";
5259 dns_zonetype_t type;
5261 result = zone_from_args(server, args, &zone);
5262 if (result != ISC_R_SUCCESS)
5263 return (result);
5264 if (zone == NULL)
5265 return (ISC_R_UNEXPECTEDEND);
5267 type = dns_zone_gettype(zone);
5268 if (type == dns_zone_slave || type == dns_zone_stub) {
5269 dns_zone_refresh(zone);
5270 dns_zone_detach(&zone);
5271 if (sizeof(msg1) <= isc_buffer_availablelength(text))
5272 isc_buffer_putmem(text, msg1, sizeof(msg1));
5273 return (ISC_R_SUCCESS);
5276 dns_zone_detach(&zone);
5277 if (sizeof(msg2) <= isc_buffer_availablelength(text))
5278 isc_buffer_putmem(text, msg2, sizeof(msg2));
5279 return (ISC_R_FAILURE);
5282 isc_result_t
5283 ns_server_togglequerylog(ns_server_t *server) {
5284 server->log_queries = server->log_queries ? ISC_FALSE : ISC_TRUE;
5286 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5287 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5288 "query logging is now %s",
5289 server->log_queries ? "on" : "off");
5290 return (ISC_R_SUCCESS);
5293 static isc_result_t
5294 ns_listenlist_fromconfig(const cfg_obj_t *listenlist, const cfg_obj_t *config,
5295 cfg_aclconfctx_t *actx,
5296 isc_mem_t *mctx, ns_listenlist_t **target)
5298 isc_result_t result;
5299 const cfg_listelt_t *element;
5300 ns_listenlist_t *dlist = NULL;
5302 REQUIRE(target != NULL && *target == NULL);
5304 result = ns_listenlist_create(mctx, &dlist);
5305 if (result != ISC_R_SUCCESS)
5306 return (result);
5308 for (element = cfg_list_first(listenlist);
5309 element != NULL;
5310 element = cfg_list_next(element))
5312 ns_listenelt_t *delt = NULL;
5313 const cfg_obj_t *listener = cfg_listelt_value(element);
5314 result = ns_listenelt_fromconfig(listener, config, actx,
5315 mctx, &delt);
5316 if (result != ISC_R_SUCCESS)
5317 goto cleanup;
5318 ISC_LIST_APPEND(dlist->elts, delt, link);
5320 *target = dlist;
5321 return (ISC_R_SUCCESS);
5323 cleanup:
5324 ns_listenlist_detach(&dlist);
5325 return (result);
5329 * Create a listen list from the corresponding configuration
5330 * data structure.
5332 static isc_result_t
5333 ns_listenelt_fromconfig(const cfg_obj_t *listener, const cfg_obj_t *config,
5334 cfg_aclconfctx_t *actx,
5335 isc_mem_t *mctx, ns_listenelt_t **target)
5337 isc_result_t result;
5338 const cfg_obj_t *portobj;
5339 in_port_t port;
5340 ns_listenelt_t *delt = NULL;
5341 REQUIRE(target != NULL && *target == NULL);
5343 portobj = cfg_tuple_get(listener, "port");
5344 if (!cfg_obj_isuint32(portobj)) {
5345 if (ns_g_port != 0) {
5346 port = ns_g_port;
5347 } else {
5348 result = ns_config_getport(config, &port);
5349 if (result != ISC_R_SUCCESS)
5350 return (result);
5352 } else {
5353 if (cfg_obj_asuint32(portobj) >= ISC_UINT16_MAX) {
5354 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
5355 "port value '%u' is out of range",
5356 cfg_obj_asuint32(portobj));
5357 return (ISC_R_RANGE);
5359 port = (in_port_t)cfg_obj_asuint32(portobj);
5362 result = ns_listenelt_create(mctx, port, NULL, &delt);
5363 if (result != ISC_R_SUCCESS)
5364 return (result);
5366 result = cfg_acl_fromconfig(cfg_tuple_get(listener, "acl"),
5367 config, ns_g_lctx, actx, mctx, 0,
5368 &delt->acl);
5369 if (result != ISC_R_SUCCESS) {
5370 ns_listenelt_destroy(delt);
5371 return (result);
5373 *target = delt;
5374 return (ISC_R_SUCCESS);
5377 isc_result_t
5378 ns_server_dumpstats(ns_server_t *server) {
5379 isc_result_t result;
5380 FILE *fp = NULL;
5382 CHECKMF(isc_stdio_open(server->statsfile, "a", &fp),
5383 "could not open statistics dump file", server->statsfile);
5385 result = ns_stats_dump(server, fp);
5386 CHECK(result);
5388 cleanup:
5389 if (fp != NULL)
5390 (void)isc_stdio_close(fp);
5391 if (result == ISC_R_SUCCESS)
5392 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5393 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5394 "dumpstats complete");
5395 else
5396 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5397 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5398 "dumpstats failed: %s",
5399 dns_result_totext(result));
5400 return (result);
5403 static isc_result_t
5404 add_zone_tolist(dns_zone_t *zone, void *uap) {
5405 struct dumpcontext *dctx = uap;
5406 struct zonelistentry *zle;
5408 zle = isc_mem_get(dctx->mctx, sizeof *zle);
5409 if (zle == NULL)
5410 return (ISC_R_NOMEMORY);
5411 zle->zone = NULL;
5412 dns_zone_attach(zone, &zle->zone);
5413 ISC_LINK_INIT(zle, link);
5414 ISC_LIST_APPEND(ISC_LIST_TAIL(dctx->viewlist)->zonelist, zle, link);
5415 return (ISC_R_SUCCESS);
5418 static isc_result_t
5419 add_view_tolist(struct dumpcontext *dctx, dns_view_t *view) {
5420 struct viewlistentry *vle;
5421 isc_result_t result = ISC_R_SUCCESS;
5424 * Prevent duplicate views.
5426 for (vle = ISC_LIST_HEAD(dctx->viewlist);
5427 vle != NULL;
5428 vle = ISC_LIST_NEXT(vle, link))
5429 if (vle->view == view)
5430 return (ISC_R_SUCCESS);
5432 vle = isc_mem_get(dctx->mctx, sizeof *vle);
5433 if (vle == NULL)
5434 return (ISC_R_NOMEMORY);
5435 vle->view = NULL;
5436 dns_view_attach(view, &vle->view);
5437 ISC_LINK_INIT(vle, link);
5438 ISC_LIST_INIT(vle->zonelist);
5439 ISC_LIST_APPEND(dctx->viewlist, vle, link);
5440 if (dctx->dumpzones)
5441 result = dns_zt_apply(view->zonetable, ISC_TRUE,
5442 add_zone_tolist, dctx);
5443 return (result);
5446 static void
5447 dumpcontext_destroy(struct dumpcontext *dctx) {
5448 struct viewlistentry *vle;
5449 struct zonelistentry *zle;
5451 vle = ISC_LIST_HEAD(dctx->viewlist);
5452 while (vle != NULL) {
5453 ISC_LIST_UNLINK(dctx->viewlist, vle, link);
5454 zle = ISC_LIST_HEAD(vle->zonelist);
5455 while (zle != NULL) {
5456 ISC_LIST_UNLINK(vle->zonelist, zle, link);
5457 dns_zone_detach(&zle->zone);
5458 isc_mem_put(dctx->mctx, zle, sizeof *zle);
5459 zle = ISC_LIST_HEAD(vle->zonelist);
5461 dns_view_detach(&vle->view);
5462 isc_mem_put(dctx->mctx, vle, sizeof *vle);
5463 vle = ISC_LIST_HEAD(dctx->viewlist);
5465 if (dctx->version != NULL)
5466 dns_db_closeversion(dctx->db, &dctx->version, ISC_FALSE);
5467 if (dctx->db != NULL)
5468 dns_db_detach(&dctx->db);
5469 if (dctx->cache != NULL)
5470 dns_db_detach(&dctx->cache);
5471 if (dctx->task != NULL)
5472 isc_task_detach(&dctx->task);
5473 if (dctx->fp != NULL)
5474 (void)isc_stdio_close(dctx->fp);
5475 if (dctx->mdctx != NULL)
5476 dns_dumpctx_detach(&dctx->mdctx);
5477 isc_mem_put(dctx->mctx, dctx, sizeof *dctx);
5480 static void
5481 dumpdone(void *arg, isc_result_t result) {
5482 struct dumpcontext *dctx = arg;
5483 char buf[1024+32];
5484 const dns_master_style_t *style;
5486 if (result != ISC_R_SUCCESS)
5487 goto cleanup;
5488 if (dctx->mdctx != NULL)
5489 dns_dumpctx_detach(&dctx->mdctx);
5490 if (dctx->view == NULL) {
5491 dctx->view = ISC_LIST_HEAD(dctx->viewlist);
5492 if (dctx->view == NULL)
5493 goto done;
5494 INSIST(dctx->zone == NULL);
5495 } else
5496 goto resume;
5497 nextview:
5498 fprintf(dctx->fp, ";\n; Start view %s\n;\n", dctx->view->view->name);
5499 resume:
5500 if (dctx->dumpcache && dns_view_iscacheshared(dctx->view->view)) {
5501 fprintf(dctx->fp,
5502 ";\n; Cache of view '%s' is shared as '%s'\n",
5503 dctx->view->view->name,
5504 dns_cache_getname(dctx->view->view->cache));
5505 } else if (dctx->zone == NULL && dctx->cache == NULL &&
5506 dctx->dumpcache)
5508 style = &dns_master_style_cache;
5509 /* start cache dump */
5510 if (dctx->view->view->cachedb != NULL)
5511 dns_db_attach(dctx->view->view->cachedb, &dctx->cache);
5512 if (dctx->cache != NULL) {
5513 fprintf(dctx->fp,
5514 ";\n; Cache dump of view '%s' (cache %s)\n;\n",
5515 dctx->view->view->name,
5516 dns_cache_getname(dctx->view->view->cache));
5517 result = dns_master_dumptostreaminc(dctx->mctx,
5518 dctx->cache, NULL,
5519 style, dctx->fp,
5520 dctx->task,
5521 dumpdone, dctx,
5522 &dctx->mdctx);
5523 if (result == DNS_R_CONTINUE)
5524 return;
5525 if (result == ISC_R_NOTIMPLEMENTED)
5526 fprintf(dctx->fp, "; %s\n",
5527 dns_result_totext(result));
5528 else if (result != ISC_R_SUCCESS)
5529 goto cleanup;
5532 if (dctx->cache != NULL) {
5533 dns_adb_dump(dctx->view->view->adb, dctx->fp);
5534 dns_db_detach(&dctx->cache);
5536 if (dctx->dumpzones) {
5537 style = &dns_master_style_full;
5538 nextzone:
5539 if (dctx->version != NULL)
5540 dns_db_closeversion(dctx->db, &dctx->version,
5541 ISC_FALSE);
5542 if (dctx->db != NULL)
5543 dns_db_detach(&dctx->db);
5544 if (dctx->zone == NULL)
5545 dctx->zone = ISC_LIST_HEAD(dctx->view->zonelist);
5546 else
5547 dctx->zone = ISC_LIST_NEXT(dctx->zone, link);
5548 if (dctx->zone != NULL) {
5549 /* start zone dump */
5550 dns_zone_name(dctx->zone->zone, buf, sizeof(buf));
5551 fprintf(dctx->fp, ";\n; Zone dump of '%s'\n;\n", buf);
5552 result = dns_zone_getdb(dctx->zone->zone, &dctx->db);
5553 if (result != ISC_R_SUCCESS) {
5554 fprintf(dctx->fp, "; %s\n",
5555 dns_result_totext(result));
5556 goto nextzone;
5558 dns_db_currentversion(dctx->db, &dctx->version);
5559 result = dns_master_dumptostreaminc(dctx->mctx,
5560 dctx->db,
5561 dctx->version,
5562 style, dctx->fp,
5563 dctx->task,
5564 dumpdone, dctx,
5565 &dctx->mdctx);
5566 if (result == DNS_R_CONTINUE)
5567 return;
5568 if (result == ISC_R_NOTIMPLEMENTED) {
5569 fprintf(dctx->fp, "; %s\n",
5570 dns_result_totext(result));
5571 result = ISC_R_SUCCESS;
5572 goto nextzone;
5574 if (result != ISC_R_SUCCESS)
5575 goto cleanup;
5578 if (dctx->view != NULL)
5579 dctx->view = ISC_LIST_NEXT(dctx->view, link);
5580 if (dctx->view != NULL)
5581 goto nextview;
5582 done:
5583 fprintf(dctx->fp, "; Dump complete\n");
5584 result = isc_stdio_flush(dctx->fp);
5585 if (result == ISC_R_SUCCESS)
5586 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5587 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5588 "dumpdb complete");
5589 cleanup:
5590 if (result != ISC_R_SUCCESS)
5591 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5592 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5593 "dumpdb failed: %s", dns_result_totext(result));
5594 dumpcontext_destroy(dctx);
5597 isc_result_t
5598 ns_server_dumpdb(ns_server_t *server, char *args) {
5599 struct dumpcontext *dctx = NULL;
5600 dns_view_t *view;
5601 isc_result_t result;
5602 char *ptr;
5603 const char *sep;
5605 /* Skip the command name. */
5606 ptr = next_token(&args, " \t");
5607 if (ptr == NULL)
5608 return (ISC_R_UNEXPECTEDEND);
5610 dctx = isc_mem_get(server->mctx, sizeof(*dctx));
5611 if (dctx == NULL)
5612 return (ISC_R_NOMEMORY);
5614 dctx->mctx = server->mctx;
5615 dctx->dumpcache = ISC_TRUE;
5616 dctx->dumpzones = ISC_FALSE;
5617 dctx->fp = NULL;
5618 ISC_LIST_INIT(dctx->viewlist);
5619 dctx->view = NULL;
5620 dctx->zone = NULL;
5621 dctx->cache = NULL;
5622 dctx->mdctx = NULL;
5623 dctx->db = NULL;
5624 dctx->cache = NULL;
5625 dctx->task = NULL;
5626 dctx->version = NULL;
5627 isc_task_attach(server->task, &dctx->task);
5629 CHECKMF(isc_stdio_open(server->dumpfile, "w", &dctx->fp),
5630 "could not open dump file", server->dumpfile);
5632 sep = (args == NULL) ? "" : ": ";
5633 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5634 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5635 "dumpdb started%s%s", sep, (args != NULL) ? args : "");
5637 ptr = next_token(&args, " \t");
5638 if (ptr != NULL && strcmp(ptr, "-all") == 0) {
5639 dctx->dumpzones = ISC_TRUE;
5640 dctx->dumpcache = ISC_TRUE;
5641 ptr = next_token(&args, " \t");
5642 } else if (ptr != NULL && strcmp(ptr, "-cache") == 0) {
5643 dctx->dumpzones = ISC_FALSE;
5644 dctx->dumpcache = ISC_TRUE;
5645 ptr = next_token(&args, " \t");
5646 } else if (ptr != NULL && strcmp(ptr, "-zones") == 0) {
5647 dctx->dumpzones = ISC_TRUE;
5648 dctx->dumpcache = ISC_FALSE;
5649 ptr = next_token(&args, " \t");
5652 nextview:
5653 for (view = ISC_LIST_HEAD(server->viewlist);
5654 view != NULL;
5655 view = ISC_LIST_NEXT(view, link))
5657 if (ptr != NULL && strcmp(view->name, ptr) != 0)
5658 continue;
5659 CHECK(add_view_tolist(dctx, view));
5661 if (ptr != NULL) {
5662 ptr = next_token(&args, " \t");
5663 if (ptr != NULL)
5664 goto nextview;
5666 dumpdone(dctx, ISC_R_SUCCESS);
5667 return (ISC_R_SUCCESS);
5669 cleanup:
5670 if (dctx != NULL)
5671 dumpcontext_destroy(dctx);
5672 return (result);
5675 isc_result_t
5676 ns_server_dumprecursing(ns_server_t *server) {
5677 FILE *fp = NULL;
5678 isc_result_t result;
5680 CHECKMF(isc_stdio_open(server->recfile, "w", &fp),
5681 "could not open dump file", server->recfile);
5682 fprintf(fp,";\n; Recursing Queries\n;\n");
5683 ns_interfacemgr_dumprecursing(fp, server->interfacemgr);
5684 fprintf(fp, "; Dump complete\n");
5686 cleanup:
5687 if (fp != NULL)
5688 result = isc_stdio_close(fp);
5689 if (result == ISC_R_SUCCESS)
5690 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5691 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5692 "dumprecursing complete");
5693 else
5694 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5695 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5696 "dumprecursing failed: %s",
5697 dns_result_totext(result));
5698 return (result);
5701 isc_result_t
5702 ns_server_setdebuglevel(ns_server_t *server, char *args) {
5703 char *ptr;
5704 char *levelstr;
5705 char *endp;
5706 long newlevel;
5708 UNUSED(server);
5710 /* Skip the command name. */
5711 ptr = next_token(&args, " \t");
5712 if (ptr == NULL)
5713 return (ISC_R_UNEXPECTEDEND);
5715 /* Look for the new level name. */
5716 levelstr = next_token(&args, " \t");
5717 if (levelstr == NULL) {
5718 if (ns_g_debuglevel < 99)
5719 ns_g_debuglevel++;
5720 } else {
5721 newlevel = strtol(levelstr, &endp, 10);
5722 if (*endp != '\0' || newlevel < 0 || newlevel > 99)
5723 return (ISC_R_RANGE);
5724 ns_g_debuglevel = (unsigned int)newlevel;
5726 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
5727 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5728 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5729 "debug level is now %d", ns_g_debuglevel);
5730 return (ISC_R_SUCCESS);
5733 isc_result_t
5734 ns_server_validation(ns_server_t *server, char *args) {
5735 char *ptr, *viewname;
5736 dns_view_t *view;
5737 isc_boolean_t changed = ISC_FALSE;
5738 isc_result_t result;
5739 isc_boolean_t enable;
5741 /* Skip the command name. */
5742 ptr = next_token(&args, " \t");
5743 if (ptr == NULL)
5744 return (ISC_R_UNEXPECTEDEND);
5746 /* Find out what we are to do. */
5747 ptr = next_token(&args, " \t");
5748 if (ptr == NULL)
5749 return (ISC_R_UNEXPECTEDEND);
5751 if (!strcasecmp(ptr, "on") || !strcasecmp(ptr, "yes") ||
5752 !strcasecmp(ptr, "enable") || !strcasecmp(ptr, "true"))
5753 enable = ISC_TRUE;
5754 else if (!strcasecmp(ptr, "off") || !strcasecmp(ptr, "no") ||
5755 !strcasecmp(ptr, "disable") || !strcasecmp(ptr, "false"))
5756 enable = ISC_FALSE;
5757 else
5758 return (DNS_R_SYNTAX);
5760 /* Look for the view name. */
5761 viewname = next_token(&args, " \t");
5763 result = isc_task_beginexclusive(server->task);
5764 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5765 for (view = ISC_LIST_HEAD(server->viewlist);
5766 view != NULL;
5767 view = ISC_LIST_NEXT(view, link))
5769 if (viewname != NULL && strcasecmp(viewname, view->name) != 0)
5770 continue;
5771 result = dns_view_flushcache(view);
5772 if (result != ISC_R_SUCCESS)
5773 goto out;
5774 view->enablevalidation = enable;
5775 changed = ISC_TRUE;
5777 if (changed)
5778 result = ISC_R_SUCCESS;
5779 else
5780 result = ISC_R_FAILURE;
5781 out:
5782 isc_task_endexclusive(server->task);
5783 return (result);
5786 isc_result_t
5787 ns_server_flushcache(ns_server_t *server, char *args) {
5788 char *ptr, *viewname;
5789 dns_view_t *view;
5790 isc_boolean_t flushed;
5791 isc_boolean_t found;
5792 isc_result_t result;
5793 ns_cache_t *nsc;
5795 /* Skip the command name. */
5796 ptr = next_token(&args, " \t");
5797 if (ptr == NULL)
5798 return (ISC_R_UNEXPECTEDEND);
5800 /* Look for the view name. */
5801 viewname = next_token(&args, " \t");
5803 result = isc_task_beginexclusive(server->task);
5804 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5805 flushed = ISC_TRUE;
5806 found = ISC_FALSE;
5809 * Flushing a cache is tricky when caches are shared by multiple views.
5810 * We first identify which caches should be flushed in the local cache
5811 * list, flush these caches, and then update other views that refer to
5812 * the flushed cache DB.
5814 if (viewname != NULL) {
5816 * Mark caches that need to be flushed. This is an O(#view^2)
5817 * operation in the very worst case, but should be normally
5818 * much more lightweight because only a few (most typically just
5819 * one) views will match.
5821 for (view = ISC_LIST_HEAD(server->viewlist);
5822 view != NULL;
5823 view = ISC_LIST_NEXT(view, link))
5825 if (strcasecmp(viewname, view->name) != 0)
5826 continue;
5827 found = ISC_TRUE;
5828 for (nsc = ISC_LIST_HEAD(server->cachelist);
5829 nsc != NULL;
5830 nsc = ISC_LIST_NEXT(nsc, link)) {
5831 if (nsc->cache == view->cache)
5832 break;
5834 INSIST(nsc != NULL);
5835 nsc->needflush = ISC_TRUE;
5837 } else
5838 found = ISC_TRUE;
5840 /* Perform flush */
5841 for (nsc = ISC_LIST_HEAD(server->cachelist);
5842 nsc != NULL;
5843 nsc = ISC_LIST_NEXT(nsc, link)) {
5844 if (viewname != NULL && !nsc->needflush)
5845 continue;
5846 nsc->needflush = ISC_TRUE;
5847 result = dns_view_flushcache2(nsc->primaryview, ISC_FALSE);
5848 if (result != ISC_R_SUCCESS) {
5849 flushed = ISC_FALSE;
5850 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5851 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5852 "flushing cache in view '%s' failed: %s",
5853 nsc->primaryview->name,
5854 isc_result_totext(result));
5859 * Fix up views that share a flushed cache: let the views update the
5860 * cache DB they're referring to. This could also be an expensive
5861 * operation, but should typically be marginal: the inner loop is only
5862 * necessary for views that share a cache, and if there are many such
5863 * views the number of shared cache should normally be small.
5864 * A worst case is that we have n views and n/2 caches, each shared by
5865 * two views. Then this will be a O(n^2/4) operation.
5867 for (view = ISC_LIST_HEAD(server->viewlist);
5868 view != NULL;
5869 view = ISC_LIST_NEXT(view, link))
5871 if (!dns_view_iscacheshared(view))
5872 continue;
5873 for (nsc = ISC_LIST_HEAD(server->cachelist);
5874 nsc != NULL;
5875 nsc = ISC_LIST_NEXT(nsc, link)) {
5876 if (!nsc->needflush || nsc->cache != view->cache)
5877 continue;
5878 result = dns_view_flushcache2(view, ISC_TRUE);
5879 if (result != ISC_R_SUCCESS) {
5880 flushed = ISC_FALSE;
5881 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5882 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5883 "fixing cache in view '%s' "
5884 "failed: %s", view->name,
5885 isc_result_totext(result));
5890 /* Cleanup the cache list. */
5891 for (nsc = ISC_LIST_HEAD(server->cachelist);
5892 nsc != NULL;
5893 nsc = ISC_LIST_NEXT(nsc, link)) {
5894 nsc->needflush = ISC_FALSE;
5897 if (flushed && found) {
5898 if (viewname != NULL)
5899 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5900 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5901 "flushing cache in view '%s' succeeded",
5902 viewname);
5903 else
5904 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5905 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5906 "flushing caches in all views succeeded");
5907 result = ISC_R_SUCCESS;
5908 } else {
5909 if (!found) {
5910 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5911 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5912 "flushing cache in view '%s' failed: "
5913 "view not found", viewname);
5914 result = ISC_R_NOTFOUND;
5915 } else
5916 result = ISC_R_FAILURE;
5918 isc_task_endexclusive(server->task);
5919 return (result);
5922 isc_result_t
5923 ns_server_flushname(ns_server_t *server, char *args) {
5924 char *ptr, *target, *viewname;
5925 dns_view_t *view;
5926 isc_boolean_t flushed;
5927 isc_boolean_t found;
5928 isc_result_t result;
5929 isc_buffer_t b;
5930 dns_fixedname_t fixed;
5931 dns_name_t *name;
5933 /* Skip the command name. */
5934 ptr = next_token(&args, " \t");
5935 if (ptr == NULL)
5936 return (ISC_R_UNEXPECTEDEND);
5938 /* Find the domain name to flush. */
5939 target = next_token(&args, " \t");
5940 if (target == NULL)
5941 return (ISC_R_UNEXPECTEDEND);
5943 isc_buffer_init(&b, target, strlen(target));
5944 isc_buffer_add(&b, strlen(target));
5945 dns_fixedname_init(&fixed);
5946 name = dns_fixedname_name(&fixed);
5947 result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
5948 if (result != ISC_R_SUCCESS)
5949 return (result);
5951 /* Look for the view name. */
5952 viewname = next_token(&args, " \t");
5954 result = isc_task_beginexclusive(server->task);
5955 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5956 flushed = ISC_TRUE;
5957 found = ISC_FALSE;
5958 for (view = ISC_LIST_HEAD(server->viewlist);
5959 view != NULL;
5960 view = ISC_LIST_NEXT(view, link))
5962 if (viewname != NULL && strcasecmp(viewname, view->name) != 0)
5963 continue;
5964 found = ISC_TRUE;
5966 * It's a little inefficient to try flushing name for all views
5967 * if some of the views share a single cache. But since the
5968 * operation is lightweight we prefer simplicity here.
5970 result = dns_view_flushname(view, name);
5971 if (result != ISC_R_SUCCESS) {
5972 flushed = ISC_FALSE;
5973 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5974 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5975 "flushing name '%s' in cache view '%s' "
5976 "failed: %s", target, view->name,
5977 isc_result_totext(result));
5980 if (flushed && found) {
5981 if (viewname != NULL)
5982 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5983 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5984 "flushing name '%s' in cache view '%s' "
5985 "succeeded", target, viewname);
5986 else
5987 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5988 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5989 "flushing name '%s' in all cache views "
5990 "succeeded", target);
5991 result = ISC_R_SUCCESS;
5992 } else {
5993 if (!found)
5994 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5995 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5996 "flushing name '%s' in cache view '%s' "
5997 "failed: view not found", target,
5998 viewname);
5999 result = ISC_R_FAILURE;
6001 isc_task_endexclusive(server->task);
6002 return (result);
6005 isc_result_t
6006 ns_server_status(ns_server_t *server, isc_buffer_t *text) {
6007 int zonecount, xferrunning, xferdeferred, soaqueries;
6008 unsigned int n;
6009 const char *ob = "", *cb = "", *alt = "";
6011 if (ns_g_server->version_set) {
6012 ob = " (";
6013 cb = ")";
6014 if (ns_g_server->version == NULL)
6015 alt = "version.bind/txt/ch disabled";
6016 else
6017 alt = ns_g_server->version;
6019 zonecount = dns_zonemgr_getcount(server->zonemgr, DNS_ZONESTATE_ANY);
6020 xferrunning = dns_zonemgr_getcount(server->zonemgr,
6021 DNS_ZONESTATE_XFERRUNNING);
6022 xferdeferred = dns_zonemgr_getcount(server->zonemgr,
6023 DNS_ZONESTATE_XFERDEFERRED);
6024 soaqueries = dns_zonemgr_getcount(server->zonemgr,
6025 DNS_ZONESTATE_SOAQUERY);
6027 n = snprintf((char *)isc_buffer_used(text),
6028 isc_buffer_availablelength(text),
6029 "version: %s%s%s%s\n"
6030 #ifdef ISC_PLATFORM_USETHREADS
6031 "CPUs found: %u\n"
6032 "worker threads: %u\n"
6033 #endif
6034 "number of zones: %u\n"
6035 "debug level: %d\n"
6036 "xfers running: %u\n"
6037 "xfers deferred: %u\n"
6038 "soa queries in progress: %u\n"
6039 "query logging is %s\n"
6040 "recursive clients: %d/%d/%d\n"
6041 "tcp clients: %d/%d\n"
6042 "server is up and running",
6043 ns_g_version, ob, alt, cb,
6044 #ifdef ISC_PLATFORM_USETHREADS
6045 ns_g_cpus_detected, ns_g_cpus,
6046 #endif
6047 zonecount, ns_g_debuglevel, xferrunning, xferdeferred,
6048 soaqueries, server->log_queries ? "ON" : "OFF",
6049 server->recursionquota.used, server->recursionquota.soft,
6050 server->recursionquota.max,
6051 server->tcpquota.used, server->tcpquota.max);
6052 if (n >= isc_buffer_availablelength(text))
6053 return (ISC_R_NOSPACE);
6054 isc_buffer_add(text, n);
6055 return (ISC_R_SUCCESS);
6058 static isc_result_t
6059 delete_keynames(dns_tsig_keyring_t *ring, char *target,
6060 unsigned int *foundkeys)
6062 char namestr[DNS_NAME_FORMATSIZE];
6063 isc_result_t result;
6064 dns_rbtnodechain_t chain;
6065 dns_name_t foundname;
6066 dns_fixedname_t fixedorigin;
6067 dns_name_t *origin;
6068 dns_rbtnode_t *node;
6069 dns_tsigkey_t *tkey;
6071 dns_name_init(&foundname, NULL);
6072 dns_fixedname_init(&fixedorigin);
6073 origin = dns_fixedname_name(&fixedorigin);
6075 again:
6076 dns_rbtnodechain_init(&chain, ring->mctx);
6077 result = dns_rbtnodechain_first(&chain, ring->keys, &foundname,
6078 origin);
6079 if (result == ISC_R_NOTFOUND) {
6080 dns_rbtnodechain_invalidate(&chain);
6081 return (ISC_R_SUCCESS);
6083 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
6084 dns_rbtnodechain_invalidate(&chain);
6085 return (result);
6088 for (;;) {
6089 node = NULL;
6090 dns_rbtnodechain_current(&chain, &foundname, origin, &node);
6091 tkey = node->data;
6093 if (tkey != NULL) {
6094 if (!tkey->generated)
6095 goto nextkey;
6097 dns_name_format(&tkey->name, namestr, sizeof(namestr));
6098 if (strcmp(namestr, target) == 0) {
6099 (*foundkeys)++;
6100 dns_rbtnodechain_invalidate(&chain);
6101 (void)dns_rbt_deletename(ring->keys,
6102 &tkey->name,
6103 ISC_FALSE);
6104 goto again;
6108 nextkey:
6109 result = dns_rbtnodechain_next(&chain, &foundname, origin);
6110 if (result == ISC_R_NOMORE)
6111 break;
6112 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
6113 dns_rbtnodechain_invalidate(&chain);
6114 return (result);
6118 return (ISC_R_SUCCESS);
6121 isc_result_t
6122 ns_server_tsigdelete(ns_server_t *server, char *command, isc_buffer_t *text) {
6123 isc_result_t result;
6124 unsigned int n;
6125 dns_view_t *view;
6126 unsigned int foundkeys = 0;
6127 char *target;
6128 char *viewname;
6130 (void)next_token(&command, " \t"); /* skip command name */
6131 target = next_token(&command, " \t");
6132 if (target == NULL)
6133 return (ISC_R_UNEXPECTEDEND);
6134 viewname = next_token(&command, " \t");
6136 result = isc_task_beginexclusive(server->task);
6137 RUNTIME_CHECK(result == ISC_R_SUCCESS);
6138 for (view = ISC_LIST_HEAD(server->viewlist);
6139 view != NULL;
6140 view = ISC_LIST_NEXT(view, link)) {
6141 if (viewname == NULL || strcmp(view->name, viewname) == 0) {
6142 RWLOCK(&view->dynamickeys->lock, isc_rwlocktype_write);
6143 result = delete_keynames(view->dynamickeys, target,
6144 &foundkeys);
6145 RWUNLOCK(&view->dynamickeys->lock,
6146 isc_rwlocktype_write);
6147 if (result != ISC_R_SUCCESS) {
6148 isc_task_endexclusive(server->task);
6149 return (result);
6153 isc_task_endexclusive(server->task);
6155 n = snprintf((char *)isc_buffer_used(text),
6156 isc_buffer_availablelength(text),
6157 "%d tsig keys deleted.\n", foundkeys);
6158 if (n >= isc_buffer_availablelength(text)) {
6159 isc_task_endexclusive(server->task);
6160 return (ISC_R_NOSPACE);
6162 isc_buffer_add(text, n);
6164 return (ISC_R_SUCCESS);
6167 static isc_result_t
6168 list_keynames(dns_view_t *view, dns_tsig_keyring_t *ring, isc_buffer_t *text,
6169 unsigned int *foundkeys)
6171 char namestr[DNS_NAME_FORMATSIZE];
6172 char creatorstr[DNS_NAME_FORMATSIZE];
6173 isc_result_t result;
6174 dns_rbtnodechain_t chain;
6175 dns_name_t foundname;
6176 dns_fixedname_t fixedorigin;
6177 dns_name_t *origin;
6178 dns_rbtnode_t *node;
6179 dns_tsigkey_t *tkey;
6180 unsigned int n;
6181 const char *viewname;
6183 if (view != NULL)
6184 viewname = view->name;
6185 else
6186 viewname = "(global)";
6188 dns_name_init(&foundname, NULL);
6189 dns_fixedname_init(&fixedorigin);
6190 origin = dns_fixedname_name(&fixedorigin);
6191 dns_rbtnodechain_init(&chain, ring->mctx);
6192 result = dns_rbtnodechain_first(&chain, ring->keys, &foundname,
6193 origin);
6194 if (result == ISC_R_NOTFOUND) {
6195 dns_rbtnodechain_invalidate(&chain);
6196 return (ISC_R_SUCCESS);
6198 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
6199 dns_rbtnodechain_invalidate(&chain);
6200 return (result);
6203 for (;;) {
6204 node = NULL;
6205 dns_rbtnodechain_current(&chain, &foundname, origin, &node);
6206 tkey = node->data;
6208 if (tkey != NULL) {
6209 (*foundkeys)++;
6210 dns_name_format(&tkey->name, namestr, sizeof(namestr));
6211 if (tkey->generated) {
6212 dns_name_format(tkey->creator, creatorstr,
6213 sizeof(creatorstr));
6214 n = snprintf((char *)isc_buffer_used(text),
6215 isc_buffer_availablelength(text),
6216 "view \"%s\"; type \"dynamic\"; key \"%s\"; creator \"%s\";\n",
6217 viewname, namestr, creatorstr);
6218 } else {
6219 n = snprintf((char *)isc_buffer_used(text),
6220 isc_buffer_availablelength(text),
6221 "view \"%s\"; type \"static\"; key \"%s\";\n",
6222 viewname, namestr);
6224 if (n >= isc_buffer_availablelength(text)) {
6225 dns_rbtnodechain_invalidate(&chain);
6226 return (ISC_R_NOSPACE);
6228 isc_buffer_add(text, n);
6230 result = dns_rbtnodechain_next(&chain, &foundname, origin);
6231 if (result == ISC_R_NOMORE)
6232 break;
6233 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
6234 dns_rbtnodechain_invalidate(&chain);
6235 return (result);
6239 return (ISC_R_SUCCESS);
6242 isc_result_t
6243 ns_server_tsiglist(ns_server_t *server, isc_buffer_t *text) {
6244 isc_result_t result;
6245 unsigned int n;
6246 dns_view_t *view;
6247 unsigned int foundkeys = 0;
6249 result = isc_task_beginexclusive(server->task);
6250 RUNTIME_CHECK(result == ISC_R_SUCCESS);
6251 for (view = ISC_LIST_HEAD(server->viewlist);
6252 view != NULL;
6253 view = ISC_LIST_NEXT(view, link)) {
6254 RWLOCK(&view->statickeys->lock, isc_rwlocktype_read);
6255 result = list_keynames(view, view->statickeys, text,
6256 &foundkeys);
6257 RWUNLOCK(&view->statickeys->lock, isc_rwlocktype_read);
6258 if (result != ISC_R_SUCCESS) {
6259 isc_task_endexclusive(server->task);
6260 return (result);
6262 RWLOCK(&view->dynamickeys->lock, isc_rwlocktype_read);
6263 result = list_keynames(view, view->dynamickeys, text,
6264 &foundkeys);
6265 RWUNLOCK(&view->dynamickeys->lock, isc_rwlocktype_read);
6266 if (result != ISC_R_SUCCESS) {
6267 isc_task_endexclusive(server->task);
6268 return (result);
6271 isc_task_endexclusive(server->task);
6273 if (foundkeys == 0) {
6274 n = snprintf((char *)isc_buffer_used(text),
6275 isc_buffer_availablelength(text),
6276 "no tsig keys found.\n");
6277 if (n >= isc_buffer_availablelength(text)) {
6278 isc_task_endexclusive(server->task);
6279 return (ISC_R_NOSPACE);
6281 isc_buffer_add(text, n);
6284 return (ISC_R_SUCCESS);
6288 * Act on a "sign" command from the command channel.
6290 isc_result_t
6291 ns_server_sign(ns_server_t *server, char *args) {
6292 isc_result_t result;
6293 dns_zone_t *zone = NULL;
6294 dns_zonetype_t type;
6295 isc_uint16_t keyopts;
6297 result = zone_from_args(server, args, &zone);
6298 if (result != ISC_R_SUCCESS)
6299 return (result);
6300 if (zone == NULL)
6301 return (ISC_R_UNEXPECTEDEND); /* XXX: or do all zones? */
6303 type = dns_zone_gettype(zone);
6304 if (type != dns_zone_master) {
6305 dns_zone_detach(&zone);
6306 return (DNS_R_NOTMASTER);
6309 keyopts = dns_zone_getkeyopts(zone);
6310 if ((keyopts & DNS_ZONEKEY_ALLOW) != 0)
6311 result = dns_zone_rekey(zone);
6312 else
6313 result = ISC_R_NOPERM;
6315 dns_zone_detach(&zone);
6316 return (result);
6320 * Act on a "freeze" or "thaw" command from the command channel.
6322 isc_result_t
6323 ns_server_freeze(ns_server_t *server, isc_boolean_t freeze, char *args,
6324 isc_buffer_t *text)
6326 isc_result_t result, tresult;
6327 dns_zone_t *zone = NULL;
6328 dns_zonetype_t type;
6329 char classstr[DNS_RDATACLASS_FORMATSIZE];
6330 char zonename[DNS_NAME_FORMATSIZE];
6331 dns_view_t *view;
6332 char *journal;
6333 const char *vname, *sep;
6334 isc_boolean_t frozen;
6335 const char *msg = NULL;
6337 result = zone_from_args(server, args, &zone);
6338 if (result != ISC_R_SUCCESS)
6339 return (result);
6340 if (zone == NULL) {
6341 result = isc_task_beginexclusive(server->task);
6342 RUNTIME_CHECK(result == ISC_R_SUCCESS);
6343 tresult = ISC_R_SUCCESS;
6344 for (view = ISC_LIST_HEAD(server->viewlist);
6345 view != NULL;
6346 view = ISC_LIST_NEXT(view, link)) {
6347 result = dns_view_freezezones(view, freeze);
6348 if (result != ISC_R_SUCCESS &&
6349 tresult == ISC_R_SUCCESS)
6350 tresult = result;
6352 isc_task_endexclusive(server->task);
6353 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6354 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
6355 "%s all zones: %s",
6356 freeze ? "freezing" : "thawing",
6357 isc_result_totext(tresult));
6358 return (tresult);
6360 type = dns_zone_gettype(zone);
6361 if (type != dns_zone_master) {
6362 dns_zone_detach(&zone);
6363 return (DNS_R_NOTMASTER);
6366 frozen = dns_zone_getupdatedisabled(zone);
6367 if (freeze) {
6368 if (frozen) {
6369 msg = "WARNING: The zone was already frozen.\n"
6370 "Someone else may be editing it or "
6371 "it may still be re-loading.";
6372 result = DNS_R_FROZEN;
6374 if (result == ISC_R_SUCCESS) {
6375 result = dns_zone_flush(zone);
6376 if (result != ISC_R_SUCCESS)
6377 msg = "Flushing the zone updates to "
6378 "disk failed.";
6380 if (result == ISC_R_SUCCESS) {
6381 journal = dns_zone_getjournal(zone);
6382 if (journal != NULL)
6383 (void)isc_file_remove(journal);
6385 if (result == ISC_R_SUCCESS)
6386 dns_zone_setupdatedisabled(zone, freeze);
6387 } else {
6388 if (frozen) {
6389 result = dns_zone_loadandthaw(zone);
6390 switch (result) {
6391 case ISC_R_SUCCESS:
6392 case DNS_R_UPTODATE:
6393 msg = "The zone reload and thaw was "
6394 "successful.";
6395 result = ISC_R_SUCCESS;
6396 break;
6397 case DNS_R_CONTINUE:
6398 msg = "A zone reload and thaw was started.\n"
6399 "Check the logs to see the result.";
6400 result = ISC_R_SUCCESS;
6401 break;
6406 if (msg != NULL && strlen(msg) < isc_buffer_availablelength(text))
6407 isc_buffer_putmem(text, (const unsigned char *)msg,
6408 strlen(msg) + 1);
6410 view = dns_zone_getview(zone);
6411 if (strcmp(view->name, "_default") == 0 ||
6412 strcmp(view->name, "_bind") == 0 ||
6413 strcmp(view->name, "_meta"))
6415 vname = "";
6416 sep = "";
6417 } else {
6418 vname = view->name;
6419 sep = " ";
6421 dns_rdataclass_format(dns_zone_getclass(zone), classstr,
6422 sizeof(classstr));
6423 dns_name_format(dns_zone_getorigin(zone),
6424 zonename, sizeof(zonename));
6425 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6426 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
6427 "%s zone '%s/%s'%s%s: %s",
6428 freeze ? "freezing" : "thawing",
6429 zonename, classstr, sep, vname,
6430 isc_result_totext(result));
6431 dns_zone_detach(&zone);
6432 return (result);
6435 #ifdef HAVE_LIBSCF
6437 * This function adds a message for rndc to echo if named
6438 * is managed by smf and is also running chroot.
6440 isc_result_t
6441 ns_smf_add_message(isc_buffer_t *text) {
6442 unsigned int n;
6444 n = snprintf((char *)isc_buffer_used(text),
6445 isc_buffer_availablelength(text),
6446 "use svcadm(1M) to manage named");
6447 if (n >= isc_buffer_availablelength(text))
6448 return (ISC_R_NOSPACE);
6449 isc_buffer_add(text, n);
6450 return (ISC_R_SUCCESS);
6452 #endif /* HAVE_LIBSCF */