nsd-checkzone.c revision 1.4
1/*
2 * nsd-checkzone.c -- nsd-checkzone(8) checks zones for syntax errors
3 *
4 * Copyright (c) 2013, NLnet Labs. All rights reserved.
5 *
6 * See LICENSE for the license.
7 *
8 */
9
10#include "config.h"
11
12#include <assert.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <time.h>
17#include <unistd.h>
18#include <errno.h>
19
20#include "nsd.h"
21#include "options.h"
22#include "util.h"
23#include "zonec.h"
24
25struct nsd nsd;
26
27/*
28 * Print the help text.
29 *
30 */
31static void
32usage (void)
33{
34	fprintf(stderr, "Usage: nsd-checkzone <zone name> <zone file>\n");
35	fprintf(stderr, "Version %s. Report bugs to <%s>.\n",
36		PACKAGE_VERSION, PACKAGE_BUGREPORT);
37}
38
39static void
40check_zone(struct nsd* nsd, const char* name, const char* fname)
41{
42	const dname_type* dname;
43	zone_options_type* zo;
44	zone_type* zone;
45	unsigned errors;
46
47	/* init*/
48	nsd->db = namedb_open("", nsd->options);
49	dname = dname_parse(nsd->options->region, name);
50	if(!dname) {
51		/* parse failure */
52		error("cannot parse zone name '%s'", name);
53	}
54	zo = zone_options_create(nsd->options->region);
55	memset(zo, 0, sizeof(*zo));
56	zo->node.key = dname;
57	zo->name = name;
58	zone = namedb_zone_create(nsd->db, dname, zo);
59
60	/* read the zone */
61	errors = zonec_read(name, fname, zone);
62	if(errors > 0) {
63		printf("zone %s file %s has %u errors\n", name, fname, errors);
64#ifdef MEMCLEAN /* otherwise, the OS collects memory pages */
65		namedb_close(nsd->db);
66		region_destroy(nsd->options->region);
67#endif
68		exit(1);
69	}
70	printf("zone %s is ok\n", name);
71	namedb_close(nsd->db);
72}
73
74/* dummy functions to link */
75int writepid(struct nsd * ATTR_UNUSED(nsd))
76{
77	        return 0;
78}
79void unlinkpid(const char * ATTR_UNUSED(file))
80{
81}
82void bind8_stats(struct nsd * ATTR_UNUSED(nsd))
83{
84}
85
86void sig_handler(int ATTR_UNUSED(sig))
87{
88}
89
90extern char *optarg;
91extern int optind;
92
93int
94main(int argc, char *argv[])
95{
96	/* Scratch variables... */
97	int c;
98	struct nsd nsd;
99	memset(&nsd, 0, sizeof(nsd));
100
101	log_init("nsd-checkzone");
102
103	/* Parse the command line... */
104	while ((c = getopt(argc, argv, "h")) != -1) {
105		switch (c) {
106		case 'h':
107			usage();
108			exit(0);
109		case '?':
110		default:
111			usage();
112			exit(1);
113		}
114	}
115	argc -= optind;
116	argv += optind;
117
118	/* Commandline parse error */
119	if (argc != 2) {
120		fprintf(stderr, "wrong number of arguments.\n");
121		usage();
122		exit(1);
123	}
124
125	nsd.options = nsd_options_create(region_create_custom(xalloc, free,
126		DEFAULT_CHUNK_SIZE, DEFAULT_LARGE_OBJECT_SIZE,
127		DEFAULT_INITIAL_CLEANUP_SIZE, 1));
128	if (verbosity == 0)
129		verbosity = nsd.options->verbosity;
130
131	check_zone(&nsd, argv[0], argv[1]);
132	region_destroy(nsd.options->region);
133	/* yylex_destroy(); but, not available in all versions of flex */
134
135	exit(0);
136}
137