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