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