named-checkzone.c 5.46 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2003  Internet Software Consortium.
Mark Andrews's avatar
Mark Andrews committed
4 5 6 7 8
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
Mark Andrews's avatar
Mark Andrews committed
16 17
 */

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id: named-checkzone.c,v 1.33 2005/01/10 23:43:12 marka Exp $ */
Mark Andrews's avatar
Mark Andrews committed
19 20 21 22 23 24 25

#include <config.h>

#include <stdlib.h>

#include <isc/app.h>
#include <isc/commandline.h>
26
#include <isc/dir.h>
27 28
#include <isc/entropy.h>
#include <isc/hash.h>
Mark Andrews's avatar
Mark Andrews committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <isc/log.h>
#include <isc/mem.h>
#include <isc/socket.h>
#include <isc/string.h>
#include <isc/task.h>
#include <isc/timer.h>
#include <isc/util.h>

#include <dns/db.h>
#include <dns/fixedname.h>
#include <dns/log.h>
#include <dns/rdataclass.h>
#include <dns/rdataset.h>
#include <dns/result.h>
#include <dns/zone.h>

Mark Andrews's avatar
cleanup  
Mark Andrews committed
45 46
#include "check-tool.h"

Mark Andrews's avatar
Mark Andrews committed
47 48
static int quiet = 0;
static isc_mem_t *mctx = NULL;
49
static isc_entropy_t *ectx = NULL;
Mark Andrews's avatar
Mark Andrews committed
50 51
dns_zone_t *zone = NULL;
dns_zonetype_t zonetype = dns_zone_master;
52 53
static int dumpzone = 0;
static const char *output_filename;
Mark Andrews's avatar
Mark Andrews committed
54 55 56 57

#define ERRRET(result, function) \
	do { \
		if (result != ISC_R_SUCCESS) { \
Mark Andrews's avatar
Mark Andrews committed
58 59 60
			if (!quiet) \
				fprintf(stderr, "%s() returned %s\n", \
					function, dns_result_totext(result)); \
Mark Andrews's avatar
Mark Andrews committed
61 62 63 64 65
			return (result); \
		} \
	} while (0)

static void
Brian Wellington's avatar
Brian Wellington committed
66
usage(void) {
Mark Andrews's avatar
Mark Andrews committed
67
	fprintf(stderr,
68
		"usage: named-checkzone [-djqvD] [-c class] [-o output] "
69
		"[-t directory] [-w directory] [-k (ignore|warn|fail)] "
70
		"[-n (ignore|warn|fail)] [-W (ignore|warn)] zonename filename\n");
Mark Andrews's avatar
Mark Andrews committed
71 72 73 74 75
	exit(1);
}

static void
destroy(void) {
Mark Andrews's avatar
Mark Andrews committed
76 77
	if (zone != NULL)
		dns_zone_detach(&zone);
Mark Andrews's avatar
Mark Andrews committed
78 79 80 81 82
}

int
main(int argc, char **argv) {
	int c;
Mark Andrews's avatar
Mark Andrews committed
83
	char *origin = NULL;
Mark Andrews's avatar
Mark Andrews committed
84 85 86
	char *filename = NULL;
	isc_log_t *lctx = NULL;
	isc_result_t result;
87
	char classname_in[] = "IN";
88
	char *classname = classname_in;
89
	const char *workdir = NULL;
Mark Andrews's avatar
Mark Andrews committed
90

91
	while ((c = isc_commandline_parse(argc, argv, "c:dijk:n:qst:o:vw:DW:")) != EOF) {
Mark Andrews's avatar
Mark Andrews committed
92 93 94 95
		switch (c) {
		case 'c':
			classname = isc_commandline_argument;
			break;
96

Mark Andrews's avatar
Mark Andrews committed
97 98 99
		case 'd':
			debug++;
			break;
100

101
		case 'j':
102 103 104
			nomerge = ISC_FALSE;
			break;

105 106 107 108 109 110 111 112 113 114 115 116
		case 'n':
			if (!strcmp(isc_commandline_argument, "ignore"))
				zone_options &= ~(DNS_ZONEOPT_CHECKNS|
						  DNS_ZONEOPT_FATALNS);
			else if (!strcmp(isc_commandline_argument, "warn")) {
				zone_options |= DNS_ZONEOPT_CHECKNS;
				zone_options &= ~DNS_ZONEOPT_FATALNS;
			} else if (!strcmp(isc_commandline_argument, "fail"))
				zone_options |= DNS_ZONEOPT_CHECKNS|
					        DNS_ZONEOPT_FATALNS;
			break;

117
		case 'k':
118
			if (!strcmp(isc_commandline_argument, "warn")) {
119
				zone_options |= DNS_ZONEOPT_CHECKNAMES;
120
				zone_options &= ~DNS_ZONEOPT_CHECKNAMESFAIL;
121
			} else if (!strcmp(isc_commandline_argument,
122
					   "fail")) {
123 124
				zone_options |= DNS_ZONEOPT_CHECKNAMES |
						DNS_ZONEOPT_CHECKNAMESFAIL;
125 126 127 128
			} else if (!strcmp(isc_commandline_argument,
					   "ignore")) {
				zone_options &= ~(DNS_ZONEOPT_CHECKNAMES |
						  DNS_ZONEOPT_CHECKNAMESFAIL);
129 130 131
			}
			break;

Mark Andrews's avatar
Mark Andrews committed
132 133 134
		case 'q':
			quiet++;
			break;
135

Mark Andrews's avatar
Mark Andrews committed
136
		case 't':
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
			result = isc_dir_chroot(isc_commandline_argument);
			if (result != ISC_R_SUCCESS) {
				fprintf(stderr, "isc_dir_chroot: %s: %s\n",
					isc_commandline_argument,
					isc_result_totext(result));
				exit(1);
			}
			result = isc_dir_chdir("/");
			if (result != ISC_R_SUCCESS) {
				fprintf(stderr, "isc_dir_chdir: %s\n",
					isc_result_totext(result));
				exit(1);
			}
			break;

152 153 154 155
		case 'o':
			output_filename = isc_commandline_argument;
			break;

156 157 158
		case 'v':
			printf(VERSION "\n");
			exit(0);
159 160 161 162 163

		case 'w':
			workdir = isc_commandline_argument;
			break;

164 165 166 167
		case 'D':
			dumpzone++;
			break;

168 169 170 171 172 173 174
		case 'W':
			if (!strcmp(isc_commandline_argument, "warn"))
				zone_options |= DNS_ZONEOPT_CHECKWILDCARD;
			else if (!strcmp(isc_commandline_argument, "ignore"))
				zone_options &= ~DNS_ZONEOPT_CHECKWILDCARD;
			break;

Mark Andrews's avatar
Mark Andrews committed
175 176 177 178 179
		default:
			usage();
		}
	}

180 181 182 183 184 185 186 187 188
	if (workdir != NULL) {
		result = isc_dir_chdir(workdir);
		if (result != ISC_R_SUCCESS) {
			fprintf(stderr, "isc_dir_chdir: %s: %s\n",
				workdir, isc_result_totext(result));
			exit(1);
		}
	}

189
	if (isc_commandline_index + 2 > argc)
Mark Andrews's avatar
Mark Andrews committed
190 191 192
		usage();

	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
193
	if (!quiet) {
Mark Andrews's avatar
cleanup  
Mark Andrews committed
194
		RUNTIME_CHECK(setup_logging(mctx, &lctx) == ISC_R_SUCCESS);
195 196 197
		dns_log_init(lctx);
		dns_log_setcontext(lctx);
	}
198 199 200
	RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE)
		      == ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
201

202 203
	dns_result_register();

204 205
	origin = argv[isc_commandline_index++];
	filename = argv[isc_commandline_index++];
206
	result = load_zone(mctx, origin, filename, classname, &zone);
207 208 209 210 211

	if (result == ISC_R_SUCCESS && dumpzone) {
		result = dump_zone(origin, zone, output_filename);
	}

Mark Andrews's avatar
Mark Andrews committed
212
	if (!quiet && result == ISC_R_SUCCESS)
213
		fprintf(stdout, "OK\n");
Mark Andrews's avatar
Mark Andrews committed
214
	destroy();
Mark Andrews's avatar
Mark Andrews committed
215 216
	if (lctx != NULL)
		isc_log_destroy(&lctx);
217 218
	isc_hash_destroy();
	isc_entropy_detach(&ectx);
Mark Andrews's avatar
Mark Andrews committed
219
	isc_mem_destroy(&mctx);
Mark Andrews's avatar
Mark Andrews committed
220
	return ((result == ISC_R_SUCCESS) ? 0 : 1);
Mark Andrews's avatar
Mark Andrews committed
221
}