]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/lib/isccfg/namedconf.c
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[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-udp-size", &cfg_type_uint32, 0 },
1423         { "min-roots", &cfg_type_uint32, CFG_CLAUSEFLAG_NOTIMP },
1424         { "minimal-responses", &cfg_type_boolean, 0 },
1425         { "preferred-glue", &cfg_type_astring, 0 },
1426         { "provide-ixfr", &cfg_type_boolean, 0 },
1427         /*
1428          * Note that the query-source option syntax is different
1429          * from the other -source options.
1430          */
1431         { "query-source", &cfg_type_querysource4, 0 },
1432         { "query-source-v6", &cfg_type_querysource6, 0 },
1433         { "queryport-pool-ports", &cfg_type_uint32, CFG_CLAUSEFLAG_OBSOLETE },
1434         { "queryport-pool-updateinterval", &cfg_type_uint32,
1435           CFG_CLAUSEFLAG_OBSOLETE },
1436         { "recursion", &cfg_type_boolean, 0 },
1437         { "request-nsid", &cfg_type_boolean, 0 },
1438         { "resolver-query-timeout", &cfg_type_uint32, 0 },
1439         { "rfc2308-type1", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
1440         { "root-delegation-only",  &cfg_type_optional_exclude, 0 },
1441         { "rrset-order", &cfg_type_rrsetorder, 0 },
1442         { "sortlist", &cfg_type_bracketed_aml, 0 },
1443         { "suppress-initial-notify", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
1444         { "topology", &cfg_type_bracketed_aml, CFG_CLAUSEFLAG_NOTIMP },
1445         { "transfer-format", &cfg_type_transferformat, 0 },
1446         { "use-queryport-pool", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1447         { "zero-no-soa-ttl-cache", &cfg_type_boolean, 0 },
1448 #ifdef ALLOW_FILTER_AAAA_ON_V4
1449         { "filter-aaaa", &cfg_type_bracketed_aml, 0 },
1450         { "filter-aaaa-on-v4", &cfg_type_v4_aaaa, 0 },
1451 #else
1452         { "filter-aaaa", &cfg_type_bracketed_aml,
1453            CFG_CLAUSEFLAG_NOTCONFIGURED },
1454         { "filter-aaaa-on-v4", &cfg_type_v4_aaaa,
1455            CFG_CLAUSEFLAG_NOTCONFIGURED },
1456 #endif
1457         { "response-policy", &cfg_type_rpz, 0 },
1458 #ifdef USE_RRL
1459         { "rate-limit", &cfg_type_rrl, 0 },
1460 #endif /* USE_RRL */
1461         { NULL, NULL, 0 }
1462 };
1463
1464 /*%
1465  * Clauses that can be found within the 'view' statement only.
1466  */
1467 static cfg_clausedef_t
1468 view_only_clauses[] = {
1469         { "match-clients", &cfg_type_bracketed_aml, 0 },
1470         { "match-destinations", &cfg_type_bracketed_aml, 0 },
1471         { "match-recursive-only", &cfg_type_boolean, 0 },
1472         { NULL, NULL, 0 }
1473 };
1474
1475 /*%
1476  * Sig-validity-interval.
1477  */
1478 static isc_result_t
1479 parse_optional_uint32(cfg_parser_t *pctx, const cfg_type_t *type,
1480                       cfg_obj_t **ret)
1481 {
1482         isc_result_t result;
1483         UNUSED(type);
1484
1485         CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER | ISC_LEXOPT_CNUMBER));
1486         if (pctx->token.type == isc_tokentype_number) {
1487                 CHECK(cfg_parse_obj(pctx, &cfg_type_uint32, ret));
1488         } else {
1489                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
1490         }
1491  cleanup:
1492         return (result);
1493 }
1494
1495 static void
1496 doc_optional_uint32(cfg_printer_t *pctx, const cfg_type_t *type) {
1497         UNUSED(type);
1498         cfg_print_cstr(pctx, "[ <integer> ]");
1499 }
1500
1501 static cfg_type_t cfg_type_optional_uint32 = {
1502         "optional_uint32", parse_optional_uint32, NULL, doc_optional_uint32,
1503         NULL, NULL };
1504
1505 static cfg_tuplefielddef_t validityinterval_fields[] = {
1506         { "validity", &cfg_type_uint32, 0 },
1507         { "re-sign", &cfg_type_optional_uint32, 0 },
1508         { NULL, NULL, 0 }
1509 };
1510
1511 static cfg_type_t cfg_type_validityinterval = {
1512         "validityinterval", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
1513         &cfg_rep_tuple, validityinterval_fields
1514 };
1515
1516 /*%
1517  * Clauses that can be found in a 'zone' statement,
1518  * with defaults in the 'view' or 'options' statement.
1519  */
1520 static cfg_clausedef_t
1521 zone_clauses[] = {
1522         { "allow-notify", &cfg_type_bracketed_aml, 0 },
1523         { "allow-query", &cfg_type_bracketed_aml, 0 },
1524         { "allow-query-on", &cfg_type_bracketed_aml, 0 },
1525         { "allow-transfer", &cfg_type_bracketed_aml, 0 },
1526         { "allow-update", &cfg_type_bracketed_aml, 0 },
1527         { "allow-update-forwarding", &cfg_type_bracketed_aml, 0 },
1528         { "also-notify", &cfg_type_namesockaddrkeylist, 0 },
1529         { "alt-transfer-source", &cfg_type_sockaddr4wild, 0 },
1530         { "alt-transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1531         { "auto-dnssec", &cfg_type_autodnssec, 0 },
1532         { "check-dup-records", &cfg_type_checkmode, 0 },
1533         { "check-integrity", &cfg_type_boolean, 0 },
1534         { "check-mx", &cfg_type_checkmode, 0 },
1535         { "check-mx-cname", &cfg_type_checkmode, 0 },
1536         { "check-sibling", &cfg_type_boolean, 0 },
1537         { "check-spf", &cfg_type_warn, 0 },
1538         { "check-srv-cname", &cfg_type_checkmode, 0 },
1539         { "check-wildcard", &cfg_type_boolean, 0 },
1540         { "dialup", &cfg_type_dialuptype, 0 },
1541         { "dnssec-dnskey-kskonly", &cfg_type_boolean, 0 },
1542         { "dnssec-loadkeys-interval", &cfg_type_uint32, 0 },
1543         { "dnssec-secure-to-insecure", &cfg_type_boolean, 0 },
1544         { "dnssec-update-mode", &cfg_type_dnssecupdatemode, 0 },
1545         { "forward", &cfg_type_forwardtype, 0 },
1546         { "forwarders", &cfg_type_portiplist, 0 },
1547         { "key-directory", &cfg_type_qstring, 0 },
1548         { "maintain-ixfr-base", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1549         { "masterfile-format", &cfg_type_masterformat, 0 },
1550         { "max-ixfr-log-size", &cfg_type_size, CFG_CLAUSEFLAG_OBSOLETE },
1551         { "max-journal-size", &cfg_type_sizenodefault, 0 },
1552         { "max-refresh-time", &cfg_type_uint32, 0 },
1553         { "max-retry-time", &cfg_type_uint32, 0 },
1554         { "max-transfer-idle-in", &cfg_type_uint32, 0 },
1555         { "max-transfer-idle-out", &cfg_type_uint32, 0 },
1556         { "max-transfer-time-in", &cfg_type_uint32, 0 },
1557         { "max-transfer-time-out", &cfg_type_uint32, 0 },
1558         { "min-refresh-time", &cfg_type_uint32, 0 },
1559         { "min-retry-time", &cfg_type_uint32, 0 },
1560         { "multi-master", &cfg_type_boolean, 0 },
1561         { "notify", &cfg_type_notifytype, 0 },
1562         { "notify-delay", &cfg_type_uint32, 0 },
1563         { "notify-source", &cfg_type_sockaddr4wild, 0 },
1564         { "notify-source-v6", &cfg_type_sockaddr6wild, 0 },
1565         { "notify-to-soa", &cfg_type_boolean, 0 },
1566         { "nsec3-test-zone", &cfg_type_boolean, CFG_CLAUSEFLAG_TESTONLY },
1567         { "serial-update-method", &cfg_type_updatemethod, 0 },
1568         { "request-ixfr", &cfg_type_boolean, 0 },
1569         { "sig-signing-nodes", &cfg_type_uint32, 0 },
1570         { "sig-signing-signatures", &cfg_type_uint32, 0 },
1571         { "sig-signing-type", &cfg_type_uint32, 0 },
1572         { "sig-validity-interval", &cfg_type_validityinterval, 0 },
1573         { "inline-signing", &cfg_type_boolean, 0 },
1574         { "transfer-source", &cfg_type_sockaddr4wild, 0 },
1575         { "transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1576         { "try-tcp-refresh", &cfg_type_boolean, 0 },
1577         { "update-check-ksk", &cfg_type_boolean, 0 },
1578         { "use-alt-transfer-source", &cfg_type_boolean, 0 },
1579         { "zero-no-soa-ttl", &cfg_type_boolean, 0 },
1580         { "zone-statistics", &cfg_type_zonestat, 0 },
1581         { NULL, NULL, 0 }
1582 };
1583
1584 /*%
1585  * Clauses that can be found in a 'zone' statement
1586  * only.
1587  */
1588 static cfg_clausedef_t
1589 zone_only_clauses[] = {
1590         { "type", &cfg_type_zonetype, 0 },
1591         { "file", &cfg_type_qstring, 0 },
1592         { "journal", &cfg_type_qstring, 0 },
1593         { "ixfr-base", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
1594         { "ixfr-tmp-file", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
1595         { "masters", &cfg_type_namesockaddrkeylist, 0 },
1596         { "pubkey", &cfg_type_pubkey,
1597           CFG_CLAUSEFLAG_MULTI | CFG_CLAUSEFLAG_OBSOLETE },
1598         { "update-policy", &cfg_type_updatepolicy, 0 },
1599         { "database", &cfg_type_astring, 0 },
1600         { "delegation-only", &cfg_type_boolean, 0 },
1601         /*
1602          * Note that the format of the check-names option is different between
1603          * the zone options and the global/view options.  Ugh.
1604          */
1605         { "check-names", &cfg_type_checkmode, 0 },
1606         { "ixfr-from-differences", &cfg_type_boolean, 0 },
1607         { "server-addresses", &cfg_type_bracketed_sockaddrlist, 0 },
1608         { "server-names", &cfg_type_namelist, 0 },
1609         { NULL, NULL, 0 }
1610 };
1611
1612
1613 /*% The top-level named.conf syntax. */
1614
1615 static cfg_clausedef_t *
1616 namedconf_clausesets[] = {
1617         namedconf_clauses,
1618         namedconf_or_view_clauses,
1619         NULL
1620 };
1621 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_namedconf = {
1622         "namedconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1623         &cfg_rep_map, namedconf_clausesets
1624 };
1625
1626 /*% The bind.keys syntax (trusted-keys/managed-keys only). */
1627 static cfg_clausedef_t *
1628 bindkeys_clausesets[] = {
1629         bindkeys_clauses,
1630         NULL
1631 };
1632 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_bindkeys = {
1633         "bindkeys", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1634         &cfg_rep_map, bindkeys_clausesets
1635 };
1636
1637 /*% The new-zone-file syntax (for zones added by 'rndc addzone') */
1638 static cfg_clausedef_t
1639 newzones_clauses[] = {
1640         { "zone", &cfg_type_zone, CFG_CLAUSEFLAG_MULTI },
1641         { NULL, NULL, 0 }
1642 };
1643
1644 static cfg_clausedef_t *
1645 newzones_clausesets[] = {
1646         newzones_clauses,
1647         NULL
1648 };
1649
1650 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_newzones = {
1651         "newzones", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1652         &cfg_rep_map, newzones_clausesets
1653 };
1654
1655 /*% The "options" statement syntax. */
1656
1657 static cfg_clausedef_t *
1658 options_clausesets[] = {
1659         options_clauses,
1660         view_clauses,
1661         zone_clauses,
1662         NULL
1663 };
1664 static cfg_type_t cfg_type_options = {
1665         "options", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, options_clausesets };
1666
1667 /*% The "view" statement syntax. */
1668
1669 static cfg_clausedef_t *
1670 view_clausesets[] = {
1671         view_only_clauses,
1672         namedconf_or_view_clauses,
1673         view_clauses,
1674         zone_clauses,
1675         dynamically_loadable_zones_clauses,
1676         NULL
1677 };
1678 static cfg_type_t cfg_type_viewopts = {
1679         "view", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, view_clausesets };
1680
1681 /*% The "zone" statement syntax. */
1682
1683 static cfg_clausedef_t *
1684 zone_clausesets[] = {
1685         zone_only_clauses,
1686         zone_clauses,
1687         NULL
1688 };
1689 static cfg_type_t cfg_type_zoneopts = {
1690         "zoneopts", cfg_parse_map, cfg_print_map,
1691         cfg_doc_map, &cfg_rep_map, zone_clausesets };
1692
1693 /*% The "dynamically loadable zones" statement syntax. */
1694
1695 static cfg_clausedef_t *
1696 dynamically_loadable_zones_clausesets[] = {
1697         dynamically_loadable_zones_clauses,
1698         NULL
1699 };
1700 static cfg_type_t cfg_type_dynamically_loadable_zones_opts = {
1701         "dynamically_loadable_zones_opts", cfg_parse_map,
1702         cfg_print_map, cfg_doc_map, &cfg_rep_map,
1703         dynamically_loadable_zones_clausesets
1704 };
1705
1706 /*%
1707  * Clauses that can be found within the 'key' statement.
1708  */
1709 static cfg_clausedef_t
1710 key_clauses[] = {
1711         { "algorithm", &cfg_type_astring, 0 },
1712         { "secret", &cfg_type_sstring, 0 },
1713         { NULL, NULL, 0 }
1714 };
1715
1716 static cfg_clausedef_t *
1717 key_clausesets[] = {
1718         key_clauses,
1719         NULL
1720 };
1721 static cfg_type_t cfg_type_key = {
1722         "key", cfg_parse_named_map, cfg_print_map,
1723         cfg_doc_map, &cfg_rep_map, key_clausesets
1724 };
1725
1726
1727 /*%
1728  * Clauses that can be found in a 'server' statement.
1729  */
1730 static cfg_clausedef_t
1731 server_clauses[] = {
1732         { "bogus", &cfg_type_boolean, 0 },
1733         { "provide-ixfr", &cfg_type_boolean, 0 },
1734         { "request-ixfr", &cfg_type_boolean, 0 },
1735         { "support-ixfr", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1736         { "transfers", &cfg_type_uint32, 0 },
1737         { "transfer-format", &cfg_type_transferformat, 0 },
1738         { "keys", &cfg_type_server_key_kludge, 0 },
1739         { "edns", &cfg_type_boolean, 0 },
1740         { "edns-udp-size", &cfg_type_uint32, 0 },
1741         { "max-udp-size", &cfg_type_uint32, 0 },
1742         { "notify-source", &cfg_type_sockaddr4wild, 0 },
1743         { "notify-source-v6", &cfg_type_sockaddr6wild, 0 },
1744         { "query-source", &cfg_type_querysource4, 0 },
1745         { "query-source-v6", &cfg_type_querysource6, 0 },
1746         { "transfer-source", &cfg_type_sockaddr4wild, 0 },
1747         { "transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1748         { NULL, NULL, 0 }
1749 };
1750 static cfg_clausedef_t *
1751 server_clausesets[] = {
1752         server_clauses,
1753         NULL
1754 };
1755 static cfg_type_t cfg_type_server = {
1756         "server", cfg_parse_netprefix_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,
1757         server_clausesets
1758 };
1759
1760
1761 /*%
1762  * Clauses that can be found in a 'channel' clause in the
1763  * 'logging' statement.
1764  *
1765  * These have some additional constraints that need to be
1766  * checked after parsing:
1767  *  - There must exactly one of file/syslog/null/stderr
1768  *
1769  */
1770 static cfg_clausedef_t
1771 channel_clauses[] = {
1772         /* Destinations.  We no longer require these to be first. */
1773         { "file", &cfg_type_logfile, 0 },
1774         { "syslog", &cfg_type_optional_facility, 0 },
1775         { "null", &cfg_type_void, 0 },
1776         { "stderr", &cfg_type_void, 0 },
1777         /* Options.  We now accept these for the null channel, too. */
1778         { "severity", &cfg_type_logseverity, 0 },
1779         { "print-time", &cfg_type_boolean, 0 },
1780         { "print-severity", &cfg_type_boolean, 0 },
1781         { "print-category", &cfg_type_boolean, 0 },
1782         { NULL, NULL, 0 }
1783 };
1784 static cfg_clausedef_t *
1785 channel_clausesets[] = {
1786         channel_clauses,
1787         NULL
1788 };
1789 static cfg_type_t cfg_type_channel = {
1790         "channel", cfg_parse_named_map, cfg_print_map, cfg_doc_map,
1791         &cfg_rep_map, channel_clausesets
1792 };
1793
1794 /*% A list of log destination, used in the "category" clause. */
1795 static cfg_type_t cfg_type_destinationlist = {
1796         "destinationlist", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
1797         &cfg_rep_list, &cfg_type_astring };
1798
1799 /*%
1800  * Clauses that can be found in a 'logging' statement.
1801  */
1802 static cfg_clausedef_t
1803 logging_clauses[] = {
1804         { "channel", &cfg_type_channel, CFG_CLAUSEFLAG_MULTI },
1805         { "category", &cfg_type_category, CFG_CLAUSEFLAG_MULTI },
1806         { NULL, NULL, 0 }
1807 };
1808 static cfg_clausedef_t *
1809 logging_clausesets[] = {
1810         logging_clauses,
1811         NULL
1812 };
1813 static cfg_type_t cfg_type_logging = {
1814         "logging", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, logging_clausesets };
1815
1816
1817 /*%
1818  * For parsing an 'addzone' statement
1819  */
1820
1821 static cfg_tuplefielddef_t addzone_fields[] = {
1822         { "name", &cfg_type_astring, 0 },
1823         { "class", &cfg_type_optional_class, 0 },
1824         { "view", &cfg_type_optional_class, 0 },
1825         { "options", &cfg_type_zoneopts, 0 },
1826         { NULL, NULL, 0 }
1827 };
1828 static cfg_type_t cfg_type_addzone = {
1829         "addzone", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, addzone_fields };
1830
1831 static cfg_clausedef_t
1832 addzoneconf_clauses[] = {
1833         { "addzone", &cfg_type_addzone, 0 },
1834         { NULL, NULL, 0 }
1835 };
1836
1837 static cfg_clausedef_t *
1838 addzoneconf_clausesets[] = {
1839         addzoneconf_clauses,
1840         NULL
1841 };
1842
1843 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_addzoneconf = {
1844         "addzoneconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1845         &cfg_rep_map, addzoneconf_clausesets
1846 };
1847
1848
1849 static isc_result_t
1850 parse_unitstring(char *str, isc_resourcevalue_t *valuep) {
1851         char *endp;
1852         unsigned int len;
1853         isc_uint64_t value;
1854         isc_uint64_t unit;
1855
1856         value = isc_string_touint64(str, &endp, 10);
1857         if (*endp == 0) {
1858                 *valuep = value;
1859                 return (ISC_R_SUCCESS);
1860         }
1861
1862         len = strlen(str);
1863         if (len < 2 || endp[1] != '\0')
1864                 return (ISC_R_FAILURE);
1865
1866         switch (str[len - 1]) {
1867         case 'k':
1868         case 'K':
1869                 unit = 1024;
1870                 break;
1871         case 'm':
1872         case 'M':
1873                 unit = 1024 * 1024;
1874                 break;
1875         case 'g':
1876         case 'G':
1877                 unit = 1024 * 1024 * 1024;
1878                 break;
1879         default:
1880                 return (ISC_R_FAILURE);
1881         }
1882         if (value > ISC_UINT64_MAX / unit)
1883                 return (ISC_R_FAILURE);
1884         *valuep = value * unit;
1885         return (ISC_R_SUCCESS);
1886 }
1887
1888 static isc_result_t
1889 parse_sizeval(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1890         isc_result_t result;
1891         cfg_obj_t *obj = NULL;
1892         isc_uint64_t val;
1893
1894         UNUSED(type);
1895
1896         CHECK(cfg_gettoken(pctx, 0));
1897         if (pctx->token.type != isc_tokentype_string) {
1898                 result = ISC_R_UNEXPECTEDTOKEN;
1899                 goto cleanup;
1900         }
1901         CHECK(parse_unitstring(TOKEN_STRING(pctx), &val));
1902
1903         CHECK(cfg_create_obj(pctx, &cfg_type_uint64, &obj));
1904         obj->value.uint64 = val;
1905         *ret = obj;
1906         return (ISC_R_SUCCESS);
1907
1908  cleanup:
1909         cfg_parser_error(pctx, CFG_LOG_NEAR, "expected integer and optional unit");
1910         return (result);
1911 }
1912
1913 /*%
1914  * A size value (number + optional unit).
1915  */
1916 static cfg_type_t cfg_type_sizeval = {
1917         "sizeval", parse_sizeval, cfg_print_uint64, cfg_doc_terminal,
1918         &cfg_rep_uint64, NULL };
1919
1920 /*%
1921  * A size, "unlimited", or "default".
1922  */
1923
1924 static isc_result_t
1925 parse_size(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1926         return (parse_enum_or_other(pctx, type, &cfg_type_sizeval, ret));
1927 }
1928
1929 static const char *size_enums[] = { "unlimited", "default", NULL };
1930 static cfg_type_t cfg_type_size = {
1931         "size", parse_size, cfg_print_ustring, cfg_doc_terminal,
1932         &cfg_rep_string, size_enums
1933 };
1934
1935 /*%
1936  * A size or "unlimited", but not "default".
1937  */
1938 static const char *sizenodefault_enums[] = { "unlimited", NULL };
1939 static cfg_type_t cfg_type_sizenodefault = {
1940         "size_no_default", parse_size, cfg_print_ustring, cfg_doc_terminal,
1941         &cfg_rep_string, sizenodefault_enums
1942 };
1943
1944 /*%
1945  * optional_keyvalue
1946  */
1947 static isc_result_t
1948 parse_maybe_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type,
1949                               isc_boolean_t optional, cfg_obj_t **ret)
1950 {
1951         isc_result_t result;
1952         cfg_obj_t *obj = NULL;
1953         const keyword_type_t *kw = type->of;
1954
1955         CHECK(cfg_peektoken(pctx, 0));
1956         if (pctx->token.type == isc_tokentype_string &&
1957             strcasecmp(TOKEN_STRING(pctx), kw->name) == 0) {
1958                 CHECK(cfg_gettoken(pctx, 0));
1959                 CHECK(kw->type->parse(pctx, kw->type, &obj));
1960                 obj->type = type; /* XXX kludge */
1961         } else {
1962                 if (optional) {
1963                         CHECK(cfg_parse_void(pctx, NULL, &obj));
1964                 } else {
1965                         cfg_parser_error(pctx, CFG_LOG_NEAR, "expected '%s'",
1966                                      kw->name);
1967                         result = ISC_R_UNEXPECTEDTOKEN;
1968                         goto cleanup;
1969                 }
1970         }
1971         *ret = obj;
1972  cleanup:
1973         return (result);
1974 }
1975
1976 static isc_result_t
1977 parse_enum_or_other(cfg_parser_t *pctx, const cfg_type_t *enumtype,
1978                     const cfg_type_t *othertype, cfg_obj_t **ret)
1979 {
1980         isc_result_t result;
1981         CHECK(cfg_peektoken(pctx, 0));
1982         if (pctx->token.type == isc_tokentype_string &&
1983             cfg_is_enum(TOKEN_STRING(pctx), enumtype->of)) {
1984                 CHECK(cfg_parse_enum(pctx, enumtype, ret));
1985         } else {
1986                 CHECK(cfg_parse_obj(pctx, othertype, ret));
1987         }
1988  cleanup:
1989         return (result);
1990 }
1991
1992 static void
1993 doc_enum_or_other(cfg_printer_t *pctx, const cfg_type_t *type) {
1994         cfg_doc_terminal(pctx, type);
1995 #if 0 /* XXX */
1996         cfg_print_chars(pctx, "( ", 2);...
1997 #endif
1998
1999 }
2000
2001 static isc_result_t
2002 parse_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2003         return (parse_maybe_optional_keyvalue(pctx, type, ISC_FALSE, ret));
2004 }
2005
2006 static isc_result_t
2007 parse_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2008         return (parse_maybe_optional_keyvalue(pctx, type, ISC_TRUE, ret));
2009 }
2010
2011 static void
2012 print_keyvalue(cfg_printer_t *pctx, const cfg_obj_t *obj) {
2013         const keyword_type_t *kw = obj->type->of;
2014         cfg_print_cstr(pctx, kw->name);
2015         cfg_print_chars(pctx, " ", 1);
2016         kw->type->print(pctx, obj);
2017 }
2018
2019 static void
2020 doc_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type) {
2021         const keyword_type_t *kw = type->of;
2022         cfg_print_cstr(pctx, kw->name);
2023         cfg_print_chars(pctx, " ", 1);
2024         cfg_doc_obj(pctx, kw->type);
2025 }
2026
2027 static void
2028 doc_optional_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type) {
2029         const keyword_type_t *kw = type->of;
2030         cfg_print_chars(pctx, "[ ", 2);
2031         cfg_print_cstr(pctx, kw->name);
2032         cfg_print_chars(pctx, " ", 1);
2033         cfg_doc_obj(pctx, kw->type);
2034         cfg_print_chars(pctx, " ]", 2);
2035 }
2036
2037 static const char *dialup_enums[] = {
2038         "notify", "notify-passive", "refresh", "passive", NULL };
2039 static isc_result_t
2040 parse_dialup_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2041         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
2042 }
2043 static cfg_type_t cfg_type_dialuptype = {
2044         "dialuptype", parse_dialup_type, cfg_print_ustring, doc_enum_or_other,
2045         &cfg_rep_string, dialup_enums
2046 };
2047
2048 static const char *notify_enums[] = { "explicit", "master-only", NULL };
2049 static isc_result_t
2050 parse_notify_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2051         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
2052 }
2053 static cfg_type_t cfg_type_notifytype = {
2054         "notifytype", parse_notify_type, cfg_print_ustring, doc_enum_or_other,
2055         &cfg_rep_string, notify_enums,
2056 };
2057
2058 static const char *ixfrdiff_enums[] = { "master", "slave", NULL };
2059 static isc_result_t
2060 parse_ixfrdiff_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2061         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
2062 }
2063 static cfg_type_t cfg_type_ixfrdifftype = {
2064         "ixfrdiff", parse_ixfrdiff_type, cfg_print_ustring, doc_enum_or_other,
2065         &cfg_rep_string, ixfrdiff_enums,
2066 };
2067
2068 static const char *v4_aaaa_enums[] = { "break-dnssec", NULL };
2069 static isc_result_t
2070 parse_v4_aaaa(cfg_parser_t *pctx, const cfg_type_t *type,
2071                      cfg_obj_t **ret) {
2072         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
2073 }
2074 static cfg_type_t cfg_type_v4_aaaa = {
2075         "v4_aaaa", parse_v4_aaaa, cfg_print_ustring,
2076         doc_enum_or_other, &cfg_rep_string, v4_aaaa_enums,
2077 };
2078
2079 static keyword_type_t key_kw = { "key", &cfg_type_astring };
2080
2081 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_keyref = {
2082         "keyref", parse_keyvalue, print_keyvalue, doc_keyvalue,
2083         &cfg_rep_string, &key_kw
2084 };
2085
2086 static cfg_type_t cfg_type_optional_keyref = {
2087         "optional_keyref", parse_optional_keyvalue, print_keyvalue,
2088         doc_optional_keyvalue, &cfg_rep_string, &key_kw
2089 };
2090
2091 /*%
2092  * A "controls" statement is represented as a map with the multivalued
2093  * "inet" and "unix" clauses.
2094  */
2095
2096 static keyword_type_t controls_allow_kw = {
2097         "allow", &cfg_type_bracketed_aml };
2098
2099 static cfg_type_t cfg_type_controls_allow = {
2100         "controls_allow", parse_keyvalue,
2101         print_keyvalue, doc_keyvalue,
2102         &cfg_rep_list, &controls_allow_kw
2103 };
2104
2105 static keyword_type_t controls_keys_kw = {
2106         "keys", &cfg_type_keylist };
2107
2108 static cfg_type_t cfg_type_controls_keys = {
2109         "controls_keys", parse_optional_keyvalue,
2110         print_keyvalue, doc_optional_keyvalue,
2111         &cfg_rep_list, &controls_keys_kw
2112 };
2113
2114 static cfg_tuplefielddef_t inetcontrol_fields[] = {
2115         { "address", &cfg_type_controls_sockaddr, 0 },
2116         { "allow", &cfg_type_controls_allow, 0 },
2117         { "keys", &cfg_type_controls_keys, 0 },
2118         { NULL, NULL, 0 }
2119 };
2120
2121 static cfg_type_t cfg_type_inetcontrol = {
2122         "inetcontrol", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
2123         inetcontrol_fields
2124 };
2125
2126 static keyword_type_t controls_perm_kw = {
2127         "perm", &cfg_type_uint32 };
2128
2129 static cfg_type_t cfg_type_controls_perm = {
2130         "controls_perm", parse_keyvalue,
2131         print_keyvalue, doc_keyvalue,
2132         &cfg_rep_uint32, &controls_perm_kw
2133 };
2134
2135 static keyword_type_t controls_owner_kw = {
2136         "owner", &cfg_type_uint32 };
2137
2138 static cfg_type_t cfg_type_controls_owner = {
2139         "controls_owner", parse_keyvalue,
2140         print_keyvalue, doc_keyvalue,
2141         &cfg_rep_uint32, &controls_owner_kw
2142 };
2143
2144 static keyword_type_t controls_group_kw = {
2145         "group", &cfg_type_uint32 };
2146
2147 static cfg_type_t cfg_type_controls_group = {
2148         "controls_allow", parse_keyvalue,
2149         print_keyvalue, doc_keyvalue,
2150         &cfg_rep_uint32, &controls_group_kw
2151 };
2152
2153 static cfg_tuplefielddef_t unixcontrol_fields[] = {
2154         { "path", &cfg_type_qstring, 0 },
2155         { "perm", &cfg_type_controls_perm, 0 },
2156         { "owner", &cfg_type_controls_owner, 0 },
2157         { "group", &cfg_type_controls_group, 0 },
2158         { "keys", &cfg_type_controls_keys, 0 },
2159         { NULL, NULL, 0 }
2160 };
2161
2162 static cfg_type_t cfg_type_unixcontrol = {
2163         "unixcontrol", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
2164         unixcontrol_fields
2165 };
2166
2167 static cfg_clausedef_t
2168 controls_clauses[] = {
2169         { "inet", &cfg_type_inetcontrol, CFG_CLAUSEFLAG_MULTI },
2170         { "unix", &cfg_type_unixcontrol, CFG_CLAUSEFLAG_MULTI },
2171         { NULL, NULL, 0 }
2172 };
2173
2174 static cfg_clausedef_t *
2175 controls_clausesets[] = {
2176         controls_clauses,
2177         NULL
2178 };
2179 static cfg_type_t cfg_type_controls = {
2180         "controls", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,    &controls_clausesets
2181 };
2182
2183 /*%
2184  * A "statistics-channels" statement is represented as a map with the
2185  * multivalued "inet" clauses.
2186  */
2187 static void
2188 doc_optional_bracketed_list(cfg_printer_t *pctx, const cfg_type_t *type) {
2189         const keyword_type_t *kw = type->of;
2190         cfg_print_chars(pctx, "[ ", 2);
2191         cfg_print_cstr(pctx, kw->name);
2192         cfg_print_chars(pctx, " ", 1);
2193         cfg_doc_obj(pctx, kw->type);
2194         cfg_print_chars(pctx, " ]", 2);
2195 }
2196
2197 static cfg_type_t cfg_type_optional_allow = {
2198         "optional_allow", parse_optional_keyvalue, print_keyvalue,
2199         doc_optional_bracketed_list, &cfg_rep_list, &controls_allow_kw
2200 };
2201
2202 static cfg_tuplefielddef_t statserver_fields[] = {
2203         { "address", &cfg_type_controls_sockaddr, 0 }, /* reuse controls def */
2204         { "allow", &cfg_type_optional_allow, 0 },
2205         { NULL, NULL, 0 }
2206 };
2207
2208 static cfg_type_t cfg_type_statschannel = {
2209         "statschannel", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2210         &cfg_rep_tuple, statserver_fields
2211 };
2212
2213 static cfg_clausedef_t
2214 statservers_clauses[] = {
2215         { "inet", &cfg_type_statschannel, CFG_CLAUSEFLAG_MULTI },
2216         { NULL, NULL, 0 }
2217 };
2218
2219 static cfg_clausedef_t *
2220 statservers_clausesets[] = {
2221         statservers_clauses,
2222         NULL
2223 };
2224
2225 static cfg_type_t cfg_type_statschannels = {
2226         "statistics-channels", cfg_parse_map, cfg_print_map, cfg_doc_map,
2227         &cfg_rep_map,   &statservers_clausesets
2228 };
2229
2230 /*%
2231  * An optional class, as used in view and zone statements.
2232  */
2233 static isc_result_t
2234 parse_optional_class(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2235         isc_result_t result;
2236         UNUSED(type);
2237         CHECK(cfg_peektoken(pctx, 0));
2238         if (pctx->token.type == isc_tokentype_string)
2239                 CHECK(cfg_parse_obj(pctx, &cfg_type_ustring, ret));
2240         else
2241                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
2242  cleanup:
2243         return (result);
2244 }
2245
2246 static cfg_type_t cfg_type_optional_class = {
2247         "optional_class", parse_optional_class, NULL, cfg_doc_terminal,
2248         NULL, NULL
2249 };
2250
2251 static isc_result_t
2252 parse_querysource(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2253         isc_result_t result;
2254         cfg_obj_t *obj = NULL;
2255         isc_netaddr_t netaddr;
2256         in_port_t port;
2257         unsigned int have_address = 0;
2258         unsigned int have_port = 0;
2259         const unsigned int *flagp = type->of;
2260
2261         if ((*flagp & CFG_ADDR_V4OK) != 0)
2262                 isc_netaddr_any(&netaddr);
2263         else if ((*flagp & CFG_ADDR_V6OK) != 0)
2264                 isc_netaddr_any6(&netaddr);
2265         else
2266                 INSIST(0);
2267
2268         port = 0;
2269
2270         for (;;) {
2271                 CHECK(cfg_peektoken(pctx, 0));
2272                 if (pctx->token.type == isc_tokentype_string) {
2273                         if (strcasecmp(TOKEN_STRING(pctx),
2274                                        "address") == 0)
2275                         {
2276                                 /* read "address" */
2277                                 CHECK(cfg_gettoken(pctx, 0));
2278                                 CHECK(cfg_parse_rawaddr(pctx, *flagp,
2279                                                         &netaddr));
2280                                 have_address++;
2281                         } else if (strcasecmp(TOKEN_STRING(pctx), "port") == 0)
2282                         {
2283                                 /* read "port" */
2284                                 CHECK(cfg_gettoken(pctx, 0));
2285                                 CHECK(cfg_parse_rawport(pctx,
2286                                                         CFG_ADDR_WILDOK,
2287                                                         &port));
2288                                 have_port++;
2289                         } else if (have_port == 0 && have_address == 0) {
2290                                 return (cfg_parse_sockaddr(pctx, type, ret));
2291                         } else {
2292                                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2293                                              "expected 'address' or 'port'");
2294                                 return (ISC_R_UNEXPECTEDTOKEN);
2295                         }
2296                 } else
2297                         break;
2298         }
2299         if (have_address > 1 || have_port > 1 ||
2300             have_address + have_port == 0) {
2301                 cfg_parser_error(pctx, 0, "expected one address and/or port");
2302                 return (ISC_R_UNEXPECTEDTOKEN);
2303         }
2304
2305         CHECK(cfg_create_obj(pctx, &cfg_type_querysource, &obj));
2306         isc_sockaddr_fromnetaddr(&obj->value.sockaddr, &netaddr, port);
2307         *ret = obj;
2308         return (ISC_R_SUCCESS);
2309
2310  cleanup:
2311         cfg_parser_error(pctx, CFG_LOG_NEAR, "invalid query source");
2312         CLEANUP_OBJ(obj);
2313         return (result);
2314 }
2315
2316 static void
2317 print_querysource(cfg_printer_t *pctx, const cfg_obj_t *obj) {
2318         isc_netaddr_t na;
2319         isc_netaddr_fromsockaddr(&na, &obj->value.sockaddr);
2320         cfg_print_cstr(pctx, "address ");
2321         cfg_print_rawaddr(pctx, &na);
2322         cfg_print_cstr(pctx, " port ");
2323         cfg_print_rawuint(pctx, isc_sockaddr_getport(&obj->value.sockaddr));
2324 }
2325
2326 static unsigned int sockaddr4wild_flags = CFG_ADDR_WILDOK | CFG_ADDR_V4OK;
2327 static unsigned int sockaddr6wild_flags = CFG_ADDR_WILDOK | CFG_ADDR_V6OK;
2328
2329 static cfg_type_t cfg_type_querysource4 = {
2330         "querysource4", parse_querysource, NULL, cfg_doc_terminal,
2331         NULL, &sockaddr4wild_flags
2332 };
2333
2334 static cfg_type_t cfg_type_querysource6 = {
2335         "querysource6", parse_querysource, NULL, cfg_doc_terminal,
2336         NULL, &sockaddr6wild_flags
2337 };
2338
2339 static cfg_type_t cfg_type_querysource = {
2340         "querysource", NULL, print_querysource, NULL, &cfg_rep_sockaddr, NULL
2341 };
2342
2343 /*% addrmatchelt */
2344
2345 static isc_result_t
2346 parse_addrmatchelt(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2347         isc_result_t result;
2348         UNUSED(type);
2349
2350         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2351
2352         if (pctx->token.type == isc_tokentype_string ||
2353             pctx->token.type == isc_tokentype_qstring) {
2354                 if (pctx->token.type == isc_tokentype_string &&
2355                     (strcasecmp(TOKEN_STRING(pctx), "key") == 0)) {
2356                         CHECK(cfg_parse_obj(pctx, &cfg_type_keyref, ret));
2357                 } else {
2358                         if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK |
2359                                                   CFG_ADDR_V4PREFIXOK |
2360                                                   CFG_ADDR_V6OK))
2361                         {
2362                                 CHECK(cfg_parse_netprefix(pctx, NULL, ret));
2363                         } else {
2364                                 CHECK(cfg_parse_astring(pctx, NULL, ret));
2365                         }
2366                 }
2367         } else if (pctx->token.type == isc_tokentype_special) {
2368                 if (pctx->token.value.as_char == '{') {
2369                         /* Nested match list. */
2370                         CHECK(cfg_parse_obj(pctx, &cfg_type_bracketed_aml, ret));
2371                 } else if (pctx->token.value.as_char == '!') {
2372                         CHECK(cfg_gettoken(pctx, 0)); /* read "!" */
2373                         CHECK(cfg_parse_obj(pctx, &cfg_type_negated, ret));
2374                 } else {
2375                         goto bad;
2376                 }
2377         } else {
2378         bad:
2379                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2380                              "expected IP match list element");
2381                 return (ISC_R_UNEXPECTEDTOKEN);
2382         }
2383  cleanup:
2384         return (result);
2385 }
2386
2387 /*%
2388  * A negated address match list element (like "! 10.0.0.1").
2389  * Somewhat sneakily, the caller is expected to parse the
2390  * "!", but not to print it.
2391  */
2392
2393 static cfg_tuplefielddef_t negated_fields[] = {
2394         { "value", &cfg_type_addrmatchelt, 0 },
2395         { NULL, NULL, 0 }
2396 };
2397
2398 static void
2399 print_negated(cfg_printer_t *pctx, const cfg_obj_t *obj) {
2400         cfg_print_chars(pctx, "!", 1);
2401         cfg_print_tuple(pctx, obj);
2402 }
2403
2404 static cfg_type_t cfg_type_negated = {
2405         "negated", cfg_parse_tuple, print_negated, NULL, &cfg_rep_tuple,
2406         &negated_fields
2407 };
2408
2409 /*% An address match list element */
2410
2411 static cfg_type_t cfg_type_addrmatchelt = {
2412         "address_match_element", parse_addrmatchelt, NULL, cfg_doc_terminal,
2413         NULL, NULL
2414 };
2415
2416 /*% A bracketed address match list */
2417
2418 static cfg_type_t cfg_type_bracketed_aml = {
2419         "bracketed_aml", cfg_parse_bracketed_list, cfg_print_bracketed_list,
2420         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_addrmatchelt
2421 };
2422
2423 /*%
2424  * The socket address syntax in the "controls" statement is silly.
2425  * It allows both socket address families, but also allows "*",
2426  * whis is gratuitously interpreted as the IPv4 wildcard address.
2427  */
2428 static unsigned int controls_sockaddr_flags =
2429         CFG_ADDR_V4OK | CFG_ADDR_V6OK | CFG_ADDR_WILDOK;
2430 static cfg_type_t cfg_type_controls_sockaddr = {
2431         "controls_sockaddr", cfg_parse_sockaddr, cfg_print_sockaddr,
2432         cfg_doc_sockaddr, &cfg_rep_sockaddr, &controls_sockaddr_flags
2433 };
2434
2435 /*%
2436  * Handle the special kludge syntax of the "keys" clause in the "server"
2437  * statement, which takes a single key with or without braces and semicolon.
2438  */
2439 static isc_result_t
2440 parse_server_key_kludge(cfg_parser_t *pctx, const cfg_type_t *type,
2441                         cfg_obj_t **ret)
2442 {
2443         isc_result_t result;
2444         isc_boolean_t braces = ISC_FALSE;
2445         UNUSED(type);
2446
2447         /* Allow opening brace. */
2448         CHECK(cfg_peektoken(pctx, 0));
2449         if (pctx->token.type == isc_tokentype_special &&
2450             pctx->token.value.as_char == '{') {
2451                 CHECK(cfg_gettoken(pctx, 0));
2452                 braces = ISC_TRUE;
2453         }
2454
2455         CHECK(cfg_parse_obj(pctx, &cfg_type_astring, ret));
2456
2457         if (braces) {
2458                 /* Skip semicolon if present. */
2459                 CHECK(cfg_peektoken(pctx, 0));
2460                 if (pctx->token.type == isc_tokentype_special &&
2461                     pctx->token.value.as_char == ';')
2462                         CHECK(cfg_gettoken(pctx, 0));
2463
2464                 CHECK(cfg_parse_special(pctx, '}'));
2465         }
2466  cleanup:
2467         return (result);
2468 }
2469 static cfg_type_t cfg_type_server_key_kludge = {
2470         "server_key", parse_server_key_kludge, NULL, cfg_doc_terminal,
2471         NULL, NULL
2472 };
2473
2474
2475 /*%
2476  * An optional logging facility.
2477  */
2478
2479 static isc_result_t
2480 parse_optional_facility(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
2481 {
2482         isc_result_t result;
2483         UNUSED(type);
2484
2485         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2486         if (pctx->token.type == isc_tokentype_string ||
2487             pctx->token.type == isc_tokentype_qstring) {
2488                 CHECK(cfg_parse_obj(pctx, &cfg_type_astring, ret));
2489         } else {
2490                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
2491         }
2492  cleanup:
2493         return (result);
2494 }
2495
2496 static cfg_type_t cfg_type_optional_facility = {
2497         "optional_facility", parse_optional_facility, NULL, cfg_doc_terminal,
2498         NULL, NULL };
2499
2500
2501 /*%
2502  * A log severity.  Return as a string, except "debug N",
2503  * which is returned as a keyword object.
2504  */
2505
2506 static keyword_type_t debug_kw = { "debug", &cfg_type_uint32 };
2507 static cfg_type_t cfg_type_debuglevel = {
2508         "debuglevel", parse_keyvalue,
2509         print_keyvalue, doc_keyvalue,
2510         &cfg_rep_uint32, &debug_kw
2511 };
2512
2513 static isc_result_t
2514 parse_logseverity(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2515         isc_result_t result;
2516         UNUSED(type);
2517
2518         CHECK(cfg_peektoken(pctx, 0));
2519         if (pctx->token.type == isc_tokentype_string &&
2520             strcasecmp(TOKEN_STRING(pctx), "debug") == 0) {
2521                 CHECK(cfg_gettoken(pctx, 0)); /* read "debug" */
2522                 CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER));
2523                 if (pctx->token.type == isc_tokentype_number) {
2524                         CHECK(cfg_parse_uint32(pctx, NULL, ret));
2525                 } else {
2526                         /*
2527                          * The debug level is optional and defaults to 1.
2528                          * This makes little sense, but we support it for
2529                          * compatibility with BIND 8.
2530                          */
2531                         CHECK(cfg_create_obj(pctx, &cfg_type_uint32, ret));
2532                         (*ret)->value.uint32 = 1;
2533                 }
2534                 (*ret)->type = &cfg_type_debuglevel; /* XXX kludge */
2535         } else {
2536                 CHECK(cfg_parse_obj(pctx, &cfg_type_loglevel, ret));
2537         }
2538  cleanup:
2539         return (result);
2540 }
2541
2542 static cfg_type_t cfg_type_logseverity = {
2543         "log_severity", parse_logseverity, NULL, cfg_doc_terminal,
2544         NULL, NULL };
2545
2546 /*%
2547  * The "file" clause of the "channel" statement.
2548  * This is yet another special case.
2549  */
2550
2551 static const char *logversions_enums[] = { "unlimited", NULL };
2552 static isc_result_t
2553 parse_logversions(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2554         return (parse_enum_or_other(pctx, type, &cfg_type_uint32, ret));
2555 }
2556
2557 static cfg_type_t cfg_type_logversions = {
2558         "logversions", parse_logversions, cfg_print_ustring, cfg_doc_terminal,
2559         &cfg_rep_string, logversions_enums
2560 };
2561
2562 static cfg_tuplefielddef_t logfile_fields[] = {
2563         { "file", &cfg_type_qstring, 0 },
2564         { "versions", &cfg_type_logversions, 0 },
2565         { "size", &cfg_type_size, 0 },
2566         { NULL, NULL, 0 }
2567 };
2568
2569 static isc_result_t
2570 parse_logfile(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2571         isc_result_t result;
2572         cfg_obj_t *obj = NULL;
2573         const cfg_tuplefielddef_t *fields = type->of;
2574
2575         CHECK(cfg_create_tuple(pctx, type, &obj));
2576
2577         /* Parse the mandatory "file" field */
2578         CHECK(cfg_parse_obj(pctx, fields[0].type, &obj->value.tuple[0]));
2579
2580         /* Parse "versions" and "size" fields in any order. */
2581         for (;;) {
2582                 CHECK(cfg_peektoken(pctx, 0));
2583                 if (pctx->token.type == isc_tokentype_string) {
2584                         CHECK(cfg_gettoken(pctx, 0));
2585                         if (strcasecmp(TOKEN_STRING(pctx),
2586                                        "versions") == 0 &&
2587                             obj->value.tuple[1] == NULL) {
2588                                 CHECK(cfg_parse_obj(pctx, fields[1].type,
2589                                             &obj->value.tuple[1]));
2590                         } else if (strcasecmp(TOKEN_STRING(pctx),
2591                                               "size") == 0 &&
2592                                    obj->value.tuple[2] == NULL) {
2593                                 CHECK(cfg_parse_obj(pctx, fields[2].type,
2594                                             &obj->value.tuple[2]));
2595                         } else {
2596                                 break;
2597                         }
2598                 } else {
2599                         break;
2600                 }
2601         }
2602
2603         /* Create void objects for missing optional values. */
2604         if (obj->value.tuple[1] == NULL)
2605                 CHECK(cfg_parse_void(pctx, NULL, &obj->value.tuple[1]));
2606         if (obj->value.tuple[2] == NULL)
2607                 CHECK(cfg_parse_void(pctx, NULL, &obj->value.tuple[2]));
2608
2609         *ret = obj;
2610         return (ISC_R_SUCCESS);
2611
2612  cleanup:
2613         CLEANUP_OBJ(obj);
2614         return (result);
2615 }
2616
2617 static void
2618 print_logfile(cfg_printer_t *pctx, const cfg_obj_t *obj) {
2619         cfg_print_obj(pctx, obj->value.tuple[0]); /* file */
2620         if (obj->value.tuple[1]->type->print != cfg_print_void) {
2621                 cfg_print_cstr(pctx, " versions ");
2622                 cfg_print_obj(pctx, obj->value.tuple[1]);
2623         }
2624         if (obj->value.tuple[2]->type->print != cfg_print_void) {
2625                 cfg_print_cstr(pctx, " size ");
2626                 cfg_print_obj(pctx, obj->value.tuple[2]);
2627         }
2628 }
2629
2630
2631 static void
2632 doc_logfile(cfg_printer_t *pctx, const cfg_type_t *type) {
2633         UNUSED(type);
2634         cfg_print_cstr(pctx, "<quoted_string>");
2635         cfg_print_chars(pctx, " ", 1);
2636         cfg_print_cstr(pctx, "[ versions ( \"unlimited\" | <integer> ) ]");
2637         cfg_print_chars(pctx, " ", 1);
2638         cfg_print_cstr(pctx, "[ size <size> ]");
2639 }
2640
2641 static cfg_type_t cfg_type_logfile = {
2642         "log_file", parse_logfile, print_logfile, doc_logfile,
2643         &cfg_rep_tuple, logfile_fields
2644 };
2645
2646 /*% An IPv4 address with optional port, "*" accepted as wildcard. */
2647 static cfg_type_t cfg_type_sockaddr4wild = {
2648         "sockaddr4wild", cfg_parse_sockaddr, cfg_print_sockaddr,
2649         cfg_doc_sockaddr, &cfg_rep_sockaddr, &sockaddr4wild_flags
2650 };
2651
2652 /*% An IPv6 address with optional port, "*" accepted as wildcard. */
2653 static cfg_type_t cfg_type_sockaddr6wild = {
2654         "v6addrportwild", cfg_parse_sockaddr, cfg_print_sockaddr,
2655         cfg_doc_sockaddr, &cfg_rep_sockaddr, &sockaddr6wild_flags
2656 };
2657
2658 /*%
2659  * lwres
2660  */
2661
2662 static cfg_tuplefielddef_t lwres_view_fields[] = {
2663         { "name", &cfg_type_astring, 0 },
2664         { "class", &cfg_type_optional_class, 0 },
2665         { NULL, NULL, 0 }
2666 };
2667 static cfg_type_t cfg_type_lwres_view = {
2668         "lwres_view", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
2669         lwres_view_fields
2670 };
2671
2672 static cfg_type_t cfg_type_lwres_searchlist = {
2673         "lwres_searchlist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
2674         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_astring };
2675
2676 static cfg_clausedef_t
2677 lwres_clauses[] = {
2678         { "listen-on", &cfg_type_portiplist, 0 },
2679         { "view", &cfg_type_lwres_view, 0 },
2680         { "search", &cfg_type_lwres_searchlist, 0 },
2681         { "ndots", &cfg_type_uint32, 0 },
2682         { NULL, NULL, 0 }
2683 };
2684
2685 static cfg_clausedef_t *
2686 lwres_clausesets[] = {
2687         lwres_clauses,
2688         NULL
2689 };
2690 static cfg_type_t cfg_type_lwres = {
2691         "lwres", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,
2692         lwres_clausesets
2693 };
2694
2695 /*%
2696  * rndc
2697  */
2698
2699 static cfg_clausedef_t
2700 rndcconf_options_clauses[] = {
2701         { "default-key", &cfg_type_astring, 0 },
2702         { "default-port", &cfg_type_uint32, 0 },
2703         { "default-server", &cfg_type_astring, 0 },
2704         { "default-source-address", &cfg_type_netaddr4wild, 0 },
2705         { "default-source-address-v6", &cfg_type_netaddr6wild, 0 },
2706         { NULL, NULL, 0 }
2707 };
2708
2709 static cfg_clausedef_t *
2710 rndcconf_options_clausesets[] = {
2711         rndcconf_options_clauses,
2712         NULL
2713 };
2714
2715 static cfg_type_t cfg_type_rndcconf_options = {
2716         "rndcconf_options", cfg_parse_map, cfg_print_map, cfg_doc_map,
2717         &cfg_rep_map, rndcconf_options_clausesets
2718 };
2719
2720 static cfg_clausedef_t
2721 rndcconf_server_clauses[] = {
2722         { "key", &cfg_type_astring, 0 },
2723         { "port", &cfg_type_uint32, 0 },
2724         { "source-address", &cfg_type_netaddr4wild, 0 },
2725         { "source-address-v6", &cfg_type_netaddr6wild, 0 },
2726         { "addresses", &cfg_type_bracketed_sockaddrnameportlist, 0 },
2727         { NULL, NULL, 0 }
2728 };
2729
2730 static cfg_clausedef_t *
2731 rndcconf_server_clausesets[] = {
2732         rndcconf_server_clauses,
2733         NULL
2734 };
2735
2736 static cfg_type_t cfg_type_rndcconf_server = {
2737         "rndcconf_server", cfg_parse_named_map, cfg_print_map, cfg_doc_map,
2738         &cfg_rep_map, rndcconf_server_clausesets
2739 };
2740
2741 static cfg_clausedef_t
2742 rndcconf_clauses[] = {
2743         { "key", &cfg_type_key, CFG_CLAUSEFLAG_MULTI },
2744         { "server", &cfg_type_rndcconf_server, CFG_CLAUSEFLAG_MULTI },
2745         { "options", &cfg_type_rndcconf_options, 0 },
2746         { NULL, NULL, 0 }
2747 };
2748
2749 static cfg_clausedef_t *
2750 rndcconf_clausesets[] = {
2751         rndcconf_clauses,
2752         NULL
2753 };
2754
2755 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_rndcconf = {
2756         "rndcconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2757         &cfg_rep_map, rndcconf_clausesets
2758 };
2759
2760 static cfg_clausedef_t
2761 rndckey_clauses[] = {
2762         { "key", &cfg_type_key, 0 },
2763         { NULL, NULL, 0 }
2764 };
2765
2766 static cfg_clausedef_t *
2767 rndckey_clausesets[] = {
2768         rndckey_clauses,
2769         NULL
2770 };
2771
2772 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_rndckey = {
2773         "rndckey", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2774         &cfg_rep_map, rndckey_clausesets
2775 };
2776
2777 /*
2778  * session.key has exactly the same syntax as rndc.key, but it's defined
2779  * separately for clarity (and so we can extend it someday, if needed).
2780  */
2781 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_sessionkey = {
2782         "sessionkey", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2783         &cfg_rep_map, rndckey_clausesets
2784 };
2785
2786 static cfg_tuplefielddef_t nameport_fields[] = {
2787         { "name", &cfg_type_astring, 0 },
2788         { "port", &cfg_type_optional_port, 0 },
2789         { NULL, NULL, 0 }
2790 };
2791 static cfg_type_t cfg_type_nameport = {
2792         "nameport", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2793         &cfg_rep_tuple, nameport_fields
2794 };
2795
2796 static void
2797 doc_sockaddrnameport(cfg_printer_t *pctx, const cfg_type_t *type) {
2798         UNUSED(type);
2799         cfg_print_chars(pctx, "( ", 2);
2800         cfg_print_cstr(pctx, "<quoted_string>");
2801         cfg_print_chars(pctx, " ", 1);
2802         cfg_print_cstr(pctx, "[ port <integer> ]");
2803         cfg_print_chars(pctx, " | ", 3);
2804         cfg_print_cstr(pctx, "<ipv4_address>");
2805         cfg_print_chars(pctx, " ", 1);
2806         cfg_print_cstr(pctx, "[ port <integer> ]");
2807         cfg_print_chars(pctx, " | ", 3);
2808         cfg_print_cstr(pctx, "<ipv6_address>");
2809         cfg_print_chars(pctx, " ", 1);
2810         cfg_print_cstr(pctx, "[ port <integer> ]");
2811         cfg_print_chars(pctx, " )", 2);
2812 }
2813
2814 static isc_result_t
2815 parse_sockaddrnameport(cfg_parser_t *pctx, const cfg_type_t *type,
2816                        cfg_obj_t **ret)
2817 {
2818         isc_result_t result;
2819         cfg_obj_t *obj = NULL;
2820         UNUSED(type);
2821
2822         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2823         if (pctx->token.type == isc_tokentype_string ||
2824             pctx->token.type == isc_tokentype_qstring) {
2825                 if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK | CFG_ADDR_V6OK))
2826                         CHECK(cfg_parse_sockaddr(pctx, &cfg_type_sockaddr, ret));
2827                 else {
2828                         const cfg_tuplefielddef_t *fields =
2829                                                    cfg_type_nameport.of;
2830                         CHECK(cfg_create_tuple(pctx, &cfg_type_nameport,
2831                                                &obj));
2832                         CHECK(cfg_parse_obj(pctx, fields[0].type,
2833                                             &obj->value.tuple[0]));
2834                         CHECK(cfg_parse_obj(pctx, fields[1].type,
2835                                             &obj->value.tuple[1]));
2836                         *ret = obj;
2837                         obj = NULL;
2838                 }
2839         } else {
2840                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2841                              "expected IP address or hostname");
2842                 return (ISC_R_UNEXPECTEDTOKEN);
2843         }
2844  cleanup:
2845         CLEANUP_OBJ(obj);
2846         return (result);
2847 }
2848
2849 static cfg_type_t cfg_type_sockaddrnameport = {
2850         "sockaddrnameport_element", parse_sockaddrnameport, NULL,
2851          doc_sockaddrnameport, NULL, NULL
2852 };
2853
2854 static cfg_type_t cfg_type_bracketed_sockaddrnameportlist = {
2855         "bracketed_sockaddrnameportlist", cfg_parse_bracketed_list,
2856         cfg_print_bracketed_list, cfg_doc_bracketed_list,
2857         &cfg_rep_list, &cfg_type_sockaddrnameport
2858 };
2859
2860 /*%
2861  * A list of socket addresses or name with an optional default port,
2862  * as used in the dual-stack-servers option.  E.g.,
2863  * "port 1234 { dual-stack-servers.net; 10.0.0.1; 1::2 port 69; }"
2864  */
2865 static cfg_tuplefielddef_t nameportiplist_fields[] = {
2866         { "port", &cfg_type_optional_port, 0 },
2867         { "addresses", &cfg_type_bracketed_sockaddrnameportlist, 0 },
2868         { NULL, NULL, 0 }
2869 };
2870
2871 static cfg_type_t cfg_type_nameportiplist = {
2872         "nameportiplist", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2873         &cfg_rep_tuple, nameportiplist_fields
2874 };
2875
2876 /*%
2877  * masters element.
2878  */
2879
2880 static void
2881 doc_masterselement(cfg_printer_t *pctx, const cfg_type_t *type) {
2882         UNUSED(type);
2883         cfg_print_chars(pctx, "( ", 2);
2884         cfg_print_cstr(pctx, "<masters>");
2885         cfg_print_chars(pctx, " | ", 3);
2886         cfg_print_cstr(pctx, "<ipv4_address>");
2887         cfg_print_chars(pctx, " ", 1);
2888         cfg_print_cstr(pctx, "[ port <integer> ]");
2889         cfg_print_chars(pctx, " | ", 3);
2890         cfg_print_cstr(pctx, "<ipv6_address>");
2891         cfg_print_chars(pctx, " ", 1);
2892         cfg_print_cstr(pctx, "[ port <integer> ]");
2893         cfg_print_chars(pctx, " )", 2);
2894 }
2895
2896 static isc_result_t
2897 parse_masterselement(cfg_parser_t *pctx, const cfg_type_t *type,
2898                      cfg_obj_t **ret)
2899 {
2900         isc_result_t result;
2901         cfg_obj_t *obj = NULL;
2902         UNUSED(type);
2903
2904         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2905         if (pctx->token.type == isc_tokentype_string ||
2906             pctx->token.type == isc_tokentype_qstring) {
2907                 if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK | CFG_ADDR_V6OK))
2908                         CHECK(cfg_parse_sockaddr(pctx, &cfg_type_sockaddr, ret));
2909                 else
2910                         CHECK(cfg_parse_astring(pctx, &cfg_type_astring, ret));
2911         } else {
2912                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2913                              "expected IP address or masters name");
2914                 return (ISC_R_UNEXPECTEDTOKEN);
2915         }
2916  cleanup:
2917         CLEANUP_OBJ(obj);
2918         return (result);
2919 }
2920
2921 static cfg_type_t cfg_type_masterselement = {
2922         "masters_element", parse_masterselement, NULL,
2923          doc_masterselement, NULL, NULL
2924 };