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