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