Detect FPU by checking CPUID features.
[dragonfly.git] / contrib / bind-9.5.2 / lib / isccfg / namedconf.c
1 /*
2  * Copyright (C) 2004-2008  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2002, 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: namedconf.c,v 1.78.46.11 2008/09/27 23:39:42 jinmei Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <isc/lex.h>
27 #include <isc/result.h>
28 #include <isc/string.h>
29 #include <isc/util.h>
30
31 #include <isccfg/cfg.h>
32 #include <isccfg/grammar.h>
33 #include <isccfg/log.h>
34
35 #define TOKEN_STRING(pctx) (pctx->token.value.as_textregion.base)
36
37 /*% Check a return value. */
38 #define CHECK(op)                                               \
39         do { result = (op);                                     \
40                 if (result != ISC_R_SUCCESS) goto cleanup;      \
41         } while (0)
42
43 /*% Clean up a configuration object if non-NULL. */
44 #define CLEANUP_OBJ(obj) \
45         do { if ((obj) != NULL) cfg_obj_destroy(pctx, &(obj)); } while (0)
46
47
48 /*%
49  * Forward declarations of static functions.
50  */
51
52 static isc_result_t
53 parse_enum_or_other(cfg_parser_t *pctx, const cfg_type_t *enumtype,
54                     const cfg_type_t *othertype, cfg_obj_t **ret);
55
56 static isc_result_t
57 parse_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
58
59 static isc_result_t
60 parse_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
61
62 static void
63 print_keyvalue(cfg_printer_t *pctx, const cfg_obj_t *obj);
64
65 static void
66 doc_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type);
67
68 static void
69 doc_optional_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type);
70
71 static cfg_type_t cfg_type_acl;
72 static cfg_type_t cfg_type_addrmatchelt;
73 static cfg_type_t cfg_type_bracketed_aml;
74 static cfg_type_t cfg_type_bracketed_namesockaddrkeylist;
75 static cfg_type_t cfg_type_bracketed_sockaddrlist;
76 static cfg_type_t cfg_type_bracketed_sockaddrnameportlist;
77 static cfg_type_t cfg_type_controls;
78 static cfg_type_t cfg_type_controls_sockaddr;
79 static cfg_type_t cfg_type_destinationlist;
80 static cfg_type_t cfg_type_dialuptype;
81 static cfg_type_t cfg_type_ixfrdifftype;
82 static cfg_type_t cfg_type_key;
83 static cfg_type_t cfg_type_logfile;
84 static cfg_type_t cfg_type_logging;
85 static cfg_type_t cfg_type_logseverity;
86 static cfg_type_t cfg_type_lwres;
87 static cfg_type_t cfg_type_masterselement;
88 static cfg_type_t cfg_type_nameportiplist;
89 static cfg_type_t cfg_type_negated;
90 static cfg_type_t cfg_type_notifytype;
91 static cfg_type_t cfg_type_optional_allow;
92 static cfg_type_t cfg_type_optional_class;
93 static cfg_type_t cfg_type_optional_facility;
94 static cfg_type_t cfg_type_optional_keyref;
95 static cfg_type_t cfg_type_optional_port;
96 static cfg_type_t cfg_type_options;
97 static cfg_type_t cfg_type_portiplist;
98 static cfg_type_t cfg_type_querysource4;
99 static cfg_type_t cfg_type_querysource6;
100 static cfg_type_t cfg_type_querysource;
101 static cfg_type_t cfg_type_server;
102 static cfg_type_t cfg_type_server_key_kludge;
103 static cfg_type_t cfg_type_size;
104 static cfg_type_t cfg_type_sizenodefault;
105 static cfg_type_t cfg_type_sockaddr4wild;
106 static cfg_type_t cfg_type_sockaddr6wild;
107 static cfg_type_t cfg_type_statschannels;
108 static cfg_type_t cfg_type_view;
109 static cfg_type_t cfg_type_viewopts;
110 static cfg_type_t cfg_type_zone;
111 static cfg_type_t cfg_type_zoneopts;
112 static cfg_type_t cfg_type_dynamically_loadable_zones;
113 static cfg_type_t cfg_type_dynamically_loadable_zones_opts;
114
115 /*
116  * Clauses that can be found in a 'dynamically loadable zones' statement
117  */
118 static cfg_clausedef_t
119 dynamically_loadable_zones_clauses[] = {
120         { "database", &cfg_type_astring, 0 },
121         { NULL, NULL, 0 }
122 };
123
124 /*
125  * A dynamically loadable zones statement.
126  */
127 static cfg_tuplefielddef_t dynamically_loadable_zones_fields[] = {
128         { "name", &cfg_type_astring, 0 },
129         { "options", &cfg_type_dynamically_loadable_zones_opts, 0 },
130         { NULL, NULL, 0 }
131 };
132
133 static cfg_type_t cfg_type_dynamically_loadable_zones = {
134         "dlz", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
135         &cfg_rep_tuple,
136         dynamically_loadable_zones_fields
137         };
138
139
140 /*% tkey-dhkey */
141
142 static cfg_tuplefielddef_t tkey_dhkey_fields[] = {
143         { "name", &cfg_type_qstring, 0 },
144         { "keyid", &cfg_type_uint32, 0 },
145         { NULL, NULL, 0 }
146 };
147
148 static cfg_type_t cfg_type_tkey_dhkey = {
149         "tkey-dhkey", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
150         tkey_dhkey_fields
151 };
152
153 /*% listen-on */
154
155 static cfg_tuplefielddef_t listenon_fields[] = {
156         { "port", &cfg_type_optional_port, 0 },
157         { "acl", &cfg_type_bracketed_aml, 0 },
158         { NULL, NULL, 0 }
159 };
160 static cfg_type_t cfg_type_listenon = {
161         "listenon", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, listenon_fields };
162
163 /*% acl */
164
165 static cfg_tuplefielddef_t acl_fields[] = {
166         { "name", &cfg_type_astring, 0 },
167         { "value", &cfg_type_bracketed_aml, 0 },
168         { NULL, NULL, 0 }
169 };
170
171 static cfg_type_t cfg_type_acl = {
172         "acl", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, acl_fields };
173
174 /*% masters */
175 static cfg_tuplefielddef_t masters_fields[] = {
176         { "name", &cfg_type_astring, 0 },
177         { "port", &cfg_type_optional_port, 0 },
178         { "addresses", &cfg_type_bracketed_namesockaddrkeylist, 0 },
179         { NULL, NULL, 0 }
180 };
181
182 static cfg_type_t cfg_type_masters = {
183         "masters", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, masters_fields };
184
185 /*%
186  * "sockaddrkeylist", a list of socket addresses with optional keys
187  * and an optional default port, as used in the masters option.
188  * E.g.,
189  *   "port 1234 { mymasters; 10.0.0.1 key foo; 1::2 port 69; }"
190  */
191
192 static cfg_tuplefielddef_t namesockaddrkey_fields[] = {
193         { "masterselement", &cfg_type_masterselement, 0 },
194         { "key", &cfg_type_optional_keyref, 0 },
195         { NULL, NULL, 0 },
196 };
197
198 static cfg_type_t cfg_type_namesockaddrkey = {
199         "namesockaddrkey", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
200         namesockaddrkey_fields
201 };
202
203 static cfg_type_t cfg_type_bracketed_namesockaddrkeylist = {
204         "bracketed_namesockaddrkeylist", cfg_parse_bracketed_list,
205         cfg_print_bracketed_list, cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_namesockaddrkey
206 };
207
208 static cfg_tuplefielddef_t namesockaddrkeylist_fields[] = {
209         { "port", &cfg_type_optional_port, 0 },
210         { "addresses", &cfg_type_bracketed_namesockaddrkeylist, 0 },
211         { NULL, NULL, 0 }
212 };
213 static cfg_type_t cfg_type_namesockaddrkeylist = {
214         "sockaddrkeylist", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
215         namesockaddrkeylist_fields
216 };
217
218 /*%
219  * A list of socket addresses with an optional default port,
220  * as used in the also-notify option.  E.g.,
221  * "port 1234 { 10.0.0.1; 1::2 port 69; }"
222  */
223 static cfg_tuplefielddef_t portiplist_fields[] = {
224         { "port", &cfg_type_optional_port, 0 },
225         { "addresses", &cfg_type_bracketed_sockaddrlist, 0 },
226         { NULL, NULL, 0 }
227 };
228 static cfg_type_t cfg_type_portiplist = {
229         "portiplist", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
230         portiplist_fields
231 };
232
233 /*%
234  * A public key, as in the "pubkey" statement.
235  */
236 static cfg_tuplefielddef_t pubkey_fields[] = {
237         { "flags", &cfg_type_uint32, 0 },
238         { "protocol", &cfg_type_uint32, 0 },
239         { "algorithm", &cfg_type_uint32, 0 },
240         { "key", &cfg_type_qstring, 0 },
241         { NULL, NULL, 0 }
242 };
243 static cfg_type_t cfg_type_pubkey = {
244         "pubkey", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, pubkey_fields };
245
246 /*%
247  * A list of RR types, used in grant statements.
248  * Note that the old parser allows quotes around the RR type names.
249  */
250 static cfg_type_t cfg_type_rrtypelist = {
251         "rrtypelist", cfg_parse_spacelist, cfg_print_spacelist, cfg_doc_terminal,
252         &cfg_rep_list, &cfg_type_astring
253 };
254
255 static const char *mode_enums[] = { "grant", "deny", NULL };
256 static cfg_type_t cfg_type_mode = {
257         "mode", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
258         &mode_enums
259 };
260
261 static const char *matchtype_enums[] = {
262         "name", "subdomain", "wildcard", "self", "selfsub", "selfwild",
263         "krb5-self", "ms-self", "krb5-subdomain", "ms-subdomain", NULL };
264 static cfg_type_t cfg_type_matchtype = {
265         "matchtype", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
266         &matchtype_enums
267 };
268
269 /*%
270  * A grant statement, used in the update policy.
271  */
272 static cfg_tuplefielddef_t grant_fields[] = {
273         { "mode", &cfg_type_mode, 0 },
274         { "identity", &cfg_type_astring, 0 }, /* domain name */
275         { "matchtype", &cfg_type_matchtype, 0 },
276         { "name", &cfg_type_astring, 0 }, /* domain name */
277         { "types", &cfg_type_rrtypelist, 0 },
278         { NULL, NULL, 0 }
279 };
280 static cfg_type_t cfg_type_grant = {
281         "grant", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, grant_fields };
282
283 static cfg_type_t cfg_type_updatepolicy = {
284         "update_policy", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
285         &cfg_rep_list, &cfg_type_grant
286 };
287
288 /*%
289  * A view statement.
290  */
291 static cfg_tuplefielddef_t view_fields[] = {
292         { "name", &cfg_type_astring, 0 },
293         { "class", &cfg_type_optional_class, 0 },
294         { "options", &cfg_type_viewopts, 0 },
295         { NULL, NULL, 0 }
296 };
297 static cfg_type_t cfg_type_view = {
298         "view", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, view_fields };
299
300 /*%
301  * A zone statement.
302  */
303 static cfg_tuplefielddef_t zone_fields[] = {
304         { "name", &cfg_type_astring, 0 },
305         { "class", &cfg_type_optional_class, 0 },
306         { "options", &cfg_type_zoneopts, 0 },
307         { NULL, NULL, 0 }
308 };
309 static cfg_type_t cfg_type_zone = {
310         "zone", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, zone_fields };
311
312 /*%
313  * A "category" clause in the "logging" statement.
314  */
315 static cfg_tuplefielddef_t category_fields[] = {
316         { "name", &cfg_type_astring, 0 },
317         { "destinations", &cfg_type_destinationlist,0 },
318         { NULL, NULL, 0 }
319 };
320 static cfg_type_t cfg_type_category = {
321         "category", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple, category_fields };
322
323
324 /*%
325  * A trusted key, as used in the "trusted-keys" statement.
326  */
327 static cfg_tuplefielddef_t trustedkey_fields[] = {
328         { "name", &cfg_type_astring, 0 },
329         { "flags", &cfg_type_uint32, 0 },
330         { "protocol", &cfg_type_uint32, 0 },
331         { "algorithm", &cfg_type_uint32, 0 },
332         { "key", &cfg_type_qstring, 0 },
333         { NULL, NULL, 0 }
334 };
335 static cfg_type_t cfg_type_trustedkey = {
336         "trustedkey", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
337         trustedkey_fields
338 };
339
340 static keyword_type_t wild_class_kw = { "class", &cfg_type_ustring };
341
342 static cfg_type_t cfg_type_optional_wild_class = {
343         "optional_wild_class", parse_optional_keyvalue, print_keyvalue,
344         doc_optional_keyvalue, &cfg_rep_string, &wild_class_kw
345 };
346
347 static keyword_type_t wild_type_kw = { "type", &cfg_type_ustring };
348
349 static cfg_type_t cfg_type_optional_wild_type = {
350         "optional_wild_type", parse_optional_keyvalue,
351         print_keyvalue, doc_optional_keyvalue, &cfg_rep_string, &wild_type_kw
352 };
353
354 static keyword_type_t wild_name_kw = { "name", &cfg_type_qstring };
355
356 static cfg_type_t cfg_type_optional_wild_name = {
357         "optional_wild_name", parse_optional_keyvalue,
358         print_keyvalue, doc_optional_keyvalue, &cfg_rep_string, &wild_name_kw
359 };
360
361 /*%
362  * An rrset ordering element.
363  */
364 static cfg_tuplefielddef_t rrsetorderingelement_fields[] = {
365         { "class", &cfg_type_optional_wild_class, 0 },
366         { "type", &cfg_type_optional_wild_type, 0 },
367         { "name", &cfg_type_optional_wild_name, 0 },
368         { "order", &cfg_type_ustring, 0 }, /* must be literal "order" */
369         { "ordering", &cfg_type_ustring, 0 },
370         { NULL, NULL, 0 }
371 };
372 static cfg_type_t cfg_type_rrsetorderingelement = {
373         "rrsetorderingelement", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
374         rrsetorderingelement_fields
375 };
376
377 /*%
378  * A global or view "check-names" option.  Note that the zone
379  * "check-names" option has a different syntax.
380  */
381
382 static const char *checktype_enums[] = { "master", "slave", "response", NULL };
383 static cfg_type_t cfg_type_checktype = {
384         "checktype", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
385         &cfg_rep_string, &checktype_enums
386 };
387
388 static const char *checkmode_enums[] = { "fail", "warn", "ignore", NULL };
389 static cfg_type_t cfg_type_checkmode = {
390         "checkmode", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
391         &cfg_rep_string, &checkmode_enums
392 };
393
394 static cfg_tuplefielddef_t checknames_fields[] = {
395         { "type", &cfg_type_checktype, 0 },
396         { "mode", &cfg_type_checkmode, 0 },
397         { NULL, NULL, 0 }
398 };
399 static cfg_type_t cfg_type_checknames = {
400         "checknames", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
401         checknames_fields
402 };
403
404 static cfg_type_t cfg_type_bracketed_sockaddrlist = {
405         "bracketed_sockaddrlist", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
406         &cfg_rep_list, &cfg_type_sockaddr
407 };
408
409 static cfg_type_t cfg_type_rrsetorder = {
410         "rrsetorder", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
411         &cfg_rep_list, &cfg_type_rrsetorderingelement
412 };
413
414 static keyword_type_t port_kw = { "port", &cfg_type_uint32 };
415
416 static cfg_type_t cfg_type_optional_port = {
417         "optional_port", parse_optional_keyvalue, print_keyvalue,
418         doc_optional_keyvalue, &cfg_rep_uint32, &port_kw
419 };
420
421 /*% A list of keys, as in the "key" clause of the controls statement. */
422 static cfg_type_t cfg_type_keylist = {
423         "keylist", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list, &cfg_rep_list,
424         &cfg_type_astring
425 };
426
427 static cfg_type_t cfg_type_trustedkeys = {
428         "trusted-keys", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list, &cfg_rep_list,
429         &cfg_type_trustedkey
430 };
431
432 static const char *forwardtype_enums[] = { "first", "only", NULL };
433 static cfg_type_t cfg_type_forwardtype = {
434         "forwardtype", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
435         &forwardtype_enums
436 };
437
438 static const char *zonetype_enums[] = {
439         "master", "slave", "stub", "hint", "forward", "delegation-only", NULL };
440 static cfg_type_t cfg_type_zonetype = {
441         "zonetype", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
442         &cfg_rep_string, &zonetype_enums
443 };
444
445 static const char *loglevel_enums[] = {
446         "critical", "error", "warning", "notice", "info", "dynamic", NULL };
447 static cfg_type_t cfg_type_loglevel = {
448         "loglevel", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
449         &loglevel_enums
450 };
451
452 static const char *transferformat_enums[] = {
453         "many-answers", "one-answer", NULL };
454 static cfg_type_t cfg_type_transferformat = {
455         "transferformat", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum, &cfg_rep_string,
456         &transferformat_enums
457 };
458
459 /*%
460  * The special keyword "none", as used in the pid-file option.
461  */
462
463 static void
464 print_none(cfg_printer_t *pctx, const cfg_obj_t *obj) {
465         UNUSED(obj);
466         cfg_print_chars(pctx, "none", 4);
467 }
468
469 static cfg_type_t cfg_type_none = {
470         "none", NULL, print_none, NULL, &cfg_rep_void, NULL
471 };
472
473 /*%
474  * A quoted string or the special keyword "none".  Used in the pid-file option.
475  */
476 static isc_result_t
477 parse_qstringornone(cfg_parser_t *pctx, const cfg_type_t *type,
478                     cfg_obj_t **ret)
479 {
480         isc_result_t result;
481         CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
482         if (pctx->token.type == isc_tokentype_string &&
483             strcasecmp(TOKEN_STRING(pctx), "none") == 0)
484                 return (cfg_create_obj(pctx, &cfg_type_none, ret));
485         cfg_ungettoken(pctx);
486         return (cfg_parse_qstring(pctx, type, ret));
487  cleanup:
488         return (result);
489 }
490
491 static void
492 doc_qstringornone(cfg_printer_t *pctx, const cfg_type_t *type) {
493         UNUSED(type);
494         cfg_print_chars(pctx, "( <quoted_string> | none )", 26);
495 }
496
497 static cfg_type_t cfg_type_qstringornone = {
498         "qstringornone", parse_qstringornone, NULL, doc_qstringornone, NULL, NULL };
499
500 /*%
501  * keyword hostname
502  */
503
504 static void
505 print_hostname(cfg_printer_t *pctx, const cfg_obj_t *obj) {
506         UNUSED(obj);
507         cfg_print_chars(pctx, "hostname", 4);
508 }
509
510 static cfg_type_t cfg_type_hostname = {
511         "hostname", NULL, print_hostname, NULL, &cfg_rep_boolean, NULL
512 };
513
514 /*%
515  * "server-id" argument.
516  */
517
518 static isc_result_t
519 parse_serverid(cfg_parser_t *pctx, const cfg_type_t *type,
520                     cfg_obj_t **ret)
521 {
522         isc_result_t result;
523         CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
524         if (pctx->token.type == isc_tokentype_string &&
525             strcasecmp(TOKEN_STRING(pctx), "none") == 0)
526                 return (cfg_create_obj(pctx, &cfg_type_none, ret));
527         if (pctx->token.type == isc_tokentype_string &&
528             strcasecmp(TOKEN_STRING(pctx), "hostname") == 0) {
529                 return (cfg_create_obj(pctx, &cfg_type_hostname, ret));
530         }
531         cfg_ungettoken(pctx);
532         return (cfg_parse_qstring(pctx, type, ret));
533  cleanup:
534         return (result);
535 }
536
537 static void
538 doc_serverid(cfg_printer_t *pctx, const cfg_type_t *type) {
539         UNUSED(type);
540         cfg_print_chars(pctx, "( <quoted_string> | none | hostname )", 26);
541 }
542
543 static cfg_type_t cfg_type_serverid = {
544         "serverid", parse_serverid, NULL, doc_serverid, NULL, NULL };
545
546 /*%
547  * Port list.
548  */
549 static cfg_tuplefielddef_t porttuple_fields[] = {
550         { "loport", &cfg_type_uint32, 0 },
551         { "hiport", &cfg_type_uint32, 0 },
552         { NULL, NULL, 0 }
553 };
554 static cfg_type_t cfg_type_porttuple = {
555         "porttuple", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
556         &cfg_rep_tuple, porttuple_fields
557 };
558
559 static isc_result_t
560 parse_port(cfg_parser_t *pctx, cfg_obj_t **ret) {
561         isc_result_t result;
562
563         CHECK(cfg_parse_uint32(pctx, NULL, ret));
564         if ((*ret)->value.uint32 > 0xffff) {
565                 cfg_parser_error(pctx, CFG_LOG_NEAR, "invalid port");
566                 cfg_obj_destroy(pctx, ret);
567                 result = ISC_R_RANGE;
568         }
569
570  cleanup:
571         return (result);
572 }
573
574 static isc_result_t
575 parse_portrange(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
576         isc_result_t result;
577         cfg_obj_t *obj = NULL;
578
579         UNUSED(type);
580
581         CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER | ISC_LEXOPT_CNUMBER));
582         if (pctx->token.type == isc_tokentype_number)
583                 CHECK(parse_port(pctx, ret));
584         else {
585                 CHECK(cfg_gettoken(pctx, 0));
586                 if (pctx->token.type != isc_tokentype_string ||
587                     strcasecmp(TOKEN_STRING(pctx), "range") != 0) {
588                         cfg_parser_error(pctx, CFG_LOG_NEAR,
589                                          "expected integer or 'range'");
590                         return (ISC_R_UNEXPECTEDTOKEN);
591                 }
592                 CHECK(cfg_create_tuple(pctx, &cfg_type_porttuple, &obj));
593                 CHECK(parse_port(pctx, &obj->value.tuple[0]));
594                 CHECK(parse_port(pctx, &obj->value.tuple[1]));
595                 if (obj->value.tuple[0]->value.uint32 >
596                     obj->value.tuple[1]->value.uint32) {
597                         cfg_parser_error(pctx, CFG_LOG_NOPREP,
598                                          "low port '%u' must not be larger "
599                                          "than high port",
600                                          obj->value.tuple[0]->value.uint32);
601                         result = ISC_R_RANGE;
602                         goto cleanup;
603                 }
604                 *ret = obj;
605                 obj = NULL;
606         }
607
608  cleanup:
609         if (obj != NULL)
610                 cfg_obj_destroy(pctx, &obj);
611         return (result);
612 }
613
614 static cfg_type_t cfg_type_portrange = {
615         "portrange", parse_portrange, NULL, cfg_doc_terminal,
616         NULL, NULL
617 };
618
619 static cfg_type_t cfg_type_bracketed_portlist = {
620         "bracketed_sockaddrlist", cfg_parse_bracketed_list,
621         cfg_print_bracketed_list, cfg_doc_bracketed_list,
622         &cfg_rep_list, &cfg_type_portrange
623 };
624
625 /*%
626  * Clauses that can be found within the top level of the named.conf
627  * file only.
628  */
629 static cfg_clausedef_t
630 namedconf_clauses[] = {
631         { "options", &cfg_type_options, 0 },
632         { "controls", &cfg_type_controls, CFG_CLAUSEFLAG_MULTI },
633         { "acl", &cfg_type_acl, CFG_CLAUSEFLAG_MULTI },
634         { "masters", &cfg_type_masters, CFG_CLAUSEFLAG_MULTI },
635         { "logging", &cfg_type_logging, 0 },
636         { "view", &cfg_type_view, CFG_CLAUSEFLAG_MULTI },
637         { "lwres", &cfg_type_lwres, CFG_CLAUSEFLAG_MULTI },
638         { "statistics-channels", &cfg_type_statschannels,
639           CFG_CLAUSEFLAG_MULTI },
640         { NULL, NULL, 0 }
641 };
642
643 /*%
644  * Clauses that can occur at the top level or in the view
645  * statement, but not in the options block.
646  */
647 static cfg_clausedef_t
648 namedconf_or_view_clauses[] = {
649         { "key", &cfg_type_key, CFG_CLAUSEFLAG_MULTI },
650         { "zone", &cfg_type_zone, CFG_CLAUSEFLAG_MULTI },
651         /* only 1 DLZ per view allowed */
652         { "dlz", &cfg_type_dynamically_loadable_zones, 0 },
653         { "server", &cfg_type_server, CFG_CLAUSEFLAG_MULTI },
654         { "trusted-keys", &cfg_type_trustedkeys, CFG_CLAUSEFLAG_MULTI },
655         { NULL, NULL, 0 }
656 };
657
658 /*%
659  * Clauses that can be found within the 'options' statement.
660  */
661 static cfg_clausedef_t
662 options_clauses[] = {
663         { "use-v4-udp-ports", &cfg_type_bracketed_portlist, 0 },
664         { "use-v6-udp-ports", &cfg_type_bracketed_portlist, 0 },
665         { "avoid-v4-udp-ports", &cfg_type_bracketed_portlist, 0 },
666         { "avoid-v6-udp-ports", &cfg_type_bracketed_portlist, 0 },
667         { "blackhole", &cfg_type_bracketed_aml, 0 },
668         { "coresize", &cfg_type_size, 0 },
669         { "datasize", &cfg_type_size, 0 },
670         { "deallocate-on-exit", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
671         { "directory", &cfg_type_qstring, CFG_CLAUSEFLAG_CALLBACK },
672         { "dump-file", &cfg_type_qstring, 0 },
673         { "fake-iquery", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
674         { "files", &cfg_type_size, 0 },
675         { "has-old-clients", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
676         { "heartbeat-interval", &cfg_type_uint32, 0 },
677         { "host-statistics", &cfg_type_boolean, CFG_CLAUSEFLAG_NOTIMP },
678         { "host-statistics-max", &cfg_type_uint32, CFG_CLAUSEFLAG_NOTIMP },
679         { "hostname", &cfg_type_qstringornone, 0 },
680         { "interface-interval", &cfg_type_uint32, 0 },
681         { "listen-on", &cfg_type_listenon, CFG_CLAUSEFLAG_MULTI },
682         { "listen-on-v6", &cfg_type_listenon, CFG_CLAUSEFLAG_MULTI },
683         { "match-mapped-addresses", &cfg_type_boolean, 0 },
684         { "memstatistics-file", &cfg_type_qstring, 0 },
685         { "memstatistics", &cfg_type_boolean, 0 },
686         { "multiple-cnames", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
687         { "named-xfer", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
688         { "pid-file", &cfg_type_qstringornone, 0 },
689         { "port", &cfg_type_uint32, 0 },
690         { "querylog", &cfg_type_boolean, 0 },
691         { "recursing-file", &cfg_type_qstring, 0 },
692         { "random-device", &cfg_type_qstring, 0 },
693         { "recursive-clients", &cfg_type_uint32, 0 },
694         { "reserved-sockets", &cfg_type_uint32, 0 },
695         { "serial-queries", &cfg_type_uint32, CFG_CLAUSEFLAG_OBSOLETE },
696         { "serial-query-rate", &cfg_type_uint32, 0 },
697         { "server-id", &cfg_type_serverid, 0 },
698         { "stacksize", &cfg_type_size, 0 },
699         { "statistics-file", &cfg_type_qstring, 0 },
700         { "statistics-interval", &cfg_type_uint32, CFG_CLAUSEFLAG_NYI },
701         { "tcp-clients", &cfg_type_uint32, 0 },
702         { "tcp-listen-queue", &cfg_type_uint32, 0 },
703         { "tkey-dhkey", &cfg_type_tkey_dhkey, 0 },
704         { "tkey-gssapi-credential", &cfg_type_qstring, 0 },
705         { "tkey-domain", &cfg_type_qstring, 0 },
706         { "transfers-per-ns", &cfg_type_uint32, 0 },
707         { "transfers-in", &cfg_type_uint32, 0 },
708         { "transfers-out", &cfg_type_uint32, 0 },
709         { "treat-cr-as-space", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
710         { "use-id-pool", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
711         { "use-ixfr", &cfg_type_boolean, 0 },
712         { "version", &cfg_type_qstringornone, 0 },
713         { "flush-zones-on-shutdown", &cfg_type_boolean, 0 },
714         { NULL, NULL, 0 }
715 };
716
717
718 static cfg_type_t cfg_type_namelist = {
719         "namelist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
720         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_qstring };
721
722 static keyword_type_t exclude_kw = { "exclude", &cfg_type_namelist };
723
724 static cfg_type_t cfg_type_optional_exclude = {
725         "optional_exclude", parse_optional_keyvalue, print_keyvalue,
726         doc_optional_keyvalue, &cfg_rep_list, &exclude_kw };
727
728 static cfg_type_t cfg_type_algorithmlist = {
729         "algorithmlist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
730         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_astring };
731
732 static cfg_tuplefielddef_t disablealgorithm_fields[] = {
733         { "name", &cfg_type_astring, 0 },
734         { "algorithms", &cfg_type_algorithmlist, 0 },
735         { NULL, NULL, 0 }
736 };
737
738 static cfg_type_t cfg_type_disablealgorithm = {
739         "disablealgorithm", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
740         &cfg_rep_tuple, disablealgorithm_fields
741 };
742
743 static cfg_tuplefielddef_t mustbesecure_fields[] = {
744         { "name", &cfg_type_astring, 0 },
745         { "value", &cfg_type_boolean, 0 },
746         { NULL, NULL, 0 }
747 };
748
749 static cfg_type_t cfg_type_mustbesecure = {
750         "mustbesecure", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
751         &cfg_rep_tuple, mustbesecure_fields
752 };
753
754 static const char *masterformat_enums[] = { "text", "raw", NULL };
755 static cfg_type_t cfg_type_masterformat = {
756         "masterformat", cfg_parse_enum, cfg_print_ustring, cfg_doc_enum,
757         &cfg_rep_string, &masterformat_enums
758 };
759
760 /*%
761  * dnssec-lookaside
762  */
763
764 static keyword_type_t trustanchor_kw = { "trust-anchor", &cfg_type_astring };
765
766 static cfg_type_t cfg_type_trustanchor = {
767         "trust-anchor", parse_keyvalue, print_keyvalue, doc_keyvalue,
768         &cfg_rep_string, &trustanchor_kw
769 };
770
771 static cfg_tuplefielddef_t lookaside_fields[] = {
772         { "domain", &cfg_type_astring, 0 },
773         { "trust-anchor", &cfg_type_trustanchor, 0 },
774         { NULL, NULL, 0 }
775 };
776
777 static cfg_type_t cfg_type_lookaside = {
778         "lookaside", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
779         &cfg_rep_tuple, lookaside_fields
780 };
781
782 /*%
783  * Clauses that can be found within the 'view' statement,
784  * with defaults in the 'options' statement.
785  */
786
787 static cfg_clausedef_t
788 view_clauses[] = {
789         { "acache-cleaning-interval", &cfg_type_uint32, 0 },
790         { "acache-enable", &cfg_type_boolean, 0 },
791         { "additional-from-auth", &cfg_type_boolean, 0 },
792         { "additional-from-cache", &cfg_type_boolean, 0 },
793         { "allow-query-cache", &cfg_type_bracketed_aml, 0 },
794         { "allow-query-cache-on", &cfg_type_bracketed_aml, 0 },
795         { "allow-recursion", &cfg_type_bracketed_aml, 0 },
796         { "allow-recursion-on", &cfg_type_bracketed_aml, 0 },
797         { "allow-v6-synthesis", &cfg_type_bracketed_aml,
798           CFG_CLAUSEFLAG_OBSOLETE },
799         { "auth-nxdomain", &cfg_type_boolean, CFG_CLAUSEFLAG_NEWDEFAULT },
800         { "cache-file", &cfg_type_qstring, 0 },
801         { "check-names", &cfg_type_checknames, CFG_CLAUSEFLAG_MULTI },
802         { "cleaning-interval", &cfg_type_uint32, 0 },
803         { "clients-per-query", &cfg_type_uint32, 0 },
804         { "disable-algorithms", &cfg_type_disablealgorithm,
805           CFG_CLAUSEFLAG_MULTI },
806         { "disable-empty-zone", &cfg_type_astring, CFG_CLAUSEFLAG_MULTI },
807         { "dnssec-accept-expired", &cfg_type_boolean, 0 },
808         { "dnssec-enable", &cfg_type_boolean, 0 },
809         { "dnssec-lookaside", &cfg_type_lookaside, CFG_CLAUSEFLAG_MULTI },
810         { "dnssec-must-be-secure",  &cfg_type_mustbesecure,
811           CFG_CLAUSEFLAG_MULTI },
812         { "dnssec-validation", &cfg_type_boolean, 0 },
813         { "dual-stack-servers", &cfg_type_nameportiplist, 0 },
814         { "edns-udp-size", &cfg_type_uint32, 0 },
815         { "empty-contact", &cfg_type_astring, 0 },
816         { "empty-server", &cfg_type_astring, 0 },
817         { "empty-zones-enable", &cfg_type_boolean, 0 },
818         { "fetch-glue", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
819         { "ixfr-from-differences", &cfg_type_ixfrdifftype, 0 },
820         { "lame-ttl", &cfg_type_uint32, 0 },
821         { "max-acache-size", &cfg_type_sizenodefault, 0 },
822         { "max-cache-size", &cfg_type_sizenodefault, 0 },
823         { "max-cache-ttl", &cfg_type_uint32, 0 },
824         { "max-clients-per-query", &cfg_type_uint32, 0 },
825         { "max-ncache-ttl", &cfg_type_uint32, 0 },
826         { "max-udp-size", &cfg_type_uint32, 0 },
827         { "min-roots", &cfg_type_uint32, CFG_CLAUSEFLAG_NOTIMP },
828         { "minimal-responses", &cfg_type_boolean, 0 },
829         { "preferred-glue", &cfg_type_astring, 0 },
830         { "provide-ixfr", &cfg_type_boolean, 0 },
831         /*
832          * Note that the query-source option syntax is different
833          * from the other -source options.
834          */
835         { "query-source", &cfg_type_querysource4, 0 },
836         { "query-source-v6", &cfg_type_querysource6, 0 },
837         { "queryport-pool-ports", &cfg_type_uint32, CFG_CLAUSEFLAG_OBSOLETE },
838         { "queryport-pool-updateinterval", &cfg_type_uint32,
839           CFG_CLAUSEFLAG_OBSOLETE},
840         { "recursion", &cfg_type_boolean, 0 },
841         { "request-ixfr", &cfg_type_boolean, 0 },
842         { "request-nsid", &cfg_type_boolean, 0 },
843         { "rfc2308-type1", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
844         { "root-delegation-only",  &cfg_type_optional_exclude, 0 },
845         { "rrset-order", &cfg_type_rrsetorder, 0 },
846         { "sortlist", &cfg_type_bracketed_aml, 0 },
847         { "suppress-initial-notify", &cfg_type_boolean, CFG_CLAUSEFLAG_NYI },
848         { "topology", &cfg_type_bracketed_aml, CFG_CLAUSEFLAG_NOTIMP },
849         { "transfer-format", &cfg_type_transferformat, 0 },
850         { "use-queryport-pool", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
851         { "zero-no-soa-ttl-cache", &cfg_type_boolean, 0 },
852         { NULL, NULL, 0 }
853 };
854
855 /*%
856  * Clauses that can be found within the 'view' statement only.
857  */
858 static cfg_clausedef_t
859 view_only_clauses[] = {
860         { "match-clients", &cfg_type_bracketed_aml, 0 },
861         { "match-destinations", &cfg_type_bracketed_aml, 0 },
862         { "match-recursive-only", &cfg_type_boolean, 0 },
863         { NULL, NULL, 0 }
864 };
865
866 /*%
867  * Clauses that can be found in a 'zone' statement,
868  * with defaults in the 'view' or 'options' statement.
869  */
870 static cfg_clausedef_t
871 zone_clauses[] = {
872         { "allow-notify", &cfg_type_bracketed_aml, 0 },
873         { "allow-query", &cfg_type_bracketed_aml, 0 },
874         { "allow-query-on", &cfg_type_bracketed_aml, 0 },
875         { "allow-transfer", &cfg_type_bracketed_aml, 0 },
876         { "allow-update", &cfg_type_bracketed_aml, 0 },
877         { "allow-update-forwarding", &cfg_type_bracketed_aml, 0 },
878         { "also-notify", &cfg_type_portiplist, 0 },
879         { "alt-transfer-source", &cfg_type_sockaddr4wild, 0 },
880         { "alt-transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
881         { "check-integrity", &cfg_type_boolean, 0 },
882         { "check-mx", &cfg_type_checkmode, 0 },
883         { "check-mx-cname", &cfg_type_checkmode, 0 },
884         { "check-sibling", &cfg_type_boolean, 0 },
885         { "check-srv-cname", &cfg_type_checkmode, 0 },
886         { "check-wildcard", &cfg_type_boolean, 0 },
887         { "dialup", &cfg_type_dialuptype, 0 },
888         { "forward", &cfg_type_forwardtype, 0 },
889         { "forwarders", &cfg_type_portiplist, 0 },
890         { "key-directory", &cfg_type_qstring, 0 },
891         { "maintain-ixfr-base", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
892         { "masterfile-format", &cfg_type_masterformat, 0 },
893         { "max-ixfr-log-size", &cfg_type_size, CFG_CLAUSEFLAG_OBSOLETE },
894         { "max-journal-size", &cfg_type_sizenodefault, 0 },
895         { "max-refresh-time", &cfg_type_uint32, 0 },
896         { "max-retry-time", &cfg_type_uint32, 0 },
897         { "max-transfer-idle-in", &cfg_type_uint32, 0 },
898         { "max-transfer-idle-out", &cfg_type_uint32, 0 },
899         { "max-transfer-time-in", &cfg_type_uint32, 0 },
900         { "max-transfer-time-out", &cfg_type_uint32, 0 },
901         { "min-refresh-time", &cfg_type_uint32, 0 },
902         { "min-retry-time", &cfg_type_uint32, 0 },
903         { "multi-master", &cfg_type_boolean, 0 },
904         { "notify", &cfg_type_notifytype, 0 },
905         { "notify-delay", &cfg_type_uint32, 0 },
906         { "notify-source", &cfg_type_sockaddr4wild, 0 },
907         { "notify-source-v6", &cfg_type_sockaddr6wild, 0 },
908         { "notify-to-soa", &cfg_type_boolean, 0 },
909         { "sig-validity-interval", &cfg_type_uint32, 0 },
910         { "transfer-source", &cfg_type_sockaddr4wild, 0 },
911         { "transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
912         { "try-tcp-refresh", &cfg_type_boolean, 0 },
913         { "update-check-ksk", &cfg_type_boolean, 0 },
914         { "use-alt-transfer-source", &cfg_type_boolean, 0 },
915         { "zero-no-soa-ttl", &cfg_type_boolean, 0 },
916         { "zone-statistics", &cfg_type_boolean, 0 },
917         { NULL, NULL, 0 }
918 };
919
920 /*%
921  * Clauses that can be found in a 'zone' statement
922  * only.
923  */
924 static cfg_clausedef_t
925 zone_only_clauses[] = {
926         { "type", &cfg_type_zonetype, 0 },
927         { "file", &cfg_type_qstring, 0 },
928         { "journal", &cfg_type_qstring, 0 },
929         { "ixfr-base", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
930         { "ixfr-tmp-file", &cfg_type_qstring, CFG_CLAUSEFLAG_OBSOLETE },
931         { "masters", &cfg_type_namesockaddrkeylist, 0 },
932         { "pubkey", &cfg_type_pubkey,
933           CFG_CLAUSEFLAG_MULTI | CFG_CLAUSEFLAG_OBSOLETE },
934         { "update-policy", &cfg_type_updatepolicy, 0 },
935         { "database", &cfg_type_astring, 0 },
936         { "delegation-only", &cfg_type_boolean, 0 },
937         /*
938          * Note that the format of the check-names option is different between
939          * the zone options and the global/view options.  Ugh.
940          */
941         { "check-names", &cfg_type_checkmode, 0 },
942         { "ixfr-from-differences", &cfg_type_boolean, 0 },
943         { NULL, NULL, 0 }
944 };
945
946
947 /*% The top-level named.conf syntax. */
948
949 static cfg_clausedef_t *
950 namedconf_clausesets[] = {
951         namedconf_clauses,
952         namedconf_or_view_clauses,
953         NULL
954 };
955
956 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_namedconf = {
957         "namedconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
958         &cfg_rep_map, namedconf_clausesets
959 };
960
961 /*% The "options" statement syntax. */
962
963 static cfg_clausedef_t *
964 options_clausesets[] = {
965         options_clauses,
966         view_clauses,
967         zone_clauses,
968         NULL
969 };
970 static cfg_type_t cfg_type_options = {
971         "options", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, options_clausesets };
972
973 /*% The "view" statement syntax. */
974
975 static cfg_clausedef_t *
976 view_clausesets[] = {
977         view_only_clauses,
978         namedconf_or_view_clauses,
979         view_clauses,
980         zone_clauses,
981         dynamically_loadable_zones_clauses,
982         NULL
983 };
984 static cfg_type_t cfg_type_viewopts = {
985         "view", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, view_clausesets };
986
987 /*% The "zone" statement syntax. */
988
989 static cfg_clausedef_t *
990 zone_clausesets[] = {
991         zone_only_clauses,
992         zone_clauses,
993         NULL
994 };
995 static cfg_type_t cfg_type_zoneopts = {
996         "zoneopts", cfg_parse_map, cfg_print_map,
997         cfg_doc_map, &cfg_rep_map, zone_clausesets };
998
999 /*% The "dynamically loadable zones" statement syntax. */
1000
1001 static cfg_clausedef_t *
1002 dynamically_loadable_zones_clausesets[] = {
1003         dynamically_loadable_zones_clauses,
1004         NULL
1005 };
1006 static cfg_type_t cfg_type_dynamically_loadable_zones_opts = {
1007         "dynamically_loadable_zones_opts", cfg_parse_map,
1008         cfg_print_map, cfg_doc_map, &cfg_rep_map,
1009         dynamically_loadable_zones_clausesets
1010 };
1011
1012 /*%
1013  * Clauses that can be found within the 'key' statement.
1014  */
1015 static cfg_clausedef_t
1016 key_clauses[] = {
1017         { "algorithm", &cfg_type_astring, 0 },
1018         { "secret", &cfg_type_astring, 0 },
1019         { NULL, NULL, 0 }
1020 };
1021
1022 static cfg_clausedef_t *
1023 key_clausesets[] = {
1024         key_clauses,
1025         NULL
1026 };
1027 static cfg_type_t cfg_type_key = {
1028         "key", cfg_parse_named_map, cfg_print_map,
1029         cfg_doc_map, &cfg_rep_map, key_clausesets
1030 };
1031
1032
1033 /*%
1034  * Clauses that can be found in a 'server' statement.
1035  */
1036 static cfg_clausedef_t
1037 server_clauses[] = {
1038         { "bogus", &cfg_type_boolean, 0 },
1039         { "provide-ixfr", &cfg_type_boolean, 0 },
1040         { "request-ixfr", &cfg_type_boolean, 0 },
1041         { "support-ixfr", &cfg_type_boolean, CFG_CLAUSEFLAG_OBSOLETE },
1042         { "transfers", &cfg_type_uint32, 0 },
1043         { "transfer-format", &cfg_type_transferformat, 0 },
1044         { "keys", &cfg_type_server_key_kludge, 0 },
1045         { "edns", &cfg_type_boolean, 0 },
1046         { "edns-udp-size", &cfg_type_uint32, 0 },
1047         { "max-udp-size", &cfg_type_uint32, 0 },
1048         { "notify-source", &cfg_type_sockaddr4wild, 0 },
1049         { "notify-source-v6", &cfg_type_sockaddr6wild, 0 },
1050         { "query-source", &cfg_type_querysource4, 0 },
1051         { "query-source-v6", &cfg_type_querysource6, 0 },
1052         { "transfer-source", &cfg_type_sockaddr4wild, 0 },
1053         { "transfer-source-v6", &cfg_type_sockaddr6wild, 0 },
1054         { NULL, NULL, 0 }
1055 };
1056 static cfg_clausedef_t *
1057 server_clausesets[] = {
1058         server_clauses,
1059         NULL
1060 };
1061 static cfg_type_t cfg_type_server = {
1062         "server", cfg_parse_netprefix_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,
1063         server_clausesets
1064 };
1065
1066
1067 /*%
1068  * Clauses that can be found in a 'channel' clause in the
1069  * 'logging' statement.
1070  *
1071  * These have some additional constraints that need to be
1072  * checked after parsing:
1073  *  - There must exactly one of file/syslog/null/stderr
1074  *
1075  */
1076 static cfg_clausedef_t
1077 channel_clauses[] = {
1078         /* Destinations.  We no longer require these to be first. */
1079         { "file", &cfg_type_logfile, 0 },
1080         { "syslog", &cfg_type_optional_facility, 0 },
1081         { "null", &cfg_type_void, 0 },
1082         { "stderr", &cfg_type_void, 0 },
1083         /* Options.  We now accept these for the null channel, too. */
1084         { "severity", &cfg_type_logseverity, 0 },
1085         { "print-time", &cfg_type_boolean, 0 },
1086         { "print-severity", &cfg_type_boolean, 0 },
1087         { "print-category", &cfg_type_boolean, 0 },
1088         { NULL, NULL, 0 }
1089 };
1090 static cfg_clausedef_t *
1091 channel_clausesets[] = {
1092         channel_clauses,
1093         NULL
1094 };
1095 static cfg_type_t cfg_type_channel = {
1096         "channel", cfg_parse_named_map, cfg_print_map, cfg_doc_map,
1097         &cfg_rep_map, channel_clausesets
1098 };
1099
1100 /*% A list of log destination, used in the "category" clause. */
1101 static cfg_type_t cfg_type_destinationlist = {
1102         "destinationlist", cfg_parse_bracketed_list, cfg_print_bracketed_list, cfg_doc_bracketed_list,
1103         &cfg_rep_list, &cfg_type_astring };
1104
1105 /*%
1106  * Clauses that can be found in a 'logging' statement.
1107  */
1108 static cfg_clausedef_t
1109 logging_clauses[] = {
1110         { "channel", &cfg_type_channel, CFG_CLAUSEFLAG_MULTI },
1111         { "category", &cfg_type_category, CFG_CLAUSEFLAG_MULTI },
1112         { NULL, NULL, 0 }
1113 };
1114 static cfg_clausedef_t *
1115 logging_clausesets[] = {
1116         logging_clauses,
1117         NULL
1118 };
1119 static cfg_type_t cfg_type_logging = {
1120         "logging", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map, logging_clausesets };
1121
1122
1123 static isc_result_t
1124 parse_unitstring(char *str, isc_resourcevalue_t *valuep) {
1125         char *endp;
1126         unsigned int len;
1127         isc_uint64_t value;
1128         isc_uint64_t unit;
1129
1130         value = isc_string_touint64(str, &endp, 10);
1131         if (*endp == 0) {
1132                 *valuep = value;
1133                 return (ISC_R_SUCCESS);
1134         }
1135
1136         len = strlen(str);
1137         if (len < 2 || endp[1] != '\0')
1138                 return (ISC_R_FAILURE);
1139
1140         switch (str[len - 1]) {
1141         case 'k':
1142         case 'K':
1143                 unit = 1024;
1144                 break;
1145         case 'm':
1146         case 'M':
1147                 unit = 1024 * 1024;
1148                 break;
1149         case 'g':
1150         case 'G':
1151                 unit = 1024 * 1024 * 1024;
1152                 break;
1153         default:
1154                 return (ISC_R_FAILURE);
1155         }
1156         if (value > ISC_UINT64_MAX / unit)
1157                 return (ISC_R_FAILURE);
1158         *valuep = value * unit;
1159         return (ISC_R_SUCCESS);
1160 }
1161
1162 static isc_result_t
1163 parse_sizeval(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1164         isc_result_t result;
1165         cfg_obj_t *obj = NULL;
1166         isc_uint64_t val;
1167
1168         UNUSED(type);
1169
1170         CHECK(cfg_gettoken(pctx, 0));
1171         if (pctx->token.type != isc_tokentype_string) {
1172                 result = ISC_R_UNEXPECTEDTOKEN;
1173                 goto cleanup;
1174         }
1175         CHECK(parse_unitstring(TOKEN_STRING(pctx), &val));
1176
1177         CHECK(cfg_create_obj(pctx, &cfg_type_uint64, &obj));
1178         obj->value.uint64 = val;
1179         *ret = obj;
1180         return (ISC_R_SUCCESS);
1181
1182  cleanup:
1183         cfg_parser_error(pctx, CFG_LOG_NEAR, "expected integer and optional unit");
1184         return (result);
1185 }
1186
1187 /*%
1188  * A size value (number + optional unit).
1189  */
1190 static cfg_type_t cfg_type_sizeval = {
1191         "sizeval", parse_sizeval, cfg_print_uint64, cfg_doc_terminal,
1192         &cfg_rep_uint64, NULL };
1193
1194 /*%
1195  * A size, "unlimited", or "default".
1196  */
1197
1198 static isc_result_t
1199 parse_size(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1200         return (parse_enum_or_other(pctx, type, &cfg_type_sizeval, ret));
1201 }
1202
1203 static const char *size_enums[] = { "unlimited", "default", NULL };
1204 static cfg_type_t cfg_type_size = {
1205         "size", parse_size, cfg_print_ustring, cfg_doc_terminal,
1206         &cfg_rep_string, size_enums
1207 };
1208
1209 /*%
1210  * A size or "unlimited", but not "default".
1211  */
1212 static const char *sizenodefault_enums[] = { "unlimited", NULL };
1213 static cfg_type_t cfg_type_sizenodefault = {
1214         "size_no_default", parse_size, cfg_print_ustring, cfg_doc_terminal,
1215         &cfg_rep_string, sizenodefault_enums
1216 };
1217
1218 /*%
1219  * optional_keyvalue
1220  */
1221 static isc_result_t
1222 parse_maybe_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type,
1223                               isc_boolean_t optional, cfg_obj_t **ret)
1224 {
1225         isc_result_t result;
1226         cfg_obj_t *obj = NULL;
1227         const keyword_type_t *kw = type->of;
1228
1229         CHECK(cfg_peektoken(pctx, 0));
1230         if (pctx->token.type == isc_tokentype_string &&
1231             strcasecmp(TOKEN_STRING(pctx), kw->name) == 0) {
1232                 CHECK(cfg_gettoken(pctx, 0));
1233                 CHECK(kw->type->parse(pctx, kw->type, &obj));
1234                 obj->type = type; /* XXX kludge */
1235         } else {
1236                 if (optional) {
1237                         CHECK(cfg_parse_void(pctx, NULL, &obj));
1238                 } else {
1239                         cfg_parser_error(pctx, CFG_LOG_NEAR, "expected '%s'",
1240                                      kw->name);
1241                         result = ISC_R_UNEXPECTEDTOKEN;
1242                         goto cleanup;
1243                 }
1244         }
1245         *ret = obj;
1246  cleanup:
1247         return (result);
1248 }
1249
1250 static isc_result_t
1251 parse_enum_or_other(cfg_parser_t *pctx, const cfg_type_t *enumtype,
1252                     const cfg_type_t *othertype, cfg_obj_t **ret)
1253 {
1254         isc_result_t result;
1255         CHECK(cfg_peektoken(pctx, 0));
1256         if (pctx->token.type == isc_tokentype_string &&
1257             cfg_is_enum(TOKEN_STRING(pctx), enumtype->of)) {
1258                 CHECK(cfg_parse_enum(pctx, enumtype, ret));
1259         } else {
1260                 CHECK(cfg_parse_obj(pctx, othertype, ret));
1261         }
1262  cleanup:
1263         return (result);
1264 }
1265
1266 static void
1267 doc_enum_or_other(cfg_printer_t *pctx, const cfg_type_t *type) {
1268         cfg_doc_terminal(pctx, type);
1269 #if 0 /* XXX */
1270         cfg_print_chars(pctx, "( ", 2);...
1271 #endif
1272
1273 }
1274
1275 static isc_result_t
1276 parse_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1277         return (parse_maybe_optional_keyvalue(pctx, type, ISC_FALSE, ret));
1278 }
1279
1280 static isc_result_t
1281 parse_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1282         return (parse_maybe_optional_keyvalue(pctx, type, ISC_TRUE, ret));
1283 }
1284
1285 static void
1286 print_keyvalue(cfg_printer_t *pctx, const cfg_obj_t *obj) {
1287         const keyword_type_t *kw = obj->type->of;
1288         cfg_print_cstr(pctx, kw->name);
1289         cfg_print_chars(pctx, " ", 1);
1290         kw->type->print(pctx, obj);
1291 }
1292
1293 static void
1294 doc_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type) {
1295         const keyword_type_t *kw = type->of;
1296         cfg_print_cstr(pctx, kw->name);
1297         cfg_print_chars(pctx, " ", 1);
1298         cfg_doc_obj(pctx, kw->type);
1299 }
1300
1301 static void
1302 doc_optional_keyvalue(cfg_printer_t *pctx, const cfg_type_t *type) {
1303         const keyword_type_t *kw = type->of;
1304         cfg_print_chars(pctx, "[ ", 2);
1305         cfg_print_cstr(pctx, kw->name);
1306         cfg_print_chars(pctx, " ", 1);
1307         cfg_doc_obj(pctx, kw->type);
1308         cfg_print_chars(pctx, " ]", 2);
1309 }
1310
1311 static const char *dialup_enums[] = {
1312         "notify", "notify-passive", "refresh", "passive", NULL };
1313 static isc_result_t
1314 parse_dialup_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1315         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1316 }
1317 static cfg_type_t cfg_type_dialuptype = {
1318         "dialuptype", parse_dialup_type, cfg_print_ustring, doc_enum_or_other,
1319         &cfg_rep_string, dialup_enums
1320 };
1321
1322 static const char *notify_enums[] = { "explicit", "master-only", NULL };
1323 static isc_result_t
1324 parse_notify_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1325         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1326 }
1327 static cfg_type_t cfg_type_notifytype = {
1328         "notifytype", parse_notify_type, cfg_print_ustring, doc_enum_or_other,
1329         &cfg_rep_string, notify_enums,
1330 };
1331
1332 static const char *ixfrdiff_enums[] = { "master", "slave", NULL };
1333 static isc_result_t
1334 parse_ixfrdiff_type(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1335         return (parse_enum_or_other(pctx, type, &cfg_type_boolean, ret));
1336 }
1337 static cfg_type_t cfg_type_ixfrdifftype = {
1338         "ixfrdiff", parse_ixfrdiff_type, cfg_print_ustring, doc_enum_or_other,
1339         &cfg_rep_string, ixfrdiff_enums,
1340 };
1341
1342 static keyword_type_t key_kw = { "key", &cfg_type_astring };
1343
1344 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_keyref = {
1345         "keyref", parse_keyvalue, print_keyvalue, doc_keyvalue,
1346         &cfg_rep_string, &key_kw
1347 };
1348
1349 static cfg_type_t cfg_type_optional_keyref = {
1350         "optional_keyref", parse_optional_keyvalue, print_keyvalue,
1351         doc_optional_keyvalue, &cfg_rep_string, &key_kw
1352 };
1353
1354 /*%
1355  * A "controls" statement is represented as a map with the multivalued
1356  * "inet" and "unix" clauses.
1357  */
1358
1359 static keyword_type_t controls_allow_kw = {
1360         "allow", &cfg_type_bracketed_aml };
1361
1362 static cfg_type_t cfg_type_controls_allow = {
1363         "controls_allow", parse_keyvalue,
1364         print_keyvalue, doc_keyvalue,
1365         &cfg_rep_list, &controls_allow_kw
1366 };
1367
1368 static keyword_type_t controls_keys_kw = {
1369         "keys", &cfg_type_keylist };
1370
1371 static cfg_type_t cfg_type_controls_keys = {
1372         "controls_keys", parse_optional_keyvalue,
1373         print_keyvalue, doc_optional_keyvalue,
1374         &cfg_rep_list, &controls_keys_kw
1375 };
1376
1377 static cfg_tuplefielddef_t inetcontrol_fields[] = {
1378         { "address", &cfg_type_controls_sockaddr, 0 },
1379         { "allow", &cfg_type_controls_allow, 0 },
1380         { "keys", &cfg_type_controls_keys, 0 },
1381         { NULL, NULL, 0 }
1382 };
1383
1384 static cfg_type_t cfg_type_inetcontrol = {
1385         "inetcontrol", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
1386         inetcontrol_fields
1387 };
1388
1389 static keyword_type_t controls_perm_kw = {
1390         "perm", &cfg_type_uint32 };
1391
1392 static cfg_type_t cfg_type_controls_perm = {
1393         "controls_perm", parse_keyvalue,
1394         print_keyvalue, doc_keyvalue,
1395         &cfg_rep_uint32, &controls_perm_kw
1396 };
1397
1398 static keyword_type_t controls_owner_kw = {
1399         "owner", &cfg_type_uint32 };
1400
1401 static cfg_type_t cfg_type_controls_owner = {
1402         "controls_owner", parse_keyvalue,
1403         print_keyvalue, doc_keyvalue,
1404         &cfg_rep_uint32, &controls_owner_kw
1405 };
1406
1407 static keyword_type_t controls_group_kw = {
1408         "group", &cfg_type_uint32 };
1409
1410 static cfg_type_t cfg_type_controls_group = {
1411         "controls_allow", parse_keyvalue,
1412         print_keyvalue, doc_keyvalue,
1413         &cfg_rep_uint32, &controls_group_kw
1414 };
1415
1416 static cfg_tuplefielddef_t unixcontrol_fields[] = {
1417         { "path", &cfg_type_qstring, 0 },
1418         { "perm", &cfg_type_controls_perm, 0 },
1419         { "owner", &cfg_type_controls_owner, 0 },
1420         { "group", &cfg_type_controls_group, 0 },
1421         { "keys", &cfg_type_controls_keys, 0 },
1422         { NULL, NULL, 0 }
1423 };
1424
1425 static cfg_type_t cfg_type_unixcontrol = {
1426         "unixcontrol", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
1427         unixcontrol_fields
1428 };
1429
1430 static cfg_clausedef_t
1431 controls_clauses[] = {
1432         { "inet", &cfg_type_inetcontrol, CFG_CLAUSEFLAG_MULTI },
1433         { "unix", &cfg_type_unixcontrol, CFG_CLAUSEFLAG_MULTI },
1434         { NULL, NULL, 0 }
1435 };
1436
1437 static cfg_clausedef_t *
1438 controls_clausesets[] = {
1439         controls_clauses,
1440         NULL
1441 };
1442 static cfg_type_t cfg_type_controls = {
1443         "controls", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,    &controls_clausesets
1444 };
1445
1446 /*%
1447  * A "statistics-channels" statement is represented as a map with the
1448  * multivalued "inet" clauses.
1449  */
1450 static void
1451 doc_optional_bracketed_list(cfg_printer_t *pctx, const cfg_type_t *type) {
1452         const keyword_type_t *kw = type->of;
1453         cfg_print_chars(pctx, "[ ", 2);
1454         cfg_print_cstr(pctx, kw->name);
1455         cfg_print_chars(pctx, " ", 1);
1456         cfg_doc_obj(pctx, kw->type);
1457         cfg_print_chars(pctx, " ]", 2);
1458 }
1459
1460 static cfg_type_t cfg_type_optional_allow = {
1461         "optional_allow", parse_optional_keyvalue, print_keyvalue,
1462         doc_optional_bracketed_list, &cfg_rep_list, &controls_allow_kw
1463 };
1464
1465 static cfg_tuplefielddef_t statserver_fields[] = {
1466         { "address", &cfg_type_controls_sockaddr, 0 }, /* reuse controls def */
1467         { "allow", &cfg_type_optional_allow, 0 },
1468         { NULL, NULL, 0 }
1469 };
1470
1471 static cfg_type_t cfg_type_statschannel = {
1472         "statschannel", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
1473         &cfg_rep_tuple, statserver_fields
1474 };
1475
1476 static cfg_clausedef_t
1477 statservers_clauses[] = {
1478         { "inet", &cfg_type_statschannel, CFG_CLAUSEFLAG_MULTI },
1479         { NULL, NULL, 0 }
1480 };
1481
1482 static cfg_clausedef_t *
1483 statservers_clausesets[] = {
1484         statservers_clauses,
1485         NULL
1486 };
1487
1488 static cfg_type_t cfg_type_statschannels = {
1489         "statistics-channels", cfg_parse_map, cfg_print_map, cfg_doc_map,
1490         &cfg_rep_map,   &statservers_clausesets
1491 };
1492
1493 /*%
1494  * An optional class, as used in view and zone statements.
1495  */
1496 static isc_result_t
1497 parse_optional_class(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1498         isc_result_t result;
1499         UNUSED(type);
1500         CHECK(cfg_peektoken(pctx, 0));
1501         if (pctx->token.type == isc_tokentype_string)
1502                 CHECK(cfg_parse_obj(pctx, &cfg_type_ustring, ret));
1503         else
1504                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
1505  cleanup:
1506         return (result);
1507 }
1508
1509 static cfg_type_t cfg_type_optional_class = {
1510         "optional_class", parse_optional_class, NULL, cfg_doc_terminal,
1511         NULL, NULL
1512 };
1513
1514 static isc_result_t
1515 parse_querysource(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1516         isc_result_t result;
1517         cfg_obj_t *obj = NULL;
1518         isc_netaddr_t netaddr;
1519         in_port_t port;
1520         unsigned int have_address = 0;
1521         unsigned int have_port = 0;
1522         const unsigned int *flagp = type->of;
1523
1524         if ((*flagp & CFG_ADDR_V4OK) != 0)
1525                 isc_netaddr_any(&netaddr);
1526         else if ((*flagp & CFG_ADDR_V6OK) != 0)
1527                 isc_netaddr_any6(&netaddr);
1528         else
1529                 INSIST(0);
1530
1531         port = 0;
1532
1533         for (;;) {
1534                 CHECK(cfg_peektoken(pctx, 0));
1535                 if (pctx->token.type == isc_tokentype_string) {
1536                         if (strcasecmp(TOKEN_STRING(pctx),
1537                                        "address") == 0)
1538                         {
1539                                 /* read "address" */
1540                                 CHECK(cfg_gettoken(pctx, 0));
1541                                 CHECK(cfg_parse_rawaddr(pctx, *flagp,
1542                                                         &netaddr));
1543                                 have_address++;
1544                         } else if (strcasecmp(TOKEN_STRING(pctx), "port") == 0)
1545                         {
1546                                 /* read "port" */
1547                                 CHECK(cfg_gettoken(pctx, 0));
1548                                 CHECK(cfg_parse_rawport(pctx,
1549                                                         CFG_ADDR_WILDOK,
1550                                                         &port));
1551                                 have_port++;
1552                         } else if (have_port == 0 && have_address == 0) {
1553                                 return (cfg_parse_sockaddr(pctx, type, ret));
1554                         } else {
1555                                 cfg_parser_error(pctx, CFG_LOG_NEAR,
1556                                              "expected 'address' or 'port'");
1557                                 return (ISC_R_UNEXPECTEDTOKEN);
1558                         }
1559                 } else
1560                         break;
1561         }
1562         if (have_address > 1 || have_port > 1 ||
1563             have_address + have_port == 0) {
1564                 cfg_parser_error(pctx, 0, "expected one address and/or port");
1565                 return (ISC_R_UNEXPECTEDTOKEN);
1566         }
1567
1568         CHECK(cfg_create_obj(pctx, &cfg_type_querysource, &obj));
1569         isc_sockaddr_fromnetaddr(&obj->value.sockaddr, &netaddr, port);
1570         *ret = obj;
1571         return (ISC_R_SUCCESS);
1572
1573  cleanup:
1574         cfg_parser_error(pctx, CFG_LOG_NEAR, "invalid query source");
1575         CLEANUP_OBJ(obj);
1576         return (result);
1577 }
1578
1579 static void
1580 print_querysource(cfg_printer_t *pctx, const cfg_obj_t *obj) {
1581         isc_netaddr_t na;
1582         isc_netaddr_fromsockaddr(&na, &obj->value.sockaddr);
1583         cfg_print_chars(pctx, "address ", 8);
1584         cfg_print_rawaddr(pctx, &na);
1585         cfg_print_chars(pctx, " port ", 6);
1586         cfg_print_rawuint(pctx, isc_sockaddr_getport(&obj->value.sockaddr));
1587 }
1588
1589 static unsigned int sockaddr4wild_flags = CFG_ADDR_WILDOK | CFG_ADDR_V4OK;
1590 static unsigned int sockaddr6wild_flags = CFG_ADDR_WILDOK | CFG_ADDR_V6OK;
1591
1592 static cfg_type_t cfg_type_querysource4 = {
1593         "querysource4", parse_querysource, NULL, cfg_doc_terminal,
1594         NULL, &sockaddr4wild_flags
1595 };
1596
1597 static cfg_type_t cfg_type_querysource6 = {
1598         "querysource6", parse_querysource, NULL, cfg_doc_terminal,
1599         NULL, &sockaddr6wild_flags
1600 };
1601
1602 static cfg_type_t cfg_type_querysource = {
1603         "querysource", NULL, print_querysource, NULL, &cfg_rep_sockaddr, NULL
1604 };
1605
1606 /*% addrmatchelt */
1607
1608 static isc_result_t
1609 parse_addrmatchelt(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1610         isc_result_t result;
1611         UNUSED(type);
1612
1613         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
1614
1615         if (pctx->token.type == isc_tokentype_string ||
1616             pctx->token.type == isc_tokentype_qstring) {
1617                 if (pctx->token.type == isc_tokentype_string &&
1618                     (strcasecmp(TOKEN_STRING(pctx), "key") == 0)) {
1619                         CHECK(cfg_parse_obj(pctx, &cfg_type_keyref, ret));
1620                 } else {
1621                         if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK |
1622                                                   CFG_ADDR_V4PREFIXOK |
1623                                                   CFG_ADDR_V6OK))
1624                         {
1625                                 CHECK(cfg_parse_netprefix(pctx, NULL, ret));
1626                         } else {
1627                                 CHECK(cfg_parse_astring(pctx, NULL, ret));
1628                         }
1629                 }
1630         } else if (pctx->token.type == isc_tokentype_special) {
1631                 if (pctx->token.value.as_char == '{') {
1632                         /* Nested match list. */
1633                         CHECK(cfg_parse_obj(pctx, &cfg_type_bracketed_aml, ret));
1634                 } else if (pctx->token.value.as_char == '!') {
1635                         CHECK(cfg_gettoken(pctx, 0)); /* read "!" */
1636                         CHECK(cfg_parse_obj(pctx, &cfg_type_negated, ret));
1637                 } else {
1638                         goto bad;
1639                 }
1640         } else {
1641         bad:
1642                 cfg_parser_error(pctx, CFG_LOG_NEAR,
1643                              "expected IP match list element");
1644                 return (ISC_R_UNEXPECTEDTOKEN);
1645         }
1646  cleanup:
1647         return (result);
1648 }
1649
1650 /*%
1651  * A negated address match list element (like "! 10.0.0.1").
1652  * Somewhat sneakily, the caller is expected to parse the
1653  * "!", but not to print it.
1654  */
1655
1656 static cfg_tuplefielddef_t negated_fields[] = {
1657         { "value", &cfg_type_addrmatchelt, 0 },
1658         { NULL, NULL, 0 }
1659 };
1660
1661 static void
1662 print_negated(cfg_printer_t *pctx, const cfg_obj_t *obj) {
1663         cfg_print_chars(pctx, "!", 1);
1664         cfg_print_tuple(pctx, obj);
1665 }
1666
1667 static cfg_type_t cfg_type_negated = {
1668         "negated", cfg_parse_tuple, print_negated, NULL, &cfg_rep_tuple,
1669         &negated_fields
1670 };
1671
1672 /*% An address match list element */
1673
1674 static cfg_type_t cfg_type_addrmatchelt = {
1675         "address_match_element", parse_addrmatchelt, NULL, cfg_doc_terminal,
1676         NULL, NULL
1677 };
1678
1679 /*% A bracketed address match list */
1680
1681 static cfg_type_t cfg_type_bracketed_aml = {
1682         "bracketed_aml", cfg_parse_bracketed_list, cfg_print_bracketed_list,
1683         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_addrmatchelt
1684 };
1685
1686 /*%
1687  * The socket address syntax in the "controls" statement is silly.
1688  * It allows both socket address families, but also allows "*",
1689  * whis is gratuitously interpreted as the IPv4 wildcard address.
1690  */
1691 static unsigned int controls_sockaddr_flags =
1692         CFG_ADDR_V4OK | CFG_ADDR_V6OK | CFG_ADDR_WILDOK;
1693 static cfg_type_t cfg_type_controls_sockaddr = {
1694         "controls_sockaddr", cfg_parse_sockaddr, cfg_print_sockaddr,
1695         cfg_doc_sockaddr, &cfg_rep_sockaddr, &controls_sockaddr_flags
1696 };
1697
1698 /*%
1699  * Handle the special kludge syntax of the "keys" clause in the "server"
1700  * statement, which takes a single key with or without braces and semicolon.
1701  */
1702 static isc_result_t
1703 parse_server_key_kludge(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
1704 {
1705         isc_result_t result;
1706         isc_boolean_t braces = ISC_FALSE;
1707         UNUSED(type);
1708
1709         /* Allow opening brace. */
1710         CHECK(cfg_peektoken(pctx, 0));
1711         if (pctx->token.type == isc_tokentype_special &&
1712             pctx->token.value.as_char == '{') {
1713                 result = cfg_gettoken(pctx, 0);
1714                 braces = ISC_TRUE;
1715         }
1716
1717         CHECK(cfg_parse_obj(pctx, &cfg_type_astring, ret));
1718
1719         if (braces) {
1720                 /* Skip semicolon if present. */
1721                 CHECK(cfg_peektoken(pctx, 0));
1722                 if (pctx->token.type == isc_tokentype_special &&
1723                     pctx->token.value.as_char == ';')
1724                         CHECK(cfg_gettoken(pctx, 0));
1725
1726                 CHECK(cfg_parse_special(pctx, '}'));
1727         }
1728  cleanup:
1729         return (result);
1730 }
1731 static cfg_type_t cfg_type_server_key_kludge = {
1732         "server_key", parse_server_key_kludge, NULL, cfg_doc_terminal,
1733         NULL, NULL
1734 };
1735
1736
1737 /*%
1738  * An optional logging facility.
1739  */
1740
1741 static isc_result_t
1742 parse_optional_facility(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
1743 {
1744         isc_result_t result;
1745         UNUSED(type);
1746
1747         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
1748         if (pctx->token.type == isc_tokentype_string ||
1749             pctx->token.type == isc_tokentype_qstring) {
1750                 CHECK(cfg_parse_obj(pctx, &cfg_type_astring, ret));
1751         } else {
1752                 CHECK(cfg_parse_obj(pctx, &cfg_type_void, ret));
1753         }
1754  cleanup:
1755         return (result);
1756 }
1757
1758 static cfg_type_t cfg_type_optional_facility = {
1759         "optional_facility", parse_optional_facility, NULL, cfg_doc_terminal,
1760         NULL, NULL };
1761
1762
1763 /*%
1764  * A log severity.  Return as a string, except "debug N",
1765  * which is returned as a keyword object.
1766  */
1767
1768 static keyword_type_t debug_kw = { "debug", &cfg_type_uint32 };
1769 static cfg_type_t cfg_type_debuglevel = {
1770         "debuglevel", parse_keyvalue,
1771         print_keyvalue, doc_keyvalue,
1772         &cfg_rep_uint32, &debug_kw
1773 };
1774
1775 static isc_result_t
1776 parse_logseverity(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1777         isc_result_t result;
1778         UNUSED(type);
1779
1780         CHECK(cfg_peektoken(pctx, 0));
1781         if (pctx->token.type == isc_tokentype_string &&
1782             strcasecmp(TOKEN_STRING(pctx), "debug") == 0) {
1783                 CHECK(cfg_gettoken(pctx, 0)); /* read "debug" */
1784                 CHECK(cfg_peektoken(pctx, ISC_LEXOPT_NUMBER));
1785                 if (pctx->token.type == isc_tokentype_number) {
1786                         CHECK(cfg_parse_uint32(pctx, NULL, ret));
1787                 } else {
1788                         /*
1789                          * The debug level is optional and defaults to 1.
1790                          * This makes little sense, but we support it for
1791                          * compatibility with BIND 8.
1792                          */
1793                         CHECK(cfg_create_obj(pctx, &cfg_type_uint32, ret));
1794                         (*ret)->value.uint32 = 1;
1795                 }
1796                 (*ret)->type = &cfg_type_debuglevel; /* XXX kludge */
1797         } else {
1798                 CHECK(cfg_parse_obj(pctx, &cfg_type_loglevel, ret));
1799         }
1800  cleanup:
1801         return (result);
1802 }
1803
1804 static cfg_type_t cfg_type_logseverity = {
1805         "log_severity", parse_logseverity, NULL, cfg_doc_terminal,
1806         NULL, NULL };
1807
1808 /*%
1809  * The "file" clause of the "channel" statement.
1810  * This is yet another special case.
1811  */
1812
1813 static const char *logversions_enums[] = { "unlimited", NULL };
1814 static isc_result_t
1815 parse_logversions(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1816         return (parse_enum_or_other(pctx, type, &cfg_type_uint32, ret));
1817 }
1818
1819 static cfg_type_t cfg_type_logversions = {
1820         "logversions", parse_logversions, cfg_print_ustring, cfg_doc_terminal,
1821         &cfg_rep_string, logversions_enums
1822 };
1823
1824 static cfg_tuplefielddef_t logfile_fields[] = {
1825         { "file", &cfg_type_qstring, 0 },
1826         { "versions", &cfg_type_logversions, 0 },
1827         { "size", &cfg_type_size, 0 },
1828         { NULL, NULL, 0 }
1829 };
1830
1831 static isc_result_t
1832 parse_logfile(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
1833         isc_result_t result;
1834         cfg_obj_t *obj = NULL;
1835         const cfg_tuplefielddef_t *fields = type->of;
1836
1837         CHECK(cfg_create_tuple(pctx, type, &obj));
1838
1839         /* Parse the mandatory "file" field */
1840         CHECK(cfg_parse_obj(pctx, fields[0].type, &obj->value.tuple[0]));
1841
1842         /* Parse "versions" and "size" fields in any order. */
1843         for (;;) {
1844                 CHECK(cfg_peektoken(pctx, 0));
1845                 if (pctx->token.type == isc_tokentype_string) {
1846                         CHECK(cfg_gettoken(pctx, 0));
1847                         if (strcasecmp(TOKEN_STRING(pctx),
1848                                        "versions") == 0 &&
1849                             obj->value.tuple[1] == NULL) {
1850                                 CHECK(cfg_parse_obj(pctx, fields[1].type,
1851                                             &obj->value.tuple[1]));
1852                         } else if (strcasecmp(TOKEN_STRING(pctx),
1853                                               "size") == 0 &&
1854                                    obj->value.tuple[2] == NULL) {
1855                                 CHECK(cfg_parse_obj(pctx, fields[2].type,
1856                                             &obj->value.tuple[2]));
1857                         } else {
1858                                 break;
1859                         }
1860                 } else {
1861                         break;
1862                 }
1863         }
1864
1865         /* Create void objects for missing optional values. */
1866         if (obj->value.tuple[1] == NULL)
1867                 CHECK(cfg_parse_void(pctx, NULL, &obj->value.tuple[1]));
1868         if (obj->value.tuple[2] == NULL)
1869                 CHECK(cfg_parse_void(pctx, NULL, &obj->value.tuple[2]));
1870
1871         *ret = obj;
1872         return (ISC_R_SUCCESS);
1873
1874  cleanup:
1875         CLEANUP_OBJ(obj);
1876         return (result);
1877 }
1878
1879 static void
1880 print_logfile(cfg_printer_t *pctx, const cfg_obj_t *obj) {
1881         cfg_print_obj(pctx, obj->value.tuple[0]); /* file */
1882         if (obj->value.tuple[1]->type->print != cfg_print_void) {
1883                 cfg_print_chars(pctx, " versions ", 10);
1884                 cfg_print_obj(pctx, obj->value.tuple[1]);
1885         }
1886         if (obj->value.tuple[2]->type->print != cfg_print_void) {
1887                 cfg_print_chars(pctx, " size ", 6);
1888                 cfg_print_obj(pctx, obj->value.tuple[2]);
1889         }
1890 }
1891
1892
1893 static void
1894 doc_logfile(cfg_printer_t *pctx, const cfg_type_t *type) {
1895         UNUSED(type);
1896         cfg_print_cstr(pctx, "<quoted_string>");
1897         cfg_print_chars(pctx, " ", 1);
1898         cfg_print_cstr(pctx, "[ versions ( \"unlimited\" | <integer> ) ]");
1899         cfg_print_chars(pctx, " ", 1);
1900         cfg_print_cstr(pctx, "[ size <size> ]");
1901 }
1902
1903 static cfg_type_t cfg_type_logfile = {
1904         "log_file", parse_logfile, print_logfile, doc_logfile,
1905         &cfg_rep_tuple, logfile_fields
1906 };
1907
1908 /*% An IPv4 address with optional port, "*" accepted as wildcard. */
1909 static cfg_type_t cfg_type_sockaddr4wild = {
1910         "sockaddr4wild", cfg_parse_sockaddr, cfg_print_sockaddr,
1911         cfg_doc_sockaddr, &cfg_rep_sockaddr, &sockaddr4wild_flags
1912 };
1913
1914 /*% An IPv6 address with optional port, "*" accepted as wildcard. */
1915 static cfg_type_t cfg_type_sockaddr6wild = {
1916         "v6addrportwild", cfg_parse_sockaddr, cfg_print_sockaddr,
1917         cfg_doc_sockaddr, &cfg_rep_sockaddr, &sockaddr6wild_flags
1918 };
1919
1920 /*%
1921  * lwres
1922  */
1923
1924 static cfg_tuplefielddef_t lwres_view_fields[] = {
1925         { "name", &cfg_type_astring, 0 },
1926         { "class", &cfg_type_optional_class, 0 },
1927         { NULL, NULL, 0 }
1928 };
1929 static cfg_type_t cfg_type_lwres_view = {
1930         "lwres_view", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple, &cfg_rep_tuple,
1931         lwres_view_fields
1932 };
1933
1934 static cfg_type_t cfg_type_lwres_searchlist = {
1935         "lwres_searchlist", cfg_parse_bracketed_list, cfg_print_bracketed_list,
1936         cfg_doc_bracketed_list, &cfg_rep_list, &cfg_type_astring };
1937
1938 static cfg_clausedef_t
1939 lwres_clauses[] = {
1940         { "listen-on", &cfg_type_portiplist, 0 },
1941         { "view", &cfg_type_lwres_view, 0 },
1942         { "search", &cfg_type_lwres_searchlist, 0 },
1943         { "ndots", &cfg_type_uint32, 0 },
1944         { NULL, NULL, 0 }
1945 };
1946
1947 static cfg_clausedef_t *
1948 lwres_clausesets[] = {
1949         lwres_clauses,
1950         NULL
1951 };
1952 static cfg_type_t cfg_type_lwres = {
1953         "lwres", cfg_parse_map, cfg_print_map, cfg_doc_map, &cfg_rep_map,
1954         lwres_clausesets
1955 };
1956
1957 /*%
1958  * rndc
1959  */
1960
1961 static cfg_clausedef_t
1962 rndcconf_options_clauses[] = {
1963         { "default-key", &cfg_type_astring, 0 },
1964         { "default-port", &cfg_type_uint32, 0 },
1965         { "default-server", &cfg_type_astring, 0 },
1966         { "default-source-address", &cfg_type_netaddr4wild, 0 },
1967         { "default-source-address-v6", &cfg_type_netaddr6wild, 0 },
1968         { NULL, NULL, 0 }
1969 };
1970
1971 static cfg_clausedef_t *
1972 rndcconf_options_clausesets[] = {
1973         rndcconf_options_clauses,
1974         NULL
1975 };
1976
1977 static cfg_type_t cfg_type_rndcconf_options = {
1978         "rndcconf_options", cfg_parse_map, cfg_print_map, cfg_doc_map,
1979         &cfg_rep_map, rndcconf_options_clausesets
1980 };
1981
1982 static cfg_clausedef_t
1983 rndcconf_server_clauses[] = {
1984         { "key", &cfg_type_astring, 0 },
1985         { "port", &cfg_type_uint32, 0 },
1986         { "source-address", &cfg_type_netaddr4wild, 0 },
1987         { "source-address-v6", &cfg_type_netaddr6wild, 0 },
1988         { "addresses", &cfg_type_bracketed_sockaddrnameportlist, 0 },
1989         { NULL, NULL, 0 }
1990 };
1991
1992 static cfg_clausedef_t *
1993 rndcconf_server_clausesets[] = {
1994         rndcconf_server_clauses,
1995         NULL
1996 };
1997
1998 static cfg_type_t cfg_type_rndcconf_server = {
1999         "rndcconf_server", cfg_parse_named_map, cfg_print_map, cfg_doc_map,
2000         &cfg_rep_map, rndcconf_server_clausesets
2001 };
2002
2003 static cfg_clausedef_t
2004 rndcconf_clauses[] = {
2005         { "key", &cfg_type_key, CFG_CLAUSEFLAG_MULTI },
2006         { "server", &cfg_type_rndcconf_server, CFG_CLAUSEFLAG_MULTI },
2007         { "options", &cfg_type_rndcconf_options, 0 },
2008         { NULL, NULL, 0 }
2009 };
2010
2011 static cfg_clausedef_t *
2012 rndcconf_clausesets[] = {
2013         rndcconf_clauses,
2014         NULL
2015 };
2016
2017 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_rndcconf = {
2018         "rndcconf", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2019         &cfg_rep_map, rndcconf_clausesets
2020 };
2021
2022 static cfg_clausedef_t
2023 rndckey_clauses[] = {
2024         { "key", &cfg_type_key, 0 },
2025         { NULL, NULL, 0 }
2026 };
2027
2028 static cfg_clausedef_t *
2029 rndckey_clausesets[] = {
2030         rndckey_clauses,
2031         NULL
2032 };
2033
2034 LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_rndckey = {
2035         "rndckey", cfg_parse_mapbody, cfg_print_mapbody, cfg_doc_mapbody,
2036         &cfg_rep_map, rndckey_clausesets
2037 };
2038
2039 static cfg_tuplefielddef_t nameport_fields[] = {
2040         { "name", &cfg_type_astring, 0 },
2041         { "port", &cfg_type_optional_port, 0 },
2042         { NULL, NULL, 0 }
2043 };
2044 static cfg_type_t cfg_type_nameport = {
2045         "nameport", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2046         &cfg_rep_tuple, nameport_fields
2047 };
2048
2049 static void
2050 doc_sockaddrnameport(cfg_printer_t *pctx, const cfg_type_t *type) {
2051         UNUSED(type);
2052         cfg_print_chars(pctx, "( ", 2);
2053         cfg_print_cstr(pctx, "<quoted_string>");
2054         cfg_print_chars(pctx, " ", 1);
2055         cfg_print_cstr(pctx, "[ port <integer> ]");
2056         cfg_print_chars(pctx, " | ", 3);
2057         cfg_print_cstr(pctx, "<ipv4_address>");
2058         cfg_print_chars(pctx, " ", 1);
2059         cfg_print_cstr(pctx, "[ port <integer> ]");
2060         cfg_print_chars(pctx, " | ", 3);
2061         cfg_print_cstr(pctx, "<ipv6_address>");
2062         cfg_print_chars(pctx, " ", 1);
2063         cfg_print_cstr(pctx, "[ port <integer> ]");
2064         cfg_print_chars(pctx, " )", 2);
2065 }
2066
2067 static isc_result_t
2068 parse_sockaddrnameport(cfg_parser_t *pctx, const cfg_type_t *type,
2069                        cfg_obj_t **ret)
2070 {
2071         isc_result_t result;
2072         cfg_obj_t *obj = NULL;
2073         UNUSED(type);
2074
2075         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2076         if (pctx->token.type == isc_tokentype_string ||
2077             pctx->token.type == isc_tokentype_qstring) {
2078                 if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK | CFG_ADDR_V6OK))
2079                         CHECK(cfg_parse_sockaddr(pctx, &cfg_type_sockaddr, ret));
2080                 else {
2081                         const cfg_tuplefielddef_t *fields =
2082                                                    cfg_type_nameport.of;
2083                         CHECK(cfg_create_tuple(pctx, &cfg_type_nameport,
2084                                                &obj));
2085                         CHECK(cfg_parse_obj(pctx, fields[0].type,
2086                                             &obj->value.tuple[0]));
2087                         CHECK(cfg_parse_obj(pctx, fields[1].type,
2088                                             &obj->value.tuple[1]));
2089                         *ret = obj;
2090                         obj = NULL;
2091                 }
2092         } else {
2093                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2094                              "expected IP address or hostname");
2095                 return (ISC_R_UNEXPECTEDTOKEN);
2096         }
2097  cleanup:
2098         CLEANUP_OBJ(obj);
2099         return (result);
2100 }
2101
2102 static cfg_type_t cfg_type_sockaddrnameport = {
2103         "sockaddrnameport_element", parse_sockaddrnameport, NULL,
2104          doc_sockaddrnameport, NULL, NULL
2105 };
2106
2107 static cfg_type_t cfg_type_bracketed_sockaddrnameportlist = {
2108         "bracketed_sockaddrnameportlist", cfg_parse_bracketed_list,
2109         cfg_print_bracketed_list, cfg_doc_bracketed_list,
2110         &cfg_rep_list, &cfg_type_sockaddrnameport
2111 };
2112
2113 /*%
2114  * A list of socket addresses or name with an optional default port,
2115  * as used in the dual-stack-servers option.  E.g.,
2116  * "port 1234 { dual-stack-servers.net; 10.0.0.1; 1::2 port 69; }"
2117  */
2118 static cfg_tuplefielddef_t nameportiplist_fields[] = {
2119         { "port", &cfg_type_optional_port, 0 },
2120         { "addresses", &cfg_type_bracketed_sockaddrnameportlist, 0 },
2121         { NULL, NULL, 0 }
2122 };
2123
2124 static cfg_type_t cfg_type_nameportiplist = {
2125         "nameportiplist", cfg_parse_tuple, cfg_print_tuple, cfg_doc_tuple,
2126         &cfg_rep_tuple, nameportiplist_fields
2127 };
2128
2129 /*%
2130  * masters element.
2131  */
2132
2133 static void
2134 doc_masterselement(cfg_printer_t *pctx, const cfg_type_t *type) {
2135         UNUSED(type);
2136         cfg_print_chars(pctx, "( ", 2);
2137         cfg_print_cstr(pctx, "<masters>");
2138         cfg_print_chars(pctx, " | ", 3);
2139         cfg_print_cstr(pctx, "<ipv4_address>");
2140         cfg_print_chars(pctx, " ", 1);
2141         cfg_print_cstr(pctx, "[ port <integer> ]");
2142         cfg_print_chars(pctx, " | ", 3);
2143         cfg_print_cstr(pctx, "<ipv6_address>");
2144         cfg_print_chars(pctx, " ", 1);
2145         cfg_print_cstr(pctx, "[ port <integer> ]");
2146         cfg_print_chars(pctx, " )", 2);
2147 }
2148
2149 static isc_result_t
2150 parse_masterselement(cfg_parser_t *pctx, const cfg_type_t *type,
2151                      cfg_obj_t **ret)
2152 {
2153         isc_result_t result;
2154         cfg_obj_t *obj = NULL;
2155         UNUSED(type);
2156
2157         CHECK(cfg_peektoken(pctx, CFG_LEXOPT_QSTRING));
2158         if (pctx->token.type == isc_tokentype_string ||
2159             pctx->token.type == isc_tokentype_qstring) {
2160                 if (cfg_lookingat_netaddr(pctx, CFG_ADDR_V4OK | CFG_ADDR_V6OK))
2161                         CHECK(cfg_parse_sockaddr(pctx, &cfg_type_sockaddr, ret));
2162                 else
2163                         CHECK(cfg_parse_astring(pctx, &cfg_type_astring, ret));
2164         } else {
2165                 cfg_parser_error(pctx, CFG_LOG_NEAR,
2166                              "expected IP address or masters name");
2167                 return (ISC_R_UNEXPECTEDTOKEN);
2168         }
2169  cleanup:
2170         CLEANUP_OBJ(obj);
2171         return (result);
2172 }
2173
2174 static cfg_type_t cfg_type_masterselement = {
2175         "masters_element", parse_masterselement, NULL,
2176          doc_masterselement, NULL, NULL
2177 };