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

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id: nsupdate.c,v 1.129 2004/03/04 05:33:03 marka Exp $ */
Michael Sawyer's avatar
Michael Sawyer committed
19
20

#include <config.h>
Brian Wellington's avatar
Brian Wellington committed
21

22
23
24
25
26
27
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>

28
#include <isc/app.h>
Michael Sawyer's avatar
Michael Sawyer committed
29
#include <isc/base64.h>
Michael Sawyer's avatar
Michael Sawyer committed
30
#include <isc/buffer.h>
Brian Wellington's avatar
Brian Wellington committed
31
#include <isc/commandline.h>
Michael Sawyer's avatar
Michael Sawyer committed
32
#include <isc/entropy.h>
33
#include <isc/event.h>
34
#include <isc/hash.h>
Michael Sawyer's avatar
Michael Sawyer committed
35
#include <isc/lex.h>
Michael Sawyer's avatar
Michael Sawyer committed
36
#include <isc/mem.h>
37
#include <isc/parseint.h>
Michael Sawyer's avatar
Michael Sawyer committed
38
#include <isc/region.h>
Michael Sawyer's avatar
Michael Sawyer committed
39
40
#include <isc/sockaddr.h>
#include <isc/socket.h>
41
#include <isc/stdio.h>
Michael Sawyer's avatar
Michael Sawyer committed
42
#include <isc/string.h>
Michael Sawyer's avatar
Michael Sawyer committed
43
#include <isc/task.h>
Michael Sawyer's avatar
Michael Sawyer committed
44
#include <isc/timer.h>
Michael Sawyer's avatar
Michael Sawyer committed
45
#include <isc/types.h>
Michael Sawyer's avatar
Michael Sawyer committed
46
#include <isc/util.h>
Michael Sawyer's avatar
Michael Sawyer committed
47

Brian Wellington's avatar
Brian Wellington committed
48
49
#include <dns/callbacks.h>
#include <dns/dispatch.h>
50
#include <dns/dnssec.h>
Brian Wellington's avatar
Brian Wellington committed
51
#include <dns/events.h>
52
#include <dns/fixedname.h>
53
#include <dns/masterdump.h>
Brian Wellington's avatar
Brian Wellington committed
54
55
#include <dns/message.h>
#include <dns/name.h>
56
#include <dns/rcode.h>
Brian Wellington's avatar
Brian Wellington committed
57
58
59
60
61
62
63
64
65
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatastruct.h>
#include <dns/rdatatype.h>
#include <dns/request.h>
#include <dns/result.h>
#include <dns/tsig.h>
66

Brian Wellington's avatar
Brian Wellington committed
67
68
#include <dst/dst.h>

69
#include <lwres/lwres.h>
70
#include <lwres/net.h>
71

72
73
#include <bind9/getaddresses.h>

74
75
76
77
78
79
80
81
#ifdef HAVE_ADDRINFO
#ifdef HAVE_GETADDRINFO
#ifdef HAVE_GAISTRERROR
#define USE_GETADDRINFO
#endif
#endif
#endif

Danny Mayer's avatar
Danny Mayer committed
82
83
#ifndef USE_GETADDRINFO
#ifndef ISC_PLATFORM_NONSTDHERRNO
84
85
extern int h_errno;
#endif
Danny Mayer's avatar
Danny Mayer committed
86
#endif
87

88
#define MAXCMD (4 * 1024)
89
#define MAXWIRE (64 * 1024)
90
91
92
#define PACKETSIZE ((64 * 1024) - 1)
#define INITTEXT (2 * 1024)
#define MAXTEXT (128 * 1024)
Michael Sawyer's avatar
Michael Sawyer committed
93
#define FIND_TIMEOUT 5
94
#define TTL_MAX 2147483647U	/* Maximum signed 32 bit integer. */
Michael Sawyer's avatar
Michael Sawyer committed
95

96
97
#define DNSDEFAULTPORT 53

98
#ifndef RESOLV_CONF
Michael Sawyer's avatar
Michael Sawyer committed
99
#define RESOLV_CONF "/etc/resolv.conf"
100
#endif
Michael Sawyer's avatar
Michael Sawyer committed
101

Brian Wellington's avatar
Brian Wellington committed
102
static isc_boolean_t debugging = ISC_FALSE, ddebugging = ISC_FALSE;
103
static isc_boolean_t memdebugging = ISC_FALSE;
Brian Wellington's avatar
Brian Wellington committed
104
static isc_boolean_t have_ipv4 = ISC_FALSE;
Brian Wellington's avatar
Brian Wellington committed
105
106
static isc_boolean_t have_ipv6 = ISC_FALSE;
static isc_boolean_t is_dst_up = ISC_FALSE;
Brian Wellington's avatar
Brian Wellington committed
107
static isc_boolean_t usevc = ISC_FALSE;
Brian Wellington's avatar
Brian Wellington committed
108
109
static isc_taskmgr_t *taskmgr = NULL;
static isc_task_t *global_task = NULL;
110
static isc_event_t *global_event = NULL;
Brian Wellington's avatar
Brian Wellington committed
111
112
113
114
115
116
static isc_mem_t *mctx = NULL;
static dns_dispatchmgr_t *dispatchmgr = NULL;
static dns_requestmgr_t *requestmgr = NULL;
static isc_socketmgr_t *socketmgr = NULL;
static isc_timermgr_t *timermgr = NULL;
static dns_dispatch_t *dispatchv4 = NULL;
Brian Wellington's avatar
Brian Wellington committed
117
static dns_dispatch_t *dispatchv6 = NULL;
Brian Wellington's avatar
Brian Wellington committed
118
static dns_message_t *updatemsg = NULL;
119
120
static dns_fixedname_t fuserzone;
static dns_name_t *userzone = NULL;
121
122
static dns_tsigkey_t *tsigkey = NULL;
static dst_key_t *sig0key;
123
124
static lwres_context_t *lwctx = NULL;
static lwres_conf_t *lwconf;
125
static isc_sockaddr_t *servers;
Brian Wellington's avatar
Brian Wellington committed
126
static int ns_inuse = 0;
127
128
static int ns_total = 0;
static isc_sockaddr_t *userserver = NULL;
129
static isc_sockaddr_t *localaddr = NULL;
130
static char *keystr = NULL, *keyfile = NULL;
Brian Wellington's avatar
Brian Wellington committed
131
static isc_entropy_t *entp = NULL;
132
static isc_boolean_t shuttingdown = ISC_FALSE;
133
static FILE *input;
134
static isc_boolean_t interactive = ISC_TRUE;
135
static isc_boolean_t seenerror = ISC_FALSE;
Danny Mayer's avatar
Danny Mayer committed
136
static const dns_master_style_t *style;
137
static int requests = 0;
Michael Graff's avatar
Michael Graff committed
138
139
140
static unsigned int timeout = 300;
static unsigned int udp_timeout = 3;
static unsigned int udp_retries = 3;
141
142
static dns_rdataclass_t defaultclass = dns_rdataclass_in;
static dns_rdataclass_t zoneclass = dns_rdataclass_none;
143
static dns_message_t *answer = NULL;
Brian Wellington's avatar
Brian Wellington committed
144

145
146
147
148
149
typedef struct nsu_requestinfo {
	dns_message_t *msg;
	isc_sockaddr_t *addr;
} nsu_requestinfo_t;

150
static void
151
152
sendrequest(isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
	    dns_message_t *msg, dns_request_t **request);
153
154
155
156
157
158
159
160
static void
fatal(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);

static void
debug(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);

static void
ddebug(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
Michael Sawyer's avatar
Michael Sawyer committed
161

162
163
164
165
#define STATUS_MORE	(isc_uint16_t)0
#define STATUS_SEND	(isc_uint16_t)1
#define STATUS_QUIT	(isc_uint16_t)2
#define STATUS_SYNTAX	(isc_uint16_t)3
Michael Sawyer's avatar
Michael Sawyer committed
166

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
static dns_rdataclass_t
getzoneclass(void) {
	if (zoneclass == dns_rdataclass_none)
		zoneclass = defaultclass;
	return (zoneclass);
}

static isc_boolean_t
setzoneclass(dns_rdataclass_t rdclass) {
	if (zoneclass == dns_rdataclass_none ||
	    rdclass == dns_rdataclass_none)
		zoneclass = rdclass;
	if (zoneclass != rdclass)
		return (ISC_FALSE);
	return (ISC_TRUE);
}

Michael Sawyer's avatar
Michael Sawyer committed
184
185
186
187
static void
fatal(const char *format, ...) {
	va_list args;

188
	va_start(args, format);
Michael Sawyer's avatar
Michael Sawyer committed
189
190
191
	vfprintf(stderr, format, args);
	va_end(args);
	fprintf(stderr, "\n");
Brian Wellington's avatar
Brian Wellington committed
192
	exit(1);
Michael Sawyer's avatar
Michael Sawyer committed
193
194
195
196
197
198
199
}

static void
debug(const char *format, ...) {
	va_list args;

	if (debugging) {
200
		va_start(args, format);
Michael Sawyer's avatar
Michael Sawyer committed
201
202
203
204
205
206
		vfprintf(stderr, format, args);
		va_end(args);
		fprintf(stderr, "\n");
	}
}

207
208
209
210
211
static void
ddebug(const char *format, ...) {
	va_list args;

	if (ddebugging) {
212
		va_start(args, format);
213
214
215
216
217
218
		vfprintf(stderr, format, args);
		va_end(args);
		fprintf(stderr, "\n");
	}
}

219
static inline void
Michael Sawyer's avatar
Michael Sawyer committed
220
check_result(isc_result_t result, const char *msg) {
Brian Wellington's avatar
Brian Wellington committed
221
	if (result != ISC_R_SUCCESS)
Michael Sawyer's avatar
Michael Sawyer committed
222
223
224
		fatal("%s: %s", msg, isc_result_totext(result));
}

225
226
227
static void *
mem_alloc(void *arg, size_t size) {
	return (isc_mem_get(arg, size));
228
229
}

230
231
232
static void
mem_free(void *arg, void *mem, size_t size) {
	isc_mem_put(arg, mem, size);
233
234
235
236
237
238
239
240
241
242
243
244
}

static char *
nsu_strsep(char **stringp, const char *delim) {
	char *string = *stringp;
	char *s;
	const char *d;
	char sc, dc;

	if (string == NULL)
		return (NULL);

245
246
247
248
249
250
251
252
253
254
	for (; *string != '\0'; string++) {
		sc = *string;
		for (d = delim; (dc = *d) != '\0'; d++) {
			if (sc == dc)
				break;
		}
		if (dc == 0)
			break;
	}

255
256
	for (s = string; *s != '\0'; s++) {
		sc = *s;
257
		for (d = delim; (dc = *d) != '\0'; d++) {
258
259
260
261
262
			if (sc == dc) {
				*s++ = '\0';
				*stringp = s;
				return (string);
			}
263
		}
264
265
266
267
268
	}
	*stringp = NULL;
	return (string);
}

Michael Sawyer's avatar
Michael Sawyer committed
269
static void
Brian Wellington's avatar
Brian Wellington committed
270
reset_system(void) {
Michael Sawyer's avatar
Michael Sawyer committed
271
272
	isc_result_t result;

273
	ddebug("reset_system()");
Michael Sawyer's avatar
Michael Sawyer committed
274
275
	/* If the update message is still around, destroy it */
	if (updatemsg != NULL)
Brian Wellington's avatar
Brian Wellington committed
276
277
278
279
		dns_message_reset(updatemsg, DNS_MESSAGE_INTENTRENDER);
	else {
		result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
					    &updatemsg);
280
		check_result(result, "dns_message_create");
Brian Wellington's avatar
Brian Wellington committed
281
	}
Michael Sawyer's avatar
Michael Sawyer committed
282
283
284
	updatemsg->opcode = dns_opcode_update;
}

285
static void
286
setup_keystr(void) {
287
288
289
290
	unsigned char *secret = NULL;
	int secretlen;
	isc_buffer_t secretbuf;
	isc_result_t result;
291
292
293
	isc_buffer_t keynamesrc;
	char *secretstr;
	char *s;
294
295
296
	dns_fixedname_t fkeyname;
	dns_name_t *keyname;

297
298
299
	dns_fixedname_init(&fkeyname);
	keyname = dns_fixedname_name(&fkeyname);

300
	debug("Creating key...");
301

302
303
304
305
	s = strchr(keystr, ':');
	if (s == NULL || s == keystr || *s == 0)
		fatal("key option must specify keyname:secret");
	secretstr = s + 1;
306

307
308
	isc_buffer_init(&keynamesrc, keystr, s - keystr);
	isc_buffer_add(&keynamesrc, s - keystr);
309

310
311
312
313
	debug("namefromtext");
	result = dns_name_fromtext(keyname, &keynamesrc, dns_rootname,
				   ISC_FALSE, NULL);
	check_result(result, "dns_name_fromtext");
314

315
316
317
318
	secretlen = strlen(secretstr) * 3 / 4;
	secret = isc_mem_allocate(mctx, secretlen);
	if (secret == NULL)
		fatal("out of memory");
319

320
321
322
323
324
325
	isc_buffer_init(&secretbuf, secret, secretlen);
	result = isc_base64_decodestring(secretstr, &secretbuf);
	if (result != ISC_R_SUCCESS) {
		fprintf(stderr, "could not create key from %s: %s\n",
			keystr, isc_result_totext(result));
		goto failure;
326
	}
327

328
329
	secretlen = isc_buffer_usedlength(&secretbuf);

330
331
	debug("keycreate");
	result = dns_tsigkey_create(keyname, dns_tsig_hmacmd5_name,
332
				    secret, secretlen, ISC_TRUE, NULL,
333
				    0, 0, mctx, NULL, &tsigkey);
334
	if (result != ISC_R_SUCCESS)
335
		fprintf(stderr, "could not create key from %s: %s\n",
336
			keystr, dns_result_totext(result));
337
338
339
340
341
 failure:
	if (secret != NULL)
		isc_mem_free(mctx, secret);
}

342
343
344
345
346
347
348
349
350
351
352
353
354
355
static void
setup_keyfile(void) {
	dst_key_t *dstkey = NULL;
	isc_result_t result;

	debug("Creating key...");

	result = dst_key_fromnamedfile(keyfile, DST_TYPE_PRIVATE, mctx,
				       &dstkey);
	if (result != ISC_R_SUCCESS) {
		fprintf(stderr, "could not read key from %s: %s\n",
			keyfile, isc_result_totext(result));
		return;
	}
356
357
358
359
360
361
362
363
364
365
366
367
368
	if (dst_key_alg(dstkey) == DST_ALG_HMACMD5) {
		result = dns_tsigkey_createfromkey(dst_key_name(dstkey),
						   dns_tsig_hmacmd5_name,
						   dstkey, ISC_FALSE, NULL,
						   0, 0, mctx, NULL, &tsigkey);
		if (result != ISC_R_SUCCESS) {
			fprintf(stderr, "could not create key from %s: %s\n",
				keyfile, isc_result_totext(result));
			dst_key_free(&dstkey);
			return;
		}
	} else
		sig0key = dstkey;
369
370
}

371
static void
372
doshutdown(void) {
373
	isc_task_detach(&global_task);
374
375
376
377
378
379
380

	if (userserver != NULL)
		isc_mem_put(mctx, userserver, sizeof(isc_sockaddr_t));

	if (localaddr != NULL)
		isc_mem_put(mctx, localaddr, sizeof(isc_sockaddr_t));

381
382
383
384
385
386
387
388
	if (tsigkey != NULL) {
		ddebug("Freeing TSIG key");
		dns_tsigkey_detach(&tsigkey);
	}

	if (sig0key != NULL) {
		ddebug("Freeing SIG(0) key");
		dst_key_free(&sig0key);
389
390
391
392
393
394
	}

	if (updatemsg != NULL)
		dns_message_destroy(&updatemsg);

	if (is_dst_up) {
395
		ddebug("Destroy DST lib");
396
397
398
399
400
		dst_lib_destroy();
		is_dst_up = ISC_FALSE;
	}

	if (entp != NULL) {
401
		ddebug("Detach from entropy");
402
403
404
405
406
407
408
409
		isc_entropy_detach(&entp);
	}

	lwres_conf_clear(lwctx);
	lwres_context_destroy(&lwctx);

	isc_mem_put(mctx, servers, ns_total * sizeof(isc_sockaddr_t));

410
	ddebug("Destroying request manager");
411
412
413
414
415
416
417
418
419
420
421
	dns_requestmgr_detach(&requestmgr);

	ddebug("Freeing the dispatchers");
	if (have_ipv4)
		dns_dispatch_detach(&dispatchv4);
	if (have_ipv6)
		dns_dispatch_detach(&dispatchv6);

	ddebug("Shutting down dispatch manager");
	dns_dispatchmgr_destroy(&dispatchmgr);

422
423
}

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
static void
maybeshutdown(void) {
	ddebug("Shutting down request manager");
	dns_requestmgr_shutdown(requestmgr);

	if (requests != 0)
		return;

	doshutdown();
}

static void
shutdown_program(isc_task_t *task, isc_event_t *event) {
	REQUIRE(task == global_task);
	UNUSED(task);

	ddebug("shutdown_program()");
	isc_event_free(&event);

	shuttingdown = ISC_TRUE;
	maybeshutdown();
}

Michael Sawyer's avatar
Michael Sawyer committed
447
static void
Brian Wellington's avatar
Brian Wellington committed
448
setup_system(void) {
Michael Sawyer's avatar
Michael Sawyer committed
449
	isc_result_t result;
Brian Wellington's avatar
Brian Wellington committed
450
	isc_sockaddr_t bind_any, bind_any6;
451
	lwres_result_t lwresult;
Brian Wellington's avatar
Brian Wellington committed
452
	unsigned int attrs, attrmask;
453
	int i;
Michael Sawyer's avatar
Michael Sawyer committed
454

455
	ddebug("setup_system()");
Michael Sawyer's avatar
Michael Sawyer committed
456

457
458
	dns_result_register();

Michael Sawyer's avatar
Michael Sawyer committed
459
	result = isc_net_probeipv4();
Brian Wellington's avatar
Brian Wellington committed
460
461
	if (result == ISC_R_SUCCESS)
		have_ipv4 = ISC_TRUE;
Michael Sawyer's avatar
Michael Sawyer committed
462
463
464

	result = isc_net_probeipv6();
	if (result == ISC_R_SUCCESS)
Brian Wellington's avatar
Brian Wellington committed
465
		have_ipv6 = ISC_TRUE;
Michael Sawyer's avatar
Michael Sawyer committed
466

Brian Wellington's avatar
Brian Wellington committed
467
	if (!have_ipv4 && !have_ipv6)
468
		fatal("could not find either IPv4 or IPv6");
Brian Wellington's avatar
Brian Wellington committed
469

Michael Sawyer's avatar
Michael Sawyer committed
470
471
472
	result = isc_mem_create(0, 0, &mctx);
	check_result(result, "isc_mem_create");

473
474
475
476
	lwresult = lwres_context_create(&lwctx, mctx, mem_alloc, mem_free, 1);
	if (lwresult != LWRES_R_SUCCESS)
		fatal("lwres_context_create failed");

477
	(void)lwres_conf_parse(lwctx, RESOLV_CONF);
478
	lwconf = lwres_conf_get(lwctx);
Brian Wellington's avatar
Brian Wellington committed
479

480
	ns_total = lwconf->nsnext;
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
	if (ns_total <= 0) {
		/* No name servers in resolv.conf; default to loopback. */
		struct in_addr localhost;
		ns_total = 1;
		servers = isc_mem_get(mctx, ns_total * sizeof(isc_sockaddr_t));
		if (servers == NULL)
			fatal("out of memory");
		localhost.s_addr = htonl(INADDR_LOOPBACK);
		isc_sockaddr_fromin(&servers[0], &localhost, DNSDEFAULTPORT);
	} else {
		servers = isc_mem_get(mctx, ns_total * sizeof(isc_sockaddr_t));
		if (servers == NULL)
			fatal("out of memory");
		for (i = 0; i < ns_total; i++) {
			if (lwconf->nameservers[i].family == LWRES_ADDRTYPE_V4) {
				struct in_addr in4;
				memcpy(&in4, lwconf->nameservers[i].address, 4);
				isc_sockaddr_fromin(&servers[i], &in4, DNSDEFAULTPORT);
			} else {
				struct in6_addr in6;
				memcpy(&in6, lwconf->nameservers[i].address, 16);
				isc_sockaddr_fromin6(&servers[i], &in6,
						     DNSDEFAULTPORT);
			}
505
506
507
		}
	}

508
509
510
	result = isc_entropy_create(mctx, &entp);
	check_result(result, "isc_entropy_create");

511
512
513
514
	result = isc_hash_create(mctx, entp, DNS_NAME_MAXWIRE);
	check_result(result, "isc_hash_create");
	isc_hash_init();

515
	result = dns_dispatchmgr_create(mctx, entp, &dispatchmgr);
Michael Sawyer's avatar
Michael Sawyer committed
516
517
518
519
520
521
522
523
	check_result(result, "dns_dispatchmgr_create");

	result = isc_socketmgr_create(mctx, &socketmgr);
	check_result(result, "dns_socketmgr_create");

	result = isc_timermgr_create(mctx, &timermgr);
	check_result(result, "dns_timermgr_create");

524
	result = isc_taskmgr_create(mctx, 1, 0, &taskmgr);
Michael Sawyer's avatar
Michael Sawyer committed
525
526
	check_result(result, "isc_taskmgr_create");

527
	result = isc_task_create(taskmgr, 0, &global_task);
Michael Sawyer's avatar
Michael Sawyer committed
528
529
	check_result(result, "isc_task_create");

530
531
532
	result = isc_task_onshutdown(global_task, shutdown_program, NULL);
	check_result(result, "isc_task_onshutdown");

533
	result = dst_lib_init(mctx, entp, 0);
Michael Sawyer's avatar
Michael Sawyer committed
534
535
536
	check_result(result, "dst_lib_init");
	is_dst_up = ISC_TRUE;

Brian Wellington's avatar
Brian Wellington committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
	attrmask = DNS_DISPATCHATTR_UDP | DNS_DISPATCHATTR_TCP;
	attrmask |= DNS_DISPATCHATTR_IPV4 | DNS_DISPATCHATTR_IPV6;

	if (have_ipv6) {
		attrs = DNS_DISPATCHATTR_UDP;
		attrs |= DNS_DISPATCHATTR_MAKEQUERY;
		attrs |= DNS_DISPATCHATTR_IPV6;
		isc_sockaddr_any6(&bind_any6);
		result = dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr,
					     &bind_any6, PACKETSIZE,
					     4, 2, 3, 5,
					     attrs, attrmask, &dispatchv6);
		check_result(result, "dns_dispatch_getudp (v6)");
	}
Michael Sawyer's avatar
Michael Sawyer committed
551

Brian Wellington's avatar
Brian Wellington committed
552
553
554
555
556
557
558
559
560
561
562
	if (have_ipv4) {
		attrs = DNS_DISPATCHATTR_UDP;
		attrs |= DNS_DISPATCHATTR_MAKEQUERY;
		attrs |= DNS_DISPATCHATTR_IPV4;
		isc_sockaddr_any(&bind_any);
		result = dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr,
					     &bind_any, PACKETSIZE,
					     4, 2, 3, 5,
					     attrs, attrmask, &dispatchv4);
		check_result(result, "dns_dispatch_getudp (v4)");
	}
563

Michael Sawyer's avatar
Michael Sawyer committed
564
565
	result = dns_requestmgr_create(mctx, timermgr,
				       socketmgr, taskmgr, dispatchmgr,
Brian Wellington's avatar
Brian Wellington committed
566
				       dispatchv4, dispatchv6, &requestmgr);
Michael Sawyer's avatar
Michael Sawyer committed
567
568
	check_result(result, "dns_requestmgr_create");

569
570
571
572
	if (keystr != NULL)
		setup_keystr();
	else if (keyfile != NULL)
		setup_keyfile();
Michael Sawyer's avatar
Michael Sawyer committed
573
}
Michael Sawyer's avatar
Michael Sawyer committed
574

575
576
static void
get_address(char *host, in_port_t port, isc_sockaddr_t *sockaddr) {
577
578
	int count;
	isc_result_t result;
579

580
581
582
583
584
585
586
	isc_app_block();
	result = bind9_getaddresses(host, port, sockaddr, 1, &count);
	isc_app_unblock();
	if (result != ISC_R_SUCCESS)
		fatal("couldn't get address for '%s': %s",
		      host, isc_result_totext(result));
	INSIST(count == 1);
587
588
}

Michael Sawyer's avatar
Michael Sawyer committed
589
static void
Michael Sawyer's avatar
Michael Sawyer committed
590
parse_args(int argc, char **argv) {
Brian Wellington's avatar
Brian Wellington committed
591
	int ch;
592
	isc_result_t result;
Michael Sawyer's avatar
Michael Sawyer committed
593

Michael Sawyer's avatar
Michael Sawyer committed
594
	debug("parse_args");
Michael Graff's avatar
Michael Graff committed
595
596
	while ((ch = isc_commandline_parse(argc, argv, "dDMy:vk:r:t:u:")) != -1)
	{
Brian Wellington's avatar
Brian Wellington committed
597
598
599
600
601
		switch (ch) {
		case 'd':
			debugging = ISC_TRUE;
			break;
		case 'D': /* was -dd */
Michael Sawyer's avatar
Michael Sawyer committed
602
603
			debugging = ISC_TRUE;
			ddebugging = ISC_TRUE;
Brian Wellington's avatar
Brian Wellington committed
604
605
606
			break;
		case 'M': /* was -dm */
			debugging = ISC_TRUE;
Michael Sawyer's avatar
Michael Sawyer committed
607
			ddebugging = ISC_TRUE;
608
609
610
			memdebugging = ISC_TRUE;
			isc_mem_debugging = ISC_MEM_DEBUGTRACE |
					    ISC_MEM_DEBUGRECORD;
Brian Wellington's avatar
Brian Wellington committed
611
612
613
614
615
			break;
		case 'y':
			keystr = isc_commandline_argument;
			break;
		case 'v':
Brian Wellington's avatar
Brian Wellington committed
616
			usevc = ISC_TRUE;
Brian Wellington's avatar
Brian Wellington committed
617
618
			break;
		case 'k':
619
			keyfile = isc_commandline_argument;
Brian Wellington's avatar
Brian Wellington committed
620
			break;
Michael Graff's avatar
Michael Graff committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
		case 't':
			result = isc_parse_uint32(&timeout,
						  isc_commandline_argument, 10);
			if (result != ISC_R_SUCCESS) {
				fprintf(stderr, "bad timeout '%s'\n",						isc_commandline_argument);
				exit(1);
			}
			if (timeout == 0)
				timeout = ULONG_MAX;
			break;
		case 'u':
			result = isc_parse_uint32(&udp_timeout,
						  isc_commandline_argument, 10);
			if (result != ISC_R_SUCCESS) {
				fprintf(stderr, "bad udp timeout '%s'\n",						isc_commandline_argument);
				exit(1);
			}
			if (udp_timeout == 0)
				udp_timeout = ULONG_MAX;
			break;
		case 'r':
			result = isc_parse_uint32(&udp_retries,
						  isc_commandline_argument, 10);
			if (result != ISC_R_SUCCESS) {
				fprintf(stderr, "bad udp retries '%s'\n",						isc_commandline_argument);
				exit(1);
			}
			break;
Brian Wellington's avatar
Brian Wellington committed
649
650
651
		default:
			fprintf(stderr, "%s: invalid argument -%c\n",
				argv[0], ch);
652
			fprintf(stderr, "usage: nsupdate [-d] "
653
654
				"[-y keyname:secret | -k keyfile] [-v] "
				"[filename]\n");
Brian Wellington's avatar
Brian Wellington committed
655
656
			exit(1);
		}
Michael Sawyer's avatar
Michael Sawyer committed
657
	}
658
	if (keyfile != NULL && keystr != NULL) {
659
660
		fprintf(stderr, "%s: cannot specify both -k and -y\n",
			argv[0]);
661
662
		exit(1);
	}
663
664

	if (argv[isc_commandline_index] != NULL) {
665
666
667
668
669
670
		if (strcmp(argv[isc_commandline_index], "-") == 0) {
			input = stdin;
		} else {
			result = isc_stdio_open(argv[isc_commandline_index],
						"r", &input);
			if (result != ISC_R_SUCCESS) {
671
				fprintf(stderr, "could not open '%s': %s\n",
672
673
674
675
					argv[isc_commandline_index],
					isc_result_totext(result));
				exit(1);
			}
676
		}
677
		interactive = ISC_FALSE;
678
	}
Michael Sawyer's avatar
Michael Sawyer committed
679
680
681
}

static isc_uint16_t
682
parse_name(char **cmdlinep, dns_message_t *msg, dns_name_t **namep) {
Michael Sawyer's avatar
Michael Sawyer committed
683
	isc_result_t result;
684
	char *word;
Brian Wellington's avatar
Brian Wellington committed
685
	isc_buffer_t *namebuf = NULL;
Michael Sawyer's avatar
Michael Sawyer committed
686
	isc_buffer_t source;
Michael Sawyer's avatar
Michael Sawyer committed
687

688
	word = nsu_strsep(cmdlinep, " \t\r\n");
689
	if (*word == 0) {
690
		fprintf(stderr, "could not read owner name\n");
Brian Wellington's avatar
Brian Wellington committed
691
		return (STATUS_SYNTAX);
Michael Sawyer's avatar
Michael Sawyer committed
692
693
	}

694
	result = dns_message_gettempname(msg, namep);
Michael Sawyer's avatar
Michael Sawyer committed
695
	check_result(result, "dns_message_gettempname");
696
	result = isc_buffer_allocate(mctx, &namebuf, DNS_NAME_MAXWIRE);
Michael Sawyer's avatar
Michael Sawyer committed
697
	check_result(result, "isc_buffer_allocate");
698
699
700
	dns_name_init(*namep, NULL);
	dns_name_setbuffer(*namep, namebuf);
	dns_message_takebuffer(msg, &namebuf);
701
702
	isc_buffer_init(&source, word, strlen(word));
	isc_buffer_add(&source, strlen(word));
703
	result = dns_name_fromtext(*namep, &source, dns_rootname,
Michael Sawyer's avatar
Michael Sawyer committed
704
705
				   ISC_FALSE, NULL);
	check_result(result, "dns_name_fromtext");
706
707
708
709
710
711
	isc_buffer_invalidate(&source);
	return (STATUS_MORE);
}

static isc_uint16_t
parse_rdata(char **cmdlinep, dns_rdataclass_t rdataclass,
712
	    dns_rdatatype_t rdatatype, dns_message_t *msg,
713
	    dns_rdata_t *rdata)
714
715
{
	char *cmdline = *cmdlinep;
716
717
	isc_buffer_t source, *buf = NULL, *newbuf = NULL;
	isc_region_t r;
718
719
720
721
	isc_lex_t *lex = NULL;
	dns_rdatacallbacks_t callbacks;
	isc_result_t result;

722
	while (*cmdline != 0 && isspace((unsigned char)*cmdline))
723
724
725
		cmdline++;

	if (*cmdline != 0) {
726
		dns_rdatacallbacks_init(&callbacks);
727
728
729
730
731
732
733
734
735
		result = isc_lex_create(mctx, strlen(cmdline), &lex);
		check_result(result, "isc_lex_create");
		isc_buffer_init(&source, cmdline, strlen(cmdline));
		isc_buffer_add(&source, strlen(cmdline));
		result = isc_lex_openbuffer(lex, &source);
		check_result(result, "isc_lex_openbuffer");
		result = isc_buffer_allocate(mctx, &buf, MAXWIRE);
		check_result(result, "isc_buffer_allocate");
		result = dns_rdata_fromtext(rdata, rdataclass, rdatatype, lex,
736
					    dns_rootname, 0, mctx, buf,
737
738
739
740
741
					    &callbacks);
		isc_lex_destroy(&lex);
		if (result == ISC_R_SUCCESS) {
			isc_buffer_usedregion(buf, &r);
			result = isc_buffer_allocate(mctx, &newbuf, r.length);
742
			check_result(result, "isc_buffer_allocate");
743
744
745
746
747
748
			isc_buffer_putmem(newbuf, r.base, r.length);
			isc_buffer_usedregion(newbuf, &r);
			dns_rdata_fromregion(rdata, rdataclass, rdatatype, &r);
			isc_buffer_free(&buf);
			dns_message_takebuffer(msg, &newbuf);
		} else {
749
750
			fprintf(stderr, "invalid rdata format: %s\n",
				isc_result_totext(result));
751
			isc_buffer_free(&buf);
752
753
			return (STATUS_SYNTAX);
		}
754
755
	} else {
		rdata->flags = DNS_RDATA_UPDATE;
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
	}
	*cmdlinep = cmdline;
	return (STATUS_MORE);
}

static isc_uint16_t
make_prereq(char *cmdline, isc_boolean_t ispositive, isc_boolean_t isrrset) {
	isc_result_t result;
	char *word;
	dns_name_t *name = NULL;
	isc_textregion_t region;
	dns_rdataset_t *rdataset = NULL;
	dns_rdatalist_t *rdatalist = NULL;
	dns_rdataclass_t rdataclass;
	dns_rdatatype_t rdatatype;
	dns_rdata_t *rdata = NULL;
	isc_uint16_t retval;

774
	ddebug("make_prereq()");
775
776
777
778
779
780
781
782

	/*
	 * Read the owner name
	 */
	retval = parse_name(&cmdline, updatemsg, &name);
	if (retval != STATUS_MORE)
		return (retval);

Brian Wellington's avatar
Brian Wellington committed
783
784
785
786
	/*
	 * If this is an rrset prereq, read the class or type.
	 */
	if (isrrset) {
787
		word = nsu_strsep(&cmdline, " \t\r\n");
788
		if (*word == 0) {
789
			fprintf(stderr, "could not read class or type\n");
790
			goto failure;
Brian Wellington's avatar
Brian Wellington committed
791
		}
792
793
		region.base = word;
		region.length = strlen(word);
Brian Wellington's avatar
Brian Wellington committed
794
795
		result = dns_rdataclass_fromtext(&rdataclass, &region);
		if (result == ISC_R_SUCCESS) {
796
797
798
799
			if (!setzoneclass(rdataclass)) {
				fprintf(stderr, "class mismatch: %s\n", word);
				goto failure;
			}
Brian Wellington's avatar
Brian Wellington committed
800
801
802
			/*
			 * Now read the type.
			 */
803
			word = nsu_strsep(&cmdline, " \t\r\n");
804
			if (*word == 0) {
805
				fprintf(stderr, "could not read type\n");
806
				goto failure;
Brian Wellington's avatar
Brian Wellington committed
807
			}
808
809
			region.base = word;
			region.length = strlen(word);
Brian Wellington's avatar
Brian Wellington committed
810
			result = dns_rdatatype_fromtext(&rdatatype, &region);
811
			if (result != ISC_R_SUCCESS) {
Brian Wellington's avatar
Brian Wellington committed
812
				fprintf(stderr, "invalid type: %s\n", word);
813
814
				goto failure;
			}
Brian Wellington's avatar
Brian Wellington committed
815
		} else {
816
			rdataclass = getzoneclass();
Brian Wellington's avatar
Brian Wellington committed
817
			result = dns_rdatatype_fromtext(&rdatatype, &region);
818
			if (result != ISC_R_SUCCESS) {
Brian Wellington's avatar
Brian Wellington committed
819
				fprintf(stderr, "invalid type: %s\n", word);
820
821
				goto failure;
			}
Brian Wellington's avatar
Brian Wellington committed
822
823
824
		}
	} else
		rdatatype = dns_rdatatype_any;
Michael Sawyer's avatar
Michael Sawyer committed
825

826
827
828
829
830
831
832
	result = dns_message_gettemprdata(updatemsg, &rdata);
	check_result(result, "dns_message_gettemprdata");

	rdata->data = NULL;
	rdata->length = 0;

	if (isrrset && ispositive) {
833
		retval = parse_rdata(&cmdline, rdataclass, rdatatype,
834
				     updatemsg, rdata);
835
		if (retval != STATUS_MORE)
836
			goto failure;
837
838
	} else
		rdata->flags = DNS_RDATA_UPDATE;
Michael Sawyer's avatar
Michael Sawyer committed
839

840
841
	result = dns_message_gettemprdatalist(updatemsg, &rdatalist);
	check_result(result, "dns_message_gettemprdatalist");
Michael Sawyer's avatar
Michael Sawyer committed
842
843
	result = dns_message_gettemprdataset(updatemsg, &rdataset);
	check_result(result, "dns_message_gettemprdataset");
844
	dns_rdatalist_init(rdatalist);
Brian Wellington's avatar
Brian Wellington committed
845
	rdatalist->type = rdatatype;
846
847
848
849
850
851
	if (ispositive) {
		if (isrrset && rdata->data != NULL)
			rdatalist->rdclass = rdataclass;
		else
			rdatalist->rdclass = dns_rdataclass_any;
	} else
Brian Wellington's avatar
Brian Wellington committed
852
		rdatalist->rdclass = dns_rdataclass_none;
853
854
	rdatalist->covers = 0;
	rdatalist->ttl = 0;
Brian Wellington's avatar
Brian Wellington committed
855
856
	rdata->rdclass = rdatalist->rdclass;
	rdata->type = rdatatype;
857
858
859
	ISC_LIST_INIT(rdatalist->rdata);
	ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
	dns_rdataset_init(rdataset);
860
	dns_rdatalist_tordataset(rdatalist, rdataset);
Michael Sawyer's avatar
Michael Sawyer committed
861
862
863
	ISC_LIST_INIT(name->list);
	ISC_LIST_APPEND(name->list, rdataset, link);
	dns_message_addname(updatemsg, name, DNS_SECTION_PREREQUISITE);
Brian Wellington's avatar
Brian Wellington committed
864
	return (STATUS_MORE);
865
866
867
868
869

 failure:
	if (name != NULL)
		dns_message_puttempname(updatemsg, &name);
	return (STATUS_SYNTAX);
Michael Sawyer's avatar
Michael Sawyer committed
870
}
Brian Wellington's avatar
Brian Wellington committed
871

Michael Sawyer's avatar
Michael Sawyer committed
872
static isc_uint16_t
873
874
evaluate_prereq(char *cmdline) {
	char *word;
Brian Wellington's avatar
Brian Wellington committed
875
	isc_boolean_t ispositive, isrrset;
Michael Sawyer's avatar
Michael Sawyer committed
876

877
	ddebug("evaluate_prereq()");
878
	word = nsu_strsep(&cmdline, " \t\r\n");
879
	if (*word == 0) {
880
		fprintf(stderr, "could not read operation code\n");
Brian Wellington's avatar
Brian Wellington committed
881
882
		return (STATUS_SYNTAX);
	}
883
	if (strcasecmp(word, "nxdomain") == 0) {
Brian Wellington's avatar
Brian Wellington committed
884
885
		ispositive = ISC_FALSE;
		isrrset = ISC_FALSE;
886
	} else if (strcasecmp(word, "yxdomain") == 0) {
Brian Wellington's avatar
Brian Wellington committed
887
888
		ispositive = ISC_TRUE;
		isrrset = ISC_FALSE;
889
	} else if (strcasecmp(word, "nxrrset") == 0) {
Brian Wellington's avatar
Brian Wellington committed
890
891
		ispositive = ISC_FALSE;
		isrrset = ISC_TRUE;
892
	} else if (strcasecmp(word, "yxrrset") == 0) {
Brian Wellington's avatar
Brian Wellington committed
893
894
895
		ispositive = ISC_TRUE;
		isrrset = ISC_TRUE;
	} else {
896
		fprintf(stderr, "incorrect operation code: %s\n", word);
Brian Wellington's avatar
Brian Wellington committed
897
		return (STATUS_SYNTAX);
Michael Sawyer's avatar
Michael Sawyer committed
898
	}
899
	return (make_prereq(cmdline, ispositive, isrrset));
Michael Sawyer's avatar
Michael Sawyer committed
900
901
902
}

static isc_uint16_t
903
evaluate_server(char *cmdline) {
904
	char *word, *server;
905
	long port;
906
907
908

	word = nsu_strsep(&cmdline, " \t\r\n");
	if (*word == 0) {
909
		fprintf(stderr, "could not read server name\n");
910
911
912
913
914
915
916
917
918
919
920
		return (STATUS_SYNTAX);
	}
	server = word;

	word = nsu_strsep(&cmdline, " \t\r\n");
	if (*word == 0)
		port = DNSDEFAULTPORT;
	else {
		char *endp;
		port = strtol(word, &endp, 10);
		if (*endp != 0) {
921
			fprintf(stderr, "port '%s' is not numeric\n", word);
922
			return (STATUS_SYNTAX);
923
924
925
926
		} else if (port < 1 || port > 65535) {
			fprintf(stderr, "port '%s' is out of range "
				"(1 to 65535)\n", word);
			return (STATUS_SYNTAX);
927
928
929
930
931
932
933
934
935
		}
	}

	if (userserver == NULL) {
		userserver = isc_mem_get(mctx, sizeof(isc_sockaddr_t));
		if (userserver == NULL)
			fatal("out of memory");
	}

936
	get_address(server, (in_port_t)port, userserver);
937

Brian Wellington's avatar
Brian Wellington committed
938
	return (STATUS_MORE);
Michael Sawyer's avatar
Michael Sawyer committed
939
940
}

941
942
943
944
static isc_uint16_t
evaluate_local(char *cmdline) {
	char *word, *local;
	long port;
945
946
	struct in_addr in4;
	struct in6_addr in6;
947
948
949

	word = nsu_strsep(&cmdline, " \t\r\n");
	if (*word == 0) {
950
		fprintf(stderr, "could not read server name\n");
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
		return (STATUS_SYNTAX);
	}
	local = word;

	word = nsu_strsep(&cmdline, " \t\r\n");
	if (*word == 0)
		port = 0;
	else {
		char *endp;
		port = strtol(word, &endp, 10);
		if (*endp != 0) {
			fprintf(stderr, "port '%s' is not numeric\n", word);
			return (STATUS_SYNTAX);
		} else if (port < 1 || port > 65535) {
			fprintf(stderr, "port '%s' is out of range "
				"(1 to 65535)\n", word);
			return (STATUS_SYNTAX);
		}
	}

	if (localaddr == NULL) {
		localaddr = isc_mem_get(mctx, sizeof(isc_sockaddr_t));
		if (localaddr == NULL)
			fatal("out of memory");
	}

977
978
979
980
981
982
983
984
	if (have_ipv6 && inet_pton(AF_INET6, local, &in6) == 1)
		isc_sockaddr_fromin6(localaddr, &in6, (in_port_t)port);
	else if (have_ipv4 && inet_pton(AF_INET, local, &in4) == 1)
		isc_sockaddr_fromin(localaddr, &in4, (in_port_t)port);
	else {
		fprintf(stderr, "invalid address %s", local);
		return (STATUS_SYNTAX);
	}
985
986
987
988

	return (STATUS_MORE);
}

989
990
991
992
993
994
995
996
997
998
999
1000
static isc_uint16_t
evaluate_key(char *cmdline) {
	char *namestr;
	char *secretstr;
	isc_buffer_t b;
	isc_result_t result;
	dns_fixedname_t fkeyname;
	dns_name_t *keyname;
	int secretlen;
	unsigned char *secret = NULL;
	isc_buffer_t secretbuf;

For faster browsing, not all history is shown. View entire blame