gsstest: fixed compilation errors
[tridge/bind9.git] / lib / isccfg / namedconf.c
1 /*
2  * Copyright (C) 2004-2010  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.126 2010/12/18 01:56:23 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", 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", "create",
546                                           "off", NULL };
547 static cfg_type_t cfg_type_autodnssec = {
548         "autodnssec", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
549         &cfg_rep_string, &autodnssec_enums
550 };
551
552 static cfg_type_t cfg_type_rrsetorder = {
553         "rrsetorder", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
554         &cfg_rep_list, &cfg_type_rrsetorderingelement
555 };
556
557 static keyword_type_t port_kw = { "port", &cfg_type_uint32 };
558
559 static cfg_type_t cfg_type_optional_port = {
560         "optional_port", parse_optional_keyvalue, print_keyvalue,
561         doc_optional_keyvalue, &cfg_rep_uint32, &port_kw
562 };
563
564 /*% A list of keys, as in the "key" clause of the controls statement. */
565 static cfg_type_t cfg_type_keylist = {
566         "keylist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
567         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_astring
568 };
569
570 /*% A list of dnssec keys, as in "trusted-keys" */
571 static cfg_type_t cfg_type_dnsseckeys = {
572         "dnsseckeys", cfg_parse_bracketed_list, cfg_print_bracketed_list,
573         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_dnsseckey
574 };
575
576 /*%
577  * A list of managed key entries, as in "trusted-keys".  Currently
578  * (9.7.0) this has a format similar to dnssec keys, except the keyname
579  * is followed by the keyword "initial-key".  In future releases, this
580  * keyword may take other values indicating different methods for the
581  * key to be initialized.
582  */
583
584 static cfg_type_t cfg_type_managedkeys = {
585         "managedkeys", cfg_parse_bracketed_list, cfg_print_bracketed_list,
586         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_managedkey
587 };
588
589 static const char *forwardtype_enums[] = { "first", "only", NULL };
590 static cfg_type_t cfg_type_forwardtype = {
591         "forwardtype", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
592         &forwardtype_enums
593 };
594
595 static const char *zonetype_enums[] = {
596         "master", "slave", "stub", "static-stub", "hint", "forward",
597         "delegation-only", NULL };
598 static cfg_type_t cfg_type_zonetype = {
599         "zonetype", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
600         &cfg_rep_string, &zonetype_enums
601 };
602
603 static const char *loglevel_enums[] = {
604         "critical", "error", "warning", "notice", "info", "dynamic", NULL };
605 static cfg_type_t cfg_type_loglevel = {
606         "loglevel", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
607         &loglevel_enums
608 };
609
610 static const char *transferformat_enums[] = {
611         "many-answers", "one-answer", NULL };
612 static cfg_type_t cfg_type_transferformat = {
613         "transferformat", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
614         &transferformat_enums
615 };
616
617 /*%
618  * The special keyword "none", as used in the pid-file option.
619  */
620
621 static void
622 print_none(cfg_printer_t *pctx, const cfg_obj_t *obj) {
623         UNUSED(obj);
624         cfg_print_cstr(pctx, "none");
625 }
626
627 static cfg_type_t cfg_type_none = {
628         "none", NULL, print_none, NULL, &cfg_rep_void, NULL
629 };
630
631 /*%
632  * A quoted string or the special keyword "none".  Used in the pid-file option.
633  */
634 static isc_result_t
635 parse_qstringornone(cfg_parser_t *pctx, const cfg_type_t *type,
636                     cfg_obj_t **ret)
637 {
638         isc_result_t result;
639
640         CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
641         if (pctx->token.type == isc_tokentype_string &&
642             strcasecmp(TOKEN_STRING(pctx), "none") == 0)
643                 return (cfg_create_obj(pctx, &cfg_type_none, ret));
644         cfg_ungettoken(pctx);
645         return (cfg_parse_qstring(pctx, type, ret));
646  cleanup:
647         return (result);
648 }
649
650 static void
651 doc_qstringornone(cfg_printer_t *pctx, const cfg_type_t *type) {
652         UNUSED(type);
653         cfg_print_cstr(pctx, "( <quoted_string> | none )");
654 }
655
656 static cfg_type_t cfg_type_qstringornone = {
657         "qstringornone", parse_qstringornone, NULL, doc_qstringornone,
658         NULL, NULL
659 };
660
661 /*%
662  * keyword hostname
663  */
664
665 static void
666 print_hostname(cfg_printer_t *pctx, const cfg_obj_t *obj) {
667         UNUSED(obj);
668         cfg_print_cstr(pctx, "hostname");
669 }
670
671 static cfg_type_t cfg_type_hostname = {
672         "hostname", NULL, print_hostname, NULL, &cfg_rep_boolean, NULL
673 };
674
675 /*%
676  * "server-id" argument.
677  */
678
679 static isc_result_t
680 parse_serverid(cfg_parser_t *pctx, const cfg_type_t *type,
681                     cfg_obj_t **ret)
682 {
683         isc_result_t result;
684         CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
685         if (pctx->token.type == isc_tokentype_string &&
686             strcasecmp(TOKEN_STRING(pctx), "none") == 0)
687                 return (cfg_create_obj(pctx, &cfg_type_none, ret));
688         if (pctx->token.type == isc_tokentype_string &&
689             strcasecmp(TOKEN_STRING(pctx), "hostname") == 0) {
690                 return (cfg_create_obj(pctx, &cfg_type_hostname, ret));
691         }
692         cfg_ungettoken(pctx);
693         return (cfg_parse_qstring(pctx, type, ret));
694  cleanup:
695         return (result);
696 }
697
698 static void
699 doc_serverid(cfg_printer_t *pctx, const cfg_type_t *type) {
700         UNUSED(type);
701         cfg_print_cstr(pctx, "( <quoted_string> | none | hostname )");
702 }
703
704 static cfg_type_t cfg_type_serverid = {
705         "serverid", parse_serverid, NULL, doc_serverid, NULL, NULL };
706
707 /*%
708  * Port list.
709  */
710 static cfg_tuplefielddef_t porttuple_fields[] = {
711         { "loport", &cfg_type_uint32, 0 },
712         { "hiport", &cfg_type_uint32, 0 },
713         { NULL, NULL, 0 }
714 };
715 static cfg_type_t cfg_type_porttuple = {
716         "porttuple", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
717         &cfg_rep_tuple, porttuple_fields
718 };
719
720 static isc_result_t
721 parse_port(cfg_parser_t *pctx, cfg_obj_t **ret) {
722         isc_result_t result;
723
724         CHECK(cfg_parse_uint32(pctx, NULL, ret));
725         if ((*ret)->value.uint32 > 0xffff) {
726                 cfg_parser_error(pctx, CFG_LOG_NEAR, "invalid port");
727                 cfg_obj_destroy(pctx, ret);
728                 result = ISC_R_RANGE;
729         }
730
731  cleanup:
732         return (result);
733 }
734
735 static isc_result_t
736 parse_portrange(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
737         isc_result_t result;
738         cfg_obj_t *obj = NULL;
739
740         UNUSED(type);
741
742         CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER | ISC_LEXOPT_CNUMBER));
743         if (pctx->token.type == isc_tokentype_number)
744                 CHECK(parse_port(pctx, ret));
745         else {
746                 CHECK(cfg_gettoken(pctx, 0));
747                 if (pctx->token.type != isc_tokentype_string ||
748                     strcasecmp(TOKEN_STRING(pctx), "range") != 0) {
749                         cfg_parser_error(pctx, CFG_LOG_NEAR,
750                                          "expected integer or 'range'");
751                         return (ISC_R_UNEXPECTEDTOKEN);
752                 }
753                 CHECK(cfg_create_tuple(pctx, &cfg_type_porttuple, &obj));
754                 CHECK(parse_port(pctx, &obj->value.tuple[0]));
755                 CHECK(parse_port(pctx, &obj->value.tuple[1]));
756                 if (obj->value.tuple[0]->value.uint32 >
757                     obj->value.tuple[1]->value.uint32) {
758                         cfg_parser_error(pctx, CFG_LOG_NOPREP,
759                                          "low port '%u' must not be larger "
760                                          "than high port",
761                                          obj->value.tuple[0]->value.uint32);
762                         result = ISC_R_RANGE;
763                         goto cleanup;
764                 }
765                 *ret = obj;
766                 obj = NULL;
767         }
768
769  cleanup:
770         if (obj != NULL)
771                 cfg_obj_destroy(pctx, &obj);
772         return (result);
773 }
774
775 static cfg_type_t cfg_type_portrange = {
776         "portrange", parse_portrange, NULL, cfg_doc_terminal,
777         NULL, NULL
778 };
779
780 static cfg_type_t cfg_type_bracketed_portlist = {
781         "bracketed_sockaddrlist", cfg_parse_bracketed_list,
782         cfg_print_bracketed_list, cfg_doc_bracketed_list,
783         &cfg_rep_list, &cfg_type_portrange
784 };
785
786 /*%
787  * Clauses that can be found within the top level of the named.conf
788  * file only.
789  */
790 static cfg_clausedef_t
791 namedconf_clauses[] = {
792         { "options", &cfg_type_options, 0 },
793         { "controls", &cfg_type_controls, CFG_CLAUSEFLAG_MULTI },
794         { "acl", &cfg_type_acl, CFG_CLAUSEFLAG_MULTI },
795         { "masters", &cfg_type_masters, CFG_CLAUSEFLAG_MULTI },
796         { "logging", &cfg_type_logging, 0 },
797         { "view", &cfg_type_view, CFG_CLAUSEFLAG_MULTI },
798         { "lwres", &cfg_type_lwres, CFG_CLAUSEFLAG_MULTI },
799         { "statistics-channels", &cfg_type_statschannels,
800           CFG_CLAUSEFLAG_MULTI },
801         { NULL, NULL, 0 }
802 };
803
804 /*%
805  * Clauses that can occur at the top level or in the view
806  * statement, but not in the options block.
807  */
808 static cfg_clausedef_t
809 namedconf_or_view_clauses[] = {
810         { "key", &cfg_type_key, CFG_CLAUSEFLAG_MULTI },
811         { "zone", &cfg_type_zone, CFG_CLAUSEFLAG_MULTI },
812         /* only 1 DLZ per view allowed */
813         { "dlz", &cfg_type_dynamically_loadable_zones, 0 },
814         { "server", &cfg_type_server, CFG_CLAUSEFLAG_MULTI },
815         { "trusted-keys", &cfg_type_dnsseckeys, CFG_CLAUSEFLAG_MULTI },
816         { "managed-keys", &cfg_type_managedkeys, CFG_CLAUSEFLAG_MULTI },
817         { NULL, NULL, 0 }
818 };
819
820 /*%
821  * Clauses that can occur in the bind.keys file.
822  */
823 static cfg_clausedef_t
824 bindkeys_clauses[] = {
825         { "trusted-keys", &cfg_type_dnsseckeys, CFG_CLAUSEFLAG_MULTI },
826         { "managed-keys", &cfg_type_managedkeys, CFG_CLAUSEFLAG_MULTI },
827         { NULL, NULL, 0 }
828 };
829
830 /*%
831  * Clauses that can be found within the 'options' statement.
832  */
833 static cfg_clausedef_t
834 options_clauses[] = {
835         { "avoid-v4-udp-ports", &cfg_type_bracketed_portlist, 0 },
836         { "avoid-v6-udp-ports", &cfg_type_bracketed_portlist, 0 },
837         { "bindkeys-file", &cfg_type_qstring, 0 },
838         { "blackhole", &cfg_type_bracketed_aml, 0 },
839         { "coresize", &cfg_type_size, 0 },
840         { "datasize", &cfg_type_size, 0 },
841         { "session-keyfile", &cfg_type_qstringornone, 0 },
842         { "session-keyname", &cfg_type_astring, 0 },
843         { "session-keyalg", &cfg_type_astring, 0 },
844         { "deallocate-on-exit", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
845         { "directory", &cfg_type_qstring, CFG_CLAUSEFLAG_CALLBACK },
846         { "dump-file", &cfg_type_qstring, 0 },
847         { "fake-iquery", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
848         { "files", &cfg_type_size, 0 },
849         { "flush-zones-on-shutdown", &cfg_type_boolean, 0 },
850         { "has-old-clients", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
851         { "heartbeat-interval", &cfg_type_uint32, 0 },
852         { "host-statistics", &cfg_type_boolean, CFG_CLAUSEFLAG_NOTIMP },
853         { "host-statistics-max", &cfg_type_uint32, CFG_CLAUSEFLAG_NOTIMP },
854         { "hostname", &cfg_type_qstringornone, 0 },
855         { "interface-interval", &cfg_type_uint32, 0 },
856         { "listen-on", &cfg_type_listenon, CFG_CLAUSEFLAG_MULTI },
857         { "listen-on-v6", &cfg_type_listenon, CFG_CLAUSEFLAG_MULTI },
858         { "managed-keys-directory", &cfg_type_qstring, 0 },
859         { "match-mapped-addresses", &cfg_type_boolean, 0 },
860         { "memstatistics-file", &cfg_type_qstring, 0 },
861         { "memstatistics", &cfg_type_boolean, 0 },
862         { "multiple-cnames", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
863         { "named-xfer", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
864         { "pid-file", &cfg_type_qstringornone, 0 },
865         { "port", &cfg_type_uint32, 0 },
866         { "querylog", &cfg_type_boolean, 0 },
867         { "recursing-file", &cfg_type_qstring, 0 },
868         { "random-device", &cfg_type_qstring, 0 },
869         { "recursive-clients", &cfg_type_uint32, 0 },
870         { "reserved-sockets", &cfg_type_uint32, 0 },
871         { "secroots-file", &cfg_type_qstring, 0 },
872         { "serial-queries", &cfg_type_uint32, CFG_CLAUSEFLAG_OBSOLETE },
873         { "serial-query-rate", &cfg_type_uint32, 0 },
874         { "server-id", &cfg_type_serverid, 0 },
875         { "stacksize", &cfg_type_size, 0 },
876         { "statistics-file", &cfg_type_qstring, 0 },
877         { "statistics-interval", &cfg_type_uint32, CFG_CLAUSEFLAG_NYI },
878         { "tcp-clients", &cfg_type_uint32, 0 },
879         { "tcp-listen-queue", &cfg_type_uint32, 0 },
880         { "tkey-dhkey", &cfg_type_tkey_dhkey, 0 },
881         { "tkey-gssapi-credential", &cfg_type_qstring, 0 },
882         { "tkey-gssapi-keytab", &cfg_type_qstring, 0 },
883         { "tkey-domain", &cfg_type_qstring, 0 },
884         { "transfers-per-ns", &cfg_type_uint32, 0 },
885         { "transfers-in", &cfg_type_uint32, 0 },
886         { "transfers-out", &cfg_type_uint32, 0 },
887         { "treat-cr-as-space", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
888         { "use-id-pool", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
889         { "use-ixfr", &cfg_type_boolean, 0 },
890         { "use-v4-udp-ports", &cfg_type_bracketed_portlist, 0 },
891         { "use-v6-udp-ports", &cfg_type_bracketed_portlist, 0 },
892         { "version", &cfg_type_qstringornone, 0 },
893         { NULL, NULL, 0 }
894 };
895
896
897 static cfg_type_t cfg_type_namelist = {
898         "namelist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
899         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_qstring };
900
901 static keyword_type_t exclude_kw = { "exclude", &cfg_type_namelist };
902
903 static cfg_type_t cfg_type_optional_exclude = {
904         "optional_exclude", parse_optional_keyvalue, print_keyvalue,
905         doc_optional_keyvalue, &cfg_rep_list, &exclude_kw };
906
907 static keyword_type_t exceptionnames_kw = { "except-from", &cfg_type_namelist };
908
909 static cfg_type_t cfg_type_optional_exceptionnames = {
910         "optional_allow", parse_optional_keyvalue, print_keyvalue,
911         doc_optional_keyvalue, &cfg_rep_list, &exceptionnames_kw };
912
913 static cfg_tuplefielddef_t denyaddresses_fields[] = {
914         { "acl", &cfg_type_bracketed_aml, 0 },
915         { "except-from", &cfg_type_optional_exceptionnames, 0 },
916         { NULL, NULL, 0 }
917 };
918
919 static cfg_type_t cfg_type_denyaddresses = {
920         "denyaddresses", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
921         &cfg_rep_tuple, denyaddresses_fields
922 };
923
924 static cfg_tuplefielddef_t denyaliases_fields[] = {
925         { "name", &cfg_type_namelist, 0 },
926         { "except-from", &cfg_type_optional_exceptionnames, 0 },
927         { NULL, NULL, 0 }
928 };
929
930 static cfg_type_t cfg_type_denyaliases = {
931         "denyaliases", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
932         &cfg_rep_tuple, denyaliases_fields
933 };
934
935 static cfg_type_t cfg_type_algorithmlist = {
936         "algorithmlist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
937         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_astring };
938
939 static cfg_tuplefielddef_t disablealgorithm_fields[] = {
940         { "name", &cfg_type_astring, 0 },
941         { "algorithms", &cfg_type_algorithmlist, 0 },
942         { NULL, NULL, 0 }
943 };
944
945 static cfg_type_t cfg_type_disablealgorithm = {
946         "disablealgorithm", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
947         &cfg_rep_tuple, disablealgorithm_fields
948 };
949
950 static cfg_tuplefielddef_t mustbesecure_fields[] = {
951         { "name", &cfg_type_astring, 0 },
952         { "value", &cfg_type_boolean, 0 },
953         { NULL, NULL, 0 }
954 };
955
956 static cfg_type_t cfg_type_mustbesecure = {
957         "mustbesecure", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
958         &cfg_rep_tuple, mustbesecure_fields
959 };
960
961 static const char *masterformat_enums[] = { "text", "raw", NULL };
962 static cfg_type_t cfg_type_masterformat = {
963         "masterformat", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
964         &cfg_rep_string, &masterformat_enums
965 };
966
967 /*%
968  * dnssec-lookaside
969  */
970
971 static keyword_type_t trustanchor_kw = { "trust-anchor", &cfg_type_astring };
972
973 static cfg_type_t cfg_type_optional_trustanchor = {
974         "optional_trustanchor", parse_optional_keyvalue, print_keyvalue,
975         doc_keyvalue, &cfg_rep_string, &trustanchor_kw
976 };
977
978 static cfg_tuplefielddef_t lookaside_fields[] = {
979         { "domain", &cfg_type_astring, 0 },
980         { "trust-anchor", &cfg_type_optional_trustanchor, 0 },
981         { NULL, NULL, 0 }
982 };
983
984 static cfg_type_t cfg_type_lookaside = {
985         "lookaside", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
986         &cfg_rep_tuple, lookaside_fields
987 };
988
989 /*
990  * DNS64.
991  */
992 static cfg_clausedef_t
993 dns64_clauses[] = {
994         { "clients", &cfg_type_bracketed_aml, 0 },
995         { "mapped", &cfg_type_bracketed_aml, 0 },
996         { "exclude", &cfg_type_bracketed_aml, 0 },
997         { "suffix", &cfg_type_netaddr6, 0 },
998         { "recursive-only", &cfg_type_boolean, 0 },
999         { "break-dnssec", &cfg_type_boolean, 0 },
1000         { NULL, NULL, 0 },
1001 };
1002
1003 static cfg_clausedef_t *
1004 dns64_clausesets[] = {
1005         dns64_clauses,
1006         NULL
1007 };
1008
1009 static cfg_type_t cfg_type_dns64 = {
1010         "dns64", cfg_parse_netprefix_map, cfg_print_map, cfg_doc_map,
1011         &cfg_rep_map, dns64_clausesets
1012 };
1013
1014 /*%
1015  * Clauses that can be found within the 'view' statement,
1016  * with defaults in the 'options' statement.
1017  */
1018
1019 static cfg_clausedef_t
1020 view_clauses[] = {
1021         { "acache-cleaning-interval", &cfg_type_uint32, 0 },
1022         { "acache-enable", &cfg_type_boolean, 0 },
1023         { "additional-from-auth", &cfg_type_boolean, 0 },
1024         { "additional-from-cache", &cfg_type_boolean, 0 },
1025         { "allow-new-zones", &cfg_type_boolean, 0 },
1026         { "allow-query-cache", &cfg_type_bracketed_aml, 0 },
1027         { "allow-query-cache-on", &cfg_type_bracketed_aml, 0 },
1028         { "allow-recursion", &cfg_type_bracketed_aml, 0 },
1029         { "allow-recursion-on", &cfg_type_bracketed_aml, 0 },
1030         { "allow-v6-synthesis", &cfg_type_bracketed_aml,
1031           CFG_CLAUSEFLAG_OBSOLETE },
1032         { "attach-cache", &cfg_type_astring, 0 },
1033         { "auth-nxdomain", &cfg_type_boolean, CFG_CLAUSEFLAG_NEWDEFAULT },
1034         { "cache-file", &cfg_type_qstring, 0 },
1035         { "check-names", &cfg_type_checknames, CFG_CLAUSEFLAG_MULTI },
1036         { "cleaning-interval", &cfg_type_uint32, 0 },
1037         { "clients-per-query", &cfg_type_uint32, 0 },
1038         { "deny-answer-addresses", &cfg_type_denyaddresses, 0 },
1039         { "deny-answer-aliases", &cfg_type_denyaliases, 0 },
1040         { "disable-algorithms", &cfg_type_disablealgorithm,
1041           CFG_CLAUSEFLAG_MULTI },
1042         { "disable-empty-zone", &cfg_type_astring, CFG_CLAUSEFLAG_MULTI },
1043         { "dns64", &cfg_type_dns64, CFG_CLAUSEFLAG_MULTI },
1044         { "dnssec-accept-expired", &cfg_type_boolean, 0 },
1045         { "dnssec-enable", &cfg_type_boolean, 0 },
1046         { "dnssec-lookaside", &cfg_type_lookaside, CFG_CLAUSEFLAG_MULTI },
1047         { "dnssec-must-be-secure",  &cfg_type_mustbesecure,
1048           CFG_CLAUSEFLAG_MULTI },
1049         { "dnssec-validation", &cfg_type_boolean, 0 },
1050         { "dual-stack-servers", &cfg_type_nameportiplist, 0 },
1051         { "edns-udp-size", &cfg_type_uint32, 0 },
1052         { "empty-contact", &cfg_type_astring, 0 },
1053         { "empty-server", &cfg_type_astring, 0 },
1054         { "empty-zones-enable", &cfg_type_boolean, 0 },
1055         { "fetch-glue", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1056         { "ixfr-from-differences", &cfg_type_ixfrdifftype, 0 },
1057         { "lame-ttl", &cfg_type_uint32, 0 },
1058         { "max-acache-size", &cfg_type_sizenodefault, 0 },
1059         { "max-cache-size", &cfg_type_sizenodefault, 0 },
1060         { "max-cache-ttl", &cfg_type_uint32, 0 },
1061         { "max-clients-per-query", &cfg_type_uint32, 0 },
1062         { "max-ncache-ttl", &cfg_type_uint32, 0 },
1063         { "max-udp-size", &cfg_type_uint32, 0 },
1064         { "min-roots", &cfg_type_uint32, CFG_CLAUSEFLAG_NOTIMP },
1065         { "minimal-responses", &cfg_type_boolean, 0 },
1066         { "preferred-glue", &cfg_type_astring, 0 },
1067         { "provide-ixfr", &cfg_type_boolean, 0 },
1068         /*
1069          * Note that the query-source option syntax is different
1070          * from the other -source options.
1071          */
1072         { "query-source", &cfg_type_querysource4, 0 },
1073         { "query-source-v6", &cfg_type_querysource6, 0 },
1074         { "queryport-pool-ports", &cfg_type_uint32, CFG_CLAUSEFLAG_OBSOLETE },
1075         { "queryport-pool-updateinterval", &cfg_type_uint32,
1076           CFG_CLAUSEFLAG_OBSOLETE },
1077         { "recursion", &cfg_type_boolean, 0 },
1078         { "request-ixfr", &cfg_type_boolean, 0 },
1079         { "request-nsid", &cfg_type_boolean, 0 },
1080         { "rfc2308-type1", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
1081         { "root-delegation-only",  &cfg_type_optional_exclude, 0 },
1082         { "rrset-order", &cfg_type_rrsetorder, 0 },
1083         { "sortlist", &cfg_type_bracketed_aml, 0 },
1084         { "suppress-initial-notify", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
1085         { "topology", &cfg_type_bracketed_aml, CFG_CLAUSEFLAG_NOTIMP },
1086         { "transfer-format", &cfg_type_transferformat, 0 },
1087         { "use-queryport-pool", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1088         { "zero-no-soa-ttl-cache", &cfg_type_boolean, 0 },
1089 #ifdef ALLOW_FILTER_AAAA_ON_V4
1090         { "filter-aaaa", &cfg_type_bracketed_aml, 0 },
1091         { "filter-aaaa-on-v4", &cfg_type_v4_aaaa, 0 },
1092 #else
1093         { "filter-aaaa", &cfg_type_bracketed_aml,
1094            CFG_CLAUSEFLAG_NOTCONFIGURED },
1095         { "filter-aaaa-on-v4", &cfg_type_v4_aaaa,
1096            CFG_CLAUSEFLAG_NOTCONFIGURED },
1097 #endif
1098         { NULL, NULL, 0 }
1099 };
1100
1101 /*%
1102  * Clauses that can be found within the 'view' statement only.
1103  */
1104 static cfg_clausedef_t
1105 view_only_clauses[] = {
1106         { "match-clients", &cfg_type_bracketed_aml, 0 },
1107         { "match-destinations", &cfg_type_bracketed_aml, 0 },
1108         { "match-recursive-only", &cfg_type_boolean, 0 },
1109         { NULL, NULL, 0 }
1110 };
1111
1112 /*%
1113  * Sig-validity-interval.
1114  */
1115 static isc_result_t
1116 parse_optional_uint32(cfg_parser_t *pctx, const cfg_type_t *type,
1117                       cfg_obj_t **ret)
1118 {
1119         isc_result_t result;
1120         UNUSED(type);
1121
1122         CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER | ISC_LEXOPT_CNUMBER));
1123         if (pctx->token.type == isc_tokentype_number) {
1124                 CHECK(cfg_parse_obj(pctx, &cfg_type_uint32, ret));
1125         } else {
1126                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
1127         }
1128  cleanup:
1129         return (result);
1130 }
1131
1132 static void
1133 doc_optional_uint32(cfg_printer_t *pctx, const cfg_type_t *type) {
1134         UNUSED(type);
1135         cfg_print_cstr(pctx, "[ <integer> ]");
1136 }
1137
1138 static cfg_type_t cfg_type_optional_uint32 = {
1139         "optional_uint32", parse_optional_uint32, NULL, doc_optional_uint32,
1140         NULL, NULL };
1141
1142 static cfg_tuplefielddef_t validityinterval_fields[] = {
1143         { "validity", &cfg_type_uint32, 0 },
1144         { "re-sign", &cfg_type_optional_uint32, 0 },
1145         { NULL, NULL, 0 }
1146 };
1147
1148 static cfg_type_t cfg_type_validityinterval = {
1149         "validityinterval", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
1150         &cfg_rep_tuple, validityinterval_fields
1151 };
1152
1153 /*%
1154  * Clauses that can be found in a 'zone' statement,
1155  * with defaults in the 'view' or 'options' statement.
1156  */
1157 static cfg_clausedef_t
1158 zone_clauses[] = {
1159         { "allow-notify", &cfg_type_bracketed_aml, 0 },
1160         { "allow-query", &cfg_type_bracketed_aml, 0 },
1161         { "allow-query-on", &cfg_type_bracketed_aml, 0 },
1162         { "allow-transfer", &cfg_type_bracketed_aml, 0 },
1163         { "allow-update", &cfg_type_bracketed_aml, 0 },
1164         { "allow-update-forwarding", &cfg_type_bracketed_aml, 0 },
1165         { "also-notify", &cfg_type_portiplist, 0 },
1166         { "alt-transfer-source", &cfg_type_sockaddr4wild, 0 },
1167         { "alt-transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1168         { "check-dup-records", &cfg_type_checkmode, 0 },
1169         { "check-integrity", &cfg_type_boolean, 0 },
1170         { "check-mx", &cfg_type_checkmode, 0 },
1171         { "check-mx-cname", &cfg_type_checkmode, 0 },
1172         { "check-sibling", &cfg_type_boolean, 0 },
1173         { "check-srv-cname", &cfg_type_checkmode, 0 },
1174         { "check-wildcard", &cfg_type_boolean, 0 },
1175         { "dialup", &cfg_type_dialuptype, 0 },
1176         { "dnssec-dnskey-kskonly", &cfg_type_boolean, 0 },
1177         { "dnssec-secure-to-insecure", &cfg_type_boolean, 0 },
1178         { "forward", &cfg_type_forwardtype, 0 },
1179         { "forwarders", &cfg_type_portiplist, 0 },
1180         { "key-directory", &cfg_type_qstring, 0 },
1181         { "maintain-ixfr-base", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1182         { "masterfile-format", &cfg_type_masterformat, 0 },
1183         { "max-ixfr-log-size", &cfg_type_size, CFG_CLAUSEFLAG_OBSOLETE },
1184         { "max-journal-size", &cfg_type_sizenodefault, 0 },
1185         { "max-refresh-time", &cfg_type_uint32, 0 },
1186         { "max-retry-time", &cfg_type_uint32, 0 },
1187         { "max-transfer-idle-in", &cfg_type_uint32, 0 },
1188         { "max-transfer-idle-out", &cfg_type_uint32, 0 },
1189         { "max-transfer-time-in", &cfg_type_uint32, 0 },
1190         { "max-transfer-time-out", &cfg_type_uint32, 0 },
1191         { "min-refresh-time", &cfg_type_uint32, 0 },
1192         { "min-retry-time", &cfg_type_uint32, 0 },
1193         { "multi-master", &cfg_type_boolean, 0 },
1194         { "notify", &cfg_type_notifytype, 0 },
1195         { "notify-delay", &cfg_type_uint32, 0 },
1196         { "notify-source", &cfg_type_sockaddr4wild, 0 },
1197         { "notify-source-v6", &cfg_type_sockaddr6wild, 0 },
1198         { "notify-to-soa", &cfg_type_boolean, 0 },
1199         { "nsec3-test-zone", &cfg_type_boolean, CFG_CLAUSEFLAG_TESTONLY },
1200         { "sig-signing-nodes", &cfg_type_uint32, 0 },
1201         { "sig-signing-signatures", &cfg_type_uint32, 0 },
1202         { "sig-signing-type", &cfg_type_uint32, 0 },
1203         { "sig-validity-interval", &cfg_type_validityinterval, 0 },
1204         { "transfer-source", &cfg_type_sockaddr4wild, 0 },
1205         { "transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1206         { "try-tcp-refresh", &cfg_type_boolean, 0 },
1207         { "update-check-ksk", &cfg_type_boolean, 0 },
1208         { "use-alt-transfer-source", &cfg_type_boolean, 0 },
1209         { "zero-no-soa-ttl", &cfg_type_boolean, 0 },
1210         { "zone-statistics", &cfg_type_boolean, 0 },
1211         { NULL, NULL, 0 }
1212 };
1213
1214 /*%
1215  * Clauses that can be found in a 'zone' statement
1216  * only.
1217  */
1218 static cfg_clausedef_t
1219 zone_only_clauses[] = {
1220         { "type", &cfg_type_zonetype, 0 },
1221         { "file", &cfg_type_qstring, 0 },
1222         { "journal", &cfg_type_qstring, 0 },
1223         { "ixfr-base", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
1224         { "ixfr-tmp-file", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
1225         { "masters", &cfg_type_namesockaddrkeylist, 0 },
1226         { "pubkey", &cfg_type_pubkey,
1227           CFG_CLAUSEFLAG_MULTI | CFG_CLAUSEFLAG_OBSOLETE },
1228         { "update-policy", &cfg_type_updatepolicy, 0 },
1229         { "database", &cfg_type_astring, 0 },
1230         { "delegation-only", &cfg_type_boolean, 0 },
1231         /*
1232          * Note that the format of the check-names option is different between
1233          * the zone options and the global/view options.  Ugh.
1234          */
1235         { "check-names", &cfg_type_checkmode, 0 },
1236         { "ixfr-from-differences", &cfg_type_boolean, 0 },
1237         { "auto-dnssec", &cfg_type_autodnssec, 0 },
1238         { "server-addresses", &cfg_type_bracketed_sockaddrlist, 0 },
1239         { "server-names", &cfg_type_namelist, 0 },
1240         { NULL, NULL, 0 }
1241 };
1242
1243
1244 /*% The top-level named.conf syntax. */
1245
1246 static cfg_clausedef_t *
1247 namedconf_clausesets[] = {
1248         namedconf_clauses,
1249         namedconf_or_view_clauses,
1250         NULL
1251 };
1252 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_namedconf = {
1253         "namedconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1254         &cfg_rep_map, namedconf_clausesets
1255 };
1256
1257 /*% The bind.keys syntax (trusted-keys/managed-keys only). */
1258 static cfg_clausedef_t *
1259 bindkeys_clausesets[] = {
1260         bindkeys_clauses,
1261         NULL
1262 };
1263 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_bindkeys = {
1264         "bindkeys", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1265         &cfg_rep_map, bindkeys_clausesets
1266 };
1267
1268 /*% The new-zone-file syntax (for zones added by 'rndc addzone') */
1269 static cfg_clausedef_t
1270 newzones_clauses[] = {
1271         { "zone", &cfg_type_zone, CFG_CLAUSEFLAG_MULTI },
1272         { NULL, NULL, 0 }
1273 };
1274
1275 static cfg_clausedef_t *
1276 newzones_clausesets[] = {
1277         newzones_clauses,
1278         NULL
1279 };
1280
1281 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_newzones = {
1282         "newzones", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1283         &cfg_rep_map, newzones_clausesets
1284 };
1285
1286 /*% The "options" statement syntax. */
1287
1288 static cfg_clausedef_t *
1289 options_clausesets[] = {
1290         options_clauses,
1291         view_clauses,
1292         zone_clauses,
1293         NULL
1294 };
1295 static cfg_type_t cfg_type_options = {
1296         "options", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, options_clausesets };
1297
1298 /*% The "view" statement syntax. */
1299
1300 static cfg_clausedef_t *
1301 view_clausesets[] = {
1302         view_only_clauses,
1303         namedconf_or_view_clauses,
1304         view_clauses,
1305         zone_clauses,
1306         dynamically_loadable_zones_clauses,
1307         NULL
1308 };
1309 static cfg_type_t cfg_type_viewopts = {
1310         "view", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, view_clausesets };
1311
1312 /*% The "zone" statement syntax. */
1313
1314 static cfg_clausedef_t *
1315 zone_clausesets[] = {
1316         zone_only_clauses,
1317         zone_clauses,
1318         NULL
1319 };
1320 static cfg_type_t cfg_type_zoneopts = {
1321         "zoneopts", cfg_parse_map, cfg_print_map,
1322         cfg_doc_map, &cfg_rep_map, zone_clausesets };
1323
1324 /*% The "dynamically loadable zones" statement syntax. */
1325
1326 static cfg_clausedef_t *
1327 dynamically_loadable_zones_clausesets[] = {
1328         dynamically_loadable_zones_clauses,
1329         NULL
1330 };
1331 static cfg_type_t cfg_type_dynamically_loadable_zones_opts = {
1332         "dynamically_loadable_zones_opts", cfg_parse_map,
1333         cfg_print_map, cfg_doc_map, &cfg_rep_map,
1334         dynamically_loadable_zones_clausesets
1335 };
1336
1337 /*%
1338  * Clauses that can be found within the 'key' statement.
1339  */
1340 static cfg_clausedef_t
1341 key_clauses[] = {
1342         { "algorithm", &cfg_type_astring, 0 },
1343         { "secret", &cfg_type_astring, 0 },
1344         { NULL, NULL, 0 }
1345 };
1346
1347 static cfg_clausedef_t *
1348 key_clausesets[] = {
1349         key_clauses,
1350         NULL
1351 };
1352 static cfg_type_t cfg_type_key = {
1353         "key", cfg_parse_named_map, cfg_print_map,
1354         cfg_doc_map, &cfg_rep_map, key_clausesets
1355 };
1356
1357
1358 /*%
1359  * Clauses that can be found in a 'server' statement.
1360  */
1361 static cfg_clausedef_t
1362 server_clauses[] = {
1363         { "bogus", &cfg_type_boolean, 0 },
1364         { "provide-ixfr", &cfg_type_boolean, 0 },
1365         { "request-ixfr", &cfg_type_boolean, 0 },
1366         { "support-ixfr", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1367         { "transfers", &cfg_type_uint32, 0 },
1368         { "transfer-format", &cfg_type_transferformat, 0 },
1369         { "keys", &cfg_type_server_key_kludge, 0 },
1370         { "edns", &cfg_type_boolean, 0 },
1371         { "edns-udp-size", &cfg_type_uint32, 0 },
1372         { "max-udp-size", &cfg_type_uint32, 0 },
1373         { "notify-source", &cfg_type_sockaddr4wild, 0 },
1374         { "notify-source-v6", &cfg_type_sockaddr6wild, 0 },
1375         { "query-source", &cfg_type_querysource4, 0 },
1376         { "query-source-v6", &cfg_type_querysource6, 0 },
1377         { "transfer-source", &cfg_type_sockaddr4wild, 0 },
1378         { "transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1379         { NULL, NULL, 0 }
1380 };
1381 static cfg_clausedef_t *
1382 server_clausesets[] = {
1383         server_clauses,
1384         NULL
1385 };
1386 static cfg_type_t cfg_type_server = {
1387         "server", cfg_parse_netprefix_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,
1388         server_clausesets
1389 };
1390
1391
1392 /*%
1393  * Clauses that can be found in a 'channel' clause in the
1394  * 'logging' statement.
1395  *
1396  * These have some additional constraints that need to be
1397  * checked after parsing:
1398  *  - There must exactly one of file/syslog/null/stderr
1399  *
1400  */
1401 static cfg_clausedef_t
1402 channel_clauses[] = {
1403         /* Destinations.  We no longer require these to be first. */
1404         { "file", &cfg_type_logfile, 0 },
1405         { "syslog", &cfg_type_optional_facility, 0 },
1406         { "null", &cfg_type_void, 0 },
1407         { "stderr", &cfg_type_void, 0 },
1408         /* Options.  We now accept these for the null channel, too. */
1409         { "severity", &cfg_type_logseverity, 0 },
1410         { "print-time", &cfg_type_boolean, 0 },
1411         { "print-severity", &cfg_type_boolean, 0 },
1412         { "print-category", &cfg_type_boolean, 0 },
1413         { NULL, NULL, 0 }
1414 };
1415 static cfg_clausedef_t *
1416 channel_clausesets[] = {
1417         channel_clauses,
1418         NULL
1419 };
1420 static cfg_type_t cfg_type_channel = {
1421         "channel", cfg_parse_named_map, cfg_print_map, cfg_doc_map,
1422         &cfg_rep_map, channel_clausesets
1423 };
1424
1425 /*% A list of log destination, used in the "category" clause. */
1426 static cfg_type_t cfg_type_destinationlist = {
1427         "destinationlist", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
1428         &cfg_rep_list, &cfg_type_astring };
1429
1430 /*%
1431  * Clauses that can be found in a 'logging' statement.
1432  */
1433 static cfg_clausedef_t
1434 logging_clauses[] = {
1435         { "channel", &cfg_type_channel, CFG_CLAUSEFLAG_MULTI },
1436         { "category", &cfg_type_category, CFG_CLAUSEFLAG_MULTI },
1437         { NULL, NULL, 0 }
1438 };
1439 static cfg_clausedef_t *
1440 logging_clausesets[] = {
1441         logging_clauses,
1442         NULL
1443 };
1444 static cfg_type_t cfg_type_logging = {
1445         "logging", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, logging_clausesets };
1446
1447
1448 /*%
1449  * For parsing an 'addzone' statement
1450  */
1451
1452 static cfg_tuplefielddef_t addzone_fields[] = {
1453         { "name", &cfg_type_astring, 0 },
1454         { "class", &cfg_type_optional_class, 0 },
1455         { "view", &cfg_type_optional_class, 0 },
1456         { "options", &cfg_type_zoneopts, 0 },
1457         { NULL, NULL, 0 }
1458 };
1459 static cfg_type_t cfg_type_addzone = {
1460         "addzone", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, addzone_fields };
1461
1462 static cfg_clausedef_t
1463 addzoneconf_clauses[] = {
1464         { "addzone", &cfg_type_addzone, 0 },
1465         { NULL, NULL, 0 }
1466 };
1467
1468 static cfg_clausedef_t *
1469 addzoneconf_clausesets[] = {
1470         addzoneconf_clauses,
1471         NULL
1472 };
1473
1474 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_addzoneconf = {
1475         "addzoneconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
1476         &cfg_rep_map, addzoneconf_clausesets
1477 };
1478
1479
1480 static isc_result_t
1481 parse_unitstring(char *str, isc_resourcevalue_t *valuep) {
1482         char *endp;
1483         unsigned int len;
1484         isc_uint64_t value;
1485         isc_uint64_t unit;
1486
1487         value = isc_string_touint64(str, &endp, 10);
1488         if (*endp == 0) {
1489                 *valuep = value;
1490                 return (ISC_R_SUCCESS);
1491         }
1492
1493         len = strlen(str);
1494         if (len < 2 || endp[1] != '\0')
1495                 return (ISC_R_FAILURE);
1496
1497         switch (str[len - 1]) {
1498         case 'k':
1499         case 'K':
1500                 unit = 1024;
1501                 break;
1502         case 'm':
1503         case 'M':
1504                 unit = 1024 * 1024;
1505                 break;
1506         case 'g':
1507         case 'G':
1508                 unit = 1024 * 1024 * 1024;
1509                 break;
1510         default:
1511                 return (ISC_R_FAILURE);
1512         }
1513         if (value > ISC_UINT64_MAX / unit)
1514                 return (ISC_R_FAILURE);
1515         *valuep = value * unit;
1516         return (ISC_R_SUCCESS);
1517 }
1518
1519 static isc_result_t
1520 parse_sizeval(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1521         isc_result_t result;
1522         cfg_obj_t *obj = NULL;
1523         isc_uint64_t val;
1524
1525         UNUSED(type);
1526
1527         CHECK(cfg_gettoken(pctx, 0));
1528         if (pctx->token.type != isc_tokentype_string) {
1529                 result = ISC_R_UNEXPECTEDTOKEN;
1530                 goto cleanup;
1531         }
1532         CHECK(parse_unitstring(TOKEN_STRING(pctx), &val));
1533
1534         CHECK(cfg_create_obj(pctx, &cfg_type_uint64, &obj));
1535         obj->value.uint64 = val;
1536         *ret = obj;
1537         return (ISC_R_SUCCESS);
1538
1539  cleanup:
1540         cfg_parser_error(pctx, CFG_LOG_NEAR, "expected integer and optional unit");
1541         return (result);
1542 }
1543
1544 /*%
1545  * A size value (number + optional unit).
1546  */
1547 static cfg_type_t cfg_type_sizeval = {
1548         "sizeval", parse_sizeval, cfg_print_uint64, cfg_doc_terminal,
1549         &cfg_rep_uint64, NULL };
1550
1551 /*%
1552  * A size, "unlimited", or "default".
1553  */
1554
1555 static isc_result_t
1556 parse_size(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1557         return (parse_enum_or_other(pctx, type, &cfg_type_sizeval, ret));
1558 }
1559
1560 static const char *size_enums[] = { "unlimited", "default", NULL };
1561 static cfg_type_t cfg_type_size = {
1562         "size", parse_size, cfg_print_ustring, cfg_doc_terminal,
1563         &cfg_rep_string, size_enums
1564 };
1565
1566 /*%
1567  * A size or "unlimited", but not "default".
1568  */
1569 static const char *sizenodefault_enums[] = { "unlimited", NULL };
1570 static cfg_type_t cfg_type_sizenodefault = {
1571         "size_no_default", parse_size, cfg_print_ustring, cfg_doc_terminal,
1572         &cfg_rep_string, sizenodefault_enums
1573 };
1574
1575 /*%
1576  * optional_keyvalue
1577  */
1578 static isc_result_t
1579 parse_maybe_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type,
1580                               isc_boolean_t optional, cfg_obj_t **ret)
1581 {
1582         isc_result_t result;
1583         cfg_obj_t *obj = NULL;
1584         const keyword_type_t *kw = type->of;
1585
1586         CHECK(cfg_peektoken(pctx, 0));
1587         if (pctx->token.type == isc_tokentype_string &&
1588             strcasecmp(TOKEN_STRING(pctx), kw->name) == 0) {
1589                 CHECK(cfg_gettoken(pctx, 0));
1590                 CHECK(kw->type->parse(pctx, kw->type, &obj));
1591                 obj->type = type; /* XXX kludge */
1592         } else {
1593                 if (optional) {
1594                         CHECK(cfg_parse_void(pctx, NULL, &obj));
1595                 } else {
1596                         cfg_parser_error(pctx, CFG_LOG_NEAR, "expected '%s'",
1597                                      kw->name);
1598                         result = ISC_R_UNEXPECTEDTOKEN;
1599                         goto cleanup;
1600                 }
1601         }
1602         *ret = obj;
1603  cleanup:
1604         return (result);
1605 }
1606
1607 static isc_result_t
1608 parse_enum_or_other(cfg_parser_t *pctx, const cfg_type_t *enumtype,
1609                     const cfg_type_t *othertype, cfg_obj_t **ret)
1610 {
1611         isc_result_t result;
1612         CHECK(cfg_peektoken(pctx, 0));
1613         if (pctx->token.type == isc_tokentype_string &&
1614             cfg_is_enum(TOKEN_STRING(pctx), enumtype->of)) {
1615                 CHECK(cfg_parse_enum(pctx, enumtype, ret));
1616         } else {
1617                 CHECK(cfg_parse_obj(pctx, othertype, ret));
1618         }
1619  cleanup:
1620         return (result);
1621 }
1622
1623 static void
1624 doc_enum_or_other(cfg_printer_t *pctx, const cfg_type_t *type) {
1625         cfg_doc_terminal(pctx, type);
1626 #if 0 /* XXX */
1627         cfg_print_chars(pctx, "( ", 2);...
1628 #endif
1629
1630 }
1631
1632 static isc_result_t
1633 parse_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1634         return (parse_maybe_optional_keyvalue(pctx, type, ISC_FALSE, ret));
1635 }
1636
1637 static isc_result_t
1638 parse_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1639         return (parse_maybe_optional_keyvalue(pctx, type, ISC_TRUE, ret));
1640 }
1641
1642 static void
1643 print_keyvalue(cfg_printer_t *pctx, const cfg_obj_t *obj) {
1644         const keyword_type_t *kw = obj->type->of;
1645         cfg_print_cstr(pctx, kw->name);
1646         cfg_print_chars(pctx, " ", 1);
1647         kw->type->print(pctx, obj);
1648 }
1649
1650 static void
1651 doc_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type) {
1652         const keyword_type_t *kw = type->of;
1653         cfg_print_cstr(pctx, kw->name);
1654         cfg_print_chars(pctx, " ", 1);
1655         cfg_doc_obj(pctx, kw->type);
1656 }
1657
1658 static void
1659 doc_optional_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type) {
1660         const keyword_type_t *kw = type->of;
1661         cfg_print_chars(pctx, "[ ", 2);
1662         cfg_print_cstr(pctx, kw->name);
1663         cfg_print_chars(pctx, " ", 1);
1664         cfg_doc_obj(pctx, kw->type);
1665         cfg_print_chars(pctx, " ]", 2);
1666 }
1667
1668 static const char *dialup_enums[] = {
1669         "notify", "notify-passive", "refresh", "passive", NULL };
1670 static isc_result_t
1671 parse_dialup_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1672         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1673 }
1674 static cfg_type_t cfg_type_dialuptype = {
1675         "dialuptype", parse_dialup_type, cfg_print_ustring, doc_enum_or_other,
1676         &cfg_rep_string, dialup_enums
1677 };
1678
1679 static const char *notify_enums[] = { "explicit", "master-only", NULL };
1680 static isc_result_t
1681 parse_notify_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1682         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1683 }
1684 static cfg_type_t cfg_type_notifytype = {
1685         "notifytype", parse_notify_type, cfg_print_ustring, doc_enum_or_other,
1686         &cfg_rep_string, notify_enums,
1687 };
1688
1689 static const char *ixfrdiff_enums[] = { "master", "slave", NULL };
1690 static isc_result_t
1691 parse_ixfrdiff_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1692         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1693 }
1694 static cfg_type_t cfg_type_ixfrdifftype = {
1695         "ixfrdiff", parse_ixfrdiff_type, cfg_print_ustring, doc_enum_or_other,
1696         &cfg_rep_string, ixfrdiff_enums,
1697 };
1698
1699 static const char *v4_aaaa_enums[] = { "break-dnssec", NULL };
1700 static isc_result_t
1701 parse_v4_aaaa(cfg_parser_t *pctx, const cfg_type_t *type,
1702                      cfg_obj_t **ret) {
1703         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1704 }
1705 static cfg_type_t cfg_type_v4_aaaa = {
1706         "v4_aaaa", parse_v4_aaaa, cfg_print_ustring,
1707         doc_enum_or_other, &cfg_rep_string, v4_aaaa_enums,
1708 };
1709
1710 static keyword_type_t key_kw = { "key", &cfg_type_astring };
1711
1712 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_keyref = {
1713         "keyref", parse_keyvalue, print_keyvalue, doc_keyvalue,
1714         &cfg_rep_string, &key_kw
1715 };
1716
1717 static cfg_type_t cfg_type_optional_keyref = {
1718         "optional_keyref", parse_optional_keyvalue, print_keyvalue,
1719         doc_optional_keyvalue, &cfg_rep_string, &key_kw
1720 };
1721
1722 /*%
1723  * A "controls" statement is represented as a map with the multivalued
1724  * "inet" and "unix" clauses.
1725  */
1726
1727 static keyword_type_t controls_allow_kw = {
1728         "allow", &cfg_type_bracketed_aml };
1729
1730 static cfg_type_t cfg_type_controls_allow = {
1731         "controls_allow", parse_keyvalue,
1732         print_keyvalue, doc_keyvalue,
1733         &cfg_rep_list, &controls_allow_kw
1734 };
1735
1736 static keyword_type_t controls_keys_kw = {
1737         "keys", &cfg_type_keylist };
1738
1739 static cfg_type_t cfg_type_controls_keys = {
1740         "controls_keys", parse_optional_keyvalue,
1741         print_keyvalue, doc_optional_keyvalue,
1742         &cfg_rep_list, &controls_keys_kw
1743 };
1744
1745 static cfg_tuplefielddef_t inetcontrol_fields[] = {
1746         { "address", &cfg_type_controls_sockaddr, 0 },
1747         { "allow", &cfg_type_controls_allow, 0 },
1748         { "keys", &cfg_type_controls_keys, 0 },
1749         { NULL, NULL, 0 }
1750 };
1751
1752 static cfg_type_t cfg_type_inetcontrol = {
1753         "inetcontrol", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
1754         inetcontrol_fields
1755 };
1756
1757 static keyword_type_t controls_perm_kw = {
1758         "perm", &cfg_type_uint32 };
1759
1760 static cfg_type_t cfg_type_controls_perm = {
1761         "controls_perm", parse_keyvalue,
1762         print_keyvalue, doc_keyvalue,
1763         &cfg_rep_uint32, &controls_perm_kw
1764 };
1765
1766 static keyword_type_t controls_owner_kw = {
1767         "owner", &cfg_type_uint32 };
1768
1769 static cfg_type_t cfg_type_controls_owner = {
1770         "controls_owner", parse_keyvalue,
1771         print_keyvalue, doc_keyvalue,
1772         &cfg_rep_uint32, &controls_owner_kw
1773 };
1774
1775 static keyword_type_t controls_group_kw = {
1776         "group", &cfg_type_uint32 };
1777
1778 static cfg_type_t cfg_type_controls_group = {
1779         "controls_allow", parse_keyvalue,
1780         print_keyvalue, doc_keyvalue,
1781         &cfg_rep_uint32, &controls_group_kw
1782 };
1783
1784 static cfg_tuplefielddef_t unixcontrol_fields[] = {
1785         { "path", &cfg_type_qstring, 0 },
1786         { "perm", &cfg_type_controls_perm, 0 },
1787         { "owner", &cfg_type_controls_owner, 0 },
1788         { "group", &cfg_type_controls_group, 0 },
1789         { "keys", &cfg_type_controls_keys, 0 },
1790         { NULL, NULL, 0 }
1791 };
1792
1793 static cfg_type_t cfg_type_unixcontrol = {
1794         "unixcontrol", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
1795         unixcontrol_fields
1796 };
1797
1798 static cfg_clausedef_t
1799 controls_clauses[] = {
1800         { "inet", &cfg_type_inetcontrol, CFG_CLAUSEFLAG_MULTI },
1801         { "unix", &cfg_type_unixcontrol, CFG_CLAUSEFLAG_MULTI },
1802         { NULL, NULL, 0 }
1803 };
1804
1805 static cfg_clausedef_t *
1806 controls_clausesets[] = {
1807         controls_clauses,
1808         NULL
1809 };
1810 static cfg_type_t cfg_type_controls = {
1811         "controls", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,    &controls_clausesets
1812 };
1813
1814 /*%
1815  * A "statistics-channels" statement is represented as a map with the
1816  * multivalued "inet" clauses.
1817  */
1818 static void
1819 doc_optional_bracketed_list(cfg_printer_t *pctx, const cfg_type_t *type) {
1820         const keyword_type_t *kw = type->of;
1821         cfg_print_chars(pctx, "[ ", 2);
1822         cfg_print_cstr(pctx, kw->name);
1823         cfg_print_chars(pctx, " ", 1);
1824         cfg_doc_obj(pctx, kw->type);
1825         cfg_print_chars(pctx, " ]", 2);
1826 }
1827
1828 static cfg_type_t cfg_type_optional_allow = {
1829         "optional_allow", parse_optional_keyvalue, print_keyvalue,
1830         doc_optional_bracketed_list, &cfg_rep_list, &controls_allow_kw
1831 };
1832
1833 static cfg_tuplefielddef_t statserver_fields[] = {
1834         { "address", &cfg_type_controls_sockaddr, 0 }, /* reuse controls def */
1835         { "allow", &cfg_type_optional_allow, 0 },
1836         { NULL, NULL, 0 }
1837 };
1838
1839 static cfg_type_t cfg_type_statschannel = {
1840         "statschannel", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
1841         &cfg_rep_tuple, statserver_fields
1842 };
1843
1844 static cfg_clausedef_t
1845 statservers_clauses[] = {
1846         { "inet", &cfg_type_statschannel, CFG_CLAUSEFLAG_MULTI },
1847         { NULL, NULL, 0 }
1848 };
1849
1850 static cfg_clausedef_t *
1851 statservers_clausesets[] = {
1852         statservers_clauses,
1853         NULL
1854 };
1855
1856 static cfg_type_t cfg_type_statschannels = {
1857         "statistics-channels", cfg_parse_map, cfg_print_map, cfg_doc_map,
1858         &cfg_rep_map,   &statservers_clausesets
1859 };
1860
1861 /*%
1862  * An optional class, as used in view and zone statements.
1863  */
1864 static isc_result_t
1865 parse_optional_class(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1866         isc_result_t result;
1867         UNUSED(type);
1868         CHECK(cfg_peektoken(pctx, 0));
1869         if (pctx->token.type == isc_tokentype_string)
1870                 CHECK(cfg_parse_obj(pctx, &cfg_type_ustring, ret));
1871         else
1872                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
1873  cleanup:
1874         return (result);
1875 }
1876
1877 static cfg_type_t cfg_type_optional_class = {
1878         "optional_class", parse_optional_class, NULL, cfg_doc_terminal,
1879         NULL, NULL
1880 };
1881
1882 static isc_result_t
1883 parse_querysource(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1884         isc_result_t result;
1885         cfg_obj_t *obj = NULL;
1886         isc_netaddr_t netaddr;
1887         in_port_t port;
1888         unsigned int have_address = 0;
1889         unsigned int have_port = 0;
1890         const unsigned int *flagp = type->of;
1891
1892         if ((*flagp & CFG_ADDR_V4OK) != 0)
1893                 isc_netaddr_any(&netaddr);
1894         else if ((*flagp & CFG_ADDR_V6OK) != 0)
1895                 isc_netaddr_any6(&netaddr);
1896         else
1897                 INSIST(0);
1898
1899         port = 0;
1900
1901         for (;;) {
1902                 CHECK(cfg_peektoken(pctx, 0));
1903                 if (pctx->token.type == isc_tokentype_string) {
1904                         if (strcasecmp(TOKEN_STRING(pctx),
1905                                        "address") == 0)
1906                         {
1907                                 /* read "address" */
1908                                 CHECK(cfg_gettoken(pctx, 0));
1909                                 CHECK(cfg_parse_rawaddr(pctx, *flagp,
1910                                                         &netaddr));
1911                                 have_address++;
1912                         } else if (strcasecmp(TOKEN_STRING(pctx), "port") == 0)
1913                         {
1914                                 /* read "port" */
1915                                 CHECK(cfg_gettoken(pctx, 0));
1916                                 CHECK(cfg_parse_rawport(pctx,
1917                                                         CFG_ADDR_WILDOK,
1918                                                         &port));
1919                                 have_port++;
1920                         } else if (have_port == 0 && have_address == 0) {
1921                                 return (cfg_parse_sockaddr(pctx, type, ret));
1922                         } else {
1923                                 cfg_parser_error(pctx, CFG_LOG_NEAR,
1924                                              "expected 'address' or 'port'");
1925                                 return (ISC_R_UNEXPECTEDTOKEN);
1926                         }
1927                 } else
1928                         break;
1929         }
1930         if (have_address > 1 || have_port > 1 ||
1931             have_address + have_port == 0) {
1932                 cfg_parser_error(pctx, 0, "expected one address and/or port");
1933                 return (ISC_R_UNEXPECTEDTOKEN);
1934         }
1935
1936         CHECK(cfg_create_obj(pctx, &cfg_type_querysource, &obj));
1937         isc_sockaddr_fromnetaddr(&obj->value.sockaddr, &netaddr, port);
1938         *ret = obj;
1939         return (ISC_R_SUCCESS);
1940
1941  cleanup:
1942         cfg_parser_error(pctx, CFG_LOG_NEAR, "invalid query source");
1943         CLEANUP_OBJ(obj);
1944         return (result);
1945 }
1946
1947 static void
1948 print_querysource(cfg_printer_t *pctx, const cfg_obj_t *obj) {
1949         isc_netaddr_t na;
1950         isc_netaddr_fromsockaddr(&na, &obj->value.sockaddr);
1951         cfg_print_cstr(pctx, "address ");
1952         cfg_print_rawaddr(pctx, &na);
1953         cfg_print_cstr(pctx, " port ");
1954         cfg_print_rawuint(pctx, isc_sockaddr_getport(&obj->value.sockaddr));
1955 }
1956
1957 static unsigned int sockaddr4wild_flags = CFG_ADDR_WILDOK | CFG_ADDR_V4OK;
1958 static unsigned int sockaddr6wild_flags = CFG_ADDR_WILDOK | CFG_ADDR_V6OK;
1959
1960 static cfg_type_t cfg_type_querysource4 = {
1961         "querysource4", parse_querysource, NULL, cfg_doc_terminal,
1962         NULL, &sockaddr4wild_flags
1963 };
1964
1965 static cfg_type_t cfg_type_querysource6 = {
1966         "querysource6", parse_querysource, NULL, cfg_doc_terminal,
1967         NULL, &sockaddr6wild_flags
1968 };
1969
1970 static cfg_type_t cfg_type_querysource = {
1971         "querysource", NULL, print_querysource, NULL, &cfg_rep_sockaddr, NULL
1972 };
1973
1974 /*% addrmatchelt */
1975
1976 static isc_result_t
1977 parse_addrmatchelt(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1978         isc_result_t result;
1979         UNUSED(type);
1980
1981         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
1982
1983         if (pctx->token.type == isc_tokentype_string ||
1984             pctx->token.type == isc_tokentype_qstring) {
1985                 if (pctx->token.type == isc_tokentype_string &&
1986                     (strcasecmp(TOKEN_STRING(pctx), "key") == 0)) {
1987                         CHECK(cfg_parse_obj(pctx, &cfg_type_keyref, ret));
1988                 } else {
1989                         if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK |
1990                                                   CFG_ADDR_V4PREFIXOK |
1991                                                   CFG_ADDR_V6OK))
1992                         {
1993                                 CHECK(cfg_parse_netprefix(pctx, NULL, ret));
1994                         } else {
1995                                 CHECK(cfg_parse_astring(pctx, NULL, ret));
1996                         }
1997                 }
1998         } else if (pctx->token.type == isc_tokentype_special) {
1999                 if (pctx->token.value.as_char == '{') {
2000                         /* Nested match list. */
2001                         CHECK(cfg_parse_obj(pctx, &cfg_type_bracketed_aml, ret));
2002                 } else if (pctx->token.value.as_char == '!') {
2003                         CHECK(cfg_gettoken(pctx, 0)); /* read "!" */
2004                         CHECK(cfg_parse_obj(pctx, &cfg_type_negated, ret));
2005                 } else {
2006                         goto bad;
2007                 }
2008         } else {
2009         bad:
2010                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2011                              "expected IP match list element");
2012                 return (ISC_R_UNEXPECTEDTOKEN);
2013         }
2014  cleanup:
2015         return (result);
2016 }
2017
2018 /*%
2019  * A negated address match list element (like "! 10.0.0.1").
2020  * Somewhat sneakily, the caller is expected to parse the
2021  * "!", but not to print it.
2022  */
2023
2024 static cfg_tuplefielddef_t negated_fields[] = {
2025         { "value", &cfg_type_addrmatchelt, 0 },
2026         { NULL, NULL, 0 }
2027 };
2028
2029 static void
2030 print_negated(cfg_printer_t *pctx, const cfg_obj_t *obj) {
2031         cfg_print_chars(pctx, "!", 1);
2032         cfg_print_tuple(pctx, obj);
2033 }
2034
2035 static cfg_type_t cfg_type_negated = {
2036         "negated", cfg_parse_tuple, print_negated, NULL, &cfg_rep_tuple,
2037         &negated_fields
2038 };
2039
2040 /*% An address match list element */
2041
2042 static cfg_type_t cfg_type_addrmatchelt = {
2043         "address_match_element", parse_addrmatchelt, NULL, cfg_doc_terminal,
2044         NULL, NULL
2045 };
2046
2047 /*% A bracketed address match list */
2048
2049 static cfg_type_t cfg_type_bracketed_aml = {
2050         "bracketed_aml", cfg_parse_bracketed_list, cfg_print_bracketed_list,
2051         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_addrmatchelt
2052 };
2053
2054 /*%
2055  * The socket address syntax in the "controls" statement is silly.
2056  * It allows both socket address families, but also allows "*",
2057  * whis is gratuitously interpreted as the IPv4 wildcard address.
2058  */
2059 static unsigned int controls_sockaddr_flags =
2060         CFG_ADDR_V4OK | CFG_ADDR_V6OK | CFG_ADDR_WILDOK;
2061 static cfg_type_t cfg_type_controls_sockaddr = {
2062         "controls_sockaddr", cfg_parse_sockaddr, cfg_print_sockaddr,
2063         cfg_doc_sockaddr, &cfg_rep_sockaddr, &controls_sockaddr_flags
2064 };
2065
2066 /*%
2067  * Handle the special kludge syntax of the "keys" clause in the "server"
2068  * statement, which takes a single key with or without braces and semicolon.
2069  */
2070 static isc_result_t
2071 parse_server_key_kludge(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
2072 {
2073         isc_result_t result;
2074         isc_boolean_t braces = ISC_FALSE;
2075         UNUSED(type);
2076
2077         /* Allow opening brace. */
2078         CHECK(cfg_peektoken(pctx, 0));
2079         if (pctx->token.type == isc_tokentype_special &&
2080             pctx->token.value.as_char == '{') {
2081                 result = cfg_gettoken(pctx, 0);
2082                 braces = ISC_TRUE;
2083         }
2084
2085         CHECK(cfg_parse_obj(pctx, &cfg_type_astring, ret));
2086
2087         if (braces) {
2088                 /* Skip semicolon if present. */
2089                 CHECK(cfg_peektoken(pctx, 0));
2090                 if (pctx->token.type == isc_tokentype_special &&
2091                     pctx->token.value.as_char == ';')
2092                         CHECK(cfg_gettoken(pctx, 0));
2093
2094                 CHECK(cfg_parse_special(pctx, '}'));
2095         }
2096  cleanup:
2097         return (result);
2098 }
2099 static cfg_type_t cfg_type_server_key_kludge = {
2100         "server_key", parse_server_key_kludge, NULL, cfg_doc_terminal,
2101         NULL, NULL
2102 };
2103
2104
2105 /*%
2106  * An optional logging facility.
2107  */
2108
2109 static isc_result_t
2110 parse_optional_facility(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
2111 {
2112         isc_result_t result;
2113         UNUSED(type);
2114
2115         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2116         if (pctx->token.type == isc_tokentype_string ||
2117             pctx->token.type == isc_tokentype_qstring) {
2118                 CHECK(cfg_parse_obj(pctx, &cfg_type_astring, ret));
2119         } else {
2120                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
2121         }
2122  cleanup:
2123         return (result);
2124 }
2125
2126 static cfg_type_t cfg_type_optional_facility = {
2127         "optional_facility", parse_optional_facility, NULL, cfg_doc_terminal,
2128         NULL, NULL };
2129
2130
2131 /*%
2132  * A log severity.  Return as a string, except "debug N",
2133  * which is returned as a keyword object.
2134  */
2135
2136 static keyword_type_t debug_kw = { "debug", &cfg_type_uint32 };
2137 static cfg_type_t cfg_type_debuglevel = {
2138         "debuglevel", parse_keyvalue,
2139         print_keyvalue, doc_keyvalue,
2140         &cfg_rep_uint32, &debug_kw
2141 };
2142
2143 static isc_result_t
2144 parse_logseverity(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2145         isc_result_t result;
2146         UNUSED(type);
2147
2148         CHECK(cfg_peektoken(pctx, 0));
2149         if (pctx->token.type == isc_tokentype_string &&
2150             strcasecmp(TOKEN_STRING(pctx), "debug") == 0) {
2151                 CHECK(cfg_gettoken(pctx, 0)); /* read "debug" */
2152                 CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER));
2153                 if (pctx->token.type == isc_tokentype_number) {
2154                         CHECK(cfg_parse_uint32(pctx, NULL, ret));
2155                 } else {
2156                         /*
2157                          * The debug level is optional and defaults to 1.
2158                          * This makes little sense, but we support it for
2159                          * compatibility with BIND 8.
2160                          */
2161                         CHECK(cfg_create_obj(pctx, &cfg_type_uint32, ret));
2162                         (*ret)->value.uint32 = 1;
2163                 }
2164                 (*ret)->type = &cfg_type_debuglevel; /* XXX kludge */
2165         } else {
2166                 CHECK(cfg_parse_obj(pctx, &cfg_type_loglevel, ret));
2167         }
2168  cleanup:
2169         return (result);
2170 }
2171
2172 static cfg_type_t cfg_type_logseverity = {
2173         "log_severity", parse_logseverity, NULL, cfg_doc_terminal,
2174         NULL, NULL };
2175
2176 /*%
2177  * The "file" clause of the "channel" statement.
2178  * This is yet another special case.
2179  */
2180
2181 static const char *logversions_enums[] = { "unlimited", NULL };
2182 static isc_result_t
2183 parse_logversions(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2184         return (parse_enum_or_other(pctx, type, &cfg_type_uint32, ret));
2185 }
2186
2187 static cfg_type_t cfg_type_logversions = {
2188         "logversions", parse_logversions, cfg_print_ustring, cfg_doc_terminal,
2189         &cfg_rep_string, logversions_enums
2190 };
2191
2192 static cfg_tuplefielddef_t logfile_fields[] = {
2193         { "file", &cfg_type_qstring, 0 },
2194         { "versions", &cfg_type_logversions, 0 },
2195         { "size", &cfg_type_size, 0 },
2196         { NULL, NULL, 0 }
2197 };
2198
2199 static isc_result_t
2200 parse_logfile(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
2201         isc_result_t result;
2202         cfg_obj_t *obj = NULL;
2203         const cfg_tuplefielddef_t *fields = type->of;
2204
2205         CHECK(cfg_create_tuple(pctx, type, &obj));
2206
2207         /* Parse the mandatory "file" field */
2208         CHECK(cfg_parse_obj(pctx, fields[0].type, &obj->value.tuple[0]));
2209
2210         /* Parse "versions" and "size" fields in any order. */
2211         for (;;) {
2212                 CHECK(cfg_peektoken(pctx, 0));
2213                 if (pctx->token.type == isc_tokentype_string) {
2214                         CHECK(cfg_gettoken(pctx, 0));
2215                         if (strcasecmp(TOKEN_STRING(pctx),
2216                                        "versions") == 0 &&
2217                             obj->value.tuple[1] == NULL) {
2218                                 CHECK(cfg_parse_obj(pctx, fields[1].type,
2219                                             &obj->value.tuple[1]));
2220                         } else if (strcasecmp(TOKEN_STRING(pctx),
2221                                               "size") == 0 &&
2222                                    obj->value.tuple[2] == NULL) {
2223                                 CHECK(cfg_parse_obj(pctx, fields[2].type,
2224                                             &obj->value.tuple[2]));
2225                         } else {
2226                                 break;
2227                         }
2228                 } else {
2229                         break;
2230                 }
2231         }
2232
2233         /* Create void objects for missing optional values. */
2234         if (obj->value.tuple[1] == NULL)
2235                 CHECK(cfg_parse_void(pctx, NULL, &obj->value.tuple[1]));
2236         if (obj->value.tuple[2] == NULL)
2237                 CHECK(cfg_parse_void(pctx, NULL, &obj->value.tuple[2]));
2238
2239         *ret = obj;
2240         return (ISC_R_SUCCESS);
2241
2242  cleanup:
2243         CLEANUP_OBJ(obj);
2244         return (result);
2245 }
2246
2247 static void
2248 print_logfile(cfg_printer_t *pctx, const cfg_obj_t *obj) {
2249         cfg_print_obj(pctx, obj->value.tuple[0]); /* file */
2250         if (obj->value.tuple[1]->type->print != cfg_print_void) {
2251                 cfg_print_cstr(pctx, " versions ");
2252                 cfg_print_obj(pctx, obj->value.tuple[1]);
2253         }
2254         if (obj->value.tuple[2]->type->print != cfg_print_void) {
2255                 cfg_print_cstr(pctx, " size ");
2256                 cfg_print_obj(pctx, obj->value.tuple[2]);
2257         }
2258 }
2259
2260
2261 static void
2262 doc_logfile(cfg_printer_t *pctx, const cfg_type_t *type) {
2263         UNUSED(type);
2264         cfg_print_cstr(pctx, "<quoted_string>");
2265         cfg_print_chars(pctx, " ", 1);
2266         cfg_print_cstr(pctx, "[ versions ( \"unlimited\" | <integer> ) ]");
2267         cfg_print_chars(pctx, " ", 1);
2268         cfg_print_cstr(pctx, "[ size <size> ]");
2269 }
2270
2271 static cfg_type_t cfg_type_logfile = {
2272         "log_file", parse_logfile, print_logfile, doc_logfile,
2273         &cfg_rep_tuple, logfile_fields
2274 };
2275
2276 /*% An IPv4 address with optional port, "*" accepted as wildcard. */
2277 static cfg_type_t cfg_type_sockaddr4wild = {
2278         "sockaddr4wild", cfg_parse_sockaddr, cfg_print_sockaddr,
2279         cfg_doc_sockaddr, &cfg_rep_sockaddr, &sockaddr4wild_flags
2280 };
2281
2282 /*% An IPv6 address with optional port, "*" accepted as wildcard. */
2283 static cfg_type_t cfg_type_sockaddr6wild = {
2284         "v6addrportwild", cfg_parse_sockaddr, cfg_print_sockaddr,
2285         cfg_doc_sockaddr, &cfg_rep_sockaddr, &sockaddr6wild_flags
2286 };
2287
2288 /*%
2289  * lwres
2290  */
2291
2292 static cfg_tuplefielddef_t lwres_view_fields[] = {
2293         { "name", &cfg_type_astring, 0 },
2294         { "class", &cfg_type_optional_class, 0 },
2295         { NULL, NULL, 0 }
2296 };
2297 static cfg_type_t cfg_type_lwres_view = {
2298         "lwres_view", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
2299         lwres_view_fields
2300 };
2301
2302 static cfg_type_t cfg_type_lwres_searchlist = {
2303         "lwres_searchlist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
2304         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_astring };
2305
2306 static cfg_clausedef_t
2307 lwres_clauses[] = {
2308         { "listen-on", &cfg_type_portiplist, 0 },
2309         { "view", &cfg_type_lwres_view, 0 },
2310         { "search", &cfg_type_lwres_searchlist, 0 },
2311         { "ndots", &cfg_type_uint32, 0 },
2312         { NULL, NULL, 0 }
2313 };
2314
2315 static cfg_clausedef_t *
2316 lwres_clausesets[] = {
2317         lwres_clauses,
2318         NULL
2319 };
2320 static cfg_type_t cfg_type_lwres = {
2321         "lwres", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,
2322         lwres_clausesets
2323 };
2324
2325 /*%
2326  * rndc
2327  */
2328
2329 static cfg_clausedef_t
2330 rndcconf_options_clauses[] = {
2331         { "default-key", &cfg_type_astring, 0 },
2332         { "default-port", &cfg_type_uint32, 0 },
2333         { "default-server", &cfg_type_astring, 0 },
2334         { "default-source-address", &cfg_type_netaddr4wild, 0 },
2335         { "default-source-address-v6", &cfg_type_netaddr6wild, 0 },
2336         { NULL, NULL, 0 }
2337 };
2338
2339 static cfg_clausedef_t *
2340 rndcconf_options_clausesets[] = {
2341         rndcconf_options_clauses,
2342         NULL
2343 };
2344
2345 static cfg_type_t cfg_type_rndcconf_options = {
2346         "rndcconf_options", cfg_parse_map, cfg_print_map, cfg_doc_map,
2347         &cfg_rep_map, rndcconf_options_clausesets
2348 };
2349
2350 static cfg_clausedef_t
2351 rndcconf_server_clauses[] = {
2352         { "key", &cfg_type_astring, 0 },
2353         { "port", &cfg_type_uint32, 0 },
2354         { "source-address", &cfg_type_netaddr4wild, 0 },
2355         { "source-address-v6", &cfg_type_netaddr6wild, 0 },
2356         { "addresses", &cfg_type_bracketed_sockaddrnameportlist, 0 },
2357         { NULL, NULL, 0 }
2358 };
2359
2360 static cfg_clausedef_t *
2361 rndcconf_server_clausesets[] = {
2362         rndcconf_server_clauses,
2363         NULL
2364 };
2365
2366 static cfg_type_t cfg_type_rndcconf_server = {
2367         "rndcconf_server", cfg_parse_named_map, cfg_print_map, cfg_doc_map,
2368         &cfg_rep_map, rndcconf_server_clausesets
2369 };
2370
2371 static cfg_clausedef_t
2372 rndcconf_clauses[] = {
2373         { "key", &cfg_type_key, CFG_CLAUSEFLAG_MULTI },
2374         { "server", &cfg_type_rndcconf_server, CFG_CLAUSEFLAG_MULTI },
2375         { "options", &cfg_type_rndcconf_options, 0 },
2376         { NULL, NULL, 0 }
2377 };
2378
2379 static cfg_clausedef_t *
2380 rndcconf_clausesets[] = {
2381         rndcconf_clauses,
2382         NULL
2383 };
2384
2385 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_rndcconf = {
2386         "rndcconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2387         &cfg_rep_map, rndcconf_clausesets
2388 };
2389
2390 static cfg_clausedef_t
2391 rndckey_clauses[] = {
2392         { "key", &cfg_type_key, 0 },
2393         { NULL, NULL, 0 }
2394 };
2395
2396 static cfg_clausedef_t *
2397 rndckey_clausesets[] = {
2398         rndckey_clauses,
2399         NULL
2400 };
2401
2402 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_rndckey = {
2403         "rndckey", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2404         &cfg_rep_map, rndckey_clausesets
2405 };
2406
2407 /*
2408  * session.key has exactly the same syntax as rndc.key, but it's defined
2409  * separately for clarity (and so we can extend it someday, if needed).
2410  */
2411 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_sessionkey = {
2412         "sessionkey", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2413         &cfg_rep_map, rndckey_clausesets
2414 };
2415
2416 static cfg_tuplefielddef_t nameport_fields[] = {
2417         { "name", &cfg_type_astring, 0 },
2418         { "port", &cfg_type_optional_port, 0 },
2419         { NULL, NULL, 0 }
2420 };
2421 static cfg_type_t cfg_type_nameport = {
2422         "nameport", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2423         &cfg_rep_tuple, nameport_fields
2424 };
2425
2426 static void
2427 doc_sockaddrnameport(cfg_printer_t *pctx, const cfg_type_t *type) {
2428         UNUSED(type);
2429         cfg_print_chars(pctx, "( ", 2);
2430         cfg_print_cstr(pctx, "<quoted_string>");
2431         cfg_print_chars(pctx, " ", 1);
2432         cfg_print_cstr(pctx, "[ port <integer> ]");
2433         cfg_print_chars(pctx, " | ", 3);
2434         cfg_print_cstr(pctx, "<ipv4_address>");
2435         cfg_print_chars(pctx, " ", 1);
2436         cfg_print_cstr(pctx, "[ port <integer> ]");
2437         cfg_print_chars(pctx, " | ", 3);
2438         cfg_print_cstr(pctx, "<ipv6_address>");
2439         cfg_print_chars(pctx, " ", 1);
2440         cfg_print_cstr(pctx, "[ port <integer> ]");
2441         cfg_print_chars(pctx, " )", 2);
2442 }
2443
2444 static isc_result_t
2445 parse_sockaddrnameport(cfg_parser_t *pctx, const cfg_type_t *type,
2446                        cfg_obj_t **ret)
2447 {
2448         isc_result_t result;
2449         cfg_obj_t *obj = NULL;
2450         UNUSED(type);
2451
2452         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2453         if (pctx->token.type == isc_tokentype_string ||
2454             pctx->token.type == isc_tokentype_qstring) {
2455                 if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK | CFG_ADDR_V6OK))
2456                         CHECK(cfg_parse_sockaddr(pctx, &cfg_type_sockaddr, ret));
2457                 else {
2458                         const cfg_tuplefielddef_t *fields =
2459                                                    cfg_type_nameport.of;
2460                         CHECK(cfg_create_tuple(pctx, &cfg_type_nameport,
2461                                                &obj));
2462                         CHECK(cfg_parse_obj(pctx, fields[0].type,
2463                                             &obj->value.tuple[0]));
2464                         CHECK(cfg_parse_obj(pctx, fields[1].type,
2465                                             &obj->value.tuple[1]));
2466                         *ret = obj;
2467                         obj = NULL;
2468                 }
2469         } else {
2470                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2471                              "expected IP address or hostname");
2472                 return (ISC_R_UNEXPECTEDTOKEN);
2473         }
2474  cleanup:
2475         CLEANUP_OBJ(obj);
2476         return (result);
2477 }
2478
2479 static cfg_type_t cfg_type_sockaddrnameport = {
2480         "sockaddrnameport_element", parse_sockaddrnameport, NULL,
2481          doc_sockaddrnameport, NULL, NULL
2482 };
2483
2484 static cfg_type_t cfg_type_bracketed_sockaddrnameportlist = {
2485         "bracketed_sockaddrnameportlist", cfg_parse_bracketed_list,
2486         cfg_print_bracketed_list, cfg_doc_bracketed_list,
2487         &cfg_rep_list, &cfg_type_sockaddrnameport
2488 };
2489
2490 /*%
2491  * A list of socket addresses or name with an optional default port,
2492  * as used in the dual-stack-servers option.  E.g.,
2493  * "port 1234 { dual-stack-servers.net; 10.0.0.1; 1::2 port 69; }"
2494  */
2495 static cfg_tuplefielddef_t nameportiplist_fields[] = {
2496         { "port", &cfg_type_optional_port, 0 },
2497         { "addresses", &cfg_type_bracketed_sockaddrnameportlist, 0 },
2498         { NULL, NULL, 0 }
2499 };
2500
2501 static cfg_type_t cfg_type_nameportiplist = {
2502         "nameportiplist", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2503         &cfg_rep_tuple, nameportiplist_fields
2504 };
2505
2506 /*%
2507  * masters element.
2508  */
2509
2510 static void
2511 doc_masterselement(cfg_printer_t *pctx, const cfg_type_t *type) {
2512         UNUSED(type);
2513         cfg_print_chars(pctx, "( ", 2);
2514         cfg_print_cstr(pctx, "<masters>");
2515         cfg_print_chars(pctx, " | ", 3);
2516         cfg_print_cstr(pctx, "<ipv4_address>");
2517         cfg_print_chars(pctx, " ", 1);
2518         cfg_print_cstr(pctx, "[ port <integer> ]");
2519         cfg_print_chars(pctx, " | ", 3);
2520         cfg_print_cstr(pctx, "<ipv6_address>");
2521         cfg_print_chars(pctx, " ", 1);
2522         cfg_print_cstr(pctx, "[ port <integer> ]");
2523         cfg_print_chars(pctx, " )", 2);
2524 }
2525
2526 static isc_result_t
2527 parse_masterselement(cfg_parser_t *pctx, const cfg_type_t *type,
2528                      cfg_obj_t **ret)
2529 {
2530         isc_result_t result;
2531         cfg_obj_t *obj = NULL;
2532         UNUSED(type);
2533
2534         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2535         if (pctx->token.type == isc_tokentype_string ||
2536             pctx->token.type == isc_tokentype_qstring) {
2537                 if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK | CFG_ADDR_V6OK))
2538                         CHECK(cfg_parse_sockaddr(pctx, &cfg_type_sockaddr, ret));
2539                 else
2540                         CHECK(cfg_parse_astring(pctx, &cfg_type_astring, ret));
2541         } else {
2542                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2543                              "expected IP address or masters name");
2544                 return (ISC_R_UNEXPECTEDTOKEN);
2545         }
2546  cleanup:
2547         CLEANUP_OBJ(obj);
2548         return (result);
2549 }
2550
2551 static cfg_type_t cfg_type_masterselement = {
2552         "masters_element", parse_masterselement, NULL,
2553          doc_masterselement, NULL, NULL
2554 };