2 * Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: server.c,v 1.520.12.11.8.2 2010/02/25 10:57:11 tbox Exp $ */
28 #include <isc/base64.h>
30 #include <isc/entropy.h>
33 #include <isc/httpd.h>
35 #include <isc/parseint.h>
36 #include <isc/portset.h>
37 #include <isc/print.h>
38 #include <isc/resource.h>
39 #include <isc/socket.h>
40 #include <isc/stats.h>
41 #include <isc/stdio.h>
42 #include <isc/string.h>
44 #include <isc/timer.h>
48 #include <isccfg/namedconf.h>
50 #include <bind9/check.h>
52 #include <dns/acache.h>
54 #include <dns/cache.h>
56 #include <dns/dispatch.h>
60 #include <dns/forward.h>
61 #include <dns/journal.h>
62 #include <dns/keytable.h>
64 #include <dns/master.h>
65 #include <dns/masterdump.h>
66 #include <dns/order.h>
68 #include <dns/portlist.h>
70 #include <dns/rdataclass.h>
71 #include <dns/rdataset.h>
72 #include <dns/rdatastruct.h>
73 #include <dns/resolver.h>
74 #include <dns/rootns.h>
75 #include <dns/secalg.h>
76 #include <dns/stats.h>
84 #include <dst/result.h>
86 #include <named/client.h>
87 #include <named/config.h>
88 #include <named/control.h>
89 #include <named/interfacemgr.h>
90 #include <named/log.h>
91 #include <named/logconf.h>
92 #include <named/lwresd.h>
93 #include <named/main.h>
95 #include <named/server.h>
96 #include <named/statschannel.h>
97 #include <named/tkeyconf.h>
98 #include <named/tsigconf.h>
99 #include <named/zoneconf.h>
101 #include <named/ns_smf_globals.h>
106 * Check an operation for failure. Assumes that the function
107 * using it has a 'result' variable and a 'cleanup' label.
110 do { result = (op); \
111 if (result != ISC_R_SUCCESS) goto cleanup; \
114 #define CHECKM(op, msg) \
115 do { result = (op); \
116 if (result != ISC_R_SUCCESS) { \
117 isc_log_write(ns_g_lctx, \
118 NS_LOGCATEGORY_GENERAL, \
119 NS_LOGMODULE_SERVER, \
122 isc_result_totext(result)); \
127 #define CHECKMF(op, msg, file) \
128 do { result = (op); \
129 if (result != ISC_R_SUCCESS) { \
130 isc_log_write(ns_g_lctx, \
131 NS_LOGCATEGORY_GENERAL, \
132 NS_LOGMODULE_SERVER, \
134 "%s '%s': %s", msg, file, \
135 isc_result_totext(result)); \
140 #define CHECKFATAL(op, msg) \
141 do { result = (op); \
142 if (result != ISC_R_SUCCESS) \
143 fatal(msg, result); \
148 unsigned int dispatchgen;
149 dns_dispatch_t *dispatch;
150 ISC_LINK(struct ns_dispatch) link;
155 isc_boolean_t dumpcache;
156 isc_boolean_t dumpzones;
158 ISC_LIST(struct viewlistentry) viewlist;
159 struct viewlistentry *view;
160 struct zonelistentry *zone;
161 dns_dumpctx_t *mdctx;
165 dns_dbversion_t *version;
168 struct viewlistentry {
170 ISC_LINK(struct viewlistentry) link;
171 ISC_LIST(struct zonelistentry) zonelist;
174 struct zonelistentry {
176 ISC_LINK(struct zonelistentry) link;
180 * These zones should not leak onto the Internet.
182 static const struct {
184 isc_boolean_t rfc1918;
188 { "10.IN-ADDR.ARPA", ISC_TRUE },
189 { "16.172.IN-ADDR.ARPA", ISC_TRUE },
190 { "17.172.IN-ADDR.ARPA", ISC_TRUE },
191 { "18.172.IN-ADDR.ARPA", ISC_TRUE },
192 { "19.172.IN-ADDR.ARPA", ISC_TRUE },
193 { "20.172.IN-ADDR.ARPA", ISC_TRUE },
194 { "21.172.IN-ADDR.ARPA", ISC_TRUE },
195 { "22.172.IN-ADDR.ARPA", ISC_TRUE },
196 { "23.172.IN-ADDR.ARPA", ISC_TRUE },
197 { "24.172.IN-ADDR.ARPA", ISC_TRUE },
198 { "25.172.IN-ADDR.ARPA", ISC_TRUE },
199 { "26.172.IN-ADDR.ARPA", ISC_TRUE },
200 { "27.172.IN-ADDR.ARPA", ISC_TRUE },
201 { "28.172.IN-ADDR.ARPA", ISC_TRUE },
202 { "29.172.IN-ADDR.ARPA", ISC_TRUE },
203 { "30.172.IN-ADDR.ARPA", ISC_TRUE },
204 { "31.172.IN-ADDR.ARPA", ISC_TRUE },
205 { "168.192.IN-ADDR.ARPA", ISC_TRUE },
209 { "0.IN-ADDR.ARPA", ISC_FALSE }, /* THIS NETWORK */
210 { "127.IN-ADDR.ARPA", ISC_FALSE }, /* LOOPBACK */
211 { "254.169.IN-ADDR.ARPA", ISC_FALSE }, /* LINK LOCAL */
212 { "2.0.192.IN-ADDR.ARPA", ISC_FALSE }, /* TEST NET */
213 { "255.255.255.255.IN-ADDR.ARPA", ISC_FALSE }, /* BROADCAST */
215 /* Local IPv6 Unicast Addresses */
216 { "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 },
217 { "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 },
218 /* LOCALLY ASSIGNED LOCAL ADDRESS SCOPE */
219 { "D.F.IP6.ARPA", ISC_FALSE },
220 { "8.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
221 { "9.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
222 { "A.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
223 { "B.E.F.IP6.ARPA", ISC_FALSE }, /* LINK LOCAL */
229 fatal(const char *msg, isc_result_t result);
232 ns_server_reload(isc_task_t *task, isc_event_t *event);
235 ns_listenelt_fromconfig(const cfg_obj_t *listener, const cfg_obj_t *config,
236 cfg_aclconfctx_t *actx,
237 isc_mem_t *mctx, ns_listenelt_t **target);
239 ns_listenlist_fromconfig(const cfg_obj_t *listenlist, const cfg_obj_t *config,
240 cfg_aclconfctx_t *actx,
241 isc_mem_t *mctx, ns_listenlist_t **target);
244 configure_forward(const cfg_obj_t *config, dns_view_t *view, dns_name_t *origin,
245 const cfg_obj_t *forwarders, const cfg_obj_t *forwardtype);
248 configure_alternates(const cfg_obj_t *config, dns_view_t *view,
249 const cfg_obj_t *alternates);
252 configure_zone(const cfg_obj_t *config, const cfg_obj_t *zconfig,
253 const cfg_obj_t *vconfig, isc_mem_t *mctx, dns_view_t *view,
254 cfg_aclconfctx_t *aclconf);
257 end_reserved_dispatches(ns_server_t *server, isc_boolean_t all);
260 * Configure a single view ACL at '*aclp'. Get its configuration from
261 * 'vconfig' (for per-view configuration) and maybe from 'config'
264 configure_view_acl(const cfg_obj_t *vconfig, const cfg_obj_t *config,
265 const char *aclname, cfg_aclconfctx_t *actx,
266 isc_mem_t *mctx, dns_acl_t **aclp)
269 const cfg_obj_t *maps[3];
270 const cfg_obj_t *aclobj = NULL;
274 dns_acl_detach(aclp);
276 maps[i++] = cfg_tuple_get(vconfig, "options");
277 if (config != NULL) {
278 const cfg_obj_t *options = NULL;
279 (void)cfg_map_get(config, "options", &options);
285 (void)ns_config_get(maps, aclname, &aclobj);
288 * No value available. *aclp == NULL.
290 return (ISC_R_SUCCESS);
292 result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
293 actx, mctx, 0, aclp);
300 * Configure a sortlist at '*aclp'. Essentially the same as
301 * configure_view_acl() except it calls cfg_acl_fromconfig with a
302 * nest_level value of 2.
305 configure_view_sortlist(const cfg_obj_t *vconfig, const cfg_obj_t *config,
306 cfg_aclconfctx_t *actx, isc_mem_t *mctx,
310 const cfg_obj_t *maps[3];
311 const cfg_obj_t *aclobj = NULL;
315 dns_acl_detach(aclp);
317 maps[i++] = cfg_tuple_get(vconfig, "options");
318 if (config != NULL) {
319 const cfg_obj_t *options = NULL;
320 (void)cfg_map_get(config, "options", &options);
326 (void)ns_config_get(maps, "sortlist", &aclobj);
328 return (ISC_R_SUCCESS);
331 * Use a nest level of 3 for the "top level" of the sortlist;
332 * this means each entry in the top three levels will be stored
333 * as lists of separate, nested ACLs, rather than merged together
334 * into IP tables as is usually done with ACLs.
336 result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx,
337 actx, mctx, 3, aclp);
343 configure_view_dnsseckey(const cfg_obj_t *vconfig, const cfg_obj_t *key,
344 dns_keytable_t *keytable, isc_mem_t *mctx)
346 dns_rdataclass_t viewclass;
347 dns_rdata_dnskey_t keystruct;
348 isc_uint32_t flags, proto, alg;
349 const char *keystr, *keynamestr;
350 unsigned char keydata[4096];
351 isc_buffer_t keydatabuf;
352 unsigned char rrdata[4096];
353 isc_buffer_t rrdatabuf;
355 dns_fixedname_t fkeyname;
357 isc_buffer_t namebuf;
359 dst_key_t *dstkey = NULL;
361 flags = cfg_obj_asuint32(cfg_tuple_get(key, "flags"));
362 proto = cfg_obj_asuint32(cfg_tuple_get(key, "protocol"));
363 alg = cfg_obj_asuint32(cfg_tuple_get(key, "algorithm"));
364 keyname = dns_fixedname_name(&fkeyname);
365 keynamestr = cfg_obj_asstring(cfg_tuple_get(key, "name"));
368 viewclass = dns_rdataclass_in;
370 const cfg_obj_t *classobj = cfg_tuple_get(vconfig, "class");
371 CHECK(ns_config_getclass(classobj, dns_rdataclass_in,
374 keystruct.common.rdclass = viewclass;
375 keystruct.common.rdtype = dns_rdatatype_dnskey;
377 * The key data in keystruct is not dynamically allocated.
379 keystruct.mctx = NULL;
381 ISC_LINK_INIT(&keystruct.common, link);
384 CHECKM(ISC_R_RANGE, "key flags");
386 CHECKM(ISC_R_RANGE, "key protocol");
388 CHECKM(ISC_R_RANGE, "key algorithm");
389 keystruct.flags = (isc_uint16_t)flags;
390 keystruct.protocol = (isc_uint8_t)proto;
391 keystruct.algorithm = (isc_uint8_t)alg;
393 isc_buffer_init(&keydatabuf, keydata, sizeof(keydata));
394 isc_buffer_init(&rrdatabuf, rrdata, sizeof(rrdata));
396 keystr = cfg_obj_asstring(cfg_tuple_get(key, "key"));
397 CHECK(isc_base64_decodestring(keystr, &keydatabuf));
398 isc_buffer_usedregion(&keydatabuf, &r);
399 keystruct.datalen = r.length;
400 keystruct.data = r.base;
402 if ((keystruct.algorithm == DST_ALG_RSASHA1 ||
403 keystruct.algorithm == DST_ALG_RSAMD5) &&
404 r.length > 1 && r.base[0] == 1 && r.base[1] == 3)
405 cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
406 "trusted key '%s' has a weak exponent",
409 CHECK(dns_rdata_fromstruct(NULL,
410 keystruct.common.rdclass,
411 keystruct.common.rdtype,
412 &keystruct, &rrdatabuf));
413 dns_fixedname_init(&fkeyname);
414 isc_buffer_init(&namebuf, keynamestr, strlen(keynamestr));
415 isc_buffer_add(&namebuf, strlen(keynamestr));
416 CHECK(dns_name_fromtext(keyname, &namebuf,
417 dns_rootname, ISC_FALSE,
419 CHECK(dst_key_fromdns(keyname, viewclass, &rrdatabuf,
422 CHECK(dns_keytable_add(keytable, &dstkey));
423 INSIST(dstkey == NULL);
424 return (ISC_R_SUCCESS);
427 if (result == DST_R_NOCRYPTO) {
428 cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
429 "ignoring trusted key for '%s': no crypto support",
431 result = ISC_R_SUCCESS;
433 cfg_obj_log(key, ns_g_lctx, ISC_LOG_ERROR,
434 "configuring trusted key for '%s': %s",
435 keynamestr, isc_result_totext(result));
436 result = ISC_R_FAILURE;
440 dst_key_free(&dstkey);
446 * Configure DNSSEC keys for a view. Currently used only for
447 * the security roots.
449 * The per-view configuration values and the server-global defaults are read
450 * from 'vconfig' and 'config'. The variable to be configured is '*target'.
453 configure_view_dnsseckeys(const cfg_obj_t *vconfig, const cfg_obj_t *config,
454 isc_mem_t *mctx, dns_keytable_t **target)
457 const cfg_obj_t *keys = NULL;
458 const cfg_obj_t *voptions = NULL;
459 const cfg_listelt_t *element, *element2;
460 const cfg_obj_t *keylist;
461 const cfg_obj_t *key;
462 dns_keytable_t *keytable = NULL;
464 CHECK(dns_keytable_create(mctx, &keytable));
467 voptions = cfg_tuple_get(vconfig, "options");
470 if (voptions != NULL)
471 (void)cfg_map_get(voptions, "trusted-keys", &keys);
473 (void)cfg_map_get(config, "trusted-keys", &keys);
475 for (element = cfg_list_first(keys);
477 element = cfg_list_next(element))
479 keylist = cfg_listelt_value(element);
480 for (element2 = cfg_list_first(keylist);
482 element2 = cfg_list_next(element2))
484 key = cfg_listelt_value(element2);
485 CHECK(configure_view_dnsseckey(vconfig, key,
490 dns_keytable_detach(target);
491 *target = keytable; /* Transfer ownership. */
493 result = ISC_R_SUCCESS;
500 mustbesecure(const cfg_obj_t *mbs, dns_resolver_t *resolver)
502 const cfg_listelt_t *element;
503 const cfg_obj_t *obj;
505 dns_fixedname_t fixed;
511 dns_fixedname_init(&fixed);
512 name = dns_fixedname_name(&fixed);
513 for (element = cfg_list_first(mbs);
515 element = cfg_list_next(element))
517 obj = cfg_listelt_value(element);
518 str = cfg_obj_asstring(cfg_tuple_get(obj, "name"));
519 isc_buffer_init(&b, str, strlen(str));
520 isc_buffer_add(&b, strlen(str));
521 CHECK(dns_name_fromtext(name, &b, dns_rootname,
523 value = cfg_obj_asboolean(cfg_tuple_get(obj, "value"));
524 CHECK(dns_resolver_setmustbesecure(resolver, name, value));
527 result = ISC_R_SUCCESS;
534 * Get a dispatch appropriate for the resolver of a given view.
537 get_view_querysource_dispatch(const cfg_obj_t **maps,
538 int af, dns_dispatch_t **dispatchp,
539 isc_boolean_t is_firstview)
542 dns_dispatch_t *disp;
544 unsigned int attrs, attrmask;
545 const cfg_obj_t *obj = NULL;
546 unsigned int maxdispatchbuffers;
549 * Make compiler happy.
551 result = ISC_R_FAILURE;
555 result = ns_config_get(maps, "query-source", &obj);
556 INSIST(result == ISC_R_SUCCESS);
559 result = ns_config_get(maps, "query-source-v6", &obj);
560 INSIST(result == ISC_R_SUCCESS);
566 sa = *(cfg_obj_assockaddr(obj));
567 INSIST(isc_sockaddr_pf(&sa) == af);
570 * If we don't support this address family, we're done!
574 result = isc_net_probeipv4();
577 result = isc_net_probeipv6();
582 if (result != ISC_R_SUCCESS)
583 return (ISC_R_SUCCESS);
586 * Try to find a dispatcher that we can share.
589 attrs |= DNS_DISPATCHATTR_UDP;
592 attrs |= DNS_DISPATCHATTR_IPV4;
595 attrs |= DNS_DISPATCHATTR_IPV6;
598 if (isc_sockaddr_getport(&sa) == 0) {
599 attrs |= DNS_DISPATCHATTR_EXCLUSIVE;
600 maxdispatchbuffers = 4096;
604 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_INFO,
605 "using specific query-source port "
606 "suppresses port randomization and can be "
609 maxdispatchbuffers = 1000;
613 attrmask |= DNS_DISPATCHATTR_UDP;
614 attrmask |= DNS_DISPATCHATTR_TCP;
615 attrmask |= DNS_DISPATCHATTR_IPV4;
616 attrmask |= DNS_DISPATCHATTR_IPV6;
619 result = dns_dispatch_getudp(ns_g_dispatchmgr, ns_g_socketmgr,
620 ns_g_taskmgr, &sa, 4096,
621 maxdispatchbuffers, 32768, 16411, 16433,
622 attrs, attrmask, &disp);
623 if (result != ISC_R_SUCCESS) {
625 char buf[ISC_SOCKADDR_FORMATSIZE];
629 isc_sockaddr_any(&any);
632 isc_sockaddr_any6(&any);
635 if (isc_sockaddr_equal(&sa, &any))
636 return (ISC_R_SUCCESS);
637 isc_sockaddr_format(&sa, buf, sizeof(buf));
638 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
639 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
640 "could not get query source dispatcher (%s)",
647 return (ISC_R_SUCCESS);
651 configure_order(dns_order_t *order, const cfg_obj_t *ent) {
652 dns_rdataclass_t rdclass;
653 dns_rdatatype_t rdtype;
654 const cfg_obj_t *obj;
655 dns_fixedname_t fixed;
656 unsigned int mode = 0;
660 isc_boolean_t addroot;
662 result = ns_config_getclass(cfg_tuple_get(ent, "class"),
663 dns_rdataclass_any, &rdclass);
664 if (result != ISC_R_SUCCESS)
667 result = ns_config_gettype(cfg_tuple_get(ent, "type"),
668 dns_rdatatype_any, &rdtype);
669 if (result != ISC_R_SUCCESS)
672 obj = cfg_tuple_get(ent, "name");
673 if (cfg_obj_isstring(obj))
674 str = cfg_obj_asstring(obj);
677 addroot = ISC_TF(strcmp(str, "*") == 0);
678 isc_buffer_init(&b, str, strlen(str));
679 isc_buffer_add(&b, strlen(str));
680 dns_fixedname_init(&fixed);
681 result = dns_name_fromtext(dns_fixedname_name(&fixed), &b,
682 dns_rootname, ISC_FALSE, NULL);
683 if (result != ISC_R_SUCCESS)
686 obj = cfg_tuple_get(ent, "ordering");
687 INSIST(cfg_obj_isstring(obj));
688 str = cfg_obj_asstring(obj);
689 if (!strcasecmp(str, "fixed"))
690 mode = DNS_RDATASETATTR_FIXEDORDER;
691 else if (!strcasecmp(str, "random"))
692 mode = DNS_RDATASETATTR_RANDOMIZE;
693 else if (!strcasecmp(str, "cyclic"))
699 * "*" should match everything including the root (BIND 8 compat).
700 * As dns_name_matcheswildcard(".", "*.") returns FALSE add a
701 * explicit entry for "." when the name is "*".
704 result = dns_order_add(order, dns_rootname,
705 rdtype, rdclass, mode);
706 if (result != ISC_R_SUCCESS)
710 return (dns_order_add(order, dns_fixedname_name(&fixed),
711 rdtype, rdclass, mode));
715 configure_peer(const cfg_obj_t *cpeer, isc_mem_t *mctx, dns_peer_t **peerp) {
718 const cfg_obj_t *obj;
721 unsigned int prefixlen;
723 cfg_obj_asnetprefix(cfg_map_getname(cpeer), &na, &prefixlen);
726 result = dns_peer_newprefix(mctx, &na, prefixlen, &peer);
727 if (result != ISC_R_SUCCESS)
731 (void)cfg_map_get(cpeer, "bogus", &obj);
733 CHECK(dns_peer_setbogus(peer, cfg_obj_asboolean(obj)));
736 (void)cfg_map_get(cpeer, "provide-ixfr", &obj);
738 CHECK(dns_peer_setprovideixfr(peer, cfg_obj_asboolean(obj)));
741 (void)cfg_map_get(cpeer, "request-ixfr", &obj);
743 CHECK(dns_peer_setrequestixfr(peer, cfg_obj_asboolean(obj)));
746 (void)cfg_map_get(cpeer, "request-nsid", &obj);
748 CHECK(dns_peer_setrequestnsid(peer, cfg_obj_asboolean(obj)));
751 (void)cfg_map_get(cpeer, "edns", &obj);
753 CHECK(dns_peer_setsupportedns(peer, cfg_obj_asboolean(obj)));
756 (void)cfg_map_get(cpeer, "edns-udp-size", &obj);
758 isc_uint32_t udpsize = cfg_obj_asuint32(obj);
763 CHECK(dns_peer_setudpsize(peer, (isc_uint16_t)udpsize));
767 (void)cfg_map_get(cpeer, "max-udp-size", &obj);
769 isc_uint32_t udpsize = cfg_obj_asuint32(obj);
774 CHECK(dns_peer_setmaxudp(peer, (isc_uint16_t)udpsize));
778 (void)cfg_map_get(cpeer, "transfers", &obj);
780 CHECK(dns_peer_settransfers(peer, cfg_obj_asuint32(obj)));
783 (void)cfg_map_get(cpeer, "transfer-format", &obj);
785 str = cfg_obj_asstring(obj);
786 if (strcasecmp(str, "many-answers") == 0)
787 CHECK(dns_peer_settransferformat(peer,
789 else if (strcasecmp(str, "one-answer") == 0)
790 CHECK(dns_peer_settransferformat(peer,
797 (void)cfg_map_get(cpeer, "keys", &obj);
799 result = dns_peer_setkeybycharp(peer, cfg_obj_asstring(obj));
800 if (result != ISC_R_SUCCESS)
805 if (na.family == AF_INET)
806 (void)cfg_map_get(cpeer, "transfer-source", &obj);
808 (void)cfg_map_get(cpeer, "transfer-source-v6", &obj);
810 result = dns_peer_settransfersource(peer,
811 cfg_obj_assockaddr(obj));
812 if (result != ISC_R_SUCCESS)
814 ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
818 if (na.family == AF_INET)
819 (void)cfg_map_get(cpeer, "notify-source", &obj);
821 (void)cfg_map_get(cpeer, "notify-source-v6", &obj);
823 result = dns_peer_setnotifysource(peer,
824 cfg_obj_assockaddr(obj));
825 if (result != ISC_R_SUCCESS)
827 ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
831 if (na.family == AF_INET)
832 (void)cfg_map_get(cpeer, "query-source", &obj);
834 (void)cfg_map_get(cpeer, "query-source-v6", &obj);
836 result = dns_peer_setquerysource(peer,
837 cfg_obj_assockaddr(obj));
838 if (result != ISC_R_SUCCESS)
840 ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
844 return (ISC_R_SUCCESS);
847 dns_peer_detach(&peer);
852 disable_algorithms(const cfg_obj_t *disabled, dns_resolver_t *resolver) {
854 const cfg_obj_t *algorithms;
855 const cfg_listelt_t *element;
857 dns_fixedname_t fixed;
861 dns_fixedname_init(&fixed);
862 name = dns_fixedname_name(&fixed);
863 str = cfg_obj_asstring(cfg_tuple_get(disabled, "name"));
864 isc_buffer_init(&b, str, strlen(str));
865 isc_buffer_add(&b, strlen(str));
866 CHECK(dns_name_fromtext(name, &b, dns_rootname, ISC_FALSE, NULL));
868 algorithms = cfg_tuple_get(disabled, "algorithms");
869 for (element = cfg_list_first(algorithms);
871 element = cfg_list_next(element))
876 DE_CONST(cfg_obj_asstring(cfg_listelt_value(element)), r.base);
877 r.length = strlen(r.base);
879 result = dns_secalg_fromtext(&alg, &r);
880 if (result != ISC_R_SUCCESS) {
882 result = isc_parse_uint8(&ui, r.base, 10);
885 if (result != ISC_R_SUCCESS) {
886 cfg_obj_log(cfg_listelt_value(element),
887 ns_g_lctx, ISC_LOG_ERROR,
888 "invalid algorithm");
891 CHECK(dns_resolver_disable_algorithm(resolver, name, alg));
898 on_disable_list(const cfg_obj_t *disablelist, dns_name_t *zonename) {
899 const cfg_listelt_t *element;
900 dns_fixedname_t fixed;
903 const cfg_obj_t *value;
907 dns_fixedname_init(&fixed);
908 name = dns_fixedname_name(&fixed);
910 for (element = cfg_list_first(disablelist);
912 element = cfg_list_next(element))
914 value = cfg_listelt_value(element);
915 str = cfg_obj_asstring(value);
916 isc_buffer_init(&b, str, strlen(str));
917 isc_buffer_add(&b, strlen(str));
918 result = dns_name_fromtext(name, &b, dns_rootname,
920 RUNTIME_CHECK(result == ISC_R_SUCCESS);
921 if (dns_name_equal(name, zonename))
928 check_dbtype(dns_zone_t **zonep, unsigned int dbtypec, const char **dbargv,
935 result = dns_zone_getdbtype(*zonep, &argv, mctx);
936 if (result != ISC_R_SUCCESS) {
937 dns_zone_detach(zonep);
942 * Check that all the arguments match.
944 for (i = 0; i < dbtypec; i++)
945 if (argv[i] == NULL || strcmp(argv[i], dbargv[i]) != 0) {
946 dns_zone_detach(zonep);
951 * Check that there are not extra arguments.
953 if (i == dbtypec && argv[i] != NULL)
954 dns_zone_detach(zonep);
955 isc_mem_free(mctx, argv);
959 setquerystats(dns_zone_t *zone, isc_mem_t *mctx, isc_boolean_t on) {
961 isc_stats_t *zoneqrystats;
965 result = isc_stats_create(mctx, &zoneqrystats,
966 dns_nsstatscounter_max);
967 if (result != ISC_R_SUCCESS)
970 dns_zone_setrequeststats(zone, zoneqrystats);
971 if (zoneqrystats != NULL)
972 isc_stats_detach(&zoneqrystats);
974 return (ISC_R_SUCCESS);
978 cache_reusable(dns_view_t *originview, dns_view_t *view,
979 isc_boolean_t new_zero_no_soattl)
981 if (originview->checknames != view->checknames ||
982 dns_resolver_getzeronosoattl(originview->resolver) !=
983 new_zero_no_soattl ||
984 originview->acceptexpired != view->acceptexpired ||
985 originview->enablevalidation != view->enablevalidation ||
986 originview->maxcachettl != view->maxcachettl ||
987 originview->maxncachettl != view->maxncachettl) {
995 * Configure 'view' according to 'vconfig', taking defaults from 'config'
996 * where values are missing in 'vconfig'.
998 * When configuring the default view, 'vconfig' will be NULL and the
999 * global defaults in 'config' used exclusively.
1002 configure_view(dns_view_t *view, const cfg_obj_t *config,
1003 const cfg_obj_t *vconfig, isc_mem_t *mctx,
1004 cfg_aclconfctx_t *actx, isc_boolean_t need_hints)
1006 const cfg_obj_t *maps[4];
1007 const cfg_obj_t *cfgmaps[3];
1008 const cfg_obj_t *options = NULL;
1009 const cfg_obj_t *voptions = NULL;
1010 const cfg_obj_t *forwardtype;
1011 const cfg_obj_t *forwarders;
1012 const cfg_obj_t *alternates;
1013 const cfg_obj_t *zonelist;
1015 const cfg_obj_t *dlz;
1016 unsigned int dlzargc;
1019 const cfg_obj_t *disabled;
1020 const cfg_obj_t *obj;
1021 const cfg_listelt_t *element;
1023 dns_cache_t *cache = NULL;
1024 isc_result_t result;
1025 isc_uint32_t max_adb_size;
1026 isc_uint32_t max_cache_size;
1027 isc_uint32_t max_acache_size;
1028 isc_uint32_t lame_ttl;
1029 dns_tsig_keyring_t *ring;
1030 dns_view_t *pview = NULL; /* Production view */
1032 dns_dispatch_t *dispatch4 = NULL;
1033 dns_dispatch_t *dispatch6 = NULL;
1034 isc_boolean_t reused_cache = ISC_FALSE;
1037 dns_order_t *order = NULL;
1038 isc_uint32_t udpsize;
1039 unsigned int resopts = 0;
1040 dns_zone_t *zone = NULL;
1041 isc_uint32_t max_clients_per_query;
1042 const char *sep = ": view ";
1043 const char *viewname = view->name;
1044 const char *forview = " for view ";
1045 isc_boolean_t rfc1918;
1046 isc_boolean_t empty_zones_enable;
1047 const cfg_obj_t *disablelist = NULL;
1048 isc_stats_t *resstats = NULL;
1049 dns_stats_t *resquerystats = NULL;
1050 isc_boolean_t zero_no_soattl;
1052 REQUIRE(DNS_VIEW_VALID(view));
1057 (void)cfg_map_get(config, "options", &options);
1060 if (vconfig != NULL) {
1061 voptions = cfg_tuple_get(vconfig, "options");
1062 maps[i++] = voptions;
1064 if (options != NULL)
1065 maps[i++] = options;
1066 maps[i++] = ns_g_defaults;
1070 if (voptions != NULL)
1071 cfgmaps[i++] = voptions;
1073 cfgmaps[i++] = config;
1076 if (!strcmp(viewname, "_default")) {
1083 * Set the view's port number for outgoing queries.
1085 CHECKM(ns_config_getport(config, &port), "port");
1086 dns_view_setdstport(view, port);
1089 * Create additional cache for this view and zones under the view
1090 * if explicitly enabled.
1091 * XXX950 default to on.
1094 (void)ns_config_get(maps, "acache-enable", &obj);
1095 if (obj != NULL && cfg_obj_asboolean(obj)) {
1097 CHECK(isc_mem_create(0, 0, &cmctx));
1098 CHECK(dns_acache_create(&view->acache, cmctx, ns_g_taskmgr,
1100 isc_mem_setname(cmctx, "acache", NULL);
1101 isc_mem_detach(&cmctx);
1103 if (view->acache != NULL) {
1105 result = ns_config_get(maps, "acache-cleaning-interval", &obj);
1106 INSIST(result == ISC_R_SUCCESS);
1107 dns_acache_setcleaninginterval(view->acache,
1108 cfg_obj_asuint32(obj) * 60);
1111 result = ns_config_get(maps, "max-acache-size", &obj);
1112 INSIST(result == ISC_R_SUCCESS);
1113 if (cfg_obj_isstring(obj)) {
1114 str = cfg_obj_asstring(obj);
1115 INSIST(strcasecmp(str, "unlimited") == 0);
1116 max_acache_size = ISC_UINT32_MAX;
1118 isc_resourcevalue_t value;
1120 value = cfg_obj_asuint64(obj);
1121 if (value > ISC_UINT32_MAX) {
1122 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
1124 "%" ISC_PRINT_QUADFORMAT
1127 result = ISC_R_RANGE;
1130 max_acache_size = (isc_uint32_t)value;
1132 dns_acache_setcachesize(view->acache, max_acache_size);
1136 * Configure the zones.
1139 if (voptions != NULL)
1140 (void)cfg_map_get(voptions, "zone", &zonelist);
1142 (void)cfg_map_get(config, "zone", &zonelist);
1143 for (element = cfg_list_first(zonelist);
1145 element = cfg_list_next(element))
1147 const cfg_obj_t *zconfig = cfg_listelt_value(element);
1148 CHECK(configure_zone(config, zconfig, vconfig, mctx, view,
1154 * Create Dynamically Loadable Zone driver.
1157 if (voptions != NULL)
1158 (void)cfg_map_get(voptions, "dlz", &dlz);
1160 (void)cfg_map_get(config, "dlz", &dlz);
1164 (void)cfg_map_get(cfg_tuple_get(dlz, "options"),
1167 char *s = isc_mem_strdup(mctx, cfg_obj_asstring(obj));
1169 result = ISC_R_NOMEMORY;
1173 result = dns_dlzstrtoargv(mctx, s, &dlzargc, &dlzargv);
1174 if (result != ISC_R_SUCCESS) {
1175 isc_mem_free(mctx, s);
1179 obj = cfg_tuple_get(dlz, "name");
1180 result = dns_dlzcreate(mctx, cfg_obj_asstring(obj),
1181 dlzargv[0], dlzargc, dlzargv,
1182 &view->dlzdatabase);
1183 isc_mem_free(mctx, s);
1184 isc_mem_put(mctx, dlzargv, dlzargc * sizeof(*dlzargv));
1185 if (result != ISC_R_SUCCESS)
1192 * Obtain configuration parameters that affect the decision of whether
1193 * we can reuse/share an existing cache.
1197 result = ns_checknames_get(maps, "response", &obj);
1198 INSIST(result == ISC_R_SUCCESS);
1200 str = cfg_obj_asstring(obj);
1201 if (strcasecmp(str, "fail") == 0) {
1202 resopts |= DNS_RESOLVER_CHECKNAMES |
1203 DNS_RESOLVER_CHECKNAMESFAIL;
1204 view->checknames = ISC_TRUE;
1205 } else if (strcasecmp(str, "warn") == 0) {
1206 resopts |= DNS_RESOLVER_CHECKNAMES;
1207 view->checknames = ISC_FALSE;
1208 } else if (strcasecmp(str, "ignore") == 0) {
1209 view->checknames = ISC_FALSE;
1214 result = ns_config_get(maps, "zero-no-soa-ttl-cache", &obj);
1215 INSIST(result == ISC_R_SUCCESS);
1216 zero_no_soattl = cfg_obj_asboolean(obj);
1219 result = ns_config_get(maps, "dnssec-accept-expired", &obj);
1220 INSIST(result == ISC_R_SUCCESS);
1221 view->acceptexpired = cfg_obj_asboolean(obj);
1224 result = ns_config_get(maps, "dnssec-validation", &obj);
1225 INSIST(result == ISC_R_SUCCESS);
1226 view->enablevalidation = cfg_obj_asboolean(obj);
1229 result = ns_config_get(maps, "max-cache-ttl", &obj);
1230 INSIST(result == ISC_R_SUCCESS);
1231 view->maxcachettl = cfg_obj_asuint32(obj);
1234 result = ns_config_get(maps, "max-ncache-ttl", &obj);
1235 INSIST(result == ISC_R_SUCCESS);
1236 view->maxncachettl = cfg_obj_asuint32(obj);
1237 if (view->maxncachettl > 7 * 24 * 3600)
1238 view->maxncachettl = 7 * 24 * 3600;
1241 * Configure the view's cache. Try to reuse an existing
1242 * cache if possible, otherwise create a new cache.
1243 * Note that the ADB is not preserved in either case.
1244 * When a matching view is found, the associated statistics are
1245 * also retrieved and reused.
1247 * XXX Determining when it is safe to reuse a cache is tricky.
1248 * When the view's configuration changes, the cached data may become
1249 * invalid because it reflects our old view of the world. We check
1250 * some of the configuration parameters that could invalidate the cache,
1251 * but there are other configuration options that should be checked.
1252 * For example, if a view uses a forwarder, changes in the forwarder
1253 * configuration may invalidate the cache. At the moment, it's the
1254 * administrator's responsibility to ensure these configuration options
1255 * don't invalidate reusing.
1257 result = dns_viewlist_find(&ns_g_server->viewlist,
1258 view->name, view->rdclass,
1260 if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
1262 if (pview != NULL) {
1263 if (cache_reusable(pview, view, zero_no_soattl)) {
1264 INSIST(pview->cache != NULL);
1265 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1266 NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(3),
1267 "reusing existing cache");
1268 reused_cache = ISC_TRUE;
1269 dns_cache_attach(pview->cache, &cache);
1271 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1272 NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(1),
1273 "cache cannot be reused for view %s "
1274 "due to configuration parameter mismatch",
1277 dns_view_getresstats(pview, &resstats);
1278 dns_view_getresquerystats(pview, &resquerystats);
1279 dns_view_detach(&pview);
1281 if (cache == NULL) {
1282 CHECK(isc_mem_create(0, 0, &cmctx));
1283 CHECK(dns_cache_create(cmctx, ns_g_taskmgr, ns_g_timermgr,
1284 view->rdclass, "rbt", 0, NULL, &cache));
1285 isc_mem_setname(cmctx, "cache", NULL);
1287 dns_view_setcache(view, cache);
1290 * cache-file cannot be inherited if views are present, but this
1291 * should be caught by the configuration checking stage.
1294 result = ns_config_get(maps, "cache-file", &obj);
1295 if (result == ISC_R_SUCCESS && strcmp(view->name, "_bind") != 0) {
1296 CHECK(dns_cache_setfilename(cache, cfg_obj_asstring(obj)));
1298 CHECK(dns_cache_load(cache));
1302 result = ns_config_get(maps, "cleaning-interval", &obj);
1303 INSIST(result == ISC_R_SUCCESS);
1304 dns_cache_setcleaninginterval(cache, cfg_obj_asuint32(obj) * 60);
1307 result = ns_config_get(maps, "max-cache-size", &obj);
1308 INSIST(result == ISC_R_SUCCESS);
1309 if (cfg_obj_isstring(obj)) {
1310 str = cfg_obj_asstring(obj);
1311 INSIST(strcasecmp(str, "unlimited") == 0);
1312 max_cache_size = ISC_UINT32_MAX;
1314 isc_resourcevalue_t value;
1315 value = cfg_obj_asuint64(obj);
1316 if (value > ISC_UINT32_MAX) {
1317 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
1319 "%" ISC_PRINT_QUADFORMAT "d' is too large",
1321 result = ISC_R_RANGE;
1324 max_cache_size = (isc_uint32_t)value;
1326 dns_cache_setcachesize(cache, max_cache_size);
1328 dns_cache_detach(&cache);
1333 * XXXRTH Hardwired number of tasks.
1335 CHECK(get_view_querysource_dispatch(maps, AF_INET, &dispatch4,
1336 ISC_TF(ISC_LIST_PREV(view, link)
1338 CHECK(get_view_querysource_dispatch(maps, AF_INET6, &dispatch6,
1339 ISC_TF(ISC_LIST_PREV(view, link)
1341 if (dispatch4 == NULL && dispatch6 == NULL) {
1342 UNEXPECTED_ERROR(__FILE__, __LINE__,
1343 "unable to obtain neither an IPv4 nor"
1344 " an IPv6 dispatch");
1345 result = ISC_R_UNEXPECTED;
1348 CHECK(dns_view_createresolver(view, ns_g_taskmgr, 31,
1349 ns_g_socketmgr, ns_g_timermgr,
1350 resopts, ns_g_dispatchmgr,
1351 dispatch4, dispatch6));
1353 if (resstats == NULL) {
1354 CHECK(isc_stats_create(mctx, &resstats,
1355 dns_resstatscounter_max));
1357 dns_view_setresstats(view, resstats);
1358 if (resquerystats == NULL)
1359 CHECK(dns_rdatatypestats_create(mctx, &resquerystats));
1360 dns_view_setresquerystats(view, resquerystats);
1363 * Set the ADB cache size to 1/8th of the max-cache-size.
1366 if (max_cache_size != 0) {
1367 max_adb_size = max_cache_size / 8;
1368 if (max_adb_size == 0)
1369 max_adb_size = 1; /* Force minimum. */
1371 dns_adb_setadbsize(view->adb, max_adb_size);
1374 * Set resolver's lame-ttl.
1377 result = ns_config_get(maps, "lame-ttl", &obj);
1378 INSIST(result == ISC_R_SUCCESS);
1379 lame_ttl = cfg_obj_asuint32(obj);
1380 if (lame_ttl > 1800)
1382 dns_resolver_setlamettl(view->resolver, lame_ttl);
1385 * Set the resolver's EDNS UDP size.
1388 result = ns_config_get(maps, "edns-udp-size", &obj);
1389 INSIST(result == ISC_R_SUCCESS);
1390 udpsize = cfg_obj_asuint32(obj);
1395 dns_resolver_setudpsize(view->resolver, (isc_uint16_t)udpsize);
1398 * Set the maximum UDP response size.
1401 result = ns_config_get(maps, "max-udp-size", &obj);
1402 INSIST(result == ISC_R_SUCCESS);
1403 udpsize = cfg_obj_asuint32(obj);
1408 view->maxudp = udpsize;
1411 * Set supported DNSSEC algorithms.
1413 dns_resolver_reset_algorithms(view->resolver);
1415 (void)ns_config_get(maps, "disable-algorithms", &disabled);
1416 if (disabled != NULL) {
1417 for (element = cfg_list_first(disabled);
1419 element = cfg_list_next(element))
1420 CHECK(disable_algorithms(cfg_listelt_value(element),
1425 * A global or view "forwarders" option, if present,
1426 * creates an entry for "." in the forwarding table.
1430 (void)ns_config_get(maps, "forward", &forwardtype);
1431 (void)ns_config_get(maps, "forwarders", &forwarders);
1432 if (forwarders != NULL)
1433 CHECK(configure_forward(config, view, dns_rootname,
1434 forwarders, forwardtype));
1437 * Dual Stack Servers.
1440 (void)ns_config_get(maps, "dual-stack-servers", &alternates);
1441 if (alternates != NULL)
1442 CHECK(configure_alternates(config, view, alternates));
1445 * We have default hints for class IN if we need them.
1447 if (view->rdclass == dns_rdataclass_in && view->hints == NULL)
1448 dns_view_sethints(view, ns_g_server->in_roothints);
1451 * If we still have no hints, this is a non-IN view with no
1452 * "hints zone" configured. Issue a warning, except if this
1453 * is a root server. Root servers never need to consult
1454 * their hints, so it's no point requiring users to configure
1457 if (view->hints == NULL) {
1458 dns_zone_t *rootzone = NULL;
1459 (void)dns_view_findzone(view, dns_rootname, &rootzone);
1460 if (rootzone != NULL) {
1461 dns_zone_detach(&rootzone);
1462 need_hints = ISC_FALSE;
1465 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1466 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
1467 "no root hints for view '%s'",
1472 * Configure the view's TSIG keys.
1475 CHECK(ns_tsigkeyring_fromconfig(config, vconfig, view->mctx, &ring));
1476 dns_view_setkeyring(view, ring);
1479 * Configure the view's peer list.
1482 const cfg_obj_t *peers = NULL;
1483 const cfg_listelt_t *element;
1484 dns_peerlist_t *newpeers = NULL;
1486 (void)ns_config_get(cfgmaps, "server", &peers);
1487 CHECK(dns_peerlist_new(mctx, &newpeers));
1488 for (element = cfg_list_first(peers);
1490 element = cfg_list_next(element))
1492 const cfg_obj_t *cpeer = cfg_listelt_value(element);
1495 CHECK(configure_peer(cpeer, mctx, &peer));
1496 dns_peerlist_addpeer(newpeers, peer);
1497 dns_peer_detach(&peer);
1499 dns_peerlist_detach(&view->peers);
1500 view->peers = newpeers; /* Transfer ownership. */
1504 * Configure the views rrset-order.
1507 const cfg_obj_t *rrsetorder = NULL;
1508 const cfg_listelt_t *element;
1510 (void)ns_config_get(maps, "rrset-order", &rrsetorder);
1511 CHECK(dns_order_create(mctx, &order));
1512 for (element = cfg_list_first(rrsetorder);
1514 element = cfg_list_next(element))
1516 const cfg_obj_t *ent = cfg_listelt_value(element);
1518 CHECK(configure_order(order, ent));
1520 if (view->order != NULL)
1521 dns_order_detach(&view->order);
1522 dns_order_attach(order, &view->order);
1523 dns_order_detach(&order);
1526 * Copy the aclenv object.
1528 dns_aclenv_copy(&view->aclenv, &ns_g_server->aclenv);
1531 * Configure the "match-clients" and "match-destinations" ACL.
1533 CHECK(configure_view_acl(vconfig, config, "match-clients", actx,
1534 ns_g_mctx, &view->matchclients));
1535 CHECK(configure_view_acl(vconfig, config, "match-destinations", actx,
1536 ns_g_mctx, &view->matchdestinations));
1539 * Configure the "match-recursive-only" option.
1542 (void)ns_config_get(maps, "match-recursive-only", &obj);
1543 if (obj != NULL && cfg_obj_asboolean(obj))
1544 view->matchrecursiveonly = ISC_TRUE;
1546 view->matchrecursiveonly = ISC_FALSE;
1549 * Configure other configurable data.
1552 result = ns_config_get(maps, "recursion", &obj);
1553 INSIST(result == ISC_R_SUCCESS);
1554 view->recursion = cfg_obj_asboolean(obj);
1557 result = ns_config_get(maps, "auth-nxdomain", &obj);
1558 INSIST(result == ISC_R_SUCCESS);
1559 view->auth_nxdomain = cfg_obj_asboolean(obj);
1562 result = ns_config_get(maps, "minimal-responses", &obj);
1563 INSIST(result == ISC_R_SUCCESS);
1564 view->minimalresponses = cfg_obj_asboolean(obj);
1567 result = ns_config_get(maps, "transfer-format", &obj);
1568 INSIST(result == ISC_R_SUCCESS);
1569 str = cfg_obj_asstring(obj);
1570 if (strcasecmp(str, "many-answers") == 0)
1571 view->transfer_format = dns_many_answers;
1572 else if (strcasecmp(str, "one-answer") == 0)
1573 view->transfer_format = dns_one_answer;
1578 * Set sources where additional data and CNAME/DNAME
1579 * targets for authoritative answers may be found.
1582 result = ns_config_get(maps, "additional-from-auth", &obj);
1583 INSIST(result == ISC_R_SUCCESS);
1584 view->additionalfromauth = cfg_obj_asboolean(obj);
1585 if (view->recursion && ! view->additionalfromauth) {
1586 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_WARNING,
1587 "'additional-from-auth no' is only supported "
1588 "with 'recursion no'");
1589 view->additionalfromauth = ISC_TRUE;
1593 result = ns_config_get(maps, "additional-from-cache", &obj);
1594 INSIST(result == ISC_R_SUCCESS);
1595 view->additionalfromcache = cfg_obj_asboolean(obj);
1596 if (view->recursion && ! view->additionalfromcache) {
1597 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_WARNING,
1598 "'additional-from-cache no' is only supported "
1599 "with 'recursion no'");
1600 view->additionalfromcache = ISC_TRUE;
1604 * Set "allow-query-cache", "allow-query-cache-on",
1605 * "allow-recursion", and "allow-recursion-on" acls if
1606 * configured in named.conf.
1608 CHECK(configure_view_acl(vconfig, config, "allow-query-cache",
1609 actx, ns_g_mctx, &view->queryacl));
1610 CHECK(configure_view_acl(vconfig, config, "allow-query-cache-on",
1611 actx, ns_g_mctx, &view->queryonacl));
1612 if (view->queryonacl == NULL)
1613 CHECK(configure_view_acl(NULL, ns_g_config,
1614 "allow-query-cache-on", actx,
1615 ns_g_mctx, &view->queryonacl));
1616 if (strcmp(view->name, "_bind") != 0) {
1617 CHECK(configure_view_acl(vconfig, config, "allow-recursion",
1619 &view->recursionacl));
1620 CHECK(configure_view_acl(vconfig, config, "allow-recursion-on",
1622 &view->recursiononacl));
1626 * "allow-query-cache" inherits from "allow-recursion" if set,
1627 * otherwise from "allow-query" if set.
1628 * "allow-recursion" inherits from "allow-query-cache" if set,
1629 * otherwise from "allow-query" if set.
1631 if (view->queryacl == NULL && view->recursionacl != NULL)
1632 dns_acl_attach(view->recursionacl, &view->queryacl);
1633 if (view->queryacl == NULL && view->recursion)
1634 CHECK(configure_view_acl(vconfig, config, "allow-query",
1635 actx, ns_g_mctx, &view->queryacl));
1636 if (view->recursion &&
1637 view->recursionacl == NULL && view->queryacl != NULL)
1638 dns_acl_attach(view->queryacl, &view->recursionacl);
1641 * Set default "allow-recursion", "allow-recursion-on" and
1642 * "allow-query-cache" acls.
1644 if (view->recursionacl == NULL && view->recursion)
1645 CHECK(configure_view_acl(NULL, ns_g_config,
1648 &view->recursionacl));
1649 if (view->recursiononacl == NULL && view->recursion)
1650 CHECK(configure_view_acl(NULL, ns_g_config,
1651 "allow-recursion-on",
1653 &view->recursiononacl));
1654 if (view->queryacl == NULL) {
1655 if (view->recursion)
1656 CHECK(configure_view_acl(NULL, ns_g_config,
1657 "allow-query-cache", actx,
1658 ns_g_mctx, &view->queryacl));
1660 if (view->queryacl != NULL)
1661 dns_acl_detach(&view->queryacl);
1662 CHECK(dns_acl_none(ns_g_mctx, &view->queryacl));
1667 * Configure sortlist, if set
1669 CHECK(configure_view_sortlist(vconfig, config, actx, ns_g_mctx,
1673 * Configure default allow-transfer, allow-notify, allow-update
1674 * and allow-update-forwarding ACLs, if set, so they can be
1675 * inherited by zones.
1677 if (view->notifyacl == NULL)
1678 CHECK(configure_view_acl(NULL, ns_g_config,
1679 "allow-notify", actx,
1680 ns_g_mctx, &view->notifyacl));
1681 if (view->transferacl == NULL)
1682 CHECK(configure_view_acl(NULL, ns_g_config,
1683 "allow-transfer", actx,
1684 ns_g_mctx, &view->transferacl));
1685 if (view->updateacl == NULL)
1686 CHECK(configure_view_acl(NULL, ns_g_config,
1687 "allow-update", actx,
1688 ns_g_mctx, &view->updateacl));
1689 if (view->upfwdacl == NULL)
1690 CHECK(configure_view_acl(NULL, ns_g_config,
1691 "allow-update-forwarding", actx,
1692 ns_g_mctx, &view->upfwdacl));
1695 result = ns_config_get(maps, "request-ixfr", &obj);
1696 INSIST(result == ISC_R_SUCCESS);
1697 view->requestixfr = cfg_obj_asboolean(obj);
1700 result = ns_config_get(maps, "provide-ixfr", &obj);
1701 INSIST(result == ISC_R_SUCCESS);
1702 view->provideixfr = cfg_obj_asboolean(obj);
1705 result = ns_config_get(maps, "request-nsid", &obj);
1706 INSIST(result == ISC_R_SUCCESS);
1707 view->requestnsid = cfg_obj_asboolean(obj);
1710 result = ns_config_get(maps, "max-clients-per-query", &obj);
1711 INSIST(result == ISC_R_SUCCESS);
1712 max_clients_per_query = cfg_obj_asuint32(obj);
1715 result = ns_config_get(maps, "clients-per-query", &obj);
1716 INSIST(result == ISC_R_SUCCESS);
1717 dns_resolver_setclientsperquery(view->resolver,
1718 cfg_obj_asuint32(obj),
1719 max_clients_per_query);
1722 result = ns_config_get(maps, "dnssec-enable", &obj);
1723 INSIST(result == ISC_R_SUCCESS);
1724 view->enablednssec = cfg_obj_asboolean(obj);
1727 result = ns_config_get(maps, "dnssec-lookaside", &obj);
1728 if (result == ISC_R_SUCCESS) {
1729 for (element = cfg_list_first(obj);
1731 element = cfg_list_next(element))
1737 obj = cfg_listelt_value(element);
1739 dns_fixedname_t fixed;
1743 * When we support multiple dnssec-lookaside
1744 * entries this is how to find the domain to be
1747 dns_fixedname_init(&fixed);
1748 name = dns_fixedname_name(&fixed);
1749 str = cfg_obj_asstring(cfg_tuple_get(obj,
1751 isc_buffer_init(&b, str, strlen(str));
1752 isc_buffer_add(&b, strlen(str));
1753 CHECK(dns_name_fromtext(name, &b, dns_rootname,
1756 str = cfg_obj_asstring(cfg_tuple_get(obj,
1758 isc_buffer_init(&b, str, strlen(str));
1759 isc_buffer_add(&b, strlen(str));
1760 dlv = dns_fixedname_name(&view->dlv_fixed);
1761 CHECK(dns_name_fromtext(dlv, &b, dns_rootname,
1763 view->dlv = dns_fixedname_name(&view->dlv_fixed);
1769 * For now, there is only one kind of trusted keys, the
1772 CHECK(configure_view_dnsseckeys(vconfig, config, mctx,
1774 dns_resolver_resetmustbesecure(view->resolver);
1776 result = ns_config_get(maps, "dnssec-must-be-secure", &obj);
1777 if (result == ISC_R_SUCCESS)
1778 CHECK(mustbesecure(obj, view->resolver));
1781 result = ns_config_get(maps, "preferred-glue", &obj);
1782 if (result == ISC_R_SUCCESS) {
1783 str = cfg_obj_asstring(obj);
1784 if (strcasecmp(str, "a") == 0)
1785 view->preferred_glue = dns_rdatatype_a;
1786 else if (strcasecmp(str, "aaaa") == 0)
1787 view->preferred_glue = dns_rdatatype_aaaa;
1789 view->preferred_glue = 0;
1791 view->preferred_glue = 0;
1794 result = ns_config_get(maps, "root-delegation-only", &obj);
1795 if (result == ISC_R_SUCCESS) {
1796 dns_view_setrootdelonly(view, ISC_TRUE);
1797 if (!cfg_obj_isvoid(obj)) {
1798 dns_fixedname_t fixed;
1802 const cfg_obj_t *exclude;
1804 dns_fixedname_init(&fixed);
1805 name = dns_fixedname_name(&fixed);
1806 for (element = cfg_list_first(obj);
1808 element = cfg_list_next(element)) {
1809 exclude = cfg_listelt_value(element);
1810 str = cfg_obj_asstring(exclude);
1811 isc_buffer_init(&b, str, strlen(str));
1812 isc_buffer_add(&b, strlen(str));
1813 CHECK(dns_name_fromtext(name, &b, dns_rootname,
1815 CHECK(dns_view_excludedelegationonly(view,
1820 dns_view_setrootdelonly(view, ISC_FALSE);
1823 * Setup automatic empty zones. If recursion is off then
1824 * they are disabled by default.
1827 (void)ns_config_get(maps, "empty-zones-enable", &obj);
1828 (void)ns_config_get(maps, "disable-empty-zone", &disablelist);
1829 if (obj == NULL && disablelist == NULL &&
1830 view->rdclass == dns_rdataclass_in) {
1831 rfc1918 = ISC_FALSE;
1832 empty_zones_enable = view->recursion;
1833 } else if (view->rdclass == dns_rdataclass_in) {
1836 empty_zones_enable = cfg_obj_asboolean(obj);
1838 empty_zones_enable = view->recursion;
1840 rfc1918 = ISC_FALSE;
1841 empty_zones_enable = ISC_FALSE;
1843 if (empty_zones_enable) {
1846 dns_fixedname_t fixed;
1848 isc_buffer_t buffer;
1850 char server[DNS_NAME_FORMATSIZE + 1];
1851 char contact[DNS_NAME_FORMATSIZE + 1];
1852 isc_boolean_t logit;
1853 const char *empty_dbtype[4] =
1854 { "_builtin", "empty", NULL, NULL };
1855 int empty_dbtypec = 4;
1856 isc_boolean_t zonestats_on;
1858 dns_fixedname_init(&fixed);
1859 name = dns_fixedname_name(&fixed);
1862 result = ns_config_get(maps, "empty-server", &obj);
1863 if (result == ISC_R_SUCCESS) {
1864 str = cfg_obj_asstring(obj);
1865 isc_buffer_init(&buffer, str, strlen(str));
1866 isc_buffer_add(&buffer, strlen(str));
1867 CHECK(dns_name_fromtext(name, &buffer, dns_rootname,
1869 isc_buffer_init(&buffer, server, sizeof(server) - 1);
1870 CHECK(dns_name_totext(name, ISC_FALSE, &buffer));
1871 server[isc_buffer_usedlength(&buffer)] = 0;
1872 empty_dbtype[2] = server;
1874 empty_dbtype[2] = "@";
1877 result = ns_config_get(maps, "empty-contact", &obj);
1878 if (result == ISC_R_SUCCESS) {
1879 str = cfg_obj_asstring(obj);
1880 isc_buffer_init(&buffer, str, strlen(str));
1881 isc_buffer_add(&buffer, strlen(str));
1882 CHECK(dns_name_fromtext(name, &buffer, dns_rootname,
1884 isc_buffer_init(&buffer, contact, sizeof(contact) - 1);
1885 CHECK(dns_name_totext(name, ISC_FALSE, &buffer));
1886 contact[isc_buffer_usedlength(&buffer)] = 0;
1887 empty_dbtype[3] = contact;
1889 empty_dbtype[3] = ".";
1892 result = ns_config_get(maps, "zone-statistics", &obj);
1893 INSIST(result == ISC_R_SUCCESS);
1894 zonestats_on = cfg_obj_asboolean(obj);
1897 for (empty = empty_zones[empty_zone].zone;
1899 empty = empty_zones[++empty_zone].zone)
1901 dns_forwarders_t *forwarders = NULL;
1902 dns_view_t *pview = NULL;
1904 isc_buffer_init(&buffer, empty, strlen(empty));
1905 isc_buffer_add(&buffer, strlen(empty));
1907 * Look for zone on drop list.
1909 CHECK(dns_name_fromtext(name, &buffer, dns_rootname,
1911 if (disablelist != NULL &&
1912 on_disable_list(disablelist, name))
1916 * This zone already exists.
1918 (void)dns_view_findzone(view, name, &zone);
1920 CHECK(setquerystats(zone, mctx, zonestats_on));
1921 dns_zone_detach(&zone);
1926 * If we would forward this name don't add a
1927 * empty zone for it.
1929 result = dns_fwdtable_find(view->fwdtable, name,
1931 if (result == ISC_R_SUCCESS &&
1932 forwarders->fwdpolicy == dns_fwdpolicy_only)
1935 if (!rfc1918 && empty_zones[empty_zone].rfc1918) {
1937 isc_log_write(ns_g_lctx,
1938 NS_LOGCATEGORY_GENERAL,
1939 NS_LOGMODULE_SERVER,
1942 "'empty-zones-enable/"
1943 "disable-empty-zone' "
1944 "not set: disabling "
1945 "RFC 1918 empty zones",
1953 * See if we can re-use a existing zone.
1955 result = dns_viewlist_find(&ns_g_server->viewlist,
1956 view->name, view->rdclass,
1958 if (result != ISC_R_NOTFOUND &&
1959 result != ISC_R_SUCCESS)
1962 if (pview != NULL) {
1963 (void)dns_view_findzone(pview, name, &zone);
1964 dns_view_detach(&pview);
1966 check_dbtype(&zone, empty_dbtypec,
1967 empty_dbtype, mctx);
1969 dns_zone_setview(zone, view);
1970 CHECK(dns_view_addzone(view, zone));
1971 CHECK(setquerystats(zone, mctx,
1973 dns_zone_detach(&zone);
1978 CHECK(dns_zone_create(&zone, mctx));
1979 CHECK(dns_zone_setorigin(zone, name));
1980 dns_zone_setview(zone, view);
1981 CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr, zone));
1982 dns_zone_setclass(zone, view->rdclass);
1983 dns_zone_settype(zone, dns_zone_master);
1984 dns_zone_setstats(zone, ns_g_server->zonestats);
1985 CHECK(dns_zone_setdbtype(zone, empty_dbtypec,
1987 if (view->queryacl != NULL)
1988 dns_zone_setqueryacl(zone, view->queryacl);
1989 if (view->queryonacl != NULL)
1990 dns_zone_setqueryonacl(zone, view->queryonacl);
1991 dns_zone_setdialup(zone, dns_dialuptype_no);
1992 dns_zone_setnotifytype(zone, dns_notifytype_no);
1993 dns_zone_setoption(zone, DNS_ZONEOPT_NOCHECKNS,
1995 CHECK(setquerystats(zone, mctx, zonestats_on));
1996 CHECK(dns_view_addzone(view, zone));
1997 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1998 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
1999 "automatic empty zone%s%s: %s",
2000 sep, viewname, empty);
2001 dns_zone_detach(&zone);
2005 result = ISC_R_SUCCESS;
2009 dns_zone_detach(&zone);
2010 if (dispatch4 != NULL)
2011 dns_dispatch_detach(&dispatch4);
2012 if (dispatch6 != NULL)
2013 dns_dispatch_detach(&dispatch6);
2014 if (resstats != NULL)
2015 isc_stats_detach(&resstats);
2016 if (resquerystats != NULL)
2017 dns_stats_detach(&resquerystats);
2019 dns_order_detach(&order);
2021 isc_mem_detach(&cmctx);
2024 dns_cache_detach(&cache);
2030 configure_hints(dns_view_t *view, const char *filename) {
2031 isc_result_t result;
2035 result = dns_rootns_create(view->mctx, view->rdclass, filename, &db);
2036 if (result == ISC_R_SUCCESS) {
2037 dns_view_sethints(view, db);
2045 configure_alternates(const cfg_obj_t *config, dns_view_t *view,
2046 const cfg_obj_t *alternates)
2048 const cfg_obj_t *portobj;
2049 const cfg_obj_t *addresses;
2050 const cfg_listelt_t *element;
2051 isc_result_t result = ISC_R_SUCCESS;
2055 * Determine which port to send requests to.
2057 if (ns_g_lwresdonly && ns_g_port != 0)
2060 CHECKM(ns_config_getport(config, &port), "port");
2062 if (alternates != NULL) {
2063 portobj = cfg_tuple_get(alternates, "port");
2064 if (cfg_obj_isuint32(portobj)) {
2065 isc_uint32_t val = cfg_obj_asuint32(portobj);
2066 if (val > ISC_UINT16_MAX) {
2067 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
2068 "port '%u' out of range", val);
2069 return (ISC_R_RANGE);
2071 port = (in_port_t) val;
2076 if (alternates != NULL)
2077 addresses = cfg_tuple_get(alternates, "addresses");
2079 for (element = cfg_list_first(addresses);
2081 element = cfg_list_next(element))
2083 const cfg_obj_t *alternate = cfg_listelt_value(element);
2086 if (!cfg_obj_issockaddr(alternate)) {
2087 dns_fixedname_t fixed;
2089 const char *str = cfg_obj_asstring(cfg_tuple_get(
2090 alternate, "name"));
2091 isc_buffer_t buffer;
2092 in_port_t myport = port;
2094 isc_buffer_init(&buffer, str, strlen(str));
2095 isc_buffer_add(&buffer, strlen(str));
2096 dns_fixedname_init(&fixed);
2097 name = dns_fixedname_name(&fixed);
2098 CHECK(dns_name_fromtext(name, &buffer, dns_rootname,
2101 portobj = cfg_tuple_get(alternate, "port");
2102 if (cfg_obj_isuint32(portobj)) {
2103 isc_uint32_t val = cfg_obj_asuint32(portobj);
2104 if (val > ISC_UINT16_MAX) {
2105 cfg_obj_log(portobj, ns_g_lctx,
2107 "port '%u' out of range",
2109 return (ISC_R_RANGE);
2111 myport = (in_port_t) val;
2113 CHECK(dns_resolver_addalternate(view->resolver, NULL,
2118 sa = *cfg_obj_assockaddr(alternate);
2119 if (isc_sockaddr_getport(&sa) == 0)
2120 isc_sockaddr_setport(&sa, port);
2121 CHECK(dns_resolver_addalternate(view->resolver, &sa,
2130 configure_forward(const cfg_obj_t *config, dns_view_t *view, dns_name_t *origin,
2131 const cfg_obj_t *forwarders, const cfg_obj_t *forwardtype)
2133 const cfg_obj_t *portobj;
2134 const cfg_obj_t *faddresses;
2135 const cfg_listelt_t *element;
2136 dns_fwdpolicy_t fwdpolicy = dns_fwdpolicy_none;
2137 isc_sockaddrlist_t addresses;
2139 isc_result_t result;
2142 ISC_LIST_INIT(addresses);
2145 * Determine which port to send forwarded requests to.
2147 if (ns_g_lwresdonly && ns_g_port != 0)
2150 CHECKM(ns_config_getport(config, &port), "port");
2152 if (forwarders != NULL) {
2153 portobj = cfg_tuple_get(forwarders, "port");
2154 if (cfg_obj_isuint32(portobj)) {
2155 isc_uint32_t val = cfg_obj_asuint32(portobj);
2156 if (val > ISC_UINT16_MAX) {
2157 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
2158 "port '%u' out of range", val);
2159 return (ISC_R_RANGE);
2161 port = (in_port_t) val;
2166 if (forwarders != NULL)
2167 faddresses = cfg_tuple_get(forwarders, "addresses");
2169 for (element = cfg_list_first(faddresses);
2171 element = cfg_list_next(element))
2173 const cfg_obj_t *forwarder = cfg_listelt_value(element);
2174 sa = isc_mem_get(view->mctx, sizeof(isc_sockaddr_t));
2176 result = ISC_R_NOMEMORY;
2179 *sa = *cfg_obj_assockaddr(forwarder);
2180 if (isc_sockaddr_getport(sa) == 0)
2181 isc_sockaddr_setport(sa, port);
2182 ISC_LINK_INIT(sa, link);
2183 ISC_LIST_APPEND(addresses, sa, link);
2186 if (ISC_LIST_EMPTY(addresses)) {
2187 if (forwardtype != NULL)
2188 cfg_obj_log(forwarders, ns_g_lctx, ISC_LOG_WARNING,
2189 "no forwarders seen; disabling "
2191 fwdpolicy = dns_fwdpolicy_none;
2193 if (forwardtype == NULL)
2194 fwdpolicy = dns_fwdpolicy_first;
2196 const char *forwardstr = cfg_obj_asstring(forwardtype);
2197 if (strcasecmp(forwardstr, "first") == 0)
2198 fwdpolicy = dns_fwdpolicy_first;
2199 else if (strcasecmp(forwardstr, "only") == 0)
2200 fwdpolicy = dns_fwdpolicy_only;
2206 result = dns_fwdtable_add(view->fwdtable, origin, &addresses,
2208 if (result != ISC_R_SUCCESS) {
2209 char namebuf[DNS_NAME_FORMATSIZE];
2210 dns_name_format(origin, namebuf, sizeof(namebuf));
2211 cfg_obj_log(forwarders, ns_g_lctx, ISC_LOG_WARNING,
2212 "could not set up forwarding for domain '%s': %s",
2213 namebuf, isc_result_totext(result));
2217 result = ISC_R_SUCCESS;
2221 while (!ISC_LIST_EMPTY(addresses)) {
2222 sa = ISC_LIST_HEAD(addresses);
2223 ISC_LIST_UNLINK(addresses, sa, link);
2224 isc_mem_put(view->mctx, sa, sizeof(isc_sockaddr_t));
2231 * Create a new view and add it to the list.
2233 * If 'vconfig' is NULL, create the default view.
2235 * The view created is attached to '*viewp'.
2238 create_view(const cfg_obj_t *vconfig, dns_viewlist_t *viewlist,
2241 isc_result_t result;
2242 const char *viewname;
2243 dns_rdataclass_t viewclass;
2244 dns_view_t *view = NULL;
2246 if (vconfig != NULL) {
2247 const cfg_obj_t *classobj = NULL;
2249 viewname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
2250 classobj = cfg_tuple_get(vconfig, "class");
2251 result = ns_config_getclass(classobj, dns_rdataclass_in,
2254 viewname = "_default";
2255 viewclass = dns_rdataclass_in;
2257 result = dns_viewlist_find(viewlist, viewname, viewclass, &view);
2258 if (result == ISC_R_SUCCESS)
2259 return (ISC_R_EXISTS);
2260 if (result != ISC_R_NOTFOUND)
2262 INSIST(view == NULL);
2264 result = dns_view_create(ns_g_mctx, viewclass, viewname, &view);
2265 if (result != ISC_R_SUCCESS)
2268 ISC_LIST_APPEND(*viewlist, view, link);
2269 dns_view_attach(view, viewp);
2270 return (ISC_R_SUCCESS);
2274 * Configure or reconfigure a zone.
2277 configure_zone(const cfg_obj_t *config, const cfg_obj_t *zconfig,
2278 const cfg_obj_t *vconfig, isc_mem_t *mctx, dns_view_t *view,
2279 cfg_aclconfctx_t *aclconf)
2281 dns_view_t *pview = NULL; /* Production view */
2282 dns_zone_t *zone = NULL; /* New or reused zone */
2283 dns_zone_t *dupzone = NULL;
2284 const cfg_obj_t *options = NULL;
2285 const cfg_obj_t *zoptions = NULL;
2286 const cfg_obj_t *typeobj = NULL;
2287 const cfg_obj_t *forwarders = NULL;
2288 const cfg_obj_t *forwardtype = NULL;
2289 const cfg_obj_t *only = NULL;
2290 isc_result_t result;
2291 isc_result_t tresult;
2292 isc_buffer_t buffer;
2293 dns_fixedname_t fixorigin;
2296 dns_rdataclass_t zclass;
2297 const char *ztypestr;
2300 (void)cfg_map_get(config, "options", &options);
2302 zoptions = cfg_tuple_get(zconfig, "options");
2305 * Get the zone origin as a dns_name_t.
2307 zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
2308 isc_buffer_init(&buffer, zname, strlen(zname));
2309 isc_buffer_add(&buffer, strlen(zname));
2310 dns_fixedname_init(&fixorigin);
2311 CHECK(dns_name_fromtext(dns_fixedname_name(&fixorigin),
2312 &buffer, dns_rootname, ISC_FALSE, NULL));
2313 origin = dns_fixedname_name(&fixorigin);
2315 CHECK(ns_config_getclass(cfg_tuple_get(zconfig, "class"),
2316 view->rdclass, &zclass));
2317 if (zclass != view->rdclass) {
2318 const char *vname = NULL;
2319 if (vconfig != NULL)
2320 vname = cfg_obj_asstring(cfg_tuple_get(vconfig,
2323 vname = "<default view>";
2325 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2326 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
2327 "zone '%s': wrong class for view '%s'",
2329 result = ISC_R_FAILURE;
2333 (void)cfg_map_get(zoptions, "type", &typeobj);
2334 if (typeobj == NULL) {
2335 cfg_obj_log(zconfig, ns_g_lctx, ISC_LOG_ERROR,
2336 "zone '%s' 'type' not specified", zname);
2337 return (ISC_R_FAILURE);
2339 ztypestr = cfg_obj_asstring(typeobj);
2342 * "hints zones" aren't zones. If we've got one,
2343 * configure it and return.
2345 if (strcasecmp(ztypestr, "hint") == 0) {
2346 const cfg_obj_t *fileobj = NULL;
2347 if (cfg_map_get(zoptions, "file", &fileobj) != ISC_R_SUCCESS) {
2348 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2349 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
2350 "zone '%s': 'file' not specified",
2352 result = ISC_R_FAILURE;
2355 if (dns_name_equal(origin, dns_rootname)) {
2356 const char *hintsfile = cfg_obj_asstring(fileobj);
2358 result = configure_hints(view, hintsfile);
2359 if (result != ISC_R_SUCCESS) {
2360 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2361 NS_LOGMODULE_SERVER,
2363 "could not configure root hints "
2364 "from '%s': %s", hintsfile,
2365 isc_result_totext(result));
2369 * Hint zones may also refer to delegation only points.
2372 tresult = cfg_map_get(zoptions, "delegation-only",
2374 if (tresult == ISC_R_SUCCESS && cfg_obj_asboolean(only))
2375 CHECK(dns_view_adddelegationonly(view, origin));
2377 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2378 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
2379 "ignoring non-root hint zone '%s'",
2381 result = ISC_R_SUCCESS;
2383 /* Skip ordinary zone processing. */
2388 * "forward zones" aren't zones either. Translate this syntax into
2389 * the appropriate selective forwarding configuration and return.
2391 if (strcasecmp(ztypestr, "forward") == 0) {
2395 (void)cfg_map_get(zoptions, "forward", &forwardtype);
2396 (void)cfg_map_get(zoptions, "forwarders", &forwarders);
2397 result = configure_forward(config, view, origin, forwarders,
2403 * "delegation-only zones" aren't zones either.
2405 if (strcasecmp(ztypestr, "delegation-only") == 0) {
2406 result = dns_view_adddelegationonly(view, origin);
2411 * Check for duplicates in the new zone table.
2413 result = dns_view_findzone(view, origin, &dupzone);
2414 if (result == ISC_R_SUCCESS) {
2416 * We already have this zone!
2418 cfg_obj_log(zconfig, ns_g_lctx, ISC_LOG_ERROR,
2419 "zone '%s' already exists", zname);
2420 dns_zone_detach(&dupzone);
2421 result = ISC_R_EXISTS;
2424 INSIST(dupzone == NULL);
2427 * See if we can reuse an existing zone. This is
2428 * only possible if all of these are true:
2429 * - The zone's view exists
2430 * - A zone with the right name exists in the view
2431 * - The zone is compatible with the config
2432 * options (e.g., an existing master zone cannot
2433 * be reused if the options specify a slave zone)
2435 result = dns_viewlist_find(&ns_g_server->viewlist,
2436 view->name, view->rdclass,
2438 if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
2441 result = dns_view_findzone(pview, origin, &zone);
2442 if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS)
2444 if (zone != NULL && !ns_zone_reusable(zone, zconfig))
2445 dns_zone_detach(&zone);
2449 * We found a reusable zone. Make it use the
2452 dns_zone_setview(zone, view);
2453 if (view->acache != NULL)
2454 dns_zone_setacache(zone, view->acache);
2457 * We cannot reuse an existing zone, we have
2458 * to create a new one.
2460 CHECK(dns_zone_create(&zone, mctx));
2461 CHECK(dns_zone_setorigin(zone, origin));
2462 dns_zone_setview(zone, view);
2463 if (view->acache != NULL)
2464 dns_zone_setacache(zone, view->acache);
2465 CHECK(dns_zonemgr_managezone(ns_g_server->zonemgr, zone));
2466 dns_zone_setstats(zone, ns_g_server->zonestats);
2470 * If the zone contains a 'forwarders' statement, configure
2471 * selective forwarding.
2474 if (cfg_map_get(zoptions, "forwarders", &forwarders) == ISC_R_SUCCESS)
2477 (void)cfg_map_get(zoptions, "forward", &forwardtype);
2478 CHECK(configure_forward(config, view, origin, forwarders,
2483 * Stub and forward zones may also refer to delegation only points.
2486 if (cfg_map_get(zoptions, "delegation-only", &only) == ISC_R_SUCCESS)
2488 if (cfg_obj_asboolean(only))
2489 CHECK(dns_view_adddelegationonly(view, origin));
2493 * Configure the zone.
2495 CHECK(ns_zone_configure(config, vconfig, zconfig, aclconf, zone));
2498 * Add the zone to its view in the new view list.
2500 CHECK(dns_view_addzone(view, zone));
2504 dns_zone_detach(&zone);
2506 dns_view_detach(&pview);
2512 * Configure a single server quota.
2515 configure_server_quota(const cfg_obj_t **maps, const char *name,
2518 const cfg_obj_t *obj = NULL;
2519 isc_result_t result;
2521 result = ns_config_get(maps, name, &obj);
2522 INSIST(result == ISC_R_SUCCESS);
2523 isc_quota_max(quota, cfg_obj_asuint32(obj));
2527 * This function is called as soon as the 'directory' statement has been
2528 * parsed. This can be extended to support other options if necessary.
2531 directory_callback(const char *clausename, const cfg_obj_t *obj, void *arg) {
2532 isc_result_t result;
2533 const char *directory;
2535 REQUIRE(strcasecmp("directory", clausename) == 0);
2543 directory = cfg_obj_asstring(obj);
2545 if (! isc_file_ischdiridempotent(directory))
2546 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_WARNING,
2547 "option 'directory' contains relative path '%s'",
2550 result = isc_dir_chdir(directory);
2551 if (result != ISC_R_SUCCESS) {
2552 cfg_obj_log(obj, ns_g_lctx, ISC_LOG_ERROR,
2553 "change directory to '%s' failed: %s",
2554 directory, isc_result_totext(result));
2558 return (ISC_R_SUCCESS);
2562 scan_interfaces(ns_server_t *server, isc_boolean_t verbose) {
2563 isc_boolean_t match_mapped = server->aclenv.match_mapped;
2565 ns_interfacemgr_scan(server->interfacemgr, verbose);
2567 * Update the "localhost" and "localnets" ACLs to match the
2568 * current set of network interfaces.
2570 dns_aclenv_copy(&server->aclenv,
2571 ns_interfacemgr_getaclenv(server->interfacemgr));
2573 server->aclenv.match_mapped = match_mapped;
2577 add_listenelt(isc_mem_t *mctx, ns_listenlist_t *list, isc_sockaddr_t *addr,
2578 isc_boolean_t wcardport_ok)
2580 ns_listenelt_t *lelt = NULL;
2581 dns_acl_t *src_acl = NULL;
2582 isc_result_t result;
2583 isc_sockaddr_t any_sa6;
2584 isc_netaddr_t netaddr;
2586 REQUIRE(isc_sockaddr_pf(addr) == AF_INET6);
2588 isc_sockaddr_any6(&any_sa6);
2589 if (!isc_sockaddr_equal(&any_sa6, addr) &&
2590 (wcardport_ok || isc_sockaddr_getport(addr) != 0)) {
2591 isc_netaddr_fromin6(&netaddr, &addr->type.sin6.sin6_addr);
2593 result = dns_acl_create(mctx, 0, &src_acl);
2594 if (result != ISC_R_SUCCESS)
2597 result = dns_iptable_addprefix(src_acl->iptable,
2598 &netaddr, 128, ISC_TRUE);
2599 if (result != ISC_R_SUCCESS)
2602 result = ns_listenelt_create(mctx, isc_sockaddr_getport(addr),
2604 if (result != ISC_R_SUCCESS)
2606 ISC_LIST_APPEND(list->elts, lelt, link);
2609 return (ISC_R_SUCCESS);
2612 INSIST(lelt == NULL);
2613 dns_acl_detach(&src_acl);
2619 * Make a list of xxx-source addresses and call ns_interfacemgr_adjust()
2620 * to update the listening interfaces accordingly.
2621 * We currently only consider IPv6, because this only affects IPv6 wildcard
2625 adjust_interfaces(ns_server_t *server, isc_mem_t *mctx) {
2626 isc_result_t result;
2627 ns_listenlist_t *list = NULL;
2629 dns_zone_t *zone, *next;
2630 isc_sockaddr_t addr, *addrp;
2632 result = ns_listenlist_create(mctx, &list);
2633 if (result != ISC_R_SUCCESS)
2636 for (view = ISC_LIST_HEAD(server->viewlist);
2638 view = ISC_LIST_NEXT(view, link)) {
2639 dns_dispatch_t *dispatch6;
2641 dispatch6 = dns_resolver_dispatchv6(view->resolver);
2642 if (dispatch6 == NULL)
2644 result = dns_dispatch_getlocaladdress(dispatch6, &addr);
2645 if (result != ISC_R_SUCCESS)
2649 * We always add non-wildcard address regardless of whether
2650 * the port is 'any' (the fourth arg is TRUE): if the port is
2651 * specific, we need to add it since it may conflict with a
2652 * listening interface; if it's zero, we'll dynamically open
2653 * query ports, and some of them may override an existing
2654 * wildcard IPv6 port.
2656 result = add_listenelt(mctx, list, &addr, ISC_TRUE);
2657 if (result != ISC_R_SUCCESS)
2662 for (result = dns_zone_first(server->zonemgr, &zone);
2663 result == ISC_R_SUCCESS;
2664 next = NULL, result = dns_zone_next(zone, &next), zone = next) {
2665 dns_view_t *zoneview;
2668 * At this point the zone list may contain a stale zone
2669 * just removed from the configuration. To see the validity,
2670 * check if the corresponding view is in our current view list.
2671 * There may also be old zones that are still in the process
2672 * of shutting down and have detached from their old view
2673 * (zoneview == NULL).
2675 zoneview = dns_zone_getview(zone);
2676 if (zoneview == NULL)
2678 for (view = ISC_LIST_HEAD(server->viewlist);
2679 view != NULL && view != zoneview;
2680 view = ISC_LIST_NEXT(view, link))
2685 addrp = dns_zone_getnotifysrc6(zone);
2686 result = add_listenelt(mctx, list, addrp, ISC_FALSE);
2687 if (result != ISC_R_SUCCESS)
2690 addrp = dns_zone_getxfrsource6(zone);
2691 result = add_listenelt(mctx, list, addrp, ISC_FALSE);
2692 if (result != ISC_R_SUCCESS)
2696 ns_interfacemgr_adjust(server->interfacemgr, list, ISC_TRUE);
2699 ns_listenlist_detach(&list);
2704 * Even when we failed the procedure, most of other interfaces
2705 * should work correctly. We therefore just warn it.
2707 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2708 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
2709 "could not adjust the listen-on list; "
2710 "some interfaces may not work");
2715 * This event callback is invoked to do periodic network
2716 * interface scanning.
2719 interface_timer_tick(isc_task_t *task, isc_event_t *event) {
2720 isc_result_t result;
2721 ns_server_t *server = (ns_server_t *) event->ev_arg;
2722 INSIST(task == server->task);
2724 isc_event_free(&event);
2726 * XXX should scan interfaces unlocked and get exclusive access
2727 * only to replace ACLs.
2729 result = isc_task_beginexclusive(server->task);
2730 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2731 scan_interfaces(server, ISC_FALSE);
2732 isc_task_endexclusive(server->task);
2736 heartbeat_timer_tick(isc_task_t *task, isc_event_t *event) {
2737 ns_server_t *server = (ns_server_t *) event->ev_arg;
2741 isc_event_free(&event);
2742 view = ISC_LIST_HEAD(server->viewlist);
2743 while (view != NULL) {
2744 dns_view_dialup(view);
2745 view = ISC_LIST_NEXT(view, link);
2750 pps_timer_tick(isc_task_t *task, isc_event_t *event) {
2751 static unsigned int oldrequests = 0;
2752 unsigned int requests = ns_client_requests;
2755 isc_event_free(&event);
2758 * Don't worry about wrapping as the overflow result will be right.
2760 dns_pps = (requests - oldrequests) / 1200;
2761 oldrequests = requests;
2765 * Replace the current value of '*field', a dynamically allocated
2766 * string or NULL, with a dynamically allocated copy of the
2767 * null-terminated string pointed to by 'value', or NULL.
2770 setstring(ns_server_t *server, char **field, const char *value) {
2773 if (value != NULL) {
2774 copy = isc_mem_strdup(server->mctx, value);
2776 return (ISC_R_NOMEMORY);
2782 isc_mem_free(server->mctx, *field);
2785 return (ISC_R_SUCCESS);
2789 * Replace the current value of '*field', a dynamically allocated
2790 * string or NULL, with another dynamically allocated string
2791 * or NULL if whether 'obj' is a string or void value, respectively.
2794 setoptstring(ns_server_t *server, char **field, const cfg_obj_t *obj) {
2795 if (cfg_obj_isvoid(obj))
2796 return (setstring(server, field, NULL));
2798 return (setstring(server, field, cfg_obj_asstring(obj)));
2802 set_limit(const cfg_obj_t **maps, const char *configname,
2803 const char *description, isc_resource_t resourceid,
2804 isc_resourcevalue_t defaultvalue)
2806 const cfg_obj_t *obj = NULL;
2807 const char *resource;
2808 isc_resourcevalue_t value;
2809 isc_result_t result;
2811 if (ns_config_get(maps, configname, &obj) != ISC_R_SUCCESS)
2814 if (cfg_obj_isstring(obj)) {
2815 resource = cfg_obj_asstring(obj);
2816 if (strcasecmp(resource, "unlimited") == 0)
2817 value = ISC_RESOURCE_UNLIMITED;
2819 INSIST(strcasecmp(resource, "default") == 0);
2820 value = defaultvalue;
2823 value = cfg_obj_asuint64(obj);
2825 result = isc_resource_setlimit(resourceid, value);
2826 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
2827 result == ISC_R_SUCCESS ?
2828 ISC_LOG_DEBUG(3) : ISC_LOG_WARNING,
2829 "set maximum %s to %" ISC_PRINT_QUADFORMAT "u: %s",
2830 description, value, isc_result_totext(result));
2833 #define SETLIMIT(cfgvar, resource, description) \
2834 set_limit(maps, cfgvar, description, isc_resource_ ## resource, \
2835 ns_g_init ## resource)
2838 set_limits(const cfg_obj_t **maps) {
2839 SETLIMIT("stacksize", stacksize, "stack size");
2840 SETLIMIT("datasize", datasize, "data size");
2841 SETLIMIT("coresize", coresize, "core size");
2842 SETLIMIT("files", openfiles, "open files");
2846 portset_fromconf(isc_portset_t *portset, const cfg_obj_t *ports,
2847 isc_boolean_t positive)
2849 const cfg_listelt_t *element;
2851 for (element = cfg_list_first(ports);
2853 element = cfg_list_next(element)) {
2854 const cfg_obj_t *obj = cfg_listelt_value(element);
2856 if (cfg_obj_isuint32(obj)) {
2857 in_port_t port = (in_port_t)cfg_obj_asuint32(obj);
2860 isc_portset_add(portset, port);
2862 isc_portset_remove(portset, port);
2864 const cfg_obj_t *obj_loport, *obj_hiport;
2865 in_port_t loport, hiport;
2867 obj_loport = cfg_tuple_get(obj, "loport");
2868 loport = (in_port_t)cfg_obj_asuint32(obj_loport);
2869 obj_hiport = cfg_tuple_get(obj, "hiport");
2870 hiport = (in_port_t)cfg_obj_asuint32(obj_hiport);
2873 isc_portset_addrange(portset, loport, hiport);
2875 isc_portset_removerange(portset, loport,
2883 removed(dns_zone_t *zone, void *uap) {
2886 if (dns_zone_getview(zone) != uap)
2887 return (ISC_R_SUCCESS);
2889 switch (dns_zone_gettype(zone)) {
2890 case dns_zone_master:
2893 case dns_zone_slave:
2903 dns_zone_log(zone, ISC_LOG_INFO, "(%s) removed", type);
2904 return (ISC_R_SUCCESS);
2908 load_configuration(const char *filename, ns_server_t *server,
2909 isc_boolean_t first_time)
2911 cfg_aclconfctx_t aclconfctx;
2913 cfg_parser_t *parser = NULL;
2914 const cfg_listelt_t *element;
2915 const cfg_obj_t *builtin_views;
2916 const cfg_obj_t *maps[3];
2917 const cfg_obj_t *obj;
2918 const cfg_obj_t *options;
2919 const cfg_obj_t *usev4ports, *avoidv4ports, *usev6ports, *avoidv6ports;
2920 const cfg_obj_t *views;
2921 dns_view_t *view = NULL;
2922 dns_view_t *view_next;
2923 dns_viewlist_t tmpviewlist;
2924 dns_viewlist_t viewlist;
2925 in_port_t listen_port, udpport_low, udpport_high;
2927 isc_interval_t interval;
2928 isc_portset_t *v4portset = NULL;
2929 isc_portset_t *v6portset = NULL;
2930 isc_resourcevalue_t nfiles;
2931 isc_result_t result;
2932 isc_uint32_t heartbeat_interval;
2933 isc_uint32_t interface_interval;
2934 isc_uint32_t reserved;
2935 isc_uint32_t udpsize;
2936 unsigned int maxsocks;
2938 cfg_aclconfctx_init(&aclconfctx);
2939 ISC_LIST_INIT(viewlist);
2941 /* Ensure exclusive access to configuration data. */
2942 result = isc_task_beginexclusive(server->task);
2943 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2946 * Parse the global default pseudo-config file.
2949 CHECK(ns_config_parsedefaults(ns_g_parser, &ns_g_config));
2950 RUNTIME_CHECK(cfg_map_get(ns_g_config, "options",
2956 * Parse the configuration file using the new config code.
2958 result = ISC_R_FAILURE;
2962 * Unless this is lwresd with the -C option, parse the config file.
2964 if (!(ns_g_lwresdonly && lwresd_g_useresolvconf)) {
2965 isc_log_write(ns_g_lctx,
2966 NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
2967 ISC_LOG_INFO, "loading configuration from '%s'",
2969 CHECK(cfg_parser_create(ns_g_mctx, ns_g_lctx, &parser));
2970 cfg_parser_setcallback(parser, directory_callback, NULL);
2971 result = cfg_parse_file(parser, filename, &cfg_type_namedconf,
2976 * If this is lwresd with the -C option, or lwresd with no -C or -c
2977 * option where the above parsing failed, parse resolv.conf.
2979 if (ns_g_lwresdonly &&
2980 (lwresd_g_useresolvconf ||
2981 (!ns_g_conffileset && result == ISC_R_FILENOTFOUND)))
2983 isc_log_write(ns_g_lctx,
2984 NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
2985 ISC_LOG_INFO, "loading configuration from '%s'",
2986 lwresd_g_resolvconffile);
2988 cfg_parser_destroy(&parser);
2989 CHECK(cfg_parser_create(ns_g_mctx, ns_g_lctx, &parser));
2990 result = ns_lwresd_parseeresolvconf(ns_g_mctx, parser,
2996 * Check the validity of the configuration.
2998 CHECK(bind9_check_namedconf(config, ns_g_lctx, ns_g_mctx));
3001 * Fill in the maps array, used for resolving defaults.
3005 result = cfg_map_get(config, "options", &options);
3006 if (result == ISC_R_SUCCESS)
3007 maps[i++] = options;
3008 maps[i++] = ns_g_defaults;
3012 * Set process limits, which (usually) needs to be done as root.
3017 * Check if max number of open sockets that the system allows is
3018 * sufficiently large. Failing this condition is not necessarily fatal,
3019 * but may cause subsequent runtime failures for a busy recursive
3022 result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &maxsocks);
3023 if (result != ISC_R_SUCCESS)
3025 result = isc_resource_getcurlimit(isc_resource_openfiles, &nfiles);
3026 if (result == ISC_R_SUCCESS && (isc_resourcevalue_t)maxsocks > nfiles) {
3027 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3028 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
3029 "max open files (%" ISC_PRINT_QUADFORMAT "u)"
3030 " is smaller than max sockets (%u)",
3035 * Set the number of socket reserved for TCP, stdio etc.
3038 result = ns_config_get(maps, "reserved-sockets", &obj);
3039 INSIST(result == ISC_R_SUCCESS);
3040 reserved = cfg_obj_asuint32(obj);
3041 if (maxsocks != 0) {
3042 if (maxsocks < 128U) /* Prevent underflow. */
3044 else if (reserved > maxsocks - 128U) /* Minimum UDP space. */
3045 reserved = maxsocks - 128;
3047 /* Minimum TCP/stdio space. */
3048 if (reserved < 128U)
3050 if (reserved + 128U > maxsocks && maxsocks != 0) {
3051 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3052 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
3053 "less than 128 UDP sockets available after "
3054 "applying 'reserved-sockets' and 'maxsockets'");
3056 isc__socketmgr_setreserved(ns_g_socketmgr, reserved);
3059 * Configure various server options.
3061 configure_server_quota(maps, "transfers-out", &server->xfroutquota);
3062 configure_server_quota(maps, "tcp-clients", &server->tcpquota);
3063 configure_server_quota(maps, "recursive-clients",
3064 &server->recursionquota);
3065 if (server->recursionquota.max > 1000)
3066 isc_quota_soft(&server->recursionquota,
3067 server->recursionquota.max - 100);
3069 isc_quota_soft(&server->recursionquota, 0);
3071 CHECK(configure_view_acl(NULL, config, "blackhole", &aclconfctx,
3072 ns_g_mctx, &server->blackholeacl));
3073 if (server->blackholeacl != NULL)
3074 dns_dispatchmgr_setblackhole(ns_g_dispatchmgr,
3075 server->blackholeacl);
3078 result = ns_config_get(maps, "match-mapped-addresses", &obj);
3079 INSIST(result == ISC_R_SUCCESS);
3080 server->aclenv.match_mapped = cfg_obj_asboolean(obj);
3082 CHECKM(ns_statschannels_configure(ns_g_server, config, &aclconfctx),
3083 "configuring statistics server(s)");
3086 * Configure sets of UDP query source ports.
3088 CHECKM(isc_portset_create(ns_g_mctx, &v4portset),
3089 "creating UDP port set");
3090 CHECKM(isc_portset_create(ns_g_mctx, &v6portset),
3091 "creating UDP port set");
3095 avoidv4ports = NULL;
3096 avoidv6ports = NULL;
3098 (void)ns_config_get(maps, "use-v4-udp-ports", &usev4ports);
3099 if (usev4ports != NULL)
3100 portset_fromconf(v4portset, usev4ports, ISC_TRUE);
3102 CHECKM(isc_net_getudpportrange(AF_INET, &udpport_low,
3104 "get the default UDP/IPv4 port range");
3105 if (udpport_low == udpport_high)
3106 isc_portset_add(v4portset, udpport_low);
3108 isc_portset_addrange(v4portset, udpport_low,
3111 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3112 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
3113 "using default UDP/IPv4 port range: [%d, %d]",
3114 udpport_low, udpport_high);
3116 (void)ns_config_get(maps, "avoid-v4-udp-ports", &avoidv4ports);
3117 if (avoidv4ports != NULL)
3118 portset_fromconf(v4portset, avoidv4ports, ISC_FALSE);
3120 (void)ns_config_get(maps, "use-v6-udp-ports", &usev6ports);
3121 if (usev6ports != NULL)
3122 portset_fromconf(v6portset, usev6ports, ISC_TRUE);
3124 CHECKM(isc_net_getudpportrange(AF_INET6, &udpport_low,
3126 "get the default UDP/IPv6 port range");
3127 if (udpport_low == udpport_high)
3128 isc_portset_add(v6portset, udpport_low);
3130 isc_portset_addrange(v6portset, udpport_low,
3133 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3134 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
3135 "using default UDP/IPv6 port range: [%d, %d]",
3136 udpport_low, udpport_high);
3138 (void)ns_config_get(maps, "avoid-v6-udp-ports", &avoidv6ports);
3139 if (avoidv6ports != NULL)
3140 portset_fromconf(v6portset, avoidv6ports, ISC_FALSE);
3142 dns_dispatchmgr_setavailports(ns_g_dispatchmgr, v4portset, v6portset);
3145 * Set the EDNS UDP size when we don't match a view.
3148 result = ns_config_get(maps, "edns-udp-size", &obj);
3149 INSIST(result == ISC_R_SUCCESS);
3150 udpsize = cfg_obj_asuint32(obj);
3155 ns_g_udpsize = (isc_uint16_t)udpsize;
3158 * Configure the zone manager.
3161 result = ns_config_get(maps, "transfers-in", &obj);
3162 INSIST(result == ISC_R_SUCCESS);
3163 dns_zonemgr_settransfersin(server->zonemgr, cfg_obj_asuint32(obj));
3166 result = ns_config_get(maps, "transfers-per-ns", &obj);
3167 INSIST(result == ISC_R_SUCCESS);
3168 dns_zonemgr_settransfersperns(server->zonemgr, cfg_obj_asuint32(obj));
3171 result = ns_config_get(maps, "serial-query-rate", &obj);
3172 INSIST(result == ISC_R_SUCCESS);
3173 dns_zonemgr_setserialqueryrate(server->zonemgr, cfg_obj_asuint32(obj));
3176 * Determine which port to use for listening for incoming connections.
3179 listen_port = ns_g_port;
3181 CHECKM(ns_config_getport(config, &listen_port), "port");
3184 * Find the listen queue depth.
3187 result = ns_config_get(maps, "tcp-listen-queue", &obj);
3188 INSIST(result == ISC_R_SUCCESS);
3189 ns_g_listen = cfg_obj_asuint32(obj);
3190 if (ns_g_listen < 3)
3194 * Configure the interface manager according to the "listen-on"
3198 const cfg_obj_t *clistenon = NULL;
3199 ns_listenlist_t *listenon = NULL;
3203 * Even though listen-on is present in the default
3204 * configuration, we can't use it here, since it isn't
3205 * used if we're in lwresd mode. This way is easier.
3207 if (options != NULL)
3208 (void)cfg_map_get(options, "listen-on", &clistenon);
3209 if (clistenon != NULL) {
3210 result = ns_listenlist_fromconfig(clistenon,
3215 } else if (!ns_g_lwresdonly) {
3217 * Not specified, use default.
3219 CHECK(ns_listenlist_default(ns_g_mctx, listen_port,
3220 ISC_TRUE, &listenon));
3222 if (listenon != NULL) {
3223 ns_interfacemgr_setlistenon4(server->interfacemgr,
3225 ns_listenlist_detach(&listenon);
3232 const cfg_obj_t *clistenon = NULL;
3233 ns_listenlist_t *listenon = NULL;
3235 if (options != NULL)
3236 (void)cfg_map_get(options, "listen-on-v6", &clistenon);
3237 if (clistenon != NULL) {
3238 result = ns_listenlist_fromconfig(clistenon,
3243 } else if (!ns_g_lwresdonly) {
3244 isc_boolean_t enable;
3246 * Not specified, use default.
3248 enable = ISC_TF(isc_net_probeipv4() != ISC_R_SUCCESS);
3249 CHECK(ns_listenlist_default(ns_g_mctx, listen_port,
3250 enable, &listenon));
3252 if (listenon != NULL) {
3253 ns_interfacemgr_setlistenon6(server->interfacemgr,
3255 ns_listenlist_detach(&listenon);
3260 * Rescan the interface list to pick up changes in the
3261 * listen-on option. It's important that we do this before we try
3262 * to configure the query source, since the dispatcher we use might
3263 * be shared with an interface.
3265 scan_interfaces(server, ISC_TRUE);
3268 * Arrange for further interface scanning to occur periodically
3269 * as specified by the "interface-interval" option.
3272 result = ns_config_get(maps, "interface-interval", &obj);
3273 INSIST(result == ISC_R_SUCCESS);
3274 interface_interval = cfg_obj_asuint32(obj) * 60;
3275 if (interface_interval == 0) {
3276 CHECK(isc_timer_reset(server->interface_timer,
3277 isc_timertype_inactive,
3278 NULL, NULL, ISC_TRUE));
3279 } else if (server->interface_interval != interface_interval) {
3280 isc_interval_set(&interval, interface_interval, 0);
3281 CHECK(isc_timer_reset(server->interface_timer,
3282 isc_timertype_ticker,
3283 NULL, &interval, ISC_FALSE));
3285 server->interface_interval = interface_interval;
3288 * Configure the dialup heartbeat timer.
3291 result = ns_config_get(maps, "heartbeat-interval", &obj);
3292 INSIST(result == ISC_R_SUCCESS);
3293 heartbeat_interval = cfg_obj_asuint32(obj) * 60;
3294 if (heartbeat_interval == 0) {
3295 CHECK(isc_timer_reset(server->heartbeat_timer,
3296 isc_timertype_inactive,
3297 NULL, NULL, ISC_TRUE));
3298 } else if (server->heartbeat_interval != heartbeat_interval) {
3299 isc_interval_set(&interval, heartbeat_interval, 0);
3300 CHECK(isc_timer_reset(server->heartbeat_timer,
3301 isc_timertype_ticker,
3302 NULL, &interval, ISC_FALSE));
3304 server->heartbeat_interval = heartbeat_interval;
3306 isc_interval_set(&interval, 1200, 0);
3307 CHECK(isc_timer_reset(server->pps_timer, isc_timertype_ticker, NULL,
3308 &interval, ISC_FALSE));
3311 * Configure and freeze all explicit views. Explicit
3312 * views that have zones were already created at parsing
3313 * time, but views with no zones must be created here.
3316 (void)cfg_map_get(config, "view", &views);
3317 for (element = cfg_list_first(views);
3319 element = cfg_list_next(element))
3321 const cfg_obj_t *vconfig = cfg_listelt_value(element);
3324 CHECK(create_view(vconfig, &viewlist, &view));
3325 INSIST(view != NULL);
3326 CHECK(configure_view(view, config, vconfig,
3327 ns_g_mctx, &aclconfctx, ISC_TRUE));
3328 dns_view_freeze(view);
3329 dns_view_detach(&view);
3333 * Make sure we have a default view if and only if there
3334 * were no explicit views.
3336 if (views == NULL) {
3338 * No explicit views; there ought to be a default view.
3339 * There may already be one created as a side effect
3340 * of zone statements, or we may have to create one.
3341 * In either case, we need to configure and freeze it.
3343 CHECK(create_view(NULL, &viewlist, &view));
3344 CHECK(configure_view(view, config, NULL, ns_g_mctx,
3345 &aclconfctx, ISC_TRUE));
3346 dns_view_freeze(view);
3347 dns_view_detach(&view);
3351 * Create (or recreate) the built-in views. Currently
3352 * there is only one, the _bind view.
3354 builtin_views = NULL;
3355 RUNTIME_CHECK(cfg_map_get(ns_g_config, "view",
3356 &builtin_views) == ISC_R_SUCCESS);
3357 for (element = cfg_list_first(builtin_views);
3359 element = cfg_list_next(element))
3361 const cfg_obj_t *vconfig = cfg_listelt_value(element);
3362 CHECK(create_view(vconfig, &viewlist, &view));
3363 CHECK(configure_view(view, config, vconfig, ns_g_mctx,
3364 &aclconfctx, ISC_FALSE));
3365 dns_view_freeze(view);
3366 dns_view_detach(&view);
3371 * Swap our new view list with the production one.
3373 tmpviewlist = server->viewlist;
3374 server->viewlist = viewlist;
3375 viewlist = tmpviewlist;
3378 * Load the TKEY information from the configuration.
3380 if (options != NULL) {
3381 dns_tkeyctx_t *t = NULL;
3382 CHECKM(ns_tkeyctx_fromconfig(options, ns_g_mctx, ns_g_entropy,
3384 "configuring TKEY");
3385 if (server->tkeyctx != NULL)
3386 dns_tkeyctx_destroy(&server->tkeyctx);
3387 server->tkeyctx = t;
3391 * Bind the control port(s).
3393 CHECKM(ns_controls_configure(ns_g_server->controls, config,
3395 "binding control channel(s)");
3398 * Bind the lwresd port(s).
3400 CHECKM(ns_lwresd_configure(ns_g_mctx, config),
3401 "binding lightweight resolver ports");
3404 * Open the source of entropy.
3408 result = ns_config_get(maps, "random-device", &obj);
3409 if (result != ISC_R_SUCCESS) {
3410 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3411 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
3412 "no source of entropy found");
3414 const char *randomdev = cfg_obj_asstring(obj);
3415 result = isc_entropy_createfilesource(ns_g_entropy,
3417 if (result != ISC_R_SUCCESS)
3418 isc_log_write(ns_g_lctx,
3419 NS_LOGCATEGORY_GENERAL,
3420 NS_LOGMODULE_SERVER,
3422 "could not open entropy source "
3425 isc_result_totext(result));
3426 #ifdef PATH_RANDOMDEV
3427 if (ns_g_fallbackentropy != NULL) {
3428 if (result != ISC_R_SUCCESS) {
3429 isc_log_write(ns_g_lctx,
3430 NS_LOGCATEGORY_GENERAL,
3431 NS_LOGMODULE_SERVER,
3433 "using pre-chroot entropy source "
3436 isc_entropy_detach(&ns_g_entropy);
3437 isc_entropy_attach(ns_g_fallbackentropy,
3440 isc_entropy_detach(&ns_g_fallbackentropy);
3447 * Relinquish root privileges.
3453 * Check that the working directory is writable.
3455 if (access(".", W_OK) != 0) {
3456 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3457 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
3458 "the working directory is not writable");
3462 * Configure the logging system.
3464 * Do this after changing UID to make sure that any log
3465 * files specified in named.conf get created by the
3466 * unprivileged user, not root.
3468 if (ns_g_logstderr) {
3469 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3470 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
3471 "ignoring config file logging "
3472 "statement due to -g option");
3474 const cfg_obj_t *logobj = NULL;
3475 isc_logconfig_t *logc = NULL;
3477 CHECKM(isc_logconfig_create(ns_g_lctx, &logc),
3478 "creating new logging configuration");
3481 (void)cfg_map_get(config, "logging", &logobj);
3482 if (logobj != NULL) {
3483 CHECKM(ns_log_configure(logc, logobj),
3484 "configuring logging");
3486 CHECKM(ns_log_setdefaultchannels(logc),
3487 "setting up default logging channels");
3488 CHECKM(ns_log_setunmatchedcategory(logc),
3489 "setting up default 'category unmatched'");
3490 CHECKM(ns_log_setdefaultcategory(logc),
3491 "setting up default 'category default'");
3494 result = isc_logconfig_use(ns_g_lctx, logc);
3495 if (result != ISC_R_SUCCESS) {
3496 isc_logconfig_destroy(&logc);
3497 CHECKM(result, "installing logging configuration");
3500 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3501 NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(1),
3502 "now using logging configuration from "
3507 * Set the default value of the query logging flag depending
3508 * whether a "queries" category has been defined. This is
3509 * a disgusting hack, but we need to do this for BIND 8
3513 const cfg_obj_t *logobj = NULL;
3514 const cfg_obj_t *categories = NULL;
3517 if (ns_config_get(maps, "querylog", &obj) == ISC_R_SUCCESS) {
3518 server->log_queries = cfg_obj_asboolean(obj);
3521 (void)cfg_map_get(config, "logging", &logobj);
3523 (void)cfg_map_get(logobj, "category",
3525 if (categories != NULL) {
3526 const cfg_listelt_t *element;
3527 for (element = cfg_list_first(categories);
3529 element = cfg_list_next(element))
3531 const cfg_obj_t *catobj;
3534 obj = cfg_listelt_value(element);
3535 catobj = cfg_tuple_get(obj, "name");
3536 str = cfg_obj_asstring(catobj);
3537 if (strcasecmp(str, "queries") == 0)
3538 server->log_queries = ISC_TRUE;
3545 if (ns_config_get(maps, "pid-file", &obj) == ISC_R_SUCCESS)
3546 if (cfg_obj_isvoid(obj))
3547 ns_os_writepidfile(NULL, first_time);
3549 ns_os_writepidfile(cfg_obj_asstring(obj), first_time);
3550 else if (ns_g_lwresdonly)
3551 ns_os_writepidfile(lwresd_g_defaultpidfile, first_time);
3553 ns_os_writepidfile(ns_g_defaultpidfile, first_time);
3556 if (options != NULL &&
3557 cfg_map_get(options, "memstatistics", &obj) == ISC_R_SUCCESS)
3558 ns_g_memstatistics = cfg_obj_asboolean(obj);
3560 ns_g_memstatistics =
3561 ISC_TF((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0);
3564 if (ns_config_get(maps, "memstatistics-file", &obj) == ISC_R_SUCCESS)
3565 ns_main_setmemstats(cfg_obj_asstring(obj));
3566 else if (ns_g_memstatistics)
3567 ns_main_setmemstats("named.memstats");
3569 ns_main_setmemstats(NULL);
3572 result = ns_config_get(maps, "statistics-file", &obj);
3573 INSIST(result == ISC_R_SUCCESS);
3574 CHECKM(setstring(server, &server->statsfile, cfg_obj_asstring(obj)),
3578 result = ns_config_get(maps, "dump-file", &obj);
3579 INSIST(result == ISC_R_SUCCESS);
3580 CHECKM(setstring(server, &server->dumpfile, cfg_obj_asstring(obj)),
3584 result = ns_config_get(maps, "recursing-file", &obj);
3585 INSIST(result == ISC_R_SUCCESS);
3586 CHECKM(setstring(server, &server->recfile, cfg_obj_asstring(obj)),
3590 result = ns_config_get(maps, "version", &obj);
3591 if (result == ISC_R_SUCCESS) {
3592 CHECKM(setoptstring(server, &server->version, obj), "strdup");
3593 server->version_set = ISC_TRUE;
3595 server->version_set = ISC_FALSE;
3599 result = ns_config_get(maps, "hostname", &obj);
3600 if (result == ISC_R_SUCCESS) {
3601 CHECKM(setoptstring(server, &server->hostname, obj), "strdup");
3602 server->hostname_set = ISC_TRUE;
3604 server->hostname_set = ISC_FALSE;
3608 result = ns_config_get(maps, "server-id", &obj);
3609 server->server_usehostname = ISC_FALSE;
3610 if (result == ISC_R_SUCCESS && cfg_obj_isboolean(obj)) {
3611 /* The parser translates "hostname" to ISC_TRUE */
3612 server->server_usehostname = cfg_obj_asboolean(obj);
3613 result = setstring(server, &server->server_id, NULL);
3614 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3615 } else if (result == ISC_R_SUCCESS) {
3616 /* Found a quoted string */
3617 CHECKM(setoptstring(server, &server->server_id, obj), "strdup");
3619 result = setstring(server, &server->server_id, NULL);
3620 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3624 result = ns_config_get(maps, "flush-zones-on-shutdown", &obj);
3625 if (result == ISC_R_SUCCESS) {
3626 server->flushonshutdown = cfg_obj_asboolean(obj);
3628 server->flushonshutdown = ISC_FALSE;
3631 result = ISC_R_SUCCESS;
3634 if (v4portset != NULL)
3635 isc_portset_destroy(ns_g_mctx, &v4portset);
3637 if (v6portset != NULL)
3638 isc_portset_destroy(ns_g_mctx, &v6portset);
3640 cfg_aclconfctx_destroy(&aclconfctx);
3642 if (parser != NULL) {
3644 cfg_obj_destroy(parser, &config);
3645 cfg_parser_destroy(&parser);
3649 dns_view_detach(&view);
3652 * This cleans up either the old production view list
3653 * or our temporary list depending on whether they
3654 * were swapped above or not.
3656 for (view = ISC_LIST_HEAD(viewlist);
3659 view_next = ISC_LIST_NEXT(view, link);
3660 ISC_LIST_UNLINK(viewlist, view, link);
3661 if (result == ISC_R_SUCCESS &&
3662 strcmp(view->name, "_bind") != 0)
3663 (void)dns_zt_apply(view->zonetable, ISC_FALSE,
3665 dns_view_detach(&view);
3669 * Adjust the listening interfaces in accordance with the source
3670 * addresses specified in views and zones.
3672 if (isc_net_probeipv6() == ISC_R_SUCCESS)
3673 adjust_interfaces(server, ns_g_mctx);
3675 /* Relinquish exclusive access to configuration data. */
3676 isc_task_endexclusive(server->task);
3678 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
3679 ISC_LOG_DEBUG(1), "load_configuration: %s",
3680 isc_result_totext(result));
3686 load_zones(ns_server_t *server, isc_boolean_t stop) {
3687 isc_result_t result;
3690 result = isc_task_beginexclusive(server->task);
3691 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3694 * Load zone data from disk.
3696 for (view = ISC_LIST_HEAD(server->viewlist);
3698 view = ISC_LIST_NEXT(view, link))
3700 CHECK(dns_view_load(view, stop));
3704 * Force zone maintenance. Do this after loading
3705 * so that we know when we need to force AXFR of
3706 * slave zones whose master files are missing.
3708 CHECK(dns_zonemgr_forcemaint(server->zonemgr));
3710 isc_task_endexclusive(server->task);
3715 load_new_zones(ns_server_t *server, isc_boolean_t stop) {
3716 isc_result_t result;
3719 result = isc_task_beginexclusive(server->task);
3720 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3723 * Load zone data from disk.
3725 for (view = ISC_LIST_HEAD(server->viewlist);
3727 view = ISC_LIST_NEXT(view, link))
3729 CHECK(dns_view_loadnew(view, stop));
3732 * Force zone maintenance. Do this after loading
3733 * so that we know when we need to force AXFR of
3734 * slave zones whose master files are missing.
3736 dns_zonemgr_resumexfrs(server->zonemgr);
3738 isc_task_endexclusive(server->task);
3743 run_server(isc_task_t *task, isc_event_t *event) {
3744 isc_result_t result;
3745 ns_server_t *server = (ns_server_t *)event->ev_arg;
3747 INSIST(task == server->task);
3749 isc_event_free(&event);
3751 CHECKFATAL(dns_dispatchmgr_create(ns_g_mctx, ns_g_entropy,
3753 "creating dispatch manager");
3755 dns_dispatchmgr_setstats(ns_g_dispatchmgr, server->resolverstats);
3757 CHECKFATAL(ns_interfacemgr_create(ns_g_mctx, ns_g_taskmgr,
3758 ns_g_socketmgr, ns_g_dispatchmgr,
3759 &server->interfacemgr),
3760 "creating interface manager");
3762 CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
3763 NULL, NULL, server->task,
3764 interface_timer_tick,
3765 server, &server->interface_timer),
3766 "creating interface timer");
3768 CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
3769 NULL, NULL, server->task,
3770 heartbeat_timer_tick,
3771 server, &server->heartbeat_timer),
3772 "creating heartbeat timer");
3774 CHECKFATAL(isc_timer_create(ns_g_timermgr, isc_timertype_inactive,
3775 NULL, NULL, server->task, pps_timer_tick,
3776 server, &server->pps_timer),
3777 "creating pps timer");
3779 CHECKFATAL(cfg_parser_create(ns_g_mctx, NULL, &ns_g_parser),
3780 "creating default configuration parser");
3782 if (ns_g_lwresdonly)
3783 CHECKFATAL(load_configuration(lwresd_g_conffile, server,
3785 "loading configuration");
3787 CHECKFATAL(load_configuration(ns_g_conffile, server, ISC_TRUE),
3788 "loading configuration");
3792 CHECKFATAL(load_zones(server, ISC_FALSE), "loading zones");
3795 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
3796 ISC_LOG_NOTICE, "running");
3800 ns_server_flushonshutdown(ns_server_t *server, isc_boolean_t flush) {
3802 REQUIRE(NS_SERVER_VALID(server));
3804 server->flushonshutdown = flush;
3808 shutdown_server(isc_task_t *task, isc_event_t *event) {
3809 isc_result_t result;
3810 dns_view_t *view, *view_next;
3811 ns_server_t *server = (ns_server_t *)event->ev_arg;
3812 isc_boolean_t flush = server->flushonshutdown;
3815 INSIST(task == server->task);
3817 result = isc_task_beginexclusive(server->task);
3818 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3820 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
3821 ISC_LOG_INFO, "shutting down%s",
3822 flush ? ": flushing changes" : "");
3824 ns_statschannels_shutdown(server);
3825 ns_controls_shutdown(server->controls);
3826 end_reserved_dispatches(server, ISC_TRUE);
3828 cfg_obj_destroy(ns_g_parser, &ns_g_config);
3829 cfg_parser_destroy(&ns_g_parser);
3831 for (view = ISC_LIST_HEAD(server->viewlist);
3834 view_next = ISC_LIST_NEXT(view, link);
3835 ISC_LIST_UNLINK(server->viewlist, view, link);
3837 dns_view_flushanddetach(&view);
3839 dns_view_detach(&view);
3842 isc_timer_detach(&server->interface_timer);
3843 isc_timer_detach(&server->heartbeat_timer);
3844 isc_timer_detach(&server->pps_timer);
3846 ns_interfacemgr_shutdown(server->interfacemgr);
3847 ns_interfacemgr_detach(&server->interfacemgr);
3849 dns_dispatchmgr_destroy(&ns_g_dispatchmgr);
3851 dns_zonemgr_shutdown(server->zonemgr);
3853 if (server->blackholeacl != NULL)
3854 dns_acl_detach(&server->blackholeacl);
3856 dns_db_detach(&server->in_roothints);
3858 isc_task_endexclusive(server->task);
3860 isc_task_detach(&server->task);
3862 isc_event_free(&event);
3866 ns_server_create(isc_mem_t *mctx, ns_server_t **serverp) {
3867 isc_result_t result;
3869 ns_server_t *server = isc_mem_get(mctx, sizeof(*server));
3871 fatal("allocating server object", ISC_R_NOMEMORY);
3873 server->mctx = mctx;
3874 server->task = NULL;
3876 /* Initialize configuration data with default values. */
3878 result = isc_quota_init(&server->xfroutquota, 10);
3879 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3880 result = isc_quota_init(&server->tcpquota, 10);
3881 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3882 result = isc_quota_init(&server->recursionquota, 100);
3883 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3885 result = dns_aclenv_init(mctx, &server->aclenv);
3886 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3888 /* Initialize server data structures. */
3889 server->zonemgr = NULL;
3890 server->interfacemgr = NULL;
3891 ISC_LIST_INIT(server->viewlist);
3892 server->in_roothints = NULL;
3893 server->blackholeacl = NULL;
3895 CHECKFATAL(dns_rootns_create(mctx, dns_rdataclass_in, NULL,
3896 &server->in_roothints),
3897 "setting up root hints");
3899 CHECKFATAL(isc_mutex_init(&server->reload_event_lock),
3900 "initializing reload event lock");
3901 server->reload_event =
3902 isc_event_allocate(ns_g_mctx, server,
3906 sizeof(isc_event_t));
3907 CHECKFATAL(server->reload_event == NULL ?
3908 ISC_R_NOMEMORY : ISC_R_SUCCESS,
3909 "allocating reload event");
3911 CHECKFATAL(dst_lib_init(ns_g_mctx, ns_g_entropy, ISC_ENTROPY_GOODONLY),
3912 "initializing DST");
3914 server->tkeyctx = NULL;
3915 CHECKFATAL(dns_tkeyctx_create(ns_g_mctx, ns_g_entropy,
3917 "creating TKEY context");
3920 * Setup the server task, which is responsible for coordinating
3921 * startup and shutdown of the server.
3923 CHECKFATAL(isc_task_create(ns_g_taskmgr, 0, &server->task),
3924 "creating server task");
3925 isc_task_setname(server->task, "server", server);
3926 CHECKFATAL(isc_task_onshutdown(server->task, shutdown_server, server),
3927 "isc_task_onshutdown");
3928 CHECKFATAL(isc_app_onrun(ns_g_mctx, server->task, run_server, server),
3931 server->interface_timer = NULL;
3932 server->heartbeat_timer = NULL;
3933 server->pps_timer = NULL;
3935 server->interface_interval = 0;
3936 server->heartbeat_interval = 0;
3938 CHECKFATAL(dns_zonemgr_create(ns_g_mctx, ns_g_taskmgr, ns_g_timermgr,
3939 ns_g_socketmgr, &server->zonemgr),
3940 "dns_zonemgr_create");
3942 server->statsfile = isc_mem_strdup(server->mctx, "named.stats");
3943 CHECKFATAL(server->statsfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
3945 server->nsstats = NULL;
3946 server->rcvquerystats = NULL;
3947 server->opcodestats = NULL;
3948 server->zonestats = NULL;
3949 server->resolverstats = NULL;
3950 server->sockstats = NULL;
3951 CHECKFATAL(isc_stats_create(server->mctx, &server->sockstats,
3952 isc_sockstatscounter_max),
3953 "isc_stats_create");
3954 isc_socketmgr_setstats(ns_g_socketmgr, server->sockstats);
3956 server->dumpfile = isc_mem_strdup(server->mctx, "named_dump.db");
3957 CHECKFATAL(server->dumpfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
3960 server->recfile = isc_mem_strdup(server->mctx, "named.recursing");
3961 CHECKFATAL(server->recfile == NULL ? ISC_R_NOMEMORY : ISC_R_SUCCESS,
3964 server->hostname_set = ISC_FALSE;
3965 server->hostname = NULL;
3966 server->version_set = ISC_FALSE;
3967 server->version = NULL;
3968 server->server_usehostname = ISC_FALSE;
3969 server->server_id = NULL;
3971 CHECKFATAL(isc_stats_create(ns_g_mctx, &server->nsstats,
3972 dns_nsstatscounter_max),
3973 "dns_stats_create (server)");
3975 CHECKFATAL(dns_rdatatypestats_create(ns_g_mctx,
3976 &server->rcvquerystats),
3977 "dns_stats_create (rcvquery)");
3979 CHECKFATAL(dns_opcodestats_create(ns_g_mctx, &server->opcodestats),
3980 "dns_stats_create (opcode)");
3982 CHECKFATAL(isc_stats_create(ns_g_mctx, &server->zonestats,
3983 dns_zonestatscounter_max),
3984 "dns_stats_create (zone)");
3986 CHECKFATAL(isc_stats_create(ns_g_mctx, &server->resolverstats,
3987 dns_resstatscounter_max),
3988 "dns_stats_create (resolver)");
3990 server->flushonshutdown = ISC_FALSE;
3991 server->log_queries = ISC_FALSE;
3993 server->controls = NULL;
3994 CHECKFATAL(ns_controls_create(server, &server->controls),
3995 "ns_controls_create");
3996 server->dispatchgen = 0;
3997 ISC_LIST_INIT(server->dispatches);
3999 ISC_LIST_INIT(server->statschannels);
4001 server->magic = NS_SERVER_MAGIC;
4006 ns_server_destroy(ns_server_t **serverp) {
4007 ns_server_t *server = *serverp;
4008 REQUIRE(NS_SERVER_VALID(server));
4010 ns_controls_destroy(&server->controls);
4012 isc_stats_detach(&server->nsstats);
4013 dns_stats_detach(&server->rcvquerystats);
4014 dns_stats_detach(&server->opcodestats);
4015 isc_stats_detach(&server->zonestats);
4016 isc_stats_detach(&server->resolverstats);
4017 isc_stats_detach(&server->sockstats);
4019 isc_mem_free(server->mctx, server->statsfile);
4020 isc_mem_free(server->mctx, server->dumpfile);
4021 isc_mem_free(server->mctx, server->recfile);
4023 if (server->version != NULL)
4024 isc_mem_free(server->mctx, server->version);
4025 if (server->hostname != NULL)
4026 isc_mem_free(server->mctx, server->hostname);
4027 if (server->server_id != NULL)
4028 isc_mem_free(server->mctx, server->server_id);
4030 dns_zonemgr_detach(&server->zonemgr);
4032 if (server->tkeyctx != NULL)
4033 dns_tkeyctx_destroy(&server->tkeyctx);
4037 isc_event_free(&server->reload_event);
4039 INSIST(ISC_LIST_EMPTY(server->viewlist));
4041 dns_aclenv_destroy(&server->aclenv);
4043 isc_quota_destroy(&server->recursionquota);
4044 isc_quota_destroy(&server->tcpquota);
4045 isc_quota_destroy(&server->xfroutquota);
4048 isc_mem_put(server->mctx, server, sizeof(*server));
4053 fatal(const char *msg, isc_result_t result) {
4054 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
4055 ISC_LOG_CRITICAL, "%s: %s", msg,
4056 isc_result_totext(result));
4057 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
4058 ISC_LOG_CRITICAL, "exiting (due to fatal error)");
4063 start_reserved_dispatches(ns_server_t *server) {
4065 REQUIRE(NS_SERVER_VALID(server));
4067 server->dispatchgen++;
4071 end_reserved_dispatches(ns_server_t *server, isc_boolean_t all) {
4072 ns_dispatch_t *dispatch, *nextdispatch;
4074 REQUIRE(NS_SERVER_VALID(server));
4076 for (dispatch = ISC_LIST_HEAD(server->dispatches);
4078 dispatch = nextdispatch) {
4079 nextdispatch = ISC_LIST_NEXT(dispatch, link);
4080 if (!all && server->dispatchgen == dispatch-> dispatchgen)
4082 ISC_LIST_UNLINK(server->dispatches, dispatch, link);
4083 dns_dispatch_detach(&dispatch->dispatch);
4084 isc_mem_put(server->mctx, dispatch, sizeof(*dispatch));
4089 ns_add_reserved_dispatch(ns_server_t *server, const isc_sockaddr_t *addr) {
4090 ns_dispatch_t *dispatch;
4092 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
4093 isc_result_t result;
4094 unsigned int attrs, attrmask;
4096 REQUIRE(NS_SERVER_VALID(server));
4098 port = isc_sockaddr_getport(addr);
4099 if (port == 0 || port >= 1024)
4102 for (dispatch = ISC_LIST_HEAD(server->dispatches);
4104 dispatch = ISC_LIST_NEXT(dispatch, link)) {
4105 if (isc_sockaddr_equal(&dispatch->addr, addr))
4108 if (dispatch != NULL) {
4109 dispatch->dispatchgen = server->dispatchgen;
4113 dispatch = isc_mem_get(server->mctx, sizeof(*dispatch));
4114 if (dispatch == NULL) {
4115 result = ISC_R_NOMEMORY;
4119 dispatch->addr = *addr;
4120 dispatch->dispatchgen = server->dispatchgen;
4121 dispatch->dispatch = NULL;
4124 attrs |= DNS_DISPATCHATTR_UDP;
4125 switch (isc_sockaddr_pf(addr)) {
4127 attrs |= DNS_DISPATCHATTR_IPV4;
4130 attrs |= DNS_DISPATCHATTR_IPV6;
4133 result = ISC_R_NOTIMPLEMENTED;
4137 attrmask |= DNS_DISPATCHATTR_UDP;
4138 attrmask |= DNS_DISPATCHATTR_TCP;
4139 attrmask |= DNS_DISPATCHATTR_IPV4;
4140 attrmask |= DNS_DISPATCHATTR_IPV6;
4142 result = dns_dispatch_getudp(ns_g_dispatchmgr, ns_g_socketmgr,
4143 ns_g_taskmgr, &dispatch->addr, 4096,
4144 1000, 32768, 16411, 16433,
4145 attrs, attrmask, &dispatch->dispatch);
4146 if (result != ISC_R_SUCCESS)
4149 ISC_LIST_INITANDPREPEND(server->dispatches, dispatch, link);
4154 if (dispatch != NULL)
4155 isc_mem_put(server->mctx, dispatch, sizeof(*dispatch));
4156 isc_sockaddr_format(addr, addrbuf, sizeof(addrbuf));
4157 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4158 NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
4159 "unable to create dispatch for reserved port %s: %s",
4160 addrbuf, isc_result_totext(result));
4165 loadconfig(ns_server_t *server) {
4166 isc_result_t result;
4167 start_reserved_dispatches(server);
4168 result = load_configuration(ns_g_lwresdonly ?
4169 lwresd_g_conffile : ns_g_conffile,
4171 if (result == ISC_R_SUCCESS) {
4172 end_reserved_dispatches(server, ISC_FALSE);
4173 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4174 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4175 "reloading configuration succeeded");
4177 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4178 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
4179 "reloading configuration failed: %s",
4180 isc_result_totext(result));
4186 reload(ns_server_t *server) {
4187 isc_result_t result;
4188 CHECK(loadconfig(server));
4190 result = load_zones(server, ISC_FALSE);
4191 if (result == ISC_R_SUCCESS)
4192 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4193 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4194 "reloading zones succeeded");
4196 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4197 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
4198 "reloading zones failed: %s",
4199 isc_result_totext(result));
4206 reconfig(ns_server_t *server) {
4207 isc_result_t result;
4208 CHECK(loadconfig(server));
4210 result = load_new_zones(server, ISC_FALSE);
4211 if (result == ISC_R_SUCCESS)
4212 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4213 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4214 "any newly configured zones are now loaded");
4216 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4217 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
4218 "loading new zones failed: %s",
4219 isc_result_totext(result));
4225 * Handle a reload event (from SIGHUP).
4228 ns_server_reload(isc_task_t *task, isc_event_t *event) {
4229 ns_server_t *server = (ns_server_t *)event->ev_arg;
4231 INSIST(task = server->task);
4234 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4235 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4236 "received SIGHUP signal to reload zones");
4237 (void)reload(server);
4239 LOCK(&server->reload_event_lock);
4240 INSIST(server->reload_event == NULL);
4241 server->reload_event = event;
4242 UNLOCK(&server->reload_event_lock);
4246 ns_server_reloadwanted(ns_server_t *server) {
4247 LOCK(&server->reload_event_lock);
4248 if (server->reload_event != NULL)
4249 isc_task_send(server->task, &server->reload_event);
4250 UNLOCK(&server->reload_event_lock);
4254 next_token(char **stringp, const char *delim) {
4258 res = strsep(stringp, delim);
4261 } while (*res == '\0');
4266 * Find the zone specified in the control channel command 'args',
4267 * if any. If a zone is specified, point '*zonep' at it, otherwise
4268 * set '*zonep' to NULL.
4271 zone_from_args(ns_server_t *server, char *args, dns_zone_t **zonep) {
4273 const char *zonetxt;
4275 const char *viewtxt = NULL;
4276 dns_fixedname_t name;
4277 isc_result_t result;
4279 dns_view_t *view = NULL;
4280 dns_rdataclass_t rdclass;
4282 REQUIRE(zonep != NULL && *zonep == NULL);
4286 /* Skip the command name. */
4287 ptr = next_token(&input, " \t");
4289 return (ISC_R_UNEXPECTEDEND);
4291 /* Look for the zone name. */
4292 zonetxt = next_token(&input, " \t");
4293 if (zonetxt == NULL)
4294 return (ISC_R_SUCCESS);
4296 /* Look for the optional class name. */
4297 classtxt = next_token(&input, " \t");
4298 if (classtxt != NULL) {
4299 /* Look for the optional view name. */
4300 viewtxt = next_token(&input, " \t");
4303 isc_buffer_init(&buf, zonetxt, strlen(zonetxt));
4304 isc_buffer_add(&buf, strlen(zonetxt));
4305 dns_fixedname_init(&name);
4306 result = dns_name_fromtext(dns_fixedname_name(&name),
4307 &buf, dns_rootname, ISC_FALSE, NULL);
4308 if (result != ISC_R_SUCCESS)
4311 if (classtxt != NULL) {
4314 r.length = strlen(classtxt);
4315 result = dns_rdataclass_fromtext(&rdclass, &r);
4316 if (result != ISC_R_SUCCESS)
4319 rdclass = dns_rdataclass_in;
4321 if (viewtxt == NULL) {
4322 result = dns_viewlist_findzone(&server->viewlist,
4323 dns_fixedname_name(&name),
4324 ISC_TF(classtxt == NULL),
4327 result = dns_viewlist_find(&server->viewlist, viewtxt,
4329 if (result != ISC_R_SUCCESS)
4332 result = dns_zt_find(view->zonetable, dns_fixedname_name(&name),
4334 dns_view_detach(&view);
4337 /* Partial match? */
4338 if (result != ISC_R_SUCCESS && *zonep != NULL)
4339 dns_zone_detach(zonep);
4340 if (result == DNS_R_PARTIALMATCH)
4341 result = ISC_R_NOTFOUND;
4347 * Act on a "retransfer" command from the command channel.
4350 ns_server_retransfercommand(ns_server_t *server, char *args) {
4351 isc_result_t result;
4352 dns_zone_t *zone = NULL;
4353 dns_zonetype_t type;
4355 result = zone_from_args(server, args, &zone);
4356 if (result != ISC_R_SUCCESS)
4359 return (ISC_R_UNEXPECTEDEND);
4360 type = dns_zone_gettype(zone);
4361 if (type == dns_zone_slave || type == dns_zone_stub)
4362 dns_zone_forcereload(zone);
4364 result = ISC_R_NOTFOUND;
4365 dns_zone_detach(&zone);
4370 * Act on a "reload" command from the command channel.
4373 ns_server_reloadcommand(ns_server_t *server, char *args, isc_buffer_t *text) {
4374 isc_result_t result;
4375 dns_zone_t *zone = NULL;
4376 dns_zonetype_t type;
4377 const char *msg = NULL;
4379 result = zone_from_args(server, args, &zone);
4380 if (result != ISC_R_SUCCESS)
4383 result = reload(server);
4384 if (result == ISC_R_SUCCESS)
4385 msg = "server reload successful";
4387 type = dns_zone_gettype(zone);
4388 if (type == dns_zone_slave || type == dns_zone_stub) {
4389 dns_zone_refresh(zone);
4390 dns_zone_detach(&zone);
4391 msg = "zone refresh queued";
4393 result = dns_zone_load(zone);
4394 dns_zone_detach(&zone);
4397 msg = "zone reload successful";
4399 case DNS_R_CONTINUE:
4400 msg = "zone reload queued";
4401 result = ISC_R_SUCCESS;
4403 case DNS_R_UPTODATE:
4404 msg = "zone reload up-to-date";
4405 result = ISC_R_SUCCESS;
4408 /* failure message will be generated by rndc */
4413 if (msg != NULL && strlen(msg) < isc_buffer_availablelength(text))
4414 isc_buffer_putmem(text, (const unsigned char *)msg,
4420 * Act on a "reconfig" command from the command channel.
4423 ns_server_reconfigcommand(ns_server_t *server, char *args) {
4427 return (ISC_R_SUCCESS);
4431 * Act on a "notify" command from the command channel.
4434 ns_server_notifycommand(ns_server_t *server, char *args, isc_buffer_t *text) {
4435 isc_result_t result;
4436 dns_zone_t *zone = NULL;
4437 const unsigned char msg[] = "zone notify queued";
4439 result = zone_from_args(server, args, &zone);
4440 if (result != ISC_R_SUCCESS)
4443 return (ISC_R_UNEXPECTEDEND);
4445 dns_zone_notify(zone);
4446 dns_zone_detach(&zone);
4447 if (sizeof(msg) <= isc_buffer_availablelength(text))
4448 isc_buffer_putmem(text, msg, sizeof(msg));
4450 return (ISC_R_SUCCESS);
4454 * Act on a "refresh" command from the command channel.
4457 ns_server_refreshcommand(ns_server_t *server, char *args, isc_buffer_t *text) {
4458 isc_result_t result;
4459 dns_zone_t *zone = NULL;
4460 const unsigned char msg1[] = "zone refresh queued";
4461 const unsigned char msg2[] = "not a slave or stub zone";
4462 dns_zonetype_t type;
4464 result = zone_from_args(server, args, &zone);
4465 if (result != ISC_R_SUCCESS)
4468 return (ISC_R_UNEXPECTEDEND);
4470 type = dns_zone_gettype(zone);
4471 if (type == dns_zone_slave || type == dns_zone_stub) {
4472 dns_zone_refresh(zone);
4473 dns_zone_detach(&zone);
4474 if (sizeof(msg1) <= isc_buffer_availablelength(text))
4475 isc_buffer_putmem(text, msg1, sizeof(msg1));
4476 return (ISC_R_SUCCESS);
4479 dns_zone_detach(&zone);
4480 if (sizeof(msg2) <= isc_buffer_availablelength(text))
4481 isc_buffer_putmem(text, msg2, sizeof(msg2));
4482 return (ISC_R_FAILURE);
4486 ns_server_togglequerylog(ns_server_t *server) {
4487 server->log_queries = server->log_queries ? ISC_FALSE : ISC_TRUE;
4489 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4490 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4491 "query logging is now %s",
4492 server->log_queries ? "on" : "off");
4493 return (ISC_R_SUCCESS);
4497 ns_listenlist_fromconfig(const cfg_obj_t *listenlist, const cfg_obj_t *config,
4498 cfg_aclconfctx_t *actx,
4499 isc_mem_t *mctx, ns_listenlist_t **target)
4501 isc_result_t result;
4502 const cfg_listelt_t *element;
4503 ns_listenlist_t *dlist = NULL;
4505 REQUIRE(target != NULL && *target == NULL);
4507 result = ns_listenlist_create(mctx, &dlist);
4508 if (result != ISC_R_SUCCESS)
4511 for (element = cfg_list_first(listenlist);
4513 element = cfg_list_next(element))
4515 ns_listenelt_t *delt = NULL;
4516 const cfg_obj_t *listener = cfg_listelt_value(element);
4517 result = ns_listenelt_fromconfig(listener, config, actx,
4519 if (result != ISC_R_SUCCESS)
4521 ISC_LIST_APPEND(dlist->elts, delt, link);
4524 return (ISC_R_SUCCESS);
4527 ns_listenlist_detach(&dlist);
4532 * Create a listen list from the corresponding configuration
4536 ns_listenelt_fromconfig(const cfg_obj_t *listener, const cfg_obj_t *config,
4537 cfg_aclconfctx_t *actx,
4538 isc_mem_t *mctx, ns_listenelt_t **target)
4540 isc_result_t result;
4541 const cfg_obj_t *portobj;
4543 ns_listenelt_t *delt = NULL;
4544 REQUIRE(target != NULL && *target == NULL);
4546 portobj = cfg_tuple_get(listener, "port");
4547 if (!cfg_obj_isuint32(portobj)) {
4548 if (ns_g_port != 0) {
4551 result = ns_config_getport(config, &port);
4552 if (result != ISC_R_SUCCESS)
4556 if (cfg_obj_asuint32(portobj) >= ISC_UINT16_MAX) {
4557 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
4558 "port value '%u' is out of range",
4559 cfg_obj_asuint32(portobj));
4560 return (ISC_R_RANGE);
4562 port = (in_port_t)cfg_obj_asuint32(portobj);
4565 result = ns_listenelt_create(mctx, port, NULL, &delt);
4566 if (result != ISC_R_SUCCESS)
4569 result = cfg_acl_fromconfig(cfg_tuple_get(listener, "acl"),
4570 config, ns_g_lctx, actx, mctx, 0,
4572 if (result != ISC_R_SUCCESS) {
4573 ns_listenelt_destroy(delt);
4577 return (ISC_R_SUCCESS);
4581 ns_server_dumpstats(ns_server_t *server) {
4582 isc_result_t result;
4585 CHECKMF(isc_stdio_open(server->statsfile, "a", &fp),
4586 "could not open statistics dump file", server->statsfile);
4588 result = ns_stats_dump(server, fp);
4593 (void)isc_stdio_close(fp);
4594 if (result == ISC_R_SUCCESS)
4595 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4596 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4597 "dumpstats complete");
4599 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4600 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
4601 "dumpstats failed: %s",
4602 dns_result_totext(result));
4607 add_zone_tolist(dns_zone_t *zone, void *uap) {
4608 struct dumpcontext *dctx = uap;
4609 struct zonelistentry *zle;
4611 zle = isc_mem_get(dctx->mctx, sizeof *zle);
4613 return (ISC_R_NOMEMORY);
4615 dns_zone_attach(zone, &zle->zone);
4616 ISC_LINK_INIT(zle, link);
4617 ISC_LIST_APPEND(ISC_LIST_TAIL(dctx->viewlist)->zonelist, zle, link);
4618 return (ISC_R_SUCCESS);
4622 add_view_tolist(struct dumpcontext *dctx, dns_view_t *view) {
4623 struct viewlistentry *vle;
4624 isc_result_t result = ISC_R_SUCCESS;
4627 * Prevent duplicate views.
4629 for (vle = ISC_LIST_HEAD(dctx->viewlist);
4631 vle = ISC_LIST_NEXT(vle, link))
4632 if (vle->view == view)
4633 return (ISC_R_SUCCESS);
4635 vle = isc_mem_get(dctx->mctx, sizeof *vle);
4637 return (ISC_R_NOMEMORY);
4639 dns_view_attach(view, &vle->view);
4640 ISC_LINK_INIT(vle, link);
4641 ISC_LIST_INIT(vle->zonelist);
4642 ISC_LIST_APPEND(dctx->viewlist, vle, link);
4643 if (dctx->dumpzones)
4644 result = dns_zt_apply(view->zonetable, ISC_TRUE,
4645 add_zone_tolist, dctx);
4650 dumpcontext_destroy(struct dumpcontext *dctx) {
4651 struct viewlistentry *vle;
4652 struct zonelistentry *zle;
4654 vle = ISC_LIST_HEAD(dctx->viewlist);
4655 while (vle != NULL) {
4656 ISC_LIST_UNLINK(dctx->viewlist, vle, link);
4657 zle = ISC_LIST_HEAD(vle->zonelist);
4658 while (zle != NULL) {
4659 ISC_LIST_UNLINK(vle->zonelist, zle, link);
4660 dns_zone_detach(&zle->zone);
4661 isc_mem_put(dctx->mctx, zle, sizeof *zle);
4662 zle = ISC_LIST_HEAD(vle->zonelist);
4664 dns_view_detach(&vle->view);
4665 isc_mem_put(dctx->mctx, vle, sizeof *vle);
4666 vle = ISC_LIST_HEAD(dctx->viewlist);
4668 if (dctx->version != NULL)
4669 dns_db_closeversion(dctx->db, &dctx->version, ISC_FALSE);
4670 if (dctx->db != NULL)
4671 dns_db_detach(&dctx->db);
4672 if (dctx->cache != NULL)
4673 dns_db_detach(&dctx->cache);
4674 if (dctx->task != NULL)
4675 isc_task_detach(&dctx->task);
4676 if (dctx->fp != NULL)
4677 (void)isc_stdio_close(dctx->fp);
4678 if (dctx->mdctx != NULL)
4679 dns_dumpctx_detach(&dctx->mdctx);
4680 isc_mem_put(dctx->mctx, dctx, sizeof *dctx);
4684 dumpdone(void *arg, isc_result_t result) {
4685 struct dumpcontext *dctx = arg;
4687 const dns_master_style_t *style;
4689 if (result != ISC_R_SUCCESS)
4691 if (dctx->mdctx != NULL)
4692 dns_dumpctx_detach(&dctx->mdctx);
4693 if (dctx->view == NULL) {
4694 dctx->view = ISC_LIST_HEAD(dctx->viewlist);
4695 if (dctx->view == NULL)
4697 INSIST(dctx->zone == NULL);
4701 fprintf(dctx->fp, ";\n; Start view %s\n;\n", dctx->view->view->name);
4703 if (dctx->zone == NULL && dctx->cache == NULL && dctx->dumpcache) {
4704 style = &dns_master_style_cache;
4705 /* start cache dump */
4706 if (dctx->view->view->cachedb != NULL)
4707 dns_db_attach(dctx->view->view->cachedb, &dctx->cache);
4708 if (dctx->cache != NULL) {
4710 fprintf(dctx->fp, ";\n; Cache dump of view '%s'\n;\n",
4711 dctx->view->view->name);
4712 result = dns_master_dumptostreaminc(dctx->mctx,
4718 if (result == DNS_R_CONTINUE)
4720 if (result == ISC_R_NOTIMPLEMENTED)
4721 fprintf(dctx->fp, "; %s\n",
4722 dns_result_totext(result));
4723 else if (result != ISC_R_SUCCESS)
4727 if (dctx->cache != NULL) {
4728 dns_adb_dump(dctx->view->view->adb, dctx->fp);
4729 dns_resolver_printbadcache(dctx->view->view->resolver,
4731 dns_db_detach(&dctx->cache);
4733 if (dctx->dumpzones) {
4734 style = &dns_master_style_full;
4736 if (dctx->version != NULL)
4737 dns_db_closeversion(dctx->db, &dctx->version,
4739 if (dctx->db != NULL)
4740 dns_db_detach(&dctx->db);
4741 if (dctx->zone == NULL)
4742 dctx->zone = ISC_LIST_HEAD(dctx->view->zonelist);
4744 dctx->zone = ISC_LIST_NEXT(dctx->zone, link);
4745 if (dctx->zone != NULL) {
4746 /* start zone dump */
4747 dns_zone_name(dctx->zone->zone, buf, sizeof(buf));
4748 fprintf(dctx->fp, ";\n; Zone dump of '%s'\n;\n", buf);
4749 result = dns_zone_getdb(dctx->zone->zone, &dctx->db);
4750 if (result != ISC_R_SUCCESS) {
4751 fprintf(dctx->fp, "; %s\n",
4752 dns_result_totext(result));
4755 dns_db_currentversion(dctx->db, &dctx->version);
4756 result = dns_master_dumptostreaminc(dctx->mctx,
4763 if (result == DNS_R_CONTINUE)
4765 if (result == ISC_R_NOTIMPLEMENTED) {
4766 fprintf(dctx->fp, "; %s\n",
4767 dns_result_totext(result));
4768 result = ISC_R_SUCCESS;
4771 if (result != ISC_R_SUCCESS)
4775 if (dctx->view != NULL)
4776 dctx->view = ISC_LIST_NEXT(dctx->view, link);
4777 if (dctx->view != NULL)
4780 fprintf(dctx->fp, "; Dump complete\n");
4781 result = isc_stdio_flush(dctx->fp);
4782 if (result == ISC_R_SUCCESS)
4783 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4784 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4787 if (result != ISC_R_SUCCESS)
4788 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4789 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
4790 "dumpdb failed: %s", dns_result_totext(result));
4791 dumpcontext_destroy(dctx);
4795 ns_server_dumpdb(ns_server_t *server, char *args) {
4796 struct dumpcontext *dctx = NULL;
4798 isc_result_t result;
4802 /* Skip the command name. */
4803 ptr = next_token(&args, " \t");
4805 return (ISC_R_UNEXPECTEDEND);
4807 dctx = isc_mem_get(server->mctx, sizeof(*dctx));
4809 return (ISC_R_NOMEMORY);
4811 dctx->mctx = server->mctx;
4812 dctx->dumpcache = ISC_TRUE;
4813 dctx->dumpzones = ISC_FALSE;
4815 ISC_LIST_INIT(dctx->viewlist);
4823 dctx->version = NULL;
4824 isc_task_attach(server->task, &dctx->task);
4826 CHECKMF(isc_stdio_open(server->dumpfile, "w", &dctx->fp),
4827 "could not open dump file", server->dumpfile);
4829 sep = (args == NULL) ? "" : ": ";
4830 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4831 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4832 "dumpdb started%s%s", sep, (args != NULL) ? args : "");
4834 ptr = next_token(&args, " \t");
4835 if (ptr != NULL && strcmp(ptr, "-all") == 0) {
4836 dctx->dumpzones = ISC_TRUE;
4837 dctx->dumpcache = ISC_TRUE;
4838 ptr = next_token(&args, " \t");
4839 } else if (ptr != NULL && strcmp(ptr, "-cache") == 0) {
4840 dctx->dumpzones = ISC_FALSE;
4841 dctx->dumpcache = ISC_TRUE;
4842 ptr = next_token(&args, " \t");
4843 } else if (ptr != NULL && strcmp(ptr, "-zones") == 0) {
4844 dctx->dumpzones = ISC_TRUE;
4845 dctx->dumpcache = ISC_FALSE;
4846 ptr = next_token(&args, " \t");
4850 for (view = ISC_LIST_HEAD(server->viewlist);
4852 view = ISC_LIST_NEXT(view, link))
4854 if (ptr != NULL && strcmp(view->name, ptr) != 0)
4856 CHECK(add_view_tolist(dctx, view));
4859 ptr = next_token(&args, " \t");
4863 dumpdone(dctx, ISC_R_SUCCESS);
4864 return (ISC_R_SUCCESS);
4868 dumpcontext_destroy(dctx);
4873 ns_server_dumprecursing(ns_server_t *server) {
4875 isc_result_t result;
4877 CHECKMF(isc_stdio_open(server->recfile, "w", &fp),
4878 "could not open dump file", server->recfile);
4879 fprintf(fp,";\n; Recursing Queries\n;\n");
4880 ns_interfacemgr_dumprecursing(fp, server->interfacemgr);
4881 fprintf(fp, "; Dump complete\n");
4885 result = isc_stdio_close(fp);
4886 if (result == ISC_R_SUCCESS)
4887 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4888 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4889 "dumprecursing complete");
4891 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4892 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
4893 "dumprecursing failed: %s",
4894 dns_result_totext(result));
4899 ns_server_setdebuglevel(ns_server_t *server, char *args) {
4907 /* Skip the command name. */
4908 ptr = next_token(&args, " \t");
4910 return (ISC_R_UNEXPECTEDEND);
4912 /* Look for the new level name. */
4913 levelstr = next_token(&args, " \t");
4914 if (levelstr == NULL) {
4915 if (ns_g_debuglevel < 99)
4918 newlevel = strtol(levelstr, &endp, 10);
4919 if (*endp != '\0' || newlevel < 0 || newlevel > 99)
4920 return (ISC_R_RANGE);
4921 ns_g_debuglevel = (unsigned int)newlevel;
4923 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
4924 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4925 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
4926 "debug level is now %d", ns_g_debuglevel);
4927 return (ISC_R_SUCCESS);
4931 ns_server_validation(ns_server_t *server, char *args) {
4932 char *ptr, *viewname;
4934 isc_boolean_t changed = ISC_FALSE;
4935 isc_result_t result;
4936 isc_boolean_t enable;
4938 /* Skip the command name. */
4939 ptr = next_token(&args, " \t");
4941 return (ISC_R_UNEXPECTEDEND);
4943 /* Find out what we are to do. */
4944 ptr = next_token(&args, " \t");
4946 return (ISC_R_UNEXPECTEDEND);
4948 if (!strcasecmp(ptr, "on") || !strcasecmp(ptr, "yes") ||
4949 !strcasecmp(ptr, "enable") || !strcasecmp(ptr, "true"))
4951 else if (!strcasecmp(ptr, "off") || !strcasecmp(ptr, "no") ||
4952 !strcasecmp(ptr, "disable") || !strcasecmp(ptr, "false"))
4955 return (DNS_R_SYNTAX);
4957 /* Look for the view name. */
4958 viewname = next_token(&args, " \t");
4960 result = isc_task_beginexclusive(server->task);
4961 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4962 for (view = ISC_LIST_HEAD(server->viewlist);
4964 view = ISC_LIST_NEXT(view, link))
4966 if (viewname != NULL && strcasecmp(viewname, view->name) != 0)
4968 result = dns_view_flushcache(view);
4969 if (result != ISC_R_SUCCESS)
4971 view->enablevalidation = enable;
4975 result = ISC_R_SUCCESS;
4977 result = ISC_R_FAILURE;
4979 isc_task_endexclusive(server->task);
4984 ns_server_flushcache(ns_server_t *server, char *args) {
4985 char *ptr, *viewname;
4987 isc_boolean_t flushed;
4988 isc_boolean_t found;
4989 isc_result_t result;
4991 /* Skip the command name. */
4992 ptr = next_token(&args, " \t");
4994 return (ISC_R_UNEXPECTEDEND);
4996 /* Look for the view name. */
4997 viewname = next_token(&args, " \t");
4999 result = isc_task_beginexclusive(server->task);
5000 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5003 for (view = ISC_LIST_HEAD(server->viewlist);
5005 view = ISC_LIST_NEXT(view, link))
5007 if (viewname != NULL && strcasecmp(viewname, view->name) != 0)
5010 result = dns_view_flushcache(view);
5011 if (result != ISC_R_SUCCESS) {
5012 flushed = ISC_FALSE;
5013 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5014 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5015 "flushing cache in view '%s' failed: %s",
5016 view->name, isc_result_totext(result));
5019 if (flushed && found) {
5020 if (viewname != NULL)
5021 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5022 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5023 "flushing cache in view '%s' succeeded",
5026 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5027 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5028 "flushing caches in all views succeeded");
5029 result = ISC_R_SUCCESS;
5032 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5033 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5034 "flushing cache in view '%s' failed: "
5035 "view not found", viewname);
5036 result = ISC_R_NOTFOUND;
5038 result = ISC_R_FAILURE;
5040 isc_task_endexclusive(server->task);
5045 ns_server_flushname(ns_server_t *server, char *args) {
5046 char *ptr, *target, *viewname;
5048 isc_boolean_t flushed;
5049 isc_boolean_t found;
5050 isc_result_t result;
5052 dns_fixedname_t fixed;
5055 /* Skip the command name. */
5056 ptr = next_token(&args, " \t");
5058 return (ISC_R_UNEXPECTEDEND);
5060 /* Find the domain name to flush. */
5061 target = next_token(&args, " \t");
5063 return (ISC_R_UNEXPECTEDEND);
5065 isc_buffer_init(&b, target, strlen(target));
5066 isc_buffer_add(&b, strlen(target));
5067 dns_fixedname_init(&fixed);
5068 name = dns_fixedname_name(&fixed);
5069 result = dns_name_fromtext(name, &b, dns_rootname, ISC_FALSE, NULL);
5070 if (result != ISC_R_SUCCESS)
5073 /* Look for the view name. */
5074 viewname = next_token(&args, " \t");
5076 result = isc_task_beginexclusive(server->task);
5077 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5080 for (view = ISC_LIST_HEAD(server->viewlist);
5082 view = ISC_LIST_NEXT(view, link))
5084 if (viewname != NULL && strcasecmp(viewname, view->name) != 0)
5087 result = dns_view_flushname(view, name);
5088 if (result != ISC_R_SUCCESS) {
5089 flushed = ISC_FALSE;
5090 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5091 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5092 "flushing name '%s' in cache view '%s' "
5093 "failed: %s", target, view->name,
5094 isc_result_totext(result));
5097 if (flushed && found) {
5098 if (viewname != NULL)
5099 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5100 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5101 "flushing name '%s' in cache view '%s' "
5102 "succeeded", target, viewname);
5104 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5105 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5106 "flushing name '%s' in all cache views "
5107 "succeeded", target);
5108 result = ISC_R_SUCCESS;
5111 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5112 NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
5113 "flushing name '%s' in cache view '%s' "
5114 "failed: view not found", target,
5116 result = ISC_R_FAILURE;
5118 isc_task_endexclusive(server->task);
5123 ns_server_status(ns_server_t *server, isc_buffer_t *text) {
5124 int zonecount, xferrunning, xferdeferred, soaqueries;
5126 const char *ob = "", *cb = "", *alt = "";
5128 if (ns_g_server->version_set) {
5131 if (ns_g_server->version == NULL)
5132 alt = "version.bind/txt/ch disabled";
5134 alt = ns_g_server->version;
5136 zonecount = dns_zonemgr_getcount(server->zonemgr, DNS_ZONESTATE_ANY);
5137 xferrunning = dns_zonemgr_getcount(server->zonemgr,
5138 DNS_ZONESTATE_XFERRUNNING);
5139 xferdeferred = dns_zonemgr_getcount(server->zonemgr,
5140 DNS_ZONESTATE_XFERDEFERRED);
5141 soaqueries = dns_zonemgr_getcount(server->zonemgr,
5142 DNS_ZONESTATE_SOAQUERY);
5144 n = snprintf((char *)isc_buffer_used(text),
5145 isc_buffer_availablelength(text),
5146 "version: %s%s%s%s\n"
5147 #ifdef ISC_PLATFORM_USETHREADS
5149 "worker threads: %u\n"
5151 "number of zones: %u\n"
5153 "xfers running: %u\n"
5154 "xfers deferred: %u\n"
5155 "soa queries in progress: %u\n"
5156 "query logging is %s\n"
5157 "recursive clients: %d/%d/%d\n"
5158 "tcp clients: %d/%d\n"
5159 "server is up and running",
5160 ns_g_version, ob, alt, cb,
5161 #ifdef ISC_PLATFORM_USETHREADS
5162 ns_g_cpus_detected, ns_g_cpus,
5164 zonecount, ns_g_debuglevel, xferrunning, xferdeferred,
5165 soaqueries, server->log_queries ? "ON" : "OFF",
5166 server->recursionquota.used, server->recursionquota.soft,
5167 server->recursionquota.max,
5168 server->tcpquota.used, server->tcpquota.max);
5169 if (n >= isc_buffer_availablelength(text))
5170 return (ISC_R_NOSPACE);
5171 isc_buffer_add(text, n);
5172 return (ISC_R_SUCCESS);
5176 delete_keynames(dns_tsig_keyring_t *ring, char *target,
5177 unsigned int *foundkeys)
5179 char namestr[DNS_NAME_FORMATSIZE];
5180 isc_result_t result;
5181 dns_rbtnodechain_t chain;
5182 dns_name_t foundname;
5183 dns_fixedname_t fixedorigin;
5185 dns_rbtnode_t *node;
5186 dns_tsigkey_t *tkey;
5188 dns_name_init(&foundname, NULL);
5189 dns_fixedname_init(&fixedorigin);
5190 origin = dns_fixedname_name(&fixedorigin);
5193 dns_rbtnodechain_init(&chain, ring->mctx);
5194 result = dns_rbtnodechain_first(&chain, ring->keys, &foundname,
5196 if (result == ISC_R_NOTFOUND) {
5197 dns_rbtnodechain_invalidate(&chain);
5198 return (ISC_R_SUCCESS);
5200 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
5201 dns_rbtnodechain_invalidate(&chain);
5207 dns_rbtnodechain_current(&chain, &foundname, origin, &node);
5211 if (!tkey->generated)
5214 dns_name_format(&tkey->name, namestr, sizeof(namestr));
5215 if (strcmp(namestr, target) == 0) {
5217 dns_rbtnodechain_invalidate(&chain);
5218 (void)dns_rbt_deletename(ring->keys,
5226 result = dns_rbtnodechain_next(&chain, &foundname, origin);
5227 if (result == ISC_R_NOMORE)
5229 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
5230 dns_rbtnodechain_invalidate(&chain);
5235 return (ISC_R_SUCCESS);
5239 ns_server_tsigdelete(ns_server_t *server, char *command, isc_buffer_t *text) {
5240 isc_result_t result;
5243 unsigned int foundkeys = 0;
5247 (void)next_token(&command, " \t"); /* skip command name */
5248 target = next_token(&command, " \t");
5250 return (ISC_R_UNEXPECTEDEND);
5251 viewname = next_token(&command, " \t");
5253 result = isc_task_beginexclusive(server->task);
5254 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5255 for (view = ISC_LIST_HEAD(server->viewlist);
5257 view = ISC_LIST_NEXT(view, link)) {
5258 if (viewname == NULL || strcmp(view->name, viewname) == 0) {
5259 RWLOCK(&view->dynamickeys->lock, isc_rwlocktype_write);
5260 result = delete_keynames(view->dynamickeys, target,
5262 RWUNLOCK(&view->dynamickeys->lock,
5263 isc_rwlocktype_write);
5264 if (result != ISC_R_SUCCESS) {
5265 isc_task_endexclusive(server->task);
5270 isc_task_endexclusive(server->task);
5272 n = snprintf((char *)isc_buffer_used(text),
5273 isc_buffer_availablelength(text),
5274 "%d tsig keys deleted.\n", foundkeys);
5275 if (n >= isc_buffer_availablelength(text)) {
5276 isc_task_endexclusive(server->task);
5277 return (ISC_R_NOSPACE);
5279 isc_buffer_add(text, n);
5281 return (ISC_R_SUCCESS);
5285 list_keynames(dns_view_t *view, dns_tsig_keyring_t *ring, isc_buffer_t *text,
5286 unsigned int *foundkeys)
5288 char namestr[DNS_NAME_FORMATSIZE];
5289 char creatorstr[DNS_NAME_FORMATSIZE];
5290 isc_result_t result;
5291 dns_rbtnodechain_t chain;
5292 dns_name_t foundname;
5293 dns_fixedname_t fixedorigin;
5295 dns_rbtnode_t *node;
5296 dns_tsigkey_t *tkey;
5298 const char *viewname;
5301 viewname = view->name;
5303 viewname = "(global)";
5305 dns_name_init(&foundname, NULL);
5306 dns_fixedname_init(&fixedorigin);
5307 origin = dns_fixedname_name(&fixedorigin);
5308 dns_rbtnodechain_init(&chain, ring->mctx);
5309 result = dns_rbtnodechain_first(&chain, ring->keys, &foundname,
5311 if (result == ISC_R_NOTFOUND) {
5312 dns_rbtnodechain_invalidate(&chain);
5313 return (ISC_R_SUCCESS);
5315 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
5316 dns_rbtnodechain_invalidate(&chain);
5322 dns_rbtnodechain_current(&chain, &foundname, origin, &node);
5327 dns_name_format(&tkey->name, namestr, sizeof(namestr));
5328 if (tkey->generated) {
5329 dns_name_format(tkey->creator, creatorstr,
5330 sizeof(creatorstr));
5331 n = snprintf((char *)isc_buffer_used(text),
5332 isc_buffer_availablelength(text),
5333 "view \"%s\"; type \"dynamic\"; key \"%s\"; creator \"%s\";\n",
5334 viewname, namestr, creatorstr);
5336 n = snprintf((char *)isc_buffer_used(text),
5337 isc_buffer_availablelength(text),
5338 "view \"%s\"; type \"static\"; key \"%s\";\n",
5341 if (n >= isc_buffer_availablelength(text)) {
5342 dns_rbtnodechain_invalidate(&chain);
5343 return (ISC_R_NOSPACE);
5345 isc_buffer_add(text, n);
5347 result = dns_rbtnodechain_next(&chain, &foundname, origin);
5348 if (result == ISC_R_NOMORE)
5350 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
5351 dns_rbtnodechain_invalidate(&chain);
5356 return (ISC_R_SUCCESS);
5360 ns_server_tsiglist(ns_server_t *server, isc_buffer_t *text) {
5361 isc_result_t result;
5364 unsigned int foundkeys = 0;
5366 result = isc_task_beginexclusive(server->task);
5367 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5368 for (view = ISC_LIST_HEAD(server->viewlist);
5370 view = ISC_LIST_NEXT(view, link)) {
5371 RWLOCK(&view->statickeys->lock, isc_rwlocktype_read);
5372 result = list_keynames(view, view->statickeys, text,
5374 RWUNLOCK(&view->statickeys->lock, isc_rwlocktype_read);
5375 if (result != ISC_R_SUCCESS) {
5376 isc_task_endexclusive(server->task);
5379 RWLOCK(&view->dynamickeys->lock, isc_rwlocktype_read);
5380 result = list_keynames(view, view->dynamickeys, text,
5382 RWUNLOCK(&view->dynamickeys->lock, isc_rwlocktype_read);
5383 if (result != ISC_R_SUCCESS) {
5384 isc_task_endexclusive(server->task);
5388 isc_task_endexclusive(server->task);
5390 if (foundkeys == 0) {
5391 n = snprintf((char *)isc_buffer_used(text),
5392 isc_buffer_availablelength(text),
5393 "no tsig keys found.\n");
5394 if (n >= isc_buffer_availablelength(text)) {
5395 isc_task_endexclusive(server->task);
5396 return (ISC_R_NOSPACE);
5398 isc_buffer_add(text, n);
5401 return (ISC_R_SUCCESS);
5405 * Act on a "freeze" or "thaw" command from the command channel.
5408 ns_server_freeze(ns_server_t *server, isc_boolean_t freeze, char *args,
5411 isc_result_t result, tresult;
5412 dns_zone_t *zone = NULL;
5413 dns_zonetype_t type;
5414 char classstr[DNS_RDATACLASS_FORMATSIZE];
5415 char zonename[DNS_NAME_FORMATSIZE];
5418 const char *vname, *sep;
5419 isc_boolean_t frozen;
5420 const char *msg = NULL;
5422 result = zone_from_args(server, args, &zone);
5423 if (result != ISC_R_SUCCESS)
5426 result = isc_task_beginexclusive(server->task);
5427 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5428 tresult = ISC_R_SUCCESS;
5429 for (view = ISC_LIST_HEAD(server->viewlist);
5431 view = ISC_LIST_NEXT(view, link)) {
5432 result = dns_view_freezezones(view, freeze);
5433 if (result != ISC_R_SUCCESS &&
5434 tresult == ISC_R_SUCCESS)
5437 isc_task_endexclusive(server->task);
5438 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5439 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5441 freeze ? "freezing" : "thawing",
5442 isc_result_totext(tresult));
5445 type = dns_zone_gettype(zone);
5446 if (type != dns_zone_master) {
5447 dns_zone_detach(&zone);
5448 return (ISC_R_NOTFOUND);
5451 result = isc_task_beginexclusive(server->task);
5452 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5453 frozen = dns_zone_getupdatedisabled(zone);
5456 msg = "WARNING: The zone was already frozen.\n"
5457 "Someone else may be editing it or "
5458 "it may still be re-loading.";
5459 result = DNS_R_FROZEN;
5461 if (result == ISC_R_SUCCESS) {
5462 result = dns_zone_flush(zone);
5463 if (result != ISC_R_SUCCESS)
5464 msg = "Flushing the zone updates to "
5467 if (result == ISC_R_SUCCESS) {
5468 journal = dns_zone_getjournal(zone);
5469 if (journal != NULL)
5470 (void)isc_file_remove(journal);
5472 if (result == ISC_R_SUCCESS)
5473 dns_zone_setupdatedisabled(zone, freeze);
5476 result = dns_zone_loadandthaw(zone);
5479 case DNS_R_UPTODATE:
5480 msg = "The zone reload and thaw was "
5482 result = ISC_R_SUCCESS;
5484 case DNS_R_CONTINUE:
5485 msg = "A zone reload and thaw was started.\n"
5486 "Check the logs to see the result.";
5487 result = ISC_R_SUCCESS;
5492 isc_task_endexclusive(server->task);
5494 if (msg != NULL && strlen(msg) < isc_buffer_availablelength(text))
5495 isc_buffer_putmem(text, (const unsigned char *)msg,
5498 view = dns_zone_getview(zone);
5499 if (strcmp(view->name, "_bind") == 0 ||
5500 strcmp(view->name, "_default") == 0)
5508 dns_rdataclass_format(dns_zone_getclass(zone), classstr,
5510 dns_name_format(dns_zone_getorigin(zone),
5511 zonename, sizeof(zonename));
5512 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5513 NS_LOGMODULE_SERVER, ISC_LOG_INFO,
5514 "%s zone '%s/%s'%s%s: %s",
5515 freeze ? "freezing" : "thawing",
5516 zonename, classstr, sep, vname,
5517 isc_result_totext(result));
5518 dns_zone_detach(&zone);
5524 * This function adds a message for rndc to echo if named
5525 * is managed by smf and is also running chroot.
5528 ns_smf_add_message(isc_buffer_t *text) {
5531 n = snprintf((char *)isc_buffer_used(text),
5532 isc_buffer_availablelength(text),
5533 "use svcadm(1M) to manage named");
5534 if (n >= isc_buffer_availablelength(text))
5535 return (ISC_R_NOSPACE);
5536 isc_buffer_add(text, n);
5537 return (ISC_R_SUCCESS);
5539 #endif /* HAVE_LIBSCF */