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