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.495.10.29.2.2 2010/02/25 10:56:00 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
"d: %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
) {
3245 * Not specified, use default.
3247 CHECK(ns_listenlist_default(ns_g_mctx
, listen_port
,
3248 ISC_FALSE
, &listenon
));
3250 if (listenon
!= NULL
) {
3251 ns_interfacemgr_setlistenon6(server
->interfacemgr
,
3253 ns_listenlist_detach(&listenon
);
3258 * Rescan the interface list to pick up changes in the
3259 * listen-on option. It's important that we do this before we try
3260 * to configure the query source, since the dispatcher we use might
3261 * be shared with an interface.
3263 scan_interfaces(server
, ISC_TRUE
);
3266 * Arrange for further interface scanning to occur periodically
3267 * as specified by the "interface-interval" option.
3270 result
= ns_config_get(maps
, "interface-interval", &obj
);
3271 INSIST(result
== ISC_R_SUCCESS
);
3272 interface_interval
= cfg_obj_asuint32(obj
) * 60;
3273 if (interface_interval
== 0) {
3274 CHECK(isc_timer_reset(server
->interface_timer
,
3275 isc_timertype_inactive
,
3276 NULL
, NULL
, ISC_TRUE
));
3277 } else if (server
->interface_interval
!= interface_interval
) {
3278 isc_interval_set(&interval
, interface_interval
, 0);
3279 CHECK(isc_timer_reset(server
->interface_timer
,
3280 isc_timertype_ticker
,
3281 NULL
, &interval
, ISC_FALSE
));
3283 server
->interface_interval
= interface_interval
;
3286 * Configure the dialup heartbeat timer.
3289 result
= ns_config_get(maps
, "heartbeat-interval", &obj
);
3290 INSIST(result
== ISC_R_SUCCESS
);
3291 heartbeat_interval
= cfg_obj_asuint32(obj
) * 60;
3292 if (heartbeat_interval
== 0) {
3293 CHECK(isc_timer_reset(server
->heartbeat_timer
,
3294 isc_timertype_inactive
,
3295 NULL
, NULL
, ISC_TRUE
));
3296 } else if (server
->heartbeat_interval
!= heartbeat_interval
) {
3297 isc_interval_set(&interval
, heartbeat_interval
, 0);
3298 CHECK(isc_timer_reset(server
->heartbeat_timer
,
3299 isc_timertype_ticker
,
3300 NULL
, &interval
, ISC_FALSE
));
3302 server
->heartbeat_interval
= heartbeat_interval
;
3304 isc_interval_set(&interval
, 1200, 0);
3305 CHECK(isc_timer_reset(server
->pps_timer
, isc_timertype_ticker
, NULL
,
3306 &interval
, ISC_FALSE
));
3309 * Configure and freeze all explicit views. Explicit
3310 * views that have zones were already created at parsing
3311 * time, but views with no zones must be created here.
3314 (void)cfg_map_get(config
, "view", &views
);
3315 for (element
= cfg_list_first(views
);
3317 element
= cfg_list_next(element
))
3319 const cfg_obj_t
*vconfig
= cfg_listelt_value(element
);
3322 CHECK(create_view(vconfig
, &viewlist
, &view
));
3323 INSIST(view
!= NULL
);
3324 CHECK(configure_view(view
, config
, vconfig
,
3325 ns_g_mctx
, &aclconfctx
, ISC_TRUE
));
3326 dns_view_freeze(view
);
3327 dns_view_detach(&view
);
3331 * Make sure we have a default view if and only if there
3332 * were no explicit views.
3334 if (views
== NULL
) {
3336 * No explicit views; there ought to be a default view.
3337 * There may already be one created as a side effect
3338 * of zone statements, or we may have to create one.
3339 * In either case, we need to configure and freeze it.
3341 CHECK(create_view(NULL
, &viewlist
, &view
));
3342 CHECK(configure_view(view
, config
, NULL
, ns_g_mctx
,
3343 &aclconfctx
, ISC_TRUE
));
3344 dns_view_freeze(view
);
3345 dns_view_detach(&view
);
3349 * Create (or recreate) the built-in views. Currently
3350 * there is only one, the _bind view.
3352 builtin_views
= NULL
;
3353 RUNTIME_CHECK(cfg_map_get(ns_g_config
, "view",
3354 &builtin_views
) == ISC_R_SUCCESS
);
3355 for (element
= cfg_list_first(builtin_views
);
3357 element
= cfg_list_next(element
))
3359 const cfg_obj_t
*vconfig
= cfg_listelt_value(element
);
3360 CHECK(create_view(vconfig
, &viewlist
, &view
));
3361 CHECK(configure_view(view
, config
, vconfig
, ns_g_mctx
,
3362 &aclconfctx
, ISC_FALSE
));
3363 dns_view_freeze(view
);
3364 dns_view_detach(&view
);
3369 * Swap our new view list with the production one.
3371 tmpviewlist
= server
->viewlist
;
3372 server
->viewlist
= viewlist
;
3373 viewlist
= tmpviewlist
;
3376 * Load the TKEY information from the configuration.
3378 if (options
!= NULL
) {
3379 dns_tkeyctx_t
*t
= NULL
;
3380 CHECKM(ns_tkeyctx_fromconfig(options
, ns_g_mctx
, ns_g_entropy
,
3382 "configuring TKEY");
3383 if (server
->tkeyctx
!= NULL
)
3384 dns_tkeyctx_destroy(&server
->tkeyctx
);
3385 server
->tkeyctx
= t
;
3389 * Bind the control port(s).
3391 CHECKM(ns_controls_configure(ns_g_server
->controls
, config
,
3393 "binding control channel(s)");
3396 * Bind the lwresd port(s).
3398 CHECKM(ns_lwresd_configure(ns_g_mctx
, config
),
3399 "binding lightweight resolver ports");
3402 * Open the source of entropy.
3406 result
= ns_config_get(maps
, "random-device", &obj
);
3407 if (result
!= ISC_R_SUCCESS
) {
3408 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
3409 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
3410 "no source of entropy found");
3412 const char *randomdev
= cfg_obj_asstring(obj
);
3413 result
= isc_entropy_createfilesource(ns_g_entropy
,
3415 if (result
!= ISC_R_SUCCESS
)
3416 isc_log_write(ns_g_lctx
,
3417 NS_LOGCATEGORY_GENERAL
,
3418 NS_LOGMODULE_SERVER
,
3420 "could not open entropy source "
3423 isc_result_totext(result
));
3424 #ifdef PATH_RANDOMDEV
3425 if (ns_g_fallbackentropy
!= NULL
) {
3426 if (result
!= ISC_R_SUCCESS
) {
3427 isc_log_write(ns_g_lctx
,
3428 NS_LOGCATEGORY_GENERAL
,
3429 NS_LOGMODULE_SERVER
,
3431 "using pre-chroot entropy source "
3434 isc_entropy_detach(&ns_g_entropy
);
3435 isc_entropy_attach(ns_g_fallbackentropy
,
3438 isc_entropy_detach(&ns_g_fallbackentropy
);
3445 * Relinquish root privileges.
3451 * Check that the working directory is writable.
3453 if (access(".", W_OK
) != 0) {
3454 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
3455 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
3456 "the working directory is not writable");
3460 * Configure the logging system.
3462 * Do this after changing UID to make sure that any log
3463 * files specified in named.conf get created by the
3464 * unprivileged user, not root.
3466 if (ns_g_logstderr
) {
3467 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
3468 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
3469 "ignoring config file logging "
3470 "statement due to -g option");
3472 const cfg_obj_t
*logobj
= NULL
;
3473 isc_logconfig_t
*logc
= NULL
;
3475 CHECKM(isc_logconfig_create(ns_g_lctx
, &logc
),
3476 "creating new logging configuration");
3479 (void)cfg_map_get(config
, "logging", &logobj
);
3480 if (logobj
!= NULL
) {
3481 CHECKM(ns_log_configure(logc
, logobj
),
3482 "configuring logging");
3484 CHECKM(ns_log_setdefaultchannels(logc
),
3485 "setting up default logging channels");
3486 CHECKM(ns_log_setunmatchedcategory(logc
),
3487 "setting up default 'category unmatched'");
3488 CHECKM(ns_log_setdefaultcategory(logc
),
3489 "setting up default 'category default'");
3492 result
= isc_logconfig_use(ns_g_lctx
, logc
);
3493 if (result
!= ISC_R_SUCCESS
) {
3494 isc_logconfig_destroy(&logc
);
3495 CHECKM(result
, "installing logging configuration");
3498 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
3499 NS_LOGMODULE_SERVER
, ISC_LOG_DEBUG(1),
3500 "now using logging configuration from "
3505 * Set the default value of the query logging flag depending
3506 * whether a "queries" category has been defined. This is
3507 * a disgusting hack, but we need to do this for BIND 8
3511 const cfg_obj_t
*logobj
= NULL
;
3512 const cfg_obj_t
*categories
= NULL
;
3515 if (ns_config_get(maps
, "querylog", &obj
) == ISC_R_SUCCESS
) {
3516 server
->log_queries
= cfg_obj_asboolean(obj
);
3519 (void)cfg_map_get(config
, "logging", &logobj
);
3521 (void)cfg_map_get(logobj
, "category",
3523 if (categories
!= NULL
) {
3524 const cfg_listelt_t
*element
;
3525 for (element
= cfg_list_first(categories
);
3527 element
= cfg_list_next(element
))
3529 const cfg_obj_t
*catobj
;
3532 obj
= cfg_listelt_value(element
);
3533 catobj
= cfg_tuple_get(obj
, "name");
3534 str
= cfg_obj_asstring(catobj
);
3535 if (strcasecmp(str
, "queries") == 0)
3536 server
->log_queries
= ISC_TRUE
;
3543 if (ns_config_get(maps
, "pid-file", &obj
) == ISC_R_SUCCESS
)
3544 if (cfg_obj_isvoid(obj
))
3545 ns_os_writepidfile(NULL
, first_time
);
3547 ns_os_writepidfile(cfg_obj_asstring(obj
), first_time
);
3548 else if (ns_g_lwresdonly
)
3549 ns_os_writepidfile(lwresd_g_defaultpidfile
, first_time
);
3551 ns_os_writepidfile(ns_g_defaultpidfile
, first_time
);
3554 if (options
!= NULL
&&
3555 cfg_map_get(options
, "memstatistics", &obj
) == ISC_R_SUCCESS
)
3556 ns_g_memstatistics
= cfg_obj_asboolean(obj
);
3558 ns_g_memstatistics
=
3559 ISC_TF((isc_mem_debugging
& ISC_MEM_DEBUGRECORD
) != 0);
3562 if (ns_config_get(maps
, "memstatistics-file", &obj
) == ISC_R_SUCCESS
)
3563 ns_main_setmemstats(cfg_obj_asstring(obj
));
3564 else if (ns_g_memstatistics
)
3565 ns_main_setmemstats("named.memstats");
3567 ns_main_setmemstats(NULL
);
3570 result
= ns_config_get(maps
, "statistics-file", &obj
);
3571 INSIST(result
== ISC_R_SUCCESS
);
3572 CHECKM(setstring(server
, &server
->statsfile
, cfg_obj_asstring(obj
)),
3576 result
= ns_config_get(maps
, "dump-file", &obj
);
3577 INSIST(result
== ISC_R_SUCCESS
);
3578 CHECKM(setstring(server
, &server
->dumpfile
, cfg_obj_asstring(obj
)),
3582 result
= ns_config_get(maps
, "recursing-file", &obj
);
3583 INSIST(result
== ISC_R_SUCCESS
);
3584 CHECKM(setstring(server
, &server
->recfile
, cfg_obj_asstring(obj
)),
3588 result
= ns_config_get(maps
, "version", &obj
);
3589 if (result
== ISC_R_SUCCESS
) {
3590 CHECKM(setoptstring(server
, &server
->version
, obj
), "strdup");
3591 server
->version_set
= ISC_TRUE
;
3593 server
->version_set
= ISC_FALSE
;
3597 result
= ns_config_get(maps
, "hostname", &obj
);
3598 if (result
== ISC_R_SUCCESS
) {
3599 CHECKM(setoptstring(server
, &server
->hostname
, obj
), "strdup");
3600 server
->hostname_set
= ISC_TRUE
;
3602 server
->hostname_set
= ISC_FALSE
;
3606 result
= ns_config_get(maps
, "server-id", &obj
);
3607 server
->server_usehostname
= ISC_FALSE
;
3608 if (result
== ISC_R_SUCCESS
&& cfg_obj_isboolean(obj
)) {
3609 /* The parser translates "hostname" to ISC_TRUE */
3610 server
->server_usehostname
= cfg_obj_asboolean(obj
);
3611 result
= setstring(server
, &server
->server_id
, NULL
);
3612 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3613 } else if (result
== ISC_R_SUCCESS
) {
3614 /* Found a quoted string */
3615 CHECKM(setoptstring(server
, &server
->server_id
, obj
), "strdup");
3617 result
= setstring(server
, &server
->server_id
, NULL
);
3618 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3622 result
= ns_config_get(maps
, "flush-zones-on-shutdown", &obj
);
3623 if (result
== ISC_R_SUCCESS
) {
3624 server
->flushonshutdown
= cfg_obj_asboolean(obj
);
3626 server
->flushonshutdown
= ISC_FALSE
;
3629 result
= ISC_R_SUCCESS
;
3632 if (v4portset
!= NULL
)
3633 isc_portset_destroy(ns_g_mctx
, &v4portset
);
3635 if (v6portset
!= NULL
)
3636 isc_portset_destroy(ns_g_mctx
, &v6portset
);
3638 cfg_aclconfctx_destroy(&aclconfctx
);
3640 if (parser
!= NULL
) {
3642 cfg_obj_destroy(parser
, &config
);
3643 cfg_parser_destroy(&parser
);
3647 dns_view_detach(&view
);
3650 * This cleans up either the old production view list
3651 * or our temporary list depending on whether they
3652 * were swapped above or not.
3654 for (view
= ISC_LIST_HEAD(viewlist
);
3657 view_next
= ISC_LIST_NEXT(view
, link
);
3658 ISC_LIST_UNLINK(viewlist
, view
, link
);
3659 if (result
== ISC_R_SUCCESS
&&
3660 strcmp(view
->name
, "_bind") != 0)
3661 (void)dns_zt_apply(view
->zonetable
, ISC_FALSE
,
3663 dns_view_detach(&view
);
3667 * Adjust the listening interfaces in accordance with the source
3668 * addresses specified in views and zones.
3670 if (isc_net_probeipv6() == ISC_R_SUCCESS
)
3671 adjust_interfaces(server
, ns_g_mctx
);
3673 /* Relinquish exclusive access to configuration data. */
3674 isc_task_endexclusive(server
->task
);
3676 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
, NS_LOGMODULE_SERVER
,
3677 ISC_LOG_DEBUG(1), "load_configuration: %s",
3678 isc_result_totext(result
));
3684 load_zones(ns_server_t
*server
, isc_boolean_t stop
) {
3685 isc_result_t result
;
3688 result
= isc_task_beginexclusive(server
->task
);
3689 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3692 * Load zone data from disk.
3694 for (view
= ISC_LIST_HEAD(server
->viewlist
);
3696 view
= ISC_LIST_NEXT(view
, link
))
3698 CHECK(dns_view_load(view
, stop
));
3702 * Force zone maintenance. Do this after loading
3703 * so that we know when we need to force AXFR of
3704 * slave zones whose master files are missing.
3706 CHECK(dns_zonemgr_forcemaint(server
->zonemgr
));
3708 isc_task_endexclusive(server
->task
);
3713 load_new_zones(ns_server_t
*server
, isc_boolean_t stop
) {
3714 isc_result_t result
;
3717 result
= isc_task_beginexclusive(server
->task
);
3718 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3721 * Load zone data from disk.
3723 for (view
= ISC_LIST_HEAD(server
->viewlist
);
3725 view
= ISC_LIST_NEXT(view
, link
))
3727 CHECK(dns_view_loadnew(view
, stop
));
3730 * Force zone maintenance. Do this after loading
3731 * so that we know when we need to force AXFR of
3732 * slave zones whose master files are missing.
3734 dns_zonemgr_resumexfrs(server
->zonemgr
);
3736 isc_task_endexclusive(server
->task
);
3741 run_server(isc_task_t
*task
, isc_event_t
*event
) {
3742 isc_result_t result
;
3743 ns_server_t
*server
= (ns_server_t
*)event
->ev_arg
;
3745 INSIST(task
== server
->task
);
3747 isc_event_free(&event
);
3749 CHECKFATAL(dns_dispatchmgr_create(ns_g_mctx
, ns_g_entropy
,
3751 "creating dispatch manager");
3753 dns_dispatchmgr_setstats(ns_g_dispatchmgr
, server
->resolverstats
);
3755 CHECKFATAL(ns_interfacemgr_create(ns_g_mctx
, ns_g_taskmgr
,
3756 ns_g_socketmgr
, ns_g_dispatchmgr
,
3757 &server
->interfacemgr
),
3758 "creating interface manager");
3760 CHECKFATAL(isc_timer_create(ns_g_timermgr
, isc_timertype_inactive
,
3761 NULL
, NULL
, server
->task
,
3762 interface_timer_tick
,
3763 server
, &server
->interface_timer
),
3764 "creating interface timer");
3766 CHECKFATAL(isc_timer_create(ns_g_timermgr
, isc_timertype_inactive
,
3767 NULL
, NULL
, server
->task
,
3768 heartbeat_timer_tick
,
3769 server
, &server
->heartbeat_timer
),
3770 "creating heartbeat timer");
3772 CHECKFATAL(isc_timer_create(ns_g_timermgr
, isc_timertype_inactive
,
3773 NULL
, NULL
, server
->task
, pps_timer_tick
,
3774 server
, &server
->pps_timer
),
3775 "creating pps timer");
3777 CHECKFATAL(cfg_parser_create(ns_g_mctx
, NULL
, &ns_g_parser
),
3778 "creating default configuration parser");
3780 if (ns_g_lwresdonly
)
3781 CHECKFATAL(load_configuration(lwresd_g_conffile
, server
,
3783 "loading configuration");
3785 CHECKFATAL(load_configuration(ns_g_conffile
, server
, ISC_TRUE
),
3786 "loading configuration");
3790 CHECKFATAL(load_zones(server
, ISC_FALSE
), "loading zones");
3793 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
, NS_LOGMODULE_SERVER
,
3794 ISC_LOG_NOTICE
, "running");
3798 ns_server_flushonshutdown(ns_server_t
*server
, isc_boolean_t flush
) {
3800 REQUIRE(NS_SERVER_VALID(server
));
3802 server
->flushonshutdown
= flush
;
3806 shutdown_server(isc_task_t
*task
, isc_event_t
*event
) {
3807 isc_result_t result
;
3808 dns_view_t
*view
, *view_next
;
3809 ns_server_t
*server
= (ns_server_t
*)event
->ev_arg
;
3810 isc_boolean_t flush
= server
->flushonshutdown
;
3813 INSIST(task
== server
->task
);
3815 result
= isc_task_beginexclusive(server
->task
);
3816 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3818 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
, NS_LOGMODULE_SERVER
,
3819 ISC_LOG_INFO
, "shutting down%s",
3820 flush
? ": flushing changes" : "");
3822 ns_statschannels_shutdown(server
);
3823 ns_controls_shutdown(server
->controls
);
3824 end_reserved_dispatches(server
, ISC_TRUE
);
3826 cfg_obj_destroy(ns_g_parser
, &ns_g_config
);
3827 cfg_parser_destroy(&ns_g_parser
);
3829 for (view
= ISC_LIST_HEAD(server
->viewlist
);
3832 view_next
= ISC_LIST_NEXT(view
, link
);
3833 ISC_LIST_UNLINK(server
->viewlist
, view
, link
);
3835 dns_view_flushanddetach(&view
);
3837 dns_view_detach(&view
);
3840 isc_timer_detach(&server
->interface_timer
);
3841 isc_timer_detach(&server
->heartbeat_timer
);
3842 isc_timer_detach(&server
->pps_timer
);
3844 ns_interfacemgr_shutdown(server
->interfacemgr
);
3845 ns_interfacemgr_detach(&server
->interfacemgr
);
3847 dns_dispatchmgr_destroy(&ns_g_dispatchmgr
);
3849 dns_zonemgr_shutdown(server
->zonemgr
);
3851 if (server
->blackholeacl
!= NULL
)
3852 dns_acl_detach(&server
->blackholeacl
);
3854 dns_db_detach(&server
->in_roothints
);
3856 isc_task_endexclusive(server
->task
);
3858 isc_task_detach(&server
->task
);
3860 isc_event_free(&event
);
3864 ns_server_create(isc_mem_t
*mctx
, ns_server_t
**serverp
) {
3865 isc_result_t result
;
3867 ns_server_t
*server
= isc_mem_get(mctx
, sizeof(*server
));
3869 fatal("allocating server object", ISC_R_NOMEMORY
);
3871 server
->mctx
= mctx
;
3872 server
->task
= NULL
;
3874 /* Initialize configuration data with default values. */
3876 result
= isc_quota_init(&server
->xfroutquota
, 10);
3877 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3878 result
= isc_quota_init(&server
->tcpquota
, 10);
3879 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3880 result
= isc_quota_init(&server
->recursionquota
, 100);
3881 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3883 result
= dns_aclenv_init(mctx
, &server
->aclenv
);
3884 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
3886 /* Initialize server data structures. */
3887 server
->zonemgr
= NULL
;
3888 server
->interfacemgr
= NULL
;
3889 ISC_LIST_INIT(server
->viewlist
);
3890 server
->in_roothints
= NULL
;
3891 server
->blackholeacl
= NULL
;
3893 CHECKFATAL(dns_rootns_create(mctx
, dns_rdataclass_in
, NULL
,
3894 &server
->in_roothints
),
3895 "setting up root hints");
3897 CHECKFATAL(isc_mutex_init(&server
->reload_event_lock
),
3898 "initializing reload event lock");
3899 server
->reload_event
=
3900 isc_event_allocate(ns_g_mctx
, server
,
3904 sizeof(isc_event_t
));
3905 CHECKFATAL(server
->reload_event
== NULL
?
3906 ISC_R_NOMEMORY
: ISC_R_SUCCESS
,
3907 "allocating reload event");
3909 CHECKFATAL(dst_lib_init(ns_g_mctx
, ns_g_entropy
, ISC_ENTROPY_GOODONLY
),
3910 "initializing DST");
3912 server
->tkeyctx
= NULL
;
3913 CHECKFATAL(dns_tkeyctx_create(ns_g_mctx
, ns_g_entropy
,
3915 "creating TKEY context");
3918 * Setup the server task, which is responsible for coordinating
3919 * startup and shutdown of the server.
3921 CHECKFATAL(isc_task_create(ns_g_taskmgr
, 0, &server
->task
),
3922 "creating server task");
3923 isc_task_setname(server
->task
, "server", server
);
3924 CHECKFATAL(isc_task_onshutdown(server
->task
, shutdown_server
, server
),
3925 "isc_task_onshutdown");
3926 CHECKFATAL(isc_app_onrun(ns_g_mctx
, server
->task
, run_server
, server
),
3929 server
->interface_timer
= NULL
;
3930 server
->heartbeat_timer
= NULL
;
3931 server
->pps_timer
= NULL
;
3933 server
->interface_interval
= 0;
3934 server
->heartbeat_interval
= 0;
3936 CHECKFATAL(dns_zonemgr_create(ns_g_mctx
, ns_g_taskmgr
, ns_g_timermgr
,
3937 ns_g_socketmgr
, &server
->zonemgr
),
3938 "dns_zonemgr_create");
3940 server
->statsfile
= isc_mem_strdup(server
->mctx
, "named.stats");
3941 CHECKFATAL(server
->statsfile
== NULL
? ISC_R_NOMEMORY
: ISC_R_SUCCESS
,
3943 server
->nsstats
= NULL
;
3944 server
->rcvquerystats
= NULL
;
3945 server
->opcodestats
= NULL
;
3946 server
->zonestats
= NULL
;
3947 server
->resolverstats
= NULL
;
3948 server
->sockstats
= NULL
;
3949 CHECKFATAL(isc_stats_create(server
->mctx
, &server
->sockstats
,
3950 isc_sockstatscounter_max
),
3951 "isc_stats_create");
3952 isc_socketmgr_setstats(ns_g_socketmgr
, server
->sockstats
);
3954 server
->dumpfile
= isc_mem_strdup(server
->mctx
, "named_dump.db");
3955 CHECKFATAL(server
->dumpfile
== NULL
? ISC_R_NOMEMORY
: ISC_R_SUCCESS
,
3958 server
->recfile
= isc_mem_strdup(server
->mctx
, "named.recursing");
3959 CHECKFATAL(server
->recfile
== NULL
? ISC_R_NOMEMORY
: ISC_R_SUCCESS
,
3962 server
->hostname_set
= ISC_FALSE
;
3963 server
->hostname
= NULL
;
3964 server
->version_set
= ISC_FALSE
;
3965 server
->version
= NULL
;
3966 server
->server_usehostname
= ISC_FALSE
;
3967 server
->server_id
= NULL
;
3969 CHECKFATAL(isc_stats_create(ns_g_mctx
, &server
->nsstats
,
3970 dns_nsstatscounter_max
),
3971 "dns_stats_create (server)");
3973 CHECKFATAL(dns_rdatatypestats_create(ns_g_mctx
,
3974 &server
->rcvquerystats
),
3975 "dns_stats_create (rcvquery)");
3977 CHECKFATAL(dns_opcodestats_create(ns_g_mctx
, &server
->opcodestats
),
3978 "dns_stats_create (opcode)");
3980 CHECKFATAL(isc_stats_create(ns_g_mctx
, &server
->zonestats
,
3981 dns_zonestatscounter_max
),
3982 "dns_stats_create (zone)");
3984 CHECKFATAL(isc_stats_create(ns_g_mctx
, &server
->resolverstats
,
3985 dns_resstatscounter_max
),
3986 "dns_stats_create (resolver)");
3988 server
->flushonshutdown
= ISC_FALSE
;
3989 server
->log_queries
= ISC_FALSE
;
3991 server
->controls
= NULL
;
3992 CHECKFATAL(ns_controls_create(server
, &server
->controls
),
3993 "ns_controls_create");
3994 server
->dispatchgen
= 0;
3995 ISC_LIST_INIT(server
->dispatches
);
3997 ISC_LIST_INIT(server
->statschannels
);
3999 server
->magic
= NS_SERVER_MAGIC
;
4004 ns_server_destroy(ns_server_t
**serverp
) {
4005 ns_server_t
*server
= *serverp
;
4006 REQUIRE(NS_SERVER_VALID(server
));
4008 ns_controls_destroy(&server
->controls
);
4010 isc_stats_detach(&server
->nsstats
);
4011 dns_stats_detach(&server
->rcvquerystats
);
4012 dns_stats_detach(&server
->opcodestats
);
4013 isc_stats_detach(&server
->zonestats
);
4014 isc_stats_detach(&server
->resolverstats
);
4015 isc_stats_detach(&server
->sockstats
);
4017 isc_mem_free(server
->mctx
, server
->statsfile
);
4018 isc_mem_free(server
->mctx
, server
->dumpfile
);
4019 isc_mem_free(server
->mctx
, server
->recfile
);
4021 if (server
->version
!= NULL
)
4022 isc_mem_free(server
->mctx
, server
->version
);
4023 if (server
->hostname
!= NULL
)
4024 isc_mem_free(server
->mctx
, server
->hostname
);
4025 if (server
->server_id
!= NULL
)
4026 isc_mem_free(server
->mctx
, server
->server_id
);
4028 dns_zonemgr_detach(&server
->zonemgr
);
4030 if (server
->tkeyctx
!= NULL
)
4031 dns_tkeyctx_destroy(&server
->tkeyctx
);
4035 isc_event_free(&server
->reload_event
);
4037 INSIST(ISC_LIST_EMPTY(server
->viewlist
));
4039 dns_aclenv_destroy(&server
->aclenv
);
4041 isc_quota_destroy(&server
->recursionquota
);
4042 isc_quota_destroy(&server
->tcpquota
);
4043 isc_quota_destroy(&server
->xfroutquota
);
4046 isc_mem_put(server
->mctx
, server
, sizeof(*server
));
4051 fatal(const char *msg
, isc_result_t result
) {
4052 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
, NS_LOGMODULE_SERVER
,
4053 ISC_LOG_CRITICAL
, "%s: %s", msg
,
4054 isc_result_totext(result
));
4055 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
, NS_LOGMODULE_SERVER
,
4056 ISC_LOG_CRITICAL
, "exiting (due to fatal error)");
4061 start_reserved_dispatches(ns_server_t
*server
) {
4063 REQUIRE(NS_SERVER_VALID(server
));
4065 server
->dispatchgen
++;
4069 end_reserved_dispatches(ns_server_t
*server
, isc_boolean_t all
) {
4070 ns_dispatch_t
*dispatch
, *nextdispatch
;
4072 REQUIRE(NS_SERVER_VALID(server
));
4074 for (dispatch
= ISC_LIST_HEAD(server
->dispatches
);
4076 dispatch
= nextdispatch
) {
4077 nextdispatch
= ISC_LIST_NEXT(dispatch
, link
);
4078 if (!all
&& server
->dispatchgen
== dispatch
-> dispatchgen
)
4080 ISC_LIST_UNLINK(server
->dispatches
, dispatch
, link
);
4081 dns_dispatch_detach(&dispatch
->dispatch
);
4082 isc_mem_put(server
->mctx
, dispatch
, sizeof(*dispatch
));
4087 ns_add_reserved_dispatch(ns_server_t
*server
, const isc_sockaddr_t
*addr
) {
4088 ns_dispatch_t
*dispatch
;
4090 char addrbuf
[ISC_SOCKADDR_FORMATSIZE
];
4091 isc_result_t result
;
4092 unsigned int attrs
, attrmask
;
4094 REQUIRE(NS_SERVER_VALID(server
));
4096 port
= isc_sockaddr_getport(addr
);
4097 if (port
== 0 || port
>= 1024)
4100 for (dispatch
= ISC_LIST_HEAD(server
->dispatches
);
4102 dispatch
= ISC_LIST_NEXT(dispatch
, link
)) {
4103 if (isc_sockaddr_equal(&dispatch
->addr
, addr
))
4106 if (dispatch
!= NULL
) {
4107 dispatch
->dispatchgen
= server
->dispatchgen
;
4111 dispatch
= isc_mem_get(server
->mctx
, sizeof(*dispatch
));
4112 if (dispatch
== NULL
) {
4113 result
= ISC_R_NOMEMORY
;
4117 dispatch
->addr
= *addr
;
4118 dispatch
->dispatchgen
= server
->dispatchgen
;
4119 dispatch
->dispatch
= NULL
;
4122 attrs
|= DNS_DISPATCHATTR_UDP
;
4123 switch (isc_sockaddr_pf(addr
)) {
4125 attrs
|= DNS_DISPATCHATTR_IPV4
;
4128 attrs
|= DNS_DISPATCHATTR_IPV6
;
4131 result
= ISC_R_NOTIMPLEMENTED
;
4135 attrmask
|= DNS_DISPATCHATTR_UDP
;
4136 attrmask
|= DNS_DISPATCHATTR_TCP
;
4137 attrmask
|= DNS_DISPATCHATTR_IPV4
;
4138 attrmask
|= DNS_DISPATCHATTR_IPV6
;
4140 result
= dns_dispatch_getudp(ns_g_dispatchmgr
, ns_g_socketmgr
,
4141 ns_g_taskmgr
, &dispatch
->addr
, 4096,
4142 1000, 32768, 16411, 16433,
4143 attrs
, attrmask
, &dispatch
->dispatch
);
4144 if (result
!= ISC_R_SUCCESS
)
4147 ISC_LIST_INITANDPREPEND(server
->dispatches
, dispatch
, link
);
4152 if (dispatch
!= NULL
)
4153 isc_mem_put(server
->mctx
, dispatch
, sizeof(*dispatch
));
4154 isc_sockaddr_format(addr
, addrbuf
, sizeof(addrbuf
));
4155 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4156 NS_LOGMODULE_SERVER
, ISC_LOG_WARNING
,
4157 "unable to create dispatch for reserved port %s: %s",
4158 addrbuf
, isc_result_totext(result
));
4163 loadconfig(ns_server_t
*server
) {
4164 isc_result_t result
;
4165 start_reserved_dispatches(server
);
4166 result
= load_configuration(ns_g_lwresdonly
?
4167 lwresd_g_conffile
: ns_g_conffile
,
4169 if (result
== ISC_R_SUCCESS
) {
4170 end_reserved_dispatches(server
, ISC_FALSE
);
4171 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4172 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4173 "reloading configuration succeeded");
4175 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4176 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
4177 "reloading configuration failed: %s",
4178 isc_result_totext(result
));
4184 reload(ns_server_t
*server
) {
4185 isc_result_t result
;
4186 CHECK(loadconfig(server
));
4188 result
= load_zones(server
, ISC_FALSE
);
4189 if (result
== ISC_R_SUCCESS
)
4190 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4191 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4192 "reloading zones succeeded");
4194 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4195 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
4196 "reloading zones failed: %s",
4197 isc_result_totext(result
));
4204 reconfig(ns_server_t
*server
) {
4205 isc_result_t result
;
4206 CHECK(loadconfig(server
));
4208 result
= load_new_zones(server
, ISC_FALSE
);
4209 if (result
== ISC_R_SUCCESS
)
4210 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4211 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4212 "any newly configured zones are now loaded");
4214 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4215 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
4216 "loading new zones failed: %s",
4217 isc_result_totext(result
));
4223 * Handle a reload event (from SIGHUP).
4226 ns_server_reload(isc_task_t
*task
, isc_event_t
*event
) {
4227 ns_server_t
*server
= (ns_server_t
*)event
->ev_arg
;
4229 INSIST(task
= server
->task
);
4232 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4233 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4234 "received SIGHUP signal to reload zones");
4235 (void)reload(server
);
4237 LOCK(&server
->reload_event_lock
);
4238 INSIST(server
->reload_event
== NULL
);
4239 server
->reload_event
= event
;
4240 UNLOCK(&server
->reload_event_lock
);
4244 ns_server_reloadwanted(ns_server_t
*server
) {
4245 LOCK(&server
->reload_event_lock
);
4246 if (server
->reload_event
!= NULL
)
4247 isc_task_send(server
->task
, &server
->reload_event
);
4248 UNLOCK(&server
->reload_event_lock
);
4252 next_token(char **stringp
, const char *delim
) {
4256 res
= strsep(stringp
, delim
);
4259 } while (*res
== '\0');
4264 * Find the zone specified in the control channel command 'args',
4265 * if any. If a zone is specified, point '*zonep' at it, otherwise
4266 * set '*zonep' to NULL.
4269 zone_from_args(ns_server_t
*server
, char *args
, dns_zone_t
**zonep
) {
4271 const char *zonetxt
;
4273 const char *viewtxt
= NULL
;
4274 dns_fixedname_t name
;
4275 isc_result_t result
;
4277 dns_view_t
*view
= NULL
;
4278 dns_rdataclass_t rdclass
;
4280 REQUIRE(zonep
!= NULL
&& *zonep
== NULL
);
4284 /* Skip the command name. */
4285 ptr
= next_token(&input
, " \t");
4287 return (ISC_R_UNEXPECTEDEND
);
4289 /* Look for the zone name. */
4290 zonetxt
= next_token(&input
, " \t");
4291 if (zonetxt
== NULL
)
4292 return (ISC_R_SUCCESS
);
4294 /* Look for the optional class name. */
4295 classtxt
= next_token(&input
, " \t");
4296 if (classtxt
!= NULL
) {
4297 /* Look for the optional view name. */
4298 viewtxt
= next_token(&input
, " \t");
4301 isc_buffer_init(&buf
, zonetxt
, strlen(zonetxt
));
4302 isc_buffer_add(&buf
, strlen(zonetxt
));
4303 dns_fixedname_init(&name
);
4304 result
= dns_name_fromtext(dns_fixedname_name(&name
),
4305 &buf
, dns_rootname
, ISC_FALSE
, NULL
);
4306 if (result
!= ISC_R_SUCCESS
)
4309 if (classtxt
!= NULL
) {
4312 r
.length
= strlen(classtxt
);
4313 result
= dns_rdataclass_fromtext(&rdclass
, &r
);
4314 if (result
!= ISC_R_SUCCESS
)
4317 rdclass
= dns_rdataclass_in
;
4319 if (viewtxt
== NULL
) {
4320 result
= dns_viewlist_findzone(&server
->viewlist
,
4321 dns_fixedname_name(&name
),
4322 ISC_TF(classtxt
== NULL
),
4325 result
= dns_viewlist_find(&server
->viewlist
, viewtxt
,
4327 if (result
!= ISC_R_SUCCESS
)
4330 result
= dns_zt_find(view
->zonetable
, dns_fixedname_name(&name
),
4332 dns_view_detach(&view
);
4335 /* Partial match? */
4336 if (result
!= ISC_R_SUCCESS
&& *zonep
!= NULL
)
4337 dns_zone_detach(zonep
);
4343 * Act on a "retransfer" command from the command channel.
4346 ns_server_retransfercommand(ns_server_t
*server
, char *args
) {
4347 isc_result_t result
;
4348 dns_zone_t
*zone
= NULL
;
4349 dns_zonetype_t type
;
4351 result
= zone_from_args(server
, args
, &zone
);
4352 if (result
!= ISC_R_SUCCESS
)
4355 return (ISC_R_UNEXPECTEDEND
);
4356 type
= dns_zone_gettype(zone
);
4357 if (type
== dns_zone_slave
|| type
== dns_zone_stub
)
4358 dns_zone_forcereload(zone
);
4360 result
= ISC_R_NOTFOUND
;
4361 dns_zone_detach(&zone
);
4366 * Act on a "reload" command from the command channel.
4369 ns_server_reloadcommand(ns_server_t
*server
, char *args
, isc_buffer_t
*text
) {
4370 isc_result_t result
;
4371 dns_zone_t
*zone
= NULL
;
4372 dns_zonetype_t type
;
4373 const char *msg
= NULL
;
4375 result
= zone_from_args(server
, args
, &zone
);
4376 if (result
!= ISC_R_SUCCESS
)
4379 result
= reload(server
);
4380 if (result
== ISC_R_SUCCESS
)
4381 msg
= "server reload successful";
4383 type
= dns_zone_gettype(zone
);
4384 if (type
== dns_zone_slave
|| type
== dns_zone_stub
) {
4385 dns_zone_refresh(zone
);
4386 dns_zone_detach(&zone
);
4387 msg
= "zone refresh queued";
4389 result
= dns_zone_load(zone
);
4390 dns_zone_detach(&zone
);
4393 msg
= "zone reload successful";
4395 case DNS_R_CONTINUE
:
4396 msg
= "zone reload queued";
4397 result
= ISC_R_SUCCESS
;
4399 case DNS_R_UPTODATE
:
4400 msg
= "zone reload up-to-date";
4401 result
= ISC_R_SUCCESS
;
4404 /* failure message will be generated by rndc */
4409 if (msg
!= NULL
&& strlen(msg
) < isc_buffer_availablelength(text
))
4410 isc_buffer_putmem(text
, (const unsigned char *)msg
,
4416 * Act on a "reconfig" command from the command channel.
4419 ns_server_reconfigcommand(ns_server_t
*server
, char *args
) {
4423 return (ISC_R_SUCCESS
);
4427 * Act on a "notify" command from the command channel.
4430 ns_server_notifycommand(ns_server_t
*server
, char *args
, isc_buffer_t
*text
) {
4431 isc_result_t result
;
4432 dns_zone_t
*zone
= NULL
;
4433 const unsigned char msg
[] = "zone notify queued";
4435 result
= zone_from_args(server
, args
, &zone
);
4436 if (result
!= ISC_R_SUCCESS
)
4439 return (ISC_R_UNEXPECTEDEND
);
4441 dns_zone_notify(zone
);
4442 dns_zone_detach(&zone
);
4443 if (sizeof(msg
) <= isc_buffer_availablelength(text
))
4444 isc_buffer_putmem(text
, msg
, sizeof(msg
));
4446 return (ISC_R_SUCCESS
);
4450 * Act on a "refresh" command from the command channel.
4453 ns_server_refreshcommand(ns_server_t
*server
, char *args
, isc_buffer_t
*text
) {
4454 isc_result_t result
;
4455 dns_zone_t
*zone
= NULL
;
4456 const unsigned char msg1
[] = "zone refresh queued";
4457 const unsigned char msg2
[] = "not a slave or stub zone";
4458 dns_zonetype_t type
;
4460 result
= zone_from_args(server
, args
, &zone
);
4461 if (result
!= ISC_R_SUCCESS
)
4464 return (ISC_R_UNEXPECTEDEND
);
4466 type
= dns_zone_gettype(zone
);
4467 if (type
== dns_zone_slave
|| type
== dns_zone_stub
) {
4468 dns_zone_refresh(zone
);
4469 dns_zone_detach(&zone
);
4470 if (sizeof(msg1
) <= isc_buffer_availablelength(text
))
4471 isc_buffer_putmem(text
, msg1
, sizeof(msg1
));
4472 return (ISC_R_SUCCESS
);
4475 dns_zone_detach(&zone
);
4476 if (sizeof(msg2
) <= isc_buffer_availablelength(text
))
4477 isc_buffer_putmem(text
, msg2
, sizeof(msg2
));
4478 return (ISC_R_FAILURE
);
4482 ns_server_togglequerylog(ns_server_t
*server
) {
4483 server
->log_queries
= server
->log_queries
? ISC_FALSE
: ISC_TRUE
;
4485 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4486 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4487 "query logging is now %s",
4488 server
->log_queries
? "on" : "off");
4489 return (ISC_R_SUCCESS
);
4493 ns_listenlist_fromconfig(const cfg_obj_t
*listenlist
, const cfg_obj_t
*config
,
4494 cfg_aclconfctx_t
*actx
,
4495 isc_mem_t
*mctx
, ns_listenlist_t
**target
)
4497 isc_result_t result
;
4498 const cfg_listelt_t
*element
;
4499 ns_listenlist_t
*dlist
= NULL
;
4501 REQUIRE(target
!= NULL
&& *target
== NULL
);
4503 result
= ns_listenlist_create(mctx
, &dlist
);
4504 if (result
!= ISC_R_SUCCESS
)
4507 for (element
= cfg_list_first(listenlist
);
4509 element
= cfg_list_next(element
))
4511 ns_listenelt_t
*delt
= NULL
;
4512 const cfg_obj_t
*listener
= cfg_listelt_value(element
);
4513 result
= ns_listenelt_fromconfig(listener
, config
, actx
,
4515 if (result
!= ISC_R_SUCCESS
)
4517 ISC_LIST_APPEND(dlist
->elts
, delt
, link
);
4520 return (ISC_R_SUCCESS
);
4523 ns_listenlist_detach(&dlist
);
4528 * Create a listen list from the corresponding configuration
4532 ns_listenelt_fromconfig(const cfg_obj_t
*listener
, const cfg_obj_t
*config
,
4533 cfg_aclconfctx_t
*actx
,
4534 isc_mem_t
*mctx
, ns_listenelt_t
**target
)
4536 isc_result_t result
;
4537 const cfg_obj_t
*portobj
;
4539 ns_listenelt_t
*delt
= NULL
;
4540 REQUIRE(target
!= NULL
&& *target
== NULL
);
4542 portobj
= cfg_tuple_get(listener
, "port");
4543 if (!cfg_obj_isuint32(portobj
)) {
4544 if (ns_g_port
!= 0) {
4547 result
= ns_config_getport(config
, &port
);
4548 if (result
!= ISC_R_SUCCESS
)
4552 if (cfg_obj_asuint32(portobj
) >= ISC_UINT16_MAX
) {
4553 cfg_obj_log(portobj
, ns_g_lctx
, ISC_LOG_ERROR
,
4554 "port value '%u' is out of range",
4555 cfg_obj_asuint32(portobj
));
4556 return (ISC_R_RANGE
);
4558 port
= (in_port_t
)cfg_obj_asuint32(portobj
);
4561 result
= ns_listenelt_create(mctx
, port
, NULL
, &delt
);
4562 if (result
!= ISC_R_SUCCESS
)
4565 result
= cfg_acl_fromconfig(cfg_tuple_get(listener
, "acl"),
4566 config
, ns_g_lctx
, actx
, mctx
, 0,
4568 if (result
!= ISC_R_SUCCESS
) {
4569 ns_listenelt_destroy(delt
);
4573 return (ISC_R_SUCCESS
);
4577 ns_server_dumpstats(ns_server_t
*server
) {
4578 isc_result_t result
;
4581 CHECKMF(isc_stdio_open(server
->statsfile
, "a", &fp
),
4582 "could not open statistics dump file", server
->statsfile
);
4584 result
= ns_stats_dump(server
, fp
);
4589 (void)isc_stdio_close(fp
);
4590 if (result
== ISC_R_SUCCESS
)
4591 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4592 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4593 "dumpstats complete");
4595 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4596 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
4597 "dumpstats failed: %s",
4598 dns_result_totext(result
));
4603 add_zone_tolist(dns_zone_t
*zone
, void *uap
) {
4604 struct dumpcontext
*dctx
= uap
;
4605 struct zonelistentry
*zle
;
4607 zle
= isc_mem_get(dctx
->mctx
, sizeof *zle
);
4609 return (ISC_R_NOMEMORY
);
4611 dns_zone_attach(zone
, &zle
->zone
);
4612 ISC_LINK_INIT(zle
, link
);
4613 ISC_LIST_APPEND(ISC_LIST_TAIL(dctx
->viewlist
)->zonelist
, zle
, link
);
4614 return (ISC_R_SUCCESS
);
4618 add_view_tolist(struct dumpcontext
*dctx
, dns_view_t
*view
) {
4619 struct viewlistentry
*vle
;
4620 isc_result_t result
= ISC_R_SUCCESS
;
4623 * Prevent duplicate views.
4625 for (vle
= ISC_LIST_HEAD(dctx
->viewlist
);
4627 vle
= ISC_LIST_NEXT(vle
, link
))
4628 if (vle
->view
== view
)
4629 return (ISC_R_SUCCESS
);
4631 vle
= isc_mem_get(dctx
->mctx
, sizeof *vle
);
4633 return (ISC_R_NOMEMORY
);
4635 dns_view_attach(view
, &vle
->view
);
4636 ISC_LINK_INIT(vle
, link
);
4637 ISC_LIST_INIT(vle
->zonelist
);
4638 ISC_LIST_APPEND(dctx
->viewlist
, vle
, link
);
4639 if (dctx
->dumpzones
)
4640 result
= dns_zt_apply(view
->zonetable
, ISC_TRUE
,
4641 add_zone_tolist
, dctx
);
4646 dumpcontext_destroy(struct dumpcontext
*dctx
) {
4647 struct viewlistentry
*vle
;
4648 struct zonelistentry
*zle
;
4650 vle
= ISC_LIST_HEAD(dctx
->viewlist
);
4651 while (vle
!= NULL
) {
4652 ISC_LIST_UNLINK(dctx
->viewlist
, vle
, link
);
4653 zle
= ISC_LIST_HEAD(vle
->zonelist
);
4654 while (zle
!= NULL
) {
4655 ISC_LIST_UNLINK(vle
->zonelist
, zle
, link
);
4656 dns_zone_detach(&zle
->zone
);
4657 isc_mem_put(dctx
->mctx
, zle
, sizeof *zle
);
4658 zle
= ISC_LIST_HEAD(vle
->zonelist
);
4660 dns_view_detach(&vle
->view
);
4661 isc_mem_put(dctx
->mctx
, vle
, sizeof *vle
);
4662 vle
= ISC_LIST_HEAD(dctx
->viewlist
);
4664 if (dctx
->version
!= NULL
)
4665 dns_db_closeversion(dctx
->db
, &dctx
->version
, ISC_FALSE
);
4666 if (dctx
->db
!= NULL
)
4667 dns_db_detach(&dctx
->db
);
4668 if (dctx
->cache
!= NULL
)
4669 dns_db_detach(&dctx
->cache
);
4670 if (dctx
->task
!= NULL
)
4671 isc_task_detach(&dctx
->task
);
4672 if (dctx
->fp
!= NULL
)
4673 (void)isc_stdio_close(dctx
->fp
);
4674 if (dctx
->mdctx
!= NULL
)
4675 dns_dumpctx_detach(&dctx
->mdctx
);
4676 isc_mem_put(dctx
->mctx
, dctx
, sizeof *dctx
);
4680 dumpdone(void *arg
, isc_result_t result
) {
4681 struct dumpcontext
*dctx
= arg
;
4683 const dns_master_style_t
*style
;
4685 if (result
!= ISC_R_SUCCESS
)
4687 if (dctx
->mdctx
!= NULL
)
4688 dns_dumpctx_detach(&dctx
->mdctx
);
4689 if (dctx
->view
== NULL
) {
4690 dctx
->view
= ISC_LIST_HEAD(dctx
->viewlist
);
4691 if (dctx
->view
== NULL
)
4693 INSIST(dctx
->zone
== NULL
);
4697 fprintf(dctx
->fp
, ";\n; Start view %s\n;\n", dctx
->view
->view
->name
);
4699 if (dctx
->zone
== NULL
&& dctx
->cache
== NULL
&& dctx
->dumpcache
) {
4700 style
= &dns_master_style_cache
;
4701 /* start cache dump */
4702 if (dctx
->view
->view
->cachedb
!= NULL
)
4703 dns_db_attach(dctx
->view
->view
->cachedb
, &dctx
->cache
);
4704 if (dctx
->cache
!= NULL
) {
4706 fprintf(dctx
->fp
, ";\n; Cache dump of view '%s'\n;\n",
4707 dctx
->view
->view
->name
);
4708 result
= dns_master_dumptostreaminc(dctx
->mctx
,
4714 if (result
== DNS_R_CONTINUE
)
4716 if (result
== ISC_R_NOTIMPLEMENTED
)
4717 fprintf(dctx
->fp
, "; %s\n",
4718 dns_result_totext(result
));
4719 else if (result
!= ISC_R_SUCCESS
)
4723 if (dctx
->cache
!= NULL
) {
4724 dns_adb_dump(dctx
->view
->view
->adb
, dctx
->fp
);
4725 dns_resolver_printbadcache(dctx
->view
->view
->resolver
,
4727 dns_db_detach(&dctx
->cache
);
4729 if (dctx
->dumpzones
) {
4730 style
= &dns_master_style_full
;
4732 if (dctx
->version
!= NULL
)
4733 dns_db_closeversion(dctx
->db
, &dctx
->version
,
4735 if (dctx
->db
!= NULL
)
4736 dns_db_detach(&dctx
->db
);
4737 if (dctx
->zone
== NULL
)
4738 dctx
->zone
= ISC_LIST_HEAD(dctx
->view
->zonelist
);
4740 dctx
->zone
= ISC_LIST_NEXT(dctx
->zone
, link
);
4741 if (dctx
->zone
!= NULL
) {
4742 /* start zone dump */
4743 dns_zone_name(dctx
->zone
->zone
, buf
, sizeof(buf
));
4744 fprintf(dctx
->fp
, ";\n; Zone dump of '%s'\n;\n", buf
);
4745 result
= dns_zone_getdb(dctx
->zone
->zone
, &dctx
->db
);
4746 if (result
!= ISC_R_SUCCESS
) {
4747 fprintf(dctx
->fp
, "; %s\n",
4748 dns_result_totext(result
));
4751 dns_db_currentversion(dctx
->db
, &dctx
->version
);
4752 result
= dns_master_dumptostreaminc(dctx
->mctx
,
4759 if (result
== DNS_R_CONTINUE
)
4761 if (result
== ISC_R_NOTIMPLEMENTED
) {
4762 fprintf(dctx
->fp
, "; %s\n",
4763 dns_result_totext(result
));
4764 result
= ISC_R_SUCCESS
;
4767 if (result
!= ISC_R_SUCCESS
)
4771 if (dctx
->view
!= NULL
)
4772 dctx
->view
= ISC_LIST_NEXT(dctx
->view
, link
);
4773 if (dctx
->view
!= NULL
)
4776 fprintf(dctx
->fp
, "; Dump complete\n");
4777 result
= isc_stdio_flush(dctx
->fp
);
4778 if (result
== ISC_R_SUCCESS
)
4779 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4780 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4783 if (result
!= ISC_R_SUCCESS
)
4784 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4785 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
4786 "dumpdb failed: %s", dns_result_totext(result
));
4787 dumpcontext_destroy(dctx
);
4791 ns_server_dumpdb(ns_server_t
*server
, char *args
) {
4792 struct dumpcontext
*dctx
= NULL
;
4794 isc_result_t result
;
4798 /* Skip the command name. */
4799 ptr
= next_token(&args
, " \t");
4801 return (ISC_R_UNEXPECTEDEND
);
4803 dctx
= isc_mem_get(server
->mctx
, sizeof(*dctx
));
4805 return (ISC_R_NOMEMORY
);
4807 dctx
->mctx
= server
->mctx
;
4808 dctx
->dumpcache
= ISC_TRUE
;
4809 dctx
->dumpzones
= ISC_FALSE
;
4811 ISC_LIST_INIT(dctx
->viewlist
);
4819 dctx
->version
= NULL
;
4820 isc_task_attach(server
->task
, &dctx
->task
);
4822 CHECKMF(isc_stdio_open(server
->dumpfile
, "w", &dctx
->fp
),
4823 "could not open dump file", server
->dumpfile
);
4825 sep
= (args
== NULL
) ? "" : ": ";
4826 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4827 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4828 "dumpdb started%s%s", sep
, (args
!= NULL
) ? args
: "");
4830 ptr
= next_token(&args
, " \t");
4831 if (ptr
!= NULL
&& strcmp(ptr
, "-all") == 0) {
4832 dctx
->dumpzones
= ISC_TRUE
;
4833 dctx
->dumpcache
= ISC_TRUE
;
4834 ptr
= next_token(&args
, " \t");
4835 } else if (ptr
!= NULL
&& strcmp(ptr
, "-cache") == 0) {
4836 dctx
->dumpzones
= ISC_FALSE
;
4837 dctx
->dumpcache
= ISC_TRUE
;
4838 ptr
= next_token(&args
, " \t");
4839 } else if (ptr
!= NULL
&& strcmp(ptr
, "-zones") == 0) {
4840 dctx
->dumpzones
= ISC_TRUE
;
4841 dctx
->dumpcache
= ISC_FALSE
;
4842 ptr
= next_token(&args
, " \t");
4846 for (view
= ISC_LIST_HEAD(server
->viewlist
);
4848 view
= ISC_LIST_NEXT(view
, link
))
4850 if (ptr
!= NULL
&& strcmp(view
->name
, ptr
) != 0)
4852 CHECK(add_view_tolist(dctx
, view
));
4855 ptr
= next_token(&args
, " \t");
4859 dumpdone(dctx
, ISC_R_SUCCESS
);
4860 return (ISC_R_SUCCESS
);
4864 dumpcontext_destroy(dctx
);
4869 ns_server_dumprecursing(ns_server_t
*server
) {
4871 isc_result_t result
;
4873 CHECKMF(isc_stdio_open(server
->recfile
, "w", &fp
),
4874 "could not open dump file", server
->recfile
);
4875 fprintf(fp
,";\n; Recursing Queries\n;\n");
4876 ns_interfacemgr_dumprecursing(fp
, server
->interfacemgr
);
4877 fprintf(fp
, "; Dump complete\n");
4881 result
= isc_stdio_close(fp
);
4882 if (result
== ISC_R_SUCCESS
)
4883 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4884 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4885 "dumprecursing complete");
4887 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4888 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
4889 "dumprecursing failed: %s",
4890 dns_result_totext(result
));
4895 ns_server_setdebuglevel(ns_server_t
*server
, char *args
) {
4903 /* Skip the command name. */
4904 ptr
= next_token(&args
, " \t");
4906 return (ISC_R_UNEXPECTEDEND
);
4908 /* Look for the new level name. */
4909 levelstr
= next_token(&args
, " \t");
4910 if (levelstr
== NULL
) {
4911 if (ns_g_debuglevel
< 99)
4914 newlevel
= strtol(levelstr
, &endp
, 10);
4915 if (*endp
!= '\0' || newlevel
< 0 || newlevel
> 99)
4916 return (ISC_R_RANGE
);
4917 ns_g_debuglevel
= (unsigned int)newlevel
;
4919 isc_log_setdebuglevel(ns_g_lctx
, ns_g_debuglevel
);
4920 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
4921 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
4922 "debug level is now %d", ns_g_debuglevel
);
4923 return (ISC_R_SUCCESS
);
4927 ns_server_validation(ns_server_t
*server
, char *args
) {
4928 char *ptr
, *viewname
;
4930 isc_boolean_t changed
= ISC_FALSE
;
4931 isc_result_t result
;
4932 isc_boolean_t enable
;
4934 /* Skip the command name. */
4935 ptr
= next_token(&args
, " \t");
4937 return (ISC_R_UNEXPECTEDEND
);
4939 /* Find out what we are to do. */
4940 ptr
= next_token(&args
, " \t");
4942 return (ISC_R_UNEXPECTEDEND
);
4944 if (!strcasecmp(ptr
, "on") || !strcasecmp(ptr
, "yes") ||
4945 !strcasecmp(ptr
, "enable") || !strcasecmp(ptr
, "true"))
4947 else if (!strcasecmp(ptr
, "off") || !strcasecmp(ptr
, "no") ||
4948 !strcasecmp(ptr
, "disable") || !strcasecmp(ptr
, "false"))
4951 return (DNS_R_SYNTAX
);
4953 /* Look for the view name. */
4954 viewname
= next_token(&args
, " \t");
4956 result
= isc_task_beginexclusive(server
->task
);
4957 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
4958 for (view
= ISC_LIST_HEAD(server
->viewlist
);
4960 view
= ISC_LIST_NEXT(view
, link
))
4962 if (viewname
!= NULL
&& strcasecmp(viewname
, view
->name
) != 0)
4964 result
= dns_view_flushcache(view
);
4965 if (result
!= ISC_R_SUCCESS
)
4967 view
->enablevalidation
= enable
;
4971 result
= ISC_R_SUCCESS
;
4973 result
= ISC_R_FAILURE
;
4975 isc_task_endexclusive(server
->task
);
4980 ns_server_flushcache(ns_server_t
*server
, char *args
) {
4981 char *ptr
, *viewname
;
4983 isc_boolean_t flushed
;
4984 isc_boolean_t found
;
4985 isc_result_t result
;
4987 /* Skip the command name. */
4988 ptr
= next_token(&args
, " \t");
4990 return (ISC_R_UNEXPECTEDEND
);
4992 /* Look for the view name. */
4993 viewname
= next_token(&args
, " \t");
4995 result
= isc_task_beginexclusive(server
->task
);
4996 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
4999 for (view
= ISC_LIST_HEAD(server
->viewlist
);
5001 view
= ISC_LIST_NEXT(view
, link
))
5003 if (viewname
!= NULL
&& strcasecmp(viewname
, view
->name
) != 0)
5006 result
= dns_view_flushcache(view
);
5007 if (result
!= ISC_R_SUCCESS
) {
5008 flushed
= ISC_FALSE
;
5009 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5010 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
5011 "flushing cache in view '%s' failed: %s",
5012 view
->name
, isc_result_totext(result
));
5015 if (flushed
&& found
) {
5016 if (viewname
!= NULL
)
5017 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5018 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
5019 "flushing cache in view '%s' succeeded",
5022 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5023 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
5024 "flushing caches in all views succeeded");
5025 result
= ISC_R_SUCCESS
;
5028 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5029 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
5030 "flushing cache in view '%s' failed: "
5031 "view not found", viewname
);
5032 result
= ISC_R_NOTFOUND
;
5034 result
= ISC_R_FAILURE
;
5036 isc_task_endexclusive(server
->task
);
5041 ns_server_flushname(ns_server_t
*server
, char *args
) {
5042 char *ptr
, *target
, *viewname
;
5044 isc_boolean_t flushed
;
5045 isc_boolean_t found
;
5046 isc_result_t result
;
5048 dns_fixedname_t fixed
;
5051 /* Skip the command name. */
5052 ptr
= next_token(&args
, " \t");
5054 return (ISC_R_UNEXPECTEDEND
);
5056 /* Find the domain name to flush. */
5057 target
= next_token(&args
, " \t");
5059 return (ISC_R_UNEXPECTEDEND
);
5061 isc_buffer_init(&b
, target
, strlen(target
));
5062 isc_buffer_add(&b
, strlen(target
));
5063 dns_fixedname_init(&fixed
);
5064 name
= dns_fixedname_name(&fixed
);
5065 result
= dns_name_fromtext(name
, &b
, dns_rootname
, ISC_FALSE
, NULL
);
5066 if (result
!= ISC_R_SUCCESS
)
5069 /* Look for the view name. */
5070 viewname
= next_token(&args
, " \t");
5072 result
= isc_task_beginexclusive(server
->task
);
5073 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
5076 for (view
= ISC_LIST_HEAD(server
->viewlist
);
5078 view
= ISC_LIST_NEXT(view
, link
))
5080 if (viewname
!= NULL
&& strcasecmp(viewname
, view
->name
) != 0)
5083 result
= dns_view_flushname(view
, name
);
5084 if (result
!= ISC_R_SUCCESS
) {
5085 flushed
= ISC_FALSE
;
5086 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5087 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
5088 "flushing name '%s' in cache view '%s' "
5089 "failed: %s", target
, view
->name
,
5090 isc_result_totext(result
));
5093 if (flushed
&& found
) {
5094 if (viewname
!= NULL
)
5095 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5096 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
5097 "flushing name '%s' in cache view '%s' "
5098 "succeeded", target
, viewname
);
5100 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5101 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
5102 "flushing name '%s' in all cache views "
5103 "succeeded", target
);
5104 result
= ISC_R_SUCCESS
;
5107 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5108 NS_LOGMODULE_SERVER
, ISC_LOG_ERROR
,
5109 "flushing name '%s' in cache view '%s' "
5110 "failed: view not found", target
,
5112 result
= ISC_R_FAILURE
;
5114 isc_task_endexclusive(server
->task
);
5119 ns_server_status(ns_server_t
*server
, isc_buffer_t
*text
) {
5120 int zonecount
, xferrunning
, xferdeferred
, soaqueries
;
5122 const char *ob
= "", *cb
= "", *alt
= "";
5124 if (ns_g_server
->version_set
) {
5127 if (ns_g_server
->version
== NULL
)
5128 alt
= "version.bind/txt/ch disabled";
5130 alt
= ns_g_server
->version
;
5132 zonecount
= dns_zonemgr_getcount(server
->zonemgr
, DNS_ZONESTATE_ANY
);
5133 xferrunning
= dns_zonemgr_getcount(server
->zonemgr
,
5134 DNS_ZONESTATE_XFERRUNNING
);
5135 xferdeferred
= dns_zonemgr_getcount(server
->zonemgr
,
5136 DNS_ZONESTATE_XFERDEFERRED
);
5137 soaqueries
= dns_zonemgr_getcount(server
->zonemgr
,
5138 DNS_ZONESTATE_SOAQUERY
);
5139 n
= snprintf((char *)isc_buffer_used(text
),
5140 isc_buffer_availablelength(text
),
5141 "version: %s%s%s%s\n"
5142 "number of zones: %u\n"
5144 "xfers running: %u\n"
5145 "xfers deferred: %u\n"
5146 "soa queries in progress: %u\n"
5147 "query logging is %s\n"
5148 "recursive clients: %d/%d/%d\n"
5149 "tcp clients: %d/%d\n"
5150 "server is up and running",
5151 ns_g_version
, ob
, alt
, cb
,
5152 zonecount
, ns_g_debuglevel
, xferrunning
, xferdeferred
,
5153 soaqueries
, server
->log_queries
? "ON" : "OFF",
5154 server
->recursionquota
.used
, server
->recursionquota
.soft
,
5155 server
->recursionquota
.max
,
5156 server
->tcpquota
.used
, server
->tcpquota
.max
);
5157 if (n
>= isc_buffer_availablelength(text
))
5158 return (ISC_R_NOSPACE
);
5159 isc_buffer_add(text
, n
);
5160 return (ISC_R_SUCCESS
);
5164 delete_keynames(dns_tsig_keyring_t
*ring
, char *target
,
5165 unsigned int *foundkeys
)
5167 char namestr
[DNS_NAME_FORMATSIZE
];
5168 isc_result_t result
;
5169 dns_rbtnodechain_t chain
;
5170 dns_name_t foundname
;
5171 dns_fixedname_t fixedorigin
;
5173 dns_rbtnode_t
*node
;
5174 dns_tsigkey_t
*tkey
;
5176 dns_name_init(&foundname
, NULL
);
5177 dns_fixedname_init(&fixedorigin
);
5178 origin
= dns_fixedname_name(&fixedorigin
);
5181 dns_rbtnodechain_init(&chain
, ring
->mctx
);
5182 result
= dns_rbtnodechain_first(&chain
, ring
->keys
, &foundname
,
5184 if (result
== ISC_R_NOTFOUND
) {
5185 dns_rbtnodechain_invalidate(&chain
);
5186 return (ISC_R_SUCCESS
);
5188 if (result
!= ISC_R_SUCCESS
&& result
!= DNS_R_NEWORIGIN
) {
5189 dns_rbtnodechain_invalidate(&chain
);
5195 dns_rbtnodechain_current(&chain
, &foundname
, origin
, &node
);
5199 if (!tkey
->generated
)
5202 dns_name_format(&tkey
->name
, namestr
, sizeof(namestr
));
5203 if (strcmp(namestr
, target
) == 0) {
5205 dns_rbtnodechain_invalidate(&chain
);
5206 (void)dns_rbt_deletename(ring
->keys
,
5214 result
= dns_rbtnodechain_next(&chain
, &foundname
, origin
);
5215 if (result
== ISC_R_NOMORE
)
5217 if (result
!= ISC_R_SUCCESS
&& result
!= DNS_R_NEWORIGIN
) {
5218 dns_rbtnodechain_invalidate(&chain
);
5223 return (ISC_R_SUCCESS
);
5227 ns_server_tsigdelete(ns_server_t
*server
, char *command
, isc_buffer_t
*text
) {
5228 isc_result_t result
;
5231 unsigned int foundkeys
= 0;
5235 (void)next_token(&command
, " \t"); /* skip command name */
5236 target
= next_token(&command
, " \t");
5238 return (ISC_R_UNEXPECTEDEND
);
5239 viewname
= next_token(&command
, " \t");
5241 result
= isc_task_beginexclusive(server
->task
);
5242 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
5243 for (view
= ISC_LIST_HEAD(server
->viewlist
);
5245 view
= ISC_LIST_NEXT(view
, link
)) {
5246 if (viewname
== NULL
|| strcmp(view
->name
, viewname
) == 0) {
5247 RWLOCK(&view
->dynamickeys
->lock
, isc_rwlocktype_write
);
5248 result
= delete_keynames(view
->dynamickeys
, target
,
5250 RWUNLOCK(&view
->dynamickeys
->lock
,
5251 isc_rwlocktype_write
);
5252 if (result
!= ISC_R_SUCCESS
) {
5253 isc_task_endexclusive(server
->task
);
5258 isc_task_endexclusive(server
->task
);
5260 n
= snprintf((char *)isc_buffer_used(text
),
5261 isc_buffer_availablelength(text
),
5262 "%d tsig keys deleted.\n", foundkeys
);
5263 if (n
>= isc_buffer_availablelength(text
)) {
5264 isc_task_endexclusive(server
->task
);
5265 return (ISC_R_NOSPACE
);
5267 isc_buffer_add(text
, n
);
5269 return (ISC_R_SUCCESS
);
5273 list_keynames(dns_view_t
*view
, dns_tsig_keyring_t
*ring
, isc_buffer_t
*text
,
5274 unsigned int *foundkeys
)
5276 char namestr
[DNS_NAME_FORMATSIZE
];
5277 char creatorstr
[DNS_NAME_FORMATSIZE
];
5278 isc_result_t result
;
5279 dns_rbtnodechain_t chain
;
5280 dns_name_t foundname
;
5281 dns_fixedname_t fixedorigin
;
5283 dns_rbtnode_t
*node
;
5284 dns_tsigkey_t
*tkey
;
5286 const char *viewname
;
5289 viewname
= view
->name
;
5291 viewname
= "(global)";
5293 dns_name_init(&foundname
, NULL
);
5294 dns_fixedname_init(&fixedorigin
);
5295 origin
= dns_fixedname_name(&fixedorigin
);
5296 dns_rbtnodechain_init(&chain
, ring
->mctx
);
5297 result
= dns_rbtnodechain_first(&chain
, ring
->keys
, &foundname
,
5299 if (result
== ISC_R_NOTFOUND
) {
5300 dns_rbtnodechain_invalidate(&chain
);
5301 return (ISC_R_SUCCESS
);
5303 if (result
!= ISC_R_SUCCESS
&& result
!= DNS_R_NEWORIGIN
) {
5304 dns_rbtnodechain_invalidate(&chain
);
5310 dns_rbtnodechain_current(&chain
, &foundname
, origin
, &node
);
5315 dns_name_format(&tkey
->name
, namestr
, sizeof(namestr
));
5316 if (tkey
->generated
) {
5317 dns_name_format(tkey
->creator
, creatorstr
,
5318 sizeof(creatorstr
));
5319 n
= snprintf((char *)isc_buffer_used(text
),
5320 isc_buffer_availablelength(text
),
5321 "view \"%s\"; type \"dynamic\"; key \"%s\"; creator \"%s\";\n",
5322 viewname
, namestr
, creatorstr
);
5324 n
= snprintf((char *)isc_buffer_used(text
),
5325 isc_buffer_availablelength(text
),
5326 "view \"%s\"; type \"static\"; key \"%s\";\n",
5329 if (n
>= isc_buffer_availablelength(text
)) {
5330 dns_rbtnodechain_invalidate(&chain
);
5331 return (ISC_R_NOSPACE
);
5333 isc_buffer_add(text
, n
);
5335 result
= dns_rbtnodechain_next(&chain
, &foundname
, origin
);
5336 if (result
== ISC_R_NOMORE
)
5338 if (result
!= ISC_R_SUCCESS
&& result
!= DNS_R_NEWORIGIN
) {
5339 dns_rbtnodechain_invalidate(&chain
);
5344 return (ISC_R_SUCCESS
);
5348 ns_server_tsiglist(ns_server_t
*server
, isc_buffer_t
*text
) {
5349 isc_result_t result
;
5352 unsigned int foundkeys
= 0;
5354 result
= isc_task_beginexclusive(server
->task
);
5355 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
5356 for (view
= ISC_LIST_HEAD(server
->viewlist
);
5358 view
= ISC_LIST_NEXT(view
, link
)) {
5359 RWLOCK(&view
->statickeys
->lock
, isc_rwlocktype_read
);
5360 result
= list_keynames(view
, view
->statickeys
, text
,
5362 RWUNLOCK(&view
->statickeys
->lock
, isc_rwlocktype_read
);
5363 if (result
!= ISC_R_SUCCESS
) {
5364 isc_task_endexclusive(server
->task
);
5367 RWLOCK(&view
->dynamickeys
->lock
, isc_rwlocktype_read
);
5368 result
= list_keynames(view
, view
->dynamickeys
, text
,
5370 RWUNLOCK(&view
->dynamickeys
->lock
, isc_rwlocktype_read
);
5371 if (result
!= ISC_R_SUCCESS
) {
5372 isc_task_endexclusive(server
->task
);
5376 isc_task_endexclusive(server
->task
);
5378 if (foundkeys
== 0) {
5379 n
= snprintf((char *)isc_buffer_used(text
),
5380 isc_buffer_availablelength(text
),
5381 "no tsig keys found.\n");
5382 if (n
>= isc_buffer_availablelength(text
)) {
5383 isc_task_endexclusive(server
->task
);
5384 return (ISC_R_NOSPACE
);
5386 isc_buffer_add(text
, n
);
5389 return (ISC_R_SUCCESS
);
5393 * Act on a "freeze" or "thaw" command from the command channel.
5396 ns_server_freeze(ns_server_t
*server
, isc_boolean_t freeze
, char *args
,
5399 isc_result_t result
, tresult
;
5400 dns_zone_t
*zone
= NULL
;
5401 dns_zonetype_t type
;
5402 char classstr
[DNS_RDATACLASS_FORMATSIZE
];
5403 char zonename
[DNS_NAME_FORMATSIZE
];
5406 const char *vname
, *sep
;
5407 isc_boolean_t frozen
;
5408 const char *msg
= NULL
;
5410 result
= zone_from_args(server
, args
, &zone
);
5411 if (result
!= ISC_R_SUCCESS
)
5414 result
= isc_task_beginexclusive(server
->task
);
5415 RUNTIME_CHECK(result
== ISC_R_SUCCESS
);
5416 tresult
= ISC_R_SUCCESS
;
5417 for (view
= ISC_LIST_HEAD(server
->viewlist
);
5419 view
= ISC_LIST_NEXT(view
, link
)) {
5420 result
= dns_view_freezezones(view
, freeze
);
5421 if (result
!= ISC_R_SUCCESS
&&
5422 tresult
== ISC_R_SUCCESS
)
5425 isc_task_endexclusive(server
->task
);
5426 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5427 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
5429 freeze
? "freezing" : "thawing",
5430 isc_result_totext(tresult
));
5433 type
= dns_zone_gettype(zone
);
5434 if (type
!= dns_zone_master
) {
5435 dns_zone_detach(&zone
);
5436 return (ISC_R_NOTFOUND
);
5439 frozen
= dns_zone_getupdatedisabled(zone
);
5442 msg
= "WARNING: The zone was already frozen.\n"
5443 "Someone else may be editing it or "
5444 "it may still be re-loading.";
5445 result
= DNS_R_FROZEN
;
5447 if (result
== ISC_R_SUCCESS
) {
5448 result
= dns_zone_flush(zone
);
5449 if (result
!= ISC_R_SUCCESS
)
5450 msg
= "Flushing the zone updates to "
5453 if (result
== ISC_R_SUCCESS
) {
5454 journal
= dns_zone_getjournal(zone
);
5455 if (journal
!= NULL
)
5456 (void)isc_file_remove(journal
);
5458 if (result
== ISC_R_SUCCESS
)
5459 dns_zone_setupdatedisabled(zone
, freeze
);
5462 result
= dns_zone_loadandthaw(zone
);
5465 case DNS_R_UPTODATE
:
5466 msg
= "The zone reload and thaw was "
5468 result
= ISC_R_SUCCESS
;
5470 case DNS_R_CONTINUE
:
5471 msg
= "A zone reload and thaw was started.\n"
5472 "Check the logs to see the result.";
5473 result
= ISC_R_SUCCESS
;
5479 if (msg
!= NULL
&& strlen(msg
) < isc_buffer_availablelength(text
))
5480 isc_buffer_putmem(text
, (const unsigned char *)msg
,
5483 view
= dns_zone_getview(zone
);
5484 if (strcmp(view
->name
, "_bind") == 0 ||
5485 strcmp(view
->name
, "_default") == 0)
5493 dns_rdataclass_format(dns_zone_getclass(zone
), classstr
,
5495 dns_name_format(dns_zone_getorigin(zone
),
5496 zonename
, sizeof(zonename
));
5497 isc_log_write(ns_g_lctx
, NS_LOGCATEGORY_GENERAL
,
5498 NS_LOGMODULE_SERVER
, ISC_LOG_INFO
,
5499 "%s zone '%s/%s'%s%s: %s",
5500 freeze
? "freezing" : "thawing",
5501 zonename
, classstr
, sep
, vname
,
5502 isc_result_totext(result
));
5503 dns_zone_detach(&zone
);
5509 * This function adds a message for rndc to echo if named
5510 * is managed by smf and is also running chroot.
5513 ns_smf_add_message(isc_buffer_t
*text
) {
5516 n
= snprintf((char *)isc_buffer_used(text
),
5517 isc_buffer_availablelength(text
),
5518 "use svcadm(1M) to manage named");
5519 if (n
>= isc_buffer_availablelength(text
))
5520 return (ISC_R_NOSPACE
);
5521 isc_buffer_add(text
, n
);
5522 return (ISC_R_SUCCESS
);
5524 #endif /* HAVE_LIBSCF */