named-checkconf.c revision 35f6a21f5f8114542c050bfcb484b39ce513d4bd
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews/*
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Copyright (C) 2004-2007, 2009-2014 Internet Systems Consortium, Inc. ("ISC")
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Copyright (C) 1999-2002 Internet Software Consortium.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews *
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Permission to use, copy, modify, and/or distribute this software for any
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * purpose with or without fee is hereby granted, provided that the above
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * copyright notice and this permission notice appear in all copies.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews *
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews/* $Id: named-checkconf.c,v 1.56 2011/03/12 04:59:46 tbox Exp $ */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews/*! \file */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <config.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <errno.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <stdlib.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <stdio.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isc/commandline.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isc/dir.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isc/entropy.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isc/hash.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isc/log.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isc/mem.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isc/result.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isc/string.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isc/util.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <isccfg/namedconf.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <bind9/check.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <dns/db.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <dns/fixedname.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <dns/log.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <dns/name.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <dns/rdataclass.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <dns/result.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <dns/rootns.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include <dns/zone.h>
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#include "check-tool.h"
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic const char *program = "named-checkconf";
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsisc_log_t *logc = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#define CHECK(r)\
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews do { \
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews result = (r); \
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (result != ISC_R_SUCCESS) \
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews goto cleanup; \
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews } while (0)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews/*% usage */
44a966dff66061ac3f266c6b451a70733eb78e82Mark AndrewsISC_PLATFORM_NORETURN_PRE static void
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsusage(void) ISC_PLATFORM_NORETURN_POST;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic void
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsusage(void) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews fprintf(stderr, "usage: %s [-h] [-j] [-p] [-v] [-z] [-t directory] "
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews "[named.conf]\n", program);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews exit(1);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews}
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews/*% directory callback */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic isc_result_t
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsdirectory_callback(const char *clausename, const cfg_obj_t *obj, void *arg) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews isc_result_t result;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const char *directory;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews REQUIRE(strcasecmp("directory", clausename) == 0);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews UNUSED(arg);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews UNUSED(clausename);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews /*
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Change directory.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews directory = cfg_obj_asstring(obj);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews result = isc_dir_chdir(directory);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (result != ISC_R_SUCCESS) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews cfg_obj_log(obj, logc, ISC_LOG_ERROR,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews "change directory to '%s' failed: %s\n",
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews directory, isc_result_totext(result));
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (result);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews }
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_R_SUCCESS);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews}
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic isc_boolean_t
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsget_maps(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews int i;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews for (i = 0;; i++) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (maps[i] == NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_FALSE);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_TRUE);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews }
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews}
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic isc_boolean_t
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsget_checknames(const cfg_obj_t **maps, const cfg_obj_t **obj) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_listelt_t *element;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *checknames;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *type;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *value;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews isc_result_t result;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews int i;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews for (i = 0;; i++) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (maps[i] == NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_FALSE);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews checknames = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews result = cfg_map_get(maps[i], "check-names", &checknames);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (result != ISC_R_SUCCESS)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews continue;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (checknames != NULL && !cfg_obj_islist(checknames)) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews *obj = checknames;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_TRUE);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews }
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews for (element = cfg_list_first(checknames);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews element != NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews element = cfg_list_next(element)) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews value = cfg_listelt_value(element);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews type = cfg_tuple_get(value, "type");
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (strcasecmp(cfg_obj_asstring(type), "master") != 0)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews continue;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews *obj = cfg_tuple_get(value, "mode");
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_TRUE);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews }
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews }
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews}
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic isc_result_t
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsconfig_get(const cfg_obj_t **maps, const char *name, const cfg_obj_t **obj) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews int i;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews for (i = 0;; i++) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (maps[i] == NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_R_NOTFOUND);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (cfg_map_get(maps[i], name, obj) == ISC_R_SUCCESS)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_R_SUCCESS);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews }
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews}
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic isc_result_t
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsconfigure_hint(const char *zfile, const char *zclass, isc_mem_t *mctx) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews isc_result_t result;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews dns_db_t *db = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews dns_rdataclass_t rdclass;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews isc_textregion_t r;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (zfile == NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_R_FAILURE);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews DE_CONST(zclass, r.base);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews r.length = strlen(zclass);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews result = dns_rdataclass_fromtext(&rdclass, &r);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (result != ISC_R_SUCCESS)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (result);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews result = dns_rootns_create(mctx, rdclass, zfile, &db);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (result != ISC_R_SUCCESS)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (result);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews dns_db_detach(&db);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_R_SUCCESS);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews}
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews/*% configure the zone */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic isc_result_t
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsconfigure_zone(const char *vclass, const char *view,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *zconfig, const cfg_obj_t *vconfig,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *config, isc_mem_t *mctx)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews{
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews int i = 0;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews isc_result_t result;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const char *zclass;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const char *zname;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const char *zfile = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *maps[4];
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *zoptions = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *classobj = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *typeobj = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *fileobj = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *dbobj = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *obj = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews const cfg_obj_t *fmtobj = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews dns_masterformat_t masterformat;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews dns_ttl_t maxttl = 0;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews zone_options = DNS_ZONEOPT_CHECKNS | DNS_ZONEOPT_MANYERRORS;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews classobj = cfg_tuple_get(zconfig, "class");
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (!cfg_obj_isstring(classobj))
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews zclass = vclass;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews else
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews zclass = cfg_obj_asstring(classobj);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews zoptions = cfg_tuple_get(zconfig, "options");
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews maps[i++] = zoptions;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (vconfig != NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews maps[i++] = cfg_tuple_get(vconfig, "options");
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (config != NULL) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews cfg_map_get(config, "options", &obj);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (obj != NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews maps[i++] = obj;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews }
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews maps[i] = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews cfg_map_get(zoptions, "type", &typeobj);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (typeobj == NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_R_FAILURE);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews cfg_map_get(zoptions, "file", &fileobj);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (fileobj != NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews zfile = cfg_obj_asstring(fileobj);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews /*
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Check hints files for hint zones.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Skip loading checks for any type other than
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * master and redirect
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (strcasecmp(cfg_obj_asstring(typeobj), "hint") == 0)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (configure_hint(zfile, zclass, mctx));
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews else if ((strcasecmp(cfg_obj_asstring(typeobj), "master") != 0) &&
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews (strcasecmp(cfg_obj_asstring(typeobj), "redirect") != 0))
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_R_SUCCESS);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (zfile == NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_R_FAILURE);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews cfg_map_get(zoptions, "database", &dbobj);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (dbobj != NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews return (ISC_R_SUCCESS);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews obj = NULL;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (get_maps(maps, "check-dup-records", &obj)) {
if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
zone_options |= DNS_ZONEOPT_CHECKDUPRR;
zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
zone_options |= DNS_ZONEOPT_CHECKDUPRR;
zone_options |= DNS_ZONEOPT_CHECKDUPRRFAIL;
} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
zone_options &= ~DNS_ZONEOPT_CHECKDUPRR;
zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
} else
INSIST(0);
} else {
zone_options |= DNS_ZONEOPT_CHECKDUPRR;
zone_options &= ~DNS_ZONEOPT_CHECKDUPRRFAIL;
}
obj = NULL;
if (get_maps(maps, "check-mx", &obj)) {
if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
zone_options |= DNS_ZONEOPT_CHECKMX;
zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
zone_options |= DNS_ZONEOPT_CHECKMX;
zone_options |= DNS_ZONEOPT_CHECKMXFAIL;
} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
zone_options &= ~DNS_ZONEOPT_CHECKMX;
zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
} else
INSIST(0);
} else {
zone_options |= DNS_ZONEOPT_CHECKMX;
zone_options &= ~DNS_ZONEOPT_CHECKMXFAIL;
}
obj = NULL;
if (get_maps(maps, "check-integrity", &obj)) {
if (cfg_obj_asboolean(obj))
zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
else
zone_options &= ~DNS_ZONEOPT_CHECKINTEGRITY;
} else
zone_options |= DNS_ZONEOPT_CHECKINTEGRITY;
obj = NULL;
if (get_maps(maps, "check-mx-cname", &obj)) {
if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
zone_options |= DNS_ZONEOPT_WARNMXCNAME;
zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
zone_options &= ~DNS_ZONEOPT_WARNMXCNAME;
zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
zone_options |= DNS_ZONEOPT_WARNMXCNAME;
zone_options |= DNS_ZONEOPT_IGNOREMXCNAME;
} else
INSIST(0);
} else {
zone_options |= DNS_ZONEOPT_WARNMXCNAME;
zone_options &= ~DNS_ZONEOPT_IGNOREMXCNAME;
}
obj = NULL;
if (get_maps(maps, "check-srv-cname", &obj)) {
if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
zone_options &= ~DNS_ZONEOPT_WARNSRVCNAME;
zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
zone_options |= DNS_ZONEOPT_IGNORESRVCNAME;
} else
INSIST(0);
} else {
zone_options |= DNS_ZONEOPT_WARNSRVCNAME;
zone_options &= ~DNS_ZONEOPT_IGNORESRVCNAME;
}
obj = NULL;
if (get_maps(maps, "check-sibling", &obj)) {
if (cfg_obj_asboolean(obj))
zone_options |= DNS_ZONEOPT_CHECKSIBLING;
else
zone_options &= ~DNS_ZONEOPT_CHECKSIBLING;
}
obj = NULL;
if (get_maps(maps, "check-spf", &obj)) {
if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
zone_options |= DNS_ZONEOPT_CHECKSPF;
} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
zone_options &= ~DNS_ZONEOPT_CHECKSPF;
} else
INSIST(0);
} else {
zone_options |= DNS_ZONEOPT_CHECKSPF;
}
obj = NULL;
if (get_checknames(maps, &obj)) {
if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
zone_options |= DNS_ZONEOPT_CHECKNAMES;
zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
zone_options |= DNS_ZONEOPT_CHECKNAMES;
zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
zone_options &= ~DNS_ZONEOPT_CHECKNAMES;
zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
} else
INSIST(0);
} else {
zone_options |= DNS_ZONEOPT_CHECKNAMES;
zone_options |= DNS_ZONEOPT_CHECKNAMESFAIL;
}
masterformat = dns_masterformat_text;
fmtobj = NULL;
result = config_get(maps, "masterfile-format", &fmtobj);
if (result == ISC_R_SUCCESS) {
const char *masterformatstr = cfg_obj_asstring(fmtobj);
if (strcasecmp(masterformatstr, "text") == 0)
masterformat = dns_masterformat_text;
else if (strcasecmp(masterformatstr, "raw") == 0)
masterformat = dns_masterformat_raw;
else if (strcasecmp(masterformatstr, "map") == 0)
masterformat = dns_masterformat_map;
else
INSIST(0);
}
obj = NULL;
if (get_maps(maps, "max-zone-ttl", &obj)) {
maxttl = cfg_obj_asuint32(obj);
zone_options2 |= DNS_ZONEOPT2_CHECKTTL;
}
result = load_zone(mctx, zname, zfile, masterformat,
zclass, maxttl, NULL);
if (result != ISC_R_SUCCESS)
fprintf(stderr, "%s/%s/%s: %s\n", view, zname, zclass,
dns_result_totext(result));
return (result);
}
/*% configure a view */
static isc_result_t
configure_view(const char *vclass, const char *view, const cfg_obj_t *config,
const cfg_obj_t *vconfig, isc_mem_t *mctx)
{
const cfg_listelt_t *element;
const cfg_obj_t *voptions;
const cfg_obj_t *zonelist;
isc_result_t result = ISC_R_SUCCESS;
isc_result_t tresult;
voptions = NULL;
if (vconfig != NULL)
voptions = cfg_tuple_get(vconfig, "options");
zonelist = NULL;
if (voptions != NULL)
(void)cfg_map_get(voptions, "zone", &zonelist);
else
(void)cfg_map_get(config, "zone", &zonelist);
for (element = cfg_list_first(zonelist);
element != NULL;
element = cfg_list_next(element))
{
const cfg_obj_t *zconfig = cfg_listelt_value(element);
tresult = configure_zone(vclass, view, zconfig, vconfig,
config, mctx);
if (tresult != ISC_R_SUCCESS)
result = tresult;
}
return (result);
}
/*% load zones from the configuration */
static isc_result_t
load_zones_fromconfig(const cfg_obj_t *config, isc_mem_t *mctx) {
const cfg_listelt_t *element;
const cfg_obj_t *classobj;
const cfg_obj_t *views;
const cfg_obj_t *vconfig;
const char *vclass;
isc_result_t result = ISC_R_SUCCESS;
isc_result_t tresult;
views = NULL;
(void)cfg_map_get(config, "view", &views);
for (element = cfg_list_first(views);
element != NULL;
element = cfg_list_next(element))
{
const char *vname;
vclass = "IN";
vconfig = cfg_listelt_value(element);
if (vconfig != NULL) {
classobj = cfg_tuple_get(vconfig, "class");
if (cfg_obj_isstring(classobj))
vclass = cfg_obj_asstring(classobj);
}
vname = cfg_obj_asstring(cfg_tuple_get(vconfig, "name"));
tresult = configure_view(vclass, vname, config, vconfig, mctx);
if (tresult != ISC_R_SUCCESS)
result = tresult;
}
if (views == NULL) {
tresult = configure_view("IN", "_default", config, NULL, mctx);
if (tresult != ISC_R_SUCCESS)
result = tresult;
}
return (result);
}
static void
output(void *closure, const char *text, int textlen) {
UNUSED(closure);
if (fwrite(text, 1, textlen, stdout) != (size_t)textlen) {
perror("fwrite");
exit(1);
}
}
/*% The main processing routine */
int
main(int argc, char **argv) {
int c;
cfg_parser_t *parser = NULL;
cfg_obj_t *config = NULL;
const char *conffile = NULL;
isc_mem_t *mctx = NULL;
isc_result_t result;
int exit_status = 0;
isc_entropy_t *ectx = NULL;
isc_boolean_t load_zones = ISC_FALSE;
isc_boolean_t print = ISC_FALSE;
unsigned int flags = 0;
isc_commandline_errprint = ISC_FALSE;
while ((c = isc_commandline_parse(argc, argv, "dhjt:pvxz")) != EOF) {
switch (c) {
case 'd':
debug++;
break;
case 'j':
nomerge = ISC_FALSE;
break;
case 't':
result = isc_dir_chroot(isc_commandline_argument);
if (result != ISC_R_SUCCESS) {
fprintf(stderr, "isc_dir_chroot: %s\n",
isc_result_totext(result));
exit(1);
}
break;
case 'p':
print = ISC_TRUE;
break;
case 'v':
printf(VERSION "\n");
exit(0);
case 'x':
flags |= CFG_PRINTER_XKEY;
break;
case 'z':
load_zones = ISC_TRUE;
docheckmx = ISC_FALSE;
docheckns = ISC_FALSE;
dochecksrv = ISC_FALSE;
break;
case '?':
if (isc_commandline_option != '?')
fprintf(stderr, "%s: invalid argument -%c\n",
program, isc_commandline_option);
/* FALLTHROUGH */
case 'h':
usage();
default:
fprintf(stderr, "%s: unhandled option -%c\n",
program, isc_commandline_option);
exit(1);
}
}
if (((flags & CFG_PRINTER_XKEY) != 0) && !print) {
fprintf(stderr, "%s: -x cannot be used without -p\n", program);
exit(1);
}
if (isc_commandline_index + 1 < argc)
usage();
if (argv[isc_commandline_index] != NULL)
conffile = argv[isc_commandline_index];
if (conffile == NULL || conffile[0] == '\0')
conffile = NAMED_CONFFILE;
#ifdef _WIN32
InitSockets();
#endif
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
RUNTIME_CHECK(setup_logging(mctx, stdout, &logc) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE)
== ISC_R_SUCCESS);
dns_result_register();
RUNTIME_CHECK(cfg_parser_create(mctx, logc, &parser) == ISC_R_SUCCESS);
cfg_parser_setcallback(parser, directory_callback, NULL);
if (cfg_parse_file(parser, conffile, &cfg_type_namedconf, &config) !=
ISC_R_SUCCESS)
exit(1);
result = bind9_check_namedconf(config, logc, mctx);
if (result != ISC_R_SUCCESS)
exit_status = 1;
if (result == ISC_R_SUCCESS && load_zones) {
result = load_zones_fromconfig(config, mctx);
if (result != ISC_R_SUCCESS)
exit_status = 1;
}
if (print && exit_status == 0)
cfg_printx(config, flags, output, NULL);
cfg_obj_destroy(parser, &config);
cfg_parser_destroy(&parser);
dns_name_destroy();
isc_log_destroy(&logc);
isc_hash_destroy();
isc_entropy_detach(&ectx);
isc_mem_destroy(&mctx);
#ifdef _WIN32
DestroySockets();
#endif
return (exit_status);
}