Merge from vendor branch FILE:
[dragonfly.git] / contrib / bind-9.3 / bin / named / config.c
1 /*
2  * Copyright (C) 2004  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.29 2004/10/05 02:52:26 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(cfg_obj_t **maps, const char *name, 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(cfg_obj_t **maps, const char *which, cfg_obj_t **obj) {
212         cfg_listelt_t *element;
213         cfg_obj_t *checknames;
214         cfg_obj_t *type;
215         cfg_obj_t *value;
216         int i;
217
218         for (i = 0;; i++) {
219                 if (maps[i] == NULL)
220                         return (ISC_R_NOTFOUND);
221                 checknames = NULL;
222                 if (cfg_map_get(maps[i], "check-names", &checknames) == ISC_R_SUCCESS) {
223                         /*
224                          * Zone map entry is not a list.
225                          */
226                         if (checknames != NULL && !cfg_obj_islist(checknames)) {
227                                 *obj = checknames;
228                                 return (ISC_R_SUCCESS);
229                         }
230                         for (element = cfg_list_first(checknames);
231                              element != NULL;
232                              element = cfg_list_next(element)) {
233                                 value = cfg_listelt_value(element);
234                                 type = cfg_tuple_get(value, "type");
235                                 if (strcasecmp(cfg_obj_asstring(type), which) == 0) {
236                                         *obj = cfg_tuple_get(value, "mode");
237                                         return (ISC_R_SUCCESS);
238                                 }
239                         }
240
241                 }
242         }
243 }
244
245 int
246 ns_config_listcount(cfg_obj_t *list) {
247         cfg_listelt_t *e;
248         int i = 0;
249
250         for (e = cfg_list_first(list); e != NULL; e = cfg_list_next(e))
251                 i++;
252
253         return (i);
254 }
255
256 isc_result_t
257 ns_config_getclass(cfg_obj_t *classobj, dns_rdataclass_t defclass,
258                    dns_rdataclass_t *classp) {
259         char *str;
260         isc_textregion_t r;
261         isc_result_t result;
262
263         if (!cfg_obj_isstring(classobj)) {
264                 *classp = defclass;
265                 return (ISC_R_SUCCESS);
266         }
267         str = cfg_obj_asstring(classobj);
268         r.base = str;
269         r.length = strlen(str);
270         result = dns_rdataclass_fromtext(classp, &r);
271         if (result != ISC_R_SUCCESS)
272                 cfg_obj_log(classobj, ns_g_lctx, ISC_LOG_ERROR,
273                             "unknown class '%s'", str);
274         return (result);
275 }
276
277 isc_result_t
278 ns_config_gettype(cfg_obj_t *typeobj, dns_rdatatype_t deftype,
279                    dns_rdatatype_t *typep) {
280         char *str;
281         isc_textregion_t r;
282         isc_result_t result;
283
284         if (!cfg_obj_isstring(typeobj)) {
285                 *typep = deftype;
286                 return (ISC_R_SUCCESS);
287         }
288         str = cfg_obj_asstring(typeobj);
289         r.base = str;
290         r.length = strlen(str);
291         result = dns_rdatatype_fromtext(typep, &r);
292         if (result != ISC_R_SUCCESS)
293                 cfg_obj_log(typeobj, ns_g_lctx, ISC_LOG_ERROR,
294                             "unknown type '%s'", str);
295         return (result);
296 }
297
298 dns_zonetype_t
299 ns_config_getzonetype(cfg_obj_t *zonetypeobj) {
300         dns_zonetype_t ztype = dns_zone_none;
301         char *str;
302
303         str = cfg_obj_asstring(zonetypeobj);
304         if (strcasecmp(str, "master") == 0)
305                 ztype = dns_zone_master;
306         else if (strcasecmp(str, "slave") == 0)
307                 ztype = dns_zone_slave;
308         else if (strcasecmp(str, "stub") == 0)
309                 ztype = dns_zone_stub;
310         else
311                 INSIST(0);
312         return (ztype);
313 }
314
315 isc_result_t
316 ns_config_getiplist(cfg_obj_t *config, cfg_obj_t *list,
317                     in_port_t defport, isc_mem_t *mctx,
318                     isc_sockaddr_t **addrsp, isc_uint32_t *countp)
319 {
320         int count, i = 0;
321         cfg_obj_t *addrlist;
322         cfg_obj_t *portobj;
323         cfg_listelt_t *element;
324         isc_sockaddr_t *addrs;
325         in_port_t port;
326         isc_result_t result;
327
328         INSIST(addrsp != NULL && *addrsp == NULL);
329         INSIST(countp != NULL);
330
331         addrlist = cfg_tuple_get(list, "addresses");
332         count = ns_config_listcount(addrlist);
333
334         portobj = cfg_tuple_get(list, "port");
335         if (cfg_obj_isuint32(portobj)) {
336                 isc_uint32_t val = cfg_obj_asuint32(portobj);
337                 if (val > ISC_UINT16_MAX) {
338                         cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
339                                     "port '%u' out of range", val);
340                         return (ISC_R_RANGE);
341                 }
342                 port = (in_port_t) val;
343         } else if (defport != 0)
344                 port = defport;
345         else {
346                 result = ns_config_getport(config, &port);
347                 if (result != ISC_R_SUCCESS)
348                         return (result);
349         }
350
351         addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));
352         if (addrs == NULL)
353                 return (ISC_R_NOMEMORY);
354
355         for (element = cfg_list_first(addrlist);
356              element != NULL;
357              element = cfg_list_next(element), i++)
358         {
359                 INSIST(i < count);
360                 addrs[i] = *cfg_obj_assockaddr(cfg_listelt_value(element));
361                 if (isc_sockaddr_getport(&addrs[i]) == 0)
362                         isc_sockaddr_setport(&addrs[i], port);
363         }
364         INSIST(i == count);
365
366         *addrsp = addrs;
367         *countp = count;
368
369         return (ISC_R_SUCCESS);
370 }
371
372 void
373 ns_config_putiplist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
374                     isc_uint32_t count)
375 {
376         INSIST(addrsp != NULL && *addrsp != NULL);
377
378         isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
379         *addrsp = NULL;
380 }
381
382 static isc_result_t
383 get_masters_def(cfg_obj_t *cctx, char *name, cfg_obj_t **ret) {
384         isc_result_t result;
385         cfg_obj_t *masters = NULL;
386         cfg_listelt_t *elt;
387
388         result = cfg_map_get(cctx, "masters", &masters);
389         if (result != ISC_R_SUCCESS)
390                 return (result);
391         for (elt = cfg_list_first(masters);
392              elt != NULL;
393              elt = cfg_list_next(elt)) {
394                 cfg_obj_t *list;
395                 const char *listname;
396
397                 list = cfg_listelt_value(elt);
398                 listname = cfg_obj_asstring(cfg_tuple_get(list, "name"));
399
400                 if (strcasecmp(listname, name) == 0) {
401                         *ret = list;
402                         return (ISC_R_SUCCESS);
403                 }
404         }
405         return (ISC_R_NOTFOUND);
406 }
407
408 isc_result_t
409 ns_config_getipandkeylist(cfg_obj_t *config, cfg_obj_t *list, isc_mem_t *mctx,
410                           isc_sockaddr_t **addrsp, dns_name_t ***keysp,
411                           isc_uint32_t *countp)
412 {
413         isc_uint32_t addrcount = 0, keycount = 0, i = 0;
414         isc_uint32_t listcount = 0, l = 0, j;
415         isc_uint32_t stackcount = 0, pushed = 0;
416         isc_result_t result;
417         cfg_listelt_t *element;
418         cfg_obj_t *addrlist;
419         cfg_obj_t *portobj;
420         in_port_t port;
421         dns_fixedname_t fname;
422         isc_sockaddr_t *addrs = NULL;
423         dns_name_t **keys = NULL;
424         char **lists = NULL;
425         struct {
426                 cfg_listelt_t *element;
427                 in_port_t port;
428         } *stack = NULL;
429
430         REQUIRE(addrsp != NULL && *addrsp == NULL);
431         REQUIRE(keysp != NULL && *keysp == NULL);
432         REQUIRE(countp != NULL);
433
434  newlist:
435         addrlist = cfg_tuple_get(list, "addresses");
436         portobj = cfg_tuple_get(list, "port");
437         if (cfg_obj_isuint32(portobj)) {
438                 isc_uint32_t val = cfg_obj_asuint32(portobj);
439                 if (val > ISC_UINT16_MAX) {
440                         cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
441                                     "port '%u' out of range", val);
442                         return (ISC_R_RANGE);
443                 }
444                 port = (in_port_t) val;
445         } else {
446                 result = ns_config_getport(config, &port);
447                 if (result != ISC_R_SUCCESS)
448                         return (result);
449         }
450
451         result = ISC_R_NOMEMORY;
452
453         element = cfg_list_first(addrlist);
454  resume:
455         for ( ;
456              element != NULL;
457              element = cfg_list_next(element))
458         {
459                 cfg_obj_t *addr;
460                 cfg_obj_t *key;
461                 char *keystr;
462                 isc_buffer_t b;
463
464                 addr = cfg_tuple_get(cfg_listelt_value(element),
465                                      "masterselement");
466                 key = cfg_tuple_get(cfg_listelt_value(element), "key");
467
468                 if (!cfg_obj_issockaddr(addr)) {
469                         char *listname = cfg_obj_asstring(addr);
470                         isc_result_t tresult;
471
472                         /* Grow lists? */
473                         if (listcount == l) {
474                                 void * new;
475                                 isc_uint32_t newlen = listcount + 16;
476                                 size_t newsize, oldsize;
477
478                                 newsize = newlen * sizeof(*lists);
479                                 oldsize = listcount * sizeof(*lists);
480                                 new = isc_mem_get(mctx, newsize);
481                                 if (new == NULL)
482                                         goto cleanup;
483                                 if (listcount != 0) {
484                                         memcpy(new, lists, oldsize);
485                                         isc_mem_put(mctx, lists, oldsize);
486                                 }
487                                 lists = new;
488                                 listcount = newlen;
489                         }
490                         /* Seen? */
491                         for (j = 0; j < l; j++)
492                                 if (strcasecmp(lists[j], listname) == 0)
493                                         break;
494                         if (j < l)
495                                 continue;
496                         tresult = get_masters_def(config, listname, &list);
497                         if (tresult == ISC_R_NOTFOUND) {
498                                 cfg_obj_log(addr, ns_g_lctx, ISC_LOG_ERROR,
499                                     "masters \"%s\" not found", listname);
500
501                                 result = tresult;
502                                 goto cleanup;
503                         }
504                         if (tresult != ISC_R_SUCCESS)
505                                 goto cleanup;
506                         lists[l++] = listname;
507                         /* Grow stack? */
508                         if (stackcount == pushed) {
509                                 void * new;
510                                 isc_uint32_t newlen = stackcount + 16;
511                                 size_t newsize, oldsize;
512
513                                 newsize = newlen * sizeof(*stack);
514                                 oldsize = stackcount * sizeof(*stack);
515                                 new = isc_mem_get(mctx, newsize);
516                                 if (new == NULL)
517                                         goto cleanup;
518                                 if (stackcount != 0) {
519                                         memcpy(new, stack, oldsize);
520                                         isc_mem_put(mctx, stack, oldsize);
521                                 }
522                                 stack = new;
523                                 stackcount = newlen;
524                         }
525                         /*
526                          * We want to resume processing this list on the
527                          * next element.
528                          */
529                         stack[pushed].element = cfg_list_next(element);
530                         stack[pushed].port = port;
531                         pushed++;
532                         goto newlist;
533                 }
534
535                 if (i == addrcount) {
536                         void * new;
537                         isc_uint32_t newlen = addrcount + 16;
538                         size_t newsize, oldsize;
539
540                         newsize = newlen * sizeof(isc_sockaddr_t);
541                         oldsize = addrcount * sizeof(isc_sockaddr_t);
542                         new = isc_mem_get(mctx, newsize);
543                         if (new == NULL)
544                                 goto cleanup;
545                         if (addrcount != 0) {
546                                 memcpy(new, addrs, oldsize);
547                                 isc_mem_put(mctx, addrs, oldsize);
548                         }
549                         addrs = new;
550                         addrcount = newlen;
551
552                         newsize = newlen * sizeof(dns_name_t *);
553                         oldsize = keycount * sizeof(dns_name_t *);
554                         new = isc_mem_get(mctx, newsize);
555                         if (new == NULL)
556                                 goto cleanup;
557                         if (keycount != 0) {
558                                 memcpy(new, keys, oldsize);
559                                 isc_mem_put(mctx, keys, oldsize);
560                         }
561                         keys = new;
562                         keycount = newlen;
563                 }
564
565                 addrs[i] = *cfg_obj_assockaddr(addr);
566                 if (isc_sockaddr_getport(&addrs[i]) == 0)
567                         isc_sockaddr_setport(&addrs[i], port);
568                 keys[i] = NULL;
569                 if (!cfg_obj_isstring(key)) {
570                         i++;
571                         continue;
572                 }
573                 keys[i] = isc_mem_get(mctx, sizeof(dns_name_t));
574                 if (keys[i] == NULL)
575                         goto cleanup;
576                 dns_name_init(keys[i], NULL);
577                 
578                 keystr = cfg_obj_asstring(key);
579                 isc_buffer_init(&b, keystr, strlen(keystr));
580                 isc_buffer_add(&b, strlen(keystr));
581                 dns_fixedname_init(&fname);
582                 result = dns_name_fromtext(dns_fixedname_name(&fname), &b,
583                                            dns_rootname, ISC_FALSE, NULL);
584                 if (result != ISC_R_SUCCESS)
585                         goto cleanup;
586                 result = dns_name_dup(dns_fixedname_name(&fname), mctx,
587                                       keys[i]);
588                 if (result != ISC_R_SUCCESS)
589                         goto cleanup;
590                 i++;
591         }
592         if (pushed != 0) {
593                 pushed--;
594                 element = stack[pushed].element;
595                 port = stack[pushed].port;
596                 goto resume;
597         }
598         if (i < addrcount) {
599                 void * new;
600                 size_t newsize, oldsize;
601
602                 newsize = i * sizeof(isc_sockaddr_t);
603                 oldsize = addrcount * sizeof(isc_sockaddr_t);
604                 if (i != 0) {
605                         new = isc_mem_get(mctx, newsize);
606                         if (new == NULL)
607                                 goto cleanup;
608                         memcpy(new, addrs, newsize);
609                         isc_mem_put(mctx, addrs, oldsize);
610                 } else
611                         new = NULL;
612                 addrs = new;
613                 addrcount = i;
614
615                 newsize = i * sizeof(dns_name_t *);
616                 oldsize = keycount * sizeof(dns_name_t *);
617                 if (i != 0) {
618                         new = isc_mem_get(mctx, newsize);
619                         if (new == NULL)
620                                 goto cleanup;
621                         memcpy(new, keys,  newsize);
622                         isc_mem_put(mctx, keys, oldsize);
623                 } else
624                         new = NULL;
625                 keys = new;
626                 keycount = i;
627         }
628
629         if (lists != NULL)
630                 isc_mem_put(mctx, lists, listcount * sizeof(*lists));
631         if (stack != NULL)
632                 isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
633         
634         INSIST(keycount == addrcount);
635
636         *addrsp = addrs;
637         *keysp = keys;
638         *countp = addrcount;
639
640         return (ISC_R_SUCCESS);
641
642  cleanup:
643         if (addrs != NULL)
644                 isc_mem_put(mctx, addrs, addrcount * sizeof(isc_sockaddr_t));
645         if (keys != NULL) {
646                 for (j = 0; j <= i; j++) {
647                         if (keys[j] == NULL)
648                                 continue;
649                         if (dns_name_dynamic(keys[j]))
650                                 dns_name_free(keys[j], mctx);
651                         isc_mem_put(mctx, keys[j], sizeof(dns_name_t));
652                 }
653                 isc_mem_put(mctx, keys, keycount * sizeof(dns_name_t *));
654         }
655         if (lists != NULL)
656                 isc_mem_put(mctx, lists, listcount * sizeof(*lists));
657         if (stack != NULL)
658                 isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
659         return (result);
660 }
661
662 void
663 ns_config_putipandkeylist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
664                           dns_name_t ***keysp, isc_uint32_t count)
665 {
666         unsigned int i;
667         dns_name_t **keys = *keysp;
668
669         INSIST(addrsp != NULL && *addrsp != NULL);
670
671         isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
672         for (i = 0; i < count; i++) {
673                 if (keys[i] == NULL)
674                         continue;
675                 if (dns_name_dynamic(keys[i]))
676                         dns_name_free(keys[i], mctx);
677                 isc_mem_put(mctx, keys[i], sizeof(dns_name_t));
678         }
679         isc_mem_put(mctx, *keysp, count * sizeof(dns_name_t *));
680         *addrsp = NULL;
681         *keysp = NULL;
682 }
683
684 isc_result_t
685 ns_config_getport(cfg_obj_t *config, in_port_t *portp) {
686         cfg_obj_t *maps[3];
687         cfg_obj_t *options = NULL;
688         cfg_obj_t *portobj = NULL;
689         isc_result_t result;
690         int i;
691
692         (void)cfg_map_get(config, "options", &options);
693         i = 0;
694         if (options != NULL)
695                 maps[i++] = options;
696         maps[i++] = ns_g_defaults;
697         maps[i] = NULL;
698
699         result = ns_config_get(maps, "port", &portobj);
700         INSIST(result == ISC_R_SUCCESS);
701         if (cfg_obj_asuint32(portobj) >= ISC_UINT16_MAX) {
702                 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
703                             "port '%u' out of range",
704                             cfg_obj_asuint32(portobj));
705                 return (ISC_R_RANGE);
706         }
707         *portp = (in_port_t)cfg_obj_asuint32(portobj);
708         return (ISC_R_SUCCESS);
709 }
710
711 isc_result_t
712 ns_config_getkeyalgorithm(const char *str, dns_name_t **name)
713 {
714         if (strcasecmp(str, "hmac-md5") == 0 ||
715             strcasecmp(str, "hmac-md5.sig-alg.reg.int") == 0 ||
716             strcasecmp(str, "hmac-md5.sig-alg.reg.int.") == 0)
717         {
718                 if (name != NULL)
719                         *name = dns_tsig_hmacmd5_name;
720                 return (ISC_R_SUCCESS);
721         }
722         return (ISC_R_NOTFOUND);
723 }