]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - contrib/bind9/lib/isccfg/namedconf.c
MFC: r253983-253984
[FreeBSD/stable/8.git] / contrib / bind9 / lib / isccfg / namedconf.c
1 /*
2  * Copyright (C) 2004-2014  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2002, 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$ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <isc/lex.h>
27 #include <isc/mem.h>
28 #include <isc/result.h>
29 #include <isc/string.h>
30 #include <isc/util.h>
31
32 #include <isccfg/cfg.h>
33 #include <isccfg/grammar.h>
34 #include <isccfg/log.h>
35
36 #define TOKEN_STRING(pctx) (pctx->token.value.as_textregion.base)
37
38 /*% Check a return value. */
39 #define CHECK(op)                                               \
40         do { result = (op);                                     \
41                 if (result != ISC_R_SUCCESS) goto cleanup;      \
42         } while (0)
43
44 /*% Clean up a configuration object if non-NULL. */
45 #define CLEANUP_OBJ(obj) \
46         do { if ((obj) != NULL) cfg_obj_destroy(pctx, &(obj)); } while (0)
47
48
49 /*%
50  * Forward declarations of static functions.
51  */
52
53 static isc_result_t
54 parse_enum_or_other(cfg_parser_t *pctx, const cfg_type_t *enumtype,
55                     const cfg_type_t *othertype, cfg_obj_t **ret);
56
57 static isc_result_t
58 parse_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
59
60 static isc_result_t
61 parse_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type,
62                         cfg_obj_t **ret);
63
64 static isc_result_t
65 parse_updatepolicy(cfg_parser_t *pctx, const cfg_type_t *type,
66                    cfg_obj_t **ret);
67 static void
68 print_updatepolicy(cfg_printer_t *pctx, const cfg_obj_t *obj);
69
70 static void
71 doc_updatepolicy(cfg_printer_t *pctx, const cfg_type_t *type);
72
73 static void
74 print_keyvalue(cfg_printer_t *pctx, const cfg_obj_t *obj);
75
76 static void
77 doc_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type);
78
79 static void
80 doc_optional_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type);
81
82 static cfg_type_t cfg_type_acl;
83 static cfg_type_t cfg_type_addrmatchelt;
84 static cfg_type_t cfg_type_bracketed_aml;
85 static cfg_type_t cfg_type_bracketed_namesockaddrkeylist;
86 static cfg_type_t cfg_type_bracketed_sockaddrlist;
87 static cfg_type_t cfg_type_bracketed_sockaddrnameportlist;
88 static cfg_type_t cfg_type_controls;
89 static cfg_type_t cfg_type_controls_sockaddr;
90 static cfg_type_t cfg_type_destinationlist;
91 static cfg_type_t cfg_type_dialuptype;
92 static cfg_type_t cfg_type_ixfrdifftype;
93 static cfg_type_t cfg_type_key;
94 static cfg_type_t cfg_type_logfile;
95 static cfg_type_t cfg_type_logging;
96 static cfg_type_t cfg_type_logseverity;
97 static cfg_type_t cfg_type_lwres;
98 static cfg_type_t cfg_type_masterselement;
99 static cfg_type_t cfg_type_nameportiplist;
100 static cfg_type_t cfg_type_negated;
101 static cfg_type_t cfg_type_notifytype;
102 static cfg_type_t cfg_type_optional_allow;
103 static cfg_type_t cfg_type_optional_class;
104 static cfg_type_t cfg_type_optional_facility;
105 static cfg_type_t cfg_type_optional_keyref;
106 static cfg_type_t cfg_type_optional_port;
107 static cfg_type_t cfg_type_options;
108 static cfg_type_t cfg_type_portiplist;
109 static cfg_type_t cfg_type_querysource4;
110 static cfg_type_t cfg_type_querysource6;
111 static cfg_type_t cfg_type_querysource;
112 static cfg_type_t cfg_type_server;
113 static cfg_type_t cfg_type_server_key_kludge;
114 static cfg_type_t cfg_type_size;
115 static cfg_type_t cfg_type_sizenodefault;
116 static cfg_type_t cfg_type_sockaddr4wild;
117 static cfg_type_t cfg_type_sockaddr6wild;
118 static cfg_type_t cfg_type_statschannels;
119 static cfg_type_t cfg_type_view;
120 static cfg_type_t cfg_type_viewopts;
121 static cfg_type_t cfg_type_zone;
122 static cfg_type_t cfg_type_zoneopts;
123 static cfg_type_t cfg_type_dynamically_loadable_zones;
124 static cfg_type_t cfg_type_dynamically_loadable_zones_opts;
125 static cfg_type_t cfg_type_v4_aaaa;
126
127 /*
128  * Clauses that can be found in a 'dynamically loadable zones' statement
129  */
130 static cfg_clausedef_t
131 dynamically_loadable_zones_clauses[] = {
132         { "database", &cfg_type_astring, 0 },
133         { NULL, NULL, 0 }
134 };
135
136 /*
137  * A dynamically loadable zones statement.
138  */
139 static cfg_tuplefielddef_t dynamically_loadable_zones_fields[] = {
140         { "name", &cfg_type_astring, 0 },
141         { "options", &cfg_type_dynamically_loadable_zones_opts, 0 },
142         { NULL, NULL, 0 }
143 };
144
145 static cfg_type_t cfg_type_dynamically_loadable_zones = {
146         "dlz", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
147         &cfg_rep_tuple,
148         dynamically_loadable_zones_fields
149         };
150
151
152 /*% tkey-dhkey */
153
154 static cfg_tuplefielddef_t tkey_dhkey_fields[] = {
155         { "name", &cfg_type_qstring, 0 },
156         { "keyid", &cfg_type_uint32, 0 },
157         { NULL, NULL, 0 }
158 };
159
160 static cfg_type_t cfg_type_tkey_dhkey = {
161         "tkey-dhkey", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
162         tkey_dhkey_fields
163 };
164
165 /*% listen-on */
166
167 static cfg_tuplefielddef_t listenon_fields[] = {
168         { "port", &cfg_type_optional_port, 0 },
169         { "acl", &cfg_type_bracketed_aml, 0 },
170         { NULL, NULL, 0 }
171 };
172 static cfg_type_t cfg_type_listenon = {
173         "listenon", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, listenon_fields };
174
175 /*% acl */
176
177 static cfg_tuplefielddef_t acl_fields[] = {
178         { "name", &cfg_type_astring, 0 },
179         { "value", &cfg_type_bracketed_aml, 0 },
180         { NULL, NULL, 0 }
181 };
182
183 static cfg_type_t cfg_type_acl = {
184         "acl", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, acl_fields };
185
186 /*% masters */
187 static cfg_tuplefielddef_t masters_fields[] = {
188         { "name", &cfg_type_astring, 0 },
189         { "port", &cfg_type_optional_port, 0 },
190         { "addresses", &cfg_type_bracketed_namesockaddrkeylist, 0 },
191         { NULL, NULL, 0 }
192 };
193
194 static cfg_type_t cfg_type_masters = {
195         "masters", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, masters_fields };
196
197 /*%
198  * "sockaddrkeylist", a list of socket addresses with optional keys
199  * and an optional default port, as used in the masters option.
200  * E.g.,
201  *   "port 1234 { mymasters; 10.0.0.1 key foo; 1::2 port 69; }"
202  */
203
204 static cfg_tuplefielddef_t namesockaddrkey_fields[] = {
205         { "masterselement", &cfg_type_masterselement, 0 },
206         { "key", &cfg_type_optional_keyref, 0 },
207         { NULL, NULL, 0 },
208 };
209
210 static cfg_type_t cfg_type_namesockaddrkey = {
211         "namesockaddrkey", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
212         namesockaddrkey_fields
213 };
214
215 static cfg_type_t cfg_type_bracketed_namesockaddrkeylist = {
216         "bracketed_namesockaddrkeylist", cfg_parse_bracketed_list,
217         cfg_print_bracketed_list, cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_namesockaddrkey
218 };
219
220 static cfg_tuplefielddef_t namesockaddrkeylist_fields[] = {
221         { "port", &cfg_type_optional_port, 0 },
222         { "addresses", &cfg_type_bracketed_namesockaddrkeylist, 0 },
223         { NULL, NULL, 0 }
224 };
225 static cfg_type_t cfg_type_namesockaddrkeylist = {
226         "sockaddrkeylist", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
227         namesockaddrkeylist_fields
228 };
229
230 /*%
231  * A list of socket addresses with an optional default port,
232  * as used in the also-notify option.  E.g.,
233  * "port 1234 { 10.0.0.1; 1::2 port 69; }"
234  */
235 static cfg_tuplefielddef_t portiplist_fields[] = {
236         { "port", &cfg_type_optional_port, 0 },
237         { "addresses", &cfg_type_bracketed_sockaddrlist, 0 },
238         { NULL, NULL, 0 }
239 };
240 static cfg_type_t cfg_type_portiplist = {
241         "portiplist", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
242         portiplist_fields
243 };
244
245 /*%
246  * A public key, as in the "pubkey" statement.
247  */
248 static cfg_tuplefielddef_t pubkey_fields[] = {
249         { "flags", &cfg_type_uint32, 0 },
250         { "protocol", &cfg_type_uint32, 0 },
251         { "algorithm", &cfg_type_uint32, 0 },
252         { "key", &cfg_type_qstring, 0 },
253         { NULL, NULL, 0 }
254 };
255 static cfg_type_t cfg_type_pubkey = {
256         "pubkey", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
257         &cfg_rep_tuple, pubkey_fields };
258
259 /*%
260  * A list of RR types, used in grant statements.
261  * Note that the old parser allows quotes around the RR type names.
262  */
263 static cfg_type_t cfg_type_rrtypelist = {
264         "rrtypelist", cfg_parse_spacelist, cfg_print_spacelist,
265         cfg_doc_terminal, &cfg_rep_list, &cfg_type_astring
266 };
267
268 static const char *mode_enums[] = { "grant", "deny", NULL };
269 static cfg_type_t cfg_type_mode = {
270         "mode", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
271         &cfg_rep_string, &mode_enums
272 };
273
274 static isc_result_t
275 parse_matchtype(cfg_parser_t *pctx, const cfg_type_t *type,
276                 cfg_obj_t **ret) {
277         isc_result_t result;
278
279         CHECK(cfg_peektoken(pctx, 0));
280         if (pctx->token.type == isc_tokentype_string &&
281             strcasecmp(TOKEN_STRING(pctx), "zonesub") == 0) {
282                 pctx->flags |= CFG_PCTX_SKIP;
283         }
284         return (cfg_parse_enum(pctx, type, ret));
285
286  cleanup:
287         return (result);
288 }
289
290 static isc_result_t
291 parse_matchname(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
292         isc_result_t result;
293         cfg_obj_t *obj = NULL;
294
295         if ((pctx->flags & CFG_PCTX_SKIP) != 0) {
296                 pctx->flags &= ~CFG_PCTX_SKIP;
297                 CHECK(cfg_parse_void(pctx, NULL, &obj));
298         } else
299                 result = cfg_parse_astring(pctx, type, &obj);
300
301         *ret = obj;
302  cleanup:
303         return (result);
304 }
305
306 static void
307 doc_matchname(cfg_printer_t *pctx, const cfg_type_t *type) {
308         cfg_print_chars(pctx, "[ ", 2);
309         cfg_doc_obj(pctx, type->of);
310         cfg_print_chars(pctx, " ]", 2);
311 }
312
313 static const char *matchtype_enums[] = {
314         "name", "subdomain", "wildcard", "self", "selfsub", "selfwild",
315         "krb5-self", "ms-self", "krb5-subdomain", "ms-subdomain",
316         "tcp-self", "6to4-self", "zonesub", "external", NULL };
317
318 static cfg_type_t cfg_type_matchtype = {
319         "matchtype", parse_matchtype, cfg_print_ustring,
320         cfg_doc_enum, &cfg_rep_string, &matchtype_enums
321 };
322
323 static cfg_type_t cfg_type_matchname = {
324         "optional_matchname", parse_matchname, cfg_print_ustring,
325         &doc_matchname, &cfg_rep_tuple, &cfg_type_ustring
326 };
327
328 /*%
329  * A grant statement, used in the update policy.
330  */
331 static cfg_tuplefielddef_t grant_fields[] = {
332         { "mode", &cfg_type_mode, 0 },
333         { "identity", &cfg_type_astring, 0 }, /* domain name */
334         { "matchtype", &cfg_type_matchtype, 0 },
335         { "name", &cfg_type_matchname, 0 }, /* domain name */
336         { "types", &cfg_type_rrtypelist, 0 },
337         { NULL, NULL, 0 }
338 };
339 static cfg_type_t cfg_type_grant = {
340         "grant", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
341          &cfg_rep_tuple, grant_fields
342 };
343
344 static cfg_type_t cfg_type_updatepolicy = {
345         "update_policy", parse_updatepolicy, print_updatepolicy,
346         doc_updatepolicy, &cfg_rep_list, &cfg_type_grant
347 };
348
349 static isc_result_t
350 parse_updatepolicy(cfg_parser_t *pctx, const cfg_type_t *type,
351                    cfg_obj_t **ret) {
352         isc_result_t result;
353         CHECK(cfg_gettoken(pctx, 0));
354         if (pctx->token.type == isc_tokentype_special &&
355             pctx->token.value.as_char == '{') {
356                 cfg_ungettoken(pctx);
357                 return (cfg_parse_bracketed_list(pctx, type, ret));
358         }
359
360         if (pctx->token.type == isc_tokentype_string &&
361             strcasecmp(TOKEN_STRING(pctx), "local") == 0) {
362                 cfg_obj_t *obj = NULL;
363                 CHECK(cfg_create_obj(pctx, &cfg_type_ustring, &obj));
364                 obj->value.string.length = strlen("local");
365                 obj->value.string.base  = isc_mem_get(pctx->mctx,
366                                                 obj->value.string.length + 1);
367                 if (obj->value.string.base == NULL) {
368                         isc_mem_put(pctx->mctx, obj, sizeof(*obj));
369                         return (ISC_R_NOMEMORY);
370                 }
371                 memmove(obj->value.string.base, "local", 5);
372                 obj->value.string.base[5] = '\0';
373                 *ret = obj;
374                 return (ISC_R_SUCCESS);
375         }
376
377         cfg_ungettoken(pctx);
378         return (ISC_R_UNEXPECTEDTOKEN);
379
380  cleanup:
381         return (result);
382 }
383
384 static void
385 print_updatepolicy(cfg_printer_t *pctx, const cfg_obj_t *obj) {
386         if (cfg_obj_isstring(obj))
387                 cfg_print_ustring(pctx, obj);
388         else
389                 cfg_print_bracketed_list(pctx, obj);
390 }
391
392 static void
393 doc_updatepolicy(cfg_printer_t *pctx, const cfg_type_t *type) {
394         cfg_print_cstr(pctx, "( local | { ");
395         cfg_doc_obj(pctx, type->of);
396         cfg_print_cstr(pctx, "; ... }");
397 }
398
399 /*%
400  * A view statement.
401  */
402 static cfg_tuplefielddef_t view_fields[] = {
403         { "name", &cfg_type_astring, 0 },
404         { "class", &cfg_type_optional_class, 0 },
405         { "options", &cfg_type_viewopts, 0 },
406         { NULL, NULL, 0 }
407 };
408 static cfg_type_t cfg_type_view = {
409         "view", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
410          &cfg_rep_tuple, view_fields
411 };
412
413 /*%
414  * A zone statement.
415  */
416 static cfg_tuplefielddef_t zone_fields[] = {
417         { "name", &cfg_type_astring, 0 },
418         { "class", &cfg_type_optional_class, 0 },
419         { "options", &cfg_type_zoneopts, 0 },
420         { NULL, NULL, 0 }
421 };
422 static cfg_type_t cfg_type_zone = {
423         "zone", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
424         &cfg_rep_tuple, zone_fields
425 };
426
427 /*%
428  * A "category" clause in the "logging" statement.
429  */
430 static cfg_tuplefielddef_t category_fields[] = {
431         { "name", &cfg_type_astring, 0 },
432         { "destinations", &cfg_type_destinationlist,0 },
433         { NULL, NULL, 0 }
434 };
435 static cfg_type_t cfg_type_category = {
436         "category", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
437         &cfg_rep_tuple, category_fields
438 };
439
440
441 /*%
442  * A dnssec key, as used in the "trusted-keys" statement.
443  */
444 static cfg_tuplefielddef_t dnsseckey_fields[] = {
445         { "name", &cfg_type_astring, 0 },
446         { "flags", &cfg_type_uint32, 0 },
447         { "protocol", &cfg_type_uint32, 0 },
448         { "algorithm", &cfg_type_uint32, 0 },
449         { "key", &cfg_type_qstring, 0 },
450         { NULL, NULL, 0 }
451 };
452 static cfg_type_t cfg_type_dnsseckey = {
453         "dnsseckey", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
454         &cfg_rep_tuple, dnsseckey_fields
455 };
456
457 /*%
458  * A managed key initialization specifier, as used in the
459  * "managed-keys" statement.
460  */
461 static cfg_tuplefielddef_t managedkey_fields[] = {
462         { "name", &cfg_type_astring, 0 },
463         { "init", &cfg_type_ustring, 0 },   /* must be literal "initial-key" */
464         { "flags", &cfg_type_uint32, 0 },
465         { "protocol", &cfg_type_uint32, 0 },
466         { "algorithm", &cfg_type_uint32, 0 },
467         { "key", &cfg_type_qstring, 0 },
468         { NULL, NULL, 0 }
469 };
470 static cfg_type_t cfg_type_managedkey = {
471         "managedkey", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
472         &cfg_rep_tuple, managedkey_fields
473 };
474
475 static keyword_type_t wild_class_kw = { "class", &cfg_type_ustring };
476
477 static cfg_type_t cfg_type_optional_wild_class = {
478         "optional_wild_class", parse_optional_keyvalue, print_keyvalue,
479         doc_optional_keyvalue, &cfg_rep_string, &wild_class_kw
480 };
481
482 static keyword_type_t wild_type_kw = { "type", &cfg_type_ustring };
483
484 static cfg_type_t cfg_type_optional_wild_type = {
485         "optional_wild_type", parse_optional_keyvalue,
486         print_keyvalue, doc_optional_keyvalue, &cfg_rep_string, &wild_type_kw
487 };
488
489 static keyword_type_t wild_name_kw = { "name", &cfg_type_qstring };
490
491 static cfg_type_t cfg_type_optional_wild_name = {
492         "optional_wild_name", parse_optional_keyvalue,
493         print_keyvalue, doc_optional_keyvalue, &cfg_rep_string, &wild_name_kw
494 };
495
496 /*%
497  * An rrset ordering element.
498  */
499 static cfg_tuplefielddef_t rrsetorderingelement_fields[] = {
500         { "class", &cfg_type_optional_wild_class, 0 },
501         { "type", &cfg_type_optional_wild_type, 0 },
502         { "name", &cfg_type_optional_wild_name, 0 },
503         { "order", &cfg_type_ustring, 0 }, /* must be literal "order" */
504         { "ordering", &cfg_type_ustring, 0 },
505         { NULL, NULL, 0 }
506 };
507 static cfg_type_t cfg_type_rrsetorderingelement = {
508         "rrsetorderingelement", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
509         rrsetorderingelement_fields
510 };
511
512 /*%
513  * A global or view "check-names" option.  Note that the zone
514  * "check-names" option has a different syntax.
515  */
516
517 static const char *checktype_enums[] = { "master", "slave", "response", NULL };
518 static cfg_type_t cfg_type_checktype = {
519         "checktype", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
520         &cfg_rep_string, &checktype_enums
521 };
522
523 static const char *checkmode_enums[] = { "fail", "warn", "ignore", NULL };
524 static cfg_type_t cfg_type_checkmode = {
525         "checkmode", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
526         &cfg_rep_string, &checkmode_enums
527 };
528
529 static const char *warn_enums[] = { "warn", "ignore", NULL };
530 static cfg_type_t cfg_type_warn = {
531         "warn", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
532         &cfg_rep_string, &warn_enums
533 };
534
535 static cfg_tuplefielddef_t checknames_fields[] = {
536         { "type", &cfg_type_checktype, 0 },
537         { "mode", &cfg_type_checkmode, 0 },
538         { NULL, NULL, 0 }
539 };
540
541 static cfg_type_t cfg_type_checknames = {
542         "checknames", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
543         checknames_fields
544 };
545
546 static cfg_type_t cfg_type_bracketed_sockaddrlist = {
547         "bracketed_sockaddrlist", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
548         &cfg_rep_list, &cfg_type_sockaddr
549 };
550
551 static const char *autodnssec_enums[] = { "allow", "maintain", "off", NULL };
552 static cfg_type_t cfg_type_autodnssec = {
553         "autodnssec", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
554         &cfg_rep_string, &autodnssec_enums
555 };
556
557 static cfg_type_t cfg_type_rrsetorder = {
558         "rrsetorder", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
559         &cfg_rep_list, &cfg_type_rrsetorderingelement
560 };
561
562 static keyword_type_t port_kw = { "port", &cfg_type_uint32 };
563
564 static cfg_type_t cfg_type_optional_port = {
565         "optional_port", parse_optional_keyvalue, print_keyvalue,
566         doc_optional_keyvalue, &cfg_rep_uint32, &port_kw
567 };
568
569 /*% A list of keys, as in the "key" clause of the controls statement. */
570 static cfg_type_t cfg_type_keylist = {
571         "keylist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
572         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_astring
573 };
574
575 /*% A list of dnssec keys, as in "trusted-keys" */
576 static cfg_type_t cfg_type_dnsseckeys = {
577         "dnsseckeys", cfg_parse_bracketed_list, cfg_print_bracketed_list,
578         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_dnsseckey
579 };
580
581 /*%
582  * A list of managed key entries, as in "trusted-keys".  Currently
583  * (9.7.0) this has a format similar to dnssec keys, except the keyname
584  * is followed by the keyword "initial-key".  In future releases, this
585  * keyword may take other values indicating different methods for the
586  * key to be initialized.
587  */
588
589 static cfg_type_t cfg_type_managedkeys = {
590         "managedkeys", cfg_parse_bracketed_list, cfg_print_bracketed_list,
591         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_managedkey
592 };
593
594 static const char *forwardtype_enums[] = { "first", "only", NULL };
595 static cfg_type_t cfg_type_forwardtype = {
596         "forwardtype", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
597         &forwardtype_enums
598 };
599
600 static const char *zonetype_enums[] = {
601         "master", "slave", "stub", "static-stub", "hint", "forward",
602         "delegation-only", NULL };
603 static cfg_type_t cfg_type_zonetype = {
604         "zonetype", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
605         &cfg_rep_string, &zonetype_enums
606 };
607
608 static const char *loglevel_enums[] = {
609         "critical", "error", "warning", "notice", "info", "dynamic", NULL };
610 static cfg_type_t cfg_type_loglevel = {
611         "loglevel", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
612         &loglevel_enums
613 };
614
615 static const char *transferformat_enums[] = {
616         "many-answers", "one-answer", NULL };
617 static cfg_type_t cfg_type_transferformat = {
618         "transferformat", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
619         &transferformat_enums
620 };
621
622 /*%
623  * The special keyword "none", as used in the pid-file option.
624  */
625
626 static void
627 print_none(cfg_printer_t *pctx, const cfg_obj_t *obj) {
628         UNUSED(obj);
629         cfg_print_cstr(pctx, "none");
630 }
631
632 static cfg_type_t cfg_type_none = {
633         "none", NULL, print_none, NULL, &cfg_rep_void, NULL
634 };
635
636 /*%
637  * A quoted string or the special keyword "none".  Used in the pid-file option.
638  */
639 static isc_result_t
640 parse_qstringornone(cfg_parser_t *pctx, const cfg_type_t *type,
641                     cfg_obj_t **ret)
642 {
643         isc_result_t result;
644
645         CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
646         if (pctx->token.type == isc_tokentype_string &&
647             strcasecmp(TOKEN_STRING(pctx), "none") == 0)
648                 return (cfg_create_obj(pctx, &cfg_type_none, ret));
649         cfg_ungettoken(pctx);
650         return (cfg_parse_qstring(pctx, type, ret));
651  cleanup:
652         return (result);
653 }
654
655 static void
656 doc_qstringornone(cfg_printer_t *pctx, const cfg_type_t *type) {
657         UNUSED(type);
658         cfg_print_cstr(pctx, "( <quoted_string> | none )");
659 }
660
661 static cfg_type_t cfg_type_qstringornone = {
662         "qstringornone", parse_qstringornone, NULL, doc_qstringornone,
663         NULL, NULL
664 };
665
666 /*%
667  * A boolean ("yes" or "no"), or the special keyword "auto".
668  * Used in the dnssec-validation option.
669  */
670 static void
671 print_auto(cfg_printer_t *pctx, const cfg_obj_t *obj) {
672         UNUSED(obj);
673         cfg_print_cstr(pctx, "auto");
674 }
675
676 static cfg_type_t cfg_type_auto = {
677         "auto", NULL, print_auto, NULL, &cfg_rep_void, NULL
678 };
679
680 static isc_result_t
681 parse_boolorauto(cfg_parser_t *pctx, const cfg_type_t *type,
682                     cfg_obj_t **ret)
683 {
684         isc_result_t result;
685
686         CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
687         if (pctx->token.type == isc_tokentype_string &&
688             strcasecmp(TOKEN_STRING(pctx), "auto") == 0)
689                 return (cfg_create_obj(pctx, &cfg_type_auto, ret));
690         cfg_ungettoken(pctx);
691         return (cfg_parse_boolean(pctx, type, ret));
692  cleanup:
693         return (result);
694 }
695
696 static void
697 print_boolorauto(cfg_printer_t *pctx, const cfg_obj_t *obj) {
698         if (obj->type->rep == &cfg_rep_void)
699                 cfg_print_chars(pctx, "auto", 4);
700         else if (obj->value.boolean)
701                 cfg_print_chars(pctx, "yes", 3);
702         else
703                 cfg_print_chars(pctx, "no", 2);
704 }
705
706 static void
707 doc_boolorauto(cfg_printer_t *pctx, const cfg_type_t *type) {
708         UNUSED(type);
709         cfg_print_cstr(pctx, "( yes | no | auto )");
710 }
711
712 static cfg_type_t cfg_type_boolorauto = {
713         "boolorauto", parse_boolorauto, print_boolorauto,
714         doc_boolorauto, NULL, NULL
715 };
716
717 /*%
718  * keyword hostname
719  */
720 static void
721 print_hostname(cfg_printer_t *pctx, const cfg_obj_t *obj) {
722         UNUSED(obj);
723         cfg_print_cstr(pctx, "hostname");
724 }
725
726 static cfg_type_t cfg_type_hostname = {
727         "hostname", NULL, print_hostname, NULL, &cfg_rep_boolean, NULL
728 };
729
730 /*%
731  * "server-id" argument.
732  */
733
734 static isc_result_t
735 parse_serverid(cfg_parser_t *pctx, const cfg_type_t *type,
736                     cfg_obj_t **ret)
737 {
738         isc_result_t result;
739         CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
740         if (pctx->token.type == isc_tokentype_string &&
741             strcasecmp(TOKEN_STRING(pctx), "none") == 0)
742                 return (cfg_create_obj(pctx, &cfg_type_none, ret));
743         if (pctx->token.type == isc_tokentype_string &&
744             strcasecmp(TOKEN_STRING(pctx), "hostname") == 0) {
745                 return (cfg_create_obj(pctx, &cfg_type_hostname, ret));
746         }
747         cfg_ungettoken(pctx);
748         return (cfg_parse_qstring(pctx, type, ret));
749  cleanup:
750         return (result);
751 }
752
753 static void
754 doc_serverid(cfg_printer_t *pctx, const cfg_type_t *type) {
755         UNUSED(type);
756         cfg_print_cstr(pctx, "( <quoted_string> | none | hostname )");
757 }
758
759 static cfg_type_t cfg_type_serverid = {
760         "serverid", parse_serverid, NULL, doc_serverid, NULL, NULL };
761
762 /*%
763  * Port list.
764  */
765 static cfg_tuplefielddef_t porttuple_fields[] = {
766         { "loport", &cfg_type_uint32, 0 },
767         { "hiport", &cfg_type_uint32, 0 },
768         { NULL, NULL, 0 }
769 };
770 static cfg_type_t cfg_type_porttuple = {
771         "porttuple", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
772         &cfg_rep_tuple, porttuple_fields
773 };
774
775 static isc_result_t
776 parse_port(cfg_parser_t *pctx, cfg_obj_t **ret) {
777         isc_result_t result;
778
779         CHECK(cfg_parse_uint32(pctx, NULL, ret));
780         if ((*ret)->value.uint32 > 0xffff) {
781                 cfg_parser_error(pctx, CFG_LOG_NEAR, "invalid port");
782                 cfg_obj_destroy(pctx, ret);
783                 result = ISC_R_RANGE;
784         }
785
786  cleanup:
787         return (result);
788 }
789
790 static isc_result_t
791 parse_portrange(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
792         isc_result_t result;
793         cfg_obj_t *obj = NULL;
794
795         UNUSED(type);
796
797         CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER | ISC_LEXOPT_CNUMBER));
798         if (pctx->token.type == isc_tokentype_number)
799                 CHECK(parse_port(pctx, ret));
800         else {
801                 CHECK(cfg_gettoken(pctx, 0));
802                 if (pctx->token.type != isc_tokentype_string ||
803                     strcasecmp(TOKEN_STRING(pctx), "range") != 0) {
804                         cfg_parser_error(pctx, CFG_LOG_NEAR,
805                                          "expected integer or 'range'");
806                         return (ISC_R_UNEXPECTEDTOKEN);
807                 }
808                 CHECK(cfg_create_tuple(pctx, &cfg_type_porttuple, &obj));
809                 CHECK(parse_port(pctx, &obj->value.tuple[0]));
810                 CHECK(parse_port(pctx, &obj->value.tuple[1]));
811                 if (obj->value.tuple[0]->value.uint32 >
812                     obj->value.tuple[1]->value.uint32) {
813                         cfg_parser_error(pctx, CFG_LOG_NOPREP,
814                                          "low port '%u' must not be larger "
815                                          "than high port",
816                                          obj->value.tuple[0]->value.uint32);
817                         result = ISC_R_RANGE;
818                         goto cleanup;
819                 }
820                 *ret = obj;
821                 obj = NULL;
822         }
823
824  cleanup:
825         if (obj != NULL)
826                 cfg_obj_destroy(pctx, &obj);
827         return (result);
828 }
829
830 static cfg_type_t cfg_type_portrange = {
831         "portrange", parse_portrange, NULL, cfg_doc_terminal,
832         NULL, NULL
833 };
834
835 static cfg_type_t cfg_type_bracketed_portlist = {
836         "bracketed_sockaddrlist", cfg_parse_bracketed_list,
837         cfg_print_bracketed_list, cfg_doc_bracketed_list,
838         &cfg_rep_list, &cfg_type_portrange
839 };
840
841 /*%
842  * Clauses that can be found within the top level of the named.conf
843  * file only.
844  */
845 static cfg_clausedef_t
846 namedconf_clauses[] = {
847         { "options", &cfg_type_options, 0 },
848         { "controls", &cfg_type_controls, CFG_CLAUSEFLAG_MULTI },
849         { "acl", &cfg_type_acl, CFG_CLAUSEFLAG_MULTI },
850         { "masters", &cfg_type_masters, CFG_CLAUSEFLAG_MULTI },
851         { "logging", &cfg_type_logging, 0 },
852         { "view", &cfg_type_view, CFG_CLAUSEFLAG_MULTI },
853         { "lwres", &cfg_type_lwres, CFG_CLAUSEFLAG_MULTI },
854         { "statistics-channels", &cfg_type_statschannels,
855           CFG_CLAUSEFLAG_MULTI },
856         { NULL, NULL, 0 }
857 };
858
859 /*%
860  * Clauses that can occur at the top level or in the view
861  * statement, but not in the options block.
862  */
863 static cfg_clausedef_t
864 namedconf_or_view_clauses[] = {
865         { "key", &cfg_type_key, CFG_CLAUSEFLAG_MULTI },
866         { "zone", &cfg_type_zone, CFG_CLAUSEFLAG_MULTI },
867         /* only 1 DLZ per view allowed */
868         { "dlz", &cfg_type_dynamically_loadable_zones, 0 },
869         { "server", &cfg_type_server, CFG_CLAUSEFLAG_MULTI },
870         { "trusted-keys", &cfg_type_dnsseckeys, CFG_CLAUSEFLAG_MULTI },
871         { "managed-keys", &cfg_type_managedkeys, CFG_CLAUSEFLAG_MULTI },
872         { NULL, NULL, 0 }
873 };
874
875 /*%
876  * Clauses that can occur in the bind.keys file.
877  */
878 static cfg_clausedef_t
879 bindkeys_clauses[] = {
880         { "trusted-keys", &cfg_type_dnsseckeys, CFG_CLAUSEFLAG_MULTI },
881         { "managed-keys", &cfg_type_managedkeys, CFG_CLAUSEFLAG_MULTI },
882         { NULL, NULL, 0 }
883 };
884
885 /*%
886  * Clauses that can be found within the 'options' statement.
887  */
888 static cfg_clausedef_t
889 options_clauses[] = {
890         { "avoid-v4-udp-ports", &cfg_type_bracketed_portlist, 0 },
891         { "avoid-v6-udp-ports", &cfg_type_bracketed_portlist, 0 },
892         { "bindkeys-file", &cfg_type_qstring, 0 },
893         { "blackhole", &cfg_type_bracketed_aml, 0 },
894         { "coresize", &cfg_type_size, 0 },
895         { "datasize", &cfg_type_size, 0 },
896         { "session-keyfile", &cfg_type_qstringornone, 0 },
897         { "session-keyname", &cfg_type_astring, 0 },
898         { "session-keyalg", &cfg_type_astring, 0 },
899         { "deallocate-on-exit", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
900         { "directory", &cfg_type_qstring, CFG_CLAUSEFLAG_CALLBACK },
901         { "dump-file", &cfg_type_qstring, 0 },
902         { "fake-iquery", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
903         { "files", &cfg_type_size, 0 },
904         { "flush-zones-on-shutdown", &cfg_type_boolean, 0 },
905         { "has-old-clients", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
906         { "heartbeat-interval", &cfg_type_uint32, 0 },
907         { "host-statistics", &cfg_type_boolean, CFG_CLAUSEFLAG_NOTIMP },
908         { "host-statistics-max", &cfg_type_uint32, CFG_CLAUSEFLAG_NOTIMP },
909         { "hostname", &cfg_type_qstringornone, 0 },
910         { "interface-interval", &cfg_type_uint32, 0 },
911         { "listen-on", &cfg_type_listenon, CFG_CLAUSEFLAG_MULTI },
912         { "listen-on-v6", &cfg_type_listenon, CFG_CLAUSEFLAG_MULTI },
913         { "managed-keys-directory", &cfg_type_qstring, 0 },
914         { "match-mapped-addresses", &cfg_type_boolean, 0 },
915         { "memstatistics-file", &cfg_type_qstring, 0 },
916         { "memstatistics", &cfg_type_boolean, 0 },
917         { "multiple-cnames", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
918         { "named-xfer", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
919         { "pid-file", &cfg_type_qstringornone, 0 },
920         { "port", &cfg_type_uint32, 0 },
921         { "querylog", &cfg_type_boolean, 0 },
922         { "recursing-file", &cfg_type_qstring, 0 },
923         { "random-device", &cfg_type_qstring, 0 },
924         { "recursive-clients", &cfg_type_uint32, 0 },
925         { "reserved-sockets", &cfg_type_uint32, 0 },
926         { "secroots-file", &cfg_type_qstring, 0 },
927         { "serial-queries", &cfg_type_uint32, CFG_CLAUSEFLAG_OBSOLETE },
928         { "serial-query-rate", &cfg_type_uint32, 0 },
929         { "server-id", &cfg_type_serverid, 0 },
930         { "stacksize", &cfg_type_size, 0 },
931         { "statistics-file", &cfg_type_qstring, 0 },
932         { "statistics-interval", &cfg_type_uint32, CFG_CLAUSEFLAG_NYI },
933         { "tcp-clients", &cfg_type_uint32, 0 },
934         { "tcp-listen-queue", &cfg_type_uint32, 0 },
935         { "tkey-dhkey", &cfg_type_tkey_dhkey, 0 },
936         { "tkey-gssapi-credential", &cfg_type_qstring, 0 },
937         { "tkey-gssapi-keytab", &cfg_type_qstring, 0 },
938         { "tkey-domain", &cfg_type_qstring, 0 },
939         { "transfers-per-ns", &cfg_type_uint32, 0 },
940         { "transfers-in", &cfg_type_uint32, 0 },
941         { "transfers-out", &cfg_type_uint32, 0 },
942         { "treat-cr-as-space", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
943         { "use-id-pool", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
944         { "use-ixfr", &cfg_type_boolean, 0 },
945         { "use-v4-udp-ports", &cfg_type_bracketed_portlist, 0 },
946         { "use-v6-udp-ports", &cfg_type_bracketed_portlist, 0 },
947         { "version", &cfg_type_qstringornone, 0 },
948         { NULL, NULL, 0 }
949 };
950
951 static cfg_type_t cfg_type_namelist = {
952         "namelist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
953         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_qstring };
954
955 static keyword_type_t exclude_kw = { "exclude", &cfg_type_namelist };
956
957 static cfg_type_t cfg_type_optional_exclude = {
958         "optional_exclude", parse_optional_keyvalue, print_keyvalue,
959         doc_optional_keyvalue, &cfg_rep_list, &exclude_kw };
960
961 static keyword_type_t exceptionnames_kw = { "except-from", &cfg_type_namelist };
962
963 static cfg_type_t cfg_type_optional_exceptionnames = {
964         "optional_allow", parse_optional_keyvalue, print_keyvalue,
965         doc_optional_keyvalue, &cfg_rep_list, &exceptionnames_kw };
966
967 static cfg_tuplefielddef_t denyaddresses_fields[] = {
968         { "acl", &cfg_type_bracketed_aml, 0 },
969         { "except-from", &cfg_type_optional_exceptionnames, 0 },
970         { NULL, NULL, 0 }
971 };
972
973 static cfg_type_t cfg_type_denyaddresses = {
974         "denyaddresses", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
975         &cfg_rep_tuple, denyaddresses_fields
976 };
977
978 static cfg_tuplefielddef_t denyaliases_fields[] = {
979         { "name", &cfg_type_namelist, 0 },
980         { "except-from", &cfg_type_optional_exceptionnames, 0 },
981         { NULL, NULL, 0 }
982 };
983
984 static cfg_type_t cfg_type_denyaliases = {
985         "denyaliases", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
986         &cfg_rep_tuple, denyaliases_fields
987 };
988
989 static cfg_type_t cfg_type_algorithmlist = {
990         "algorithmlist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
991         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_astring };
992
993 static cfg_tuplefielddef_t disablealgorithm_fields[] = {
994         { "name", &cfg_type_astring, 0 },
995         { "algorithms", &cfg_type_algorithmlist, 0 },
996         { NULL, NULL, 0 }
997 };
998
999 static cfg_type_t cfg_type_disablealgorithm = {
1000         "disablealgorithm", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
1001         &cfg_rep_tuple, disablealgorithm_fields
1002 };
1003
1004 static cfg_tuplefielddef_t mustbesecure_fields[] = {
1005         { "name", &cfg_type_astring, 0 },
1006         { "value", &cfg_type_boolean, 0 },
1007         { NULL, NULL, 0 }
1008 };
1009
1010 static cfg_type_t cfg_type_mustbesecure = {
1011         "mustbesecure", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
1012         &cfg_rep_tuple, mustbesecure_fields
1013 };
1014
1015 static const char *masterformat_enums[] = { "text", "raw", NULL };
1016 static cfg_type_t cfg_type_masterformat = {
1017         "masterformat", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
1018         &cfg_rep_string, &masterformat_enums
1019 };
1020
1021
1022
1023 /*%
1024  *  response-policy {
1025  *      zone <string> [ policy (given|disabled|passthru|
1026  *                                      nxdomain|nodata|cname <domain> ) ]
1027  *                    [ recursive-only yes|no ] [ max-policy-ttl number ] ;
1028  *  } [ recursive-only yes|no ] [ max-policy-ttl number ] ;
1029  *       [ break-dnssec yes|no ] [ min-ns-dots number ] ;
1030  */
1031
1032 static void
1033 doc_rpz_policy(cfg_printer_t *pctx, const cfg_type_t *type) {
1034         const char * const *p;
1035         /*
1036          * This is cfg_doc_enum() without the trailing " )".
1037          */
1038         cfg_print_chars(pctx, "( ", 2);
1039         for (p = type->of; *p != NULL; p++) {
1040                 cfg_print_cstr(pctx, *p);
1041                 if (p[1] != NULL)
1042                         cfg_print_chars(pctx, " | ", 3);
1043         }
1044 }
1045
1046 static void
1047 doc_rpz_cname(cfg_printer_t *pctx, const cfg_type_t *type) {
1048         cfg_doc_terminal(pctx, type);
1049         cfg_print_chars(pctx, " )", 2);
1050 }
1051
1052 /*
1053  * Parse
1054  *      given|disabled|passthru|nxdomain|nodata|cname <domain>
1055  */
1056 static isc_result_t
1057 cfg_parse_rpz_policy(cfg_parser_t *pctx, const cfg_type_t *type,
1058                      cfg_obj_t **ret)
1059 {
1060         isc_result_t result;
1061         cfg_obj_t *obj;
1062         const cfg_tuplefielddef_t *fields;
1063
1064         CHECK(cfg_create_tuple(pctx, type, &obj));
1065
1066         fields = type->of;
1067         CHECK(cfg_parse_obj(pctx, fields[0].type, &obj->value.tuple[0]));
1068         /*
1069          * parse cname domain only after "policy cname"
1070          */
1071         if (strcasecmp("cname", cfg_obj_asstring(obj->value.tuple[0])) != 0) {
1072                 CHECK(cfg_parse_void(pctx, NULL, &obj->value.tuple[1]));
1073         } else {
1074                 CHECK(cfg_parse_obj(pctx, fields[1].type,
1075                                     &obj->value.tuple[1]));
1076         }
1077
1078         *ret = obj;
1079         return (ISC_R_SUCCESS);
1080
1081 cleanup:
1082         CLEANUP_OBJ(obj);
1083         return (result);
1084 }
1085
1086 /*
1087  * Parse a tuple consisting of any kind of  required field followed
1088  * by 2 or more optional keyvalues that can be in any order.
1089  */
1090 static isc_result_t
1091 cfg_parse_kv_tuple(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1092         const cfg_tuplefielddef_t *fields, *f;
1093         cfg_obj_t *obj;
1094         int fn;
1095         isc_result_t result;
1096
1097         obj = NULL;
1098         CHECK(cfg_create_tuple(pctx, type, &obj));
1099
1100         /*
1101          * The zone first field is required and always first.
1102          */
1103         fields = type->of;
1104         CHECK(cfg_parse_obj(pctx, fields[0].type, &obj->value.tuple[0]));
1105
1106         for (;;) {
1107                 CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
1108                 if (pctx->token.type != isc_tokentype_string)
1109                         break;
1110
1111                 for (fn = 1, f = &fields[1]; ; ++fn, ++f) {
1112                         if (f->name == NULL) {
1113                                 cfg_parser_error(pctx, 0, "unexpected '%s'",
1114                                                  TOKEN_STRING(pctx));
1115                                 result = ISC_R_UNEXPECTEDTOKEN;
1116                                 goto cleanup;
1117                         }
1118                         if (obj->value.tuple[fn] == NULL &&
1119                             strcasecmp(f->name, TOKEN_STRING(pctx)) == 0)
1120                                 break;
1121                 }
1122
1123                 CHECK(cfg_gettoken(pctx, 0));
1124                 CHECK(cfg_parse_obj(pctx, f->type, &obj->value.tuple[fn]));
1125         }
1126
1127         for (fn = 1, f = &fields[1]; f->name != NULL; ++fn, ++f) {
1128                 if (obj->value.tuple[fn] == NULL)
1129                         CHECK(cfg_parse_void(pctx, NULL,
1130                                              &obj->value.tuple[fn]));
1131         }
1132
1133         *ret = obj;
1134         return (ISC_R_SUCCESS);
1135
1136 cleanup:
1137         CLEANUP_OBJ(obj);
1138         return (result);
1139 }
1140
1141 static void
1142 cfg_print_kv_tuple(cfg_printer_t *pctx, const cfg_obj_t *obj) {
1143         unsigned int i;
1144         const cfg_tuplefielddef_t *fields, *f;
1145         const cfg_obj_t *fieldobj;
1146
1147         fields = obj->type->of;
1148         for (f = fields, i = 0; f->name != NULL; f++, i++) {
1149                 fieldobj = obj->value.tuple[i];
1150                 if (fieldobj->type->print == cfg_print_void)
1151                         continue;
1152                 if (i != 0) {
1153                         cfg_print_chars(pctx, " ", 1);
1154                         cfg_print_cstr(pctx, f->name);
1155                         cfg_print_chars(pctx, " ", 1);
1156                 }
1157                 cfg_print_obj(pctx, fieldobj);
1158         }
1159 }
1160
1161 static void
1162 cfg_doc_kv_tuple(cfg_printer_t *pctx, const cfg_type_t *type) {
1163         const cfg_tuplefielddef_t *fields, *f;
1164
1165         fields = type->of;
1166         for (f = fields; f->name != NULL; f++) {
1167                 if (f != fields) {
1168                         cfg_print_chars(pctx, " [ ", 3);
1169                         cfg_print_cstr(pctx, f->name);
1170                         if (f->type->doc != cfg_doc_void)
1171                                 cfg_print_chars(pctx, " ", 1);
1172                 }
1173                 cfg_doc_obj(pctx, f->type);
1174                 if (f != fields)
1175                         cfg_print_chars(pctx, " ]", 2);
1176         }
1177 }
1178
1179 static keyword_type_t zone_kw = {"zone", &cfg_type_qstring};
1180 static cfg_type_t cfg_type_rpz_zone = {
1181         "zone", parse_keyvalue, print_keyvalue,
1182         doc_keyvalue, &cfg_rep_string,
1183         &zone_kw
1184 };
1185 static const char *rpz_policies[] = {
1186         "given", "disabled", "passthru", "no-op", "nxdomain", "nodata",
1187         "cname", NULL
1188 };
1189 static cfg_type_t cfg_type_rpz_policy_name = {
1190         "policy name", cfg_parse_enum, cfg_print_ustring,
1191         doc_rpz_policy, &cfg_rep_string,
1192         &rpz_policies
1193 };
1194 static cfg_type_t cfg_type_rpz_cname = {
1195         "quoted_string", cfg_parse_astring, NULL,
1196         doc_rpz_cname, &cfg_rep_string,
1197         NULL
1198 };
1199 static cfg_tuplefielddef_t rpz_policy_fields[] = {
1200         { "policy name", &cfg_type_rpz_policy_name, 0 },
1201         { "cname", &cfg_type_rpz_cname, 0 },
1202         { NULL, NULL, 0 }
1203 };
1204 static cfg_type_t cfg_type_rpz_policy = {
1205         "policy tuple", cfg_parse_rpz_policy,
1206         cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
1207         rpz_policy_fields
1208 };
1209 static cfg_tuplefielddef_t rpz_zone_fields[] = {
1210         { "zone name", &cfg_type_rpz_zone, 0 },
1211         { "policy", &cfg_type_rpz_policy, 0 },
1212         { "recursive-only", &cfg_type_boolean, 0 },
1213         { "max-policy-ttl", &cfg_type_uint32, 0 },
1214         { NULL, NULL, 0 }
1215 };
1216 static cfg_type_t cfg_type_rpz_tuple = {
1217         "rpz tuple", cfg_parse_kv_tuple,
1218         cfg_print_kv_tuple, cfg_doc_kv_tuple, &cfg_rep_tuple,
1219         rpz_zone_fields
1220 };
1221 static cfg_type_t cfg_type_rpz_list = {
1222         "zone list", cfg_parse_bracketed_list, cfg_print_bracketed_list,
1223         cfg_doc_bracketed_list, &cfg_rep_list,
1224         &cfg_type_rpz_tuple
1225 };
1226 static cfg_tuplefielddef_t rpz_fields[] = {
1227         { "zone list", &cfg_type_rpz_list, 0 },
1228         { "recursive-only", &cfg_type_boolean, 0 },
1229         { "break-dnssec", &cfg_type_boolean, 0 },
1230         { "max-policy-ttl", &cfg_type_uint32, 0 },
1231         { "min-ns-dots", &cfg_type_uint32, 0 },
1232         { NULL, NULL, 0 }
1233 };
1234 static cfg_type_t cfg_type_rpz = {
1235         "rpz", cfg_parse_kv_tuple,
1236         cfg_print_kv_tuple, cfg_doc_kv_tuple, &cfg_rep_tuple,
1237         rpz_fields
1238 };
1239
1240
1241 /*%
1242  * dnssec-lookaside
1243  */
1244
1245 static void
1246 print_lookaside(cfg_printer_t *pctx, const cfg_obj_t *obj)
1247 {
1248         const cfg_obj_t *domain = obj->value.tuple[0];
1249
1250         if (domain->value.string.length == 4 &&
1251             strncmp(domain->value.string.base, "auto", 4) == 0)
1252                 cfg_print_cstr(pctx, "auto");
1253         else
1254                 cfg_print_tuple(pctx, obj);
1255 }
1256
1257 static void
1258 doc_lookaside(cfg_printer_t *pctx, const cfg_type_t *type) {
1259         UNUSED(type);
1260         cfg_print_cstr(pctx, "( <string> trust-anchor <string> | auto | no )");
1261 }
1262
1263 static keyword_type_t trustanchor_kw = { "trust-anchor", &cfg_type_astring };
1264
1265 static cfg_type_t cfg_type_optional_trustanchor = {
1266         "optional_trustanchor", parse_optional_keyvalue, print_keyvalue,
1267         doc_keyvalue, &cfg_rep_string, &trustanchor_kw
1268 };
1269
1270 static cfg_tuplefielddef_t lookaside_fields[] = {
1271         { "domain", &cfg_type_astring, 0 },
1272         { "trust-anchor", &cfg_type_optional_trustanchor, 0 },
1273         { NULL, NULL, 0 }
1274 };
1275
1276 static cfg_type_t cfg_type_lookaside = {
1277         "lookaside", cfg_parse_tuple, print_lookaside, doc_lookaside,
1278         &cfg_rep_tuple, lookaside_fields
1279 };
1280
1281 /*
1282  * DNS64.
1283  */
1284 static cfg_clausedef_t
1285 dns64_clauses[] = {
1286         { "clients", &cfg_type_bracketed_aml, 0 },
1287         { "mapped", &cfg_type_bracketed_aml, 0 },
1288         { "exclude", &cfg_type_bracketed_aml, 0 },
1289         { "suffix", &cfg_type_netaddr6, 0 },
1290         { "recursive-only", &cfg_type_boolean, 0 },
1291         { "break-dnssec", &cfg_type_boolean, 0 },
1292         { NULL, NULL, 0 },
1293 };
1294
1295 static cfg_clausedef_t *
1296 dns64_clausesets[] = {
1297         dns64_clauses,
1298         NULL
1299 };
1300
1301 static cfg_type_t cfg_type_dns64 = {
1302         "dns64", cfg_parse_netprefix_map, cfg_print_map, cfg_doc_map,
1303         &cfg_rep_map, dns64_clausesets
1304 };
1305
1306 /*%
1307  * Clauses that can be found within the 'view' statement,
1308  * with defaults in the 'options' statement.
1309  */
1310
1311 static cfg_clausedef_t
1312 view_clauses[] = {
1313         { "acache-cleaning-interval", &cfg_type_uint32, 0 },
1314         { "acache-enable", &cfg_type_boolean, 0 },
1315         { "additional-from-auth", &cfg_type_boolean, 0 },
1316         { "additional-from-cache", &cfg_type_boolean, 0 },
1317         { "allow-new-zones", &cfg_type_boolean, 0 },
1318         { "allow-query-cache", &cfg_type_bracketed_aml, 0 },
1319         { "allow-query-cache-on", &cfg_type_bracketed_aml, 0 },
1320         { "allow-recursion", &cfg_type_bracketed_aml, 0 },
1321         { "allow-recursion-on", &cfg_type_bracketed_aml, 0 },
1322         { "allow-v6-synthesis", &cfg_type_bracketed_aml,
1323           CFG_CLAUSEFLAG_OBSOLETE },
1324         { "attach-cache", &cfg_type_astring, 0 },
1325         { "auth-nxdomain", &cfg_type_boolean, CFG_CLAUSEFLAG_NEWDEFAULT },
1326         { "cache-file", &cfg_type_qstring, 0 },
1327         { "check-names", &cfg_type_checknames, CFG_CLAUSEFLAG_MULTI },
1328         { "cleaning-interval", &cfg_type_uint32, 0 },
1329         { "clients-per-query", &cfg_type_uint32, 0 },
1330         { "deny-answer-addresses", &cfg_type_denyaddresses, 0 },
1331         { "deny-answer-aliases", &cfg_type_denyaliases, 0 },
1332         { "disable-algorithms", &cfg_type_disablealgorithm,
1333           CFG_CLAUSEFLAG_MULTI },
1334         { "disable-empty-zone", &cfg_type_astring, CFG_CLAUSEFLAG_MULTI },
1335         { "dns64", &cfg_type_dns64, CFG_CLAUSEFLAG_MULTI },
1336         { "dns64-server", &cfg_type_astring, 0 },
1337         { "dns64-contact", &cfg_type_astring, 0 },
1338         { "dnssec-accept-expired", &cfg_type_boolean, 0 },
1339         { "dnssec-enable", &cfg_type_boolean, 0 },
1340         { "dnssec-lookaside", &cfg_type_lookaside, CFG_CLAUSEFLAG_MULTI },
1341         { "dnssec-must-be-secure",  &cfg_type_mustbesecure,
1342           CFG_CLAUSEFLAG_MULTI },
1343         { "dnssec-validation", &cfg_type_boolorauto, 0 },
1344         { "dual-stack-servers", &cfg_type_nameportiplist, 0 },
1345         { "edns-udp-size", &cfg_type_uint32, 0 },
1346         { "empty-contact", &cfg_type_astring, 0 },
1347         { "empty-server", &cfg_type_astring, 0 },
1348         { "empty-zones-enable", &cfg_type_boolean, 0 },
1349         { "fetch-glue", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1350         { "ixfr-from-differences", &cfg_type_ixfrdifftype, 0 },
1351         { "lame-ttl", &cfg_type_uint32, 0 },
1352         { "max-acache-size", &cfg_type_sizenodefault, 0 },
1353         { "max-cache-size", &cfg_type_sizenodefault, 0 },
1354         { "max-cache-ttl", &cfg_type_uint32, 0 },
1355         { "max-clients-per-query", &cfg_type_uint32, 0 },
1356         { "max-ncache-ttl", &cfg_type_uint32, 0 },
1357         { "max-udp-size", &cfg_type_uint32, 0 },
1358         { "min-roots", &cfg_type_uint32, CFG_CLAUSEFLAG_NOTIMP },
1359         { "minimal-responses", &cfg_type_boolean, 0 },
1360         { "preferred-glue", &cfg_type_astring, 0 },
1361         { "provide-ixfr", &cfg_type_boolean, 0 },
1362         /*
1363          * Note that the query-source option syntax is different
1364          * from the other -source options.
1365          */
1366         { "query-source", &cfg_type_querysource4, 0 },
1367         { "query-source-v6", &cfg_type_querysource6, 0 },
1368         { "queryport-pool-ports", &cfg_type_uint32, CFG_CLAUSEFLAG_OBSOLETE },
1369         { "queryport-pool-updateinterval", &cfg_type_uint32,
1370           CFG_CLAUSEFLAG_OBSOLETE },
1371         { "recursion", &cfg_type_boolean, 0 },
1372         { "request-ixfr", &cfg_type_boolean, 0 },
1373         { "request-nsid", &cfg_type_boolean, 0 },
1374         { "resolver-query-timeout", &cfg_type_uint32, 0 },
1375         { "rfc2308-type1", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
1376         { "root-delegation-only",  &cfg_type_optional_exclude, 0 },
1377         { "rrset-order", &cfg_type_rrsetorder, 0 },
1378         { "sortlist", &cfg_type_bracketed_aml, 0 },
1379         { "suppress-initial-notify", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
1380         { "topology", &cfg_type_bracketed_aml, CFG_CLAUSEFLAG_NOTIMP },
1381         { "transfer-format", &cfg_type_transferformat, 0 },
1382         { "use-queryport-pool", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1383         { "zero-no-soa-ttl-cache", &cfg_type_boolean, 0 },
1384 #ifdef ALLOW_FILTER_AAAA_ON_V4
1385         { "filter-aaaa", &cfg_type_bracketed_aml, 0 },
1386         { "filter-aaaa-on-v4", &cfg_type_v4_aaaa, 0 },
1387 #else
1388         { "filter-aaaa", &cfg_type_bracketed_aml,
1389            CFG_CLAUSEFLAG_NOTCONFIGURED },
1390         { "filter-aaaa-on-v4", &cfg_type_v4_aaaa,
1391            CFG_CLAUSEFLAG_NOTCONFIGURED },
1392 #endif
1393         { "response-policy", &cfg_type_rpz, 0 },
1394         { NULL, NULL, 0 }
1395 };
1396
1397 /*%
1398  * Clauses that can be found within the 'view' statement only.
1399  */
1400 static cfg_clausedef_t
1401 view_only_clauses[] = {
1402         { "match-clients", &cfg_type_bracketed_aml, 0 },
1403         { "match-destinations", &cfg_type_bracketed_aml, 0 },
1404         { "match-recursive-only", &cfg_type_boolean, 0 },
1405         { NULL, NULL, 0 }
1406 };
1407
1408 /*%
1409  * Sig-validity-interval.
1410  */
1411 static isc_result_t
1412 parse_optional_uint32(cfg_parser_t *pctx, const cfg_type_t *type,
1413                       cfg_obj_t **ret)
1414 {
1415         isc_result_t result;
1416         UNUSED(type);
1417
1418         CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER | ISC_LEXOPT_CNUMBER));
1419         if (pctx->token.type == isc_tokentype_number) {
1420                 CHECK(cfg_parse_obj(pctx, &cfg_type_uint32, ret));
1421         } else {
1422                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
1423         }
1424  cleanup:
1425         return (result);
1426 }
1427
1428 static void
1429 doc_optional_uint32(cfg_printer_t *pctx, const cfg_type_t *type) {
1430         UNUSED(type);
1431         cfg_print_cstr(pctx, "[ <integer> ]");
1432 }
1433
1434 static cfg_type_t cfg_type_optional_uint32 = {
1435         "optional_uint32", parse_optional_uint32, NULL, doc_optional_uint32,
1436         NULL, NULL };
1437
1438 static cfg_tuplefielddef_t validityinterval_fields[] = {
1439         { "validity", &cfg_type_uint32, 0 },
1440         { "re-sign", &cfg_type_optional_uint32, 0 },
1441         { NULL, NULL, 0 }
1442 };
1443
1444 static cfg_type_t cfg_type_validityinterval = {
1445         "validityinterval", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
1446         &cfg_rep_tuple, validityinterval_fields
1447 };
1448
1449 /*%
1450  * Clauses that can be found in a 'zone' statement,
1451  * with defaults in the 'view' or 'options' statement.
1452  */
1453 static cfg_clausedef_t
1454 zone_clauses[] = {
1455         { "allow-notify", &cfg_type_bracketed_aml, 0 },
1456         { "allow-query", &cfg_type_bracketed_aml, 0 },
1457         { "allow-query-on", &cfg_type_bracketed_aml, 0 },
1458         { "allow-transfer", &cfg_type_bracketed_aml, 0 },
1459         { "allow-update", &cfg_type_bracketed_aml, 0 },
1460         { "allow-update-forwarding", &cfg_type_bracketed_aml, 0 },
1461         { "also-notify", &cfg_type_portiplist, 0 },
1462         { "alt-transfer-source", &cfg_type_sockaddr4wild, 0 },
1463         { "alt-transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1464         { "auto-dnssec", &cfg_type_autodnssec, 0 },
1465         { "check-dup-records", &cfg_type_checkmode, 0 },
1466         { "check-integrity", &cfg_type_boolean, 0 },
1467         { "check-mx", &cfg_type_checkmode, 0 },
1468         { "check-mx-cname", &cfg_type_checkmode, 0 },
1469         { "check-sibling", &cfg_type_boolean, 0 },
1470         { "check-spf", &cfg_type_warn, 0 },
1471         { "check-srv-cname", &cfg_type_checkmode, 0 },
1472         { "check-wildcard", &cfg_type_boolean, 0 },
1473         { "dialup", &cfg_type_dialuptype, 0 },
1474         { "dnssec-dnskey-kskonly", &cfg_type_boolean, 0 },
1475         { "dnssec-secure-to-insecure", &cfg_type_boolean, 0 },
1476         { "forward", &cfg_type_forwardtype, 0 },
1477         { "forwarders", &cfg_type_portiplist, 0 },
1478         { "key-directory", &cfg_type_qstring, 0 },
1479         { "maintain-ixfr-base", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1480         { "masterfile-format", &cfg_type_masterformat, 0 },
1481         { "max-ixfr-log-size", &cfg_type_size, CFG_CLAUSEFLAG_OBSOLETE },
1482         { "max-journal-size", &cfg_type_sizenodefault, 0 },
1483         { "max-refresh-time", &cfg_type_uint32, 0 },
1484         { "max-retry-time", &cfg_type_uint32, 0 },
1485         { "max-transfer-idle-in", &cfg_type_uint32, 0 },
1486         { "max-transfer-idle-out", &cfg_type_uint32, 0 },
1487         { "max-transfer-time-in", &cfg_type_uint32, 0 },
1488         { "max-transfer-time-out", &cfg_type_uint32, 0 },
1489         { "min-refresh-time", &cfg_type_uint32, 0 },
1490         { "min-retry-time", &cfg_type_uint32, 0 },
1491         { "multi-master", &cfg_type_boolean, 0 },
1492         { "notify", &cfg_type_notifytype, 0 },
1493         { "notify-delay", &cfg_type_uint32, 0 },
1494         { "notify-source", &cfg_type_sockaddr4wild, 0 },
1495         { "notify-source-v6", &cfg_type_sockaddr6wild, 0 },
1496         { "notify-to-soa", &cfg_type_boolean, 0 },
1497         { "nsec3-test-zone", &cfg_type_boolean, CFG_CLAUSEFLAG_TESTONLY },
1498         { "sig-signing-nodes", &cfg_type_uint32, 0 },
1499         { "sig-signing-signatures", &cfg_type_uint32, 0 },
1500         { "sig-signing-type", &cfg_type_uint32, 0 },
1501         { "sig-validity-interval", &cfg_type_validityinterval, 0 },
1502         { "transfer-source", &cfg_type_sockaddr4wild, 0 },
1503         { "transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1504         { "try-tcp-refresh", &cfg_type_boolean, 0 },
1505         { "update-check-ksk", &cfg_type_boolean, 0 },
1506         { "use-alt-transfer-source", &cfg_type_boolean, 0 },
1507         { "zero-no-soa-ttl", &cfg_type_boolean, 0 },
1508         { "zone-statistics", &cfg_type_boolean, 0 },
1509         { NULL, NULL, 0 }
1510 };
1511
1512 /*%
1513  * Clauses that can be found in a 'zone' statement
1514  * only.
1515  */
1516 static cfg_clausedef_t
1517 zone_only_clauses[] = {
1518         { "type", &cfg_type_zonetype, 0 },
1519         { "file", &cfg_type_qstring, 0 },
1520         { "journal", &cfg_type_qstring, 0 },
1521         { "ixfr-base", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
1522         { "ixfr-tmp-file", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
1523         { "masters", &cfg_type_namesockaddrkeylist, 0 },
1524         { "pubkey", &cfg_type_pubkey,
1525           CFG_CLAUSEFLAG_MULTI | CFG_CLAUSEFLAG_OBSOLETE },
1526         { "update-policy", &cfg_type_updatepolicy, 0 },
1527         { "database", &cfg_type_astring, 0 },
1528         { "delegation-only", &cfg_type_boolean, 0 },
1529         /*
1530          * Note that the format of the check-names option is different between
1531          * the zone options and the global/view options.  Ugh.
1532          */
1533         { "check-names", &cfg_type_checkmode, 0 },
1534         { "ixfr-from-differences", &cfg_type_boolean, 0 },
1535         { "server-addresses", &cfg_type_bracketed_sockaddrlist, 0 },
1536         { "server-names", &cfg_type_namelist, 0 },
1537         { NULL, NULL, 0 }
1538 };
1539
1540
1541 /*% The top-level named.conf syntax. */
1542
1543 static cfg_clausedef_t *
1544 namedconf_clausesets[] = {
1545         namedconf_clauses,
1546         namedconf_or_view_clauses,
1547         NULL
1548 };
1549 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_namedconf = {
1550         "namedconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1551         &cfg_rep_map, namedconf_clausesets
1552 };
1553
1554 /*% The bind.keys syntax (trusted-keys/managed-keys only). */
1555 static cfg_clausedef_t *
1556 bindkeys_clausesets[] = {
1557         bindkeys_clauses,
1558         NULL
1559 };
1560 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_bindkeys = {
1561         "bindkeys", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1562         &cfg_rep_map, bindkeys_clausesets
1563 };
1564
1565 /*% The new-zone-file syntax (for zones added by 'rndc addzone') */
1566 static cfg_clausedef_t
1567 newzones_clauses[] = {
1568         { "zone", &cfg_type_zone, CFG_CLAUSEFLAG_MULTI },
1569         { NULL, NULL, 0 }
1570 };
1571
1572 static cfg_clausedef_t *
1573 newzones_clausesets[] = {
1574         newzones_clauses,
1575         NULL
1576 };
1577
1578 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_newzones = {
1579         "newzones", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1580         &cfg_rep_map, newzones_clausesets
1581 };
1582
1583 /*% The "options" statement syntax. */
1584
1585 static cfg_clausedef_t *
1586 options_clausesets[] = {
1587         options_clauses,
1588         view_clauses,
1589         zone_clauses,
1590         NULL
1591 };
1592 static cfg_type_t cfg_type_options = {
1593         "options", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, options_clausesets };
1594
1595 /*% The "view" statement syntax. */
1596
1597 static cfg_clausedef_t *
1598 view_clausesets[] = {
1599         view_only_clauses,
1600         namedconf_or_view_clauses,
1601         view_clauses,
1602         zone_clauses,
1603         dynamically_loadable_zones_clauses,
1604         NULL
1605 };
1606 static cfg_type_t cfg_type_viewopts = {
1607         "view", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, view_clausesets };
1608
1609 /*% The "zone" statement syntax. */
1610
1611 static cfg_clausedef_t *
1612 zone_clausesets[] = {
1613         zone_only_clauses,
1614         zone_clauses,
1615         NULL
1616 };
1617 static cfg_type_t cfg_type_zoneopts = {
1618         "zoneopts", cfg_parse_map, cfg_print_map,
1619         cfg_doc_map, &cfg_rep_map, zone_clausesets };
1620
1621 /*% The "dynamically loadable zones" statement syntax. */
1622
1623 static cfg_clausedef_t *
1624 dynamically_loadable_zones_clausesets[] = {
1625         dynamically_loadable_zones_clauses,
1626         NULL
1627 };
1628 static cfg_type_t cfg_type_dynamically_loadable_zones_opts = {
1629         "dynamically_loadable_zones_opts", cfg_parse_map,
1630         cfg_print_map, cfg_doc_map, &cfg_rep_map,
1631         dynamically_loadable_zones_clausesets
1632 };
1633
1634 /*%
1635  * Clauses that can be found within the 'key' statement.
1636  */
1637 static cfg_clausedef_t
1638 key_clauses[] = {
1639         { "algorithm", &cfg_type_astring, 0 },
1640         { "secret", &cfg_type_sstring, 0 },
1641         { NULL, NULL, 0 }
1642 };
1643
1644 static cfg_clausedef_t *
1645 key_clausesets[] = {
1646         key_clauses,
1647         NULL
1648 };
1649 static cfg_type_t cfg_type_key = {
1650         "key", cfg_parse_named_map, cfg_print_map,
1651         cfg_doc_map, &cfg_rep_map, key_clausesets
1652 };
1653
1654
1655 /*%
1656  * Clauses that can be found in a 'server' statement.
1657  */
1658 static cfg_clausedef_t
1659 server_clauses[] = {
1660         { "bogus", &cfg_type_boolean, 0 },
1661         { "provide-ixfr", &cfg_type_boolean, 0 },
1662         { "request-ixfr", &cfg_type_boolean, 0 },
1663         { "support-ixfr", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1664         { "transfers", &cfg_type_uint32, 0 },
1665         { "transfer-format", &cfg_type_transferformat, 0 },
1666         { "keys", &cfg_type_server_key_kludge, 0 },
1667         { "edns", &cfg_type_boolean, 0 },
1668         { "edns-udp-size", &cfg_type_uint32, 0 },
1669         { "max-udp-size", &cfg_type_uint32, 0 },
1670         { "notify-source", &cfg_type_sockaddr4wild, 0 },
1671         { "notify-source-v6", &cfg_type_sockaddr6wild, 0 },
1672         { "query-source", &cfg_type_querysource4, 0 },
1673         { "query-source-v6", &cfg_type_querysource6, 0 },
1674         { "transfer-source", &cfg_type_sockaddr4wild, 0 },
1675         { "transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1676         { NULL, NULL, 0 }
1677 };
1678 static cfg_clausedef_t *
1679 server_clausesets[] = {
1680         server_clauses,
1681         NULL
1682 };
1683 static cfg_type_t cfg_type_server = {
1684         "server", cfg_parse_netprefix_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,
1685         server_clausesets
1686 };
1687
1688
1689 /*%
1690  * Clauses that can be found in a 'channel' clause in the
1691  * 'logging' statement.
1692  *
1693  * These have some additional constraints that need to be
1694  * checked after parsing:
1695  *  - There must exactly one of file/syslog/null/stderr
1696  *
1697  */
1698 static cfg_clausedef_t
1699 channel_clauses[] = {
1700         /* Destinations.  We no longer require these to be first. */
1701         { "file", &cfg_type_logfile, 0 },
1702         { "syslog", &cfg_type_optional_facility, 0 },
1703         { "null", &cfg_type_void, 0 },
1704         { "stderr", &cfg_type_void, 0 },
1705         /* Options.  We now accept these for the null channel, too. */
1706         { "severity", &cfg_type_logseverity, 0 },
1707         { "print-time", &cfg_type_boolean, 0 },
1708         { "print-severity", &cfg_type_boolean, 0 },
1709         { "print-category", &cfg_type_boolean, 0 },
1710         { NULL, NULL, 0 }
1711 };
1712 static cfg_clausedef_t *
1713 channel_clausesets[] = {
1714         channel_clauses,
1715         NULL
1716 };
1717 static cfg_type_t cfg_type_channel = {
1718         "channel", cfg_parse_named_map, cfg_print_map, cfg_doc_map,
1719         &cfg_rep_map, channel_clausesets
1720 };
1721
1722 /*% A list of log destination, used in the "category" clause. */
1723 static cfg_type_t cfg_type_destinationlist = {
1724         "destinationlist", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
1725         &cfg_rep_list, &cfg_type_astring };
1726
1727 /*%
1728  * Clauses that can be found in a 'logging' statement.
1729  */
1730 static cfg_clausedef_t
1731 logging_clauses[] = {
1732         { "channel", &cfg_type_channel, CFG_CLAUSEFLAG_MULTI },
1733         { "category", &cfg_type_category, CFG_CLAUSEFLAG_MULTI },
1734         { NULL, NULL, 0 }
1735 };
1736 static cfg_clausedef_t *
1737 logging_clausesets[] = {
1738         logging_clauses,
1739         NULL
1740 };
1741 static cfg_type_t cfg_type_logging = {
1742         "logging", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, logging_clausesets };
1743
1744
1745 /*%
1746  * For parsing an 'addzone' statement
1747  */
1748
1749 static cfg_tuplefielddef_t addzone_fields[] = {
1750         { "name", &cfg_type_astring, 0 },
1751         { "class", &cfg_type_optional_class, 0 },
1752         { "view", &cfg_type_optional_class, 0 },
1753         { "options", &cfg_type_zoneopts, 0 },
1754         { NULL, NULL, 0 }
1755 };
1756 static cfg_type_t cfg_type_addzone = {
1757         "addzone", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, addzone_fields };
1758
1759 static cfg_clausedef_t
1760 addzoneconf_clauses[] = {
1761         { "addzone", &cfg_type_addzone, 0 },
1762         { NULL, NULL, 0 }
1763 };
1764
1765 static cfg_clausedef_t *
1766 addzoneconf_clausesets[] = {
1767         addzoneconf_clauses,
1768         NULL
1769 };
1770
1771 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_addzoneconf = {
1772         "addzoneconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1773         &cfg_rep_map, addzoneconf_clausesets
1774 };
1775
1776
1777 static isc_result_t
1778 parse_unitstring(char *str, isc_resourcevalue_t *valuep) {
1779         char *endp;
1780         unsigned int len;
1781         isc_uint64_t value;
1782         isc_uint64_t unit;
1783
1784         value = isc_string_touint64(str, &endp, 10);
1785         if (*endp == 0) {
1786                 *valuep = value;
1787                 return (ISC_R_SUCCESS);
1788         }
1789
1790         len = strlen(str);
1791         if (len < 2 || endp[1] != '\0')
1792                 return (ISC_R_FAILURE);
1793
1794         switch (str[len - 1]) {
1795         case 'k':
1796         case 'K':
1797                 unit = 1024;
1798                 break;
1799         case 'm':
1800         case 'M':
1801                 unit = 1024 * 1024;
1802                 break;
1803         case 'g':
1804         case 'G':
1805                 unit = 1024 * 1024 * 1024;
1806                 break;
1807         default:
1808                 return (ISC_R_FAILURE);
1809         }
1810         if (value > ISC_UINT64_MAX / unit)
1811                 return (ISC_R_FAILURE);
1812         *valuep = value * unit;
1813         return (ISC_R_SUCCESS);
1814 }
1815
1816 static isc_result_t
1817 parse_sizeval(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1818         isc_result_t result;
1819         cfg_obj_t *obj = NULL;
1820         isc_uint64_t val;
1821
1822         UNUSED(type);
1823
1824         CHECK(cfg_gettoken(pctx, 0));
1825         if (pctx->token.type != isc_tokentype_string) {
1826                 result = ISC_R_UNEXPECTEDTOKEN;
1827                 goto cleanup;
1828         }
1829         CHECK(parse_unitstring(TOKEN_STRING(pctx), &val));
1830
1831         CHECK(cfg_create_obj(pctx, &cfg_type_uint64, &obj));
1832         obj->value.uint64 = val;
1833         *ret = obj;
1834         return (ISC_R_SUCCESS);
1835
1836  cleanup:
1837         cfg_parser_error(pctx, CFG_LOG_NEAR, "expected integer and optional unit");
1838         return (result);
1839 }
1840
1841 /*%
1842  * A size value (number + optional unit).
1843  */
1844 static cfg_type_t cfg_type_sizeval = {
1845         "sizeval", parse_sizeval, cfg_print_uint64, cfg_doc_terminal,
1846         &cfg_rep_uint64, NULL };
1847
1848 /*%
1849  * A size, "unlimited", or "default".
1850  */
1851
1852 static isc_result_t
1853 parse_size(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1854         return (parse_enum_or_other(pctx, type, &cfg_type_sizeval, ret));
1855 }
1856
1857 static const char *size_enums[] = { "unlimited", "default", NULL };
1858 static cfg_type_t cfg_type_size = {
1859         "size", parse_size, cfg_print_ustring, cfg_doc_terminal,
1860         &cfg_rep_string, size_enums
1861 };
1862
1863 /*%
1864  * A size or "unlimited", but not "default".
1865  */
1866 static const char *sizenodefault_enums[] = { "unlimited", NULL };
1867 static cfg_type_t cfg_type_sizenodefault = {
1868         "size_no_default", parse_size, cfg_print_ustring, cfg_doc_terminal,
1869         &cfg_rep_string, sizenodefault_enums
1870 };
1871
1872 /*%
1873  * optional_keyvalue
1874  */
1875 static isc_result_t
1876 parse_maybe_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type,
1877                               isc_boolean_t optional, cfg_obj_t **ret)
1878 {
1879         isc_result_t result;
1880         cfg_obj_t *obj = NULL;
1881         const keyword_type_t *kw = type->of;
1882
1883         CHECK(cfg_peektoken(pctx, 0));
1884         if (pctx->token.type == isc_tokentype_string &&
1885             strcasecmp(TOKEN_STRING(pctx), kw->name) == 0) {
1886                 CHECK(cfg_gettoken(pctx, 0));
1887                 CHECK(kw->type->parse(pctx, kw->type, &obj));
1888                 obj->type = type; /* XXX kludge */
1889         } else {
1890                 if (optional) {
1891                         CHECK(cfg_parse_void(pctx, NULL, &obj));
1892                 } else {
1893                         cfg_parser_error(pctx, CFG_LOG_NEAR, "expected '%s'",
1894                                      kw->name);
1895                         result = ISC_R_UNEXPECTEDTOKEN;
1896                         goto cleanup;
1897                 }
1898         }
1899         *ret = obj;
1900  cleanup:
1901         return (result);
1902 }
1903
1904 static isc_result_t
1905 parse_enum_or_other(cfg_parser_t *pctx, const cfg_type_t *enumtype,
1906                     const cfg_type_t *othertype, cfg_obj_t **ret)
1907 {
1908         isc_result_t result;
1909         CHECK(cfg_peektoken(pctx, 0));
1910         if (pctx->token.type == isc_tokentype_string &&
1911             cfg_is_enum(TOKEN_STRING(pctx), enumtype->of)) {
1912                 CHECK(cfg_parse_enum(pctx, enumtype, ret));
1913         } else {
1914                 CHECK(cfg_parse_obj(pctx, othertype, ret));
1915         }
1916  cleanup:
1917         return (result);
1918 }
1919
1920 static void
1921 doc_enum_or_other(cfg_printer_t *pctx, const cfg_type_t *type) {
1922         cfg_doc_terminal(pctx, type);
1923 #if 0 /* XXX */
1924         cfg_print_chars(pctx, "( ", 2);...
1925 #endif
1926
1927 }
1928
1929 static isc_result_t
1930 parse_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1931         return (parse_maybe_optional_keyvalue(pctx, type, ISC_FALSE, ret));
1932 }
1933
1934 static isc_result_t
1935 parse_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1936         return (parse_maybe_optional_keyvalue(pctx, type, ISC_TRUE, ret));
1937 }
1938
1939 static void
1940 print_keyvalue(cfg_printer_t *pctx, const cfg_obj_t *obj) {
1941         const keyword_type_t *kw = obj->type->of;
1942         cfg_print_cstr(pctx, kw->name);
1943         cfg_print_chars(pctx, " ", 1);
1944         kw->type->print(pctx, obj);
1945 }
1946
1947 static void
1948 doc_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type) {
1949         const keyword_type_t *kw = type->of;
1950         cfg_print_cstr(pctx, kw->name);
1951         cfg_print_chars(pctx, " ", 1);
1952         cfg_doc_obj(pctx, kw->type);
1953 }
1954
1955 static void
1956 doc_optional_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type) {
1957         const keyword_type_t *kw = type->of;
1958         cfg_print_chars(pctx, "[ ", 2);
1959         cfg_print_cstr(pctx, kw->name);
1960         cfg_print_chars(pctx, " ", 1);
1961         cfg_doc_obj(pctx, kw->type);
1962         cfg_print_chars(pctx, " ]", 2);
1963 }
1964
1965 static const char *dialup_enums[] = {
1966         "notify", "notify-passive", "refresh", "passive", NULL };
1967 static isc_result_t
1968 parse_dialup_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1969         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1970 }
1971 static cfg_type_t cfg_type_dialuptype = {
1972         "dialuptype", parse_dialup_type, cfg_print_ustring, doc_enum_or_other,
1973         &cfg_rep_string, dialup_enums
1974 };
1975
1976 static const char *notify_enums[] = { "explicit", "master-only", NULL };
1977 static isc_result_t
1978 parse_notify_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1979         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1980 }
1981 static cfg_type_t cfg_type_notifytype = {
1982         "notifytype", parse_notify_type, cfg_print_ustring, doc_enum_or_other,
1983         &cfg_rep_string, notify_enums,
1984 };
1985
1986 static const char *ixfrdiff_enums[] = { "master", "slave", NULL };
1987 static isc_result_t
1988 parse_ixfrdiff_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1989         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1990 }
1991 static cfg_type_t cfg_type_ixfrdifftype = {
1992         "ixfrdiff", parse_ixfrdiff_type, cfg_print_ustring, doc_enum_or_other,
1993         &cfg_rep_string, ixfrdiff_enums,
1994 };
1995
1996 static const char *v4_aaaa_enums[] = { "break-dnssec", NULL };
1997 static isc_result_t
1998 parse_v4_aaaa(cfg_parser_t *pctx, const cfg_type_t *type,
1999                      cfg_obj_t **ret) {
2000         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
2001 }
2002 static cfg_type_t cfg_type_v4_aaaa = {
2003         "v4_aaaa", parse_v4_aaaa, cfg_print_ustring,
2004         doc_enum_or_other, &cfg_rep_string, v4_aaaa_enums,
2005 };
2006
2007 static keyword_type_t key_kw = { "key", &cfg_type_astring };
2008
2009 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_keyref = {
2010         "keyref", parse_keyvalue, print_keyvalue, doc_keyvalue,
2011         &cfg_rep_string, &key_kw
2012 };
2013
2014 static cfg_type_t cfg_type_optional_keyref = {
2015         "optional_keyref", parse_optional_keyvalue, print_keyvalue,
2016         doc_optional_keyvalue, &cfg_rep_string, &key_kw
2017 };
2018
2019 /*%
2020  * A "controls" statement is represented as a map with the multivalued
2021  * "inet" and "unix" clauses.
2022  */
2023
2024 static keyword_type_t controls_allow_kw = {
2025         "allow", &cfg_type_bracketed_aml };
2026
2027 static cfg_type_t cfg_type_controls_allow = {
2028         "controls_allow", parse_keyvalue,
2029         print_keyvalue, doc_keyvalue,
2030         &cfg_rep_list, &controls_allow_kw
2031 };
2032
2033 static keyword_type_t controls_keys_kw = {
2034         "keys", &cfg_type_keylist };
2035
2036 static cfg_type_t cfg_type_controls_keys = {
2037         "controls_keys", parse_optional_keyvalue,
2038         print_keyvalue, doc_optional_keyvalue,
2039         &cfg_rep_list, &controls_keys_kw
2040 };
2041
2042 static cfg_tuplefielddef_t inetcontrol_fields[] = {
2043         { "address", &cfg_type_controls_sockaddr, 0 },
2044         { "allow", &cfg_type_controls_allow, 0 },
2045         { "keys", &cfg_type_controls_keys, 0 },
2046         { NULL, NULL, 0 }
2047 };
2048
2049 static cfg_type_t cfg_type_inetcontrol = {
2050         "inetcontrol", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
2051         inetcontrol_fields
2052 };
2053
2054 static keyword_type_t controls_perm_kw = {
2055         "perm", &cfg_type_uint32 };
2056
2057 static cfg_type_t cfg_type_controls_perm = {
2058         "controls_perm", parse_keyvalue,
2059         print_keyvalue, doc_keyvalue,
2060         &cfg_rep_uint32, &controls_perm_kw
2061 };
2062
2063 static keyword_type_t controls_owner_kw = {
2064         "owner", &cfg_type_uint32 };
2065
2066 static cfg_type_t cfg_type_controls_owner = {
2067         "controls_owner", parse_keyvalue,
2068         print_keyvalue, doc_keyvalue,
2069         &cfg_rep_uint32, &controls_owner_kw
2070 };
2071
2072 static keyword_type_t controls_group_kw = {
2073         "group", &cfg_type_uint32 };
2074
2075 static cfg_type_t cfg_type_controls_group = {
2076         "controls_allow", parse_keyvalue,
2077         print_keyvalue, doc_keyvalue,
2078         &cfg_rep_uint32, &controls_group_kw
2079 };
2080
2081 static cfg_tuplefielddef_t unixcontrol_fields[] = {
2082         { "path", &cfg_type_qstring, 0 },
2083         { "perm", &cfg_type_controls_perm, 0 },
2084         { "owner", &cfg_type_controls_owner, 0 },
2085         { "group", &cfg_type_controls_group, 0 },
2086         { "keys", &cfg_type_controls_keys, 0 },
2087         { NULL, NULL, 0 }
2088 };
2089
2090 static cfg_type_t cfg_type_unixcontrol = {
2091         "unixcontrol", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
2092         unixcontrol_fields
2093 };
2094
2095 static cfg_clausedef_t
2096 controls_clauses[] = {
2097         { "inet", &cfg_type_inetcontrol, CFG_CLAUSEFLAG_MULTI },
2098         { "unix", &cfg_type_unixcontrol, CFG_CLAUSEFLAG_MULTI },
2099         { NULL, NULL, 0 }
2100 };
2101
2102 static cfg_clausedef_t *
2103 controls_clausesets[] = {
2104         controls_clauses,
2105         NULL
2106 };
2107 static cfg_type_t cfg_type_controls = {
2108         "controls", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,    &controls_clausesets
2109 };
2110
2111 /*%
2112  * A "statistics-channels" statement is represented as a map with the
2113  * multivalued "inet" clauses.
2114  */
2115 static void
2116 doc_optional_bracketed_list(cfg_printer_t *pctx, const cfg_type_t *type) {
2117         const keyword_type_t *kw = type->of;
2118         cfg_print_chars(pctx, "[ ", 2);
2119         cfg_print_cstr(pctx, kw->name);
2120         cfg_print_chars(pctx, " ", 1);
2121         cfg_doc_obj(pctx, kw->type);
2122         cfg_print_chars(pctx, " ]", 2);
2123 }
2124
2125 static cfg_type_t cfg_type_optional_allow = {
2126         "optional_allow", parse_optional_keyvalue, print_keyvalue,
2127         doc_optional_bracketed_list, &cfg_rep_list, &controls_allow_kw
2128 };
2129
2130 static cfg_tuplefielddef_t statserver_fields[] = {
2131         { "address", &cfg_type_controls_sockaddr, 0 }, /* reuse controls def */
2132         { "allow", &cfg_type_optional_allow, 0 },
2133         { NULL, NULL, 0 }
2134 };
2135
2136 static cfg_type_t cfg_type_statschannel = {
2137         "statschannel", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2138         &cfg_rep_tuple, statserver_fields
2139 };
2140
2141 static cfg_clausedef_t
2142 statservers_clauses[] = {
2143         { "inet", &cfg_type_statschannel, CFG_CLAUSEFLAG_MULTI },
2144         { NULL, NULL, 0 }
2145 };
2146
2147 static cfg_clausedef_t *
2148 statservers_clausesets[] = {
2149         statservers_clauses,
2150         NULL
2151 };
2152
2153 static cfg_type_t cfg_type_statschannels = {
2154         "statistics-channels", cfg_parse_map, cfg_print_map, cfg_doc_map,
2155         &cfg_rep_map,   &statservers_clausesets
2156 };
2157
2158 /*%
2159  * An optional class, as used in view and zone statements.
2160  */
2161 static isc_result_t
2162 parse_optional_class(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2163         isc_result_t result;
2164         UNUSED(type);
2165         CHECK(cfg_peektoken(pctx, 0));
2166         if (pctx->token.type == isc_tokentype_string)
2167                 CHECK(cfg_parse_obj(pctx, &cfg_type_ustring, ret));
2168         else
2169                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
2170  cleanup:
2171         return (result);
2172 }
2173
2174 static cfg_type_t cfg_type_optional_class = {
2175         "optional_class", parse_optional_class, NULL, cfg_doc_terminal,
2176         NULL, NULL
2177 };
2178
2179 static isc_result_t
2180 parse_querysource(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2181         isc_result_t result;
2182         cfg_obj_t *obj = NULL;
2183         isc_netaddr_t netaddr;
2184         in_port_t port;
2185         unsigned int have_address = 0;
2186         unsigned int have_port = 0;
2187         const unsigned int *flagp = type->of;
2188
2189         if ((*flagp & CFG_ADDR_V4OK) != 0)
2190                 isc_netaddr_any(&netaddr);
2191         else if ((*flagp & CFG_ADDR_V6OK) != 0)
2192                 isc_netaddr_any6(&netaddr);
2193         else
2194                 INSIST(0);
2195
2196         port = 0;
2197
2198         for (;;) {
2199                 CHECK(cfg_peektoken(pctx, 0));
2200                 if (pctx->token.type == isc_tokentype_string) {
2201                         if (strcasecmp(TOKEN_STRING(pctx),
2202                                        "address") == 0)
2203                         {
2204                                 /* read "address" */
2205                                 CHECK(cfg_gettoken(pctx, 0));
2206                                 CHECK(cfg_parse_rawaddr(pctx, *flagp,
2207                                                         &netaddr));
2208                                 have_address++;
2209                         } else if (strcasecmp(TOKEN_STRING(pctx), "port") == 0)
2210                         {
2211                                 /* read "port" */
2212                                 CHECK(cfg_gettoken(pctx, 0));
2213                                 CHECK(cfg_parse_rawport(pctx,
2214                                                         CFG_ADDR_WILDOK,
2215                                                         &port));
2216                                 have_port++;
2217                         } else if (have_port == 0 && have_address == 0) {
2218                                 return (cfg_parse_sockaddr(pctx, type, ret));
2219                         } else {
2220                                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2221                                              "expected 'address' or 'port'");
2222                                 return (ISC_R_UNEXPECTEDTOKEN);
2223                         }
2224                 } else
2225                         break;
2226         }
2227         if (have_address > 1 || have_port > 1 ||
2228             have_address + have_port == 0) {
2229                 cfg_parser_error(pctx, 0, "expected one address and/or port");
2230                 return (ISC_R_UNEXPECTEDTOKEN);
2231         }
2232
2233         CHECK(cfg_create_obj(pctx, &cfg_type_querysource, &obj));
2234         isc_sockaddr_fromnetaddr(&obj->value.sockaddr, &netaddr, port);
2235         *ret = obj;
2236         return (ISC_R_SUCCESS);
2237
2238  cleanup:
2239         cfg_parser_error(pctx, CFG_LOG_NEAR, "invalid query source");
2240         CLEANUP_OBJ(obj);
2241         return (result);
2242 }
2243
2244 static void
2245 print_querysource(cfg_printer_t *pctx, const cfg_obj_t *obj) {
2246         isc_netaddr_t na;
2247         isc_netaddr_fromsockaddr(&na, &obj->value.sockaddr);
2248         cfg_print_cstr(pctx, "address ");
2249         cfg_print_rawaddr(pctx, &na);
2250         cfg_print_cstr(pctx, " port ");
2251         cfg_print_rawuint(pctx, isc_sockaddr_getport(&obj->value.sockaddr));
2252 }
2253
2254 static unsigned int sockaddr4wild_flags = CFG_ADDR_WILDOK | CFG_ADDR_V4OK;
2255 static unsigned int sockaddr6wild_flags = CFG_ADDR_WILDOK | CFG_ADDR_V6OK;
2256
2257 static cfg_type_t cfg_type_querysource4 = {
2258         "querysource4", parse_querysource, NULL, cfg_doc_terminal,
2259         NULL, &sockaddr4wild_flags
2260 };
2261
2262 static cfg_type_t cfg_type_querysource6 = {
2263         "querysource6", parse_querysource, NULL, cfg_doc_terminal,
2264         NULL, &sockaddr6wild_flags
2265 };
2266
2267 static cfg_type_t cfg_type_querysource = {
2268         "querysource", NULL, print_querysource, NULL, &cfg_rep_sockaddr, NULL
2269 };
2270
2271 /*% addrmatchelt */
2272
2273 static isc_result_t
2274 parse_addrmatchelt(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2275         isc_result_t result;
2276         UNUSED(type);
2277
2278         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2279
2280         if (pctx->token.type == isc_tokentype_string ||
2281             pctx->token.type == isc_tokentype_qstring) {
2282                 if (pctx->token.type == isc_tokentype_string &&
2283                     (strcasecmp(TOKEN_STRING(pctx), "key") == 0)) {
2284                         CHECK(cfg_parse_obj(pctx, &cfg_type_keyref, ret));
2285                 } else {
2286                         if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK |
2287                                                   CFG_ADDR_V4PREFIXOK |
2288                                                   CFG_ADDR_V6OK))
2289                         {
2290                                 CHECK(cfg_parse_netprefix(pctx, NULL, ret));
2291                         } else {
2292                                 CHECK(cfg_parse_astring(pctx, NULL, ret));
2293                         }
2294                 }
2295         } else if (pctx->token.type == isc_tokentype_special) {
2296                 if (pctx->token.value.as_char == '{') {
2297                         /* Nested match list. */
2298                         CHECK(cfg_parse_obj(pctx, &cfg_type_bracketed_aml, ret));
2299                 } else if (pctx->token.value.as_char == '!') {
2300                         CHECK(cfg_gettoken(pctx, 0)); /* read "!" */
2301                         CHECK(cfg_parse_obj(pctx, &cfg_type_negated, ret));
2302                 } else {
2303                         goto bad;
2304                 }
2305         } else {
2306         bad:
2307                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2308                              "expected IP match list element");
2309                 return (ISC_R_UNEXPECTEDTOKEN);
2310         }
2311  cleanup:
2312         return (result);
2313 }
2314
2315 /*%
2316  * A negated address match list element (like "! 10.0.0.1").
2317  * Somewhat sneakily, the caller is expected to parse the
2318  * "!", but not to print it.
2319  */
2320
2321 static cfg_tuplefielddef_t negated_fields[] = {
2322         { "value", &cfg_type_addrmatchelt, 0 },
2323         { NULL, NULL, 0 }
2324 };
2325
2326 static void
2327 print_negated(cfg_printer_t *pctx, const cfg_obj_t *obj) {
2328         cfg_print_chars(pctx, "!", 1);
2329         cfg_print_tuple(pctx, obj);
2330 }
2331
2332 static cfg_type_t cfg_type_negated = {
2333         "negated", cfg_parse_tuple, print_negated, NULL, &cfg_rep_tuple,
2334         &negated_fields
2335 };
2336
2337 /*% An address match list element */
2338
2339 static cfg_type_t cfg_type_addrmatchelt = {
2340         "address_match_element", parse_addrmatchelt, NULL, cfg_doc_terminal,
2341         NULL, NULL
2342 };
2343
2344 /*% A bracketed address match list */
2345
2346 static cfg_type_t cfg_type_bracketed_aml = {
2347         "bracketed_aml", cfg_parse_bracketed_list, cfg_print_bracketed_list,
2348         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_addrmatchelt
2349 };
2350
2351 /*%
2352  * The socket address syntax in the "controls" statement is silly.
2353  * It allows both socket address families, but also allows "*",
2354  * whis is gratuitously interpreted as the IPv4 wildcard address.
2355  */
2356 static unsigned int controls_sockaddr_flags =
2357         CFG_ADDR_V4OK | CFG_ADDR_V6OK | CFG_ADDR_WILDOK;
2358 static cfg_type_t cfg_type_controls_sockaddr = {
2359         "controls_sockaddr", cfg_parse_sockaddr, cfg_print_sockaddr,
2360         cfg_doc_sockaddr, &cfg_rep_sockaddr, &controls_sockaddr_flags
2361 };
2362
2363 /*%
2364  * Handle the special kludge syntax of the "keys" clause in the "server"
2365  * statement, which takes a single key with or without braces and semicolon.
2366  */
2367 static isc_result_t
2368 parse_server_key_kludge(cfg_parser_t *pctx, const cfg_type_t *type,
2369                         cfg_obj_t **ret)
2370 {
2371         isc_result_t result;
2372         isc_boolean_t braces = ISC_FALSE;
2373         UNUSED(type);
2374
2375         /* Allow opening brace. */
2376         CHECK(cfg_peektoken(pctx, 0));
2377         if (pctx->token.type == isc_tokentype_special &&
2378             pctx->token.value.as_char == '{') {
2379                 CHECK(cfg_gettoken(pctx, 0));
2380                 braces = ISC_TRUE;
2381         }
2382
2383         CHECK(cfg_parse_obj(pctx, &cfg_type_astring, ret));
2384
2385         if (braces) {
2386                 /* Skip semicolon if present. */
2387                 CHECK(cfg_peektoken(pctx, 0));
2388                 if (pctx->token.type == isc_tokentype_special &&
2389                     pctx->token.value.as_char == ';')
2390                         CHECK(cfg_gettoken(pctx, 0));
2391
2392                 CHECK(cfg_parse_special(pctx, '}'));
2393         }
2394  cleanup:
2395         return (result);
2396 }
2397 static cfg_type_t cfg_type_server_key_kludge = {
2398         "server_key", parse_server_key_kludge, NULL, cfg_doc_terminal,
2399         NULL, NULL
2400 };
2401
2402
2403 /*%
2404  * An optional logging facility.
2405  */
2406
2407 static isc_result_t
2408 parse_optional_facility(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
2409 {
2410         isc_result_t result;
2411         UNUSED(type);
2412
2413         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2414         if (pctx->token.type == isc_tokentype_string ||
2415             pctx->token.type == isc_tokentype_qstring) {
2416                 CHECK(cfg_parse_obj(pctx, &cfg_type_astring, ret));
2417         } else {
2418                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
2419         }
2420  cleanup:
2421         return (result);
2422 }
2423
2424 static cfg_type_t cfg_type_optional_facility = {
2425         "optional_facility", parse_optional_facility, NULL, cfg_doc_terminal,
2426         NULL, NULL };
2427
2428
2429 /*%
2430  * A log severity.  Return as a string, except "debug N",
2431  * which is returned as a keyword object.
2432  */
2433
2434 static keyword_type_t debug_kw = { "debug", &cfg_type_uint32 };
2435 static cfg_type_t cfg_type_debuglevel = {
2436         "debuglevel", parse_keyvalue,
2437         print_keyvalue, doc_keyvalue,
2438         &cfg_rep_uint32, &debug_kw
2439 };
2440
2441 static isc_result_t
2442 parse_logseverity(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2443         isc_result_t result;
2444         UNUSED(type);
2445
2446         CHECK(cfg_peektoken(pctx, 0));
2447         if (pctx->token.type == isc_tokentype_string &&
2448             strcasecmp(TOKEN_STRING(pctx), "debug") == 0) {
2449                 CHECK(cfg_gettoken(pctx, 0)); /* read "debug" */
2450                 CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER));
2451                 if (pctx->token.type == isc_tokentype_number) {
2452                         CHECK(cfg_parse_uint32(pctx, NULL, ret));
2453                 } else {
2454                         /*
2455                          * The debug level is optional and defaults to 1.
2456                          * This makes little sense, but we support it for
2457                          * compatibility with BIND 8.
2458                          */
2459                         CHECK(cfg_create_obj(pctx, &cfg_type_uint32, ret));
2460                         (*ret)->value.uint32 = 1;
2461                 }
2462                 (*ret)->type = &cfg_type_debuglevel; /* XXX kludge */
2463         } else {
2464                 CHECK(cfg_parse_obj(pctx, &cfg_type_loglevel, ret));
2465         }
2466  cleanup:
2467         return (result);
2468 }
2469
2470 static cfg_type_t cfg_type_logseverity = {
2471         "log_severity", parse_logseverity, NULL, cfg_doc_terminal,
2472         NULL, NULL };
2473
2474 /*%
2475  * The "file" clause of the "channel" statement.
2476  * This is yet another special case.
2477  */
2478
2479 static const char *logversions_enums[] = { "unlimited", NULL };
2480 static isc_result_t
2481 parse_logversions(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2482         return (parse_enum_or_other(pctx, type, &cfg_type_uint32, ret));
2483 }
2484
2485 static cfg_type_t cfg_type_logversions = {
2486         "logversions", parse_logversions, cfg_print_ustring, cfg_doc_terminal,
2487         &cfg_rep_string, logversions_enums
2488 };
2489
2490 static cfg_tuplefielddef_t logfile_fields[] = {
2491         { "file", &cfg_type_qstring, 0 },
2492         { "versions", &cfg_type_logversions, 0 },
2493         { "size", &cfg_type_size, 0 },
2494         { NULL, NULL, 0 }
2495 };
2496
2497 static isc_result_t
2498 parse_logfile(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2499         isc_result_t result;
2500         cfg_obj_t *obj = NULL;
2501         const cfg_tuplefielddef_t *fields = type->of;
2502
2503         CHECK(cfg_create_tuple(pctx, type, &obj));
2504
2505         /* Parse the mandatory "file" field */
2506         CHECK(cfg_parse_obj(pctx, fields[0].type, &obj->value.tuple[0]));
2507
2508         /* Parse "versions" and "size" fields in any order. */
2509         for (;;) {
2510                 CHECK(cfg_peektoken(pctx, 0));
2511                 if (pctx->token.type == isc_tokentype_string) {
2512                         CHECK(cfg_gettoken(pctx, 0));
2513                         if (strcasecmp(TOKEN_STRING(pctx),
2514                                        "versions") == 0 &&
2515                             obj->value.tuple[1] == NULL) {
2516                                 CHECK(cfg_parse_obj(pctx, fields[1].type,
2517                                             &obj->value.tuple[1]));
2518                         } else if (strcasecmp(TOKEN_STRING(pctx),
2519                                               "size") == 0 &&
2520                                    obj->value.tuple[2] == NULL) {
2521                                 CHECK(cfg_parse_obj(pctx, fields[2].type,
2522                                             &obj->value.tuple[2]));
2523                         } else {
2524                                 break;
2525                         }
2526                 } else {
2527                         break;
2528                 }
2529         }
2530
2531         /* Create void objects for missing optional values. */
2532         if (obj->value.tuple[1] == NULL)
2533                 CHECK(cfg_parse_void(pctx, NULL, &obj->value.tuple[1]));
2534         if (obj->value.tuple[2] == NULL)
2535                 CHECK(cfg_parse_void(pctx, NULL, &obj->value.tuple[2]));
2536
2537         *ret = obj;
2538         return (ISC_R_SUCCESS);
2539
2540  cleanup:
2541         CLEANUP_OBJ(obj);
2542         return (result);
2543 }
2544
2545 static void
2546 print_logfile(cfg_printer_t *pctx, const cfg_obj_t *obj) {
2547         cfg_print_obj(pctx, obj->value.tuple[0]); /* file */
2548         if (obj->value.tuple[1]->type->print != cfg_print_void) {
2549                 cfg_print_cstr(pctx, " versions ");
2550                 cfg_print_obj(pctx, obj->value.tuple[1]);
2551         }
2552         if (obj->value.tuple[2]->type->print != cfg_print_void) {
2553                 cfg_print_cstr(pctx, " size ");
2554                 cfg_print_obj(pctx, obj->value.tuple[2]);
2555         }
2556 }
2557
2558
2559 static void
2560 doc_logfile(cfg_printer_t *pctx, const cfg_type_t *type) {
2561         UNUSED(type);
2562         cfg_print_cstr(pctx, "<quoted_string>");
2563         cfg_print_chars(pctx, " ", 1);
2564         cfg_print_cstr(pctx, "[ versions ( \"unlimited\" | <integer> ) ]");
2565         cfg_print_chars(pctx, " ", 1);
2566         cfg_print_cstr(pctx, "[ size <size> ]");
2567 }
2568
2569 static cfg_type_t cfg_type_logfile = {
2570         "log_file", parse_logfile, print_logfile, doc_logfile,
2571         &cfg_rep_tuple, logfile_fields
2572 };
2573
2574 /*% An IPv4 address with optional port, "*" accepted as wildcard. */
2575 static cfg_type_t cfg_type_sockaddr4wild = {
2576         "sockaddr4wild", cfg_parse_sockaddr, cfg_print_sockaddr,
2577         cfg_doc_sockaddr, &cfg_rep_sockaddr, &sockaddr4wild_flags
2578 };
2579
2580 /*% An IPv6 address with optional port, "*" accepted as wildcard. */
2581 static cfg_type_t cfg_type_sockaddr6wild = {
2582         "v6addrportwild", cfg_parse_sockaddr, cfg_print_sockaddr,
2583         cfg_doc_sockaddr, &cfg_rep_sockaddr, &sockaddr6wild_flags
2584 };
2585
2586 /*%
2587  * lwres
2588  */
2589
2590 static cfg_tuplefielddef_t lwres_view_fields[] = {
2591         { "name", &cfg_type_astring, 0 },
2592         { "class", &cfg_type_optional_class, 0 },
2593         { NULL, NULL, 0 }
2594 };
2595 static cfg_type_t cfg_type_lwres_view = {
2596         "lwres_view", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
2597         lwres_view_fields
2598 };
2599
2600 static cfg_type_t cfg_type_lwres_searchlist = {
2601         "lwres_searchlist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
2602         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_astring };
2603
2604 static cfg_clausedef_t
2605 lwres_clauses[] = {
2606         { "listen-on", &cfg_type_portiplist, 0 },
2607         { "view", &cfg_type_lwres_view, 0 },
2608         { "search", &cfg_type_lwres_searchlist, 0 },
2609         { "ndots", &cfg_type_uint32, 0 },
2610         { NULL, NULL, 0 }
2611 };
2612
2613 static cfg_clausedef_t *
2614 lwres_clausesets[] = {
2615         lwres_clauses,
2616         NULL
2617 };
2618 static cfg_type_t cfg_type_lwres = {
2619         "lwres", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,
2620         lwres_clausesets
2621 };
2622
2623 /*%
2624  * rndc
2625  */
2626
2627 static cfg_clausedef_t
2628 rndcconf_options_clauses[] = {
2629         { "default-key", &cfg_type_astring, 0 },
2630         { "default-port", &cfg_type_uint32, 0 },
2631         { "default-server", &cfg_type_astring, 0 },
2632         { "default-source-address", &cfg_type_netaddr4wild, 0 },
2633         { "default-source-address-v6", &cfg_type_netaddr6wild, 0 },
2634         { NULL, NULL, 0 }
2635 };
2636
2637 static cfg_clausedef_t *
2638 rndcconf_options_clausesets[] = {
2639         rndcconf_options_clauses,
2640         NULL
2641 };
2642
2643 static cfg_type_t cfg_type_rndcconf_options = {
2644         "rndcconf_options", cfg_parse_map, cfg_print_map, cfg_doc_map,
2645         &cfg_rep_map, rndcconf_options_clausesets
2646 };
2647
2648 static cfg_clausedef_t
2649 rndcconf_server_clauses[] = {
2650         { "key", &cfg_type_astring, 0 },
2651         { "port", &cfg_type_uint32, 0 },
2652         { "source-address", &cfg_type_netaddr4wild, 0 },
2653         { "source-address-v6", &cfg_type_netaddr6wild, 0 },
2654         { "addresses", &cfg_type_bracketed_sockaddrnameportlist, 0 },
2655         { NULL, NULL, 0 }
2656 };
2657
2658 static cfg_clausedef_t *
2659 rndcconf_server_clausesets[] = {
2660         rndcconf_server_clauses,
2661         NULL
2662 };
2663
2664 static cfg_type_t cfg_type_rndcconf_server = {
2665         "rndcconf_server", cfg_parse_named_map, cfg_print_map, cfg_doc_map,
2666         &cfg_rep_map, rndcconf_server_clausesets
2667 };
2668
2669 static cfg_clausedef_t
2670 rndcconf_clauses[] = {
2671         { "key", &cfg_type_key, CFG_CLAUSEFLAG_MULTI },
2672         { "server", &cfg_type_rndcconf_server, CFG_CLAUSEFLAG_MULTI },
2673         { "options", &cfg_type_rndcconf_options, 0 },
2674         { NULL, NULL, 0 }
2675 };
2676
2677 static cfg_clausedef_t *
2678 rndcconf_clausesets[] = {
2679         rndcconf_clauses,
2680         NULL
2681 };
2682
2683 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_rndcconf = {
2684         "rndcconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2685         &cfg_rep_map, rndcconf_clausesets
2686 };
2687
2688 static cfg_clausedef_t
2689 rndckey_clauses[] = {
2690         { "key", &cfg_type_key, 0 },
2691         { NULL, NULL, 0 }
2692 };
2693
2694 static cfg_clausedef_t *
2695 rndckey_clausesets[] = {
2696         rndckey_clauses,
2697         NULL
2698 };
2699
2700 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_rndckey = {
2701         "rndckey", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2702         &cfg_rep_map, rndckey_clausesets
2703 };
2704
2705 /*
2706  * session.key has exactly the same syntax as rndc.key, but it's defined
2707  * separately for clarity (and so we can extend it someday, if needed).
2708  */
2709 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_sessionkey = {
2710         "sessionkey", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2711         &cfg_rep_map, rndckey_clausesets
2712 };
2713
2714 static cfg_tuplefielddef_t nameport_fields[] = {
2715         { "name", &cfg_type_astring, 0 },
2716         { "port", &cfg_type_optional_port, 0 },
2717         { NULL, NULL, 0 }
2718 };
2719 static cfg_type_t cfg_type_nameport = {
2720         "nameport", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2721         &cfg_rep_tuple, nameport_fields
2722 };
2723
2724 static void
2725 doc_sockaddrnameport(cfg_printer_t *pctx, const cfg_type_t *type) {
2726         UNUSED(type);
2727         cfg_print_chars(pctx, "( ", 2);
2728         cfg_print_cstr(pctx, "<quoted_string>");
2729         cfg_print_chars(pctx, " ", 1);
2730         cfg_print_cstr(pctx, "[ port <integer> ]");
2731         cfg_print_chars(pctx, " | ", 3);
2732         cfg_print_cstr(pctx, "<ipv4_address>");
2733         cfg_print_chars(pctx, " ", 1);
2734         cfg_print_cstr(pctx, "[ port <integer> ]");
2735         cfg_print_chars(pctx, " | ", 3);
2736         cfg_print_cstr(pctx, "<ipv6_address>");
2737         cfg_print_chars(pctx, " ", 1);
2738         cfg_print_cstr(pctx, "[ port <integer> ]");
2739         cfg_print_chars(pctx, " )", 2);
2740 }
2741
2742 static isc_result_t
2743 parse_sockaddrnameport(cfg_parser_t *pctx, const cfg_type_t *type,
2744                        cfg_obj_t **ret)
2745 {
2746         isc_result_t result;
2747         cfg_obj_t *obj = NULL;
2748         UNUSED(type);
2749
2750         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2751         if (pctx->token.type == isc_tokentype_string ||
2752             pctx->token.type == isc_tokentype_qstring) {
2753                 if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK | CFG_ADDR_V6OK))
2754                         CHECK(cfg_parse_sockaddr(pctx, &cfg_type_sockaddr, ret));
2755                 else {
2756                         const cfg_tuplefielddef_t *fields =
2757                                                    cfg_type_nameport.of;
2758                         CHECK(cfg_create_tuple(pctx, &cfg_type_nameport,
2759                                                &obj));
2760                         CHECK(cfg_parse_obj(pctx, fields[0].type,
2761                                             &obj->value.tuple[0]));
2762                         CHECK(cfg_parse_obj(pctx, fields[1].type,
2763                                             &obj->value.tuple[1]));
2764                         *ret = obj;
2765                         obj = NULL;
2766                 }
2767         } else {
2768                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2769                              "expected IP address or hostname");
2770                 return (ISC_R_UNEXPECTEDTOKEN);
2771         }
2772  cleanup:
2773         CLEANUP_OBJ(obj);
2774         return (result);
2775 }
2776
2777 static cfg_type_t cfg_type_sockaddrnameport = {
2778         "sockaddrnameport_element", parse_sockaddrnameport, NULL,
2779          doc_sockaddrnameport, NULL, NULL
2780 };
2781
2782 static cfg_type_t cfg_type_bracketed_sockaddrnameportlist = {
2783         "bracketed_sockaddrnameportlist", cfg_parse_bracketed_list,
2784         cfg_print_bracketed_list, cfg_doc_bracketed_list,
2785         &cfg_rep_list, &cfg_type_sockaddrnameport
2786 };
2787
2788 /*%
2789  * A list of socket addresses or name with an optional default port,
2790  * as used in the dual-stack-servers option.  E.g.,
2791  * "port 1234 { dual-stack-servers.net; 10.0.0.1; 1::2 port 69; }"
2792  */
2793 static cfg_tuplefielddef_t nameportiplist_fields[] = {
2794         { "port", &cfg_type_optional_port, 0 },
2795         { "addresses", &cfg_type_bracketed_sockaddrnameportlist, 0 },
2796         { NULL, NULL, 0 }
2797 };
2798
2799 static cfg_type_t cfg_type_nameportiplist = {
2800         "nameportiplist", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2801         &cfg_rep_tuple, nameportiplist_fields
2802 };
2803
2804 /*%
2805  * masters element.
2806  */
2807
2808 static void
2809 doc_masterselement(cfg_printer_t *pctx, const cfg_type_t *type) {
2810         UNUSED(type);
2811         cfg_print_chars(pctx, "( ", 2);
2812         cfg_print_cstr(pctx, "<masters>");
2813         cfg_print_chars(pctx, " | ", 3);
2814         cfg_print_cstr(pctx, "<ipv4_address>");
2815         cfg_print_chars(pctx, " ", 1);
2816         cfg_print_cstr(pctx, "[ port <integer> ]");
2817         cfg_print_chars(pctx, " | ", 3);
2818         cfg_print_cstr(pctx, "<ipv6_address>");
2819         cfg_print_chars(pctx, " ", 1);
2820         cfg_print_cstr(pctx, "[ port <integer> ]");
2821         cfg_print_chars(pctx, " )", 2);
2822 }
2823
2824 static isc_result_t
2825 parse_masterselement(cfg_parser_t *pctx, const cfg_type_t *type,
2826                      cfg_obj_t **ret)
2827 {
2828         isc_result_t result;
2829         cfg_obj_t *obj = NULL;
2830         UNUSED(type);
2831
2832         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2833         if (pctx->token.type == isc_tokentype_string ||
2834             pctx->token.type == isc_tokentype_qstring) {
2835                 if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK | CFG_ADDR_V6OK))
2836                         CHECK(cfg_parse_sockaddr(pctx, &cfg_type_sockaddr, ret));
2837                 else
2838                         CHECK(cfg_parse_astring(pctx, &cfg_type_astring, ret));
2839         } else {
2840                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2841                              "expected IP address or masters name");
2842                 return (ISC_R_UNEXPECTEDTOKEN);
2843         }
2844  cleanup:
2845         CLEANUP_OBJ(obj);
2846         return (result);
2847 }
2848
2849 static cfg_type_t cfg_type_masterselement = {
2850         "masters_element", parse_masterselement, NULL,
2851          doc_masterselement, NULL, NULL
2852 };