xfrd.c revision 1.1.1.4 1 /*
2 * xfrd.c - XFR (transfer) Daemon source file. Coordinates SOA updates.
3 *
4 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
5 *
6 * See LICENSE for the license.
7 *
8 */
9
10 #include "config.h"
11 #include <assert.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include "xfrd.h"
19 #include "xfrd-tcp.h"
20 #include "xfrd-disk.h"
21 #include "xfrd-notify.h"
22 #include "options.h"
23 #include "util.h"
24 #include "netio.h"
25 #include "region-allocator.h"
26 #include "nsd.h"
27 #include "packet.h"
28 #include "rdata.h"
29 #include "difffile.h"
30 #include "ipc.h"
31 #include "remote.h"
32 #include "rrl.h"
33 #ifdef USE_DNSTAP
34 #include "dnstap/dnstap_collector.h"
35 #endif
36
37 #ifdef HAVE_SYSTEMD
38 #include <systemd/sd-daemon.h>
39 #endif
40
41 #define XFRD_UDP_TIMEOUT 10 /* seconds, before a udp request times out */
42 #define XFRD_NO_IXFR_CACHE 172800 /* 48h before retrying ixfr's after notimpl */
43 #define XFRD_LOWERBOUND_REFRESH 1 /* seconds, smallest refresh timeout */
44 #define XFRD_LOWERBOUND_RETRY 1 /* seconds, smallest retry timeout */
45 #define XFRD_MAX_ROUNDS 1 /* max number of rounds along the masters */
46 #define XFRD_TSIG_MAX_UNSIGNED 103 /* max number of packets without tsig in a tcp stream. */
47 /* rfc recommends 100, +3 for offbyone errors/interoperability. */
48 #define XFRD_CHILD_REAP_TIMEOUT 60 /* seconds to wakeup and reap lost children */
49 /* these are reload processes that SIGCHILDed but the signal
50 * was lost, and need waitpid to remove their process entry. */
51
52 /* the daemon state */
53 xfrd_state_type* xfrd = 0;
54
55 /* main xfrd loop */
56 static void xfrd_main(void);
57 /* shut down xfrd, close sockets. */
58 static void xfrd_shutdown(void);
59 /* delete pending task xfr files in tmp */
60 static void xfrd_clean_pending_tasks(struct nsd* nsd, udb_base* u);
61 /* create zone rbtree at start */
62 static void xfrd_init_zones(void);
63 /* initial handshake with SOAINFO from main and send expire to main */
64 static void xfrd_receive_soa(int socket, int shortsoa);
65
66 /* handle incoming notification message. soa can be NULL. true if transfer needed. */
67 static int xfrd_handle_incoming_notify(xfrd_zone_type* zone,
68 xfrd_soa_type* soa);
69
70 /* call with buffer just after the soa dname. returns 0 on error. */
71 static int xfrd_parse_soa_info(buffer_type* packet, xfrd_soa_type* soa);
72 /* set the zone state to a new state (takes care of expiry messages) */
73 static void xfrd_set_zone_state(xfrd_zone_type* zone,
74 enum xfrd_zone_state new_zone_state);
75 /* set timer for retry amount (depends on zone_state) */
76 static void xfrd_set_timer_retry(xfrd_zone_type* zone);
77 /* set timer for refresh timeout (depends on zone_state) */
78 static void xfrd_set_timer_refresh(xfrd_zone_type* zone);
79
80 /* set reload timeout */
81 static void xfrd_set_reload_timeout(void);
82 /* handle reload timeout */
83 static void xfrd_handle_reload(int fd, short event, void* arg);
84 /* handle child timeout */
85 static void xfrd_handle_child_timer(int fd, short event, void* arg);
86
87 /* send ixfr request, returns fd of connection to read on */
88 static int xfrd_send_ixfr_request_udp(xfrd_zone_type* zone);
89 /* obtain udp socket slot */
90 static void xfrd_udp_obtain(xfrd_zone_type* zone);
91
92 /* read data via udp */
93 static void xfrd_udp_read(xfrd_zone_type* zone);
94
95 /* find master by notify number */
96 static int find_same_master_notify(xfrd_zone_type* zone, int acl_num_nfy);
97
98 /* set the write timer to activate */
99 static void xfrd_write_timer_set(void);
100
101 static void
102 xfrd_signal_callback(int sig, short event, void* ATTR_UNUSED(arg))
103 {
104 if(!(event & EV_SIGNAL))
105 return;
106 sig_handler(sig);
107 }
108
109 static struct event* xfrd_sig_evs[10];
110 static int xfrd_sig_num = 0;
111
112 static void
113 xfrd_sigsetup(int sig)
114 {
115 struct event *ev = xalloc_zero(sizeof(*ev));
116 assert(xfrd_sig_num <= (int)(sizeof(xfrd_sig_evs)/sizeof(ev)));
117 xfrd_sig_evs[xfrd_sig_num++] = ev;
118 signal_set(ev, sig, xfrd_signal_callback, NULL);
119 if(event_base_set(xfrd->event_base, ev) != 0) {
120 log_msg(LOG_ERR, "xfrd sig handler: event_base_set failed");
121 }
122 if(signal_add(ev, NULL) != 0) {
123 log_msg(LOG_ERR, "xfrd sig handler: signal_add failed");
124 }
125 }
126
127 void
128 xfrd_init(int socket, struct nsd* nsd, int shortsoa, int reload_active,
129 pid_t nsd_pid)
130 {
131 region_type* region;
132
133 assert(xfrd == 0);
134 /* to setup signalhandling */
135 nsd->server_kind = NSD_SERVER_MAIN;
136
137 region = region_create_custom(xalloc, free, DEFAULT_CHUNK_SIZE,
138 DEFAULT_LARGE_OBJECT_SIZE, DEFAULT_INITIAL_CLEANUP_SIZE, 1);
139 xfrd = (xfrd_state_type*)region_alloc(region, sizeof(xfrd_state_type));
140 memset(xfrd, 0, sizeof(xfrd_state_type));
141 xfrd->region = region;
142 xfrd->xfrd_start_time = time(0);
143 xfrd->event_base = nsd_child_event_base();
144 if(!xfrd->event_base) {
145 log_msg(LOG_ERR, "xfrd: cannot create event base");
146 exit(1);
147 }
148 xfrd->nsd = nsd;
149 xfrd->packet = buffer_create(xfrd->region, QIOBUFSZ);
150 xfrd->udp_waiting_first = NULL;
151 xfrd->udp_waiting_last = NULL;
152 xfrd->udp_use_num = 0;
153 xfrd->got_time = 0;
154 xfrd->xfrfilenumber = 0;
155 #ifdef USE_ZONE_STATS
156 xfrd->zonestat_safe = nsd->zonestatdesired;
157 #endif
158 xfrd->activated_first = NULL;
159 xfrd->ipc_pass = buffer_create(xfrd->region, QIOBUFSZ);
160 xfrd->last_task = region_alloc(xfrd->region, sizeof(*xfrd->last_task));
161 udb_ptr_init(xfrd->last_task, xfrd->nsd->task[xfrd->nsd->mytask]);
162 assert(shortsoa || udb_base_get_userdata(xfrd->nsd->task[xfrd->nsd->mytask])->data == 0);
163
164 xfrd->reload_handler.ev_fd = -1;
165 xfrd->reload_added = 0;
166 xfrd->reload_timeout.tv_sec = 0;
167 xfrd->reload_cmd_last_sent = xfrd->xfrd_start_time;
168 xfrd->can_send_reload = !reload_active;
169 xfrd->reload_pid = nsd_pid;
170 xfrd->child_timer_added = 0;
171
172 xfrd->ipc_send_blocked = 0;
173 event_set(&xfrd->ipc_handler, socket, EV_PERSIST|EV_READ,
174 xfrd_handle_ipc, xfrd);
175 if(event_base_set(xfrd->event_base, &xfrd->ipc_handler) != 0)
176 log_msg(LOG_ERR, "xfrd ipc handler: event_base_set failed");
177 if(event_add(&xfrd->ipc_handler, NULL) != 0)
178 log_msg(LOG_ERR, "xfrd ipc handler: event_add failed");
179 xfrd->ipc_handler_flags = EV_PERSIST|EV_READ;
180 xfrd->ipc_conn = xfrd_tcp_create(xfrd->region, QIOBUFSZ);
181 /* not reading using ipc_conn yet */
182 xfrd->ipc_conn->is_reading = 0;
183 xfrd->ipc_conn->fd = socket;
184 xfrd->need_to_send_reload = 0;
185 xfrd->need_to_send_shutdown = 0;
186 xfrd->need_to_send_stats = 0;
187
188 xfrd->write_zonefile_needed = 0;
189 if(nsd->options->zonefiles_write)
190 xfrd_write_timer_set();
191
192 xfrd->notify_waiting_first = NULL;
193 xfrd->notify_waiting_last = NULL;
194 xfrd->notify_udp_num = 0;
195
196 #ifdef HAVE_SSL
197 daemon_remote_attach(xfrd->nsd->rc, xfrd);
198 #endif
199
200 xfrd->tcp_set = xfrd_tcp_set_create(xfrd->region);
201 xfrd->tcp_set->tcp_timeout = nsd->tcp_timeout;
202 #ifndef HAVE_ARC4RANDOM
203 srandom((unsigned long) getpid() * (unsigned long) time(NULL));
204 #endif
205
206 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd pre-startup"));
207 xfrd_init_zones();
208 xfrd_receive_soa(socket, shortsoa);
209 if(nsd->options->xfrdfile != NULL && nsd->options->xfrdfile[0]!=0)
210 xfrd_read_state(xfrd);
211
212 /* did we get killed before startup was successful? */
213 if(nsd->signal_hint_shutdown) {
214 kill(nsd_pid, SIGTERM);
215 xfrd_shutdown();
216 return;
217 }
218
219 /* init libevent signals now, so that in the previous init scripts
220 * the normal sighandler is called, and can set nsd->signal_hint..
221 * these are also looked at in sig_process before we run the main loop*/
222 xfrd_sigsetup(SIGHUP);
223 xfrd_sigsetup(SIGTERM);
224 xfrd_sigsetup(SIGQUIT);
225 xfrd_sigsetup(SIGCHLD);
226 xfrd_sigsetup(SIGALRM);
227 xfrd_sigsetup(SIGILL);
228 xfrd_sigsetup(SIGUSR1);
229 xfrd_sigsetup(SIGINT);
230
231 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd startup"));
232 #ifdef HAVE_SYSTEMD
233 sd_notify(0, "READY=1");
234 #endif
235 xfrd_main();
236 }
237
238 static void
239 xfrd_process_activated(void)
240 {
241 xfrd_zone_type* zone;
242 while((zone = xfrd->activated_first)) {
243 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s activation",
244 zone->apex_str));
245 /* pop zone from activated list */
246 xfrd->activated_first = zone->activated_next;
247 if(zone->activated_next)
248 zone->activated_next->activated_prev = NULL;
249 zone->is_activated = 0;
250 /* run it : no events, specifically not the TIMEOUT event,
251 * so that running zone transfers are not interrupted */
252 xfrd_handle_zone(zone->zone_handler.ev_fd, 0, zone);
253 }
254 }
255
256 static void
257 xfrd_sig_process(void)
258 {
259 int status;
260 pid_t child_pid;
261
262 if(xfrd->nsd->signal_hint_quit || xfrd->nsd->signal_hint_shutdown) {
263 xfrd->nsd->signal_hint_quit = 0;
264 xfrd->nsd->signal_hint_shutdown = 0;
265 xfrd->need_to_send_shutdown = 1;
266 if(!(xfrd->ipc_handler_flags&EV_WRITE)) {
267 ipc_xfrd_set_listening(xfrd, EV_PERSIST|EV_READ|EV_WRITE);
268 }
269 } else if(xfrd->nsd->signal_hint_reload_hup) {
270 log_msg(LOG_WARNING, "SIGHUP received, reloading...");
271 xfrd->nsd->signal_hint_reload_hup = 0;
272 if(xfrd->nsd->options->zonefiles_check) {
273 task_new_check_zonefiles(xfrd->nsd->task[
274 xfrd->nsd->mytask], xfrd->last_task, NULL);
275 }
276 xfrd_set_reload_now(xfrd);
277 } else if(xfrd->nsd->signal_hint_statsusr) {
278 xfrd->nsd->signal_hint_statsusr = 0;
279 xfrd->need_to_send_stats = 1;
280 if(!(xfrd->ipc_handler_flags&EV_WRITE)) {
281 ipc_xfrd_set_listening(xfrd, EV_PERSIST|EV_READ|EV_WRITE);
282 }
283 }
284
285 /* collect children that exited. */
286 xfrd->nsd->signal_hint_child = 0;
287 while((child_pid = waitpid(-1, &status, WNOHANG)) != -1 && child_pid != 0) {
288 if(status != 0) {
289 log_msg(LOG_ERR, "process %d exited with status %d",
290 (int)child_pid, status);
291 }
292 }
293 if(!xfrd->child_timer_added) {
294 struct timeval tv;
295 tv.tv_sec = XFRD_CHILD_REAP_TIMEOUT;
296 tv.tv_usec = 0;
297 event_set(&xfrd->child_timer, -1, EV_TIMEOUT,
298 xfrd_handle_child_timer, xfrd);
299 if(event_base_set(xfrd->event_base, &xfrd->child_timer) != 0)
300 log_msg(LOG_ERR, "xfrd child timer: event_base_set failed");
301 if(event_add(&xfrd->child_timer, &tv) != 0)
302 log_msg(LOG_ERR, "xfrd child timer: event_add failed");
303 xfrd->child_timer_added = 1;
304 }
305 }
306
307 static void
308 xfrd_main(void)
309 {
310 /* we may have signals from the startup period, process them */
311 xfrd_sig_process();
312 xfrd->shutdown = 0;
313 while(!xfrd->shutdown)
314 {
315 /* process activated zones before blocking in select again */
316 xfrd_process_activated();
317 /* dispatch may block for a longer period, so current is gone */
318 xfrd->got_time = 0;
319 if(event_base_loop(xfrd->event_base, EVLOOP_ONCE) == -1) {
320 if (errno != EINTR) {
321 log_msg(LOG_ERR,
322 "xfrd dispatch failed: %s",
323 strerror(errno));
324 }
325 }
326 xfrd_sig_process();
327 }
328 xfrd_shutdown();
329 }
330
331 static void
332 xfrd_shutdown()
333 {
334 xfrd_zone_type* zone;
335
336 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd shutdown"));
337 #ifdef HAVE_SYSTEMD
338 sd_notify(0, "STOPPING=1");
339 #endif
340 event_del(&xfrd->ipc_handler);
341 close(xfrd->ipc_handler.ev_fd); /* notifies parent we stop */
342 zone_list_close(nsd.options);
343 if(xfrd->nsd->options->xfrdfile != NULL && xfrd->nsd->options->xfrdfile[0]!=0)
344 xfrd_write_state(xfrd);
345 if(xfrd->reload_added) {
346 event_del(&xfrd->reload_handler);
347 xfrd->reload_added = 0;
348 }
349 if(xfrd->child_timer_added) {
350 event_del(&xfrd->child_timer);
351 xfrd->child_timer_added = 0;
352 }
353 if(xfrd->nsd->options->zonefiles_write) {
354 event_del(&xfrd->write_timer);
355 }
356 #ifdef HAVE_SSL
357 daemon_remote_close(xfrd->nsd->rc); /* close sockets of rc */
358 #endif
359 /* close sockets */
360 RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
361 {
362 if(zone->event_added) {
363 event_del(&zone->zone_handler);
364 if(zone->zone_handler.ev_fd != -1) {
365 close(zone->zone_handler.ev_fd);
366 zone->zone_handler.ev_fd = -1;
367 }
368 zone->event_added = 0;
369 }
370 }
371 close_notify_fds(xfrd->notify_zones);
372
373 /* wait for server parent (if necessary) */
374 if(xfrd->reload_pid != -1) {
375 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd wait for servermain %d",
376 (int)xfrd->reload_pid));
377 while(1) {
378 if(waitpid(xfrd->reload_pid, NULL, 0) == -1) {
379 if(errno == EINTR) continue;
380 if(errno == ECHILD) break;
381 log_msg(LOG_ERR, "xfrd: waitpid(%d): %s",
382 (int)xfrd->reload_pid, strerror(errno));
383 }
384 break;
385 }
386 }
387
388 /* if we are killed past this point this is not a problem,
389 * some files left in /tmp are cleaned by the OS, but it is neater
390 * to clean them out */
391
392 /* unlink xfr files for running transfers */
393 RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
394 {
395 if(zone->msg_seq_nr)
396 xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
397 }
398 /* unlink xfr files in not-yet-done task file */
399 xfrd_clean_pending_tasks(xfrd->nsd, xfrd->nsd->task[xfrd->nsd->mytask]);
400 xfrd_del_tempdir(xfrd->nsd);
401 #ifdef HAVE_SSL
402 daemon_remote_delete(xfrd->nsd->rc); /* ssl-delete secret keys */
403 #endif
404 #ifdef USE_DNSTAP
405 dt_collector_close(nsd.dt_collector, &nsd);
406 #endif
407
408 /* process-exit cleans up memory used by xfrd process */
409 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd shutdown complete"));
410 #ifdef MEMCLEAN /* OS collects memory pages */
411 if(xfrd->zones) {
412 xfrd_zone_type* z;
413 RBTREE_FOR(z, xfrd_zone_type*, xfrd->zones) {
414 tsig_delete_record(&z->tsig, NULL);
415 }
416 }
417 if(xfrd->notify_zones) {
418 struct notify_zone* n;
419 RBTREE_FOR(n, struct notify_zone*, xfrd->notify_zones) {
420 tsig_delete_record(&n->notify_tsig, NULL);
421 }
422 }
423 if(xfrd_sig_num > 0) {
424 int i;
425 for(i=0; i<xfrd_sig_num; i++) {
426 signal_del(xfrd_sig_evs[i]);
427 free(xfrd_sig_evs[i]);
428 }
429 for(i=0; i<(int)nsd.ifs; i++) {
430 if(nsd.udp[i].s != -1 && nsd.udp[i].addr)
431 freeaddrinfo(nsd.udp[i].addr);
432 if(nsd.tcp[i].s != -1 && nsd.tcp[i].addr)
433 freeaddrinfo(nsd.tcp[i].addr);
434 }
435 }
436 #ifdef RATELIMIT
437 rrl_mmap_deinit();
438 #endif
439 #ifdef USE_DNSTAP
440 dt_collector_destroy(nsd.dt_collector, &nsd);
441 #endif
442 udb_base_free(nsd.task[0]);
443 udb_base_free(nsd.task[1]);
444 event_base_free(xfrd->event_base);
445 region_destroy(xfrd->region);
446 nsd_options_destroy(nsd.options);
447 region_destroy(nsd.region);
448 log_finalize();
449 #endif
450
451 exit(0);
452 }
453
454 static void
455 xfrd_clean_pending_tasks(struct nsd* nsd, udb_base* u)
456 {
457 udb_ptr t;
458 udb_ptr_new(&t, u, udb_base_get_userdata(u));
459 /* no dealloc of entries, we delete the entire file when done */
460 while(!udb_ptr_is_null(&t)) {
461 if(TASKLIST(&t)->task_type == task_apply_xfr) {
462 xfrd_unlink_xfrfile(nsd, TASKLIST(&t)->yesno);
463 }
464 udb_ptr_set_rptr(&t, u, &TASKLIST(&t)->next);
465 }
466 udb_ptr_unlink(&t, u);
467 }
468
469 void
470 xfrd_init_slave_zone(xfrd_state_type* xfrd, struct zone_options* zone_opt)
471 {
472 xfrd_zone_type *xzone;
473 xzone = (xfrd_zone_type*)region_alloc(xfrd->region,
474 sizeof(xfrd_zone_type));
475 memset(xzone, 0, sizeof(xfrd_zone_type));
476 xzone->apex = zone_opt->node.key;
477 xzone->apex_str = zone_opt->name;
478 xzone->state = xfrd_zone_refreshing;
479 xzone->zone_options = zone_opt;
480 /* first retry will use first master */
481 xzone->master = xzone->zone_options->pattern->request_xfr;
482 xzone->master_num = 0;
483 xzone->next_master = 0;
484 xzone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_START;
485
486 xzone->soa_nsd_acquired = 0;
487 xzone->soa_disk_acquired = 0;
488 xzone->soa_notified_acquired = 0;
489 /* [0]=1, [1]=0; "." domain name */
490 xzone->soa_nsd.prim_ns[0] = 1;
491 xzone->soa_nsd.email[0] = 1;
492 xzone->soa_disk.prim_ns[0]=1;
493 xzone->soa_disk.email[0]=1;
494 xzone->soa_notified.prim_ns[0]=1;
495 xzone->soa_notified.email[0]=1;
496
497 xzone->zone_handler.ev_fd = -1;
498 xzone->zone_handler_flags = 0;
499 xzone->event_added = 0;
500
501 xzone->tcp_conn = -1;
502 xzone->tcp_waiting = 0;
503 xzone->udp_waiting = 0;
504 xzone->is_activated = 0;
505
506 xzone->multi_master_first_master = -1;
507 xzone->multi_master_update_check = -1;
508 tsig_create_record_custom(&xzone->tsig, NULL, 0, 0, 4);
509
510 /* set refreshing anyway, if we have data it may be old */
511 xfrd_set_refresh_now(xzone);
512
513 xzone->node.key = xzone->apex;
514 rbtree_insert(xfrd->zones, (rbnode_type*)xzone);
515 }
516
517 static void
518 xfrd_init_zones()
519 {
520 struct zone_options *zone_opt;
521 assert(xfrd->zones == 0);
522
523 xfrd->zones = rbtree_create(xfrd->region,
524 (int (*)(const void *, const void *)) dname_compare);
525 xfrd->notify_zones = rbtree_create(xfrd->region,
526 (int (*)(const void *, const void *)) dname_compare);
527
528 RBTREE_FOR(zone_opt, struct zone_options*, xfrd->nsd->options->zone_options)
529 {
530 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: adding %s zone",
531 zone_opt->name));
532
533 init_notify_send(xfrd->notify_zones, xfrd->region, zone_opt);
534 if(!zone_is_slave(zone_opt)) {
535 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s, "
536 "master zone has no outgoing xfr requests",
537 zone_opt->name));
538 continue;
539 }
540 xfrd_init_slave_zone(xfrd, zone_opt);
541 }
542 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: started server %d "
543 "secondary zones", (int)xfrd->zones->count));
544 }
545
546 static void
547 xfrd_process_soa_info_task(struct task_list_d* task)
548 {
549 xfrd_soa_type soa;
550 xfrd_soa_type* soa_ptr = &soa;
551 xfrd_zone_type* zone;
552 DEBUG(DEBUG_IPC,1, (LOG_INFO, "xfrd: process SOAINFO %s",
553 dname_to_string(task->zname, 0)));
554 zone = (xfrd_zone_type*)rbtree_search(xfrd->zones, task->zname);
555 if(task->size <= sizeof(struct task_list_d)+dname_total_size(
556 task->zname)+sizeof(uint32_t)*6 + sizeof(uint8_t)*2) {
557 /* NSD has zone without any info */
558 DEBUG(DEBUG_IPC,1, (LOG_INFO, "SOAINFO for %s lost zone",
559 dname_to_string(task->zname,0)));
560 soa_ptr = NULL;
561 } else {
562 uint8_t* p = (uint8_t*)task->zname + dname_total_size(
563 task->zname);
564 /* read the soa info */
565 memset(&soa, 0, sizeof(soa));
566 /* left out type, klass, count for speed */
567 soa.type = htons(TYPE_SOA);
568 soa.klass = htons(CLASS_IN);
569 memmove(&soa.ttl, p, sizeof(uint32_t));
570 p += sizeof(uint32_t);
571 soa.rdata_count = htons(7);
572 memmove(soa.prim_ns, p, sizeof(uint8_t));
573 p += sizeof(uint8_t);
574 memmove(soa.prim_ns+1, p, soa.prim_ns[0]);
575 p += soa.prim_ns[0];
576 memmove(soa.email, p, sizeof(uint8_t));
577 p += sizeof(uint8_t);
578 memmove(soa.email+1, p, soa.email[0]);
579 p += soa.email[0];
580 memmove(&soa.serial, p, sizeof(uint32_t));
581 p += sizeof(uint32_t);
582 memmove(&soa.refresh, p, sizeof(uint32_t));
583 p += sizeof(uint32_t);
584 memmove(&soa.retry, p, sizeof(uint32_t));
585 p += sizeof(uint32_t);
586 memmove(&soa.expire, p, sizeof(uint32_t));
587 p += sizeof(uint32_t);
588 memmove(&soa.minimum, p, sizeof(uint32_t));
589 /* p += sizeof(uint32_t); if we wanted to read further */
590 DEBUG(DEBUG_IPC,1, (LOG_INFO, "SOAINFO for %s %u",
591 dname_to_string(task->zname,0),
592 (unsigned)ntohl(soa.serial)));
593 }
594
595 if(!zone) {
596 DEBUG(DEBUG_IPC,1, (LOG_INFO, "xfrd: zone %s master zone updated",
597 dname_to_string(task->zname,0)));
598 notify_handle_master_zone_soainfo(xfrd->notify_zones,
599 task->zname, soa_ptr);
600 return;
601 }
602 xfrd_handle_incoming_soa(zone, soa_ptr, xfrd_time());
603 }
604
605 static void
606 xfrd_receive_soa(int socket, int shortsoa)
607 {
608 sig_atomic_t cmd;
609 struct udb_base* xtask = xfrd->nsd->task[xfrd->nsd->mytask];
610 udb_ptr last_task, t;
611 xfrd_zone_type* zone;
612
613 if(!shortsoa) {
614 /* put all expired zones into mytask */
615 udb_ptr_init(&last_task, xtask);
616 RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones) {
617 if(zone->state == xfrd_zone_expired) {
618 task_new_expire(xtask, &last_task, zone->apex, 1);
619 }
620 }
621 udb_ptr_unlink(&last_task, xtask);
622
623 /* send RELOAD to main to give it this tasklist */
624 task_process_sync(xtask);
625 cmd = NSD_RELOAD;
626 if(!write_socket(socket, &cmd, sizeof(cmd))) {
627 log_msg(LOG_ERR, "problems sending reload xfrdtomain: %s",
628 strerror(errno));
629 }
630 }
631
632 /* receive RELOAD_DONE to get SOAINFO tasklist */
633 if(block_read(&nsd, socket, &cmd, sizeof(cmd), -1) != sizeof(cmd) ||
634 cmd != NSD_RELOAD_DONE) {
635 if(nsd.signal_hint_shutdown)
636 return;
637 log_msg(LOG_ERR, "did not get start signal from main");
638 exit(1);
639 }
640 if(block_read(NULL, socket, &xfrd->reload_pid, sizeof(pid_t), -1)
641 != sizeof(pid_t)) {
642 log_msg(LOG_ERR, "xfrd cannot get reload_pid");
643 }
644
645 /* process tasklist (SOAINFO data) */
646 udb_ptr_unlink(xfrd->last_task, xtask);
647 /* if shortsoa: then use my own taskdb that nsdparent filled */
648 if(!shortsoa)
649 xfrd->nsd->mytask = 1 - xfrd->nsd->mytask;
650 xtask = xfrd->nsd->task[xfrd->nsd->mytask];
651 task_remap(xtask);
652 udb_ptr_new(&t, xtask, udb_base_get_userdata(xtask));
653 while(!udb_ptr_is_null(&t)) {
654 xfrd_process_soa_info_task(TASKLIST(&t));
655 udb_ptr_set_rptr(&t, xtask, &TASKLIST(&t)->next);
656 }
657 udb_ptr_unlink(&t, xtask);
658 task_clear(xtask);
659 udb_ptr_init(xfrd->last_task, xfrd->nsd->task[xfrd->nsd->mytask]);
660
661 if(!shortsoa) {
662 /* receive RELOAD_DONE that signals the other tasklist is
663 * empty, and thus xfrd can operate (can call reload and swap
664 * to the other, empty, tasklist) */
665 if(block_read(NULL, socket, &cmd, sizeof(cmd), -1) !=
666 sizeof(cmd) ||
667 cmd != NSD_RELOAD_DONE) {
668 log_msg(LOG_ERR, "did not get start signal 2 from "
669 "main");
670 exit(1);
671 }
672 } else {
673 /* for shortsoa version, do expire later */
674 /* if expire notifications, put in my task and
675 * schedule a reload to make sure they are processed */
676 RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones) {
677 if(zone->state == xfrd_zone_expired) {
678 xfrd_send_expire_notification(zone);
679 }
680 }
681 }
682 }
683
684 void
685 xfrd_reopen_logfile(void)
686 {
687 if (xfrd->nsd->file_rotation_ok)
688 log_reopen(xfrd->nsd->log_filename, 0);
689 }
690
691 void
692 xfrd_deactivate_zone(xfrd_zone_type* z)
693 {
694 if(z->is_activated) {
695 /* delete from activated list */
696 if(z->activated_prev)
697 z->activated_prev->activated_next = z->activated_next;
698 else xfrd->activated_first = z->activated_next;
699 if(z->activated_next)
700 z->activated_next->activated_prev = z->activated_prev;
701 z->is_activated = 0;
702 }
703 }
704
705 void
706 xfrd_del_slave_zone(xfrd_state_type* xfrd, const dname_type* dname)
707 {
708 xfrd_zone_type* z = (xfrd_zone_type*)rbtree_delete(xfrd->zones, dname);
709 if(!z) return;
710
711 /* io */
712 if(z->tcp_waiting) {
713 /* delete from tcp waiting list */
714 if(z->tcp_waiting_prev)
715 z->tcp_waiting_prev->tcp_waiting_next =
716 z->tcp_waiting_next;
717 else xfrd->tcp_set->tcp_waiting_first = z->tcp_waiting_next;
718 if(z->tcp_waiting_next)
719 z->tcp_waiting_next->tcp_waiting_prev =
720 z->tcp_waiting_prev;
721 else xfrd->tcp_set->tcp_waiting_last = z->tcp_waiting_prev;
722 z->tcp_waiting = 0;
723 }
724 if(z->udp_waiting) {
725 /* delete from udp waiting list */
726 if(z->udp_waiting_prev)
727 z->udp_waiting_prev->udp_waiting_next =
728 z->udp_waiting_next;
729 else xfrd->udp_waiting_first = z->udp_waiting_next;
730 if(z->udp_waiting_next)
731 z->udp_waiting_next->udp_waiting_prev =
732 z->udp_waiting_prev;
733 else xfrd->udp_waiting_last = z->udp_waiting_prev;
734 z->udp_waiting = 0;
735 }
736 xfrd_deactivate_zone(z);
737 if(z->tcp_conn != -1) {
738 xfrd_tcp_release(xfrd->tcp_set, z);
739 } else if(z->zone_handler.ev_fd != -1 && z->event_added) {
740 xfrd_udp_release(z);
741 } else if(z->event_added)
742 event_del(&z->zone_handler);
743 if(z->msg_seq_nr)
744 xfrd_unlink_xfrfile(xfrd->nsd, z->xfrfilenumber);
745
746 /* tsig */
747 tsig_delete_record(&z->tsig, NULL);
748
749 /* z->dname is recycled when the zone_options is removed */
750 region_recycle(xfrd->region, z, sizeof(*z));
751 }
752
753 void
754 xfrd_free_namedb(struct nsd* nsd)
755 {
756 namedb_close_udb(nsd->db);
757 namedb_close(nsd->db);
758 nsd->db = 0;
759 }
760
761 static void
762 xfrd_set_timer_refresh(xfrd_zone_type* zone)
763 {
764 time_t set_refresh;
765 time_t set_expire;
766 time_t set_min;
767 time_t set;
768 if(zone->soa_disk_acquired == 0 || zone->state != xfrd_zone_ok) {
769 xfrd_set_timer_retry(zone);
770 return;
771 }
772 /* refresh or expire timeout, whichever is earlier */
773 set_refresh = ntohl(zone->soa_disk.refresh);
774 if (set_refresh > (time_t)zone->zone_options->pattern->max_refresh_time)
775 set_refresh = zone->zone_options->pattern->max_refresh_time;
776 else if (set_refresh < (time_t)zone->zone_options->pattern->min_refresh_time)
777 set_refresh = zone->zone_options->pattern->min_refresh_time;
778 set_refresh += zone->soa_disk_acquired;
779 set_expire = zone->soa_disk_acquired + ntohl(zone->soa_disk.expire);
780 if(set_refresh < set_expire)
781 set = set_refresh;
782 else set = set_expire;
783 set_min = zone->soa_disk_acquired + XFRD_LOWERBOUND_REFRESH;
784 if(set < set_min)
785 set = set_min;
786 if(set < xfrd_time())
787 set = 0;
788 else set -= xfrd_time();
789 xfrd_set_timer(zone, set);
790 }
791
792 static void
793 xfrd_set_timer_retry(xfrd_zone_type* zone)
794 {
795 time_t set_retry;
796 int mult;
797 /* perform exponential backoff in all the cases */
798 if(zone->fresh_xfr_timeout == 0)
799 zone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_START;
800 else {
801 /* exponential backoff - some master data in zones is paid-for
802 but non-working, and will not get fixed. */
803 zone->fresh_xfr_timeout *= 2;
804 if(zone->fresh_xfr_timeout > XFRD_TRANSFER_TIMEOUT_MAX)
805 zone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_MAX;
806 }
807 /* exponential backoff multiplier, starts at 1, backs off */
808 mult = zone->fresh_xfr_timeout / XFRD_TRANSFER_TIMEOUT_START;
809 if(mult == 0) mult = 1;
810
811 /* set timer for next retry or expire timeout if earlier. */
812 if(zone->soa_disk_acquired == 0) {
813 /* if no information, use reasonable timeout */
814 #ifdef HAVE_ARC4RANDOM_UNIFORM
815 xfrd_set_timer(zone, zone->fresh_xfr_timeout
816 + arc4random_uniform(zone->fresh_xfr_timeout));
817 #elif HAVE_ARC4RANDOM
818 xfrd_set_timer(zone, zone->fresh_xfr_timeout
819 + arc4random() % zone->fresh_xfr_timeout);
820 #else
821 xfrd_set_timer(zone, zone->fresh_xfr_timeout
822 + random()%zone->fresh_xfr_timeout);
823 #endif
824 } else if(zone->state == xfrd_zone_expired ||
825 xfrd_time() + (time_t)ntohl(zone->soa_disk.retry)*mult <
826 zone->soa_disk_acquired + (time_t)ntohl(zone->soa_disk.expire))
827 {
828 set_retry = ntohl(zone->soa_disk.retry);
829 set_retry *= mult;
830 if(set_retry > (time_t)zone->zone_options->pattern->max_retry_time)
831 set_retry = zone->zone_options->pattern->max_retry_time;
832 else if(set_retry < (time_t)zone->zone_options->pattern->min_retry_time)
833 set_retry = zone->zone_options->pattern->min_retry_time;
834 if(set_retry < XFRD_LOWERBOUND_RETRY)
835 set_retry = XFRD_LOWERBOUND_RETRY;
836 xfrd_set_timer(zone, set_retry);
837 } else {
838 set_retry = ntohl(zone->soa_disk.expire);
839 if(set_retry < XFRD_LOWERBOUND_RETRY)
840 xfrd_set_timer(zone, XFRD_LOWERBOUND_RETRY);
841 else {
842 if(zone->soa_disk_acquired + set_retry < xfrd_time())
843 xfrd_set_timer(zone, XFRD_LOWERBOUND_RETRY);
844 else xfrd_set_timer(zone, zone->soa_disk_acquired +
845 set_retry - xfrd_time());
846 }
847 }
848 }
849
850 void
851 xfrd_handle_zone(int ATTR_UNUSED(fd), short event, void* arg)
852 {
853 xfrd_zone_type* zone = (xfrd_zone_type*)arg;
854
855 if(zone->tcp_conn != -1) {
856 if(event == 0) /* activated, but already in TCP, nothing to do*/
857 return;
858 /* busy in tcp transaction: an internal error */
859 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s event tcp", zone->apex_str));
860 xfrd_tcp_release(xfrd->tcp_set, zone);
861 /* continue to retry; as if a timeout happened */
862 event = EV_TIMEOUT;
863 }
864
865 if((event & EV_READ)) {
866 /* busy in udp transaction */
867 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s event udp read", zone->apex_str));
868 xfrd_udp_read(zone);
869 return;
870 }
871
872 /* timeout */
873 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s timeout", zone->apex_str));
874 if(zone->zone_handler.ev_fd != -1 && zone->event_added &&
875 (event & EV_TIMEOUT)) {
876 assert(zone->tcp_conn == -1);
877 xfrd_udp_release(zone);
878 }
879
880 if(zone->tcp_waiting) {
881 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s skips retry, TCP connections full",
882 zone->apex_str));
883 xfrd_unset_timer(zone);
884 return;
885 }
886 if(zone->udp_waiting) {
887 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s skips retry, UDP connections full",
888 zone->apex_str));
889 xfrd_unset_timer(zone);
890 return;
891 }
892
893 if(zone->soa_disk_acquired)
894 {
895 if (zone->state != xfrd_zone_expired &&
896 xfrd_time() >= zone->soa_disk_acquired + (time_t)ntohl(zone->soa_disk.expire)) {
897 /* zone expired */
898 log_msg(LOG_ERR, "xfrd: zone %s has expired", zone->apex_str);
899 xfrd_set_zone_state(zone, xfrd_zone_expired);
900 }
901 else if(zone->state == xfrd_zone_ok &&
902 xfrd_time() >= zone->soa_disk_acquired + (time_t)ntohl(zone->soa_disk.refresh)) {
903 /* zone goes to refreshing state. */
904 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s is refreshing", zone->apex_str));
905 xfrd_set_zone_state(zone, xfrd_zone_refreshing);
906 }
907 }
908
909 /* only make a new request if no request is running (UDPorTCP) */
910 if(zone->zone_handler.ev_fd == -1 && zone->tcp_conn == -1) {
911 /* make a new request */
912 xfrd_make_request(zone);
913 }
914 }
915
916 void
917 xfrd_make_request(xfrd_zone_type* zone)
918 {
919 if(zone->next_master != -1) {
920 /* we are told to use this next master */
921 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
922 "xfrd zone %s use master %i",
923 zone->apex_str, zone->next_master));
924 zone->master_num = zone->next_master;
925 zone->master = acl_find_num(zone->zone_options->pattern->
926 request_xfr, zone->master_num);
927 /* if there is no next master, fallback to use the first one */
928 if(!zone->master) {
929 zone->master = zone->zone_options->pattern->request_xfr;
930 zone->master_num = 0;
931 }
932 /* fallback to cycle master */
933 zone->next_master = -1;
934 zone->round_num = 0; /* fresh set of retries after notify */
935 } else {
936 /* cycle master */
937
938 if(zone->round_num != -1 && zone->master && zone->master->next)
939 {
940 /* try the next master */
941 zone->master = zone->master->next;
942 zone->master_num++;
943 } else {
944 /* start a new round */
945 zone->master = zone->zone_options->pattern->request_xfr;
946 zone->master_num = 0;
947 zone->round_num++;
948 }
949 if(zone->round_num >= XFRD_MAX_ROUNDS) {
950 /* tried all servers that many times, wait */
951 zone->round_num = -1;
952 xfrd_set_timer_retry(zone);
953 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
954 "xfrd zone %s makereq wait_retry, rd %d mr %d nx %d",
955 zone->apex_str, zone->round_num, zone->master_num, zone->next_master));
956 zone->multi_master_first_master = -1;
957 return;
958 }
959 }
960
961 /* multi-master-check */
962 if(zone->zone_options->pattern->multi_master_check) {
963 if(zone->multi_master_first_master == zone->master_num &&
964 zone->round_num > 0 &&
965 zone->state != xfrd_zone_expired) {
966 /* tried all servers and update zone */
967 if(zone->multi_master_update_check >= 0) {
968 VERBOSITY(2, (LOG_INFO, "xfrd: multi master "
969 "check: zone %s completed transfers",
970 zone->apex_str));
971 }
972 zone->round_num = -1; /* next try start anew */
973 zone->multi_master_first_master = -1;
974 xfrd_set_timer_refresh(zone);
975 return;
976 }
977 if(zone->multi_master_first_master < 0) {
978 zone->multi_master_first_master = zone->master_num;
979 zone->multi_master_update_check = -1;
980 }
981 }
982
983 /* cache ixfr_disabled only for XFRD_NO_IXFR_CACHE time */
984 if (zone->master->ixfr_disabled &&
985 (zone->master->ixfr_disabled + XFRD_NO_IXFR_CACHE) <= time(NULL)) {
986 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "clear negative caching ixfr "
987 "disabled for master %s num "
988 "%d ",
989 zone->master->ip_address_spec, zone->master_num));
990 zone->master->ixfr_disabled = 0;
991 }
992
993 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s make request round %d mr %d nx %d",
994 zone->apex_str, zone->round_num, zone->master_num, zone->next_master));
995 /* perform xfr request */
996 if (!zone->master->use_axfr_only && zone->soa_disk_acquired > 0 &&
997 !zone->master->ixfr_disabled) {
998
999 if (zone->master->allow_udp) {
1000 xfrd_set_timer(zone, XFRD_UDP_TIMEOUT);
1001 xfrd_udp_obtain(zone);
1002 }
1003 else { /* doing 3 rounds of IXFR/TCP might not be useful */
1004 xfrd_set_timer(zone, xfrd->tcp_set->tcp_timeout);
1005 xfrd_tcp_obtain(xfrd->tcp_set, zone);
1006 }
1007 }
1008 else if (zone->master->use_axfr_only || zone->soa_disk_acquired <= 0) {
1009 xfrd_set_timer(zone, xfrd->tcp_set->tcp_timeout);
1010 xfrd_tcp_obtain(xfrd->tcp_set, zone);
1011 }
1012 else if (zone->master->ixfr_disabled) {
1013 if (zone->zone_options->pattern->allow_axfr_fallback) {
1014 xfrd_set_timer(zone, xfrd->tcp_set->tcp_timeout);
1015 xfrd_tcp_obtain(xfrd->tcp_set, zone);
1016 } else {
1017 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s axfr "
1018 "fallback not allowed, skipping master %s.",
1019 zone->apex_str, zone->master->ip_address_spec));
1020 }
1021 }
1022 }
1023
1024 static void
1025 xfrd_udp_obtain(xfrd_zone_type* zone)
1026 {
1027 assert(zone->udp_waiting == 0);
1028 if(zone->tcp_conn != -1) {
1029 /* no tcp and udp at the same time */
1030 xfrd_tcp_release(xfrd->tcp_set, zone);
1031 }
1032 if(xfrd->udp_use_num < XFRD_MAX_UDP) {
1033 int fd;
1034 xfrd->udp_use_num++;
1035 fd = xfrd_send_ixfr_request_udp(zone);
1036 if(fd == -1)
1037 xfrd->udp_use_num--;
1038 else {
1039 if(zone->event_added)
1040 event_del(&zone->zone_handler);
1041 event_set(&zone->zone_handler, fd,
1042 EV_PERSIST|EV_READ|EV_TIMEOUT,
1043 xfrd_handle_zone, zone);
1044 if(event_base_set(xfrd->event_base, &zone->zone_handler) != 0)
1045 log_msg(LOG_ERR, "xfrd udp: event_base_set failed");
1046 if(event_add(&zone->zone_handler, &zone->timeout) != 0)
1047 log_msg(LOG_ERR, "xfrd udp: event_add failed");
1048 zone->zone_handler_flags=EV_PERSIST|EV_READ|EV_TIMEOUT;
1049 zone->event_added = 1;
1050 }
1051 return;
1052 }
1053 /* queue the zone as last */
1054 zone->udp_waiting = 1;
1055 zone->udp_waiting_next = NULL;
1056 zone->udp_waiting_prev = xfrd->udp_waiting_last;
1057 if(!xfrd->udp_waiting_first)
1058 xfrd->udp_waiting_first = zone;
1059 if(xfrd->udp_waiting_last)
1060 xfrd->udp_waiting_last->udp_waiting_next = zone;
1061 xfrd->udp_waiting_last = zone;
1062 xfrd_unset_timer(zone);
1063 }
1064
1065 time_t
1066 xfrd_time()
1067 {
1068 if(!xfrd->got_time) {
1069 xfrd->current_time = time(0);
1070 xfrd->got_time = 1;
1071 }
1072 return xfrd->current_time;
1073 }
1074
1075 void
1076 xfrd_copy_soa(xfrd_soa_type* soa, rr_type* rr)
1077 {
1078 const uint8_t* rr_ns_wire = dname_name(domain_dname(rdata_atom_domain(rr->rdatas[0])));
1079 uint8_t rr_ns_len = domain_dname(rdata_atom_domain(rr->rdatas[0]))->name_size;
1080 const uint8_t* rr_em_wire = dname_name(domain_dname(rdata_atom_domain(rr->rdatas[1])));
1081 uint8_t rr_em_len = domain_dname(rdata_atom_domain(rr->rdatas[1]))->name_size;
1082
1083 if(rr->type != TYPE_SOA || rr->rdata_count != 7) {
1084 log_msg(LOG_ERR, "xfrd: copy_soa called with bad rr, type %d rrs %u.",
1085 rr->type, rr->rdata_count);
1086 return;
1087 }
1088 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: copy_soa rr, type %d rrs %u, ttl %u.",
1089 (int)rr->type, (unsigned)rr->rdata_count, (unsigned)rr->ttl));
1090 soa->type = htons(rr->type);
1091 soa->klass = htons(rr->klass);
1092 soa->ttl = htonl(rr->ttl);
1093 soa->rdata_count = htons(rr->rdata_count);
1094
1095 /* copy dnames */
1096 soa->prim_ns[0] = rr_ns_len;
1097 memcpy(soa->prim_ns+1, rr_ns_wire, rr_ns_len);
1098 soa->email[0] = rr_em_len;
1099 memcpy(soa->email+1, rr_em_wire, rr_em_len);
1100
1101 /* already in network format */
1102 memcpy(&soa->serial, rdata_atom_data(rr->rdatas[2]), sizeof(uint32_t));
1103 memcpy(&soa->refresh, rdata_atom_data(rr->rdatas[3]), sizeof(uint32_t));
1104 memcpy(&soa->retry, rdata_atom_data(rr->rdatas[4]), sizeof(uint32_t));
1105 memcpy(&soa->expire, rdata_atom_data(rr->rdatas[5]), sizeof(uint32_t));
1106 memcpy(&soa->minimum, rdata_atom_data(rr->rdatas[6]), sizeof(uint32_t));
1107 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1108 "xfrd: copy_soa rr, serial %u refresh %u retry %u expire %u",
1109 (unsigned)ntohl(soa->serial), (unsigned)ntohl(soa->refresh),
1110 (unsigned)ntohl(soa->retry), (unsigned)ntohl(soa->expire)));
1111 }
1112
1113 static void
1114 xfrd_set_zone_state(xfrd_zone_type* zone, enum xfrd_zone_state s)
1115 {
1116 if(s != zone->state) {
1117 enum xfrd_zone_state old = zone->state;
1118 zone->state = s;
1119 if((s == xfrd_zone_expired || old == xfrd_zone_expired)
1120 && s!=old) {
1121 xfrd_send_expire_notification(zone);
1122 }
1123 }
1124 }
1125
1126 void
1127 xfrd_set_refresh_now(xfrd_zone_type* zone)
1128 {
1129 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s is activated, state %d",
1130 zone->apex_str, zone->state));
1131 if(!zone->is_activated) {
1132 /* push onto list */
1133 zone->activated_prev = 0;
1134 zone->activated_next = xfrd->activated_first;
1135 if(xfrd->activated_first)
1136 xfrd->activated_first->activated_prev = zone;
1137 xfrd->activated_first = zone;
1138 zone->is_activated = 1;
1139 }
1140 }
1141
1142 void
1143 xfrd_unset_timer(xfrd_zone_type* zone)
1144 {
1145 assert(zone->zone_handler.ev_fd == -1);
1146 if(zone->event_added)
1147 event_del(&zone->zone_handler);
1148 zone->zone_handler_flags = 0;
1149 zone->event_added = 0;
1150 }
1151
1152 void
1153 xfrd_set_timer(xfrd_zone_type* zone, time_t t)
1154 {
1155 int fd = zone->zone_handler.ev_fd;
1156 int fl = ((fd == -1)?EV_TIMEOUT:zone->zone_handler_flags);
1157 /* randomize the time, within 90%-100% of original */
1158 /* not later so zones cannot expire too late */
1159 /* only for times far in the future */
1160 if(t > 10) {
1161 time_t base = t*9/10;
1162 #ifdef HAVE_ARC4RANDOM_UNIFORM
1163 t = base + arc4random_uniform(t-base);
1164 #elif HAVE_ARC4RANDOM
1165 t = base + arc4random() % (t-base);
1166 #else
1167 t = base + random()%(t-base);
1168 #endif
1169 }
1170
1171 /* keep existing flags and fd, but re-add with timeout */
1172 if(zone->event_added)
1173 event_del(&zone->zone_handler);
1174 else fd = -1;
1175 zone->timeout.tv_sec = t;
1176 zone->timeout.tv_usec = 0;
1177 event_set(&zone->zone_handler, fd, fl, xfrd_handle_zone, zone);
1178 if(event_base_set(xfrd->event_base, &zone->zone_handler) != 0)
1179 log_msg(LOG_ERR, "xfrd timer: event_base_set failed");
1180 if(event_add(&zone->zone_handler, &zone->timeout) != 0)
1181 log_msg(LOG_ERR, "xfrd timer: event_add failed");
1182 zone->zone_handler_flags = fl;
1183 zone->event_added = 1;
1184 }
1185
1186 void
1187 xfrd_handle_incoming_soa(xfrd_zone_type* zone,
1188 xfrd_soa_type* soa, time_t acquired)
1189 {
1190 if(soa == NULL) {
1191 /* nsd no longer has a zone in memory */
1192 zone->soa_nsd_acquired = 0;
1193 xfrd_set_zone_state(zone, xfrd_zone_refreshing);
1194 xfrd_set_refresh_now(zone);
1195 return;
1196 }
1197 if(zone->soa_nsd_acquired && soa->serial == zone->soa_nsd.serial)
1198 return;
1199
1200 if(zone->soa_disk_acquired && soa->serial == zone->soa_disk.serial)
1201 {
1202 /* soa in disk has been loaded in memory */
1203 log_msg(LOG_INFO, "zone %s serial %u is updated to %u.",
1204 zone->apex_str, (unsigned)ntohl(zone->soa_nsd.serial),
1205 (unsigned)ntohl(soa->serial));
1206 zone->soa_nsd = zone->soa_disk;
1207 zone->soa_nsd_acquired = zone->soa_disk_acquired;
1208 xfrd->write_zonefile_needed = 1;
1209 /* reset exponential backoff, we got a normal timer now */
1210 zone->fresh_xfr_timeout = 0;
1211 if(xfrd_time() - zone->soa_disk_acquired
1212 < (time_t)ntohl(zone->soa_disk.refresh))
1213 {
1214 /* zone ok, wait for refresh time */
1215 xfrd_set_zone_state(zone, xfrd_zone_ok);
1216 zone->round_num = -1;
1217 xfrd_set_timer_refresh(zone);
1218 } else if(xfrd_time() - zone->soa_disk_acquired
1219 < (time_t)ntohl(zone->soa_disk.expire))
1220 {
1221 /* zone refreshing */
1222 xfrd_set_zone_state(zone, xfrd_zone_refreshing);
1223 xfrd_set_refresh_now(zone);
1224 }
1225 if(xfrd_time() - zone->soa_disk_acquired
1226 >= (time_t)ntohl(zone->soa_disk.expire)) {
1227 /* zone expired */
1228 xfrd_set_zone_state(zone, xfrd_zone_expired);
1229 xfrd_set_refresh_now(zone);
1230 }
1231
1232 if(zone->soa_notified_acquired != 0 &&
1233 (zone->soa_notified.serial == 0 ||
1234 compare_serial(ntohl(zone->soa_disk.serial),
1235 ntohl(zone->soa_notified.serial)) >= 0))
1236 { /* read was in response to this notification */
1237 zone->soa_notified_acquired = 0;
1238 }
1239 if(zone->soa_notified_acquired && zone->state == xfrd_zone_ok)
1240 {
1241 /* refresh because of notification */
1242 xfrd_set_zone_state(zone, xfrd_zone_refreshing);
1243 xfrd_set_refresh_now(zone);
1244 }
1245 xfrd_send_notify(xfrd->notify_zones, zone->apex, &zone->soa_nsd);
1246 return;
1247 }
1248
1249 /* user must have manually provided zone data */
1250 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1251 "xfrd: zone %s serial %u from zonefile. refreshing",
1252 zone->apex_str, (unsigned)ntohl(soa->serial)));
1253 zone->soa_nsd = *soa;
1254 zone->soa_disk = *soa;
1255 zone->soa_nsd_acquired = acquired;
1256 zone->soa_disk_acquired = acquired;
1257 if(zone->soa_notified_acquired != 0 &&
1258 (zone->soa_notified.serial == 0 ||
1259 compare_serial(ntohl(zone->soa_disk.serial),
1260 ntohl(zone->soa_notified.serial)) >= 0))
1261 { /* user provided in response to this notification */
1262 zone->soa_notified_acquired = 0;
1263 }
1264 xfrd_set_zone_state(zone, xfrd_zone_refreshing);
1265 xfrd_set_refresh_now(zone);
1266 xfrd_send_notify(xfrd->notify_zones, zone->apex, &zone->soa_nsd);
1267 }
1268
1269 void
1270 xfrd_send_expire_notification(xfrd_zone_type* zone)
1271 {
1272 task_new_expire(xfrd->nsd->task[xfrd->nsd->mytask], xfrd->last_task,
1273 zone->apex, zone->state == xfrd_zone_expired);
1274 xfrd_set_reload_timeout();
1275 }
1276
1277 int
1278 xfrd_udp_read_packet(buffer_type* packet, int fd, struct sockaddr* src,
1279 socklen_t* srclen)
1280 {
1281 ssize_t received;
1282
1283 /* read the data */
1284 buffer_clear(packet);
1285 received = recvfrom(fd, buffer_begin(packet), buffer_remaining(packet),
1286 0, src, srclen);
1287 if(received == -1) {
1288 log_msg(LOG_ERR, "xfrd: recvfrom failed: %s",
1289 strerror(errno));
1290 return 0;
1291 }
1292 buffer_set_limit(packet, received);
1293 return 1;
1294 }
1295
1296 void
1297 xfrd_udp_release(xfrd_zone_type* zone)
1298 {
1299 assert(zone->udp_waiting == 0);
1300 if(zone->event_added)
1301 event_del(&zone->zone_handler);
1302 if(zone->zone_handler.ev_fd != -1) {
1303 close(zone->zone_handler.ev_fd);
1304 }
1305 zone->zone_handler.ev_fd = -1;
1306 zone->zone_handler_flags = 0;
1307 zone->event_added = 0;
1308 /* see if there are waiting zones */
1309 if(xfrd->udp_use_num == XFRD_MAX_UDP)
1310 {
1311 while(xfrd->udp_waiting_first) {
1312 /* snip off waiting list */
1313 xfrd_zone_type* wz = xfrd->udp_waiting_first;
1314 assert(wz->udp_waiting);
1315 wz->udp_waiting = 0;
1316 xfrd->udp_waiting_first = wz->udp_waiting_next;
1317 if(wz->udp_waiting_next)
1318 wz->udp_waiting_next->udp_waiting_prev = NULL;
1319 if(xfrd->udp_waiting_last == wz)
1320 xfrd->udp_waiting_last = NULL;
1321 /* see if this zone needs udp connection */
1322 if(wz->tcp_conn == -1) {
1323 int fd = xfrd_send_ixfr_request_udp(wz);
1324 if(fd != -1) {
1325 if(wz->event_added)
1326 event_del(&wz->zone_handler);
1327 event_set(&wz->zone_handler, fd,
1328 EV_READ|EV_TIMEOUT|EV_PERSIST,
1329 xfrd_handle_zone, wz);
1330 if(event_base_set(xfrd->event_base,
1331 &wz->zone_handler) != 0)
1332 log_msg(LOG_ERR, "cannot set event_base for ixfr");
1333 if(event_add(&wz->zone_handler, &wz->timeout) != 0)
1334 log_msg(LOG_ERR, "cannot add event for ixfr");
1335 wz->zone_handler_flags = EV_READ|EV_TIMEOUT|EV_PERSIST;
1336 wz->event_added = 1;
1337 return;
1338 } else {
1339 /* make this zone do something with
1340 * this failure to act */
1341 xfrd_set_refresh_now(wz);
1342 }
1343 }
1344 }
1345 }
1346 /* no waiting zones */
1347 if(xfrd->udp_use_num > 0)
1348 xfrd->udp_use_num--;
1349 }
1350
1351 /** disable ixfr for master */
1352 void
1353 xfrd_disable_ixfr(xfrd_zone_type* zone)
1354 {
1355 if(!(zone->master->ixfr_disabled &&
1356 (zone->master->ixfr_disabled + XFRD_NO_IXFR_CACHE) <= time(NULL))) {
1357 /* start new round, with IXFR disabled */
1358 zone->round_num = 0;
1359 zone->next_master = zone->master_num;
1360 }
1361 zone->master->ixfr_disabled = time(NULL);
1362 }
1363
1364 static void
1365 xfrd_udp_read(xfrd_zone_type* zone)
1366 {
1367 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s read udp data", zone->apex_str));
1368 if(!xfrd_udp_read_packet(xfrd->packet, zone->zone_handler.ev_fd,
1369 NULL, NULL)) {
1370 zone->master->bad_xfr_count++;
1371 if (zone->master->bad_xfr_count > 2) {
1372 xfrd_disable_ixfr(zone);
1373 zone->master->bad_xfr_count = 0;
1374 }
1375 /* drop packet */
1376 xfrd_udp_release(zone);
1377 /* query next server */
1378 xfrd_make_request(zone);
1379 return;
1380 }
1381 switch(xfrd_handle_received_xfr_packet(zone, xfrd->packet)) {
1382 case xfrd_packet_tcp:
1383 xfrd_set_timer(zone, xfrd->tcp_set->tcp_timeout);
1384 xfrd_udp_release(zone);
1385 xfrd_tcp_obtain(xfrd->tcp_set, zone);
1386 break;
1387 case xfrd_packet_transfer:
1388 if(zone->zone_options->pattern->multi_master_check) {
1389 xfrd_udp_release(zone);
1390 xfrd_make_request(zone);
1391 break;
1392 }
1393 /* fallthrough */
1394 case xfrd_packet_newlease:
1395 /* nothing more to do */
1396 assert(zone->round_num == -1);
1397 xfrd_udp_release(zone);
1398 break;
1399 case xfrd_packet_notimpl:
1400 xfrd_disable_ixfr(zone);
1401 /* drop packet */
1402 xfrd_udp_release(zone);
1403 /* query next server */
1404 xfrd_make_request(zone);
1405 break;
1406 case xfrd_packet_more:
1407 case xfrd_packet_drop:
1408 /* drop packet */
1409 xfrd_udp_release(zone);
1410 /* query next server */
1411 xfrd_make_request(zone);
1412 break;
1413 case xfrd_packet_bad:
1414 default:
1415 zone->master->bad_xfr_count++;
1416 if (zone->master->bad_xfr_count > 2) {
1417 xfrd_disable_ixfr(zone);
1418 zone->master->bad_xfr_count = 0;
1419 }
1420 /* drop packet */
1421 xfrd_udp_release(zone);
1422 /* query next server */
1423 xfrd_make_request(zone);
1424 break;
1425 }
1426 }
1427
1428 int
1429 xfrd_send_udp(struct acl_options* acl, buffer_type* packet,
1430 struct acl_options* ifc)
1431 {
1432 #ifdef INET6
1433 struct sockaddr_storage to;
1434 #else
1435 struct sockaddr_in to;
1436 #endif /* INET6 */
1437 int fd, family;
1438
1439 /* this will set the remote port to acl->port or TCP_PORT */
1440 socklen_t to_len = xfrd_acl_sockaddr_to(acl, &to);
1441
1442 /* get the address family of the remote host */
1443 if(acl->is_ipv6) {
1444 #ifdef INET6
1445 family = PF_INET6;
1446 #else
1447 return -1;
1448 #endif /* INET6 */
1449 } else {
1450 family = PF_INET;
1451 }
1452
1453 fd = socket(family, SOCK_DGRAM, IPPROTO_UDP);
1454 if(fd == -1) {
1455 log_msg(LOG_ERR, "xfrd: cannot create udp socket to %s: %s",
1456 acl->ip_address_spec, strerror(errno));
1457 return -1;
1458 }
1459
1460 /* bind it */
1461 if (!xfrd_bind_local_interface(fd, ifc, acl, 0)) {
1462 log_msg(LOG_ERR, "xfrd: cannot bind outgoing interface '%s' to "
1463 "udp socket: No matching ip addresses found",
1464 ifc->ip_address_spec);
1465 close(fd);
1466 return -1;
1467 }
1468
1469 /* send it (udp) */
1470 if(sendto(fd,
1471 buffer_current(packet),
1472 buffer_remaining(packet), 0,
1473 (struct sockaddr*)&to, to_len) == -1)
1474 {
1475 log_msg(LOG_ERR, "xfrd: sendto %s failed %s",
1476 acl->ip_address_spec, strerror(errno));
1477 close(fd);
1478 return -1;
1479 }
1480 return fd;
1481 }
1482
1483 int
1484 xfrd_bind_local_interface(int sockd, struct acl_options* ifc,
1485 struct acl_options* acl, int tcp)
1486 {
1487 #ifdef SO_LINGER
1488 struct linger linger = {1, 0};
1489 #endif
1490 socklen_t frm_len;
1491 #ifdef INET6
1492 struct sockaddr_storage frm;
1493 #else
1494 struct sockaddr_in frm;
1495 #endif /* INET6 */
1496 int ret = 1;
1497
1498 if (!ifc) /* no outgoing interface set */
1499 return 1;
1500
1501 while (ifc) {
1502 if (ifc->is_ipv6 != acl->is_ipv6) {
1503 /* check if we have a matching address family */
1504 ifc = ifc->next;
1505 continue;
1506 }
1507
1508 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: bind() %s to %s socket",
1509 ifc->ip_address_spec, tcp? "tcp":"udp"));
1510 ret = 0;
1511 frm_len = xfrd_acl_sockaddr_frm(ifc, &frm);
1512
1513 if (tcp) {
1514 #ifdef SO_REUSEADDR
1515 if (setsockopt(sockd, SOL_SOCKET, SO_REUSEADDR, &frm,
1516 frm_len) < 0) {
1517 VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt "
1518 "SO_REUSEADDR failed: %s", strerror(errno)));
1519 }
1520 #else
1521 VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt SO_REUSEADDR "
1522 "failed: SO_REUSEADDR not defined"));
1523 #endif /* SO_REUSEADDR */
1524
1525 if (ifc->port != 0) {
1526 #ifdef SO_LINGER
1527 if (setsockopt(sockd, SOL_SOCKET, SO_LINGER,
1528 &linger, sizeof(linger)) < 0) {
1529 VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt "
1530 "SO_LINGER failed: %s", strerror(errno)));
1531 }
1532 #else
1533 VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt SO_LINGER "
1534 "failed: SO_LINGER not defined"));
1535 #endif /* SO_LINGER */
1536 }
1537 }
1538
1539 /* found one */
1540 if(bind(sockd, (struct sockaddr*)&frm, frm_len) >= 0) {
1541 DEBUG(DEBUG_XFRD,2, (LOG_INFO, "xfrd: bind() %s to %s "
1542 "socket was successful",
1543 ifc->ip_address_spec, tcp? "tcp":"udp"));
1544 return 1;
1545 }
1546
1547 DEBUG(DEBUG_XFRD,2, (LOG_INFO, "xfrd: bind() %s to %s socket"
1548 "failed: %s",
1549 ifc->ip_address_spec, tcp? "tcp":"udp",
1550 strerror(errno)));
1551
1552 log_msg(LOG_WARNING, "xfrd: could not bind source address:port to "
1553 "socket: %s", strerror(errno));
1554 /* try another */
1555 ifc = ifc->next;
1556 }
1557 return ret;
1558 }
1559
1560 void
1561 xfrd_tsig_sign_request(buffer_type* packet, tsig_record_type* tsig,
1562 struct acl_options* acl)
1563 {
1564 tsig_algorithm_type* algo;
1565 assert(acl->key_options && acl->key_options->tsig_key);
1566 algo = tsig_get_algorithm_by_name(acl->key_options->algorithm);
1567 if(!algo) {
1568 log_msg(LOG_ERR, "tsig unknown algorithm %s",
1569 acl->key_options->algorithm);
1570 return;
1571 }
1572 assert(algo);
1573 tsig_init_record(tsig, algo, acl->key_options->tsig_key);
1574 tsig_init_query(tsig, ID(packet));
1575 tsig_prepare(tsig);
1576 tsig_update(tsig, packet, buffer_position(packet));
1577 tsig_sign(tsig);
1578 tsig_append_rr(tsig, packet);
1579 ARCOUNT_SET(packet, ARCOUNT(packet) + 1);
1580 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "appending tsig to packet"));
1581 /* prepare for validating tsigs */
1582 tsig_prepare(tsig);
1583 }
1584
1585 static int
1586 xfrd_send_ixfr_request_udp(xfrd_zone_type* zone)
1587 {
1588 int fd;
1589
1590 /* make sure we have a master to query the ixfr request to */
1591 assert(zone->master);
1592
1593 if(zone->tcp_conn != -1) {
1594 /* tcp is using the zone_handler.fd */
1595 log_msg(LOG_ERR, "xfrd: %s tried to send udp whilst tcp engaged",
1596 zone->apex_str);
1597 return -1;
1598 }
1599 xfrd_setup_packet(xfrd->packet, TYPE_IXFR, CLASS_IN, zone->apex,
1600 qid_generate());
1601 zone->query_id = ID(xfrd->packet);
1602 /* delete old xfr file? */
1603 if(zone->msg_seq_nr)
1604 xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
1605 zone->msg_seq_nr = 0;
1606 zone->msg_rr_count = 0;
1607 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "sent query with ID %d", zone->query_id));
1608 NSCOUNT_SET(xfrd->packet, 1);
1609 xfrd_write_soa_buffer(xfrd->packet, zone->apex, &zone->soa_disk);
1610 /* if we have tsig keys, sign the ixfr query */
1611 if(zone->master->key_options && zone->master->key_options->tsig_key) {
1612 xfrd_tsig_sign_request(xfrd->packet, &zone->tsig, zone->master);
1613 }
1614 buffer_flip(xfrd->packet);
1615 xfrd_set_timer(zone, XFRD_UDP_TIMEOUT);
1616
1617 if((fd = xfrd_send_udp(zone->master, xfrd->packet,
1618 zone->zone_options->pattern->outgoing_interface)) == -1)
1619 return -1;
1620
1621 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1622 "xfrd sent udp request for ixfr=%u for zone %s to %s",
1623 (unsigned)ntohl(zone->soa_disk.serial),
1624 zone->apex_str, zone->master->ip_address_spec));
1625 return fd;
1626 }
1627
1628 static int xfrd_parse_soa_info(buffer_type* packet, xfrd_soa_type* soa)
1629 {
1630 if(!buffer_available(packet, 10))
1631 return 0;
1632 soa->type = htons(buffer_read_u16(packet));
1633 soa->klass = htons(buffer_read_u16(packet));
1634 soa->ttl = htonl(buffer_read_u32(packet));
1635 if(ntohs(soa->type) != TYPE_SOA || ntohs(soa->klass) != CLASS_IN)
1636 {
1637 return 0;
1638 }
1639
1640 if(!buffer_available(packet, buffer_read_u16(packet)) /* rdata length */ ||
1641 !(soa->prim_ns[0] = dname_make_wire_from_packet(soa->prim_ns+1, packet, 1)) ||
1642 !(soa->email[0] = dname_make_wire_from_packet(soa->email+1, packet, 1)))
1643 {
1644 return 0;
1645 }
1646 soa->rdata_count = 7; /* rdata in SOA */
1647 soa->serial = htonl(buffer_read_u32(packet));
1648 soa->refresh = htonl(buffer_read_u32(packet));
1649 soa->retry = htonl(buffer_read_u32(packet));
1650 soa->expire = htonl(buffer_read_u32(packet));
1651 soa->minimum = htonl(buffer_read_u32(packet));
1652
1653 return 1;
1654 }
1655
1656
1657 /*
1658 * Check the RRs in an IXFR/AXFR reply.
1659 * returns 0 on error, 1 on correct parseable packet.
1660 * done = 1 if the last SOA in an IXFR/AXFR has been seen.
1661 * soa then contains that soa info.
1662 * (soa contents is modified by the routine)
1663 */
1664 static int
1665 xfrd_xfr_check_rrs(xfrd_zone_type* zone, buffer_type* packet, size_t count,
1666 int *done, xfrd_soa_type* soa, region_type* temp)
1667 {
1668 /* first RR has already been checked */
1669 uint32_t tmp_serial = 0;
1670 uint16_t type, rrlen;
1671 size_t i, soapos, mempos;
1672 const dname_type* dname;
1673 domain_table_type* owners;
1674 rdata_atom_type* rdatas;
1675
1676 for(i=0; i<count; ++i,++zone->msg_rr_count)
1677 {
1678 if (*done) {
1679 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr has "
1680 "trailing garbage", zone->apex_str));
1681 return 0;
1682 }
1683 region_free_all(temp);
1684 owners = domain_table_create(temp);
1685 /* check the dname for errors */
1686 dname = dname_make_from_packet(temp, packet, 1, 1);
1687 if(!dname) {
1688 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr unable "
1689 "to parse owner name", zone->apex_str));
1690 return 0;
1691 }
1692 if(!buffer_available(packet, 10)) {
1693 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr hdr "
1694 "too small", zone->apex_str));
1695 return 0;
1696 }
1697 soapos = buffer_position(packet);
1698 type = buffer_read_u16(packet);
1699 (void)buffer_read_u16(packet); /* class */
1700 (void)buffer_read_u32(packet); /* ttl */
1701 rrlen = buffer_read_u16(packet);
1702 if(!buffer_available(packet, rrlen)) {
1703 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr pkt "
1704 "too small", zone->apex_str));
1705 return 0;
1706 }
1707 mempos = buffer_position(packet);
1708 if(rdata_wireformat_to_rdata_atoms(temp, owners, type, rrlen,
1709 packet, &rdatas) == -1) {
1710 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr unable "
1711 "to parse rdata", zone->apex_str));
1712 return 0;
1713 }
1714 if(type == TYPE_SOA) {
1715 /* check the SOAs */
1716 buffer_set_position(packet, soapos);
1717 if(!xfrd_parse_soa_info(packet, soa)) {
1718 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1719 "unable to parse soainfo", zone->apex_str));
1720 return 0;
1721 }
1722 if(zone->msg_rr_count == 1 &&
1723 ntohl(soa->serial) != zone->msg_new_serial) {
1724 /* 2nd RR is SOA with lower serial, this is an IXFR */
1725 zone->msg_is_ixfr = 1;
1726 if(!zone->soa_disk_acquired) {
1727 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1728 "got ixfr but need axfr", zone->apex_str));
1729 return 0; /* got IXFR but need AXFR */
1730 }
1731 if(ntohl(soa->serial) != ntohl(zone->soa_disk.serial)) {
1732 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1733 "bad start serial", zone->apex_str));
1734 return 0; /* bad start serial in IXFR */
1735 }
1736 zone->msg_old_serial = ntohl(soa->serial);
1737 tmp_serial = ntohl(soa->serial);
1738 }
1739 else if(ntohl(soa->serial) == zone->msg_new_serial) {
1740 /* saw another SOA of new serial. */
1741 if(zone->msg_is_ixfr == 1) {
1742 zone->msg_is_ixfr = 2; /* seen middle SOA in ixfr */
1743 } else {
1744 /* 2nd SOA for AXFR or 3rd newSOA for IXFR */
1745 *done = 1;
1746 }
1747 }
1748 else if (zone->msg_is_ixfr) {
1749 /* some additional checks */
1750 if(ntohl(soa->serial) > zone->msg_new_serial) {
1751 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1752 "bad middle serial", zone->apex_str));
1753 return 0; /* bad middle serial in IXFR */
1754 }
1755 if(ntohl(soa->serial) < tmp_serial) {
1756 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1757 "serial decreasing not allowed", zone->apex_str));
1758 return 0; /* middle serial decreases in IXFR */
1759 }
1760 /* serial ok, update tmp serial */
1761 tmp_serial = ntohl(soa->serial);
1762 }
1763 }
1764 buffer_set_position(packet, mempos);
1765 buffer_skip(packet, rrlen);
1766 }
1767 /* packet seems to have a valid DNS RR structure */
1768 return 1;
1769 }
1770
1771 static int
1772 xfrd_xfr_process_tsig(xfrd_zone_type* zone, buffer_type* packet)
1773 {
1774 int have_tsig = 0;
1775 assert(zone && zone->master && zone->master->key_options
1776 && zone->master->key_options->tsig_key && packet);
1777 if(!tsig_find_rr(&zone->tsig, packet)) {
1778 log_msg(LOG_ERR, "xfrd: zone %s, from %s: malformed tsig RR",
1779 zone->apex_str, zone->master->ip_address_spec);
1780 return 0;
1781 }
1782 if(zone->tsig.status == TSIG_OK) {
1783 have_tsig = 1;
1784 if (zone->tsig.error_code != TSIG_ERROR_NOERROR) {
1785 log_msg(LOG_ERR, "xfrd: zone %s, from %s: tsig error "
1786 "(%s)", zone->apex_str,
1787 zone->master->ip_address_spec,
1788 tsig_error(zone->tsig.error_code));
1789 }
1790 }
1791 if(have_tsig) {
1792 /* strip the TSIG resource record off... */
1793 buffer_set_limit(packet, zone->tsig.position);
1794 ARCOUNT_SET(packet, ARCOUNT(packet) - 1);
1795 }
1796
1797 /* keep running the TSIG hash */
1798 tsig_update(&zone->tsig, packet, buffer_limit(packet));
1799 if(have_tsig) {
1800 if (!tsig_verify(&zone->tsig)) {
1801 log_msg(LOG_ERR, "xfrd: zone %s, from %s: bad tsig signature",
1802 zone->apex_str, zone->master->ip_address_spec);
1803 return 0;
1804 }
1805 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s, from %s: good tsig signature",
1806 zone->apex_str, zone->master->ip_address_spec));
1807 /* prepare for next tsigs */
1808 tsig_prepare(&zone->tsig);
1809 }
1810 else if(zone->tsig.updates_since_last_prepare > XFRD_TSIG_MAX_UNSIGNED) {
1811 /* we allow a number of non-tsig signed packets */
1812 log_msg(LOG_INFO, "xfrd: zone %s, from %s: too many consecutive "
1813 "packets without TSIG", zone->apex_str,
1814 zone->master->ip_address_spec);
1815 return 0;
1816 }
1817
1818 if(!have_tsig && zone->msg_seq_nr == 0) {
1819 log_msg(LOG_ERR, "xfrd: zone %s, from %s: no tsig in first packet of reply",
1820 zone->apex_str, zone->master->ip_address_spec);
1821 return 0;
1822 }
1823 return 1;
1824 }
1825
1826 /* parse the received packet. returns xfrd packet result code. */
1827 static enum xfrd_packet_result
1828 xfrd_parse_received_xfr_packet(xfrd_zone_type* zone, buffer_type* packet,
1829 xfrd_soa_type* soa)
1830 {
1831 size_t rr_count;
1832 size_t qdcount = QDCOUNT(packet);
1833 size_t ancount = ANCOUNT(packet), ancount_todo;
1834 size_t nscount = NSCOUNT(packet);
1835 int done = 0;
1836 region_type* tempregion = NULL;
1837
1838 /* has to be axfr / ixfr reply */
1839 if(!buffer_available(packet, QHEADERSZ)) {
1840 log_msg(LOG_INFO, "packet too small");
1841 return xfrd_packet_bad;
1842 }
1843
1844 /* only check ID in first response message. Could also check that
1845 * AA bit and QR bit are set, but not needed.
1846 */
1847 DEBUG(DEBUG_XFRD,2, (LOG_INFO,
1848 "got query with ID %d and %d needed", ID(packet), zone->query_id));
1849 if(ID(packet) != zone->query_id) {
1850 log_msg(LOG_ERR, "xfrd: zone %s received bad query id from %s, "
1851 "dropped",
1852 zone->apex_str, zone->master->ip_address_spec);
1853 return xfrd_packet_bad;
1854 }
1855 /* check RCODE in all response messages */
1856 if(RCODE(packet) != RCODE_OK) {
1857 log_msg(LOG_ERR, "xfrd: zone %s received error code %s from "
1858 "%s",
1859 zone->apex_str, rcode2str(RCODE(packet)),
1860 zone->master->ip_address_spec);
1861 if (RCODE(packet) == RCODE_IMPL ||
1862 RCODE(packet) == RCODE_FORMAT) {
1863 return xfrd_packet_notimpl;
1864 }
1865 if (RCODE(packet) != RCODE_NOTAUTH) {
1866 /* RFC 2845: If NOTAUTH, client should do TSIG checking */
1867 return xfrd_packet_drop;
1868 }
1869 }
1870 /* check TSIG */
1871 if(zone->master->key_options) {
1872 if(!xfrd_xfr_process_tsig(zone, packet)) {
1873 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "dropping xfr reply due "
1874 "to bad TSIG"));
1875 return xfrd_packet_bad;
1876 }
1877 }
1878 if (RCODE(packet) == RCODE_NOTAUTH) {
1879 return xfrd_packet_drop;
1880 }
1881
1882 buffer_skip(packet, QHEADERSZ);
1883
1884 /* skip question section */
1885 for(rr_count = 0; rr_count < qdcount; ++rr_count) {
1886 if (!packet_skip_rr(packet, 1)) {
1887 log_msg(LOG_ERR, "xfrd: zone %s, from %s: bad RR in "
1888 "question section",
1889 zone->apex_str, zone->master->ip_address_spec);
1890 return xfrd_packet_bad;
1891 }
1892 }
1893 if(zone->msg_rr_count == 0 && ancount == 0) {
1894 if(zone->tcp_conn == -1 && TC(packet)) {
1895 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: TC flagged"));
1896 return xfrd_packet_tcp;
1897 }
1898 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: too short xfr packet: no "
1899 "answer"));
1900 /* if IXFR is unknown, fallback to AXFR (if allowed) */
1901 if (nscount == 1) {
1902 if(!packet_skip_dname(packet) || !xfrd_parse_soa_info(packet, soa)) {
1903 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1904 "no SOA begins authority section",
1905 zone->apex_str, zone->master->ip_address_spec));
1906 return xfrd_packet_bad;
1907 }
1908 return xfrd_packet_notimpl;
1909 }
1910 return xfrd_packet_bad;
1911 }
1912 ancount_todo = ancount;
1913
1914 tempregion = region_create(xalloc, free);
1915 if(zone->msg_rr_count == 0) {
1916 const dname_type* soaname = dname_make_from_packet(tempregion,
1917 packet, 1, 1);
1918 if(!soaname) { /* parse failure */
1919 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1920 "parse error in SOA record",
1921 zone->apex_str, zone->master->ip_address_spec));
1922 region_destroy(tempregion);
1923 return xfrd_packet_bad;
1924 }
1925 if(dname_compare(soaname, zone->apex) != 0) { /* wrong name */
1926 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1927 "wrong SOA record",
1928 zone->apex_str, zone->master->ip_address_spec));
1929 region_destroy(tempregion);
1930 return xfrd_packet_bad;
1931 }
1932
1933 /* parse the first RR, see if it is a SOA */
1934 if(!xfrd_parse_soa_info(packet, soa))
1935 {
1936 DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1937 "bad SOA rdata",
1938 zone->apex_str, zone->master->ip_address_spec));
1939 region_destroy(tempregion);
1940 return xfrd_packet_bad;
1941 }
1942 if(zone->soa_disk_acquired != 0 &&
1943 zone->state != xfrd_zone_expired /* if expired - accept anything */ &&
1944 compare_serial(ntohl(soa->serial), ntohl(zone->soa_disk.serial)) < 0) {
1945 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1946 "xfrd: zone %s ignoring old serial from %s",
1947 zone->apex_str, zone->master->ip_address_spec));
1948 VERBOSITY(1, (LOG_INFO,
1949 "xfrd: zone %s ignoring old serial from %s",
1950 zone->apex_str, zone->master->ip_address_spec));
1951 region_destroy(tempregion);
1952 return xfrd_packet_bad;
1953 }
1954 if(zone->soa_disk_acquired != 0 && zone->soa_disk.serial == soa->serial) {
1955 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s got "
1956 "update indicating "
1957 "current serial",
1958 zone->apex_str));
1959 /* (even if notified) the lease on the current soa is renewed */
1960 zone->soa_disk_acquired = xfrd_time();
1961 if(zone->soa_nsd.serial == soa->serial)
1962 zone->soa_nsd_acquired = xfrd_time();
1963 xfrd_set_zone_state(zone, xfrd_zone_ok);
1964 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s is ok",
1965 zone->apex_str));
1966 if(zone->zone_options->pattern->multi_master_check) {
1967 region_destroy(tempregion);
1968 return xfrd_packet_drop;
1969 }
1970 if(zone->soa_notified_acquired == 0) {
1971 /* not notified or anything, so stop asking around */
1972 zone->round_num = -1; /* next try start a new round */
1973 xfrd_set_timer_refresh(zone);
1974 region_destroy(tempregion);
1975 return xfrd_packet_newlease;
1976 }
1977 /* try next master */
1978 region_destroy(tempregion);
1979 return xfrd_packet_drop;
1980 }
1981 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "IXFR reply has ok serial (have \
1982 %u, reply %u).", (unsigned)ntohl(zone->soa_disk.serial), (unsigned)ntohl(soa->serial)));
1983 /* serial is newer than soa_disk */
1984 if(ancount == 1) {
1985 /* single record means it is like a notify */
1986 (void)xfrd_handle_incoming_notify(zone, soa);
1987 }
1988 else if(zone->soa_notified_acquired && zone->soa_notified.serial &&
1989 compare_serial(ntohl(zone->soa_notified.serial), ntohl(soa->serial)) < 0) {
1990 /* this AXFR/IXFR notifies me that an even newer serial exists */
1991 zone->soa_notified.serial = soa->serial;
1992 }
1993 zone->msg_new_serial = ntohl(soa->serial);
1994 zone->msg_rr_count = 1;
1995 zone->msg_is_ixfr = 0;
1996 if(zone->soa_disk_acquired)
1997 zone->msg_old_serial = ntohl(zone->soa_disk.serial);
1998 else zone->msg_old_serial = 0;
1999 ancount_todo = ancount - 1;
2000 }
2001
2002 if(zone->tcp_conn == -1 && TC(packet)) {
2003 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2004 "xfrd: zone %s received TC from %s. retry tcp.",
2005 zone->apex_str, zone->master->ip_address_spec));
2006 region_destroy(tempregion);
2007 return xfrd_packet_tcp;
2008 }
2009
2010 if(zone->tcp_conn == -1 && ancount < 2) {
2011 /* too short to be a real ixfr/axfr data transfer: need at */
2012 /* least two RRs in the answer section. */
2013 /* The serial is newer, so try tcp to this master. */
2014 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: udp reply is short. Try "
2015 "tcp anyway."));
2016 region_destroy(tempregion);
2017 return xfrd_packet_tcp;
2018 }
2019
2020 if(!xfrd_xfr_check_rrs(zone, packet, ancount_todo, &done, soa,
2021 tempregion))
2022 {
2023 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s sent bad xfr "
2024 "reply.", zone->apex_str));
2025 region_destroy(tempregion);
2026 return xfrd_packet_bad;
2027 }
2028 region_destroy(tempregion);
2029 if(zone->tcp_conn == -1 && done == 0) {
2030 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: udp reply incomplete"));
2031 return xfrd_packet_bad;
2032 }
2033 if(done == 0)
2034 return xfrd_packet_more;
2035 if(zone->master->key_options) {
2036 if(zone->tsig.updates_since_last_prepare != 0) {
2037 log_msg(LOG_INFO, "xfrd: last packet of reply has no "
2038 "TSIG");
2039 return xfrd_packet_bad;
2040 }
2041 }
2042 return xfrd_packet_transfer;
2043 }
2044
2045 const char*
2046 xfrd_pretty_time(time_t v)
2047 {
2048 struct tm* tm = localtime(&v);
2049 static char buf[64];
2050 if(!strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", tm))
2051 snprintf(buf, sizeof(buf), "strftime-err-%u", (unsigned)v);
2052 return buf;
2053 }
2054
2055 enum xfrd_packet_result
2056 xfrd_handle_received_xfr_packet(xfrd_zone_type* zone, buffer_type* packet)
2057 {
2058 xfrd_soa_type soa;
2059 enum xfrd_packet_result res;
2060 uint64_t xfrfile_size;
2061
2062 /* parse and check the packet - see if it ends the xfr */
2063 switch((res=xfrd_parse_received_xfr_packet(zone, packet, &soa)))
2064 {
2065 case xfrd_packet_more:
2066 case xfrd_packet_transfer:
2067 /* continue with commit */
2068 break;
2069 case xfrd_packet_newlease:
2070 return xfrd_packet_newlease;
2071 case xfrd_packet_tcp:
2072 return xfrd_packet_tcp;
2073 case xfrd_packet_notimpl:
2074 case xfrd_packet_bad:
2075 case xfrd_packet_drop:
2076 default:
2077 {
2078 /* rollback */
2079 if(zone->msg_seq_nr > 0) {
2080 /* do not process xfr - if only one part simply ignore it. */
2081 /* delete file with previous parts of commit */
2082 xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
2083 VERBOSITY(1, (LOG_INFO, "xfrd: zone %s "
2084 "reverted transfer %u from %s",
2085 zone->apex_str, zone->msg_rr_count?
2086 (int)zone->msg_new_serial:0,
2087 zone->master->ip_address_spec));
2088 zone->msg_seq_nr = 0;
2089 } else if (res == xfrd_packet_bad) {
2090 VERBOSITY(1, (LOG_INFO, "xfrd: zone %s "
2091 "bad transfer %u from %s",
2092 zone->apex_str, zone->msg_rr_count?
2093 (int)zone->msg_new_serial:0,
2094 zone->master->ip_address_spec));
2095 }
2096 if (res == xfrd_packet_notimpl)
2097 return res;
2098 else
2099 return xfrd_packet_bad;
2100 }
2101 }
2102
2103 /* dump reply on disk to diff file */
2104 /* if first part, get new filenumber. Numbers can wrap around, 64bit
2105 * is enough so we do not collide with older-transfers-in-progress */
2106 if(zone->msg_seq_nr == 0)
2107 zone->xfrfilenumber = xfrd->xfrfilenumber++;
2108 diff_write_packet(dname_to_string(zone->apex,0),
2109 zone->zone_options->pattern->pname,
2110 zone->msg_old_serial, zone->msg_new_serial, zone->msg_seq_nr,
2111 buffer_begin(packet), buffer_limit(packet), xfrd->nsd,
2112 zone->xfrfilenumber);
2113 VERBOSITY(3, (LOG_INFO,
2114 "xfrd: zone %s written received XFR packet from %s with serial %u to "
2115 "disk", zone->apex_str, zone->master->ip_address_spec,
2116 (int)zone->msg_new_serial));
2117 zone->msg_seq_nr++;
2118
2119 xfrfile_size = xfrd_get_xfrfile_size(xfrd->nsd, zone->xfrfilenumber);
2120 if( zone->zone_options->pattern->size_limit_xfr != 0 &&
2121 xfrfile_size > zone->zone_options->pattern->size_limit_xfr ) {
2122 /* xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
2123 xfrd_set_reload_timeout(); */
2124 log_msg(LOG_INFO, "xfrd : transferred zone data was too large %llu", (long long unsigned)xfrfile_size);
2125 return xfrd_packet_bad;
2126 }
2127 if(res == xfrd_packet_more) {
2128 /* wait for more */
2129 return xfrd_packet_more;
2130 }
2131
2132 /* done. we are completely sure of this */
2133 buffer_clear(packet);
2134 buffer_printf(packet, "received update to serial %u at %s from %s",
2135 (unsigned)zone->msg_new_serial, xfrd_pretty_time(xfrd_time()),
2136 zone->master->ip_address_spec);
2137 if(zone->master->key_options) {
2138 buffer_printf(packet, " TSIG verified with key %s",
2139 zone->master->key_options->name);
2140 }
2141 buffer_flip(packet);
2142 diff_write_commit(zone->apex_str, zone->msg_old_serial,
2143 zone->msg_new_serial, zone->msg_seq_nr, 1,
2144 (char*)buffer_begin(packet), xfrd->nsd, zone->xfrfilenumber);
2145 VERBOSITY(1, (LOG_INFO, "xfrd: zone %s committed \"%s\"",
2146 zone->apex_str, (char*)buffer_begin(packet)));
2147 /* reset msg seq nr, so if that is nonnull we know xfr file exists */
2148 zone->msg_seq_nr = 0;
2149 /* now put apply_xfr task on the tasklist */
2150 if(!task_new_apply_xfr(xfrd->nsd->task[xfrd->nsd->mytask],
2151 xfrd->last_task, zone->apex, zone->msg_old_serial,
2152 zone->msg_new_serial, zone->xfrfilenumber)) {
2153 /* delete the file and pretend transfer was bad to continue */
2154 xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
2155 xfrd_set_reload_timeout();
2156 return xfrd_packet_bad;
2157 }
2158 /* update the disk serial no. */
2159 zone->soa_disk_acquired = xfrd_time();
2160 zone->soa_disk = soa;
2161 if(zone->soa_notified_acquired && (
2162 zone->soa_notified.serial == 0 ||
2163 compare_serial(htonl(zone->soa_disk.serial),
2164 htonl(zone->soa_notified.serial)) >= 0))
2165 {
2166 zone->soa_notified_acquired = 0;
2167 }
2168 if(!zone->soa_notified_acquired) {
2169 /* do not set expired zone to ok:
2170 * it would cause nsd to start answering
2171 * bad data, since the zone is not loaded yet.
2172 * if nsd does not reload < retry time, more
2173 * queries (for even newer versions) are made.
2174 * For expired zone after reload it is set ok (SOAINFO ipc). */
2175 if(zone->state != xfrd_zone_expired)
2176 xfrd_set_zone_state(zone, xfrd_zone_ok);
2177 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2178 "xfrd: zone %s is waiting for reload",
2179 zone->apex_str));
2180 if(zone->zone_options->pattern->multi_master_check) {
2181 zone->multi_master_update_check = zone->master_num;
2182 xfrd_set_reload_timeout();
2183 return xfrd_packet_transfer;
2184 }
2185 zone->round_num = -1; /* next try start anew */
2186 xfrd_set_timer_refresh(zone);
2187 xfrd_set_reload_timeout();
2188 return xfrd_packet_transfer;
2189 } else {
2190 /* try to get an even newer serial */
2191 /* pretend it was bad to continue queries */
2192 xfrd_set_reload_timeout();
2193 return xfrd_packet_bad;
2194 }
2195 }
2196
2197 static void
2198 xfrd_set_reload_timeout()
2199 {
2200 if(xfrd->nsd->options->xfrd_reload_timeout == -1)
2201 return; /* automatic reload disabled. */
2202 if(xfrd->reload_timeout.tv_sec == 0 ||
2203 xfrd_time() >= (time_t)xfrd->reload_timeout.tv_sec ) {
2204 /* no reload wait period (or it passed), do it right away */
2205 xfrd_set_reload_now(xfrd);
2206 /* start reload wait period */
2207 xfrd->reload_timeout.tv_sec = xfrd_time() +
2208 xfrd->nsd->options->xfrd_reload_timeout;
2209 xfrd->reload_timeout.tv_usec = 0;
2210 return;
2211 }
2212 /* cannot reload now, set that after the timeout a reload has to happen */
2213 if(xfrd->reload_added == 0) {
2214 struct timeval tv;
2215 tv.tv_sec = xfrd->reload_timeout.tv_sec - xfrd_time();
2216 tv.tv_usec = 0;
2217 if(tv.tv_sec > xfrd->nsd->options->xfrd_reload_timeout)
2218 tv.tv_sec = xfrd->nsd->options->xfrd_reload_timeout;
2219 event_set(&xfrd->reload_handler, -1, EV_TIMEOUT,
2220 xfrd_handle_reload, xfrd);
2221 if(event_base_set(xfrd->event_base, &xfrd->reload_handler) != 0)
2222 log_msg(LOG_ERR, "cannot set reload event base");
2223 if(event_add(&xfrd->reload_handler, &tv) != 0)
2224 log_msg(LOG_ERR, "cannot add reload event");
2225 xfrd->reload_added = 1;
2226 }
2227 }
2228
2229 static void
2230 xfrd_handle_reload(int ATTR_UNUSED(fd), short event, void* ATTR_UNUSED(arg))
2231 {
2232 /* reload timeout */
2233 assert(event & EV_TIMEOUT);
2234 (void)event;
2235 /* timeout wait period after this request is sent */
2236 xfrd->reload_added = 0;
2237 xfrd->reload_timeout.tv_sec = xfrd_time() +
2238 xfrd->nsd->options->xfrd_reload_timeout;
2239 xfrd_set_reload_now(xfrd);
2240 }
2241
2242 void
2243 xfrd_handle_notify_and_start_xfr(xfrd_zone_type* zone, xfrd_soa_type* soa)
2244 {
2245 if(xfrd_handle_incoming_notify(zone, soa)) {
2246 if(zone->zone_handler.ev_fd == -1 && zone->tcp_conn == -1 &&
2247 !zone->tcp_waiting && !zone->udp_waiting) {
2248 xfrd_set_refresh_now(zone);
2249 }
2250 /* zones with no content start expbackoff again; this is also
2251 * for nsd-control started transfer commands, and also when
2252 * the master apparently sends notifies (is back up) */
2253 if(zone->soa_disk_acquired == 0)
2254 zone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_START;
2255 }
2256 }
2257
2258 void
2259 xfrd_handle_passed_packet(buffer_type* packet,
2260 int acl_num, int acl_num_xfr)
2261 {
2262 uint8_t qnamebuf[MAXDOMAINLEN];
2263 uint16_t qtype, qclass;
2264 const dname_type* dname;
2265 region_type* tempregion = region_create(xalloc, free);
2266 xfrd_zone_type* zone;
2267
2268 buffer_skip(packet, QHEADERSZ);
2269 if(!packet_read_query_section(packet, qnamebuf, &qtype, &qclass)) {
2270 region_destroy(tempregion);
2271 return; /* drop bad packet */
2272 }
2273
2274 dname = dname_make(tempregion, qnamebuf, 1);
2275 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: got passed packet for %s, acl "
2276 "%d", dname_to_string(dname,0), acl_num));
2277
2278 /* find the zone */
2279 zone = (xfrd_zone_type*)rbtree_search(xfrd->zones, dname);
2280 if(!zone) {
2281 /* this could be because the zone has been deleted meanwhile */
2282 DEBUG(DEBUG_XFRD, 1, (LOG_INFO, "xfrd: incoming packet for "
2283 "unknown zone %s", dname_to_string(dname,0)));
2284 region_destroy(tempregion);
2285 return; /* drop packet for unknown zone */
2286 }
2287 region_destroy(tempregion);
2288
2289 /* handle */
2290 if(OPCODE(packet) == OPCODE_NOTIFY) {
2291 xfrd_soa_type soa;
2292 int have_soa = 0;
2293 int next;
2294 /* get serial from a SOA */
2295 if(ANCOUNT(packet) == 1 && packet_skip_dname(packet) &&
2296 xfrd_parse_soa_info(packet, &soa)) {
2297 have_soa = 1;
2298 }
2299 xfrd_handle_notify_and_start_xfr(zone, have_soa?&soa:NULL);
2300 /* First, see if our notifier has a match in provide-xfr */
2301 if (acl_find_num(zone->zone_options->pattern->request_xfr,
2302 acl_num_xfr))
2303 next = acl_num_xfr;
2304 else /* If not, find master that matches notifiers ACL entry */
2305 next = find_same_master_notify(zone, acl_num);
2306 if(next != -1) {
2307 zone->next_master = next;
2308 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2309 "xfrd: notify set next master to query %d",
2310 next));
2311 }
2312 }
2313 else {
2314 /* ignore other types of messages */
2315 }
2316 }
2317
2318 static int
2319 xfrd_handle_incoming_notify(xfrd_zone_type* zone, xfrd_soa_type* soa)
2320 {
2321 if(soa && zone->soa_disk_acquired && zone->state != xfrd_zone_expired &&
2322 compare_serial(ntohl(soa->serial),ntohl(zone->soa_disk.serial)) <= 0)
2323 {
2324 DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2325 "xfrd: ignored notify %s %u old serial, zone valid "
2326 "(soa disk serial %u)", zone->apex_str,
2327 (unsigned)ntohl(soa->serial),
2328 (unsigned)ntohl(zone->soa_disk.serial)));
2329 return 0; /* ignore notify with old serial, we have a valid zone */
2330 }
2331 if(soa == 0) {
2332 zone->soa_notified.serial = 0;
2333 }
2334 else if (zone->soa_notified_acquired == 0 ||
2335 zone->soa_notified.serial == 0 ||
2336 compare_serial(ntohl(soa->serial),
2337 ntohl(zone->soa_notified.serial)) > 0)
2338 {
2339 zone->soa_notified = *soa;
2340 }
2341 zone->soa_notified_acquired = xfrd_time();
2342 if(zone->state == xfrd_zone_ok) {
2343 xfrd_set_zone_state(zone, xfrd_zone_refreshing);
2344 }
2345 /* transfer right away */
2346 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "Handle incoming notify for zone %s",
2347 zone->apex_str));
2348 return 1;
2349 }
2350
2351 static int
2352 find_same_master_notify(xfrd_zone_type* zone, int acl_num_nfy)
2353 {
2354 struct acl_options* nfy_acl = acl_find_num(zone->zone_options->pattern->
2355 allow_notify, acl_num_nfy);
2356 int num = 0;
2357 struct acl_options* master = zone->zone_options->pattern->request_xfr;
2358 if(!nfy_acl)
2359 return -1;
2360 while(master)
2361 {
2362 if(acl_addr_matches_host(nfy_acl, master))
2363 return num;
2364 master = master->next;
2365 num++;
2366 }
2367 return -1;
2368 }
2369
2370 void
2371 xfrd_check_failed_updates()
2372 {
2373 /* see if updates have not come through */
2374 xfrd_zone_type* zone;
2375 RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
2376 {
2377 /* zone has a disk soa, and no nsd soa or a different nsd soa */
2378 if(zone->soa_disk_acquired != 0 &&
2379 (zone->soa_nsd_acquired == 0 ||
2380 zone->soa_disk.serial != zone->soa_nsd.serial))
2381 {
2382 if(zone->soa_disk_acquired <
2383 xfrd->reload_cmd_last_sent)
2384 {
2385 /* this zone should have been loaded, since its disk
2386 soa time is before the time of the reload cmd. */
2387 xfrd_soa_type dumped_soa = zone->soa_disk;
2388 log_msg(LOG_ERR, "xfrd: zone %s: soa serial %u "
2389 "update failed, restarting "
2390 "transfer (notified zone)",
2391 zone->apex_str, (unsigned)ntohl(zone->soa_disk.serial));
2392 /* revert the soa; it has not been acquired properly */
2393 if(zone->soa_disk_acquired == zone->soa_nsd_acquired) {
2394 /* this was the same as served,
2395 * perform force_axfr , re-download
2396 * same serial from master */
2397 zone->soa_disk_acquired = 0;
2398 zone->soa_nsd_acquired = 0;
2399 } else {
2400 /* revert soa to the one in server */
2401 zone->soa_disk_acquired = zone->soa_nsd_acquired;
2402 zone->soa_disk = zone->soa_nsd;
2403 }
2404 /* pretend we are notified with disk soa.
2405 This will cause a refetch of the data, and reload. */
2406 xfrd_handle_incoming_notify(zone, &dumped_soa);
2407 xfrd_set_timer_refresh(zone);
2408 } else if(zone->soa_disk_acquired >= xfrd->reload_cmd_last_sent) {
2409 /* this zone still has to be loaded,
2410 make sure reload is set to be sent. */
2411 if(xfrd->need_to_send_reload == 0 &&
2412 xfrd->reload_added == 0) {
2413 log_msg(LOG_ERR, "xfrd: zone %s: needs "
2414 "to be loaded. reload lost? "
2415 "try again", zone->apex_str);
2416 xfrd_set_reload_timeout();
2417 }
2418 }
2419 }
2420 }
2421 }
2422
2423 void
2424 xfrd_prepare_zones_for_reload()
2425 {
2426 xfrd_zone_type* zone;
2427 RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
2428 {
2429 /* zone has a disk soa, and no nsd soa or a different nsd soa */
2430 if(zone->soa_disk_acquired != 0 &&
2431 (zone->soa_nsd_acquired == 0 ||
2432 zone->soa_disk.serial != zone->soa_nsd.serial))
2433 {
2434 if(zone->soa_disk_acquired == xfrd_time()) {
2435 /* antedate by one second.
2436 * this makes sure that the zone time is before
2437 * reload, so that check_failed_zones() is
2438 * certain of the result.
2439 */
2440 zone->soa_disk_acquired--;
2441 }
2442 }
2443 }
2444 }
2445
2446 struct buffer*
2447 xfrd_get_temp_buffer()
2448 {
2449 return xfrd->packet;
2450 }
2451
2452 #ifdef BIND8_STATS
2453 /** process stat info task */
2454 static void
2455 xfrd_process_stat_info_task(xfrd_state_type* xfrd, struct task_list_d* task)
2456 {
2457 size_t i;
2458 stc_type* p = (void*)task->zname + sizeof(struct nsdst);
2459 stats_add(&xfrd->nsd->st, (struct nsdst*)task->zname);
2460 for(i=0; i<xfrd->nsd->child_count; i++) {
2461 xfrd->nsd->children[i].query_count += *p++;
2462 }
2463 /* got total, now see if users are interested in these statistics */
2464 #ifdef HAVE_SSL
2465 daemon_remote_process_stats(xfrd->nsd->rc);
2466 #endif
2467 }
2468 #endif /* BIND8_STATS */
2469
2470 #ifdef USE_ZONE_STATS
2471 /** process zonestat inc task */
2472 static void
2473 xfrd_process_zonestat_inc_task(xfrd_state_type* xfrd, struct task_list_d* task)
2474 {
2475 xfrd->zonestat_safe = (unsigned)task->oldserial;
2476 zonestat_remap(xfrd->nsd, 0, xfrd->zonestat_safe*sizeof(struct nsdst));
2477 xfrd->nsd->zonestatsize[0] = xfrd->zonestat_safe;
2478 zonestat_remap(xfrd->nsd, 1, xfrd->zonestat_safe*sizeof(struct nsdst));
2479 xfrd->nsd->zonestatsize[1] = xfrd->zonestat_safe;
2480 }
2481 #endif /* USE_ZONE_STATS */
2482
2483 static void
2484 xfrd_handle_taskresult(xfrd_state_type* xfrd, struct task_list_d* task)
2485 {
2486 #ifndef BIND8_STATS
2487 (void)xfrd;
2488 #endif
2489 switch(task->task_type) {
2490 case task_soa_info:
2491 xfrd_process_soa_info_task(task);
2492 break;
2493 #ifdef BIND8_STATS
2494 case task_stat_info:
2495 xfrd_process_stat_info_task(xfrd, task);
2496 break;
2497 #endif /* BIND8_STATS */
2498 #ifdef USE_ZONE_STATS
2499 case task_zonestat_inc:
2500 xfrd_process_zonestat_inc_task(xfrd, task);
2501 break;
2502 #endif
2503 default:
2504 log_msg(LOG_WARNING, "unhandled task result in xfrd from "
2505 "reload type %d", (int)task->task_type);
2506 }
2507 }
2508
2509 void xfrd_process_task_result(xfrd_state_type* xfrd, struct udb_base* taskudb)
2510 {
2511 udb_ptr t;
2512 /* remap it for usage */
2513 task_remap(taskudb);
2514 /* process the task-results in the taskudb */
2515 udb_ptr_new(&t, taskudb, udb_base_get_userdata(taskudb));
2516 while(!udb_ptr_is_null(&t)) {
2517 xfrd_handle_taskresult(xfrd, TASKLIST(&t));
2518 udb_ptr_set_rptr(&t, taskudb, &TASKLIST(&t)->next);
2519 }
2520 udb_ptr_unlink(&t, taskudb);
2521 /* clear the udb so it can be used by xfrd to make new tasks for
2522 * reload, this happens when the reload signal is sent, and thus
2523 * the taskudbs are swapped */
2524 task_clear(taskudb);
2525 #ifdef HAVE_SYSTEMD
2526 sd_notify(0, "READY=1");
2527 #endif
2528 }
2529
2530 void xfrd_set_reload_now(xfrd_state_type* xfrd)
2531 {
2532 #ifdef HAVE_SYSTEMD
2533 sd_notify(0, "RELOADING=1");
2534 #endif
2535 xfrd->need_to_send_reload = 1;
2536 if(!(xfrd->ipc_handler_flags&EV_WRITE)) {
2537 ipc_xfrd_set_listening(xfrd, EV_PERSIST|EV_READ|EV_WRITE);
2538 }
2539 }
2540
2541 static void
2542 xfrd_handle_write_timer(int ATTR_UNUSED(fd), short event, void* ATTR_UNUSED(arg))
2543 {
2544 /* timeout for write events */
2545 assert(event & EV_TIMEOUT);
2546 (void)event;
2547 if(xfrd->nsd->options->zonefiles_write == 0)
2548 return;
2549 /* call reload to write changed zonefiles */
2550 if(!xfrd->write_zonefile_needed) {
2551 DEBUG(DEBUG_XFRD,2, (LOG_INFO, "zonefiles write timer (nothing)"));
2552 xfrd_write_timer_set();
2553 return;
2554 }
2555 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "zonefiles write timer"));
2556 task_new_write_zonefiles(xfrd->nsd->task[xfrd->nsd->mytask],
2557 xfrd->last_task, NULL);
2558 xfrd_set_reload_now(xfrd);
2559 xfrd->write_zonefile_needed = 0;
2560 xfrd_write_timer_set();
2561 }
2562
2563 static void xfrd_write_timer_set()
2564 {
2565 struct timeval tv;
2566 if(xfrd->nsd->options->zonefiles_write == 0)
2567 return;
2568 tv.tv_sec = xfrd->nsd->options->zonefiles_write;
2569 tv.tv_usec = 0;
2570 event_set(&xfrd->write_timer, -1, EV_TIMEOUT,
2571 xfrd_handle_write_timer, xfrd);
2572 if(event_base_set(xfrd->event_base, &xfrd->write_timer) != 0)
2573 log_msg(LOG_ERR, "xfrd write timer: event_base_set failed");
2574 if(event_add(&xfrd->write_timer, &tv) != 0)
2575 log_msg(LOG_ERR, "xfrd write timer: event_add failed");
2576 }
2577
2578 static void xfrd_handle_child_timer(int ATTR_UNUSED(fd), short event,
2579 void* ATTR_UNUSED(arg))
2580 {
2581 assert(event & EV_TIMEOUT);
2582 (void)event;
2583 /* only used to wakeup the process to reap children, note the
2584 * event is no longer registered */
2585 xfrd->child_timer_added = 0;
2586 }
2587