named-checkzone.c 4.93 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
3
 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
 * 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
 */

18
/* $Id: named-checkzone.c,v 1.30 2004/10/06 05:56:28 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>
Mark Andrews's avatar
Mark Andrews committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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
43
44
#include "check-tool.h"

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

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

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

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

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

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

Mark Andrews's avatar
Mark Andrews committed
94
95
96
		case 'd':
			debug++;
			break;
97

98
		case 'j':
99
100
101
			nomerge = ISC_FALSE;
			break;

102
103
104
105
106
107
108
109
110
111
112
113
		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;

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

Mark Andrews's avatar
Mark Andrews committed
129
130
131
		case 'q':
			quiet++;
			break;
132

Mark Andrews's avatar
Mark Andrews committed
133
		case 't':
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
			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;

149
150
151
152
		case 'o':
			output_filename = isc_commandline_argument;
			break;

153
154
155
		case 'v':
			printf(VERSION "\n");
			exit(0);
156
157
158
159
160

		case 'w':
			workdir = isc_commandline_argument;
			break;

161
162
163
164
		case 'D':
			dumpzone++;
			break;

Mark Andrews's avatar
Mark Andrews committed
165
166
167
168
169
		default:
			usage();
		}
	}

170
171
172
173
174
175
176
177
178
	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);
		}
	}

179
	if (isc_commandline_index + 2 > argc)
Mark Andrews's avatar
Mark Andrews committed
180
181
182
		usage();

	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
183
	if (!quiet) {
Mark Andrews's avatar
cleanup    
Mark Andrews committed
184
		RUNTIME_CHECK(setup_logging(mctx, &lctx) == ISC_R_SUCCESS);
185
186
187
		dns_log_init(lctx);
		dns_log_setcontext(lctx);
	}
Mark Andrews's avatar
Mark Andrews committed
188

189
190
	dns_result_register();

191
192
	origin = argv[isc_commandline_index++];
	filename = argv[isc_commandline_index++];
193
	result = load_zone(mctx, origin, filename, classname, &zone);
194
195
196
197
198

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

Mark Andrews's avatar
Mark Andrews committed
199
	if (!quiet && result == ISC_R_SUCCESS)
200
		fprintf(stdout, "OK\n");
Mark Andrews's avatar
Mark Andrews committed
201
	destroy();
Mark Andrews's avatar
Mark Andrews committed
202
203
	if (lctx != NULL)
		isc_log_destroy(&lctx);
Mark Andrews's avatar
Mark Andrews committed
204
	isc_mem_destroy(&mctx);
Mark Andrews's avatar
Mark Andrews committed
205
	return ((result == ISC_R_SUCCESS) ? 0 : 1);
Mark Andrews's avatar
Mark Andrews committed
206
}