1 /* 2 * daemon/remote.c - remote control for the unbound daemon. 3 * 4 * Copyright (c) 2008, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * Neither the name of the NLNET LABS nor the names of its contributors may 20 * be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /** 37 * \file 38 * 39 * This file contains the remote control functionality for the daemon. 40 * The remote control can be performed using either the commandline 41 * unbound-control tool, or a TLS capable web browser. 42 * The channel is secured using TLSv1, and certificates. 43 * Both the server and the client(control tool) have their own keys. 44 */ 45 #include "config.h" 46 #ifdef HAVE_OPENSSL_ERR_H 47 #include <openssl/err.h> 48 #endif 49 #include <ctype.h> 50 #include "daemon/remote.h" 51 #include "daemon/worker.h" 52 #include "daemon/daemon.h" 53 #include "daemon/stats.h" 54 #include "daemon/cachedump.h" 55 #include "util/log.h" 56 #include "util/config_file.h" 57 #include "util/net_help.h" 58 #include "util/module.h" 59 #include "services/listen_dnsport.h" 60 #include "services/cache/rrset.h" 61 #include "services/cache/infra.h" 62 #include "services/mesh.h" 63 #include "services/localzone.h" 64 #include "util/storage/slabhash.h" 65 #include "util/fptr_wlist.h" 66 #include "util/data/dname.h" 67 #include "validator/validator.h" 68 #include "validator/val_kcache.h" 69 #include "validator/val_kentry.h" 70 #include "validator/val_anchor.h" 71 #include "iterator/iterator.h" 72 #include "iterator/iter_fwd.h" 73 #include "iterator/iter_hints.h" 74 #include "iterator/iter_delegpt.h" 75 #include "services/outbound_list.h" 76 #include "services/outside_network.h" 77 #include "ldns/str2wire.h" 78 #include "ldns/parseutil.h" 79 #include "ldns/wire2str.h" 80 #include "ldns/sbuffer.h" 81 82 #ifdef HAVE_SYS_TYPES_H 83 # include <sys/types.h> 84 #endif 85 #ifdef HAVE_NETDB_H 86 #include <netdb.h> 87 #endif 88 89 /* just for portability */ 90 #ifdef SQ 91 #undef SQ 92 #endif 93 94 /** what to put on statistics lines between var and value, ": " or "=" */ 95 #define SQ "=" 96 /** if true, inhibits a lot of =0 lines from the stats output */ 97 static const int inhibit_zero = 1; 98 99 /** subtract timers and the values do not overflow or become negative */ 100 static void 101 timeval_subtract(struct timeval* d, const struct timeval* end, 102 const struct timeval* start) 103 { 104 #ifndef S_SPLINT_S 105 time_t end_usec = end->tv_usec; 106 d->tv_sec = end->tv_sec - start->tv_sec; 107 if(end_usec < start->tv_usec) { 108 end_usec += 1000000; 109 d->tv_sec--; 110 } 111 d->tv_usec = end_usec - start->tv_usec; 112 #endif 113 } 114 115 /** divide sum of timers to get average */ 116 static void 117 timeval_divide(struct timeval* avg, const struct timeval* sum, size_t d) 118 { 119 #ifndef S_SPLINT_S 120 size_t leftover; 121 if(d == 0) { 122 avg->tv_sec = 0; 123 avg->tv_usec = 0; 124 return; 125 } 126 avg->tv_sec = sum->tv_sec / d; 127 avg->tv_usec = sum->tv_usec / d; 128 /* handle fraction from seconds divide */ 129 leftover = sum->tv_sec - avg->tv_sec*d; 130 avg->tv_usec += (leftover*1000000)/d; 131 #endif 132 } 133 134 struct daemon_remote* 135 daemon_remote_create(struct config_file* cfg) 136 { 137 char* s_cert; 138 char* s_key; 139 struct daemon_remote* rc = (struct daemon_remote*)calloc(1, 140 sizeof(*rc)); 141 if(!rc) { 142 log_err("out of memory in daemon_remote_create"); 143 return NULL; 144 } 145 rc->max_active = 10; 146 147 if(!cfg->remote_control_enable) { 148 rc->ctx = NULL; 149 return rc; 150 } 151 rc->ctx = SSL_CTX_new(SSLv23_server_method()); 152 if(!rc->ctx) { 153 log_crypto_err("could not SSL_CTX_new"); 154 free(rc); 155 return NULL; 156 } 157 /* no SSLv2, SSLv3 because has defects */ 158 if(!(SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)){ 159 log_crypto_err("could not set SSL_OP_NO_SSLv2"); 160 daemon_remote_delete(rc); 161 return NULL; 162 } 163 if(!(SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)){ 164 log_crypto_err("could not set SSL_OP_NO_SSLv3"); 165 daemon_remote_delete(rc); 166 return NULL; 167 } 168 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1); 169 s_key = fname_after_chroot(cfg->server_key_file, cfg, 1); 170 if(!s_cert || !s_key) { 171 log_err("out of memory in remote control fname"); 172 goto setup_error; 173 } 174 verbose(VERB_ALGO, "setup SSL certificates"); 175 if (!SSL_CTX_use_certificate_file(rc->ctx,s_cert,SSL_FILETYPE_PEM)) { 176 log_err("Error for server-cert-file: %s", s_cert); 177 log_crypto_err("Error in SSL_CTX use_certificate_file"); 178 goto setup_error; 179 } 180 if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) { 181 log_err("Error for server-key-file: %s", s_key); 182 log_crypto_err("Error in SSL_CTX use_PrivateKey_file"); 183 goto setup_error; 184 } 185 if(!SSL_CTX_check_private_key(rc->ctx)) { 186 log_err("Error for server-key-file: %s", s_key); 187 log_crypto_err("Error in SSL_CTX check_private_key"); 188 goto setup_error; 189 } 190 if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) { 191 log_crypto_err("Error setting up SSL_CTX verify locations"); 192 setup_error: 193 free(s_cert); 194 free(s_key); 195 daemon_remote_delete(rc); 196 return NULL; 197 } 198 SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert)); 199 SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL); 200 free(s_cert); 201 free(s_key); 202 203 return rc; 204 } 205 206 void daemon_remote_clear(struct daemon_remote* rc) 207 { 208 struct rc_state* p, *np; 209 if(!rc) return; 210 /* but do not close the ports */ 211 listen_list_delete(rc->accept_list); 212 rc->accept_list = NULL; 213 /* do close these sockets */ 214 p = rc->busy_list; 215 while(p) { 216 np = p->next; 217 if(p->ssl) 218 SSL_free(p->ssl); 219 comm_point_delete(p->c); 220 free(p); 221 p = np; 222 } 223 rc->busy_list = NULL; 224 rc->active = 0; 225 rc->worker = NULL; 226 } 227 228 void daemon_remote_delete(struct daemon_remote* rc) 229 { 230 if(!rc) return; 231 daemon_remote_clear(rc); 232 if(rc->ctx) { 233 SSL_CTX_free(rc->ctx); 234 } 235 free(rc); 236 } 237 238 /** 239 * Add and open a new control port 240 * @param ip: ip str 241 * @param nr: port nr 242 * @param list: list head 243 * @param noproto_is_err: if lack of protocol support is an error. 244 * @return false on failure. 245 */ 246 static int 247 add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err) 248 { 249 struct addrinfo hints; 250 struct addrinfo* res; 251 struct listen_port* n; 252 int noproto; 253 int fd, r; 254 char port[15]; 255 snprintf(port, sizeof(port), "%d", nr); 256 port[sizeof(port)-1]=0; 257 memset(&hints, 0, sizeof(hints)); 258 hints.ai_socktype = SOCK_STREAM; 259 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 260 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) { 261 #ifdef USE_WINSOCK 262 if(!noproto_is_err && r == EAI_NONAME) { 263 /* tried to lookup the address as name */ 264 return 1; /* return success, but do nothing */ 265 } 266 #endif /* USE_WINSOCK */ 267 log_err("control interface %s:%s getaddrinfo: %s %s", 268 ip?ip:"default", port, gai_strerror(r), 269 #ifdef EAI_SYSTEM 270 r==EAI_SYSTEM?(char*)strerror(errno):"" 271 #else 272 "" 273 #endif 274 ); 275 return 0; 276 } 277 278 /* open fd */ 279 fd = create_tcp_accept_sock(res, 1, &noproto, 0); 280 freeaddrinfo(res); 281 if(fd == -1 && noproto) { 282 if(!noproto_is_err) 283 return 1; /* return success, but do nothing */ 284 log_err("cannot open control interface %s %d : " 285 "protocol not supported", ip, nr); 286 return 0; 287 } 288 if(fd == -1) { 289 log_err("cannot open control interface %s %d", ip, nr); 290 return 0; 291 } 292 293 /* alloc */ 294 n = (struct listen_port*)calloc(1, sizeof(*n)); 295 if(!n) { 296 #ifndef USE_WINSOCK 297 close(fd); 298 #else 299 closesocket(fd); 300 #endif 301 log_err("out of memory"); 302 return 0; 303 } 304 n->next = *list; 305 *list = n; 306 n->fd = fd; 307 return 1; 308 } 309 310 struct listen_port* daemon_remote_open_ports(struct config_file* cfg) 311 { 312 struct listen_port* l = NULL; 313 log_assert(cfg->remote_control_enable && cfg->control_port); 314 if(cfg->control_ifs) { 315 struct config_strlist* p; 316 for(p = cfg->control_ifs; p; p = p->next) { 317 if(!add_open(p->str, cfg->control_port, &l, 1)) { 318 listening_ports_free(l); 319 return NULL; 320 } 321 } 322 } else { 323 /* defaults */ 324 if(cfg->do_ip6 && 325 !add_open("::1", cfg->control_port, &l, 0)) { 326 listening_ports_free(l); 327 return NULL; 328 } 329 if(cfg->do_ip4 && 330 !add_open("127.0.0.1", cfg->control_port, &l, 1)) { 331 listening_ports_free(l); 332 return NULL; 333 } 334 } 335 return l; 336 } 337 338 /** open accept commpoint */ 339 static int 340 accept_open(struct daemon_remote* rc, int fd) 341 { 342 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n)); 343 if(!n) { 344 log_err("out of memory"); 345 return 0; 346 } 347 n->next = rc->accept_list; 348 rc->accept_list = n; 349 /* open commpt */ 350 n->com = comm_point_create_raw(rc->worker->base, fd, 0, 351 &remote_accept_callback, rc); 352 if(!n->com) 353 return 0; 354 /* keep this port open, its fd is kept in the rc portlist */ 355 n->com->do_not_close = 1; 356 return 1; 357 } 358 359 int daemon_remote_open_accept(struct daemon_remote* rc, 360 struct listen_port* ports, struct worker* worker) 361 { 362 struct listen_port* p; 363 rc->worker = worker; 364 for(p = ports; p; p = p->next) { 365 if(!accept_open(rc, p->fd)) { 366 log_err("could not create accept comm point"); 367 return 0; 368 } 369 } 370 return 1; 371 } 372 373 void daemon_remote_stop_accept(struct daemon_remote* rc) 374 { 375 struct listen_list* p; 376 for(p=rc->accept_list; p; p=p->next) { 377 comm_point_stop_listening(p->com); 378 } 379 } 380 381 void daemon_remote_start_accept(struct daemon_remote* rc) 382 { 383 struct listen_list* p; 384 for(p=rc->accept_list; p; p=p->next) { 385 comm_point_start_listening(p->com, -1, -1); 386 } 387 } 388 389 int remote_accept_callback(struct comm_point* c, void* arg, int err, 390 struct comm_reply* ATTR_UNUSED(rep)) 391 { 392 struct daemon_remote* rc = (struct daemon_remote*)arg; 393 struct sockaddr_storage addr; 394 socklen_t addrlen; 395 int newfd; 396 struct rc_state* n; 397 if(err != NETEVENT_NOERROR) { 398 log_err("error %d on remote_accept_callback", err); 399 return 0; 400 } 401 /* perform the accept */ 402 newfd = comm_point_perform_accept(c, &addr, &addrlen); 403 if(newfd == -1) 404 return 0; 405 /* create new commpoint unless we are servicing already */ 406 if(rc->active >= rc->max_active) { 407 log_warn("drop incoming remote control: too many connections"); 408 close_exit: 409 #ifndef USE_WINSOCK 410 close(newfd); 411 #else 412 closesocket(newfd); 413 #endif 414 return 0; 415 } 416 417 /* setup commpoint to service the remote control command */ 418 n = (struct rc_state*)calloc(1, sizeof(*n)); 419 if(!n) { 420 log_err("out of memory"); 421 goto close_exit; 422 } 423 /* start in reading state */ 424 n->c = comm_point_create_raw(rc->worker->base, newfd, 0, 425 &remote_control_callback, n); 426 if(!n->c) { 427 log_err("out of memory"); 428 free(n); 429 goto close_exit; 430 } 431 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen); 432 n->c->do_not_close = 0; 433 comm_point_stop_listening(n->c); 434 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT); 435 memcpy(&n->c->repinfo.addr, &addr, addrlen); 436 n->c->repinfo.addrlen = addrlen; 437 n->shake_state = rc_hs_read; 438 n->ssl = SSL_new(rc->ctx); 439 if(!n->ssl) { 440 log_crypto_err("could not SSL_new"); 441 comm_point_delete(n->c); 442 free(n); 443 goto close_exit; 444 } 445 SSL_set_accept_state(n->ssl); 446 (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY); 447 if(!SSL_set_fd(n->ssl, newfd)) { 448 log_crypto_err("could not SSL_set_fd"); 449 SSL_free(n->ssl); 450 comm_point_delete(n->c); 451 free(n); 452 goto close_exit; 453 } 454 455 n->rc = rc; 456 n->next = rc->busy_list; 457 rc->busy_list = n; 458 rc->active ++; 459 460 /* perform the first nonblocking read already, for windows, 461 * so it can return wouldblock. could be faster too. */ 462 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL); 463 return 0; 464 } 465 466 /** delete from list */ 467 static void 468 state_list_remove_elem(struct rc_state** list, struct comm_point* c) 469 { 470 while(*list) { 471 if( (*list)->c == c) { 472 *list = (*list)->next; 473 return; 474 } 475 list = &(*list)->next; 476 } 477 } 478 479 /** decrease active count and remove commpoint from busy list */ 480 static void 481 clean_point(struct daemon_remote* rc, struct rc_state* s) 482 { 483 state_list_remove_elem(&rc->busy_list, s->c); 484 rc->active --; 485 if(s->ssl) { 486 SSL_shutdown(s->ssl); 487 SSL_free(s->ssl); 488 } 489 comm_point_delete(s->c); 490 free(s); 491 } 492 493 int 494 ssl_print_text(SSL* ssl, const char* text) 495 { 496 int r; 497 if(!ssl) 498 return 0; 499 ERR_clear_error(); 500 if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) { 501 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { 502 verbose(VERB_QUERY, "warning, in SSL_write, peer " 503 "closed connection"); 504 return 0; 505 } 506 log_crypto_err("could not SSL_write"); 507 return 0; 508 } 509 return 1; 510 } 511 512 /** print text over the ssl connection */ 513 static int 514 ssl_print_vmsg(SSL* ssl, const char* format, va_list args) 515 { 516 char msg[1024]; 517 vsnprintf(msg, sizeof(msg), format, args); 518 return ssl_print_text(ssl, msg); 519 } 520 521 /** printf style printing to the ssl connection */ 522 int ssl_printf(SSL* ssl, const char* format, ...) 523 { 524 va_list args; 525 int ret; 526 va_start(args, format); 527 ret = ssl_print_vmsg(ssl, format, args); 528 va_end(args); 529 return ret; 530 } 531 532 int 533 ssl_read_line(SSL* ssl, char* buf, size_t max) 534 { 535 int r; 536 size_t len = 0; 537 if(!ssl) 538 return 0; 539 while(len < max) { 540 ERR_clear_error(); 541 if((r=SSL_read(ssl, buf+len, 1)) <= 0) { 542 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { 543 buf[len] = 0; 544 return 1; 545 } 546 log_crypto_err("could not SSL_read"); 547 return 0; 548 } 549 if(buf[len] == '\n') { 550 /* return string without \n */ 551 buf[len] = 0; 552 return 1; 553 } 554 len++; 555 } 556 buf[max-1] = 0; 557 log_err("control line too long (%d): %s", (int)max, buf); 558 return 0; 559 } 560 561 /** skip whitespace, return new pointer into string */ 562 static char* 563 skipwhite(char* str) 564 { 565 /* EOS \0 is not a space */ 566 while( isspace((unsigned char)*str) ) 567 str++; 568 return str; 569 } 570 571 /** send the OK to the control client */ 572 static void send_ok(SSL* ssl) 573 { 574 (void)ssl_printf(ssl, "ok\n"); 575 } 576 577 /** do the stop command */ 578 static void 579 do_stop(SSL* ssl, struct daemon_remote* rc) 580 { 581 rc->worker->need_to_exit = 1; 582 comm_base_exit(rc->worker->base); 583 send_ok(ssl); 584 } 585 586 /** do the reload command */ 587 static void 588 do_reload(SSL* ssl, struct daemon_remote* rc) 589 { 590 rc->worker->need_to_exit = 0; 591 comm_base_exit(rc->worker->base); 592 send_ok(ssl); 593 } 594 595 /** do the verbosity command */ 596 static void 597 do_verbosity(SSL* ssl, char* str) 598 { 599 int val = atoi(str); 600 if(val == 0 && strcmp(str, "0") != 0) { 601 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str); 602 return; 603 } 604 verbosity = val; 605 send_ok(ssl); 606 } 607 608 /** print stats from statinfo */ 609 static int 610 print_stats(SSL* ssl, const char* nm, struct stats_info* s) 611 { 612 struct timeval avg; 613 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm, 614 (unsigned long)s->svr.num_queries)) return 0; 615 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm, 616 (unsigned long)(s->svr.num_queries 617 - s->svr.num_queries_missed_cache))) return 0; 618 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm, 619 (unsigned long)s->svr.num_queries_missed_cache)) return 0; 620 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm, 621 (unsigned long)s->svr.num_queries_prefetch)) return 0; 622 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm, 623 (unsigned long)s->mesh_replies_sent)) return 0; 624 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm, 625 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)? 626 (double)s->svr.sum_query_list_size/ 627 (s->svr.num_queries_missed_cache+ 628 s->svr.num_queries_prefetch) : 0.0)) return 0; 629 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm, 630 (unsigned long)s->svr.max_query_list_size)) return 0; 631 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm, 632 (unsigned long)s->mesh_jostled)) return 0; 633 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm, 634 (unsigned long)s->mesh_dropped)) return 0; 635 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm, 636 (unsigned long)s->mesh_num_states)) return 0; 637 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm, 638 (unsigned long)s->mesh_num_reply_states)) return 0; 639 timeval_divide(&avg, &s->mesh_replies_sum_wait, s->mesh_replies_sent); 640 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm, 641 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0; 642 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm, 643 s->mesh_time_median)) return 0; 644 return 1; 645 } 646 647 /** print stats for one thread */ 648 static int 649 print_thread_stats(SSL* ssl, int i, struct stats_info* s) 650 { 651 char nm[16]; 652 snprintf(nm, sizeof(nm), "thread%d", i); 653 nm[sizeof(nm)-1]=0; 654 return print_stats(ssl, nm, s); 655 } 656 657 /** print long number */ 658 static int 659 print_longnum(SSL* ssl, const char* desc, size_t x) 660 { 661 if(x > 1024*1024*1024) { 662 /* more than a Gb */ 663 size_t front = x / (size_t)1000000; 664 size_t back = x % (size_t)1000000; 665 return ssl_printf(ssl, "%s%u%6.6u\n", desc, 666 (unsigned)front, (unsigned)back); 667 } else { 668 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x); 669 } 670 } 671 672 /** print mem stats */ 673 static int 674 print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon) 675 { 676 int m; 677 size_t msg, rrset, val, iter; 678 #ifdef HAVE_SBRK 679 extern void* unbound_start_brk; 680 void* cur = sbrk(0); 681 if(!print_longnum(ssl, "mem.total.sbrk"SQ, 682 (size_t)((char*)cur - (char*)unbound_start_brk))) return 0; 683 #endif /* HAVE_SBRK */ 684 msg = slabhash_get_mem(daemon->env->msg_cache); 685 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table); 686 val=0; 687 iter=0; 688 m = modstack_find(&worker->env.mesh->mods, "validator"); 689 if(m != -1) { 690 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh-> 691 mods.mod[m]->get_mem)); 692 val = (*worker->env.mesh->mods.mod[m]->get_mem) 693 (&worker->env, m); 694 } 695 m = modstack_find(&worker->env.mesh->mods, "iterator"); 696 if(m != -1) { 697 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh-> 698 mods.mod[m]->get_mem)); 699 iter = (*worker->env.mesh->mods.mod[m]->get_mem) 700 (&worker->env, m); 701 } 702 703 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset)) 704 return 0; 705 if(!print_longnum(ssl, "mem.cache.message"SQ, msg)) 706 return 0; 707 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter)) 708 return 0; 709 if(!print_longnum(ssl, "mem.mod.validator"SQ, val)) 710 return 0; 711 return 1; 712 } 713 714 /** print uptime stats */ 715 static int 716 print_uptime(SSL* ssl, struct worker* worker, int reset) 717 { 718 struct timeval now = *worker->env.now_tv; 719 struct timeval up, dt; 720 timeval_subtract(&up, &now, &worker->daemon->time_boot); 721 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat); 722 if(reset) 723 worker->daemon->time_last_stat = now; 724 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n", 725 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0; 726 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n", 727 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0; 728 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n", 729 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0; 730 return 1; 731 } 732 733 /** print extended histogram */ 734 static int 735 print_hist(SSL* ssl, struct stats_info* s) 736 { 737 struct timehist* hist; 738 size_t i; 739 hist = timehist_setup(); 740 if(!hist) { 741 log_err("out of memory"); 742 return 0; 743 } 744 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST); 745 for(i=0; i<hist->num; i++) { 746 if(!ssl_printf(ssl, 747 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n", 748 (int)hist->buckets[i].lower.tv_sec, 749 (int)hist->buckets[i].lower.tv_usec, 750 (int)hist->buckets[i].upper.tv_sec, 751 (int)hist->buckets[i].upper.tv_usec, 752 (unsigned long)hist->buckets[i].count)) { 753 timehist_delete(hist); 754 return 0; 755 } 756 } 757 timehist_delete(hist); 758 return 1; 759 } 760 761 /** print extended stats */ 762 static int 763 print_ext(SSL* ssl, struct stats_info* s) 764 { 765 int i; 766 char nm[16]; 767 const sldns_rr_descriptor* desc; 768 const sldns_lookup_table* lt; 769 /* TYPE */ 770 for(i=0; i<STATS_QTYPE_NUM; i++) { 771 if(inhibit_zero && s->svr.qtype[i] == 0) 772 continue; 773 desc = sldns_rr_descript((uint16_t)i); 774 if(desc && desc->_name) { 775 snprintf(nm, sizeof(nm), "%s", desc->_name); 776 } else if (i == LDNS_RR_TYPE_IXFR) { 777 snprintf(nm, sizeof(nm), "IXFR"); 778 } else if (i == LDNS_RR_TYPE_AXFR) { 779 snprintf(nm, sizeof(nm), "AXFR"); 780 } else if (i == LDNS_RR_TYPE_MAILA) { 781 snprintf(nm, sizeof(nm), "MAILA"); 782 } else if (i == LDNS_RR_TYPE_MAILB) { 783 snprintf(nm, sizeof(nm), "MAILB"); 784 } else if (i == LDNS_RR_TYPE_ANY) { 785 snprintf(nm, sizeof(nm), "ANY"); 786 } else { 787 snprintf(nm, sizeof(nm), "TYPE%d", i); 788 } 789 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n", 790 nm, (unsigned long)s->svr.qtype[i])) return 0; 791 } 792 if(!inhibit_zero || s->svr.qtype_big) { 793 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n", 794 (unsigned long)s->svr.qtype_big)) return 0; 795 } 796 /* CLASS */ 797 for(i=0; i<STATS_QCLASS_NUM; i++) { 798 if(inhibit_zero && s->svr.qclass[i] == 0) 799 continue; 800 lt = sldns_lookup_by_id(sldns_rr_classes, i); 801 if(lt && lt->name) { 802 snprintf(nm, sizeof(nm), "%s", lt->name); 803 } else { 804 snprintf(nm, sizeof(nm), "CLASS%d", i); 805 } 806 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n", 807 nm, (unsigned long)s->svr.qclass[i])) return 0; 808 } 809 if(!inhibit_zero || s->svr.qclass_big) { 810 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n", 811 (unsigned long)s->svr.qclass_big)) return 0; 812 } 813 /* OPCODE */ 814 for(i=0; i<STATS_OPCODE_NUM; i++) { 815 if(inhibit_zero && s->svr.qopcode[i] == 0) 816 continue; 817 lt = sldns_lookup_by_id(sldns_opcodes, i); 818 if(lt && lt->name) { 819 snprintf(nm, sizeof(nm), "%s", lt->name); 820 } else { 821 snprintf(nm, sizeof(nm), "OPCODE%d", i); 822 } 823 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n", 824 nm, (unsigned long)s->svr.qopcode[i])) return 0; 825 } 826 /* transport */ 827 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n", 828 (unsigned long)s->svr.qtcp)) return 0; 829 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n", 830 (unsigned long)s->svr.qtcp_outgoing)) return 0; 831 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n", 832 (unsigned long)s->svr.qipv6)) return 0; 833 /* flags */ 834 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n", 835 (unsigned long)s->svr.qbit_QR)) return 0; 836 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n", 837 (unsigned long)s->svr.qbit_AA)) return 0; 838 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n", 839 (unsigned long)s->svr.qbit_TC)) return 0; 840 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n", 841 (unsigned long)s->svr.qbit_RD)) return 0; 842 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n", 843 (unsigned long)s->svr.qbit_RA)) return 0; 844 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n", 845 (unsigned long)s->svr.qbit_Z)) return 0; 846 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n", 847 (unsigned long)s->svr.qbit_AD)) return 0; 848 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n", 849 (unsigned long)s->svr.qbit_CD)) return 0; 850 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n", 851 (unsigned long)s->svr.qEDNS)) return 0; 852 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n", 853 (unsigned long)s->svr.qEDNS_DO)) return 0; 854 855 /* RCODE */ 856 for(i=0; i<STATS_RCODE_NUM; i++) { 857 /* Always include RCODEs 0-5 */ 858 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0) 859 continue; 860 lt = sldns_lookup_by_id(sldns_rcodes, i); 861 if(lt && lt->name) { 862 snprintf(nm, sizeof(nm), "%s", lt->name); 863 } else { 864 snprintf(nm, sizeof(nm), "RCODE%d", i); 865 } 866 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n", 867 nm, (unsigned long)s->svr.ans_rcode[i])) return 0; 868 } 869 if(!inhibit_zero || s->svr.ans_rcode_nodata) { 870 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n", 871 (unsigned long)s->svr.ans_rcode_nodata)) return 0; 872 } 873 /* validation */ 874 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n", 875 (unsigned long)s->svr.ans_secure)) return 0; 876 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n", 877 (unsigned long)s->svr.ans_bogus)) return 0; 878 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n", 879 (unsigned long)s->svr.rrset_bogus)) return 0; 880 /* threat detection */ 881 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n", 882 (unsigned long)s->svr.unwanted_queries)) return 0; 883 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n", 884 (unsigned long)s->svr.unwanted_replies)) return 0; 885 /* cache counts */ 886 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n", 887 (unsigned)s->svr.msg_cache_count)) return 0; 888 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n", 889 (unsigned)s->svr.rrset_cache_count)) return 0; 890 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n", 891 (unsigned)s->svr.infra_cache_count)) return 0; 892 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n", 893 (unsigned)s->svr.key_cache_count)) return 0; 894 return 1; 895 } 896 897 /** do the stats command */ 898 static void 899 do_stats(SSL* ssl, struct daemon_remote* rc, int reset) 900 { 901 struct daemon* daemon = rc->worker->daemon; 902 struct stats_info total; 903 struct stats_info s; 904 int i; 905 log_assert(daemon->num > 0); 906 /* gather all thread statistics in one place */ 907 for(i=0; i<daemon->num; i++) { 908 server_stats_obtain(rc->worker, daemon->workers[i], &s, reset); 909 if(!print_thread_stats(ssl, i, &s)) 910 return; 911 if(i == 0) 912 total = s; 913 else server_stats_add(&total, &s); 914 } 915 /* print the thread statistics */ 916 total.mesh_time_median /= (double)daemon->num; 917 if(!print_stats(ssl, "total", &total)) 918 return; 919 if(!print_uptime(ssl, rc->worker, reset)) 920 return; 921 if(daemon->cfg->stat_extended) { 922 if(!print_mem(ssl, rc->worker, daemon)) 923 return; 924 if(!print_hist(ssl, &total)) 925 return; 926 if(!print_ext(ssl, &total)) 927 return; 928 } 929 } 930 931 /** parse commandline argument domain name */ 932 static int 933 parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs) 934 { 935 uint8_t nm[LDNS_MAX_DOMAINLEN+1]; 936 size_t nmlen = sizeof(nm); 937 int status; 938 *res = NULL; 939 *len = 0; 940 *labs = 0; 941 status = sldns_str2wire_dname_buf(str, nm, &nmlen); 942 if(status != 0) { 943 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str, 944 LDNS_WIREPARSE_OFFSET(status), 945 sldns_get_errorstr_parse(status)); 946 return 0; 947 } 948 *res = memdup(nm, nmlen); 949 if(!*res) { 950 ssl_printf(ssl, "error out of memory\n"); 951 return 0; 952 } 953 *labs = dname_count_size_labels(*res, len); 954 return 1; 955 } 956 957 /** find second argument, modifies string */ 958 static int 959 find_arg2(SSL* ssl, char* arg, char** arg2) 960 { 961 char* as = strchr(arg, ' '); 962 char* at = strchr(arg, '\t'); 963 if(as && at) { 964 if(at < as) 965 as = at; 966 as[0]=0; 967 *arg2 = skipwhite(as+1); 968 } else if(as) { 969 as[0]=0; 970 *arg2 = skipwhite(as+1); 971 } else if(at) { 972 at[0]=0; 973 *arg2 = skipwhite(at+1); 974 } else { 975 ssl_printf(ssl, "error could not find next argument " 976 "after %s\n", arg); 977 return 0; 978 } 979 return 1; 980 } 981 982 /** Add a new zone */ 983 static void 984 do_zone_add(SSL* ssl, struct worker* worker, char* arg) 985 { 986 uint8_t* nm; 987 int nmlabs; 988 size_t nmlen; 989 char* arg2; 990 enum localzone_type t; 991 struct local_zone* z; 992 if(!find_arg2(ssl, arg, &arg2)) 993 return; 994 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 995 return; 996 if(!local_zone_str2type(arg2, &t)) { 997 ssl_printf(ssl, "error not a zone type. %s\n", arg2); 998 free(nm); 999 return; 1000 } 1001 lock_rw_wrlock(&worker->daemon->local_zones->lock); 1002 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen, 1003 nmlabs, LDNS_RR_CLASS_IN))) { 1004 /* already present in tree */ 1005 lock_rw_wrlock(&z->lock); 1006 z->type = t; /* update type anyway */ 1007 lock_rw_unlock(&z->lock); 1008 free(nm); 1009 lock_rw_unlock(&worker->daemon->local_zones->lock); 1010 send_ok(ssl); 1011 return; 1012 } 1013 if(!local_zones_add_zone(worker->daemon->local_zones, nm, nmlen, 1014 nmlabs, LDNS_RR_CLASS_IN, t)) { 1015 lock_rw_unlock(&worker->daemon->local_zones->lock); 1016 ssl_printf(ssl, "error out of memory\n"); 1017 return; 1018 } 1019 lock_rw_unlock(&worker->daemon->local_zones->lock); 1020 send_ok(ssl); 1021 } 1022 1023 /** Remove a zone */ 1024 static void 1025 do_zone_remove(SSL* ssl, struct worker* worker, char* arg) 1026 { 1027 uint8_t* nm; 1028 int nmlabs; 1029 size_t nmlen; 1030 struct local_zone* z; 1031 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1032 return; 1033 lock_rw_wrlock(&worker->daemon->local_zones->lock); 1034 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen, 1035 nmlabs, LDNS_RR_CLASS_IN))) { 1036 /* present in tree */ 1037 local_zones_del_zone(worker->daemon->local_zones, z); 1038 } 1039 lock_rw_unlock(&worker->daemon->local_zones->lock); 1040 free(nm); 1041 send_ok(ssl); 1042 } 1043 1044 /** Add new RR data */ 1045 static void 1046 do_data_add(SSL* ssl, struct worker* worker, char* arg) 1047 { 1048 if(!local_zones_add_RR(worker->daemon->local_zones, arg)) { 1049 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg); 1050 return; 1051 } 1052 send_ok(ssl); 1053 } 1054 1055 /** Remove RR data */ 1056 static void 1057 do_data_remove(SSL* ssl, struct worker* worker, char* arg) 1058 { 1059 uint8_t* nm; 1060 int nmlabs; 1061 size_t nmlen; 1062 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1063 return; 1064 local_zones_del_data(worker->daemon->local_zones, nm, 1065 nmlen, nmlabs, LDNS_RR_CLASS_IN); 1066 free(nm); 1067 send_ok(ssl); 1068 } 1069 1070 /** cache lookup of nameservers */ 1071 static void 1072 do_lookup(SSL* ssl, struct worker* worker, char* arg) 1073 { 1074 uint8_t* nm; 1075 int nmlabs; 1076 size_t nmlen; 1077 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1078 return; 1079 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs); 1080 free(nm); 1081 } 1082 1083 /** flush something from rrset and msg caches */ 1084 static void 1085 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen, 1086 uint16_t t, uint16_t c) 1087 { 1088 hashvalue_t h; 1089 struct query_info k; 1090 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0); 1091 if(t == LDNS_RR_TYPE_SOA) 1092 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 1093 PACKED_RRSET_SOA_NEG); 1094 k.qname = nm; 1095 k.qname_len = nmlen; 1096 k.qtype = t; 1097 k.qclass = c; 1098 h = query_info_hash(&k, 0); 1099 slabhash_remove(worker->env.msg_cache, h, &k); 1100 if(t == LDNS_RR_TYPE_AAAA) { 1101 /* for AAAA also flush dns64 bit_cd packet */ 1102 h = query_info_hash(&k, BIT_CD); 1103 slabhash_remove(worker->env.msg_cache, h, &k); 1104 } 1105 } 1106 1107 /** flush a type */ 1108 static void 1109 do_flush_type(SSL* ssl, struct worker* worker, char* arg) 1110 { 1111 uint8_t* nm; 1112 int nmlabs; 1113 size_t nmlen; 1114 char* arg2; 1115 uint16_t t; 1116 if(!find_arg2(ssl, arg, &arg2)) 1117 return; 1118 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1119 return; 1120 t = sldns_get_rr_type_by_name(arg2); 1121 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN); 1122 1123 free(nm); 1124 send_ok(ssl); 1125 } 1126 1127 /** flush statistics */ 1128 static void 1129 do_flush_stats(SSL* ssl, struct worker* worker) 1130 { 1131 worker_stats_clear(worker); 1132 send_ok(ssl); 1133 } 1134 1135 /** 1136 * Local info for deletion functions 1137 */ 1138 struct del_info { 1139 /** worker */ 1140 struct worker* worker; 1141 /** name to delete */ 1142 uint8_t* name; 1143 /** length */ 1144 size_t len; 1145 /** labels */ 1146 int labs; 1147 /** now */ 1148 time_t now; 1149 /** time to invalidate to */ 1150 time_t expired; 1151 /** number of rrsets removed */ 1152 size_t num_rrsets; 1153 /** number of msgs removed */ 1154 size_t num_msgs; 1155 /** number of key entries removed */ 1156 size_t num_keys; 1157 /** length of addr */ 1158 socklen_t addrlen; 1159 /** socket address for host deletion */ 1160 struct sockaddr_storage addr; 1161 }; 1162 1163 /** callback to delete hosts in infra cache */ 1164 static void 1165 infra_del_host(struct lruhash_entry* e, void* arg) 1166 { 1167 /* entry is locked */ 1168 struct del_info* inf = (struct del_info*)arg; 1169 struct infra_key* k = (struct infra_key*)e->key; 1170 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) { 1171 struct infra_data* d = (struct infra_data*)e->data; 1172 d->probedelay = 0; 1173 d->timeout_A = 0; 1174 d->timeout_AAAA = 0; 1175 d->timeout_other = 0; 1176 rtt_init(&d->rtt); 1177 if(d->ttl >= inf->now) { 1178 d->ttl = inf->expired; 1179 inf->num_keys++; 1180 } 1181 } 1182 } 1183 1184 /** flush infra cache */ 1185 static void 1186 do_flush_infra(SSL* ssl, struct worker* worker, char* arg) 1187 { 1188 struct sockaddr_storage addr; 1189 socklen_t len; 1190 struct del_info inf; 1191 if(strcmp(arg, "all") == 0) { 1192 slabhash_clear(worker->env.infra_cache->hosts); 1193 send_ok(ssl); 1194 return; 1195 } 1196 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) { 1197 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg); 1198 return; 1199 } 1200 /* delete all entries from cache */ 1201 /* what we do is to set them all expired */ 1202 inf.worker = worker; 1203 inf.name = 0; 1204 inf.len = 0; 1205 inf.labs = 0; 1206 inf.now = *worker->env.now; 1207 inf.expired = *worker->env.now; 1208 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1209 inf.num_rrsets = 0; 1210 inf.num_msgs = 0; 1211 inf.num_keys = 0; 1212 inf.addrlen = len; 1213 memmove(&inf.addr, &addr, len); 1214 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host, 1215 &inf); 1216 send_ok(ssl); 1217 } 1218 1219 /** flush requestlist */ 1220 static void 1221 do_flush_requestlist(SSL* ssl, struct worker* worker) 1222 { 1223 mesh_delete_all(worker->env.mesh); 1224 send_ok(ssl); 1225 } 1226 1227 /** callback to delete rrsets in a zone */ 1228 static void 1229 zone_del_rrset(struct lruhash_entry* e, void* arg) 1230 { 1231 /* entry is locked */ 1232 struct del_info* inf = (struct del_info*)arg; 1233 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 1234 if(dname_subdomain_c(k->rk.dname, inf->name)) { 1235 struct packed_rrset_data* d = 1236 (struct packed_rrset_data*)e->data; 1237 if(d->ttl >= inf->now) { 1238 d->ttl = inf->expired; 1239 inf->num_rrsets++; 1240 } 1241 } 1242 } 1243 1244 /** callback to delete messages in a zone */ 1245 static void 1246 zone_del_msg(struct lruhash_entry* e, void* arg) 1247 { 1248 /* entry is locked */ 1249 struct del_info* inf = (struct del_info*)arg; 1250 struct msgreply_entry* k = (struct msgreply_entry*)e->key; 1251 if(dname_subdomain_c(k->key.qname, inf->name)) { 1252 struct reply_info* d = (struct reply_info*)e->data; 1253 if(d->ttl >= inf->now) { 1254 d->ttl = inf->expired; 1255 inf->num_msgs++; 1256 } 1257 } 1258 } 1259 1260 /** callback to delete keys in zone */ 1261 static void 1262 zone_del_kcache(struct lruhash_entry* e, void* arg) 1263 { 1264 /* entry is locked */ 1265 struct del_info* inf = (struct del_info*)arg; 1266 struct key_entry_key* k = (struct key_entry_key*)e->key; 1267 if(dname_subdomain_c(k->name, inf->name)) { 1268 struct key_entry_data* d = (struct key_entry_data*)e->data; 1269 if(d->ttl >= inf->now) { 1270 d->ttl = inf->expired; 1271 inf->num_keys++; 1272 } 1273 } 1274 } 1275 1276 /** remove all rrsets and keys from zone from cache */ 1277 static void 1278 do_flush_zone(SSL* ssl, struct worker* worker, char* arg) 1279 { 1280 uint8_t* nm; 1281 int nmlabs; 1282 size_t nmlen; 1283 struct del_info inf; 1284 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1285 return; 1286 /* delete all RRs and key entries from zone */ 1287 /* what we do is to set them all expired */ 1288 inf.worker = worker; 1289 inf.name = nm; 1290 inf.len = nmlen; 1291 inf.labs = nmlabs; 1292 inf.now = *worker->env.now; 1293 inf.expired = *worker->env.now; 1294 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1295 inf.num_rrsets = 0; 1296 inf.num_msgs = 0; 1297 inf.num_keys = 0; 1298 slabhash_traverse(&worker->env.rrset_cache->table, 1, 1299 &zone_del_rrset, &inf); 1300 1301 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf); 1302 1303 /* and validator cache */ 1304 if(worker->env.key_cache) { 1305 slabhash_traverse(worker->env.key_cache->slab, 1, 1306 &zone_del_kcache, &inf); 1307 } 1308 1309 free(nm); 1310 1311 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1312 "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1313 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1314 } 1315 1316 /** callback to delete bogus rrsets */ 1317 static void 1318 bogus_del_rrset(struct lruhash_entry* e, void* arg) 1319 { 1320 /* entry is locked */ 1321 struct del_info* inf = (struct del_info*)arg; 1322 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 1323 if(d->security == sec_status_bogus) { 1324 d->ttl = inf->expired; 1325 inf->num_rrsets++; 1326 } 1327 } 1328 1329 /** callback to delete bogus messages */ 1330 static void 1331 bogus_del_msg(struct lruhash_entry* e, void* arg) 1332 { 1333 /* entry is locked */ 1334 struct del_info* inf = (struct del_info*)arg; 1335 struct reply_info* d = (struct reply_info*)e->data; 1336 if(d->security == sec_status_bogus) { 1337 d->ttl = inf->expired; 1338 inf->num_msgs++; 1339 } 1340 } 1341 1342 /** callback to delete bogus keys */ 1343 static void 1344 bogus_del_kcache(struct lruhash_entry* e, void* arg) 1345 { 1346 /* entry is locked */ 1347 struct del_info* inf = (struct del_info*)arg; 1348 struct key_entry_data* d = (struct key_entry_data*)e->data; 1349 if(d->isbad) { 1350 d->ttl = inf->expired; 1351 inf->num_keys++; 1352 } 1353 } 1354 1355 /** remove all bogus rrsets, msgs and keys from cache */ 1356 static void 1357 do_flush_bogus(SSL* ssl, struct worker* worker) 1358 { 1359 struct del_info inf; 1360 /* what we do is to set them all expired */ 1361 inf.worker = worker; 1362 inf.now = *worker->env.now; 1363 inf.expired = *worker->env.now; 1364 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1365 inf.num_rrsets = 0; 1366 inf.num_msgs = 0; 1367 inf.num_keys = 0; 1368 slabhash_traverse(&worker->env.rrset_cache->table, 1, 1369 &bogus_del_rrset, &inf); 1370 1371 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf); 1372 1373 /* and validator cache */ 1374 if(worker->env.key_cache) { 1375 slabhash_traverse(worker->env.key_cache->slab, 1, 1376 &bogus_del_kcache, &inf); 1377 } 1378 1379 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1380 "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1381 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1382 } 1383 1384 /** callback to delete negative and servfail rrsets */ 1385 static void 1386 negative_del_rrset(struct lruhash_entry* e, void* arg) 1387 { 1388 /* entry is locked */ 1389 struct del_info* inf = (struct del_info*)arg; 1390 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 1391 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 1392 /* delete the parentside negative cache rrsets, 1393 * these are namerserver rrsets that failed lookup, rdata empty */ 1394 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 && 1395 d->rrsig_count == 0 && d->rr_len[0] == 0) { 1396 d->ttl = inf->expired; 1397 inf->num_rrsets++; 1398 } 1399 } 1400 1401 /** callback to delete negative and servfail messages */ 1402 static void 1403 negative_del_msg(struct lruhash_entry* e, void* arg) 1404 { 1405 /* entry is locked */ 1406 struct del_info* inf = (struct del_info*)arg; 1407 struct reply_info* d = (struct reply_info*)e->data; 1408 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error 1409 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */ 1410 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) { 1411 d->ttl = inf->expired; 1412 inf->num_msgs++; 1413 } 1414 } 1415 1416 /** callback to delete negative key entries */ 1417 static void 1418 negative_del_kcache(struct lruhash_entry* e, void* arg) 1419 { 1420 /* entry is locked */ 1421 struct del_info* inf = (struct del_info*)arg; 1422 struct key_entry_data* d = (struct key_entry_data*)e->data; 1423 /* could be bad because of lookup failure on the DS, DNSKEY, which 1424 * was nxdomain or servfail, and thus a result of negative lookups */ 1425 if(d->isbad) { 1426 d->ttl = inf->expired; 1427 inf->num_keys++; 1428 } 1429 } 1430 1431 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */ 1432 static void 1433 do_flush_negative(SSL* ssl, struct worker* worker) 1434 { 1435 struct del_info inf; 1436 /* what we do is to set them all expired */ 1437 inf.worker = worker; 1438 inf.now = *worker->env.now; 1439 inf.expired = *worker->env.now; 1440 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1441 inf.num_rrsets = 0; 1442 inf.num_msgs = 0; 1443 inf.num_keys = 0; 1444 slabhash_traverse(&worker->env.rrset_cache->table, 1, 1445 &negative_del_rrset, &inf); 1446 1447 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf); 1448 1449 /* and validator cache */ 1450 if(worker->env.key_cache) { 1451 slabhash_traverse(worker->env.key_cache->slab, 1, 1452 &negative_del_kcache, &inf); 1453 } 1454 1455 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1456 "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1457 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1458 } 1459 1460 /** remove name rrset from cache */ 1461 static void 1462 do_flush_name(SSL* ssl, struct worker* w, char* arg) 1463 { 1464 uint8_t* nm; 1465 int nmlabs; 1466 size_t nmlen; 1467 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1468 return; 1469 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN); 1470 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN); 1471 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN); 1472 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN); 1473 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN); 1474 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN); 1475 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN); 1476 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN); 1477 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN); 1478 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN); 1479 1480 free(nm); 1481 send_ok(ssl); 1482 } 1483 1484 /** printout a delegation point info */ 1485 static int 1486 ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass, 1487 struct delegpt* dp) 1488 { 1489 char buf[257]; 1490 struct delegpt_ns* ns; 1491 struct delegpt_addr* a; 1492 int f = 0; 1493 if(str) { /* print header for forward, stub */ 1494 char* c = sldns_wire2str_class(dclass); 1495 dname_str(nm, buf); 1496 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) { 1497 free(c); 1498 return 0; 1499 } 1500 free(c); 1501 } 1502 for(ns = dp->nslist; ns; ns = ns->next) { 1503 dname_str(ns->name, buf); 1504 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 1505 return 0; 1506 f = 1; 1507 } 1508 for(a = dp->target_list; a; a = a->next_target) { 1509 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf)); 1510 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 1511 return 0; 1512 f = 1; 1513 } 1514 return ssl_printf(ssl, "\n"); 1515 } 1516 1517 1518 /** print root forwards */ 1519 static int 1520 print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root) 1521 { 1522 struct delegpt* dp; 1523 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN); 1524 if(!dp) 1525 return ssl_printf(ssl, "off (using root hints)\n"); 1526 /* if dp is returned it must be the root */ 1527 log_assert(query_dname_compare(dp->name, root)==0); 1528 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp); 1529 } 1530 1531 /** parse args into delegpt */ 1532 static struct delegpt* 1533 parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names) 1534 { 1535 /* parse args and add in */ 1536 char* p = args; 1537 char* todo; 1538 struct delegpt* dp = delegpt_create_mlc(nm); 1539 struct sockaddr_storage addr; 1540 socklen_t addrlen; 1541 if(!dp) { 1542 (void)ssl_printf(ssl, "error out of memory\n"); 1543 return NULL; 1544 } 1545 while(p) { 1546 todo = p; 1547 p = strchr(p, ' '); /* find next spot, if any */ 1548 if(p) { 1549 *p++ = 0; /* end this spot */ 1550 p = skipwhite(p); /* position at next spot */ 1551 } 1552 /* parse address */ 1553 if(!extstrtoaddr(todo, &addr, &addrlen)) { 1554 if(allow_names) { 1555 uint8_t* n = NULL; 1556 size_t ln; 1557 int lb; 1558 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) { 1559 (void)ssl_printf(ssl, "error cannot " 1560 "parse IP address or name " 1561 "'%s'\n", todo); 1562 delegpt_free_mlc(dp); 1563 return NULL; 1564 } 1565 if(!delegpt_add_ns_mlc(dp, n, 0)) { 1566 (void)ssl_printf(ssl, "error out of memory\n"); 1567 free(n); 1568 delegpt_free_mlc(dp); 1569 return NULL; 1570 } 1571 free(n); 1572 1573 } else { 1574 (void)ssl_printf(ssl, "error cannot parse" 1575 " IP address '%s'\n", todo); 1576 delegpt_free_mlc(dp); 1577 return NULL; 1578 } 1579 } else { 1580 /* add address */ 1581 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) { 1582 (void)ssl_printf(ssl, "error out of memory\n"); 1583 delegpt_free_mlc(dp); 1584 return NULL; 1585 } 1586 } 1587 } 1588 return dp; 1589 } 1590 1591 /** do the status command */ 1592 static void 1593 do_forward(SSL* ssl, struct worker* worker, char* args) 1594 { 1595 struct iter_forwards* fwd = worker->env.fwds; 1596 uint8_t* root = (uint8_t*)"\000"; 1597 if(!fwd) { 1598 (void)ssl_printf(ssl, "error: structure not allocated\n"); 1599 return; 1600 } 1601 if(args == NULL || args[0] == 0) { 1602 (void)print_root_fwds(ssl, fwd, root); 1603 return; 1604 } 1605 /* set root forwards for this thread. since we are in remote control 1606 * the actual mesh is not running, so we can freely edit it. */ 1607 /* delete all the existing queries first */ 1608 mesh_delete_all(worker->env.mesh); 1609 if(strcmp(args, "off") == 0) { 1610 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root); 1611 } else { 1612 struct delegpt* dp; 1613 if(!(dp = parse_delegpt(ssl, args, root, 0))) 1614 return; 1615 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) { 1616 (void)ssl_printf(ssl, "error out of memory\n"); 1617 return; 1618 } 1619 } 1620 send_ok(ssl); 1621 } 1622 1623 static int 1624 parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp, 1625 int* insecure, int* prime) 1626 { 1627 char* zonename; 1628 char* rest; 1629 size_t nmlen; 1630 int nmlabs; 1631 /* parse all -x args */ 1632 while(args[0] == '+') { 1633 if(!find_arg2(ssl, args, &rest)) 1634 return 0; 1635 while(*(++args) != 0) { 1636 if(*args == 'i' && insecure) 1637 *insecure = 1; 1638 else if(*args == 'p' && prime) 1639 *prime = 1; 1640 else { 1641 (void)ssl_printf(ssl, "error: unknown option %s\n", args); 1642 return 0; 1643 } 1644 } 1645 args = rest; 1646 } 1647 /* parse name */ 1648 if(dp) { 1649 if(!find_arg2(ssl, args, &rest)) 1650 return 0; 1651 zonename = args; 1652 args = rest; 1653 } else zonename = args; 1654 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs)) 1655 return 0; 1656 1657 /* parse dp */ 1658 if(dp) { 1659 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) { 1660 free(*nm); 1661 return 0; 1662 } 1663 } 1664 return 1; 1665 } 1666 1667 /** do the forward_add command */ 1668 static void 1669 do_forward_add(SSL* ssl, struct worker* worker, char* args) 1670 { 1671 struct iter_forwards* fwd = worker->env.fwds; 1672 int insecure = 0; 1673 uint8_t* nm = NULL; 1674 struct delegpt* dp = NULL; 1675 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL)) 1676 return; 1677 if(insecure && worker->env.anchors) { 1678 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 1679 nm)) { 1680 (void)ssl_printf(ssl, "error out of memory\n"); 1681 delegpt_free_mlc(dp); 1682 free(nm); 1683 return; 1684 } 1685 } 1686 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) { 1687 (void)ssl_printf(ssl, "error out of memory\n"); 1688 free(nm); 1689 return; 1690 } 1691 free(nm); 1692 send_ok(ssl); 1693 } 1694 1695 /** do the forward_remove command */ 1696 static void 1697 do_forward_remove(SSL* ssl, struct worker* worker, char* args) 1698 { 1699 struct iter_forwards* fwd = worker->env.fwds; 1700 int insecure = 0; 1701 uint8_t* nm = NULL; 1702 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL)) 1703 return; 1704 if(insecure && worker->env.anchors) 1705 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 1706 nm); 1707 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm); 1708 free(nm); 1709 send_ok(ssl); 1710 } 1711 1712 /** do the stub_add command */ 1713 static void 1714 do_stub_add(SSL* ssl, struct worker* worker, char* args) 1715 { 1716 struct iter_forwards* fwd = worker->env.fwds; 1717 int insecure = 0, prime = 0; 1718 uint8_t* nm = NULL; 1719 struct delegpt* dp = NULL; 1720 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime)) 1721 return; 1722 if(insecure && worker->env.anchors) { 1723 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 1724 nm)) { 1725 (void)ssl_printf(ssl, "error out of memory\n"); 1726 delegpt_free_mlc(dp); 1727 free(nm); 1728 return; 1729 } 1730 } 1731 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) { 1732 if(insecure && worker->env.anchors) 1733 anchors_delete_insecure(worker->env.anchors, 1734 LDNS_RR_CLASS_IN, nm); 1735 (void)ssl_printf(ssl, "error out of memory\n"); 1736 delegpt_free_mlc(dp); 1737 free(nm); 1738 return; 1739 } 1740 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) { 1741 (void)ssl_printf(ssl, "error out of memory\n"); 1742 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm); 1743 if(insecure && worker->env.anchors) 1744 anchors_delete_insecure(worker->env.anchors, 1745 LDNS_RR_CLASS_IN, nm); 1746 free(nm); 1747 return; 1748 } 1749 free(nm); 1750 send_ok(ssl); 1751 } 1752 1753 /** do the stub_remove command */ 1754 static void 1755 do_stub_remove(SSL* ssl, struct worker* worker, char* args) 1756 { 1757 struct iter_forwards* fwd = worker->env.fwds; 1758 int insecure = 0; 1759 uint8_t* nm = NULL; 1760 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL)) 1761 return; 1762 if(insecure && worker->env.anchors) 1763 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 1764 nm); 1765 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm); 1766 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm); 1767 free(nm); 1768 send_ok(ssl); 1769 } 1770 1771 /** do the insecure_add command */ 1772 static void 1773 do_insecure_add(SSL* ssl, struct worker* worker, char* arg) 1774 { 1775 size_t nmlen; 1776 int nmlabs; 1777 uint8_t* nm = NULL; 1778 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1779 return; 1780 if(worker->env.anchors) { 1781 if(!anchors_add_insecure(worker->env.anchors, 1782 LDNS_RR_CLASS_IN, nm)) { 1783 (void)ssl_printf(ssl, "error out of memory\n"); 1784 free(nm); 1785 return; 1786 } 1787 } 1788 free(nm); 1789 send_ok(ssl); 1790 } 1791 1792 /** do the insecure_remove command */ 1793 static void 1794 do_insecure_remove(SSL* ssl, struct worker* worker, char* arg) 1795 { 1796 size_t nmlen; 1797 int nmlabs; 1798 uint8_t* nm = NULL; 1799 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1800 return; 1801 if(worker->env.anchors) 1802 anchors_delete_insecure(worker->env.anchors, 1803 LDNS_RR_CLASS_IN, nm); 1804 free(nm); 1805 send_ok(ssl); 1806 } 1807 1808 /** do the status command */ 1809 static void 1810 do_status(SSL* ssl, struct worker* worker) 1811 { 1812 int i; 1813 time_t uptime; 1814 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION)) 1815 return; 1816 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity)) 1817 return; 1818 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num)) 1819 return; 1820 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num)) 1821 return; 1822 for(i=0; i<worker->daemon->mods.num; i++) { 1823 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name)) 1824 return; 1825 } 1826 if(!ssl_printf(ssl, " ]\n")) 1827 return; 1828 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec; 1829 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime)) 1830 return; 1831 if(!ssl_printf(ssl, "options:%s%s\n" , 1832 (worker->daemon->reuseport?" reuseport":""), 1833 (worker->daemon->rc->accept_list?" control(ssl)":""))) 1834 return; 1835 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n", 1836 (int)getpid())) 1837 return; 1838 } 1839 1840 /** get age for the mesh state */ 1841 static void 1842 get_mesh_age(struct mesh_state* m, char* buf, size_t len, 1843 struct module_env* env) 1844 { 1845 if(m->reply_list) { 1846 struct timeval d; 1847 struct mesh_reply* r = m->reply_list; 1848 /* last reply is the oldest */ 1849 while(r && r->next) 1850 r = r->next; 1851 timeval_subtract(&d, env->now_tv, &r->start_time); 1852 snprintf(buf, len, ARG_LL "d.%6.6d", 1853 (long long)d.tv_sec, (int)d.tv_usec); 1854 } else { 1855 snprintf(buf, len, "-"); 1856 } 1857 } 1858 1859 /** get status of a mesh state */ 1860 static void 1861 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m, 1862 char* buf, size_t len) 1863 { 1864 enum module_ext_state s = m->s.ext_state[m->s.curmod]; 1865 const char *modname = mesh->mods.mod[m->s.curmod]->name; 1866 size_t l; 1867 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply && 1868 m->s.minfo[m->s.curmod]) { 1869 /* break into iterator to find out who its waiting for */ 1870 struct iter_qstate* qstate = (struct iter_qstate*) 1871 m->s.minfo[m->s.curmod]; 1872 struct outbound_list* ol = &qstate->outlist; 1873 struct outbound_entry* e; 1874 snprintf(buf, len, "%s wait for", modname); 1875 l = strlen(buf); 1876 buf += l; len -= l; 1877 if(ol->first == NULL) 1878 snprintf(buf, len, " (empty_list)"); 1879 for(e = ol->first; e; e = e->next) { 1880 snprintf(buf, len, " "); 1881 l = strlen(buf); 1882 buf += l; len -= l; 1883 addr_to_str(&e->qsent->addr, e->qsent->addrlen, 1884 buf, len); 1885 l = strlen(buf); 1886 buf += l; len -= l; 1887 } 1888 } else if(s == module_wait_subquery) { 1889 /* look in subs from mesh state to see what */ 1890 char nm[257]; 1891 struct mesh_state_ref* sub; 1892 snprintf(buf, len, "%s wants", modname); 1893 l = strlen(buf); 1894 buf += l; len -= l; 1895 if(m->sub_set.count == 0) 1896 snprintf(buf, len, " (empty_list)"); 1897 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) { 1898 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype); 1899 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass); 1900 dname_str(sub->s->s.qinfo.qname, nm); 1901 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"), 1902 (c?c:"CLASS??"), nm); 1903 l = strlen(buf); 1904 buf += l; len -= l; 1905 free(t); 1906 free(c); 1907 } 1908 } else { 1909 snprintf(buf, len, "%s is %s", modname, strextstate(s)); 1910 } 1911 } 1912 1913 /** do the dump_requestlist command */ 1914 static void 1915 do_dump_requestlist(SSL* ssl, struct worker* worker) 1916 { 1917 struct mesh_area* mesh; 1918 struct mesh_state* m; 1919 int num = 0; 1920 char buf[257]; 1921 char timebuf[32]; 1922 char statbuf[10240]; 1923 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num)) 1924 return; 1925 if(!ssl_printf(ssl, "# type cl name seconds module status\n")) 1926 return; 1927 /* show worker mesh contents */ 1928 mesh = worker->env.mesh; 1929 if(!mesh) return; 1930 RBTREE_FOR(m, struct mesh_state*, &mesh->all) { 1931 char* t = sldns_wire2str_type(m->s.qinfo.qtype); 1932 char* c = sldns_wire2str_class(m->s.qinfo.qclass); 1933 dname_str(m->s.qinfo.qname, buf); 1934 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env); 1935 get_mesh_status(mesh, m, statbuf, sizeof(statbuf)); 1936 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n", 1937 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf, 1938 statbuf)) { 1939 free(t); 1940 free(c); 1941 return; 1942 } 1943 num++; 1944 free(t); 1945 free(c); 1946 } 1947 } 1948 1949 /** structure for argument data for dump infra host */ 1950 struct infra_arg { 1951 /** the infra cache */ 1952 struct infra_cache* infra; 1953 /** the SSL connection */ 1954 SSL* ssl; 1955 /** the time now */ 1956 time_t now; 1957 /** ssl failure? stop writing and skip the rest. If the tcp 1958 * connection is broken, and writes fail, we then stop writing. */ 1959 int ssl_failed; 1960 }; 1961 1962 /** callback for every host element in the infra cache */ 1963 static void 1964 dump_infra_host(struct lruhash_entry* e, void* arg) 1965 { 1966 struct infra_arg* a = (struct infra_arg*)arg; 1967 struct infra_key* k = (struct infra_key*)e->key; 1968 struct infra_data* d = (struct infra_data*)e->data; 1969 char ip_str[1024]; 1970 char name[257]; 1971 if(a->ssl_failed) 1972 return; 1973 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str)); 1974 dname_str(k->zonename, name); 1975 /* skip expired stuff (only backed off) */ 1976 if(d->ttl < a->now) { 1977 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) { 1978 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str, 1979 name, d->rtt.rto)) { 1980 a->ssl_failed = 1; 1981 return; 1982 } 1983 } 1984 return; 1985 } 1986 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d " 1987 "tA %d tAAAA %d tother %d " 1988 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d " 1989 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now), 1990 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto, 1991 d->timeout_A, d->timeout_AAAA, d->timeout_other, 1992 (int)d->edns_lame_known, (int)d->edns_version, 1993 (int)(a->now<d->probedelay?d->probedelay-a->now:0), 1994 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A, 1995 (int)d->lame_other)) { 1996 a->ssl_failed = 1; 1997 return; 1998 } 1999 } 2000 2001 /** do the dump_infra command */ 2002 static void 2003 do_dump_infra(SSL* ssl, struct worker* worker) 2004 { 2005 struct infra_arg arg; 2006 arg.infra = worker->env.infra_cache; 2007 arg.ssl = ssl; 2008 arg.now = *worker->env.now; 2009 arg.ssl_failed = 0; 2010 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg); 2011 } 2012 2013 /** do the log_reopen command */ 2014 static void 2015 do_log_reopen(SSL* ssl, struct worker* worker) 2016 { 2017 struct config_file* cfg = worker->env.cfg; 2018 send_ok(ssl); 2019 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir); 2020 } 2021 2022 /** do the set_option command */ 2023 static void 2024 do_set_option(SSL* ssl, struct worker* worker, char* arg) 2025 { 2026 char* arg2; 2027 if(!find_arg2(ssl, arg, &arg2)) 2028 return; 2029 if(!config_set_option(worker->env.cfg, arg, arg2)) { 2030 (void)ssl_printf(ssl, "error setting option\n"); 2031 return; 2032 } 2033 send_ok(ssl); 2034 } 2035 2036 /* routine to printout option values over SSL */ 2037 void remote_get_opt_ssl(char* line, void* arg) 2038 { 2039 SSL* ssl = (SSL*)arg; 2040 (void)ssl_printf(ssl, "%s\n", line); 2041 } 2042 2043 /** do the get_option command */ 2044 static void 2045 do_get_option(SSL* ssl, struct worker* worker, char* arg) 2046 { 2047 int r; 2048 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl); 2049 if(!r) { 2050 (void)ssl_printf(ssl, "error unknown option\n"); 2051 return; 2052 } 2053 } 2054 2055 /** do the list_forwards command */ 2056 static void 2057 do_list_forwards(SSL* ssl, struct worker* worker) 2058 { 2059 /* since its a per-worker structure no locks needed */ 2060 struct iter_forwards* fwds = worker->env.fwds; 2061 struct iter_forward_zone* z; 2062 struct trust_anchor* a; 2063 int insecure; 2064 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) { 2065 if(!z->dp) continue; /* skip empty marker for stub */ 2066 2067 /* see if it is insecure */ 2068 insecure = 0; 2069 if(worker->env.anchors && 2070 (a=anchor_find(worker->env.anchors, z->name, 2071 z->namelabs, z->namelen, z->dclass))) { 2072 if(!a->keylist && !a->numDS && !a->numDNSKEY) 2073 insecure = 1; 2074 lock_basic_unlock(&a->lock); 2075 } 2076 2077 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"), 2078 z->name, z->dclass, z->dp)) 2079 return; 2080 } 2081 } 2082 2083 /** do the list_stubs command */ 2084 static void 2085 do_list_stubs(SSL* ssl, struct worker* worker) 2086 { 2087 struct iter_hints_stub* z; 2088 struct trust_anchor* a; 2089 int insecure; 2090 char str[32]; 2091 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) { 2092 2093 /* see if it is insecure */ 2094 insecure = 0; 2095 if(worker->env.anchors && 2096 (a=anchor_find(worker->env.anchors, z->node.name, 2097 z->node.labs, z->node.len, z->node.dclass))) { 2098 if(!a->keylist && !a->numDS && !a->numDNSKEY) 2099 insecure = 1; 2100 lock_basic_unlock(&a->lock); 2101 } 2102 2103 snprintf(str, sizeof(str), "stub %sprime%s", 2104 (z->noprime?"no":""), (insecure?" +i":"")); 2105 if(!ssl_print_name_dp(ssl, str, z->node.name, 2106 z->node.dclass, z->dp)) 2107 return; 2108 } 2109 } 2110 2111 /** do the list_local_zones command */ 2112 static void 2113 do_list_local_zones(SSL* ssl, struct worker* worker) 2114 { 2115 struct local_zones* zones = worker->daemon->local_zones; 2116 struct local_zone* z; 2117 char buf[257]; 2118 lock_rw_rdlock(&zones->lock); 2119 RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2120 lock_rw_rdlock(&z->lock); 2121 dname_str(z->name, buf); 2122 if(!ssl_printf(ssl, "%s %s\n", buf, 2123 local_zone_type2str(z->type))) { 2124 /* failure to print */ 2125 lock_rw_unlock(&z->lock); 2126 lock_rw_unlock(&zones->lock); 2127 return; 2128 } 2129 lock_rw_unlock(&z->lock); 2130 } 2131 lock_rw_unlock(&zones->lock); 2132 } 2133 2134 /** do the list_local_data command */ 2135 static void 2136 do_list_local_data(SSL* ssl, struct worker* worker) 2137 { 2138 struct local_zones* zones = worker->daemon->local_zones; 2139 struct local_zone* z; 2140 struct local_data* d; 2141 struct local_rrset* p; 2142 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer); 2143 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer); 2144 lock_rw_rdlock(&zones->lock); 2145 RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2146 lock_rw_rdlock(&z->lock); 2147 RBTREE_FOR(d, struct local_data*, &z->data) { 2148 for(p = d->rrsets; p; p = p->next) { 2149 struct packed_rrset_data* d = 2150 (struct packed_rrset_data*)p->rrset->entry.data; 2151 size_t i; 2152 for(i=0; i<d->count + d->rrsig_count; i++) { 2153 if(!packed_rr_to_string(p->rrset, i, 2154 0, s, slen)) { 2155 if(!ssl_printf(ssl, "BADRR\n")) 2156 return; 2157 } 2158 if(!ssl_printf(ssl, "%s\n", s)) 2159 return; 2160 } 2161 } 2162 } 2163 lock_rw_unlock(&z->lock); 2164 } 2165 lock_rw_unlock(&zones->lock); 2166 } 2167 2168 /** tell other processes to execute the command */ 2169 static void 2170 distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd) 2171 { 2172 int i; 2173 if(!cmd || !ssl) 2174 return; 2175 /* skip i=0 which is me */ 2176 for(i=1; i<rc->worker->daemon->num; i++) { 2177 worker_send_cmd(rc->worker->daemon->workers[i], 2178 worker_cmd_remote); 2179 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd, 2180 (uint8_t*)cmd, strlen(cmd)+1, 0)) { 2181 ssl_printf(ssl, "error could not distribute cmd\n"); 2182 return; 2183 } 2184 } 2185 } 2186 2187 /** check for name with end-of-string, space or tab after it */ 2188 static int 2189 cmdcmp(char* p, const char* cmd, size_t len) 2190 { 2191 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t'); 2192 } 2193 2194 /** execute a remote control command */ 2195 static void 2196 execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd, 2197 struct worker* worker) 2198 { 2199 char* p = skipwhite(cmd); 2200 /* compare command */ 2201 if(cmdcmp(p, "stop", 4)) { 2202 do_stop(ssl, rc); 2203 return; 2204 } else if(cmdcmp(p, "reload", 6)) { 2205 do_reload(ssl, rc); 2206 return; 2207 } else if(cmdcmp(p, "stats_noreset", 13)) { 2208 do_stats(ssl, rc, 0); 2209 return; 2210 } else if(cmdcmp(p, "stats", 5)) { 2211 do_stats(ssl, rc, 1); 2212 return; 2213 } else if(cmdcmp(p, "status", 6)) { 2214 do_status(ssl, worker); 2215 return; 2216 } else if(cmdcmp(p, "dump_cache", 10)) { 2217 (void)dump_cache(ssl, worker); 2218 return; 2219 } else if(cmdcmp(p, "load_cache", 10)) { 2220 if(load_cache(ssl, worker)) send_ok(ssl); 2221 return; 2222 } else if(cmdcmp(p, "list_forwards", 13)) { 2223 do_list_forwards(ssl, worker); 2224 return; 2225 } else if(cmdcmp(p, "list_stubs", 10)) { 2226 do_list_stubs(ssl, worker); 2227 return; 2228 } else if(cmdcmp(p, "list_local_zones", 16)) { 2229 do_list_local_zones(ssl, worker); 2230 return; 2231 } else if(cmdcmp(p, "list_local_data", 15)) { 2232 do_list_local_data(ssl, worker); 2233 return; 2234 } else if(cmdcmp(p, "stub_add", 8)) { 2235 /* must always distribute this cmd */ 2236 if(rc) distribute_cmd(rc, ssl, cmd); 2237 do_stub_add(ssl, worker, skipwhite(p+8)); 2238 return; 2239 } else if(cmdcmp(p, "stub_remove", 11)) { 2240 /* must always distribute this cmd */ 2241 if(rc) distribute_cmd(rc, ssl, cmd); 2242 do_stub_remove(ssl, worker, skipwhite(p+11)); 2243 return; 2244 } else if(cmdcmp(p, "forward_add", 11)) { 2245 /* must always distribute this cmd */ 2246 if(rc) distribute_cmd(rc, ssl, cmd); 2247 do_forward_add(ssl, worker, skipwhite(p+11)); 2248 return; 2249 } else if(cmdcmp(p, "forward_remove", 14)) { 2250 /* must always distribute this cmd */ 2251 if(rc) distribute_cmd(rc, ssl, cmd); 2252 do_forward_remove(ssl, worker, skipwhite(p+14)); 2253 return; 2254 } else if(cmdcmp(p, "insecure_add", 12)) { 2255 /* must always distribute this cmd */ 2256 if(rc) distribute_cmd(rc, ssl, cmd); 2257 do_insecure_add(ssl, worker, skipwhite(p+12)); 2258 return; 2259 } else if(cmdcmp(p, "insecure_remove", 15)) { 2260 /* must always distribute this cmd */ 2261 if(rc) distribute_cmd(rc, ssl, cmd); 2262 do_insecure_remove(ssl, worker, skipwhite(p+15)); 2263 return; 2264 } else if(cmdcmp(p, "forward", 7)) { 2265 /* must always distribute this cmd */ 2266 if(rc) distribute_cmd(rc, ssl, cmd); 2267 do_forward(ssl, worker, skipwhite(p+7)); 2268 return; 2269 } else if(cmdcmp(p, "flush_stats", 11)) { 2270 /* must always distribute this cmd */ 2271 if(rc) distribute_cmd(rc, ssl, cmd); 2272 do_flush_stats(ssl, worker); 2273 return; 2274 } else if(cmdcmp(p, "flush_requestlist", 17)) { 2275 /* must always distribute this cmd */ 2276 if(rc) distribute_cmd(rc, ssl, cmd); 2277 do_flush_requestlist(ssl, worker); 2278 return; 2279 } else if(cmdcmp(p, "lookup", 6)) { 2280 do_lookup(ssl, worker, skipwhite(p+6)); 2281 return; 2282 } 2283 2284 #ifdef THREADS_DISABLED 2285 /* other processes must execute the command as well */ 2286 /* commands that should not be distributed, returned above. */ 2287 if(rc) { /* only if this thread is the master (rc) thread */ 2288 /* done before the code below, which may split the string */ 2289 distribute_cmd(rc, ssl, cmd); 2290 } 2291 #endif 2292 if(cmdcmp(p, "verbosity", 9)) { 2293 do_verbosity(ssl, skipwhite(p+9)); 2294 } else if(cmdcmp(p, "local_zone_remove", 17)) { 2295 do_zone_remove(ssl, worker, skipwhite(p+17)); 2296 } else if(cmdcmp(p, "local_zone", 10)) { 2297 do_zone_add(ssl, worker, skipwhite(p+10)); 2298 } else if(cmdcmp(p, "local_data_remove", 17)) { 2299 do_data_remove(ssl, worker, skipwhite(p+17)); 2300 } else if(cmdcmp(p, "local_data", 10)) { 2301 do_data_add(ssl, worker, skipwhite(p+10)); 2302 } else if(cmdcmp(p, "flush_zone", 10)) { 2303 do_flush_zone(ssl, worker, skipwhite(p+10)); 2304 } else if(cmdcmp(p, "flush_type", 10)) { 2305 do_flush_type(ssl, worker, skipwhite(p+10)); 2306 } else if(cmdcmp(p, "flush_infra", 11)) { 2307 do_flush_infra(ssl, worker, skipwhite(p+11)); 2308 } else if(cmdcmp(p, "flush", 5)) { 2309 do_flush_name(ssl, worker, skipwhite(p+5)); 2310 } else if(cmdcmp(p, "dump_requestlist", 16)) { 2311 do_dump_requestlist(ssl, worker); 2312 } else if(cmdcmp(p, "dump_infra", 10)) { 2313 do_dump_infra(ssl, worker); 2314 } else if(cmdcmp(p, "log_reopen", 10)) { 2315 do_log_reopen(ssl, worker); 2316 } else if(cmdcmp(p, "set_option", 10)) { 2317 do_set_option(ssl, worker, skipwhite(p+10)); 2318 } else if(cmdcmp(p, "get_option", 10)) { 2319 do_get_option(ssl, worker, skipwhite(p+10)); 2320 } else if(cmdcmp(p, "flush_bogus", 11)) { 2321 do_flush_bogus(ssl, worker); 2322 } else if(cmdcmp(p, "flush_negative", 14)) { 2323 do_flush_negative(ssl, worker); 2324 } else { 2325 (void)ssl_printf(ssl, "error unknown command '%s'\n", p); 2326 } 2327 } 2328 2329 void 2330 daemon_remote_exec(struct worker* worker) 2331 { 2332 /* read the cmd string */ 2333 uint8_t* msg = NULL; 2334 uint32_t len = 0; 2335 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) { 2336 log_err("daemon_remote_exec: tube_read_msg failed"); 2337 return; 2338 } 2339 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg); 2340 execute_cmd(NULL, NULL, (char*)msg, worker); 2341 free(msg); 2342 } 2343 2344 /** handle remote control request */ 2345 static void 2346 handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl) 2347 { 2348 int r; 2349 char pre[10]; 2350 char magic[7]; 2351 char buf[1024]; 2352 #ifdef USE_WINSOCK 2353 /* makes it possible to set the socket blocking again. */ 2354 /* basically removes it from winsock_event ... */ 2355 WSAEventSelect(s->c->fd, NULL, 0); 2356 #endif 2357 fd_set_block(s->c->fd); 2358 2359 /* try to read magic UBCT[version]_space_ string */ 2360 ERR_clear_error(); 2361 if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) { 2362 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) 2363 return; 2364 log_crypto_err("could not SSL_read"); 2365 return; 2366 } 2367 magic[6] = 0; 2368 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) { 2369 verbose(VERB_QUERY, "control connection has bad magic string"); 2370 /* probably wrong tool connected, ignore it completely */ 2371 return; 2372 } 2373 2374 /* read the command line */ 2375 if(!ssl_read_line(ssl, buf, sizeof(buf))) { 2376 return; 2377 } 2378 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION); 2379 if(strcmp(magic, pre) != 0) { 2380 verbose(VERB_QUERY, "control connection had bad " 2381 "version %s, cmd: %s", magic, buf); 2382 ssl_printf(ssl, "error version mismatch\n"); 2383 return; 2384 } 2385 verbose(VERB_DETAIL, "control cmd: %s", buf); 2386 2387 /* figure out what to do */ 2388 execute_cmd(rc, ssl, buf, rc->worker); 2389 } 2390 2391 int remote_control_callback(struct comm_point* c, void* arg, int err, 2392 struct comm_reply* ATTR_UNUSED(rep)) 2393 { 2394 struct rc_state* s = (struct rc_state*)arg; 2395 struct daemon_remote* rc = s->rc; 2396 int r; 2397 if(err != NETEVENT_NOERROR) { 2398 if(err==NETEVENT_TIMEOUT) 2399 log_err("remote control timed out"); 2400 clean_point(rc, s); 2401 return 0; 2402 } 2403 /* (continue to) setup the SSL connection */ 2404 ERR_clear_error(); 2405 r = SSL_do_handshake(s->ssl); 2406 if(r != 1) { 2407 int r2 = SSL_get_error(s->ssl, r); 2408 if(r2 == SSL_ERROR_WANT_READ) { 2409 if(s->shake_state == rc_hs_read) { 2410 /* try again later */ 2411 return 0; 2412 } 2413 s->shake_state = rc_hs_read; 2414 comm_point_listen_for_rw(c, 1, 0); 2415 return 0; 2416 } else if(r2 == SSL_ERROR_WANT_WRITE) { 2417 if(s->shake_state == rc_hs_write) { 2418 /* try again later */ 2419 return 0; 2420 } 2421 s->shake_state = rc_hs_write; 2422 comm_point_listen_for_rw(c, 0, 1); 2423 return 0; 2424 } else { 2425 if(r == 0) 2426 log_err("remote control connection closed prematurely"); 2427 log_addr(1, "failed connection from", 2428 &s->c->repinfo.addr, s->c->repinfo.addrlen); 2429 log_crypto_err("remote control failed ssl"); 2430 clean_point(rc, s); 2431 return 0; 2432 } 2433 } 2434 s->shake_state = rc_none; 2435 2436 /* once handshake has completed, check authentication */ 2437 if(SSL_get_verify_result(s->ssl) == X509_V_OK) { 2438 X509* x = SSL_get_peer_certificate(s->ssl); 2439 if(!x) { 2440 verbose(VERB_DETAIL, "remote control connection " 2441 "provided no client certificate"); 2442 clean_point(rc, s); 2443 return 0; 2444 } 2445 verbose(VERB_ALGO, "remote control connection authenticated"); 2446 X509_free(x); 2447 } else { 2448 verbose(VERB_DETAIL, "remote control connection failed to " 2449 "authenticate with client certificate"); 2450 clean_point(rc, s); 2451 return 0; 2452 } 2453 2454 /* if OK start to actually handle the request */ 2455 handle_req(rc, s, s->ssl); 2456 2457 verbose(VERB_ALGO, "remote control operation completed"); 2458 clean_point(rc, s); 2459 return 0; 2460 } 2461