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