1933707f3Ssthen /* 2933707f3Ssthen * daemon/remote.c - remote control for the unbound daemon. 3933707f3Ssthen * 4933707f3Ssthen * Copyright (c) 2008, NLnet Labs. All rights reserved. 5933707f3Ssthen * 6933707f3Ssthen * This software is open source. 7933707f3Ssthen * 8933707f3Ssthen * Redistribution and use in source and binary forms, with or without 9933707f3Ssthen * modification, are permitted provided that the following conditions 10933707f3Ssthen * are met: 11933707f3Ssthen * 12933707f3Ssthen * Redistributions of source code must retain the above copyright notice, 13933707f3Ssthen * this list of conditions and the following disclaimer. 14933707f3Ssthen * 15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice, 16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation 17933707f3Ssthen * and/or other materials provided with the distribution. 18933707f3Ssthen * 19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may 20933707f3Ssthen * be used to endorse or promote products derived from this software without 21933707f3Ssthen * specific prior written permission. 22933707f3Ssthen * 23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 240b68ff31Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 250b68ff31Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 260b68ff31Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 270b68ff31Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 280b68ff31Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 290b68ff31Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 300b68ff31Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 310b68ff31Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 320b68ff31Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 330b68ff31Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34933707f3Ssthen */ 35933707f3Ssthen 36933707f3Ssthen /** 37933707f3Ssthen * \file 38933707f3Ssthen * 39933707f3Ssthen * This file contains the remote control functionality for the daemon. 40933707f3Ssthen * The remote control can be performed using either the commandline 41e10d3884Sbrad * unbound-control tool, or a TLS capable web browser. 42e10d3884Sbrad * The channel is secured using TLSv1, and certificates. 43933707f3Ssthen * Both the server and the client(control tool) have their own keys. 44933707f3Ssthen */ 45933707f3Ssthen #include "config.h" 46933707f3Ssthen #ifdef HAVE_OPENSSL_ERR_H 47933707f3Ssthen #include <openssl/err.h> 48933707f3Ssthen #endif 4977079be7Ssthen #ifdef HAVE_OPENSSL_DH_H 5031f127bbSsthen #include <openssl/dh.h> 5131f127bbSsthen #endif 5277079be7Ssthen #ifdef HAVE_OPENSSL_BN_H 5377079be7Ssthen #include <openssl/bn.h> 5477079be7Ssthen #endif 5531f127bbSsthen 56933707f3Ssthen #include <ctype.h> 57933707f3Ssthen #include "daemon/remote.h" 58933707f3Ssthen #include "daemon/worker.h" 59933707f3Ssthen #include "daemon/daemon.h" 60933707f3Ssthen #include "daemon/stats.h" 61933707f3Ssthen #include "daemon/cachedump.h" 62933707f3Ssthen #include "util/log.h" 63933707f3Ssthen #include "util/config_file.h" 64933707f3Ssthen #include "util/net_help.h" 65933707f3Ssthen #include "util/module.h" 66933707f3Ssthen #include "services/listen_dnsport.h" 67933707f3Ssthen #include "services/cache/rrset.h" 68933707f3Ssthen #include "services/cache/infra.h" 69933707f3Ssthen #include "services/mesh.h" 70933707f3Ssthen #include "services/localzone.h" 71933707f3Ssthen #include "util/storage/slabhash.h" 72933707f3Ssthen #include "util/fptr_wlist.h" 73933707f3Ssthen #include "util/data/dname.h" 74933707f3Ssthen #include "validator/validator.h" 75933707f3Ssthen #include "validator/val_kcache.h" 76933707f3Ssthen #include "validator/val_kentry.h" 77163a4143Ssthen #include "validator/val_anchor.h" 78933707f3Ssthen #include "iterator/iterator.h" 79933707f3Ssthen #include "iterator/iter_fwd.h" 80933707f3Ssthen #include "iterator/iter_hints.h" 81933707f3Ssthen #include "iterator/iter_delegpt.h" 82933707f3Ssthen #include "services/outbound_list.h" 83933707f3Ssthen #include "services/outside_network.h" 84a58bff56Ssthen #include "sldns/str2wire.h" 85a58bff56Ssthen #include "sldns/parseutil.h" 86a58bff56Ssthen #include "sldns/wire2str.h" 87a58bff56Ssthen #include "sldns/sbuffer.h" 88933707f3Ssthen 89933707f3Ssthen #ifdef HAVE_SYS_TYPES_H 90933707f3Ssthen # include <sys/types.h> 91933707f3Ssthen #endif 9231f127bbSsthen #ifdef HAVE_SYS_STAT_H 9331f127bbSsthen #include <sys/stat.h> 9431f127bbSsthen #endif 95933707f3Ssthen #ifdef HAVE_NETDB_H 96933707f3Ssthen #include <netdb.h> 97933707f3Ssthen #endif 98933707f3Ssthen 99933707f3Ssthen /* just for portability */ 100933707f3Ssthen #ifdef SQ 101933707f3Ssthen #undef SQ 102933707f3Ssthen #endif 103933707f3Ssthen 104933707f3Ssthen /** what to put on statistics lines between var and value, ": " or "=" */ 105933707f3Ssthen #define SQ "=" 106933707f3Ssthen /** if true, inhibits a lot of =0 lines from the stats output */ 107933707f3Ssthen static const int inhibit_zero = 1; 108933707f3Ssthen 109933707f3Ssthen /** subtract timers and the values do not overflow or become negative */ 110933707f3Ssthen static void 111933707f3Ssthen timeval_subtract(struct timeval* d, const struct timeval* end, 112933707f3Ssthen const struct timeval* start) 113933707f3Ssthen { 114933707f3Ssthen #ifndef S_SPLINT_S 115933707f3Ssthen time_t end_usec = end->tv_usec; 116933707f3Ssthen d->tv_sec = end->tv_sec - start->tv_sec; 117933707f3Ssthen if(end_usec < start->tv_usec) { 118933707f3Ssthen end_usec += 1000000; 119933707f3Ssthen d->tv_sec--; 120933707f3Ssthen } 121933707f3Ssthen d->tv_usec = end_usec - start->tv_usec; 122933707f3Ssthen #endif 123933707f3Ssthen } 124933707f3Ssthen 125933707f3Ssthen /** divide sum of timers to get average */ 126933707f3Ssthen static void 1272be9e038Ssthen timeval_divide(struct timeval* avg, const struct timeval* sum, long long d) 128933707f3Ssthen { 129933707f3Ssthen #ifndef S_SPLINT_S 130933707f3Ssthen size_t leftover; 131933707f3Ssthen if(d == 0) { 132933707f3Ssthen avg->tv_sec = 0; 133933707f3Ssthen avg->tv_usec = 0; 134933707f3Ssthen return; 135933707f3Ssthen } 136933707f3Ssthen avg->tv_sec = sum->tv_sec / d; 137933707f3Ssthen avg->tv_usec = sum->tv_usec / d; 138933707f3Ssthen /* handle fraction from seconds divide */ 139933707f3Ssthen leftover = sum->tv_sec - avg->tv_sec*d; 140933707f3Ssthen avg->tv_usec += (leftover*1000000)/d; 141933707f3Ssthen #endif 142933707f3Ssthen } 143933707f3Ssthen 14431f127bbSsthen /* 14531f127bbSsthen * The following function was generated using the openssl utility, using 1462ee382b6Ssthen * the command : "openssl dhparam -C 2048" 147a58bff56Ssthen * (some openssl versions reject DH that is 'too small', eg. 512). 14831f127bbSsthen */ 14977079be7Ssthen #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL) 15031f127bbSsthen #ifndef S_SPLINT_S 15177079be7Ssthen static DH *get_dh2048(void) 15231f127bbSsthen { 1532ee382b6Ssthen static unsigned char dh2048_p[]={ 1542ee382b6Ssthen 0xE7,0x36,0x28,0x3B,0xE4,0xC3,0x32,0x1C,0x01,0xC3,0x67,0xD6, 1552ee382b6Ssthen 0xF5,0xF3,0xDA,0xDC,0x71,0xC0,0x42,0x8B,0xE6,0xEB,0x8D,0x80, 1562ee382b6Ssthen 0x35,0x7F,0x09,0x45,0x30,0xE5,0xB2,0x92,0x81,0x3F,0x08,0xCD, 1572ee382b6Ssthen 0x36,0x5E,0x19,0x83,0x62,0xCC,0xAE,0x9B,0x81,0x66,0x24,0xEE, 1582ee382b6Ssthen 0x16,0x6F,0xA9,0x9E,0xF4,0x82,0x1B,0xDD,0x46,0xC7,0x33,0x5D, 1592ee382b6Ssthen 0xF4,0xCA,0xE6,0x8F,0xFC,0xD4,0xD8,0x58,0x94,0x24,0x5D,0xFF, 1602ee382b6Ssthen 0x0A,0xE8,0xEF,0x3D,0xCE,0xBB,0x50,0x94,0xE0,0x5F,0xE8,0x41, 1612ee382b6Ssthen 0xC3,0x35,0x30,0x37,0xD5,0xCB,0x8F,0x3D,0x95,0x15,0x1A,0x77, 1622ee382b6Ssthen 0x42,0xB2,0x06,0x86,0xF6,0x09,0x66,0x0E,0x9A,0x25,0x94,0x3E, 1632ee382b6Ssthen 0xD2,0x04,0x25,0x25,0x1D,0x23,0xEB,0xDC,0x4D,0x0C,0x83,0x28, 1642ee382b6Ssthen 0x2E,0x15,0x81,0x2D,0xC1,0xAF,0x8D,0x36,0x64,0xE3,0x9A,0x83, 1652ee382b6Ssthen 0x78,0xC2,0x8D,0xC0,0x9D,0xD9,0x3A,0x1C,0xC5,0x2B,0x50,0x68, 1662ee382b6Ssthen 0x07,0xA9,0x4B,0x8C,0x07,0x57,0xD6,0x15,0x03,0x4E,0x9E,0x01, 1672ee382b6Ssthen 0xF2,0x6F,0x35,0xAC,0x26,0x9C,0x92,0x68,0x61,0x13,0xFB,0x01, 1682ee382b6Ssthen 0xBA,0x22,0x36,0x01,0x55,0xB6,0x62,0xD9,0xB2,0x98,0xCE,0x5D, 1692ee382b6Ssthen 0x4B,0xA5,0x41,0xD6,0xE5,0x70,0x78,0x12,0x1F,0x64,0xB6,0x6F, 1702ee382b6Ssthen 0xB0,0x91,0x51,0x91,0x92,0xC0,0x94,0x3A,0xD1,0x28,0x4D,0x30, 1712ee382b6Ssthen 0x84,0x3E,0xE4,0xE4,0x7F,0x47,0x89,0xB1,0xB6,0x8C,0x8E,0x0E, 1722ee382b6Ssthen 0x26,0xDB,0xCD,0x17,0x07,0x2A,0x21,0x7A,0xCC,0x68,0xE8,0x57, 1732ee382b6Ssthen 0x94,0x9E,0x59,0x61,0xEC,0x20,0x34,0x26,0x0D,0x66,0x44,0xEB, 1742ee382b6Ssthen 0x6F,0x02,0x58,0xE2,0xED,0xF6,0xF3,0x1B,0xBF,0x9E,0x45,0x52, 1752ee382b6Ssthen 0x5A,0x49,0xA1,0x5B, 17631f127bbSsthen }; 1772ee382b6Ssthen static unsigned char dh2048_g[]={ 1782ee382b6Ssthen 0x02, 17931f127bbSsthen }; 18077079be7Ssthen DH *dh = NULL; 18177079be7Ssthen BIGNUM *p = NULL, *g = NULL; 18231f127bbSsthen 18377079be7Ssthen dh = DH_new(); 18477079be7Ssthen p = BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL); 18577079be7Ssthen g = BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL); 18677079be7Ssthen if (!dh || !p || !g) 18777079be7Ssthen goto err; 18877079be7Ssthen 18977079be7Ssthen #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL) 19077079be7Ssthen dh->p = p; 19177079be7Ssthen dh->g = g; 19277079be7Ssthen #else 19377079be7Ssthen if (!DH_set0_pqg(dh, p, NULL, g)) 19477079be7Ssthen goto err; 19577079be7Ssthen #endif 19677079be7Ssthen return dh; 19777079be7Ssthen err: 19877079be7Ssthen if (p) 19977079be7Ssthen BN_free(p); 20077079be7Ssthen if (g) 20177079be7Ssthen BN_free(g); 20277079be7Ssthen if (dh) 20377079be7Ssthen DH_free(dh); 20477079be7Ssthen return NULL; 20531f127bbSsthen } 20631f127bbSsthen #endif /* SPLINT */ 20777079be7Ssthen #endif /* OPENSSL_VERSION_NUMBER < 0x10100000 */ 20831f127bbSsthen 209933707f3Ssthen struct daemon_remote* 210933707f3Ssthen daemon_remote_create(struct config_file* cfg) 211933707f3Ssthen { 212933707f3Ssthen char* s_cert; 213933707f3Ssthen char* s_key; 214933707f3Ssthen struct daemon_remote* rc = (struct daemon_remote*)calloc(1, 215933707f3Ssthen sizeof(*rc)); 216933707f3Ssthen if(!rc) { 217933707f3Ssthen log_err("out of memory in daemon_remote_create"); 218933707f3Ssthen return NULL; 219933707f3Ssthen } 220933707f3Ssthen rc->max_active = 10; 221933707f3Ssthen 222933707f3Ssthen if(!cfg->remote_control_enable) { 223933707f3Ssthen rc->ctx = NULL; 224933707f3Ssthen return rc; 225933707f3Ssthen } 226933707f3Ssthen rc->ctx = SSL_CTX_new(SSLv23_server_method()); 227933707f3Ssthen if(!rc->ctx) { 228933707f3Ssthen log_crypto_err("could not SSL_CTX_new"); 229933707f3Ssthen free(rc); 230933707f3Ssthen return NULL; 231933707f3Ssthen } 232*7191de28Ssthen if(!listen_sslctx_setup(rc->ctx)) { 233933707f3Ssthen daemon_remote_delete(rc); 234933707f3Ssthen return NULL; 235933707f3Ssthen } 23631f127bbSsthen 23731f127bbSsthen if (cfg->remote_control_use_cert == 0) { 23831f127bbSsthen /* No certificates are requested */ 23977079be7Ssthen #ifdef HAVE_SSL_CTX_SET_SECURITY_LEVEL 24077079be7Ssthen SSL_CTX_set_security_level(rc->ctx, 0); 24177079be7Ssthen #endif 24277079be7Ssthen if(!SSL_CTX_set_cipher_list(rc->ctx, "aNULL, eNULL")) { 24331f127bbSsthen log_crypto_err("Failed to set aNULL cipher list"); 24477079be7Ssthen daemon_remote_delete(rc); 24531f127bbSsthen return NULL; 24631f127bbSsthen } 24731f127bbSsthen 24877079be7Ssthen /* in openssl 1.1, the securitylevel 0 allows eNULL, that 24977079be7Ssthen * does not need the DH */ 25077079be7Ssthen #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL) 25131f127bbSsthen /* Since we have no certificates and hence no source of 25231f127bbSsthen * DH params, let's generate and set them 25331f127bbSsthen */ 2542ee382b6Ssthen if(!SSL_CTX_set_tmp_dh(rc->ctx,get_dh2048())) { 25531f127bbSsthen log_crypto_err("Wanted to set DH param, but failed"); 25677079be7Ssthen daemon_remote_delete(rc); 25731f127bbSsthen return NULL; 25831f127bbSsthen } 25977079be7Ssthen #endif 26031f127bbSsthen return rc; 26131f127bbSsthen } 26231f127bbSsthen rc->use_cert = 1; 263933707f3Ssthen s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1); 264933707f3Ssthen s_key = fname_after_chroot(cfg->server_key_file, cfg, 1); 265933707f3Ssthen if(!s_cert || !s_key) { 266933707f3Ssthen log_err("out of memory in remote control fname"); 267933707f3Ssthen goto setup_error; 268933707f3Ssthen } 269933707f3Ssthen verbose(VERB_ALGO, "setup SSL certificates"); 270a961b961Ssthen if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) { 271933707f3Ssthen log_err("Error for server-cert-file: %s", s_cert); 272a961b961Ssthen log_crypto_err("Error in SSL_CTX use_certificate_chain_file"); 273933707f3Ssthen goto setup_error; 274933707f3Ssthen } 275933707f3Ssthen if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) { 276933707f3Ssthen log_err("Error for server-key-file: %s", s_key); 277933707f3Ssthen log_crypto_err("Error in SSL_CTX use_PrivateKey_file"); 278933707f3Ssthen goto setup_error; 279933707f3Ssthen } 280933707f3Ssthen if(!SSL_CTX_check_private_key(rc->ctx)) { 281933707f3Ssthen log_err("Error for server-key-file: %s", s_key); 282933707f3Ssthen log_crypto_err("Error in SSL_CTX check_private_key"); 283933707f3Ssthen goto setup_error; 284933707f3Ssthen } 285*7191de28Ssthen listen_sslctx_setup_2(rc->ctx); 286933707f3Ssthen if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) { 287933707f3Ssthen log_crypto_err("Error setting up SSL_CTX verify locations"); 288933707f3Ssthen setup_error: 289933707f3Ssthen free(s_cert); 290933707f3Ssthen free(s_key); 291933707f3Ssthen daemon_remote_delete(rc); 292933707f3Ssthen return NULL; 293933707f3Ssthen } 294933707f3Ssthen SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert)); 295933707f3Ssthen SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL); 296933707f3Ssthen free(s_cert); 297933707f3Ssthen free(s_key); 298933707f3Ssthen 299933707f3Ssthen return rc; 300933707f3Ssthen } 301933707f3Ssthen 302933707f3Ssthen void daemon_remote_clear(struct daemon_remote* rc) 303933707f3Ssthen { 304933707f3Ssthen struct rc_state* p, *np; 305933707f3Ssthen if(!rc) return; 306933707f3Ssthen /* but do not close the ports */ 307933707f3Ssthen listen_list_delete(rc->accept_list); 308933707f3Ssthen rc->accept_list = NULL; 309933707f3Ssthen /* do close these sockets */ 310933707f3Ssthen p = rc->busy_list; 311933707f3Ssthen while(p) { 312933707f3Ssthen np = p->next; 313933707f3Ssthen if(p->ssl) 314933707f3Ssthen SSL_free(p->ssl); 315933707f3Ssthen comm_point_delete(p->c); 316933707f3Ssthen free(p); 317933707f3Ssthen p = np; 318933707f3Ssthen } 319933707f3Ssthen rc->busy_list = NULL; 320933707f3Ssthen rc->active = 0; 321933707f3Ssthen rc->worker = NULL; 322933707f3Ssthen } 323933707f3Ssthen 324933707f3Ssthen void daemon_remote_delete(struct daemon_remote* rc) 325933707f3Ssthen { 326933707f3Ssthen if(!rc) return; 327933707f3Ssthen daemon_remote_clear(rc); 328933707f3Ssthen if(rc->ctx) { 329933707f3Ssthen SSL_CTX_free(rc->ctx); 330933707f3Ssthen } 331933707f3Ssthen free(rc); 332933707f3Ssthen } 333933707f3Ssthen 334933707f3Ssthen /** 335933707f3Ssthen * Add and open a new control port 336933707f3Ssthen * @param ip: ip str 337933707f3Ssthen * @param nr: port nr 338933707f3Ssthen * @param list: list head 339933707f3Ssthen * @param noproto_is_err: if lack of protocol support is an error. 34031f127bbSsthen * @param cfg: config with username for chown of unix-sockets. 341933707f3Ssthen * @return false on failure. 342933707f3Ssthen */ 343933707f3Ssthen static int 34431f127bbSsthen add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err, 34531f127bbSsthen struct config_file* cfg) 346933707f3Ssthen { 347933707f3Ssthen struct addrinfo hints; 348933707f3Ssthen struct addrinfo* res; 349933707f3Ssthen struct listen_port* n; 350933707f3Ssthen int noproto; 351933707f3Ssthen int fd, r; 352933707f3Ssthen char port[15]; 353933707f3Ssthen snprintf(port, sizeof(port), "%d", nr); 354933707f3Ssthen port[sizeof(port)-1]=0; 355933707f3Ssthen memset(&hints, 0, sizeof(hints)); 35631f127bbSsthen 35731f127bbSsthen if(ip[0] == '/') { 35831f127bbSsthen /* This looks like a local socket */ 35977079be7Ssthen fd = create_local_accept_sock(ip, &noproto, cfg->use_systemd); 36031f127bbSsthen /* 36131f127bbSsthen * Change socket ownership and permissions so users other 36231f127bbSsthen * than root can access it provided they are in the same 36331f127bbSsthen * group as the user we run as. 36431f127bbSsthen */ 36531f127bbSsthen if(fd != -1) { 36631f127bbSsthen #ifdef HAVE_CHOWN 367a58bff56Ssthen if (cfg->username && cfg->username[0] && 36877079be7Ssthen cfg_uid != (uid_t)-1) { 36977079be7Ssthen if(chown(ip, cfg_uid, cfg_gid) == -1) 370*7191de28Ssthen verbose(VERB_QUERY, "cannot chown %u.%u %s: %s", 37177079be7Ssthen (unsigned)cfg_uid, (unsigned)cfg_gid, 37277079be7Ssthen ip, strerror(errno)); 37377079be7Ssthen } 37431f127bbSsthen chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)); 37531f127bbSsthen #else 37631f127bbSsthen (void)cfg; 37731f127bbSsthen #endif 37831f127bbSsthen } 37931f127bbSsthen } else { 380933707f3Ssthen hints.ai_socktype = SOCK_STREAM; 381933707f3Ssthen hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 382933707f3Ssthen if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) { 383933707f3Ssthen #ifdef USE_WINSOCK 384933707f3Ssthen if(!noproto_is_err && r == EAI_NONAME) { 385933707f3Ssthen /* tried to lookup the address as name */ 386933707f3Ssthen return 1; /* return success, but do nothing */ 387933707f3Ssthen } 388933707f3Ssthen #endif /* USE_WINSOCK */ 389933707f3Ssthen log_err("control interface %s:%s getaddrinfo: %s %s", 390933707f3Ssthen ip?ip:"default", port, gai_strerror(r), 391933707f3Ssthen #ifdef EAI_SYSTEM 392933707f3Ssthen r==EAI_SYSTEM?(char*)strerror(errno):"" 393933707f3Ssthen #else 394933707f3Ssthen "" 395933707f3Ssthen #endif 396933707f3Ssthen ); 397933707f3Ssthen return 0; 398933707f3Ssthen } 399933707f3Ssthen 400933707f3Ssthen /* open fd */ 401a58bff56Ssthen fd = create_tcp_accept_sock(res, 1, &noproto, 0, 40277079be7Ssthen cfg->ip_transparent, 0, cfg->ip_freebind, cfg->use_systemd); 403933707f3Ssthen freeaddrinfo(res); 40431f127bbSsthen } 40531f127bbSsthen 406933707f3Ssthen if(fd == -1 && noproto) { 407933707f3Ssthen if(!noproto_is_err) 408933707f3Ssthen return 1; /* return success, but do nothing */ 409933707f3Ssthen log_err("cannot open control interface %s %d : " 410933707f3Ssthen "protocol not supported", ip, nr); 411933707f3Ssthen return 0; 412933707f3Ssthen } 413933707f3Ssthen if(fd == -1) { 414933707f3Ssthen log_err("cannot open control interface %s %d", ip, nr); 415933707f3Ssthen return 0; 416933707f3Ssthen } 417933707f3Ssthen 418933707f3Ssthen /* alloc */ 419933707f3Ssthen n = (struct listen_port*)calloc(1, sizeof(*n)); 420933707f3Ssthen if(!n) { 421933707f3Ssthen #ifndef USE_WINSOCK 422933707f3Ssthen close(fd); 423933707f3Ssthen #else 424933707f3Ssthen closesocket(fd); 425933707f3Ssthen #endif 426933707f3Ssthen log_err("out of memory"); 427933707f3Ssthen return 0; 428933707f3Ssthen } 429933707f3Ssthen n->next = *list; 430933707f3Ssthen *list = n; 431933707f3Ssthen n->fd = fd; 432933707f3Ssthen return 1; 433933707f3Ssthen } 434933707f3Ssthen 435933707f3Ssthen struct listen_port* daemon_remote_open_ports(struct config_file* cfg) 436933707f3Ssthen { 437933707f3Ssthen struct listen_port* l = NULL; 438933707f3Ssthen log_assert(cfg->remote_control_enable && cfg->control_port); 439933707f3Ssthen if(cfg->control_ifs) { 440933707f3Ssthen struct config_strlist* p; 441933707f3Ssthen for(p = cfg->control_ifs; p; p = p->next) { 44231f127bbSsthen if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) { 443933707f3Ssthen listening_ports_free(l); 444933707f3Ssthen return NULL; 445933707f3Ssthen } 446933707f3Ssthen } 447933707f3Ssthen } else { 448933707f3Ssthen /* defaults */ 449933707f3Ssthen if(cfg->do_ip6 && 45031f127bbSsthen !add_open("::1", cfg->control_port, &l, 0, cfg)) { 451933707f3Ssthen listening_ports_free(l); 452933707f3Ssthen return NULL; 453933707f3Ssthen } 454933707f3Ssthen if(cfg->do_ip4 && 45531f127bbSsthen !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) { 456933707f3Ssthen listening_ports_free(l); 457933707f3Ssthen return NULL; 458933707f3Ssthen } 459933707f3Ssthen } 460933707f3Ssthen return l; 461933707f3Ssthen } 462933707f3Ssthen 463933707f3Ssthen /** open accept commpoint */ 464933707f3Ssthen static int 465933707f3Ssthen accept_open(struct daemon_remote* rc, int fd) 466933707f3Ssthen { 467933707f3Ssthen struct listen_list* n = (struct listen_list*)malloc(sizeof(*n)); 468933707f3Ssthen if(!n) { 469933707f3Ssthen log_err("out of memory"); 470933707f3Ssthen return 0; 471933707f3Ssthen } 472933707f3Ssthen n->next = rc->accept_list; 473933707f3Ssthen rc->accept_list = n; 474933707f3Ssthen /* open commpt */ 475933707f3Ssthen n->com = comm_point_create_raw(rc->worker->base, fd, 0, 476933707f3Ssthen &remote_accept_callback, rc); 477933707f3Ssthen if(!n->com) 478933707f3Ssthen return 0; 479933707f3Ssthen /* keep this port open, its fd is kept in the rc portlist */ 480933707f3Ssthen n->com->do_not_close = 1; 481933707f3Ssthen return 1; 482933707f3Ssthen } 483933707f3Ssthen 484933707f3Ssthen int daemon_remote_open_accept(struct daemon_remote* rc, 485933707f3Ssthen struct listen_port* ports, struct worker* worker) 486933707f3Ssthen { 487933707f3Ssthen struct listen_port* p; 488933707f3Ssthen rc->worker = worker; 489933707f3Ssthen for(p = ports; p; p = p->next) { 490933707f3Ssthen if(!accept_open(rc, p->fd)) { 491933707f3Ssthen log_err("could not create accept comm point"); 492933707f3Ssthen return 0; 493933707f3Ssthen } 494933707f3Ssthen } 495933707f3Ssthen return 1; 496933707f3Ssthen } 497933707f3Ssthen 498af4988b1Ssthen void daemon_remote_stop_accept(struct daemon_remote* rc) 499af4988b1Ssthen { 500af4988b1Ssthen struct listen_list* p; 501af4988b1Ssthen for(p=rc->accept_list; p; p=p->next) { 502af4988b1Ssthen comm_point_stop_listening(p->com); 503af4988b1Ssthen } 504af4988b1Ssthen } 505af4988b1Ssthen 506af4988b1Ssthen void daemon_remote_start_accept(struct daemon_remote* rc) 507af4988b1Ssthen { 508af4988b1Ssthen struct listen_list* p; 509af4988b1Ssthen for(p=rc->accept_list; p; p=p->next) { 510af4988b1Ssthen comm_point_start_listening(p->com, -1, -1); 511af4988b1Ssthen } 512af4988b1Ssthen } 513af4988b1Ssthen 514933707f3Ssthen int remote_accept_callback(struct comm_point* c, void* arg, int err, 515933707f3Ssthen struct comm_reply* ATTR_UNUSED(rep)) 516933707f3Ssthen { 517933707f3Ssthen struct daemon_remote* rc = (struct daemon_remote*)arg; 518933707f3Ssthen struct sockaddr_storage addr; 519933707f3Ssthen socklen_t addrlen; 520933707f3Ssthen int newfd; 521933707f3Ssthen struct rc_state* n; 522933707f3Ssthen if(err != NETEVENT_NOERROR) { 523933707f3Ssthen log_err("error %d on remote_accept_callback", err); 524933707f3Ssthen return 0; 525933707f3Ssthen } 526933707f3Ssthen /* perform the accept */ 527933707f3Ssthen newfd = comm_point_perform_accept(c, &addr, &addrlen); 528933707f3Ssthen if(newfd == -1) 529933707f3Ssthen return 0; 530933707f3Ssthen /* create new commpoint unless we are servicing already */ 531933707f3Ssthen if(rc->active >= rc->max_active) { 532933707f3Ssthen log_warn("drop incoming remote control: too many connections"); 533933707f3Ssthen close_exit: 534933707f3Ssthen #ifndef USE_WINSOCK 535933707f3Ssthen close(newfd); 536933707f3Ssthen #else 537933707f3Ssthen closesocket(newfd); 538933707f3Ssthen #endif 539933707f3Ssthen return 0; 540933707f3Ssthen } 541933707f3Ssthen 542933707f3Ssthen /* setup commpoint to service the remote control command */ 543933707f3Ssthen n = (struct rc_state*)calloc(1, sizeof(*n)); 544933707f3Ssthen if(!n) { 545933707f3Ssthen log_err("out of memory"); 546933707f3Ssthen goto close_exit; 547933707f3Ssthen } 548933707f3Ssthen /* start in reading state */ 549933707f3Ssthen n->c = comm_point_create_raw(rc->worker->base, newfd, 0, 550933707f3Ssthen &remote_control_callback, n); 551933707f3Ssthen if(!n->c) { 552933707f3Ssthen log_err("out of memory"); 553933707f3Ssthen free(n); 554933707f3Ssthen goto close_exit; 555933707f3Ssthen } 556933707f3Ssthen log_addr(VERB_QUERY, "new control connection from", &addr, addrlen); 557933707f3Ssthen n->c->do_not_close = 0; 558933707f3Ssthen comm_point_stop_listening(n->c); 559933707f3Ssthen comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT); 560933707f3Ssthen memcpy(&n->c->repinfo.addr, &addr, addrlen); 561933707f3Ssthen n->c->repinfo.addrlen = addrlen; 562933707f3Ssthen n->shake_state = rc_hs_read; 563933707f3Ssthen n->ssl = SSL_new(rc->ctx); 564933707f3Ssthen if(!n->ssl) { 565933707f3Ssthen log_crypto_err("could not SSL_new"); 566933707f3Ssthen comm_point_delete(n->c); 567933707f3Ssthen free(n); 568933707f3Ssthen goto close_exit; 569933707f3Ssthen } 570933707f3Ssthen SSL_set_accept_state(n->ssl); 571933707f3Ssthen (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY); 572933707f3Ssthen if(!SSL_set_fd(n->ssl, newfd)) { 573933707f3Ssthen log_crypto_err("could not SSL_set_fd"); 574933707f3Ssthen SSL_free(n->ssl); 575933707f3Ssthen comm_point_delete(n->c); 576933707f3Ssthen free(n); 577933707f3Ssthen goto close_exit; 578933707f3Ssthen } 579933707f3Ssthen 580933707f3Ssthen n->rc = rc; 581933707f3Ssthen n->next = rc->busy_list; 582933707f3Ssthen rc->busy_list = n; 583933707f3Ssthen rc->active ++; 584933707f3Ssthen 585933707f3Ssthen /* perform the first nonblocking read already, for windows, 586933707f3Ssthen * so it can return wouldblock. could be faster too. */ 587933707f3Ssthen (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL); 588933707f3Ssthen return 0; 589933707f3Ssthen } 590933707f3Ssthen 591933707f3Ssthen /** delete from list */ 592933707f3Ssthen static void 593933707f3Ssthen state_list_remove_elem(struct rc_state** list, struct comm_point* c) 594933707f3Ssthen { 595933707f3Ssthen while(*list) { 596933707f3Ssthen if( (*list)->c == c) { 597933707f3Ssthen *list = (*list)->next; 598933707f3Ssthen return; 599933707f3Ssthen } 600933707f3Ssthen list = &(*list)->next; 601933707f3Ssthen } 602933707f3Ssthen } 603933707f3Ssthen 604933707f3Ssthen /** decrease active count and remove commpoint from busy list */ 605933707f3Ssthen static void 606933707f3Ssthen clean_point(struct daemon_remote* rc, struct rc_state* s) 607933707f3Ssthen { 608933707f3Ssthen state_list_remove_elem(&rc->busy_list, s->c); 609933707f3Ssthen rc->active --; 610933707f3Ssthen if(s->ssl) { 611933707f3Ssthen SSL_shutdown(s->ssl); 612933707f3Ssthen SSL_free(s->ssl); 613933707f3Ssthen } 614933707f3Ssthen comm_point_delete(s->c); 615933707f3Ssthen free(s); 616933707f3Ssthen } 617933707f3Ssthen 618933707f3Ssthen int 619933707f3Ssthen ssl_print_text(SSL* ssl, const char* text) 620933707f3Ssthen { 621933707f3Ssthen int r; 622933707f3Ssthen if(!ssl) 623933707f3Ssthen return 0; 624933707f3Ssthen ERR_clear_error(); 625933707f3Ssthen if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) { 626933707f3Ssthen if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { 627933707f3Ssthen verbose(VERB_QUERY, "warning, in SSL_write, peer " 628933707f3Ssthen "closed connection"); 629933707f3Ssthen return 0; 630933707f3Ssthen } 631933707f3Ssthen log_crypto_err("could not SSL_write"); 632933707f3Ssthen return 0; 633933707f3Ssthen } 634933707f3Ssthen return 1; 635933707f3Ssthen } 636933707f3Ssthen 637933707f3Ssthen /** print text over the ssl connection */ 638933707f3Ssthen static int 639933707f3Ssthen ssl_print_vmsg(SSL* ssl, const char* format, va_list args) 640933707f3Ssthen { 641933707f3Ssthen char msg[1024]; 642933707f3Ssthen vsnprintf(msg, sizeof(msg), format, args); 643933707f3Ssthen return ssl_print_text(ssl, msg); 644933707f3Ssthen } 645933707f3Ssthen 646933707f3Ssthen /** printf style printing to the ssl connection */ 647933707f3Ssthen int ssl_printf(SSL* ssl, const char* format, ...) 648933707f3Ssthen { 649933707f3Ssthen va_list args; 650933707f3Ssthen int ret; 651933707f3Ssthen va_start(args, format); 652933707f3Ssthen ret = ssl_print_vmsg(ssl, format, args); 653933707f3Ssthen va_end(args); 654933707f3Ssthen return ret; 655933707f3Ssthen } 656933707f3Ssthen 657933707f3Ssthen int 658933707f3Ssthen ssl_read_line(SSL* ssl, char* buf, size_t max) 659933707f3Ssthen { 660933707f3Ssthen int r; 661933707f3Ssthen size_t len = 0; 662933707f3Ssthen if(!ssl) 663933707f3Ssthen return 0; 664933707f3Ssthen while(len < max) { 665933707f3Ssthen ERR_clear_error(); 666933707f3Ssthen if((r=SSL_read(ssl, buf+len, 1)) <= 0) { 667933707f3Ssthen if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { 668933707f3Ssthen buf[len] = 0; 669933707f3Ssthen return 1; 670933707f3Ssthen } 671933707f3Ssthen log_crypto_err("could not SSL_read"); 672933707f3Ssthen return 0; 673933707f3Ssthen } 674933707f3Ssthen if(buf[len] == '\n') { 675933707f3Ssthen /* return string without \n */ 676933707f3Ssthen buf[len] = 0; 677933707f3Ssthen return 1; 678933707f3Ssthen } 679933707f3Ssthen len++; 680933707f3Ssthen } 681933707f3Ssthen buf[max-1] = 0; 682933707f3Ssthen log_err("control line too long (%d): %s", (int)max, buf); 683933707f3Ssthen return 0; 684933707f3Ssthen } 685933707f3Ssthen 686933707f3Ssthen /** skip whitespace, return new pointer into string */ 687933707f3Ssthen static char* 688933707f3Ssthen skipwhite(char* str) 689933707f3Ssthen { 690933707f3Ssthen /* EOS \0 is not a space */ 691e10d3884Sbrad while( isspace((unsigned char)*str) ) 692933707f3Ssthen str++; 693933707f3Ssthen return str; 694933707f3Ssthen } 695933707f3Ssthen 696933707f3Ssthen /** send the OK to the control client */ 697933707f3Ssthen static void send_ok(SSL* ssl) 698933707f3Ssthen { 699933707f3Ssthen (void)ssl_printf(ssl, "ok\n"); 700933707f3Ssthen } 701933707f3Ssthen 702933707f3Ssthen /** do the stop command */ 703933707f3Ssthen static void 704933707f3Ssthen do_stop(SSL* ssl, struct daemon_remote* rc) 705933707f3Ssthen { 706933707f3Ssthen rc->worker->need_to_exit = 1; 707933707f3Ssthen comm_base_exit(rc->worker->base); 708933707f3Ssthen send_ok(ssl); 709933707f3Ssthen } 710933707f3Ssthen 711933707f3Ssthen /** do the reload command */ 712933707f3Ssthen static void 713933707f3Ssthen do_reload(SSL* ssl, struct daemon_remote* rc) 714933707f3Ssthen { 715933707f3Ssthen rc->worker->need_to_exit = 0; 716933707f3Ssthen comm_base_exit(rc->worker->base); 717933707f3Ssthen send_ok(ssl); 718933707f3Ssthen } 719933707f3Ssthen 720933707f3Ssthen /** do the verbosity command */ 721933707f3Ssthen static void 722933707f3Ssthen do_verbosity(SSL* ssl, char* str) 723933707f3Ssthen { 724933707f3Ssthen int val = atoi(str); 725933707f3Ssthen if(val == 0 && strcmp(str, "0") != 0) { 726933707f3Ssthen ssl_printf(ssl, "error in verbosity number syntax: %s\n", str); 727933707f3Ssthen return; 728933707f3Ssthen } 729933707f3Ssthen verbosity = val; 730933707f3Ssthen send_ok(ssl); 731933707f3Ssthen } 732933707f3Ssthen 733933707f3Ssthen /** print stats from statinfo */ 734933707f3Ssthen static int 7352be9e038Ssthen print_stats(SSL* ssl, const char* nm, struct ub_stats_info* s) 736933707f3Ssthen { 7372be9e038Ssthen struct timeval sumwait, avg; 738e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm, 739e10d3884Sbrad (unsigned long)s->svr.num_queries)) return 0; 74077079be7Ssthen if(!ssl_printf(ssl, "%s.num.queries_ip_ratelimited"SQ"%lu\n", nm, 74177079be7Ssthen (unsigned long)s->svr.num_queries_ip_ratelimited)) return 0; 742e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm, 743e10d3884Sbrad (unsigned long)(s->svr.num_queries 744933707f3Ssthen - s->svr.num_queries_missed_cache))) return 0; 745e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm, 746e10d3884Sbrad (unsigned long)s->svr.num_queries_missed_cache)) return 0; 747e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm, 748e10d3884Sbrad (unsigned long)s->svr.num_queries_prefetch)) return 0; 74977079be7Ssthen if(!ssl_printf(ssl, "%s.num.zero_ttl"SQ"%lu\n", nm, 75077079be7Ssthen (unsigned long)s->svr.zero_ttl_responses)) return 0; 751e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm, 752e10d3884Sbrad (unsigned long)s->mesh_replies_sent)) return 0; 7532be9e038Ssthen #ifdef USE_DNSCRYPT 7542be9e038Ssthen if(!ssl_printf(ssl, "%s.num.dnscrypt.crypted"SQ"%lu\n", nm, 7552be9e038Ssthen (unsigned long)s->svr.num_query_dnscrypt_crypted)) return 0; 7562be9e038Ssthen if(!ssl_printf(ssl, "%s.num.dnscrypt.cert"SQ"%lu\n", nm, 7572be9e038Ssthen (unsigned long)s->svr.num_query_dnscrypt_cert)) return 0; 7582be9e038Ssthen if(!ssl_printf(ssl, "%s.num.dnscrypt.cleartext"SQ"%lu\n", nm, 7592be9e038Ssthen (unsigned long)s->svr.num_query_dnscrypt_cleartext)) return 0; 7602be9e038Ssthen if(!ssl_printf(ssl, "%s.num.dnscrypt.malformed"SQ"%lu\n", nm, 7612be9e038Ssthen (unsigned long)s->svr.num_query_dnscrypt_crypted_malformed)) return 0; 7622be9e038Ssthen #endif 763933707f3Ssthen if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm, 764933707f3Ssthen (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)? 765933707f3Ssthen (double)s->svr.sum_query_list_size/ 7662be9e038Ssthen (double)(s->svr.num_queries_missed_cache+ 767933707f3Ssthen s->svr.num_queries_prefetch) : 0.0)) return 0; 768e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm, 769e10d3884Sbrad (unsigned long)s->svr.max_query_list_size)) return 0; 770e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm, 771e10d3884Sbrad (unsigned long)s->mesh_jostled)) return 0; 772e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm, 773e10d3884Sbrad (unsigned long)s->mesh_dropped)) return 0; 774e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm, 775e10d3884Sbrad (unsigned long)s->mesh_num_states)) return 0; 776e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm, 777e10d3884Sbrad (unsigned long)s->mesh_num_reply_states)) return 0; 7782be9e038Ssthen #ifndef S_SPLINT_S 7792be9e038Ssthen sumwait.tv_sec = s->mesh_replies_sum_wait_sec; 7802be9e038Ssthen sumwait.tv_usec = s->mesh_replies_sum_wait_usec; 7812be9e038Ssthen #endif 7822be9e038Ssthen timeval_divide(&avg, &sumwait, s->mesh_replies_sent); 7830b68ff31Ssthen if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm, 784e9c7b4efSsthen (long long)avg.tv_sec, (int)avg.tv_usec)) return 0; 785933707f3Ssthen if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm, 786933707f3Ssthen s->mesh_time_median)) return 0; 787a58bff56Ssthen if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm, 788a58bff56Ssthen (unsigned long)s->svr.tcp_accept_usage)) return 0; 789933707f3Ssthen return 1; 790933707f3Ssthen } 791933707f3Ssthen 792933707f3Ssthen /** print stats for one thread */ 793933707f3Ssthen static int 7942be9e038Ssthen print_thread_stats(SSL* ssl, int i, struct ub_stats_info* s) 795933707f3Ssthen { 796*7191de28Ssthen char nm[32]; 797933707f3Ssthen snprintf(nm, sizeof(nm), "thread%d", i); 798933707f3Ssthen nm[sizeof(nm)-1]=0; 799933707f3Ssthen return print_stats(ssl, nm, s); 800933707f3Ssthen } 801933707f3Ssthen 802933707f3Ssthen /** print long number */ 803933707f3Ssthen static int 804e10d3884Sbrad print_longnum(SSL* ssl, const char* desc, size_t x) 805933707f3Ssthen { 806933707f3Ssthen if(x > 1024*1024*1024) { 807933707f3Ssthen /* more than a Gb */ 808933707f3Ssthen size_t front = x / (size_t)1000000; 809933707f3Ssthen size_t back = x % (size_t)1000000; 810933707f3Ssthen return ssl_printf(ssl, "%s%u%6.6u\n", desc, 811933707f3Ssthen (unsigned)front, (unsigned)back); 812933707f3Ssthen } else { 813e10d3884Sbrad return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x); 814933707f3Ssthen } 815933707f3Ssthen } 816933707f3Ssthen 817933707f3Ssthen /** print mem stats */ 818933707f3Ssthen static int 819933707f3Ssthen print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon) 820933707f3Ssthen { 8212be9e038Ssthen size_t msg, rrset, val, iter, respip; 8222be9e038Ssthen #ifdef CLIENT_SUBNET 8232be9e038Ssthen size_t subnet = 0; 8242be9e038Ssthen #endif /* CLIENT_SUBNET */ 8252be9e038Ssthen #ifdef USE_IPSECMOD 8262be9e038Ssthen size_t ipsecmod = 0; 8272be9e038Ssthen #endif /* USE_IPSECMOD */ 828*7191de28Ssthen #ifdef USE_DNSCRYPT 829*7191de28Ssthen size_t dnscrypt_shared_secret = 0; 830*7191de28Ssthen #endif /* USE_DNSCRYPT */ 831933707f3Ssthen msg = slabhash_get_mem(daemon->env->msg_cache); 832933707f3Ssthen rrset = slabhash_get_mem(&daemon->env->rrset_cache->table); 8332be9e038Ssthen val = mod_get_mem(&worker->env, "validator"); 8342be9e038Ssthen iter = mod_get_mem(&worker->env, "iterator"); 8352be9e038Ssthen respip = mod_get_mem(&worker->env, "respip"); 8362be9e038Ssthen #ifdef CLIENT_SUBNET 8372be9e038Ssthen subnet = mod_get_mem(&worker->env, "subnet"); 8382be9e038Ssthen #endif /* CLIENT_SUBNET */ 8392be9e038Ssthen #ifdef USE_IPSECMOD 8402be9e038Ssthen ipsecmod = mod_get_mem(&worker->env, "ipsecmod"); 8412be9e038Ssthen #endif /* USE_IPSECMOD */ 842*7191de28Ssthen #ifdef USE_DNSCRYPT 843*7191de28Ssthen if(daemon->dnscenv) { 844*7191de28Ssthen dnscrypt_shared_secret = slabhash_get_mem( 845*7191de28Ssthen daemon->dnscenv->shared_secrets_cache); 846*7191de28Ssthen } 847*7191de28Ssthen #endif /* USE_DNSCRYPT */ 848933707f3Ssthen 849933707f3Ssthen if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset)) 850933707f3Ssthen return 0; 851933707f3Ssthen if(!print_longnum(ssl, "mem.cache.message"SQ, msg)) 852933707f3Ssthen return 0; 853933707f3Ssthen if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter)) 854933707f3Ssthen return 0; 855933707f3Ssthen if(!print_longnum(ssl, "mem.mod.validator"SQ, val)) 856933707f3Ssthen return 0; 8572be9e038Ssthen if(!print_longnum(ssl, "mem.mod.respip"SQ, respip)) 8582be9e038Ssthen return 0; 8592be9e038Ssthen #ifdef CLIENT_SUBNET 8602be9e038Ssthen if(!print_longnum(ssl, "mem.mod.subnet"SQ, subnet)) 8612be9e038Ssthen return 0; 8622be9e038Ssthen #endif /* CLIENT_SUBNET */ 8632be9e038Ssthen #ifdef USE_IPSECMOD 8642be9e038Ssthen if(!print_longnum(ssl, "mem.mod.ipsecmod"SQ, ipsecmod)) 8652be9e038Ssthen return 0; 8662be9e038Ssthen #endif /* USE_IPSECMOD */ 867*7191de28Ssthen #ifdef USE_DNSCRYPT 868*7191de28Ssthen if(!print_longnum(ssl, "mem.cache.dnscrypt_shared_secret"SQ, 869*7191de28Ssthen dnscrypt_shared_secret)) 870*7191de28Ssthen return 0; 871*7191de28Ssthen #endif /* USE_DNSCRYPT */ 872933707f3Ssthen return 1; 873933707f3Ssthen } 874933707f3Ssthen 875933707f3Ssthen /** print uptime stats */ 876933707f3Ssthen static int 877933707f3Ssthen print_uptime(SSL* ssl, struct worker* worker, int reset) 878933707f3Ssthen { 879933707f3Ssthen struct timeval now = *worker->env.now_tv; 880933707f3Ssthen struct timeval up, dt; 881933707f3Ssthen timeval_subtract(&up, &now, &worker->daemon->time_boot); 882933707f3Ssthen timeval_subtract(&dt, &now, &worker->daemon->time_last_stat); 883933707f3Ssthen if(reset) 884933707f3Ssthen worker->daemon->time_last_stat = now; 8850b68ff31Ssthen if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n", 886e9c7b4efSsthen (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0; 8870b68ff31Ssthen if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n", 888e9c7b4efSsthen (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0; 8890b68ff31Ssthen if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n", 890e9c7b4efSsthen (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0; 891933707f3Ssthen return 1; 892933707f3Ssthen } 893933707f3Ssthen 894933707f3Ssthen /** print extended histogram */ 895933707f3Ssthen static int 8962be9e038Ssthen print_hist(SSL* ssl, struct ub_stats_info* s) 897933707f3Ssthen { 898933707f3Ssthen struct timehist* hist; 899933707f3Ssthen size_t i; 900933707f3Ssthen hist = timehist_setup(); 901933707f3Ssthen if(!hist) { 902933707f3Ssthen log_err("out of memory"); 903933707f3Ssthen return 0; 904933707f3Ssthen } 905933707f3Ssthen timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST); 906933707f3Ssthen for(i=0; i<hist->num; i++) { 907933707f3Ssthen if(!ssl_printf(ssl, 908e10d3884Sbrad "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n", 909933707f3Ssthen (int)hist->buckets[i].lower.tv_sec, 910933707f3Ssthen (int)hist->buckets[i].lower.tv_usec, 911933707f3Ssthen (int)hist->buckets[i].upper.tv_sec, 912933707f3Ssthen (int)hist->buckets[i].upper.tv_usec, 913e10d3884Sbrad (unsigned long)hist->buckets[i].count)) { 914933707f3Ssthen timehist_delete(hist); 915933707f3Ssthen return 0; 916933707f3Ssthen } 917933707f3Ssthen } 918933707f3Ssthen timehist_delete(hist); 919933707f3Ssthen return 1; 920933707f3Ssthen } 921933707f3Ssthen 922933707f3Ssthen /** print extended stats */ 923933707f3Ssthen static int 9242be9e038Ssthen print_ext(SSL* ssl, struct ub_stats_info* s) 925933707f3Ssthen { 926933707f3Ssthen int i; 927933707f3Ssthen char nm[16]; 9280b68ff31Ssthen const sldns_rr_descriptor* desc; 9290b68ff31Ssthen const sldns_lookup_table* lt; 930933707f3Ssthen /* TYPE */ 9312be9e038Ssthen for(i=0; i<UB_STATS_QTYPE_NUM; i++) { 932933707f3Ssthen if(inhibit_zero && s->svr.qtype[i] == 0) 933933707f3Ssthen continue; 9340b68ff31Ssthen desc = sldns_rr_descript((uint16_t)i); 935933707f3Ssthen if(desc && desc->_name) { 936933707f3Ssthen snprintf(nm, sizeof(nm), "%s", desc->_name); 937933707f3Ssthen } else if (i == LDNS_RR_TYPE_IXFR) { 938933707f3Ssthen snprintf(nm, sizeof(nm), "IXFR"); 939933707f3Ssthen } else if (i == LDNS_RR_TYPE_AXFR) { 940933707f3Ssthen snprintf(nm, sizeof(nm), "AXFR"); 941933707f3Ssthen } else if (i == LDNS_RR_TYPE_MAILA) { 942933707f3Ssthen snprintf(nm, sizeof(nm), "MAILA"); 943933707f3Ssthen } else if (i == LDNS_RR_TYPE_MAILB) { 944933707f3Ssthen snprintf(nm, sizeof(nm), "MAILB"); 945933707f3Ssthen } else if (i == LDNS_RR_TYPE_ANY) { 946933707f3Ssthen snprintf(nm, sizeof(nm), "ANY"); 947933707f3Ssthen } else { 948933707f3Ssthen snprintf(nm, sizeof(nm), "TYPE%d", i); 949933707f3Ssthen } 950e10d3884Sbrad if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n", 951e10d3884Sbrad nm, (unsigned long)s->svr.qtype[i])) return 0; 952933707f3Ssthen } 953933707f3Ssthen if(!inhibit_zero || s->svr.qtype_big) { 954e10d3884Sbrad if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n", 955e10d3884Sbrad (unsigned long)s->svr.qtype_big)) return 0; 956933707f3Ssthen } 957933707f3Ssthen /* CLASS */ 9582be9e038Ssthen for(i=0; i<UB_STATS_QCLASS_NUM; i++) { 959933707f3Ssthen if(inhibit_zero && s->svr.qclass[i] == 0) 960933707f3Ssthen continue; 9610b68ff31Ssthen lt = sldns_lookup_by_id(sldns_rr_classes, i); 962933707f3Ssthen if(lt && lt->name) { 963933707f3Ssthen snprintf(nm, sizeof(nm), "%s", lt->name); 964933707f3Ssthen } else { 965933707f3Ssthen snprintf(nm, sizeof(nm), "CLASS%d", i); 966933707f3Ssthen } 967e10d3884Sbrad if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n", 968e10d3884Sbrad nm, (unsigned long)s->svr.qclass[i])) return 0; 969933707f3Ssthen } 970933707f3Ssthen if(!inhibit_zero || s->svr.qclass_big) { 971e10d3884Sbrad if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n", 972e10d3884Sbrad (unsigned long)s->svr.qclass_big)) return 0; 973933707f3Ssthen } 974933707f3Ssthen /* OPCODE */ 9752be9e038Ssthen for(i=0; i<UB_STATS_OPCODE_NUM; i++) { 976933707f3Ssthen if(inhibit_zero && s->svr.qopcode[i] == 0) 977933707f3Ssthen continue; 9780b68ff31Ssthen lt = sldns_lookup_by_id(sldns_opcodes, i); 979933707f3Ssthen if(lt && lt->name) { 980933707f3Ssthen snprintf(nm, sizeof(nm), "%s", lt->name); 981933707f3Ssthen } else { 982933707f3Ssthen snprintf(nm, sizeof(nm), "OPCODE%d", i); 983933707f3Ssthen } 984e10d3884Sbrad if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n", 985e10d3884Sbrad nm, (unsigned long)s->svr.qopcode[i])) return 0; 986933707f3Ssthen } 987933707f3Ssthen /* transport */ 988e10d3884Sbrad if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n", 989e10d3884Sbrad (unsigned long)s->svr.qtcp)) return 0; 990e10d3884Sbrad if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n", 991e10d3884Sbrad (unsigned long)s->svr.qtcp_outgoing)) return 0; 992e10d3884Sbrad if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n", 993e10d3884Sbrad (unsigned long)s->svr.qipv6)) return 0; 994933707f3Ssthen /* flags */ 995e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n", 996e10d3884Sbrad (unsigned long)s->svr.qbit_QR)) return 0; 997e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n", 998e10d3884Sbrad (unsigned long)s->svr.qbit_AA)) return 0; 999e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n", 1000e10d3884Sbrad (unsigned long)s->svr.qbit_TC)) return 0; 1001e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n", 1002e10d3884Sbrad (unsigned long)s->svr.qbit_RD)) return 0; 1003e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n", 1004e10d3884Sbrad (unsigned long)s->svr.qbit_RA)) return 0; 1005e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n", 1006e10d3884Sbrad (unsigned long)s->svr.qbit_Z)) return 0; 1007e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n", 1008e10d3884Sbrad (unsigned long)s->svr.qbit_AD)) return 0; 1009e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n", 1010e10d3884Sbrad (unsigned long)s->svr.qbit_CD)) return 0; 1011e10d3884Sbrad if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n", 1012e10d3884Sbrad (unsigned long)s->svr.qEDNS)) return 0; 1013e10d3884Sbrad if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n", 1014e10d3884Sbrad (unsigned long)s->svr.qEDNS_DO)) return 0; 1015933707f3Ssthen 1016933707f3Ssthen /* RCODE */ 10172be9e038Ssthen for(i=0; i<UB_STATS_RCODE_NUM; i++) { 1018d50c774aSbrad /* Always include RCODEs 0-5 */ 1019d50c774aSbrad if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0) 1020933707f3Ssthen continue; 10210b68ff31Ssthen lt = sldns_lookup_by_id(sldns_rcodes, i); 1022933707f3Ssthen if(lt && lt->name) { 1023933707f3Ssthen snprintf(nm, sizeof(nm), "%s", lt->name); 1024933707f3Ssthen } else { 1025933707f3Ssthen snprintf(nm, sizeof(nm), "RCODE%d", i); 1026933707f3Ssthen } 1027e10d3884Sbrad if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n", 1028e10d3884Sbrad nm, (unsigned long)s->svr.ans_rcode[i])) return 0; 1029933707f3Ssthen } 1030933707f3Ssthen if(!inhibit_zero || s->svr.ans_rcode_nodata) { 1031e10d3884Sbrad if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n", 1032e10d3884Sbrad (unsigned long)s->svr.ans_rcode_nodata)) return 0; 1033933707f3Ssthen } 1034*7191de28Ssthen /* iteration */ 1035*7191de28Ssthen if(!ssl_printf(ssl, "num.query.ratelimited"SQ"%lu\n", 1036*7191de28Ssthen (unsigned long)s->svr.queries_ratelimited)) return 0; 1037933707f3Ssthen /* validation */ 1038e10d3884Sbrad if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n", 1039e10d3884Sbrad (unsigned long)s->svr.ans_secure)) return 0; 1040e10d3884Sbrad if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n", 1041e10d3884Sbrad (unsigned long)s->svr.ans_bogus)) return 0; 1042e10d3884Sbrad if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n", 1043e10d3884Sbrad (unsigned long)s->svr.rrset_bogus)) return 0; 1044933707f3Ssthen /* threat detection */ 1045e10d3884Sbrad if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n", 1046e10d3884Sbrad (unsigned long)s->svr.unwanted_queries)) return 0; 1047e10d3884Sbrad if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n", 1048e10d3884Sbrad (unsigned long)s->svr.unwanted_replies)) return 0; 1049e10d3884Sbrad /* cache counts */ 1050e10d3884Sbrad if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n", 1051e10d3884Sbrad (unsigned)s->svr.msg_cache_count)) return 0; 1052e10d3884Sbrad if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n", 1053e10d3884Sbrad (unsigned)s->svr.rrset_cache_count)) return 0; 1054e10d3884Sbrad if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n", 1055e10d3884Sbrad (unsigned)s->svr.infra_cache_count)) return 0; 1056e10d3884Sbrad if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n", 1057e10d3884Sbrad (unsigned)s->svr.key_cache_count)) return 0; 1058*7191de28Ssthen #ifdef USE_DNSCRYPT 1059*7191de28Ssthen if(!ssl_printf(ssl, "dnscrypt_shared_secret.cache.count"SQ"%u\n", 1060*7191de28Ssthen (unsigned)s->svr.shared_secret_cache_count)) return 0; 1061*7191de28Ssthen if(!ssl_printf(ssl, "num.query.dnscrypt.shared_secret.cachemiss"SQ"%lu\n", 1062*7191de28Ssthen (unsigned long)s->svr.num_query_dnscrypt_secret_missed_cache)) return 0; 1063*7191de28Ssthen #endif /* USE_DNSCRYPT */ 1064933707f3Ssthen return 1; 1065933707f3Ssthen } 1066933707f3Ssthen 1067933707f3Ssthen /** do the stats command */ 1068933707f3Ssthen static void 1069933707f3Ssthen do_stats(SSL* ssl, struct daemon_remote* rc, int reset) 1070933707f3Ssthen { 1071933707f3Ssthen struct daemon* daemon = rc->worker->daemon; 10722be9e038Ssthen struct ub_stats_info total; 10732be9e038Ssthen struct ub_stats_info s; 1074933707f3Ssthen int i; 1075933707f3Ssthen log_assert(daemon->num > 0); 1076933707f3Ssthen /* gather all thread statistics in one place */ 1077933707f3Ssthen for(i=0; i<daemon->num; i++) { 1078933707f3Ssthen server_stats_obtain(rc->worker, daemon->workers[i], &s, reset); 1079933707f3Ssthen if(!print_thread_stats(ssl, i, &s)) 1080933707f3Ssthen return; 1081933707f3Ssthen if(i == 0) 1082933707f3Ssthen total = s; 1083933707f3Ssthen else server_stats_add(&total, &s); 1084933707f3Ssthen } 1085933707f3Ssthen /* print the thread statistics */ 1086933707f3Ssthen total.mesh_time_median /= (double)daemon->num; 1087933707f3Ssthen if(!print_stats(ssl, "total", &total)) 1088933707f3Ssthen return; 1089933707f3Ssthen if(!print_uptime(ssl, rc->worker, reset)) 1090933707f3Ssthen return; 1091933707f3Ssthen if(daemon->cfg->stat_extended) { 1092933707f3Ssthen if(!print_mem(ssl, rc->worker, daemon)) 1093933707f3Ssthen return; 1094933707f3Ssthen if(!print_hist(ssl, &total)) 1095933707f3Ssthen return; 1096933707f3Ssthen if(!print_ext(ssl, &total)) 1097933707f3Ssthen return; 1098933707f3Ssthen } 1099933707f3Ssthen } 1100933707f3Ssthen 1101933707f3Ssthen /** parse commandline argument domain name */ 1102933707f3Ssthen static int 1103933707f3Ssthen parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs) 1104933707f3Ssthen { 11050b68ff31Ssthen uint8_t nm[LDNS_MAX_DOMAINLEN+1]; 11060b68ff31Ssthen size_t nmlen = sizeof(nm); 11070b68ff31Ssthen int status; 1108933707f3Ssthen *res = NULL; 1109933707f3Ssthen *len = 0; 1110933707f3Ssthen *labs = 0; 11110b68ff31Ssthen status = sldns_str2wire_dname_buf(str, nm, &nmlen); 11120b68ff31Ssthen if(status != 0) { 11130b68ff31Ssthen ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str, 11140b68ff31Ssthen LDNS_WIREPARSE_OFFSET(status), 11150b68ff31Ssthen sldns_get_errorstr_parse(status)); 1116933707f3Ssthen return 0; 1117933707f3Ssthen } 11180b68ff31Ssthen *res = memdup(nm, nmlen); 1119933707f3Ssthen if(!*res) { 1120933707f3Ssthen ssl_printf(ssl, "error out of memory\n"); 1121933707f3Ssthen return 0; 1122933707f3Ssthen } 1123933707f3Ssthen *labs = dname_count_size_labels(*res, len); 1124933707f3Ssthen return 1; 1125933707f3Ssthen } 1126933707f3Ssthen 1127933707f3Ssthen /** find second argument, modifies string */ 1128933707f3Ssthen static int 1129933707f3Ssthen find_arg2(SSL* ssl, char* arg, char** arg2) 1130933707f3Ssthen { 1131933707f3Ssthen char* as = strchr(arg, ' '); 1132933707f3Ssthen char* at = strchr(arg, '\t'); 1133933707f3Ssthen if(as && at) { 1134933707f3Ssthen if(at < as) 1135933707f3Ssthen as = at; 1136933707f3Ssthen as[0]=0; 1137933707f3Ssthen *arg2 = skipwhite(as+1); 1138933707f3Ssthen } else if(as) { 1139933707f3Ssthen as[0]=0; 1140933707f3Ssthen *arg2 = skipwhite(as+1); 1141933707f3Ssthen } else if(at) { 1142933707f3Ssthen at[0]=0; 1143933707f3Ssthen *arg2 = skipwhite(at+1); 1144933707f3Ssthen } else { 1145933707f3Ssthen ssl_printf(ssl, "error could not find next argument " 1146933707f3Ssthen "after %s\n", arg); 1147933707f3Ssthen return 0; 1148933707f3Ssthen } 1149933707f3Ssthen return 1; 1150933707f3Ssthen } 1151933707f3Ssthen 1152933707f3Ssthen /** Add a new zone */ 115377079be7Ssthen static int 115477079be7Ssthen perform_zone_add(SSL* ssl, struct local_zones* zones, char* arg) 1155933707f3Ssthen { 1156933707f3Ssthen uint8_t* nm; 1157933707f3Ssthen int nmlabs; 1158933707f3Ssthen size_t nmlen; 1159933707f3Ssthen char* arg2; 1160933707f3Ssthen enum localzone_type t; 1161933707f3Ssthen struct local_zone* z; 1162933707f3Ssthen if(!find_arg2(ssl, arg, &arg2)) 116377079be7Ssthen return 0; 1164933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 116577079be7Ssthen return 0; 1166933707f3Ssthen if(!local_zone_str2type(arg2, &t)) { 1167933707f3Ssthen ssl_printf(ssl, "error not a zone type. %s\n", arg2); 1168933707f3Ssthen free(nm); 116977079be7Ssthen return 0; 1170933707f3Ssthen } 117177079be7Ssthen lock_rw_wrlock(&zones->lock); 117277079be7Ssthen if((z=local_zones_find(zones, nm, nmlen, 1173933707f3Ssthen nmlabs, LDNS_RR_CLASS_IN))) { 1174933707f3Ssthen /* already present in tree */ 1175933707f3Ssthen lock_rw_wrlock(&z->lock); 1176933707f3Ssthen z->type = t; /* update type anyway */ 1177933707f3Ssthen lock_rw_unlock(&z->lock); 1178933707f3Ssthen free(nm); 117977079be7Ssthen lock_rw_unlock(&zones->lock); 118077079be7Ssthen return 1; 1181933707f3Ssthen } 118277079be7Ssthen if(!local_zones_add_zone(zones, nm, nmlen, 1183933707f3Ssthen nmlabs, LDNS_RR_CLASS_IN, t)) { 118477079be7Ssthen lock_rw_unlock(&zones->lock); 1185933707f3Ssthen ssl_printf(ssl, "error out of memory\n"); 118677079be7Ssthen return 0; 1187933707f3Ssthen } 118877079be7Ssthen lock_rw_unlock(&zones->lock); 118977079be7Ssthen return 1; 119077079be7Ssthen } 119177079be7Ssthen 119277079be7Ssthen /** Do the local_zone command */ 119377079be7Ssthen static void 119477079be7Ssthen do_zone_add(SSL* ssl, struct local_zones* zones, char* arg) 119577079be7Ssthen { 119677079be7Ssthen if(!perform_zone_add(ssl, zones, arg)) 119777079be7Ssthen return; 1198933707f3Ssthen send_ok(ssl); 1199933707f3Ssthen } 1200933707f3Ssthen 120177079be7Ssthen /** Do the local_zones command */ 1202933707f3Ssthen static void 120377079be7Ssthen do_zones_add(SSL* ssl, struct local_zones* zones) 120477079be7Ssthen { 120577079be7Ssthen char buf[2048]; 120677079be7Ssthen int num = 0; 120777079be7Ssthen while(ssl_read_line(ssl, buf, sizeof(buf))) { 120877079be7Ssthen if(buf[0] == 0x04 && buf[1] == 0) 120977079be7Ssthen break; /* end of transmission */ 121077079be7Ssthen if(!perform_zone_add(ssl, zones, buf)) { 121177079be7Ssthen if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 121277079be7Ssthen return; 121377079be7Ssthen } 121477079be7Ssthen else 121577079be7Ssthen num++; 121677079be7Ssthen } 121777079be7Ssthen (void)ssl_printf(ssl, "added %d zones\n", num); 121877079be7Ssthen } 121977079be7Ssthen 122077079be7Ssthen /** Remove a zone */ 122177079be7Ssthen static int 122277079be7Ssthen perform_zone_remove(SSL* ssl, struct local_zones* zones, char* arg) 1223933707f3Ssthen { 1224933707f3Ssthen uint8_t* nm; 1225933707f3Ssthen int nmlabs; 1226933707f3Ssthen size_t nmlen; 1227933707f3Ssthen struct local_zone* z; 1228933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 122977079be7Ssthen return 0; 123077079be7Ssthen lock_rw_wrlock(&zones->lock); 123177079be7Ssthen if((z=local_zones_find(zones, nm, nmlen, 1232933707f3Ssthen nmlabs, LDNS_RR_CLASS_IN))) { 1233933707f3Ssthen /* present in tree */ 123477079be7Ssthen local_zones_del_zone(zones, z); 1235933707f3Ssthen } 123677079be7Ssthen lock_rw_unlock(&zones->lock); 1237933707f3Ssthen free(nm); 123877079be7Ssthen return 1; 123977079be7Ssthen } 124077079be7Ssthen 124177079be7Ssthen /** Do the local_zone_remove command */ 124277079be7Ssthen static void 124377079be7Ssthen do_zone_remove(SSL* ssl, struct local_zones* zones, char* arg) 124477079be7Ssthen { 124577079be7Ssthen if(!perform_zone_remove(ssl, zones, arg)) 124677079be7Ssthen return; 1247933707f3Ssthen send_ok(ssl); 1248933707f3Ssthen } 1249933707f3Ssthen 125077079be7Ssthen /** Do the local_zones_remove command */ 125177079be7Ssthen static void 125277079be7Ssthen do_zones_remove(SSL* ssl, struct local_zones* zones) 125377079be7Ssthen { 125477079be7Ssthen char buf[2048]; 125577079be7Ssthen int num = 0; 125677079be7Ssthen while(ssl_read_line(ssl, buf, sizeof(buf))) { 125777079be7Ssthen if(buf[0] == 0x04 && buf[1] == 0) 125877079be7Ssthen break; /* end of transmission */ 125977079be7Ssthen if(!perform_zone_remove(ssl, zones, buf)) { 126077079be7Ssthen if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 126177079be7Ssthen return; 126277079be7Ssthen } 126377079be7Ssthen else 126477079be7Ssthen num++; 126577079be7Ssthen } 126677079be7Ssthen (void)ssl_printf(ssl, "removed %d zones\n", num); 126777079be7Ssthen } 126877079be7Ssthen 1269933707f3Ssthen /** Add new RR data */ 127077079be7Ssthen static int 127177079be7Ssthen perform_data_add(SSL* ssl, struct local_zones* zones, char* arg) 1272933707f3Ssthen { 127377079be7Ssthen if(!local_zones_add_RR(zones, arg)) { 1274933707f3Ssthen ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg); 127577079be7Ssthen return 0; 1276933707f3Ssthen } 127777079be7Ssthen return 1; 127877079be7Ssthen } 127977079be7Ssthen 128077079be7Ssthen /** Do the local_data command */ 128177079be7Ssthen static void 128277079be7Ssthen do_data_add(SSL* ssl, struct local_zones* zones, char* arg) 128377079be7Ssthen { 128477079be7Ssthen if(!perform_data_add(ssl, zones, arg)) 128577079be7Ssthen return; 1286933707f3Ssthen send_ok(ssl); 1287933707f3Ssthen } 1288933707f3Ssthen 128977079be7Ssthen /** Do the local_datas command */ 1290933707f3Ssthen static void 129177079be7Ssthen do_datas_add(SSL* ssl, struct local_zones* zones) 129277079be7Ssthen { 129377079be7Ssthen char buf[2048]; 129477079be7Ssthen int num = 0; 129577079be7Ssthen while(ssl_read_line(ssl, buf, sizeof(buf))) { 129677079be7Ssthen if(buf[0] == 0x04 && buf[1] == 0) 129777079be7Ssthen break; /* end of transmission */ 129877079be7Ssthen if(!perform_data_add(ssl, zones, buf)) { 129977079be7Ssthen if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 130077079be7Ssthen return; 130177079be7Ssthen } 130277079be7Ssthen else 130377079be7Ssthen num++; 130477079be7Ssthen } 130577079be7Ssthen (void)ssl_printf(ssl, "added %d datas\n", num); 130677079be7Ssthen } 130777079be7Ssthen 130877079be7Ssthen /** Remove RR data */ 130977079be7Ssthen static int 131077079be7Ssthen perform_data_remove(SSL* ssl, struct local_zones* zones, char* arg) 1311933707f3Ssthen { 1312933707f3Ssthen uint8_t* nm; 1313933707f3Ssthen int nmlabs; 1314933707f3Ssthen size_t nmlen; 1315933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 131677079be7Ssthen return 0; 131777079be7Ssthen local_zones_del_data(zones, nm, 1318933707f3Ssthen nmlen, nmlabs, LDNS_RR_CLASS_IN); 1319933707f3Ssthen free(nm); 132077079be7Ssthen return 1; 132177079be7Ssthen } 132277079be7Ssthen 132377079be7Ssthen /** Do the local_data_remove command */ 132477079be7Ssthen static void 132577079be7Ssthen do_data_remove(SSL* ssl, struct local_zones* zones, char* arg) 132677079be7Ssthen { 132777079be7Ssthen if(!perform_data_remove(ssl, zones, arg)) 132877079be7Ssthen return; 1329933707f3Ssthen send_ok(ssl); 1330933707f3Ssthen } 1331933707f3Ssthen 133277079be7Ssthen /** Do the local_datas_remove command */ 133377079be7Ssthen static void 133477079be7Ssthen do_datas_remove(SSL* ssl, struct local_zones* zones) 133577079be7Ssthen { 133677079be7Ssthen char buf[2048]; 133777079be7Ssthen int num = 0; 133877079be7Ssthen while(ssl_read_line(ssl, buf, sizeof(buf))) { 133977079be7Ssthen if(buf[0] == 0x04 && buf[1] == 0) 134077079be7Ssthen break; /* end of transmission */ 134177079be7Ssthen if(!perform_data_remove(ssl, zones, buf)) { 134277079be7Ssthen if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 134377079be7Ssthen return; 134477079be7Ssthen } 134577079be7Ssthen else 134677079be7Ssthen num++; 134777079be7Ssthen } 134877079be7Ssthen (void)ssl_printf(ssl, "removed %d datas\n", num); 134977079be7Ssthen } 135077079be7Ssthen 135177079be7Ssthen /** Add a new zone to view */ 135277079be7Ssthen static void 135377079be7Ssthen do_view_zone_add(SSL* ssl, struct worker* worker, char* arg) 135477079be7Ssthen { 135577079be7Ssthen char* arg2; 135677079be7Ssthen struct view* v; 135777079be7Ssthen if(!find_arg2(ssl, arg, &arg2)) 135877079be7Ssthen return; 135977079be7Ssthen v = views_find_view(worker->daemon->views, 136077079be7Ssthen arg, 1 /* get write lock*/); 136177079be7Ssthen if(!v) { 136277079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 136377079be7Ssthen return; 136477079be7Ssthen } 13652be9e038Ssthen if(!v->local_zones) { 13662be9e038Ssthen if(!(v->local_zones = local_zones_create())){ 13672be9e038Ssthen lock_rw_unlock(&v->lock); 13682be9e038Ssthen ssl_printf(ssl,"error out of memory\n"); 13692be9e038Ssthen return; 13702be9e038Ssthen } 13712be9e038Ssthen if(!v->isfirst) { 13722be9e038Ssthen /* Global local-zone is not used for this view, 13732be9e038Ssthen * therefore add defaults to this view-specic 13742be9e038Ssthen * local-zone. */ 13752be9e038Ssthen struct config_file lz_cfg; 13762be9e038Ssthen memset(&lz_cfg, 0, sizeof(lz_cfg)); 13772be9e038Ssthen local_zone_enter_defaults(v->local_zones, &lz_cfg); 13782be9e038Ssthen } 13792be9e038Ssthen } 138077079be7Ssthen do_zone_add(ssl, v->local_zones, arg2); 138177079be7Ssthen lock_rw_unlock(&v->lock); 138277079be7Ssthen } 138377079be7Ssthen 138477079be7Ssthen /** Remove a zone from view */ 138577079be7Ssthen static void 138677079be7Ssthen do_view_zone_remove(SSL* ssl, struct worker* worker, char* arg) 138777079be7Ssthen { 138877079be7Ssthen char* arg2; 138977079be7Ssthen struct view* v; 139077079be7Ssthen if(!find_arg2(ssl, arg, &arg2)) 139177079be7Ssthen return; 139277079be7Ssthen v = views_find_view(worker->daemon->views, 139377079be7Ssthen arg, 1 /* get write lock*/); 139477079be7Ssthen if(!v) { 139577079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 139677079be7Ssthen return; 139777079be7Ssthen } 13982be9e038Ssthen if(!v->local_zones) { 13992be9e038Ssthen lock_rw_unlock(&v->lock); 14002be9e038Ssthen send_ok(ssl); 14012be9e038Ssthen return; 14022be9e038Ssthen } 140377079be7Ssthen do_zone_remove(ssl, v->local_zones, arg2); 140477079be7Ssthen lock_rw_unlock(&v->lock); 140577079be7Ssthen } 140677079be7Ssthen 140777079be7Ssthen /** Add new RR data to view */ 140877079be7Ssthen static void 140977079be7Ssthen do_view_data_add(SSL* ssl, struct worker* worker, char* arg) 141077079be7Ssthen { 141177079be7Ssthen char* arg2; 141277079be7Ssthen struct view* v; 141377079be7Ssthen if(!find_arg2(ssl, arg, &arg2)) 141477079be7Ssthen return; 141577079be7Ssthen v = views_find_view(worker->daemon->views, 141677079be7Ssthen arg, 1 /* get write lock*/); 141777079be7Ssthen if(!v) { 141877079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 141977079be7Ssthen return; 142077079be7Ssthen } 14212be9e038Ssthen if(!v->local_zones) { 14222be9e038Ssthen if(!(v->local_zones = local_zones_create())){ 14232be9e038Ssthen lock_rw_unlock(&v->lock); 14242be9e038Ssthen ssl_printf(ssl,"error out of memory\n"); 14252be9e038Ssthen return; 14262be9e038Ssthen } 14272be9e038Ssthen } 142877079be7Ssthen do_data_add(ssl, v->local_zones, arg2); 142977079be7Ssthen lock_rw_unlock(&v->lock); 143077079be7Ssthen } 143177079be7Ssthen 143277079be7Ssthen /** Remove RR data from view */ 143377079be7Ssthen static void 143477079be7Ssthen do_view_data_remove(SSL* ssl, struct worker* worker, char* arg) 143577079be7Ssthen { 143677079be7Ssthen char* arg2; 143777079be7Ssthen struct view* v; 143877079be7Ssthen if(!find_arg2(ssl, arg, &arg2)) 143977079be7Ssthen return; 144077079be7Ssthen v = views_find_view(worker->daemon->views, 144177079be7Ssthen arg, 1 /* get write lock*/); 144277079be7Ssthen if(!v) { 144377079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 144477079be7Ssthen return; 144577079be7Ssthen } 14462be9e038Ssthen if(!v->local_zones) { 14472be9e038Ssthen lock_rw_unlock(&v->lock); 14482be9e038Ssthen send_ok(ssl); 14492be9e038Ssthen return; 14502be9e038Ssthen } 145177079be7Ssthen do_data_remove(ssl, v->local_zones, arg2); 145277079be7Ssthen lock_rw_unlock(&v->lock); 145377079be7Ssthen } 145477079be7Ssthen 1455933707f3Ssthen /** cache lookup of nameservers */ 1456933707f3Ssthen static void 1457933707f3Ssthen do_lookup(SSL* ssl, struct worker* worker, char* arg) 1458933707f3Ssthen { 1459933707f3Ssthen uint8_t* nm; 1460933707f3Ssthen int nmlabs; 1461933707f3Ssthen size_t nmlen; 1462933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1463933707f3Ssthen return; 1464933707f3Ssthen (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs); 1465933707f3Ssthen free(nm); 1466933707f3Ssthen } 1467933707f3Ssthen 1468933707f3Ssthen /** flush something from rrset and msg caches */ 1469933707f3Ssthen static void 1470933707f3Ssthen do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen, 1471933707f3Ssthen uint16_t t, uint16_t c) 1472933707f3Ssthen { 147377079be7Ssthen hashvalue_type h; 1474933707f3Ssthen struct query_info k; 1475933707f3Ssthen rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0); 1476933707f3Ssthen if(t == LDNS_RR_TYPE_SOA) 1477933707f3Ssthen rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 1478933707f3Ssthen PACKED_RRSET_SOA_NEG); 1479933707f3Ssthen k.qname = nm; 1480933707f3Ssthen k.qname_len = nmlen; 1481933707f3Ssthen k.qtype = t; 1482933707f3Ssthen k.qclass = c; 148377079be7Ssthen k.local_alias = NULL; 148457dceb2aSbrad h = query_info_hash(&k, 0); 1485933707f3Ssthen slabhash_remove(worker->env.msg_cache, h, &k); 148657dceb2aSbrad if(t == LDNS_RR_TYPE_AAAA) { 148757dceb2aSbrad /* for AAAA also flush dns64 bit_cd packet */ 148857dceb2aSbrad h = query_info_hash(&k, BIT_CD); 148957dceb2aSbrad slabhash_remove(worker->env.msg_cache, h, &k); 149057dceb2aSbrad } 1491933707f3Ssthen } 1492933707f3Ssthen 1493933707f3Ssthen /** flush a type */ 1494933707f3Ssthen static void 1495933707f3Ssthen do_flush_type(SSL* ssl, struct worker* worker, char* arg) 1496933707f3Ssthen { 1497933707f3Ssthen uint8_t* nm; 1498933707f3Ssthen int nmlabs; 1499933707f3Ssthen size_t nmlen; 1500933707f3Ssthen char* arg2; 1501933707f3Ssthen uint16_t t; 1502933707f3Ssthen if(!find_arg2(ssl, arg, &arg2)) 1503933707f3Ssthen return; 1504933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1505933707f3Ssthen return; 15060b68ff31Ssthen t = sldns_get_rr_type_by_name(arg2); 1507933707f3Ssthen do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN); 1508933707f3Ssthen 1509933707f3Ssthen free(nm); 1510933707f3Ssthen send_ok(ssl); 1511933707f3Ssthen } 1512933707f3Ssthen 1513933707f3Ssthen /** flush statistics */ 1514933707f3Ssthen static void 1515933707f3Ssthen do_flush_stats(SSL* ssl, struct worker* worker) 1516933707f3Ssthen { 1517933707f3Ssthen worker_stats_clear(worker); 1518933707f3Ssthen send_ok(ssl); 1519933707f3Ssthen } 1520933707f3Ssthen 1521933707f3Ssthen /** 1522933707f3Ssthen * Local info for deletion functions 1523933707f3Ssthen */ 1524933707f3Ssthen struct del_info { 1525933707f3Ssthen /** worker */ 1526933707f3Ssthen struct worker* worker; 1527933707f3Ssthen /** name to delete */ 1528933707f3Ssthen uint8_t* name; 1529933707f3Ssthen /** length */ 1530933707f3Ssthen size_t len; 1531933707f3Ssthen /** labels */ 1532933707f3Ssthen int labs; 1533933707f3Ssthen /** time to invalidate to */ 1534e9c7b4efSsthen time_t expired; 1535933707f3Ssthen /** number of rrsets removed */ 1536933707f3Ssthen size_t num_rrsets; 1537933707f3Ssthen /** number of msgs removed */ 1538933707f3Ssthen size_t num_msgs; 1539933707f3Ssthen /** number of key entries removed */ 1540933707f3Ssthen size_t num_keys; 1541933707f3Ssthen /** length of addr */ 1542933707f3Ssthen socklen_t addrlen; 1543933707f3Ssthen /** socket address for host deletion */ 1544933707f3Ssthen struct sockaddr_storage addr; 1545933707f3Ssthen }; 1546933707f3Ssthen 1547933707f3Ssthen /** callback to delete hosts in infra cache */ 1548933707f3Ssthen static void 1549933707f3Ssthen infra_del_host(struct lruhash_entry* e, void* arg) 1550933707f3Ssthen { 1551933707f3Ssthen /* entry is locked */ 1552933707f3Ssthen struct del_info* inf = (struct del_info*)arg; 1553933707f3Ssthen struct infra_key* k = (struct infra_key*)e->key; 1554933707f3Ssthen if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) { 1555933707f3Ssthen struct infra_data* d = (struct infra_data*)e->data; 1556163a4143Ssthen d->probedelay = 0; 1557163a4143Ssthen d->timeout_A = 0; 1558163a4143Ssthen d->timeout_AAAA = 0; 1559163a4143Ssthen d->timeout_other = 0; 1560163a4143Ssthen rtt_init(&d->rtt); 1561a961b961Ssthen if(d->ttl > inf->expired) { 1562933707f3Ssthen d->ttl = inf->expired; 1563933707f3Ssthen inf->num_keys++; 1564933707f3Ssthen } 1565933707f3Ssthen } 1566933707f3Ssthen } 1567933707f3Ssthen 1568933707f3Ssthen /** flush infra cache */ 1569933707f3Ssthen static void 1570933707f3Ssthen do_flush_infra(SSL* ssl, struct worker* worker, char* arg) 1571933707f3Ssthen { 1572933707f3Ssthen struct sockaddr_storage addr; 1573933707f3Ssthen socklen_t len; 1574933707f3Ssthen struct del_info inf; 1575933707f3Ssthen if(strcmp(arg, "all") == 0) { 1576933707f3Ssthen slabhash_clear(worker->env.infra_cache->hosts); 1577933707f3Ssthen send_ok(ssl); 1578933707f3Ssthen return; 1579933707f3Ssthen } 1580933707f3Ssthen if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) { 1581933707f3Ssthen (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg); 1582933707f3Ssthen return; 1583933707f3Ssthen } 1584933707f3Ssthen /* delete all entries from cache */ 1585933707f3Ssthen /* what we do is to set them all expired */ 1586933707f3Ssthen inf.worker = worker; 1587933707f3Ssthen inf.name = 0; 1588933707f3Ssthen inf.len = 0; 1589933707f3Ssthen inf.labs = 0; 1590933707f3Ssthen inf.expired = *worker->env.now; 1591933707f3Ssthen inf.expired -= 3; /* handle 3 seconds skew between threads */ 1592933707f3Ssthen inf.num_rrsets = 0; 1593933707f3Ssthen inf.num_msgs = 0; 1594933707f3Ssthen inf.num_keys = 0; 1595933707f3Ssthen inf.addrlen = len; 1596933707f3Ssthen memmove(&inf.addr, &addr, len); 1597933707f3Ssthen slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host, 1598933707f3Ssthen &inf); 1599933707f3Ssthen send_ok(ssl); 1600933707f3Ssthen } 1601933707f3Ssthen 1602933707f3Ssthen /** flush requestlist */ 1603933707f3Ssthen static void 1604933707f3Ssthen do_flush_requestlist(SSL* ssl, struct worker* worker) 1605933707f3Ssthen { 1606933707f3Ssthen mesh_delete_all(worker->env.mesh); 1607933707f3Ssthen send_ok(ssl); 1608933707f3Ssthen } 1609933707f3Ssthen 1610933707f3Ssthen /** callback to delete rrsets in a zone */ 1611933707f3Ssthen static void 1612933707f3Ssthen zone_del_rrset(struct lruhash_entry* e, void* arg) 1613933707f3Ssthen { 1614933707f3Ssthen /* entry is locked */ 1615933707f3Ssthen struct del_info* inf = (struct del_info*)arg; 1616933707f3Ssthen struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 1617933707f3Ssthen if(dname_subdomain_c(k->rk.dname, inf->name)) { 1618933707f3Ssthen struct packed_rrset_data* d = 1619933707f3Ssthen (struct packed_rrset_data*)e->data; 1620a961b961Ssthen if(d->ttl > inf->expired) { 1621933707f3Ssthen d->ttl = inf->expired; 1622933707f3Ssthen inf->num_rrsets++; 1623933707f3Ssthen } 1624933707f3Ssthen } 1625933707f3Ssthen } 1626933707f3Ssthen 1627933707f3Ssthen /** callback to delete messages in a zone */ 1628933707f3Ssthen static void 1629933707f3Ssthen zone_del_msg(struct lruhash_entry* e, void* arg) 1630933707f3Ssthen { 1631933707f3Ssthen /* entry is locked */ 1632933707f3Ssthen struct del_info* inf = (struct del_info*)arg; 1633933707f3Ssthen struct msgreply_entry* k = (struct msgreply_entry*)e->key; 1634933707f3Ssthen if(dname_subdomain_c(k->key.qname, inf->name)) { 1635933707f3Ssthen struct reply_info* d = (struct reply_info*)e->data; 1636a961b961Ssthen if(d->ttl > inf->expired) { 1637933707f3Ssthen d->ttl = inf->expired; 1638933707f3Ssthen inf->num_msgs++; 1639933707f3Ssthen } 1640933707f3Ssthen } 1641933707f3Ssthen } 1642933707f3Ssthen 1643933707f3Ssthen /** callback to delete keys in zone */ 1644933707f3Ssthen static void 1645933707f3Ssthen zone_del_kcache(struct lruhash_entry* e, void* arg) 1646933707f3Ssthen { 1647933707f3Ssthen /* entry is locked */ 1648933707f3Ssthen struct del_info* inf = (struct del_info*)arg; 1649933707f3Ssthen struct key_entry_key* k = (struct key_entry_key*)e->key; 1650933707f3Ssthen if(dname_subdomain_c(k->name, inf->name)) { 1651933707f3Ssthen struct key_entry_data* d = (struct key_entry_data*)e->data; 1652a961b961Ssthen if(d->ttl > inf->expired) { 1653933707f3Ssthen d->ttl = inf->expired; 1654933707f3Ssthen inf->num_keys++; 1655933707f3Ssthen } 1656933707f3Ssthen } 1657933707f3Ssthen } 1658933707f3Ssthen 1659933707f3Ssthen /** remove all rrsets and keys from zone from cache */ 1660933707f3Ssthen static void 1661933707f3Ssthen do_flush_zone(SSL* ssl, struct worker* worker, char* arg) 1662933707f3Ssthen { 1663933707f3Ssthen uint8_t* nm; 1664933707f3Ssthen int nmlabs; 1665933707f3Ssthen size_t nmlen; 1666933707f3Ssthen struct del_info inf; 1667933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1668933707f3Ssthen return; 1669933707f3Ssthen /* delete all RRs and key entries from zone */ 1670933707f3Ssthen /* what we do is to set them all expired */ 1671933707f3Ssthen inf.worker = worker; 1672933707f3Ssthen inf.name = nm; 1673933707f3Ssthen inf.len = nmlen; 1674933707f3Ssthen inf.labs = nmlabs; 1675933707f3Ssthen inf.expired = *worker->env.now; 1676933707f3Ssthen inf.expired -= 3; /* handle 3 seconds skew between threads */ 1677933707f3Ssthen inf.num_rrsets = 0; 1678933707f3Ssthen inf.num_msgs = 0; 1679933707f3Ssthen inf.num_keys = 0; 1680933707f3Ssthen slabhash_traverse(&worker->env.rrset_cache->table, 1, 1681933707f3Ssthen &zone_del_rrset, &inf); 1682933707f3Ssthen 1683933707f3Ssthen slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf); 1684933707f3Ssthen 1685933707f3Ssthen /* and validator cache */ 1686933707f3Ssthen if(worker->env.key_cache) { 1687933707f3Ssthen slabhash_traverse(worker->env.key_cache->slab, 1, 1688933707f3Ssthen &zone_del_kcache, &inf); 1689933707f3Ssthen } 1690933707f3Ssthen 1691933707f3Ssthen free(nm); 1692933707f3Ssthen 1693e10d3884Sbrad (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1694e10d3884Sbrad "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1695e10d3884Sbrad (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1696933707f3Ssthen } 1697933707f3Ssthen 1698cebdf579Ssthen /** callback to delete bogus rrsets */ 1699cebdf579Ssthen static void 1700cebdf579Ssthen bogus_del_rrset(struct lruhash_entry* e, void* arg) 1701cebdf579Ssthen { 1702cebdf579Ssthen /* entry is locked */ 1703cebdf579Ssthen struct del_info* inf = (struct del_info*)arg; 1704cebdf579Ssthen struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 1705cebdf579Ssthen if(d->security == sec_status_bogus) { 1706cebdf579Ssthen d->ttl = inf->expired; 1707cebdf579Ssthen inf->num_rrsets++; 1708cebdf579Ssthen } 1709cebdf579Ssthen } 1710cebdf579Ssthen 1711cebdf579Ssthen /** callback to delete bogus messages */ 1712cebdf579Ssthen static void 1713cebdf579Ssthen bogus_del_msg(struct lruhash_entry* e, void* arg) 1714cebdf579Ssthen { 1715cebdf579Ssthen /* entry is locked */ 1716cebdf579Ssthen struct del_info* inf = (struct del_info*)arg; 1717cebdf579Ssthen struct reply_info* d = (struct reply_info*)e->data; 1718cebdf579Ssthen if(d->security == sec_status_bogus) { 1719cebdf579Ssthen d->ttl = inf->expired; 1720cebdf579Ssthen inf->num_msgs++; 1721cebdf579Ssthen } 1722cebdf579Ssthen } 1723cebdf579Ssthen 1724cebdf579Ssthen /** callback to delete bogus keys */ 1725cebdf579Ssthen static void 1726cebdf579Ssthen bogus_del_kcache(struct lruhash_entry* e, void* arg) 1727cebdf579Ssthen { 1728cebdf579Ssthen /* entry is locked */ 1729cebdf579Ssthen struct del_info* inf = (struct del_info*)arg; 1730cebdf579Ssthen struct key_entry_data* d = (struct key_entry_data*)e->data; 1731cebdf579Ssthen if(d->isbad) { 1732cebdf579Ssthen d->ttl = inf->expired; 1733cebdf579Ssthen inf->num_keys++; 1734cebdf579Ssthen } 1735cebdf579Ssthen } 1736cebdf579Ssthen 1737e10d3884Sbrad /** remove all bogus rrsets, msgs and keys from cache */ 1738cebdf579Ssthen static void 1739cebdf579Ssthen do_flush_bogus(SSL* ssl, struct worker* worker) 1740cebdf579Ssthen { 1741cebdf579Ssthen struct del_info inf; 1742cebdf579Ssthen /* what we do is to set them all expired */ 1743cebdf579Ssthen inf.worker = worker; 1744cebdf579Ssthen inf.expired = *worker->env.now; 1745cebdf579Ssthen inf.expired -= 3; /* handle 3 seconds skew between threads */ 1746cebdf579Ssthen inf.num_rrsets = 0; 1747cebdf579Ssthen inf.num_msgs = 0; 1748cebdf579Ssthen inf.num_keys = 0; 1749cebdf579Ssthen slabhash_traverse(&worker->env.rrset_cache->table, 1, 1750cebdf579Ssthen &bogus_del_rrset, &inf); 1751cebdf579Ssthen 1752cebdf579Ssthen slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf); 1753cebdf579Ssthen 1754cebdf579Ssthen /* and validator cache */ 1755cebdf579Ssthen if(worker->env.key_cache) { 1756cebdf579Ssthen slabhash_traverse(worker->env.key_cache->slab, 1, 1757cebdf579Ssthen &bogus_del_kcache, &inf); 1758cebdf579Ssthen } 1759cebdf579Ssthen 1760e10d3884Sbrad (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1761e10d3884Sbrad "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1762e10d3884Sbrad (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1763e10d3884Sbrad } 1764e10d3884Sbrad 1765e10d3884Sbrad /** callback to delete negative and servfail rrsets */ 1766e10d3884Sbrad static void 1767e10d3884Sbrad negative_del_rrset(struct lruhash_entry* e, void* arg) 1768e10d3884Sbrad { 1769e10d3884Sbrad /* entry is locked */ 1770e10d3884Sbrad struct del_info* inf = (struct del_info*)arg; 1771e10d3884Sbrad struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 1772e10d3884Sbrad struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 1773e10d3884Sbrad /* delete the parentside negative cache rrsets, 1774e10d3884Sbrad * these are namerserver rrsets that failed lookup, rdata empty */ 1775e10d3884Sbrad if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 && 1776e10d3884Sbrad d->rrsig_count == 0 && d->rr_len[0] == 0) { 1777e10d3884Sbrad d->ttl = inf->expired; 1778e10d3884Sbrad inf->num_rrsets++; 1779e10d3884Sbrad } 1780e10d3884Sbrad } 1781e10d3884Sbrad 1782e10d3884Sbrad /** callback to delete negative and servfail messages */ 1783e10d3884Sbrad static void 1784e10d3884Sbrad negative_del_msg(struct lruhash_entry* e, void* arg) 1785e10d3884Sbrad { 1786e10d3884Sbrad /* entry is locked */ 1787e10d3884Sbrad struct del_info* inf = (struct del_info*)arg; 1788e10d3884Sbrad struct reply_info* d = (struct reply_info*)e->data; 1789e10d3884Sbrad /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error 1790e10d3884Sbrad * or NOERROR rcode with ANCOUNT==0: a NODATA answer */ 1791e10d3884Sbrad if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) { 1792e10d3884Sbrad d->ttl = inf->expired; 1793e10d3884Sbrad inf->num_msgs++; 1794e10d3884Sbrad } 1795e10d3884Sbrad } 1796e10d3884Sbrad 1797e10d3884Sbrad /** callback to delete negative key entries */ 1798e10d3884Sbrad static void 1799e10d3884Sbrad negative_del_kcache(struct lruhash_entry* e, void* arg) 1800e10d3884Sbrad { 1801e10d3884Sbrad /* entry is locked */ 1802e10d3884Sbrad struct del_info* inf = (struct del_info*)arg; 1803e10d3884Sbrad struct key_entry_data* d = (struct key_entry_data*)e->data; 1804e10d3884Sbrad /* could be bad because of lookup failure on the DS, DNSKEY, which 1805e10d3884Sbrad * was nxdomain or servfail, and thus a result of negative lookups */ 1806e10d3884Sbrad if(d->isbad) { 1807e10d3884Sbrad d->ttl = inf->expired; 1808e10d3884Sbrad inf->num_keys++; 1809e10d3884Sbrad } 1810e10d3884Sbrad } 1811e10d3884Sbrad 1812e10d3884Sbrad /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */ 1813e10d3884Sbrad static void 1814e10d3884Sbrad do_flush_negative(SSL* ssl, struct worker* worker) 1815e10d3884Sbrad { 1816e10d3884Sbrad struct del_info inf; 1817e10d3884Sbrad /* what we do is to set them all expired */ 1818e10d3884Sbrad inf.worker = worker; 1819e10d3884Sbrad inf.expired = *worker->env.now; 1820e10d3884Sbrad inf.expired -= 3; /* handle 3 seconds skew between threads */ 1821e10d3884Sbrad inf.num_rrsets = 0; 1822e10d3884Sbrad inf.num_msgs = 0; 1823e10d3884Sbrad inf.num_keys = 0; 1824e10d3884Sbrad slabhash_traverse(&worker->env.rrset_cache->table, 1, 1825e10d3884Sbrad &negative_del_rrset, &inf); 1826e10d3884Sbrad 1827e10d3884Sbrad slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf); 1828e10d3884Sbrad 1829e10d3884Sbrad /* and validator cache */ 1830e10d3884Sbrad if(worker->env.key_cache) { 1831e10d3884Sbrad slabhash_traverse(worker->env.key_cache->slab, 1, 1832e10d3884Sbrad &negative_del_kcache, &inf); 1833e10d3884Sbrad } 1834e10d3884Sbrad 1835e10d3884Sbrad (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1836e10d3884Sbrad "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1837e10d3884Sbrad (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1838cebdf579Ssthen } 1839cebdf579Ssthen 1840933707f3Ssthen /** remove name rrset from cache */ 1841933707f3Ssthen static void 1842933707f3Ssthen do_flush_name(SSL* ssl, struct worker* w, char* arg) 1843933707f3Ssthen { 1844933707f3Ssthen uint8_t* nm; 1845933707f3Ssthen int nmlabs; 1846933707f3Ssthen size_t nmlen; 1847933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1848933707f3Ssthen return; 1849933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN); 1850933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN); 1851933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN); 1852933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN); 1853933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN); 1854933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN); 1855933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN); 1856933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN); 1857933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN); 1858933707f3Ssthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN); 1859933707f3Ssthen 1860933707f3Ssthen free(nm); 1861933707f3Ssthen send_ok(ssl); 1862933707f3Ssthen } 1863933707f3Ssthen 1864933707f3Ssthen /** printout a delegation point info */ 1865933707f3Ssthen static int 1866e10d3884Sbrad ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass, 1867933707f3Ssthen struct delegpt* dp) 1868933707f3Ssthen { 1869933707f3Ssthen char buf[257]; 1870933707f3Ssthen struct delegpt_ns* ns; 1871933707f3Ssthen struct delegpt_addr* a; 1872933707f3Ssthen int f = 0; 1873933707f3Ssthen if(str) { /* print header for forward, stub */ 18740b68ff31Ssthen char* c = sldns_wire2str_class(dclass); 1875933707f3Ssthen dname_str(nm, buf); 1876e10d3884Sbrad if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) { 1877933707f3Ssthen free(c); 1878933707f3Ssthen return 0; 1879933707f3Ssthen } 1880933707f3Ssthen free(c); 1881933707f3Ssthen } 1882933707f3Ssthen for(ns = dp->nslist; ns; ns = ns->next) { 1883933707f3Ssthen dname_str(ns->name, buf); 1884933707f3Ssthen if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 1885933707f3Ssthen return 0; 1886933707f3Ssthen f = 1; 1887933707f3Ssthen } 1888933707f3Ssthen for(a = dp->target_list; a; a = a->next_target) { 1889933707f3Ssthen addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf)); 1890933707f3Ssthen if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 1891933707f3Ssthen return 0; 1892933707f3Ssthen f = 1; 1893933707f3Ssthen } 1894933707f3Ssthen return ssl_printf(ssl, "\n"); 1895933707f3Ssthen } 1896933707f3Ssthen 1897933707f3Ssthen 1898933707f3Ssthen /** print root forwards */ 1899933707f3Ssthen static int 1900933707f3Ssthen print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root) 1901933707f3Ssthen { 1902933707f3Ssthen struct delegpt* dp; 1903933707f3Ssthen dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN); 1904933707f3Ssthen if(!dp) 1905933707f3Ssthen return ssl_printf(ssl, "off (using root hints)\n"); 1906933707f3Ssthen /* if dp is returned it must be the root */ 1907933707f3Ssthen log_assert(query_dname_compare(dp->name, root)==0); 1908933707f3Ssthen return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp); 1909933707f3Ssthen } 1910933707f3Ssthen 1911933707f3Ssthen /** parse args into delegpt */ 1912933707f3Ssthen static struct delegpt* 1913163a4143Ssthen parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names) 1914933707f3Ssthen { 1915933707f3Ssthen /* parse args and add in */ 1916933707f3Ssthen char* p = args; 1917933707f3Ssthen char* todo; 1918163a4143Ssthen struct delegpt* dp = delegpt_create_mlc(nm); 1919933707f3Ssthen struct sockaddr_storage addr; 1920933707f3Ssthen socklen_t addrlen; 1921163a4143Ssthen if(!dp) { 1922933707f3Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 1923933707f3Ssthen return NULL; 1924933707f3Ssthen } 1925933707f3Ssthen while(p) { 1926933707f3Ssthen todo = p; 1927933707f3Ssthen p = strchr(p, ' '); /* find next spot, if any */ 1928933707f3Ssthen if(p) { 1929933707f3Ssthen *p++ = 0; /* end this spot */ 1930933707f3Ssthen p = skipwhite(p); /* position at next spot */ 1931933707f3Ssthen } 1932933707f3Ssthen /* parse address */ 1933933707f3Ssthen if(!extstrtoaddr(todo, &addr, &addrlen)) { 1934163a4143Ssthen if(allow_names) { 1935163a4143Ssthen uint8_t* n = NULL; 1936163a4143Ssthen size_t ln; 1937163a4143Ssthen int lb; 1938163a4143Ssthen if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) { 1939163a4143Ssthen (void)ssl_printf(ssl, "error cannot " 1940163a4143Ssthen "parse IP address or name " 1941163a4143Ssthen "'%s'\n", todo); 1942163a4143Ssthen delegpt_free_mlc(dp); 1943933707f3Ssthen return NULL; 1944933707f3Ssthen } 1945163a4143Ssthen if(!delegpt_add_ns_mlc(dp, n, 0)) { 1946933707f3Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 1947cebdf579Ssthen free(n); 1948163a4143Ssthen delegpt_free_mlc(dp); 1949933707f3Ssthen return NULL; 1950933707f3Ssthen } 1951163a4143Ssthen free(n); 1952163a4143Ssthen 1953163a4143Ssthen } else { 1954163a4143Ssthen (void)ssl_printf(ssl, "error cannot parse" 1955163a4143Ssthen " IP address '%s'\n", todo); 1956163a4143Ssthen delegpt_free_mlc(dp); 1957163a4143Ssthen return NULL; 1958163a4143Ssthen } 1959163a4143Ssthen } else { 1960163a4143Ssthen /* add address */ 1961163a4143Ssthen if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) { 1962163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 1963163a4143Ssthen delegpt_free_mlc(dp); 1964163a4143Ssthen return NULL; 1965163a4143Ssthen } 1966163a4143Ssthen } 1967933707f3Ssthen } 1968a961b961Ssthen dp->has_parent_side_NS = 1; 1969933707f3Ssthen return dp; 1970933707f3Ssthen } 1971933707f3Ssthen 1972933707f3Ssthen /** do the status command */ 1973933707f3Ssthen static void 1974933707f3Ssthen do_forward(SSL* ssl, struct worker* worker, char* args) 1975933707f3Ssthen { 1976933707f3Ssthen struct iter_forwards* fwd = worker->env.fwds; 1977933707f3Ssthen uint8_t* root = (uint8_t*)"\000"; 1978933707f3Ssthen if(!fwd) { 1979933707f3Ssthen (void)ssl_printf(ssl, "error: structure not allocated\n"); 1980933707f3Ssthen return; 1981933707f3Ssthen } 1982933707f3Ssthen if(args == NULL || args[0] == 0) { 1983933707f3Ssthen (void)print_root_fwds(ssl, fwd, root); 1984933707f3Ssthen return; 1985933707f3Ssthen } 1986933707f3Ssthen /* set root forwards for this thread. since we are in remote control 1987933707f3Ssthen * the actual mesh is not running, so we can freely edit it. */ 1988933707f3Ssthen /* delete all the existing queries first */ 1989933707f3Ssthen mesh_delete_all(worker->env.mesh); 1990933707f3Ssthen if(strcmp(args, "off") == 0) { 1991933707f3Ssthen forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root); 1992933707f3Ssthen } else { 1993933707f3Ssthen struct delegpt* dp; 1994163a4143Ssthen if(!(dp = parse_delegpt(ssl, args, root, 0))) 1995933707f3Ssthen return; 1996933707f3Ssthen if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) { 1997933707f3Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 1998933707f3Ssthen return; 1999933707f3Ssthen } 2000933707f3Ssthen } 2001933707f3Ssthen send_ok(ssl); 2002933707f3Ssthen } 2003933707f3Ssthen 2004163a4143Ssthen static int 2005163a4143Ssthen parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp, 2006163a4143Ssthen int* insecure, int* prime) 2007163a4143Ssthen { 2008163a4143Ssthen char* zonename; 2009163a4143Ssthen char* rest; 2010163a4143Ssthen size_t nmlen; 2011163a4143Ssthen int nmlabs; 2012163a4143Ssthen /* parse all -x args */ 2013163a4143Ssthen while(args[0] == '+') { 2014163a4143Ssthen if(!find_arg2(ssl, args, &rest)) 2015163a4143Ssthen return 0; 2016163a4143Ssthen while(*(++args) != 0) { 2017163a4143Ssthen if(*args == 'i' && insecure) 2018163a4143Ssthen *insecure = 1; 2019163a4143Ssthen else if(*args == 'p' && prime) 2020163a4143Ssthen *prime = 1; 2021163a4143Ssthen else { 2022163a4143Ssthen (void)ssl_printf(ssl, "error: unknown option %s\n", args); 2023163a4143Ssthen return 0; 2024163a4143Ssthen } 2025163a4143Ssthen } 2026163a4143Ssthen args = rest; 2027163a4143Ssthen } 2028163a4143Ssthen /* parse name */ 2029163a4143Ssthen if(dp) { 2030163a4143Ssthen if(!find_arg2(ssl, args, &rest)) 2031163a4143Ssthen return 0; 2032163a4143Ssthen zonename = args; 2033163a4143Ssthen args = rest; 2034163a4143Ssthen } else zonename = args; 2035163a4143Ssthen if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs)) 2036163a4143Ssthen return 0; 2037163a4143Ssthen 2038163a4143Ssthen /* parse dp */ 2039163a4143Ssthen if(dp) { 2040163a4143Ssthen if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) { 2041163a4143Ssthen free(*nm); 2042163a4143Ssthen return 0; 2043163a4143Ssthen } 2044163a4143Ssthen } 2045163a4143Ssthen return 1; 2046163a4143Ssthen } 2047163a4143Ssthen 2048163a4143Ssthen /** do the forward_add command */ 2049163a4143Ssthen static void 2050163a4143Ssthen do_forward_add(SSL* ssl, struct worker* worker, char* args) 2051163a4143Ssthen { 2052163a4143Ssthen struct iter_forwards* fwd = worker->env.fwds; 2053163a4143Ssthen int insecure = 0; 2054163a4143Ssthen uint8_t* nm = NULL; 2055163a4143Ssthen struct delegpt* dp = NULL; 2056163a4143Ssthen if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL)) 2057163a4143Ssthen return; 20580b68ff31Ssthen if(insecure && worker->env.anchors) { 2059163a4143Ssthen if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2060163a4143Ssthen nm)) { 2061163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2062163a4143Ssthen delegpt_free_mlc(dp); 2063163a4143Ssthen free(nm); 2064163a4143Ssthen return; 2065163a4143Ssthen } 2066163a4143Ssthen } 2067163a4143Ssthen if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) { 2068163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2069163a4143Ssthen free(nm); 2070163a4143Ssthen return; 2071163a4143Ssthen } 2072163a4143Ssthen free(nm); 2073163a4143Ssthen send_ok(ssl); 2074163a4143Ssthen } 2075163a4143Ssthen 2076163a4143Ssthen /** do the forward_remove command */ 2077163a4143Ssthen static void 2078163a4143Ssthen do_forward_remove(SSL* ssl, struct worker* worker, char* args) 2079163a4143Ssthen { 2080163a4143Ssthen struct iter_forwards* fwd = worker->env.fwds; 2081163a4143Ssthen int insecure = 0; 2082163a4143Ssthen uint8_t* nm = NULL; 2083163a4143Ssthen if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL)) 2084163a4143Ssthen return; 20850b68ff31Ssthen if(insecure && worker->env.anchors) 2086163a4143Ssthen anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2087163a4143Ssthen nm); 2088163a4143Ssthen forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm); 2089163a4143Ssthen free(nm); 2090163a4143Ssthen send_ok(ssl); 2091163a4143Ssthen } 2092163a4143Ssthen 2093163a4143Ssthen /** do the stub_add command */ 2094163a4143Ssthen static void 2095163a4143Ssthen do_stub_add(SSL* ssl, struct worker* worker, char* args) 2096163a4143Ssthen { 2097163a4143Ssthen struct iter_forwards* fwd = worker->env.fwds; 2098163a4143Ssthen int insecure = 0, prime = 0; 2099163a4143Ssthen uint8_t* nm = NULL; 2100163a4143Ssthen struct delegpt* dp = NULL; 2101163a4143Ssthen if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime)) 2102163a4143Ssthen return; 21030b68ff31Ssthen if(insecure && worker->env.anchors) { 2104163a4143Ssthen if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2105163a4143Ssthen nm)) { 2106163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2107163a4143Ssthen delegpt_free_mlc(dp); 2108163a4143Ssthen free(nm); 2109163a4143Ssthen return; 2110163a4143Ssthen } 2111163a4143Ssthen } 2112163a4143Ssthen if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) { 21130b68ff31Ssthen if(insecure && worker->env.anchors) 21140b68ff31Ssthen anchors_delete_insecure(worker->env.anchors, 2115163a4143Ssthen LDNS_RR_CLASS_IN, nm); 2116163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2117163a4143Ssthen delegpt_free_mlc(dp); 2118163a4143Ssthen free(nm); 2119163a4143Ssthen return; 2120163a4143Ssthen } 2121163a4143Ssthen if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) { 2122163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2123163a4143Ssthen forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm); 21240b68ff31Ssthen if(insecure && worker->env.anchors) 21250b68ff31Ssthen anchors_delete_insecure(worker->env.anchors, 2126163a4143Ssthen LDNS_RR_CLASS_IN, nm); 2127163a4143Ssthen free(nm); 2128163a4143Ssthen return; 2129163a4143Ssthen } 2130163a4143Ssthen free(nm); 2131163a4143Ssthen send_ok(ssl); 2132163a4143Ssthen } 2133163a4143Ssthen 2134163a4143Ssthen /** do the stub_remove command */ 2135163a4143Ssthen static void 2136163a4143Ssthen do_stub_remove(SSL* ssl, struct worker* worker, char* args) 2137163a4143Ssthen { 2138163a4143Ssthen struct iter_forwards* fwd = worker->env.fwds; 2139163a4143Ssthen int insecure = 0; 2140163a4143Ssthen uint8_t* nm = NULL; 2141163a4143Ssthen if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL)) 2142163a4143Ssthen return; 21430b68ff31Ssthen if(insecure && worker->env.anchors) 2144163a4143Ssthen anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2145163a4143Ssthen nm); 2146163a4143Ssthen forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm); 2147163a4143Ssthen hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm); 2148163a4143Ssthen free(nm); 2149163a4143Ssthen send_ok(ssl); 2150163a4143Ssthen } 2151163a4143Ssthen 2152e9c7b4efSsthen /** do the insecure_add command */ 2153e9c7b4efSsthen static void 2154e9c7b4efSsthen do_insecure_add(SSL* ssl, struct worker* worker, char* arg) 2155e9c7b4efSsthen { 2156e9c7b4efSsthen size_t nmlen; 2157e9c7b4efSsthen int nmlabs; 2158e9c7b4efSsthen uint8_t* nm = NULL; 2159e9c7b4efSsthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2160e9c7b4efSsthen return; 21610b68ff31Ssthen if(worker->env.anchors) { 21620b68ff31Ssthen if(!anchors_add_insecure(worker->env.anchors, 21630b68ff31Ssthen LDNS_RR_CLASS_IN, nm)) { 2164e9c7b4efSsthen (void)ssl_printf(ssl, "error out of memory\n"); 2165e9c7b4efSsthen free(nm); 2166e9c7b4efSsthen return; 2167e9c7b4efSsthen } 21680b68ff31Ssthen } 2169e9c7b4efSsthen free(nm); 2170e9c7b4efSsthen send_ok(ssl); 2171e9c7b4efSsthen } 2172e9c7b4efSsthen 2173e9c7b4efSsthen /** do the insecure_remove command */ 2174e9c7b4efSsthen static void 2175e9c7b4efSsthen do_insecure_remove(SSL* ssl, struct worker* worker, char* arg) 2176e9c7b4efSsthen { 2177e9c7b4efSsthen size_t nmlen; 2178e9c7b4efSsthen int nmlabs; 2179e9c7b4efSsthen uint8_t* nm = NULL; 2180e9c7b4efSsthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2181e9c7b4efSsthen return; 21820b68ff31Ssthen if(worker->env.anchors) 21830b68ff31Ssthen anchors_delete_insecure(worker->env.anchors, 21840b68ff31Ssthen LDNS_RR_CLASS_IN, nm); 2185e9c7b4efSsthen free(nm); 2186e9c7b4efSsthen send_ok(ssl); 2187e9c7b4efSsthen } 2188e9c7b4efSsthen 2189a58bff56Ssthen static void 2190a58bff56Ssthen do_insecure_list(SSL* ssl, struct worker* worker) 2191a58bff56Ssthen { 2192a58bff56Ssthen char buf[257]; 2193a58bff56Ssthen struct trust_anchor* a; 2194a58bff56Ssthen if(worker->env.anchors) { 2195a58bff56Ssthen RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) { 2196a58bff56Ssthen if(a->numDS == 0 && a->numDNSKEY == 0) { 2197a58bff56Ssthen dname_str(a->name, buf); 2198a58bff56Ssthen ssl_printf(ssl, "%s\n", buf); 2199a58bff56Ssthen } 2200a58bff56Ssthen } 2201a58bff56Ssthen } 2202a58bff56Ssthen } 2203a58bff56Ssthen 2204933707f3Ssthen /** do the status command */ 2205933707f3Ssthen static void 2206933707f3Ssthen do_status(SSL* ssl, struct worker* worker) 2207933707f3Ssthen { 2208933707f3Ssthen int i; 2209933707f3Ssthen time_t uptime; 2210933707f3Ssthen if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION)) 2211933707f3Ssthen return; 2212933707f3Ssthen if(!ssl_printf(ssl, "verbosity: %d\n", verbosity)) 2213933707f3Ssthen return; 2214933707f3Ssthen if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num)) 2215933707f3Ssthen return; 2216933707f3Ssthen if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num)) 2217933707f3Ssthen return; 2218933707f3Ssthen for(i=0; i<worker->daemon->mods.num; i++) { 2219933707f3Ssthen if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name)) 2220933707f3Ssthen return; 2221933707f3Ssthen } 2222933707f3Ssthen if(!ssl_printf(ssl, " ]\n")) 2223933707f3Ssthen return; 2224933707f3Ssthen uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec; 22250b68ff31Ssthen if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime)) 2226933707f3Ssthen return; 2227e10d3884Sbrad if(!ssl_printf(ssl, "options:%s%s\n" , 2228e10d3884Sbrad (worker->daemon->reuseport?" reuseport":""), 2229e10d3884Sbrad (worker->daemon->rc->accept_list?" control(ssl)":""))) 2230e10d3884Sbrad return; 2231933707f3Ssthen if(!ssl_printf(ssl, "unbound (pid %d) is running...\n", 2232933707f3Ssthen (int)getpid())) 2233933707f3Ssthen return; 2234933707f3Ssthen } 2235933707f3Ssthen 2236933707f3Ssthen /** get age for the mesh state */ 2237933707f3Ssthen static void 2238933707f3Ssthen get_mesh_age(struct mesh_state* m, char* buf, size_t len, 2239933707f3Ssthen struct module_env* env) 2240933707f3Ssthen { 2241933707f3Ssthen if(m->reply_list) { 2242933707f3Ssthen struct timeval d; 2243933707f3Ssthen struct mesh_reply* r = m->reply_list; 2244933707f3Ssthen /* last reply is the oldest */ 2245933707f3Ssthen while(r && r->next) 2246933707f3Ssthen r = r->next; 2247933707f3Ssthen timeval_subtract(&d, env->now_tv, &r->start_time); 22480b68ff31Ssthen snprintf(buf, len, ARG_LL "d.%6.6d", 22490b68ff31Ssthen (long long)d.tv_sec, (int)d.tv_usec); 2250933707f3Ssthen } else { 2251933707f3Ssthen snprintf(buf, len, "-"); 2252933707f3Ssthen } 2253933707f3Ssthen } 2254933707f3Ssthen 2255933707f3Ssthen /** get status of a mesh state */ 2256933707f3Ssthen static void 2257933707f3Ssthen get_mesh_status(struct mesh_area* mesh, struct mesh_state* m, 2258933707f3Ssthen char* buf, size_t len) 2259933707f3Ssthen { 2260933707f3Ssthen enum module_ext_state s = m->s.ext_state[m->s.curmod]; 2261933707f3Ssthen const char *modname = mesh->mods.mod[m->s.curmod]->name; 2262933707f3Ssthen size_t l; 2263933707f3Ssthen if(strcmp(modname, "iterator") == 0 && s == module_wait_reply && 2264933707f3Ssthen m->s.minfo[m->s.curmod]) { 2265933707f3Ssthen /* break into iterator to find out who its waiting for */ 2266933707f3Ssthen struct iter_qstate* qstate = (struct iter_qstate*) 2267933707f3Ssthen m->s.minfo[m->s.curmod]; 2268933707f3Ssthen struct outbound_list* ol = &qstate->outlist; 2269933707f3Ssthen struct outbound_entry* e; 2270933707f3Ssthen snprintf(buf, len, "%s wait for", modname); 2271933707f3Ssthen l = strlen(buf); 2272933707f3Ssthen buf += l; len -= l; 2273933707f3Ssthen if(ol->first == NULL) 2274933707f3Ssthen snprintf(buf, len, " (empty_list)"); 2275933707f3Ssthen for(e = ol->first; e; e = e->next) { 2276933707f3Ssthen snprintf(buf, len, " "); 2277933707f3Ssthen l = strlen(buf); 2278933707f3Ssthen buf += l; len -= l; 2279933707f3Ssthen addr_to_str(&e->qsent->addr, e->qsent->addrlen, 2280933707f3Ssthen buf, len); 2281933707f3Ssthen l = strlen(buf); 2282933707f3Ssthen buf += l; len -= l; 2283933707f3Ssthen } 2284933707f3Ssthen } else if(s == module_wait_subquery) { 2285933707f3Ssthen /* look in subs from mesh state to see what */ 2286933707f3Ssthen char nm[257]; 2287933707f3Ssthen struct mesh_state_ref* sub; 2288933707f3Ssthen snprintf(buf, len, "%s wants", modname); 2289933707f3Ssthen l = strlen(buf); 2290933707f3Ssthen buf += l; len -= l; 2291933707f3Ssthen if(m->sub_set.count == 0) 2292933707f3Ssthen snprintf(buf, len, " (empty_list)"); 2293933707f3Ssthen RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) { 22940b68ff31Ssthen char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype); 22950b68ff31Ssthen char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass); 2296933707f3Ssthen dname_str(sub->s->s.qinfo.qname, nm); 22970b68ff31Ssthen snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"), 22980b68ff31Ssthen (c?c:"CLASS??"), nm); 2299933707f3Ssthen l = strlen(buf); 2300933707f3Ssthen buf += l; len -= l; 2301933707f3Ssthen free(t); 2302933707f3Ssthen free(c); 2303933707f3Ssthen } 2304933707f3Ssthen } else { 2305933707f3Ssthen snprintf(buf, len, "%s is %s", modname, strextstate(s)); 2306933707f3Ssthen } 2307933707f3Ssthen } 2308933707f3Ssthen 2309933707f3Ssthen /** do the dump_requestlist command */ 2310933707f3Ssthen static void 2311933707f3Ssthen do_dump_requestlist(SSL* ssl, struct worker* worker) 2312933707f3Ssthen { 2313933707f3Ssthen struct mesh_area* mesh; 2314933707f3Ssthen struct mesh_state* m; 2315933707f3Ssthen int num = 0; 2316933707f3Ssthen char buf[257]; 2317933707f3Ssthen char timebuf[32]; 2318933707f3Ssthen char statbuf[10240]; 2319933707f3Ssthen if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num)) 2320933707f3Ssthen return; 2321933707f3Ssthen if(!ssl_printf(ssl, "# type cl name seconds module status\n")) 2322933707f3Ssthen return; 2323933707f3Ssthen /* show worker mesh contents */ 2324933707f3Ssthen mesh = worker->env.mesh; 2325933707f3Ssthen if(!mesh) return; 2326933707f3Ssthen RBTREE_FOR(m, struct mesh_state*, &mesh->all) { 23270b68ff31Ssthen char* t = sldns_wire2str_type(m->s.qinfo.qtype); 23280b68ff31Ssthen char* c = sldns_wire2str_class(m->s.qinfo.qclass); 2329933707f3Ssthen dname_str(m->s.qinfo.qname, buf); 2330933707f3Ssthen get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env); 2331933707f3Ssthen get_mesh_status(mesh, m, statbuf, sizeof(statbuf)); 2332933707f3Ssthen if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n", 23330b68ff31Ssthen num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf, 23340b68ff31Ssthen statbuf)) { 2335933707f3Ssthen free(t); 2336933707f3Ssthen free(c); 2337933707f3Ssthen return; 2338933707f3Ssthen } 2339933707f3Ssthen num++; 2340933707f3Ssthen free(t); 2341933707f3Ssthen free(c); 2342933707f3Ssthen } 2343933707f3Ssthen } 2344933707f3Ssthen 2345933707f3Ssthen /** structure for argument data for dump infra host */ 2346933707f3Ssthen struct infra_arg { 2347933707f3Ssthen /** the infra cache */ 2348933707f3Ssthen struct infra_cache* infra; 2349933707f3Ssthen /** the SSL connection */ 2350933707f3Ssthen SSL* ssl; 2351933707f3Ssthen /** the time now */ 2352e9c7b4efSsthen time_t now; 2353e10d3884Sbrad /** ssl failure? stop writing and skip the rest. If the tcp 2354e10d3884Sbrad * connection is broken, and writes fail, we then stop writing. */ 2355e10d3884Sbrad int ssl_failed; 2356933707f3Ssthen }; 2357933707f3Ssthen 2358933707f3Ssthen /** callback for every host element in the infra cache */ 2359933707f3Ssthen static void 2360933707f3Ssthen dump_infra_host(struct lruhash_entry* e, void* arg) 2361933707f3Ssthen { 2362933707f3Ssthen struct infra_arg* a = (struct infra_arg*)arg; 2363933707f3Ssthen struct infra_key* k = (struct infra_key*)e->key; 2364933707f3Ssthen struct infra_data* d = (struct infra_data*)e->data; 2365933707f3Ssthen char ip_str[1024]; 2366933707f3Ssthen char name[257]; 2367*7191de28Ssthen int port; 2368e10d3884Sbrad if(a->ssl_failed) 2369e10d3884Sbrad return; 2370933707f3Ssthen addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str)); 2371933707f3Ssthen dname_str(k->zonename, name); 2372*7191de28Ssthen port = (int)ntohs(((struct sockaddr_in*)&k->addr)->sin_port); 2373*7191de28Ssthen if(port != UNBOUND_DNS_PORT) { 2374*7191de28Ssthen snprintf(ip_str+strlen(ip_str), sizeof(ip_str)-strlen(ip_str), 2375*7191de28Ssthen "@%d", port); 2376*7191de28Ssthen } 2377933707f3Ssthen /* skip expired stuff (only backed off) */ 2378933707f3Ssthen if(d->ttl < a->now) { 2379933707f3Ssthen if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) { 2380933707f3Ssthen if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str, 2381e10d3884Sbrad name, d->rtt.rto)) { 2382e10d3884Sbrad a->ssl_failed = 1; 2383e10d3884Sbrad return; 2384e10d3884Sbrad } 2385933707f3Ssthen } 2386933707f3Ssthen return; 2387933707f3Ssthen } 2388e10d3884Sbrad if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d " 2389163a4143Ssthen "tA %d tAAAA %d tother %d " 2390933707f3Ssthen "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d " 2391e10d3884Sbrad "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now), 2392933707f3Ssthen d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto, 2393163a4143Ssthen d->timeout_A, d->timeout_AAAA, d->timeout_other, 2394933707f3Ssthen (int)d->edns_lame_known, (int)d->edns_version, 2395a58bff56Ssthen (int)(a->now<d->probedelay?(d->probedelay - a->now):0), 2396933707f3Ssthen (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A, 2397e10d3884Sbrad (int)d->lame_other)) { 2398e10d3884Sbrad a->ssl_failed = 1; 2399933707f3Ssthen return; 2400933707f3Ssthen } 2401e10d3884Sbrad } 2402933707f3Ssthen 2403933707f3Ssthen /** do the dump_infra command */ 2404933707f3Ssthen static void 2405933707f3Ssthen do_dump_infra(SSL* ssl, struct worker* worker) 2406933707f3Ssthen { 2407933707f3Ssthen struct infra_arg arg; 2408933707f3Ssthen arg.infra = worker->env.infra_cache; 2409933707f3Ssthen arg.ssl = ssl; 2410933707f3Ssthen arg.now = *worker->env.now; 2411e10d3884Sbrad arg.ssl_failed = 0; 2412933707f3Ssthen slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg); 2413933707f3Ssthen } 2414933707f3Ssthen 2415933707f3Ssthen /** do the log_reopen command */ 2416933707f3Ssthen static void 2417933707f3Ssthen do_log_reopen(SSL* ssl, struct worker* worker) 2418933707f3Ssthen { 2419933707f3Ssthen struct config_file* cfg = worker->env.cfg; 2420933707f3Ssthen send_ok(ssl); 2421933707f3Ssthen log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir); 2422933707f3Ssthen } 2423933707f3Ssthen 2424933707f3Ssthen /** do the set_option command */ 2425933707f3Ssthen static void 2426933707f3Ssthen do_set_option(SSL* ssl, struct worker* worker, char* arg) 2427933707f3Ssthen { 2428933707f3Ssthen char* arg2; 2429933707f3Ssthen if(!find_arg2(ssl, arg, &arg2)) 2430933707f3Ssthen return; 2431933707f3Ssthen if(!config_set_option(worker->env.cfg, arg, arg2)) { 2432933707f3Ssthen (void)ssl_printf(ssl, "error setting option\n"); 2433933707f3Ssthen return; 2434933707f3Ssthen } 243577079be7Ssthen /* effectuate some arguments */ 243677079be7Ssthen if(strcmp(arg, "val-override-date:") == 0) { 243777079be7Ssthen int m = modstack_find(&worker->env.mesh->mods, "validator"); 243877079be7Ssthen struct val_env* val_env = NULL; 243977079be7Ssthen if(m != -1) val_env = (struct val_env*)worker->env.modinfo[m]; 244077079be7Ssthen if(val_env) 244177079be7Ssthen val_env->date_override = worker->env.cfg->val_date_override; 244277079be7Ssthen } 2443933707f3Ssthen send_ok(ssl); 2444933707f3Ssthen } 2445933707f3Ssthen 2446933707f3Ssthen /* routine to printout option values over SSL */ 2447933707f3Ssthen void remote_get_opt_ssl(char* line, void* arg) 2448933707f3Ssthen { 2449933707f3Ssthen SSL* ssl = (SSL*)arg; 2450933707f3Ssthen (void)ssl_printf(ssl, "%s\n", line); 2451933707f3Ssthen } 2452933707f3Ssthen 2453933707f3Ssthen /** do the get_option command */ 2454933707f3Ssthen static void 2455933707f3Ssthen do_get_option(SSL* ssl, struct worker* worker, char* arg) 2456933707f3Ssthen { 2457933707f3Ssthen int r; 2458933707f3Ssthen r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl); 2459933707f3Ssthen if(!r) { 2460933707f3Ssthen (void)ssl_printf(ssl, "error unknown option\n"); 2461933707f3Ssthen return; 2462933707f3Ssthen } 2463933707f3Ssthen } 2464933707f3Ssthen 2465933707f3Ssthen /** do the list_forwards command */ 2466933707f3Ssthen static void 2467933707f3Ssthen do_list_forwards(SSL* ssl, struct worker* worker) 2468933707f3Ssthen { 2469933707f3Ssthen /* since its a per-worker structure no locks needed */ 2470933707f3Ssthen struct iter_forwards* fwds = worker->env.fwds; 2471933707f3Ssthen struct iter_forward_zone* z; 2472e10d3884Sbrad struct trust_anchor* a; 2473e10d3884Sbrad int insecure; 2474933707f3Ssthen RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) { 2475933707f3Ssthen if(!z->dp) continue; /* skip empty marker for stub */ 2476e10d3884Sbrad 2477e10d3884Sbrad /* see if it is insecure */ 2478e10d3884Sbrad insecure = 0; 2479e10d3884Sbrad if(worker->env.anchors && 2480e10d3884Sbrad (a=anchor_find(worker->env.anchors, z->name, 2481e10d3884Sbrad z->namelabs, z->namelen, z->dclass))) { 2482e10d3884Sbrad if(!a->keylist && !a->numDS && !a->numDNSKEY) 2483e10d3884Sbrad insecure = 1; 2484e10d3884Sbrad lock_basic_unlock(&a->lock); 2485e10d3884Sbrad } 2486e10d3884Sbrad 2487e10d3884Sbrad if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"), 2488e10d3884Sbrad z->name, z->dclass, z->dp)) 2489933707f3Ssthen return; 2490933707f3Ssthen } 2491933707f3Ssthen } 2492933707f3Ssthen 2493933707f3Ssthen /** do the list_stubs command */ 2494933707f3Ssthen static void 2495933707f3Ssthen do_list_stubs(SSL* ssl, struct worker* worker) 2496933707f3Ssthen { 2497933707f3Ssthen struct iter_hints_stub* z; 2498e10d3884Sbrad struct trust_anchor* a; 2499e10d3884Sbrad int insecure; 2500e10d3884Sbrad char str[32]; 2501163a4143Ssthen RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) { 2502e10d3884Sbrad 2503e10d3884Sbrad /* see if it is insecure */ 2504e10d3884Sbrad insecure = 0; 2505e10d3884Sbrad if(worker->env.anchors && 2506e10d3884Sbrad (a=anchor_find(worker->env.anchors, z->node.name, 2507e10d3884Sbrad z->node.labs, z->node.len, z->node.dclass))) { 2508e10d3884Sbrad if(!a->keylist && !a->numDS && !a->numDNSKEY) 2509e10d3884Sbrad insecure = 1; 2510e10d3884Sbrad lock_basic_unlock(&a->lock); 2511e10d3884Sbrad } 2512e10d3884Sbrad 2513e10d3884Sbrad snprintf(str, sizeof(str), "stub %sprime%s", 2514e10d3884Sbrad (z->noprime?"no":""), (insecure?" +i":"")); 2515e10d3884Sbrad if(!ssl_print_name_dp(ssl, str, z->node.name, 2516933707f3Ssthen z->node.dclass, z->dp)) 2517933707f3Ssthen return; 2518933707f3Ssthen } 2519933707f3Ssthen } 2520933707f3Ssthen 2521933707f3Ssthen /** do the list_local_zones command */ 2522933707f3Ssthen static void 252377079be7Ssthen do_list_local_zones(SSL* ssl, struct local_zones* zones) 2524933707f3Ssthen { 2525933707f3Ssthen struct local_zone* z; 2526933707f3Ssthen char buf[257]; 25270b68ff31Ssthen lock_rw_rdlock(&zones->lock); 2528933707f3Ssthen RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2529933707f3Ssthen lock_rw_rdlock(&z->lock); 2530933707f3Ssthen dname_str(z->name, buf); 2531e10d3884Sbrad if(!ssl_printf(ssl, "%s %s\n", buf, 2532e10d3884Sbrad local_zone_type2str(z->type))) { 2533e10d3884Sbrad /* failure to print */ 2534e10d3884Sbrad lock_rw_unlock(&z->lock); 2535e10d3884Sbrad lock_rw_unlock(&zones->lock); 2536e10d3884Sbrad return; 2537e10d3884Sbrad } 2538933707f3Ssthen lock_rw_unlock(&z->lock); 2539933707f3Ssthen } 25400b68ff31Ssthen lock_rw_unlock(&zones->lock); 2541933707f3Ssthen } 2542933707f3Ssthen 2543933707f3Ssthen /** do the list_local_data command */ 2544933707f3Ssthen static void 254577079be7Ssthen do_list_local_data(SSL* ssl, struct worker* worker, struct local_zones* zones) 2546933707f3Ssthen { 2547933707f3Ssthen struct local_zone* z; 2548933707f3Ssthen struct local_data* d; 2549933707f3Ssthen struct local_rrset* p; 25500b68ff31Ssthen char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer); 25510b68ff31Ssthen size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer); 25520b68ff31Ssthen lock_rw_rdlock(&zones->lock); 2553933707f3Ssthen RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2554933707f3Ssthen lock_rw_rdlock(&z->lock); 2555933707f3Ssthen RBTREE_FOR(d, struct local_data*, &z->data) { 2556933707f3Ssthen for(p = d->rrsets; p; p = p->next) { 25570b68ff31Ssthen struct packed_rrset_data* d = 25580b68ff31Ssthen (struct packed_rrset_data*)p->rrset->entry.data; 25590b68ff31Ssthen size_t i; 25600b68ff31Ssthen for(i=0; i<d->count + d->rrsig_count; i++) { 25610b68ff31Ssthen if(!packed_rr_to_string(p->rrset, i, 25620b68ff31Ssthen 0, s, slen)) { 2563a961b961Ssthen if(!ssl_printf(ssl, "BADRR\n")) { 2564a961b961Ssthen lock_rw_unlock(&z->lock); 2565a961b961Ssthen lock_rw_unlock(&zones->lock); 25660b68ff31Ssthen return; 25670b68ff31Ssthen } 2568a961b961Ssthen } 2569a961b961Ssthen if(!ssl_printf(ssl, "%s\n", s)) { 2570a961b961Ssthen lock_rw_unlock(&z->lock); 2571a961b961Ssthen lock_rw_unlock(&zones->lock); 25720b68ff31Ssthen return; 25730b68ff31Ssthen } 2574933707f3Ssthen } 2575933707f3Ssthen } 2576a961b961Ssthen } 2577933707f3Ssthen lock_rw_unlock(&z->lock); 2578933707f3Ssthen } 25790b68ff31Ssthen lock_rw_unlock(&zones->lock); 2580933707f3Ssthen } 2581933707f3Ssthen 258277079be7Ssthen /** do the view_list_local_zones command */ 258377079be7Ssthen static void 258477079be7Ssthen do_view_list_local_zones(SSL* ssl, struct worker* worker, char* arg) 258577079be7Ssthen { 258677079be7Ssthen struct view* v = views_find_view(worker->daemon->views, 258777079be7Ssthen arg, 0 /* get read lock*/); 258877079be7Ssthen if(!v) { 258977079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 259077079be7Ssthen return; 259177079be7Ssthen } 25922be9e038Ssthen if(v->local_zones) { 259377079be7Ssthen do_list_local_zones(ssl, v->local_zones); 25942be9e038Ssthen } 259577079be7Ssthen lock_rw_unlock(&v->lock); 259677079be7Ssthen } 259777079be7Ssthen 259877079be7Ssthen /** do the view_list_local_data command */ 259977079be7Ssthen static void 260077079be7Ssthen do_view_list_local_data(SSL* ssl, struct worker* worker, char* arg) 260177079be7Ssthen { 260277079be7Ssthen struct view* v = views_find_view(worker->daemon->views, 260377079be7Ssthen arg, 0 /* get read lock*/); 260477079be7Ssthen if(!v) { 260577079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 260677079be7Ssthen return; 260777079be7Ssthen } 26082be9e038Ssthen if(v->local_zones) { 260977079be7Ssthen do_list_local_data(ssl, worker, v->local_zones); 26102be9e038Ssthen } 261177079be7Ssthen lock_rw_unlock(&v->lock); 261277079be7Ssthen } 261377079be7Ssthen 2614a58bff56Ssthen /** struct for user arg ratelimit list */ 2615a58bff56Ssthen struct ratelimit_list_arg { 2616a58bff56Ssthen /** the infra cache */ 2617a58bff56Ssthen struct infra_cache* infra; 2618a58bff56Ssthen /** the SSL to print to */ 2619a58bff56Ssthen SSL* ssl; 2620a58bff56Ssthen /** all or only ratelimited */ 2621a58bff56Ssthen int all; 2622a58bff56Ssthen /** current time */ 2623a58bff56Ssthen time_t now; 2624a58bff56Ssthen }; 2625a58bff56Ssthen 262677079be7Ssthen #define ip_ratelimit_list_arg ratelimit_list_arg 262777079be7Ssthen 2628a58bff56Ssthen /** list items in the ratelimit table */ 2629a58bff56Ssthen static void 2630a58bff56Ssthen rate_list(struct lruhash_entry* e, void* arg) 2631a58bff56Ssthen { 2632a58bff56Ssthen struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg; 2633a58bff56Ssthen struct rate_key* k = (struct rate_key*)e->key; 2634a58bff56Ssthen struct rate_data* d = (struct rate_data*)e->data; 2635a58bff56Ssthen char buf[257]; 2636a58bff56Ssthen int lim = infra_find_ratelimit(a->infra, k->name, k->namelen); 2637a58bff56Ssthen int max = infra_rate_max(d, a->now); 2638a58bff56Ssthen if(a->all == 0) { 2639a58bff56Ssthen if(max < lim) 2640a58bff56Ssthen return; 2641a58bff56Ssthen } 2642a58bff56Ssthen dname_str(k->name, buf); 2643a58bff56Ssthen ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim); 2644a58bff56Ssthen } 2645a58bff56Ssthen 264677079be7Ssthen /** list items in the ip_ratelimit table */ 264777079be7Ssthen static void 264877079be7Ssthen ip_rate_list(struct lruhash_entry* e, void* arg) 264977079be7Ssthen { 265077079be7Ssthen char ip[128]; 265177079be7Ssthen struct ip_ratelimit_list_arg* a = (struct ip_ratelimit_list_arg*)arg; 265277079be7Ssthen struct ip_rate_key* k = (struct ip_rate_key*)e->key; 265377079be7Ssthen struct ip_rate_data* d = (struct ip_rate_data*)e->data; 265477079be7Ssthen int lim = infra_ip_ratelimit; 265577079be7Ssthen int max = infra_rate_max(d, a->now); 265677079be7Ssthen if(a->all == 0) { 265777079be7Ssthen if(max < lim) 265877079be7Ssthen return; 265977079be7Ssthen } 266077079be7Ssthen addr_to_str(&k->addr, k->addrlen, ip, sizeof(ip)); 266177079be7Ssthen ssl_printf(a->ssl, "%s %d limit %d\n", ip, max, lim); 266277079be7Ssthen } 266377079be7Ssthen 2664a58bff56Ssthen /** do the ratelimit_list command */ 2665a58bff56Ssthen static void 2666a58bff56Ssthen do_ratelimit_list(SSL* ssl, struct worker* worker, char* arg) 2667a58bff56Ssthen { 2668a58bff56Ssthen struct ratelimit_list_arg a; 2669a58bff56Ssthen a.all = 0; 2670a58bff56Ssthen a.infra = worker->env.infra_cache; 2671a58bff56Ssthen a.now = *worker->env.now; 2672a58bff56Ssthen a.ssl = ssl; 2673a58bff56Ssthen arg = skipwhite(arg); 2674a58bff56Ssthen if(strcmp(arg, "+a") == 0) 2675a58bff56Ssthen a.all = 1; 2676a58bff56Ssthen if(a.infra->domain_rates==NULL || 2677a58bff56Ssthen (a.all == 0 && infra_dp_ratelimit == 0)) 2678a58bff56Ssthen return; 2679a58bff56Ssthen slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a); 2680a58bff56Ssthen } 2681a58bff56Ssthen 268277079be7Ssthen /** do the ip_ratelimit_list command */ 268377079be7Ssthen static void 268477079be7Ssthen do_ip_ratelimit_list(SSL* ssl, struct worker* worker, char* arg) 268577079be7Ssthen { 268677079be7Ssthen struct ip_ratelimit_list_arg a; 268777079be7Ssthen a.all = 0; 268877079be7Ssthen a.infra = worker->env.infra_cache; 268977079be7Ssthen a.now = *worker->env.now; 269077079be7Ssthen a.ssl = ssl; 269177079be7Ssthen arg = skipwhite(arg); 269277079be7Ssthen if(strcmp(arg, "+a") == 0) 269377079be7Ssthen a.all = 1; 269477079be7Ssthen if(a.infra->client_ip_rates==NULL || 269577079be7Ssthen (a.all == 0 && infra_ip_ratelimit == 0)) 269677079be7Ssthen return; 269777079be7Ssthen slabhash_traverse(a.infra->client_ip_rates, 0, ip_rate_list, &a); 269877079be7Ssthen } 269977079be7Ssthen 2700933707f3Ssthen /** tell other processes to execute the command */ 2701933707f3Ssthen static void 2702933707f3Ssthen distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd) 2703933707f3Ssthen { 2704933707f3Ssthen int i; 2705933707f3Ssthen if(!cmd || !ssl) 2706933707f3Ssthen return; 2707933707f3Ssthen /* skip i=0 which is me */ 2708933707f3Ssthen for(i=1; i<rc->worker->daemon->num; i++) { 2709933707f3Ssthen worker_send_cmd(rc->worker->daemon->workers[i], 2710933707f3Ssthen worker_cmd_remote); 2711933707f3Ssthen if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd, 2712933707f3Ssthen (uint8_t*)cmd, strlen(cmd)+1, 0)) { 2713933707f3Ssthen ssl_printf(ssl, "error could not distribute cmd\n"); 2714933707f3Ssthen return; 2715933707f3Ssthen } 2716933707f3Ssthen } 2717933707f3Ssthen } 2718933707f3Ssthen 2719933707f3Ssthen /** check for name with end-of-string, space or tab after it */ 2720933707f3Ssthen static int 2721933707f3Ssthen cmdcmp(char* p, const char* cmd, size_t len) 2722933707f3Ssthen { 2723933707f3Ssthen return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t'); 2724933707f3Ssthen } 2725933707f3Ssthen 2726933707f3Ssthen /** execute a remote control command */ 2727933707f3Ssthen static void 2728933707f3Ssthen execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd, 2729933707f3Ssthen struct worker* worker) 2730933707f3Ssthen { 2731933707f3Ssthen char* p = skipwhite(cmd); 2732933707f3Ssthen /* compare command */ 2733933707f3Ssthen if(cmdcmp(p, "stop", 4)) { 2734933707f3Ssthen do_stop(ssl, rc); 2735933707f3Ssthen return; 2736933707f3Ssthen } else if(cmdcmp(p, "reload", 6)) { 2737933707f3Ssthen do_reload(ssl, rc); 2738933707f3Ssthen return; 2739933707f3Ssthen } else if(cmdcmp(p, "stats_noreset", 13)) { 2740933707f3Ssthen do_stats(ssl, rc, 0); 2741933707f3Ssthen return; 2742933707f3Ssthen } else if(cmdcmp(p, "stats", 5)) { 2743933707f3Ssthen do_stats(ssl, rc, 1); 2744933707f3Ssthen return; 2745933707f3Ssthen } else if(cmdcmp(p, "status", 6)) { 2746933707f3Ssthen do_status(ssl, worker); 2747933707f3Ssthen return; 2748933707f3Ssthen } else if(cmdcmp(p, "dump_cache", 10)) { 2749933707f3Ssthen (void)dump_cache(ssl, worker); 2750933707f3Ssthen return; 2751933707f3Ssthen } else if(cmdcmp(p, "load_cache", 10)) { 2752933707f3Ssthen if(load_cache(ssl, worker)) send_ok(ssl); 2753933707f3Ssthen return; 2754933707f3Ssthen } else if(cmdcmp(p, "list_forwards", 13)) { 2755933707f3Ssthen do_list_forwards(ssl, worker); 2756933707f3Ssthen return; 2757933707f3Ssthen } else if(cmdcmp(p, "list_stubs", 10)) { 2758933707f3Ssthen do_list_stubs(ssl, worker); 2759933707f3Ssthen return; 2760a58bff56Ssthen } else if(cmdcmp(p, "list_insecure", 13)) { 2761a58bff56Ssthen do_insecure_list(ssl, worker); 2762a58bff56Ssthen return; 2763933707f3Ssthen } else if(cmdcmp(p, "list_local_zones", 16)) { 276477079be7Ssthen do_list_local_zones(ssl, worker->daemon->local_zones); 2765933707f3Ssthen return; 2766933707f3Ssthen } else if(cmdcmp(p, "list_local_data", 15)) { 276777079be7Ssthen do_list_local_data(ssl, worker, worker->daemon->local_zones); 276877079be7Ssthen return; 276977079be7Ssthen } else if(cmdcmp(p, "view_list_local_zones", 21)) { 277077079be7Ssthen do_view_list_local_zones(ssl, worker, skipwhite(p+21)); 277177079be7Ssthen return; 277277079be7Ssthen } else if(cmdcmp(p, "view_list_local_data", 20)) { 277377079be7Ssthen do_view_list_local_data(ssl, worker, skipwhite(p+20)); 2774933707f3Ssthen return; 2775a58bff56Ssthen } else if(cmdcmp(p, "ratelimit_list", 14)) { 2776a58bff56Ssthen do_ratelimit_list(ssl, worker, p+14); 2777a58bff56Ssthen return; 277877079be7Ssthen } else if(cmdcmp(p, "ip_ratelimit_list", 17)) { 277977079be7Ssthen do_ip_ratelimit_list(ssl, worker, p+17); 278077079be7Ssthen return; 2781163a4143Ssthen } else if(cmdcmp(p, "stub_add", 8)) { 2782163a4143Ssthen /* must always distribute this cmd */ 2783163a4143Ssthen if(rc) distribute_cmd(rc, ssl, cmd); 2784163a4143Ssthen do_stub_add(ssl, worker, skipwhite(p+8)); 2785163a4143Ssthen return; 2786163a4143Ssthen } else if(cmdcmp(p, "stub_remove", 11)) { 2787163a4143Ssthen /* must always distribute this cmd */ 2788163a4143Ssthen if(rc) distribute_cmd(rc, ssl, cmd); 2789163a4143Ssthen do_stub_remove(ssl, worker, skipwhite(p+11)); 2790163a4143Ssthen return; 2791163a4143Ssthen } else if(cmdcmp(p, "forward_add", 11)) { 2792163a4143Ssthen /* must always distribute this cmd */ 2793163a4143Ssthen if(rc) distribute_cmd(rc, ssl, cmd); 2794163a4143Ssthen do_forward_add(ssl, worker, skipwhite(p+11)); 2795163a4143Ssthen return; 2796163a4143Ssthen } else if(cmdcmp(p, "forward_remove", 14)) { 2797163a4143Ssthen /* must always distribute this cmd */ 2798163a4143Ssthen if(rc) distribute_cmd(rc, ssl, cmd); 2799163a4143Ssthen do_forward_remove(ssl, worker, skipwhite(p+14)); 2800163a4143Ssthen return; 2801e9c7b4efSsthen } else if(cmdcmp(p, "insecure_add", 12)) { 2802e9c7b4efSsthen /* must always distribute this cmd */ 2803e9c7b4efSsthen if(rc) distribute_cmd(rc, ssl, cmd); 2804e9c7b4efSsthen do_insecure_add(ssl, worker, skipwhite(p+12)); 2805e9c7b4efSsthen return; 2806e9c7b4efSsthen } else if(cmdcmp(p, "insecure_remove", 15)) { 2807e9c7b4efSsthen /* must always distribute this cmd */ 2808e9c7b4efSsthen if(rc) distribute_cmd(rc, ssl, cmd); 2809e9c7b4efSsthen do_insecure_remove(ssl, worker, skipwhite(p+15)); 2810e9c7b4efSsthen return; 2811933707f3Ssthen } else if(cmdcmp(p, "forward", 7)) { 2812933707f3Ssthen /* must always distribute this cmd */ 2813933707f3Ssthen if(rc) distribute_cmd(rc, ssl, cmd); 2814933707f3Ssthen do_forward(ssl, worker, skipwhite(p+7)); 2815933707f3Ssthen return; 2816933707f3Ssthen } else if(cmdcmp(p, "flush_stats", 11)) { 2817933707f3Ssthen /* must always distribute this cmd */ 2818933707f3Ssthen if(rc) distribute_cmd(rc, ssl, cmd); 2819933707f3Ssthen do_flush_stats(ssl, worker); 2820933707f3Ssthen return; 2821933707f3Ssthen } else if(cmdcmp(p, "flush_requestlist", 17)) { 2822933707f3Ssthen /* must always distribute this cmd */ 2823933707f3Ssthen if(rc) distribute_cmd(rc, ssl, cmd); 2824933707f3Ssthen do_flush_requestlist(ssl, worker); 2825933707f3Ssthen return; 2826933707f3Ssthen } else if(cmdcmp(p, "lookup", 6)) { 2827933707f3Ssthen do_lookup(ssl, worker, skipwhite(p+6)); 2828933707f3Ssthen return; 2829933707f3Ssthen } 2830933707f3Ssthen 2831933707f3Ssthen #ifdef THREADS_DISABLED 2832933707f3Ssthen /* other processes must execute the command as well */ 2833933707f3Ssthen /* commands that should not be distributed, returned above. */ 2834933707f3Ssthen if(rc) { /* only if this thread is the master (rc) thread */ 2835933707f3Ssthen /* done before the code below, which may split the string */ 2836933707f3Ssthen distribute_cmd(rc, ssl, cmd); 2837933707f3Ssthen } 2838933707f3Ssthen #endif 2839933707f3Ssthen if(cmdcmp(p, "verbosity", 9)) { 2840933707f3Ssthen do_verbosity(ssl, skipwhite(p+9)); 2841933707f3Ssthen } else if(cmdcmp(p, "local_zone_remove", 17)) { 284277079be7Ssthen do_zone_remove(ssl, worker->daemon->local_zones, skipwhite(p+17)); 284377079be7Ssthen } else if(cmdcmp(p, "local_zones_remove", 18)) { 284477079be7Ssthen do_zones_remove(ssl, worker->daemon->local_zones); 2845933707f3Ssthen } else if(cmdcmp(p, "local_zone", 10)) { 284677079be7Ssthen do_zone_add(ssl, worker->daemon->local_zones, skipwhite(p+10)); 284777079be7Ssthen } else if(cmdcmp(p, "local_zones", 11)) { 284877079be7Ssthen do_zones_add(ssl, worker->daemon->local_zones); 2849933707f3Ssthen } else if(cmdcmp(p, "local_data_remove", 17)) { 285077079be7Ssthen do_data_remove(ssl, worker->daemon->local_zones, skipwhite(p+17)); 285177079be7Ssthen } else if(cmdcmp(p, "local_datas_remove", 18)) { 285277079be7Ssthen do_datas_remove(ssl, worker->daemon->local_zones); 2853933707f3Ssthen } else if(cmdcmp(p, "local_data", 10)) { 285477079be7Ssthen do_data_add(ssl, worker->daemon->local_zones, skipwhite(p+10)); 285577079be7Ssthen } else if(cmdcmp(p, "local_datas", 11)) { 285677079be7Ssthen do_datas_add(ssl, worker->daemon->local_zones); 285777079be7Ssthen } else if(cmdcmp(p, "view_local_zone_remove", 22)) { 285877079be7Ssthen do_view_zone_remove(ssl, worker, skipwhite(p+22)); 285977079be7Ssthen } else if(cmdcmp(p, "view_local_zone", 15)) { 286077079be7Ssthen do_view_zone_add(ssl, worker, skipwhite(p+15)); 286177079be7Ssthen } else if(cmdcmp(p, "view_local_data_remove", 22)) { 286277079be7Ssthen do_view_data_remove(ssl, worker, skipwhite(p+22)); 286377079be7Ssthen } else if(cmdcmp(p, "view_local_data", 15)) { 286477079be7Ssthen do_view_data_add(ssl, worker, skipwhite(p+15)); 2865933707f3Ssthen } else if(cmdcmp(p, "flush_zone", 10)) { 2866933707f3Ssthen do_flush_zone(ssl, worker, skipwhite(p+10)); 2867933707f3Ssthen } else if(cmdcmp(p, "flush_type", 10)) { 2868933707f3Ssthen do_flush_type(ssl, worker, skipwhite(p+10)); 2869933707f3Ssthen } else if(cmdcmp(p, "flush_infra", 11)) { 2870933707f3Ssthen do_flush_infra(ssl, worker, skipwhite(p+11)); 2871933707f3Ssthen } else if(cmdcmp(p, "flush", 5)) { 2872933707f3Ssthen do_flush_name(ssl, worker, skipwhite(p+5)); 2873933707f3Ssthen } else if(cmdcmp(p, "dump_requestlist", 16)) { 2874933707f3Ssthen do_dump_requestlist(ssl, worker); 2875933707f3Ssthen } else if(cmdcmp(p, "dump_infra", 10)) { 2876933707f3Ssthen do_dump_infra(ssl, worker); 2877933707f3Ssthen } else if(cmdcmp(p, "log_reopen", 10)) { 2878933707f3Ssthen do_log_reopen(ssl, worker); 2879933707f3Ssthen } else if(cmdcmp(p, "set_option", 10)) { 2880933707f3Ssthen do_set_option(ssl, worker, skipwhite(p+10)); 2881933707f3Ssthen } else if(cmdcmp(p, "get_option", 10)) { 2882933707f3Ssthen do_get_option(ssl, worker, skipwhite(p+10)); 2883cebdf579Ssthen } else if(cmdcmp(p, "flush_bogus", 11)) { 2884cebdf579Ssthen do_flush_bogus(ssl, worker); 2885e10d3884Sbrad } else if(cmdcmp(p, "flush_negative", 14)) { 2886e10d3884Sbrad do_flush_negative(ssl, worker); 2887933707f3Ssthen } else { 2888933707f3Ssthen (void)ssl_printf(ssl, "error unknown command '%s'\n", p); 2889933707f3Ssthen } 2890933707f3Ssthen } 2891933707f3Ssthen 2892933707f3Ssthen void 2893933707f3Ssthen daemon_remote_exec(struct worker* worker) 2894933707f3Ssthen { 2895933707f3Ssthen /* read the cmd string */ 2896933707f3Ssthen uint8_t* msg = NULL; 2897933707f3Ssthen uint32_t len = 0; 2898933707f3Ssthen if(!tube_read_msg(worker->cmd, &msg, &len, 0)) { 2899933707f3Ssthen log_err("daemon_remote_exec: tube_read_msg failed"); 2900933707f3Ssthen return; 2901933707f3Ssthen } 2902933707f3Ssthen verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg); 2903933707f3Ssthen execute_cmd(NULL, NULL, (char*)msg, worker); 2904933707f3Ssthen free(msg); 2905933707f3Ssthen } 2906933707f3Ssthen 2907933707f3Ssthen /** handle remote control request */ 2908933707f3Ssthen static void 2909933707f3Ssthen handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl) 2910933707f3Ssthen { 2911933707f3Ssthen int r; 2912933707f3Ssthen char pre[10]; 2913933707f3Ssthen char magic[7]; 2914933707f3Ssthen char buf[1024]; 2915933707f3Ssthen #ifdef USE_WINSOCK 2916933707f3Ssthen /* makes it possible to set the socket blocking again. */ 2917933707f3Ssthen /* basically removes it from winsock_event ... */ 2918933707f3Ssthen WSAEventSelect(s->c->fd, NULL, 0); 2919933707f3Ssthen #endif 2920933707f3Ssthen fd_set_block(s->c->fd); 2921933707f3Ssthen 2922933707f3Ssthen /* try to read magic UBCT[version]_space_ string */ 2923933707f3Ssthen ERR_clear_error(); 2924933707f3Ssthen if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) { 2925933707f3Ssthen if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) 2926933707f3Ssthen return; 2927933707f3Ssthen log_crypto_err("could not SSL_read"); 2928933707f3Ssthen return; 2929933707f3Ssthen } 2930933707f3Ssthen magic[6] = 0; 2931933707f3Ssthen if( r != 6 || strncmp(magic, "UBCT", 4) != 0) { 2932933707f3Ssthen verbose(VERB_QUERY, "control connection has bad magic string"); 2933933707f3Ssthen /* probably wrong tool connected, ignore it completely */ 2934933707f3Ssthen return; 2935933707f3Ssthen } 2936933707f3Ssthen 2937933707f3Ssthen /* read the command line */ 2938933707f3Ssthen if(!ssl_read_line(ssl, buf, sizeof(buf))) { 2939933707f3Ssthen return; 2940933707f3Ssthen } 2941933707f3Ssthen snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION); 2942933707f3Ssthen if(strcmp(magic, pre) != 0) { 2943933707f3Ssthen verbose(VERB_QUERY, "control connection had bad " 2944933707f3Ssthen "version %s, cmd: %s", magic, buf); 2945933707f3Ssthen ssl_printf(ssl, "error version mismatch\n"); 2946933707f3Ssthen return; 2947933707f3Ssthen } 2948933707f3Ssthen verbose(VERB_DETAIL, "control cmd: %s", buf); 2949933707f3Ssthen 2950933707f3Ssthen /* figure out what to do */ 2951933707f3Ssthen execute_cmd(rc, ssl, buf, rc->worker); 2952933707f3Ssthen } 2953933707f3Ssthen 2954933707f3Ssthen int remote_control_callback(struct comm_point* c, void* arg, int err, 2955933707f3Ssthen struct comm_reply* ATTR_UNUSED(rep)) 2956933707f3Ssthen { 2957933707f3Ssthen struct rc_state* s = (struct rc_state*)arg; 2958933707f3Ssthen struct daemon_remote* rc = s->rc; 2959933707f3Ssthen int r; 2960933707f3Ssthen if(err != NETEVENT_NOERROR) { 2961933707f3Ssthen if(err==NETEVENT_TIMEOUT) 2962933707f3Ssthen log_err("remote control timed out"); 2963933707f3Ssthen clean_point(rc, s); 2964933707f3Ssthen return 0; 2965933707f3Ssthen } 2966933707f3Ssthen /* (continue to) setup the SSL connection */ 2967933707f3Ssthen ERR_clear_error(); 2968933707f3Ssthen r = SSL_do_handshake(s->ssl); 2969933707f3Ssthen if(r != 1) { 2970933707f3Ssthen int r2 = SSL_get_error(s->ssl, r); 2971933707f3Ssthen if(r2 == SSL_ERROR_WANT_READ) { 2972933707f3Ssthen if(s->shake_state == rc_hs_read) { 2973933707f3Ssthen /* try again later */ 2974933707f3Ssthen return 0; 2975933707f3Ssthen } 2976933707f3Ssthen s->shake_state = rc_hs_read; 2977933707f3Ssthen comm_point_listen_for_rw(c, 1, 0); 2978933707f3Ssthen return 0; 2979933707f3Ssthen } else if(r2 == SSL_ERROR_WANT_WRITE) { 2980933707f3Ssthen if(s->shake_state == rc_hs_write) { 2981933707f3Ssthen /* try again later */ 2982933707f3Ssthen return 0; 2983933707f3Ssthen } 2984933707f3Ssthen s->shake_state = rc_hs_write; 2985933707f3Ssthen comm_point_listen_for_rw(c, 0, 1); 2986933707f3Ssthen return 0; 2987933707f3Ssthen } else { 2988933707f3Ssthen if(r == 0) 2989933707f3Ssthen log_err("remote control connection closed prematurely"); 2990933707f3Ssthen log_addr(1, "failed connection from", 2991933707f3Ssthen &s->c->repinfo.addr, s->c->repinfo.addrlen); 2992933707f3Ssthen log_crypto_err("remote control failed ssl"); 2993933707f3Ssthen clean_point(rc, s); 2994933707f3Ssthen return 0; 2995933707f3Ssthen } 2996933707f3Ssthen } 2997933707f3Ssthen s->shake_state = rc_none; 2998933707f3Ssthen 2999933707f3Ssthen /* once handshake has completed, check authentication */ 300031f127bbSsthen if (!rc->use_cert) { 300131f127bbSsthen verbose(VERB_ALGO, "unauthenticated remote control connection"); 300231f127bbSsthen } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) { 3003933707f3Ssthen X509* x = SSL_get_peer_certificate(s->ssl); 3004933707f3Ssthen if(!x) { 3005933707f3Ssthen verbose(VERB_DETAIL, "remote control connection " 3006933707f3Ssthen "provided no client certificate"); 3007933707f3Ssthen clean_point(rc, s); 3008933707f3Ssthen return 0; 3009933707f3Ssthen } 3010933707f3Ssthen verbose(VERB_ALGO, "remote control connection authenticated"); 3011933707f3Ssthen X509_free(x); 3012933707f3Ssthen } else { 3013933707f3Ssthen verbose(VERB_DETAIL, "remote control connection failed to " 3014933707f3Ssthen "authenticate with client certificate"); 3015933707f3Ssthen clean_point(rc, s); 3016933707f3Ssthen return 0; 3017933707f3Ssthen } 3018933707f3Ssthen 3019933707f3Ssthen /* if OK start to actually handle the request */ 3020933707f3Ssthen handle_req(rc, s, s->ssl); 3021933707f3Ssthen 3022933707f3Ssthen verbose(VERB_ALGO, "remote control operation completed"); 3023933707f3Ssthen clean_point(rc, s); 3024933707f3Ssthen return 0; 3025933707f3Ssthen } 3026