1 /*
2 * ixfr.c -- generating IXFR responses.
3 *
4 * Copyright (c) 2021, NLnet Labs. All rights reserved.
5 *
6 * See LICENSE for the license.
7 *
8 */
9
10 #include "config.h"
11
12 #include <errno.h>
13 #include <string.h>
14 #include <ctype.h>
15 #ifdef HAVE_SYS_TYPES_H
16 # include <sys/types.h>
17 #endif
18 #ifdef HAVE_SYS_STAT_H
19 # include <sys/stat.h>
20 #endif
21 #include <unistd.h>
22
23 #include "ixfr.h"
24 #include "packet.h"
25 #include "rdata.h"
26 #include "axfr.h"
27 #include "options.h"
28 #include "zonec.h"
29
30 /*
31 * For optimal compression IXFR response packets are limited in size
32 * to MAX_COMPRESSION_OFFSET.
33 */
34 #define IXFR_MAX_MESSAGE_LEN MAX_COMPRESSION_OFFSET
35
36 /* draft-ietf-dnsop-rfc2845bis-06, section 5.3.1 says to sign every packet */
37 #define IXFR_TSIG_SIGN_EVERY_NTH 0 /* tsig sign every N packets. */
38
39 /* initial space in rrs data for storing records */
40 #define IXFR_STORE_INITIAL_SIZE 4096
41
42 /* store compression for one name */
43 struct rrcompress_entry {
44 /* rbtree node, key is this struct */
45 struct rbnode node;
46 /* the uncompressed domain name */
47 const uint8_t* dname;
48 /* the length of the dname, includes terminating 0 label */
49 uint16_t len;
50 /* the offset of the dname in the packet */
51 uint16_t offset;
52 };
53
54 /* structure to store compression data for the packet */
55 struct pktcompression {
56 /* rbtree of rrcompress_entry. sorted by dname */
57 struct rbtree tree;
58 /* allocation information, how many bytes allocated now */
59 size_t alloc_now;
60 /* allocation information, total size in block */
61 size_t alloc_max;
62 /* region to use if block full, this is NULL if unused */
63 struct region* region;
64 /* block of temp data for allocation */
65 uint8_t block[sizeof(struct rrcompress_entry)*1024];
66 };
67
68 /* compare two elements in the compression tree. Returns -1, 0, or 1. */
compression_cmp(const void * a,const void * b)69 static int compression_cmp(const void* a, const void* b)
70 {
71 struct rrcompress_entry* rra = (struct rrcompress_entry*)a;
72 struct rrcompress_entry* rrb = (struct rrcompress_entry*)b;
73 if(rra->len != rrb->len) {
74 if(rra->len < rrb->len)
75 return -1;
76 return 1;
77 }
78 return memcmp(rra->dname, rrb->dname, rra->len);
79 }
80
81 /* init the pktcompression to a new packet */
pktcompression_init(struct pktcompression * pcomp)82 static void pktcompression_init(struct pktcompression* pcomp)
83 {
84 pcomp->alloc_now = 0;
85 pcomp->alloc_max = sizeof(pcomp->block);
86 pcomp->region = NULL;
87 pcomp->tree.root = RBTREE_NULL;
88 pcomp->tree.count = 0;
89 pcomp->tree.region = NULL;
90 pcomp->tree.cmp = &compression_cmp;
91 }
92
93 /* freeup the pktcompression data */
pktcompression_freeup(struct pktcompression * pcomp)94 static void pktcompression_freeup(struct pktcompression* pcomp)
95 {
96 if(pcomp->region) {
97 region_destroy(pcomp->region);
98 pcomp->region = NULL;
99 }
100 pcomp->alloc_now = 0;
101 pcomp->tree.root = RBTREE_NULL;
102 pcomp->tree.count = 0;
103 }
104
105 /* alloc data in pktcompression */
pktcompression_alloc(struct pktcompression * pcomp,size_t s)106 static void* pktcompression_alloc(struct pktcompression* pcomp, size_t s)
107 {
108 /* first attempt to allocate in the fixed block,
109 * that is very fast and on the stack in the pcomp struct */
110 if(pcomp->alloc_now + s <= pcomp->alloc_max) {
111 void* ret = pcomp->block + pcomp->alloc_now;
112 pcomp->alloc_now += s;
113 return ret;
114 }
115
116 /* if that fails, create a region to allocate in,
117 * it is freed in the freeup */
118 if(!pcomp->region) {
119 pcomp->region = region_create(xalloc, free);
120 if(!pcomp->region)
121 return NULL;
122 }
123 return region_alloc(pcomp->region, s);
124 }
125
126 /* find a pktcompression name, return offset if found */
pktcompression_find(struct pktcompression * pcomp,const uint8_t * dname,size_t len)127 static uint16_t pktcompression_find(struct pktcompression* pcomp,
128 const uint8_t* dname, size_t len)
129 {
130 struct rrcompress_entry key, *found;
131 key.node.key = &key;
132 key.dname = dname;
133 key.len = len;
134 found = (struct rrcompress_entry*)rbtree_search(&pcomp->tree, &key);
135 if(found) return found->offset;
136 return 0;
137 }
138
139 /* insert a new domain name into the compression tree.
140 * it fails silently, no need to compress then. */
pktcompression_insert(struct pktcompression * pcomp,const uint8_t * dname,size_t len,uint16_t offset)141 static void pktcompression_insert(struct pktcompression* pcomp,
142 const uint8_t* dname, size_t len, uint16_t offset)
143 {
144 struct rrcompress_entry* entry;
145 if(len > 65535)
146 return;
147 if(offset > MAX_COMPRESSION_OFFSET)
148 return; /* too far for a compression pointer */
149 entry = pktcompression_alloc(pcomp, sizeof(*entry));
150 if(!entry)
151 return;
152 memset(&entry->node, 0, sizeof(entry->node));
153 entry->node.key = entry;
154 entry->dname = dname;
155 entry->len = len;
156 entry->offset = offset;
157 (void)rbtree_insert(&pcomp->tree, &entry->node);
158 }
159
160 /* insert all the labels of a domain name */
pktcompression_insert_with_labels(struct pktcompression * pcomp,uint8_t * dname,size_t len,uint16_t offset)161 static void pktcompression_insert_with_labels(struct pktcompression* pcomp,
162 uint8_t* dname, size_t len, uint16_t offset)
163 {
164 if(!dname)
165 return;
166 if(offset > MAX_COMPRESSION_OFFSET)
167 return;
168
169 /* while we have not seen the end root label */
170 while(len > 0 && dname[0] != 0) {
171 size_t lablen;
172 pktcompression_insert(pcomp, dname, len, offset);
173 lablen = (size_t)(dname[0]);
174 if( (lablen&0xc0) )
175 return; /* the dname should be uncompressed */
176 if(lablen+1 > len)
177 return; /* len should be uncompressed wireformat len */
178 if(offset > MAX_COMPRESSION_OFFSET - lablen - 1)
179 return; /* offset moves too far for compression */
180 /* skip label */
181 len -= lablen+1;
182 dname += lablen+1;
183 offset += lablen+1;
184 }
185 }
186
187 /* calculate length of dname in uncompressed wireformat in buffer */
dname_length(const uint8_t * buf,size_t len)188 static size_t dname_length(const uint8_t* buf, size_t len)
189 {
190 size_t l = 0;
191 if(!buf || len == 0)
192 return l;
193 while(len > 0 && buf[0] != 0) {
194 size_t lablen = (size_t)(buf[0]);
195 if( (lablen&0xc0) )
196 return 0; /* the name should be uncompressed */
197 if(lablen+1 > len)
198 return 0; /* should fit in the buffer */
199 l += lablen+1;
200 len -= lablen+1;
201 buf += lablen+1;
202 }
203 if(len == 0)
204 return 0; /* end label should fit in buffer */
205 if(buf[0] != 0)
206 return 0; /* must end in root label */
207 l += 1; /* for the end root label */
208 return l;
209 }
210
211 /* write a compressed domain name into the packet,
212 * returns uncompressed wireformat length,
213 * 0 if it does not fit and -1 on failure, bad dname. */
pktcompression_write_dname(struct buffer * packet,struct pktcompression * pcomp,const uint8_t * rr,size_t rrlen)214 static int pktcompression_write_dname(struct buffer* packet,
215 struct pktcompression* pcomp, const uint8_t* rr, size_t rrlen)
216 {
217 size_t wirelen = 0;
218 size_t dname_len = dname_length(rr, rrlen);
219 if(!rr || rrlen == 0 || dname_len == 0)
220 return 0;
221 while(rrlen > 0 && rr[0] != 0) {
222 size_t lablen = (size_t)(rr[0]);
223 uint16_t offset;
224 if( (lablen&0xc0) )
225 return -1; /* name should be uncompressed */
226 if(lablen+1 > rrlen)
227 return -1; /* name should fit */
228
229 /* see if the domain name has a compression pointer */
230 if((offset=pktcompression_find(pcomp, rr, dname_len))!=0) {
231 if(!buffer_available(packet, 2))
232 return 0;
233 buffer_write_u16(packet, (uint16_t)(0xc000 | offset));
234 wirelen += dname_len;
235 return wirelen;
236 } else {
237 if(!buffer_available(packet, lablen+1))
238 return 0;
239 /* insert the domain name at this position */
240 pktcompression_insert(pcomp, rr, dname_len,
241 buffer_position(packet));
242 /* write it */
243 buffer_write(packet, rr, lablen+1);
244 }
245
246 wirelen += lablen+1;
247 rr += lablen+1;
248 rrlen -= lablen+1;
249 dname_len -= lablen+1;
250 }
251 if(rrlen > 0 && rr[0] == 0) {
252 /* write end root label */
253 if(!buffer_available(packet, 1))
254 return 0;
255 buffer_write_u8(packet, 0);
256 wirelen += 1;
257 }
258 return wirelen;
259 }
260
261 /* write an RR into the packet with compression for domain names,
262 * return 0 and resets position if it does not fit in the packet. */
ixfr_write_rr_pkt(struct query * query,struct buffer * packet,struct pktcompression * pcomp,const uint8_t * rr,size_t rrlen,uint16_t total_added)263 static int ixfr_write_rr_pkt(struct query* query, struct buffer* packet,
264 struct pktcompression* pcomp, const uint8_t* rr, size_t rrlen,
265 uint16_t total_added)
266 {
267 size_t oldpos = buffer_position(packet);
268 size_t rdpos;
269 uint16_t tp;
270 int dname_len;
271 size_t rdlen;
272 size_t i;
273 rrtype_descriptor_type* descriptor;
274
275 if(total_added == 0) {
276 size_t oldmaxlen = query->maxlen;
277 /* RR > 16K can be first RR */
278 query->maxlen = (query->tcp?TCP_MAX_MESSAGE_LEN:UDP_MAX_MESSAGE_LEN);
279 if(query_overflow(query)) {
280 query->maxlen = oldmaxlen;
281 return 0;
282 }
283 query->maxlen = oldmaxlen;
284 } else {
285 if(buffer_position(packet) > MAX_COMPRESSION_OFFSET
286 || query_overflow(query)) {
287 /* we are past the maximum length */
288 return 0;
289 }
290 }
291
292 /* write owner */
293 dname_len = pktcompression_write_dname(packet, pcomp, rr, rrlen);
294 if(dname_len == -1)
295 return 1; /* attempt to skip this malformed rr, could assert */
296 if(dname_len == 0) {
297 buffer_set_position(packet, oldpos);
298 return 0;
299 }
300 rr += dname_len;
301 rrlen -= dname_len;
302
303 /* type, class, ttl, rdatalen */
304 if(!buffer_available(packet, 10)) {
305 buffer_set_position(packet, oldpos);
306 return 0;
307 }
308 if(10 > rrlen)
309 return 1; /* attempt to skip this malformed rr, could assert */
310 tp = read_uint16(rr);
311 buffer_write(packet, rr, 8);
312 rr += 8;
313 rrlen -= 8;
314 rdlen = read_uint16(rr);
315 rr += 2;
316 rrlen -= 2;
317 rdpos = buffer_position(packet);
318 buffer_write_u16(packet, 0);
319 if(rdlen > rrlen)
320 return 1; /* attempt to skip this malformed rr, could assert */
321
322 /* rdata */
323 descriptor = rrtype_descriptor_by_type(tp);
324 for(i=0; i<descriptor->maximum; i++) {
325 size_t copy_len = 0;
326 if(rdlen == 0)
327 break;
328
329 switch(rdata_atom_wireformat_type(tp, i)) {
330 case RDATA_WF_COMPRESSED_DNAME:
331 dname_len = pktcompression_write_dname(packet, pcomp,
332 rr, rdlen);
333 if(dname_len == -1)
334 return 1; /* attempt to skip malformed rr */
335 if(dname_len == 0) {
336 buffer_set_position(packet, oldpos);
337 return 0;
338 }
339 rr += dname_len;
340 rdlen -= dname_len;
341 break;
342 case RDATA_WF_UNCOMPRESSED_DNAME:
343 case RDATA_WF_LITERAL_DNAME:
344 copy_len = rdlen;
345 break;
346 case RDATA_WF_BYTE:
347 copy_len = 1;
348 break;
349 case RDATA_WF_SHORT:
350 copy_len = 2;
351 break;
352 case RDATA_WF_LONG:
353 copy_len = 4;
354 break;
355 case RDATA_WF_TEXTS:
356 case RDATA_WF_LONG_TEXT:
357 copy_len = rdlen;
358 break;
359 case RDATA_WF_TEXT:
360 case RDATA_WF_BINARYWITHLENGTH:
361 copy_len = 1;
362 if(rdlen > copy_len)
363 copy_len += rr[0];
364 break;
365 case RDATA_WF_A:
366 copy_len = 4;
367 break;
368 case RDATA_WF_AAAA:
369 copy_len = 16;
370 break;
371 case RDATA_WF_ILNP64:
372 copy_len = 8;
373 break;
374 case RDATA_WF_EUI48:
375 copy_len = EUI48ADDRLEN;
376 break;
377 case RDATA_WF_EUI64:
378 copy_len = EUI64ADDRLEN;
379 break;
380 case RDATA_WF_BINARY:
381 copy_len = rdlen;
382 break;
383 case RDATA_WF_APL:
384 copy_len = (sizeof(uint16_t) /* address family */
385 + sizeof(uint8_t) /* prefix */
386 + sizeof(uint8_t)); /* length */
387 if(copy_len <= rdlen)
388 copy_len += (rr[copy_len-1]&APL_LENGTH_MASK);
389 break;
390 case RDATA_WF_IPSECGATEWAY:
391 copy_len = rdlen;
392 break;
393 case RDATA_WF_SVCPARAM:
394 copy_len = 4;
395 if(copy_len <= rdlen)
396 copy_len += read_uint16(rr+2);
397 break;
398 default:
399 copy_len = rdlen;
400 break;
401 }
402 if(copy_len) {
403 if(!buffer_available(packet, copy_len)) {
404 buffer_set_position(packet, oldpos);
405 return 0;
406 }
407 if(copy_len > rdlen)
408 return 1; /* assert of skip malformed */
409 buffer_write(packet, rr, copy_len);
410 rr += copy_len;
411 rdlen -= copy_len;
412 }
413 }
414 /* write compressed rdata length */
415 buffer_write_u16_at(packet, rdpos, buffer_position(packet)-rdpos-2);
416 if(total_added == 0) {
417 size_t oldmaxlen = query->maxlen;
418 query->maxlen = (query->tcp?TCP_MAX_MESSAGE_LEN:UDP_MAX_MESSAGE_LEN);
419 if(query_overflow(query)) {
420 query->maxlen = oldmaxlen;
421 buffer_set_position(packet, oldpos);
422 return 0;
423 }
424 query->maxlen = oldmaxlen;
425 } else {
426 if(query_overflow(query)) {
427 /* we are past the maximum length */
428 buffer_set_position(packet, oldpos);
429 return 0;
430 }
431 }
432 return 1;
433 }
434
435 /* parse the serial number from the IXFR query */
parse_qserial(struct buffer * packet,uint32_t * qserial,size_t * snip_pos)436 static int parse_qserial(struct buffer* packet, uint32_t* qserial,
437 size_t* snip_pos)
438 {
439 unsigned int i;
440 uint16_t type, rdlen;
441 /* we must have a SOA in the authority section */
442 if(NSCOUNT(packet) == 0)
443 return 0;
444 /* skip over the question section, we want only one */
445 buffer_set_position(packet, QHEADERSZ);
446 if(QDCOUNT(packet) != 1)
447 return 0;
448 if(!packet_skip_rr(packet, 1))
449 return 0;
450 /* set position to snip off the authority section */
451 *snip_pos = buffer_position(packet);
452 /* skip over the authority section RRs until we find the SOA */
453 for(i=0; i<NSCOUNT(packet); i++) {
454 /* is this the SOA record? */
455 if(!packet_skip_dname(packet))
456 return 0; /* malformed name */
457 if(!buffer_available(packet, 10))
458 return 0; /* no type,class,ttl,rdatalen */
459 type = buffer_read_u16(packet);
460 buffer_skip(packet, 6);
461 rdlen = buffer_read_u16(packet);
462 if(!buffer_available(packet, rdlen))
463 return 0;
464 if(type == TYPE_SOA) {
465 /* read serial from rdata, skip two dnames, then
466 * read the 32bit value */
467 if(!packet_skip_dname(packet))
468 return 0; /* malformed nsname */
469 if(!packet_skip_dname(packet))
470 return 0; /* malformed rname */
471 if(!buffer_available(packet, 4))
472 return 0;
473 *qserial = buffer_read_u32(packet);
474 return 1;
475 }
476 buffer_skip(packet, rdlen);
477 }
478 return 0;
479 }
480
481 /* get serial from SOA RR */
soa_rr_get_serial(struct rr * rr)482 static uint32_t soa_rr_get_serial(struct rr* rr)
483 {
484 if(rr->rdata_count < 3)
485 return 0;
486 if(rr->rdatas[2].data[0] < 4)
487 return 0;
488 return read_uint32(&rr->rdatas[2].data[1]);
489 }
490
491 /* get the current serial from the zone */
zone_get_current_serial(struct zone * zone)492 uint32_t zone_get_current_serial(struct zone* zone)
493 {
494 if(!zone || !zone->soa_rrset)
495 return 0;
496 if(zone->soa_rrset->rr_count == 0)
497 return 0;
498 if(zone->soa_rrset->rrs[0].rdata_count < 3)
499 return 0;
500 if(zone->soa_rrset->rrs[0].rdatas[2].data[0] < 4)
501 return 0;
502 return read_uint32(&zone->soa_rrset->rrs[0].rdatas[2].data[1]);
503 }
504
505 /* iterator over ixfr data. find first element, eg. oldest zone version
506 * change.
507 * The iterator can be started with the ixfr_data_first, but also with
508 * ixfr_data_last, or with an existing ixfr_data element to start from.
509 * Continue by using ixfr_data_next or ixfr_data_prev to ask for more elements
510 * until that returns NULL. NULL because end of list or loop was detected.
511 * The ixfr_data_prev uses a counter, start it at 0, it returns NULL when
512 * a loop is detected.
513 */
ixfr_data_first(struct zone_ixfr * ixfr)514 static struct ixfr_data* ixfr_data_first(struct zone_ixfr* ixfr)
515 {
516 struct ixfr_data* n;
517 if(!ixfr || !ixfr->data || ixfr->data->count==0)
518 return NULL;
519 n = (struct ixfr_data*)rbtree_search(ixfr->data, &ixfr->oldest_serial);
520 if(!n || n == (struct ixfr_data*)RBTREE_NULL)
521 return NULL;
522 return n;
523 }
524
525 /* iterator over ixfr data. find last element, eg. newest zone version
526 * change. */
ixfr_data_last(struct zone_ixfr * ixfr)527 static struct ixfr_data* ixfr_data_last(struct zone_ixfr* ixfr)
528 {
529 struct ixfr_data* n;
530 if(!ixfr || !ixfr->data || ixfr->data->count==0)
531 return NULL;
532 n = (struct ixfr_data*)rbtree_search(ixfr->data, &ixfr->newest_serial);
533 if(!n || n == (struct ixfr_data*)RBTREE_NULL)
534 return NULL;
535 return n;
536 }
537
538 /* iterator over ixfr data. fetch next item. If loop or nothing, NULL */
ixfr_data_next(struct zone_ixfr * ixfr,struct ixfr_data * cur)539 static struct ixfr_data* ixfr_data_next(struct zone_ixfr* ixfr,
540 struct ixfr_data* cur)
541 {
542 struct ixfr_data* n;
543 if(!cur || cur == (struct ixfr_data*)RBTREE_NULL)
544 return NULL;
545 if(cur->oldserial == ixfr->newest_serial)
546 return NULL; /* that was the last element */
547 n = (struct ixfr_data*)rbtree_next(&cur->node);
548 if(n && n != (struct ixfr_data*)RBTREE_NULL &&
549 cur->newserial == n->oldserial) {
550 /* the next rbtree item is the next ixfr data item */
551 return n;
552 }
553 /* If the next item is last of tree, and we have to loop around,
554 * the search performs the lookup for the next item we need.
555 * If the next item exists, but also is not connected, the search
556 * finds the correct connected ixfr in the sorted tree. */
557 /* try searching for the correct ixfr data item */
558 n = (struct ixfr_data*)rbtree_search(ixfr->data, &cur->newserial);
559 if(!n || n == (struct ixfr_data*)RBTREE_NULL)
560 return NULL;
561 return n;
562 }
563
564 /* iterator over ixfr data. fetch the previous item. If loop or nothing NULL.*/
ixfr_data_prev(struct zone_ixfr * ixfr,struct ixfr_data * cur,size_t * prevcount)565 static struct ixfr_data* ixfr_data_prev(struct zone_ixfr* ixfr,
566 struct ixfr_data* cur, size_t* prevcount)
567 {
568 struct ixfr_data* prev;
569 if(!cur || cur == (struct ixfr_data*)RBTREE_NULL)
570 return NULL;
571 if(cur->oldserial == ixfr->oldest_serial)
572 return NULL; /* this was the first element */
573 prev = (struct ixfr_data*)rbtree_previous(&cur->node);
574 if(!prev || prev == (struct ixfr_data*)RBTREE_NULL) {
575 /* We hit the first element in the tree, go again
576 * at the last one. Wrap around. */
577 prev = (struct ixfr_data*)rbtree_last(ixfr->data);
578 }
579 while(prev && prev != (struct ixfr_data*)RBTREE_NULL) {
580 if(prev->newserial == cur->oldserial) {
581 /* This is the correct matching previous ixfr data */
582 /* Increase the prevcounter every time the routine
583 * returns an item, and if that becomes too large, we
584 * are in a loop. in that case, stop. */
585 if(prevcount) {
586 (*prevcount)++;
587 if(*prevcount > ixfr->data->count + 12) {
588 /* Larger than the max number of items
589 * plus a small margin. The longest
590 * chain is all the ixfr elements in
591 * the tree. It loops. */
592 return NULL;
593 }
594 }
595 return prev;
596 }
597 prev = (struct ixfr_data*)rbtree_previous(&prev->node);
598 if(!prev || prev == (struct ixfr_data*)RBTREE_NULL) {
599 /* We hit the first element in the tree, go again
600 * at the last one. Wrap around. */
601 prev = (struct ixfr_data*)rbtree_last(ixfr->data);
602 }
603 }
604 /* no elements in list */
605 return NULL;
606 }
607
608 /* connect IXFRs, return true if connected, false if not. Return last serial */
connect_ixfrs(struct zone_ixfr * ixfr,struct ixfr_data * data,uint32_t * end_serial)609 static int connect_ixfrs(struct zone_ixfr* ixfr, struct ixfr_data* data,
610 uint32_t* end_serial)
611 {
612 struct ixfr_data* p = data;
613 while(p != NULL) {
614 struct ixfr_data* next = ixfr_data_next(ixfr, p);
615 if(next) {
616 if(p->newserial != next->oldserial) {
617 /* These ixfrs are not connected,
618 * during IXFR processing that could already
619 * have been deleted, but we check here
620 * in any case */
621 return 0;
622 }
623 } else {
624 /* the chain of IXFRs ends in this serial number */
625 *end_serial = p->newserial;
626 }
627 p = next;
628 }
629 return 1;
630 }
631
632 /* Count length of next record in data */
count_rr_length(const uint8_t * data,size_t data_len,size_t current)633 static size_t count_rr_length(const uint8_t* data, size_t data_len,
634 size_t current)
635 {
636 uint8_t label_size;
637 uint16_t rdlen;
638 size_t i = current;
639 if(current >= data_len)
640 return 0;
641 /* pass the owner dname */
642 while(1) {
643 if(i+1 > data_len)
644 return 0;
645 label_size = data[i++];
646 if(label_size == 0) {
647 break;
648 } else if((label_size &0xc0) != 0) {
649 return 0; /* uncompressed dnames in IXFR store */
650 } else if(i+label_size > data_len) {
651 return 0;
652 } else {
653 i += label_size;
654 }
655 }
656 /* after dname, we pass type, class, ttl, rdatalen */
657 if(i+10 > data_len)
658 return 0;
659 i += 8;
660 rdlen = read_uint16(data+i);
661 i += 2;
662 /* pass over the rdata */
663 if(i+((size_t)rdlen) > data_len)
664 return 0;
665 i += ((size_t)rdlen);
666 return i-current;
667 }
668
669 /* Copy RRs into packet until packet full, return number RRs added */
ixfr_copy_rrs_into_packet(struct query * query,struct pktcompression * pcomp)670 static uint16_t ixfr_copy_rrs_into_packet(struct query* query,
671 struct pktcompression* pcomp)
672 {
673 uint16_t total_added = 0;
674
675 /* Copy RRs into the packet until the answer is full,
676 * when an RR does not fit, we return and add no more. */
677
678 /* Add first SOA */
679 if(query->ixfr_count_newsoa < query->ixfr_end_data->newsoa_len) {
680 /* the new SOA is added from the end_data segment, it is
681 * the final SOA of the result of the IXFR */
682 if(ixfr_write_rr_pkt(query, query->packet, pcomp,
683 query->ixfr_end_data->newsoa,
684 query->ixfr_end_data->newsoa_len, total_added)) {
685 query->ixfr_count_newsoa = query->ixfr_end_data->newsoa_len;
686 total_added++;
687 query->ixfr_pos_of_newsoa = buffer_position(query->packet);
688 } else {
689 /* cannot add another RR, so return */
690 return total_added;
691 }
692 }
693
694 /* Add second SOA */
695 if(query->ixfr_count_oldsoa < query->ixfr_data->oldsoa_len) {
696 if(ixfr_write_rr_pkt(query, query->packet, pcomp,
697 query->ixfr_data->oldsoa,
698 query->ixfr_data->oldsoa_len, total_added)) {
699 query->ixfr_count_oldsoa = query->ixfr_data->oldsoa_len;
700 total_added++;
701 } else {
702 /* cannot add another RR, so return */
703 return total_added;
704 }
705 }
706
707 /* Add del data, with deleted RRs and a SOA */
708 while(query->ixfr_count_del < query->ixfr_data->del_len) {
709 size_t rrlen = count_rr_length(query->ixfr_data->del,
710 query->ixfr_data->del_len, query->ixfr_count_del);
711 if(rrlen && ixfr_write_rr_pkt(query, query->packet, pcomp,
712 query->ixfr_data->del + query->ixfr_count_del,
713 rrlen, total_added)) {
714 query->ixfr_count_del += rrlen;
715 total_added++;
716 } else {
717 /* the next record does not fit in the remaining
718 * space of the packet */
719 return total_added;
720 }
721 }
722
723 /* Add add data, with added RRs and a SOA */
724 while(query->ixfr_count_add < query->ixfr_data->add_len) {
725 size_t rrlen = count_rr_length(query->ixfr_data->add,
726 query->ixfr_data->add_len, query->ixfr_count_add);
727 if(rrlen && ixfr_write_rr_pkt(query, query->packet, pcomp,
728 query->ixfr_data->add + query->ixfr_count_add,
729 rrlen, total_added)) {
730 query->ixfr_count_add += rrlen;
731 total_added++;
732 } else {
733 /* the next record does not fit in the remaining
734 * space of the packet */
735 return total_added;
736 }
737 }
738 return total_added;
739 }
740
query_ixfr(struct nsd * nsd,struct query * query)741 query_state_type query_ixfr(struct nsd *nsd, struct query *query)
742 {
743 uint16_t total_added = 0;
744 struct pktcompression pcomp;
745
746 if (query->ixfr_is_done)
747 return QUERY_PROCESSED;
748
749 pktcompression_init(&pcomp);
750 if (query->maxlen > IXFR_MAX_MESSAGE_LEN)
751 query->maxlen = IXFR_MAX_MESSAGE_LEN;
752
753 assert(!query_overflow(query));
754 /* only keep running values for most packets */
755 query->tsig_prepare_it = 0;
756 query->tsig_update_it = 1;
757 if(query->tsig_sign_it) {
758 /* prepare for next updates */
759 query->tsig_prepare_it = 1;
760 query->tsig_sign_it = 0;
761 }
762
763 if (query->ixfr_data == NULL) {
764 /* This is the first packet, process the query further */
765 uint32_t qserial = 0, current_serial = 0, end_serial = 0;
766 struct zone* zone;
767 struct ixfr_data* ixfr_data;
768 size_t oldpos;
769
770 STATUP(nsd, rixfr);
771 /* parse the serial number from the IXFR request */
772 oldpos = QHEADERSZ;
773 if(!parse_qserial(query->packet, &qserial, &oldpos)) {
774 NSCOUNT_SET(query->packet, 0);
775 ARCOUNT_SET(query->packet, 0);
776 buffer_set_position(query->packet, oldpos);
777 RCODE_SET(query->packet, RCODE_FORMAT);
778 return QUERY_PROCESSED;
779 }
780 NSCOUNT_SET(query->packet, 0);
781 ARCOUNT_SET(query->packet, 0);
782 buffer_set_position(query->packet, oldpos);
783 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "ixfr query routine, %s IXFR=%u",
784 dname_to_string(query->qname, NULL), (unsigned)qserial));
785
786 /* do we have an IXFR with this serial number? If not, serve AXFR */
787 zone = namedb_find_zone(nsd->db, query->qname);
788 if(!zone) {
789 /* no zone is present */
790 RCODE_SET(query->packet, RCODE_NOTAUTH);
791 return QUERY_PROCESSED;
792 }
793 ZTATUP(nsd, zone, rixfr);
794
795 /* if the query is for same or newer serial than our current
796 * serial, then serve a single SOA with our current serial */
797 current_serial = zone_get_current_serial(zone);
798 if(compare_serial(qserial, current_serial) >= 0) {
799 if(!zone->soa_rrset || zone->soa_rrset->rr_count != 1){
800 RCODE_SET(query->packet, RCODE_SERVFAIL);
801 return QUERY_PROCESSED;
802 }
803 query_add_compression_domain(query, zone->apex,
804 QHEADERSZ);
805 if(packet_encode_rr(query, zone->apex,
806 &zone->soa_rrset->rrs[0],
807 zone->soa_rrset->rrs[0].ttl)) {
808 ANCOUNT_SET(query->packet, 1);
809 } else {
810 RCODE_SET(query->packet, RCODE_SERVFAIL);
811 }
812 AA_SET(query->packet);
813 query_clear_compression_tables(query);
814 if(query->tsig.status == TSIG_OK)
815 query->tsig_sign_it = 1;
816 return QUERY_PROCESSED;
817 }
818
819 if(!zone->ixfr) {
820 /* we have no ixfr information for the zone, make an AXFR */
821 if(query->tsig_prepare_it)
822 query->tsig_sign_it = 1;
823 VERBOSITY(2, (LOG_INFO, "ixfr fallback to axfr, no ixfr info for zone: %s",
824 dname_to_string(query->qname, NULL)));
825 return query_axfr(nsd, query, 0);
826 }
827 ixfr_data = zone_ixfr_find_serial(zone->ixfr, qserial);
828 if(!ixfr_data) {
829 /* the specific version is not available, make an AXFR */
830 if(query->tsig_prepare_it)
831 query->tsig_sign_it = 1;
832 VERBOSITY(2, (LOG_INFO, "ixfr fallback to axfr, no history for serial for zone: %s",
833 dname_to_string(query->qname, NULL)));
834 return query_axfr(nsd, query, 0);
835 }
836 /* see if the IXFRs connect to the next IXFR, and if it ends
837 * at the current served zone, if not, AXFR */
838 if(!connect_ixfrs(zone->ixfr, ixfr_data, &end_serial) ||
839 end_serial != current_serial) {
840 if(query->tsig_prepare_it)
841 query->tsig_sign_it = 1;
842 VERBOSITY(2, (LOG_INFO, "ixfr fallback to axfr, incomplete history from this serial for zone: %s",
843 dname_to_string(query->qname, NULL)));
844 return query_axfr(nsd, query, 0);
845 }
846
847 query->zone = zone;
848 query->ixfr_data = ixfr_data;
849 query->ixfr_is_done = 0;
850 /* set up to copy the last version's SOA as first SOA */
851 query->ixfr_end_data = ixfr_data_last(zone->ixfr);
852 query->ixfr_count_newsoa = 0;
853 query->ixfr_count_oldsoa = 0;
854 query->ixfr_count_del = 0;
855 query->ixfr_count_add = 0;
856 query->ixfr_pos_of_newsoa = 0;
857 /* the query name can be compressed to */
858 pktcompression_insert_with_labels(&pcomp,
859 buffer_at(query->packet, QHEADERSZ),
860 query->qname->name_size, QHEADERSZ);
861 if(query->tsig.status == TSIG_OK) {
862 query->tsig_sign_it = 1; /* sign first packet in stream */
863 }
864 } else {
865 /*
866 * Query name need not be repeated after the
867 * first response packet.
868 */
869 buffer_set_limit(query->packet, QHEADERSZ);
870 QDCOUNT_SET(query->packet, 0);
871 query_prepare_response(query);
872 }
873
874 total_added = ixfr_copy_rrs_into_packet(query, &pcomp);
875
876 while(query->ixfr_count_add >= query->ixfr_data->add_len) {
877 struct ixfr_data* next = ixfr_data_next(query->zone->ixfr,
878 query->ixfr_data);
879 /* finished the ixfr_data */
880 if(next) {
881 /* move to the next IXFR */
882 query->ixfr_data = next;
883 /* we need to skip the SOA records, set len to done*/
884 /* the newsoa count is already done, at end_data len */
885 query->ixfr_count_oldsoa = next->oldsoa_len;
886 /* and then set up to copy the del and add sections */
887 query->ixfr_count_del = 0;
888 query->ixfr_count_add = 0;
889 total_added += ixfr_copy_rrs_into_packet(query, &pcomp);
890 } else {
891 /* we finished the IXFR */
892 /* sign the last packet */
893 query->tsig_sign_it = 1;
894 query->ixfr_is_done = 1;
895 break;
896 }
897 }
898
899 /* return the answer */
900 AA_SET(query->packet);
901 ANCOUNT_SET(query->packet, total_added);
902 NSCOUNT_SET(query->packet, 0);
903 ARCOUNT_SET(query->packet, 0);
904
905 if(!query->tcp && !query->ixfr_is_done) {
906 TC_SET(query->packet);
907 if(query->ixfr_pos_of_newsoa) {
908 /* if we recorded the newsoa in the result, snip off
909 * the rest of the response, the RFC1995 response for
910 * when it does not fit is only the latest SOA */
911 buffer_set_position(query->packet, query->ixfr_pos_of_newsoa);
912 ANCOUNT_SET(query->packet, 1);
913 }
914 query->ixfr_is_done = 1;
915 }
916
917 /* check if it needs tsig signatures */
918 if(query->tsig.status == TSIG_OK) {
919 #if IXFR_TSIG_SIGN_EVERY_NTH > 0
920 if(query->tsig.updates_since_last_prepare >= IXFR_TSIG_SIGN_EVERY_NTH) {
921 #endif
922 query->tsig_sign_it = 1;
923 #if IXFR_TSIG_SIGN_EVERY_NTH > 0
924 }
925 #endif
926 }
927 pktcompression_freeup(&pcomp);
928 return QUERY_IN_IXFR;
929 }
930
931 /* free ixfr_data structure */
ixfr_data_free(struct ixfr_data * data)932 static void ixfr_data_free(struct ixfr_data* data)
933 {
934 if(!data)
935 return;
936 free(data->newsoa);
937 free(data->oldsoa);
938 free(data->del);
939 free(data->add);
940 free(data->log_str);
941 free(data);
942 }
943
ixfr_data_size(struct ixfr_data * data)944 size_t ixfr_data_size(struct ixfr_data* data)
945 {
946 return sizeof(struct ixfr_data) + data->newsoa_len + data->oldsoa_len
947 + data->del_len + data->add_len;
948 }
949
ixfr_store_start(struct zone * zone,struct ixfr_store * ixfr_store_mem)950 struct ixfr_store* ixfr_store_start(struct zone* zone,
951 struct ixfr_store* ixfr_store_mem)
952 {
953 struct ixfr_store* ixfr_store = ixfr_store_mem;
954 memset(ixfr_store, 0, sizeof(*ixfr_store));
955 ixfr_store->zone = zone;
956 ixfr_store->data = xalloc_zero(sizeof(*ixfr_store->data));
957 return ixfr_store;
958 }
959
ixfr_store_cancel(struct ixfr_store * ixfr_store)960 void ixfr_store_cancel(struct ixfr_store* ixfr_store)
961 {
962 ixfr_store->cancelled = 1;
963 ixfr_data_free(ixfr_store->data);
964 ixfr_store->data = NULL;
965 }
966
ixfr_store_free(struct ixfr_store * ixfr_store)967 void ixfr_store_free(struct ixfr_store* ixfr_store)
968 {
969 if(!ixfr_store)
970 return;
971 ixfr_data_free(ixfr_store->data);
972 }
973
974 /* make space in record data for the new size, grows the allocation */
ixfr_rrs_make_space(uint8_t ** rrs,size_t * len,size_t * capacity,size_t added)975 static void ixfr_rrs_make_space(uint8_t** rrs, size_t* len, size_t* capacity,
976 size_t added)
977 {
978 size_t newsize = 0;
979 if(*rrs == NULL) {
980 newsize = IXFR_STORE_INITIAL_SIZE;
981 } else {
982 if(*len + added <= *capacity)
983 return; /* already enough space */
984 newsize = (*capacity)*2;
985 }
986 if(*len + added > newsize)
987 newsize = *len + added;
988 if(*rrs == NULL) {
989 *rrs = xalloc(newsize);
990 } else {
991 *rrs = xrealloc(*rrs, newsize);
992 }
993 *capacity = newsize;
994 }
995
996 /* put new SOA record after delrrs and addrrs */
ixfr_put_newsoa(struct ixfr_store * ixfr_store,uint8_t ** rrs,size_t * len,size_t * capacity)997 static void ixfr_put_newsoa(struct ixfr_store* ixfr_store, uint8_t** rrs,
998 size_t* len, size_t* capacity)
999 {
1000 uint8_t* soa;
1001 size_t soa_len;
1002 if(!ixfr_store->data)
1003 return; /* data should be nonNULL, we are not cancelled */
1004 soa = ixfr_store->data->newsoa;
1005 soa_len= ixfr_store->data->newsoa_len;
1006 ixfr_rrs_make_space(rrs, len, capacity, soa_len);
1007 if(!*rrs || *len + soa_len > *capacity) {
1008 log_msg(LOG_ERR, "ixfr_store addrr: cannot allocate space");
1009 ixfr_store_cancel(ixfr_store);
1010 return;
1011 }
1012 memmove(*rrs + *len, soa, soa_len);
1013 *len += soa_len;
1014 }
1015
1016 /* trim unused storage from the rrs data */
ixfr_trim_capacity(uint8_t ** rrs,size_t * len,size_t * capacity)1017 static void ixfr_trim_capacity(uint8_t** rrs, size_t* len, size_t* capacity)
1018 {
1019 if(*rrs == NULL)
1020 return;
1021 if(*capacity == *len)
1022 return;
1023 *rrs = xrealloc(*rrs, *len);
1024 *capacity = *len;
1025 }
1026
ixfr_store_finish_data(struct ixfr_store * ixfr_store)1027 void ixfr_store_finish_data(struct ixfr_store* ixfr_store)
1028 {
1029 if(ixfr_store->data_trimmed)
1030 return;
1031 ixfr_store->data_trimmed = 1;
1032
1033 /* put new serial SOA record after delrrs and addrrs */
1034 ixfr_put_newsoa(ixfr_store, &ixfr_store->data->del,
1035 &ixfr_store->data->del_len, &ixfr_store->del_capacity);
1036 ixfr_put_newsoa(ixfr_store, &ixfr_store->data->add,
1037 &ixfr_store->data->add_len, &ixfr_store->add_capacity);
1038
1039 /* trim the data in the store, the overhead from capacity is
1040 * removed */
1041 if(!ixfr_store->data)
1042 return; /* data should be nonNULL, we are not cancelled */
1043 ixfr_trim_capacity(&ixfr_store->data->del,
1044 &ixfr_store->data->del_len, &ixfr_store->del_capacity);
1045 ixfr_trim_capacity(&ixfr_store->data->add,
1046 &ixfr_store->data->add_len, &ixfr_store->add_capacity);
1047 }
1048
ixfr_store_finish(struct ixfr_store * ixfr_store,struct nsd * nsd,char * log_buf)1049 void ixfr_store_finish(struct ixfr_store* ixfr_store, struct nsd* nsd,
1050 char* log_buf)
1051 {
1052 if(ixfr_store->cancelled) {
1053 ixfr_store_free(ixfr_store);
1054 return;
1055 }
1056
1057 ixfr_store_finish_data(ixfr_store);
1058
1059 if(ixfr_store->cancelled) {
1060 ixfr_store_free(ixfr_store);
1061 return;
1062 }
1063
1064 if(log_buf && !ixfr_store->data->log_str)
1065 ixfr_store->data->log_str = strdup(log_buf);
1066
1067 /* store the data in the zone */
1068 if(!ixfr_store->zone->ixfr)
1069 ixfr_store->zone->ixfr = zone_ixfr_create(nsd);
1070 zone_ixfr_make_space(ixfr_store->zone->ixfr, ixfr_store->zone,
1071 ixfr_store->data, ixfr_store);
1072 if(ixfr_store->cancelled) {
1073 ixfr_store_free(ixfr_store);
1074 return;
1075 }
1076 zone_ixfr_add(ixfr_store->zone->ixfr, ixfr_store->data, 1);
1077 ixfr_store->data = NULL;
1078
1079 /* free structure */
1080 ixfr_store_free(ixfr_store);
1081 }
1082
1083 /* read SOA rdata section for SOA storage */
read_soa_rdata(struct buffer * packet,uint8_t * primns,int * primns_len,uint8_t * email,int * email_len,uint32_t * serial,uint32_t * refresh,uint32_t * retry,uint32_t * expire,uint32_t * minimum,size_t * sz)1084 static int read_soa_rdata(struct buffer* packet, uint8_t* primns,
1085 int* primns_len, uint8_t* email, int* email_len,
1086 uint32_t* serial, uint32_t* refresh, uint32_t* retry,
1087 uint32_t* expire, uint32_t* minimum, size_t* sz)
1088 {
1089 if(!(*primns_len = dname_make_wire_from_packet(primns, packet, 1))) {
1090 log_msg(LOG_ERR, "ixfr_store: cannot parse soa nsname in packet");
1091 return 0;
1092 }
1093 *sz += *primns_len;
1094 if(!(*email_len = dname_make_wire_from_packet(email, packet, 1))) {
1095 log_msg(LOG_ERR, "ixfr_store: cannot parse soa maintname in packet");
1096 return 0;
1097 }
1098 *sz += *email_len;
1099 *serial = buffer_read_u32(packet);
1100 *sz += 4;
1101 *refresh = buffer_read_u32(packet);
1102 *sz += 4;
1103 *retry = buffer_read_u32(packet);
1104 *sz += 4;
1105 *expire = buffer_read_u32(packet);
1106 *sz += 4;
1107 *minimum = buffer_read_u32(packet);
1108 *sz += 4;
1109 return 1;
1110 }
1111
1112 /* store SOA record data in memory buffer */
store_soa(uint8_t * soa,struct zone * zone,uint32_t ttl,uint16_t rdlen_uncompressed,uint8_t * primns,int primns_len,uint8_t * email,int email_len,uint32_t serial,uint32_t refresh,uint32_t retry,uint32_t expire,uint32_t minimum)1113 static void store_soa(uint8_t* soa, struct zone* zone, uint32_t ttl,
1114 uint16_t rdlen_uncompressed, uint8_t* primns, int primns_len,
1115 uint8_t* email, int email_len, uint32_t serial, uint32_t refresh,
1116 uint32_t retry, uint32_t expire, uint32_t minimum)
1117 {
1118 uint8_t* sp = soa;
1119 memmove(sp, dname_name(domain_dname(zone->apex)),
1120 domain_dname(zone->apex)->name_size);
1121 sp += domain_dname(zone->apex)->name_size;
1122 write_uint16(sp, TYPE_SOA);
1123 sp += 2;
1124 write_uint16(sp, CLASS_IN);
1125 sp += 2;
1126 write_uint32(sp, ttl);
1127 sp += 4;
1128 write_uint16(sp, rdlen_uncompressed);
1129 sp += 2;
1130 memmove(sp, primns, primns_len);
1131 sp += primns_len;
1132 memmove(sp, email, email_len);
1133 sp += email_len;
1134 write_uint32(sp, serial);
1135 sp += 4;
1136 write_uint32(sp, refresh);
1137 sp += 4;
1138 write_uint32(sp, retry);
1139 sp += 4;
1140 write_uint32(sp, expire);
1141 sp += 4;
1142 write_uint32(sp, minimum);
1143 }
1144
ixfr_store_add_newsoa(struct ixfr_store * ixfr_store,uint32_t ttl,struct buffer * packet,size_t rrlen)1145 void ixfr_store_add_newsoa(struct ixfr_store* ixfr_store, uint32_t ttl,
1146 struct buffer* packet, size_t rrlen)
1147 {
1148 size_t oldpos, sz = 0;
1149 uint32_t serial, refresh, retry, expire, minimum;
1150 uint16_t rdlen_uncompressed;
1151 int primns_len = 0, email_len = 0;
1152 uint8_t primns[MAXDOMAINLEN + 1], email[MAXDOMAINLEN + 1];
1153
1154 if(ixfr_store->cancelled)
1155 return;
1156 if(ixfr_store->data->newsoa) {
1157 free(ixfr_store->data->newsoa);
1158 ixfr_store->data->newsoa = NULL;
1159 ixfr_store->data->newsoa_len = 0;
1160 }
1161 oldpos = buffer_position(packet);
1162
1163 /* calculate the length */
1164 sz = domain_dname(ixfr_store->zone->apex)->name_size;
1165 sz += 2 /* type */ + 2 /* class */ + 4 /* ttl */ + 2 /* rdlen */;
1166 if(!buffer_available(packet, rrlen)) {
1167 /* not possible already parsed, but fail nicely anyway */
1168 log_msg(LOG_ERR, "ixfr_store: not enough rdata space in packet");
1169 ixfr_store_cancel(ixfr_store);
1170 buffer_set_position(packet, oldpos);
1171 return;
1172 }
1173 if(!read_soa_rdata(packet, primns, &primns_len, email, &email_len,
1174 &serial, &refresh, &retry, &expire, &minimum, &sz)) {
1175 log_msg(LOG_ERR, "ixfr_store newsoa: cannot parse packet");
1176 ixfr_store_cancel(ixfr_store);
1177 buffer_set_position(packet, oldpos);
1178 return;
1179 }
1180 rdlen_uncompressed = primns_len + email_len + 4 + 4 + 4 + 4 + 4;
1181
1182 ixfr_store->data->newserial = serial;
1183
1184 /* store the soa record */
1185 ixfr_store->data->newsoa = xalloc(sz);
1186 ixfr_store->data->newsoa_len = sz;
1187 store_soa(ixfr_store->data->newsoa, ixfr_store->zone, ttl,
1188 rdlen_uncompressed, primns, primns_len, email, email_len,
1189 serial, refresh, retry, expire, minimum);
1190
1191 buffer_set_position(packet, oldpos);
1192 }
1193
ixfr_store_add_oldsoa(struct ixfr_store * ixfr_store,uint32_t ttl,struct buffer * packet,size_t rrlen)1194 void ixfr_store_add_oldsoa(struct ixfr_store* ixfr_store, uint32_t ttl,
1195 struct buffer* packet, size_t rrlen)
1196 {
1197 size_t oldpos, sz = 0;
1198 uint32_t serial, refresh, retry, expire, minimum;
1199 uint16_t rdlen_uncompressed;
1200 int primns_len = 0, email_len = 0;
1201 uint8_t primns[MAXDOMAINLEN + 1], email[MAXDOMAINLEN + 1];
1202
1203 if(ixfr_store->cancelled)
1204 return;
1205 if(ixfr_store->data->oldsoa) {
1206 free(ixfr_store->data->oldsoa);
1207 ixfr_store->data->oldsoa = NULL;
1208 ixfr_store->data->oldsoa_len = 0;
1209 }
1210 /* we have the old SOA and thus we are sure it is an IXFR, make space*/
1211 zone_ixfr_make_space(ixfr_store->zone->ixfr, ixfr_store->zone,
1212 ixfr_store->data, ixfr_store);
1213 if(ixfr_store->cancelled)
1214 return;
1215 oldpos = buffer_position(packet);
1216
1217 /* calculate the length */
1218 sz = domain_dname(ixfr_store->zone->apex)->name_size;
1219 sz += 2 /*type*/ + 2 /*class*/ + 4 /*ttl*/ + 2 /*rdlen*/;
1220 if(!buffer_available(packet, rrlen)) {
1221 /* not possible already parsed, but fail nicely anyway */
1222 log_msg(LOG_ERR, "ixfr_store oldsoa: not enough rdata space in packet");
1223 ixfr_store_cancel(ixfr_store);
1224 buffer_set_position(packet, oldpos);
1225 return;
1226 }
1227 if(!read_soa_rdata(packet, primns, &primns_len, email, &email_len,
1228 &serial, &refresh, &retry, &expire, &minimum, &sz)) {
1229 log_msg(LOG_ERR, "ixfr_store oldsoa: cannot parse packet");
1230 ixfr_store_cancel(ixfr_store);
1231 buffer_set_position(packet, oldpos);
1232 return;
1233 }
1234 rdlen_uncompressed = primns_len + email_len + 4 + 4 + 4 + 4 + 4;
1235
1236 ixfr_store->data->oldserial = serial;
1237
1238 /* store the soa record */
1239 ixfr_store->data->oldsoa = xalloc(sz);
1240 ixfr_store->data->oldsoa_len = sz;
1241 store_soa(ixfr_store->data->oldsoa, ixfr_store->zone, ttl,
1242 rdlen_uncompressed, primns, primns_len, email, email_len,
1243 serial, refresh, retry, expire, minimum);
1244
1245 buffer_set_position(packet, oldpos);
1246 }
1247
1248 /* store RR in data segment */
ixfr_putrr(const struct dname * dname,uint16_t type,uint16_t klass,uint32_t ttl,rdata_atom_type * rdatas,ssize_t rdata_num,uint8_t ** rrs,size_t * rrs_len,size_t * rrs_capacity)1249 static int ixfr_putrr(const struct dname* dname, uint16_t type, uint16_t klass,
1250 uint32_t ttl, rdata_atom_type* rdatas, ssize_t rdata_num,
1251 uint8_t** rrs, size_t* rrs_len, size_t* rrs_capacity)
1252 {
1253 size_t rdlen_uncompressed, sz;
1254 uint8_t* sp;
1255 int i;
1256
1257 /* find rdatalen */
1258 rdlen_uncompressed = 0;
1259 for(i=0; i<rdata_num; i++) {
1260 if(rdata_atom_is_domain(type, i)) {
1261 rdlen_uncompressed += domain_dname(rdatas[i].domain)
1262 ->name_size;
1263 } else {
1264 rdlen_uncompressed += rdatas[i].data[0];
1265 }
1266 }
1267 sz = dname->name_size + 2 /*type*/ + 2 /*class*/ + 4 /*ttl*/ +
1268 2 /*rdlen*/ + rdlen_uncompressed;
1269
1270 /* store RR in IXFR data */
1271 ixfr_rrs_make_space(rrs, rrs_len, rrs_capacity, sz);
1272 if(!*rrs || *rrs_len + sz > *rrs_capacity) {
1273 return 0;
1274 }
1275 /* copy data into add */
1276 sp = *rrs + *rrs_len;
1277 *rrs_len += sz;
1278 memmove(sp, dname_name(dname), dname->name_size);
1279 sp += dname->name_size;
1280 write_uint16(sp, type);
1281 sp += 2;
1282 write_uint16(sp, klass);
1283 sp += 2;
1284 write_uint32(sp, ttl);
1285 sp += 4;
1286 write_uint16(sp, rdlen_uncompressed);
1287 sp += 2;
1288 for(i=0; i<rdata_num; i++) {
1289 if(rdata_atom_is_domain(type, i)) {
1290 memmove(sp, dname_name(domain_dname(rdatas[i].domain)),
1291 domain_dname(rdatas[i].domain)->name_size);
1292 sp += domain_dname(rdatas[i].domain)->name_size;
1293 } else {
1294 memmove(sp, &rdatas[i].data[1], rdatas[i].data[0]);
1295 sp += rdatas[i].data[0];
1296 }
1297 }
1298 return 1;
1299 }
1300
ixfr_store_putrr(struct ixfr_store * ixfr_store,const struct dname * dname,uint16_t type,uint16_t klass,uint32_t ttl,struct buffer * packet,uint16_t rrlen,struct region * temp_region,uint8_t ** rrs,size_t * rrs_len,size_t * rrs_capacity)1301 void ixfr_store_putrr(struct ixfr_store* ixfr_store, const struct dname* dname,
1302 uint16_t type, uint16_t klass, uint32_t ttl, struct buffer* packet,
1303 uint16_t rrlen, struct region* temp_region, uint8_t** rrs,
1304 size_t* rrs_len, size_t* rrs_capacity)
1305 {
1306 domain_table_type *temptable;
1307 rdata_atom_type *rdatas;
1308 ssize_t rdata_num;
1309 size_t oldpos;
1310
1311 if(ixfr_store->cancelled)
1312 return;
1313
1314 /* The SOA data is stored with separate calls. And then appended
1315 * during the finish operation. We do not have to store it here
1316 * when called from difffile's IXFR processing with type SOA. */
1317 if(type == TYPE_SOA)
1318 return;
1319 /* make space for these RRs we have now; basically once we
1320 * grow beyond the current allowed amount an older IXFR is deleted. */
1321 zone_ixfr_make_space(ixfr_store->zone->ixfr, ixfr_store->zone,
1322 ixfr_store->data, ixfr_store);
1323 if(ixfr_store->cancelled)
1324 return;
1325
1326 /* parse rdata */
1327 oldpos = buffer_position(packet);
1328 temptable = domain_table_create(temp_region);
1329 rdata_num = rdata_wireformat_to_rdata_atoms(temp_region, temptable,
1330 type, rrlen, packet, &rdatas);
1331 buffer_set_position(packet, oldpos);
1332 if(rdata_num == -1) {
1333 log_msg(LOG_ERR, "ixfr_store addrr: cannot parse packet");
1334 ixfr_store_cancel(ixfr_store);
1335 return;
1336 }
1337
1338 if(!ixfr_putrr(dname, type, klass, ttl, rdatas, rdata_num,
1339 rrs, rrs_len, rrs_capacity)) {
1340 log_msg(LOG_ERR, "ixfr_store addrr: cannot allocate space");
1341 ixfr_store_cancel(ixfr_store);
1342 return;
1343 }
1344 }
1345
ixfr_store_delrr(struct ixfr_store * ixfr_store,const struct dname * dname,uint16_t type,uint16_t klass,uint32_t ttl,struct buffer * packet,uint16_t rrlen,struct region * temp_region)1346 void ixfr_store_delrr(struct ixfr_store* ixfr_store, const struct dname* dname,
1347 uint16_t type, uint16_t klass, uint32_t ttl, struct buffer* packet,
1348 uint16_t rrlen, struct region* temp_region)
1349 {
1350 ixfr_store_putrr(ixfr_store, dname, type, klass, ttl, packet, rrlen,
1351 temp_region, &ixfr_store->data->del,
1352 &ixfr_store->data->del_len, &ixfr_store->del_capacity);
1353 }
1354
ixfr_store_addrr(struct ixfr_store * ixfr_store,const struct dname * dname,uint16_t type,uint16_t klass,uint32_t ttl,struct buffer * packet,uint16_t rrlen,struct region * temp_region)1355 void ixfr_store_addrr(struct ixfr_store* ixfr_store, const struct dname* dname,
1356 uint16_t type, uint16_t klass, uint32_t ttl, struct buffer* packet,
1357 uint16_t rrlen, struct region* temp_region)
1358 {
1359 ixfr_store_putrr(ixfr_store, dname, type, klass, ttl, packet, rrlen,
1360 temp_region, &ixfr_store->data->add,
1361 &ixfr_store->data->add_len, &ixfr_store->add_capacity);
1362 }
1363
ixfr_store_addrr_rdatas(struct ixfr_store * ixfr_store,const struct dname * dname,uint16_t type,uint16_t klass,uint32_t ttl,rdata_atom_type * rdatas,ssize_t rdata_num)1364 int ixfr_store_addrr_rdatas(struct ixfr_store* ixfr_store,
1365 const struct dname* dname, uint16_t type, uint16_t klass,
1366 uint32_t ttl, rdata_atom_type* rdatas, ssize_t rdata_num)
1367 {
1368 if(ixfr_store->cancelled)
1369 return 1;
1370 if(type == TYPE_SOA)
1371 return 1;
1372 return ixfr_putrr(dname, type, klass, ttl, rdatas, rdata_num,
1373 &ixfr_store->data->add, &ixfr_store->data->add_len,
1374 &ixfr_store->add_capacity);
1375 }
1376
ixfr_store_add_newsoa_rdatas(struct ixfr_store * ixfr_store,const struct dname * dname,uint16_t type,uint16_t klass,uint32_t ttl,rdata_atom_type * rdatas,ssize_t rdata_num)1377 int ixfr_store_add_newsoa_rdatas(struct ixfr_store* ixfr_store,
1378 const struct dname* dname, uint16_t type, uint16_t klass,
1379 uint32_t ttl, rdata_atom_type* rdatas, ssize_t rdata_num)
1380 {
1381 size_t capacity = 0;
1382 uint32_t serial;
1383 if(ixfr_store->cancelled)
1384 return 1;
1385 if(rdata_num < 2 || rdata_atom_size(rdatas[2]) < 4)
1386 return 0;
1387 memcpy(&serial, rdata_atom_data(rdatas[2]), sizeof(serial));
1388 ixfr_store->data->newserial = ntohl(serial);
1389 if(!ixfr_putrr(dname, type, klass, ttl, rdatas, rdata_num,
1390 &ixfr_store->data->newsoa, &ixfr_store->data->newsoa_len,
1391 &ixfr_store->add_capacity))
1392 return 0;
1393 ixfr_trim_capacity(&ixfr_store->data->newsoa,
1394 &ixfr_store->data->newsoa_len, &capacity);
1395 return 1;
1396 }
1397
1398 /* store rr uncompressed */
ixfr_storerr_uncompressed(uint8_t * dname,size_t dname_len,uint16_t type,uint16_t klass,uint32_t ttl,uint8_t * rdata,size_t rdata_len,uint8_t ** rrs,size_t * rrs_len,size_t * rrs_capacity)1399 int ixfr_storerr_uncompressed(uint8_t* dname, size_t dname_len, uint16_t type,
1400 uint16_t klass, uint32_t ttl, uint8_t* rdata, size_t rdata_len,
1401 uint8_t** rrs, size_t* rrs_len, size_t* rrs_capacity)
1402 {
1403 size_t sz;
1404 uint8_t* sp;
1405
1406 /* find rdatalen */
1407 sz = dname_len + 2 /*type*/ + 2 /*class*/ + 4 /*ttl*/ +
1408 2 /*rdlen*/ + rdata_len;
1409
1410 /* store RR in IXFR data */
1411 ixfr_rrs_make_space(rrs, rrs_len, rrs_capacity, sz);
1412 if(!*rrs || *rrs_len + sz > *rrs_capacity) {
1413 return 0;
1414 }
1415 /* copy data into add */
1416 sp = *rrs + *rrs_len;
1417 *rrs_len += sz;
1418 memmove(sp, dname, dname_len);
1419 sp += dname_len;
1420 write_uint16(sp, type);
1421 sp += 2;
1422 write_uint16(sp, klass);
1423 sp += 2;
1424 write_uint32(sp, ttl);
1425 sp += 4;
1426 write_uint16(sp, rdata_len);
1427 sp += 2;
1428 memmove(sp, rdata, rdata_len);
1429 return 1;
1430 }
1431
ixfr_store_delrr_uncompressed(struct ixfr_store * ixfr_store,uint8_t * dname,size_t dname_len,uint16_t type,uint16_t klass,uint32_t ttl,uint8_t * rdata,size_t rdata_len)1432 int ixfr_store_delrr_uncompressed(struct ixfr_store* ixfr_store,
1433 uint8_t* dname, size_t dname_len, uint16_t type, uint16_t klass,
1434 uint32_t ttl, uint8_t* rdata, size_t rdata_len)
1435 {
1436 if(ixfr_store->cancelled)
1437 return 1;
1438 if(type == TYPE_SOA)
1439 return 1;
1440 return ixfr_storerr_uncompressed(dname, dname_len, type, klass,
1441 ttl, rdata, rdata_len, &ixfr_store->data->del,
1442 &ixfr_store->data->del_len, &ixfr_store->del_capacity);
1443 }
1444
skip_dname(uint8_t * rdata,size_t rdata_len)1445 static size_t skip_dname(uint8_t* rdata, size_t rdata_len)
1446 {
1447 for (size_t index=0; index < rdata_len; ) {
1448 uint8_t label_size = rdata[index];
1449 if (label_size == 0) {
1450 return index + 1;
1451 } else if ((label_size & 0xc0) != 0) {
1452 return (index + 1 < rdata_len) ? index + 2 : 0;
1453 } else {
1454 /* loop breaks if index exceeds rdata_len */
1455 index += label_size + 1;
1456 }
1457 }
1458
1459 return 0;
1460 }
1461
ixfr_store_oldsoa_uncompressed(struct ixfr_store * ixfr_store,uint8_t * dname,size_t dname_len,uint16_t type,uint16_t klass,uint32_t ttl,uint8_t * rdata,size_t rdata_len)1462 int ixfr_store_oldsoa_uncompressed(struct ixfr_store* ixfr_store,
1463 uint8_t* dname, size_t dname_len, uint16_t type, uint16_t klass,
1464 uint32_t ttl, uint8_t* rdata, size_t rdata_len)
1465 {
1466 size_t capacity = 0;
1467 if(ixfr_store->cancelled)
1468 return 1;
1469 if(!ixfr_storerr_uncompressed(dname, dname_len, type, klass,
1470 ttl, rdata, rdata_len, &ixfr_store->data->oldsoa,
1471 &ixfr_store->data->oldsoa_len, &capacity))
1472 return 0;
1473 {
1474 uint32_t serial;
1475 size_t index, count = 0;
1476 if (!(count = skip_dname(rdata, rdata_len)))
1477 return 0;
1478 index = count;
1479 if (!(count = skip_dname(rdata+index, rdata_len-index)))
1480 return 0;
1481 index += count;
1482 if (rdata_len - index < 4)
1483 return 0;
1484 memcpy(&serial, rdata+index, sizeof(serial));
1485 ixfr_store->data->oldserial = ntohl(serial);
1486 }
1487 ixfr_trim_capacity(&ixfr_store->data->oldsoa,
1488 &ixfr_store->data->oldsoa_len, &capacity);
1489 return 1;
1490 }
1491
zone_is_ixfr_enabled(struct zone * zone)1492 int zone_is_ixfr_enabled(struct zone* zone)
1493 {
1494 return zone->opts->pattern->store_ixfr;
1495 }
1496
1497 /* compare ixfr elements */
ixfrcompare(const void * x,const void * y)1498 static int ixfrcompare(const void* x, const void* y)
1499 {
1500 uint32_t* serial_x = (uint32_t*)x;
1501 uint32_t* serial_y = (uint32_t*)y;
1502 if(*serial_x < *serial_y)
1503 return -1;
1504 if(*serial_x > *serial_y)
1505 return 1;
1506 return 0;
1507 }
1508
zone_ixfr_create(struct nsd * nsd)1509 struct zone_ixfr* zone_ixfr_create(struct nsd* nsd)
1510 {
1511 struct zone_ixfr* ixfr = xalloc_zero(sizeof(struct zone_ixfr));
1512 ixfr->data = rbtree_create(nsd->region, &ixfrcompare);
1513 return ixfr;
1514 }
1515
1516 /* traverse tree postorder */
ixfr_tree_del(struct rbnode * node)1517 static void ixfr_tree_del(struct rbnode* node)
1518 {
1519 if(node == NULL || node == RBTREE_NULL)
1520 return;
1521 ixfr_tree_del(node->left);
1522 ixfr_tree_del(node->right);
1523 ixfr_data_free((struct ixfr_data*)node);
1524 }
1525
1526 /* clear the ixfr data elements */
zone_ixfr_clear(struct zone_ixfr * ixfr)1527 static void zone_ixfr_clear(struct zone_ixfr* ixfr)
1528 {
1529 if(!ixfr)
1530 return;
1531 if(ixfr->data) {
1532 ixfr_tree_del(ixfr->data->root);
1533 ixfr->data->root = RBTREE_NULL;
1534 ixfr->data->count = 0;
1535 }
1536 ixfr->total_size = 0;
1537 ixfr->oldest_serial = 0;
1538 ixfr->newest_serial = 0;
1539 }
1540
zone_ixfr_free(struct zone_ixfr * ixfr)1541 void zone_ixfr_free(struct zone_ixfr* ixfr)
1542 {
1543 if(!ixfr)
1544 return;
1545 if(ixfr->data) {
1546 ixfr_tree_del(ixfr->data->root);
1547 ixfr->data = NULL;
1548 }
1549 free(ixfr);
1550 }
1551
ixfr_store_delixfrs(struct zone * zone)1552 void ixfr_store_delixfrs(struct zone* zone)
1553 {
1554 if(!zone)
1555 return;
1556 zone_ixfr_clear(zone->ixfr);
1557 }
1558
1559 /* remove the oldest data entry from the ixfr versions */
zone_ixfr_remove_oldest(struct zone_ixfr * ixfr)1560 static void zone_ixfr_remove_oldest(struct zone_ixfr* ixfr)
1561 {
1562 if(ixfr->data->count > 0) {
1563 struct ixfr_data* oldest = ixfr_data_first(ixfr);
1564 if(ixfr->oldest_serial == oldest->oldserial) {
1565 if(ixfr->data->count > 1) {
1566 struct ixfr_data* next = ixfr_data_next(ixfr, oldest);
1567 assert(next);
1568 if(next)
1569 ixfr->oldest_serial = next->oldserial;
1570 else ixfr->oldest_serial = oldest->newserial;
1571 } else {
1572 ixfr->oldest_serial = 0;
1573 }
1574 }
1575 if(ixfr->newest_serial == oldest->oldserial) {
1576 ixfr->newest_serial = 0;
1577 }
1578 zone_ixfr_remove(ixfr, oldest);
1579 }
1580 }
1581
zone_ixfr_make_space(struct zone_ixfr * ixfr,struct zone * zone,struct ixfr_data * data,struct ixfr_store * ixfr_store)1582 void zone_ixfr_make_space(struct zone_ixfr* ixfr, struct zone* zone,
1583 struct ixfr_data* data, struct ixfr_store* ixfr_store)
1584 {
1585 size_t addsize;
1586 if(!ixfr || !data)
1587 return;
1588 if(zone->opts->pattern->ixfr_number == 0) {
1589 ixfr_store_cancel(ixfr_store);
1590 return;
1591 }
1592
1593 /* Check the number of IXFRs allowed for this zone, if too many,
1594 * shorten the number to make space for another one */
1595 while(ixfr->data->count >= zone->opts->pattern->ixfr_number) {
1596 zone_ixfr_remove_oldest(ixfr);
1597 }
1598
1599 if(zone->opts->pattern->ixfr_size == 0) {
1600 /* no size limits imposed */
1601 return;
1602 }
1603
1604 /* Check the size of the current added data element 'data', and
1605 * see if that overflows the maximum storage size for IXFRs for
1606 * this zone, and if so, delete the oldest IXFR to make space */
1607 addsize = ixfr_data_size(data);
1608 while(ixfr->data->count > 0 && ixfr->total_size + addsize >
1609 zone->opts->pattern->ixfr_size) {
1610 zone_ixfr_remove_oldest(ixfr);
1611 }
1612
1613 /* if deleting the oldest elements does not work, then this
1614 * IXFR is too big to store and we cancel it */
1615 if(ixfr->data->count == 0 && ixfr->total_size + addsize >
1616 zone->opts->pattern->ixfr_size) {
1617 ixfr_store_cancel(ixfr_store);
1618 return;
1619 }
1620 }
1621
zone_ixfr_remove(struct zone_ixfr * ixfr,struct ixfr_data * data)1622 void zone_ixfr_remove(struct zone_ixfr* ixfr, struct ixfr_data* data)
1623 {
1624 rbtree_delete(ixfr->data, data->node.key);
1625 ixfr->total_size -= ixfr_data_size(data);
1626 ixfr_data_free(data);
1627 }
1628
zone_ixfr_add(struct zone_ixfr * ixfr,struct ixfr_data * data,int isnew)1629 void zone_ixfr_add(struct zone_ixfr* ixfr, struct ixfr_data* data, int isnew)
1630 {
1631 memset(&data->node, 0, sizeof(data->node));
1632 if(ixfr->data->count == 0) {
1633 ixfr->oldest_serial = data->oldserial;
1634 ixfr->newest_serial = data->oldserial;
1635 } else if(isnew) {
1636 /* newest entry is last there is */
1637 ixfr->newest_serial = data->oldserial;
1638 } else {
1639 /* added older entry, before the others */
1640 ixfr->oldest_serial = data->oldserial;
1641 }
1642 data->node.key = &data->oldserial;
1643 rbtree_insert(ixfr->data, &data->node);
1644 ixfr->total_size += ixfr_data_size(data);
1645 }
1646
zone_ixfr_find_serial(struct zone_ixfr * ixfr,uint32_t qserial)1647 struct ixfr_data* zone_ixfr_find_serial(struct zone_ixfr* ixfr,
1648 uint32_t qserial)
1649 {
1650 struct ixfr_data* data;
1651 if(!ixfr)
1652 return NULL;
1653 if(!ixfr->data)
1654 return NULL;
1655 data = (struct ixfr_data*)rbtree_search(ixfr->data, &qserial);
1656 if(data) {
1657 assert(data->oldserial == qserial);
1658 return data;
1659 }
1660 /* not found */
1661 return NULL;
1662 }
1663
1664 /* calculate the number of files we want */
ixfr_target_number_files(struct zone * zone)1665 static int ixfr_target_number_files(struct zone* zone)
1666 {
1667 int dest_num_files;
1668 if(!zone->ixfr || !zone->ixfr->data)
1669 return 0;
1670 if(!zone_is_ixfr_enabled(zone))
1671 return 0;
1672 /* if we store ixfr, it is the configured number of files */
1673 dest_num_files = (int)zone->opts->pattern->ixfr_number;
1674 /* but if the number of available transfers is smaller, store less */
1675 if(dest_num_files > (int)zone->ixfr->data->count)
1676 dest_num_files = (int)zone->ixfr->data->count;
1677 return dest_num_files;
1678 }
1679
1680 /* create ixfrfile name in buffer for file_num. The num is 1 .. number. */
make_ixfr_name(char * buf,size_t len,const char * zfile,int file_num)1681 static void make_ixfr_name(char* buf, size_t len, const char* zfile,
1682 int file_num)
1683 {
1684 if(file_num == 1)
1685 snprintf(buf, len, "%s.ixfr", zfile);
1686 else snprintf(buf, len, "%s.ixfr.%d", zfile, file_num);
1687 }
1688
1689 /* create temp ixfrfile name in buffer for file_num. The num is 1 .. number. */
make_ixfr_name_temp(char * buf,size_t len,const char * zfile,int file_num,int temp)1690 static void make_ixfr_name_temp(char* buf, size_t len, const char* zfile,
1691 int file_num, int temp)
1692 {
1693 if(file_num == 1)
1694 snprintf(buf, len, "%s.ixfr%s", zfile, (temp?".temp":""));
1695 else snprintf(buf, len, "%s.ixfr.%d%s", zfile, file_num,
1696 (temp?".temp":""));
1697 }
1698
1699 /* see if ixfr file exists */
ixfr_file_exists_ctmp(const char * zfile,int file_num,int temp)1700 static int ixfr_file_exists_ctmp(const char* zfile, int file_num, int temp)
1701 {
1702 struct stat statbuf;
1703 char ixfrfile[1024+24];
1704 make_ixfr_name_temp(ixfrfile, sizeof(ixfrfile), zfile, file_num, temp);
1705 memset(&statbuf, 0, sizeof(statbuf));
1706 if(stat(ixfrfile, &statbuf) < 0) {
1707 if(errno == ENOENT)
1708 return 0;
1709 /* file is not usable */
1710 return 0;
1711 }
1712 return 1;
1713 }
1714
ixfr_file_exists(const char * zfile,int file_num)1715 int ixfr_file_exists(const char* zfile, int file_num)
1716 {
1717 return ixfr_file_exists_ctmp(zfile, file_num, 0);
1718 }
1719
1720 /* see if ixfr file exists */
ixfr_file_exists_temp(const char * zfile,int file_num)1721 static int ixfr_file_exists_temp(const char* zfile, int file_num)
1722 {
1723 return ixfr_file_exists_ctmp(zfile, file_num, 1);
1724 }
1725
1726 /* unlink an ixfr file */
ixfr_unlink_it_ctmp(const char * zname,const char * zfile,int file_num,int silent_enoent,int temp)1727 static int ixfr_unlink_it_ctmp(const char* zname, const char* zfile,
1728 int file_num, int silent_enoent, int temp)
1729 {
1730 char ixfrfile[1024+24];
1731 make_ixfr_name_temp(ixfrfile, sizeof(ixfrfile), zfile, file_num, temp);
1732 VERBOSITY(3, (LOG_INFO, "delete zone %s IXFR data file %s",
1733 zname, ixfrfile));
1734 if(unlink(ixfrfile) < 0) {
1735 if(silent_enoent && errno == ENOENT)
1736 return 0;
1737 log_msg(LOG_ERR, "error to delete file %s: %s", ixfrfile,
1738 strerror(errno));
1739 return 0;
1740 }
1741 return 1;
1742 }
1743
ixfr_unlink_it(const char * zname,const char * zfile,int file_num,int silent_enoent)1744 int ixfr_unlink_it(const char* zname, const char* zfile, int file_num,
1745 int silent_enoent)
1746 {
1747 return ixfr_unlink_it_ctmp(zname, zfile, file_num, silent_enoent, 0);
1748 }
1749
1750 /* unlink an ixfr file */
ixfr_unlink_it_temp(const char * zname,const char * zfile,int file_num,int silent_enoent)1751 static int ixfr_unlink_it_temp(const char* zname, const char* zfile,
1752 int file_num, int silent_enoent)
1753 {
1754 return ixfr_unlink_it_ctmp(zname, zfile, file_num, silent_enoent, 1);
1755 }
1756
1757 /* read ixfr file header */
ixfr_read_file_header(const char * zname,const char * zfile,int file_num,uint32_t * oldserial,uint32_t * newserial,size_t * data_size,int enoent_is_err)1758 int ixfr_read_file_header(const char* zname, const char* zfile,
1759 int file_num, uint32_t* oldserial, uint32_t* newserial,
1760 size_t* data_size, int enoent_is_err)
1761 {
1762 char ixfrfile[1024+24];
1763 char buf[1024];
1764 FILE* in;
1765 int num_lines = 0, got_old = 0, got_new = 0, got_datasize = 0;
1766 make_ixfr_name(ixfrfile, sizeof(ixfrfile), zfile, file_num);
1767 in = fopen(ixfrfile, "r");
1768 if(!in) {
1769 if((errno == ENOENT && enoent_is_err) || (errno != ENOENT))
1770 log_msg(LOG_ERR, "could not open %s: %s", ixfrfile,
1771 strerror(errno));
1772 return 0;
1773 }
1774 /* read about 10 lines, this is where the header is */
1775 while(!(got_old && got_new && got_datasize) && num_lines < 10) {
1776 buf[0]=0;
1777 buf[sizeof(buf)-1]=0;
1778 if(!fgets(buf, sizeof(buf), in)) {
1779 log_msg(LOG_ERR, "could not read %s: %s", ixfrfile,
1780 strerror(errno));
1781 fclose(in);
1782 return 0;
1783 }
1784 num_lines++;
1785 if(buf[0]!=0 && buf[strlen(buf)-1]=='\n')
1786 buf[strlen(buf)-1]=0;
1787 if(strncmp(buf, "; zone ", 7) == 0) {
1788 if(strcmp(buf+7, zname) != 0) {
1789 log_msg(LOG_ERR, "file has wrong zone, expected zone %s, but found %s in file %s",
1790 zname, buf+7, ixfrfile);
1791 fclose(in);
1792 return 0;
1793 }
1794 } else if(strncmp(buf, "; from_serial ", 14) == 0) {
1795 *oldserial = atoi(buf+14);
1796 got_old = 1;
1797 } else if(strncmp(buf, "; to_serial ", 12) == 0) {
1798 *newserial = atoi(buf+12);
1799 got_new = 1;
1800 } else if(strncmp(buf, "; data_size ", 12) == 0) {
1801 *data_size = (size_t)atoi(buf+12);
1802 got_datasize = 1;
1803 }
1804 }
1805 fclose(in);
1806 if(!got_old)
1807 return 0;
1808 if(!got_new)
1809 return 0;
1810 if(!got_datasize)
1811 return 0;
1812 return 1;
1813 }
1814
1815 /* delete rest ixfr files, that are after the current item */
ixfr_delete_rest_files(struct zone * zone,struct ixfr_data * from,const char * zfile,int temp)1816 static void ixfr_delete_rest_files(struct zone* zone, struct ixfr_data* from,
1817 const char* zfile, int temp)
1818 {
1819 size_t prevcount = 0;
1820 struct ixfr_data* data = from;
1821 while(data) {
1822 if(data->file_num != 0) {
1823 (void)ixfr_unlink_it_ctmp(zone->opts->name, zfile,
1824 data->file_num, 0, temp);
1825 data->file_num = 0;
1826 }
1827 data = ixfr_data_prev(zone->ixfr, data, &prevcount);
1828 }
1829 }
1830
ixfr_delete_superfluous_files(struct zone * zone,const char * zfile,int dest_num_files)1831 void ixfr_delete_superfluous_files(struct zone* zone, const char* zfile,
1832 int dest_num_files)
1833 {
1834 int i = dest_num_files + 1;
1835 if(!ixfr_file_exists(zfile, i))
1836 return;
1837 while(ixfr_unlink_it(zone->opts->name, zfile, i, 1)) {
1838 i++;
1839 }
1840 }
1841
ixfr_rename_it(const char * zname,const char * zfile,int oldnum,int oldtemp,int newnum,int newtemp)1842 int ixfr_rename_it(const char* zname, const char* zfile, int oldnum,
1843 int oldtemp, int newnum, int newtemp)
1844 {
1845 char ixfrfile_old[1024+24];
1846 char ixfrfile_new[1024+24];
1847 make_ixfr_name_temp(ixfrfile_old, sizeof(ixfrfile_old), zfile, oldnum,
1848 oldtemp);
1849 make_ixfr_name_temp(ixfrfile_new, sizeof(ixfrfile_new), zfile, newnum,
1850 newtemp);
1851 VERBOSITY(3, (LOG_INFO, "rename zone %s IXFR data file %s to %s",
1852 zname, ixfrfile_old, ixfrfile_new));
1853 if(rename(ixfrfile_old, ixfrfile_new) < 0) {
1854 log_msg(LOG_ERR, "error to rename file %s: %s", ixfrfile_old,
1855 strerror(errno));
1856 return 0;
1857 }
1858 return 1;
1859 }
1860
1861 /* delete if we have too many items in memory */
ixfr_delete_memory_items(struct zone * zone,int dest_num_files)1862 static void ixfr_delete_memory_items(struct zone* zone, int dest_num_files)
1863 {
1864 if(!zone->ixfr || !zone->ixfr->data)
1865 return;
1866 if(dest_num_files == (int)zone->ixfr->data->count)
1867 return;
1868 if(dest_num_files > (int)zone->ixfr->data->count) {
1869 /* impossible, dest_num_files should be smaller */
1870 return;
1871 }
1872
1873 /* delete oldest ixfr, until we have dest_num_files entries */
1874 while(dest_num_files < (int)zone->ixfr->data->count) {
1875 zone_ixfr_remove_oldest(zone->ixfr);
1876 }
1877 }
1878
1879 /* rename the ixfr files that need to change name */
ixfr_rename_files(struct zone * zone,const char * zfile,int dest_num_files)1880 static int ixfr_rename_files(struct zone* zone, const char* zfile,
1881 int dest_num_files)
1882 {
1883 struct ixfr_data* data, *startspot = NULL;
1884 size_t prevcount = 0;
1885 int destnum;
1886 if(!zone->ixfr || !zone->ixfr->data)
1887 return 1;
1888
1889 /* the oldest file is at the largest number */
1890 data = ixfr_data_first(zone->ixfr);
1891 destnum = dest_num_files;
1892 if(!data)
1893 return 1; /* nothing to do */
1894 if(data->file_num == destnum)
1895 return 1; /* nothing to do for rename */
1896
1897 /* rename the files to temporary files, because otherwise the
1898 * items would overwrite each other when the list touches itself.
1899 * On fail, the temporary files are removed and we end up with
1900 * the newly written data plus the remaining files, in order.
1901 * Thus, start the temporary rename at the oldest, then rename
1902 * to the final names starting from the newest. */
1903 while(data && data->file_num != 0) {
1904 /* if existing file at temporary name, delete that */
1905 if(ixfr_file_exists_temp(zfile, data->file_num)) {
1906 (void)ixfr_unlink_it_temp(zone->opts->name, zfile,
1907 data->file_num, 0);
1908 }
1909
1910 /* rename to temporary name */
1911 if(!ixfr_rename_it(zone->opts->name, zfile, data->file_num, 0,
1912 data->file_num, 1)) {
1913 /* failure, we cannot store files */
1914 /* delete the renamed files */
1915 ixfr_delete_rest_files(zone, data, zfile, 1);
1916 return 0;
1917 }
1918
1919 /* the next cycle should start at the newest file that
1920 * has been renamed to a temporary name */
1921 startspot = data;
1922 data = ixfr_data_next(zone->ixfr, data);
1923 destnum--;
1924 }
1925
1926 /* rename the files to their final name position */
1927 data = startspot;
1928 while(data && data->file_num != 0) {
1929 destnum++;
1930
1931 /* if there is an existing file, delete it */
1932 if(ixfr_file_exists(zfile, destnum)) {
1933 (void)ixfr_unlink_it(zone->opts->name, zfile,
1934 destnum, 0);
1935 }
1936
1937 if(!ixfr_rename_it(zone->opts->name, zfile, data->file_num, 1, destnum, 0)) {
1938 /* failure, we cannot store files */
1939 ixfr_delete_rest_files(zone, data, zfile, 1);
1940 /* delete the previously renamed files, so in
1941 * memory stays as is, on disk we have the current
1942 * item (and newer transfers) okay. */
1943 return 0;
1944 }
1945 data->file_num = destnum;
1946
1947 data = ixfr_data_prev(zone->ixfr, data, &prevcount);
1948 }
1949 return 1;
1950 }
1951
1952 /* write the ixfr data file header */
ixfr_write_file_header(struct zone * zone,struct ixfr_data * data,FILE * out)1953 static int ixfr_write_file_header(struct zone* zone, struct ixfr_data* data,
1954 FILE* out)
1955 {
1956 if(!fprintf(out, "; IXFR data file\n"))
1957 return 0;
1958 if(!fprintf(out, "; zone %s\n", zone->opts->name))
1959 return 0;
1960 if(!fprintf(out, "; from_serial %u\n", (unsigned)data->oldserial))
1961 return 0;
1962 if(!fprintf(out, "; to_serial %u\n", (unsigned)data->newserial))
1963 return 0;
1964 if(!fprintf(out, "; data_size %u\n", (unsigned)ixfr_data_size(data)))
1965 return 0;
1966 if(data->log_str) {
1967 if(!fprintf(out, "; %s\n", data->log_str))
1968 return 0;
1969 }
1970 return 1;
1971 }
1972
1973 /* print rdata on one line */
1974 static int
oneline_print_rdata(buffer_type * output,rrtype_descriptor_type * descriptor,rr_type * record)1975 oneline_print_rdata(buffer_type *output, rrtype_descriptor_type *descriptor,
1976 rr_type* record)
1977 {
1978 size_t i;
1979 size_t saved_position = buffer_position(output);
1980
1981 for (i = 0; i < record->rdata_count; ++i) {
1982 if (i == 0) {
1983 buffer_printf(output, "\t");
1984 } else {
1985 buffer_printf(output, " ");
1986 }
1987 if (!rdata_atom_to_string(
1988 output,
1989 (rdata_zoneformat_type) descriptor->zoneformat[i],
1990 record->rdatas[i], record))
1991 {
1992 buffer_set_position(output, saved_position);
1993 return 0;
1994 }
1995 }
1996
1997 return 1;
1998 }
1999
2000 /* parse wireformat RR into a struct RR in temp region */
parse_wirerr_into_temp(struct zone * zone,char * fname,struct region * temp,uint8_t * buf,size_t len,const dname_type ** dname,struct rr * rr)2001 static int parse_wirerr_into_temp(struct zone* zone, char* fname,
2002 struct region* temp, uint8_t* buf, size_t len,
2003 const dname_type** dname, struct rr* rr)
2004 {
2005 size_t bufpos = 0;
2006 uint16_t rdlen;
2007 ssize_t rdata_num;
2008 buffer_type packet;
2009 domain_table_type* owners;
2010 owners = domain_table_create(temp);
2011 memset(rr, 0, sizeof(*rr));
2012 *dname = dname_make(temp, buf, 1);
2013 if(!*dname) {
2014 log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: failed to parse dname", zone->opts->name, fname);
2015 return 0;
2016 }
2017 bufpos = (*dname)->name_size;
2018 if(bufpos+10 > len) {
2019 log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: buffer too short", zone->opts->name, fname);
2020 return 0;
2021 }
2022 rr->type = read_uint16(buf+bufpos);
2023 bufpos += 2;
2024 rr->klass = read_uint16(buf+bufpos);
2025 bufpos += 2;
2026 rr->ttl = read_uint32(buf+bufpos);
2027 bufpos += 4;
2028 rdlen = read_uint16(buf+bufpos);
2029 bufpos += 2;
2030 if(bufpos + rdlen > len) {
2031 log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: buffer too short for rdatalen", zone->opts->name, fname);
2032 return 0;
2033 }
2034 buffer_create_from(&packet, buf+bufpos, rdlen);
2035 rdata_num = rdata_wireformat_to_rdata_atoms(
2036 temp, owners, rr->type, rdlen, &packet, &rr->rdatas);
2037 if(rdata_num == -1) {
2038 log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: cannot parse rdata", zone->opts->name, fname);
2039 return 0;
2040 }
2041 rr->rdata_count = rdata_num;
2042 return 1;
2043 }
2044
2045 /* print RR on one line in output buffer. caller must zeroterminate, if
2046 * that is needed. */
print_rr_oneline(struct buffer * rr_buffer,const dname_type * dname,struct rr * rr)2047 static int print_rr_oneline(struct buffer* rr_buffer, const dname_type* dname,
2048 struct rr* rr)
2049 {
2050 rrtype_descriptor_type *descriptor;
2051 descriptor = rrtype_descriptor_by_type(rr->type);
2052 buffer_printf(rr_buffer, "%s", dname_to_string(dname, NULL));
2053 buffer_printf(rr_buffer, "\t%lu\t%s\t%s", (unsigned long)rr->ttl,
2054 rrclass_to_string(rr->klass), rrtype_to_string(rr->type));
2055 if(!oneline_print_rdata(rr_buffer, descriptor, rr)) {
2056 if(!rdata_atoms_to_unknown_string(rr_buffer,
2057 descriptor, rr->rdata_count, rr->rdatas)) {
2058 return 0;
2059 }
2060 }
2061 return 1;
2062 }
2063
2064 /* write one RR to file, on one line */
ixfr_write_rr(struct zone * zone,FILE * out,char * fname,uint8_t * buf,size_t len,struct region * temp,buffer_type * rr_buffer)2065 static int ixfr_write_rr(struct zone* zone, FILE* out, char* fname,
2066 uint8_t* buf, size_t len, struct region* temp, buffer_type* rr_buffer)
2067 {
2068 const dname_type* dname;
2069 struct rr rr;
2070
2071 if(!parse_wirerr_into_temp(zone, fname, temp, buf, len, &dname, &rr)) {
2072 region_free_all(temp);
2073 return 0;
2074 }
2075
2076 buffer_clear(rr_buffer);
2077 if(!print_rr_oneline(rr_buffer, dname, &rr)) {
2078 log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: cannot spool RR string into buffer", zone->opts->name, fname);
2079 region_free_all(temp);
2080 return 0;
2081 }
2082 buffer_write_u8(rr_buffer, 0);
2083 buffer_flip(rr_buffer);
2084
2085 if(!fprintf(out, "%s\n", buffer_begin(rr_buffer))) {
2086 log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: cannot print RR string to file: %s", zone->opts->name, fname, strerror(errno));
2087 region_free_all(temp);
2088 return 0;
2089 }
2090 region_free_all(temp);
2091 return 1;
2092 }
2093
2094 /* write ixfr RRs to file */
ixfr_write_rrs(struct zone * zone,FILE * out,char * fname,uint8_t * buf,size_t len,struct region * temp,buffer_type * rr_buffer)2095 static int ixfr_write_rrs(struct zone* zone, FILE* out, char* fname,
2096 uint8_t* buf, size_t len, struct region* temp, buffer_type* rr_buffer)
2097 {
2098 size_t current = 0;
2099 if(!buf || len == 0)
2100 return 1;
2101 while(current < len) {
2102 size_t rrlen = count_rr_length(buf, len, current);
2103 if(rrlen == 0)
2104 return 0;
2105 if(current + rrlen > len)
2106 return 0;
2107 if(!ixfr_write_rr(zone, out, fname, buf+current, rrlen,
2108 temp, rr_buffer))
2109 return 0;
2110 current += rrlen;
2111 }
2112 return 1;
2113 }
2114
2115 /* write the ixfr data file data */
ixfr_write_file_data(struct zone * zone,struct ixfr_data * data,FILE * out,char * fname)2116 static int ixfr_write_file_data(struct zone* zone, struct ixfr_data* data,
2117 FILE* out, char* fname)
2118 {
2119 struct region* temp, *rrtemp;
2120 buffer_type* rr_buffer;
2121 temp = region_create(xalloc, free);
2122 rrtemp = region_create(xalloc, free);
2123 rr_buffer = buffer_create(rrtemp, MAX_RDLENGTH);
2124
2125 if(!ixfr_write_rrs(zone, out, fname, data->newsoa, data->newsoa_len,
2126 temp, rr_buffer)) {
2127 region_destroy(temp);
2128 region_destroy(rrtemp);
2129 return 0;
2130 }
2131 if(!ixfr_write_rrs(zone, out, fname, data->oldsoa, data->oldsoa_len,
2132 temp, rr_buffer)) {
2133 region_destroy(temp);
2134 region_destroy(rrtemp);
2135 return 0;
2136 }
2137 if(!ixfr_write_rrs(zone, out, fname, data->del, data->del_len,
2138 temp, rr_buffer)) {
2139 region_destroy(temp);
2140 region_destroy(rrtemp);
2141 return 0;
2142 }
2143 if(!ixfr_write_rrs(zone, out, fname, data->add, data->add_len,
2144 temp, rr_buffer)) {
2145 region_destroy(temp);
2146 region_destroy(rrtemp);
2147 return 0;
2148 }
2149 region_destroy(temp);
2150 region_destroy(rrtemp);
2151 return 1;
2152 }
2153
ixfr_write_file(struct zone * zone,struct ixfr_data * data,const char * zfile,int file_num)2154 int ixfr_write_file(struct zone* zone, struct ixfr_data* data,
2155 const char* zfile, int file_num)
2156 {
2157 char ixfrfile[1024+24];
2158 FILE* out;
2159 make_ixfr_name(ixfrfile, sizeof(ixfrfile), zfile, file_num);
2160 VERBOSITY(1, (LOG_INFO, "writing zone %s IXFR data to file %s",
2161 zone->opts->name, ixfrfile));
2162 out = fopen(ixfrfile, "w");
2163 if(!out) {
2164 log_msg(LOG_ERR, "could not open for writing zone %s IXFR file %s: %s",
2165 zone->opts->name, ixfrfile, strerror(errno));
2166 return 0;
2167 }
2168
2169 if(!ixfr_write_file_header(zone, data, out)) {
2170 log_msg(LOG_ERR, "could not write file header for zone %s IXFR file %s: %s",
2171 zone->opts->name, ixfrfile, strerror(errno));
2172 fclose(out);
2173 return 0;
2174 }
2175 if(!ixfr_write_file_data(zone, data, out, ixfrfile)) {
2176 fclose(out);
2177 return 0;
2178 }
2179
2180 fclose(out);
2181 data->file_num = file_num;
2182 return 1;
2183 }
2184
2185 /* write the ixfr files that need to be stored on disk */
ixfr_write_files(struct zone * zone,const char * zfile)2186 static void ixfr_write_files(struct zone* zone, const char* zfile)
2187 {
2188 size_t prevcount = 0;
2189 int num;
2190 struct ixfr_data* data;
2191 if(!zone->ixfr || !zone->ixfr->data)
2192 return; /* nothing to write */
2193
2194 /* write unwritten files to disk */
2195 data = ixfr_data_last(zone->ixfr);
2196 num=1;
2197 while(data && data->file_num == 0) {
2198 if(!ixfr_write_file(zone, data, zfile, num)) {
2199 /* There could be more files that are sitting on the
2200 * disk, remove them, they are not used without
2201 * this ixfr file.
2202 *
2203 * Give this element a file num, so it can be
2204 * deleted, it failed to write. It may be partial,
2205 * and we do not want to read that back in.
2206 * We are left with the newer transfers, that form
2207 * a correct list of transfers, that are wholly
2208 * written. */
2209 data->file_num = num;
2210 ixfr_delete_rest_files(zone, data, zfile, 0);
2211 return;
2212 }
2213 num++;
2214 data = ixfr_data_prev(zone->ixfr, data, &prevcount);
2215 }
2216 }
2217
ixfr_write_to_file(struct zone * zone,const char * zfile)2218 void ixfr_write_to_file(struct zone* zone, const char* zfile)
2219 {
2220 int dest_num_files = 0;
2221 /* we just wrote the zonefile zfile, and it is time to write
2222 * the IXFR contents to the disk too. */
2223 /* find out what the target number of files is that we want on
2224 * the disk */
2225 dest_num_files = ixfr_target_number_files(zone);
2226
2227 /* delete if we have more than we need */
2228 ixfr_delete_superfluous_files(zone, zfile, dest_num_files);
2229
2230 /* delete if we have too much in memory */
2231 ixfr_delete_memory_items(zone, dest_num_files);
2232
2233 /* rename the transfers that we have that already have a file */
2234 if(!ixfr_rename_files(zone, zfile, dest_num_files))
2235 return;
2236
2237 /* write the transfers that are not written yet */
2238 ixfr_write_files(zone, zfile);
2239 }
2240
2241 /* skip whitespace */
skipwhite(char * str)2242 static char* skipwhite(char* str)
2243 {
2244 while(isspace((unsigned char)*str))
2245 str++;
2246 return str;
2247 }
2248
2249 /* read one RR from file */
ixfr_data_readrr(struct zone * zone,FILE * in,const char * ixfrfile,struct region * tempregion,struct domain_table * temptable,struct zone * tempzone,struct rr ** rr)2250 static int ixfr_data_readrr(struct zone* zone, FILE* in, const char* ixfrfile,
2251 struct region* tempregion, struct domain_table* temptable,
2252 struct zone* tempzone, struct rr** rr)
2253 {
2254 char line[65536];
2255 char* str;
2256 struct domain* domain_parsed = NULL;
2257 int num_rrs = 0;
2258 line[sizeof(line)-1]=0;
2259 while(!feof(in)) {
2260 if(!fgets(line, sizeof(line), in)) {
2261 if(errno == 0) {
2262 log_msg(LOG_ERR, "zone %s IXFR data %s: "
2263 "unexpected end of file", zone->opts->name, ixfrfile);
2264 return 0;
2265 }
2266 log_msg(LOG_ERR, "zone %s IXFR data %s: "
2267 "cannot read: %s", zone->opts->name, ixfrfile,
2268 strerror(errno));
2269 return 0;
2270 }
2271 str = skipwhite(line);
2272 if(str[0] == 0) {
2273 /* empty line */
2274 continue;
2275 }
2276 if(str[0] == ';') {
2277 /* comment line */
2278 continue;
2279 }
2280 if(zonec_parse_string(tempregion, temptable, tempzone,
2281 line, &domain_parsed, &num_rrs)) {
2282 log_msg(LOG_ERR, "zone %s IXFR data %s: parse error",
2283 zone->opts->name, ixfrfile);
2284 return 0;
2285 }
2286 if(num_rrs != 1) {
2287 log_msg(LOG_ERR, "zone %s IXFR data %s: parse error",
2288 zone->opts->name, ixfrfile);
2289 return 0;
2290 }
2291 *rr = &domain_parsed->rrsets->rrs[0];
2292 return 1;
2293 }
2294 log_msg(LOG_ERR, "zone %s IXFR data %s: file too short, no newsoa",
2295 zone->opts->name, ixfrfile);
2296 return 0;
2297 }
2298
2299 /* delete from domain table */
domain_table_delete(struct domain_table * table,struct domain * domain)2300 static void domain_table_delete(struct domain_table* table,
2301 struct domain* domain)
2302 {
2303 #ifdef USE_RADIX_TREE
2304 radix_delete(table->nametree, domain->rnode);
2305 #else
2306 rbtree_delete(table->names_to_domains, domain->node.key);
2307 #endif
2308 }
2309
2310 /* can we delete temp domain */
can_del_temp_domain(struct domain * domain)2311 static int can_del_temp_domain(struct domain* domain)
2312 {
2313 struct domain* n;
2314 /* we want to keep the zone apex */
2315 if(domain->is_apex)
2316 return 0;
2317 if(domain->rrsets)
2318 return 0;
2319 if(domain->usage)
2320 return 0;
2321 /* check if there are domains under it */
2322 n = domain_next(domain);
2323 if(n && domain_is_subdomain(n, domain))
2324 return 0;
2325 return 1;
2326 }
2327
2328 /* delete temporary domain */
ixfr_temp_deldomain(struct domain_table * temptable,struct domain * domain)2329 static void ixfr_temp_deldomain(struct domain_table* temptable,
2330 struct domain* domain)
2331 {
2332 struct domain* p;
2333 if(!can_del_temp_domain(domain))
2334 return;
2335 p = domain->parent;
2336 /* see if this domain is someones wildcard-child-closest-match,
2337 * which can only be the parent, and then it should use the
2338 * one-smaller than this domain as closest-match. */
2339 if(domain->parent &&
2340 domain->parent->wildcard_child_closest_match == domain)
2341 domain->parent->wildcard_child_closest_match =
2342 domain_previous_existing_child(domain);
2343 domain_table_delete(temptable, domain);
2344 while(p) {
2345 struct domain* up = p->parent;
2346 if(!can_del_temp_domain(p))
2347 break;
2348 if(p->parent && p->parent->wildcard_child_closest_match == p)
2349 p->parent->wildcard_child_closest_match =
2350 domain_previous_existing_child(p);
2351 domain_table_delete(temptable, p);
2352 p = up;
2353 }
2354 }
2355
2356 /* clear out the just read RR from the temp table */
clear_temp_table_of_rr(struct domain_table * temptable,struct zone * tempzone,struct rr * rr)2357 static void clear_temp_table_of_rr(struct domain_table* temptable,
2358 struct zone* tempzone, struct rr* rr)
2359 {
2360 #if 0 /* clear out by removing everything, alternate for the cleanout code */
2361 /* clear domains from the tempzone,
2362 * the only domain left is the zone apex and its parents */
2363 domain_type* domain;
2364 #ifdef USE_RADIX_TREE
2365 struct radnode* first = radix_first(temptable->nametree);
2366 domain = first?(domain_type*)first->elem:NULL;
2367 #else
2368 domain = (domain_type*)rbtree_first(temptable->names_to_domains);
2369 #endif
2370 while(domain != (domain_type*)RBTREE_NULL && domain) {
2371 domain_type* next = domain_next(domain);
2372 if(domain != tempzone->apex &&
2373 !domain_is_subdomain(tempzone->apex, domain)) {
2374 domain_table_delete(temptable, domain);
2375 } else {
2376 if(!domain->parent /* is the root */ ||
2377 domain == tempzone->apex)
2378 domain->usage = 1;
2379 else domain->usage = 0;
2380 }
2381 domain = next;
2382 }
2383
2384 if(rr->owner == tempzone->apex) {
2385 tempzone->apex->rrsets = NULL;
2386 tempzone->soa_rrset = NULL;
2387 tempzone->soa_nx_rrset = NULL;
2388 tempzone->ns_rrset = NULL;
2389 }
2390 return;
2391 #endif
2392
2393 /* clear domains in the rdata */
2394 unsigned i;
2395 for(i=0; i<rr->rdata_count; i++) {
2396 if(rdata_atom_is_domain(rr->type, i)) {
2397 /* clear out that dname */
2398 struct domain* domain =
2399 rdata_atom_domain(rr->rdatas[i]);
2400 domain->usage --;
2401 if(domain != tempzone->apex && domain->usage == 0)
2402 ixfr_temp_deldomain(temptable, domain);
2403 }
2404 }
2405
2406 /* clear domain_parsed */
2407 if(rr->owner == tempzone->apex) {
2408 tempzone->apex->rrsets = NULL;
2409 tempzone->soa_rrset = NULL;
2410 tempzone->soa_nx_rrset = NULL;
2411 tempzone->ns_rrset = NULL;
2412 } else {
2413 rr->owner->rrsets = NULL;
2414 if(rr->owner->usage == 0) {
2415 ixfr_temp_deldomain(temptable, rr->owner);
2416 }
2417 }
2418 }
2419
2420 /* read ixfr data new SOA */
ixfr_data_readnewsoa(struct ixfr_data * data,struct zone * zone,FILE * in,const char * ixfrfile,struct region * tempregion,struct domain_table * temptable,struct zone * tempzone,uint32_t dest_serial)2421 static int ixfr_data_readnewsoa(struct ixfr_data* data, struct zone* zone,
2422 FILE* in, const char* ixfrfile, struct region* tempregion,
2423 struct domain_table* temptable, struct zone* tempzone,
2424 uint32_t dest_serial)
2425 {
2426 struct rr* rr;
2427 size_t capacity = 0;
2428 if(!ixfr_data_readrr(zone, in, ixfrfile, tempregion, temptable,
2429 tempzone, &rr))
2430 return 0;
2431 if(rr->type != TYPE_SOA) {
2432 log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data does not start with SOA",
2433 zone->opts->name, ixfrfile);
2434 return 0;
2435 }
2436 if(rr->klass != CLASS_IN) {
2437 log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data is not class IN",
2438 zone->opts->name, ixfrfile);
2439 return 0;
2440 }
2441 if(!zone->apex) {
2442 log_msg(LOG_ERR, "zone %s ixfr data %s: zone has no apex, no zone data",
2443 zone->opts->name, ixfrfile);
2444 return 0;
2445 }
2446 if(dname_compare(domain_dname(zone->apex), domain_dname(rr->owner)) != 0) {
2447 log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data wrong SOA for zone %s",
2448 zone->opts->name, ixfrfile, domain_to_string(rr->owner));
2449 return 0;
2450 }
2451 data->newserial = soa_rr_get_serial(rr);
2452 if(data->newserial != dest_serial) {
2453 log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data contains the wrong version, serial %u but want destination serial %u",
2454 zone->opts->name, ixfrfile, data->newserial,
2455 dest_serial);
2456 return 0;
2457 }
2458 if(!ixfr_putrr(domain_dname(rr->owner), rr->type, rr->klass, rr->ttl, rr->rdatas, rr->rdata_count, &data->newsoa, &data->newsoa_len, &capacity)) {
2459 log_msg(LOG_ERR, "zone %s ixfr data %s: cannot allocate space",
2460 zone->opts->name, ixfrfile);
2461 return 0;
2462 }
2463 clear_temp_table_of_rr(temptable, tempzone, rr);
2464 region_free_all(tempregion);
2465 ixfr_trim_capacity(&data->newsoa, &data->newsoa_len, &capacity);
2466 return 1;
2467 }
2468
2469 /* read ixfr data old SOA */
ixfr_data_readoldsoa(struct ixfr_data * data,struct zone * zone,FILE * in,const char * ixfrfile,struct region * tempregion,struct domain_table * temptable,struct zone * tempzone,uint32_t * dest_serial)2470 static int ixfr_data_readoldsoa(struct ixfr_data* data, struct zone* zone,
2471 FILE* in, const char* ixfrfile, struct region* tempregion,
2472 struct domain_table* temptable, struct zone* tempzone,
2473 uint32_t* dest_serial)
2474 {
2475 struct rr* rr;
2476 size_t capacity = 0;
2477 if(!ixfr_data_readrr(zone, in, ixfrfile, tempregion, temptable,
2478 tempzone, &rr))
2479 return 0;
2480 if(rr->type != TYPE_SOA) {
2481 log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data 2nd RR is not SOA",
2482 zone->opts->name, ixfrfile);
2483 return 0;
2484 }
2485 if(rr->klass != CLASS_IN) {
2486 log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data 2ndSOA is not class IN",
2487 zone->opts->name, ixfrfile);
2488 return 0;
2489 }
2490 if(!zone->apex) {
2491 log_msg(LOG_ERR, "zone %s ixfr data %s: zone has no apex, no zone data",
2492 zone->opts->name, ixfrfile);
2493 return 0;
2494 }
2495 if(dname_compare(domain_dname(zone->apex), domain_dname(rr->owner)) != 0) {
2496 log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data wrong 2nd SOA for zone %s",
2497 zone->opts->name, ixfrfile, domain_to_string(rr->owner));
2498 return 0;
2499 }
2500 data->oldserial = soa_rr_get_serial(rr);
2501 if(!ixfr_putrr(domain_dname(rr->owner), rr->type, rr->klass, rr->ttl, rr->rdatas, rr->rdata_count, &data->oldsoa, &data->oldsoa_len, &capacity)) {
2502 log_msg(LOG_ERR, "zone %s ixfr data %s: cannot allocate space",
2503 zone->opts->name, ixfrfile);
2504 return 0;
2505 }
2506 clear_temp_table_of_rr(temptable, tempzone, rr);
2507 region_free_all(tempregion);
2508 ixfr_trim_capacity(&data->oldsoa, &data->oldsoa_len, &capacity);
2509 *dest_serial = data->oldserial;
2510 return 1;
2511 }
2512
2513 /* read ixfr data del section */
ixfr_data_readdel(struct ixfr_data * data,struct zone * zone,FILE * in,const char * ixfrfile,struct region * tempregion,struct domain_table * temptable,struct zone * tempzone)2514 static int ixfr_data_readdel(struct ixfr_data* data, struct zone* zone,
2515 FILE* in, const char* ixfrfile, struct region* tempregion,
2516 struct domain_table* temptable, struct zone* tempzone)
2517 {
2518 struct rr* rr;
2519 size_t capacity = 0;
2520 while(1) {
2521 if(!ixfr_data_readrr(zone, in, ixfrfile, tempregion, temptable,
2522 tempzone, &rr))
2523 return 0;
2524 if(!ixfr_putrr(domain_dname(rr->owner), rr->type, rr->klass, rr->ttl, rr->rdatas, rr->rdata_count, &data->del, &data->del_len, &capacity)) {
2525 log_msg(LOG_ERR, "zone %s ixfr data %s: cannot allocate space",
2526 zone->opts->name, ixfrfile);
2527 return 0;
2528 }
2529 /* check SOA and also serial, because there could be other
2530 * add and del sections from older versions collated, we can
2531 * see this del section end when it has the serial */
2532 if(rr->type == TYPE_SOA &&
2533 soa_rr_get_serial(rr) == data->newserial) {
2534 /* end of del section. */
2535 clear_temp_table_of_rr(temptable, tempzone, rr);
2536 region_free_all(tempregion);
2537 break;
2538 }
2539 clear_temp_table_of_rr(temptable, tempzone, rr);
2540 region_free_all(tempregion);
2541 }
2542 ixfr_trim_capacity(&data->del, &data->del_len, &capacity);
2543 return 1;
2544 }
2545
2546 /* read ixfr data add section */
ixfr_data_readadd(struct ixfr_data * data,struct zone * zone,FILE * in,const char * ixfrfile,struct region * tempregion,struct domain_table * temptable,struct zone * tempzone)2547 static int ixfr_data_readadd(struct ixfr_data* data, struct zone* zone,
2548 FILE* in, const char* ixfrfile, struct region* tempregion,
2549 struct domain_table* temptable, struct zone* tempzone)
2550 {
2551 struct rr* rr;
2552 size_t capacity = 0;
2553 while(1) {
2554 if(!ixfr_data_readrr(zone, in, ixfrfile, tempregion, temptable,
2555 tempzone, &rr))
2556 return 0;
2557 if(!ixfr_putrr(domain_dname(rr->owner), rr->type, rr->klass, rr->ttl, rr->rdatas, rr->rdata_count, &data->add, &data->add_len, &capacity)) {
2558 log_msg(LOG_ERR, "zone %s ixfr data %s: cannot allocate space",
2559 zone->opts->name, ixfrfile);
2560 return 0;
2561 }
2562 if(rr->type == TYPE_SOA &&
2563 soa_rr_get_serial(rr) == data->newserial) {
2564 /* end of add section. */
2565 clear_temp_table_of_rr(temptable, tempzone, rr);
2566 region_free_all(tempregion);
2567 break;
2568 }
2569 clear_temp_table_of_rr(temptable, tempzone, rr);
2570 region_free_all(tempregion);
2571 }
2572 ixfr_trim_capacity(&data->add, &data->add_len, &capacity);
2573 return 1;
2574 }
2575
2576 /* read ixfr data from file */
ixfr_data_read(struct nsd * nsd,struct zone * zone,FILE * in,const char * ixfrfile,uint32_t * dest_serial,int file_num)2577 static int ixfr_data_read(struct nsd* nsd, struct zone* zone, FILE* in,
2578 const char* ixfrfile, uint32_t* dest_serial, int file_num)
2579 {
2580 struct ixfr_data* data = NULL;
2581 struct region* tempregion, *stayregion;
2582 struct domain_table* temptable;
2583 struct zone* tempzone;
2584
2585 if(zone->ixfr &&
2586 zone->ixfr->data->count == zone->opts->pattern->ixfr_number) {
2587 VERBOSITY(3, (LOG_INFO, "zone %s skip %s IXFR data because only %d ixfr-number configured",
2588 zone->opts->name, ixfrfile, (int)zone->opts->pattern->ixfr_number));
2589 return 0;
2590 }
2591
2592 /* the file has header comments, new soa, old soa, delsection,
2593 * addsection. The delsection and addsection end in a SOA of oldver
2594 * and newver respectively. */
2595 data = xalloc_zero(sizeof(*data));
2596 data->file_num = file_num;
2597
2598 /* the temp region is cleared after every RR */
2599 tempregion = region_create(xalloc, free);
2600 /* the stay region holds the temporary data that stays between RRs */
2601 stayregion = region_create(xalloc, free);
2602 temptable = domain_table_create(stayregion);
2603 tempzone = region_alloc_zero(stayregion, sizeof(zone_type));
2604 if(!zone->apex) {
2605 ixfr_data_free(data);
2606 region_destroy(tempregion);
2607 region_destroy(stayregion);
2608 return 0;
2609 }
2610 tempzone->apex = domain_table_insert(temptable,
2611 domain_dname(zone->apex));
2612 temptable->root->usage++;
2613 tempzone->apex->usage++;
2614 tempzone->opts = zone->opts;
2615 /* switch to per RR region for new allocations in temp domain table */
2616 temptable->region = tempregion;
2617
2618 if(!ixfr_data_readnewsoa(data, zone, in, ixfrfile, tempregion,
2619 temptable, tempzone, *dest_serial)) {
2620 ixfr_data_free(data);
2621 region_destroy(tempregion);
2622 region_destroy(stayregion);
2623 return 0;
2624 }
2625 if(!ixfr_data_readoldsoa(data, zone, in, ixfrfile, tempregion,
2626 temptable, tempzone, dest_serial)) {
2627 ixfr_data_free(data);
2628 region_destroy(tempregion);
2629 region_destroy(stayregion);
2630 return 0;
2631 }
2632 if(!ixfr_data_readdel(data, zone, in, ixfrfile, tempregion, temptable,
2633 tempzone)) {
2634 ixfr_data_free(data);
2635 region_destroy(tempregion);
2636 region_destroy(stayregion);
2637 return 0;
2638 }
2639 if(!ixfr_data_readadd(data, zone, in, ixfrfile, tempregion, temptable,
2640 tempzone)) {
2641 ixfr_data_free(data);
2642 region_destroy(tempregion);
2643 region_destroy(stayregion);
2644 return 0;
2645 }
2646
2647 region_destroy(tempregion);
2648 region_destroy(stayregion);
2649
2650 if(!zone->ixfr)
2651 zone->ixfr = zone_ixfr_create(nsd);
2652 if(zone->opts->pattern->ixfr_size != 0 &&
2653 zone->ixfr->total_size + ixfr_data_size(data) >
2654 zone->opts->pattern->ixfr_size) {
2655 VERBOSITY(3, (LOG_INFO, "zone %s skip %s IXFR data because only ixfr-size: %u configured, and it is %u size",
2656 zone->opts->name, ixfrfile, (unsigned)zone->opts->pattern->ixfr_size, (unsigned)ixfr_data_size(data)));
2657 ixfr_data_free(data);
2658 return 0;
2659 }
2660 zone_ixfr_add(zone->ixfr, data, 0);
2661 VERBOSITY(3, (LOG_INFO, "zone %s read %s IXFR data of %u bytes",
2662 zone->opts->name, ixfrfile, (unsigned)ixfr_data_size(data)));
2663 return 1;
2664 }
2665
2666 /* try to read the next ixfr file. returns false if it fails or if it
2667 * does not fit in the configured sizes */
ixfr_read_one_more_file(struct nsd * nsd,struct zone * zone,const char * zfile,int num_files,uint32_t * dest_serial)2668 static int ixfr_read_one_more_file(struct nsd* nsd, struct zone* zone,
2669 const char* zfile, int num_files, uint32_t *dest_serial)
2670 {
2671 char ixfrfile[1024+24];
2672 FILE* in;
2673 int file_num = num_files+1;
2674 make_ixfr_name(ixfrfile, sizeof(ixfrfile), zfile, file_num);
2675 in = fopen(ixfrfile, "r");
2676 if(!in) {
2677 if(errno == ENOENT) {
2678 /* the file does not exist, we reached the end
2679 * of the list of IXFR files */
2680 return 0;
2681 }
2682 log_msg(LOG_ERR, "could not read zone %s IXFR file %s: %s",
2683 zone->opts->name, ixfrfile, strerror(errno));
2684 return 0;
2685 }
2686 warn_if_directory("IXFR data", in, ixfrfile);
2687 if(!ixfr_data_read(nsd, zone, in, ixfrfile, dest_serial, file_num)) {
2688 fclose(in);
2689 return 0;
2690 }
2691 fclose(in);
2692 return 1;
2693 }
2694
ixfr_read_from_file(struct nsd * nsd,struct zone * zone,const char * zfile)2695 void ixfr_read_from_file(struct nsd* nsd, struct zone* zone, const char* zfile)
2696 {
2697 uint32_t serial;
2698 int num_files = 0;
2699 /* delete the existing data, the zone data in memory has likely
2700 * changed, eg. due to reading a new zonefile. So that needs new
2701 * IXFRs */
2702 zone_ixfr_clear(zone->ixfr);
2703
2704 /* track the serial number that we need to end up with, and check
2705 * that the IXFRs match up and result in the required version */
2706 serial = zone_get_current_serial(zone);
2707
2708 while(ixfr_read_one_more_file(nsd, zone, zfile, num_files, &serial)) {
2709 num_files++;
2710 }
2711 if(num_files > 0) {
2712 VERBOSITY(1, (LOG_INFO, "zone %s read %d IXFR transfers with success",
2713 zone->opts->name, num_files));
2714 }
2715 }
2716