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 #ifdef HAVE_OPENSSL_DH_H 50 #include <openssl/dh.h> 51 #endif 52 #ifdef HAVE_OPENSSL_BN_H 53 #include <openssl/bn.h> 54 #endif 55 56 #include <ctype.h> 57 #include "daemon/remote.h" 58 #include "daemon/worker.h" 59 #include "daemon/daemon.h" 60 #include "daemon/stats.h" 61 #include "daemon/cachedump.h" 62 #include "util/log.h" 63 #include "util/config_file.h" 64 #include "util/net_help.h" 65 #include "util/module.h" 66 #include "services/listen_dnsport.h" 67 #include "services/cache/rrset.h" 68 #include "services/cache/infra.h" 69 #include "services/mesh.h" 70 #include "services/localzone.h" 71 #include "services/authzone.h" 72 #include "services/rpz.h" 73 #include "util/storage/slabhash.h" 74 #include "util/fptr_wlist.h" 75 #include "util/data/dname.h" 76 #include "validator/validator.h" 77 #include "validator/val_kcache.h" 78 #include "validator/val_kentry.h" 79 #include "validator/val_anchor.h" 80 #include "iterator/iterator.h" 81 #include "iterator/iter_fwd.h" 82 #include "iterator/iter_hints.h" 83 #include "iterator/iter_delegpt.h" 84 #include "services/outbound_list.h" 85 #include "services/outside_network.h" 86 #include "sldns/str2wire.h" 87 #include "sldns/parseutil.h" 88 #include "sldns/wire2str.h" 89 #include "sldns/sbuffer.h" 90 91 #ifdef HAVE_SYS_TYPES_H 92 # include <sys/types.h> 93 #endif 94 #ifdef HAVE_SYS_STAT_H 95 #include <sys/stat.h> 96 #endif 97 #ifdef HAVE_NETDB_H 98 #include <netdb.h> 99 #endif 100 101 /* just for portability */ 102 #ifdef SQ 103 #undef SQ 104 #endif 105 106 /** what to put on statistics lines between var and value, ": " or "=" */ 107 #define SQ "=" 108 /** if true, inhibits a lot of =0 lines from the stats output */ 109 static const int inhibit_zero = 1; 110 111 /** subtract timers and the values do not overflow or become negative */ 112 static void 113 timeval_subtract(struct timeval* d, const struct timeval* end, 114 const struct timeval* start) 115 { 116 #ifndef S_SPLINT_S 117 time_t end_usec = end->tv_usec; 118 d->tv_sec = end->tv_sec - start->tv_sec; 119 if(end_usec < start->tv_usec) { 120 end_usec += 1000000; 121 d->tv_sec--; 122 } 123 d->tv_usec = end_usec - start->tv_usec; 124 #endif 125 } 126 127 /** divide sum of timers to get average */ 128 static void 129 timeval_divide(struct timeval* avg, const struct timeval* sum, long long d) 130 { 131 #ifndef S_SPLINT_S 132 size_t leftover; 133 if(d == 0) { 134 avg->tv_sec = 0; 135 avg->tv_usec = 0; 136 return; 137 } 138 avg->tv_sec = sum->tv_sec / d; 139 avg->tv_usec = sum->tv_usec / d; 140 /* handle fraction from seconds divide */ 141 leftover = sum->tv_sec - avg->tv_sec*d; 142 avg->tv_usec += (leftover*1000000)/d; 143 #endif 144 } 145 146 static int 147 remote_setup_ctx(struct daemon_remote* rc, struct config_file* cfg) 148 { 149 char* s_cert; 150 char* s_key; 151 rc->ctx = SSL_CTX_new(SSLv23_server_method()); 152 if(!rc->ctx) { 153 log_crypto_err("could not SSL_CTX_new"); 154 return 0; 155 } 156 if(!listen_sslctx_setup(rc->ctx)) { 157 return 0; 158 } 159 160 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1); 161 s_key = fname_after_chroot(cfg->server_key_file, cfg, 1); 162 if(!s_cert || !s_key) { 163 log_err("out of memory in remote control fname"); 164 goto setup_error; 165 } 166 verbose(VERB_ALGO, "setup SSL certificates"); 167 if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) { 168 log_err("Error for server-cert-file: %s", s_cert); 169 log_crypto_err("Error in SSL_CTX use_certificate_chain_file"); 170 goto setup_error; 171 } 172 if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) { 173 log_err("Error for server-key-file: %s", s_key); 174 log_crypto_err("Error in SSL_CTX use_PrivateKey_file"); 175 goto setup_error; 176 } 177 if(!SSL_CTX_check_private_key(rc->ctx)) { 178 log_err("Error for server-key-file: %s", s_key); 179 log_crypto_err("Error in SSL_CTX check_private_key"); 180 goto setup_error; 181 } 182 listen_sslctx_setup_2(rc->ctx); 183 if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) { 184 log_crypto_err("Error setting up SSL_CTX verify locations"); 185 setup_error: 186 free(s_cert); 187 free(s_key); 188 return 0; 189 } 190 SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert)); 191 SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL); 192 free(s_cert); 193 free(s_key); 194 return 1; 195 } 196 197 struct daemon_remote* 198 daemon_remote_create(struct config_file* cfg) 199 { 200 struct daemon_remote* rc = (struct daemon_remote*)calloc(1, 201 sizeof(*rc)); 202 if(!rc) { 203 log_err("out of memory in daemon_remote_create"); 204 return NULL; 205 } 206 rc->max_active = 10; 207 208 if(!cfg->remote_control_enable) { 209 rc->ctx = NULL; 210 return rc; 211 } 212 if(options_remote_is_address(cfg) && cfg->control_use_cert) { 213 if(!remote_setup_ctx(rc, cfg)) { 214 daemon_remote_delete(rc); 215 return NULL; 216 } 217 rc->use_cert = 1; 218 } else { 219 struct config_strlist* p; 220 rc->ctx = NULL; 221 rc->use_cert = 0; 222 if(!options_remote_is_address(cfg)) 223 for(p = cfg->control_ifs.first; p; p = p->next) { 224 if(p->str && p->str[0] != '/') 225 log_warn("control-interface %s is not using TLS, but plain transfer, because first control-interface in config file is a local socket (starts with a /).", p->str); 226 } 227 } 228 return rc; 229 } 230 231 void daemon_remote_clear(struct daemon_remote* rc) 232 { 233 struct rc_state* p, *np; 234 if(!rc) return; 235 /* but do not close the ports */ 236 listen_list_delete(rc->accept_list); 237 rc->accept_list = NULL; 238 /* do close these sockets */ 239 p = rc->busy_list; 240 while(p) { 241 np = p->next; 242 if(p->ssl) 243 SSL_free(p->ssl); 244 comm_point_delete(p->c); 245 free(p); 246 p = np; 247 } 248 rc->busy_list = NULL; 249 rc->active = 0; 250 rc->worker = NULL; 251 } 252 253 void daemon_remote_delete(struct daemon_remote* rc) 254 { 255 if(!rc) return; 256 daemon_remote_clear(rc); 257 if(rc->ctx) { 258 SSL_CTX_free(rc->ctx); 259 } 260 free(rc); 261 } 262 263 /** 264 * Add and open a new control port 265 * @param ip: ip str 266 * @param nr: port nr 267 * @param list: list head 268 * @param noproto_is_err: if lack of protocol support is an error. 269 * @param cfg: config with username for chown of unix-sockets. 270 * @return false on failure. 271 */ 272 static int 273 add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err, 274 struct config_file* cfg) 275 { 276 struct addrinfo hints; 277 struct addrinfo* res; 278 struct listen_port* n; 279 int noproto = 0; 280 int fd, r; 281 char port[15]; 282 snprintf(port, sizeof(port), "%d", nr); 283 port[sizeof(port)-1]=0; 284 memset(&hints, 0, sizeof(hints)); 285 log_assert(ip); 286 287 if(ip[0] == '/') { 288 /* This looks like a local socket */ 289 fd = create_local_accept_sock(ip, &noproto, cfg->use_systemd); 290 /* 291 * Change socket ownership and permissions so users other 292 * than root can access it provided they are in the same 293 * group as the user we run as. 294 */ 295 if(fd != -1) { 296 #ifdef HAVE_CHOWN 297 if (cfg->username && cfg->username[0] && 298 cfg_uid != (uid_t)-1) { 299 if(chown(ip, cfg_uid, cfg_gid) == -1) 300 verbose(VERB_QUERY, "cannot chown %u.%u %s: %s", 301 (unsigned)cfg_uid, (unsigned)cfg_gid, 302 ip, strerror(errno)); 303 } 304 chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)); 305 #else 306 (void)cfg; 307 #endif 308 } 309 } else { 310 hints.ai_socktype = SOCK_STREAM; 311 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 312 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) { 313 #ifdef USE_WINSOCK 314 if(!noproto_is_err && r == EAI_NONAME) { 315 /* tried to lookup the address as name */ 316 return 1; /* return success, but do nothing */ 317 } 318 #endif /* USE_WINSOCK */ 319 log_err("control interface %s:%s getaddrinfo: %s %s", 320 ip?ip:"default", port, gai_strerror(r), 321 #ifdef EAI_SYSTEM 322 r==EAI_SYSTEM?(char*)strerror(errno):"" 323 #else 324 "" 325 #endif 326 ); 327 return 0; 328 } 329 330 /* open fd */ 331 fd = create_tcp_accept_sock(res, 1, &noproto, 0, 332 cfg->ip_transparent, 0, cfg->ip_freebind, cfg->use_systemd, cfg->ip_dscp); 333 freeaddrinfo(res); 334 } 335 336 if(fd == -1 && noproto) { 337 if(!noproto_is_err) 338 return 1; /* return success, but do nothing */ 339 log_err("cannot open control interface %s %d : " 340 "protocol not supported", ip, nr); 341 return 0; 342 } 343 if(fd == -1) { 344 log_err("cannot open control interface %s %d", ip, nr); 345 return 0; 346 } 347 348 /* alloc */ 349 n = (struct listen_port*)calloc(1, sizeof(*n)); 350 if(!n) { 351 #ifndef USE_WINSOCK 352 close(fd); 353 #else 354 closesocket(fd); 355 #endif 356 log_err("out of memory"); 357 return 0; 358 } 359 n->next = *list; 360 *list = n; 361 n->fd = fd; 362 return 1; 363 } 364 365 struct listen_port* daemon_remote_open_ports(struct config_file* cfg) 366 { 367 struct listen_port* l = NULL; 368 log_assert(cfg->remote_control_enable && cfg->control_port); 369 if(cfg->control_ifs.first) { 370 struct config_strlist* p; 371 for(p = cfg->control_ifs.first; p; p = p->next) { 372 if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) { 373 listening_ports_free(l); 374 return NULL; 375 } 376 } 377 } else { 378 /* defaults */ 379 if(cfg->do_ip6 && 380 !add_open("::1", cfg->control_port, &l, 0, cfg)) { 381 listening_ports_free(l); 382 return NULL; 383 } 384 if(cfg->do_ip4 && 385 !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) { 386 listening_ports_free(l); 387 return NULL; 388 } 389 } 390 return l; 391 } 392 393 /** open accept commpoint */ 394 static int 395 accept_open(struct daemon_remote* rc, int fd) 396 { 397 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n)); 398 if(!n) { 399 log_err("out of memory"); 400 return 0; 401 } 402 n->next = rc->accept_list; 403 rc->accept_list = n; 404 /* open commpt */ 405 n->com = comm_point_create_raw(rc->worker->base, fd, 0, 406 &remote_accept_callback, rc); 407 if(!n->com) 408 return 0; 409 /* keep this port open, its fd is kept in the rc portlist */ 410 n->com->do_not_close = 1; 411 return 1; 412 } 413 414 int daemon_remote_open_accept(struct daemon_remote* rc, 415 struct listen_port* ports, struct worker* worker) 416 { 417 struct listen_port* p; 418 rc->worker = worker; 419 for(p = ports; p; p = p->next) { 420 if(!accept_open(rc, p->fd)) { 421 log_err("could not create accept comm point"); 422 return 0; 423 } 424 } 425 return 1; 426 } 427 428 void daemon_remote_stop_accept(struct daemon_remote* rc) 429 { 430 struct listen_list* p; 431 for(p=rc->accept_list; p; p=p->next) { 432 comm_point_stop_listening(p->com); 433 } 434 } 435 436 void daemon_remote_start_accept(struct daemon_remote* rc) 437 { 438 struct listen_list* p; 439 for(p=rc->accept_list; p; p=p->next) { 440 comm_point_start_listening(p->com, -1, -1); 441 } 442 } 443 444 int remote_accept_callback(struct comm_point* c, void* arg, int err, 445 struct comm_reply* ATTR_UNUSED(rep)) 446 { 447 struct daemon_remote* rc = (struct daemon_remote*)arg; 448 struct sockaddr_storage addr; 449 socklen_t addrlen; 450 int newfd; 451 struct rc_state* n; 452 if(err != NETEVENT_NOERROR) { 453 log_err("error %d on remote_accept_callback", err); 454 return 0; 455 } 456 /* perform the accept */ 457 newfd = comm_point_perform_accept(c, &addr, &addrlen); 458 if(newfd == -1) 459 return 0; 460 /* create new commpoint unless we are servicing already */ 461 if(rc->active >= rc->max_active) { 462 log_warn("drop incoming remote control: too many connections"); 463 close_exit: 464 #ifndef USE_WINSOCK 465 close(newfd); 466 #else 467 closesocket(newfd); 468 #endif 469 return 0; 470 } 471 472 /* setup commpoint to service the remote control command */ 473 n = (struct rc_state*)calloc(1, sizeof(*n)); 474 if(!n) { 475 log_err("out of memory"); 476 goto close_exit; 477 } 478 n->fd = newfd; 479 /* start in reading state */ 480 n->c = comm_point_create_raw(rc->worker->base, newfd, 0, 481 &remote_control_callback, n); 482 if(!n->c) { 483 log_err("out of memory"); 484 free(n); 485 goto close_exit; 486 } 487 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen); 488 n->c->do_not_close = 0; 489 comm_point_stop_listening(n->c); 490 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT); 491 memcpy(&n->c->repinfo.addr, &addr, addrlen); 492 n->c->repinfo.addrlen = addrlen; 493 if(rc->use_cert) { 494 n->shake_state = rc_hs_read; 495 n->ssl = SSL_new(rc->ctx); 496 if(!n->ssl) { 497 log_crypto_err("could not SSL_new"); 498 comm_point_delete(n->c); 499 free(n); 500 goto close_exit; 501 } 502 SSL_set_accept_state(n->ssl); 503 (void)SSL_set_mode(n->ssl, (long)SSL_MODE_AUTO_RETRY); 504 if(!SSL_set_fd(n->ssl, newfd)) { 505 log_crypto_err("could not SSL_set_fd"); 506 SSL_free(n->ssl); 507 comm_point_delete(n->c); 508 free(n); 509 goto close_exit; 510 } 511 } else { 512 n->ssl = NULL; 513 } 514 515 n->rc = rc; 516 n->next = rc->busy_list; 517 rc->busy_list = n; 518 rc->active ++; 519 520 /* perform the first nonblocking read already, for windows, 521 * so it can return wouldblock. could be faster too. */ 522 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL); 523 return 0; 524 } 525 526 /** delete from list */ 527 static void 528 state_list_remove_elem(struct rc_state** list, struct comm_point* c) 529 { 530 while(*list) { 531 if( (*list)->c == c) { 532 *list = (*list)->next; 533 return; 534 } 535 list = &(*list)->next; 536 } 537 } 538 539 /** decrease active count and remove commpoint from busy list */ 540 static void 541 clean_point(struct daemon_remote* rc, struct rc_state* s) 542 { 543 state_list_remove_elem(&rc->busy_list, s->c); 544 rc->active --; 545 if(s->ssl) { 546 SSL_shutdown(s->ssl); 547 SSL_free(s->ssl); 548 } 549 comm_point_delete(s->c); 550 free(s); 551 } 552 553 int 554 ssl_print_text(RES* res, const char* text) 555 { 556 int r; 557 if(!res) 558 return 0; 559 if(res->ssl) { 560 ERR_clear_error(); 561 if((r=SSL_write(res->ssl, text, (int)strlen(text))) <= 0) { 562 if(SSL_get_error(res->ssl, r) == SSL_ERROR_ZERO_RETURN) { 563 verbose(VERB_QUERY, "warning, in SSL_write, peer " 564 "closed connection"); 565 return 0; 566 } 567 log_crypto_err("could not SSL_write"); 568 return 0; 569 } 570 } else { 571 size_t at = 0; 572 while(at < strlen(text)) { 573 ssize_t r = send(res->fd, text+at, strlen(text)-at, 0); 574 if(r == -1) { 575 if(errno == EAGAIN || errno == EINTR) 576 continue; 577 #ifndef USE_WINSOCK 578 log_err("could not send: %s", strerror(errno)); 579 #else 580 log_err("could not send: %s", wsa_strerror(WSAGetLastError())); 581 #endif 582 return 0; 583 } 584 at += r; 585 } 586 } 587 return 1; 588 } 589 590 /** print text over the ssl connection */ 591 static int 592 ssl_print_vmsg(RES* ssl, const char* format, va_list args) 593 { 594 char msg[1024]; 595 vsnprintf(msg, sizeof(msg), format, args); 596 return ssl_print_text(ssl, msg); 597 } 598 599 /** printf style printing to the ssl connection */ 600 int ssl_printf(RES* ssl, const char* format, ...) 601 { 602 va_list args; 603 int ret; 604 va_start(args, format); 605 ret = ssl_print_vmsg(ssl, format, args); 606 va_end(args); 607 return ret; 608 } 609 610 int 611 ssl_read_line(RES* res, char* buf, size_t max) 612 { 613 int r; 614 size_t len = 0; 615 if(!res) 616 return 0; 617 while(len < max) { 618 if(res->ssl) { 619 ERR_clear_error(); 620 if((r=SSL_read(res->ssl, buf+len, 1)) <= 0) { 621 if(SSL_get_error(res->ssl, r) == SSL_ERROR_ZERO_RETURN) { 622 buf[len] = 0; 623 return 1; 624 } 625 log_crypto_err("could not SSL_read"); 626 return 0; 627 } 628 } else { 629 while(1) { 630 ssize_t rr = recv(res->fd, buf+len, 1, 0); 631 if(rr <= 0) { 632 if(rr == 0) { 633 buf[len] = 0; 634 return 1; 635 } 636 if(errno == EINTR || errno == EAGAIN) 637 continue; 638 #ifndef USE_WINSOCK 639 log_err("could not recv: %s", strerror(errno)); 640 #else 641 log_err("could not recv: %s", wsa_strerror(WSAGetLastError())); 642 #endif 643 return 0; 644 } 645 break; 646 } 647 } 648 if(buf[len] == '\n') { 649 /* return string without \n */ 650 buf[len] = 0; 651 return 1; 652 } 653 len++; 654 } 655 buf[max-1] = 0; 656 log_err("control line too long (%d): %s", (int)max, buf); 657 return 0; 658 } 659 660 /** skip whitespace, return new pointer into string */ 661 static char* 662 skipwhite(char* str) 663 { 664 /* EOS \0 is not a space */ 665 while( isspace((unsigned char)*str) ) 666 str++; 667 return str; 668 } 669 670 /** send the OK to the control client */ 671 static void send_ok(RES* ssl) 672 { 673 (void)ssl_printf(ssl, "ok\n"); 674 } 675 676 /** do the stop command */ 677 static void 678 do_stop(RES* ssl, struct worker* worker) 679 { 680 worker->need_to_exit = 1; 681 comm_base_exit(worker->base); 682 send_ok(ssl); 683 } 684 685 /** do the reload command */ 686 static void 687 do_reload(RES* ssl, struct worker* worker) 688 { 689 worker->need_to_exit = 0; 690 comm_base_exit(worker->base); 691 send_ok(ssl); 692 } 693 694 /** do the verbosity command */ 695 static void 696 do_verbosity(RES* ssl, char* str) 697 { 698 int val = atoi(str); 699 if(val == 0 && strcmp(str, "0") != 0) { 700 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str); 701 return; 702 } 703 verbosity = val; 704 send_ok(ssl); 705 } 706 707 /** print stats from statinfo */ 708 static int 709 print_stats(RES* ssl, const char* nm, struct ub_stats_info* s) 710 { 711 struct timeval sumwait, avg; 712 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm, 713 (unsigned long)s->svr.num_queries)) return 0; 714 if(!ssl_printf(ssl, "%s.num.queries_ip_ratelimited"SQ"%lu\n", nm, 715 (unsigned long)s->svr.num_queries_ip_ratelimited)) return 0; 716 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm, 717 (unsigned long)(s->svr.num_queries 718 - s->svr.num_queries_missed_cache))) return 0; 719 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm, 720 (unsigned long)s->svr.num_queries_missed_cache)) return 0; 721 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm, 722 (unsigned long)s->svr.num_queries_prefetch)) return 0; 723 if(!ssl_printf(ssl, "%s.num.expired"SQ"%lu\n", nm, 724 (unsigned long)s->svr.ans_expired)) return 0; 725 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm, 726 (unsigned long)s->mesh_replies_sent)) return 0; 727 #ifdef USE_DNSCRYPT 728 if(!ssl_printf(ssl, "%s.num.dnscrypt.crypted"SQ"%lu\n", nm, 729 (unsigned long)s->svr.num_query_dnscrypt_crypted)) return 0; 730 if(!ssl_printf(ssl, "%s.num.dnscrypt.cert"SQ"%lu\n", nm, 731 (unsigned long)s->svr.num_query_dnscrypt_cert)) return 0; 732 if(!ssl_printf(ssl, "%s.num.dnscrypt.cleartext"SQ"%lu\n", nm, 733 (unsigned long)s->svr.num_query_dnscrypt_cleartext)) return 0; 734 if(!ssl_printf(ssl, "%s.num.dnscrypt.malformed"SQ"%lu\n", nm, 735 (unsigned long)s->svr.num_query_dnscrypt_crypted_malformed)) return 0; 736 #endif 737 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm, 738 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)? 739 (double)s->svr.sum_query_list_size/ 740 (double)(s->svr.num_queries_missed_cache+ 741 s->svr.num_queries_prefetch) : 0.0)) return 0; 742 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm, 743 (unsigned long)s->svr.max_query_list_size)) return 0; 744 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm, 745 (unsigned long)s->mesh_jostled)) return 0; 746 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm, 747 (unsigned long)s->mesh_dropped)) return 0; 748 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm, 749 (unsigned long)s->mesh_num_states)) return 0; 750 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm, 751 (unsigned long)s->mesh_num_reply_states)) return 0; 752 #ifndef S_SPLINT_S 753 sumwait.tv_sec = s->mesh_replies_sum_wait_sec; 754 sumwait.tv_usec = s->mesh_replies_sum_wait_usec; 755 #endif 756 timeval_divide(&avg, &sumwait, s->mesh_replies_sent); 757 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm, 758 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0; 759 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm, 760 s->mesh_time_median)) return 0; 761 if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm, 762 (unsigned long)s->svr.tcp_accept_usage)) return 0; 763 return 1; 764 } 765 766 /** print stats for one thread */ 767 static int 768 print_thread_stats(RES* ssl, int i, struct ub_stats_info* s) 769 { 770 char nm[32]; 771 snprintf(nm, sizeof(nm), "thread%d", i); 772 nm[sizeof(nm)-1]=0; 773 return print_stats(ssl, nm, s); 774 } 775 776 /** print long number */ 777 static int 778 print_longnum(RES* ssl, const char* desc, size_t x) 779 { 780 if(x > 1024*1024*1024) { 781 /* more than a Gb */ 782 size_t front = x / (size_t)1000000; 783 size_t back = x % (size_t)1000000; 784 return ssl_printf(ssl, "%s%u%6.6u\n", desc, 785 (unsigned)front, (unsigned)back); 786 } else { 787 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x); 788 } 789 } 790 791 /** print mem stats */ 792 static int 793 print_mem(RES* ssl, struct worker* worker, struct daemon* daemon, 794 struct ub_stats_info* s) 795 { 796 size_t msg, rrset, val, iter, respip; 797 #ifdef CLIENT_SUBNET 798 size_t subnet = 0; 799 #endif /* CLIENT_SUBNET */ 800 #ifdef USE_IPSECMOD 801 size_t ipsecmod = 0; 802 #endif /* USE_IPSECMOD */ 803 #ifdef USE_DNSCRYPT 804 size_t dnscrypt_shared_secret = 0; 805 size_t dnscrypt_nonce = 0; 806 #endif /* USE_DNSCRYPT */ 807 #ifdef WITH_DYNLIBMODULE 808 size_t dynlib = 0; 809 #endif /* WITH_DYNLIBMODULE */ 810 msg = slabhash_get_mem(daemon->env->msg_cache); 811 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table); 812 val = mod_get_mem(&worker->env, "validator"); 813 iter = mod_get_mem(&worker->env, "iterator"); 814 respip = mod_get_mem(&worker->env, "respip"); 815 #ifdef CLIENT_SUBNET 816 subnet = mod_get_mem(&worker->env, "subnet"); 817 #endif /* CLIENT_SUBNET */ 818 #ifdef USE_IPSECMOD 819 ipsecmod = mod_get_mem(&worker->env, "ipsecmod"); 820 #endif /* USE_IPSECMOD */ 821 #ifdef USE_DNSCRYPT 822 if(daemon->dnscenv) { 823 dnscrypt_shared_secret = slabhash_get_mem( 824 daemon->dnscenv->shared_secrets_cache); 825 dnscrypt_nonce = slabhash_get_mem(daemon->dnscenv->nonces_cache); 826 } 827 #endif /* USE_DNSCRYPT */ 828 #ifdef WITH_DYNLIBMODULE 829 dynlib = mod_get_mem(&worker->env, "dynlib"); 830 #endif /* WITH_DYNLIBMODULE */ 831 832 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset)) 833 return 0; 834 if(!print_longnum(ssl, "mem.cache.message"SQ, msg)) 835 return 0; 836 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter)) 837 return 0; 838 if(!print_longnum(ssl, "mem.mod.validator"SQ, val)) 839 return 0; 840 if(!print_longnum(ssl, "mem.mod.respip"SQ, respip)) 841 return 0; 842 #ifdef CLIENT_SUBNET 843 if(!print_longnum(ssl, "mem.mod.subnet"SQ, subnet)) 844 return 0; 845 #endif /* CLIENT_SUBNET */ 846 #ifdef USE_IPSECMOD 847 if(!print_longnum(ssl, "mem.mod.ipsecmod"SQ, ipsecmod)) 848 return 0; 849 #endif /* USE_IPSECMOD */ 850 #ifdef USE_DNSCRYPT 851 if(!print_longnum(ssl, "mem.cache.dnscrypt_shared_secret"SQ, 852 dnscrypt_shared_secret)) 853 return 0; 854 if(!print_longnum(ssl, "mem.cache.dnscrypt_nonce"SQ, 855 dnscrypt_nonce)) 856 return 0; 857 #endif /* USE_DNSCRYPT */ 858 #ifdef WITH_DYNLIBMODULE 859 if(!print_longnum(ssl, "mem.mod.dynlibmod"SQ, dynlib)) 860 return 0; 861 #endif /* WITH_DYNLIBMODULE */ 862 if(!print_longnum(ssl, "mem.streamwait"SQ, 863 (size_t)s->svr.mem_stream_wait)) 864 return 0; 865 return 1; 866 } 867 868 /** print uptime stats */ 869 static int 870 print_uptime(RES* ssl, struct worker* worker, int reset) 871 { 872 struct timeval now = *worker->env.now_tv; 873 struct timeval up, dt; 874 timeval_subtract(&up, &now, &worker->daemon->time_boot); 875 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat); 876 if(reset) 877 worker->daemon->time_last_stat = now; 878 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n", 879 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0; 880 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n", 881 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0; 882 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n", 883 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0; 884 return 1; 885 } 886 887 /** print extended histogram */ 888 static int 889 print_hist(RES* ssl, struct ub_stats_info* s) 890 { 891 struct timehist* hist; 892 size_t i; 893 hist = timehist_setup(); 894 if(!hist) { 895 log_err("out of memory"); 896 return 0; 897 } 898 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST); 899 for(i=0; i<hist->num; i++) { 900 if(!ssl_printf(ssl, 901 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n", 902 (int)hist->buckets[i].lower.tv_sec, 903 (int)hist->buckets[i].lower.tv_usec, 904 (int)hist->buckets[i].upper.tv_sec, 905 (int)hist->buckets[i].upper.tv_usec, 906 (unsigned long)hist->buckets[i].count)) { 907 timehist_delete(hist); 908 return 0; 909 } 910 } 911 timehist_delete(hist); 912 return 1; 913 } 914 915 /** print extended stats */ 916 static int 917 print_ext(RES* ssl, struct ub_stats_info* s) 918 { 919 int i; 920 char nm[32]; 921 const sldns_rr_descriptor* desc; 922 const sldns_lookup_table* lt; 923 /* TYPE */ 924 for(i=0; i<UB_STATS_QTYPE_NUM; i++) { 925 if(inhibit_zero && s->svr.qtype[i] == 0) 926 continue; 927 desc = sldns_rr_descript((uint16_t)i); 928 if(desc && desc->_name) { 929 snprintf(nm, sizeof(nm), "%s", desc->_name); 930 } else if (i == LDNS_RR_TYPE_IXFR) { 931 snprintf(nm, sizeof(nm), "IXFR"); 932 } else if (i == LDNS_RR_TYPE_AXFR) { 933 snprintf(nm, sizeof(nm), "AXFR"); 934 } else if (i == LDNS_RR_TYPE_MAILA) { 935 snprintf(nm, sizeof(nm), "MAILA"); 936 } else if (i == LDNS_RR_TYPE_MAILB) { 937 snprintf(nm, sizeof(nm), "MAILB"); 938 } else if (i == LDNS_RR_TYPE_ANY) { 939 snprintf(nm, sizeof(nm), "ANY"); 940 } else { 941 snprintf(nm, sizeof(nm), "TYPE%d", i); 942 } 943 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n", 944 nm, (unsigned long)s->svr.qtype[i])) return 0; 945 } 946 if(!inhibit_zero || s->svr.qtype_big) { 947 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n", 948 (unsigned long)s->svr.qtype_big)) return 0; 949 } 950 /* CLASS */ 951 for(i=0; i<UB_STATS_QCLASS_NUM; i++) { 952 if(inhibit_zero && s->svr.qclass[i] == 0) 953 continue; 954 lt = sldns_lookup_by_id(sldns_rr_classes, i); 955 if(lt && lt->name) { 956 snprintf(nm, sizeof(nm), "%s", lt->name); 957 } else { 958 snprintf(nm, sizeof(nm), "CLASS%d", i); 959 } 960 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n", 961 nm, (unsigned long)s->svr.qclass[i])) return 0; 962 } 963 if(!inhibit_zero || s->svr.qclass_big) { 964 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n", 965 (unsigned long)s->svr.qclass_big)) return 0; 966 } 967 /* OPCODE */ 968 for(i=0; i<UB_STATS_OPCODE_NUM; i++) { 969 if(inhibit_zero && s->svr.qopcode[i] == 0) 970 continue; 971 lt = sldns_lookup_by_id(sldns_opcodes, i); 972 if(lt && lt->name) { 973 snprintf(nm, sizeof(nm), "%s", lt->name); 974 } else { 975 snprintf(nm, sizeof(nm), "OPCODE%d", i); 976 } 977 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n", 978 nm, (unsigned long)s->svr.qopcode[i])) return 0; 979 } 980 /* transport */ 981 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n", 982 (unsigned long)s->svr.qtcp)) return 0; 983 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n", 984 (unsigned long)s->svr.qtcp_outgoing)) return 0; 985 if(!ssl_printf(ssl, "num.query.tls"SQ"%lu\n", 986 (unsigned long)s->svr.qtls)) return 0; 987 if(!ssl_printf(ssl, "num.query.tls.resume"SQ"%lu\n", 988 (unsigned long)s->svr.qtls_resume)) return 0; 989 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n", 990 (unsigned long)s->svr.qipv6)) return 0; 991 /* flags */ 992 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n", 993 (unsigned long)s->svr.qbit_QR)) return 0; 994 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n", 995 (unsigned long)s->svr.qbit_AA)) return 0; 996 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n", 997 (unsigned long)s->svr.qbit_TC)) return 0; 998 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n", 999 (unsigned long)s->svr.qbit_RD)) return 0; 1000 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n", 1001 (unsigned long)s->svr.qbit_RA)) return 0; 1002 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n", 1003 (unsigned long)s->svr.qbit_Z)) return 0; 1004 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n", 1005 (unsigned long)s->svr.qbit_AD)) return 0; 1006 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n", 1007 (unsigned long)s->svr.qbit_CD)) return 0; 1008 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n", 1009 (unsigned long)s->svr.qEDNS)) return 0; 1010 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n", 1011 (unsigned long)s->svr.qEDNS_DO)) return 0; 1012 1013 /* RCODE */ 1014 for(i=0; i<UB_STATS_RCODE_NUM; i++) { 1015 /* Always include RCODEs 0-5 */ 1016 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0) 1017 continue; 1018 lt = sldns_lookup_by_id(sldns_rcodes, i); 1019 if(lt && lt->name) { 1020 snprintf(nm, sizeof(nm), "%s", lt->name); 1021 } else { 1022 snprintf(nm, sizeof(nm), "RCODE%d", i); 1023 } 1024 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n", 1025 nm, (unsigned long)s->svr.ans_rcode[i])) return 0; 1026 } 1027 if(!inhibit_zero || s->svr.ans_rcode_nodata) { 1028 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n", 1029 (unsigned long)s->svr.ans_rcode_nodata)) return 0; 1030 } 1031 /* iteration */ 1032 if(!ssl_printf(ssl, "num.query.ratelimited"SQ"%lu\n", 1033 (unsigned long)s->svr.queries_ratelimited)) return 0; 1034 /* validation */ 1035 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n", 1036 (unsigned long)s->svr.ans_secure)) return 0; 1037 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n", 1038 (unsigned long)s->svr.ans_bogus)) return 0; 1039 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n", 1040 (unsigned long)s->svr.rrset_bogus)) return 0; 1041 if(!ssl_printf(ssl, "num.query.aggressive.NOERROR"SQ"%lu\n", 1042 (unsigned long)s->svr.num_neg_cache_noerror)) return 0; 1043 if(!ssl_printf(ssl, "num.query.aggressive.NXDOMAIN"SQ"%lu\n", 1044 (unsigned long)s->svr.num_neg_cache_nxdomain)) return 0; 1045 /* threat detection */ 1046 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n", 1047 (unsigned long)s->svr.unwanted_queries)) return 0; 1048 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n", 1049 (unsigned long)s->svr.unwanted_replies)) return 0; 1050 /* cache counts */ 1051 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n", 1052 (unsigned)s->svr.msg_cache_count)) return 0; 1053 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n", 1054 (unsigned)s->svr.rrset_cache_count)) return 0; 1055 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n", 1056 (unsigned)s->svr.infra_cache_count)) return 0; 1057 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n", 1058 (unsigned)s->svr.key_cache_count)) return 0; 1059 /* applied RPZ actions */ 1060 for(i=0; i<UB_STATS_RPZ_ACTION_NUM; i++) { 1061 if(i == RPZ_NO_OVERRIDE_ACTION) 1062 continue; 1063 if(inhibit_zero && s->svr.rpz_action[i] == 0) 1064 continue; 1065 if(!ssl_printf(ssl, "num.rpz.action.%s"SQ"%lu\n", 1066 rpz_action_to_string(i), 1067 (unsigned long)s->svr.rpz_action[i])) return 0; 1068 } 1069 #ifdef USE_DNSCRYPT 1070 if(!ssl_printf(ssl, "dnscrypt_shared_secret.cache.count"SQ"%u\n", 1071 (unsigned)s->svr.shared_secret_cache_count)) return 0; 1072 if(!ssl_printf(ssl, "dnscrypt_nonce.cache.count"SQ"%u\n", 1073 (unsigned)s->svr.nonce_cache_count)) return 0; 1074 if(!ssl_printf(ssl, "num.query.dnscrypt.shared_secret.cachemiss"SQ"%lu\n", 1075 (unsigned long)s->svr.num_query_dnscrypt_secret_missed_cache)) return 0; 1076 if(!ssl_printf(ssl, "num.query.dnscrypt.replay"SQ"%lu\n", 1077 (unsigned long)s->svr.num_query_dnscrypt_replay)) return 0; 1078 #endif /* USE_DNSCRYPT */ 1079 if(!ssl_printf(ssl, "num.query.authzone.up"SQ"%lu\n", 1080 (unsigned long)s->svr.num_query_authzone_up)) return 0; 1081 if(!ssl_printf(ssl, "num.query.authzone.down"SQ"%lu\n", 1082 (unsigned long)s->svr.num_query_authzone_down)) return 0; 1083 #ifdef CLIENT_SUBNET 1084 if(!ssl_printf(ssl, "num.query.subnet"SQ"%lu\n", 1085 (unsigned long)s->svr.num_query_subnet)) return 0; 1086 if(!ssl_printf(ssl, "num.query.subnet_cache"SQ"%lu\n", 1087 (unsigned long)s->svr.num_query_subnet_cache)) return 0; 1088 #endif /* CLIENT_SUBNET */ 1089 return 1; 1090 } 1091 1092 /** do the stats command */ 1093 static void 1094 do_stats(RES* ssl, struct worker* worker, int reset) 1095 { 1096 struct daemon* daemon = worker->daemon; 1097 struct ub_stats_info total; 1098 struct ub_stats_info s; 1099 int i; 1100 memset(&total, 0, sizeof(total)); 1101 log_assert(daemon->num > 0); 1102 /* gather all thread statistics in one place */ 1103 for(i=0; i<daemon->num; i++) { 1104 server_stats_obtain(worker, daemon->workers[i], &s, reset); 1105 if(!print_thread_stats(ssl, i, &s)) 1106 return; 1107 if(i == 0) 1108 total = s; 1109 else server_stats_add(&total, &s); 1110 } 1111 /* print the thread statistics */ 1112 total.mesh_time_median /= (double)daemon->num; 1113 if(!print_stats(ssl, "total", &total)) 1114 return; 1115 if(!print_uptime(ssl, worker, reset)) 1116 return; 1117 if(daemon->cfg->stat_extended) { 1118 if(!print_mem(ssl, worker, daemon, &total)) 1119 return; 1120 if(!print_hist(ssl, &total)) 1121 return; 1122 if(!print_ext(ssl, &total)) 1123 return; 1124 } 1125 } 1126 1127 /** parse commandline argument domain name */ 1128 static int 1129 parse_arg_name(RES* ssl, char* str, uint8_t** res, size_t* len, int* labs) 1130 { 1131 uint8_t nm[LDNS_MAX_DOMAINLEN+1]; 1132 size_t nmlen = sizeof(nm); 1133 int status; 1134 *res = NULL; 1135 *len = 0; 1136 *labs = 0; 1137 if(str[0] == '\0') { 1138 ssl_printf(ssl, "error: this option requires a domain name\n"); 1139 return 0; 1140 } 1141 status = sldns_str2wire_dname_buf(str, nm, &nmlen); 1142 if(status != 0) { 1143 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str, 1144 LDNS_WIREPARSE_OFFSET(status), 1145 sldns_get_errorstr_parse(status)); 1146 return 0; 1147 } 1148 *res = memdup(nm, nmlen); 1149 if(!*res) { 1150 ssl_printf(ssl, "error out of memory\n"); 1151 return 0; 1152 } 1153 *labs = dname_count_size_labels(*res, len); 1154 return 1; 1155 } 1156 1157 /** find second argument, modifies string */ 1158 static int 1159 find_arg2(RES* ssl, char* arg, char** arg2) 1160 { 1161 char* as = strchr(arg, ' '); 1162 char* at = strchr(arg, '\t'); 1163 if(as && at) { 1164 if(at < as) 1165 as = at; 1166 as[0]=0; 1167 *arg2 = skipwhite(as+1); 1168 } else if(as) { 1169 as[0]=0; 1170 *arg2 = skipwhite(as+1); 1171 } else if(at) { 1172 at[0]=0; 1173 *arg2 = skipwhite(at+1); 1174 } else { 1175 ssl_printf(ssl, "error could not find next argument " 1176 "after %s\n", arg); 1177 return 0; 1178 } 1179 return 1; 1180 } 1181 1182 /** Add a new zone */ 1183 static int 1184 perform_zone_add(RES* ssl, struct local_zones* zones, char* arg) 1185 { 1186 uint8_t* nm; 1187 int nmlabs; 1188 size_t nmlen; 1189 char* arg2; 1190 enum localzone_type t; 1191 struct local_zone* z; 1192 if(!find_arg2(ssl, arg, &arg2)) 1193 return 0; 1194 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1195 return 0; 1196 if(!local_zone_str2type(arg2, &t)) { 1197 ssl_printf(ssl, "error not a zone type. %s\n", arg2); 1198 free(nm); 1199 return 0; 1200 } 1201 lock_rw_wrlock(&zones->lock); 1202 if((z=local_zones_find(zones, nm, nmlen, 1203 nmlabs, LDNS_RR_CLASS_IN))) { 1204 /* already present in tree */ 1205 lock_rw_wrlock(&z->lock); 1206 z->type = t; /* update type anyway */ 1207 lock_rw_unlock(&z->lock); 1208 free(nm); 1209 lock_rw_unlock(&zones->lock); 1210 return 1; 1211 } 1212 if(!local_zones_add_zone(zones, nm, nmlen, 1213 nmlabs, LDNS_RR_CLASS_IN, t)) { 1214 lock_rw_unlock(&zones->lock); 1215 ssl_printf(ssl, "error out of memory\n"); 1216 return 0; 1217 } 1218 lock_rw_unlock(&zones->lock); 1219 return 1; 1220 } 1221 1222 /** Do the local_zone command */ 1223 static void 1224 do_zone_add(RES* ssl, struct local_zones* zones, char* arg) 1225 { 1226 if(!perform_zone_add(ssl, zones, arg)) 1227 return; 1228 send_ok(ssl); 1229 } 1230 1231 /** Do the local_zones command */ 1232 static void 1233 do_zones_add(RES* ssl, struct local_zones* zones) 1234 { 1235 char buf[2048]; 1236 int num = 0; 1237 while(ssl_read_line(ssl, buf, sizeof(buf))) { 1238 if(buf[0] == 0x04 && buf[1] == 0) 1239 break; /* end of transmission */ 1240 if(!perform_zone_add(ssl, zones, buf)) { 1241 if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 1242 return; 1243 } 1244 else 1245 num++; 1246 } 1247 (void)ssl_printf(ssl, "added %d zones\n", num); 1248 } 1249 1250 /** Remove a zone */ 1251 static int 1252 perform_zone_remove(RES* ssl, struct local_zones* zones, char* arg) 1253 { 1254 uint8_t* nm; 1255 int nmlabs; 1256 size_t nmlen; 1257 struct local_zone* z; 1258 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1259 return 0; 1260 lock_rw_wrlock(&zones->lock); 1261 if((z=local_zones_find(zones, nm, nmlen, 1262 nmlabs, LDNS_RR_CLASS_IN))) { 1263 /* present in tree */ 1264 local_zones_del_zone(zones, z); 1265 } 1266 lock_rw_unlock(&zones->lock); 1267 free(nm); 1268 return 1; 1269 } 1270 1271 /** Do the local_zone_remove command */ 1272 static void 1273 do_zone_remove(RES* ssl, struct local_zones* zones, char* arg) 1274 { 1275 if(!perform_zone_remove(ssl, zones, arg)) 1276 return; 1277 send_ok(ssl); 1278 } 1279 1280 /** Do the local_zones_remove command */ 1281 static void 1282 do_zones_remove(RES* ssl, struct local_zones* zones) 1283 { 1284 char buf[2048]; 1285 int num = 0; 1286 while(ssl_read_line(ssl, buf, sizeof(buf))) { 1287 if(buf[0] == 0x04 && buf[1] == 0) 1288 break; /* end of transmission */ 1289 if(!perform_zone_remove(ssl, zones, buf)) { 1290 if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 1291 return; 1292 } 1293 else 1294 num++; 1295 } 1296 (void)ssl_printf(ssl, "removed %d zones\n", num); 1297 } 1298 1299 /** Add new RR data */ 1300 static int 1301 perform_data_add(RES* ssl, struct local_zones* zones, char* arg) 1302 { 1303 if(!local_zones_add_RR(zones, arg)) { 1304 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg); 1305 return 0; 1306 } 1307 return 1; 1308 } 1309 1310 /** Do the local_data command */ 1311 static void 1312 do_data_add(RES* ssl, struct local_zones* zones, char* arg) 1313 { 1314 if(!perform_data_add(ssl, zones, arg)) 1315 return; 1316 send_ok(ssl); 1317 } 1318 1319 /** Do the local_datas command */ 1320 static void 1321 do_datas_add(RES* ssl, struct local_zones* zones) 1322 { 1323 char buf[2048]; 1324 int num = 0; 1325 while(ssl_read_line(ssl, buf, sizeof(buf))) { 1326 if(buf[0] == 0x04 && buf[1] == 0) 1327 break; /* end of transmission */ 1328 if(!perform_data_add(ssl, zones, buf)) { 1329 if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 1330 return; 1331 } 1332 else 1333 num++; 1334 } 1335 (void)ssl_printf(ssl, "added %d datas\n", num); 1336 } 1337 1338 /** Remove RR data */ 1339 static int 1340 perform_data_remove(RES* ssl, struct local_zones* zones, char* arg) 1341 { 1342 uint8_t* nm; 1343 int nmlabs; 1344 size_t nmlen; 1345 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1346 return 0; 1347 local_zones_del_data(zones, nm, 1348 nmlen, nmlabs, LDNS_RR_CLASS_IN); 1349 free(nm); 1350 return 1; 1351 } 1352 1353 /** Do the local_data_remove command */ 1354 static void 1355 do_data_remove(RES* ssl, struct local_zones* zones, char* arg) 1356 { 1357 if(!perform_data_remove(ssl, zones, arg)) 1358 return; 1359 send_ok(ssl); 1360 } 1361 1362 /** Do the local_datas_remove command */ 1363 static void 1364 do_datas_remove(RES* ssl, struct local_zones* zones) 1365 { 1366 char buf[2048]; 1367 int num = 0; 1368 while(ssl_read_line(ssl, buf, sizeof(buf))) { 1369 if(buf[0] == 0x04 && buf[1] == 0) 1370 break; /* end of transmission */ 1371 if(!perform_data_remove(ssl, zones, buf)) { 1372 if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 1373 return; 1374 } 1375 else 1376 num++; 1377 } 1378 (void)ssl_printf(ssl, "removed %d datas\n", num); 1379 } 1380 1381 /** Add a new zone to view */ 1382 static void 1383 do_view_zone_add(RES* ssl, struct worker* worker, char* arg) 1384 { 1385 char* arg2; 1386 struct view* v; 1387 if(!find_arg2(ssl, arg, &arg2)) 1388 return; 1389 v = views_find_view(worker->daemon->views, 1390 arg, 1 /* get write lock*/); 1391 if(!v) { 1392 ssl_printf(ssl,"no view with name: %s\n", arg); 1393 return; 1394 } 1395 if(!v->local_zones) { 1396 if(!(v->local_zones = local_zones_create())){ 1397 lock_rw_unlock(&v->lock); 1398 ssl_printf(ssl,"error out of memory\n"); 1399 return; 1400 } 1401 if(!v->isfirst) { 1402 /* Global local-zone is not used for this view, 1403 * therefore add defaults to this view-specic 1404 * local-zone. */ 1405 struct config_file lz_cfg; 1406 memset(&lz_cfg, 0, sizeof(lz_cfg)); 1407 local_zone_enter_defaults(v->local_zones, &lz_cfg); 1408 } 1409 } 1410 do_zone_add(ssl, v->local_zones, arg2); 1411 lock_rw_unlock(&v->lock); 1412 } 1413 1414 /** Remove a zone from view */ 1415 static void 1416 do_view_zone_remove(RES* ssl, struct worker* worker, char* arg) 1417 { 1418 char* arg2; 1419 struct view* v; 1420 if(!find_arg2(ssl, arg, &arg2)) 1421 return; 1422 v = views_find_view(worker->daemon->views, 1423 arg, 1 /* get write lock*/); 1424 if(!v) { 1425 ssl_printf(ssl,"no view with name: %s\n", arg); 1426 return; 1427 } 1428 if(!v->local_zones) { 1429 lock_rw_unlock(&v->lock); 1430 send_ok(ssl); 1431 return; 1432 } 1433 do_zone_remove(ssl, v->local_zones, arg2); 1434 lock_rw_unlock(&v->lock); 1435 } 1436 1437 /** Add new RR data to view */ 1438 static void 1439 do_view_data_add(RES* ssl, struct worker* worker, char* arg) 1440 { 1441 char* arg2; 1442 struct view* v; 1443 if(!find_arg2(ssl, arg, &arg2)) 1444 return; 1445 v = views_find_view(worker->daemon->views, 1446 arg, 1 /* get write lock*/); 1447 if(!v) { 1448 ssl_printf(ssl,"no view with name: %s\n", arg); 1449 return; 1450 } 1451 if(!v->local_zones) { 1452 if(!(v->local_zones = local_zones_create())){ 1453 lock_rw_unlock(&v->lock); 1454 ssl_printf(ssl,"error out of memory\n"); 1455 return; 1456 } 1457 } 1458 do_data_add(ssl, v->local_zones, arg2); 1459 lock_rw_unlock(&v->lock); 1460 } 1461 1462 /** Add new RR data from stdin to view */ 1463 static void 1464 do_view_datas_add(RES* ssl, struct worker* worker, char* arg) 1465 { 1466 struct view* v; 1467 v = views_find_view(worker->daemon->views, 1468 arg, 1 /* get write lock*/); 1469 if(!v) { 1470 ssl_printf(ssl,"no view with name: %s\n", arg); 1471 return; 1472 } 1473 if(!v->local_zones) { 1474 if(!(v->local_zones = local_zones_create())){ 1475 lock_rw_unlock(&v->lock); 1476 ssl_printf(ssl,"error out of memory\n"); 1477 return; 1478 } 1479 } 1480 do_datas_add(ssl, v->local_zones); 1481 lock_rw_unlock(&v->lock); 1482 } 1483 1484 /** Remove RR data from view */ 1485 static void 1486 do_view_data_remove(RES* ssl, struct worker* worker, char* arg) 1487 { 1488 char* arg2; 1489 struct view* v; 1490 if(!find_arg2(ssl, arg, &arg2)) 1491 return; 1492 v = views_find_view(worker->daemon->views, 1493 arg, 1 /* get write lock*/); 1494 if(!v) { 1495 ssl_printf(ssl,"no view with name: %s\n", arg); 1496 return; 1497 } 1498 if(!v->local_zones) { 1499 lock_rw_unlock(&v->lock); 1500 send_ok(ssl); 1501 return; 1502 } 1503 do_data_remove(ssl, v->local_zones, arg2); 1504 lock_rw_unlock(&v->lock); 1505 } 1506 1507 /** Remove RR data from stdin from view */ 1508 static void 1509 do_view_datas_remove(RES* ssl, struct worker* worker, char* arg) 1510 { 1511 struct view* v; 1512 v = views_find_view(worker->daemon->views, 1513 arg, 1 /* get write lock*/); 1514 if(!v) { 1515 ssl_printf(ssl,"no view with name: %s\n", arg); 1516 return; 1517 } 1518 if(!v->local_zones){ 1519 lock_rw_unlock(&v->lock); 1520 ssl_printf(ssl, "removed 0 datas\n"); 1521 return; 1522 } 1523 1524 do_datas_remove(ssl, v->local_zones); 1525 lock_rw_unlock(&v->lock); 1526 } 1527 1528 /** cache lookup of nameservers */ 1529 static void 1530 do_lookup(RES* ssl, struct worker* worker, char* arg) 1531 { 1532 uint8_t* nm; 1533 int nmlabs; 1534 size_t nmlen; 1535 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1536 return; 1537 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs); 1538 free(nm); 1539 } 1540 1541 /** flush something from rrset and msg caches */ 1542 static void 1543 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen, 1544 uint16_t t, uint16_t c) 1545 { 1546 hashvalue_type h; 1547 struct query_info k; 1548 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0); 1549 if(t == LDNS_RR_TYPE_SOA) 1550 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 1551 PACKED_RRSET_SOA_NEG); 1552 k.qname = nm; 1553 k.qname_len = nmlen; 1554 k.qtype = t; 1555 k.qclass = c; 1556 k.local_alias = NULL; 1557 h = query_info_hash(&k, 0); 1558 slabhash_remove(worker->env.msg_cache, h, &k); 1559 if(t == LDNS_RR_TYPE_AAAA) { 1560 /* for AAAA also flush dns64 bit_cd packet */ 1561 h = query_info_hash(&k, BIT_CD); 1562 slabhash_remove(worker->env.msg_cache, h, &k); 1563 } 1564 } 1565 1566 /** flush a type */ 1567 static void 1568 do_flush_type(RES* ssl, struct worker* worker, char* arg) 1569 { 1570 uint8_t* nm; 1571 int nmlabs; 1572 size_t nmlen; 1573 char* arg2; 1574 uint16_t t; 1575 if(!find_arg2(ssl, arg, &arg2)) 1576 return; 1577 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1578 return; 1579 t = sldns_get_rr_type_by_name(arg2); 1580 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN); 1581 1582 free(nm); 1583 send_ok(ssl); 1584 } 1585 1586 /** flush statistics */ 1587 static void 1588 do_flush_stats(RES* ssl, struct worker* worker) 1589 { 1590 worker_stats_clear(worker); 1591 send_ok(ssl); 1592 } 1593 1594 /** 1595 * Local info for deletion functions 1596 */ 1597 struct del_info { 1598 /** worker */ 1599 struct worker* worker; 1600 /** name to delete */ 1601 uint8_t* name; 1602 /** length */ 1603 size_t len; 1604 /** labels */ 1605 int labs; 1606 /** time to invalidate to */ 1607 time_t expired; 1608 /** number of rrsets removed */ 1609 size_t num_rrsets; 1610 /** number of msgs removed */ 1611 size_t num_msgs; 1612 /** number of key entries removed */ 1613 size_t num_keys; 1614 /** length of addr */ 1615 socklen_t addrlen; 1616 /** socket address for host deletion */ 1617 struct sockaddr_storage addr; 1618 }; 1619 1620 /** callback to delete hosts in infra cache */ 1621 static void 1622 infra_del_host(struct lruhash_entry* e, void* arg) 1623 { 1624 /* entry is locked */ 1625 struct del_info* inf = (struct del_info*)arg; 1626 struct infra_key* k = (struct infra_key*)e->key; 1627 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) { 1628 struct infra_data* d = (struct infra_data*)e->data; 1629 d->probedelay = 0; 1630 d->timeout_A = 0; 1631 d->timeout_AAAA = 0; 1632 d->timeout_other = 0; 1633 rtt_init(&d->rtt); 1634 if(d->ttl > inf->expired) { 1635 d->ttl = inf->expired; 1636 inf->num_keys++; 1637 } 1638 } 1639 } 1640 1641 /** flush infra cache */ 1642 static void 1643 do_flush_infra(RES* ssl, struct worker* worker, char* arg) 1644 { 1645 struct sockaddr_storage addr; 1646 socklen_t len; 1647 struct del_info inf; 1648 if(strcmp(arg, "all") == 0) { 1649 slabhash_clear(worker->env.infra_cache->hosts); 1650 send_ok(ssl); 1651 return; 1652 } 1653 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) { 1654 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg); 1655 return; 1656 } 1657 /* delete all entries from cache */ 1658 /* what we do is to set them all expired */ 1659 inf.worker = worker; 1660 inf.name = 0; 1661 inf.len = 0; 1662 inf.labs = 0; 1663 inf.expired = *worker->env.now; 1664 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1665 inf.num_rrsets = 0; 1666 inf.num_msgs = 0; 1667 inf.num_keys = 0; 1668 inf.addrlen = len; 1669 memmove(&inf.addr, &addr, len); 1670 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host, 1671 &inf); 1672 send_ok(ssl); 1673 } 1674 1675 /** flush requestlist */ 1676 static void 1677 do_flush_requestlist(RES* ssl, struct worker* worker) 1678 { 1679 mesh_delete_all(worker->env.mesh); 1680 send_ok(ssl); 1681 } 1682 1683 /** callback to delete rrsets in a zone */ 1684 static void 1685 zone_del_rrset(struct lruhash_entry* e, void* arg) 1686 { 1687 /* entry is locked */ 1688 struct del_info* inf = (struct del_info*)arg; 1689 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 1690 if(dname_subdomain_c(k->rk.dname, inf->name)) { 1691 struct packed_rrset_data* d = 1692 (struct packed_rrset_data*)e->data; 1693 if(d->ttl > inf->expired) { 1694 d->ttl = inf->expired; 1695 inf->num_rrsets++; 1696 } 1697 } 1698 } 1699 1700 /** callback to delete messages in a zone */ 1701 static void 1702 zone_del_msg(struct lruhash_entry* e, void* arg) 1703 { 1704 /* entry is locked */ 1705 struct del_info* inf = (struct del_info*)arg; 1706 struct msgreply_entry* k = (struct msgreply_entry*)e->key; 1707 if(dname_subdomain_c(k->key.qname, inf->name)) { 1708 struct reply_info* d = (struct reply_info*)e->data; 1709 if(d->ttl > inf->expired) { 1710 d->ttl = inf->expired; 1711 d->prefetch_ttl = inf->expired; 1712 d->serve_expired_ttl = inf->expired; 1713 inf->num_msgs++; 1714 } 1715 } 1716 } 1717 1718 /** callback to delete keys in zone */ 1719 static void 1720 zone_del_kcache(struct lruhash_entry* e, void* arg) 1721 { 1722 /* entry is locked */ 1723 struct del_info* inf = (struct del_info*)arg; 1724 struct key_entry_key* k = (struct key_entry_key*)e->key; 1725 if(dname_subdomain_c(k->name, inf->name)) { 1726 struct key_entry_data* d = (struct key_entry_data*)e->data; 1727 if(d->ttl > inf->expired) { 1728 d->ttl = inf->expired; 1729 inf->num_keys++; 1730 } 1731 } 1732 } 1733 1734 /** remove all rrsets and keys from zone from cache */ 1735 static void 1736 do_flush_zone(RES* ssl, struct worker* worker, char* arg) 1737 { 1738 uint8_t* nm; 1739 int nmlabs; 1740 size_t nmlen; 1741 struct del_info inf; 1742 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1743 return; 1744 /* delete all RRs and key entries from zone */ 1745 /* what we do is to set them all expired */ 1746 inf.worker = worker; 1747 inf.name = nm; 1748 inf.len = nmlen; 1749 inf.labs = nmlabs; 1750 inf.expired = *worker->env.now; 1751 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1752 inf.num_rrsets = 0; 1753 inf.num_msgs = 0; 1754 inf.num_keys = 0; 1755 slabhash_traverse(&worker->env.rrset_cache->table, 1, 1756 &zone_del_rrset, &inf); 1757 1758 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf); 1759 1760 /* and validator cache */ 1761 if(worker->env.key_cache) { 1762 slabhash_traverse(worker->env.key_cache->slab, 1, 1763 &zone_del_kcache, &inf); 1764 } 1765 1766 free(nm); 1767 1768 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1769 "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1770 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1771 } 1772 1773 /** callback to delete bogus rrsets */ 1774 static void 1775 bogus_del_rrset(struct lruhash_entry* e, void* arg) 1776 { 1777 /* entry is locked */ 1778 struct del_info* inf = (struct del_info*)arg; 1779 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 1780 if(d->security == sec_status_bogus) { 1781 d->ttl = inf->expired; 1782 inf->num_rrsets++; 1783 } 1784 } 1785 1786 /** callback to delete bogus messages */ 1787 static void 1788 bogus_del_msg(struct lruhash_entry* e, void* arg) 1789 { 1790 /* entry is locked */ 1791 struct del_info* inf = (struct del_info*)arg; 1792 struct reply_info* d = (struct reply_info*)e->data; 1793 if(d->security == sec_status_bogus) { 1794 d->ttl = inf->expired; 1795 inf->num_msgs++; 1796 } 1797 } 1798 1799 /** callback to delete bogus keys */ 1800 static void 1801 bogus_del_kcache(struct lruhash_entry* e, void* arg) 1802 { 1803 /* entry is locked */ 1804 struct del_info* inf = (struct del_info*)arg; 1805 struct key_entry_data* d = (struct key_entry_data*)e->data; 1806 if(d->isbad) { 1807 d->ttl = inf->expired; 1808 inf->num_keys++; 1809 } 1810 } 1811 1812 /** remove all bogus rrsets, msgs and keys from cache */ 1813 static void 1814 do_flush_bogus(RES* ssl, struct worker* worker) 1815 { 1816 struct del_info inf; 1817 /* what we do is to set them all expired */ 1818 inf.worker = worker; 1819 inf.expired = *worker->env.now; 1820 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1821 inf.num_rrsets = 0; 1822 inf.num_msgs = 0; 1823 inf.num_keys = 0; 1824 slabhash_traverse(&worker->env.rrset_cache->table, 1, 1825 &bogus_del_rrset, &inf); 1826 1827 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf); 1828 1829 /* and validator cache */ 1830 if(worker->env.key_cache) { 1831 slabhash_traverse(worker->env.key_cache->slab, 1, 1832 &bogus_del_kcache, &inf); 1833 } 1834 1835 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1836 "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1837 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1838 } 1839 1840 /** callback to delete negative and servfail rrsets */ 1841 static void 1842 negative_del_rrset(struct lruhash_entry* e, void* arg) 1843 { 1844 /* entry is locked */ 1845 struct del_info* inf = (struct del_info*)arg; 1846 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 1847 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 1848 /* delete the parentside negative cache rrsets, 1849 * these are nameserver rrsets that failed lookup, rdata empty */ 1850 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 && 1851 d->rrsig_count == 0 && d->rr_len[0] == 0) { 1852 d->ttl = inf->expired; 1853 inf->num_rrsets++; 1854 } 1855 } 1856 1857 /** callback to delete negative and servfail messages */ 1858 static void 1859 negative_del_msg(struct lruhash_entry* e, void* arg) 1860 { 1861 /* entry is locked */ 1862 struct del_info* inf = (struct del_info*)arg; 1863 struct reply_info* d = (struct reply_info*)e->data; 1864 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error 1865 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */ 1866 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) { 1867 d->ttl = inf->expired; 1868 inf->num_msgs++; 1869 } 1870 } 1871 1872 /** callback to delete negative key entries */ 1873 static void 1874 negative_del_kcache(struct lruhash_entry* e, void* arg) 1875 { 1876 /* entry is locked */ 1877 struct del_info* inf = (struct del_info*)arg; 1878 struct key_entry_data* d = (struct key_entry_data*)e->data; 1879 /* could be bad because of lookup failure on the DS, DNSKEY, which 1880 * was nxdomain or servfail, and thus a result of negative lookups */ 1881 if(d->isbad) { 1882 d->ttl = inf->expired; 1883 inf->num_keys++; 1884 } 1885 } 1886 1887 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */ 1888 static void 1889 do_flush_negative(RES* ssl, struct worker* worker) 1890 { 1891 struct del_info inf; 1892 /* what we do is to set them all expired */ 1893 inf.worker = worker; 1894 inf.expired = *worker->env.now; 1895 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1896 inf.num_rrsets = 0; 1897 inf.num_msgs = 0; 1898 inf.num_keys = 0; 1899 slabhash_traverse(&worker->env.rrset_cache->table, 1, 1900 &negative_del_rrset, &inf); 1901 1902 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf); 1903 1904 /* and validator cache */ 1905 if(worker->env.key_cache) { 1906 slabhash_traverse(worker->env.key_cache->slab, 1, 1907 &negative_del_kcache, &inf); 1908 } 1909 1910 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1911 "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1912 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1913 } 1914 1915 /** remove name rrset from cache */ 1916 static void 1917 do_flush_name(RES* ssl, struct worker* w, char* arg) 1918 { 1919 uint8_t* nm; 1920 int nmlabs; 1921 size_t nmlen; 1922 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1923 return; 1924 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN); 1925 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN); 1926 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN); 1927 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN); 1928 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN); 1929 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN); 1930 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN); 1931 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN); 1932 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN); 1933 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN); 1934 1935 free(nm); 1936 send_ok(ssl); 1937 } 1938 1939 /** printout a delegation point info */ 1940 static int 1941 ssl_print_name_dp(RES* ssl, const char* str, uint8_t* nm, uint16_t dclass, 1942 struct delegpt* dp) 1943 { 1944 char buf[257]; 1945 struct delegpt_ns* ns; 1946 struct delegpt_addr* a; 1947 int f = 0; 1948 if(str) { /* print header for forward, stub */ 1949 char* c = sldns_wire2str_class(dclass); 1950 dname_str(nm, buf); 1951 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) { 1952 free(c); 1953 return 0; 1954 } 1955 free(c); 1956 } 1957 for(ns = dp->nslist; ns; ns = ns->next) { 1958 dname_str(ns->name, buf); 1959 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 1960 return 0; 1961 f = 1; 1962 } 1963 for(a = dp->target_list; a; a = a->next_target) { 1964 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf)); 1965 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 1966 return 0; 1967 f = 1; 1968 } 1969 return ssl_printf(ssl, "\n"); 1970 } 1971 1972 1973 /** print root forwards */ 1974 static int 1975 print_root_fwds(RES* ssl, struct iter_forwards* fwds, uint8_t* root) 1976 { 1977 struct delegpt* dp; 1978 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN); 1979 if(!dp) 1980 return ssl_printf(ssl, "off (using root hints)\n"); 1981 /* if dp is returned it must be the root */ 1982 log_assert(query_dname_compare(dp->name, root)==0); 1983 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp); 1984 } 1985 1986 /** parse args into delegpt */ 1987 static struct delegpt* 1988 parse_delegpt(RES* ssl, char* args, uint8_t* nm, int allow_names) 1989 { 1990 /* parse args and add in */ 1991 char* p = args; 1992 char* todo; 1993 struct delegpt* dp = delegpt_create_mlc(nm); 1994 struct sockaddr_storage addr; 1995 socklen_t addrlen; 1996 char* auth_name; 1997 if(!dp) { 1998 (void)ssl_printf(ssl, "error out of memory\n"); 1999 return NULL; 2000 } 2001 while(p) { 2002 todo = p; 2003 p = strchr(p, ' '); /* find next spot, if any */ 2004 if(p) { 2005 *p++ = 0; /* end this spot */ 2006 p = skipwhite(p); /* position at next spot */ 2007 } 2008 /* parse address */ 2009 if(!authextstrtoaddr(todo, &addr, &addrlen, &auth_name)) { 2010 if(allow_names) { 2011 uint8_t* n = NULL; 2012 size_t ln; 2013 int lb; 2014 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) { 2015 (void)ssl_printf(ssl, "error cannot " 2016 "parse IP address or name " 2017 "'%s'\n", todo); 2018 delegpt_free_mlc(dp); 2019 return NULL; 2020 } 2021 if(!delegpt_add_ns_mlc(dp, n, 0)) { 2022 (void)ssl_printf(ssl, "error out of memory\n"); 2023 free(n); 2024 delegpt_free_mlc(dp); 2025 return NULL; 2026 } 2027 free(n); 2028 2029 } else { 2030 (void)ssl_printf(ssl, "error cannot parse" 2031 " IP address '%s'\n", todo); 2032 delegpt_free_mlc(dp); 2033 return NULL; 2034 } 2035 } else { 2036 #if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST) 2037 if(auth_name) 2038 log_err("no name verification functionality in " 2039 "ssl library, ignored name for %s", todo); 2040 #endif 2041 /* add address */ 2042 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0, 2043 auth_name)) { 2044 (void)ssl_printf(ssl, "error out of memory\n"); 2045 delegpt_free_mlc(dp); 2046 return NULL; 2047 } 2048 } 2049 } 2050 dp->has_parent_side_NS = 1; 2051 return dp; 2052 } 2053 2054 /** do the status command */ 2055 static void 2056 do_forward(RES* ssl, struct worker* worker, char* args) 2057 { 2058 struct iter_forwards* fwd = worker->env.fwds; 2059 uint8_t* root = (uint8_t*)"\000"; 2060 if(!fwd) { 2061 (void)ssl_printf(ssl, "error: structure not allocated\n"); 2062 return; 2063 } 2064 if(args == NULL || args[0] == 0) { 2065 (void)print_root_fwds(ssl, fwd, root); 2066 return; 2067 } 2068 /* set root forwards for this thread. since we are in remote control 2069 * the actual mesh is not running, so we can freely edit it. */ 2070 /* delete all the existing queries first */ 2071 mesh_delete_all(worker->env.mesh); 2072 if(strcmp(args, "off") == 0) { 2073 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root); 2074 } else { 2075 struct delegpt* dp; 2076 if(!(dp = parse_delegpt(ssl, args, root, 0))) 2077 return; 2078 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) { 2079 (void)ssl_printf(ssl, "error out of memory\n"); 2080 return; 2081 } 2082 } 2083 send_ok(ssl); 2084 } 2085 2086 static int 2087 parse_fs_args(RES* ssl, char* args, uint8_t** nm, struct delegpt** dp, 2088 int* insecure, int* prime) 2089 { 2090 char* zonename; 2091 char* rest; 2092 size_t nmlen; 2093 int nmlabs; 2094 /* parse all -x args */ 2095 while(args[0] == '+') { 2096 if(!find_arg2(ssl, args, &rest)) 2097 return 0; 2098 while(*(++args) != 0) { 2099 if(*args == 'i' && insecure) 2100 *insecure = 1; 2101 else if(*args == 'p' && prime) 2102 *prime = 1; 2103 else { 2104 (void)ssl_printf(ssl, "error: unknown option %s\n", args); 2105 return 0; 2106 } 2107 } 2108 args = rest; 2109 } 2110 /* parse name */ 2111 if(dp) { 2112 if(!find_arg2(ssl, args, &rest)) 2113 return 0; 2114 zonename = args; 2115 args = rest; 2116 } else zonename = args; 2117 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs)) 2118 return 0; 2119 2120 /* parse dp */ 2121 if(dp) { 2122 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) { 2123 free(*nm); 2124 return 0; 2125 } 2126 } 2127 return 1; 2128 } 2129 2130 /** do the forward_add command */ 2131 static void 2132 do_forward_add(RES* ssl, struct worker* worker, char* args) 2133 { 2134 struct iter_forwards* fwd = worker->env.fwds; 2135 int insecure = 0; 2136 uint8_t* nm = NULL; 2137 struct delegpt* dp = NULL; 2138 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL)) 2139 return; 2140 if(insecure && worker->env.anchors) { 2141 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2142 nm)) { 2143 (void)ssl_printf(ssl, "error out of memory\n"); 2144 delegpt_free_mlc(dp); 2145 free(nm); 2146 return; 2147 } 2148 } 2149 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) { 2150 (void)ssl_printf(ssl, "error out of memory\n"); 2151 free(nm); 2152 return; 2153 } 2154 free(nm); 2155 send_ok(ssl); 2156 } 2157 2158 /** do the forward_remove command */ 2159 static void 2160 do_forward_remove(RES* ssl, struct worker* worker, char* args) 2161 { 2162 struct iter_forwards* fwd = worker->env.fwds; 2163 int insecure = 0; 2164 uint8_t* nm = NULL; 2165 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL)) 2166 return; 2167 if(insecure && worker->env.anchors) 2168 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2169 nm); 2170 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm); 2171 free(nm); 2172 send_ok(ssl); 2173 } 2174 2175 /** do the stub_add command */ 2176 static void 2177 do_stub_add(RES* ssl, struct worker* worker, char* args) 2178 { 2179 struct iter_forwards* fwd = worker->env.fwds; 2180 int insecure = 0, prime = 0; 2181 uint8_t* nm = NULL; 2182 struct delegpt* dp = NULL; 2183 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime)) 2184 return; 2185 if(insecure && worker->env.anchors) { 2186 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2187 nm)) { 2188 (void)ssl_printf(ssl, "error out of memory\n"); 2189 delegpt_free_mlc(dp); 2190 free(nm); 2191 return; 2192 } 2193 } 2194 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) { 2195 if(insecure && worker->env.anchors) 2196 anchors_delete_insecure(worker->env.anchors, 2197 LDNS_RR_CLASS_IN, nm); 2198 (void)ssl_printf(ssl, "error out of memory\n"); 2199 delegpt_free_mlc(dp); 2200 free(nm); 2201 return; 2202 } 2203 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) { 2204 (void)ssl_printf(ssl, "error out of memory\n"); 2205 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm); 2206 if(insecure && worker->env.anchors) 2207 anchors_delete_insecure(worker->env.anchors, 2208 LDNS_RR_CLASS_IN, nm); 2209 free(nm); 2210 return; 2211 } 2212 free(nm); 2213 send_ok(ssl); 2214 } 2215 2216 /** do the stub_remove command */ 2217 static void 2218 do_stub_remove(RES* ssl, struct worker* worker, char* args) 2219 { 2220 struct iter_forwards* fwd = worker->env.fwds; 2221 int insecure = 0; 2222 uint8_t* nm = NULL; 2223 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL)) 2224 return; 2225 if(insecure && worker->env.anchors) 2226 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2227 nm); 2228 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm); 2229 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm); 2230 free(nm); 2231 send_ok(ssl); 2232 } 2233 2234 /** do the insecure_add command */ 2235 static void 2236 do_insecure_add(RES* ssl, struct worker* worker, char* arg) 2237 { 2238 size_t nmlen; 2239 int nmlabs; 2240 uint8_t* nm = NULL; 2241 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2242 return; 2243 if(worker->env.anchors) { 2244 if(!anchors_add_insecure(worker->env.anchors, 2245 LDNS_RR_CLASS_IN, nm)) { 2246 (void)ssl_printf(ssl, "error out of memory\n"); 2247 free(nm); 2248 return; 2249 } 2250 } 2251 free(nm); 2252 send_ok(ssl); 2253 } 2254 2255 /** do the insecure_remove command */ 2256 static void 2257 do_insecure_remove(RES* ssl, struct worker* worker, char* arg) 2258 { 2259 size_t nmlen; 2260 int nmlabs; 2261 uint8_t* nm = NULL; 2262 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2263 return; 2264 if(worker->env.anchors) 2265 anchors_delete_insecure(worker->env.anchors, 2266 LDNS_RR_CLASS_IN, nm); 2267 free(nm); 2268 send_ok(ssl); 2269 } 2270 2271 static void 2272 do_insecure_list(RES* ssl, struct worker* worker) 2273 { 2274 char buf[257]; 2275 struct trust_anchor* a; 2276 if(worker->env.anchors) { 2277 RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) { 2278 if(a->numDS == 0 && a->numDNSKEY == 0) { 2279 dname_str(a->name, buf); 2280 ssl_printf(ssl, "%s\n", buf); 2281 } 2282 } 2283 } 2284 } 2285 2286 /** do the status command */ 2287 static void 2288 do_status(RES* ssl, struct worker* worker) 2289 { 2290 int i; 2291 time_t uptime; 2292 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION)) 2293 return; 2294 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity)) 2295 return; 2296 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num)) 2297 return; 2298 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num)) 2299 return; 2300 for(i=0; i<worker->daemon->mods.num; i++) { 2301 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name)) 2302 return; 2303 } 2304 if(!ssl_printf(ssl, " ]\n")) 2305 return; 2306 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec; 2307 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime)) 2308 return; 2309 if(!ssl_printf(ssl, "options:%s%s%s%s\n" , 2310 (worker->daemon->reuseport?" reuseport":""), 2311 (worker->daemon->rc->accept_list?" control":""), 2312 (worker->daemon->rc->accept_list && worker->daemon->rc->use_cert?"(ssl)":""), 2313 (worker->daemon->rc->accept_list && worker->daemon->cfg->control_ifs.first && worker->daemon->cfg->control_ifs.first->str && worker->daemon->cfg->control_ifs.first->str[0] == '/'?"(namedpipe)":"") 2314 )) 2315 return; 2316 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n", 2317 (int)getpid())) 2318 return; 2319 } 2320 2321 /** get age for the mesh state */ 2322 static void 2323 get_mesh_age(struct mesh_state* m, char* buf, size_t len, 2324 struct module_env* env) 2325 { 2326 if(m->reply_list) { 2327 struct timeval d; 2328 struct mesh_reply* r = m->reply_list; 2329 /* last reply is the oldest */ 2330 while(r && r->next) 2331 r = r->next; 2332 timeval_subtract(&d, env->now_tv, &r->start_time); 2333 snprintf(buf, len, ARG_LL "d.%6.6d", 2334 (long long)d.tv_sec, (int)d.tv_usec); 2335 } else { 2336 snprintf(buf, len, "-"); 2337 } 2338 } 2339 2340 /** get status of a mesh state */ 2341 static void 2342 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m, 2343 char* buf, size_t len) 2344 { 2345 enum module_ext_state s = m->s.ext_state[m->s.curmod]; 2346 const char *modname = mesh->mods.mod[m->s.curmod]->name; 2347 size_t l; 2348 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply && 2349 m->s.minfo[m->s.curmod]) { 2350 /* break into iterator to find out who its waiting for */ 2351 struct iter_qstate* qstate = (struct iter_qstate*) 2352 m->s.minfo[m->s.curmod]; 2353 struct outbound_list* ol = &qstate->outlist; 2354 struct outbound_entry* e; 2355 snprintf(buf, len, "%s wait for", modname); 2356 l = strlen(buf); 2357 buf += l; len -= l; 2358 if(ol->first == NULL) 2359 snprintf(buf, len, " (empty_list)"); 2360 for(e = ol->first; e; e = e->next) { 2361 snprintf(buf, len, " "); 2362 l = strlen(buf); 2363 buf += l; len -= l; 2364 addr_to_str(&e->qsent->addr, e->qsent->addrlen, 2365 buf, len); 2366 l = strlen(buf); 2367 buf += l; len -= l; 2368 } 2369 } else if(s == module_wait_subquery) { 2370 /* look in subs from mesh state to see what */ 2371 char nm[257]; 2372 struct mesh_state_ref* sub; 2373 snprintf(buf, len, "%s wants", modname); 2374 l = strlen(buf); 2375 buf += l; len -= l; 2376 if(m->sub_set.count == 0) 2377 snprintf(buf, len, " (empty_list)"); 2378 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) { 2379 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype); 2380 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass); 2381 dname_str(sub->s->s.qinfo.qname, nm); 2382 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"), 2383 (c?c:"CLASS??"), nm); 2384 l = strlen(buf); 2385 buf += l; len -= l; 2386 free(t); 2387 free(c); 2388 } 2389 } else { 2390 snprintf(buf, len, "%s is %s", modname, strextstate(s)); 2391 } 2392 } 2393 2394 /** do the dump_requestlist command */ 2395 static void 2396 do_dump_requestlist(RES* ssl, struct worker* worker) 2397 { 2398 struct mesh_area* mesh; 2399 struct mesh_state* m; 2400 int num = 0; 2401 char buf[257]; 2402 char timebuf[32]; 2403 char statbuf[10240]; 2404 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num)) 2405 return; 2406 if(!ssl_printf(ssl, "# type cl name seconds module status\n")) 2407 return; 2408 /* show worker mesh contents */ 2409 mesh = worker->env.mesh; 2410 if(!mesh) return; 2411 RBTREE_FOR(m, struct mesh_state*, &mesh->all) { 2412 char* t = sldns_wire2str_type(m->s.qinfo.qtype); 2413 char* c = sldns_wire2str_class(m->s.qinfo.qclass); 2414 dname_str(m->s.qinfo.qname, buf); 2415 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env); 2416 get_mesh_status(mesh, m, statbuf, sizeof(statbuf)); 2417 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n", 2418 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf, 2419 statbuf)) { 2420 free(t); 2421 free(c); 2422 return; 2423 } 2424 num++; 2425 free(t); 2426 free(c); 2427 } 2428 } 2429 2430 /** structure for argument data for dump infra host */ 2431 struct infra_arg { 2432 /** the infra cache */ 2433 struct infra_cache* infra; 2434 /** the SSL connection */ 2435 RES* ssl; 2436 /** the time now */ 2437 time_t now; 2438 /** ssl failure? stop writing and skip the rest. If the tcp 2439 * connection is broken, and writes fail, we then stop writing. */ 2440 int ssl_failed; 2441 }; 2442 2443 /** callback for every host element in the infra cache */ 2444 static void 2445 dump_infra_host(struct lruhash_entry* e, void* arg) 2446 { 2447 struct infra_arg* a = (struct infra_arg*)arg; 2448 struct infra_key* k = (struct infra_key*)e->key; 2449 struct infra_data* d = (struct infra_data*)e->data; 2450 char ip_str[1024]; 2451 char name[257]; 2452 int port; 2453 if(a->ssl_failed) 2454 return; 2455 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str)); 2456 dname_str(k->zonename, name); 2457 port = (int)ntohs(((struct sockaddr_in*)&k->addr)->sin_port); 2458 if(port != UNBOUND_DNS_PORT) { 2459 snprintf(ip_str+strlen(ip_str), sizeof(ip_str)-strlen(ip_str), 2460 "@%d", port); 2461 } 2462 /* skip expired stuff (only backed off) */ 2463 if(d->ttl < a->now) { 2464 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) { 2465 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str, 2466 name, d->rtt.rto)) { 2467 a->ssl_failed = 1; 2468 return; 2469 } 2470 } 2471 return; 2472 } 2473 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d " 2474 "tA %d tAAAA %d tother %d " 2475 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d " 2476 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now), 2477 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto, 2478 d->timeout_A, d->timeout_AAAA, d->timeout_other, 2479 (int)d->edns_lame_known, (int)d->edns_version, 2480 (int)(a->now<d->probedelay?(d->probedelay - a->now):0), 2481 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A, 2482 (int)d->lame_other)) { 2483 a->ssl_failed = 1; 2484 return; 2485 } 2486 } 2487 2488 /** do the dump_infra command */ 2489 static void 2490 do_dump_infra(RES* ssl, struct worker* worker) 2491 { 2492 struct infra_arg arg; 2493 arg.infra = worker->env.infra_cache; 2494 arg.ssl = ssl; 2495 arg.now = *worker->env.now; 2496 arg.ssl_failed = 0; 2497 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg); 2498 } 2499 2500 /** do the log_reopen command */ 2501 static void 2502 do_log_reopen(RES* ssl, struct worker* worker) 2503 { 2504 struct config_file* cfg = worker->env.cfg; 2505 send_ok(ssl); 2506 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir); 2507 } 2508 2509 /** do the auth_zone_reload command */ 2510 static void 2511 do_auth_zone_reload(RES* ssl, struct worker* worker, char* arg) 2512 { 2513 size_t nmlen; 2514 int nmlabs; 2515 uint8_t* nm = NULL; 2516 struct auth_zones* az = worker->env.auth_zones; 2517 struct auth_zone* z = NULL; 2518 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2519 return; 2520 if(az) { 2521 lock_rw_rdlock(&az->lock); 2522 z = auth_zone_find(az, nm, nmlen, LDNS_RR_CLASS_IN); 2523 if(z) { 2524 lock_rw_wrlock(&z->lock); 2525 } 2526 lock_rw_unlock(&az->lock); 2527 } 2528 free(nm); 2529 if(!z) { 2530 (void)ssl_printf(ssl, "error no auth-zone %s\n", arg); 2531 return; 2532 } 2533 if(!auth_zone_read_zonefile(z, worker->env.cfg)) { 2534 lock_rw_unlock(&z->lock); 2535 (void)ssl_printf(ssl, "error failed to read %s\n", arg); 2536 return; 2537 } 2538 lock_rw_unlock(&z->lock); 2539 send_ok(ssl); 2540 } 2541 2542 /** do the auth_zone_transfer command */ 2543 static void 2544 do_auth_zone_transfer(RES* ssl, struct worker* worker, char* arg) 2545 { 2546 size_t nmlen; 2547 int nmlabs; 2548 uint8_t* nm = NULL; 2549 struct auth_zones* az = worker->env.auth_zones; 2550 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2551 return; 2552 if(!az || !auth_zones_startprobesequence(az, &worker->env, nm, nmlen, 2553 LDNS_RR_CLASS_IN)) { 2554 (void)ssl_printf(ssl, "error zone xfr task not found %s\n", arg); 2555 free(nm); 2556 return; 2557 } 2558 free(nm); 2559 send_ok(ssl); 2560 } 2561 2562 /** do the set_option command */ 2563 static void 2564 do_set_option(RES* ssl, struct worker* worker, char* arg) 2565 { 2566 char* arg2; 2567 if(!find_arg2(ssl, arg, &arg2)) 2568 return; 2569 if(!config_set_option(worker->env.cfg, arg, arg2)) { 2570 (void)ssl_printf(ssl, "error setting option\n"); 2571 return; 2572 } 2573 /* effectuate some arguments */ 2574 if(strcmp(arg, "val-override-date:") == 0) { 2575 int m = modstack_find(&worker->env.mesh->mods, "validator"); 2576 struct val_env* val_env = NULL; 2577 if(m != -1) val_env = (struct val_env*)worker->env.modinfo[m]; 2578 if(val_env) 2579 val_env->date_override = worker->env.cfg->val_date_override; 2580 } 2581 send_ok(ssl); 2582 } 2583 2584 /* routine to printout option values over SSL */ 2585 void remote_get_opt_ssl(char* line, void* arg) 2586 { 2587 RES* ssl = (RES*)arg; 2588 (void)ssl_printf(ssl, "%s\n", line); 2589 } 2590 2591 /** do the get_option command */ 2592 static void 2593 do_get_option(RES* ssl, struct worker* worker, char* arg) 2594 { 2595 int r; 2596 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl); 2597 if(!r) { 2598 (void)ssl_printf(ssl, "error unknown option\n"); 2599 return; 2600 } 2601 } 2602 2603 /** do the list_forwards command */ 2604 static void 2605 do_list_forwards(RES* ssl, struct worker* worker) 2606 { 2607 /* since its a per-worker structure no locks needed */ 2608 struct iter_forwards* fwds = worker->env.fwds; 2609 struct iter_forward_zone* z; 2610 struct trust_anchor* a; 2611 int insecure; 2612 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) { 2613 if(!z->dp) continue; /* skip empty marker for stub */ 2614 2615 /* see if it is insecure */ 2616 insecure = 0; 2617 if(worker->env.anchors && 2618 (a=anchor_find(worker->env.anchors, z->name, 2619 z->namelabs, z->namelen, z->dclass))) { 2620 if(!a->keylist && !a->numDS && !a->numDNSKEY) 2621 insecure = 1; 2622 lock_basic_unlock(&a->lock); 2623 } 2624 2625 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"), 2626 z->name, z->dclass, z->dp)) 2627 return; 2628 } 2629 } 2630 2631 /** do the list_stubs command */ 2632 static void 2633 do_list_stubs(RES* ssl, struct worker* worker) 2634 { 2635 struct iter_hints_stub* z; 2636 struct trust_anchor* a; 2637 int insecure; 2638 char str[32]; 2639 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) { 2640 2641 /* see if it is insecure */ 2642 insecure = 0; 2643 if(worker->env.anchors && 2644 (a=anchor_find(worker->env.anchors, z->node.name, 2645 z->node.labs, z->node.len, z->node.dclass))) { 2646 if(!a->keylist && !a->numDS && !a->numDNSKEY) 2647 insecure = 1; 2648 lock_basic_unlock(&a->lock); 2649 } 2650 2651 snprintf(str, sizeof(str), "stub %sprime%s", 2652 (z->noprime?"no":""), (insecure?" +i":"")); 2653 if(!ssl_print_name_dp(ssl, str, z->node.name, 2654 z->node.dclass, z->dp)) 2655 return; 2656 } 2657 } 2658 2659 /** do the list_auth_zones command */ 2660 static void 2661 do_list_auth_zones(RES* ssl, struct auth_zones* az) 2662 { 2663 struct auth_zone* z; 2664 char buf[257], buf2[256]; 2665 lock_rw_rdlock(&az->lock); 2666 RBTREE_FOR(z, struct auth_zone*, &az->ztree) { 2667 lock_rw_rdlock(&z->lock); 2668 dname_str(z->name, buf); 2669 if(z->zone_expired) 2670 snprintf(buf2, sizeof(buf2), "expired"); 2671 else { 2672 uint32_t serial = 0; 2673 if(auth_zone_get_serial(z, &serial)) 2674 snprintf(buf2, sizeof(buf2), "serial %u", 2675 (unsigned)serial); 2676 else snprintf(buf2, sizeof(buf2), "no serial"); 2677 } 2678 if(!ssl_printf(ssl, "%s\t%s\n", buf, buf2)) { 2679 /* failure to print */ 2680 lock_rw_unlock(&z->lock); 2681 lock_rw_unlock(&az->lock); 2682 return; 2683 } 2684 lock_rw_unlock(&z->lock); 2685 } 2686 lock_rw_unlock(&az->lock); 2687 } 2688 2689 /** do the list_local_zones command */ 2690 static void 2691 do_list_local_zones(RES* ssl, struct local_zones* zones) 2692 { 2693 struct local_zone* z; 2694 char buf[257]; 2695 lock_rw_rdlock(&zones->lock); 2696 RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2697 lock_rw_rdlock(&z->lock); 2698 dname_str(z->name, buf); 2699 if(!ssl_printf(ssl, "%s %s\n", buf, 2700 local_zone_type2str(z->type))) { 2701 /* failure to print */ 2702 lock_rw_unlock(&z->lock); 2703 lock_rw_unlock(&zones->lock); 2704 return; 2705 } 2706 lock_rw_unlock(&z->lock); 2707 } 2708 lock_rw_unlock(&zones->lock); 2709 } 2710 2711 /** do the list_local_data command */ 2712 static void 2713 do_list_local_data(RES* ssl, struct worker* worker, struct local_zones* zones) 2714 { 2715 struct local_zone* z; 2716 struct local_data* d; 2717 struct local_rrset* p; 2718 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer); 2719 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer); 2720 lock_rw_rdlock(&zones->lock); 2721 RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2722 lock_rw_rdlock(&z->lock); 2723 RBTREE_FOR(d, struct local_data*, &z->data) { 2724 for(p = d->rrsets; p; p = p->next) { 2725 struct packed_rrset_data* d = 2726 (struct packed_rrset_data*)p->rrset->entry.data; 2727 size_t i; 2728 for(i=0; i<d->count + d->rrsig_count; i++) { 2729 if(!packed_rr_to_string(p->rrset, i, 2730 0, s, slen)) { 2731 if(!ssl_printf(ssl, "BADRR\n")) { 2732 lock_rw_unlock(&z->lock); 2733 lock_rw_unlock(&zones->lock); 2734 return; 2735 } 2736 } 2737 if(!ssl_printf(ssl, "%s\n", s)) { 2738 lock_rw_unlock(&z->lock); 2739 lock_rw_unlock(&zones->lock); 2740 return; 2741 } 2742 } 2743 } 2744 } 2745 lock_rw_unlock(&z->lock); 2746 } 2747 lock_rw_unlock(&zones->lock); 2748 } 2749 2750 /** do the view_list_local_zones command */ 2751 static void 2752 do_view_list_local_zones(RES* ssl, struct worker* worker, char* arg) 2753 { 2754 struct view* v = views_find_view(worker->daemon->views, 2755 arg, 0 /* get read lock*/); 2756 if(!v) { 2757 ssl_printf(ssl,"no view with name: %s\n", arg); 2758 return; 2759 } 2760 if(v->local_zones) { 2761 do_list_local_zones(ssl, v->local_zones); 2762 } 2763 lock_rw_unlock(&v->lock); 2764 } 2765 2766 /** do the view_list_local_data command */ 2767 static void 2768 do_view_list_local_data(RES* ssl, struct worker* worker, char* arg) 2769 { 2770 struct view* v = views_find_view(worker->daemon->views, 2771 arg, 0 /* get read lock*/); 2772 if(!v) { 2773 ssl_printf(ssl,"no view with name: %s\n", arg); 2774 return; 2775 } 2776 if(v->local_zones) { 2777 do_list_local_data(ssl, worker, v->local_zones); 2778 } 2779 lock_rw_unlock(&v->lock); 2780 } 2781 2782 /** struct for user arg ratelimit list */ 2783 struct ratelimit_list_arg { 2784 /** the infra cache */ 2785 struct infra_cache* infra; 2786 /** the SSL to print to */ 2787 RES* ssl; 2788 /** all or only ratelimited */ 2789 int all; 2790 /** current time */ 2791 time_t now; 2792 }; 2793 2794 #define ip_ratelimit_list_arg ratelimit_list_arg 2795 2796 /** list items in the ratelimit table */ 2797 static void 2798 rate_list(struct lruhash_entry* e, void* arg) 2799 { 2800 struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg; 2801 struct rate_key* k = (struct rate_key*)e->key; 2802 struct rate_data* d = (struct rate_data*)e->data; 2803 char buf[257]; 2804 int lim = infra_find_ratelimit(a->infra, k->name, k->namelen); 2805 int max = infra_rate_max(d, a->now); 2806 if(a->all == 0) { 2807 if(max < lim) 2808 return; 2809 } 2810 dname_str(k->name, buf); 2811 ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim); 2812 } 2813 2814 /** list items in the ip_ratelimit table */ 2815 static void 2816 ip_rate_list(struct lruhash_entry* e, void* arg) 2817 { 2818 char ip[128]; 2819 struct ip_ratelimit_list_arg* a = (struct ip_ratelimit_list_arg*)arg; 2820 struct ip_rate_key* k = (struct ip_rate_key*)e->key; 2821 struct ip_rate_data* d = (struct ip_rate_data*)e->data; 2822 int lim = infra_ip_ratelimit; 2823 int max = infra_rate_max(d, a->now); 2824 if(a->all == 0) { 2825 if(max < lim) 2826 return; 2827 } 2828 addr_to_str(&k->addr, k->addrlen, ip, sizeof(ip)); 2829 ssl_printf(a->ssl, "%s %d limit %d\n", ip, max, lim); 2830 } 2831 2832 /** do the ratelimit_list command */ 2833 static void 2834 do_ratelimit_list(RES* ssl, struct worker* worker, char* arg) 2835 { 2836 struct ratelimit_list_arg a; 2837 a.all = 0; 2838 a.infra = worker->env.infra_cache; 2839 a.now = *worker->env.now; 2840 a.ssl = ssl; 2841 arg = skipwhite(arg); 2842 if(strcmp(arg, "+a") == 0) 2843 a.all = 1; 2844 if(a.infra->domain_rates==NULL || 2845 (a.all == 0 && infra_dp_ratelimit == 0)) 2846 return; 2847 slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a); 2848 } 2849 2850 /** do the ip_ratelimit_list command */ 2851 static void 2852 do_ip_ratelimit_list(RES* ssl, struct worker* worker, char* arg) 2853 { 2854 struct ip_ratelimit_list_arg a; 2855 a.all = 0; 2856 a.infra = worker->env.infra_cache; 2857 a.now = *worker->env.now; 2858 a.ssl = ssl; 2859 arg = skipwhite(arg); 2860 if(strcmp(arg, "+a") == 0) 2861 a.all = 1; 2862 if(a.infra->client_ip_rates==NULL || 2863 (a.all == 0 && infra_ip_ratelimit == 0)) 2864 return; 2865 slabhash_traverse(a.infra->client_ip_rates, 0, ip_rate_list, &a); 2866 } 2867 2868 /** tell other processes to execute the command */ 2869 static void 2870 distribute_cmd(struct daemon_remote* rc, RES* ssl, char* cmd) 2871 { 2872 int i; 2873 if(!cmd || !ssl) 2874 return; 2875 /* skip i=0 which is me */ 2876 for(i=1; i<rc->worker->daemon->num; i++) { 2877 worker_send_cmd(rc->worker->daemon->workers[i], 2878 worker_cmd_remote); 2879 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd, 2880 (uint8_t*)cmd, strlen(cmd)+1, 0)) { 2881 ssl_printf(ssl, "error could not distribute cmd\n"); 2882 return; 2883 } 2884 } 2885 } 2886 2887 /** check for name with end-of-string, space or tab after it */ 2888 static int 2889 cmdcmp(char* p, const char* cmd, size_t len) 2890 { 2891 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t'); 2892 } 2893 2894 /** execute a remote control command */ 2895 static void 2896 execute_cmd(struct daemon_remote* rc, RES* ssl, char* cmd, 2897 struct worker* worker) 2898 { 2899 char* p = skipwhite(cmd); 2900 /* compare command */ 2901 if(cmdcmp(p, "stop", 4)) { 2902 do_stop(ssl, worker); 2903 return; 2904 } else if(cmdcmp(p, "reload", 6)) { 2905 do_reload(ssl, worker); 2906 return; 2907 } else if(cmdcmp(p, "stats_noreset", 13)) { 2908 do_stats(ssl, worker, 0); 2909 return; 2910 } else if(cmdcmp(p, "stats", 5)) { 2911 do_stats(ssl, worker, 1); 2912 return; 2913 } else if(cmdcmp(p, "status", 6)) { 2914 do_status(ssl, worker); 2915 return; 2916 } else if(cmdcmp(p, "dump_cache", 10)) { 2917 (void)dump_cache(ssl, worker); 2918 return; 2919 } else if(cmdcmp(p, "load_cache", 10)) { 2920 if(load_cache(ssl, worker)) send_ok(ssl); 2921 return; 2922 } else if(cmdcmp(p, "list_forwards", 13)) { 2923 do_list_forwards(ssl, worker); 2924 return; 2925 } else if(cmdcmp(p, "list_stubs", 10)) { 2926 do_list_stubs(ssl, worker); 2927 return; 2928 } else if(cmdcmp(p, "list_insecure", 13)) { 2929 do_insecure_list(ssl, worker); 2930 return; 2931 } else if(cmdcmp(p, "list_local_zones", 16)) { 2932 do_list_local_zones(ssl, worker->daemon->local_zones); 2933 return; 2934 } else if(cmdcmp(p, "list_local_data", 15)) { 2935 do_list_local_data(ssl, worker, worker->daemon->local_zones); 2936 return; 2937 } else if(cmdcmp(p, "view_list_local_zones", 21)) { 2938 do_view_list_local_zones(ssl, worker, skipwhite(p+21)); 2939 return; 2940 } else if(cmdcmp(p, "view_list_local_data", 20)) { 2941 do_view_list_local_data(ssl, worker, skipwhite(p+20)); 2942 return; 2943 } else if(cmdcmp(p, "ratelimit_list", 14)) { 2944 do_ratelimit_list(ssl, worker, p+14); 2945 return; 2946 } else if(cmdcmp(p, "ip_ratelimit_list", 17)) { 2947 do_ip_ratelimit_list(ssl, worker, p+17); 2948 return; 2949 } else if(cmdcmp(p, "list_auth_zones", 15)) { 2950 do_list_auth_zones(ssl, worker->env.auth_zones); 2951 return; 2952 } else if(cmdcmp(p, "auth_zone_reload", 16)) { 2953 do_auth_zone_reload(ssl, worker, skipwhite(p+16)); 2954 return; 2955 } else if(cmdcmp(p, "auth_zone_transfer", 18)) { 2956 do_auth_zone_transfer(ssl, worker, skipwhite(p+18)); 2957 return; 2958 } else if(cmdcmp(p, "stub_add", 8)) { 2959 /* must always distribute this cmd */ 2960 if(rc) distribute_cmd(rc, ssl, cmd); 2961 do_stub_add(ssl, worker, skipwhite(p+8)); 2962 return; 2963 } else if(cmdcmp(p, "stub_remove", 11)) { 2964 /* must always distribute this cmd */ 2965 if(rc) distribute_cmd(rc, ssl, cmd); 2966 do_stub_remove(ssl, worker, skipwhite(p+11)); 2967 return; 2968 } else if(cmdcmp(p, "forward_add", 11)) { 2969 /* must always distribute this cmd */ 2970 if(rc) distribute_cmd(rc, ssl, cmd); 2971 do_forward_add(ssl, worker, skipwhite(p+11)); 2972 return; 2973 } else if(cmdcmp(p, "forward_remove", 14)) { 2974 /* must always distribute this cmd */ 2975 if(rc) distribute_cmd(rc, ssl, cmd); 2976 do_forward_remove(ssl, worker, skipwhite(p+14)); 2977 return; 2978 } else if(cmdcmp(p, "insecure_add", 12)) { 2979 /* must always distribute this cmd */ 2980 if(rc) distribute_cmd(rc, ssl, cmd); 2981 do_insecure_add(ssl, worker, skipwhite(p+12)); 2982 return; 2983 } else if(cmdcmp(p, "insecure_remove", 15)) { 2984 /* must always distribute this cmd */ 2985 if(rc) distribute_cmd(rc, ssl, cmd); 2986 do_insecure_remove(ssl, worker, skipwhite(p+15)); 2987 return; 2988 } else if(cmdcmp(p, "forward", 7)) { 2989 /* must always distribute this cmd */ 2990 if(rc) distribute_cmd(rc, ssl, cmd); 2991 do_forward(ssl, worker, skipwhite(p+7)); 2992 return; 2993 } else if(cmdcmp(p, "flush_stats", 11)) { 2994 /* must always distribute this cmd */ 2995 if(rc) distribute_cmd(rc, ssl, cmd); 2996 do_flush_stats(ssl, worker); 2997 return; 2998 } else if(cmdcmp(p, "flush_requestlist", 17)) { 2999 /* must always distribute this cmd */ 3000 if(rc) distribute_cmd(rc, ssl, cmd); 3001 do_flush_requestlist(ssl, worker); 3002 return; 3003 } else if(cmdcmp(p, "lookup", 6)) { 3004 do_lookup(ssl, worker, skipwhite(p+6)); 3005 return; 3006 } 3007 3008 #ifdef THREADS_DISABLED 3009 /* other processes must execute the command as well */ 3010 /* commands that should not be distributed, returned above. */ 3011 if(rc) { /* only if this thread is the master (rc) thread */ 3012 /* done before the code below, which may split the string */ 3013 distribute_cmd(rc, ssl, cmd); 3014 } 3015 #endif 3016 if(cmdcmp(p, "verbosity", 9)) { 3017 do_verbosity(ssl, skipwhite(p+9)); 3018 } else if(cmdcmp(p, "local_zone_remove", 17)) { 3019 do_zone_remove(ssl, worker->daemon->local_zones, skipwhite(p+17)); 3020 } else if(cmdcmp(p, "local_zones_remove", 18)) { 3021 do_zones_remove(ssl, worker->daemon->local_zones); 3022 } else if(cmdcmp(p, "local_zone", 10)) { 3023 do_zone_add(ssl, worker->daemon->local_zones, skipwhite(p+10)); 3024 } else if(cmdcmp(p, "local_zones", 11)) { 3025 do_zones_add(ssl, worker->daemon->local_zones); 3026 } else if(cmdcmp(p, "local_data_remove", 17)) { 3027 do_data_remove(ssl, worker->daemon->local_zones, skipwhite(p+17)); 3028 } else if(cmdcmp(p, "local_datas_remove", 18)) { 3029 do_datas_remove(ssl, worker->daemon->local_zones); 3030 } else if(cmdcmp(p, "local_data", 10)) { 3031 do_data_add(ssl, worker->daemon->local_zones, skipwhite(p+10)); 3032 } else if(cmdcmp(p, "local_datas", 11)) { 3033 do_datas_add(ssl, worker->daemon->local_zones); 3034 } else if(cmdcmp(p, "view_local_zone_remove", 22)) { 3035 do_view_zone_remove(ssl, worker, skipwhite(p+22)); 3036 } else if(cmdcmp(p, "view_local_zone", 15)) { 3037 do_view_zone_add(ssl, worker, skipwhite(p+15)); 3038 } else if(cmdcmp(p, "view_local_data_remove", 22)) { 3039 do_view_data_remove(ssl, worker, skipwhite(p+22)); 3040 } else if(cmdcmp(p, "view_local_datas_remove", 23)){ 3041 do_view_datas_remove(ssl, worker, skipwhite(p+23)); 3042 } else if(cmdcmp(p, "view_local_data", 15)) { 3043 do_view_data_add(ssl, worker, skipwhite(p+15)); 3044 } else if(cmdcmp(p, "view_local_datas", 16)) { 3045 do_view_datas_add(ssl, worker, skipwhite(p+16)); 3046 } else if(cmdcmp(p, "flush_zone", 10)) { 3047 do_flush_zone(ssl, worker, skipwhite(p+10)); 3048 } else if(cmdcmp(p, "flush_type", 10)) { 3049 do_flush_type(ssl, worker, skipwhite(p+10)); 3050 } else if(cmdcmp(p, "flush_infra", 11)) { 3051 do_flush_infra(ssl, worker, skipwhite(p+11)); 3052 } else if(cmdcmp(p, "flush", 5)) { 3053 do_flush_name(ssl, worker, skipwhite(p+5)); 3054 } else if(cmdcmp(p, "dump_requestlist", 16)) { 3055 do_dump_requestlist(ssl, worker); 3056 } else if(cmdcmp(p, "dump_infra", 10)) { 3057 do_dump_infra(ssl, worker); 3058 } else if(cmdcmp(p, "log_reopen", 10)) { 3059 do_log_reopen(ssl, worker); 3060 } else if(cmdcmp(p, "set_option", 10)) { 3061 do_set_option(ssl, worker, skipwhite(p+10)); 3062 } else if(cmdcmp(p, "get_option", 10)) { 3063 do_get_option(ssl, worker, skipwhite(p+10)); 3064 } else if(cmdcmp(p, "flush_bogus", 11)) { 3065 do_flush_bogus(ssl, worker); 3066 } else if(cmdcmp(p, "flush_negative", 14)) { 3067 do_flush_negative(ssl, worker); 3068 } else { 3069 (void)ssl_printf(ssl, "error unknown command '%s'\n", p); 3070 } 3071 } 3072 3073 void 3074 daemon_remote_exec(struct worker* worker) 3075 { 3076 /* read the cmd string */ 3077 uint8_t* msg = NULL; 3078 uint32_t len = 0; 3079 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) { 3080 log_err("daemon_remote_exec: tube_read_msg failed"); 3081 return; 3082 } 3083 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg); 3084 execute_cmd(NULL, NULL, (char*)msg, worker); 3085 free(msg); 3086 } 3087 3088 /** handle remote control request */ 3089 static void 3090 handle_req(struct daemon_remote* rc, struct rc_state* s, RES* res) 3091 { 3092 int r; 3093 char pre[10]; 3094 char magic[7]; 3095 char buf[1024]; 3096 #ifdef USE_WINSOCK 3097 /* makes it possible to set the socket blocking again. */ 3098 /* basically removes it from winsock_event ... */ 3099 WSAEventSelect(s->c->fd, NULL, 0); 3100 #endif 3101 fd_set_block(s->c->fd); 3102 3103 /* try to read magic UBCT[version]_space_ string */ 3104 if(res->ssl) { 3105 ERR_clear_error(); 3106 if((r=SSL_read(res->ssl, magic, (int)sizeof(magic)-1)) <= 0) { 3107 if(SSL_get_error(res->ssl, r) == SSL_ERROR_ZERO_RETURN) 3108 return; 3109 log_crypto_err("could not SSL_read"); 3110 return; 3111 } 3112 } else { 3113 while(1) { 3114 ssize_t rr = recv(res->fd, magic, sizeof(magic)-1, 0); 3115 if(rr <= 0) { 3116 if(rr == 0) return; 3117 if(errno == EINTR || errno == EAGAIN) 3118 continue; 3119 #ifndef USE_WINSOCK 3120 log_err("could not recv: %s", strerror(errno)); 3121 #else 3122 log_err("could not recv: %s", wsa_strerror(WSAGetLastError())); 3123 #endif 3124 return; 3125 } 3126 r = (int)rr; 3127 break; 3128 } 3129 } 3130 magic[6] = 0; 3131 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) { 3132 verbose(VERB_QUERY, "control connection has bad magic string"); 3133 /* probably wrong tool connected, ignore it completely */ 3134 return; 3135 } 3136 3137 /* read the command line */ 3138 if(!ssl_read_line(res, buf, sizeof(buf))) { 3139 return; 3140 } 3141 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION); 3142 if(strcmp(magic, pre) != 0) { 3143 verbose(VERB_QUERY, "control connection had bad " 3144 "version %s, cmd: %s", magic, buf); 3145 ssl_printf(res, "error version mismatch\n"); 3146 return; 3147 } 3148 verbose(VERB_DETAIL, "control cmd: %s", buf); 3149 3150 /* figure out what to do */ 3151 execute_cmd(rc, res, buf, rc->worker); 3152 } 3153 3154 /** handle SSL_do_handshake changes to the file descriptor to wait for later */ 3155 static int 3156 remote_handshake_later(struct daemon_remote* rc, struct rc_state* s, 3157 struct comm_point* c, int r, int r2) 3158 { 3159 if(r2 == SSL_ERROR_WANT_READ) { 3160 if(s->shake_state == rc_hs_read) { 3161 /* try again later */ 3162 return 0; 3163 } 3164 s->shake_state = rc_hs_read; 3165 comm_point_listen_for_rw(c, 1, 0); 3166 return 0; 3167 } else if(r2 == SSL_ERROR_WANT_WRITE) { 3168 if(s->shake_state == rc_hs_write) { 3169 /* try again later */ 3170 return 0; 3171 } 3172 s->shake_state = rc_hs_write; 3173 comm_point_listen_for_rw(c, 0, 1); 3174 return 0; 3175 } else { 3176 if(r == 0) 3177 log_err("remote control connection closed prematurely"); 3178 log_addr(VERB_OPS, "failed connection from", 3179 &s->c->repinfo.addr, s->c->repinfo.addrlen); 3180 log_crypto_err("remote control failed ssl"); 3181 clean_point(rc, s); 3182 } 3183 return 0; 3184 } 3185 3186 int remote_control_callback(struct comm_point* c, void* arg, int err, 3187 struct comm_reply* ATTR_UNUSED(rep)) 3188 { 3189 RES res; 3190 struct rc_state* s = (struct rc_state*)arg; 3191 struct daemon_remote* rc = s->rc; 3192 int r; 3193 if(err != NETEVENT_NOERROR) { 3194 if(err==NETEVENT_TIMEOUT) 3195 log_err("remote control timed out"); 3196 clean_point(rc, s); 3197 return 0; 3198 } 3199 if(s->ssl) { 3200 /* (continue to) setup the SSL connection */ 3201 ERR_clear_error(); 3202 r = SSL_do_handshake(s->ssl); 3203 if(r != 1) { 3204 int r2 = SSL_get_error(s->ssl, r); 3205 return remote_handshake_later(rc, s, c, r, r2); 3206 } 3207 s->shake_state = rc_none; 3208 } 3209 3210 /* once handshake has completed, check authentication */ 3211 if (!rc->use_cert) { 3212 verbose(VERB_ALGO, "unauthenticated remote control connection"); 3213 } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) { 3214 X509* x = SSL_get_peer_certificate(s->ssl); 3215 if(!x) { 3216 verbose(VERB_DETAIL, "remote control connection " 3217 "provided no client certificate"); 3218 clean_point(rc, s); 3219 return 0; 3220 } 3221 verbose(VERB_ALGO, "remote control connection authenticated"); 3222 X509_free(x); 3223 } else { 3224 verbose(VERB_DETAIL, "remote control connection failed to " 3225 "authenticate with client certificate"); 3226 clean_point(rc, s); 3227 return 0; 3228 } 3229 3230 /* if OK start to actually handle the request */ 3231 res.ssl = s->ssl; 3232 res.fd = c->fd; 3233 handle_req(rc, s, &res); 3234 3235 verbose(VERB_ALGO, "remote control operation completed"); 3236 clean_point(rc, s); 3237 return 0; 3238 } 3239