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