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