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