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