1*bcda20f6Schristos /* $NetBSD: cfg_test.c,v 1.4 2025/01/26 16:25:17 christos Exp $ */ 28aaca124Schristos 38aaca124Schristos /* 48aaca124Schristos * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 58aaca124Schristos * 68aaca124Schristos * SPDX-License-Identifier: MPL-2.0 78aaca124Schristos * 88aaca124Schristos * This Source Code Form is subject to the terms of the Mozilla Public 98aaca124Schristos * License, v. 2.0. If a copy of the MPL was not distributed with this 108aaca124Schristos * file, you can obtain one at https://mozilla.org/MPL/2.0/. 118aaca124Schristos * 128aaca124Schristos * See the COPYRIGHT file distributed with this work for additional 138aaca124Schristos * information regarding copyright ownership. 148aaca124Schristos */ 158aaca124Schristos 168aaca124Schristos /*! \file */ 178aaca124Schristos 188aaca124Schristos #include <errno.h> 198aaca124Schristos #include <stdbool.h> 208aaca124Schristos #include <stdlib.h> 218aaca124Schristos 228aaca124Schristos #include <isc/mem.h> 238aaca124Schristos #include <isc/string.h> 248aaca124Schristos #include <isc/util.h> 258aaca124Schristos 268aaca124Schristos #include <dns/log.h> 278aaca124Schristos 288aaca124Schristos #include <isccfg/grammar.h> 298aaca124Schristos #include <isccfg/namedconf.h> 308aaca124Schristos 318aaca124Schristos static void 328aaca124Schristos check_result(isc_result_t result, const char *format, ...) { 338aaca124Schristos va_list args; 348aaca124Schristos 358aaca124Schristos if (result == ISC_R_SUCCESS) { 368aaca124Schristos return; 378aaca124Schristos } 388aaca124Schristos 398aaca124Schristos va_start(args, format); 408aaca124Schristos vfprintf(stderr, format, args); 418aaca124Schristos va_end(args); 428aaca124Schristos fprintf(stderr, ": %s\n", isc_result_totext(result)); 43dff692fcSchristos exit(EXIT_FAILURE); 448aaca124Schristos } 458aaca124Schristos 468aaca124Schristos static void 478aaca124Schristos output(void *closure, const char *text, int textlen) { 488aaca124Schristos UNUSED(closure); 498aaca124Schristos (void)fwrite(text, 1, textlen, stdout); 508aaca124Schristos } 518aaca124Schristos 528aaca124Schristos static void 538aaca124Schristos usage(void) { 548aaca124Schristos fprintf(stderr, "usage: cfg_test --rndc|--named " 558aaca124Schristos "[--grammar] [--zonegrammar] [--active] " 568aaca124Schristos "[--memstats] conffile\n"); 57dff692fcSchristos exit(EXIT_FAILURE); 588aaca124Schristos } 598aaca124Schristos 608aaca124Schristos int 618aaca124Schristos main(int argc, char **argv) { 628aaca124Schristos isc_result_t result; 638aaca124Schristos isc_mem_t *mctx = NULL; 648aaca124Schristos isc_log_t *lctx = NULL; 658aaca124Schristos isc_logconfig_t *lcfg = NULL; 668aaca124Schristos isc_logdestination_t destination; 678aaca124Schristos cfg_parser_t *pctx = NULL; 688aaca124Schristos cfg_obj_t *cfg = NULL; 698aaca124Schristos cfg_type_t *type = NULL; 708aaca124Schristos bool grammar = false; 718aaca124Schristos bool memstats = false; 728aaca124Schristos char *filename = NULL; 738aaca124Schristos unsigned int zonetype = 0; 748aaca124Schristos unsigned int pflags = 0; 758aaca124Schristos 768aaca124Schristos isc_mem_create(&mctx); 778aaca124Schristos 788aaca124Schristos isc_log_create(mctx, &lctx, &lcfg); 798aaca124Schristos isc_log_setcontext(lctx); 808aaca124Schristos 818aaca124Schristos /* 828aaca124Schristos * Create and install the default channel. 838aaca124Schristos */ 848aaca124Schristos destination.file.stream = stderr; 858aaca124Schristos destination.file.name = NULL; 868aaca124Schristos destination.file.versions = ISC_LOG_ROLLNEVER; 878aaca124Schristos destination.file.maximum_size = 0; 888aaca124Schristos isc_log_createchannel(lcfg, "_default", ISC_LOG_TOFILEDESC, 898aaca124Schristos ISC_LOG_DYNAMIC, &destination, ISC_LOG_PRINTTIME); 908aaca124Schristos 918aaca124Schristos result = isc_log_usechannel(lcfg, "_default", NULL, NULL); 928aaca124Schristos check_result(result, "isc_log_usechannel()"); 938aaca124Schristos 948aaca124Schristos /* 958aaca124Schristos * Set the initial debug level. 968aaca124Schristos */ 978aaca124Schristos isc_log_setdebuglevel(lctx, 2); 988aaca124Schristos 998aaca124Schristos if (argc < 3) { 1008aaca124Schristos usage(); 1018aaca124Schristos } 1028aaca124Schristos 1038aaca124Schristos while (argc > 1) { 1048aaca124Schristos if (strcmp(argv[1], "--active") == 0) { 1058aaca124Schristos pflags |= CFG_PRINTER_ACTIVEONLY; 1068aaca124Schristos } else if (strcmp(argv[1], "--grammar") == 0) { 1078aaca124Schristos grammar = true; 1088aaca124Schristos } else if (strcmp(argv[1], "--zonegrammar") == 0) { 1098aaca124Schristos argv++, argc--; 1108aaca124Schristos if (argc <= 1) { 1118aaca124Schristos usage(); 1128aaca124Schristos } 1138aaca124Schristos if (strcmp(argv[1], "master") == 0 || 1148aaca124Schristos strcmp(argv[1], "primary") == 0) 1158aaca124Schristos { 1168aaca124Schristos zonetype = CFG_ZONE_PRIMARY; 1178aaca124Schristos } else if (strcmp(argv[1], "slave") == 0 || 1188aaca124Schristos strcmp(argv[1], "secondary") == 0) 1198aaca124Schristos { 1208aaca124Schristos zonetype = CFG_ZONE_SECONDARY; 1218aaca124Schristos } else if (strcmp(argv[1], "mirror") == 0) { 1228aaca124Schristos zonetype = CFG_ZONE_MIRROR; 1238aaca124Schristos } else if (strcmp(argv[1], "stub") == 0) { 1248aaca124Schristos zonetype = CFG_ZONE_STUB; 1258aaca124Schristos } else if (strcmp(argv[1], "static-stub") == 0) { 1268aaca124Schristos zonetype = CFG_ZONE_STATICSTUB; 1278aaca124Schristos } else if (strcmp(argv[1], "hint") == 0) { 1288aaca124Schristos zonetype = CFG_ZONE_HINT; 1298aaca124Schristos } else if (strcmp(argv[1], "forward") == 0) { 1308aaca124Schristos zonetype = CFG_ZONE_FORWARD; 1318aaca124Schristos } else if (strcmp(argv[1], "redirect") == 0) { 1328aaca124Schristos zonetype = CFG_ZONE_REDIRECT; 1338aaca124Schristos } else if (strcmp(argv[1], "in-view") == 0) { 1348aaca124Schristos zonetype = CFG_ZONE_INVIEW; 1358aaca124Schristos } else { 1368aaca124Schristos usage(); 1378aaca124Schristos } 1388aaca124Schristos } else if (strcmp(argv[1], "--memstats") == 0) { 1398aaca124Schristos memstats = true; 1408aaca124Schristos } else if (strcmp(argv[1], "--named") == 0) { 1418aaca124Schristos type = &cfg_type_namedconf; 1428aaca124Schristos } else if (strcmp(argv[1], "--rndc") == 0) { 1438aaca124Schristos type = &cfg_type_rndcconf; 1448aaca124Schristos } else if (argv[1][0] == '-') { 1458aaca124Schristos usage(); 1468aaca124Schristos } else { 1478aaca124Schristos filename = argv[1]; 1488aaca124Schristos } 1498aaca124Schristos argv++, argc--; 1508aaca124Schristos } 1518aaca124Schristos 1528aaca124Schristos if (grammar) { 1538aaca124Schristos if (type == NULL) { 1548aaca124Schristos usage(); 1558aaca124Schristos } 1568aaca124Schristos cfg_print_grammar(type, pflags, output, NULL); 1578aaca124Schristos } else if (zonetype != 0) { 1588aaca124Schristos cfg_print_zonegrammar(zonetype, pflags, output, NULL); 1598aaca124Schristos } else { 1608aaca124Schristos if (type == NULL || filename == NULL) { 1618aaca124Schristos usage(); 1628aaca124Schristos } 1638aaca124Schristos RUNTIME_CHECK(cfg_parser_create(mctx, lctx, &pctx) == 1648aaca124Schristos ISC_R_SUCCESS); 1658aaca124Schristos 1668aaca124Schristos result = cfg_parse_file(pctx, filename, type, &cfg); 1678aaca124Schristos 1688aaca124Schristos fprintf(stderr, "read config: %s\n", isc_result_totext(result)); 1698aaca124Schristos 1708aaca124Schristos if (result != ISC_R_SUCCESS) { 171dff692fcSchristos exit(EXIT_FAILURE); 1728aaca124Schristos } 1738aaca124Schristos 1748aaca124Schristos cfg_print(cfg, output, NULL); 1758aaca124Schristos 1768aaca124Schristos cfg_obj_destroy(pctx, &cfg); 1778aaca124Schristos 1788aaca124Schristos cfg_parser_destroy(&pctx); 1798aaca124Schristos } 1808aaca124Schristos 1818aaca124Schristos isc_log_destroy(&lctx); 1828aaca124Schristos if (memstats) { 1838aaca124Schristos isc_mem_stats(mctx, stderr); 1848aaca124Schristos } 1858aaca124Schristos isc_mem_destroy(&mctx); 1868aaca124Schristos 1878aaca124Schristos fflush(stdout); 1888aaca124Schristos if (ferror(stdout)) { 1898aaca124Schristos fprintf(stderr, "write error\n"); 190*bcda20f6Schristos return 1; 1918aaca124Schristos } 192dff692fcSchristos 193*bcda20f6Schristos return 0; 1948aaca124Schristos } 195