1 /*
2 * Copyright (C) 2004-2015 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001-2003 Internet Software Consortium.
4 *
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.
8 *
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.
16 */
17
18 /*! \file */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <isc/buffer.h>
25 #include <isc/log.h>
26 #include <isc/mem.h>
27 #include <isc/parseint.h>
28 #include <isc/region.h>
29 #include <isc/result.h>
30 #include <isc/sockaddr.h>
31 #include <isc/string.h>
32 #include <isc/util.h>
33
34 #include <isccfg/namedconf.h>
35
36 #include <dns/fixedname.h>
37 #include <dns/name.h>
38 #include <dns/rdataclass.h>
39 #include <dns/rdatatype.h>
40 #include <dns/tsig.h>
41 #include <dns/zone.h>
42
43 #include <dst/dst.h>
44
45 #include <named/config.h>
46 #include <named/globals.h>
47
48 #include "bind.keys.h"
49
50 /*% default configuration */
51 static char defaultconf[] = "\
52 options {\n\
53 # blackhole {none;};\n"
54 #ifndef WIN32
55 " coresize default;\n\
56 datasize default;\n\
57 files unlimited;\n\
58 stacksize default;\n"
59 #endif
60 "# session-keyfile \"" NS_LOCALSTATEDIR "/run/named/session.key\";\n\
61 session-keyname local-ddns;\n\
62 session-keyalg hmac-sha256;\n\
63 deallocate-on-exit true;\n\
64 # directory <none>\n\
65 dump-file \"named_dump.db\";\n\
66 fake-iquery no;\n\
67 has-old-clients false;\n\
68 heartbeat-interval 60;\n\
69 host-statistics no;\n\
70 interface-interval 60;\n\
71 listen-on {any;};\n\
72 listen-on-v6 {none;};\n\
73 match-mapped-addresses no;\n\
74 max-rsa-exponent-size 0; /* no limit */\n\
75 memstatistics-file \"named.memstats\";\n\
76 multiple-cnames no;\n\
77 # named-xfer <obsolete>;\n\
78 # pid-file \"" NS_LOCALSTATEDIR "/run/named/named.pid\"; /* or /lwresd.pid */\n\
79 bindkeys-file \"" NS_SYSCONFDIR "/bind.keys\";\n\
80 port 53;\n\
81 recursing-file \"named.recursing\";\n\
82 secroots-file \"named.secroots\";\n\
83 "
84 #ifdef PATH_RANDOMDEV
85 "\
86 random-device \"" PATH_RANDOMDEV "\";\n\
87 "
88 #endif
89 "\
90 recursive-clients 1000;\n\
91 resolver-query-timeout 10;\n\
92 rrset-order { order random; };\n\
93 serial-queries 20;\n\
94 serial-query-rate 20;\n\
95 server-id none;\n\
96 statistics-file \"named.stats\";\n\
97 statistics-interval 60;\n\
98 tcp-clients 100;\n\
99 tcp-listen-queue 10;\n\
100 # tkey-dhkey <none>\n\
101 # tkey-gssapi-credential <none>\n\
102 # tkey-domain <none>\n\
103 transfers-per-ns 2;\n\
104 transfers-in 10;\n\
105 transfers-out 10;\n\
106 # treat-cr-as-space <obsolete>;\n\
107 # use-id-pool <obsolete>;\n\
108 # use-ixfr <obsolete>;\n\
109 edns-udp-size 4096;\n\
110 max-udp-size 4096;\n\
111 request-nsid false;\n\
112 reserved-sockets 512;\n\
113 \n\
114 /* DLV */\n\
115 dnssec-lookaside . trust-anchor dlv.isc.org;\n\
116 \n\
117 /* view */\n\
118 allow-notify {none;};\n\
119 allow-update-forwarding {none;};\n\
120 allow-query-cache { localnets; localhost; };\n\
121 allow-query-cache-on { any; };\n\
122 allow-recursion { localnets; localhost; };\n\
123 allow-recursion-on { any; };\n\
124 # allow-v6-synthesis <obsolete>;\n\
125 # sortlist <none>\n\
126 # topology <none>\n\
127 auth-nxdomain false;\n\
128 minimal-responses false;\n\
129 recursion true;\n\
130 provide-ixfr true;\n\
131 request-ixfr true;\n\
132 fetch-glue no;\n\
133 rfc2308-type1 no;\n\
134 additional-from-auth true;\n\
135 additional-from-cache true;\n\
136 query-source address *;\n\
137 query-source-v6 address *;\n\
138 notify-source *;\n\
139 notify-source-v6 *;\n\
140 cleaning-interval 0; /* now meaningless */\n\
141 min-roots 2;\n\
142 lame-ttl 600;\n\
143 max-ncache-ttl 10800; /* 3 hours */\n\
144 max-cache-ttl 604800; /* 1 week */\n\
145 transfer-format many-answers;\n\
146 max-cache-size 0;\n\
147 check-names master fail;\n\
148 check-names slave warn;\n\
149 check-names response ignore;\n\
150 check-dup-records warn;\n\
151 check-mx warn;\n\
152 check-spf warn;\n\
153 acache-enable no;\n\
154 acache-cleaning-interval 60;\n\
155 max-acache-size 16M;\n\
156 dnssec-enable yes;\n\
157 dnssec-validation yes; \n\
158 dnssec-accept-expired no;\n\
159 "
160 #ifdef ENABLE_FETCHLIMIT
161 " fetches-per-server 0;\n\
162 fetches-per-zone 0;\n\
163 fetch-quota-params 100 0.1 0.3 0.7;\n\
164 "
165 #endif /* ENABLE_FETCHLIMIT */
166 " clients-per-query 10;\n\
167 max-clients-per-query 100;\n\
168 max-recursion-depth 7;\n\
169 max-recursion-queries 50;\n\
170 zero-no-soa-ttl-cache no;\n\
171 nsec3-test-zone no;\n\
172 allow-new-zones no;\n\
173 "
174 #ifdef ALLOW_FILTER_AAAA_ON_V4
175 " filter-aaaa-on-v4 no;\n\
176 filter-aaaa { any; };\n\
177 "
178 #endif
179
180 " /* zone */\n\
181 allow-query {any;};\n\
182 allow-query-on {any;};\n\
183 allow-transfer {any;};\n\
184 notify yes;\n\
185 # also-notify <none>\n\
186 notify-delay 5;\n\
187 notify-to-soa no;\n\
188 dialup no;\n\
189 # forward <none>\n\
190 # forwarders <none>\n\
191 maintain-ixfr-base no;\n\
192 # max-ixfr-log-size <obsolete>\n\
193 transfer-source *;\n\
194 transfer-source-v6 *;\n\
195 alt-transfer-source *;\n\
196 alt-transfer-source-v6 *;\n\
197 max-transfer-time-in 120;\n\
198 max-transfer-time-out 120;\n\
199 max-transfer-idle-in 60;\n\
200 max-transfer-idle-out 60;\n\
201 max-retry-time 1209600; /* 2 weeks */\n\
202 min-retry-time 500;\n\
203 max-refresh-time 2419200; /* 4 weeks */\n\
204 min-refresh-time 300;\n\
205 multi-master no;\n\
206 dnssec-secure-to-insecure no;\n\
207 sig-validity-interval 30; /* days */\n\
208 sig-signing-nodes 100;\n\
209 sig-signing-signatures 10;\n\
210 sig-signing-type 65534;\n\
211 inline-signing no;\n\
212 zone-statistics terse;\n\
213 max-journal-size unlimited;\n\
214 ixfr-from-differences false;\n\
215 check-wildcard yes;\n\
216 check-sibling yes;\n\
217 check-integrity yes;\n\
218 check-mx-cname warn;\n\
219 check-srv-cname warn;\n\
220 zero-no-soa-ttl yes;\n\
221 update-check-ksk yes;\n\
222 serial-update-method increment;\n\
223 dnssec-update-mode maintain;\n\
224 dnssec-dnskey-kskonly no;\n\
225 dnssec-loadkeys-interval 60;\n\
226 try-tcp-refresh yes; /* BIND 8 compat */\n\
227 };\n\
228 "
229
230 "#\n\
231 # Zones in the \"_bind\" view are NOT counted in the count of zones.\n\
232 #\n\
233 view \"_bind\" chaos {\n\
234 recursion no;\n\
235 notify no;\n\
236 allow-new-zones no;\n\
237 "
238 #ifdef USE_RRL
239 " # Prevent use of this zone in DNS amplified reflection DoS attacks\n\
240 rate-limit {\n\
241 responses-per-second 3;\n\
242 slip 0;\n\
243 min-table-size 10;\n\
244 };\n\
245 "
246 #endif /* USE_RRL */
247 " zone \"version.bind\" chaos {\n\
248 type master;\n\
249 database \"_builtin version\";\n\
250 };\n\
251 \n\
252 zone \"hostname.bind\" chaos {\n\
253 type master;\n\
254 database \"_builtin hostname\";\n\
255 };\n\
256 \n\
257 zone \"authors.bind\" chaos {\n\
258 type master;\n\
259 database \"_builtin authors\";\n\
260 };\n\
261 \n\
262 zone \"id.server\" chaos {\n\
263 type master;\n\
264 database \"_builtin id\";\n\
265 };\n\
266 };\n\
267 "
268 "#\n\
269 # Default trusted key(s) for builtin DLV support\n\
270 # (used if \"dnssec-lookaside auto;\" is set and\n\
271 # sysconfdir/bind.keys doesn't exist).\n\
272 #\n\
273 # BEGIN MANAGED KEYS\n"
274
275 /* Imported from bind.keys.h: */
276 MANAGED_KEYS
277
278 "# END MANAGED KEYS\n\
279 ";
280
281 isc_result_t
ns_config_parsedefaults(cfg_parser_t * parser,cfg_obj_t ** conf)282 ns_config_parsedefaults(cfg_parser_t *parser, cfg_obj_t **conf) {
283 isc_buffer_t b;
284
285 isc_buffer_init(&b, defaultconf, sizeof(defaultconf) - 1);
286 isc_buffer_add(&b, sizeof(defaultconf) - 1);
287 return (cfg_parse_buffer(parser, &b, &cfg_type_namedconf, conf));
288 }
289
290 isc_result_t
ns_config_get(const cfg_obj_t ** maps,const char * name,const cfg_obj_t ** obj)291 ns_config_get(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
292 int i;
293
294 for (i = 0;; i++) {
295 if (maps[i] == NULL)
296 return (ISC_R_NOTFOUND);
297 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
298 return (ISC_R_SUCCESS);
299 }
300 }
301
302 isc_result_t
ns_checknames_get(const cfg_obj_t ** maps,const char * which,const cfg_obj_t ** obj)303 ns_checknames_get(const cfg_obj_t **maps, const char *which,
304 const cfg_obj_t **obj)
305 {
306 const cfg_listelt_t *element;
307 const cfg_obj_t *checknames;
308 const cfg_obj_t *type;
309 const cfg_obj_t *value;
310 int i;
311
312 for (i = 0;; i++) {
313 if (maps[i] == NULL)
314 return (ISC_R_NOTFOUND);
315 checknames = NULL;
316 if (cfg_map_get(maps[i], "check-names",
317 &checknames) == ISC_R_SUCCESS) {
318 /*
319 * Zone map entry is not a list.
320 */
321 if (checknames != NULL && !cfg_obj_islist(checknames)) {
322 *obj = checknames;
323 return (ISC_R_SUCCESS);
324 }
325 for (element = cfg_list_first(checknames);
326 element != NULL;
327 element = cfg_list_next(element)) {
328 value = cfg_listelt_value(element);
329 type = cfg_tuple_get(value, "type");
330 if (strcasecmp(cfg_obj_asstring(type),
331 which) == 0) {
332 *obj = cfg_tuple_get(value, "mode");
333 return (ISC_R_SUCCESS);
334 }
335 }
336
337 }
338 }
339 }
340
341 int
ns_config_listcount(const cfg_obj_t * list)342 ns_config_listcount(const cfg_obj_t *list) {
343 const cfg_listelt_t *e;
344 int i = 0;
345
346 for (e = cfg_list_first(list); e != NULL; e = cfg_list_next(e))
347 i++;
348
349 return (i);
350 }
351
352 isc_result_t
ns_config_getclass(const cfg_obj_t * classobj,dns_rdataclass_t defclass,dns_rdataclass_t * classp)353 ns_config_getclass(const cfg_obj_t *classobj, dns_rdataclass_t defclass,
354 dns_rdataclass_t *classp) {
355 isc_textregion_t r;
356 isc_result_t result;
357
358 if (!cfg_obj_isstring(classobj)) {
359 *classp = defclass;
360 return (ISC_R_SUCCESS);
361 }
362 DE_CONST(cfg_obj_asstring(classobj), r.base);
363 r.length = strlen(r.base);
364 result = dns_rdataclass_fromtext(classp, &r);
365 if (result != ISC_R_SUCCESS)
366 cfg_obj_log(classobj, ns_g_lctx, ISC_LOG_ERROR,
367 "unknown class '%s'", r.base);
368 return (result);
369 }
370
371 isc_result_t
ns_config_gettype(const cfg_obj_t * typeobj,dns_rdatatype_t deftype,dns_rdatatype_t * typep)372 ns_config_gettype(const cfg_obj_t *typeobj, dns_rdatatype_t deftype,
373 dns_rdatatype_t *typep) {
374 isc_textregion_t r;
375 isc_result_t result;
376
377 if (!cfg_obj_isstring(typeobj)) {
378 *typep = deftype;
379 return (ISC_R_SUCCESS);
380 }
381 DE_CONST(cfg_obj_asstring(typeobj), r.base);
382 r.length = strlen(r.base);
383 result = dns_rdatatype_fromtext(typep, &r);
384 if (result != ISC_R_SUCCESS)
385 cfg_obj_log(typeobj, ns_g_lctx, ISC_LOG_ERROR,
386 "unknown type '%s'", r.base);
387 return (result);
388 }
389
390 dns_zonetype_t
ns_config_getzonetype(const cfg_obj_t * zonetypeobj)391 ns_config_getzonetype(const cfg_obj_t *zonetypeobj) {
392 dns_zonetype_t ztype = dns_zone_none;
393 const char *str;
394
395 str = cfg_obj_asstring(zonetypeobj);
396 if (strcasecmp(str, "master") == 0)
397 ztype = dns_zone_master;
398 else if (strcasecmp(str, "slave") == 0)
399 ztype = dns_zone_slave;
400 else if (strcasecmp(str, "stub") == 0)
401 ztype = dns_zone_stub;
402 else if (strcasecmp(str, "static-stub") == 0)
403 ztype = dns_zone_staticstub;
404 else if (strcasecmp(str, "redirect") == 0)
405 ztype = dns_zone_redirect;
406 else
407 INSIST(0);
408 return (ztype);
409 }
410
411 isc_result_t
ns_config_getiplist(const cfg_obj_t * config,const cfg_obj_t * list,in_port_t defport,isc_mem_t * mctx,isc_sockaddr_t ** addrsp,isc_uint32_t * countp)412 ns_config_getiplist(const cfg_obj_t *config, const cfg_obj_t *list,
413 in_port_t defport, isc_mem_t *mctx,
414 isc_sockaddr_t **addrsp, isc_uint32_t *countp)
415 {
416 int count, i = 0;
417 const cfg_obj_t *addrlist;
418 const cfg_obj_t *portobj;
419 const cfg_listelt_t *element;
420 isc_sockaddr_t *addrs;
421 in_port_t port;
422 isc_result_t result;
423
424 INSIST(addrsp != NULL && *addrsp == NULL);
425 INSIST(countp != NULL);
426
427 addrlist = cfg_tuple_get(list, "addresses");
428 count = ns_config_listcount(addrlist);
429
430 portobj = cfg_tuple_get(list, "port");
431 if (cfg_obj_isuint32(portobj)) {
432 isc_uint32_t val = cfg_obj_asuint32(portobj);
433 if (val > ISC_UINT16_MAX) {
434 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
435 "port '%u' out of range", val);
436 return (ISC_R_RANGE);
437 }
438 port = (in_port_t) val;
439 } else if (defport != 0)
440 port = defport;
441 else {
442 result = ns_config_getport(config, &port);
443 if (result != ISC_R_SUCCESS)
444 return (result);
445 }
446
447 addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));
448 if (addrs == NULL)
449 return (ISC_R_NOMEMORY);
450
451 for (element = cfg_list_first(addrlist);
452 element != NULL;
453 element = cfg_list_next(element), i++)
454 {
455 INSIST(i < count);
456 addrs[i] = *cfg_obj_assockaddr(cfg_listelt_value(element));
457 if (isc_sockaddr_getport(&addrs[i]) == 0)
458 isc_sockaddr_setport(&addrs[i], port);
459 }
460 INSIST(i == count);
461
462 *addrsp = addrs;
463 *countp = count;
464
465 return (ISC_R_SUCCESS);
466 }
467
468 void
ns_config_putiplist(isc_mem_t * mctx,isc_sockaddr_t ** addrsp,isc_uint32_t count)469 ns_config_putiplist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
470 isc_uint32_t count)
471 {
472 INSIST(addrsp != NULL && *addrsp != NULL);
473
474 isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
475 *addrsp = NULL;
476 }
477
478 static isc_result_t
get_masters_def(const cfg_obj_t * cctx,const char * name,const cfg_obj_t ** ret)479 get_masters_def(const cfg_obj_t *cctx, const char *name,
480 const cfg_obj_t **ret)
481 {
482 isc_result_t result;
483 const cfg_obj_t *masters = NULL;
484 const cfg_listelt_t *elt;
485
486 result = cfg_map_get(cctx, "masters", &masters);
487 if (result != ISC_R_SUCCESS)
488 return (result);
489 for (elt = cfg_list_first(masters);
490 elt != NULL;
491 elt = cfg_list_next(elt)) {
492 const cfg_obj_t *list;
493 const char *listname;
494
495 list = cfg_listelt_value(elt);
496 listname = cfg_obj_asstring(cfg_tuple_get(list, "name"));
497
498 if (strcasecmp(listname, name) == 0) {
499 *ret = list;
500 return (ISC_R_SUCCESS);
501 }
502 }
503 return (ISC_R_NOTFOUND);
504 }
505
506 isc_result_t
ns_config_getipandkeylist(const cfg_obj_t * config,const cfg_obj_t * list,isc_mem_t * mctx,isc_sockaddr_t ** addrsp,dns_name_t *** keysp,isc_uint32_t * countp)507 ns_config_getipandkeylist(const cfg_obj_t *config, const cfg_obj_t *list,
508 isc_mem_t *mctx, isc_sockaddr_t **addrsp,
509 dns_name_t ***keysp, isc_uint32_t *countp)
510 {
511 isc_uint32_t addrcount = 0, keycount = 0, i = 0;
512 isc_uint32_t listcount = 0, l = 0, j;
513 isc_uint32_t stackcount = 0, pushed = 0;
514 isc_result_t result;
515 const cfg_listelt_t *element;
516 const cfg_obj_t *addrlist;
517 const cfg_obj_t *portobj;
518 in_port_t port;
519 dns_fixedname_t fname;
520 isc_sockaddr_t *addrs = NULL;
521 dns_name_t **keys = NULL;
522 struct { const char *name; } *lists = NULL;
523 struct {
524 const cfg_listelt_t *element;
525 in_port_t port;
526 } *stack = NULL;
527
528 REQUIRE(addrsp != NULL && *addrsp == NULL);
529 REQUIRE(keysp != NULL && *keysp == NULL);
530 REQUIRE(countp != NULL);
531
532 /*
533 * Get system defaults.
534 */
535 result = ns_config_getport(config, &port);
536 if (result != ISC_R_SUCCESS)
537 goto cleanup;
538
539 newlist:
540 addrlist = cfg_tuple_get(list, "addresses");
541 portobj = cfg_tuple_get(list, "port");
542 if (cfg_obj_isuint32(portobj)) {
543 isc_uint32_t val = cfg_obj_asuint32(portobj);
544 if (val > ISC_UINT16_MAX) {
545 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
546 "port '%u' out of range", val);
547 result = ISC_R_RANGE;
548 goto cleanup;
549 }
550 port = (in_port_t) val;
551 }
552
553 result = ISC_R_NOMEMORY;
554
555 element = cfg_list_first(addrlist);
556 resume:
557 for ( ;
558 element != NULL;
559 element = cfg_list_next(element))
560 {
561 const cfg_obj_t *addr;
562 const cfg_obj_t *key;
563 const char *keystr;
564 isc_buffer_t b;
565
566 addr = cfg_tuple_get(cfg_listelt_value(element),
567 "masterselement");
568 key = cfg_tuple_get(cfg_listelt_value(element), "key");
569
570 if (!cfg_obj_issockaddr(addr)) {
571 const char *listname = cfg_obj_asstring(addr);
572 isc_result_t tresult;
573
574 /* Grow lists? */
575 if (listcount == l) {
576 void * new;
577 isc_uint32_t newlen = listcount + 16;
578 size_t newsize, oldsize;
579
580 newsize = newlen * sizeof(*lists);
581 oldsize = listcount * sizeof(*lists);
582 new = isc_mem_get(mctx, newsize);
583 if (new == NULL)
584 goto cleanup;
585 if (listcount != 0) {
586 memmove(new, lists, oldsize);
587 isc_mem_put(mctx, lists, oldsize);
588 }
589 lists = new;
590 listcount = newlen;
591 }
592 /* Seen? */
593 for (j = 0; j < l; j++)
594 if (strcasecmp(lists[j].name, listname) == 0)
595 break;
596 if (j < l)
597 continue;
598 tresult = get_masters_def(config, listname, &list);
599 if (tresult == ISC_R_NOTFOUND) {
600 cfg_obj_log(addr, ns_g_lctx, ISC_LOG_ERROR,
601 "masters \"%s\" not found", listname);
602
603 result = tresult;
604 goto cleanup;
605 }
606 if (tresult != ISC_R_SUCCESS)
607 goto cleanup;
608 lists[l++].name = listname;
609 /* Grow stack? */
610 if (stackcount == pushed) {
611 void * new;
612 isc_uint32_t newlen = stackcount + 16;
613 size_t newsize, oldsize;
614
615 newsize = newlen * sizeof(*stack);
616 oldsize = stackcount * sizeof(*stack);
617 new = isc_mem_get(mctx, newsize);
618 if (new == NULL)
619 goto cleanup;
620 if (stackcount != 0) {
621 memmove(new, stack, oldsize);
622 isc_mem_put(mctx, stack, oldsize);
623 }
624 stack = new;
625 stackcount = newlen;
626 }
627 /*
628 * We want to resume processing this list on the
629 * next element.
630 */
631 stack[pushed].element = cfg_list_next(element);
632 stack[pushed].port = port;
633 pushed++;
634 goto newlist;
635 }
636
637 if (i == addrcount) {
638 void * new;
639 isc_uint32_t newlen = addrcount + 16;
640 size_t newsize, oldsize;
641
642 newsize = newlen * sizeof(isc_sockaddr_t);
643 oldsize = addrcount * sizeof(isc_sockaddr_t);
644 new = isc_mem_get(mctx, newsize);
645 if (new == NULL)
646 goto cleanup;
647 if (addrcount != 0) {
648 memmove(new, addrs, oldsize);
649 isc_mem_put(mctx, addrs, oldsize);
650 }
651 addrs = new;
652 addrcount = newlen;
653
654 newsize = newlen * sizeof(dns_name_t *);
655 oldsize = keycount * sizeof(dns_name_t *);
656 new = isc_mem_get(mctx, newsize);
657 if (new == NULL)
658 goto cleanup;
659 if (keycount != 0) {
660 memmove(new, keys, oldsize);
661 isc_mem_put(mctx, keys, oldsize);
662 }
663 keys = new;
664 keycount = newlen;
665 }
666
667 addrs[i] = *cfg_obj_assockaddr(addr);
668 if (isc_sockaddr_getport(&addrs[i]) == 0)
669 isc_sockaddr_setport(&addrs[i], port);
670 keys[i] = NULL;
671 i++; /* Increment here so that cleanup on error works. */
672 if (!cfg_obj_isstring(key))
673 continue;
674 keys[i - 1] = isc_mem_get(mctx, sizeof(dns_name_t));
675 if (keys[i - 1] == NULL)
676 goto cleanup;
677 dns_name_init(keys[i - 1], NULL);
678
679 keystr = cfg_obj_asstring(key);
680 isc_buffer_constinit(&b, keystr, strlen(keystr));
681 isc_buffer_add(&b, strlen(keystr));
682 dns_fixedname_init(&fname);
683 result = dns_name_fromtext(dns_fixedname_name(&fname), &b,
684 dns_rootname, 0, NULL);
685 if (result != ISC_R_SUCCESS)
686 goto cleanup;
687 result = dns_name_dup(dns_fixedname_name(&fname), mctx,
688 keys[i - 1]);
689 if (result != ISC_R_SUCCESS)
690 goto cleanup;
691 }
692 if (pushed != 0) {
693 pushed--;
694 element = stack[pushed].element;
695 port = stack[pushed].port;
696 goto resume;
697 }
698 if (i < addrcount) {
699 void * new;
700 size_t newsize, oldsize;
701
702 newsize = i * sizeof(isc_sockaddr_t);
703 oldsize = addrcount * sizeof(isc_sockaddr_t);
704 if (i != 0) {
705 new = isc_mem_get(mctx, newsize);
706 if (new == NULL)
707 goto cleanup;
708 memmove(new, addrs, newsize);
709 } else
710 new = NULL;
711 isc_mem_put(mctx, addrs, oldsize);
712 addrs = new;
713 addrcount = i;
714
715 newsize = i * sizeof(dns_name_t *);
716 oldsize = keycount * sizeof(dns_name_t *);
717 if (i != 0) {
718 new = isc_mem_get(mctx, newsize);
719 if (new == NULL)
720 goto cleanup;
721 memmove(new, keys, newsize);
722 } else
723 new = NULL;
724 isc_mem_put(mctx, keys, oldsize);
725 keys = new;
726 keycount = i;
727 }
728
729 if (lists != NULL)
730 isc_mem_put(mctx, lists, listcount * sizeof(*lists));
731 if (stack != NULL)
732 isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
733
734 INSIST(keycount == addrcount);
735
736 *addrsp = addrs;
737 *keysp = keys;
738 *countp = addrcount;
739
740 return (ISC_R_SUCCESS);
741
742 cleanup:
743 if (addrs != NULL)
744 isc_mem_put(mctx, addrs, addrcount * sizeof(isc_sockaddr_t));
745 if (keys != NULL) {
746 for (j = 0; j < i; j++) {
747 if (keys[j] == NULL)
748 continue;
749 if (dns_name_dynamic(keys[j]))
750 dns_name_free(keys[j], mctx);
751 isc_mem_put(mctx, keys[j], sizeof(dns_name_t));
752 }
753 isc_mem_put(mctx, keys, keycount * sizeof(dns_name_t *));
754 }
755 if (lists != NULL)
756 isc_mem_put(mctx, lists, listcount * sizeof(*lists));
757 if (stack != NULL)
758 isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
759 return (result);
760 }
761
762 void
ns_config_putipandkeylist(isc_mem_t * mctx,isc_sockaddr_t ** addrsp,dns_name_t *** keysp,isc_uint32_t count)763 ns_config_putipandkeylist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
764 dns_name_t ***keysp, isc_uint32_t count)
765 {
766 unsigned int i;
767 dns_name_t **keys = *keysp;
768
769 INSIST(addrsp != NULL && *addrsp != NULL);
770
771 isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
772 for (i = 0; i < count; i++) {
773 if (keys[i] == NULL)
774 continue;
775 if (dns_name_dynamic(keys[i]))
776 dns_name_free(keys[i], mctx);
777 isc_mem_put(mctx, keys[i], sizeof(dns_name_t));
778 }
779 isc_mem_put(mctx, *keysp, count * sizeof(dns_name_t *));
780 *addrsp = NULL;
781 *keysp = NULL;
782 }
783
784 isc_result_t
ns_config_getport(const cfg_obj_t * config,in_port_t * portp)785 ns_config_getport(const cfg_obj_t *config, in_port_t *portp) {
786 const cfg_obj_t *maps[3];
787 const cfg_obj_t *options = NULL;
788 const cfg_obj_t *portobj = NULL;
789 isc_result_t result;
790 int i;
791
792 (void)cfg_map_get(config, "options", &options);
793 i = 0;
794 if (options != NULL)
795 maps[i++] = options;
796 maps[i++] = ns_g_defaults;
797 maps[i] = NULL;
798
799 result = ns_config_get(maps, "port", &portobj);
800 INSIST(result == ISC_R_SUCCESS);
801 if (cfg_obj_asuint32(portobj) >= ISC_UINT16_MAX) {
802 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
803 "port '%u' out of range",
804 cfg_obj_asuint32(portobj));
805 return (ISC_R_RANGE);
806 }
807 *portp = (in_port_t)cfg_obj_asuint32(portobj);
808 return (ISC_R_SUCCESS);
809 }
810
811 struct keyalgorithms {
812 const char *str;
813 enum { hmacnone, hmacmd5, hmacsha1, hmacsha224,
814 hmacsha256, hmacsha384, hmacsha512 } hmac;
815 unsigned int type;
816 isc_uint16_t size;
817 } algorithms[] = {
818 { "hmac-md5", hmacmd5, DST_ALG_HMACMD5, 128 },
819 { "hmac-md5.sig-alg.reg.int", hmacmd5, DST_ALG_HMACMD5, 0 },
820 { "hmac-md5.sig-alg.reg.int.", hmacmd5, DST_ALG_HMACMD5, 0 },
821 { "hmac-sha1", hmacsha1, DST_ALG_HMACSHA1, 160 },
822 { "hmac-sha224", hmacsha224, DST_ALG_HMACSHA224, 224 },
823 { "hmac-sha256", hmacsha256, DST_ALG_HMACSHA256, 256 },
824 { "hmac-sha384", hmacsha384, DST_ALG_HMACSHA384, 384 },
825 { "hmac-sha512", hmacsha512, DST_ALG_HMACSHA512, 512 },
826 { NULL, hmacnone, DST_ALG_UNKNOWN, 0 }
827 };
828
829 isc_result_t
ns_config_getkeyalgorithm(const char * str,dns_name_t ** name,isc_uint16_t * digestbits)830 ns_config_getkeyalgorithm(const char *str, dns_name_t **name,
831 isc_uint16_t *digestbits)
832 {
833 return (ns_config_getkeyalgorithm2(str, name, NULL, digestbits));
834 }
835
836 isc_result_t
ns_config_getkeyalgorithm2(const char * str,dns_name_t ** name,unsigned int * typep,isc_uint16_t * digestbits)837 ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
838 unsigned int *typep, isc_uint16_t *digestbits)
839 {
840 int i;
841 size_t len = 0;
842 isc_uint16_t bits;
843 isc_result_t result;
844
845 for (i = 0; algorithms[i].str != NULL; i++) {
846 len = strlen(algorithms[i].str);
847 if (strncasecmp(algorithms[i].str, str, len) == 0 &&
848 (str[len] == '\0' ||
849 (algorithms[i].size != 0 && str[len] == '-')))
850 break;
851 }
852 if (algorithms[i].str == NULL)
853 return (ISC_R_NOTFOUND);
854 if (str[len] == '-') {
855 result = isc_parse_uint16(&bits, str + len + 1, 10);
856 if (result != ISC_R_SUCCESS)
857 return (result);
858 if (bits > algorithms[i].size)
859 return (ISC_R_RANGE);
860 } else if (algorithms[i].size == 0)
861 bits = 128;
862 else
863 bits = algorithms[i].size;
864
865 if (name != NULL) {
866 switch (algorithms[i].hmac) {
867 case hmacmd5: *name = dns_tsig_hmacmd5_name; break;
868 case hmacsha1: *name = dns_tsig_hmacsha1_name; break;
869 case hmacsha224: *name = dns_tsig_hmacsha224_name; break;
870 case hmacsha256: *name = dns_tsig_hmacsha256_name; break;
871 case hmacsha384: *name = dns_tsig_hmacsha384_name; break;
872 case hmacsha512: *name = dns_tsig_hmacsha512_name; break;
873 default:
874 INSIST(0);
875 }
876 }
877 if (typep != NULL)
878 *typep = algorithms[i].type;
879 if (digestbits != NULL)
880 *digestbits = bits;
881 return (ISC_R_SUCCESS);
882 }
883