2 * Copyright (C) 2004, 2006-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.11.2.4.8.36.4.3 2008/07/23 23:47:49 tbox Exp $ */
24 #include <isc/buffer.h>
27 #include <isc/region.h>
28 #include <isc/result.h>
29 #include <isc/sockaddr.h>
30 #include <isc/string.h>
33 #include <isccfg/namedconf.h>
35 #include <dns/fixedname.h>
37 #include <dns/rdataclass.h>
38 #include <dns/rdatatype.h>
42 #include <named/config.h>
43 #include <named/globals.h>
45 static char defaultconf[] = "\
47 # blackhole {none;};\n"
49 " coresize default;\n\
54 " deallocate-on-exit true;\n\
56 dump-file \"named_dump.db\";\n\
58 has-old-clients false;\n\
59 heartbeat-interval 60;\n\
60 host-statistics no;\n\
61 interface-interval 60;\n\
63 listen-on-v6 {none;};\n\
64 match-mapped-addresses no;\n\
65 memstatistics-file \"named.memstats\";\n\
66 multiple-cnames no;\n\
67 # named-xfer <obsolete>;\n\
68 # pid-file \"" NS_LOCALSTATEDIR "/named.pid\"; /* or /lwresd.pid */\n\
70 recursing-file \"named.recursing\";\n\
74 random-device \"" PATH_RANDOMDEV "\";\n\
78 recursive-clients 1000;\n\
79 rrset-order {order cyclic;};\n\
81 serial-query-rate 20;\n\
83 statistics-file \"named.stats\";\n\
84 statistics-interval 60;\n\
86 tcp-listen-queue 3;\n\
87 # tkey-dhkey <none>\n\
88 # tkey-gssapi-credential <none>\n\
89 # tkey-domain <none>\n\
90 transfers-per-ns 2;\n\
93 treat-cr-as-space true;\n\
96 edns-udp-size 4096;\n\
97 reserved-sockets 512;\n\
100 allow-notify {none;};\n\
101 allow-update-forwarding {none;};\n\
102 allow-recursion {any;};\n\
103 # allow-v6-synthesis <obsolete>;\n\
106 auth-nxdomain false;\n\
107 minimal-responses false;\n\
109 provide-ixfr true;\n\
110 request-ixfr true;\n\
113 additional-from-auth true;\n\
114 additional-from-cache true;\n\
115 query-source address *;\n\
116 query-source-v6 address *;\n\
118 notify-source-v6 *;\n\
119 cleaning-interval 60;\n\
122 max-ncache-ttl 10800; /* 3 hours */\n\
123 max-cache-ttl 604800; /* 1 week */\n\
124 transfer-format many-answers;\n\
126 check-names master fail;\n\
127 check-names slave warn;\n\
128 check-names response ignore;\n\
129 dnssec-enable no; /* Make yes for 9.4. */ \n\
133 allow-query {any;};\n\
134 allow-transfer {any;};\n\
136 # also-notify <none>\n\
139 # forwarders <none>\n\
140 maintain-ixfr-base no;\n\
141 # max-ixfr-log-size <obsolete>\n\
142 transfer-source *;\n\
143 transfer-source-v6 *;\n\
144 alt-transfer-source *;\n\
145 alt-transfer-source-v6 *;\n\
146 max-transfer-time-in 120;\n\
147 max-transfer-time-out 120;\n\
148 max-transfer-idle-in 60;\n\
149 max-transfer-idle-out 60;\n\
150 max-retry-time 1209600; /* 2 weeks */\n\
151 min-retry-time 500;\n\
152 max-refresh-time 2419200; /* 4 weeks */\n\
153 min-refresh-time 300;\n\
155 sig-validity-interval 30; /* days */\n\
156 zone-statistics false;\n\
157 max-journal-size unlimited;\n\
158 ixfr-from-differences false;\n\
163 # Zones in the \"_bind\" view are NOT counted in the count of zones.\n\
165 view \"_bind\" chaos {\n\
169 zone \"version.bind\" chaos {\n\
171 database \"_builtin version\";\n\
174 zone \"hostname.bind\" chaos {\n\
176 database \"_builtin hostname\";\n\
179 zone \"authors.bind\" chaos {\n\
181 database \"_builtin authors\";\n\
183 zone \"id.server\" chaos {\n\
185 database \"_builtin id\";\n\
191 ns_config_parsedefaults(cfg_parser_t *parser, cfg_obj_t **conf) {
194 isc_buffer_init(&b, defaultconf, sizeof(defaultconf) - 1);
195 isc_buffer_add(&b, sizeof(defaultconf) - 1);
196 return (cfg_parse_buffer(parser, &b, &cfg_type_namedconf, conf));
200 ns_config_get(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
205 return (ISC_R_NOTFOUND);
206 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
207 return (ISC_R_SUCCESS);
212 ns_checknames_get(const cfg_obj_t **maps, const char *which,
213 const cfg_obj_t **obj)
215 const cfg_listelt_t *element;
216 const cfg_obj_t *checknames;
217 const cfg_obj_t *type;
218 const cfg_obj_t *value;
223 return (ISC_R_NOTFOUND);
225 if (cfg_map_get(maps[i], "check-names", &checknames) == ISC_R_SUCCESS) {
227 * Zone map entry is not a list.
229 if (checknames != NULL && !cfg_obj_islist(checknames)) {
231 return (ISC_R_SUCCESS);
233 for (element = cfg_list_first(checknames);
235 element = cfg_list_next(element)) {
236 value = cfg_listelt_value(element);
237 type = cfg_tuple_get(value, "type");
238 if (strcasecmp(cfg_obj_asstring(type), which) == 0) {
239 *obj = cfg_tuple_get(value, "mode");
240 return (ISC_R_SUCCESS);
249 ns_config_listcount(const cfg_obj_t *list) {
250 const cfg_listelt_t *e;
253 for (e = cfg_list_first(list); e != NULL; e = cfg_list_next(e))
260 ns_config_getclass(const cfg_obj_t *classobj, dns_rdataclass_t defclass,
261 dns_rdataclass_t *classp) {
266 if (!cfg_obj_isstring(classobj)) {
268 return (ISC_R_SUCCESS);
270 str = cfg_obj_asstring(classobj);
271 DE_CONST(str, r.base);
272 r.length = strlen(str);
273 result = dns_rdataclass_fromtext(classp, &r);
274 if (result != ISC_R_SUCCESS)
275 cfg_obj_log(classobj, ns_g_lctx, ISC_LOG_ERROR,
276 "unknown class '%s'", str);
281 ns_config_gettype(const cfg_obj_t *typeobj, dns_rdatatype_t deftype,
282 dns_rdatatype_t *typep) {
287 if (!cfg_obj_isstring(typeobj)) {
289 return (ISC_R_SUCCESS);
291 str = cfg_obj_asstring(typeobj);
292 DE_CONST(str, r.base);
293 r.length = strlen(str);
294 result = dns_rdatatype_fromtext(typep, &r);
295 if (result != ISC_R_SUCCESS)
296 cfg_obj_log(typeobj, ns_g_lctx, ISC_LOG_ERROR,
297 "unknown type '%s'", str);
302 ns_config_getzonetype(const cfg_obj_t *zonetypeobj) {
303 dns_zonetype_t ztype = dns_zone_none;
306 str = cfg_obj_asstring(zonetypeobj);
307 if (strcasecmp(str, "master") == 0)
308 ztype = dns_zone_master;
309 else if (strcasecmp(str, "slave") == 0)
310 ztype = dns_zone_slave;
311 else if (strcasecmp(str, "stub") == 0)
312 ztype = dns_zone_stub;
319 ns_config_getiplist(const cfg_obj_t *config, const cfg_obj_t *list,
320 in_port_t defport, isc_mem_t *mctx,
321 isc_sockaddr_t **addrsp, isc_uint32_t *countp)
324 const cfg_obj_t *addrlist;
325 const cfg_obj_t *portobj;
326 const cfg_listelt_t *element;
327 isc_sockaddr_t *addrs;
331 INSIST(addrsp != NULL && *addrsp == NULL);
332 INSIST(countp != NULL);
334 addrlist = cfg_tuple_get(list, "addresses");
335 count = ns_config_listcount(addrlist);
337 portobj = cfg_tuple_get(list, "port");
338 if (cfg_obj_isuint32(portobj)) {
339 isc_uint32_t val = cfg_obj_asuint32(portobj);
340 if (val > ISC_UINT16_MAX) {
341 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
342 "port '%u' out of range", val);
343 return (ISC_R_RANGE);
345 port = (in_port_t) val;
346 } else if (defport != 0)
349 result = ns_config_getport(config, &port);
350 if (result != ISC_R_SUCCESS)
354 addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));
356 return (ISC_R_NOMEMORY);
358 for (element = cfg_list_first(addrlist);
360 element = cfg_list_next(element), i++)
363 addrs[i] = *cfg_obj_assockaddr(cfg_listelt_value(element));
364 if (isc_sockaddr_getport(&addrs[i]) == 0)
365 isc_sockaddr_setport(&addrs[i], port);
372 return (ISC_R_SUCCESS);
376 ns_config_putiplist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
379 INSIST(addrsp != NULL && *addrsp != NULL);
381 isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
386 get_masters_def(const cfg_obj_t *cctx, const char *name,
387 const cfg_obj_t **ret)
390 const cfg_obj_t *masters = NULL;
391 const cfg_listelt_t *elt;
393 result = cfg_map_get(cctx, "masters", &masters);
394 if (result != ISC_R_SUCCESS)
396 for (elt = cfg_list_first(masters);
398 elt = cfg_list_next(elt)) {
399 const cfg_obj_t *list;
400 const char *listname;
402 list = cfg_listelt_value(elt);
403 listname = cfg_obj_asstring(cfg_tuple_get(list, "name"));
405 if (strcasecmp(listname, name) == 0) {
407 return (ISC_R_SUCCESS);
410 return (ISC_R_NOTFOUND);
414 ns_config_getipandkeylist(const cfg_obj_t *config, const cfg_obj_t *list,
415 isc_mem_t *mctx, isc_sockaddr_t **addrsp,
416 dns_name_t ***keysp, isc_uint32_t *countp)
418 isc_uint32_t addrcount = 0, keycount = 0, i = 0;
419 isc_uint32_t listcount = 0, l = 0, j;
420 isc_uint32_t stackcount = 0, pushed = 0;
422 const cfg_listelt_t *element;
423 const cfg_obj_t *addrlist;
424 const cfg_obj_t *portobj;
426 dns_fixedname_t fname;
427 isc_sockaddr_t *addrs = NULL;
428 dns_name_t **keys = NULL;
429 const char **lists = NULL;
431 const cfg_listelt_t *element;
435 REQUIRE(addrsp != NULL && *addrsp == NULL);
436 REQUIRE(keysp != NULL && *keysp == NULL);
437 REQUIRE(countp != NULL);
440 addrlist = cfg_tuple_get(list, "addresses");
441 portobj = cfg_tuple_get(list, "port");
442 if (cfg_obj_isuint32(portobj)) {
443 isc_uint32_t val = cfg_obj_asuint32(portobj);
444 if (val > ISC_UINT16_MAX) {
445 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
446 "port '%u' out of range", val);
447 result = ISC_R_RANGE;
450 port = (in_port_t) val;
452 result = ns_config_getport(config, &port);
453 if (result != ISC_R_SUCCESS)
457 result = ISC_R_NOMEMORY;
459 element = cfg_list_first(addrlist);
463 element = cfg_list_next(element))
465 const cfg_obj_t *addr;
466 const cfg_obj_t *key;
470 addr = cfg_tuple_get(cfg_listelt_value(element),
472 key = cfg_tuple_get(cfg_listelt_value(element), "key");
474 if (!cfg_obj_issockaddr(addr)) {
475 const char *listname = cfg_obj_asstring(addr);
476 isc_result_t tresult;
479 if (listcount == l) {
481 isc_uint32_t newlen = listcount + 16;
482 size_t newsize, oldsize;
484 newsize = newlen * sizeof(*lists);
485 oldsize = listcount * sizeof(*lists);
486 new = isc_mem_get(mctx, newsize);
489 if (listcount != 0) {
490 memcpy(new, lists, oldsize);
491 isc_mem_put(mctx, lists, oldsize);
497 for (j = 0; j < l; j++)
498 if (strcasecmp(lists[j], listname) == 0)
502 tresult = get_masters_def(config, listname, &list);
503 if (tresult == ISC_R_NOTFOUND) {
504 cfg_obj_log(addr, ns_g_lctx, ISC_LOG_ERROR,
505 "masters \"%s\" not found", listname);
510 if (tresult != ISC_R_SUCCESS)
512 lists[l++] = listname;
514 if (stackcount == pushed) {
516 isc_uint32_t newlen = stackcount + 16;
517 size_t newsize, oldsize;
519 newsize = newlen * sizeof(*stack);
520 oldsize = stackcount * sizeof(*stack);
521 new = isc_mem_get(mctx, newsize);
524 if (stackcount != 0) {
525 memcpy(new, stack, oldsize);
526 isc_mem_put(mctx, stack, oldsize);
532 * We want to resume processing this list on the
535 stack[pushed].element = cfg_list_next(element);
536 stack[pushed].port = port;
541 if (i == addrcount) {
543 isc_uint32_t newlen = addrcount + 16;
544 size_t newsize, oldsize;
546 newsize = newlen * sizeof(isc_sockaddr_t);
547 oldsize = addrcount * sizeof(isc_sockaddr_t);
548 new = isc_mem_get(mctx, newsize);
551 if (addrcount != 0) {
552 memcpy(new, addrs, oldsize);
553 isc_mem_put(mctx, addrs, oldsize);
558 newsize = newlen * sizeof(dns_name_t *);
559 oldsize = keycount * sizeof(dns_name_t *);
560 new = isc_mem_get(mctx, newsize);
564 memcpy(new, keys, oldsize);
565 isc_mem_put(mctx, keys, oldsize);
571 addrs[i] = *cfg_obj_assockaddr(addr);
572 if (isc_sockaddr_getport(&addrs[i]) == 0)
573 isc_sockaddr_setport(&addrs[i], port);
575 if (!cfg_obj_isstring(key)) {
579 keys[i] = isc_mem_get(mctx, sizeof(dns_name_t));
582 dns_name_init(keys[i], NULL);
584 keystr = cfg_obj_asstring(key);
585 isc_buffer_init(&b, keystr, strlen(keystr));
586 isc_buffer_add(&b, strlen(keystr));
587 dns_fixedname_init(&fname);
588 result = dns_name_fromtext(dns_fixedname_name(&fname), &b,
589 dns_rootname, ISC_FALSE, NULL);
590 if (result != ISC_R_SUCCESS)
592 result = dns_name_dup(dns_fixedname_name(&fname), mctx,
594 if (result != ISC_R_SUCCESS)
600 element = stack[pushed].element;
601 port = stack[pushed].port;
606 size_t newsize, oldsize;
608 newsize = i * sizeof(isc_sockaddr_t);
609 oldsize = addrcount * sizeof(isc_sockaddr_t);
611 new = isc_mem_get(mctx, newsize);
614 memcpy(new, addrs, newsize);
617 isc_mem_put(mctx, addrs, oldsize);
621 newsize = i * sizeof(dns_name_t *);
622 oldsize = keycount * sizeof(dns_name_t *);
624 new = isc_mem_get(mctx, newsize);
627 memcpy(new, keys, newsize);
630 isc_mem_put(mctx, keys, oldsize);
636 isc_mem_put(mctx, lists, listcount * sizeof(*lists));
638 isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
640 INSIST(keycount == addrcount);
646 return (ISC_R_SUCCESS);
650 isc_mem_put(mctx, addrs, addrcount * sizeof(isc_sockaddr_t));
652 for (j = 0; j <= i; j++) {
655 if (dns_name_dynamic(keys[j]))
656 dns_name_free(keys[j], mctx);
657 isc_mem_put(mctx, keys[j], sizeof(dns_name_t));
659 isc_mem_put(mctx, keys, keycount * sizeof(dns_name_t *));
662 isc_mem_put(mctx, lists, listcount * sizeof(*lists));
664 isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
669 ns_config_putipandkeylist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
670 dns_name_t ***keysp, isc_uint32_t count)
673 dns_name_t **keys = *keysp;
675 INSIST(addrsp != NULL && *addrsp != NULL);
677 isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
678 for (i = 0; i < count; i++) {
681 if (dns_name_dynamic(keys[i]))
682 dns_name_free(keys[i], mctx);
683 isc_mem_put(mctx, keys[i], sizeof(dns_name_t));
685 isc_mem_put(mctx, *keysp, count * sizeof(dns_name_t *));
691 ns_config_getport(const cfg_obj_t *config, in_port_t *portp) {
692 const cfg_obj_t *maps[3];
693 const cfg_obj_t *options = NULL;
694 const cfg_obj_t *portobj = NULL;
698 (void)cfg_map_get(config, "options", &options);
702 maps[i++] = ns_g_defaults;
705 result = ns_config_get(maps, "port", &portobj);
706 INSIST(result == ISC_R_SUCCESS);
707 if (cfg_obj_asuint32(portobj) >= ISC_UINT16_MAX) {
708 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
709 "port '%u' out of range",
710 cfg_obj_asuint32(portobj));
711 return (ISC_R_RANGE);
713 *portp = (in_port_t)cfg_obj_asuint32(portobj);
714 return (ISC_R_SUCCESS);
718 ns_config_getkeyalgorithm(const char *str, dns_name_t **name)
720 if (strcasecmp(str, "hmac-md5") == 0 ||
721 strcasecmp(str, "hmac-md5.sig-alg.reg.int") == 0 ||
722 strcasecmp(str, "hmac-md5.sig-alg.reg.int.") == 0)
725 *name = dns_tsig_hmacmd5_name;
726 return (ISC_R_SUCCESS);
728 return (ISC_R_NOTFOUND);