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