xref: /openbsd/usr.sbin/nsd/xfrd.c (revision 73471bf0)
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_MAX_ROUNDS 1 /* max number of rounds along the masters */
44 #define XFRD_TSIG_MAX_UNSIGNED 103 /* max number of packets without tsig in a tcp stream. */
45 			/* rfc recommends 100, +3 for offbyone errors/interoperability. */
46 #define XFRD_CHILD_REAP_TIMEOUT 60 /* seconds to wakeup and reap lost children */
47 		/* these are reload processes that SIGCHILDed but the signal
48 		 * was lost, and need waitpid to remove their process entry. */
49 
50 /* the daemon state */
51 xfrd_state_type* xfrd = 0;
52 
53 /* main xfrd loop */
54 static void xfrd_main(void);
55 /* shut down xfrd, close sockets. */
56 static void xfrd_shutdown(void);
57 /* delete pending task xfr files in tmp */
58 static void xfrd_clean_pending_tasks(struct nsd* nsd, udb_base* u);
59 /* create zone rbtree at start */
60 static void xfrd_init_zones(void);
61 /* initial handshake with SOAINFO from main and send expire to main */
62 static void xfrd_receive_soa(int socket, int shortsoa);
63 
64 /* handle incoming notification message. soa can be NULL. true if transfer needed. */
65 static int xfrd_handle_incoming_notify(xfrd_zone_type* zone,
66 	xfrd_soa_type* soa);
67 
68 /* call with buffer just after the soa dname. returns 0 on error. */
69 static int xfrd_parse_soa_info(buffer_type* packet, xfrd_soa_type* soa);
70 /* set the zone state to a new state (takes care of expiry messages) */
71 static void xfrd_set_zone_state(xfrd_zone_type* zone,
72 	enum xfrd_zone_state new_zone_state);
73 /* set timer for retry amount (depends on zone_state) */
74 static void xfrd_set_timer_retry(xfrd_zone_type* zone);
75 /* set timer for refresh timeout (depends on zone_state) */
76 static void xfrd_set_timer_refresh(xfrd_zone_type* zone);
77 
78 /* set reload timeout */
79 static void xfrd_set_reload_timeout(void);
80 /* handle reload timeout */
81 static void xfrd_handle_reload(int fd, short event, void* arg);
82 /* handle child timeout */
83 static void xfrd_handle_child_timer(int fd, short event, void* arg);
84 
85 /* send ixfr request, returns fd of connection to read on */
86 static int xfrd_send_ixfr_request_udp(xfrd_zone_type* zone);
87 /* obtain udp socket slot */
88 static void xfrd_udp_obtain(xfrd_zone_type* zone);
89 
90 /* read data via udp */
91 static void xfrd_udp_read(xfrd_zone_type* zone);
92 
93 /* find master by notify number */
94 static int find_same_master_notify(xfrd_zone_type* zone, int acl_num_nfy);
95 
96 /* set the write timer to activate */
97 static void xfrd_write_timer_set(void);
98 
99 static void
100 xfrd_signal_callback(int sig, short event, void* ATTR_UNUSED(arg))
101 {
102 	if(!(event & EV_SIGNAL))
103 		return;
104 	sig_handler(sig);
105 }
106 
107 static struct event* xfrd_sig_evs[10];
108 static int xfrd_sig_num = 0;
109 
110 static void
111 xfrd_sigsetup(int sig)
112 {
113 	struct event *ev = xalloc_zero(sizeof(*ev));
114 	assert(xfrd_sig_num <= (int)(sizeof(xfrd_sig_evs)/sizeof(ev)));
115 	xfrd_sig_evs[xfrd_sig_num++] = ev;
116 	signal_set(ev, sig, xfrd_signal_callback, NULL);
117 	if(event_base_set(xfrd->event_base, ev) != 0) {
118 		log_msg(LOG_ERR, "xfrd sig handler: event_base_set failed");
119 	}
120 	if(signal_add(ev, NULL) != 0) {
121 		log_msg(LOG_ERR, "xfrd sig handler: signal_add failed");
122 	}
123 }
124 
125 void
126 xfrd_init(int socket, struct nsd* nsd, int shortsoa, int reload_active,
127 	pid_t nsd_pid)
128 {
129 	region_type* region;
130 
131 	assert(xfrd == 0);
132 	/* to setup signalhandling */
133 	nsd->server_kind = NSD_SERVER_MAIN;
134 
135 	region = region_create_custom(xalloc, free, DEFAULT_CHUNK_SIZE,
136 		DEFAULT_LARGE_OBJECT_SIZE, DEFAULT_INITIAL_CLEANUP_SIZE, 1);
137 	xfrd = (xfrd_state_type*)region_alloc(region, sizeof(xfrd_state_type));
138 	memset(xfrd, 0, sizeof(xfrd_state_type));
139 	xfrd->region = region;
140 	xfrd->xfrd_start_time = time(0);
141 	xfrd->event_base = nsd_child_event_base();
142 	if(!xfrd->event_base) {
143 		log_msg(LOG_ERR, "xfrd: cannot create event base");
144 		exit(1);
145 	}
146 	xfrd->nsd = nsd;
147 	xfrd->packet = buffer_create(xfrd->region, QIOBUFSZ);
148 	xfrd->udp_waiting_first = NULL;
149 	xfrd->udp_waiting_last = NULL;
150 	xfrd->udp_use_num = 0;
151 	xfrd->got_time = 0;
152 	xfrd->xfrfilenumber = 0;
153 #ifdef USE_ZONE_STATS
154 	xfrd->zonestat_safe = nsd->zonestatdesired;
155 #endif
156 	xfrd->activated_first = NULL;
157 	xfrd->ipc_pass = buffer_create(xfrd->region, QIOBUFSZ);
158 	xfrd->last_task = region_alloc(xfrd->region, sizeof(*xfrd->last_task));
159 	udb_ptr_init(xfrd->last_task, xfrd->nsd->task[xfrd->nsd->mytask]);
160 	assert(shortsoa || udb_base_get_userdata(xfrd->nsd->task[xfrd->nsd->mytask])->data == 0);
161 
162 	xfrd->reload_handler.ev_fd = -1;
163 	xfrd->reload_added = 0;
164 	xfrd->reload_timeout.tv_sec = 0;
165 	xfrd->reload_cmd_last_sent = xfrd->xfrd_start_time;
166 	xfrd->can_send_reload = !reload_active;
167 	xfrd->reload_pid = nsd_pid;
168 	xfrd->child_timer_added = 0;
169 
170 	xfrd->ipc_send_blocked = 0;
171 	memset(&xfrd->ipc_handler, 0, sizeof(xfrd->ipc_handler));
172 	event_set(&xfrd->ipc_handler, socket, EV_PERSIST|EV_READ,
173 		xfrd_handle_ipc, xfrd);
174 	if(event_base_set(xfrd->event_base, &xfrd->ipc_handler) != 0)
175 		log_msg(LOG_ERR, "xfrd ipc handler: event_base_set failed");
176 	if(event_add(&xfrd->ipc_handler, NULL) != 0)
177 		log_msg(LOG_ERR, "xfrd ipc handler: event_add failed");
178 	xfrd->ipc_handler_flags = EV_PERSIST|EV_READ;
179 	xfrd->ipc_conn = xfrd_tcp_create(xfrd->region, QIOBUFSZ);
180 	/* not reading using ipc_conn yet */
181 	xfrd->ipc_conn->is_reading = 0;
182 	xfrd->ipc_conn->fd = socket;
183 	xfrd->need_to_send_reload = 0;
184 	xfrd->need_to_send_shutdown = 0;
185 	xfrd->need_to_send_stats = 0;
186 
187 	xfrd->write_zonefile_needed = 0;
188 	if(nsd->options->zonefiles_write)
189 		xfrd_write_timer_set();
190 
191 	xfrd->notify_waiting_first = NULL;
192 	xfrd->notify_waiting_last = NULL;
193 	xfrd->notify_udp_num = 0;
194 
195 #ifdef HAVE_SSL
196 	daemon_remote_attach(xfrd->nsd->rc, xfrd);
197 #endif
198 
199 	xfrd->tcp_set = xfrd_tcp_set_create(xfrd->region, nsd->options->tls_cert_bundle);
200 	xfrd->tcp_set->tcp_timeout = nsd->tcp_timeout;
201 #if !defined(HAVE_ARC4RANDOM) && !defined(HAVE_GETRANDOM)
202 	srandom((unsigned long) getpid() * (unsigned long) time(NULL));
203 #endif
204 
205 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd pre-startup"));
206 	xfrd_init_zones();
207 	xfrd_receive_soa(socket, shortsoa);
208 	if(nsd->options->xfrdfile != NULL && nsd->options->xfrdfile[0]!=0)
209 		xfrd_read_state(xfrd);
210 
211 	/* did we get killed before startup was successful? */
212 	if(nsd->signal_hint_shutdown) {
213 		kill(nsd_pid, SIGTERM);
214 		xfrd_shutdown();
215 		return;
216 	}
217 
218 	/* init libevent signals now, so that in the previous init scripts
219 	 * the normal sighandler is called, and can set nsd->signal_hint..
220 	 * these are also looked at in sig_process before we run the main loop*/
221 	xfrd_sigsetup(SIGHUP);
222 	xfrd_sigsetup(SIGTERM);
223 	xfrd_sigsetup(SIGQUIT);
224 	xfrd_sigsetup(SIGCHLD);
225 	xfrd_sigsetup(SIGALRM);
226 	xfrd_sigsetup(SIGILL);
227 	xfrd_sigsetup(SIGUSR1);
228 	xfrd_sigsetup(SIGINT);
229 
230 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd startup"));
231 #ifdef HAVE_SYSTEMD
232 	sd_notify(0, "READY=1");
233 #endif
234 	xfrd_main();
235 }
236 
237 static void
238 xfrd_process_activated(void)
239 {
240 	xfrd_zone_type* zone;
241 	while((zone = xfrd->activated_first)) {
242 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s activation",
243 			zone->apex_str));
244 		/* pop zone from activated list */
245 		xfrd->activated_first = zone->activated_next;
246 		if(zone->activated_next)
247 			zone->activated_next->activated_prev = NULL;
248 		zone->is_activated = 0;
249 		/* run it : no events, specifically not the TIMEOUT event,
250 		 * so that running zone transfers are not interrupted */
251 		xfrd_handle_zone(zone->zone_handler.ev_fd, 0, zone);
252 	}
253 }
254 
255 static void
256 xfrd_sig_process(void)
257 {
258 	int status;
259 	pid_t child_pid;
260 
261 	if(xfrd->nsd->signal_hint_quit || xfrd->nsd->signal_hint_shutdown) {
262 		xfrd->nsd->signal_hint_quit = 0;
263 		xfrd->nsd->signal_hint_shutdown = 0;
264 		xfrd->need_to_send_shutdown = 1;
265 		if(!(xfrd->ipc_handler_flags&EV_WRITE)) {
266 			ipc_xfrd_set_listening(xfrd, EV_PERSIST|EV_READ|EV_WRITE);
267 		}
268 	} else if(xfrd->nsd->signal_hint_reload_hup) {
269 		log_msg(LOG_WARNING, "SIGHUP received, reloading...");
270 		xfrd->nsd->signal_hint_reload_hup = 0;
271 		if(xfrd->nsd->options->zonefiles_check) {
272 			task_new_check_zonefiles(xfrd->nsd->task[
273 				xfrd->nsd->mytask], xfrd->last_task, NULL);
274 		}
275 		xfrd_set_reload_now(xfrd);
276 	} else if(xfrd->nsd->signal_hint_statsusr) {
277 		xfrd->nsd->signal_hint_statsusr = 0;
278 		xfrd->need_to_send_stats = 1;
279 		if(!(xfrd->ipc_handler_flags&EV_WRITE)) {
280 			ipc_xfrd_set_listening(xfrd, EV_PERSIST|EV_READ|EV_WRITE);
281 		}
282 	}
283 
284 	/* collect children that exited. */
285 	xfrd->nsd->signal_hint_child = 0;
286 	while((child_pid = waitpid(-1, &status, WNOHANG)) != -1 && child_pid != 0) {
287 		if(status != 0) {
288 			log_msg(LOG_ERR, "process %d exited with status %d",
289 				(int)child_pid, status);
290 		}
291 	}
292 	if(!xfrd->child_timer_added) {
293 		struct timeval tv;
294 		tv.tv_sec = XFRD_CHILD_REAP_TIMEOUT;
295 		tv.tv_usec = 0;
296 		memset(&xfrd->child_timer, 0, sizeof(xfrd->child_timer));
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 	if (xfrd->nsd->tls_ctx)
404 		SSL_CTX_free(xfrd->nsd->tls_ctx);
405 #  ifdef HAVE_TLS_1_3
406 	if (xfrd->tcp_set->ssl_ctx)
407 		SSL_CTX_free(xfrd->tcp_set->ssl_ctx);
408 #  endif
409 #endif
410 #ifdef USE_DNSTAP
411 	dt_collector_close(nsd.dt_collector, &nsd);
412 #endif
413 
414 	/* process-exit cleans up memory used by xfrd process */
415 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd shutdown complete"));
416 #ifdef MEMCLEAN /* OS collects memory pages */
417 	if(xfrd->zones) {
418 		xfrd_zone_type* z;
419 		RBTREE_FOR(z, xfrd_zone_type*, xfrd->zones) {
420 			tsig_delete_record(&z->tsig, NULL);
421 		}
422 	}
423 	if(xfrd->notify_zones) {
424 		struct notify_zone* n;
425 		RBTREE_FOR(n, struct notify_zone*, xfrd->notify_zones) {
426 			tsig_delete_record(&n->notify_tsig, NULL);
427 		}
428 	}
429 	if(xfrd_sig_num > 0) {
430 		int i;
431 		for(i=0; i<xfrd_sig_num; i++) {
432 			signal_del(xfrd_sig_evs[i]);
433 			free(xfrd_sig_evs[i]);
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 	int num, num_xot;
473 	xfrd_zone_type *xzone;
474 	xzone = (xfrd_zone_type*)region_alloc(xfrd->region,
475 		sizeof(xfrd_zone_type));
476 	memset(xzone, 0, sizeof(xfrd_zone_type));
477 	xzone->apex = zone_opt->node.key;
478 	xzone->apex_str = zone_opt->name;
479 	xzone->state = xfrd_zone_refreshing;
480 	xzone->zone_options = zone_opt;
481 	/* first retry will use first master */
482 	xzone->master = xzone->zone_options->pattern->request_xfr;
483 	xzone->master_num = 0;
484 	xzone->next_master = 0;
485 	xzone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_START;
486 
487 	xzone->soa_nsd_acquired = 0;
488 	xzone->soa_disk_acquired = 0;
489 	xzone->soa_notified_acquired = 0;
490 	/* [0]=1, [1]=0; "." domain name */
491 	xzone->soa_nsd.prim_ns[0] = 1;
492 	xzone->soa_nsd.email[0] = 1;
493 	xzone->soa_disk.prim_ns[0]=1;
494 	xzone->soa_disk.email[0]=1;
495 	xzone->soa_notified.prim_ns[0]=1;
496 	xzone->soa_notified.email[0]=1;
497 
498 	xzone->zone_handler.ev_fd = -1;
499 	xzone->zone_handler_flags = 0;
500 	xzone->event_added = 0;
501 
502 	xzone->tcp_conn = -1;
503 	xzone->tcp_waiting = 0;
504 	xzone->udp_waiting = 0;
505 	xzone->is_activated = 0;
506 
507 	xzone->multi_master_first_master = -1;
508 	xzone->multi_master_update_check = -1;
509 	tsig_create_record_custom(&xzone->tsig, NULL, 0, 0, 4);
510 
511 	/* set refreshing anyway, if we have data it may be old */
512 	xfrd_set_refresh_now(xzone);
513 
514 	/*Check all or none of acls use XoT*/
515 	num = 0;
516 	num_xot = 0;
517 	for (; xzone->master != NULL; xzone->master = xzone->master->next, num++) {
518 		if (xzone->master->tls_auth_options != NULL) num_xot++;
519 	}
520 	if (num_xot != 0 && num != num_xot)
521 		log_msg(LOG_WARNING, "Some but not all request-xfrs for %s have XFR-over-TLS configured",
522 			xzone->apex_str);
523 
524 	xzone->node.key = xzone->apex;
525 	rbtree_insert(xfrd->zones, (rbnode_type*)xzone);
526 }
527 
528 static void
529 xfrd_init_zones()
530 {
531 	struct zone_options *zone_opt;
532 	assert(xfrd->zones == 0);
533 
534 	xfrd->zones = rbtree_create(xfrd->region,
535 		(int (*)(const void *, const void *)) dname_compare);
536 	xfrd->notify_zones = rbtree_create(xfrd->region,
537 		(int (*)(const void *, const void *)) dname_compare);
538 
539 	RBTREE_FOR(zone_opt, struct zone_options*, xfrd->nsd->options->zone_options)
540 	{
541 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: adding %s zone",
542 			zone_opt->name));
543 
544 		init_notify_send(xfrd->notify_zones, xfrd->region, zone_opt);
545 		if(!zone_is_slave(zone_opt)) {
546 			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s, "
547 				"master zone has no outgoing xfr requests",
548 				zone_opt->name));
549 			continue;
550 		}
551 		xfrd_init_slave_zone(xfrd, zone_opt);
552 	}
553 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: started server %d "
554 		"secondary zones", (int)xfrd->zones->count));
555 }
556 
557 static void
558 xfrd_process_soa_info_task(struct task_list_d* task)
559 {
560 	xfrd_soa_type soa;
561 	xfrd_soa_type* soa_ptr = &soa;
562 	xfrd_zone_type* zone;
563 	DEBUG(DEBUG_IPC,1, (LOG_INFO, "xfrd: process SOAINFO %s",
564 		dname_to_string(task->zname, 0)));
565 	zone = (xfrd_zone_type*)rbtree_search(xfrd->zones, task->zname);
566 	if(task->size <= sizeof(struct task_list_d)+dname_total_size(
567 		task->zname)+sizeof(uint32_t)*6 + sizeof(uint8_t)*2) {
568 		/* NSD has zone without any info */
569 		DEBUG(DEBUG_IPC,1, (LOG_INFO, "SOAINFO for %s lost zone",
570 			dname_to_string(task->zname,0)));
571 		soa_ptr = NULL;
572 	} else {
573 		uint8_t* p = (uint8_t*)task->zname + dname_total_size(
574 			task->zname);
575 		/* read the soa info */
576 		memset(&soa, 0, sizeof(soa));
577 		/* left out type, klass, count for speed */
578 		soa.type = htons(TYPE_SOA);
579 		soa.klass = htons(CLASS_IN);
580 		memmove(&soa.ttl, p, sizeof(uint32_t));
581 		p += sizeof(uint32_t);
582 		soa.rdata_count = htons(7);
583 		memmove(soa.prim_ns, p, sizeof(uint8_t));
584 		p += sizeof(uint8_t);
585 		memmove(soa.prim_ns+1, p, soa.prim_ns[0]);
586 		p += soa.prim_ns[0];
587 		memmove(soa.email, p, sizeof(uint8_t));
588 		p += sizeof(uint8_t);
589 		memmove(soa.email+1, p, soa.email[0]);
590 		p += soa.email[0];
591 		memmove(&soa.serial, p, sizeof(uint32_t));
592 		p += sizeof(uint32_t);
593 		memmove(&soa.refresh, p, sizeof(uint32_t));
594 		p += sizeof(uint32_t);
595 		memmove(&soa.retry, p, sizeof(uint32_t));
596 		p += sizeof(uint32_t);
597 		memmove(&soa.expire, p, sizeof(uint32_t));
598 		p += sizeof(uint32_t);
599 		memmove(&soa.minimum, p, sizeof(uint32_t));
600 		/* p += sizeof(uint32_t); if we wanted to read further */
601 		DEBUG(DEBUG_IPC,1, (LOG_INFO, "SOAINFO for %s %u",
602 			dname_to_string(task->zname,0),
603 			(unsigned)ntohl(soa.serial)));
604 	}
605 
606 	if(!zone) {
607 		DEBUG(DEBUG_IPC,1, (LOG_INFO, "xfrd: zone %s master zone updated",
608 			dname_to_string(task->zname,0)));
609 		notify_handle_master_zone_soainfo(xfrd->notify_zones,
610 			task->zname, soa_ptr);
611 		return;
612 	}
613 	xfrd_handle_incoming_soa(zone, soa_ptr, xfrd_time());
614 }
615 
616 static void
617 xfrd_receive_soa(int socket, int shortsoa)
618 {
619 	sig_atomic_t cmd;
620 	struct udb_base* xtask = xfrd->nsd->task[xfrd->nsd->mytask];
621 	udb_ptr last_task, t;
622 	xfrd_zone_type* zone;
623 
624 	if(!shortsoa) {
625 		/* put all expired zones into mytask */
626 		udb_ptr_init(&last_task, xtask);
627 		RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones) {
628 			if(zone->state == xfrd_zone_expired) {
629 				task_new_expire(xtask, &last_task, zone->apex, 1);
630 			}
631 		}
632 		udb_ptr_unlink(&last_task, xtask);
633 
634 		/* send RELOAD to main to give it this tasklist */
635 		task_process_sync(xtask);
636 		cmd = NSD_RELOAD;
637 		if(!write_socket(socket, &cmd,  sizeof(cmd))) {
638 			log_msg(LOG_ERR, "problems sending reload xfrdtomain: %s",
639 				strerror(errno));
640 		}
641 	}
642 
643 	/* receive RELOAD_DONE to get SOAINFO tasklist */
644 	if(block_read(&nsd, socket, &cmd, sizeof(cmd), -1) != sizeof(cmd) ||
645 		cmd != NSD_RELOAD_DONE) {
646 		if(nsd.signal_hint_shutdown)
647 			return;
648 		log_msg(LOG_ERR, "did not get start signal from main");
649 		exit(1);
650 	}
651 	if(block_read(NULL, socket, &xfrd->reload_pid, sizeof(pid_t), -1)
652 		!= sizeof(pid_t)) {
653 		log_msg(LOG_ERR, "xfrd cannot get reload_pid");
654 	}
655 
656 	/* process tasklist (SOAINFO data) */
657 	udb_ptr_unlink(xfrd->last_task, xtask);
658 	/* if shortsoa: then use my own taskdb that nsdparent filled */
659 	if(!shortsoa)
660 		xfrd->nsd->mytask = 1 - xfrd->nsd->mytask;
661 	xtask = xfrd->nsd->task[xfrd->nsd->mytask];
662 	task_remap(xtask);
663 	udb_ptr_new(&t, xtask, udb_base_get_userdata(xtask));
664 	while(!udb_ptr_is_null(&t)) {
665 		xfrd_process_soa_info_task(TASKLIST(&t));
666 	 	udb_ptr_set_rptr(&t, xtask, &TASKLIST(&t)->next);
667 	}
668 	udb_ptr_unlink(&t, xtask);
669 	task_clear(xtask);
670 	udb_ptr_init(xfrd->last_task, xfrd->nsd->task[xfrd->nsd->mytask]);
671 
672 	if(!shortsoa) {
673 		/* receive RELOAD_DONE that signals the other tasklist is
674 		 * empty, and thus xfrd can operate (can call reload and swap
675 		 * to the other, empty, tasklist) */
676 		if(block_read(NULL, socket, &cmd, sizeof(cmd), -1) !=
677 			sizeof(cmd) ||
678 			cmd != NSD_RELOAD_DONE) {
679 			log_msg(LOG_ERR, "did not get start signal 2 from "
680 				"main");
681 			exit(1);
682 		}
683 	} else {
684 		/* for shortsoa version, do expire later */
685 		/* if expire notifications, put in my task and
686 		 * schedule a reload to make sure they are processed */
687 		RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones) {
688 			if(zone->state == xfrd_zone_expired) {
689 				xfrd_send_expire_notification(zone);
690 			}
691 		}
692 	}
693 }
694 
695 void
696 xfrd_reopen_logfile(void)
697 {
698 	if (xfrd->nsd->file_rotation_ok)
699 		log_reopen(xfrd->nsd->log_filename, 0);
700 }
701 
702 void
703 xfrd_deactivate_zone(xfrd_zone_type* z)
704 {
705 	if(z->is_activated) {
706 		/* delete from activated list */
707 		if(z->activated_prev)
708 			z->activated_prev->activated_next = z->activated_next;
709 		else	xfrd->activated_first = z->activated_next;
710 		if(z->activated_next)
711 			z->activated_next->activated_prev = z->activated_prev;
712 		z->is_activated = 0;
713 	}
714 }
715 
716 void
717 xfrd_del_slave_zone(xfrd_state_type* xfrd, const dname_type* dname)
718 {
719 	xfrd_zone_type* z = (xfrd_zone_type*)rbtree_delete(xfrd->zones, dname);
720 	if(!z) return;
721 
722 	/* io */
723 	if(z->tcp_waiting) {
724 		/* delete from tcp waiting list */
725 		if(z->tcp_waiting_prev)
726 			z->tcp_waiting_prev->tcp_waiting_next =
727 				z->tcp_waiting_next;
728 		else xfrd->tcp_set->tcp_waiting_first = z->tcp_waiting_next;
729 		if(z->tcp_waiting_next)
730 			z->tcp_waiting_next->tcp_waiting_prev =
731 				z->tcp_waiting_prev;
732 		else xfrd->tcp_set->tcp_waiting_last = z->tcp_waiting_prev;
733 		z->tcp_waiting = 0;
734 	}
735 	if(z->udp_waiting) {
736 		/* delete from udp waiting list */
737 		if(z->udp_waiting_prev)
738 			z->udp_waiting_prev->udp_waiting_next =
739 				z->udp_waiting_next;
740 		else	xfrd->udp_waiting_first = z->udp_waiting_next;
741 		if(z->udp_waiting_next)
742 			z->udp_waiting_next->udp_waiting_prev =
743 				z->udp_waiting_prev;
744 		else	xfrd->udp_waiting_last = z->udp_waiting_prev;
745 		z->udp_waiting = 0;
746 	}
747 	xfrd_deactivate_zone(z);
748 	if(z->tcp_conn != -1) {
749 		xfrd_tcp_release(xfrd->tcp_set, z);
750 	} else if(z->zone_handler.ev_fd != -1 && z->event_added) {
751 		xfrd_udp_release(z);
752 	} else if(z->event_added)
753 		event_del(&z->zone_handler);
754 	if(z->msg_seq_nr)
755 		xfrd_unlink_xfrfile(xfrd->nsd, z->xfrfilenumber);
756 
757 	/* tsig */
758 	tsig_delete_record(&z->tsig, NULL);
759 
760 	/* z->dname is recycled when the zone_options is removed */
761 	region_recycle(xfrd->region, z, sizeof(*z));
762 }
763 
764 void
765 xfrd_free_namedb(struct nsd* nsd)
766 {
767 	namedb_close_udb(nsd->db);
768 	namedb_close(nsd->db);
769 	nsd->db = 0;
770 }
771 
772 static void
773 xfrd_set_timer_refresh(xfrd_zone_type* zone)
774 {
775 	time_t set_refresh;
776 	time_t set_expire;
777 	time_t set;
778 	if(zone->soa_disk_acquired == 0 || zone->state != xfrd_zone_ok) {
779 		xfrd_set_timer_retry(zone);
780 		return;
781 	}
782 	/* refresh or expire timeout, whichever is earlier */
783 	set_refresh = bound_soa_disk_refresh(zone);
784 	set_expire  = bound_soa_disk_expire(zone);
785 	set = zone->soa_disk_acquired + ( set_refresh < set_expire
786 	                                ? set_refresh : set_expire );
787 
788 	/* set point in time to period for xfrd_set_timer() */
789 	xfrd_set_timer(zone, within_refresh_bounds(zone,
790 		  set > xfrd_time()
791 		? set - xfrd_time() : XFRD_LOWERBOUND_REFRESH));
792 }
793 
794 static void
795 xfrd_set_timer_retry(xfrd_zone_type* zone)
796 {
797 	time_t set_retry;
798 	time_t set_expire;
799 	int mult;
800 	/* perform exponential backoff in all the cases */
801 	if(zone->fresh_xfr_timeout == 0)
802 		zone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_START;
803 	else {
804 		/* exponential backoff - some master data in zones is paid-for
805 		   but non-working, and will not get fixed. */
806 		zone->fresh_xfr_timeout *= 2;
807 		if(zone->fresh_xfr_timeout > XFRD_TRANSFER_TIMEOUT_MAX)
808 			zone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_MAX;
809 	}
810 	/* exponential backoff multiplier, starts at 1, backs off */
811 	mult = zone->fresh_xfr_timeout / XFRD_TRANSFER_TIMEOUT_START;
812 	if(mult == 0) mult = 1;
813 
814 	/* set timer for next retry or expire timeout if earlier. */
815 	if(zone->soa_disk_acquired == 0) {
816 		/* if no information, use reasonable timeout
817 		 * within configured and defined bounds
818 		 */
819 		xfrd_set_timer(zone,
820 			within_retry_bounds(zone, zone->fresh_xfr_timeout
821 				+ random_generate(zone->fresh_xfr_timeout)));
822 		return;
823 	}
824 	/* exponential backoff within configured and defined bounds */
825 	set_retry = within_retry_bounds(zone,
826 			ntohl(zone->soa_disk.retry) * mult);
827 	if(zone->state == xfrd_zone_expired) {
828 		xfrd_set_timer(zone, set_retry);
829 		return;
830 	}
831 	/* retry or expire timeout, whichever is earlier */
832 	set_expire = zone->soa_disk_acquired + bound_soa_disk_expire(zone);
833 	if(xfrd_time() + set_retry < set_expire) {
834 		xfrd_set_timer(zone, set_retry);
835 		return;
836 	}
837 	/* Not expired, but next retry will be > than expire timeout.
838 	 * Retry when the expire timeout runs out.
839 	 * set_expire is below retry upper bounds (if statement above),
840 	 * but not necessarily above lower bounds,
841 	 * so use within_retry_bounds() again.
842 	 */
843 	xfrd_set_timer(zone, within_retry_bounds(zone,
844 		  set_expire > xfrd_time()
845 		? set_expire - xfrd_time() : XFRD_LOWERBOUND_RETRY));
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
895 					+ bound_soa_disk_expire(zone)) {
896 			/* zone expired */
897 			log_msg(LOG_ERR, "xfrd: zone %s has expired", zone->apex_str);
898 			xfrd_set_zone_state(zone, xfrd_zone_expired);
899 		}
900 		else if(zone->state == xfrd_zone_ok &&
901 			xfrd_time() >= zone->soa_disk_acquired
902 			               + bound_soa_disk_refresh(zone)) {
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 			memset(&zone->zone_handler, 0,
1042 				sizeof(zone->zone_handler));
1043 			event_set(&zone->zone_handler, fd,
1044 				EV_PERSIST|EV_READ|EV_TIMEOUT,
1045 				xfrd_handle_zone, zone);
1046 			if(event_base_set(xfrd->event_base, &zone->zone_handler) != 0)
1047 				log_msg(LOG_ERR, "xfrd udp: event_base_set failed");
1048 			if(event_add(&zone->zone_handler, &zone->timeout) != 0)
1049 				log_msg(LOG_ERR, "xfrd udp: event_add failed");
1050 			zone->zone_handler_flags=EV_PERSIST|EV_READ|EV_TIMEOUT;
1051 			zone->event_added = 1;
1052 		}
1053 		return;
1054 	}
1055 	/* queue the zone as last */
1056 	zone->udp_waiting = 1;
1057 	zone->udp_waiting_next = NULL;
1058 	zone->udp_waiting_prev = xfrd->udp_waiting_last;
1059 	if(!xfrd->udp_waiting_first)
1060 		xfrd->udp_waiting_first = zone;
1061 	if(xfrd->udp_waiting_last)
1062 		xfrd->udp_waiting_last->udp_waiting_next = zone;
1063 	xfrd->udp_waiting_last = zone;
1064 	xfrd_unset_timer(zone);
1065 }
1066 
1067 time_t
1068 xfrd_time()
1069 {
1070 	if(!xfrd->got_time) {
1071 		xfrd->current_time = time(0);
1072 		xfrd->got_time = 1;
1073 	}
1074 	return xfrd->current_time;
1075 }
1076 
1077 void
1078 xfrd_copy_soa(xfrd_soa_type* soa, rr_type* rr)
1079 {
1080 	const uint8_t* rr_ns_wire = dname_name(domain_dname(rdata_atom_domain(rr->rdatas[0])));
1081 	uint8_t rr_ns_len = domain_dname(rdata_atom_domain(rr->rdatas[0]))->name_size;
1082 	const uint8_t* rr_em_wire = dname_name(domain_dname(rdata_atom_domain(rr->rdatas[1])));
1083 	uint8_t rr_em_len = domain_dname(rdata_atom_domain(rr->rdatas[1]))->name_size;
1084 
1085 	if(rr->type != TYPE_SOA || rr->rdata_count != 7) {
1086 		log_msg(LOG_ERR, "xfrd: copy_soa called with bad rr, type %d rrs %u.",
1087 			rr->type, rr->rdata_count);
1088 		return;
1089 	}
1090 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: copy_soa rr, type %d rrs %u, ttl %u.",
1091 			(int)rr->type, (unsigned)rr->rdata_count, (unsigned)rr->ttl));
1092 	soa->type = htons(rr->type);
1093 	soa->klass = htons(rr->klass);
1094 	soa->ttl = htonl(rr->ttl);
1095 	soa->rdata_count = htons(rr->rdata_count);
1096 
1097 	/* copy dnames */
1098 	soa->prim_ns[0] = rr_ns_len;
1099 	memcpy(soa->prim_ns+1, rr_ns_wire, rr_ns_len);
1100 	soa->email[0] = rr_em_len;
1101 	memcpy(soa->email+1, rr_em_wire, rr_em_len);
1102 
1103 	/* already in network format */
1104 	memcpy(&soa->serial, rdata_atom_data(rr->rdatas[2]), sizeof(uint32_t));
1105 	memcpy(&soa->refresh, rdata_atom_data(rr->rdatas[3]), sizeof(uint32_t));
1106 	memcpy(&soa->retry, rdata_atom_data(rr->rdatas[4]), sizeof(uint32_t));
1107 	memcpy(&soa->expire, rdata_atom_data(rr->rdatas[5]), sizeof(uint32_t));
1108 	memcpy(&soa->minimum, rdata_atom_data(rr->rdatas[6]), sizeof(uint32_t));
1109 	DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1110 		"xfrd: copy_soa rr, serial %u refresh %u retry %u expire %u",
1111 		(unsigned)ntohl(soa->serial), (unsigned)ntohl(soa->refresh),
1112 		(unsigned)ntohl(soa->retry), (unsigned)ntohl(soa->expire)));
1113 }
1114 
1115 static void
1116 xfrd_set_zone_state(xfrd_zone_type* zone, enum xfrd_zone_state s)
1117 {
1118 	if(s != zone->state) {
1119 		enum xfrd_zone_state old = zone->state;
1120 		zone->state = s;
1121 		if((s == xfrd_zone_expired || old == xfrd_zone_expired)
1122 			&& s!=old) {
1123 			xfrd_send_expire_notification(zone);
1124 		}
1125 	}
1126 }
1127 
1128 void
1129 xfrd_set_refresh_now(xfrd_zone_type* zone)
1130 {
1131 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s is activated, state %d",
1132 		zone->apex_str, zone->state));
1133 	if(!zone->is_activated) {
1134 		/* push onto list */
1135 		zone->activated_prev = 0;
1136 		zone->activated_next = xfrd->activated_first;
1137 		if(xfrd->activated_first)
1138 			xfrd->activated_first->activated_prev = zone;
1139 		xfrd->activated_first = zone;
1140 		zone->is_activated = 1;
1141 	}
1142 }
1143 
1144 void
1145 xfrd_unset_timer(xfrd_zone_type* zone)
1146 {
1147 	assert(zone->zone_handler.ev_fd == -1);
1148 	if(zone->event_added)
1149 		event_del(&zone->zone_handler);
1150 	zone->zone_handler_flags = 0;
1151 	zone->event_added = 0;
1152 }
1153 
1154 void
1155 xfrd_set_timer(xfrd_zone_type* zone, time_t t)
1156 {
1157 	int fd = zone->zone_handler.ev_fd;
1158 	int fl = ((fd == -1)?EV_TIMEOUT:zone->zone_handler_flags);
1159 	if(t > XFRD_TRANSFER_TIMEOUT_MAX)
1160 		t = XFRD_TRANSFER_TIMEOUT_MAX;
1161 	/* randomize the time, within 90%-100% of original */
1162 	/* not later so zones cannot expire too late */
1163 	/* only for times far in the future */
1164 	if(t > 10) {
1165 		time_t base = t*9/10;
1166 		t = base + random_generate(t-base);
1167 	}
1168 
1169 	/* keep existing flags and fd, but re-add with timeout */
1170 	if(zone->event_added)
1171 		event_del(&zone->zone_handler);
1172 	else	fd = -1;
1173 	zone->timeout.tv_sec = t;
1174 	zone->timeout.tv_usec = 0;
1175 	memset(&zone->zone_handler, 0, sizeof(zone->zone_handler));
1176 	event_set(&zone->zone_handler, fd, fl, xfrd_handle_zone, zone);
1177 	if(event_base_set(xfrd->event_base, &zone->zone_handler) != 0)
1178 		log_msg(LOG_ERR, "xfrd timer: event_base_set failed");
1179 	if(event_add(&zone->zone_handler, &zone->timeout) != 0)
1180 		log_msg(LOG_ERR, "xfrd timer: event_add failed");
1181 	zone->zone_handler_flags = fl;
1182 	zone->event_added = 1;
1183 }
1184 
1185 void
1186 xfrd_handle_incoming_soa(xfrd_zone_type* zone,
1187 	xfrd_soa_type* soa, time_t acquired)
1188 {
1189 	time_t seconds_since_acquired;
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 		seconds_since_acquired =
1212 			  xfrd_time() > zone->soa_disk_acquired
1213 			? xfrd_time() - zone->soa_disk_acquired : 0;
1214 		if(seconds_since_acquired < bound_soa_disk_refresh(zone))
1215 		{
1216 			/* zone ok, wait for refresh time */
1217 			xfrd_set_zone_state(zone, xfrd_zone_ok);
1218 			zone->round_num = -1;
1219 			xfrd_set_timer_refresh(zone);
1220 		} else if(seconds_since_acquired < bound_soa_disk_expire(zone))
1221 		{
1222 			/* zone refreshing */
1223 			xfrd_set_zone_state(zone, xfrd_zone_refreshing);
1224 			xfrd_set_refresh_now(zone);
1225 		}
1226 		if(seconds_since_acquired >= bound_soa_disk_expire(zone)) {
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 					memset(&wz->zone_handler, 0,
1328 						sizeof(wz->zone_handler));
1329 					event_set(&wz->zone_handler, fd,
1330 						EV_READ|EV_TIMEOUT|EV_PERSIST,
1331 						xfrd_handle_zone, wz);
1332 					if(event_base_set(xfrd->event_base,
1333 						&wz->zone_handler) != 0)
1334 						log_msg(LOG_ERR, "cannot set event_base for ixfr");
1335 					if(event_add(&wz->zone_handler, &wz->timeout) != 0)
1336 						log_msg(LOG_ERR, "cannot add event for ixfr");
1337 					wz->zone_handler_flags = EV_READ|EV_TIMEOUT|EV_PERSIST;
1338 					wz->event_added = 1;
1339 					return;
1340 				} else {
1341 					/* make this zone do something with
1342 					 * this failure to act */
1343 					xfrd_set_refresh_now(wz);
1344 				}
1345 			}
1346 		}
1347 	}
1348 	/* no waiting zones */
1349 	if(xfrd->udp_use_num > 0)
1350 		xfrd->udp_use_num--;
1351 }
1352 
1353 /** disable ixfr for master */
1354 void
1355 xfrd_disable_ixfr(xfrd_zone_type* zone)
1356 {
1357 	if(!(zone->master->ixfr_disabled &&
1358 		(zone->master->ixfr_disabled + XFRD_NO_IXFR_CACHE) <= time(NULL))) {
1359 		/* start new round, with IXFR disabled */
1360 		zone->round_num = 0;
1361 		zone->next_master = zone->master_num;
1362 	}
1363 	zone->master->ixfr_disabled = time(NULL);
1364 }
1365 
1366 static void
1367 xfrd_udp_read(xfrd_zone_type* zone)
1368 {
1369 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s read udp data", zone->apex_str));
1370 	if(!xfrd_udp_read_packet(xfrd->packet, zone->zone_handler.ev_fd,
1371 		NULL, NULL)) {
1372 		zone->master->bad_xfr_count++;
1373 		if (zone->master->bad_xfr_count > 2) {
1374 			xfrd_disable_ixfr(zone);
1375 			zone->master->bad_xfr_count = 0;
1376 		}
1377 		/* drop packet */
1378 		xfrd_udp_release(zone);
1379 		/* query next server */
1380 		xfrd_make_request(zone);
1381 		return;
1382 	}
1383 	switch(xfrd_handle_received_xfr_packet(zone, xfrd->packet)) {
1384 		case xfrd_packet_tcp:
1385 			xfrd_set_timer(zone, xfrd->tcp_set->tcp_timeout);
1386 			xfrd_udp_release(zone);
1387 			xfrd_tcp_obtain(xfrd->tcp_set, zone);
1388 			break;
1389 		case xfrd_packet_transfer:
1390 			if(zone->zone_options->pattern->multi_master_check) {
1391 				xfrd_udp_release(zone);
1392 				xfrd_make_request(zone);
1393 				break;
1394 			}
1395 			/* fallthrough */
1396 		case xfrd_packet_newlease:
1397 			/* nothing more to do */
1398 			assert(zone->round_num == -1);
1399 			xfrd_udp_release(zone);
1400 			break;
1401 		case xfrd_packet_notimpl:
1402 			xfrd_disable_ixfr(zone);
1403 			/* drop packet */
1404 			xfrd_udp_release(zone);
1405 			/* query next server */
1406 			xfrd_make_request(zone);
1407 			break;
1408 		case xfrd_packet_more:
1409 		case xfrd_packet_drop:
1410 			/* drop packet */
1411 			xfrd_udp_release(zone);
1412 			/* query next server */
1413 			xfrd_make_request(zone);
1414 			break;
1415 		case xfrd_packet_bad:
1416 		default:
1417 			zone->master->bad_xfr_count++;
1418 			if (zone->master->bad_xfr_count > 2) {
1419 				xfrd_disable_ixfr(zone);
1420 				zone->master->bad_xfr_count = 0;
1421 			}
1422 			/* drop packet */
1423 			xfrd_udp_release(zone);
1424 			/* query next server */
1425 			xfrd_make_request(zone);
1426 			break;
1427 	}
1428 }
1429 
1430 int
1431 xfrd_send_udp(struct acl_options* acl, buffer_type* packet,
1432 	struct acl_options* ifc)
1433 {
1434 #ifdef INET6
1435 	struct sockaddr_storage to;
1436 #else
1437 	struct sockaddr_in to;
1438 #endif /* INET6 */
1439 	int fd, family;
1440 
1441 	/* this will set the remote port to acl->port or TCP_PORT */
1442 	socklen_t to_len = xfrd_acl_sockaddr_to(acl, &to);
1443 
1444 	/* get the address family of the remote host */
1445 	if(acl->is_ipv6) {
1446 #ifdef INET6
1447 		family = PF_INET6;
1448 #else
1449 		return -1;
1450 #endif /* INET6 */
1451 	} else {
1452 		family = PF_INET;
1453 	}
1454 
1455 	fd = socket(family, SOCK_DGRAM, IPPROTO_UDP);
1456 	if(fd == -1) {
1457 		log_msg(LOG_ERR, "xfrd: cannot create udp socket to %s: %s",
1458 			acl->ip_address_spec, strerror(errno));
1459 		return -1;
1460 	}
1461 
1462 	/* bind it */
1463 	if (!xfrd_bind_local_interface(fd, ifc, acl, 0)) {
1464 		log_msg(LOG_ERR, "xfrd: cannot bind outgoing interface '%s' to "
1465 				 "udp socket: No matching ip addresses found",
1466 			ifc->ip_address_spec);
1467 		close(fd);
1468 		return -1;
1469 	}
1470 
1471 	/* send it (udp) */
1472 	if(sendto(fd,
1473 		buffer_current(packet),
1474 		buffer_remaining(packet), 0,
1475 		(struct sockaddr*)&to, to_len) == -1)
1476 	{
1477 		log_msg(LOG_ERR, "xfrd: sendto %s failed %s",
1478 			acl->ip_address_spec, strerror(errno));
1479 		close(fd);
1480 		return -1;
1481 	}
1482 	return fd;
1483 }
1484 
1485 int
1486 xfrd_bind_local_interface(int sockd, struct acl_options* ifc,
1487 	struct acl_options* acl, int tcp)
1488 {
1489 #ifdef SO_LINGER
1490 	struct linger linger = {1, 0};
1491 #endif
1492 	socklen_t frm_len;
1493 #ifdef INET6
1494 	struct sockaddr_storage frm;
1495 #else
1496 	struct sockaddr_in frm;
1497 #endif /* INET6 */
1498 	int ret = 1;
1499 
1500 	if (!ifc) /* no outgoing interface set */
1501 		return 1;
1502 
1503 	while (ifc) {
1504 		if (ifc->is_ipv6 != acl->is_ipv6) {
1505 			/* check if we have a matching address family */
1506 			ifc = ifc->next;
1507 			continue;
1508 		}
1509 
1510 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: bind() %s to %s socket",
1511 			ifc->ip_address_spec, tcp? "tcp":"udp"));
1512 		ret = 0;
1513 		frm_len = xfrd_acl_sockaddr_frm(ifc, &frm);
1514 
1515 		if (tcp) {
1516 #ifdef SO_REUSEADDR
1517 			if (setsockopt(sockd, SOL_SOCKET, SO_REUSEADDR, &frm,
1518 				frm_len) < 0) {
1519 				VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt "
1520 			     "SO_REUSEADDR failed: %s", strerror(errno)));
1521 			}
1522 #else
1523 			VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt SO_REUSEADDR "
1524 			     "failed: SO_REUSEADDR not defined"));
1525 #endif /* SO_REUSEADDR */
1526 
1527 			if (ifc->port != 0) {
1528 #ifdef SO_LINGER
1529 				if (setsockopt(sockd, SOL_SOCKET, SO_LINGER,
1530 					&linger, sizeof(linger)) < 0) {
1531 					VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt "
1532 				     "SO_LINGER failed: %s", strerror(errno)));
1533 				}
1534 #else
1535 				VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt SO_LINGER "
1536 					"failed: SO_LINGER not defined"));
1537 #endif /* SO_LINGER */
1538 			}
1539 		}
1540 
1541 		/* found one */
1542 		if(bind(sockd, (struct sockaddr*)&frm, frm_len) >= 0) {
1543 			DEBUG(DEBUG_XFRD,2, (LOG_INFO, "xfrd: bind() %s to %s "
1544 						       "socket was successful",
1545 			ifc->ip_address_spec, tcp? "tcp":"udp"));
1546 			return 1;
1547 		}
1548 
1549 		DEBUG(DEBUG_XFRD,2, (LOG_INFO, "xfrd: bind() %s to %s socket"
1550 					       "failed: %s",
1551 			ifc->ip_address_spec, tcp? "tcp":"udp",
1552 			strerror(errno)));
1553 
1554 		log_msg(LOG_WARNING, "xfrd: could not bind source address:port to "
1555 		     "socket: %s", strerror(errno));
1556 		/* try another */
1557 		ifc = ifc->next;
1558 	}
1559 	return ret;
1560 }
1561 
1562 void
1563 xfrd_tsig_sign_request(buffer_type* packet, tsig_record_type* tsig,
1564 	struct acl_options* acl)
1565 {
1566 	tsig_algorithm_type* algo;
1567 	assert(acl->key_options && acl->key_options->tsig_key);
1568 	algo = tsig_get_algorithm_by_name(acl->key_options->algorithm);
1569 	if(!algo) {
1570 		log_msg(LOG_ERR, "tsig unknown algorithm %s",
1571 			acl->key_options->algorithm);
1572 		return;
1573 	}
1574 	assert(algo);
1575 	tsig_init_record(tsig, algo, acl->key_options->tsig_key);
1576 	tsig_init_query(tsig, ID(packet));
1577 	tsig_prepare(tsig);
1578 	tsig_update(tsig, packet, buffer_position(packet));
1579 	tsig_sign(tsig);
1580 	tsig_append_rr(tsig, packet);
1581 	ARCOUNT_SET(packet, ARCOUNT(packet) + 1);
1582 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "appending tsig to packet"));
1583 	/* prepare for validating tsigs */
1584 	tsig_prepare(tsig);
1585 }
1586 
1587 static int
1588 xfrd_send_ixfr_request_udp(xfrd_zone_type* zone)
1589 {
1590 	int fd;
1591 
1592 	/* make sure we have a master to query the ixfr request to */
1593 	assert(zone->master);
1594 
1595 	if(zone->tcp_conn != -1) {
1596 		/* tcp is using the zone_handler.fd */
1597 		log_msg(LOG_ERR, "xfrd: %s tried to send udp whilst tcp engaged",
1598 			zone->apex_str);
1599 		return -1;
1600 	}
1601 	xfrd_setup_packet(xfrd->packet, TYPE_IXFR, CLASS_IN, zone->apex,
1602 		qid_generate());
1603 	zone->query_id = ID(xfrd->packet);
1604 	zone->query_type = TYPE_IXFR;
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 	if(qdcount > 64 || ancount > 65530 || nscount > 65530) {
1894 		/* 0 or 1 question section rr, and 64k limits other counts */
1895 		DEBUG(DEBUG_XFRD,1, (LOG_ERR, "dropping xfr reply, impossibly "
1896 			"high record count"));
1897 		return xfrd_packet_bad;
1898 	}
1899 
1900 	/* skip question section */
1901 	for(rr_count = 0; rr_count < qdcount; ++rr_count) {
1902 		if (!packet_skip_rr(packet, 1)) {
1903 			log_msg(LOG_ERR, "xfrd: zone %s, from %s: bad RR in "
1904 					 		 "question section",
1905 				zone->apex_str, zone->master->ip_address_spec);
1906 			return xfrd_packet_bad;
1907 		}
1908 	}
1909 	if(zone->msg_rr_count == 0 && ancount == 0) {
1910 		if(zone->tcp_conn == -1 && TC(packet)) {
1911 			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: TC flagged"));
1912 			return xfrd_packet_tcp;
1913 		}
1914 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: too short xfr packet: no "
1915 					       			   "answer"));
1916 		/* if IXFR is unknown, fallback to AXFR (if allowed) */
1917 		if (nscount == 1) {
1918 			if(!packet_skip_dname(packet) || !xfrd_parse_soa_info(packet, soa)) {
1919 				DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1920 					"no SOA begins authority section",
1921 					zone->apex_str, zone->master->ip_address_spec));
1922 				return xfrd_packet_bad;
1923 			}
1924 			return xfrd_packet_notimpl;
1925 		}
1926 		return xfrd_packet_bad;
1927 	}
1928 	ancount_todo = ancount;
1929 
1930 	tempregion = region_create(xalloc, free);
1931 	if(zone->msg_rr_count == 0) {
1932 		const dname_type* soaname = dname_make_from_packet(tempregion,
1933 			packet, 1, 1);
1934 		if(!soaname) { /* parse failure */
1935 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1936 				"parse error in SOA record",
1937 				zone->apex_str, zone->master->ip_address_spec));
1938 			region_destroy(tempregion);
1939 			return xfrd_packet_bad;
1940 		}
1941 		if(dname_compare(soaname, zone->apex) != 0) { /* wrong name */
1942 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1943 				"wrong SOA record",
1944 				zone->apex_str, zone->master->ip_address_spec));
1945 			region_destroy(tempregion);
1946 			return xfrd_packet_bad;
1947 		}
1948 
1949 		/* parse the first RR, see if it is a SOA */
1950 		if(!xfrd_parse_soa_info(packet, soa))
1951 		{
1952 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1953 						      "bad SOA rdata",
1954 				zone->apex_str, zone->master->ip_address_spec));
1955 			region_destroy(tempregion);
1956 			return xfrd_packet_bad;
1957 		}
1958 		if(zone->soa_disk_acquired != 0 &&
1959 			zone->state != xfrd_zone_expired /* if expired - accept anything */ &&
1960 			compare_serial(ntohl(soa->serial), ntohl(zone->soa_disk.serial)) < 0) {
1961                         DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1962                                 "xfrd: zone %s ignoring old serial (%u/%u) from %s",
1963                                 zone->apex_str, ntohl(zone->soa_disk.serial), ntohl(soa->serial), zone->master->ip_address_spec));
1964                         VERBOSITY(1, (LOG_INFO,
1965                                 "xfrd: zone %s ignoring old serial (%u/%u) from %s",
1966                                 zone->apex_str, ntohl(zone->soa_disk.serial), ntohl(soa->serial), zone->master->ip_address_spec));
1967 			region_destroy(tempregion);
1968 			return xfrd_packet_bad;
1969 		}
1970 		if(zone->soa_disk_acquired != 0 && zone->soa_disk.serial == soa->serial) {
1971 			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s got "
1972 						       "update indicating "
1973 						       "current serial",
1974 				zone->apex_str));
1975 			/* (even if notified) the lease on the current soa is renewed */
1976 			zone->soa_disk_acquired = xfrd_time();
1977 			if(zone->soa_nsd.serial == soa->serial)
1978 				zone->soa_nsd_acquired = xfrd_time();
1979 			xfrd_set_zone_state(zone, xfrd_zone_ok);
1980  			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s is ok",
1981 				zone->apex_str));
1982 			if(zone->zone_options->pattern->multi_master_check) {
1983 				region_destroy(tempregion);
1984 				return xfrd_packet_drop;
1985 			}
1986 			if(zone->soa_notified_acquired == 0) {
1987 				/* not notified or anything, so stop asking around */
1988 				zone->round_num = -1; /* next try start a new round */
1989 				xfrd_set_timer_refresh(zone);
1990 				region_destroy(tempregion);
1991 				return xfrd_packet_newlease;
1992 			}
1993 			/* try next master */
1994 			region_destroy(tempregion);
1995 			return xfrd_packet_drop;
1996 		}
1997 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "IXFR reply has ok serial (have \
1998 %u, reply %u).", (unsigned)ntohl(zone->soa_disk.serial), (unsigned)ntohl(soa->serial)));
1999 		/* serial is newer than soa_disk */
2000 		if(ancount == 1) {
2001 			/* single record means it is like a notify */
2002 			(void)xfrd_handle_incoming_notify(zone, soa);
2003 		}
2004 		else if(zone->soa_notified_acquired && zone->soa_notified.serial &&
2005 			compare_serial(ntohl(zone->soa_notified.serial), ntohl(soa->serial)) < 0) {
2006 			/* this AXFR/IXFR notifies me that an even newer serial exists */
2007 			zone->soa_notified.serial = soa->serial;
2008 		}
2009 		zone->msg_new_serial = ntohl(soa->serial);
2010 		zone->msg_rr_count = 1;
2011 		zone->msg_is_ixfr = 0;
2012 		if(zone->soa_disk_acquired)
2013 			zone->msg_old_serial = ntohl(zone->soa_disk.serial);
2014 		else zone->msg_old_serial = 0;
2015 		ancount_todo = ancount - 1;
2016 	}
2017 
2018 	if(zone->tcp_conn == -1 && TC(packet)) {
2019 		DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2020 			"xfrd: zone %s received TC from %s. retry tcp.",
2021 			zone->apex_str, zone->master->ip_address_spec));
2022 		region_destroy(tempregion);
2023 		return xfrd_packet_tcp;
2024 	}
2025 
2026 	if(zone->tcp_conn == -1 && ancount < 2) {
2027 		/* too short to be a real ixfr/axfr data transfer: need at */
2028 		/* least two RRs in the answer section. */
2029 		/* The serial is newer, so try tcp to this master. */
2030 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: udp reply is short. Try "
2031 					       			   "tcp anyway."));
2032 		region_destroy(tempregion);
2033 		return xfrd_packet_tcp;
2034 	}
2035 
2036 	if(!xfrd_xfr_check_rrs(zone, packet, ancount_todo, &done, soa,
2037 		tempregion))
2038 	{
2039 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s sent bad xfr "
2040 					       			   "reply.", zone->apex_str));
2041 		region_destroy(tempregion);
2042 		return xfrd_packet_bad;
2043 	}
2044 	region_destroy(tempregion);
2045 	if(zone->tcp_conn == -1 && done == 0) {
2046 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: udp reply incomplete"));
2047 		return xfrd_packet_bad;
2048 	}
2049 	if(done == 0)
2050 		return xfrd_packet_more;
2051 	if(zone->master->key_options) {
2052 		if(zone->tsig.updates_since_last_prepare != 0) {
2053 			log_msg(LOG_INFO, "xfrd: last packet of reply has no "
2054 					 		  "TSIG");
2055 			return xfrd_packet_bad;
2056 		}
2057 	}
2058 	return xfrd_packet_transfer;
2059 }
2060 
2061 const char*
2062 xfrd_pretty_time(time_t v)
2063 {
2064 	struct tm* tm = localtime(&v);
2065 	static char buf[64];
2066 	if(!strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", tm))
2067 		snprintf(buf, sizeof(buf), "strftime-err-%u", (unsigned)v);
2068 	return buf;
2069 }
2070 
2071 enum xfrd_packet_result
2072 xfrd_handle_received_xfr_packet(xfrd_zone_type* zone, buffer_type* packet)
2073 {
2074 	xfrd_soa_type soa;
2075 	enum xfrd_packet_result res;
2076         uint64_t xfrfile_size;
2077 
2078 	/* parse and check the packet - see if it ends the xfr */
2079 	switch((res=xfrd_parse_received_xfr_packet(zone, packet, &soa)))
2080 	{
2081 		case xfrd_packet_more:
2082 		case xfrd_packet_transfer:
2083 			/* continue with commit */
2084 			break;
2085 		case xfrd_packet_newlease:
2086 			return xfrd_packet_newlease;
2087 		case xfrd_packet_tcp:
2088 			return xfrd_packet_tcp;
2089 		case xfrd_packet_notimpl:
2090 		case xfrd_packet_bad:
2091 		case xfrd_packet_drop:
2092 		default:
2093 		{
2094 			/* rollback */
2095 			if(zone->msg_seq_nr > 0) {
2096 				/* do not process xfr - if only one part simply ignore it. */
2097 				/* delete file with previous parts of commit */
2098 				xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
2099 				VERBOSITY(1, (LOG_INFO, "xfrd: zone %s "
2100 					"reverted transfer %u from %s",
2101 					zone->apex_str, zone->msg_rr_count?
2102 					(int)zone->msg_new_serial:0,
2103 					zone->master->ip_address_spec));
2104 				zone->msg_seq_nr = 0;
2105 			} else if (res == xfrd_packet_bad) {
2106 				VERBOSITY(1, (LOG_INFO, "xfrd: zone %s "
2107 					"bad transfer %u from %s",
2108 					zone->apex_str, zone->msg_rr_count?
2109 					(int)zone->msg_new_serial:0,
2110 					zone->master->ip_address_spec));
2111 			}
2112 			if (res == xfrd_packet_notimpl
2113 				&& zone->query_type == TYPE_IXFR)
2114 				return res;
2115 			else
2116 				return xfrd_packet_bad;
2117 		}
2118 	}
2119 
2120 	/* dump reply on disk to diff file */
2121 	/* if first part, get new filenumber.  Numbers can wrap around, 64bit
2122 	 * is enough so we do not collide with older-transfers-in-progress */
2123 	if(zone->msg_seq_nr == 0)
2124 		zone->xfrfilenumber = xfrd->xfrfilenumber++;
2125 	diff_write_packet(dname_to_string(zone->apex,0),
2126 		zone->zone_options->pattern->pname,
2127 		zone->msg_old_serial, zone->msg_new_serial, zone->msg_seq_nr,
2128 		buffer_begin(packet), buffer_limit(packet), xfrd->nsd,
2129 		zone->xfrfilenumber);
2130 	VERBOSITY(3, (LOG_INFO,
2131 		"xfrd: zone %s written received XFR packet from %s with serial %u to "
2132 		"disk", zone->apex_str, zone->master->ip_address_spec,
2133 		(int)zone->msg_new_serial));
2134 	zone->msg_seq_nr++;
2135 
2136         xfrfile_size = xfrd_get_xfrfile_size(xfrd->nsd, zone->xfrfilenumber);
2137 	if( zone->zone_options->pattern->size_limit_xfr != 0 &&
2138 	    xfrfile_size > zone->zone_options->pattern->size_limit_xfr ) {
2139             /*	    xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
2140                     xfrd_set_reload_timeout(); */
2141             log_msg(LOG_INFO, "xfrd : transferred zone data was too large %llu", (long long unsigned)xfrfile_size);
2142 	    return xfrd_packet_bad;
2143 	}
2144 	if(res == xfrd_packet_more) {
2145 		/* wait for more */
2146 		return xfrd_packet_more;
2147 	}
2148 
2149 	/* done. we are completely sure of this */
2150 	buffer_clear(packet);
2151 	buffer_printf(packet, "received update to serial %u at %s from %s",
2152 		(unsigned)zone->msg_new_serial, xfrd_pretty_time(xfrd_time()),
2153 		zone->master->ip_address_spec);
2154 	if(zone->master->key_options) {
2155 		buffer_printf(packet, " TSIG verified with key %s",
2156 			zone->master->key_options->name);
2157 	}
2158 	buffer_flip(packet);
2159 	diff_write_commit(zone->apex_str, zone->msg_old_serial,
2160 		zone->msg_new_serial, zone->msg_seq_nr, 1,
2161 		(char*)buffer_begin(packet), xfrd->nsd, zone->xfrfilenumber);
2162 	VERBOSITY(1, (LOG_INFO, "xfrd: zone %s committed \"%s\"",
2163 		zone->apex_str, (char*)buffer_begin(packet)));
2164 	/* reset msg seq nr, so if that is nonnull we know xfr file exists */
2165 	zone->msg_seq_nr = 0;
2166 	/* now put apply_xfr task on the tasklist */
2167 	if(!task_new_apply_xfr(xfrd->nsd->task[xfrd->nsd->mytask],
2168 		xfrd->last_task, zone->apex, zone->msg_old_serial,
2169 		zone->msg_new_serial, zone->xfrfilenumber)) {
2170 		/* delete the file and pretend transfer was bad to continue */
2171 		xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
2172 		xfrd_set_reload_timeout();
2173 		return xfrd_packet_bad;
2174 	}
2175 	/* update the disk serial no. */
2176 	zone->soa_disk_acquired = xfrd_time();
2177 	zone->soa_disk = soa;
2178 	if(zone->soa_notified_acquired && (
2179 		zone->soa_notified.serial == 0 ||
2180 		compare_serial(htonl(zone->soa_disk.serial),
2181 		htonl(zone->soa_notified.serial)) >= 0))
2182 	{
2183 		zone->soa_notified_acquired = 0;
2184 	}
2185 	if(!zone->soa_notified_acquired) {
2186 		/* do not set expired zone to ok:
2187 		 * it would cause nsd to start answering
2188 		 * bad data, since the zone is not loaded yet.
2189 		 * if nsd does not reload < retry time, more
2190 		 * queries (for even newer versions) are made.
2191 		 * For expired zone after reload it is set ok (SOAINFO ipc). */
2192 		if(zone->state != xfrd_zone_expired)
2193 			xfrd_set_zone_state(zone, xfrd_zone_ok);
2194 		DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2195 			"xfrd: zone %s is waiting for reload",
2196 			zone->apex_str));
2197 		if(zone->zone_options->pattern->multi_master_check) {
2198 			zone->multi_master_update_check = zone->master_num;
2199 			xfrd_set_reload_timeout();
2200 			return xfrd_packet_transfer;
2201 		}
2202 		zone->round_num = -1; /* next try start anew */
2203 		xfrd_set_timer_refresh(zone);
2204 		xfrd_set_reload_timeout();
2205 		return xfrd_packet_transfer;
2206 	} else {
2207 		/* try to get an even newer serial */
2208 		/* pretend it was bad to continue queries */
2209 		xfrd_set_reload_timeout();
2210 		return xfrd_packet_bad;
2211 	}
2212 }
2213 
2214 static void
2215 xfrd_set_reload_timeout()
2216 {
2217 	if(xfrd->nsd->options->xfrd_reload_timeout == -1)
2218 		return; /* automatic reload disabled. */
2219 	if(xfrd->reload_timeout.tv_sec == 0 ||
2220 		xfrd_time() >= (time_t)xfrd->reload_timeout.tv_sec ) {
2221 		/* no reload wait period (or it passed), do it right away */
2222 		xfrd_set_reload_now(xfrd);
2223 		/* start reload wait period */
2224 		xfrd->reload_timeout.tv_sec = xfrd_time() +
2225 			xfrd->nsd->options->xfrd_reload_timeout;
2226 		xfrd->reload_timeout.tv_usec = 0;
2227 		return;
2228 	}
2229 	/* cannot reload now, set that after the timeout a reload has to happen */
2230 	if(xfrd->reload_added == 0) {
2231 		struct timeval tv;
2232 		tv.tv_sec = xfrd->reload_timeout.tv_sec - xfrd_time();
2233 		tv.tv_usec = 0;
2234 		if(tv.tv_sec > xfrd->nsd->options->xfrd_reload_timeout)
2235 			tv.tv_sec = xfrd->nsd->options->xfrd_reload_timeout;
2236 		memset(&xfrd->reload_handler, 0, sizeof(xfrd->reload_handler));
2237 		event_set(&xfrd->reload_handler, -1, EV_TIMEOUT,
2238 			xfrd_handle_reload, xfrd);
2239 		if(event_base_set(xfrd->event_base, &xfrd->reload_handler) != 0)
2240 			log_msg(LOG_ERR, "cannot set reload event base");
2241 		if(event_add(&xfrd->reload_handler, &tv) != 0)
2242 			log_msg(LOG_ERR, "cannot add reload event");
2243 		xfrd->reload_added = 1;
2244 	}
2245 }
2246 
2247 static void
2248 xfrd_handle_reload(int ATTR_UNUSED(fd), short event, void* ATTR_UNUSED(arg))
2249 {
2250 	/* reload timeout */
2251 	assert(event & EV_TIMEOUT);
2252 	(void)event;
2253 	/* timeout wait period after this request is sent */
2254 	xfrd->reload_added = 0;
2255 	xfrd->reload_timeout.tv_sec = xfrd_time() +
2256 		xfrd->nsd->options->xfrd_reload_timeout;
2257 	xfrd_set_reload_now(xfrd);
2258 }
2259 
2260 void
2261 xfrd_handle_notify_and_start_xfr(xfrd_zone_type* zone, xfrd_soa_type* soa)
2262 {
2263 	if(xfrd_handle_incoming_notify(zone, soa)) {
2264 		if(zone->zone_handler.ev_fd == -1 && zone->tcp_conn == -1 &&
2265 			!zone->tcp_waiting && !zone->udp_waiting) {
2266 			xfrd_set_refresh_now(zone);
2267 		}
2268 		/* zones with no content start expbackoff again; this is also
2269 		 * for nsd-control started transfer commands, and also when
2270 		 * the master apparently sends notifies (is back up) */
2271 		if(zone->soa_disk_acquired == 0)
2272 			zone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_START;
2273 	}
2274 }
2275 
2276 void
2277 xfrd_handle_passed_packet(buffer_type* packet,
2278 	int acl_num, int acl_num_xfr)
2279 {
2280 	uint8_t qnamebuf[MAXDOMAINLEN];
2281 	uint16_t qtype, qclass;
2282 	const dname_type* dname;
2283 	region_type* tempregion = region_create(xalloc, free);
2284 	xfrd_zone_type* zone;
2285 
2286 	buffer_skip(packet, QHEADERSZ);
2287 	if(!packet_read_query_section(packet, qnamebuf, &qtype, &qclass)) {
2288 		region_destroy(tempregion);
2289 		return; /* drop bad packet */
2290 	}
2291 
2292 	dname = dname_make(tempregion, qnamebuf, 1);
2293 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: got passed packet for %s, acl "
2294 		   "%d", dname_to_string(dname,0), acl_num));
2295 
2296 	/* find the zone */
2297 	zone = (xfrd_zone_type*)rbtree_search(xfrd->zones, dname);
2298 	if(!zone) {
2299 		/* this could be because the zone has been deleted meanwhile */
2300 		DEBUG(DEBUG_XFRD, 1, (LOG_INFO, "xfrd: incoming packet for "
2301 			"unknown zone %s", dname_to_string(dname,0)));
2302 		region_destroy(tempregion);
2303 		return; /* drop packet for unknown zone */
2304 	}
2305 	region_destroy(tempregion);
2306 
2307 	/* handle */
2308 	if(OPCODE(packet) == OPCODE_NOTIFY) {
2309 		xfrd_soa_type soa;
2310 		int have_soa = 0;
2311 		int next;
2312 		/* get serial from a SOA */
2313 		if(ANCOUNT(packet) == 1 && packet_skip_dname(packet) &&
2314 			xfrd_parse_soa_info(packet, &soa)) {
2315 				have_soa = 1;
2316 		}
2317 		xfrd_handle_notify_and_start_xfr(zone, have_soa?&soa:NULL);
2318 		/* First, see if our notifier has a match in provide-xfr */
2319 		if (acl_find_num(zone->zone_options->pattern->request_xfr,
2320 				acl_num_xfr))
2321 			next = acl_num_xfr;
2322 		else /* If not, find master that matches notifiers ACL entry */
2323 			next = find_same_master_notify(zone, acl_num);
2324 		if(next != -1) {
2325 			zone->next_master = next;
2326 			DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2327 				"xfrd: notify set next master to query %d",
2328 				next));
2329 		}
2330 	}
2331 	else {
2332 		/* ignore other types of messages */
2333 	}
2334 }
2335 
2336 static int
2337 xfrd_handle_incoming_notify(xfrd_zone_type* zone, xfrd_soa_type* soa)
2338 {
2339 	if(soa && zone->soa_disk_acquired && zone->state != xfrd_zone_expired &&
2340 	   compare_serial(ntohl(soa->serial),ntohl(zone->soa_disk.serial)) <= 0)
2341 	{
2342 		DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2343 			"xfrd: ignored notify %s %u old serial, zone valid "
2344 			"(soa disk serial %u)", zone->apex_str,
2345 			(unsigned)ntohl(soa->serial),
2346 			(unsigned)ntohl(zone->soa_disk.serial)));
2347 		return 0; /* ignore notify with old serial, we have a valid zone */
2348 	}
2349 	if(soa == 0) {
2350 		zone->soa_notified.serial = 0;
2351 	}
2352 	else if (zone->soa_notified_acquired == 0 ||
2353 		 zone->soa_notified.serial == 0 ||
2354 		 compare_serial(ntohl(soa->serial),
2355 			ntohl(zone->soa_notified.serial)) > 0)
2356 	{
2357 		zone->soa_notified = *soa;
2358 	}
2359 	zone->soa_notified_acquired = xfrd_time();
2360 	if(zone->state == xfrd_zone_ok) {
2361 		xfrd_set_zone_state(zone, xfrd_zone_refreshing);
2362 	}
2363 	/* transfer right away */
2364 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "Handle incoming notify for zone %s",
2365 		zone->apex_str));
2366 	return 1;
2367 }
2368 
2369 static int
2370 find_same_master_notify(xfrd_zone_type* zone, int acl_num_nfy)
2371 {
2372 	struct acl_options* nfy_acl = acl_find_num(zone->zone_options->pattern->
2373 		allow_notify, acl_num_nfy);
2374 	int num = 0;
2375 	struct acl_options* master = zone->zone_options->pattern->request_xfr;
2376 	if(!nfy_acl)
2377 		return -1;
2378 	while(master)
2379 	{
2380 		if(acl_addr_matches_host(nfy_acl, master))
2381 			return num;
2382 		master = master->next;
2383 		num++;
2384 	}
2385 	return -1;
2386 }
2387 
2388 void
2389 xfrd_check_failed_updates()
2390 {
2391 	/* see if updates have not come through */
2392 	xfrd_zone_type* zone;
2393 	RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
2394 	{
2395 		/* zone has a disk soa, and no nsd soa or a different nsd soa */
2396 		if(zone->soa_disk_acquired != 0 &&
2397 			(zone->soa_nsd_acquired == 0 ||
2398 			zone->soa_disk.serial != zone->soa_nsd.serial))
2399 		{
2400 			if(zone->soa_disk_acquired <
2401 				xfrd->reload_cmd_last_sent)
2402 			{
2403 				/* this zone should have been loaded, since its disk
2404 				   soa time is before the time of the reload cmd. */
2405 				xfrd_soa_type dumped_soa = zone->soa_disk;
2406 				log_msg(LOG_ERR, "xfrd: zone %s: soa serial %u "
2407 						 		 "update failed, restarting "
2408 						 		 "transfer (notified zone)",
2409 					zone->apex_str, (unsigned)ntohl(zone->soa_disk.serial));
2410 				/* revert the soa; it has not been acquired properly */
2411 				if(zone->soa_disk_acquired == zone->soa_nsd_acquired) {
2412 					/* this was the same as served,
2413 					 * perform force_axfr , re-download
2414 					 * same serial from master */
2415 					zone->soa_disk_acquired = 0;
2416 					zone->soa_nsd_acquired = 0;
2417 				} else {
2418 					/* revert soa to the one in server */
2419 					zone->soa_disk_acquired = zone->soa_nsd_acquired;
2420 					zone->soa_disk = zone->soa_nsd;
2421 				}
2422 				/* pretend we are notified with disk soa.
2423 				   This will cause a refetch of the data, and reload. */
2424 				xfrd_handle_incoming_notify(zone, &dumped_soa);
2425 				xfrd_set_timer_refresh(zone);
2426 			} else if(zone->soa_disk_acquired >= xfrd->reload_cmd_last_sent) {
2427 				/* this zone still has to be loaded,
2428 				   make sure reload is set to be sent. */
2429 				if(xfrd->need_to_send_reload == 0 &&
2430 					xfrd->reload_added == 0) {
2431 					log_msg(LOG_ERR, "xfrd: zone %s: needs "
2432 									 "to be loaded. reload lost? "
2433 									 "try again", zone->apex_str);
2434 					xfrd_set_reload_timeout();
2435 				}
2436 			}
2437 		}
2438 	}
2439 }
2440 
2441 void
2442 xfrd_prepare_zones_for_reload()
2443 {
2444 	xfrd_zone_type* zone;
2445 	RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
2446 	{
2447 		/* zone has a disk soa, and no nsd soa or a different nsd soa */
2448 		if(zone->soa_disk_acquired != 0 &&
2449 			(zone->soa_nsd_acquired == 0 ||
2450 			zone->soa_disk.serial != zone->soa_nsd.serial))
2451 		{
2452 			if(zone->soa_disk_acquired == xfrd_time()) {
2453 				/* antedate by one second.
2454 				 * this makes sure that the zone time is before
2455 				 * reload, so that check_failed_zones() is
2456 				 * certain of the result.
2457 				 */
2458 				zone->soa_disk_acquired--;
2459 			}
2460 		}
2461 	}
2462 }
2463 
2464 struct buffer*
2465 xfrd_get_temp_buffer()
2466 {
2467 	return xfrd->packet;
2468 }
2469 
2470 #ifdef BIND8_STATS
2471 /** process stat info task */
2472 static void
2473 xfrd_process_stat_info_task(xfrd_state_type* xfrd, struct task_list_d* task)
2474 {
2475 	size_t i;
2476 	stc_type* p = (void*)((char*)task->zname + sizeof(struct nsdst));
2477 	stats_add(&xfrd->nsd->st, (struct nsdst*)task->zname);
2478 	for(i=0; i<xfrd->nsd->child_count; i++) {
2479 		xfrd->nsd->children[i].query_count += *p++;
2480 	}
2481 	/* got total, now see if users are interested in these statistics */
2482 #ifdef HAVE_SSL
2483 	daemon_remote_process_stats(xfrd->nsd->rc);
2484 #endif
2485 }
2486 #endif /* BIND8_STATS */
2487 
2488 #ifdef USE_ZONE_STATS
2489 /** process zonestat inc task */
2490 static void
2491 xfrd_process_zonestat_inc_task(xfrd_state_type* xfrd, struct task_list_d* task)
2492 {
2493 	xfrd->zonestat_safe = (unsigned)task->oldserial;
2494 	zonestat_remap(xfrd->nsd, 0, xfrd->zonestat_safe*sizeof(struct nsdst));
2495 	xfrd->nsd->zonestatsize[0] = xfrd->zonestat_safe;
2496 	zonestat_remap(xfrd->nsd, 1, xfrd->zonestat_safe*sizeof(struct nsdst));
2497 	xfrd->nsd->zonestatsize[1] = xfrd->zonestat_safe;
2498 }
2499 #endif /* USE_ZONE_STATS */
2500 
2501 static void
2502 xfrd_handle_taskresult(xfrd_state_type* xfrd, struct task_list_d* task)
2503 {
2504 #ifndef BIND8_STATS
2505 	(void)xfrd;
2506 #endif
2507 	switch(task->task_type) {
2508 	case task_soa_info:
2509 		xfrd_process_soa_info_task(task);
2510 		break;
2511 #ifdef BIND8_STATS
2512 	case task_stat_info:
2513 		xfrd_process_stat_info_task(xfrd, task);
2514 		break;
2515 #endif /* BIND8_STATS */
2516 #ifdef USE_ZONE_STATS
2517 	case task_zonestat_inc:
2518 		xfrd_process_zonestat_inc_task(xfrd, task);
2519 		break;
2520 #endif
2521 	default:
2522 		log_msg(LOG_WARNING, "unhandled task result in xfrd from "
2523 			"reload type %d", (int)task->task_type);
2524 	}
2525 }
2526 
2527 void xfrd_process_task_result(xfrd_state_type* xfrd, struct udb_base* taskudb)
2528 {
2529 	udb_ptr t;
2530 	/* remap it for usage */
2531 	task_remap(taskudb);
2532 	/* process the task-results in the taskudb */
2533 	udb_ptr_new(&t, taskudb, udb_base_get_userdata(taskudb));
2534 	while(!udb_ptr_is_null(&t)) {
2535 		xfrd_handle_taskresult(xfrd, TASKLIST(&t));
2536 		udb_ptr_set_rptr(&t, taskudb, &TASKLIST(&t)->next);
2537 	}
2538 	udb_ptr_unlink(&t, taskudb);
2539 	/* clear the udb so it can be used by xfrd to make new tasks for
2540 	 * reload, this happens when the reload signal is sent, and thus
2541 	 * the taskudbs are swapped */
2542 	task_clear(taskudb);
2543 #ifdef HAVE_SYSTEMD
2544 	sd_notify(0, "READY=1");
2545 #endif
2546 }
2547 
2548 void xfrd_set_reload_now(xfrd_state_type* xfrd)
2549 {
2550 #ifdef HAVE_SYSTEMD
2551 	sd_notify(0, "RELOADING=1");
2552 #endif
2553 	xfrd->need_to_send_reload = 1;
2554 	if(!(xfrd->ipc_handler_flags&EV_WRITE)) {
2555 		ipc_xfrd_set_listening(xfrd, EV_PERSIST|EV_READ|EV_WRITE);
2556 	}
2557 }
2558 
2559 static void
2560 xfrd_handle_write_timer(int ATTR_UNUSED(fd), short event, void* ATTR_UNUSED(arg))
2561 {
2562 	/* timeout for write events */
2563 	assert(event & EV_TIMEOUT);
2564 	(void)event;
2565 	if(xfrd->nsd->options->zonefiles_write == 0)
2566 		return;
2567 	/* call reload to write changed zonefiles */
2568 	if(!xfrd->write_zonefile_needed) {
2569 		DEBUG(DEBUG_XFRD,2, (LOG_INFO, "zonefiles write timer (nothing)"));
2570 		xfrd_write_timer_set();
2571 		return;
2572 	}
2573 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "zonefiles write timer"));
2574 	task_new_write_zonefiles(xfrd->nsd->task[xfrd->nsd->mytask],
2575 		xfrd->last_task, NULL);
2576 	xfrd_set_reload_now(xfrd);
2577 	xfrd->write_zonefile_needed = 0;
2578 	xfrd_write_timer_set();
2579 }
2580 
2581 static void xfrd_write_timer_set()
2582 {
2583 	struct timeval tv;
2584 	if(xfrd->nsd->options->zonefiles_write == 0)
2585 		return;
2586 	tv.tv_sec = xfrd->nsd->options->zonefiles_write;
2587 	tv.tv_usec = 0;
2588 	memset(&xfrd->write_timer, 0, sizeof(xfrd->write_timer));
2589 	event_set(&xfrd->write_timer, -1, EV_TIMEOUT,
2590 		xfrd_handle_write_timer, xfrd);
2591 	if(event_base_set(xfrd->event_base, &xfrd->write_timer) != 0)
2592 		log_msg(LOG_ERR, "xfrd write timer: event_base_set failed");
2593 	if(event_add(&xfrd->write_timer, &tv) != 0)
2594 		log_msg(LOG_ERR, "xfrd write timer: event_add failed");
2595 }
2596 
2597 static void xfrd_handle_child_timer(int ATTR_UNUSED(fd), short event,
2598 	void* ATTR_UNUSED(arg))
2599 {
2600 	assert(event & EV_TIMEOUT);
2601 	(void)event;
2602 	/* only used to wakeup the process to reap children, note the
2603 	 * event is no longer registered */
2604 	xfrd->child_timer_added = 0;
2605 }
2606