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