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