summaryrefslogtreecommitdiffstats
path: root/source4/dns_server/dns_update.c
blob: 1285111c6249ceae483bca00da44e3ab08e3b268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
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
520
521
522
523
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
559
560
561
562
563
564
565
566
567
568
569
570
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
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
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
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
/*
   Unix SMB/CIFS implementation.

   DNS server handler for update requests

   Copyright (C) 2010 Kai Blin  <kai@samba.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libcli/util/ntstatus.h"
#include "librpc/ndr/libndr.h"
#include "librpc/gen_ndr/ndr_dns.h"
#include "librpc/gen_ndr/ndr_dnsp.h"
#include <ldb.h>
#include "param/param.h"
#include "param/loadparm.h"
#include "dsdb/samdb/samdb.h"
#include "dsdb/common/util.h"
#include "samba/service_task.h"
#include "dns_server/dns_server.h"
#include "auth/auth.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_DNS

static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
			     const struct dns_res_rec *rrec,
			     struct dnsp_DnssrvRpcRecord *r,
			     bool name_is_static);

static WERROR check_one_prerequisite(struct dns_server *dns,
				     TALLOC_CTX *mem_ctx,
				     const struct dns_name_question *zone,
				     const struct dns_res_rec *pr,
				     bool *final_result)
{
	bool match;
	WERROR werror;
	struct ldb_dn *dn;
	uint16_t i;
	bool found = false;
	struct dnsp_DnssrvRpcRecord *rec = NULL;
	struct dnsp_DnssrvRpcRecord *ans;
	uint16_t a_count;

	size_t host_part_len = 0;

	*final_result = true;

	if (pr->ttl != 0) {
		return DNS_ERR(FORMAT_ERROR);
	}

	match = dns_name_match(zone->name, pr->name, &host_part_len);
	if (!match) {
		return DNS_ERR(NOTZONE);
	}

	werror = dns_name2dn(dns, mem_ctx, pr->name, &dn);
	W_ERROR_NOT_OK_RETURN(werror);

	if (pr->rr_class == DNS_QCLASS_ANY) {

		if (pr->length != 0) {
			return DNS_ERR(FORMAT_ERROR);
		}


		if (pr->rr_type == DNS_QTYPE_ALL) {
			/*
			 */
			werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &a_count);
			if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
				return DNS_ERR(NAME_ERROR);
			}
			W_ERROR_NOT_OK_RETURN(werror);

			if (a_count == 0) {
				return DNS_ERR(NAME_ERROR);
			}
		} else {
			/*
			 */
			werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &a_count);
			if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
				return DNS_ERR(NXRRSET);
			}
			if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
				return DNS_ERR(NXRRSET);
			}
			W_ERROR_NOT_OK_RETURN(werror);

			for (i = 0; i < a_count; i++) {
				if (ans[i].wType == (enum dns_record_type) pr->rr_type) {
					found = true;
					break;
				}
			}
			if (!found) {
				return DNS_ERR(NXRRSET);
			}
		}

		/*
		 * RFC2136 3.2.5 doesn't actually mention the need to return
		 * OK here, but otherwise we'd always return a FORMAT_ERROR
		 * later on. This also matches Microsoft DNS behavior.
		 */
		return WERR_OK;
	}

	if (pr->rr_class == DNS_QCLASS_NONE) {
		if (pr->length != 0) {
			return DNS_ERR(FORMAT_ERROR);
		}

		if (pr->rr_type == DNS_QTYPE_ALL) {
			/*
			 */
			werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &a_count);
			if (W_ERROR_EQUAL(werror, WERR_OK)) {
				return DNS_ERR(YXDOMAIN);
			}
		} else {
			/*
			 */
			werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &a_count);
			if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
				werror = WERR_OK;
			}
			if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
				werror = WERR_OK;
			}

			for (i = 0; i < a_count; i++) {
				if (ans[i].wType == (enum dns_record_type) pr->rr_type) {
					found = true;
					break;
				}
			}
			if (found) {
				return DNS_ERR(YXRRSET);
			}
		}

		/*
		 * RFC2136 3.2.5 doesn't actually mention the need to return
		 * OK here, but otherwise we'd always return a FORMAT_ERROR
		 * later on. This also matches Microsoft DNS behavior.
		 */
		return WERR_OK;
	}

	if (pr->rr_class != zone->question_class) {
		return DNS_ERR(FORMAT_ERROR);
	}

	*final_result = false;

	werror = dns_lookup_records(dns, mem_ctx, dn, &ans, &a_count);
	if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
		return DNS_ERR(NXRRSET);
	}
	if (W_ERROR_EQUAL(werror, DNS_ERR(NAME_ERROR))) {
		return DNS_ERR(NXRRSET);
	}
	W_ERROR_NOT_OK_RETURN(werror);

	rec = talloc_zero(mem_ctx, struct dnsp_DnssrvRpcRecord);
	W_ERROR_HAVE_NO_MEMORY(rec);

	werror = dns_rr_to_dnsp(rec, pr, rec, dns_name_is_static(ans, a_count));
	W_ERROR_NOT_OK_RETURN(werror);

	for (i = 0; i < a_count; i++) {
		if (dns_record_match(rec, &ans[i])) {
			found = true;
			break;
		}
	}

	if (!found) {
		return DNS_ERR(NXRRSET);
	}

	return WERR_OK;
}

static WERROR check_prerequisites(struct dns_server *dns,
				  TALLOC_CTX *mem_ctx,
				  const struct dns_name_question *zone,
				  const struct dns_res_rec *prereqs, uint16_t count)
{
	uint16_t i;
	WERROR final_error = WERR_OK;

	for (i = 0; i < count; i++) {
		bool final;
		WERROR werror;

		werror = check_one_prerequisite(dns, mem_ctx, zone,
						&prereqs[i], &final);
		if (!W_ERROR_IS_OK(werror)) {
			if (final) {
				return werror;
			}
			if (W_ERROR_IS_OK(final_error)) {
				final_error = werror;
			}
		}
	}

	if (!W_ERROR_IS_OK(final_error)) {
		return final_error;
	}

	return WERR_OK;
}

static WERROR update_prescan(const struct dns_name_question *zone,
			     const struct dns_res_rec *updates, uint16_t count)
{
	const struct dns_res_rec *r;
	uint16_t i;
	size_t host_part_len;
	bool match;

	for (i = 0; i < count; i++) {
		r = &updates[i];
		match = dns_name_match(zone->name, r->name, &host_part_len);
		if (!match) {
			return DNS_ERR(NOTZONE);
		}
		if (zone->question_class == r->rr_class) {
			if (r->rr_type == DNS_QTYPE_ALL) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_AXFR) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_MAILB) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_MAILA) {
				return DNS_ERR(FORMAT_ERROR);
			}
		} else if (r->rr_class == DNS_QCLASS_ANY) {
			if (r->ttl != 0) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->length != 0) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_AXFR) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_MAILB) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_MAILA) {
				return DNS_ERR(FORMAT_ERROR);
			}
		} else if (r->rr_class == DNS_QCLASS_NONE) {
			if (r->ttl != 0) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_ALL) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_AXFR) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_MAILB) {
				return DNS_ERR(FORMAT_ERROR);
			}
			if (r->rr_type == DNS_QTYPE_MAILA) {
				return DNS_ERR(FORMAT_ERROR);
			}
		} else {
			return DNS_ERR(FORMAT_ERROR);
		}
	}
	return WERR_OK;
}

static WERROR dns_rr_to_dnsp(TALLOC_CTX *mem_ctx,
			     const struct dns_res_rec *rrec,
			     struct dnsp_DnssrvRpcRecord *r,
			     bool name_is_static)
{
	enum ndr_err_code ndr_err;

	if (rrec->rr_type == DNS_QTYPE_ALL) {
		return DNS_ERR(FORMAT_ERROR);
	}

	ZERO_STRUCTP(r);

	r->wType = (enum dns_record_type) rrec->rr_type;
	r->dwTtlSeconds = rrec->ttl;
	r->rank = DNS_RANK_ZONE;
	if (name_is_static) {
		r->dwTimeStamp = 0;
	} else {
		r->dwTimeStamp = unix_to_dns_timestamp(time(NULL));
	}

	/* If we get QCLASS_ANY, we're done here */
	if (rrec->rr_class == DNS_QCLASS_ANY) {
		goto done;
	}

	switch(rrec->rr_type) {
	case DNS_QTYPE_A:
		r->data.ipv4 = talloc_strdup(mem_ctx, rrec->rdata.ipv4_record);
		W_ERROR_HAVE_NO_MEMORY(r->data.ipv4);
		break;
	case DNS_QTYPE_AAAA:
		r->data.ipv6 = talloc_strdup(mem_ctx, rrec->rdata.ipv6_record);
		W_ERROR_HAVE_NO_MEMORY(r->data.ipv6);
		break;
	case DNS_QTYPE_NS:
		r->data.ns = talloc_strdup(mem_ctx, rrec->rdata.ns_record);
		W_ERROR_HAVE_NO_MEMORY(r->data.ns);
		break;
	case DNS_QTYPE_CNAME:
		r->data.cname = talloc_strdup(mem_ctx, rrec->rdata.cname_record);
		W_ERROR_HAVE_NO_MEMORY(r->data.cname);
		break;
	case DNS_QTYPE_SRV:
		r->data.srv.wPriority = rrec->rdata.srv_record.priority;
		r->data.srv.wWeight = rrec->rdata.srv_record.weight;
		r->data.srv.wPort = rrec->rdata.srv_record.port;
		r->data.srv.nameTarget = talloc_strdup(mem_ctx,
				rrec->rdata.srv_record.target);
		W_ERROR_HAVE_NO_MEMORY(r->data.srv.nameTarget);
		break;
	case DNS_QTYPE_PTR:
		r->data.ptr = talloc_strdup(mem_ctx, rrec->rdata.ptr_record);
		W_ERROR_HAVE_NO_MEMORY(r->data.ptr);
		break;
	case DNS_QTYPE_MX:
		r->data.mx.wPriority = rrec->rdata.mx_record.preference;
		r->data.mx.nameTarget = talloc_strdup(mem_ctx,
				rrec->rdata.mx_record.exchange);
		W_ERROR_HAVE_NO_MEMORY(r->data.mx.nameTarget);
		break;
	case DNS_QTYPE_TXT:
		ndr_err = ndr_dnsp_string_list_copy(mem_ctx,
						    &rrec->rdata.txt_record.txt,
						    &r->data.txt);
		if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
			return WERR_NOT_ENOUGH_MEMORY;
		}

		break;
	default:
		DEBUG(0, ("Got a qytpe of %d\n", rrec->rr_type));
		return DNS_ERR(NOT_IMPLEMENTED);
	}

done:

	return WERR_OK;
}


static WERROR handle_one_update(struct dns_server *dns,
				TALLOC_CTX *mem_ctx,
				const struct dns_name_question *zone,
				const struct dns_res_rec *update,
				const struct dns_server_tkey *tkey)
{
	struct dnsp_DnssrvRpcRecord *recs = NULL;
	uint16_t rcount = 0;
	struct ldb_dn *dn;
	uint16_t i;
	uint16_t first = 0;
	WERROR werror;
	bool tombstoned = false;
	bool needs_add = false;
	bool name_is_static;

	DBG_NOTICE("Looking at record: \n");
	if (DEBUGLVL(DBGLVL_NOTICE)) {
		NDR_PRINT_DEBUG(dns_res_rec, discard_const(update));
	}

	switch (update->rr_type) {
	case DNS_QTYPE_A:
	case DNS_QTYPE_NS:
	case DNS_QTYPE_CNAME:
	case DNS_QTYPE_SOA:
	case DNS_QTYPE_PTR:
	case DNS_QTYPE_MX:
	case DNS_QTYPE_AAAA:
	case DNS_QTYPE_SRV:
	case DNS_QTYPE_TXT:
	case DNS_QTYPE_ALL:
		break;
	default:
		DEBUG(0, ("Can't handle updates of type %u yet\n",
			  update->rr_type));
		return DNS_ERR(NOT_IMPLEMENTED);
	}

	werror = dns_name2dn(dns, mem_ctx, update->name, &dn);
	DBG_DEBUG("dns_name2dn(): %s\n", win_errstr(werror));
	W_ERROR_NOT_OK_RETURN(werror);

	werror = dns_common_lookup(dns->samdb, mem_ctx, dn,
				   &recs, &rcount, &tombstoned);
	DBG_DEBUG("dns_common_lookup(): %s\n", win_errstr(werror));
	if (W_ERROR_EQUAL(werror, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
		needs_add = true;
		werror = WERR_OK;
	}
	W_ERROR_NOT_OK_RETURN(werror);

	if (tombstoned) {
		/*
		 * we need to keep the existing tombstone record
		 * and ignore it.
		 *
		 * There *should* only be a single record of type TOMBSTONE,
		 * but we don't insist.
		 */
		if (rcount != 1) {
			DBG_WARNING("Tombstoned dnsNode has %u records, "
				    "expected 1\n", rcount);
			if (DEBUGLVL(DBGLVL_WARNING)) {
				NDR_PRINT_DEBUG(dns_res_rec, discard_const(update));
			}
		}
		first = rcount;
	}

	name_is_static = dns_name_is_static(recs, rcount);

	if (update->rr_class == zone->question_class) {
		if (update->rr_type == DNS_QTYPE_CNAME) {
			/*
			 * If there is a record in the directory
			 * that's not a CNAME, ignore update
			 */
			for (i = first; i < rcount; i++) {
				if (recs[i].wType != DNS_TYPE_CNAME) {
					DEBUG(5, ("Skipping update\n"));
					return WERR_OK;
				}
			}

			/*
			 * There should be no entries besides one CNAME record
			 * per name, so replace everything with the new CNAME
			 */

			rcount = first;
			recs = talloc_realloc(mem_ctx, recs,
					struct dnsp_DnssrvRpcRecord, rcount + 1);
			W_ERROR_HAVE_NO_MEMORY(recs);

			werror = dns_rr_to_dnsp(
			    recs, update, &recs[rcount], name_is_static);
			DBG_DEBUG("dns_rr_to_dnsp(CNAME): %s\n", win_errstr(werror));
			W_ERROR_NOT_OK_RETURN(werror);
			rcount += 1;

			werror = dns_replace_records(dns, mem_ctx, dn,
						     needs_add, recs, rcount);
			DBG_DEBUG("dns_replace_records(CNAME): %s\n", win_errstr(werror));
			W_ERROR_NOT_OK_RETURN(werror);

			return WERR_OK;
		} else {
			/*
			 * If there is a CNAME record for this name,
			 * ignore update
			 */
			for (i = first; i < rcount; i++) {
				if (recs[i].wType == DNS_TYPE_CNAME) {
					DEBUG(5, ("Skipping update\n"));
					return WERR_OK;
				}
			}
		}
		if (update->rr_type == DNS_QTYPE_SOA) {
			bool found = false;

			/*
			 * If the zone has no SOA record?? or update's
			 * serial number is smaller than existing SOA's,
			 * ignore update
			 */
			for (i = first; i < rcount; i++) {
				if (recs[i].wType == DNS_TYPE_SOA) {
					uint16_t n, o;

					n = update->rdata.soa_record.serial;
					o = recs[i].data.soa.serial;
					/*
					 * TODO: Implement RFC 1982 comparison
					 * logic for RFC2136
					 */
					if (n <= o) {
						DEBUG(5, ("Skipping update\n"));
						return WERR_OK;
					}
					found = true;
					break;
				}
			}
			if (!found) {
				DEBUG(5, ("Skipping update\n"));
				return WERR_OK;
			}

			werror = dns_rr_to_dnsp(
			    mem_ctx, update, &recs[i], name_is_static);
			DBG_DEBUG("dns_rr_to_dnsp(SOA): %s\n", win_errstr(werror));
			W_ERROR_NOT_OK_RETURN(werror);

			/*
			 * There should only be one SOA, which we have already
			 * found and replaced. We now check for and tombstone
			 * any others.
			 */
			for (i++; i < rcount; i++) {
				if (recs[i].wType != DNS_TYPE_SOA) {
					continue;
				}
				DBG_ERR("Duplicate SOA records found.\n");
				if (DEBUGLVL(DBGLVL_ERR)) {
					NDR_PRINT_DEBUG(dns_res_rec,
							discard_const(update));
				}
				recs[i] = (struct dnsp_DnssrvRpcRecord) {
					.wType = DNS_TYPE_TOMBSTONE,
				};
			}

			werror = dns_replace_records(dns, mem_ctx, dn,
						     needs_add, recs, rcount);
			DBG_DEBUG("dns_replace_records(SOA): %s\n", win_errstr(werror));
			W_ERROR_NOT_OK_RETURN(werror);

			return WERR_OK;
		}
		/* All but CNAME, SOA */
		recs = talloc_realloc(mem_ctx, recs,
				struct dnsp_DnssrvRpcRecord, rcount+1);
		W_ERROR_HAVE_NO_MEMORY(recs);

		werror =
		    dns_rr_to_dnsp(recs, update, &recs[rcount], name_is_static);
		DBG_DEBUG("dns_rr_to_dnsp(GENERIC): %s\n", win_errstr(werror));
		W_ERROR_NOT_OK_RETURN(werror);

		for (i = first; i < rcount; i++) {
			struct dnsp_DnssrvRpcRecord orig_rec = recs[i];

			if (!dns_record_match(&recs[i], &recs[rcount])) {
				continue;
			}

			recs[i].data = recs[rcount].data;
			recs[i].wType = recs[rcount].wType;
			recs[i].dwTtlSeconds = recs[rcount].dwTtlSeconds;
			recs[i].rank = recs[rcount].rank;
			recs[i].dwReserved = 0;
			recs[i].flags = 0;
			werror = dns_replace_records(dns, mem_ctx, dn,
						     needs_add, recs, rcount);
			DBG_DEBUG("dns_replace_records(REPLACE): %s\n", win_errstr(werror));
			if (W_ERROR_EQUAL(werror, WERR_ACCESS_DENIED) &&
			    !needs_add &&
			    orig_rec.dwTtlSeconds == recs[i].dwTtlSeconds)
			{
				DBG_NOTICE("dns_replace_records(REPLACE): %s "
					    "=> skip no-op\n",
					    win_errstr(werror));
				werror = WERR_OK;
			}
			W_ERROR_NOT_OK_RETURN(werror);

			return WERR_OK;
		}
		/* we did not find a matching record. This is new. */
		werror = dns_replace_records(dns, mem_ctx, dn,
					     needs_add, recs, rcount+1);
		DBG_DEBUG("dns_replace_records(ADD): %s\n", win_errstr(werror));
		W_ERROR_NOT_OK_RETURN(werror);

		return WERR_OK;
	} else if (update->rr_class == DNS_QCLASS_ANY) {
		/*
		 * Mass-deleting records by type, which we do by adding a
		 * tombstone with zero timestamp. dns_replace_records() will
		 * work out if the node as a whole needs tombstoning.
		 */
		if (update->rr_type == DNS_QTYPE_ALL) {
			if (samba_dns_name_equal(update->name, zone->name)) {
				for (i = first; i < rcount; i++) {

					if (recs[i].wType == DNS_TYPE_SOA) {
						continue;
					}

					if (recs[i].wType == DNS_TYPE_NS) {
						continue;
					}

					recs[i] = (struct dnsp_DnssrvRpcRecord) {
						.wType = DNS_TYPE_TOMBSTONE,
					};
				}

			} else {
				for (i = first; i < rcount; i++) {
					recs[i] = (struct dnsp_DnssrvRpcRecord) {
						.wType = DNS_TYPE_TOMBSTONE,
					};
				}
			}

		} else if (samba_dns_name_equal(update->name, zone->name)) {

			if (update->rr_type == DNS_QTYPE_SOA) {
				return WERR_OK;
			}

			if (update->rr_type == DNS_QTYPE_NS) {
				return WERR_OK;
			}
		}
		for (i = first; i < rcount; i++) {
			if (recs[i].wType == (enum dns_record_type) update->rr_type) {
				recs[i] = (struct dnsp_DnssrvRpcRecord) {
					.wType = DNS_TYPE_TOMBSTONE,
				};
			}
		}

		werror = dns_replace_records(dns, mem_ctx, dn,
					     needs_add, recs, rcount);
		DBG_DEBUG("dns_replace_records(DELETE-ANY): %s\n", win_errstr(werror));
		W_ERROR_NOT_OK_RETURN(werror);

		return WERR_OK;
	} else if (update->rr_class == DNS_QCLASS_NONE) {
		/* deleting individual records */
		struct dnsp_DnssrvRpcRecord *del_rec;

		if (update->rr_type == DNS_QTYPE_SOA) {
			return WERR_OK;
		}
		if (update->rr_type == DNS_QTYPE_NS) {
			bool found = false;
			struct dnsp_DnssrvRpcRecord *ns_rec = talloc(mem_ctx,
						struct dnsp_DnssrvRpcRecord);
			W_ERROR_HAVE_NO_MEMORY(ns_rec);

			werror = dns_rr_to_dnsp(
			    ns_rec, update, ns_rec, name_is_static);
			DBG_DEBUG("dns_rr_to_dnsp(NS): %s\n", win_errstr(werror));
			W_ERROR_NOT_OK_RETURN(werror);

			for (i = first; i < rcount; i++) {
				if (dns_record_match(ns_rec, &recs[i])) {
					found = true;
					break;
				}
			}
			if (found) {
				return WERR_OK;
			}
		}

		del_rec = talloc(mem_ctx, struct dnsp_DnssrvRpcRecord);
		W_ERROR_HAVE_NO_MEMORY(del_rec);

		werror =
		    dns_rr_to_dnsp(del_rec, update, del_rec, name_is_static);
		DBG_DEBUG("dns_rr_to_dnsp(DELETE-NONE): %s\n", win_errstr(werror));
		W_ERROR_NOT_OK_RETURN(werror);

		for (i = first; i < rcount; i++) {
			if (dns_record_match(del_rec, &recs[i])) {
				recs[i] = (struct dnsp_DnssrvRpcRecord) {
					.wType = DNS_TYPE_TOMBSTONE,
				};
			}
		}

		werror = dns_replace_records(dns, mem_ctx, dn,
					     needs_add, recs, rcount);
		DBG_DEBUG("dns_replace_records(DELETE-NONE): %s\n", win_errstr(werror));
		W_ERROR_NOT_OK_RETURN(werror);
	}

	return WERR_OK;
}

static WERROR handle_updates(struct dns_server *dns,
			     TALLOC_CTX *mem_ctx,
			     const struct dns_name_question *zone,
			     const struct dns_res_rec *prereqs, uint16_t pcount,
			     struct dns_res_rec *updates, uint16_t upd_count,
			     struct dns_server_tkey *tkey)
{
	struct ldb_dn *zone_dn = NULL;
	WERROR werror = WERR_OK;
	int ret;
	uint16_t ri;
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);

	if (tkey != NULL) {
		ret = ldb_set_opaque(
			dns->samdb,
			DSDB_SESSION_INFO,
			tkey->session_info);
		if (ret != LDB_SUCCESS) {
			DEBUG(1, ("unable to set session info\n"));
			werror = DNS_ERR(SERVER_FAILURE);
			goto failed;
		}
	}

	werror = dns_name2dn(dns, tmp_ctx, zone->name, &zone_dn);
	DBG_DEBUG("dns_name2dn(): %s\n", win_errstr(werror));
	W_ERROR_NOT_OK_GOTO(werror, failed);

	ret = ldb_transaction_start(dns->samdb);
	if (ret != LDB_SUCCESS) {
		werror = DNS_ERR(SERVER_FAILURE);
		goto failed;
	}

	werror = check_prerequisites(dns, tmp_ctx, zone, prereqs, pcount);
	W_ERROR_NOT_OK_GOTO(werror, failed);

	DBG_DEBUG("dns update count is %u\n", upd_count);

	for (ri = 0; ri < upd_count; ri++) {
		werror = handle_one_update(dns, tmp_ctx, zone,
					   &updates[ri], tkey);
		DBG_DEBUG("handle_one_update(%u): %s\n",
			  ri, win_errstr(werror));
		W_ERROR_NOT_OK_GOTO(werror, failed);
	}

failed:
	if (W_ERROR_IS_OK(werror)) {
		ret = ldb_transaction_commit(dns->samdb);
		if (ret != LDB_SUCCESS) {
			werror = DNS_ERR(SERVER_FAILURE);
		}
	} else {
		ldb_transaction_cancel(dns->samdb);
	}

	if (tkey != NULL) {
		ldb_set_opaque(
			dns->samdb,
			DSDB_SESSION_INFO,
			system_session(dns->task->lp_ctx));
	}

	TALLOC_FREE(tmp_ctx);
	return werror;

}

static WERROR dns_update_allowed(struct dns_server *dns,
				 const struct dns_request_state *state,
				 struct dns_server_tkey **tkey)
{
	if (lpcfg_allow_dns_updates(dns->task->lp_ctx) == DNS_UPDATE_ON) {
		DEBUG(2, ("All updates allowed.\n"));
		return WERR_OK;
	}

	if (lpcfg_allow_dns_updates(dns->task->lp_ctx) == DNS_UPDATE_OFF) {
		DEBUG(2, ("Updates disabled.\n"));
		return DNS_ERR(REFUSED);
	}

	if (state->authenticated == false ) {
		DEBUG(2, ("Update not allowed for unsigned packet.\n"));
		return DNS_ERR(REFUSED);
	}

        *tkey = dns_find_tkey(dns->tkeys, state->key_name);
	if (*tkey == NULL) {
		DEBUG(0, ("Authenticated, but key not found. Something is wrong.\n"));
		return DNS_ERR(REFUSED);
	}

	return WERR_OK;
}


WERROR dns_server_process_update(struct dns_server *dns,
				 const struct dns_request_state *state,
				 TALLOC_CTX *mem_ctx,
				 const struct dns_name_packet *in,
				 struct dns_res_rec **prereqs,    uint16_t *prereq_count,
				 struct dns_res_rec **updates,    uint16_t *update_count,
				 struct dns_res_rec **additional, uint16_t *arcount)
{
	struct dns_name_question *zone;
	const struct dns_server_zone *z;
	size_t host_part_len = 0;
	WERROR werror = DNS_ERR(NOT_IMPLEMENTED);
	struct dns_server_tkey *tkey = NULL;

	if (in->qdcount != 1) {
		return DNS_ERR(FORMAT_ERROR);
	}

	zone = &in->questions[0];

	if (zone->question_class != DNS_QCLASS_IN &&
	    zone->question_class != DNS_QCLASS_ANY) {
		return DNS_ERR(NOT_IMPLEMENTED);
	}

	if (zone->question_type != DNS_QTYPE_SOA) {
		return DNS_ERR(FORMAT_ERROR);
	}

	DEBUG(2, ("Got a dns update request.\n"));

	for (z = dns->zones; z != NULL; z = z->next) {
		bool match;

		match = dns_name_match(z->name, zone->name, &host_part_len);
		if (match) {
			break;
		}
	}

	if (z == NULL) {
		DEBUG(1, ("We're not authoritative for this zone\n"));
		return DNS_ERR(NOTAUTH);
	}

	if (host_part_len != 0) {
		/* TODO: We need to delegate this one */
		DEBUG(1, ("Would have to delegate zone '%s'.\n", zone->name));
		return DNS_ERR(NOT_IMPLEMENTED);
	}

	*prereq_count = in->ancount;
	*prereqs = in->answers;
	werror = check_prerequisites(dns, mem_ctx, in->questions, *prereqs,
				     *prereq_count);
	W_ERROR_NOT_OK_RETURN(werror);

	werror = dns_update_allowed(dns, state, &tkey);
	if (!W_ERROR_IS_OK(werror)) {
		return werror;
	}

	*update_count = in->nscount;
	*updates = in->nsrecs;
	werror = update_prescan(in->questions, *updates, *update_count);
	DBG_DEBUG("update_prescan(): %s\n", win_errstr(werror));
	W_ERROR_NOT_OK_RETURN(werror);

	werror = handle_updates(dns, mem_ctx, in->questions, *prereqs,
			        *prereq_count, *updates, *update_count, tkey);
	DBG_DEBUG("handle_updates(): %s\n", win_errstr(werror));
	W_ERROR_NOT_OK_RETURN(werror);

	return werror;
}