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