named-checkzone.c 4.21 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright (C) 1999, 2000  Internet Software Consortium.
 *
 * 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.
 */

18
/* $Id: named-checkzone.c,v 1.4 2000/12/19 19:54:29 gson Exp $ */
Mark Andrews's avatar
Mark Andrews committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <config.h>

#include <stdlib.h>

#include <isc/app.h>
#include <isc/commandline.h>
#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
42
43
#include "check-tool.h"

Mark Andrews's avatar
Mark Andrews committed
44
45
46
47
48
49
50
51
52
53
static int debug = 0;
static int quiet = 0;
static isc_mem_t *mctx = NULL;
dns_zone_t *zone = NULL;
dns_zonetype_t zonetype = dns_zone_master;
static const char *dbtype[] = { "rbt" };

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

static void
usage() {
	fprintf(stderr,
Mark Andrews's avatar
Mark Andrews committed
64
		"usage: zone_test [-dq] [-c class] zone [filename]\n");
Mark Andrews's avatar
Mark Andrews committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	exit(1);
}

static isc_result_t
setup(char *zonename, char *filename, char *classname) {
	isc_result_t result;
	dns_rdataclass_t rdclass;
	isc_textregion_t region;
	isc_buffer_t buffer;
	dns_fixedname_t fixorigin;
	dns_name_t *origin;

	if (debug)
		fprintf(stderr, "loading \"%s\" from \"%s\" class \"%s\"\n",
			zonename, filename, classname);
	result = dns_zone_create(&zone, mctx);
	ERRRET(result, "dns_zone_new");

	dns_zone_settype(zone, zonetype);

	isc_buffer_init(&buffer, zonename, strlen(zonename));
	isc_buffer_add(&buffer, strlen(zonename));
	dns_fixedname_init(&fixorigin);
	result = dns_name_fromtext(dns_fixedname_name(&fixorigin),
			  	   &buffer, dns_rootname, ISC_FALSE, NULL);
	ERRRET(result, "dns_name_fromtext");
	origin = dns_fixedname_name(&fixorigin);

	result = dns_zone_setorigin(zone, origin);
	ERRRET(result, "dns_zone_setorigin");

	result = dns_zone_setdbtype(zone, 1, (const char * const *) dbtype);
	ERRRET(result, "dns_zone_setdatabase");

	result = dns_zone_setfile(zone, filename);
	ERRRET(result, "dns_zone_setdatabase");

	region.base = classname;
	region.length = strlen(classname);
	result = dns_rdataclass_fromtext(&rdclass, &region);
	ERRRET(result, "dns_rdataclass_fromtext");

	dns_zone_setclass(zone, rdclass);

	result = dns_zone_load(zone);

	return (result);
}

static void
destroy(void) {
Mark Andrews's avatar
Mark Andrews committed
116
117
	if (zone != NULL)
		dns_zone_detach(&zone);
Mark Andrews's avatar
Mark Andrews committed
118
119
120
121
122
}

int
main(int argc, char **argv) {
	int c;
Mark Andrews's avatar
Mark Andrews committed
123
	char *origin = NULL;
Mark Andrews's avatar
Mark Andrews committed
124
	char *filename = NULL;
125
	char *classname;
Mark Andrews's avatar
Mark Andrews committed
126
127
	isc_log_t *lctx = NULL;
	isc_result_t result;
128
129
130
	char classname_in[] = "IN";

	classname = classname_in;
Mark Andrews's avatar
Mark Andrews committed
131

Mark Andrews's avatar
Mark Andrews committed
132
	while ((c = isc_commandline_parse(argc, argv, "c:dqs")) != EOF) {
Mark Andrews's avatar
Mark Andrews committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
		switch (c) {
		case 'c':
			classname = isc_commandline_argument;
			break;
		case 'd':
			debug++;
			break;
		case 'q':
			quiet++;
			break;
		default:
			usage();
		}
	}

	if (argv[isc_commandline_index] == NULL)
		usage();

	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
Mark Andrews's avatar
cleanup    
Mark Andrews committed
152
153
	if (!quiet)
		RUNTIME_CHECK(setup_logging(mctx, &lctx) == ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
154

Mark Andrews's avatar
Mark Andrews committed
155
156
157
	origin = argv[isc_commandline_index];
	isc_commandline_index++;
	if (argv[isc_commandline_index] != NULL)
Mark Andrews's avatar
Mark Andrews committed
158
		filename = argv[isc_commandline_index];
Mark Andrews's avatar
Mark Andrews committed
159
160
	else
		filename = origin;
Mark Andrews's avatar
cleanup    
Mark Andrews committed
161
	result = setup(origin, filename, (char *)classname);
Mark Andrews's avatar
Mark Andrews committed
162
163
164
	if (!quiet && result == ISC_R_SUCCESS)
		fprintf(stdout, "OK\n ");
	destroy();
Mark Andrews's avatar
Mark Andrews committed
165
166
	if (lctx != NULL)
		isc_log_destroy(&lctx);
Mark Andrews's avatar
Mark Andrews committed
167
	isc_mem_destroy(&mctx);
Mark Andrews's avatar
Mark Andrews committed
168
	return ((result == ISC_R_SUCCESS) ? 0 : 1);
Mark Andrews's avatar
Mark Andrews committed
169
}