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