named-checkzone.c 4.71 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
 */

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id: named-checkzone.c,v 1.29 2004/03/05 04:57:20 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
		"[-t directory] [-w directory] [-k option] zonename filename\n");
Mark Andrews's avatar
Mark Andrews committed
67
68
69
70
71
	exit(1);
}

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

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

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

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

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

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

113
114
115
116
117
118
119
120
121
122
		case 'k':
			if (!strcmp(isc_commandline_argument, "check-names")) {
				zone_options |= DNS_ZONEOPT_CHECKNAMES;
			} else if (!strcmp(isc_commandline_argument,
					   "check-names-fail")) {
				zone_options |= DNS_ZONEOPT_CHECKNAMES |
						DNS_ZONEOPT_CHECKNAMESFAIL;
			}
			break;

Mark Andrews's avatar
Mark Andrews committed
123
124
125
		case 'q':
			quiet++;
			break;
126

Mark Andrews's avatar
Mark Andrews committed
127
		case 't':
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
			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;

143
144
145
146
		case 'o':
			output_filename = isc_commandline_argument;
			break;

147
148
149
		case 'v':
			printf(VERSION "\n");
			exit(0);
150
151
152
153
154

		case 'w':
			workdir = isc_commandline_argument;
			break;

155
156
157
158
		case 'D':
			dumpzone++;
			break;

Mark Andrews's avatar
Mark Andrews committed
159
160
161
162
163
		default:
			usage();
		}
	}

164
165
166
167
168
169
170
171
172
	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);
		}
	}

173
	if (isc_commandline_index + 2 > argc)
Mark Andrews's avatar
Mark Andrews committed
174
175
176
		usage();

	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
177
	if (!quiet) {
Mark Andrews's avatar
cleanup    
Mark Andrews committed
178
		RUNTIME_CHECK(setup_logging(mctx, &lctx) == ISC_R_SUCCESS);
179
180
181
		dns_log_init(lctx);
		dns_log_setcontext(lctx);
	}
Mark Andrews's avatar
Mark Andrews committed
182

183
184
	dns_result_register();

185
186
	origin = argv[isc_commandline_index++];
	filename = argv[isc_commandline_index++];
187
	result = load_zone(mctx, origin, filename, classname, &zone);
188
189
190
191
192

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

Mark Andrews's avatar
Mark Andrews committed
193
	if (!quiet && result == ISC_R_SUCCESS)
194
		fprintf(stdout, "OK\n");
Mark Andrews's avatar
Mark Andrews committed
195
	destroy();
Mark Andrews's avatar
Mark Andrews committed
196
197
	if (lctx != NULL)
		isc_log_destroy(&lctx);
Mark Andrews's avatar
Mark Andrews committed
198
	isc_mem_destroy(&mctx);
Mark Andrews's avatar
Mark Andrews committed
199
	return ((result == ISC_R_SUCCESS) ? 0 : 1);
Mark Andrews's avatar
Mark Andrews committed
200
}