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