2 * Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001-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: config.c,v 1.82.38.8 2008/09/27 23:39:42 jinmei Exp $ */
26 #include <isc/buffer.h>
29 #include <isc/parseint.h>
30 #include <isc/region.h>
31 #include <isc/result.h>
32 #include <isc/sockaddr.h>
33 #include <isc/string.h>
36 #include <isccfg/namedconf.h>
38 #include <dns/fixedname.h>
40 #include <dns/rdataclass.h>
41 #include <dns/rdatatype.h>
45 #include <named/config.h>
46 #include <named/globals.h>
48 /*% default configuration */
49 static char defaultconf
[] = "\
51 # blackhole {none;};\n"
53 " coresize default;\n\
58 " deallocate-on-exit true;\n\
60 dump-file \"named_dump.db\";\n\
62 has-old-clients false;\n\
63 heartbeat-interval 60;\n\
64 host-statistics no;\n\
65 interface-interval 60;\n\
67 listen-on-v6 {none;};\n\
68 match-mapped-addresses no;\n\
69 memstatistics-file \"named.memstats\";\n\
70 multiple-cnames no;\n\
71 # named-xfer <obsolete>;\n\
72 # pid-file \"" NS_LOCALSTATEDIR
"/named.pid\"; /* or /lwresd.pid */\n\
74 recursing-file \"named.recursing\";\n\
78 random-device \"" PATH_RANDOMDEV
"\";\n\
82 recursive-clients 1000;\n\
83 rrset-order {type NS order random; order cyclic; };\n\
85 serial-query-rate 20;\n\
87 statistics-file \"named.stats\";\n\
88 statistics-interval 60;\n\
90 tcp-listen-queue 3;\n\
91 # tkey-dhkey <none>\n\
92 # tkey-gssapi-credential <none>\n\
93 # tkey-domain <none>\n\
94 transfers-per-ns 2;\n\
97 treat-cr-as-space true;\n\
100 edns-udp-size 4096;\n\
101 max-udp-size 4096;\n\
102 request-nsid false;\n\
103 reserved-sockets 512;\n\
106 allow-notify {none;};\n\
107 allow-update-forwarding {none;};\n\
108 allow-query-cache { localnets; localhost; };\n\
109 allow-query-cache-on { any; };\n\
110 allow-recursion { localnets; localhost; };\n\
111 allow-recursion-on { any; };\n\
112 # allow-v6-synthesis <obsolete>;\n\
115 auth-nxdomain false;\n\
116 minimal-responses false;\n\
118 provide-ixfr true;\n\
119 request-ixfr true;\n\
122 additional-from-auth true;\n\
123 additional-from-cache true;\n\
124 query-source address *;\n\
125 query-source-v6 address *;\n\
127 notify-source-v6 *;\n\
128 cleaning-interval 0; /* now meaningless */\n\
131 max-ncache-ttl 10800; /* 3 hours */\n\
132 max-cache-ttl 604800; /* 1 week */\n\
133 transfer-format many-answers;\n\
135 check-names master fail;\n\
136 check-names slave warn;\n\
137 check-names response ignore;\n\
140 acache-cleaning-interval 60;\n\
141 max-acache-size 16M;\n\
142 dnssec-enable yes;\n\
143 dnssec-validation yes; \n\
144 dnssec-accept-expired no;\n\
145 clients-per-query 10;\n\
146 max-clients-per-query 100;\n\
147 zero-no-soa-ttl-cache no;\n\
151 allow-query {any;};\n\
152 allow-query-on {any;};\n\
153 allow-transfer {any;};\n\
155 # also-notify <none>\n\
160 # forwarders <none>\n\
161 maintain-ixfr-base no;\n\
162 # max-ixfr-log-size <obsolete>\n\
163 transfer-source *;\n\
164 transfer-source-v6 *;\n\
165 alt-transfer-source *;\n\
166 alt-transfer-source-v6 *;\n\
167 max-transfer-time-in 120;\n\
168 max-transfer-time-out 120;\n\
169 max-transfer-idle-in 60;\n\
170 max-transfer-idle-out 60;\n\
171 max-retry-time 1209600; /* 2 weeks */\n\
172 min-retry-time 500;\n\
173 max-refresh-time 2419200; /* 4 weeks */\n\
174 min-refresh-time 300;\n\
176 sig-validity-interval 30; /* days */\n\
177 zone-statistics false;\n\
178 max-journal-size unlimited;\n\
179 ixfr-from-differences false;\n\
180 check-wildcard yes;\n\
181 check-sibling yes;\n\
182 check-integrity yes;\n\
183 check-mx-cname warn;\n\
184 check-srv-cname warn;\n\
185 zero-no-soa-ttl yes;\n\
186 update-check-ksk yes;\n\
187 try-tcp-refresh yes; /* BIND 8 compat */\n\
192 # Zones in the \"_bind\" view are NOT counted in the count of zones.\n\
194 view \"_bind\" chaos {\n\
198 zone \"version.bind\" chaos {\n\
200 database \"_builtin version\";\n\
203 zone \"hostname.bind\" chaos {\n\
205 database \"_builtin hostname\";\n\
208 zone \"authors.bind\" chaos {\n\
210 database \"_builtin authors\";\n\
212 zone \"id.server\" chaos {\n\
214 database \"_builtin id\";\n\
220 ns_config_parsedefaults(cfg_parser_t
*parser
, cfg_obj_t
**conf
) {
223 isc_buffer_init(&b
, defaultconf
, sizeof(defaultconf
) - 1);
224 isc_buffer_add(&b
, sizeof(defaultconf
) - 1);
225 return (cfg_parse_buffer(parser
, &b
, &cfg_type_namedconf
, conf
));
229 ns_config_get(const cfg_obj_t
**maps
, const char *name
, const cfg_obj_t
**obj
) {
234 return (ISC_R_NOTFOUND
);
235 if (cfg_map_get(maps
[i
], name
, obj
) == ISC_R_SUCCESS
)
236 return (ISC_R_SUCCESS
);
241 ns_checknames_get(const cfg_obj_t
**maps
, const char *which
,
242 const cfg_obj_t
**obj
)
244 const cfg_listelt_t
*element
;
245 const cfg_obj_t
*checknames
;
246 const cfg_obj_t
*type
;
247 const cfg_obj_t
*value
;
252 return (ISC_R_NOTFOUND
);
254 if (cfg_map_get(maps
[i
], "check-names", &checknames
) == ISC_R_SUCCESS
) {
256 * Zone map entry is not a list.
258 if (checknames
!= NULL
&& !cfg_obj_islist(checknames
)) {
260 return (ISC_R_SUCCESS
);
262 for (element
= cfg_list_first(checknames
);
264 element
= cfg_list_next(element
)) {
265 value
= cfg_listelt_value(element
);
266 type
= cfg_tuple_get(value
, "type");
267 if (strcasecmp(cfg_obj_asstring(type
), which
) == 0) {
268 *obj
= cfg_tuple_get(value
, "mode");
269 return (ISC_R_SUCCESS
);
278 ns_config_listcount(const cfg_obj_t
*list
) {
279 const cfg_listelt_t
*e
;
282 for (e
= cfg_list_first(list
); e
!= NULL
; e
= cfg_list_next(e
))
289 ns_config_getclass(const cfg_obj_t
*classobj
, dns_rdataclass_t defclass
,
290 dns_rdataclass_t
*classp
) {
294 if (!cfg_obj_isstring(classobj
)) {
296 return (ISC_R_SUCCESS
);
298 DE_CONST(cfg_obj_asstring(classobj
), r
.base
);
299 r
.length
= strlen(r
.base
);
300 result
= dns_rdataclass_fromtext(classp
, &r
);
301 if (result
!= ISC_R_SUCCESS
)
302 cfg_obj_log(classobj
, ns_g_lctx
, ISC_LOG_ERROR
,
303 "unknown class '%s'", r
.base
);
308 ns_config_gettype(const cfg_obj_t
*typeobj
, dns_rdatatype_t deftype
,
309 dns_rdatatype_t
*typep
) {
313 if (!cfg_obj_isstring(typeobj
)) {
315 return (ISC_R_SUCCESS
);
317 DE_CONST(cfg_obj_asstring(typeobj
), r
.base
);
318 r
.length
= strlen(r
.base
);
319 result
= dns_rdatatype_fromtext(typep
, &r
);
320 if (result
!= ISC_R_SUCCESS
)
321 cfg_obj_log(typeobj
, ns_g_lctx
, ISC_LOG_ERROR
,
322 "unknown type '%s'", r
.base
);
327 ns_config_getzonetype(const cfg_obj_t
*zonetypeobj
) {
328 dns_zonetype_t ztype
= dns_zone_none
;
331 str
= cfg_obj_asstring(zonetypeobj
);
332 if (strcasecmp(str
, "master") == 0)
333 ztype
= dns_zone_master
;
334 else if (strcasecmp(str
, "slave") == 0)
335 ztype
= dns_zone_slave
;
336 else if (strcasecmp(str
, "stub") == 0)
337 ztype
= dns_zone_stub
;
344 ns_config_getiplist(const cfg_obj_t
*config
, const cfg_obj_t
*list
,
345 in_port_t defport
, isc_mem_t
*mctx
,
346 isc_sockaddr_t
**addrsp
, isc_uint32_t
*countp
)
349 const cfg_obj_t
*addrlist
;
350 const cfg_obj_t
*portobj
;
351 const cfg_listelt_t
*element
;
352 isc_sockaddr_t
*addrs
;
356 INSIST(addrsp
!= NULL
&& *addrsp
== NULL
);
357 INSIST(countp
!= NULL
);
359 addrlist
= cfg_tuple_get(list
, "addresses");
360 count
= ns_config_listcount(addrlist
);
362 portobj
= cfg_tuple_get(list
, "port");
363 if (cfg_obj_isuint32(portobj
)) {
364 isc_uint32_t val
= cfg_obj_asuint32(portobj
);
365 if (val
> ISC_UINT16_MAX
) {
366 cfg_obj_log(portobj
, ns_g_lctx
, ISC_LOG_ERROR
,
367 "port '%u' out of range", val
);
368 return (ISC_R_RANGE
);
370 port
= (in_port_t
) val
;
371 } else if (defport
!= 0)
374 result
= ns_config_getport(config
, &port
);
375 if (result
!= ISC_R_SUCCESS
)
379 addrs
= isc_mem_get(mctx
, count
* sizeof(isc_sockaddr_t
));
381 return (ISC_R_NOMEMORY
);
383 for (element
= cfg_list_first(addrlist
);
385 element
= cfg_list_next(element
), i
++)
388 addrs
[i
] = *cfg_obj_assockaddr(cfg_listelt_value(element
));
389 if (isc_sockaddr_getport(&addrs
[i
]) == 0)
390 isc_sockaddr_setport(&addrs
[i
], port
);
397 return (ISC_R_SUCCESS
);
401 ns_config_putiplist(isc_mem_t
*mctx
, isc_sockaddr_t
**addrsp
,
404 INSIST(addrsp
!= NULL
&& *addrsp
!= NULL
);
406 isc_mem_put(mctx
, *addrsp
, count
* sizeof(isc_sockaddr_t
));
411 get_masters_def(const cfg_obj_t
*cctx
, const char *name
,
412 const cfg_obj_t
**ret
)
415 const cfg_obj_t
*masters
= NULL
;
416 const cfg_listelt_t
*elt
;
418 result
= cfg_map_get(cctx
, "masters", &masters
);
419 if (result
!= ISC_R_SUCCESS
)
421 for (elt
= cfg_list_first(masters
);
423 elt
= cfg_list_next(elt
)) {
424 const cfg_obj_t
*list
;
425 const char *listname
;
427 list
= cfg_listelt_value(elt
);
428 listname
= cfg_obj_asstring(cfg_tuple_get(list
, "name"));
430 if (strcasecmp(listname
, name
) == 0) {
432 return (ISC_R_SUCCESS
);
435 return (ISC_R_NOTFOUND
);
439 ns_config_getipandkeylist(const cfg_obj_t
*config
, const cfg_obj_t
*list
,
440 isc_mem_t
*mctx
, isc_sockaddr_t
**addrsp
,
441 dns_name_t
***keysp
, isc_uint32_t
*countp
)
443 isc_uint32_t addrcount
= 0, keycount
= 0, i
= 0;
444 isc_uint32_t listcount
= 0, l
= 0, j
;
445 isc_uint32_t stackcount
= 0, pushed
= 0;
447 const cfg_listelt_t
*element
;
448 const cfg_obj_t
*addrlist
;
449 const cfg_obj_t
*portobj
;
451 dns_fixedname_t fname
;
452 isc_sockaddr_t
*addrs
= NULL
;
453 dns_name_t
**keys
= NULL
;
454 struct { const char *name
; } *lists
= NULL
;
456 const cfg_listelt_t
*element
;
460 REQUIRE(addrsp
!= NULL
&& *addrsp
== NULL
);
461 REQUIRE(keysp
!= NULL
&& *keysp
== NULL
);
462 REQUIRE(countp
!= NULL
);
465 addrlist
= cfg_tuple_get(list
, "addresses");
466 portobj
= cfg_tuple_get(list
, "port");
467 if (cfg_obj_isuint32(portobj
)) {
468 isc_uint32_t val
= cfg_obj_asuint32(portobj
);
469 if (val
> ISC_UINT16_MAX
) {
470 cfg_obj_log(portobj
, ns_g_lctx
, ISC_LOG_ERROR
,
471 "port '%u' out of range", val
);
472 result
= ISC_R_RANGE
;
475 port
= (in_port_t
) val
;
477 result
= ns_config_getport(config
, &port
);
478 if (result
!= ISC_R_SUCCESS
)
482 result
= ISC_R_NOMEMORY
;
484 element
= cfg_list_first(addrlist
);
488 element
= cfg_list_next(element
))
490 const cfg_obj_t
*addr
;
491 const cfg_obj_t
*key
;
495 addr
= cfg_tuple_get(cfg_listelt_value(element
),
497 key
= cfg_tuple_get(cfg_listelt_value(element
), "key");
499 if (!cfg_obj_issockaddr(addr
)) {
500 const char *listname
= cfg_obj_asstring(addr
);
501 isc_result_t tresult
;
504 if (listcount
== l
) {
506 isc_uint32_t newlen
= listcount
+ 16;
507 size_t newsize
, oldsize
;
509 newsize
= newlen
* sizeof(*lists
);
510 oldsize
= listcount
* sizeof(*lists
);
511 new = isc_mem_get(mctx
, newsize
);
514 if (listcount
!= 0) {
515 memcpy(new, lists
, oldsize
);
516 isc_mem_put(mctx
, lists
, oldsize
);
522 for (j
= 0; j
< l
; j
++)
523 if (strcasecmp(lists
[j
].name
, listname
) == 0)
527 tresult
= get_masters_def(config
, listname
, &list
);
528 if (tresult
== ISC_R_NOTFOUND
) {
529 cfg_obj_log(addr
, ns_g_lctx
, ISC_LOG_ERROR
,
530 "masters \"%s\" not found", listname
);
535 if (tresult
!= ISC_R_SUCCESS
)
537 lists
[l
++].name
= listname
;
539 if (stackcount
== pushed
) {
541 isc_uint32_t newlen
= stackcount
+ 16;
542 size_t newsize
, oldsize
;
544 newsize
= newlen
* sizeof(*stack
);
545 oldsize
= stackcount
* sizeof(*stack
);
546 new = isc_mem_get(mctx
, newsize
);
549 if (stackcount
!= 0) {
550 memcpy(new, stack
, oldsize
);
551 isc_mem_put(mctx
, stack
, oldsize
);
557 * We want to resume processing this list on the
560 stack
[pushed
].element
= cfg_list_next(element
);
561 stack
[pushed
].port
= port
;
566 if (i
== addrcount
) {
568 isc_uint32_t newlen
= addrcount
+ 16;
569 size_t newsize
, oldsize
;
571 newsize
= newlen
* sizeof(isc_sockaddr_t
);
572 oldsize
= addrcount
* sizeof(isc_sockaddr_t
);
573 new = isc_mem_get(mctx
, newsize
);
576 if (addrcount
!= 0) {
577 memcpy(new, addrs
, oldsize
);
578 isc_mem_put(mctx
, addrs
, oldsize
);
583 newsize
= newlen
* sizeof(dns_name_t
*);
584 oldsize
= keycount
* sizeof(dns_name_t
*);
585 new = isc_mem_get(mctx
, newsize
);
589 memcpy(new, keys
, oldsize
);
590 isc_mem_put(mctx
, keys
, oldsize
);
596 addrs
[i
] = *cfg_obj_assockaddr(addr
);
597 if (isc_sockaddr_getport(&addrs
[i
]) == 0)
598 isc_sockaddr_setport(&addrs
[i
], port
);
600 if (!cfg_obj_isstring(key
)) {
604 keys
[i
] = isc_mem_get(mctx
, sizeof(dns_name_t
));
607 dns_name_init(keys
[i
], NULL
);
609 keystr
= cfg_obj_asstring(key
);
610 isc_buffer_init(&b
, keystr
, strlen(keystr
));
611 isc_buffer_add(&b
, strlen(keystr
));
612 dns_fixedname_init(&fname
);
613 result
= dns_name_fromtext(dns_fixedname_name(&fname
), &b
,
614 dns_rootname
, ISC_FALSE
, NULL
);
615 if (result
!= ISC_R_SUCCESS
)
617 result
= dns_name_dup(dns_fixedname_name(&fname
), mctx
,
619 if (result
!= ISC_R_SUCCESS
)
625 element
= stack
[pushed
].element
;
626 port
= stack
[pushed
].port
;
631 size_t newsize
, oldsize
;
633 newsize
= i
* sizeof(isc_sockaddr_t
);
634 oldsize
= addrcount
* sizeof(isc_sockaddr_t
);
636 new = isc_mem_get(mctx
, newsize
);
639 memcpy(new, addrs
, newsize
);
642 isc_mem_put(mctx
, addrs
, oldsize
);
646 newsize
= i
* sizeof(dns_name_t
*);
647 oldsize
= keycount
* sizeof(dns_name_t
*);
649 new = isc_mem_get(mctx
, newsize
);
652 memcpy(new, keys
, newsize
);
655 isc_mem_put(mctx
, keys
, oldsize
);
661 isc_mem_put(mctx
, lists
, listcount
* sizeof(*lists
));
663 isc_mem_put(mctx
, stack
, stackcount
* sizeof(*stack
));
665 INSIST(keycount
== addrcount
);
671 return (ISC_R_SUCCESS
);
675 isc_mem_put(mctx
, addrs
, addrcount
* sizeof(isc_sockaddr_t
));
677 for (j
= 0; j
<= i
; j
++) {
680 if (dns_name_dynamic(keys
[j
]))
681 dns_name_free(keys
[j
], mctx
);
682 isc_mem_put(mctx
, keys
[j
], sizeof(dns_name_t
));
684 isc_mem_put(mctx
, keys
, keycount
* sizeof(dns_name_t
*));
687 isc_mem_put(mctx
, lists
, listcount
* sizeof(*lists
));
689 isc_mem_put(mctx
, stack
, stackcount
* sizeof(*stack
));
694 ns_config_putipandkeylist(isc_mem_t
*mctx
, isc_sockaddr_t
**addrsp
,
695 dns_name_t
***keysp
, isc_uint32_t count
)
698 dns_name_t
**keys
= *keysp
;
700 INSIST(addrsp
!= NULL
&& *addrsp
!= NULL
);
702 isc_mem_put(mctx
, *addrsp
, count
* sizeof(isc_sockaddr_t
));
703 for (i
= 0; i
< count
; i
++) {
706 if (dns_name_dynamic(keys
[i
]))
707 dns_name_free(keys
[i
], mctx
);
708 isc_mem_put(mctx
, keys
[i
], sizeof(dns_name_t
));
710 isc_mem_put(mctx
, *keysp
, count
* sizeof(dns_name_t
*));
716 ns_config_getport(const cfg_obj_t
*config
, in_port_t
*portp
) {
717 const cfg_obj_t
*maps
[3];
718 const cfg_obj_t
*options
= NULL
;
719 const cfg_obj_t
*portobj
= NULL
;
723 (void)cfg_map_get(config
, "options", &options
);
727 maps
[i
++] = ns_g_defaults
;
730 result
= ns_config_get(maps
, "port", &portobj
);
731 INSIST(result
== ISC_R_SUCCESS
);
732 if (cfg_obj_asuint32(portobj
) >= ISC_UINT16_MAX
) {
733 cfg_obj_log(portobj
, ns_g_lctx
, ISC_LOG_ERROR
,
734 "port '%u' out of range",
735 cfg_obj_asuint32(portobj
));
736 return (ISC_R_RANGE
);
738 *portp
= (in_port_t
)cfg_obj_asuint32(portobj
);
739 return (ISC_R_SUCCESS
);
742 struct keyalgorithms
{
744 enum { hmacnone
, hmacmd5
, hmacsha1
, hmacsha224
,
745 hmacsha256
, hmacsha384
, hmacsha512
} hmac
;
748 { "hmac-md5", hmacmd5
, 128 },
749 { "hmac-md5.sig-alg.reg.int", hmacmd5
, 0 },
750 { "hmac-md5.sig-alg.reg.int.", hmacmd5
, 0 },
751 { "hmac-sha1", hmacsha1
, 160 },
752 { "hmac-sha224", hmacsha224
, 224 },
753 { "hmac-sha256", hmacsha256
, 256 },
754 { "hmac-sha384", hmacsha384
, 384 },
755 { "hmac-sha512", hmacsha512
, 512 },
756 { NULL
, hmacnone
, 0 }
760 ns_config_getkeyalgorithm(const char *str
, dns_name_t
**name
,
761 isc_uint16_t
*digestbits
)
768 for (i
= 0; algorithms
[i
].str
!= NULL
; i
++) {
769 len
= strlen(algorithms
[i
].str
);
770 if (strncasecmp(algorithms
[i
].str
, str
, len
) == 0 &&
772 (algorithms
[i
].size
!= 0 && str
[len
] == '-')))
775 if (algorithms
[i
].str
== NULL
)
776 return (ISC_R_NOTFOUND
);
777 if (str
[len
] == '-') {
778 result
= isc_parse_uint16(&bits
, str
+ len
+ 1, 10);
779 if (result
!= ISC_R_SUCCESS
)
781 if (bits
> algorithms
[i
].size
)
782 return (ISC_R_RANGE
);
783 } else if (algorithms
[i
].size
== 0)
786 bits
= algorithms
[i
].size
;
789 switch (algorithms
[i
].hmac
) {
790 case hmacmd5
: *name
= dns_tsig_hmacmd5_name
; break;
791 case hmacsha1
: *name
= dns_tsig_hmacsha1_name
; break;
792 case hmacsha224
: *name
= dns_tsig_hmacsha224_name
; break;
793 case hmacsha256
: *name
= dns_tsig_hmacsha256_name
; break;
794 case hmacsha384
: *name
= dns_tsig_hmacsha384_name
; break;
795 case hmacsha512
: *name
= dns_tsig_hmacsha512_name
; break;
800 if (digestbits
!= NULL
)
802 return (ISC_R_SUCCESS
);