summaryrefslogtreecommitdiff
path: root/usr.sbin/nsd/nsd-patch.c
blob: 46d4afeebc6526ea9ee8ce3ddf0194f0fb508e93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * nsd-patch - read database and ixfrs and patch up zone files.
 *
 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
 *
 * See LICENSE for the license.
 *
 */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "options.h"
#include "difffile.h"
#include "namedb.h"
#include "util.h"

extern char *optarg;
extern int optind;

static void
usage(void)
{
	fprintf(stderr, "usage: nsd-patch [options]\n");
	fprintf(stderr, "       Reads database and ixfrs and patches up zone files.\n");
	fprintf(stderr, "       Version %s. Report bugs to <%s>.\n\n", PACKAGE_VERSION, PACKAGE_BUGREPORT);
	fprintf(stderr, "-c configfile	Specify config file to use, instead of %s\n", CONFIGFILE);
	fprintf(stderr, "-f		Force writing of zone files.\n");
	fprintf(stderr, "-h		Print this help information.\n");
	fprintf(stderr, "-l		List contents of transfer journal difffile, %s\n", DIFFFILE);
	fprintf(stderr, "-o dbfile	Specify dbfile to output the result "
			"directly to dbfile, nsd.db.\n");
	fprintf(stderr, "-s		Skip writing of zone files.\n");
	fprintf(stderr, "-x difffile	Specify diff file to use, instead of diff file from config.\n");
	exit(1);
}

static void
list_xfr(FILE *in)
{
	uint32_t timestamp[2];
	uint32_t skiplen, len, new_serial;
	char zone_name[3072];
	uint16_t id;
	uint32_t seq_nr, len2;

	if(!diff_read_32(in, &timestamp[0]) ||
		!diff_read_32(in, &timestamp[1]) ||
		!diff_read_32(in, &len) ||
		!diff_read_str(in, zone_name, sizeof(zone_name)) ||
		!diff_read_32(in, &new_serial) ||
		!diff_read_16(in, &id) ||
		!diff_read_32(in, &seq_nr)) {
		fprintf(stderr, "incomplete zone transfer content packet\n");
		return;
	}
	skiplen = len - (sizeof(uint32_t)*3 + sizeof(uint16_t) + strlen(zone_name));
	fprintf(stdout, "zone %s transfer id %x serial %d timestamp %u.%u: "
			"seq_nr %d of %d bytes\n", zone_name, id, new_serial,
		timestamp[0], timestamp[1], seq_nr, skiplen);

	if(fseeko(in, skiplen, SEEK_CUR) == -1)
		fprintf(stderr, "fseek failed: %s\n", strerror(errno));

	if(!diff_read_32(in, &len2)) {
		fprintf(stderr, "incomplete zone transfer content packet\n");
		return;
	}
	if(len != len2) {
		fprintf(stderr, "packet seq %d had bad length check bytes!\n",
			seq_nr);
	}
}

static const char*
get_date(const char* log)
{
	const char *entry = strstr(log, " time ");
	time_t t = 0;
	static char timep[30];
	if(!entry) return "<notime>";
	t = strtol(entry + 6, NULL, 10);
	if(t == 0) return "0";
	timep[0]=0;
	strlcpy(timep, ctime(&t), sizeof(timep));
	/* remove newline at end */
	if(strlen(timep) > 0 && timep[strlen(timep)-1]=='\n')
		timep[strlen(timep)-1] = 0;
	return timep;
}

static void
list_commit(FILE *in)
{
	uint32_t timestamp[2];
	uint32_t len;
	char zone_name[3072];
	uint32_t old_serial, new_serial;
	uint16_t id;
	uint32_t num;
	uint8_t commit;
	char log_msg[10240];
	uint32_t len2;

	if(!diff_read_32(in, &timestamp[0]) ||
		!diff_read_32(in, &timestamp[1]) ||
		!diff_read_32(in, &len) ||
		!diff_read_str(in, zone_name, sizeof(zone_name)) ||
		!diff_read_32(in, &old_serial) ||
		!diff_read_32(in, &new_serial) ||
		!diff_read_16(in, &id) ||
		!diff_read_32(in, &num) ||
		!diff_read_8(in, &commit) ||
		!diff_read_str(in, log_msg, sizeof(log_msg)) ||
		!diff_read_32(in, &len2)) {
		fprintf(stderr, "incomplete commit/rollback packet\n");
		return;
	}
	fprintf(stdout, "zone %s transfer id %x serial %d: %s of %d packets\n",
		zone_name, id, new_serial, commit?"commit":"rollback", num);
	fprintf(stdout, "   time %s, from serial %d, log message: %s\n",
		get_date(log_msg), old_serial, log_msg);
	if(len != len2) {
		fprintf(stderr, "  commit packet with bad length check \
			bytes!\n");
	}
}

static void
debug_list(struct nsd_options* opt)
{
	const char* file = opt->difffile;
	FILE *f;
	uint32_t type;
	fprintf(stdout, "debug listing of the contents of %s\n", file);
	f = fopen(file, "r");
	if(!f) {
		fprintf(stderr, "error opening %s: %s\n", file,
			strerror(errno));
		return;
	}
	while(diff_read_32(f, &type)) {
		switch(type) {
		case DIFF_PART_IXFR:
			list_xfr(f);
			break;
		case DIFF_PART_SURE:
			list_commit(f);
			break;
		default:
			fprintf(stderr, "bad part of type %x\n", type);
			break;
		}
	}

	fclose(f);
}

static int
exist_difffile(struct nsd_options* opt)
{
	/* see if diff file exists */
	const char* file = opt->difffile;
	FILE *f;

	f = fopen(file, "r");
	if(!f) {
		if(errno == ENOENT)
			return 0;
		fprintf(stderr, "could not open file %s: %s\n",
			file, strerror(errno));
		return 0;
	}
	return 1;
}

static void
print_rrs(FILE* out, struct zone* zone)
{
	rrset_type *rrset;
	domain_type *domain = zone->apex;
	region_type* region = region_create(xalloc, free);
	struct state_pretty_rr* state = create_pretty_rr(region);
	/* first print the SOA record for the zone */
	if(zone->soa_rrset) {
		size_t i;
		for(i=0; i < zone->soa_rrset->rr_count; i++) {
			if(!print_rr(out, state, &zone->soa_rrset->rrs[i])){
				fprintf(stderr, "There was an error "
				   "printing SOARR to zone %s\n",
				   zone->opts->name);
			}
		}
	}
        /* go through entire tree below the zone apex (incl subzones) */
	while(domain && dname_is_subdomain(
		domain_dname(domain), domain_dname(zone->apex)))
	{
		for(rrset = domain->rrsets; rrset; rrset=rrset->next)
		{
			size_t i;
			if(rrset->zone != zone || rrset == zone->soa_rrset)
				continue;
			for(i=0; i < rrset->rr_count; i++) {
				if(!print_rr(out, state, &rrset->rrs[i])){
					fprintf(stderr, "There was an error "
					   "printing RR to zone %s\n",
					   zone->opts->name);
				}
			}
		}
		domain = domain_next(domain);
	}
	region_destroy(region);
}

static void
print_commit_log(FILE* out, const dname_type* zone, struct diff_log* commit_log)
{
	struct diff_log* p = commit_log;
	region_type* region = region_create(xalloc, free);
	while(p)
	{
		const dname_type* dname = dname_parse(region, p->zone_name);
		if(dname_compare(dname, zone) == 0)
		{
			fprintf(out, "; commit");
			if(p->error)
				fprintf(out, "(%s)", p->error);
			fprintf(out, ": %s\n", p->comment);
		}
		p = p->next;
	}
	region_destroy(region);
}

static void
write_to_zonefile(struct zone* zone, struct diff_log* commit_log)
{
	const char* filename = zone->opts->zonefile;
	time_t now = time(0);
	FILE *out;

	fprintf(stderr, "writing zone %s to file %s\n", zone->opts->name,
		filename);

	if(!zone->apex) {
		fprintf(stderr, "zone %s has no apex, no data.\n", filename);
		return;
	}

	out = fopen(filename, "w");
	if(!out) {
		fprintf(stderr, "cannot open or create file %s for writing: %s\n",
			filename, strerror(errno));
		return;
	}

	/* print zone header */
	fprintf(out, "; NSD version %s\n", PACKAGE_VERSION);
	fprintf(out, "; nsd-patch zone %s run at time %s",
		zone->opts->name, ctime(&now));
	print_commit_log(out, domain_dname(zone->apex), commit_log);

	print_rrs(out, zone);

	fclose(out);
}

int main(int argc, char* argv[])
{
	int c;
	const char* configfile = CONFIGFILE;
	const char* difffile = NULL;
	const char* dbfile = NULL;
	nsd_options_t *options;
	struct namedb* db = NULL;
	struct namedb* dbout = NULL;
	struct zone* zone;
	struct diff_log* commit_log = 0;
	size_t fake_child_count = 1;
	int debug_list_diff = 0;
	int force_write = 0;
	int skip_write = 0;
	int difffile_exists = 0;

        /* Parse the command line... */
	while ((c = getopt(argc, argv, "c:fhlo:sx:")) != -1) {
	switch (c) {
		case 'c':
			configfile = optarg;
			break;
		case 'l':
			debug_list_diff = 1;
			break;
		case 'f':
			if (skip_write)
			{
				fprintf(stderr, "Cannot force and skip writing "
						"zonefiles at the same time\n");
				exit(1);
			}
			else
				force_write = 1;
			break;
		case 's':
			if (force_write)
			{
				fprintf(stderr, "Cannot skip and force writing "
						"zonefiles at the same time\n");
				exit(1);
			}
			else
				skip_write = 1;
			break;
		case 'o':
			dbfile = optarg;
			break;
		case 'x':
			difffile = optarg;
			break;
		case 'h':
		default:
			usage();
		};
	}
	argc -= optind;
	argv += optind;
	if (argc != 0)
		usage();

	/* read config file */
	log_init("nsd-patch");
	options = nsd_options_create(region_create(xalloc, free));
	if(!parse_options_file(options, configfile)) {
		fprintf(stderr, "Could not read config: %s\n", configfile);
		exit(1);
	}
	if(options->zonesdir && options->zonesdir[0]) {
		if (chdir(options->zonesdir)) {
			fprintf(stderr, "nsd-patch: cannot chdir to %s: %s\n",
				options->zonesdir, strerror(errno));
			exit(1);
		}
	}

	/* override difffile if commandline option given */
	if(difffile)
		options->difffile = difffile;

	/* see if necessary */
	if(!exist_difffile(options)) {
		fprintf(stderr, "No diff file.\n");
		if (!force_write)
			exit(0);
	}
	else	difffile_exists = 1;

	if(debug_list_diff) {
		debug_list(options);
		exit(0);
	}

	/* read database and diff file */
	fprintf(stdout, "reading database\n");
	db = namedb_open(options->database, options, fake_child_count);
	if(!db) {
		fprintf(stderr, "could not read database: %s\n",
			options->database);
		exit(1);
	}

	/* set all updated to 0 so we know what has changed */
	for(zone = db->zones; zone; zone = zone->next)
	{
		zone->updated = 0;
	}

	if (dbfile)
		dbout = namedb_new(dbfile);
	if (dbout)
	{
		db->fd = dbout->fd;
		db->filename = (char*) dbfile;
	}

	/* read ixfr diff file */
	if (difffile_exists) {
		fprintf(stdout, "reading updates to database\n");
		if(!diff_read_file(db, options, &commit_log, fake_child_count))
		{
			fprintf(stderr, "unable to load the diff file: %s\n",
				options->difffile);
			exit(1);
		}
	}

	if (skip_write)
		fprintf(stderr, "skip patching up zonefiles.\n");
	else {
		fprintf(stdout, "writing changed zones\n");
		for(zone = db->zones; zone; zone = zone->next)
		{
			if(!force_write && !zone->updated) {
				fprintf(stdout, "zone %s had not changed.\n",
					zone->opts->name);
				continue;
			}
			/* write zone to its zone file */
			write_to_zonefile(zone, commit_log);
		}
	}

	/* output result directly to dbfile */
	if (dbout)
	{
		fprintf(stdout, "storing database to %s.\n", dbout->filename);
	        if (namedb_save(db) != 0) {
			fprintf(stderr, "error writing the database (%s): %s\n",
				dbfile, strerror(errno));
			exit(1);
		}
	}
	fprintf(stdout, "done\n");

	return 0;
}