]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/bin/named/config.c
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / contrib / bind9 / bin / named / config.c
1 /*
2  * Copyright (C) 2004-2014  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.123 2012/01/06 23:46:41 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 <dst/dst.h>
46
47 #include <named/config.h>
48 #include <named/globals.h>
49
50 #include "bind.keys.h"
51
52 /*% default configuration */
53 static char defaultconf[] = "\
54 options {\n\
55 #       blackhole {none;};\n"
56 #ifndef WIN32
57 "       coresize default;\n\
58         datasize default;\n\
59         files unlimited;\n\
60         stacksize default;\n"
61 #endif
62 "#      session-keyfile \"" NS_LOCALSTATEDIR "/run/named/session.key\";\n\
63         session-keyname local-ddns;\n\
64         session-keyalg hmac-sha256;\n\
65         deallocate-on-exit true;\n\
66 #       directory <none>\n\
67         dump-file \"named_dump.db\";\n\
68         fake-iquery no;\n\
69         has-old-clients false;\n\
70         heartbeat-interval 60;\n\
71         host-statistics no;\n\
72         interface-interval 60;\n\
73         listen-on {any;};\n\
74         listen-on-v6 {none;};\n\
75         match-mapped-addresses no;\n\
76         max-rsa-exponent-size 0; /* no limit */\n\
77         memstatistics-file \"named.memstats\";\n\
78         multiple-cnames no;\n\
79 #       named-xfer <obsolete>;\n\
80 #       pid-file \"" NS_LOCALSTATEDIR "/run/named/named.pid\"; /* or /lwresd.pid */\n\
81         bindkeys-file \"" NS_SYSCONFDIR "/bind.keys\";\n\
82         port 53;\n\
83         recursing-file \"named.recursing\";\n\
84         secroots-file \"named.secroots\";\n\
85 "
86 #ifdef PATH_RANDOMDEV
87 "\
88         random-device \"" PATH_RANDOMDEV "\";\n\
89 "
90 #endif
91 "\
92         recursive-clients 1000;\n\
93         resolver-query-timeout 10;\n\
94         rrset-order { order random; };\n\
95         serial-queries 20;\n\
96         serial-query-rate 20;\n\
97         server-id none;\n\
98         statistics-file \"named.stats\";\n\
99         statistics-interval 60;\n\
100         tcp-clients 100;\n\
101         tcp-listen-queue 10;\n\
102 #       tkey-dhkey <none>\n\
103 #       tkey-gssapi-credential <none>\n\
104 #       tkey-domain <none>\n\
105         transfers-per-ns 2;\n\
106         transfers-in 10;\n\
107         transfers-out 10;\n\
108         treat-cr-as-space true;\n\
109         use-id-pool true;\n\
110         use-ixfr true;\n\
111         edns-udp-size 4096;\n\
112         max-udp-size 4096;\n\
113         request-nsid false;\n\
114         reserved-sockets 512;\n\
115 \n\
116         /* DLV */\n\
117         dnssec-lookaside . trust-anchor dlv.isc.org;\n\
118 \n\
119         /* view */\n\
120         allow-notify {none;};\n\
121         allow-update-forwarding {none;};\n\
122         allow-query-cache { localnets; localhost; };\n\
123         allow-query-cache-on { any; };\n\
124         allow-recursion { localnets; localhost; };\n\
125         allow-recursion-on { any; };\n\
126 #       allow-v6-synthesis <obsolete>;\n\
127 #       sortlist <none>\n\
128 #       topology <none>\n\
129         auth-nxdomain false;\n\
130         minimal-responses false;\n\
131         recursion true;\n\
132         provide-ixfr true;\n\
133         request-ixfr true;\n\
134         fetch-glue no;\n\
135         rfc2308-type1 no;\n\
136         additional-from-auth true;\n\
137         additional-from-cache true;\n\
138         query-source address *;\n\
139         query-source-v6 address *;\n\
140         notify-source *;\n\
141         notify-source-v6 *;\n\
142         cleaning-interval 0;  /* now meaningless */\n\
143         min-roots 2;\n\
144         lame-ttl 600;\n\
145         max-ncache-ttl 10800; /* 3 hours */\n\
146         max-cache-ttl 604800; /* 1 week */\n\
147         transfer-format many-answers;\n\
148         max-cache-size 0;\n\
149         check-names master fail;\n\
150         check-names slave warn;\n\
151         check-names response ignore;\n\
152         check-dup-records warn;\n\
153         check-mx warn;\n\
154         check-spf warn;\n\
155         acache-enable no;\n\
156         acache-cleaning-interval 60;\n\
157         max-acache-size 16M;\n\
158         dnssec-enable yes;\n\
159         dnssec-validation yes; \n\
160         dnssec-accept-expired no;\n\
161         clients-per-query 10;\n\
162         max-clients-per-query 100;\n\
163         zero-no-soa-ttl-cache no;\n\
164         nsec3-test-zone no;\n\
165         allow-new-zones no;\n\
166 "
167 #ifdef ALLOW_FILTER_AAAA_ON_V4
168 "       filter-aaaa-on-v4 no;\n\
169         filter-aaaa { any; };\n\
170 "
171 #endif
172
173 "       /* zone */\n\
174         allow-query {any;};\n\
175         allow-query-on {any;};\n\
176         allow-transfer {any;};\n\
177         notify yes;\n\
178 #       also-notify <none>\n\
179         notify-delay 5;\n\
180         notify-to-soa no;\n\
181         dialup no;\n\
182 #       forward <none>\n\
183 #       forwarders <none>\n\
184         maintain-ixfr-base no;\n\
185 #       max-ixfr-log-size <obsolete>\n\
186         transfer-source *;\n\
187         transfer-source-v6 *;\n\
188         alt-transfer-source *;\n\
189         alt-transfer-source-v6 *;\n\
190         max-transfer-time-in 120;\n\
191         max-transfer-time-out 120;\n\
192         max-transfer-idle-in 60;\n\
193         max-transfer-idle-out 60;\n\
194         max-retry-time 1209600; /* 2 weeks */\n\
195         min-retry-time 500;\n\
196         max-refresh-time 2419200; /* 4 weeks */\n\
197         min-refresh-time 300;\n\
198         multi-master no;\n\
199         dnssec-secure-to-insecure no;\n\
200         sig-validity-interval 30; /* days */\n\
201         sig-signing-nodes 100;\n\
202         sig-signing-signatures 10;\n\
203         sig-signing-type 65534;\n\
204         inline-signing no;\n\
205         zone-statistics terse;\n\
206         max-journal-size unlimited;\n\
207         ixfr-from-differences false;\n\
208         check-wildcard yes;\n\
209         check-sibling yes;\n\
210         check-integrity yes;\n\
211         check-mx-cname warn;\n\
212         check-srv-cname warn;\n\
213         zero-no-soa-ttl yes;\n\
214         update-check-ksk yes;\n\
215         serial-update-method increment;\n\
216         dnssec-update-mode maintain;\n\
217         dnssec-dnskey-kskonly no;\n\
218         dnssec-loadkeys-interval 60;\n\
219         try-tcp-refresh yes; /* BIND 8 compat */\n\
220 };\n\
221 "
222
223 "#\n\
224 #  Zones in the \"_bind\" view are NOT counted in the count of zones.\n\
225 #\n\
226 view \"_bind\" chaos {\n\
227         recursion no;\n\
228         notify no;\n\
229         allow-new-zones no;\n\
230 "
231 #ifdef USE_RRL
232 "       # Prevent use of this zone in DNS amplified reflection DoS attacks\n\
233         rate-limit {\n\
234                 responses-per-second 3;\n\
235                 slip 0;\n\
236                 min-table-size 10;\n\
237         };\n\
238 "
239 #endif /* USE_RRL */
240 "       zone \"version.bind\" chaos {\n\
241                 type master;\n\
242                 database \"_builtin version\";\n\
243         };\n\
244 \n\
245         zone \"hostname.bind\" chaos {\n\
246                 type master;\n\
247                 database \"_builtin hostname\";\n\
248         };\n\
249 \n\
250         zone \"authors.bind\" chaos {\n\
251                 type master;\n\
252                 database \"_builtin authors\";\n\
253         };\n\
254 \n\
255         zone \"id.server\" chaos {\n\
256                 type master;\n\
257                 database \"_builtin id\";\n\
258         };\n\
259 };\n\
260 "
261 "#\n\
262 #  Default trusted key(s) for builtin DLV support\n\
263 #  (used if \"dnssec-lookaside auto;\" is set and\n\
264 #  sysconfdir/bind.keys doesn't exist).\n\
265 #\n\
266 # BEGIN MANAGED KEYS\n"
267
268 /* Imported from bind.keys.h: */
269 MANAGED_KEYS
270
271 "# END MANAGED KEYS\n\
272 ";
273
274 isc_result_t
275 ns_config_parsedefaults(cfg_parser_t *parser, cfg_obj_t **conf) {
276         isc_buffer_t b;
277
278         isc_buffer_init(&b, defaultconf, sizeof(defaultconf) - 1);
279         isc_buffer_add(&b, sizeof(defaultconf) - 1);
280         return (cfg_parse_buffer(parser, &b, &cfg_type_namedconf, conf));
281 }
282
283 isc_result_t
284 ns_config_get(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
285         int i;
286
287         for (i = 0;; i++) {
288                 if (maps[i] == NULL)
289                         return (ISC_R_NOTFOUND);
290                 if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
291                         return (ISC_R_SUCCESS);
292         }
293 }
294
295 isc_result_t
296 ns_checknames_get(const cfg_obj_t **maps, const char *which,
297                   const cfg_obj_t **obj)
298 {
299         const cfg_listelt_t *element;
300         const cfg_obj_t *checknames;
301         const cfg_obj_t *type;
302         const cfg_obj_t *value;
303         int i;
304
305         for (i = 0;; i++) {
306                 if (maps[i] == NULL)
307                         return (ISC_R_NOTFOUND);
308                 checknames = NULL;
309                 if (cfg_map_get(maps[i], "check-names",
310                                 &checknames) == ISC_R_SUCCESS) {
311                         /*
312                          * Zone map entry is not a list.
313                          */
314                         if (checknames != NULL && !cfg_obj_islist(checknames)) {
315                                 *obj = checknames;
316                                 return (ISC_R_SUCCESS);
317                         }
318                         for (element = cfg_list_first(checknames);
319                              element != NULL;
320                              element = cfg_list_next(element)) {
321                                 value = cfg_listelt_value(element);
322                                 type = cfg_tuple_get(value, "type");
323                                 if (strcasecmp(cfg_obj_asstring(type),
324                                                which) == 0) {
325                                         *obj = cfg_tuple_get(value, "mode");
326                                         return (ISC_R_SUCCESS);
327                                 }
328                         }
329
330                 }
331         }
332 }
333
334 int
335 ns_config_listcount(const cfg_obj_t *list) {
336         const cfg_listelt_t *e;
337         int i = 0;
338
339         for (e = cfg_list_first(list); e != NULL; e = cfg_list_next(e))
340                 i++;
341
342         return (i);
343 }
344
345 isc_result_t
346 ns_config_getclass(const cfg_obj_t *classobj, dns_rdataclass_t defclass,
347                    dns_rdataclass_t *classp) {
348         isc_textregion_t r;
349         isc_result_t result;
350
351         if (!cfg_obj_isstring(classobj)) {
352                 *classp = defclass;
353                 return (ISC_R_SUCCESS);
354         }
355         DE_CONST(cfg_obj_asstring(classobj), r.base);
356         r.length = strlen(r.base);
357         result = dns_rdataclass_fromtext(classp, &r);
358         if (result != ISC_R_SUCCESS)
359                 cfg_obj_log(classobj, ns_g_lctx, ISC_LOG_ERROR,
360                             "unknown class '%s'", r.base);
361         return (result);
362 }
363
364 isc_result_t
365 ns_config_gettype(const cfg_obj_t *typeobj, dns_rdatatype_t deftype,
366                    dns_rdatatype_t *typep) {
367         isc_textregion_t r;
368         isc_result_t result;
369
370         if (!cfg_obj_isstring(typeobj)) {
371                 *typep = deftype;
372                 return (ISC_R_SUCCESS);
373         }
374         DE_CONST(cfg_obj_asstring(typeobj), r.base);
375         r.length = strlen(r.base);
376         result = dns_rdatatype_fromtext(typep, &r);
377         if (result != ISC_R_SUCCESS)
378                 cfg_obj_log(typeobj, ns_g_lctx, ISC_LOG_ERROR,
379                             "unknown type '%s'", r.base);
380         return (result);
381 }
382
383 dns_zonetype_t
384 ns_config_getzonetype(const cfg_obj_t *zonetypeobj) {
385         dns_zonetype_t ztype = dns_zone_none;
386         const char *str;
387
388         str = cfg_obj_asstring(zonetypeobj);
389         if (strcasecmp(str, "master") == 0)
390                 ztype = dns_zone_master;
391         else if (strcasecmp(str, "slave") == 0)
392                 ztype = dns_zone_slave;
393         else if (strcasecmp(str, "stub") == 0)
394                 ztype = dns_zone_stub;
395         else if (strcasecmp(str, "static-stub") == 0)
396                 ztype = dns_zone_staticstub;
397         else if (strcasecmp(str, "redirect") == 0)
398                 ztype = dns_zone_redirect;
399         else
400                 INSIST(0);
401         return (ztype);
402 }
403
404 isc_result_t
405 ns_config_getiplist(const cfg_obj_t *config, const cfg_obj_t *list,
406                     in_port_t defport, isc_mem_t *mctx,
407                     isc_sockaddr_t **addrsp, isc_uint32_t *countp)
408 {
409         int count, i = 0;
410         const cfg_obj_t *addrlist;
411         const cfg_obj_t *portobj;
412         const cfg_listelt_t *element;
413         isc_sockaddr_t *addrs;
414         in_port_t port;
415         isc_result_t result;
416
417         INSIST(addrsp != NULL && *addrsp == NULL);
418         INSIST(countp != NULL);
419
420         addrlist = cfg_tuple_get(list, "addresses");
421         count = ns_config_listcount(addrlist);
422
423         portobj = cfg_tuple_get(list, "port");
424         if (cfg_obj_isuint32(portobj)) {
425                 isc_uint32_t val = cfg_obj_asuint32(portobj);
426                 if (val > ISC_UINT16_MAX) {
427                         cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
428                                     "port '%u' out of range", val);
429                         return (ISC_R_RANGE);
430                 }
431                 port = (in_port_t) val;
432         } else if (defport != 0)
433                 port = defport;
434         else {
435                 result = ns_config_getport(config, &port);
436                 if (result != ISC_R_SUCCESS)
437                         return (result);
438         }
439
440         addrs = isc_mem_get(mctx, count * sizeof(isc_sockaddr_t));
441         if (addrs == NULL)
442                 return (ISC_R_NOMEMORY);
443
444         for (element = cfg_list_first(addrlist);
445              element != NULL;
446              element = cfg_list_next(element), i++)
447         {
448                 INSIST(i < count);
449                 addrs[i] = *cfg_obj_assockaddr(cfg_listelt_value(element));
450                 if (isc_sockaddr_getport(&addrs[i]) == 0)
451                         isc_sockaddr_setport(&addrs[i], port);
452         }
453         INSIST(i == count);
454
455         *addrsp = addrs;
456         *countp = count;
457
458         return (ISC_R_SUCCESS);
459 }
460
461 void
462 ns_config_putiplist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
463                     isc_uint32_t count)
464 {
465         INSIST(addrsp != NULL && *addrsp != NULL);
466
467         isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
468         *addrsp = NULL;
469 }
470
471 static isc_result_t
472 get_masters_def(const cfg_obj_t *cctx, const char *name,
473                 const cfg_obj_t **ret)
474 {
475         isc_result_t result;
476         const cfg_obj_t *masters = NULL;
477         const cfg_listelt_t *elt;
478
479         result = cfg_map_get(cctx, "masters", &masters);
480         if (result != ISC_R_SUCCESS)
481                 return (result);
482         for (elt = cfg_list_first(masters);
483              elt != NULL;
484              elt = cfg_list_next(elt)) {
485                 const cfg_obj_t *list;
486                 const char *listname;
487
488                 list = cfg_listelt_value(elt);
489                 listname = cfg_obj_asstring(cfg_tuple_get(list, "name"));
490
491                 if (strcasecmp(listname, name) == 0) {
492                         *ret = list;
493                         return (ISC_R_SUCCESS);
494                 }
495         }
496         return (ISC_R_NOTFOUND);
497 }
498
499 isc_result_t
500 ns_config_getipandkeylist(const cfg_obj_t *config, const cfg_obj_t *list,
501                           isc_mem_t *mctx, isc_sockaddr_t **addrsp,
502                           dns_name_t ***keysp, isc_uint32_t *countp)
503 {
504         isc_uint32_t addrcount = 0, keycount = 0, i = 0;
505         isc_uint32_t listcount = 0, l = 0, j;
506         isc_uint32_t stackcount = 0, pushed = 0;
507         isc_result_t result;
508         const cfg_listelt_t *element;
509         const cfg_obj_t *addrlist;
510         const cfg_obj_t *portobj;
511         in_port_t port;
512         dns_fixedname_t fname;
513         isc_sockaddr_t *addrs = NULL;
514         dns_name_t **keys = NULL;
515         struct { const char *name; } *lists = NULL;
516         struct {
517                 const cfg_listelt_t *element;
518                 in_port_t port;
519         } *stack = NULL;
520
521         REQUIRE(addrsp != NULL && *addrsp == NULL);
522         REQUIRE(keysp != NULL && *keysp == NULL);
523         REQUIRE(countp != NULL);
524
525  newlist:
526         addrlist = cfg_tuple_get(list, "addresses");
527         portobj = cfg_tuple_get(list, "port");
528         if (cfg_obj_isuint32(portobj)) {
529                 isc_uint32_t val = cfg_obj_asuint32(portobj);
530                 if (val > ISC_UINT16_MAX) {
531                         cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
532                                     "port '%u' out of range", val);
533                         result = ISC_R_RANGE;
534                         goto cleanup;
535                 }
536                 port = (in_port_t) val;
537         } else {
538                 result = ns_config_getport(config, &port);
539                 if (result != ISC_R_SUCCESS)
540                         goto cleanup;
541         }
542
543         result = ISC_R_NOMEMORY;
544
545         element = cfg_list_first(addrlist);
546  resume:
547         for ( ;
548              element != NULL;
549              element = cfg_list_next(element))
550         {
551                 const cfg_obj_t *addr;
552                 const cfg_obj_t *key;
553                 const char *keystr;
554                 isc_buffer_t b;
555
556                 addr = cfg_tuple_get(cfg_listelt_value(element),
557                                      "masterselement");
558                 key = cfg_tuple_get(cfg_listelt_value(element), "key");
559
560                 if (!cfg_obj_issockaddr(addr)) {
561                         const char *listname = cfg_obj_asstring(addr);
562                         isc_result_t tresult;
563
564                         /* Grow lists? */
565                         if (listcount == l) {
566                                 void * new;
567                                 isc_uint32_t newlen = listcount + 16;
568                                 size_t newsize, oldsize;
569
570                                 newsize = newlen * sizeof(*lists);
571                                 oldsize = listcount * sizeof(*lists);
572                                 new = isc_mem_get(mctx, newsize);
573                                 if (new == NULL)
574                                         goto cleanup;
575                                 if (listcount != 0) {
576                                         memmove(new, lists, oldsize);
577                                         isc_mem_put(mctx, lists, oldsize);
578                                 }
579                                 lists = new;
580                                 listcount = newlen;
581                         }
582                         /* Seen? */
583                         for (j = 0; j < l; j++)
584                                 if (strcasecmp(lists[j].name, listname) == 0)
585                                         break;
586                         if (j < l)
587                                 continue;
588                         tresult = get_masters_def(config, listname, &list);
589                         if (tresult == ISC_R_NOTFOUND) {
590                                 cfg_obj_log(addr, ns_g_lctx, ISC_LOG_ERROR,
591                                     "masters \"%s\" not found", listname);
592
593                                 result = tresult;
594                                 goto cleanup;
595                         }
596                         if (tresult != ISC_R_SUCCESS)
597                                 goto cleanup;
598                         lists[l++].name = listname;
599                         /* Grow stack? */
600                         if (stackcount == pushed) {
601                                 void * new;
602                                 isc_uint32_t newlen = stackcount + 16;
603                                 size_t newsize, oldsize;
604
605                                 newsize = newlen * sizeof(*stack);
606                                 oldsize = stackcount * sizeof(*stack);
607                                 new = isc_mem_get(mctx, newsize);
608                                 if (new == NULL)
609                                         goto cleanup;
610                                 if (stackcount != 0) {
611                                         memmove(new, stack, oldsize);
612                                         isc_mem_put(mctx, stack, oldsize);
613                                 }
614                                 stack = new;
615                                 stackcount = newlen;
616                         }
617                         /*
618                          * We want to resume processing this list on the
619                          * next element.
620                          */
621                         stack[pushed].element = cfg_list_next(element);
622                         stack[pushed].port = port;
623                         pushed++;
624                         goto newlist;
625                 }
626
627                 if (i == addrcount) {
628                         void * new;
629                         isc_uint32_t newlen = addrcount + 16;
630                         size_t newsize, oldsize;
631
632                         newsize = newlen * sizeof(isc_sockaddr_t);
633                         oldsize = addrcount * sizeof(isc_sockaddr_t);
634                         new = isc_mem_get(mctx, newsize);
635                         if (new == NULL)
636                                 goto cleanup;
637                         if (addrcount != 0) {
638                                 memmove(new, addrs, oldsize);
639                                 isc_mem_put(mctx, addrs, oldsize);
640                         }
641                         addrs = new;
642                         addrcount = newlen;
643
644                         newsize = newlen * sizeof(dns_name_t *);
645                         oldsize = keycount * sizeof(dns_name_t *);
646                         new = isc_mem_get(mctx, newsize);
647                         if (new == NULL)
648                                 goto cleanup;
649                         if (keycount != 0) {
650                                 memmove(new, keys, oldsize);
651                                 isc_mem_put(mctx, keys, oldsize);
652                         }
653                         keys = new;
654                         keycount = newlen;
655                 }
656
657                 addrs[i] = *cfg_obj_assockaddr(addr);
658                 if (isc_sockaddr_getport(&addrs[i]) == 0)
659                         isc_sockaddr_setport(&addrs[i], port);
660                 keys[i] = NULL;
661                 i++;    /* Increment here so that cleanup on error works. */
662                 if (!cfg_obj_isstring(key))
663                         continue;
664                 keys[i - 1] = isc_mem_get(mctx, sizeof(dns_name_t));
665                 if (keys[i - 1] == NULL)
666                         goto cleanup;
667                 dns_name_init(keys[i - 1], NULL);
668
669                 keystr = cfg_obj_asstring(key);
670                 isc_buffer_constinit(&b, keystr, strlen(keystr));
671                 isc_buffer_add(&b, strlen(keystr));
672                 dns_fixedname_init(&fname);
673                 result = dns_name_fromtext(dns_fixedname_name(&fname), &b,
674                                            dns_rootname, 0, NULL);
675                 if (result != ISC_R_SUCCESS)
676                         goto cleanup;
677                 result = dns_name_dup(dns_fixedname_name(&fname), mctx,
678                                       keys[i - 1]);
679                 if (result != ISC_R_SUCCESS)
680                         goto cleanup;
681         }
682         if (pushed != 0) {
683                 pushed--;
684                 element = stack[pushed].element;
685                 port = stack[pushed].port;
686                 goto resume;
687         }
688         if (i < addrcount) {
689                 void * new;
690                 size_t newsize, oldsize;
691
692                 newsize = i * sizeof(isc_sockaddr_t);
693                 oldsize = addrcount * sizeof(isc_sockaddr_t);
694                 if (i != 0) {
695                         new = isc_mem_get(mctx, newsize);
696                         if (new == NULL)
697                                 goto cleanup;
698                         memmove(new, addrs, newsize);
699                 } else
700                         new = NULL;
701                 isc_mem_put(mctx, addrs, oldsize);
702                 addrs = new;
703                 addrcount = i;
704
705                 newsize = i * sizeof(dns_name_t *);
706                 oldsize = keycount * sizeof(dns_name_t *);
707                 if (i != 0) {
708                         new = isc_mem_get(mctx, newsize);
709                         if (new == NULL)
710                                 goto cleanup;
711                         memmove(new, keys,  newsize);
712                 } else
713                         new = NULL;
714                 isc_mem_put(mctx, keys, oldsize);
715                 keys = new;
716                 keycount = i;
717         }
718
719         if (lists != NULL)
720                 isc_mem_put(mctx, lists, listcount * sizeof(*lists));
721         if (stack != NULL)
722                 isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
723
724         INSIST(keycount == addrcount);
725
726         *addrsp = addrs;
727         *keysp = keys;
728         *countp = addrcount;
729
730         return (ISC_R_SUCCESS);
731
732  cleanup:
733         if (addrs != NULL)
734                 isc_mem_put(mctx, addrs, addrcount * sizeof(isc_sockaddr_t));
735         if (keys != NULL) {
736                 for (j = 0; j < i; j++) {
737                         if (keys[j] == NULL)
738                                 continue;
739                         if (dns_name_dynamic(keys[j]))
740                                 dns_name_free(keys[j], mctx);
741                         isc_mem_put(mctx, keys[j], sizeof(dns_name_t));
742                 }
743                 isc_mem_put(mctx, keys, keycount * sizeof(dns_name_t *));
744         }
745         if (lists != NULL)
746                 isc_mem_put(mctx, lists, listcount * sizeof(*lists));
747         if (stack != NULL)
748                 isc_mem_put(mctx, stack, stackcount * sizeof(*stack));
749         return (result);
750 }
751
752 void
753 ns_config_putipandkeylist(isc_mem_t *mctx, isc_sockaddr_t **addrsp,
754                           dns_name_t ***keysp, isc_uint32_t count)
755 {
756         unsigned int i;
757         dns_name_t **keys = *keysp;
758
759         INSIST(addrsp != NULL && *addrsp != NULL);
760
761         isc_mem_put(mctx, *addrsp, count * sizeof(isc_sockaddr_t));
762         for (i = 0; i < count; i++) {
763                 if (keys[i] == NULL)
764                         continue;
765                 if (dns_name_dynamic(keys[i]))
766                         dns_name_free(keys[i], mctx);
767                 isc_mem_put(mctx, keys[i], sizeof(dns_name_t));
768         }
769         isc_mem_put(mctx, *keysp, count * sizeof(dns_name_t *));
770         *addrsp = NULL;
771         *keysp = NULL;
772 }
773
774 isc_result_t
775 ns_config_getport(const cfg_obj_t *config, in_port_t *portp) {
776         const cfg_obj_t *maps[3];
777         const cfg_obj_t *options = NULL;
778         const cfg_obj_t *portobj = NULL;
779         isc_result_t result;
780         int i;
781
782         (void)cfg_map_get(config, "options", &options);
783         i = 0;
784         if (options != NULL)
785                 maps[i++] = options;
786         maps[i++] = ns_g_defaults;
787         maps[i] = NULL;
788
789         result = ns_config_get(maps, "port", &portobj);
790         INSIST(result == ISC_R_SUCCESS);
791         if (cfg_obj_asuint32(portobj) >= ISC_UINT16_MAX) {
792                 cfg_obj_log(portobj, ns_g_lctx, ISC_LOG_ERROR,
793                             "port '%u' out of range",
794                             cfg_obj_asuint32(portobj));
795                 return (ISC_R_RANGE);
796         }
797         *portp = (in_port_t)cfg_obj_asuint32(portobj);
798         return (ISC_R_SUCCESS);
799 }
800
801 struct keyalgorithms {
802         const char *str;
803         enum { hmacnone, hmacmd5, hmacsha1, hmacsha224,
804                hmacsha256, hmacsha384, hmacsha512 } hmac;
805         unsigned int type;
806         isc_uint16_t size;
807 } algorithms[] = {
808         { "hmac-md5", hmacmd5, DST_ALG_HMACMD5, 128 },
809         { "hmac-md5.sig-alg.reg.int", hmacmd5, DST_ALG_HMACMD5, 0 },
810         { "hmac-md5.sig-alg.reg.int.", hmacmd5, DST_ALG_HMACMD5, 0 },
811         { "hmac-sha1", hmacsha1, DST_ALG_HMACSHA1, 160 },
812         { "hmac-sha224", hmacsha224, DST_ALG_HMACSHA224, 224 },
813         { "hmac-sha256", hmacsha256, DST_ALG_HMACSHA256, 256 },
814         { "hmac-sha384", hmacsha384, DST_ALG_HMACSHA384, 384 },
815         { "hmac-sha512", hmacsha512, DST_ALG_HMACSHA512, 512 },
816         {  NULL, hmacnone, DST_ALG_UNKNOWN, 0 }
817 };
818
819 isc_result_t
820 ns_config_getkeyalgorithm(const char *str, dns_name_t **name,
821                           isc_uint16_t *digestbits)
822 {
823         return (ns_config_getkeyalgorithm2(str, name, NULL, digestbits));
824 }
825
826 isc_result_t
827 ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
828                            unsigned int *typep, isc_uint16_t *digestbits)
829 {
830         int i;
831         size_t len = 0;
832         isc_uint16_t bits;
833         isc_result_t result;
834
835         for (i = 0; algorithms[i].str != NULL; i++) {
836                 len = strlen(algorithms[i].str);
837                 if (strncasecmp(algorithms[i].str, str, len) == 0 &&
838                     (str[len] == '\0' ||
839                      (algorithms[i].size != 0 && str[len] == '-')))
840                         break;
841         }
842         if (algorithms[i].str == NULL)
843                 return (ISC_R_NOTFOUND);
844         if (str[len] == '-') {
845                 result = isc_parse_uint16(&bits, str + len + 1, 10);
846                 if (result != ISC_R_SUCCESS)
847                         return (result);
848                 if (bits > algorithms[i].size)
849                         return (ISC_R_RANGE);
850         } else if (algorithms[i].size == 0)
851                 bits = 128;
852         else
853                 bits = algorithms[i].size;
854
855         if (name != NULL) {
856                 switch (algorithms[i].hmac) {
857                 case hmacmd5: *name = dns_tsig_hmacmd5_name; break;
858                 case hmacsha1: *name = dns_tsig_hmacsha1_name; break;
859                 case hmacsha224: *name = dns_tsig_hmacsha224_name; break;
860                 case hmacsha256: *name = dns_tsig_hmacsha256_name; break;
861                 case hmacsha384: *name = dns_tsig_hmacsha384_name; break;
862                 case hmacsha512: *name = dns_tsig_hmacsha512_name; break;
863                 default:
864                         INSIST(0);
865                 }
866         }
867         if (typep != NULL)
868                 *typep = algorithms[i].type;
869         if (digestbits != NULL)
870                 *digestbits = bits;
871         return (ISC_R_SUCCESS);
872 }