named-checkzone.c 4.13 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 1999-2002  Internet Software Consortium.
Mark Andrews's avatar
Mark Andrews committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * INTERNET SOFTWARE CONSORTIUM 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
18
/* $Id: named-checkzone.c,v 1.26 2003/10/17 03:46:41 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 49 50 51 52
static int quiet = 0;
static isc_mem_t *mctx = NULL;
dns_zone_t *zone = NULL;
dns_zonetype_t zonetype = dns_zone_master;

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

static void
Brian Wellington's avatar
Brian Wellington committed
61
usage(void) {
Mark Andrews's avatar
Mark Andrews committed
62
	fprintf(stderr,
63 64
		"usage: named-checkzone [-djqv] [-c class] [-t directory] "
		"[-w directory] zonename filename\n");
Mark Andrews's avatar
Mark Andrews committed
65 66 67 68 69
	exit(1);
}

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

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

85
	while ((c = isc_commandline_parse(argc, argv, "c:dijn:qst:vw:")) != EOF) {
Mark Andrews's avatar
Mark Andrews committed
86 87 88 89
		switch (c) {
		case 'c':
			classname = isc_commandline_argument;
			break;
90

Mark Andrews's avatar
Mark Andrews committed
91 92 93
		case 'd':
			debug++;
			break;
94

95
		case 'j':
96 97 98
			nomerge = ISC_FALSE;
			break;

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

Mark Andrews's avatar
Mark Andrews committed
111 112 113
		case 'q':
			quiet++;
			break;
114

Mark Andrews's avatar
Mark Andrews committed
115
		case 't':
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
			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;

131 132 133
		case 'v':
			printf(VERSION "\n");
			exit(0);
134 135 136 137 138

		case 'w':
			workdir = isc_commandline_argument;
			break;

Mark Andrews's avatar
Mark Andrews committed
139 140 141 142 143
		default:
			usage();
		}
	}

144 145 146 147 148 149 150 151 152
	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);
		}
	}

153
	if (isc_commandline_index + 2 > argc)
Mark Andrews's avatar
Mark Andrews committed
154 155 156
		usage();

	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
157
	if (!quiet) {
Mark Andrews's avatar
cleanup  
Mark Andrews committed
158
		RUNTIME_CHECK(setup_logging(mctx, &lctx) == ISC_R_SUCCESS);
159 160 161
		dns_log_init(lctx);
		dns_log_setcontext(lctx);
	}
Mark Andrews's avatar
Mark Andrews committed
162

163 164
	dns_result_register();

165 166
	origin = argv[isc_commandline_index++];
	filename = argv[isc_commandline_index++];
167
	result = load_zone(mctx, origin, filename, classname, &zone);
Mark Andrews's avatar
Mark Andrews committed
168
	if (!quiet && result == ISC_R_SUCCESS)
169
		fprintf(stdout, "OK\n");
Mark Andrews's avatar
Mark Andrews committed
170
	destroy();
Mark Andrews's avatar
Mark Andrews committed
171 172
	if (lctx != NULL)
		isc_log_destroy(&lctx);
Mark Andrews's avatar
Mark Andrews committed
173
	isc_mem_destroy(&mctx);
Mark Andrews's avatar
Mark Andrews committed
174
	return ((result == ISC_R_SUCCESS) ? 0 : 1);
Mark Andrews's avatar
Mark Andrews committed
175
}