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