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