update.c 60.3 KB
Newer Older
Andreas Gustafsson's avatar
Andreas Gustafsson committed
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
/*
 * Copyright (C) 1999  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.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <isc/assertions.h>
#include <isc/error.h>
#include <isc/mem.h>
#include <isc/result.h>
29
#include <isc/taskpool.h>
Andreas Gustafsson's avatar
Andreas Gustafsson committed
30

31
#include <dns/aml.h>
32
#include <dns/confip.h>
Bob Halley's avatar
Bob Halley committed
33
34
35
36
37
#include <dns/db.h>
#include <dns/dbiterator.h>
#include <dns/dbtable.h>
#include <dns/dnssec.h>
#include <dns/events.h>
Andreas Gustafsson's avatar
Andreas Gustafsson committed
38
#include <dns/fixedname.h>
Bob Halley's avatar
Bob Halley committed
39
40
41
42
#include <dns/journal.h>
#include <dns/message.h>
#include <dns/name.h>
#include <dns/nxt.h>
Andreas Gustafsson's avatar
Andreas Gustafsson committed
43
44
45
46
47
#include <dns/rdata.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/rdatastruct.h>
Bob Halley's avatar
Bob Halley committed
48
49
#include <dns/result.h>
#include <dns/types.h>
Andreas Gustafsson's avatar
Andreas Gustafsson committed
50
#include <dns/view.h>
Bob Halley's avatar
Bob Halley committed
51
52
#include <dns/zone.h>
#include <dns/zt.h>
Andreas Gustafsson's avatar
Andreas Gustafsson committed
53
54
55
56

#include <named/globals.h>
#include <named/client.h>
#include <named/update.h>
57
#include <named/log.h>
Andreas Gustafsson's avatar
Andreas Gustafsson committed
58
59
60
61
62
63
64
65
66
67
68

/*
 * This module implements dynamic update as in RFC2136.
 */
  
/*
  XXX TODO:
  - forwarding
  - document strict minimality
*/

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

/*
 * Convenience macro of common isc_log_write() arguments
 * to use in reportings server errors.
 */
#define UPDATE_ERROR_LOGARGS \
	ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE, \
	ISC_LOG_ERROR

/*
 * Convenience macro of common isc_log_write() arguments
 * to use in tracing dynamic update protocol requests.
 */
#define UPDATE_PROTOCOL_LOGARGS \
	ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE, \
	ISC_LOG_INFO

/*
 * Convenience macro of common isc_log_write() arguments
 * to use in low-level debug tracing.
 */
#define UPDATE_DEBUG_LOGARGS \
	ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE, \
	ISC_LOG_DEBUG(8)

95
96
97
98
99
/*
 * Check an operation for failure.  These macros all assume that
 * the function using them has a 'result' variable and a 'failure'
 * label.
 */
100
101
102
103
#define CHECK(op) \
	do { result = (op); 				  	 \
	       if (result != DNS_R_SUCCESS) goto failure; 	 \
	} while (0)
104
105
106

/*
 * Fail unconditionally with result 'code', which must not
107
108
109
110
111
112
113
114
115
116
117
118
 * be DNS_R_SUCCESS.  The reason for failure presumably has
 * been logged already.
 */

#define FAIL(code) \
	do {							\
		result = (code);				\
		goto failure;					\
	} while (0)

/*
 * Fail unconditionally and log as a client error.
119
 */
120
121
#define FAILC(code, msg) \
	do {							\
122
		result = (code);				\
123
124
125
126
127
128
129
130
131
132
133
		isc_log_write(UPDATE_PROTOCOL_LOGARGS,		\
			      "dynamic update failed: %s (%s)",	\
		      	      msg, isc_result_totext(code));	\
		goto failure;					\
	} while (0)

/*
 * Fail unconditionally and log as a server error.
 */
#define FAILS(code, msg) \
	do {							\
134
		result = (code);				\
135
136
137
138
139
		isc_log_write(UPDATE_PROTOCOL_LOGARGS,		\
			      "dynamic update error: %s: %s", 	\
			      msg, isc_result_totext(code));	\
		goto failure;					\
	} while (0)
140
141
142

/**************************************************************************/

Andreas Gustafsson's avatar
Andreas Gustafsson committed
143
144
145
146
typedef struct rr rr_t;

struct rr {
	/* dns_name_t name; */
147
148
149
150
151
152
153
154
155
156
157
	isc_uint32_t 		ttl;
	dns_rdata_t 		rdata;
};

typedef struct update_event update_event_t;

struct update_event {
	ISC_EVENT_COMMON(update_event_t);
	dns_zone_t 		*zone;
	dns_result_t		result;
	
Andreas Gustafsson's avatar
Andreas Gustafsson committed
158
159
};

160
161
162
163
164
165
166
167
/**************************************************************************/
/*
 * Forward declarations.
 */

static void update_action(isc_task_t *task, isc_event_t *event);
static void updatedone_action(isc_task_t *task, isc_event_t *event);

Andreas Gustafsson's avatar
Andreas Gustafsson committed
168
169
170
171
172
/**************************************************************************/

/*
 * Update a single RR in version 'ver' of 'db' and log the
 * update in 'diff'.
173
 *
Andreas Gustafsson's avatar
Andreas Gustafsson committed
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
 * Ensures:
 *   '*tuple' == NULL.  Either the tuple is freed, or its
 *         ownership has been transferred to the diff.
 */
static dns_result_t
do_one_tuple(dns_difftuple_t **tuple,
	     dns_db_t *db, dns_dbversion_t *ver,
	     dns_diff_t *diff)
{
	dns_diff_t temp_diff;
	dns_result_t result;
	
	/* Create a singleton diff */
	dns_diff_init(diff->mctx, &temp_diff);
	ISC_LIST_APPEND(temp_diff.tuples, *tuple, link);

	/* Apply it to the database. */
	result = dns_diff_apply(&temp_diff, db, ver);
	if (result != DNS_R_SUCCESS) {
		dns_difftuple_free(tuple);
		return (result);
	}

	/* Merge it into the current pending journal entry. */
	dns_diff_appendminimal(diff, tuple);

	/* Do not clear temp_diff. */
	
	return (DNS_R_SUCCESS);
}
	      
static dns_result_t
update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
	      dns_diffop_t op, dns_name_t *name, 
	      dns_ttl_t ttl, dns_rdata_t *rdata)
{
	dns_difftuple_t *tuple = NULL;
	dns_result_t result;
	result = dns_difftuple_create(diff->mctx, op,
				      name, ttl, rdata, &tuple);
	if (result != DNS_R_SUCCESS)
		return (result);
	return (do_one_tuple(&tuple, db, ver, diff));
}

/**************************************************************************/
/*
 * Callback-style iteration over rdatasets and rdatas.
 *
 * foreach_rrset() can be used to iterate over the RRsets 
 * of a name and call a callback function with each
 * one.  Similarly, foreach_rr() can be used to iterate
 * over the individual RRs at name, optionally restricted 
 * to RRs of a given type.
 *
 * The callback functions are called "actions" and take
 * two arguments: a void pointer for passing arbitrary
 * context information, and a pointer to the current RRset
 * or RR.  By convention, their names end in "_action".
 */

/*
 * XXXRTH  We might want to make this public somewhere in libdns.
 */

/* Function type for foreach_rrset() iterator actions. */
typedef dns_result_t rrset_func(void *data, dns_rdataset_t *rrset);

/* Function type for foreach_rr() iterator actions. */
typedef dns_result_t rr_func(void *data, rr_t *rr);

/* Internal context struct for foreach_node_rr(). */
typedef struct {
	rr_func *	rr_action;
	void *		rr_action_data;
} foreach_node_rr_ctx_t;

/* Internal helper function for foreach_node_rr(). */
static dns_result_t
foreach_node_rr_action(void *data, dns_rdataset_t *rdataset)
{
	dns_result_t result;
	foreach_node_rr_ctx_t *ctx = data;
	for (result = dns_rdataset_first(rdataset);
	     result == DNS_R_SUCCESS;
	     result = dns_rdataset_next(rdataset))
	{
		rr_t rr;
		dns_rdataset_current(rdataset, &rr.rdata);
		rr.ttl = rdataset->ttl;
		result = (*ctx->rr_action)(ctx->rr_action_data, &rr);
		if (result != DNS_R_SUCCESS)
			return (result);
	}
	if (result != DNS_R_NOMORE)
		return (result);
	return (DNS_R_SUCCESS);
}

/*
 * For each rdataset of 'name' in 'ver' of 'db', call 'action'
 * with the rdataset and 'action_data' as arguments.  If the name
 * does not exist, do nothing. 
 *
 * If 'action' returns an error, abort iteration and return the error.
 */
static dns_result_t
foreach_rrset(dns_db_t *db,
	      dns_dbversion_t *ver,
	      dns_name_t *name,
	      rrset_func *action,
	      void *action_data)
{
	dns_result_t result;
	dns_dbnode_t *node;
	dns_rdatasetiter_t *iter;
	
	node = NULL;
	result = dns_db_findnode(db, name, ISC_FALSE, &node);
	if (result == DNS_R_NOTFOUND)
		return (DNS_R_SUCCESS);
	if (result != DNS_R_SUCCESS)
		return (result);
	
	iter = NULL;
	result = dns_db_allrdatasets(db, node, ver,
				     (isc_stdtime_t) 0, &iter);
	if (result != DNS_R_SUCCESS)
		goto cleanup_node;

	for (result = dns_rdatasetiter_first(iter);
	     result == DNS_R_SUCCESS;
	     result = dns_rdatasetiter_next(iter))
	{
		dns_rdataset_t rdataset;

		dns_rdataset_init(&rdataset);
		dns_rdatasetiter_current(iter, &rdataset);

		result = (*action)(action_data, &rdataset);

		dns_rdataset_disassociate(&rdataset);
		if (result != DNS_R_SUCCESS)
			goto cleanup_iterator;
	}
	if (result == DNS_R_NOMORE)
		result = DNS_R_SUCCESS;

 cleanup_iterator:
	dns_rdatasetiter_destroy(&iter);

 cleanup_node:
	dns_db_detachnode(db, &node);	

	return (result);
}

/*
 * For each RR of 'name' in 'ver' of 'db', call 'action'
 * with the RR and 'action_data' as arguments.  If the name
 * does not exist, do nothing.
 *
 * If 'action' returns an error, abort iteration
 * and return the error.
 */
static dns_result_t
foreach_node_rr(dns_db_t *db,
	    dns_dbversion_t *ver,
	    dns_name_t *name,
	    rr_func *rr_action,
	    void *rr_action_data)
{
	foreach_node_rr_ctx_t ctx;
	ctx.rr_action = rr_action;
	ctx.rr_action_data = rr_action_data;
	return (foreach_rrset(db, ver, name,
			      foreach_node_rr_action, &ctx));
}


/*
Bob Halley's avatar
Bob Halley committed
355
356
357
 * For each of the RRs specified by 'db', 'ver', 'name', 'type',
 * (which can be dns_rdatatype_any to match any type), and 'covers', call
 * 'action' with the RR and 'action_data' as arguments. If the name 
Andreas Gustafsson's avatar
Andreas Gustafsson committed
358
359
360
361
362
363
364
365
366
367
 * does not exist, or if no RRset of the given type exists at the name,
 * do nothing.
 * 
 * If 'action' returns an error, abort iteration and return the error.
 */
static dns_result_t
foreach_rr(dns_db_t *db,
	   dns_dbversion_t *ver,
	   dns_name_t *name,
	   dns_rdatatype_t type,
Bob Halley's avatar
Bob Halley committed
368
	   dns_rdatatype_t covers,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
	   rr_func *rr_action,
	   void *rr_action_data)
{

	dns_result_t result;
	dns_dbnode_t *node;
	dns_rdataset_t rdataset;

	if (type == dns_rdatatype_any)
		return (foreach_node_rr(db, ver, name, 
					rr_action, rr_action_data));
	
	node = NULL;
	result = dns_db_findnode(db, name, ISC_FALSE, &node);
	if (result == DNS_R_NOTFOUND)
		return (DNS_R_SUCCESS);
	if (result != DNS_R_SUCCESS)
		return (result);

	dns_rdataset_init(&rdataset);
Bob Halley's avatar
Bob Halley committed
389
	result = dns_db_findrdataset(db, node, ver, type, covers,
Bob Halley's avatar
Bob Halley committed
390
				     (isc_stdtime_t) 0, &rdataset, NULL);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
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
430
431
432
433
434
435
436
437
438
439
440
441
	if (result == DNS_R_NOTFOUND) {
		result = DNS_R_SUCCESS;
		goto cleanup_node;
	}
	if (result != DNS_R_SUCCESS)
		goto cleanup_node;

	for (result = dns_rdataset_first(&rdataset);
	     result == DNS_R_SUCCESS;
	     result = dns_rdataset_next(&rdataset))
	{
		rr_t rr;
		dns_rdataset_current(&rdataset, &rr.rdata);
		rr.ttl = rdataset.ttl;
		result = (*rr_action)(rr_action_data, &rr);
		if (result != DNS_R_SUCCESS)
			goto cleanup_rdataset;
	}
	if (result != DNS_R_NOMORE)
		goto cleanup_rdataset;
	result = DNS_R_SUCCESS;

 cleanup_rdataset:
	dns_rdataset_disassociate(&rdataset);
 cleanup_node:
	dns_db_detachnode(db, &node);	

	return (result);
}


/**************************************************************************/
/*
 * Various tests on the database contents (for prerequisites, etc).
 */

/*
 * Function type for predicate functions that compare a database RR 'db_rr'
 * against an update RR 'update_rr'.
 */
typedef isc_boolean_t rr_predicate(dns_rdata_t *update_rr, dns_rdata_t *db_rr);

/* Helper function for rrset_exists(). */
static dns_result_t
rrset_exists_action(void *data, rr_t *rr) /*ARGSUSED*/
{
	data = data; /* Unused */
	rr = rr; /* Unused */
	return (DNS_R_EXISTS);
}

442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Utility macro for RR existence checking functions.
 *
 * If the variable 'result' has the value DNS_R_EXISTS or
 * DNS_R_SUCCESS, set *exists to ISC_TRUE or ISC_FALSE,
 * respectively, and return success.
 *
 * If 'result' has any other value, there was a failure.
 * Return the failure result code and do not set *exists.
 *
 * This would be more readable as "do { if ... } while(0)",
 * but that form generates tons of warnings on Solaris 2.6.
 */
#define RETURN_EXISTENCE_FLAG 				\
	return ((result == DNS_R_EXISTS) ? 		\
		(*exists = ISC_TRUE, DNS_R_SUCCESS) : 	\
		((result == DNS_R_SUCCESS) ?		\
		 (*exists = ISC_FALSE, DNS_R_SUCCESS) :	\
		 result));
	
Andreas Gustafsson's avatar
Andreas Gustafsson committed
462
463
464
465
466
467
/*
 * Set '*exists' to true iff an rrset of the given type exists,
 * to false otherwise.
 */
static dns_result_t
rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
Bob Halley's avatar
Bob Halley committed
468
469
	     dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers,
	     isc_boolean_t *exists)
Andreas Gustafsson's avatar
Andreas Gustafsson committed
470
471
{
	dns_result_t result;
Bob Halley's avatar
Bob Halley committed
472
	result = foreach_rr(db, ver, name, type, covers,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
473
474
475
476
477
478
479
480
481
482
483
			    rrset_exists_action, NULL);
	RETURN_EXISTENCE_FLAG;
}

/* Helper function for cname_incompatible_rrset_exists */
static dns_result_t
cname_compatibility_action(void *data, dns_rdataset_t *rrset) 
/*ARGSUSED*/
{
	data = data; /* Unused */
	if (rrset->type != dns_rdatatype_cname &&
484
	    ! dns_rdatatype_isdnssec(rrset->type))
Andreas Gustafsson's avatar
Andreas Gustafsson committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
		return (DNS_R_EXISTS);
	return (DNS_R_SUCCESS);
}

/*
 * Check whether there is an rrset incompatible with adding a CNAME RR,
 * i.e., anything but another CNAME (which can be replaced) or a
 * DNSSEC RR (which can coexist).
 *
 * If such an incompatible rrset exists, set '*exists' to ISC_TRUE.
 * Otherwise, set it to ISC_FALSE.
 */
static dns_result_t
cname_incompatible_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
				dns_name_t *name, isc_boolean_t *exists) {
	dns_result_t result;	
	result = foreach_rrset(db, ver, name, 
			       cname_compatibility_action, NULL);
	RETURN_EXISTENCE_FLAG;
}

/* Helper function for rr_count(). */
static dns_result_t 
count_rr_action(void *data, rr_t *rr) /*ARGSUSED*/ {
	int *countp = data;
	rr = rr; /* Unused. */
	(*countp)++;
	return (DNS_R_SUCCESS);
}

/*
 * Count the number of RRs of 'type' belonging to 'name' in 'ver' of 'db'.
 */
static dns_result_t
rr_count(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
Bob Halley's avatar
Bob Halley committed
520
	 dns_rdatatype_t type, dns_rdatatype_t covers, int *countp)
Andreas Gustafsson's avatar
Andreas Gustafsson committed
521
522
{
	*countp = 0;
Bob Halley's avatar
Bob Halley committed
523
	return (foreach_rr(db, ver, name, type, covers,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
			   count_rr_action, countp));
}

/* Context struct for matching_rr_exists(). */

typedef struct {
	rr_predicate *predicate;
	dns_db_t *db;
	dns_dbversion_t *ver;
	dns_name_t *name;
	dns_rdata_t *update_rr;
} matching_rr_exists_ctx_t;

/* Helper function for matching_rr_exists(). */

static dns_result_t 
matching_rr_exists_action(void *data, rr_t *rr) {
	matching_rr_exists_ctx_t *ctx = data;
	if ((*ctx->predicate)(ctx->update_rr, &rr->rdata))
		return (DNS_R_EXISTS);
	return (DNS_R_SUCCESS);
}

/*
 * Compare the 'update_rr' with all RRs in the RRset specified by 'db', 
 * 'ver', 'name', and 'type' using 'predicate'.  If the predicate returns
 * true for at least one of them, set '*exists' to ISC_TRUE.  Otherwise,
 * set it to ISC_FALSE.
 */
static dns_result_t
matching_rr_exists(rr_predicate *predicate,
	     dns_db_t *db,
	     dns_dbversion_t *ver,
	     dns_name_t *name,
	     dns_rdatatype_t type,
Bob Halley's avatar
Bob Halley committed
559
	     dns_rdatatype_t covers,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
560
561
562
563
564
565
566
567
568
569
	     dns_rdata_t *update_rr,
	     isc_boolean_t *exists)
{
	dns_result_t result;
	matching_rr_exists_ctx_t ctx;
	ctx.predicate = predicate;
	ctx.db = db;
	ctx.ver = ver;
	ctx.name = name;
	ctx.update_rr = update_rr;
Bob Halley's avatar
Bob Halley committed
570
	result = foreach_rr(db, ver, name, type, covers,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
			    matching_rr_exists_action, &ctx);
	RETURN_EXISTENCE_FLAG;
}


/* Context struct and helper function for name_exists() */

static dns_result_t
name_exists_action(void *data, dns_rdataset_t *rrset) /*ARGSUSED*/
{
	data = data; /* Unused */
	rrset = rrset; /* Unused */
	return (DNS_R_EXISTS);
}

/*
 * Set '*exists' to true iff the given name exists, to false otherwise.
 */
static dns_result_t
name_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
	    isc_boolean_t *exists)
{
	dns_result_t result;
	result = foreach_rrset(db, ver, name,
			       name_exists_action, NULL);
	RETURN_EXISTENCE_FLAG;
}

/**************************************************************************/
/*
 * Checking of "RRset exists (value dependent)" prerequisites.
 *
 * In the RFC2136 section 3.2.5, this is the pseudocode involving
 * a variable called "temp", a mapping of <name, type> tuples to rrsets.
 *
 * Here, we represent the "temp" data structure as (non-minimial) "dns_diff_t"
 * where each typle has op==DNS_DIFFOP_EXISTS.
 */


/*
 * Append a tuple asserting the existence of the RR with
 * 'name' and 'rdata' to 'diff'.
 */	     
static dns_result_t
temp_append(dns_diff_t *diff, dns_name_t *name, dns_rdata_t *rdata)
{
	dns_result_t result;
	dns_difftuple_t *tuple = NULL;
	
Bob Halley's avatar
Bob Halley committed
621
	REQUIRE(DNS_DIFF_VALID(diff));
622
623
	CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_EXISTS,
				      name, 0, rdata, &tuple));
Andreas Gustafsson's avatar
Andreas Gustafsson committed
624
	ISC_LIST_APPEND(diff->tuples, tuple, link);
625
626
 failure:
	return (result);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
}

/*
 * Compare two rdatasets represented as sorted lists of tuples.
 * All list elements must have the same owner name and type.
 * Return DNS_R_SUCCESS if the rdatasets are equal, rcode(dns_rcode_nxrrset)
 * if not.
 */
static dns_result_t 
temp_check_rrset(dns_difftuple_t *a, dns_difftuple_t *b) {
	for (;;) {
		if (a == NULL || b == NULL)
			break;
		INSIST(a->op == DNS_DIFFOP_EXISTS && b->op == DNS_DIFFOP_EXISTS);
		INSIST(a->rdata.type == b->rdata.type);
		INSIST(dns_name_equal(&a->name, &b->name));
		if (dns_rdata_compare(&a->rdata, &b->rdata) != 0)
			return (DNS_R_NXRRSET);
		a = ISC_LIST_NEXT(a, link);
		b = ISC_LIST_NEXT(b, link);
	}
	if (a != NULL || b != NULL)
		return (DNS_R_NXRRSET);
	return (DNS_R_SUCCESS);
}

/*
 * A comparison function defining the sorting order for the entries
 * in the "temp" data structure.  The major sort key is the owner name,
 * followed by the type and rdata.
 */
static int 
temp_order(const void *av, const void *bv)
{
	dns_difftuple_t * const *ap = av;
	dns_difftuple_t * const *bp = bv;
	dns_difftuple_t *a = *ap;
	dns_difftuple_t *b = *bp;	
	int r;
	r = dns_name_compare(&a->name, &b->name);
	if (r != 0)
		return (r);
	r = (b->rdata.type - a->rdata.type);
	if (r != 0)
		return (r);
	r = dns_rdata_compare(&a->rdata, &b->rdata);
	return (r);
}

/*
 * Check the "RRset exists (value dependent)" prerequisite information
 * in 'temp' against the contents of the database 'db'.
 *
 * Return DNS_R_SUCCESS if the prerequisites are satisfied,
 * rcode(dns_rcode_nxrrset) if not.
 */

static dns_result_t
temp_check(isc_mem_t *mctx, dns_diff_t *temp, dns_db_t *db,
	   dns_dbversion_t *ver)
{
	dns_result_t result;
	dns_name_t *name;
	dns_dbnode_t *node;
	dns_difftuple_t *t;
	dns_diff_t trash;
	
	/* Exit early if the list is empty (for efficiency only). */
	if (ISC_LIST_HEAD(temp->tuples) == NULL)
		return (DNS_R_SUCCESS);

	/*
	 * Sort the prerequisite records by owner name,
	 * type, and rdata.
	 */
	result = dns_diff_sort(temp, temp_order);
	if (result != DNS_R_SUCCESS)
		return (result);

	dns_diff_init(mctx, &trash);
	
	/*
	 * For each name and type in the prerequisites,
	 * construct a sorted rdata list of the corresponding
	 * database contents, and compare the lists.
	 */
	t = ISC_LIST_HEAD(temp->tuples);
	while (t != NULL) {
		name = &t->name;

		/* A new unique name begins here. */
		node = NULL;
		result = dns_db_findnode(db, name, ISC_FALSE, &node);
		if (result == DNS_R_NOTFOUND)
			return (DNS_R_NXRRSET);
		if (result != DNS_R_SUCCESS)
			return (result);

		/* A new unique type begins here. */		
		while (t != NULL && dns_name_equal(&t->name, name)) {
Bob Halley's avatar
Bob Halley committed
727
			dns_rdatatype_t type, covers;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
728
729
730
731
732
733
734
			dns_rdataset_t rdataset;
			dns_diff_t d_rrs; /* Database RRs with 
						this name and type */
 			dns_diff_t u_rrs; /* Update RRs with
						this name and type */

			type = t->rdata.type;
Bob Halley's avatar
Bob Halley committed
735
736
737
738
			if (type == dns_rdatatype_sig)
				covers = dns_rdata_covers(&t->rdata);
			else
				covers = 0;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
739
740
741
742
743
744
745

			/*
			 * Collect all database RRs for this name and type
			 * onto d_rrs and sort them.
			 */
			dns_rdataset_init(&rdataset);
			result = dns_db_findrdataset(db, node, ver, type,
Bob Halley's avatar
Bob Halley committed
746
						     covers, (isc_stdtime_t) 0,
Bob Halley's avatar
Bob Halley committed
747
						     &rdataset, NULL);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
			if (result != DNS_R_SUCCESS) {
				dns_db_detachnode(db, &node);
				return (DNS_R_NXRRSET);
			}

			dns_diff_init(mctx, &d_rrs);
			dns_diff_init(mctx, &u_rrs);

			for (result = dns_rdataset_first(&rdataset);
			     result == DNS_R_SUCCESS;
			     result = dns_rdataset_next(&rdataset))
			{
				dns_rdata_t rdata;
				dns_rdataset_current(&rdataset, &rdata);
				result = temp_append(&d_rrs, name, &rdata);
				if (result != DNS_R_SUCCESS)
					goto failure;
			}
			if (result != DNS_R_NOMORE)
				goto failure;
			result = dns_diff_sort(&d_rrs, temp_order);
			if (result != DNS_R_SUCCESS) 
				goto failure;

			/*
			 * Collect all update RRs for this name and type
			 * onto u_rrs.  No need to sort them here - 
			 * they are already sorted.
			 */
			while (t != NULL &&
			       dns_name_equal(&t->name, name) &&
			       t->rdata.type == type)
			{
				dns_difftuple_t *next =
					ISC_LIST_NEXT(t, link);
				ISC_LIST_UNLINK(temp->tuples, t, link);
				ISC_LIST_APPEND(u_rrs.tuples, t, link);
				t = next;
			}

			/* Compare the two sorted lists. */
			result = temp_check_rrset(ISC_LIST_HEAD(u_rrs.tuples),
						  ISC_LIST_HEAD(d_rrs.tuples));
			if (result != DNS_R_SUCCESS)
				goto failure;

			/*
			 * We are done with the tuples, but we can't free
			 * them yet because "name" still points into one
			 * of them.  Move them on a temporary list.
			 */
			ISC_LIST_APPENDLIST(trash.tuples, u_rrs.tuples, link);
			ISC_LIST_APPENDLIST(trash.tuples, d_rrs.tuples, link);
			dns_rdataset_disassociate(&rdataset);

			continue;

		    failure:
			dns_diff_clear(&d_rrs);
			dns_diff_clear(&u_rrs);
			dns_diff_clear(&trash);
			dns_rdataset_disassociate(&rdataset);
			dns_db_detachnode(db, &node);
			return (result);
		}

		dns_db_detachnode(db, &node);
	}
	
	dns_diff_clear(&trash);	
	return (DNS_R_SUCCESS);
}

/**************************************************************************/
/*
 * Conditional deletion of RRs.
 */

/* Context structure for delete_if(). */

typedef struct {
	rr_predicate *predicate;
	dns_db_t *db;
	dns_dbversion_t *ver;
	dns_diff_t *diff;
	dns_name_t *name;
	dns_rdata_t *update_rr;
} conditional_delete_ctx_t;

/* Predicate functions for delete_if(). */

/* Return true iff 'update_rr' is neither a SOA nor an NS RR. */
static isc_boolean_t
type_not_soa_nor_ns_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) /*ARGSUSED*/
{
	update_rr = update_rr; /* Unused */
	return ((db_rr->type != dns_rdatatype_soa &&
		 db_rr->type != dns_rdatatype_ns) ?
		ISC_TRUE : ISC_FALSE);
}

/* Return true always. */
static isc_boolean_t
true_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) /*ARGSUSED*/
{
	update_rr = update_rr; /* Unused */ 
	db_rr = db_rr; /* Unused */ 
	return (ISC_TRUE);
}

/* Return true iff the two RRs have identical rdata. */
static isc_boolean_t
rr_equal_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
	/*
	 * XXXRTH  This is not a problem, but we should consider creating
	 *         dns_rdata_equal() (that used dns_name_equal()), since it
	 *         would be faster.  Not a priority.
	 */
	return (dns_rdata_compare(update_rr, db_rr) == 0 ?
		ISC_TRUE : ISC_FALSE);
}

/*
 * Return true iff 'update_rr' should replace 'db_rr' according
 * to the special RFC2136 rules for CNAME, SOA, and WKS records.
 */
static isc_boolean_t
replaces_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
	if (db_rr->type != update_rr->type)
		return (ISC_FALSE);
	if (db_rr->type == dns_rdatatype_cname)
		return (ISC_TRUE);
	if (db_rr->type == dns_rdatatype_soa)
		return (ISC_TRUE);
	/*
	 * RFC2136 does not mention NXT, but multiple NXTs make little
	 * sense, so we replace those, too.
	 */
	if (db_rr->type == dns_rdatatype_nxt)
		return (ISC_TRUE);
	if (db_rr->type == dns_rdatatype_wks) {
		/*
		 * Compare the address and protocol fields only.  These
		 * form the first five bytes of the RR data.  Do a 
		 * raw binary comparison; unpacking the WKS RRs using
		 * dns_rdata_tostruct() might be cleaner in some ways,
		 * but it would require us to pass around an mctx.
		 */
		INSIST(db_rr->length >= 5 && update_rr->length >= 5);
		return (memcmp(db_rr->data, update_rr->data, 5) == 0 ?
			ISC_TRUE : ISC_FALSE);
	}
	return (ISC_FALSE);
}

/* Internal helper function for delete_if(). */
static dns_result_t 
delete_if_action(void *data, rr_t *rr) {
	conditional_delete_ctx_t *ctx = data;
	if ((*ctx->predicate)(ctx->update_rr, &rr->rdata)) {
		dns_result_t result;
		result = update_one_rr(ctx->db, ctx->ver, ctx->diff,
				       DNS_DIFFOP_DEL, ctx->name,
				       rr->ttl, &rr->rdata);
		return (result);
	} else {
		return (DNS_R_SUCCESS);
	}
}

/*
 * Conditionally delete RRs.  Apply 'predicate' to the RRs
 * specified by 'db', 'ver', 'name', and 'type' (which can
 * be dns_rdatatype_any to match any type).  Delete those
 * RRs for which the predicate returns true, and log the 
 * deletions in 'diff'.
 */
static dns_result_t
delete_if(rr_predicate *predicate,
	  dns_db_t *db,
	  dns_dbversion_t *ver,
	  dns_name_t *name,
	  dns_rdatatype_t type,
Bob Halley's avatar
Bob Halley committed
931
	  dns_rdatatype_t covers,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
932
933
934
935
936
937
938
939
940
941
	  dns_rdata_t *update_rr,
	  dns_diff_t *diff)
{
	conditional_delete_ctx_t ctx;
	ctx.predicate = predicate;
	ctx.db = db;
	ctx.ver = ver;
	ctx.diff = diff;
	ctx.name = name;
	ctx.update_rr = update_rr;
Bob Halley's avatar
Bob Halley committed
942
	return (foreach_rr(db, ver, name, type, covers,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
			   delete_if_action, &ctx));
}

/**************************************************************************/
/*
 * Miscellaneous subroutines.
 */

/*
 * Extract a single update RR from 'section' of dynamic update message
 * 'msg', with consistency checking.
 *
 * Stores the owner name, rdata, and TTL of the update RR at 'name',
 * 'rdata', and 'ttl', respectively.
 */
static void
get_current_rr(dns_message_t *msg, dns_section_t section,
	       dns_rdataclass_t zoneclass,
Bob Halley's avatar
Bob Halley committed
961
962
	       dns_name_t **name, dns_rdata_t *rdata, dns_rdatatype_t *covers,
	       dns_ttl_t *ttl,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
963
964
965
966
967
968
969
970
	       dns_rdataclass_t *update_class)
{
	dns_rdataset_t *rdataset;
	dns_result_t result;
	dns_message_currentname(msg, section, name);
	rdataset = ISC_LIST_HEAD((*name)->list);
	INSIST(rdataset != NULL);
	INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
Bob Halley's avatar
Bob Halley committed
971
	*covers = rdataset->covers;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
	*ttl = rdataset->ttl;
	result = dns_rdataset_first(rdataset);
	INSIST(result == DNS_R_SUCCESS);
	dns_rdataset_current(rdataset, rdata);
	INSIST(dns_rdataset_next(rdataset) == DNS_R_NOMORE);
	*update_class = rdata->rdclass;
	rdata->rdclass = zoneclass;
}

/*
 * Increment the SOA serial number of database 'db', version 'ver'.
 * Replace the SOA record in the database, and log the 
 * change in 'diff'.
 */

	/*
	 * XXXRTH  Failures in this routine will be worth logging, when
	 *         we have a logging system.  Failure to find the zonename
	 *	   or the SOA rdataset warrant at least an UNEXPECTED_ERROR().
	 */

static dns_result_t
increment_soa_serial(dns_db_t *db, dns_dbversion_t *ver,
		     dns_diff_t *diff, isc_mem_t *mctx)
{
	dns_difftuple_t *deltuple = NULL;
	dns_difftuple_t *addtuple = NULL;
	isc_uint32_t serial;
	dns_result_t result;
	
	CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_DEL, &deltuple));
	CHECK(dns_difftuple_copy(deltuple, &addtuple));
	addtuple->op = DNS_DIFFOP_ADD;

	serial = dns_soa_getserial(&addtuple->rdata);
	
	/* RFC1982 */
	serial = (serial + 1) & 0xFFFFFFFF;
	if (serial == 0) 
		serial = 1;

	dns_soa_setserial(serial, &addtuple->rdata);
	CHECK(do_one_tuple(&addtuple, db, ver, diff));
	CHECK(do_one_tuple(&deltuple, db, ver, diff));
	result = DNS_R_SUCCESS;
	
 failure:
	if (addtuple != NULL)
		dns_difftuple_free(&addtuple);
	if (deltuple != NULL)
		dns_difftuple_free(&deltuple);
	return (result);
}

/*
 * Check that the new SOA record at 'update_rdata' does not
1028
1029
 * illegally cause the SOA serial number to decrease or stay
 * unchanged relative to the existing SOA in 'db'.
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1030
1031
 *
 * Sets '*ok' to ISC_TRUE if the update is legal, ISC_FALSE if not.
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
 *
 * William King points out that RFC2136 is inconsistent about
 * the case where the serial number stays unchanged:
 * 
 *   section 3.4.2.2 requires a server to ignore a SOA update request
 *   if the serial number on the update SOA is less_than_or_equal to
 *   the zone SOA serial.
 * 
 *   section 3.6 requires a server to ignore a SOA update request if
 *   the serial is less_than the zone SOA serial.
 *
 * Paul says 3.4.2.2 is correct.
 *
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1045
1046
1047
1048
 */
static dns_result_t
check_soa_increment(dns_db_t *db, dns_dbversion_t *ver, 
		    dns_rdata_t *update_rdata,
1049
		    isc_boolean_t *ok)
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
{
	isc_uint32_t db_serial;
	isc_uint32_t update_serial;
	dns_result_t result;

	update_serial = dns_soa_getserial(update_rdata);
	
	result = dns_db_getsoaserial(db, ver, &db_serial);
	if (result != DNS_R_SUCCESS)
		return (result);

1061
	if (DNS_SERIAL_GE(db_serial, update_serial)) {
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
		*ok = ISC_FALSE;
	} else {
		*ok = ISC_TRUE;
	}

	return (DNS_R_SUCCESS);

}

1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
/**************************************************************************/
/*
 * Incremental updating of NXTs and SIGs.
 */

#define MAXZONEKEYS 32	/* Maximum number of zone keys supported. */

/*
 * We abuse the dns_diff_t type to represent a set of domain names
 * affected by the update.
 */
static dns_result_t
namelist_append_name(dns_diff_t *list, dns_name_t *name) {
	dns_result_t result;
	dns_difftuple_t *tuple = NULL;
	static dns_rdata_t dummy_rdata = { NULL, 0, 0, 0, { NULL, NULL } };
	CHECK(dns_difftuple_create(list->mctx, DNS_DIFFOP_EXISTS, name, 0,
				   &dummy_rdata, &tuple));
	dns_diff_append(list, &tuple);
 failure:
	return (result);
}

static dns_result_t
namelist_append_subdomain(dns_db_t *db, dns_name_t *name, dns_diff_t *affected)
{
	dns_result_t result;
	dns_fixedname_t fixedname;
	dns_name_t *child;
	dns_dbiterator_t *dbit = NULL;
	
	dns_fixedname_init(&fixedname);
	child = dns_fixedname_name(&fixedname);

	CHECK(dns_db_createiterator(db, ISC_FALSE, &dbit));
	
	for (result = dns_dbiterator_seek(dbit, name);
	     result == DNS_R_SUCCESS;
	     result = dns_dbiterator_next(dbit))
	{
		dns_dbnode_t *node = NULL;
		result = dns_dbiterator_current(dbit, &node, child);
		dns_db_detachnode(db, &node);
		CHECK(result);
		if (! dns_name_issubdomain(child, name))
			break;
		CHECK(namelist_append_name(affected, child));
	}
 failure:
	if (dbit != NULL)
		dns_dbiterator_destroy(&dbit);
	return (result);
}



/* Helper function for non_nxt_rrset_exists(). */
static dns_result_t
is_non_nxt_action(void *data, dns_rdataset_t *rrset) 
/*ARGSUSED*/
{
	data = data; /* Unused */
	if (!(rrset->type == dns_rdatatype_nxt ||
	      (rrset->type == dns_rdatatype_sig &&
	       rrset->covers == dns_rdatatype_nxt)))
		return (DNS_R_EXISTS);
	return (DNS_R_SUCCESS);
}

/*
 * Check whether there is an rrset other than a NXT or SIG NXT,
 * i.e., anything that justifies the continued existence of a name
 * after a secure update.
 *
 * If such an rrset exists, set '*exists' to ISC_TRUE.
 * Otherwise, set it to ISC_FALSE.
 */
static dns_result_t
non_nxt_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
		     dns_name_t *name, isc_boolean_t *exists) {
	dns_result_t result;	
	result = foreach_rrset(db, ver, name, 
			       is_non_nxt_action, NULL);
	RETURN_EXISTENCE_FLAG;
}

/*
 * A comparison function for sorting dns_diff_t:s by name.
 */
static int 
name_order(const void *av, const void *bv)
{
	dns_difftuple_t * const *ap = av;
	dns_difftuple_t * const *bp = bv;
	dns_difftuple_t *a = *ap;
	dns_difftuple_t *b = *bp;	
	return (dns_name_compare(&a->name, &b->name));
}

static dns_result_t
uniqify_name_list(dns_diff_t *list) {
	dns_result_t result;
	dns_difftuple_t *p, *q;
	
	CHECK(dns_diff_sort(list, name_order));

	p = ISC_LIST_HEAD(list->tuples);
	while (p != NULL) {
1179
		do {
1180
1181
1182
1183
1184
			q = ISC_LIST_NEXT(p, link);
			if (q == NULL || ! dns_name_equal(&p->name, &q->name))
				break;
			ISC_LIST_UNLINK(list->tuples, q, link);
			dns_difftuple_free(&q);
1185
		} while (1);
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
		p = ISC_LIST_NEXT(p, link);
	}
 failure:
	return (result);
}


static dns_result_t
is_glue(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
	isc_boolean_t *flag)
{
	dns_result_t result;
	dns_fixedname_t foundname;
	dns_fixedname_init(&foundname);
	result = dns_db_find(db, name, ver, dns_rdatatype_any,
			     DNS_DBFIND_GLUEOK | DNS_DBFIND_NOWILD,
			     (isc_stdtime_t) 0, NULL,
			     dns_fixedname_name(&foundname),
			     NULL, NULL);
	if (result == DNS_R_SUCCESS) {
		*flag = ISC_FALSE;
		return (DNS_R_SUCCESS);
	} else if (result == DNS_R_ZONECUT) {
		/* XXX should omit non-delegation types from NXT */
		*flag = ISC_FALSE;
		return (DNS_R_SUCCESS);
	} else if (result == DNS_R_GLUE) {
		*flag = ISC_TRUE;
		return (DNS_R_SUCCESS);
	} else {
		return (result);
	}
}

/*
 * Find the next/previous name that has a NXT record.  
 * In other words, skip empty database nodes and names that
 * have  had their NXTs removed because they are obscured by
 * a zone cut.
 */
static dns_result_t
next_active(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *oldname,
     dns_name_t *newname, isc_boolean_t forward)
{
	dns_result_t result;
	dns_dbiterator_t *dbit = NULL;
	isc_boolean_t has_nxt;

	CHECK(dns_db_createiterator(db, ISC_FALSE, &dbit));
	
	CHECK(dns_dbiterator_seek(dbit, oldname));
	do {
		dns_dbnode_t *node = NULL;
		
		if (forward)
			result = dns_dbiterator_next(dbit);			
		else
			result = dns_dbiterator_prev(dbit);
		if (result == DNS_R_NOMORE) {
			/* Wrap around. */
			if (forward)
				CHECK(dns_dbiterator_first(dbit));
			else 
				CHECK(dns_dbiterator_last(dbit));
		}
		dns_dbiterator_current(dbit, &node, newname);
		dns_db_detachnode(db, &node);

		/*
		 * The iterator may hold the tree lock, and
1256
		 * rrset_exists() calls dns_db_findnode() which
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
		 * may try to reacquire it.  To avoid deadlock
		 * we must pause the iterator first.
		 */
		CHECK(dns_dbiterator_pause(dbit));
		CHECK(rrset_exists(db, ver, newname,
				   dns_rdatatype_nxt, 0, &has_nxt));

	} while (! has_nxt);
 failure:
	if (dbit != NULL)
		dns_dbiterator_destroy(&dbit);

	return (result);
}

/*
 * Add a NXT record for "name", recording the change in "diff".
 */
static dns_result_t
add_nxt(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, dns_diff_t *diff) {
	dns_result_t result;
	dns_dbnode_t *node = NULL;
	unsigned char buffer[DNS_NXT_BUFFERSIZE];
	dns_rdata_t rdata;
	dns_difftuple_t *tuple = NULL;
	dns_fixedname_t fixedname;
	dns_name_t *target;

	dns_fixedname_init(&fixedname);
	target = dns_fixedname_name(&fixedname);	
	
	      
	/* Find the successor name, aka NXT target. */
	CHECK(next_active(db, ver, name, target, ISC_TRUE));

	/* Create the NXT RDATA. */
	CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
	dns_rdata_init(&rdata);
	CHECK(dns_buildnxtrdata(db, ver, node, target, buffer, &rdata));
	dns_db_detachnode(db, &node);	

	/* Create a diff tuple, update the database, and record the change. */
	CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name,
				   3600,	/* XXXRTH */	   
				   &rdata, &tuple));
	CHECK(do_one_tuple(&tuple, db, ver, diff));
	INSIST(tuple == NULL);
	
 failure:
	if (node != NULL)
		dns_db_detachnode(db, &node);
	return (result);
}

/*
 * Add a placeholder NXT record for "name", recording the change in "diff".
 */
static dns_result_t
add_placeholder_nxt(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, dns_diff_t *diff) {
	dns_result_t result;
	dns_difftuple_t *tuple = NULL;
	isc_region_t r;
	unsigned char data[1] = { 0 }; /* The root domain, no bits. */
	dns_rdata_t rdata;

	r.base = data;
	r.length = sizeof data;
	dns_rdata_fromregion(&rdata, dns_db_class(db), dns_rdatatype_nxt, &r);
	CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, 0,
				   &rdata, &tuple));
	CHECK(do_one_tuple(&tuple, db, ver, diff));
 failure:
	return (result);
}

static dns_result_t
find_zone_keys(dns_db_t *db, dns_dbversion_t *ver, isc_mem_t *mctx,
	       unsigned int maxkeys, dst_key_t **keys, unsigned int *nkeys)
{
	dns_result_t result;
	dns_dbnode_t *node = NULL;
	CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
	CHECK(dns_dnssec_findzonekeys(db, ver, node, dns_db_origin(db),
				      mctx, maxkeys, keys, nkeys));
 failure:
	if (node != NULL)
		dns_db_detachnode(db, &node);
	return (result);
}

/*
 * Add SIG records for an RRset, recording the change in "diff".
 */
static dns_result_t
add_sigs(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
	 dns_rdatatype_t type, dns_diff_t *diff, dst_key_t **keys,
	 unsigned int nkeys, isc_mem_t *mctx, isc_stdtime_t now,
	 isc_stdtime_t expire)
{
	dns_result_t result;
	dns_dbnode_t *node = NULL;
	dns_rdataset_t rdataset;
	dns_rdata_t sig_rdata;
	isc_buffer_t buffer;
	unsigned char data[1024]; /* XXX */
	unsigned int i;

	dns_rdataset_init(&rdataset);
	isc_buffer_init(&buffer, data, sizeof data, ISC_BUFFERTYPE_BINARY);

	/* Get the rdataset to sign. */
	CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
	CHECK(dns_db_findrdataset(db, node, ver, type, 0,
				  (isc_stdtime_t) 0,
				  &rdataset, NULL));
	dns_db_detachnode(db, &node);	

	for (i = 0; i < nkeys; i++) {
		/* Calculate the signature, creating a SIG RDATA. */
		CHECK(dns_dnssec_sign(name, &rdataset, keys[i],
				      &now, &expire,
				      mctx, &buffer, &sig_rdata));

		/* Update the database and journal with the SIG. */
		/* XXX inefficient - will cause dataset merging */
		CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADD, name,
				    rdataset.ttl, &sig_rdata));
	}
		
 failure:
	if (dns_rdataset_isassociated(&rdataset))
		dns_rdataset_disassociate(&rdataset);
	if (node != NULL)
		dns_db_detachnode(db, &node);
	return (result);
} 

/*
 * Update SIG and NXT records affected by an update.  The original
 * update, including the SOA serial update but exluding the SIG & NXT 
 * changes, is in "diff" and has already been applied to "newver" of "db".
 * The database version prior to the update is "oldver".
 *
 * The necessary SIG and NXT changes will be applied to "newver"
 * and added (as a minimal diff) to "diff".
 */
static dns_result_t
update_signatures(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *oldver,
		  dns_dbversion_t *newver, dns_diff_t *diff)
{
	dns_result_t result;
	dns_difftuple_t *t;
	dns_diff_t diffnames;
	dns_diff_t affected;
	dns_diff_t sig_diff;
	dns_diff_t nxt_diff;
	dns_diff_t nxt_mindiff;
	isc_boolean_t flag;
	dst_key_t *zone_keys[MAXZONEKEYS];
	unsigned int nkeys = 0;
	unsigned int i;
	isc_stdtime_t now, expire;
	
	dns_diff_init(mctx, &diffnames);
	dns_diff_init(mctx, &affected);
	
	dns_diff_init(mctx, &sig_diff);
	dns_diff_init(mctx, &nxt_diff);
	dns_diff_init(mctx, &nxt_mindiff);

	result = find_zone_keys(db, newver, mctx, 
				MAXZONEKEYS, zone_keys, &nkeys);
1429
1430
1431
1432
1433
1434
1435
1436
	if (result != DNS_R_SUCCESS) {
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_UPDATE,
			      NS_LOGMODULE_UPDATE, ISC_LOG_ERROR,
			      "could not get zone keys for secure "
			      "dynamic update");
		goto failure;
	}

1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
	CHECK(isc_stdtime_get(&now));
	expire = 100000 + now; /* XXX */
	
	/*
	 * Find all RRsets directly affected by the update, and 
	 * update their SIGs.  Also build a list of names affected
	 * by the update in "diffnames".
	 */
	CHECK(dns_diff_sort(diff, temp_order));

	t = ISC_LIST_HEAD(diff->tuples);
	while (t != NULL) {
		dns_name_t *name = &t->name;
		/* Now "name" is a new, unique name affected by the update. */

		CHECK(namelist_append_name(&diffnames, name));
		
		while (t != NULL && dns_name_equal(&t->name, name)) {
			dns_rdatatype_t type;
			type = t->rdata.type;

			/*
			 * Now "name" and "type" denote a new unique RRset
			 * affected by the update.  
			 */

			/* Don't sign SIGs. */
			if (type == dns_rdatatype_sig)
				goto skip;

			/*
			 * Delete all old SIGs covering this type, since they
			 * are all invalid when the signed RRset has changed.
			 * We may not be able to recreate all of them - tough. 
			 */
			CHECK(delete_if(true_p, db, newver, name,
					dns_rdatatype_sig, type,
					NULL, &sig_diff));

			/*
			 * If this RRset still exists after the update,
			 * add a new signature for it.
			 */
			CHECK(rrset_exists(db, newver, name, type, 0, &flag));
			if (flag) {
				CHECK(add_sigs(db, newver, name, type,
					       &sig_diff, zone_keys, nkeys,
					       mctx, now, expire));
			}
		skip:
			/* Skip any other updates to the same RRset. */
			while (t != NULL &&
			       dns_name_equal(&t->name, name) &&
			       t->rdata.type == type)
			{
				t = ISC_LIST_NEXT(t, link);
			}
		}
	}

	/* Remove orphaned NXTs and SIG NXTs. */
	for (t = ISC_LIST_HEAD(diffnames.tuples);
	     t != NULL;
	     t = ISC_LIST_NEXT(t, link))
	{
		CHECK(non_nxt_rrset_exists(db, newver, &t->name, &flag));
		if (! flag) {
			CHECK(delete_if(true_p, db, newver, &t->name,
					dns_rdatatype_any, 0,
					NULL, &sig_diff));
		}
	}

	/*
	 * When a name is created or deleted, its predecessor needs to 
	 * have its NXT updated.
	 */
	for (t = ISC_LIST_HEAD(diffnames.tuples);
	     t != NULL;
	     t = ISC_LIST_NEXT(t, link))
	{
		isc_boolean_t existed, exists;
		dns_fixedname_t fixedname;
		dns_name_t *prevname;

		dns_fixedname_init(&fixedname);
		prevname = dns_fixedname_name(&fixedname);

		CHECK(name_exists(db, oldver, &t->name, &existed));
		CHECK(name_exists(db, newver, &t->name, &exists));
		if (exists == existed)
			continue;

		/*
		 * Find the predecessor.
		 * When names become obscured or unobscured in this update
		 * transaction, we may find the wrong predecessor because
		 * the NXTs have not yet been updated to reflect the delegation
		 * change.  This should not matter because in this case,
		 * the correct predecessor is either the delegation node or
		 * a newly unobscured node, and those nodes are on the 
		 * "affected" list in any case.
		 */
		CHECK(next_active(db, newver, &t->name, prevname, ISC_FALSE));
		CHECK(namelist_append_name(&affected, prevname));
	}

	/* Find names affected by delegation changes. */
	for (t = ISC_LIST_HEAD(diffnames.tuples);
	     t != NULL;
	     t = ISC_LIST_NEXT(t, link))
	{
		isc_boolean_t existed, exists;

		CHECK(rrset_exists(db, oldver, &t->name, dns_rdatatype_ns, 0,
				   &existed));
		CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ns, 0,
				   &exists));
		if (exists == existed)
			continue;
		/*
		 * There was a delegation change.  Mark all subdomains 
		 * of t->name as potentially needing a NXT update.
		 */
		CHECK(namelist_append_subdomain(db, &t->name, &affected));
	}

	ISC_LIST_APPENDLIST(affected.tuples, diffnames.tuples, link);
	INSIST(ISC_LIST_EMPTY(diffnames.tuples));
	
	CHECK(uniqify_name_list(&affected));
	
	/*
	 * Determine which names should have NXTs, and delete/create
	 * NXTs to make it so.  We don't know the final NXT targets yet,
	 * so we just create placeholder NXTs with arbitrary contents
	 * to indicate that their respective owner names should be part of
	 * the NXT chain.
	 */
	for (t = ISC_LIST_HEAD(affected.tuples);
	     t != NULL;
	     t = ISC_LIST_NEXT(t, link))
	{
		isc_boolean_t exists;
		CHECK(name_exists(db, newver, &t->name, &exists));
		if (! exists)
			continue;
		CHECK(is_glue(db, newver, &t->name, &flag));
		if (flag) {
			/*
			 * This name is obscured.  Delete any
			 * existing NXT record.
			 */
			CHECK(delete_if(true_p, db, newver, &t->name,
					dns_rdatatype_nxt, 0,
					NULL, &nxt_diff));
		} else {
			/* This name is not obscured.  It should have a NXT. */
			CHECK(rrset_exists(db, newver, &t->name,
					   dns_rdatatype_nxt, 0, &flag));
			if (! flag) {
				add_placeholder_nxt(db, newver, &t->name, diff);
			}
		}
	}
		
	/*
	 * Now we know which names are part of the NXT chain.
	 * Make them all point at their correct targets.
	 */
	for (t = ISC_LIST_HEAD(affected.tuples);
	     t != NULL;
	     t = ISC_LIST_NEXT(t, link))
	{
		CHECK(rrset_exists(db, newver, &t->name,
				   dns_rdatatype_nxt, 0, &flag));
		if (flag) {
			/*
			 * There is a NXT, but we don't know if it is correct.
			 * Delete it and create a correct one to be sure.
			 * If the update was unnecessary, the diff minimization
			 * will take care of eliminating it from the journal,
			 * IXFRs, etc.
			 *
			 * The SIG bit should always be set in the NXTs
			 * we generate, because they will all get SIG NXTs.
			 * (XXX what if the zone keys are missing?).
			 * Because the SIG NXTs have not necessarily been
			 * created yet, the correctness of the bit mask relies
			 * on the assumption that NXTs are only created if 
			 * there is other data, and if there is other data,
			 * there are other SIGs.
			 */
			CHECK(delete_if(true_p, db, newver, &t->name,
					dns_rdatatype_nxt, 0,
					NULL, &nxt_diff));
			CHECK(add_nxt(db, newver, &t->name, &nxt_diff));
		}
	}

	/*
	 * Minimize the set of NXT updates so that we don't
	 * have to regenerate the SIG NXTs for NXTs that were 
	 * replaced with identical ones.
	 */
	while ((t = ISC_LIST_HEAD(nxt_diff.tuples)) != NULL) {
		ISC_LIST_UNLINK(nxt_diff.tuples, t, link);
		dns_diff_appendminimal(&nxt_mindiff, &t);
	}

	/* Update SIG NXTs. */
	for (t = ISC_LIST_HEAD(nxt_mindiff.tuples);
	     t != NULL;
	     t = ISC_LIST_NEXT(t, link))
	{
		if (t->op == DNS_DIFFOP_DEL) {
			CHECK(delete_if(true_p, db, newver, &t->name,
					dns_rdatatype_sig, dns_rdatatype_nxt,
					NULL, &sig_diff));
		} else if (t->op == DNS_DIFFOP_ADD) {
			CHECK(add_sigs(db, newver, &t->name, dns_rdatatype_nxt,
				       &sig_diff, zone_keys, nkeys, mctx, now,
				       expire));
		} else {
			INSIST(0);
		}
	}

	/* Record our changes for the journal. */
	while ((t = ISC_LIST_HEAD(sig_diff.tuples)) != NULL) {
		ISC_LIST_UNLINK(sig_diff.tuples, t, link);
		dns_diff_appendminimal(diff, &t);
	}
	while ((t = ISC_LIST_HEAD(nxt_mindiff.tuples)) != NULL) {
		ISC_LIST_UNLINK(nxt_mindiff.tuples, t, link);
		dns_diff_appendminimal(diff, &t);
	}

	INSIST(ISC_LIST_EMPTY(sig_diff.tuples));
	INSIST(ISC_LIST_EMPTY(nxt_diff.tuples));
	INSIST(ISC_LIST_EMPTY(nxt_mindiff.tuples));

 failure:
	dns_diff_clear(&sig_diff);
	dns_diff_clear(&nxt_diff);
	dns_diff_clear(&nxt_mindiff);

	dns_diff_clear(&affected);
	dns_diff_clear(&diffnames);

	for (i = 0; i < nkeys; i++) 
		dst_key_free(zone_keys[i]);
	
	return (result);
}	


Andreas Gustafsson's avatar
Andreas Gustafsson committed
1694
1695
1696
1697
1698
1699
1700
/**************************************************************************/
/*
 * The actual update code in all its glory.  We try to follow
 * the RFC2136 pseudocode as closely as possible.
 */

static dns_result_t
1701
send_update_event(ns_client_t *client, dns_zone_t *zone) {
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1702
	dns_result_t result = DNS_R_SUCCESS;
1703
	update_event_t *event = NULL;
1704
1705
	isc_task_t *zonetask = NULL;
	
1706
1707
1708
1709
1710
1711
1712
1713
	event = (update_event_t *)
		isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
				   update_action, client, sizeof(*event));
	if (event == NULL)
		FAIL(DNS_R_NOMEMORY);
	event->zone = zone;
	event->result = DNS_R_SUCCESS;

1714
	dns_zone_gettask(zone, &zonetask);
1715
	isc_task_send(zonetask, (isc_event_t **) &event);
1716

1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
 failure:
	if (event != NULL)
		isc_event_free((isc_event_t **) &event);
	return (result);
}

static void
respond(ns_client_t *client, dns_result_t result) {
	int msg_result;
	dns_message_t *response = NULL;
	msg_result = dns_message_create(client->mctx, DNS_MESSAGE_INTENTRENDER,
					&response);
	if (msg_result != DNS_R_SUCCESS)
		goto msg_failure;
	
	response->id = client->message->id;
	response->rcode = (result == DNS_R_SUCCESS ?
		dns_rcode_noerror : dns_result_torcode(result));
	response->flags = client->message->flags;
	response->flags |= DNS_MESSAGEFLAG_QR;

	dns_message_destroy(&client->message);
	client->message = response;
	ns_client_send(client);
	return;
	
 msg_failure:
1744
1745
1746
1747
	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
		      ISC_LOG_ERROR,
		      "could not create update response message: %s",
		      isc_result_totext(msg_result));
1748
1749
1750
1751
1752
	ns_client_next(client, msg_result);
}

void
ns_update_start(ns_client_t *client)
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1753
{
1754
1755
	dns_message_t *request = client->message;
	dns_result_t result;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1756
1757
	dns_name_t *zonename;
	dns_rdataset_t *zone_rdataset;
1758
	dns_zone_t *zone = NULL;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1759
1760
1761
1762
1763
1764
1765
	dns_rdataclass_t zoneclass;
	
	/*
	 * Interpret the zone section.
	 */
	result = dns_message_firstname(request, DNS_SECTION_ZONE);
	if (result != DNS_R_SUCCESS)
1766
1767
		FAILC(DNS_R_FORMERR,
		      "update zone section empty");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777

	/*
	 * The zone section must contain exactly one "question", and
	 * it must be of type SOA.
	 */
	zonename = NULL;
	dns_message_currentname(request, DNS_SECTION_ZONE, &zonename);
	zone_rdataset = ISC_LIST_HEAD(zonename->list);
	zoneclass = zone_rdataset->rdclass;
	if (zone_rdataset->type != dns_rdatatype_soa)
1778
1779
		FAILC(DNS_R_FORMERR,
		      "update zone section contains non-SOA");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1780
	if (ISC_LIST_NEXT(zone_rdataset, link) != NULL)
1781
1782
		FAILC(DNS_R_FORMERR,
		      "update zone section contains multiple RRs");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1783
1784
1785
1786

	/* The zone section must have exactly one name. */
	result = dns_message_nextname(request, DNS_SECTION_ZONE);
	if (result != DNS_R_NOMORE)
1787
1788
		FAILC(DNS_R_FORMERR,
		      "update zone section contains multiple RRs");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1789

1790
	result = dns_zt_find(client->view->zonetable, zonename, NULL, &zone);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1791
	if (result != DNS_R_SUCCESS)
1792
1793
		FAILC(DNS_R_NOTAUTH,
		      "not authoritative for update zone");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1794

1795
1796
1797
1798
1799
	switch(dns_zone_gettype(zone)) {
	case dns_zone_master:
		CHECK(send_update_event(client, zone)); 
		break;	/* OK. */
	case dns_zone_slave:
1800
1801
		FAILS(DNS_R_NOTIMP,
		      "update forwarding"); /* XXX implement */
1802
	default:
1803
1804
		FAILC(DNS_R_NOTAUTH,
		      "not authoritative for update zone");
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
	}
	return;
	
 failure:
	/*
	 * We failed without having sent an update event to the zone.
	 * We are still in the client task context, so we can 
	 * simply give an error response without switching tasks.
	 */
	respond(client, result);
}
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1816

1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
static void
update_action(isc_task_t *task, isc_event_t *event)
{
	update_event_t *uev = (update_event_t *) event;
	dns_zone_t *zone = uev->zone;
	ns_client_t *client = (ns_client_t *) event->arg;

	dns_result_t result;
	dns_db_t *db = NULL;
	dns_dbversion_t *oldver = NULL;
	dns_dbversion_t *ver = NULL;
	dns_diff_t diff; 	/* Pending updates. */
	dns_diff_t temp; 	/* Pending RR existence assertions. */
	isc_boolean_t soa_serial_changed = ISC_FALSE;
	isc_mem_t *mctx = client->mctx;
	dns_rdatatype_t covers;
	dns_message_t *request = client->message;
	dns_rdataclass_t zoneclass;
	dns_name_t *zonename;
		
	INSIST(event->type == DNS_EVENT_UPDATE);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1838

1839
1840
1841
1842
1843
1844
	dns_diff_init(mctx, &diff);
	dns_diff_init(mctx, &temp);

	CHECK(dns_zone_getdb(zone, &db));
	zonename = dns_db_origin(db);
	zoneclass = dns_db_class(db);
1845
	dns_db_currentversion(db, &oldver);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
	CHECK(dns_db_newversion(db, &ver));
	
	/* Check prerequisites. */

	for (result = dns_message_firstname(request, DNS_SECTION_PREREQUISITE);
	     result == DNS_R_SUCCESS;
	     result = dns_message_nextname(request, DNS_SECTION_PREREQUISITE))
	{
		dns_name_t *name = NULL;
		dns_rdata_t rdata;
		dns_ttl_t ttl;
		dns_rdataclass_t update_class;
		isc_boolean_t flag;

		get_current_rr(request, DNS_SECTION_PREREQUISITE, zoneclass,
Bob Halley's avatar
Bob Halley committed
1861
			       &name, &rdata, &covers, &ttl, &update_class);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1862
1863

		if (ttl != 0)
1864
			FAILC(DNS_R_FORMERR, "prerequisite TTL is not zero");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1865
1866

		if (! dns_name_issubdomain(name, zonename))
1867
1868
			FAILC(DNS_R_NOTZONE,
				"prerequisite name is out of zone");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1869
1870
1871

		if (update_class == dns_rdataclass_any) {
			if (rdata.length != 0)
1872
1873
1874
				FAILC(DNS_R_FORMERR,
				      "class ANY prerequisite "
				      "RDATA is not empty");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1875
1876
1877
			if (rdata.type == dns_rdatatype_any) {
				CHECK(name_exists(db, ver, name, &flag));
				if (! flag) {
1878
1879
1880
					FAILC(DNS_R_NXDOMAIN,
					      "'name in use' prerequisite "
					      "not satisfied");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1881
1882
1883
				}
			} else {
				CHECK(rrset_exists(db, ver, name,
Bob Halley's avatar
Bob Halley committed
1884
						   rdata.type, covers, &flag));
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1885
1886
				if (! flag) {
					/* RRset does not exist. */
1887
					FAILC(DNS_R_NXRRSET,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1888
					"'rrset exists (value independent)' "
1889
					"prerequisite not satisfied");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1890
1891
1892
1893
				}
			}
		} else if (update_class == dns_rdataclass_none) {
			if (rdata.length != 0)
1894
1895
1896
				FAILC(DNS_R_FORMERR,
				      "class NONE prerequisite"
				      "RDATA is not empty");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1897
1898
1899
			if (rdata.type == dns_rdatatype_any) {
				CHECK(name_exists(db, ver, name, &flag));
				if (flag) {
1900
1901
1902
					FAILC(DNS_R_YXDOMAIN,
					      "'name not in use' prerequisite "
					      "not satisfied");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1903
1904
1905
				}
			} else {
				CHECK(rrset_exists(db, ver, name,
Bob Halley's avatar
Bob Halley committed
1906
						   rdata.type, covers, &flag));
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1907
1908
				if (flag) {
					/* RRset exists. */
1909
1910
1911
					FAILC(DNS_R_YXRRSET,
					      "'rrset does not exist' "
					      "prerequisite not satisfied");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1912
1913
1914
1915
1916
1917
1918
1919
1920
				}
			} 
		} else if (update_class == zoneclass) {
			/* "temp<rr.name, rr.type> += rr;" */
			result = temp_append(&temp, name, &rdata);
			if (result != DNS_R_SUCCESS) {
				UNEXPECTED_ERROR(__FILE__, __LINE__,
					 "temp entry creation failed: %s",
						 dns_result_totext(result));
1921
				FAIL(DNS_R_UNEXPECTED);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1922
1923
			}
		} else {
1924
			FAILC(DNS_R_FORMERR, "malformed prerequisite");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
		}
	}
	if (result != DNS_R_NOMORE)
		FAIL(result);

	/*
	 * Perform the final check of the "rrset exists (value dependent)"
	 * prerequisites.
	 */
	result = temp_check(mctx, &temp, db, ver);
	if (result != DNS_R_SUCCESS)
1936
1937
		FAILC(result, "'RRset exists (value dependent)' "
		      "prerequisite not satisfied");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1938

1939
	isc_log_write(UPDATE_DEBUG_LOGARGS, "prerequisites are OK");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1940

1941
1942
1943
1944
	/*
	 * Check Requestor's Permissions.  It seems a bit silly to do this
	 * only after prerequisite testing, but that is what RFC2136 says.
	 */
1945
1946
1947
1948
1949
	CHECK(dns_aml_checkrequest(request, ns_client_getsockaddr(client),
				   dns_zone_getupdateacl(zone),
				   ns_g_confctx->acls,
				   "update", ISC_FALSE));

Andreas Gustafsson's avatar
Andreas Gustafsson committed
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
	/* Perform the Update Section Prescan. */

	for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
	     result == DNS_R_SUCCESS;
	     result = dns_message_nextname(request, DNS_SECTION_UPDATE))
	{
		dns_name_t *name = NULL;
		dns_rdata_t rdata;
		dns_ttl_t ttl;
		dns_rdataclass_t update_class;		
		get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
Bob Halley's avatar
Bob Halley committed
1961
			       &name, &rdata, &covers, &ttl, &update_class);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1962
1963

		if (! dns_name_issubdomain(name, zonename))
1964
1965
			FAILC(DNS_R_NOTZONE,
			      "update RR is outside zone");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1966
1967
1968
1969
1970
1971
1972
		if (update_class == zoneclass) {
			/*
			 * Check for meta-RRs.  The RFC2136 pseudocode says
			 * check for ANY|AXFR|MAILA|MAILB, but the text adds
			 * "or any other QUERY metatype"
			 */
			if (dns_rdatatype_ismeta(rdata.type)) {
1973
1974
				FAILC(DNS_R_FORMERR,
				      "meta-RR in update");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1975
1976
1977
1978
1979
			}
		} else if (update_class == dns_rdataclass_any) {
			if (ttl != 0 || rdata.length != 0 ||
			    (dns_rdatatype_ismeta(rdata.type) &&
			     rdata.type != dns_rdatatype_any))
1980
1981
				FAILC(DNS_R_FORMERR, 
				      "meta-RR in update");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1982
1983
1984
		} else if (update_class == dns_rdataclass_none) {
			if (ttl != 0 || 
			    dns_rdatatype_ismeta(rdata.type))
1985
1986
				FAILC(DNS_R_FORMERR, 
				      "meta-RR in update");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1987
		} else {
1988
1989
1990
1991
			isc_log_write(ns_g_lctx, NS_LOGCATEGORY_UPDATE,
				      NS_LOGMODULE_UPDATE, ISC_LOG_WARNING,
				      "update RR has incorrect class %d", 
				      update_class);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
1992
1993
			FAIL(DNS_R_FORMERR);
		}
1994
1995
1996
1997
1998
1999
		/*
		 * draft-ietf-dnsind-simple-secure-update-01 says
		 * "Unlike traditional dynamic update, the client
		 * is forbidden from updating NXT records."
		 */
		if (dns_db_issecure(db) && rdata.type == dns_rdatatype_nxt) {
2000
2001
2002
			FAILC(DNS_R_REFUSED,
			      "explicit NXT updates are not allowed "
			      "in secure zones");
2003
		}
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2004
2005
2006
2007
	}
	if (result != DNS_R_NOMORE)
		FAIL(result);

2008
	isc_log_write(UPDATE_DEBUG_LOGARGS, "update section prescan OK");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
	
	/* Process the Update Section. */

	for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
	     result == DNS_R_SUCCESS;
	     result = dns_message_nextname(request, DNS_SECTION_UPDATE))
	{
		dns_name_t *name = NULL;
		dns_rdata_t rdata;
		dns_ttl_t ttl;
		dns_rdataclass_t update_class;
		isc_boolean_t flag;
		
		get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
Bob Halley's avatar
Bob Halley committed
2023
			       &name, &rdata, &covers, &ttl, &update_class);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2024
2025
2026
2027
2028
2029
2030

		if (update_class == zoneclass) {
			if (rdata.type == dns_rdatatype_cname) {
				CHECK(cname_incompatible_rrset_exists(db, ver,
								      name,
								      &flag));
				if (flag) {
2031
2032
2033
2034
					isc_log_write(UPDATE_PROTOCOL_LOGARGS,
						      "attempt to add CNAME "
						      "alongside non-CNAME "
						      "ignored");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2035
2036
2037
2038
					continue;
				}
			} else {
				CHECK(rrset_exists(db, ver, name,
Bob Halley's avatar
Bob Halley committed
2039
						   dns_rdatatype_cname, 0,
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2040
						   &flag));
2041
2042
2043
				if (flag && 
				    ! dns_rdatatype_isdnssec(rdata.type))
				{
2044
2045
2046
					isc_log_write(UPDATE_PROTOCOL_LOGARGS,
					      "attempt to add non-CNAME "
					      "alongside CNAME ignored");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2047
2048
2049
2050
					continue;
				}
			}
			if (rdata.type == dns_rdatatype_soa) {
2051
				isc_boolean_t ok;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2052
				CHECK(rrset_exists(db, ver, name, 
Bob Halley's avatar
Bob Halley committed
2053
2054
						   dns_rdatatype_soa, 0,
						   &flag));
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2055
				if (! flag) {
2056
2057
2058
					isc_log_write(UPDATE_PROTOCOL_LOGARGS,
						      "attempt to create 2nd "
						      "SOA ignored");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2059
2060
2061
					continue;
				}
				CHECK(check_soa_increment(db, ver, &rdata,
2062
							  &ok));
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2063
				if (! ok) {
2064
2065
2066
2067
					isc_log_write(UPDATE_PROTOCOL_LOGARGS,
						      "SOA update failed to "
						      "increment serial, "
						      "ignoring it");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2068
2069
					continue;
				}
2070
				soa_serial_changed = ISC_TRUE;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2071
2072
2073
2074
2075
2076
2077
			}
			/*
			 * Add an RR.  If an identical RR already exists,
			 * do nothing.  If a similar but not identical 
			 * CNAME, SOA, or WKS exists, remove it first.
			 */
			CHECK(matching_rr_exists(rr_equal_p, db, ver, name,
Bob Halley's avatar
Bob Halley committed
2078
2079
						 rdata.type, covers, &rdata,
						 &flag));
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2080
			if (! flag) {
2081
2082
				isc_log_write(UPDATE_PROTOCOL_LOGARGS,
					      "adding an RR");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2083
				CHECK(delete_if(replaces_p, db, ver, name,
Bob Halley's avatar
Bob Halley committed
2084
2085
						rdata.type, covers, &rdata,
						&diff));
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2086
2087
2088
2089
2090
2091
				result = update_one_rr(db, ver, &diff,
						       DNS_DIFFOP_ADD,
						       name, ttl, &rdata);
				if (result != DNS_R_SUCCESS)
					FAIL(result);
			} else {
2092
2093
2094
				isc_log_write(UPDATE_PROTOCOL_LOGARGS,
					      "attempt to add existing RR "
					      "ignored");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2095
2096
2097
			}
		} else if (update_class == dns_rdataclass_any) {
			if (rdata.type == dns_rdatatype_any) {
2098
2099
				isc_log_write(UPDATE_PROTOCOL_LOGARGS,
					      "delete all rrsets from a name");
Andreas Gustafsson's avatar
Andreas Gustafsson committed
2100
2101
2102
				if (dns_name_equal(name, zonename)) {