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