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