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