-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
difffile.c
2127 lines (1991 loc) · 61.1 KB
/
difffile.c
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
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* difffile.c - DIFF file handling source code. Read and write diff files.
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include "difffile.h"
#include "xfrd-disk.h"
#include "util.h"
#include "packet.h"
#include "rdata.h"
#include "udb.h"
#include "nsec3.h"
#include "nsd.h"
#include "rrl.h"
#include "ixfr.h"
#include "zonec.h"
#include "xfrd-catalog-zones.h"
static int
write_64(FILE *out, uint64_t val)
{
return write_data(out, &val, sizeof(val));
}
static int
write_32(FILE *out, uint32_t val)
{
val = htonl(val);
return write_data(out, &val, sizeof(val));
}
static int
write_8(FILE *out, uint8_t val)
{
return write_data(out, &val, sizeof(val));
}
static int
write_str(FILE *out, const char* str)
{
uint32_t len = strlen(str);
if(!write_32(out, len))
return 0;
return write_data(out, str, len);
}
void
diff_write_packet(const char* zone, const char* pat, uint32_t old_serial,
uint32_t new_serial, uint32_t seq_nr, uint8_t* data, size_t len,
struct nsd* nsd, uint64_t filenumber)
{
FILE* df = xfrd_open_xfrfile(nsd, filenumber, seq_nr?"a":"w");
if(!df) {
log_msg(LOG_ERR, "could not open transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
return;
}
/* if first part, first write the header */
if(seq_nr == 0) {
struct timeval tv;
if (gettimeofday(&tv, NULL) != 0) {
log_msg(LOG_ERR, "could not get timestamp for %s: %s",
zone, strerror(errno));
}
if(!write_32(df, DIFF_PART_XFRF) ||
!write_8(df, 0) /* notcommitted(yet) */ ||
!write_32(df, 0) /* numberofparts when done */ ||
!write_64(df, (uint64_t) tv.tv_sec) ||
!write_32(df, (uint32_t) tv.tv_usec) ||
!write_32(df, old_serial) ||
!write_32(df, new_serial) ||
!write_64(df, (uint64_t) tv.tv_sec) ||
!write_32(df, (uint32_t) tv.tv_usec) ||
!write_str(df, zone) ||
!write_str(df, pat)) {
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
}
if(!write_32(df, DIFF_PART_XXFR) ||
!write_32(df, len) ||
!write_data(df, data, len) ||
!write_32(df, len))
{
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
}
fclose(df);
}
void
diff_write_commit(const char* zone, uint32_t old_serial, uint32_t new_serial,
uint32_t num_parts, uint8_t commit, const char* log_str,
struct nsd* nsd, uint64_t filenumber)
{
struct timeval tv;
FILE* df;
if (gettimeofday(&tv, NULL) != 0) {
log_msg(LOG_ERR, "could not set timestamp for %s: %s",
zone, strerror(errno));
}
/* overwrite the first part of the file with 'committed = 1',
* as well as the end_time and number of parts.
* also write old_serial and new_serial, so that a bad file mixup
* will result in unusable serial numbers. */
df = xfrd_open_xfrfile(nsd, filenumber, "r+");
if(!df) {
log_msg(LOG_ERR, "could not open transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
return;
}
if(!write_32(df, DIFF_PART_XFRF) ||
!write_8(df, commit) /* committed */ ||
!write_32(df, num_parts) ||
!write_64(df, (uint64_t) tv.tv_sec) ||
!write_32(df, (uint32_t) tv.tv_usec) ||
!write_32(df, old_serial) ||
!write_32(df, new_serial))
{
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
/* append the log_str to the end of the file */
if(fseek(df, 0, SEEK_END) == -1) {
log_msg(LOG_ERR, "could not fseek transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
if(!write_str(df, log_str)) {
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
fflush(df);
fclose(df);
}
void
diff_update_commit(
const char* zone, uint8_t commit, struct nsd* nsd, uint64_t filenumber)
{
FILE *df;
assert(zone != NULL);
assert(nsd != NULL);
assert(commit == DIFF_NOT_COMMITTED ||
commit == DIFF_COMMITTED ||
commit == DIFF_CORRUPT ||
commit == DIFF_INCONSISTENT ||
commit == DIFF_VERIFIED);
df = xfrd_open_xfrfile(nsd, filenumber, "r+");
if(!df) {
log_msg(LOG_ERR, "could not open transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
return;
}
if(!write_32(df, DIFF_PART_XFRF) || !write_8(df, commit)) {
log_msg(LOG_ERR, "could not write transfer %s file %lld: %s",
zone, (long long)filenumber, strerror(errno));
fclose(df);
return;
}
fflush(df);
fclose(df);
}
int
diff_read_64(FILE *in, uint64_t* result)
{
if (fread(result, sizeof(*result), 1, in) == 1) {
return 1;
} else {
return 0;
}
}
int
diff_read_32(FILE *in, uint32_t* result)
{
if (fread(result, sizeof(*result), 1, in) == 1) {
*result = ntohl(*result);
return 1;
} else {
return 0;
}
}
int
diff_read_8(FILE *in, uint8_t* result)
{
if (fread(result, sizeof(*result), 1, in) == 1) {
return 1;
} else {
return 0;
}
}
int
diff_read_str(FILE* in, char* buf, size_t len)
{
uint32_t disklen;
if(!diff_read_32(in, &disklen))
return 0;
if(disklen >= len)
return 0;
if(fread(buf, disklen, 1, in) != 1)
return 0;
buf[disklen] = 0;
return 1;
}
static void
add_rdata_to_recyclebin(namedb_type* db, rr_type* rr)
{
/* add rdatas to recycle bin. */
size_t i;
for(i=0; i<rr->rdata_count; i++)
{
if(!rdata_atom_is_domain(rr->type, i))
region_recycle(db->region, rr->rdatas[i].data,
rdata_atom_size(rr->rdatas[i])
+ sizeof(uint16_t));
}
region_recycle(db->region, rr->rdatas,
sizeof(rdata_atom_type)*rr->rdata_count);
}
/* this routine determines if below a domain there exist names with
* data (is_existing) or no names below the domain have data.
*/
static int
has_data_below(domain_type* top)
{
domain_type* d = top;
assert(d != NULL);
/* in the canonical ordering subdomains are after this name */
d = domain_next(d);
while(d != NULL && domain_is_subdomain(d, top)) {
if(d->is_existing)
return 1;
d = domain_next(d);
}
return 0;
}
/** check if domain with 0 rrsets has become empty (nonexist) */
static domain_type*
rrset_zero_nonexist_check(domain_type* domain, domain_type* ce)
{
/* is the node now an empty node (completely deleted) */
if(domain->rrsets == 0) {
/* if there is no data below it, it becomes non existing.
also empty nonterminals above it become nonexisting */
/* check for data below this node. */
if(!has_data_below(domain)) {
/* nonexist this domain and all parent empty nonterminals */
domain_type* p = domain;
while(p != NULL && p->rrsets == 0) {
if(p == ce || has_data_below(p))
return p;
p->is_existing = 0;
/* fixup wildcard child of parent */
if(p->parent &&
p->parent->wildcard_child_closest_match == p)
p->parent->wildcard_child_closest_match = domain_previous_existing_child(p);
p = p->parent;
}
}
}
return NULL;
}
/** remove rrset. Adjusts zone params. Does not remove domain */
static void
rrset_delete(namedb_type* db, domain_type* domain, rrset_type* rrset)
{
int i;
/* find previous */
rrset_type** pp = &domain->rrsets;
while(*pp && *pp != rrset) {
pp = &( (*pp)->next );
}
if(!*pp) {
/* rrset does not exist for domain */
return;
}
*pp = rrset->next;
DEBUG(DEBUG_XFRD,2, (LOG_INFO, "delete rrset of %s type %s",
domain_to_string(domain),
rrtype_to_string(rrset_rrtype(rrset))));
/* is this a SOA rrset ? */
if(rrset->zone->soa_rrset == rrset) {
rrset->zone->soa_rrset = 0;
}
if(rrset->zone->ns_rrset == rrset) {
rrset->zone->ns_rrset = 0;
}
if(domain == rrset->zone->apex && rrset_rrtype(rrset) == TYPE_RRSIG) {
for (i = 0; i < rrset->rr_count; ++i) {
if(rr_rrsig_type_covered(&rrset->rrs[i])==TYPE_DNSKEY) {
rrset->zone->is_secure = 0;
break;
}
}
}
/* recycle the memory space of the rrset */
for (i = 0; i < rrset->rr_count; ++i)
add_rdata_to_recyclebin(db, &rrset->rrs[i]);
region_recycle(db->region, rrset->rrs,
sizeof(rr_type) * rrset->rr_count);
rrset->rr_count = 0;
region_recycle(db->region, rrset, sizeof(rrset_type));
}
static int
rdatas_equal(rdata_atom_type *a, rdata_atom_type *b, int num, uint16_t type,
int* rdnum, char** reason)
{
int k, start, end;
start = 0;
end = num;
/**
* SOA RDATA comparisons in XFR are more lenient,
* only serial rdata is checked.
**/
if (type == TYPE_SOA) {
start = 2;
end = 3;
}
for(k = start; k < end; k++)
{
if(rdata_atom_is_domain(type, k)) {
if(dname_compare(domain_dname(a[k].domain),
domain_dname(b[k].domain))!=0) {
*rdnum = k;
*reason = "dname data";
return 0;
}
} else if(rdata_atom_is_literal_domain(type, k)) {
/* literal dname, but compare case insensitive */
if(a[k].data[0] != b[k].data[0]) {
*rdnum = k;
*reason = "literal dname len";
return 0; /* uncompressed len must be equal*/
}
if(!dname_equal_nocase((uint8_t*)(a[k].data+1),
(uint8_t*)(b[k].data+1), a[k].data[0])) {
*rdnum = k;
*reason = "literal dname data";
return 0;
}
} else {
/* check length */
if(a[k].data[0] != b[k].data[0]) {
*rdnum = k;
*reason = "rdata len";
return 0;
}
/* check data */
if(memcmp(a[k].data+1, b[k].data+1, a[k].data[0])!=0) {
*rdnum = k;
*reason = "rdata data";
return 0;
}
}
}
return 1;
}
static void
debug_find_rr_num(rrset_type* rrset, uint16_t type, uint16_t klass,
rdata_atom_type *rdatas, ssize_t rdata_num)
{
int i, rd;
char* reason = "";
for(i=0; i < rrset->rr_count; ++i) {
if (rrset->rrs[i].type != type) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> does not match "
"RR num %d type %s",
dname_to_string(domain_dname(rrset->rrs[i].owner),0),
rrtype_to_string(type), i,
rrtype_to_string(rrset->rrs[i].type));
}
if (rrset->rrs[i].klass != klass) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> class %d "
"does not match RR num %d class %d",
dname_to_string(domain_dname(rrset->rrs[i].owner),0),
rrtype_to_string(type),
klass, i,
rrset->rrs[i].klass);
}
if (rrset->rrs[i].rdata_count != rdata_num) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> rdlen %u "
"does not match RR num %d rdlen %d",
dname_to_string(domain_dname(rrset->rrs[i].owner),0),
rrtype_to_string(type),
(unsigned) rdata_num, i,
(unsigned) rrset->rrs[i].rdata_count);
}
if (!rdatas_equal(rdatas, rrset->rrs[i].rdatas, rdata_num, type,
&rd, &reason)) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> rdata element "
"%d differs from RR num %d rdata (%s)",
dname_to_string(domain_dname(rrset->rrs[i].owner),0),
rrtype_to_string(type),
rd, i, reason);
}
}
}
static int
find_rr_num(rrset_type* rrset, uint16_t type, uint16_t klass,
rdata_atom_type *rdatas, ssize_t rdata_num, int add)
{
int i, rd;
char* reason;
for(i=0; i < rrset->rr_count; ++i) {
if(rrset->rrs[i].type == type &&
rrset->rrs[i].klass == klass &&
rrset->rrs[i].rdata_count == rdata_num &&
rdatas_equal(rdatas, rrset->rrs[i].rdatas, rdata_num, type,
&rd, &reason))
{
return i;
}
}
/* this is odd. Log why rr cannot be found. */
if (!add) {
debug_find_rr_num(rrset, type, klass, rdatas, rdata_num);
}
return -1;
}
#ifdef NSEC3
/* see if nsec3 deletion triggers need action */
static void
nsec3_delete_rr_trigger(namedb_type* db, rr_type* rr, zone_type* zone)
{
/* the RR has not actually been deleted yet, so we can inspect it */
if(!zone->nsec3_param)
return;
/* see if the domain was an NSEC3-domain in the chain, but no longer */
if(rr->type == TYPE_NSEC3 && rr->owner->nsec3 &&
rr->owner->nsec3->nsec3_node.key &&
nsec3_rr_uses_params(rr, zone) &&
nsec3_in_chain_count(rr->owner, zone) <= 1) {
domain_type* prev = nsec3_chain_find_prev(zone, rr->owner);
/* remove from prehash because no longer an NSEC3 domain */
if(domain_is_prehash(db->domains, rr->owner))
prehash_del(db->domains, rr->owner);
/* fixup the last in the zone */
if(rr->owner == zone->nsec3_last)
zone->nsec3_last = prev;
/* unlink from the nsec3tree */
zone_del_domain_in_hash_tree(zone->nsec3tree,
&rr->owner->nsec3->nsec3_node);
/* add previous NSEC3 to the prehash list */
if(prev && prev != rr->owner)
prehash_add(db->domains, prev);
else nsec3_clear_precompile(db, zone);
/* this domain becomes ordinary data domain: done later */
}
/* see if the rr was NSEC3PARAM that we were using */
else if(rr->type == TYPE_NSEC3PARAM && rr == zone->nsec3_param) {
/* clear trees, wipe hashes, wipe precompile */
nsec3_clear_precompile(db, zone);
/* pick up new nsec3param (from udb, or avoid deleted rr) */
nsec3_find_zone_param(db, zone, rr, 0);
/* if no more NSEC3, done */
if(!zone->nsec3_param)
return;
nsec3_precompile_newparam(db, zone);
}
}
/* see if nsec3 prehash can be removed with new rrset content */
static void
nsec3_rrsets_changed_remove_prehash(domain_type* domain, zone_type* zone)
{
/* deletion of rrset already done, we can check if conditions apply */
/* see if the domain is no longer precompiled */
/* it has a hash_node, but no longer fulfills conditions */
if(nsec3_domain_part_of_zone(domain, zone) && domain->nsec3 &&
domain->nsec3->hash_wc &&
domain->nsec3->hash_wc->hash.node.key &&
!nsec3_condition_hash(domain, zone)) {
/* remove precompile */
domain->nsec3->nsec3_cover = NULL;
domain->nsec3->nsec3_wcard_child_cover = NULL;
domain->nsec3->nsec3_is_exact = 0;
/* remove it from the hash tree */
zone_del_domain_in_hash_tree(zone->hashtree,
&domain->nsec3->hash_wc->hash.node);
zone_del_domain_in_hash_tree(zone->wchashtree,
&domain->nsec3->hash_wc->wc.node);
}
if(domain != zone->apex && domain->nsec3 &&
domain->nsec3->ds_parent_hash &&
domain->nsec3->ds_parent_hash->node.key &&
(!domain->parent || nsec3_domain_part_of_zone(domain->parent, zone)) &&
!nsec3_condition_dshash(domain, zone)) {
/* remove precompile */
domain->nsec3->nsec3_ds_parent_cover = NULL;
domain->nsec3->nsec3_ds_parent_is_exact = 0;
/* remove it from the hash tree */
zone_del_domain_in_hash_tree(zone->dshashtree,
&domain->nsec3->ds_parent_hash->node);
}
}
/* see if domain needs to get precompiled info */
static void
nsec3_rrsets_changed_add_prehash(namedb_type* db, domain_type* domain,
zone_type* zone)
{
if(!zone->nsec3_param)
return;
if((!domain->nsec3 || !domain->nsec3->hash_wc
|| !domain->nsec3->hash_wc->hash.node.key)
&& nsec3_condition_hash(domain, zone)) {
region_type* tmpregion = region_create(xalloc, free);
nsec3_precompile_domain(db, domain, zone, tmpregion);
region_destroy(tmpregion);
}
if((!domain->nsec3 || !domain->nsec3->ds_parent_hash
|| !domain->nsec3->ds_parent_hash->node.key)
&& nsec3_condition_dshash(domain, zone)) {
nsec3_precompile_domain_ds(db, domain, zone);
}
}
/* see if nsec3 rrset-deletion triggers need action */
static void
nsec3_delete_rrset_trigger(namedb_type* db, domain_type* domain,
zone_type* zone, uint16_t type)
{
if(!zone->nsec3_param)
return;
nsec3_rrsets_changed_remove_prehash(domain, zone);
/* for type nsec3, or a delegation, the domain may have become a
* 'normal' domain with its remaining data now */
if(type == TYPE_NSEC3 || type == TYPE_NS || type == TYPE_DS)
nsec3_rrsets_changed_add_prehash(db, domain, zone);
/* for type DNAME or a delegation, obscured data may be revealed */
if(type == TYPE_NS || type == TYPE_DS || type == TYPE_DNAME) {
/* walk over subdomains and check them each */
domain_type *d;
for(d=domain_next(domain); d && domain_is_subdomain(d, domain);
d=domain_next(d)) {
nsec3_rrsets_changed_add_prehash(db, d, zone);
}
}
}
/* see if nsec3 addition triggers need action */
static void
nsec3_add_rr_trigger(namedb_type* db, rr_type* rr, zone_type* zone)
{
/* the RR has been added in full, also to UDB (and thus NSEC3PARAM
* in the udb has been adjusted) */
if(zone->nsec3_param && rr->type == TYPE_NSEC3 &&
(!rr->owner->nsec3 || !rr->owner->nsec3->nsec3_node.key)
&& nsec3_rr_uses_params(rr, zone)) {
if(!zone->nsec3_last) {
/* all nsec3s have previously been deleted, but
* we have nsec3 parameters, set it up again from
* being cleared. */
nsec3_precompile_newparam(db, zone);
}
/* added NSEC3 into the chain */
nsec3_precompile_nsec3rr(db, rr->owner, zone);
/* the domain has become an NSEC3-domain, if it was precompiled
* previously, remove that, neatly done in routine above */
nsec3_rrsets_changed_remove_prehash(rr->owner, zone);
/* set this NSEC3 to prehash */
prehash_add(db->domains, rr->owner);
} else if(!zone->nsec3_param && rr->type == TYPE_NSEC3PARAM) {
/* see if this means NSEC3 chain can be used */
nsec3_find_zone_param(db, zone, NULL, 0);
if(!zone->nsec3_param)
return;
nsec3_zone_trees_create(db->region, zone);
nsec3_precompile_newparam(db, zone);
}
}
/* see if nsec3 rrset-addition triggers need action */
static void
nsec3_add_rrset_trigger(namedb_type* db, domain_type* domain, zone_type* zone,
uint16_t type)
{
/* the rrset has been added so we can inspect it */
if(!zone->nsec3_param)
return;
/* because the rrset is added we can check conditions easily.
* check if domain needs to become precompiled now */
nsec3_rrsets_changed_add_prehash(db, domain, zone);
/* if a delegation, it changes from normal name to unhashed referral */
if(type == TYPE_NS || type == TYPE_DS) {
nsec3_rrsets_changed_remove_prehash(domain, zone);
}
/* if delegation or DNAME added, then some RRs may get obscured */
if(type == TYPE_NS || type == TYPE_DS || type == TYPE_DNAME) {
/* walk over subdomains and check them each */
domain_type *d;
for(d=domain_next(domain); d && domain_is_subdomain(d, domain);
d=domain_next(d)) {
nsec3_rrsets_changed_remove_prehash(d, zone);
}
}
}
#endif /* NSEC3 */
/* fixup usage lower for domain names in the rdata */
static void
rr_lower_usage(namedb_type* db, rr_type* rr)
{
unsigned i;
for(i=0; i<rr->rdata_count; i++) {
if(rdata_atom_is_domain(rr->type, i)) {
assert(rdata_atom_domain(rr->rdatas[i])->usage > 0);
rdata_atom_domain(rr->rdatas[i])->usage --;
if(rdata_atom_domain(rr->rdatas[i])->usage == 0)
domain_table_deldomain(db,
rdata_atom_domain(rr->rdatas[i]));
}
}
}
static void
rrset_lower_usage(namedb_type* db, rrset_type* rrset)
{
unsigned i;
for(i=0; i<rrset->rr_count; i++)
rr_lower_usage(db, &rrset->rrs[i]);
}
int
delete_RR(namedb_type* db, const dname_type* dname,
uint16_t type, uint16_t klass,
buffer_type* packet, size_t rdatalen, zone_type *zone,
region_type* temp_region, int* softfail)
{
domain_type *domain;
rrset_type *rrset;
domain = domain_table_find(db->domains, dname);
if(!domain) {
log_msg(LOG_WARNING, "diff: domain %s does not exist",
dname_to_string(dname,0));
buffer_skip(packet, rdatalen);
*softfail = 1;
return 1; /* not fatal error */
}
rrset = domain_find_rrset(domain, zone, type);
if(!rrset) {
log_msg(LOG_WARNING, "diff: rrset %s does not exist",
dname_to_string(dname,0));
buffer_skip(packet, rdatalen);
*softfail = 1;
return 1; /* not fatal error */
} else {
/* find the RR in the rrset */
domain_table_type *temptable;
rdata_atom_type *rdatas;
ssize_t rdata_num;
int rrnum;
temptable = domain_table_create(temp_region);
/* This will ensure that the dnames in rdata are
* normalized, conform RFC 4035, section 6.2
*/
rdata_num = rdata_wireformat_to_rdata_atoms(
temp_region, temptable, type, rdatalen, packet, &rdatas);
if(rdata_num == -1) {
log_msg(LOG_ERR, "diff: bad rdata for %s",
dname_to_string(dname,0));
return 0;
}
rrnum = find_rr_num(rrset, type, klass, rdatas, rdata_num, 0);
if(rrnum == -1 && type == TYPE_SOA && domain == zone->apex
&& rrset->rr_count != 0)
rrnum = 0; /* replace existing SOA if no match */
if(rrnum == -1) {
log_msg(LOG_WARNING, "diff: RR <%s, %s> does not exist",
dname_to_string(dname,0), rrtype_to_string(type));
*softfail = 1;
return 1; /* not fatal error */
}
#ifdef NSEC3
/* process triggers for RR deletions */
nsec3_delete_rr_trigger(db, &rrset->rrs[rrnum], zone);
#endif
/* lower usage (possibly deleting other domains, and thus
* invalidating the current RR's domain pointers) */
rr_lower_usage(db, &rrset->rrs[rrnum]);
if(rrset->rr_count == 1) {
/* delete entire rrset */
rrset_delete(db, domain, rrset);
/* check if domain is now nonexisting (or parents) */
rrset_zero_nonexist_check(domain, NULL);
#ifdef NSEC3
/* cleanup nsec3 */
nsec3_delete_rrset_trigger(db, domain, zone, type);
#endif
/* see if the domain can be deleted (and inspect parents) */
domain_table_deldomain(db, domain);
} else {
/* swap out the bad RR and decrease the count */
rr_type* rrs_orig = rrset->rrs;
add_rdata_to_recyclebin(db, &rrset->rrs[rrnum]);
if(rrnum < rrset->rr_count-1)
rrset->rrs[rrnum] = rrset->rrs[rrset->rr_count-1];
memset(&rrset->rrs[rrset->rr_count-1], 0, sizeof(rr_type));
/* realloc the rrs array one smaller */
rrset->rrs = region_alloc_array_init(db->region, rrs_orig,
(rrset->rr_count-1), sizeof(rr_type));
if(!rrset->rrs) {
log_msg(LOG_ERR, "out of memory, %s:%d", __FILE__, __LINE__);
exit(1);
}
region_recycle(db->region, rrs_orig,
sizeof(rr_type) * rrset->rr_count);
#ifdef NSEC3
if(type == TYPE_NSEC3PARAM && zone->nsec3_param) {
/* fixup nsec3_param pointer to same RR */
assert(zone->nsec3_param >= rrs_orig &&
zone->nsec3_param <=
rrs_orig+rrset->rr_count);
/* last moved to rrnum, others at same index*/
if(zone->nsec3_param == &rrs_orig[
rrset->rr_count-1])
zone->nsec3_param = &rrset->rrs[rrnum];
else
zone->nsec3_param =
(void*)zone->nsec3_param
-(void*)rrs_orig +
(void*)rrset->rrs;
}
#endif /* NSEC3 */
rrset->rr_count --;
#ifdef NSEC3
/* for type nsec3, the domain may have become a
* 'normal' domain with its remaining data now */
if(type == TYPE_NSEC3)
nsec3_rrsets_changed_add_prehash(db, domain,
zone);
#endif /* NSEC3 */
}
}
return 1;
}
int
add_RR(namedb_type* db, const dname_type* dname,
uint16_t type, uint16_t klass, uint32_t ttl,
buffer_type* packet, size_t rdatalen, zone_type *zone,
int* softfail)
{
domain_type* domain;
rrset_type* rrset;
rdata_atom_type *rdatas;
rr_type *rrs_old;
ssize_t rdata_num;
int rrnum;
#ifdef NSEC3
int rrset_added = 0;
#endif
domain = domain_table_find(db->domains, dname);
if(!domain) {
/* create the domain */
domain = domain_table_insert(db->domains, dname);
}
rrset = domain_find_rrset(domain, zone, type);
if(!rrset) {
/* create the rrset */
rrset = region_alloc(db->region, sizeof(rrset_type));
if(!rrset) {
log_msg(LOG_ERR, "out of memory, %s:%d", __FILE__, __LINE__);
exit(1);
}
rrset->zone = zone;
rrset->rrs = 0;
rrset->rr_count = 0;
domain_add_rrset(domain, rrset);
#ifdef NSEC3
rrset_added = 1;
#endif
}
/* dnames in rdata are normalized, conform RFC 4035,
* Section 6.2
*/
rdata_num = rdata_wireformat_to_rdata_atoms(
db->region, db->domains, type, rdatalen, packet, &rdatas);
if(rdata_num == -1) {
log_msg(LOG_ERR, "diff: bad rdata for %s",
dname_to_string(dname,0));
return 0;
}
rrnum = find_rr_num(rrset, type, klass, rdatas, rdata_num, 1);
if(rrnum != -1) {
DEBUG(DEBUG_XFRD, 2, (LOG_ERR, "diff: RR <%s, %s> already exists",
dname_to_string(dname,0), rrtype_to_string(type)));
/* ignore already existing RR: lenient accepting of messages */
*softfail = 1;
return 1;
}
if(rrset->rr_count == 65535) {
log_msg(LOG_ERR, "diff: too many RRs at %s",
dname_to_string(dname,0));
return 0;
}
/* re-alloc the rrs and add the new */
rrs_old = rrset->rrs;
rrset->rrs = region_alloc_array(db->region,
(rrset->rr_count+1), sizeof(rr_type));
if(!rrset->rrs) {
log_msg(LOG_ERR, "out of memory, %s:%d", __FILE__, __LINE__);
exit(1);
}
if(rrs_old)
memcpy(rrset->rrs, rrs_old, rrset->rr_count * sizeof(rr_type));
region_recycle(db->region, rrs_old, sizeof(rr_type) * rrset->rr_count);
rrset->rr_count ++;
rrset->rrs[rrset->rr_count - 1].owner = domain;
rrset->rrs[rrset->rr_count - 1].rdatas = rdatas;
rrset->rrs[rrset->rr_count - 1].ttl = ttl;
rrset->rrs[rrset->rr_count - 1].type = type;
rrset->rrs[rrset->rr_count - 1].klass = klass;
rrset->rrs[rrset->rr_count - 1].rdata_count = rdata_num;
/* see if it is a SOA */
if(domain == zone->apex) {
apex_rrset_checks(db, rrset, domain);
#ifdef NSEC3
if(type == TYPE_NSEC3PARAM && zone->nsec3_param) {
/* the pointer just changed, fix it up to point
* to the same record */
assert(zone->nsec3_param >= rrs_old &&
zone->nsec3_param < rrs_old+rrset->rr_count);
/* in this order to make sure no overflow/underflow*/
zone->nsec3_param = (void*)zone->nsec3_param -
(void*)rrs_old + (void*)rrset->rrs;
}
#endif /* NSEC3 */
}
#ifdef NSEC3
if(rrset_added) {
domain_type* p = domain->parent;
nsec3_add_rrset_trigger(db, domain, zone, type);
/* go up and process (possibly created) empty nonterminals,
* until we hit the apex or root */
while(p && p->rrsets == NULL && !p->is_apex) {
nsec3_rrsets_changed_add_prehash(db, p, zone);
p = p->parent;
}
}
nsec3_add_rr_trigger(db, &rrset->rrs[rrset->rr_count - 1], zone);
#endif /* NSEC3 */
return 1;
}
static zone_type*
find_or_create_zone(namedb_type* db, const dname_type* zone_name,
struct nsd_options* opt, const char* zstr, const char* patname)
{
zone_type* zone;
struct zone_options* zopt;
zone = namedb_find_zone(db, zone_name);
if(zone) {
return zone;
}
zopt = zone_options_find(opt, zone_name);
if(!zopt) {
/* if _implicit_ then insert as _part_of_config */
if(strncmp(patname, PATTERN_IMPLICIT_MARKER,
strlen(PATTERN_IMPLICIT_MARKER)) == 0) {
zopt = zone_options_create(opt->region);
if(!zopt) return 0;
zopt->part_of_config = 1;
zopt->name = region_strdup(opt->region, zstr);
zopt->pattern = pattern_options_find(opt, patname);
if(!zopt->name || !zopt->pattern) return 0;
if(!nsd_options_insert_zone(opt, zopt)) {
log_msg(LOG_ERR, "bad domain name or duplicate zone '%s' "
"pattern %s", zstr, patname);
}
} else {
/* create zone : presumably already added to zonelist
* by xfrd, who wrote the AXFR or IXFR to disk, so we only
* need to add it to our config.
* This process does not need linesize and offset zonelist */
zopt = zone_list_zone_insert(opt, zstr, patname);
if(!zopt)
return 0;
}
}
zone = namedb_zone_create(db, zone_name, zopt);
return zone;
}
void
delete_zone_rrs(namedb_type* db, zone_type* zone)
{
rrset_type *rrset;
domain_type *domain = zone->apex, *next;
int nonexist_check = 0;
/* go through entire tree below the zone apex (incl subzones) */
while(domain && domain_is_subdomain(domain, zone->apex))
{
DEBUG(DEBUG_XFRD,2, (LOG_INFO, "delete zone visit %s",
domain_to_string(domain)));
/* delete all rrsets of the zone */
while((rrset = domain_find_any_rrset(domain, zone))) {
/* lower usage can delete other domains */
rrset_lower_usage(db, rrset);
/* rrset del does not delete our domain(yet) */
rrset_delete(db, domain, rrset);
/* no rrset_zero_nonexist_check, do that later */
if(domain->rrsets == 0)
nonexist_check = 1;
}
/* the delete upcoming could delete parents, but nothing next
* or after the domain so store next ptr */
next = domain_next(domain);
/* see if the domain can be deleted (and inspect parents) */
domain_table_deldomain(db, domain);
domain = next;
}
/* check if data deletions have created nonexisting domain entries,
* but after deleting domains so the checks are faster */
if(nonexist_check) {
domain_type* ce = NULL; /* for speeding up has_data_below */
DEBUG(DEBUG_XFRD, 1, (LOG_INFO, "axfrdel: zero rrset check"));
domain = zone->apex;
while(domain && domain_is_subdomain(domain, zone->apex))
{
/* the interesting domains should be existing==1
* and rrsets==0, speeding up out processing of
* sub-zones, since we only spuriously check empty
* nonterminals */
if(domain->is_existing)
ce = rrset_zero_nonexist_check(domain, ce);
domain = domain_next(domain);
}
}
DEBUG(DEBUG_XFRD, 1, (LOG_INFO, "axfrdel: recyclebin holds %lu bytes",
(unsigned long) region_get_recycle_size(db->region)));
#ifndef NDEBUG
if(nsd_debug_level >= 2)
region_log_stats(db->region);
#endif
assert(zone->soa_rrset == 0);
/* keep zone->soa_nx_rrset alloced: it is reused */
assert(zone->ns_rrset == 0);
assert(zone->is_secure == 0);
}
/* return value 0: syntaxerror,badIXFR, 1:OK, 2:done_and_skip_it */
static int
apply_ixfr(nsd_type* nsd, FILE *in, uint32_t serialno,
uint32_t seq_nr, uint32_t seq_total,
int* is_axfr, int* delete_mode, int* rr_count,
struct zone* zone, int* bytes,
int* softfail, struct ixfr_store* ixfr_store)
{
uint32_t msglen, checklen, pkttype;