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" 7120237c55Ssthen #include "services/authzone.h" 72eaf2578eSsthen #include "services/rpz.h" 73933707f3Ssthen #include "util/storage/slabhash.h" 74933707f3Ssthen #include "util/fptr_wlist.h" 75933707f3Ssthen #include "util/data/dname.h" 76933707f3Ssthen #include "validator/validator.h" 77933707f3Ssthen #include "validator/val_kcache.h" 78933707f3Ssthen #include "validator/val_kentry.h" 79163a4143Ssthen #include "validator/val_anchor.h" 80933707f3Ssthen #include "iterator/iterator.h" 81933707f3Ssthen #include "iterator/iter_fwd.h" 82933707f3Ssthen #include "iterator/iter_hints.h" 83933707f3Ssthen #include "iterator/iter_delegpt.h" 84933707f3Ssthen #include "services/outbound_list.h" 85933707f3Ssthen #include "services/outside_network.h" 86a58bff56Ssthen #include "sldns/str2wire.h" 87a58bff56Ssthen #include "sldns/parseutil.h" 88a58bff56Ssthen #include "sldns/wire2str.h" 89a58bff56Ssthen #include "sldns/sbuffer.h" 908b7325afSsthen #include "util/timeval_func.h" 91*98bc733bSsthen #include "util/edns.h" 92*98bc733bSsthen #ifdef USE_CACHEDB 93*98bc733bSsthen #include "cachedb/cachedb.h" 94*98bc733bSsthen #endif 95933707f3Ssthen 96933707f3Ssthen #ifdef HAVE_SYS_TYPES_H 97933707f3Ssthen # include <sys/types.h> 98933707f3Ssthen #endif 9931f127bbSsthen #ifdef HAVE_SYS_STAT_H 10031f127bbSsthen #include <sys/stat.h> 10131f127bbSsthen #endif 102933707f3Ssthen #ifdef HAVE_NETDB_H 103933707f3Ssthen #include <netdb.h> 104933707f3Ssthen #endif 105933707f3Ssthen 106933707f3Ssthen /* just for portability */ 107933707f3Ssthen #ifdef SQ 108933707f3Ssthen #undef SQ 109933707f3Ssthen #endif 110933707f3Ssthen 111933707f3Ssthen /** what to put on statistics lines between var and value, ": " or "=" */ 112933707f3Ssthen #define SQ "=" 113933707f3Ssthen 114*98bc733bSsthen /** Acceptable lengths of str lines */ 115*98bc733bSsthen #define MAX_CMD_STRLINE 1024 116*98bc733bSsthen #define MAX_STDIN_STRLINE 2048 117*98bc733bSsthen 11820237c55Ssthen static int 11920237c55Ssthen remote_setup_ctx(struct daemon_remote* rc, struct config_file* cfg) 120933707f3Ssthen { 121933707f3Ssthen char* s_cert; 122933707f3Ssthen char* s_key; 123933707f3Ssthen rc->ctx = SSL_CTX_new(SSLv23_server_method()); 124933707f3Ssthen if(!rc->ctx) { 125933707f3Ssthen log_crypto_err("could not SSL_CTX_new"); 12620237c55Ssthen return 0; 127933707f3Ssthen } 1287191de28Ssthen if(!listen_sslctx_setup(rc->ctx)) { 12920237c55Ssthen return 0; 130933707f3Ssthen } 13131f127bbSsthen 132933707f3Ssthen s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1); 133933707f3Ssthen s_key = fname_after_chroot(cfg->server_key_file, cfg, 1); 134933707f3Ssthen if(!s_cert || !s_key) { 135933707f3Ssthen log_err("out of memory in remote control fname"); 136933707f3Ssthen goto setup_error; 137933707f3Ssthen } 138933707f3Ssthen verbose(VERB_ALGO, "setup SSL certificates"); 139a961b961Ssthen if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) { 140933707f3Ssthen log_err("Error for server-cert-file: %s", s_cert); 141a961b961Ssthen log_crypto_err("Error in SSL_CTX use_certificate_chain_file"); 142933707f3Ssthen goto setup_error; 143933707f3Ssthen } 144933707f3Ssthen if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) { 145933707f3Ssthen log_err("Error for server-key-file: %s", s_key); 146933707f3Ssthen log_crypto_err("Error in SSL_CTX use_PrivateKey_file"); 147933707f3Ssthen goto setup_error; 148933707f3Ssthen } 149933707f3Ssthen if(!SSL_CTX_check_private_key(rc->ctx)) { 150933707f3Ssthen log_err("Error for server-key-file: %s", s_key); 151933707f3Ssthen log_crypto_err("Error in SSL_CTX check_private_key"); 152933707f3Ssthen goto setup_error; 153933707f3Ssthen } 1547191de28Ssthen listen_sslctx_setup_2(rc->ctx); 155933707f3Ssthen if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) { 156933707f3Ssthen log_crypto_err("Error setting up SSL_CTX verify locations"); 157933707f3Ssthen setup_error: 158933707f3Ssthen free(s_cert); 159933707f3Ssthen free(s_key); 16020237c55Ssthen return 0; 161933707f3Ssthen } 162933707f3Ssthen SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert)); 163933707f3Ssthen SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL); 164933707f3Ssthen free(s_cert); 165933707f3Ssthen free(s_key); 16620237c55Ssthen return 1; 16720237c55Ssthen } 168933707f3Ssthen 16920237c55Ssthen struct daemon_remote* 17020237c55Ssthen daemon_remote_create(struct config_file* cfg) 17120237c55Ssthen { 17220237c55Ssthen struct daemon_remote* rc = (struct daemon_remote*)calloc(1, 17320237c55Ssthen sizeof(*rc)); 17420237c55Ssthen if(!rc) { 17520237c55Ssthen log_err("out of memory in daemon_remote_create"); 17620237c55Ssthen return NULL; 17720237c55Ssthen } 17820237c55Ssthen rc->max_active = 10; 17920237c55Ssthen 18020237c55Ssthen if(!cfg->remote_control_enable) { 18120237c55Ssthen rc->ctx = NULL; 18220237c55Ssthen return rc; 18320237c55Ssthen } 18420237c55Ssthen if(options_remote_is_address(cfg) && cfg->control_use_cert) { 18520237c55Ssthen if(!remote_setup_ctx(rc, cfg)) { 18620237c55Ssthen daemon_remote_delete(rc); 18720237c55Ssthen return NULL; 18820237c55Ssthen } 18920237c55Ssthen rc->use_cert = 1; 19020237c55Ssthen } else { 19120237c55Ssthen struct config_strlist* p; 19220237c55Ssthen rc->ctx = NULL; 19320237c55Ssthen rc->use_cert = 0; 19420237c55Ssthen if(!options_remote_is_address(cfg)) 19520237c55Ssthen for(p = cfg->control_ifs.first; p; p = p->next) { 19620237c55Ssthen if(p->str && p->str[0] != '/') 19720237c55Ssthen log_warn("control-interface %s is not using TLS, but plain transfer, because first control-interface in config file is a local socket (starts with a /).", p->str); 19820237c55Ssthen } 19920237c55Ssthen } 200933707f3Ssthen return rc; 201933707f3Ssthen } 202933707f3Ssthen 203933707f3Ssthen void daemon_remote_clear(struct daemon_remote* rc) 204933707f3Ssthen { 205933707f3Ssthen struct rc_state* p, *np; 206933707f3Ssthen if(!rc) return; 207933707f3Ssthen /* but do not close the ports */ 208933707f3Ssthen listen_list_delete(rc->accept_list); 209933707f3Ssthen rc->accept_list = NULL; 210933707f3Ssthen /* do close these sockets */ 211933707f3Ssthen p = rc->busy_list; 212933707f3Ssthen while(p) { 213933707f3Ssthen np = p->next; 214933707f3Ssthen if(p->ssl) 215933707f3Ssthen SSL_free(p->ssl); 216933707f3Ssthen comm_point_delete(p->c); 217933707f3Ssthen free(p); 218933707f3Ssthen p = np; 219933707f3Ssthen } 220933707f3Ssthen rc->busy_list = NULL; 221933707f3Ssthen rc->active = 0; 222933707f3Ssthen rc->worker = NULL; 223933707f3Ssthen } 224933707f3Ssthen 225933707f3Ssthen void daemon_remote_delete(struct daemon_remote* rc) 226933707f3Ssthen { 227933707f3Ssthen if(!rc) return; 228933707f3Ssthen daemon_remote_clear(rc); 229933707f3Ssthen if(rc->ctx) { 230933707f3Ssthen SSL_CTX_free(rc->ctx); 231933707f3Ssthen } 232933707f3Ssthen free(rc); 233933707f3Ssthen } 234933707f3Ssthen 235933707f3Ssthen /** 236933707f3Ssthen * Add and open a new control port 237933707f3Ssthen * @param ip: ip str 238933707f3Ssthen * @param nr: port nr 239933707f3Ssthen * @param list: list head 240933707f3Ssthen * @param noproto_is_err: if lack of protocol support is an error. 24131f127bbSsthen * @param cfg: config with username for chown of unix-sockets. 242933707f3Ssthen * @return false on failure. 243933707f3Ssthen */ 244933707f3Ssthen static int 24531f127bbSsthen add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err, 24631f127bbSsthen struct config_file* cfg) 247933707f3Ssthen { 248933707f3Ssthen struct addrinfo hints; 249933707f3Ssthen struct addrinfo* res; 250933707f3Ssthen struct listen_port* n; 251452a1548Ssthen int noproto = 0; 252933707f3Ssthen int fd, r; 253933707f3Ssthen char port[15]; 254933707f3Ssthen snprintf(port, sizeof(port), "%d", nr); 255933707f3Ssthen port[sizeof(port)-1]=0; 256933707f3Ssthen memset(&hints, 0, sizeof(hints)); 257452a1548Ssthen log_assert(ip); 25831f127bbSsthen 25931f127bbSsthen if(ip[0] == '/') { 26031f127bbSsthen /* This looks like a local socket */ 26177079be7Ssthen fd = create_local_accept_sock(ip, &noproto, cfg->use_systemd); 26231f127bbSsthen /* 26331f127bbSsthen * Change socket ownership and permissions so users other 26431f127bbSsthen * than root can access it provided they are in the same 26531f127bbSsthen * group as the user we run as. 26631f127bbSsthen */ 26731f127bbSsthen if(fd != -1) { 26831f127bbSsthen #ifdef HAVE_CHOWN 269e21c60efSsthen chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)); 270a58bff56Ssthen if (cfg->username && cfg->username[0] && 27177079be7Ssthen cfg_uid != (uid_t)-1) { 27277079be7Ssthen if(chown(ip, cfg_uid, cfg_gid) == -1) 2737191de28Ssthen verbose(VERB_QUERY, "cannot chown %u.%u %s: %s", 27477079be7Ssthen (unsigned)cfg_uid, (unsigned)cfg_gid, 27577079be7Ssthen ip, strerror(errno)); 27677079be7Ssthen } 27731f127bbSsthen #else 27831f127bbSsthen (void)cfg; 27931f127bbSsthen #endif 28031f127bbSsthen } 28131f127bbSsthen } else { 282933707f3Ssthen hints.ai_socktype = SOCK_STREAM; 283933707f3Ssthen hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 284933707f3Ssthen if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) { 285933707f3Ssthen #ifdef USE_WINSOCK 286933707f3Ssthen if(!noproto_is_err && r == EAI_NONAME) { 287933707f3Ssthen /* tried to lookup the address as name */ 288933707f3Ssthen return 1; /* return success, but do nothing */ 289933707f3Ssthen } 290933707f3Ssthen #endif /* USE_WINSOCK */ 291933707f3Ssthen log_err("control interface %s:%s getaddrinfo: %s %s", 292933707f3Ssthen ip?ip:"default", port, gai_strerror(r), 293933707f3Ssthen #ifdef EAI_SYSTEM 294933707f3Ssthen r==EAI_SYSTEM?(char*)strerror(errno):"" 295933707f3Ssthen #else 296933707f3Ssthen "" 297933707f3Ssthen #endif 298933707f3Ssthen ); 299933707f3Ssthen return 0; 300933707f3Ssthen } 301933707f3Ssthen 302933707f3Ssthen /* open fd */ 303a58bff56Ssthen fd = create_tcp_accept_sock(res, 1, &noproto, 0, 3042c144df0Ssthen cfg->ip_transparent, 0, 0, cfg->ip_freebind, 3052c144df0Ssthen cfg->use_systemd, cfg->ip_dscp); 306933707f3Ssthen freeaddrinfo(res); 30731f127bbSsthen } 30831f127bbSsthen 309933707f3Ssthen if(fd == -1 && noproto) { 310933707f3Ssthen if(!noproto_is_err) 311933707f3Ssthen return 1; /* return success, but do nothing */ 312933707f3Ssthen log_err("cannot open control interface %s %d : " 313933707f3Ssthen "protocol not supported", ip, nr); 314933707f3Ssthen return 0; 315933707f3Ssthen } 316933707f3Ssthen if(fd == -1) { 317933707f3Ssthen log_err("cannot open control interface %s %d", ip, nr); 318933707f3Ssthen return 0; 319933707f3Ssthen } 320933707f3Ssthen 321933707f3Ssthen /* alloc */ 322933707f3Ssthen n = (struct listen_port*)calloc(1, sizeof(*n)); 323933707f3Ssthen if(!n) { 3242c144df0Ssthen sock_close(fd); 325933707f3Ssthen log_err("out of memory"); 326933707f3Ssthen return 0; 327933707f3Ssthen } 328933707f3Ssthen n->next = *list; 329933707f3Ssthen *list = n; 330933707f3Ssthen n->fd = fd; 331933707f3Ssthen return 1; 332933707f3Ssthen } 333933707f3Ssthen 334933707f3Ssthen struct listen_port* daemon_remote_open_ports(struct config_file* cfg) 335933707f3Ssthen { 336933707f3Ssthen struct listen_port* l = NULL; 337933707f3Ssthen log_assert(cfg->remote_control_enable && cfg->control_port); 33820237c55Ssthen if(cfg->control_ifs.first) { 339191f22c6Ssthen char** rcif = NULL; 340191f22c6Ssthen int i, num_rcif = 0; 341191f22c6Ssthen if(!resolve_interface_names(NULL, 0, cfg->control_ifs.first, 342191f22c6Ssthen &rcif, &num_rcif)) { 343191f22c6Ssthen return NULL; 344191f22c6Ssthen } 345191f22c6Ssthen for(i=0; i<num_rcif; i++) { 346191f22c6Ssthen if(!add_open(rcif[i], cfg->control_port, &l, 1, cfg)) { 347933707f3Ssthen listening_ports_free(l); 348191f22c6Ssthen config_del_strarray(rcif, num_rcif); 349933707f3Ssthen return NULL; 350933707f3Ssthen } 351933707f3Ssthen } 352191f22c6Ssthen config_del_strarray(rcif, num_rcif); 353933707f3Ssthen } else { 354933707f3Ssthen /* defaults */ 355933707f3Ssthen if(cfg->do_ip6 && 35631f127bbSsthen !add_open("::1", cfg->control_port, &l, 0, cfg)) { 357933707f3Ssthen listening_ports_free(l); 358933707f3Ssthen return NULL; 359933707f3Ssthen } 360933707f3Ssthen if(cfg->do_ip4 && 36131f127bbSsthen !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) { 362933707f3Ssthen listening_ports_free(l); 363933707f3Ssthen return NULL; 364933707f3Ssthen } 365933707f3Ssthen } 366933707f3Ssthen return l; 367933707f3Ssthen } 368933707f3Ssthen 369933707f3Ssthen /** open accept commpoint */ 370933707f3Ssthen static int 371933707f3Ssthen accept_open(struct daemon_remote* rc, int fd) 372933707f3Ssthen { 373933707f3Ssthen struct listen_list* n = (struct listen_list*)malloc(sizeof(*n)); 374933707f3Ssthen if(!n) { 375933707f3Ssthen log_err("out of memory"); 376933707f3Ssthen return 0; 377933707f3Ssthen } 378933707f3Ssthen n->next = rc->accept_list; 379933707f3Ssthen rc->accept_list = n; 380933707f3Ssthen /* open commpt */ 381933707f3Ssthen n->com = comm_point_create_raw(rc->worker->base, fd, 0, 382933707f3Ssthen &remote_accept_callback, rc); 383933707f3Ssthen if(!n->com) 384933707f3Ssthen return 0; 385933707f3Ssthen /* keep this port open, its fd is kept in the rc portlist */ 386933707f3Ssthen n->com->do_not_close = 1; 387933707f3Ssthen return 1; 388933707f3Ssthen } 389933707f3Ssthen 390933707f3Ssthen int daemon_remote_open_accept(struct daemon_remote* rc, 391933707f3Ssthen struct listen_port* ports, struct worker* worker) 392933707f3Ssthen { 393933707f3Ssthen struct listen_port* p; 394933707f3Ssthen rc->worker = worker; 395933707f3Ssthen for(p = ports; p; p = p->next) { 396933707f3Ssthen if(!accept_open(rc, p->fd)) { 397933707f3Ssthen log_err("could not create accept comm point"); 398933707f3Ssthen return 0; 399933707f3Ssthen } 400933707f3Ssthen } 401933707f3Ssthen return 1; 402933707f3Ssthen } 403933707f3Ssthen 404af4988b1Ssthen void daemon_remote_stop_accept(struct daemon_remote* rc) 405af4988b1Ssthen { 406af4988b1Ssthen struct listen_list* p; 407af4988b1Ssthen for(p=rc->accept_list; p; p=p->next) { 408af4988b1Ssthen comm_point_stop_listening(p->com); 409af4988b1Ssthen } 410af4988b1Ssthen } 411af4988b1Ssthen 412af4988b1Ssthen void daemon_remote_start_accept(struct daemon_remote* rc) 413af4988b1Ssthen { 414af4988b1Ssthen struct listen_list* p; 415af4988b1Ssthen for(p=rc->accept_list; p; p=p->next) { 416af4988b1Ssthen comm_point_start_listening(p->com, -1, -1); 417af4988b1Ssthen } 418af4988b1Ssthen } 419af4988b1Ssthen 420933707f3Ssthen int remote_accept_callback(struct comm_point* c, void* arg, int err, 421933707f3Ssthen struct comm_reply* ATTR_UNUSED(rep)) 422933707f3Ssthen { 423933707f3Ssthen struct daemon_remote* rc = (struct daemon_remote*)arg; 424933707f3Ssthen struct sockaddr_storage addr; 425933707f3Ssthen socklen_t addrlen; 426933707f3Ssthen int newfd; 427933707f3Ssthen struct rc_state* n; 428933707f3Ssthen if(err != NETEVENT_NOERROR) { 429933707f3Ssthen log_err("error %d on remote_accept_callback", err); 430933707f3Ssthen return 0; 431933707f3Ssthen } 432933707f3Ssthen /* perform the accept */ 433933707f3Ssthen newfd = comm_point_perform_accept(c, &addr, &addrlen); 434933707f3Ssthen if(newfd == -1) 435933707f3Ssthen return 0; 436933707f3Ssthen /* create new commpoint unless we are servicing already */ 437933707f3Ssthen if(rc->active >= rc->max_active) { 438933707f3Ssthen log_warn("drop incoming remote control: too many connections"); 439933707f3Ssthen close_exit: 4402c144df0Ssthen sock_close(newfd); 441933707f3Ssthen return 0; 442933707f3Ssthen } 443933707f3Ssthen 444933707f3Ssthen /* setup commpoint to service the remote control command */ 445933707f3Ssthen n = (struct rc_state*)calloc(1, sizeof(*n)); 446933707f3Ssthen if(!n) { 447933707f3Ssthen log_err("out of memory"); 448933707f3Ssthen goto close_exit; 449933707f3Ssthen } 45020237c55Ssthen n->fd = newfd; 451933707f3Ssthen /* start in reading state */ 452933707f3Ssthen n->c = comm_point_create_raw(rc->worker->base, newfd, 0, 453933707f3Ssthen &remote_control_callback, n); 454933707f3Ssthen if(!n->c) { 455933707f3Ssthen log_err("out of memory"); 456933707f3Ssthen free(n); 457933707f3Ssthen goto close_exit; 458933707f3Ssthen } 459933707f3Ssthen log_addr(VERB_QUERY, "new control connection from", &addr, addrlen); 460933707f3Ssthen n->c->do_not_close = 0; 461933707f3Ssthen comm_point_stop_listening(n->c); 462933707f3Ssthen comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT); 46345872187Ssthen memcpy(&n->c->repinfo.remote_addr, &addr, addrlen); 46445872187Ssthen n->c->repinfo.remote_addrlen = addrlen; 46520237c55Ssthen if(rc->use_cert) { 466933707f3Ssthen n->shake_state = rc_hs_read; 467933707f3Ssthen n->ssl = SSL_new(rc->ctx); 468933707f3Ssthen if(!n->ssl) { 469933707f3Ssthen log_crypto_err("could not SSL_new"); 470933707f3Ssthen comm_point_delete(n->c); 471933707f3Ssthen free(n); 472933707f3Ssthen goto close_exit; 473933707f3Ssthen } 474933707f3Ssthen SSL_set_accept_state(n->ssl); 475ebf5bb73Ssthen (void)SSL_set_mode(n->ssl, (long)SSL_MODE_AUTO_RETRY); 476933707f3Ssthen if(!SSL_set_fd(n->ssl, newfd)) { 477933707f3Ssthen log_crypto_err("could not SSL_set_fd"); 478933707f3Ssthen SSL_free(n->ssl); 479933707f3Ssthen comm_point_delete(n->c); 480933707f3Ssthen free(n); 481933707f3Ssthen goto close_exit; 482933707f3Ssthen } 48320237c55Ssthen } else { 48420237c55Ssthen n->ssl = NULL; 48520237c55Ssthen } 486933707f3Ssthen 487933707f3Ssthen n->rc = rc; 488933707f3Ssthen n->next = rc->busy_list; 489933707f3Ssthen rc->busy_list = n; 490933707f3Ssthen rc->active ++; 491933707f3Ssthen 492933707f3Ssthen /* perform the first nonblocking read already, for windows, 493933707f3Ssthen * so it can return wouldblock. could be faster too. */ 494933707f3Ssthen (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL); 495933707f3Ssthen return 0; 496933707f3Ssthen } 497933707f3Ssthen 498933707f3Ssthen /** delete from list */ 499933707f3Ssthen static void 500933707f3Ssthen state_list_remove_elem(struct rc_state** list, struct comm_point* c) 501933707f3Ssthen { 502933707f3Ssthen while(*list) { 503933707f3Ssthen if( (*list)->c == c) { 504933707f3Ssthen *list = (*list)->next; 505933707f3Ssthen return; 506933707f3Ssthen } 507933707f3Ssthen list = &(*list)->next; 508933707f3Ssthen } 509933707f3Ssthen } 510933707f3Ssthen 511933707f3Ssthen /** decrease active count and remove commpoint from busy list */ 512933707f3Ssthen static void 513933707f3Ssthen clean_point(struct daemon_remote* rc, struct rc_state* s) 514933707f3Ssthen { 515933707f3Ssthen state_list_remove_elem(&rc->busy_list, s->c); 516933707f3Ssthen rc->active --; 517933707f3Ssthen if(s->ssl) { 518933707f3Ssthen SSL_shutdown(s->ssl); 519933707f3Ssthen SSL_free(s->ssl); 520933707f3Ssthen } 521933707f3Ssthen comm_point_delete(s->c); 522933707f3Ssthen free(s); 523933707f3Ssthen } 524933707f3Ssthen 525933707f3Ssthen int 52620237c55Ssthen ssl_print_text(RES* res, const char* text) 527933707f3Ssthen { 528933707f3Ssthen int r; 52920237c55Ssthen if(!res) 530933707f3Ssthen return 0; 53120237c55Ssthen if(res->ssl) { 532933707f3Ssthen ERR_clear_error(); 53320237c55Ssthen if((r=SSL_write(res->ssl, text, (int)strlen(text))) <= 0) { 534d896b962Ssthen int r2; 535d896b962Ssthen if((r2=SSL_get_error(res->ssl, r)) == SSL_ERROR_ZERO_RETURN) { 536933707f3Ssthen verbose(VERB_QUERY, "warning, in SSL_write, peer " 537933707f3Ssthen "closed connection"); 538933707f3Ssthen return 0; 539933707f3Ssthen } 540d896b962Ssthen log_crypto_err_io("could not SSL_write", r2); 541933707f3Ssthen return 0; 542933707f3Ssthen } 54320237c55Ssthen } else { 54420237c55Ssthen size_t at = 0; 54520237c55Ssthen while(at < strlen(text)) { 54620237c55Ssthen ssize_t r = send(res->fd, text+at, strlen(text)-at, 0); 54720237c55Ssthen if(r == -1) { 54820237c55Ssthen if(errno == EAGAIN || errno == EINTR) 54920237c55Ssthen continue; 5502c144df0Ssthen log_err("could not send: %s", 5512c144df0Ssthen sock_strerror(errno)); 55220237c55Ssthen return 0; 55320237c55Ssthen } 55420237c55Ssthen at += r; 55520237c55Ssthen } 55620237c55Ssthen } 557933707f3Ssthen return 1; 558933707f3Ssthen } 559933707f3Ssthen 560933707f3Ssthen /** print text over the ssl connection */ 561933707f3Ssthen static int 56220237c55Ssthen ssl_print_vmsg(RES* ssl, const char* format, va_list args) 563933707f3Ssthen { 564f46c52bfSsthen char msg[65535]; 565933707f3Ssthen vsnprintf(msg, sizeof(msg), format, args); 566933707f3Ssthen return ssl_print_text(ssl, msg); 567933707f3Ssthen } 568933707f3Ssthen 569933707f3Ssthen /** printf style printing to the ssl connection */ 57020237c55Ssthen int ssl_printf(RES* ssl, const char* format, ...) 571933707f3Ssthen { 572933707f3Ssthen va_list args; 573933707f3Ssthen int ret; 574933707f3Ssthen va_start(args, format); 575933707f3Ssthen ret = ssl_print_vmsg(ssl, format, args); 576933707f3Ssthen va_end(args); 577933707f3Ssthen return ret; 578933707f3Ssthen } 579933707f3Ssthen 580933707f3Ssthen int 58120237c55Ssthen ssl_read_line(RES* res, char* buf, size_t max) 582933707f3Ssthen { 583933707f3Ssthen int r; 584933707f3Ssthen size_t len = 0; 58520237c55Ssthen if(!res) 586933707f3Ssthen return 0; 587933707f3Ssthen while(len < max) { 58820237c55Ssthen if(res->ssl) { 589933707f3Ssthen ERR_clear_error(); 59020237c55Ssthen if((r=SSL_read(res->ssl, buf+len, 1)) <= 0) { 591d896b962Ssthen int r2; 592d896b962Ssthen if((r2=SSL_get_error(res->ssl, r)) == SSL_ERROR_ZERO_RETURN) { 593933707f3Ssthen buf[len] = 0; 594933707f3Ssthen return 1; 595933707f3Ssthen } 596d896b962Ssthen log_crypto_err_io("could not SSL_read", r2); 597933707f3Ssthen return 0; 598933707f3Ssthen } 59920237c55Ssthen } else { 60020237c55Ssthen while(1) { 60120237c55Ssthen ssize_t rr = recv(res->fd, buf+len, 1, 0); 60220237c55Ssthen if(rr <= 0) { 60320237c55Ssthen if(rr == 0) { 60420237c55Ssthen buf[len] = 0; 60520237c55Ssthen return 1; 60620237c55Ssthen } 60720237c55Ssthen if(errno == EINTR || errno == EAGAIN) 60820237c55Ssthen continue; 609d896b962Ssthen if(rr < 0) log_err("could not recv: %s", 6102c144df0Ssthen sock_strerror(errno)); 61120237c55Ssthen return 0; 61220237c55Ssthen } 61320237c55Ssthen break; 61420237c55Ssthen } 61520237c55Ssthen } 616933707f3Ssthen if(buf[len] == '\n') { 617933707f3Ssthen /* return string without \n */ 618933707f3Ssthen buf[len] = 0; 619933707f3Ssthen return 1; 620933707f3Ssthen } 621933707f3Ssthen len++; 622933707f3Ssthen } 623933707f3Ssthen buf[max-1] = 0; 624933707f3Ssthen log_err("control line too long (%d): %s", (int)max, buf); 625933707f3Ssthen return 0; 626933707f3Ssthen } 627933707f3Ssthen 628933707f3Ssthen /** skip whitespace, return new pointer into string */ 629933707f3Ssthen static char* 630933707f3Ssthen skipwhite(char* str) 631933707f3Ssthen { 632933707f3Ssthen /* EOS \0 is not a space */ 633e10d3884Sbrad while( isspace((unsigned char)*str) ) 634933707f3Ssthen str++; 635933707f3Ssthen return str; 636933707f3Ssthen } 637933707f3Ssthen 638933707f3Ssthen /** send the OK to the control client */ 63920237c55Ssthen static void send_ok(RES* ssl) 640933707f3Ssthen { 641933707f3Ssthen (void)ssl_printf(ssl, "ok\n"); 642933707f3Ssthen } 643933707f3Ssthen 644*98bc733bSsthen /** tell other processes to execute the command */ 645*98bc733bSsthen static void 646*98bc733bSsthen distribute_cmd(struct daemon_remote* rc, RES* ssl, char* cmd) 647*98bc733bSsthen { 648*98bc733bSsthen int i; 649*98bc733bSsthen if(!cmd || !ssl) 650*98bc733bSsthen return; 651*98bc733bSsthen /* skip i=0 which is me */ 652*98bc733bSsthen for(i=1; i<rc->worker->daemon->num; i++) { 653*98bc733bSsthen worker_send_cmd(rc->worker->daemon->workers[i], 654*98bc733bSsthen worker_cmd_remote); 655*98bc733bSsthen if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd, 656*98bc733bSsthen (uint8_t*)cmd, strlen(cmd)+1, 0)) { 657*98bc733bSsthen (void)ssl_printf(ssl, "error could not distribute cmd\n"); 658*98bc733bSsthen return; 659*98bc733bSsthen } 660*98bc733bSsthen } 661*98bc733bSsthen } 662*98bc733bSsthen 663933707f3Ssthen /** do the stop command */ 664933707f3Ssthen static void 665ebf5bb73Ssthen do_stop(RES* ssl, struct worker* worker) 666933707f3Ssthen { 667ebf5bb73Ssthen worker->need_to_exit = 1; 668ebf5bb73Ssthen comm_base_exit(worker->base); 669933707f3Ssthen send_ok(ssl); 670933707f3Ssthen } 671933707f3Ssthen 672933707f3Ssthen /** do the reload command */ 673933707f3Ssthen static void 6748b7325afSsthen do_reload(RES* ssl, struct worker* worker, int reuse_cache) 675933707f3Ssthen { 6768b7325afSsthen worker->reuse_cache = reuse_cache; 677ebf5bb73Ssthen worker->need_to_exit = 0; 678ebf5bb73Ssthen comm_base_exit(worker->base); 679933707f3Ssthen send_ok(ssl); 680933707f3Ssthen } 681933707f3Ssthen 682933707f3Ssthen /** do the verbosity command */ 683933707f3Ssthen static void 68420237c55Ssthen do_verbosity(RES* ssl, char* str) 685933707f3Ssthen { 686933707f3Ssthen int val = atoi(str); 687933707f3Ssthen if(val == 0 && strcmp(str, "0") != 0) { 688933707f3Ssthen ssl_printf(ssl, "error in verbosity number syntax: %s\n", str); 689933707f3Ssthen return; 690933707f3Ssthen } 691933707f3Ssthen verbosity = val; 692933707f3Ssthen send_ok(ssl); 693933707f3Ssthen } 694933707f3Ssthen 695933707f3Ssthen /** print stats from statinfo */ 696933707f3Ssthen static int 69720237c55Ssthen print_stats(RES* ssl, const char* nm, struct ub_stats_info* s) 698933707f3Ssthen { 6992be9e038Ssthen struct timeval sumwait, avg; 700e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm, 701e10d3884Sbrad (unsigned long)s->svr.num_queries)) return 0; 70277079be7Ssthen if(!ssl_printf(ssl, "%s.num.queries_ip_ratelimited"SQ"%lu\n", nm, 70377079be7Ssthen (unsigned long)s->svr.num_queries_ip_ratelimited)) return 0; 7048b7325afSsthen if(!ssl_printf(ssl, "%s.num.queries_cookie_valid"SQ"%lu\n", nm, 7058b7325afSsthen (unsigned long)s->svr.num_queries_cookie_valid)) return 0; 7068b7325afSsthen if(!ssl_printf(ssl, "%s.num.queries_cookie_client"SQ"%lu\n", nm, 7078b7325afSsthen (unsigned long)s->svr.num_queries_cookie_client)) return 0; 7088b7325afSsthen if(!ssl_printf(ssl, "%s.num.queries_cookie_invalid"SQ"%lu\n", nm, 7098b7325afSsthen (unsigned long)s->svr.num_queries_cookie_invalid)) return 0; 710e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm, 711e10d3884Sbrad (unsigned long)(s->svr.num_queries 712933707f3Ssthen - s->svr.num_queries_missed_cache))) return 0; 713e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm, 714e10d3884Sbrad (unsigned long)s->svr.num_queries_missed_cache)) return 0; 715e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm, 716e10d3884Sbrad (unsigned long)s->svr.num_queries_prefetch)) return 0; 7178b7325afSsthen if(!ssl_printf(ssl, "%s.num.queries_timed_out"SQ"%lu\n", nm, 7188b7325afSsthen (unsigned long)s->svr.num_queries_timed_out)) return 0; 7198b7325afSsthen if(!ssl_printf(ssl, "%s.query.queue_time_us.max"SQ"%lu\n", nm, 7208b7325afSsthen (unsigned long)s->svr.max_query_time_us)) return 0; 721eaf2578eSsthen if(!ssl_printf(ssl, "%s.num.expired"SQ"%lu\n", nm, 722eaf2578eSsthen (unsigned long)s->svr.ans_expired)) return 0; 723e10d3884Sbrad if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm, 724e10d3884Sbrad (unsigned long)s->mesh_replies_sent)) return 0; 7252be9e038Ssthen #ifdef USE_DNSCRYPT 7262be9e038Ssthen if(!ssl_printf(ssl, "%s.num.dnscrypt.crypted"SQ"%lu\n", nm, 7272be9e038Ssthen (unsigned long)s->svr.num_query_dnscrypt_crypted)) return 0; 7282be9e038Ssthen if(!ssl_printf(ssl, "%s.num.dnscrypt.cert"SQ"%lu\n", nm, 7292be9e038Ssthen (unsigned long)s->svr.num_query_dnscrypt_cert)) return 0; 7302be9e038Ssthen if(!ssl_printf(ssl, "%s.num.dnscrypt.cleartext"SQ"%lu\n", nm, 7312be9e038Ssthen (unsigned long)s->svr.num_query_dnscrypt_cleartext)) return 0; 7322be9e038Ssthen if(!ssl_printf(ssl, "%s.num.dnscrypt.malformed"SQ"%lu\n", nm, 7332be9e038Ssthen (unsigned long)s->svr.num_query_dnscrypt_crypted_malformed)) return 0; 7342be9e038Ssthen #endif 735933707f3Ssthen if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm, 736933707f3Ssthen (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)? 737933707f3Ssthen (double)s->svr.sum_query_list_size/ 7382be9e038Ssthen (double)(s->svr.num_queries_missed_cache+ 739933707f3Ssthen s->svr.num_queries_prefetch) : 0.0)) return 0; 740e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm, 741e10d3884Sbrad (unsigned long)s->svr.max_query_list_size)) return 0; 742e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm, 743e10d3884Sbrad (unsigned long)s->mesh_jostled)) return 0; 744e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm, 745e10d3884Sbrad (unsigned long)s->mesh_dropped)) return 0; 746e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm, 747e10d3884Sbrad (unsigned long)s->mesh_num_states)) return 0; 748e10d3884Sbrad if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm, 749e10d3884Sbrad (unsigned long)s->mesh_num_reply_states)) return 0; 7502be9e038Ssthen #ifndef S_SPLINT_S 7512be9e038Ssthen sumwait.tv_sec = s->mesh_replies_sum_wait_sec; 7522be9e038Ssthen sumwait.tv_usec = s->mesh_replies_sum_wait_usec; 7532be9e038Ssthen #endif 7542be9e038Ssthen timeval_divide(&avg, &sumwait, s->mesh_replies_sent); 7550b68ff31Ssthen if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm, 756e9c7b4efSsthen (long long)avg.tv_sec, (int)avg.tv_usec)) return 0; 757933707f3Ssthen if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm, 758933707f3Ssthen s->mesh_time_median)) return 0; 759a58bff56Ssthen if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm, 760a58bff56Ssthen (unsigned long)s->svr.tcp_accept_usage)) return 0; 761933707f3Ssthen return 1; 762933707f3Ssthen } 763933707f3Ssthen 764933707f3Ssthen /** print stats for one thread */ 765933707f3Ssthen static int 76620237c55Ssthen print_thread_stats(RES* ssl, int i, struct ub_stats_info* s) 767933707f3Ssthen { 7687191de28Ssthen char nm[32]; 769933707f3Ssthen snprintf(nm, sizeof(nm), "thread%d", i); 770933707f3Ssthen nm[sizeof(nm)-1]=0; 771933707f3Ssthen return print_stats(ssl, nm, s); 772933707f3Ssthen } 773933707f3Ssthen 774933707f3Ssthen /** print long number */ 775933707f3Ssthen static int 77620237c55Ssthen print_longnum(RES* ssl, const char* desc, size_t x) 777933707f3Ssthen { 778933707f3Ssthen if(x > 1024*1024*1024) { 779933707f3Ssthen /* more than a Gb */ 780933707f3Ssthen size_t front = x / (size_t)1000000; 781933707f3Ssthen size_t back = x % (size_t)1000000; 782933707f3Ssthen return ssl_printf(ssl, "%s%u%6.6u\n", desc, 783933707f3Ssthen (unsigned)front, (unsigned)back); 784933707f3Ssthen } else { 785e10d3884Sbrad return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x); 786933707f3Ssthen } 787933707f3Ssthen } 788933707f3Ssthen 789933707f3Ssthen /** print mem stats */ 790933707f3Ssthen static int 791f6b99bafSsthen print_mem(RES* ssl, struct worker* worker, struct daemon* daemon, 792f6b99bafSsthen struct ub_stats_info* s) 793933707f3Ssthen { 7942be9e038Ssthen size_t msg, rrset, val, iter, respip; 7952be9e038Ssthen #ifdef CLIENT_SUBNET 7962be9e038Ssthen size_t subnet = 0; 7972be9e038Ssthen #endif /* CLIENT_SUBNET */ 7982be9e038Ssthen #ifdef USE_IPSECMOD 7992be9e038Ssthen size_t ipsecmod = 0; 8002be9e038Ssthen #endif /* USE_IPSECMOD */ 8017191de28Ssthen #ifdef USE_DNSCRYPT 8027191de28Ssthen size_t dnscrypt_shared_secret = 0; 803bdfc4d55Sflorian size_t dnscrypt_nonce = 0; 8047191de28Ssthen #endif /* USE_DNSCRYPT */ 805a3167c07Ssthen #ifdef WITH_DYNLIBMODULE 806a3167c07Ssthen size_t dynlib = 0; 807a3167c07Ssthen #endif /* WITH_DYNLIBMODULE */ 808933707f3Ssthen msg = slabhash_get_mem(daemon->env->msg_cache); 809933707f3Ssthen rrset = slabhash_get_mem(&daemon->env->rrset_cache->table); 8102be9e038Ssthen val = mod_get_mem(&worker->env, "validator"); 8112be9e038Ssthen iter = mod_get_mem(&worker->env, "iterator"); 8122be9e038Ssthen respip = mod_get_mem(&worker->env, "respip"); 8132be9e038Ssthen #ifdef CLIENT_SUBNET 814e21c60efSsthen subnet = mod_get_mem(&worker->env, "subnetcache"); 8152be9e038Ssthen #endif /* CLIENT_SUBNET */ 8162be9e038Ssthen #ifdef USE_IPSECMOD 8172be9e038Ssthen ipsecmod = mod_get_mem(&worker->env, "ipsecmod"); 8182be9e038Ssthen #endif /* USE_IPSECMOD */ 8197191de28Ssthen #ifdef USE_DNSCRYPT 8207191de28Ssthen if(daemon->dnscenv) { 8217191de28Ssthen dnscrypt_shared_secret = slabhash_get_mem( 8227191de28Ssthen daemon->dnscenv->shared_secrets_cache); 823bdfc4d55Sflorian dnscrypt_nonce = slabhash_get_mem(daemon->dnscenv->nonces_cache); 8247191de28Ssthen } 8257191de28Ssthen #endif /* USE_DNSCRYPT */ 826a3167c07Ssthen #ifdef WITH_DYNLIBMODULE 827a3167c07Ssthen dynlib = mod_get_mem(&worker->env, "dynlib"); 828a3167c07Ssthen #endif /* WITH_DYNLIBMODULE */ 829933707f3Ssthen 830933707f3Ssthen if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset)) 831933707f3Ssthen return 0; 832933707f3Ssthen if(!print_longnum(ssl, "mem.cache.message"SQ, msg)) 833933707f3Ssthen return 0; 834933707f3Ssthen if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter)) 835933707f3Ssthen return 0; 836933707f3Ssthen if(!print_longnum(ssl, "mem.mod.validator"SQ, val)) 837933707f3Ssthen return 0; 8382be9e038Ssthen if(!print_longnum(ssl, "mem.mod.respip"SQ, respip)) 8392be9e038Ssthen return 0; 8402be9e038Ssthen #ifdef CLIENT_SUBNET 8412be9e038Ssthen if(!print_longnum(ssl, "mem.mod.subnet"SQ, subnet)) 8422be9e038Ssthen return 0; 8432be9e038Ssthen #endif /* CLIENT_SUBNET */ 8442be9e038Ssthen #ifdef USE_IPSECMOD 8452be9e038Ssthen if(!print_longnum(ssl, "mem.mod.ipsecmod"SQ, ipsecmod)) 8462be9e038Ssthen return 0; 8472be9e038Ssthen #endif /* USE_IPSECMOD */ 8487191de28Ssthen #ifdef USE_DNSCRYPT 8497191de28Ssthen if(!print_longnum(ssl, "mem.cache.dnscrypt_shared_secret"SQ, 8507191de28Ssthen dnscrypt_shared_secret)) 8517191de28Ssthen return 0; 852bdfc4d55Sflorian if(!print_longnum(ssl, "mem.cache.dnscrypt_nonce"SQ, 853bdfc4d55Sflorian dnscrypt_nonce)) 854bdfc4d55Sflorian return 0; 8557191de28Ssthen #endif /* USE_DNSCRYPT */ 856a3167c07Ssthen #ifdef WITH_DYNLIBMODULE 857a3167c07Ssthen if(!print_longnum(ssl, "mem.mod.dynlibmod"SQ, dynlib)) 858a3167c07Ssthen return 0; 859a3167c07Ssthen #endif /* WITH_DYNLIBMODULE */ 860f6b99bafSsthen if(!print_longnum(ssl, "mem.streamwait"SQ, 861f6b99bafSsthen (size_t)s->svr.mem_stream_wait)) 862f6b99bafSsthen return 0; 8632c144df0Ssthen if(!print_longnum(ssl, "mem.http.query_buffer"SQ, 8642c144df0Ssthen (size_t)s->svr.mem_http2_query_buffer)) 8652c144df0Ssthen return 0; 8662c144df0Ssthen if(!print_longnum(ssl, "mem.http.response_buffer"SQ, 8672c144df0Ssthen (size_t)s->svr.mem_http2_response_buffer)) 8682c144df0Ssthen return 0; 869933707f3Ssthen return 1; 870933707f3Ssthen } 871933707f3Ssthen 872933707f3Ssthen /** print uptime stats */ 873933707f3Ssthen static int 87420237c55Ssthen print_uptime(RES* ssl, struct worker* worker, int reset) 875933707f3Ssthen { 876933707f3Ssthen struct timeval now = *worker->env.now_tv; 877933707f3Ssthen struct timeval up, dt; 878933707f3Ssthen timeval_subtract(&up, &now, &worker->daemon->time_boot); 879933707f3Ssthen timeval_subtract(&dt, &now, &worker->daemon->time_last_stat); 880933707f3Ssthen if(reset) 881933707f3Ssthen worker->daemon->time_last_stat = now; 8820b68ff31Ssthen if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n", 883e9c7b4efSsthen (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0; 8840b68ff31Ssthen if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n", 885e9c7b4efSsthen (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0; 8860b68ff31Ssthen if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n", 887e9c7b4efSsthen (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0; 888933707f3Ssthen return 1; 889933707f3Ssthen } 890933707f3Ssthen 891933707f3Ssthen /** print extended histogram */ 892933707f3Ssthen static int 89320237c55Ssthen print_hist(RES* ssl, struct ub_stats_info* s) 894933707f3Ssthen { 895933707f3Ssthen struct timehist* hist; 896933707f3Ssthen size_t i; 897933707f3Ssthen hist = timehist_setup(); 898933707f3Ssthen if(!hist) { 899933707f3Ssthen log_err("out of memory"); 900933707f3Ssthen return 0; 901933707f3Ssthen } 902933707f3Ssthen timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST); 903933707f3Ssthen for(i=0; i<hist->num; i++) { 904933707f3Ssthen if(!ssl_printf(ssl, 905e10d3884Sbrad "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n", 906933707f3Ssthen (int)hist->buckets[i].lower.tv_sec, 907933707f3Ssthen (int)hist->buckets[i].lower.tv_usec, 908933707f3Ssthen (int)hist->buckets[i].upper.tv_sec, 909933707f3Ssthen (int)hist->buckets[i].upper.tv_usec, 910e10d3884Sbrad (unsigned long)hist->buckets[i].count)) { 911933707f3Ssthen timehist_delete(hist); 912933707f3Ssthen return 0; 913933707f3Ssthen } 914933707f3Ssthen } 915933707f3Ssthen timehist_delete(hist); 916933707f3Ssthen return 1; 917933707f3Ssthen } 918933707f3Ssthen 919933707f3Ssthen /** print extended stats */ 920933707f3Ssthen static int 9218b7325afSsthen print_ext(RES* ssl, struct ub_stats_info* s, int inhibit_zero) 922933707f3Ssthen { 923933707f3Ssthen int i; 924a3167c07Ssthen char nm[32]; 9250b68ff31Ssthen const sldns_rr_descriptor* desc; 9260b68ff31Ssthen const sldns_lookup_table* lt; 927933707f3Ssthen /* TYPE */ 9282be9e038Ssthen for(i=0; i<UB_STATS_QTYPE_NUM; i++) { 929933707f3Ssthen if(inhibit_zero && s->svr.qtype[i] == 0) 930933707f3Ssthen continue; 9310b68ff31Ssthen desc = sldns_rr_descript((uint16_t)i); 932933707f3Ssthen if(desc && desc->_name) { 933933707f3Ssthen snprintf(nm, sizeof(nm), "%s", desc->_name); 934933707f3Ssthen } else if (i == LDNS_RR_TYPE_IXFR) { 935933707f3Ssthen snprintf(nm, sizeof(nm), "IXFR"); 936933707f3Ssthen } else if (i == LDNS_RR_TYPE_AXFR) { 937933707f3Ssthen snprintf(nm, sizeof(nm), "AXFR"); 938933707f3Ssthen } else if (i == LDNS_RR_TYPE_MAILA) { 939933707f3Ssthen snprintf(nm, sizeof(nm), "MAILA"); 940933707f3Ssthen } else if (i == LDNS_RR_TYPE_MAILB) { 941933707f3Ssthen snprintf(nm, sizeof(nm), "MAILB"); 942933707f3Ssthen } else if (i == LDNS_RR_TYPE_ANY) { 943933707f3Ssthen snprintf(nm, sizeof(nm), "ANY"); 944933707f3Ssthen } else { 945933707f3Ssthen snprintf(nm, sizeof(nm), "TYPE%d", i); 946933707f3Ssthen } 947e10d3884Sbrad if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n", 948e10d3884Sbrad nm, (unsigned long)s->svr.qtype[i])) return 0; 949933707f3Ssthen } 950933707f3Ssthen if(!inhibit_zero || s->svr.qtype_big) { 951e10d3884Sbrad if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n", 952e10d3884Sbrad (unsigned long)s->svr.qtype_big)) return 0; 953933707f3Ssthen } 954933707f3Ssthen /* CLASS */ 9552be9e038Ssthen for(i=0; i<UB_STATS_QCLASS_NUM; i++) { 956933707f3Ssthen if(inhibit_zero && s->svr.qclass[i] == 0) 957933707f3Ssthen continue; 9580b68ff31Ssthen lt = sldns_lookup_by_id(sldns_rr_classes, i); 959933707f3Ssthen if(lt && lt->name) { 960933707f3Ssthen snprintf(nm, sizeof(nm), "%s", lt->name); 961933707f3Ssthen } else { 962933707f3Ssthen snprintf(nm, sizeof(nm), "CLASS%d", i); 963933707f3Ssthen } 964e10d3884Sbrad if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n", 965e10d3884Sbrad nm, (unsigned long)s->svr.qclass[i])) return 0; 966933707f3Ssthen } 967933707f3Ssthen if(!inhibit_zero || s->svr.qclass_big) { 968e10d3884Sbrad if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n", 969e10d3884Sbrad (unsigned long)s->svr.qclass_big)) return 0; 970933707f3Ssthen } 971933707f3Ssthen /* OPCODE */ 9722be9e038Ssthen for(i=0; i<UB_STATS_OPCODE_NUM; i++) { 973933707f3Ssthen if(inhibit_zero && s->svr.qopcode[i] == 0) 974933707f3Ssthen continue; 9750b68ff31Ssthen lt = sldns_lookup_by_id(sldns_opcodes, i); 976933707f3Ssthen if(lt && lt->name) { 977933707f3Ssthen snprintf(nm, sizeof(nm), "%s", lt->name); 978933707f3Ssthen } else { 979933707f3Ssthen snprintf(nm, sizeof(nm), "OPCODE%d", i); 980933707f3Ssthen } 981e10d3884Sbrad if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n", 982e10d3884Sbrad nm, (unsigned long)s->svr.qopcode[i])) return 0; 983933707f3Ssthen } 984933707f3Ssthen /* transport */ 985e10d3884Sbrad if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n", 986e10d3884Sbrad (unsigned long)s->svr.qtcp)) return 0; 987e10d3884Sbrad if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n", 988e10d3884Sbrad (unsigned long)s->svr.qtcp_outgoing)) return 0; 989d1e2768aSsthen if(!ssl_printf(ssl, "num.query.udpout"SQ"%lu\n", 990d1e2768aSsthen (unsigned long)s->svr.qudp_outgoing)) return 0; 9912308e98cSsthen if(!ssl_printf(ssl, "num.query.tls"SQ"%lu\n", 9922308e98cSsthen (unsigned long)s->svr.qtls)) return 0; 993f6b99bafSsthen if(!ssl_printf(ssl, "num.query.tls.resume"SQ"%lu\n", 994f6b99bafSsthen (unsigned long)s->svr.qtls_resume)) return 0; 995e10d3884Sbrad if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n", 996e10d3884Sbrad (unsigned long)s->svr.qipv6)) return 0; 9972c144df0Ssthen if(!ssl_printf(ssl, "num.query.https"SQ"%lu\n", 9982c144df0Ssthen (unsigned long)s->svr.qhttps)) return 0; 999933707f3Ssthen /* flags */ 1000e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n", 1001e10d3884Sbrad (unsigned long)s->svr.qbit_QR)) return 0; 1002e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n", 1003e10d3884Sbrad (unsigned long)s->svr.qbit_AA)) return 0; 1004e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n", 1005e10d3884Sbrad (unsigned long)s->svr.qbit_TC)) return 0; 1006e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n", 1007e10d3884Sbrad (unsigned long)s->svr.qbit_RD)) return 0; 1008e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n", 1009e10d3884Sbrad (unsigned long)s->svr.qbit_RA)) return 0; 1010e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n", 1011e10d3884Sbrad (unsigned long)s->svr.qbit_Z)) return 0; 1012e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n", 1013e10d3884Sbrad (unsigned long)s->svr.qbit_AD)) return 0; 1014e10d3884Sbrad if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n", 1015e10d3884Sbrad (unsigned long)s->svr.qbit_CD)) return 0; 1016e10d3884Sbrad if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n", 1017e10d3884Sbrad (unsigned long)s->svr.qEDNS)) return 0; 1018e10d3884Sbrad if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n", 1019e10d3884Sbrad (unsigned long)s->svr.qEDNS_DO)) return 0; 1020933707f3Ssthen 1021933707f3Ssthen /* RCODE */ 10222be9e038Ssthen for(i=0; i<UB_STATS_RCODE_NUM; i++) { 1023d50c774aSbrad /* Always include RCODEs 0-5 */ 1024d50c774aSbrad if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0) 1025933707f3Ssthen continue; 10260b68ff31Ssthen lt = sldns_lookup_by_id(sldns_rcodes, i); 1027933707f3Ssthen if(lt && lt->name) { 1028933707f3Ssthen snprintf(nm, sizeof(nm), "%s", lt->name); 1029933707f3Ssthen } else { 1030933707f3Ssthen snprintf(nm, sizeof(nm), "RCODE%d", i); 1031933707f3Ssthen } 1032e10d3884Sbrad if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n", 1033e10d3884Sbrad nm, (unsigned long)s->svr.ans_rcode[i])) return 0; 1034933707f3Ssthen } 1035933707f3Ssthen if(!inhibit_zero || s->svr.ans_rcode_nodata) { 1036e10d3884Sbrad if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n", 1037e10d3884Sbrad (unsigned long)s->svr.ans_rcode_nodata)) return 0; 1038933707f3Ssthen } 10397191de28Ssthen /* iteration */ 10407191de28Ssthen if(!ssl_printf(ssl, "num.query.ratelimited"SQ"%lu\n", 10417191de28Ssthen (unsigned long)s->svr.queries_ratelimited)) return 0; 1042933707f3Ssthen /* validation */ 1043e10d3884Sbrad if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n", 1044e10d3884Sbrad (unsigned long)s->svr.ans_secure)) return 0; 1045e10d3884Sbrad if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n", 1046e10d3884Sbrad (unsigned long)s->svr.ans_bogus)) return 0; 1047e10d3884Sbrad if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n", 1048e10d3884Sbrad (unsigned long)s->svr.rrset_bogus)) return 0; 104920237c55Ssthen if(!ssl_printf(ssl, "num.query.aggressive.NOERROR"SQ"%lu\n", 105020237c55Ssthen (unsigned long)s->svr.num_neg_cache_noerror)) return 0; 105120237c55Ssthen if(!ssl_printf(ssl, "num.query.aggressive.NXDOMAIN"SQ"%lu\n", 105220237c55Ssthen (unsigned long)s->svr.num_neg_cache_nxdomain)) return 0; 1053933707f3Ssthen /* threat detection */ 1054e10d3884Sbrad if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n", 1055e10d3884Sbrad (unsigned long)s->svr.unwanted_queries)) return 0; 1056e10d3884Sbrad if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n", 1057e10d3884Sbrad (unsigned long)s->svr.unwanted_replies)) return 0; 1058e10d3884Sbrad /* cache counts */ 1059e10d3884Sbrad if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n", 1060e10d3884Sbrad (unsigned)s->svr.msg_cache_count)) return 0; 1061e10d3884Sbrad if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n", 1062e10d3884Sbrad (unsigned)s->svr.rrset_cache_count)) return 0; 1063e10d3884Sbrad if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n", 1064e10d3884Sbrad (unsigned)s->svr.infra_cache_count)) return 0; 1065e10d3884Sbrad if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n", 1066e10d3884Sbrad (unsigned)s->svr.key_cache_count)) return 0; 10678b7325afSsthen /* max collisions */ 10688b7325afSsthen if(!ssl_printf(ssl, "msg.cache.max_collisions"SQ"%u\n", 10698b7325afSsthen (unsigned)s->svr.msg_cache_max_collisions)) return 0; 10708b7325afSsthen if(!ssl_printf(ssl, "rrset.cache.max_collisions"SQ"%u\n", 10718b7325afSsthen (unsigned)s->svr.rrset_cache_max_collisions)) return 0; 1072eaf2578eSsthen /* applied RPZ actions */ 1073eaf2578eSsthen for(i=0; i<UB_STATS_RPZ_ACTION_NUM; i++) { 1074eaf2578eSsthen if(i == RPZ_NO_OVERRIDE_ACTION) 1075eaf2578eSsthen continue; 1076eaf2578eSsthen if(inhibit_zero && s->svr.rpz_action[i] == 0) 1077eaf2578eSsthen continue; 1078eaf2578eSsthen if(!ssl_printf(ssl, "num.rpz.action.%s"SQ"%lu\n", 1079eaf2578eSsthen rpz_action_to_string(i), 1080eaf2578eSsthen (unsigned long)s->svr.rpz_action[i])) return 0; 1081eaf2578eSsthen } 10827191de28Ssthen #ifdef USE_DNSCRYPT 10837191de28Ssthen if(!ssl_printf(ssl, "dnscrypt_shared_secret.cache.count"SQ"%u\n", 10847191de28Ssthen (unsigned)s->svr.shared_secret_cache_count)) return 0; 1085bdfc4d55Sflorian if(!ssl_printf(ssl, "dnscrypt_nonce.cache.count"SQ"%u\n", 1086bdfc4d55Sflorian (unsigned)s->svr.nonce_cache_count)) return 0; 10877191de28Ssthen if(!ssl_printf(ssl, "num.query.dnscrypt.shared_secret.cachemiss"SQ"%lu\n", 10887191de28Ssthen (unsigned long)s->svr.num_query_dnscrypt_secret_missed_cache)) return 0; 1089bdfc4d55Sflorian if(!ssl_printf(ssl, "num.query.dnscrypt.replay"SQ"%lu\n", 1090bdfc4d55Sflorian (unsigned long)s->svr.num_query_dnscrypt_replay)) return 0; 10917191de28Ssthen #endif /* USE_DNSCRYPT */ 109220237c55Ssthen if(!ssl_printf(ssl, "num.query.authzone.up"SQ"%lu\n", 109320237c55Ssthen (unsigned long)s->svr.num_query_authzone_up)) return 0; 109420237c55Ssthen if(!ssl_printf(ssl, "num.query.authzone.down"SQ"%lu\n", 109520237c55Ssthen (unsigned long)s->svr.num_query_authzone_down)) return 0; 10962308e98cSsthen #ifdef CLIENT_SUBNET 10972308e98cSsthen if(!ssl_printf(ssl, "num.query.subnet"SQ"%lu\n", 10982308e98cSsthen (unsigned long)s->svr.num_query_subnet)) return 0; 10992308e98cSsthen if(!ssl_printf(ssl, "num.query.subnet_cache"SQ"%lu\n", 11002308e98cSsthen (unsigned long)s->svr.num_query_subnet_cache)) return 0; 11012308e98cSsthen #endif /* CLIENT_SUBNET */ 11028b7325afSsthen #ifdef USE_CACHEDB 11038b7325afSsthen if(!ssl_printf(ssl, "num.query.cachedb"SQ"%lu\n", 11048b7325afSsthen (unsigned long)s->svr.num_query_cachedb)) return 0; 11058b7325afSsthen #endif /* USE_CACHEDB */ 1106933707f3Ssthen return 1; 1107933707f3Ssthen } 1108933707f3Ssthen 1109933707f3Ssthen /** do the stats command */ 1110933707f3Ssthen static void 1111ebf5bb73Ssthen do_stats(RES* ssl, struct worker* worker, int reset) 1112933707f3Ssthen { 1113ebf5bb73Ssthen struct daemon* daemon = worker->daemon; 11142be9e038Ssthen struct ub_stats_info total; 11152be9e038Ssthen struct ub_stats_info s; 1116933707f3Ssthen int i; 1117452a1548Ssthen memset(&total, 0, sizeof(total)); 1118933707f3Ssthen log_assert(daemon->num > 0); 1119933707f3Ssthen /* gather all thread statistics in one place */ 1120933707f3Ssthen for(i=0; i<daemon->num; i++) { 1121ebf5bb73Ssthen server_stats_obtain(worker, daemon->workers[i], &s, reset); 1122933707f3Ssthen if(!print_thread_stats(ssl, i, &s)) 1123933707f3Ssthen return; 1124933707f3Ssthen if(i == 0) 1125933707f3Ssthen total = s; 1126933707f3Ssthen else server_stats_add(&total, &s); 1127933707f3Ssthen } 1128933707f3Ssthen /* print the thread statistics */ 1129933707f3Ssthen total.mesh_time_median /= (double)daemon->num; 1130933707f3Ssthen if(!print_stats(ssl, "total", &total)) 1131933707f3Ssthen return; 1132ebf5bb73Ssthen if(!print_uptime(ssl, worker, reset)) 1133933707f3Ssthen return; 1134933707f3Ssthen if(daemon->cfg->stat_extended) { 1135ebf5bb73Ssthen if(!print_mem(ssl, worker, daemon, &total)) 1136933707f3Ssthen return; 1137933707f3Ssthen if(!print_hist(ssl, &total)) 1138933707f3Ssthen return; 11398b7325afSsthen if(!print_ext(ssl, &total, daemon->cfg->stat_inhibit_zero)) 1140933707f3Ssthen return; 1141933707f3Ssthen } 1142933707f3Ssthen } 1143933707f3Ssthen 1144933707f3Ssthen /** parse commandline argument domain name */ 1145933707f3Ssthen static int 114620237c55Ssthen parse_arg_name(RES* ssl, char* str, uint8_t** res, size_t* len, int* labs) 1147933707f3Ssthen { 11480b68ff31Ssthen uint8_t nm[LDNS_MAX_DOMAINLEN+1]; 11490b68ff31Ssthen size_t nmlen = sizeof(nm); 11500b68ff31Ssthen int status; 1151933707f3Ssthen *res = NULL; 1152933707f3Ssthen *len = 0; 1153933707f3Ssthen *labs = 0; 1154a3167c07Ssthen if(str[0] == '\0') { 1155a3167c07Ssthen ssl_printf(ssl, "error: this option requires a domain name\n"); 1156a3167c07Ssthen return 0; 1157a3167c07Ssthen } 11580b68ff31Ssthen status = sldns_str2wire_dname_buf(str, nm, &nmlen); 11590b68ff31Ssthen if(status != 0) { 11600b68ff31Ssthen ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str, 11610b68ff31Ssthen LDNS_WIREPARSE_OFFSET(status), 11620b68ff31Ssthen sldns_get_errorstr_parse(status)); 1163933707f3Ssthen return 0; 1164933707f3Ssthen } 11650b68ff31Ssthen *res = memdup(nm, nmlen); 1166933707f3Ssthen if(!*res) { 1167933707f3Ssthen ssl_printf(ssl, "error out of memory\n"); 1168933707f3Ssthen return 0; 1169933707f3Ssthen } 1170933707f3Ssthen *labs = dname_count_size_labels(*res, len); 1171933707f3Ssthen return 1; 1172933707f3Ssthen } 1173933707f3Ssthen 1174933707f3Ssthen /** find second argument, modifies string */ 1175933707f3Ssthen static int 117620237c55Ssthen find_arg2(RES* ssl, char* arg, char** arg2) 1177933707f3Ssthen { 1178933707f3Ssthen char* as = strchr(arg, ' '); 1179933707f3Ssthen char* at = strchr(arg, '\t'); 1180933707f3Ssthen if(as && at) { 1181933707f3Ssthen if(at < as) 1182933707f3Ssthen as = at; 1183933707f3Ssthen as[0]=0; 1184933707f3Ssthen *arg2 = skipwhite(as+1); 1185933707f3Ssthen } else if(as) { 1186933707f3Ssthen as[0]=0; 1187933707f3Ssthen *arg2 = skipwhite(as+1); 1188933707f3Ssthen } else if(at) { 1189933707f3Ssthen at[0]=0; 1190933707f3Ssthen *arg2 = skipwhite(at+1); 1191933707f3Ssthen } else { 1192933707f3Ssthen ssl_printf(ssl, "error could not find next argument " 1193933707f3Ssthen "after %s\n", arg); 1194933707f3Ssthen return 0; 1195933707f3Ssthen } 1196933707f3Ssthen return 1; 1197933707f3Ssthen } 1198933707f3Ssthen 1199933707f3Ssthen /** Add a new zone */ 120077079be7Ssthen static int 120120237c55Ssthen perform_zone_add(RES* ssl, struct local_zones* zones, char* arg) 1202933707f3Ssthen { 1203933707f3Ssthen uint8_t* nm; 1204933707f3Ssthen int nmlabs; 1205933707f3Ssthen size_t nmlen; 1206933707f3Ssthen char* arg2; 1207933707f3Ssthen enum localzone_type t; 1208933707f3Ssthen struct local_zone* z; 1209933707f3Ssthen if(!find_arg2(ssl, arg, &arg2)) 121077079be7Ssthen return 0; 1211933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 121277079be7Ssthen return 0; 1213933707f3Ssthen if(!local_zone_str2type(arg2, &t)) { 1214933707f3Ssthen ssl_printf(ssl, "error not a zone type. %s\n", arg2); 1215933707f3Ssthen free(nm); 121677079be7Ssthen return 0; 1217933707f3Ssthen } 121877079be7Ssthen lock_rw_wrlock(&zones->lock); 121977079be7Ssthen if((z=local_zones_find(zones, nm, nmlen, 1220933707f3Ssthen nmlabs, LDNS_RR_CLASS_IN))) { 1221933707f3Ssthen /* already present in tree */ 1222933707f3Ssthen lock_rw_wrlock(&z->lock); 1223933707f3Ssthen z->type = t; /* update type anyway */ 1224933707f3Ssthen lock_rw_unlock(&z->lock); 1225933707f3Ssthen free(nm); 122677079be7Ssthen lock_rw_unlock(&zones->lock); 122777079be7Ssthen return 1; 1228933707f3Ssthen } 122977079be7Ssthen if(!local_zones_add_zone(zones, nm, nmlen, 1230933707f3Ssthen nmlabs, LDNS_RR_CLASS_IN, t)) { 123177079be7Ssthen lock_rw_unlock(&zones->lock); 1232933707f3Ssthen ssl_printf(ssl, "error out of memory\n"); 123377079be7Ssthen return 0; 1234933707f3Ssthen } 123577079be7Ssthen lock_rw_unlock(&zones->lock); 123677079be7Ssthen return 1; 123777079be7Ssthen } 123877079be7Ssthen 123977079be7Ssthen /** Do the local_zone command */ 124077079be7Ssthen static void 124120237c55Ssthen do_zone_add(RES* ssl, struct local_zones* zones, char* arg) 124277079be7Ssthen { 124377079be7Ssthen if(!perform_zone_add(ssl, zones, arg)) 124477079be7Ssthen return; 1245933707f3Ssthen send_ok(ssl); 1246933707f3Ssthen } 1247933707f3Ssthen 124877079be7Ssthen /** Do the local_zones command */ 1249933707f3Ssthen static void 1250*98bc733bSsthen do_zones_add(struct daemon_remote* rc, RES* ssl, struct worker* worker) 125177079be7Ssthen { 1252*98bc733bSsthen char buf[MAX_CMD_STRLINE + MAX_STDIN_STRLINE] = "local_zone "; 125377079be7Ssthen int num = 0; 1254*98bc733bSsthen size_t cmd_len = strlen(buf); 1255*98bc733bSsthen while(ssl_read_line(ssl, buf+cmd_len, MAX_STDIN_STRLINE)) { 1256*98bc733bSsthen if(buf[0+cmd_len] == 0 || 1257*98bc733bSsthen (buf[0+cmd_len] == 0x04 && buf[1+cmd_len] == 0)) 1258d896b962Ssthen break; /* zero byte line or end of transmission */ 1259*98bc733bSsthen #ifdef THREADS_DISABLED 1260*98bc733bSsthen /* distribute single item command */ 1261*98bc733bSsthen if(rc) distribute_cmd(rc, ssl, buf); 1262*98bc733bSsthen #else 1263*98bc733bSsthen (void)rc; /* unused */ 1264*98bc733bSsthen #endif 1265*98bc733bSsthen if(!perform_zone_add(ssl, worker->daemon->local_zones, 1266*98bc733bSsthen buf+cmd_len)) { 1267*98bc733bSsthen if(!ssl_printf(ssl, "error for input line: %s\n", 1268*98bc733bSsthen buf+cmd_len)) 126977079be7Ssthen return; 127077079be7Ssthen } 1271*98bc733bSsthen else num++; 127277079be7Ssthen } 127377079be7Ssthen (void)ssl_printf(ssl, "added %d zones\n", num); 127477079be7Ssthen } 127577079be7Ssthen 127677079be7Ssthen /** Remove a zone */ 127777079be7Ssthen static int 127820237c55Ssthen perform_zone_remove(RES* ssl, struct local_zones* zones, char* arg) 1279933707f3Ssthen { 1280933707f3Ssthen uint8_t* nm; 1281933707f3Ssthen int nmlabs; 1282933707f3Ssthen size_t nmlen; 1283933707f3Ssthen struct local_zone* z; 1284933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 128577079be7Ssthen return 0; 128677079be7Ssthen lock_rw_wrlock(&zones->lock); 128777079be7Ssthen if((z=local_zones_find(zones, nm, nmlen, 1288933707f3Ssthen nmlabs, LDNS_RR_CLASS_IN))) { 1289933707f3Ssthen /* present in tree */ 129077079be7Ssthen local_zones_del_zone(zones, z); 1291933707f3Ssthen } 129277079be7Ssthen lock_rw_unlock(&zones->lock); 1293933707f3Ssthen free(nm); 129477079be7Ssthen return 1; 129577079be7Ssthen } 129677079be7Ssthen 129777079be7Ssthen /** Do the local_zone_remove command */ 129877079be7Ssthen static void 129920237c55Ssthen do_zone_remove(RES* ssl, struct local_zones* zones, char* arg) 130077079be7Ssthen { 130177079be7Ssthen if(!perform_zone_remove(ssl, zones, arg)) 130277079be7Ssthen return; 1303933707f3Ssthen send_ok(ssl); 1304933707f3Ssthen } 1305933707f3Ssthen 130677079be7Ssthen /** Do the local_zones_remove command */ 130777079be7Ssthen static void 1308*98bc733bSsthen do_zones_remove(struct daemon_remote* rc, RES* ssl, struct worker* worker) 130977079be7Ssthen { 1310*98bc733bSsthen char buf[MAX_CMD_STRLINE + MAX_STDIN_STRLINE] = "local_zone_remove "; 131177079be7Ssthen int num = 0; 1312*98bc733bSsthen size_t cmd_len = strlen(buf); 1313*98bc733bSsthen while(ssl_read_line(ssl, buf+cmd_len, MAX_STDIN_STRLINE)) { 1314*98bc733bSsthen if(buf[0+cmd_len] == 0 || 1315*98bc733bSsthen (buf[0+cmd_len] == 0x04 && buf[1+cmd_len] == 0)) 1316d896b962Ssthen break; /* zero byte line or end of transmission */ 1317*98bc733bSsthen #ifdef THREADS_DISABLED 1318*98bc733bSsthen /* distribute single item command */ 1319*98bc733bSsthen if(rc) distribute_cmd(rc, ssl, buf); 1320*98bc733bSsthen #else 1321*98bc733bSsthen (void)rc; /* unused */ 1322*98bc733bSsthen #endif 1323*98bc733bSsthen if(!perform_zone_remove(ssl, worker->daemon->local_zones, 1324*98bc733bSsthen buf+cmd_len)) { 1325*98bc733bSsthen if(!ssl_printf(ssl, "error for input line: %s\n", 1326*98bc733bSsthen buf+cmd_len)) 132777079be7Ssthen return; 132877079be7Ssthen } 1329*98bc733bSsthen else num++; 133077079be7Ssthen } 133177079be7Ssthen (void)ssl_printf(ssl, "removed %d zones\n", num); 133277079be7Ssthen } 133377079be7Ssthen 1334191f22c6Ssthen /** check syntax of newly added RR */ 1335191f22c6Ssthen static int 1336191f22c6Ssthen check_RR_syntax(RES* ssl, char* str, int line) 1337191f22c6Ssthen { 1338191f22c6Ssthen uint8_t rr[LDNS_RR_BUF_SIZE]; 1339191f22c6Ssthen size_t len = sizeof(rr), dname_len = 0; 1340191f22c6Ssthen int s = sldns_str2wire_rr_buf(str, rr, &len, &dname_len, 3600, 1341191f22c6Ssthen NULL, 0, NULL, 0); 1342191f22c6Ssthen if(s != 0) { 1343191f22c6Ssthen char linestr[32]; 1344191f22c6Ssthen if(line == 0) 1345191f22c6Ssthen linestr[0]=0; 1346191f22c6Ssthen else snprintf(linestr, sizeof(linestr), "line %d ", line); 1347191f22c6Ssthen if(!ssl_printf(ssl, "error parsing local-data at %sposition %d '%s': %s\n", 1348191f22c6Ssthen linestr, LDNS_WIREPARSE_OFFSET(s), str, 1349191f22c6Ssthen sldns_get_errorstr_parse(s))) 1350191f22c6Ssthen return 0; 1351191f22c6Ssthen return 0; 1352191f22c6Ssthen } 1353191f22c6Ssthen return 1; 1354191f22c6Ssthen } 1355191f22c6Ssthen 1356933707f3Ssthen /** Add new RR data */ 135777079be7Ssthen static int 1358191f22c6Ssthen perform_data_add(RES* ssl, struct local_zones* zones, char* arg, int line) 1359933707f3Ssthen { 1360191f22c6Ssthen if(!check_RR_syntax(ssl, arg, line)) { 1361191f22c6Ssthen return 0; 1362191f22c6Ssthen } 136377079be7Ssthen if(!local_zones_add_RR(zones, arg)) { 1364933707f3Ssthen ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg); 136577079be7Ssthen return 0; 1366933707f3Ssthen } 136777079be7Ssthen return 1; 136877079be7Ssthen } 136977079be7Ssthen 137077079be7Ssthen /** Do the local_data command */ 137177079be7Ssthen static void 137220237c55Ssthen do_data_add(RES* ssl, struct local_zones* zones, char* arg) 137377079be7Ssthen { 1374191f22c6Ssthen if(!perform_data_add(ssl, zones, arg, 0)) 137577079be7Ssthen return; 1376933707f3Ssthen send_ok(ssl); 1377933707f3Ssthen } 1378933707f3Ssthen 137977079be7Ssthen /** Do the local_datas command */ 1380933707f3Ssthen static void 1381*98bc733bSsthen do_datas_add(struct daemon_remote* rc, RES* ssl, struct worker* worker) 138277079be7Ssthen { 1383*98bc733bSsthen char buf[MAX_CMD_STRLINE + MAX_STDIN_STRLINE] = "local_data "; 1384191f22c6Ssthen int num = 0, line = 0; 1385*98bc733bSsthen size_t cmd_len = strlen(buf); 1386*98bc733bSsthen while(ssl_read_line(ssl, buf+cmd_len, MAX_STDIN_STRLINE)) { 1387*98bc733bSsthen if(buf[0+cmd_len] == 0 || 1388*98bc733bSsthen (buf[0+cmd_len] == 0x04 && buf[1+cmd_len] == 0)) 1389d896b962Ssthen break; /* zero byte line or end of transmission */ 1390*98bc733bSsthen #ifdef THREADS_DISABLED 1391*98bc733bSsthen /* distribute single item command */ 1392*98bc733bSsthen if(rc) distribute_cmd(rc, ssl, buf); 1393*98bc733bSsthen #else 1394*98bc733bSsthen (void)rc; /* unused */ 1395*98bc733bSsthen #endif 1396191f22c6Ssthen line++; 1397*98bc733bSsthen if(perform_data_add(ssl, worker->daemon->local_zones, 1398*98bc733bSsthen buf+cmd_len, line)) 139977079be7Ssthen num++; 140077079be7Ssthen } 140177079be7Ssthen (void)ssl_printf(ssl, "added %d datas\n", num); 140277079be7Ssthen } 140377079be7Ssthen 140477079be7Ssthen /** Remove RR data */ 140577079be7Ssthen static int 140620237c55Ssthen perform_data_remove(RES* ssl, struct local_zones* zones, char* arg) 1407933707f3Ssthen { 1408933707f3Ssthen uint8_t* nm; 1409933707f3Ssthen int nmlabs; 1410933707f3Ssthen size_t nmlen; 1411933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 141277079be7Ssthen return 0; 141377079be7Ssthen local_zones_del_data(zones, nm, 1414933707f3Ssthen nmlen, nmlabs, LDNS_RR_CLASS_IN); 1415933707f3Ssthen free(nm); 141677079be7Ssthen return 1; 141777079be7Ssthen } 141877079be7Ssthen 141977079be7Ssthen /** Do the local_data_remove command */ 142077079be7Ssthen static void 142120237c55Ssthen do_data_remove(RES* ssl, struct local_zones* zones, char* arg) 142277079be7Ssthen { 142377079be7Ssthen if(!perform_data_remove(ssl, zones, arg)) 142477079be7Ssthen return; 1425933707f3Ssthen send_ok(ssl); 1426933707f3Ssthen } 1427933707f3Ssthen 142877079be7Ssthen /** Do the local_datas_remove command */ 142977079be7Ssthen static void 1430*98bc733bSsthen do_datas_remove(struct daemon_remote* rc, RES* ssl, struct worker* worker) 143177079be7Ssthen { 1432*98bc733bSsthen char buf[MAX_CMD_STRLINE + MAX_STDIN_STRLINE] = "local_data_remove "; 143377079be7Ssthen int num = 0; 1434*98bc733bSsthen size_t cmd_len = strlen(buf); 1435*98bc733bSsthen while(ssl_read_line(ssl, buf+cmd_len, MAX_STDIN_STRLINE)) { 1436*98bc733bSsthen if(buf[0+cmd_len] == 0 || 1437*98bc733bSsthen (buf[0+cmd_len] == 0x04 && buf[1+cmd_len] == 0)) 1438d896b962Ssthen break; /* zero byte line or end of transmission */ 1439*98bc733bSsthen #ifdef THREADS_DISABLED 1440*98bc733bSsthen /* distribute single item command */ 1441*98bc733bSsthen if(rc) distribute_cmd(rc, ssl, buf); 1442*98bc733bSsthen #else 1443*98bc733bSsthen (void)rc; /* unused */ 1444*98bc733bSsthen #endif 1445*98bc733bSsthen if(!perform_data_remove(ssl, worker->daemon->local_zones, 1446*98bc733bSsthen buf+cmd_len)) { 1447*98bc733bSsthen if(!ssl_printf(ssl, "error for input line: %s\n", 1448*98bc733bSsthen buf+cmd_len)) 144977079be7Ssthen return; 145077079be7Ssthen } 1451*98bc733bSsthen else num++; 145277079be7Ssthen } 145377079be7Ssthen (void)ssl_printf(ssl, "removed %d datas\n", num); 145477079be7Ssthen } 145577079be7Ssthen 145677079be7Ssthen /** Add a new zone to view */ 145777079be7Ssthen static void 145820237c55Ssthen do_view_zone_add(RES* ssl, struct worker* worker, char* arg) 145977079be7Ssthen { 146077079be7Ssthen char* arg2; 146177079be7Ssthen struct view* v; 146277079be7Ssthen if(!find_arg2(ssl, arg, &arg2)) 146377079be7Ssthen return; 146477079be7Ssthen v = views_find_view(worker->daemon->views, 146577079be7Ssthen arg, 1 /* get write lock*/); 146677079be7Ssthen if(!v) { 146777079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 146877079be7Ssthen return; 146977079be7Ssthen } 14702be9e038Ssthen if(!v->local_zones) { 14712be9e038Ssthen if(!(v->local_zones = local_zones_create())){ 14722be9e038Ssthen lock_rw_unlock(&v->lock); 14732be9e038Ssthen ssl_printf(ssl,"error out of memory\n"); 14742be9e038Ssthen return; 14752be9e038Ssthen } 14762be9e038Ssthen if(!v->isfirst) { 14772be9e038Ssthen /* Global local-zone is not used for this view, 14782be9e038Ssthen * therefore add defaults to this view-specic 14792be9e038Ssthen * local-zone. */ 14802be9e038Ssthen struct config_file lz_cfg; 14812be9e038Ssthen memset(&lz_cfg, 0, sizeof(lz_cfg)); 14822be9e038Ssthen local_zone_enter_defaults(v->local_zones, &lz_cfg); 14832be9e038Ssthen } 14842be9e038Ssthen } 148577079be7Ssthen do_zone_add(ssl, v->local_zones, arg2); 148677079be7Ssthen lock_rw_unlock(&v->lock); 148777079be7Ssthen } 148877079be7Ssthen 148977079be7Ssthen /** Remove a zone from view */ 149077079be7Ssthen static void 149120237c55Ssthen do_view_zone_remove(RES* ssl, struct worker* worker, char* arg) 149277079be7Ssthen { 149377079be7Ssthen char* arg2; 149477079be7Ssthen struct view* v; 149577079be7Ssthen if(!find_arg2(ssl, arg, &arg2)) 149677079be7Ssthen return; 149777079be7Ssthen v = views_find_view(worker->daemon->views, 149877079be7Ssthen arg, 1 /* get write lock*/); 149977079be7Ssthen if(!v) { 150077079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 150177079be7Ssthen return; 150277079be7Ssthen } 15032be9e038Ssthen if(!v->local_zones) { 15042be9e038Ssthen lock_rw_unlock(&v->lock); 15052be9e038Ssthen send_ok(ssl); 15062be9e038Ssthen return; 15072be9e038Ssthen } 150877079be7Ssthen do_zone_remove(ssl, v->local_zones, arg2); 150977079be7Ssthen lock_rw_unlock(&v->lock); 151077079be7Ssthen } 151177079be7Ssthen 151277079be7Ssthen /** Add new RR data to view */ 151377079be7Ssthen static void 151420237c55Ssthen do_view_data_add(RES* ssl, struct worker* worker, char* arg) 151577079be7Ssthen { 151677079be7Ssthen char* arg2; 151777079be7Ssthen struct view* v; 151877079be7Ssthen if(!find_arg2(ssl, arg, &arg2)) 151977079be7Ssthen return; 152077079be7Ssthen v = views_find_view(worker->daemon->views, 152177079be7Ssthen arg, 1 /* get write lock*/); 152277079be7Ssthen if(!v) { 152377079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 152477079be7Ssthen return; 152577079be7Ssthen } 15262be9e038Ssthen if(!v->local_zones) { 15272be9e038Ssthen if(!(v->local_zones = local_zones_create())){ 15282be9e038Ssthen lock_rw_unlock(&v->lock); 15292be9e038Ssthen ssl_printf(ssl,"error out of memory\n"); 15302be9e038Ssthen return; 15312be9e038Ssthen } 15322be9e038Ssthen } 153377079be7Ssthen do_data_add(ssl, v->local_zones, arg2); 153477079be7Ssthen lock_rw_unlock(&v->lock); 153577079be7Ssthen } 153677079be7Ssthen 15373150e5f6Ssthen /** Add new RR data from stdin to view */ 15383150e5f6Ssthen static void 1539*98bc733bSsthen do_view_datas_add(struct daemon_remote* rc, RES* ssl, struct worker* worker, 1540*98bc733bSsthen char* arg) 15413150e5f6Ssthen { 15423150e5f6Ssthen struct view* v; 1543*98bc733bSsthen char buf[MAX_CMD_STRLINE + MAX_STDIN_STRLINE] = "view_local_data "; 1544*98bc733bSsthen size_t cmd_len; 1545*98bc733bSsthen int num = 0, line = 0; 15463150e5f6Ssthen v = views_find_view(worker->daemon->views, 15473150e5f6Ssthen arg, 1 /* get write lock*/); 15483150e5f6Ssthen if(!v) { 15493150e5f6Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 15503150e5f6Ssthen return; 15513150e5f6Ssthen } 15523150e5f6Ssthen if(!v->local_zones) { 15533150e5f6Ssthen if(!(v->local_zones = local_zones_create())){ 15543150e5f6Ssthen lock_rw_unlock(&v->lock); 15553150e5f6Ssthen ssl_printf(ssl,"error out of memory\n"); 15563150e5f6Ssthen return; 15573150e5f6Ssthen } 15583150e5f6Ssthen } 1559*98bc733bSsthen /* put the view name in the command buf */ 1560*98bc733bSsthen (void)snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s ", arg); 1561*98bc733bSsthen cmd_len = strlen(buf); 1562*98bc733bSsthen while(ssl_read_line(ssl, buf+cmd_len, MAX_STDIN_STRLINE)) { 1563*98bc733bSsthen if(buf[0+cmd_len] == 0 || 1564*98bc733bSsthen (buf[0+cmd_len] == 0x04 && buf[1+cmd_len] == 0)) 1565*98bc733bSsthen break; /* zero byte line or end of transmission */ 1566*98bc733bSsthen #ifdef THREADS_DISABLED 1567*98bc733bSsthen /* distribute single item command */ 1568*98bc733bSsthen if(rc) distribute_cmd(rc, ssl, buf); 1569*98bc733bSsthen #else 1570*98bc733bSsthen (void)rc; /* unused */ 1571*98bc733bSsthen #endif 1572*98bc733bSsthen line++; 1573*98bc733bSsthen if(perform_data_add(ssl, v->local_zones, buf+cmd_len, line)) 1574*98bc733bSsthen num++; 1575*98bc733bSsthen } 15763150e5f6Ssthen lock_rw_unlock(&v->lock); 1577*98bc733bSsthen (void)ssl_printf(ssl, "added %d datas\n", num); 15783150e5f6Ssthen } 15793150e5f6Ssthen 158077079be7Ssthen /** Remove RR data from view */ 158177079be7Ssthen static void 158220237c55Ssthen do_view_data_remove(RES* ssl, struct worker* worker, char* arg) 158377079be7Ssthen { 158477079be7Ssthen char* arg2; 158577079be7Ssthen struct view* v; 158677079be7Ssthen if(!find_arg2(ssl, arg, &arg2)) 158777079be7Ssthen return; 158877079be7Ssthen v = views_find_view(worker->daemon->views, 158977079be7Ssthen arg, 1 /* get write lock*/); 159077079be7Ssthen if(!v) { 159177079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 159277079be7Ssthen return; 159377079be7Ssthen } 15942be9e038Ssthen if(!v->local_zones) { 15952be9e038Ssthen lock_rw_unlock(&v->lock); 15962be9e038Ssthen send_ok(ssl); 15972be9e038Ssthen return; 15982be9e038Ssthen } 159977079be7Ssthen do_data_remove(ssl, v->local_zones, arg2); 160077079be7Ssthen lock_rw_unlock(&v->lock); 160177079be7Ssthen } 160277079be7Ssthen 1603eaf2578eSsthen /** Remove RR data from stdin from view */ 1604eaf2578eSsthen static void 1605*98bc733bSsthen do_view_datas_remove(struct daemon_remote* rc, RES* ssl, struct worker* worker, 1606*98bc733bSsthen char* arg) 1607eaf2578eSsthen { 1608eaf2578eSsthen struct view* v; 1609*98bc733bSsthen char buf[MAX_CMD_STRLINE + MAX_STDIN_STRLINE] = "view_local_data_remove "; 1610*98bc733bSsthen int num = 0; 1611*98bc733bSsthen size_t cmd_len; 1612eaf2578eSsthen v = views_find_view(worker->daemon->views, 1613eaf2578eSsthen arg, 1 /* get write lock*/); 1614eaf2578eSsthen if(!v) { 1615eaf2578eSsthen ssl_printf(ssl,"no view with name: %s\n", arg); 1616eaf2578eSsthen return; 1617eaf2578eSsthen } 1618eaf2578eSsthen if(!v->local_zones){ 1619eaf2578eSsthen lock_rw_unlock(&v->lock); 1620eaf2578eSsthen ssl_printf(ssl, "removed 0 datas\n"); 1621eaf2578eSsthen return; 1622eaf2578eSsthen } 1623*98bc733bSsthen /* put the view name in the command buf */ 1624*98bc733bSsthen (void)snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s ", arg); 1625*98bc733bSsthen cmd_len = strlen(buf); 1626*98bc733bSsthen while(ssl_read_line(ssl, buf+cmd_len, MAX_STDIN_STRLINE)) { 1627*98bc733bSsthen if(buf[0+cmd_len] == 0 || 1628*98bc733bSsthen (buf[0+cmd_len] == 0x04 && buf[1+cmd_len] == 0)) 1629*98bc733bSsthen break; /* zero byte line or end of transmission */ 1630*98bc733bSsthen #ifdef THREADS_DISABLED 1631*98bc733bSsthen /* distribute single item command */ 1632*98bc733bSsthen if(rc) distribute_cmd(rc, ssl, buf); 1633*98bc733bSsthen #else 1634*98bc733bSsthen (void)rc; /* unused */ 1635*98bc733bSsthen #endif 1636*98bc733bSsthen if(!perform_data_remove(ssl, v->local_zones, buf+cmd_len)) { 1637*98bc733bSsthen if(!ssl_printf(ssl, "error for input line: %s\n", 1638*98bc733bSsthen buf+cmd_len)) 1639*98bc733bSsthen return; 1640*98bc733bSsthen } 1641*98bc733bSsthen else num++; 1642*98bc733bSsthen } 1643eaf2578eSsthen lock_rw_unlock(&v->lock); 1644*98bc733bSsthen (void)ssl_printf(ssl, "removed %d datas\n", num); 1645eaf2578eSsthen } 1646eaf2578eSsthen 1647933707f3Ssthen /** cache lookup of nameservers */ 1648933707f3Ssthen static void 164920237c55Ssthen do_lookup(RES* ssl, struct worker* worker, char* arg) 1650933707f3Ssthen { 1651933707f3Ssthen uint8_t* nm; 1652933707f3Ssthen int nmlabs; 1653933707f3Ssthen size_t nmlen; 1654933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1655933707f3Ssthen return; 1656933707f3Ssthen (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs); 1657933707f3Ssthen free(nm); 1658933707f3Ssthen } 1659933707f3Ssthen 1660933707f3Ssthen /** flush something from rrset and msg caches */ 1661933707f3Ssthen static void 1662933707f3Ssthen do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen, 1663*98bc733bSsthen uint16_t t, uint16_t c, int remcachedb) 1664933707f3Ssthen { 166577079be7Ssthen hashvalue_type h; 1666933707f3Ssthen struct query_info k; 1667933707f3Ssthen rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0); 1668933707f3Ssthen if(t == LDNS_RR_TYPE_SOA) 1669933707f3Ssthen rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 1670933707f3Ssthen PACKED_RRSET_SOA_NEG); 1671933707f3Ssthen k.qname = nm; 1672933707f3Ssthen k.qname_len = nmlen; 1673933707f3Ssthen k.qtype = t; 1674933707f3Ssthen k.qclass = c; 167577079be7Ssthen k.local_alias = NULL; 167657dceb2aSbrad h = query_info_hash(&k, 0); 1677933707f3Ssthen slabhash_remove(worker->env.msg_cache, h, &k); 167857dceb2aSbrad if(t == LDNS_RR_TYPE_AAAA) { 167957dceb2aSbrad /* for AAAA also flush dns64 bit_cd packet */ 168057dceb2aSbrad h = query_info_hash(&k, BIT_CD); 168157dceb2aSbrad slabhash_remove(worker->env.msg_cache, h, &k); 168257dceb2aSbrad } 1683*98bc733bSsthen #ifdef USE_CACHEDB 1684*98bc733bSsthen if(remcachedb && worker->env.cachedb_enabled) 1685*98bc733bSsthen cachedb_msg_remove_qinfo(&worker->env, &k); 1686*98bc733bSsthen #else 1687*98bc733bSsthen (void)remcachedb; 1688*98bc733bSsthen #endif 1689*98bc733bSsthen } 1690*98bc733bSsthen 1691*98bc733bSsthen /** parse '+c' option, modifies string to return remainder. */ 1692*98bc733bSsthen static int 1693*98bc733bSsthen parse_remcachedb(RES* ssl, char** arg, int* pc) 1694*98bc733bSsthen { 1695*98bc733bSsthen *arg = skipwhite(*arg); 1696*98bc733bSsthen if((*arg)[0] == '+' && (*arg)[1] == 'c') { 1697*98bc733bSsthen char* arg2; 1698*98bc733bSsthen *pc = 1; 1699*98bc733bSsthen if(!find_arg2(ssl, *arg, &arg2)) 1700*98bc733bSsthen return 0; 1701*98bc733bSsthen *arg = arg2; 1702*98bc733bSsthen return 1; 1703*98bc733bSsthen } 1704*98bc733bSsthen /* The option was not found, no problem */ 1705*98bc733bSsthen return 1; 1706933707f3Ssthen } 1707933707f3Ssthen 1708933707f3Ssthen /** flush a type */ 1709933707f3Ssthen static void 171020237c55Ssthen do_flush_type(RES* ssl, struct worker* worker, char* arg) 1711933707f3Ssthen { 1712933707f3Ssthen uint8_t* nm; 1713933707f3Ssthen int nmlabs; 1714933707f3Ssthen size_t nmlen; 1715933707f3Ssthen char* arg2; 1716933707f3Ssthen uint16_t t; 1717*98bc733bSsthen int pc = 0; /* '+c' option */ 1718*98bc733bSsthen if(!parse_remcachedb(ssl, &arg, &pc)) 1719*98bc733bSsthen return; 1720933707f3Ssthen if(!find_arg2(ssl, arg, &arg2)) 1721933707f3Ssthen return; 1722933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1723933707f3Ssthen return; 17240b68ff31Ssthen t = sldns_get_rr_type_by_name(arg2); 17258b7325afSsthen if(t == 0 && strcmp(arg2, "TYPE0") != 0) { 1726*98bc733bSsthen (void)ssl_printf(ssl, "error parsing RRset type: '%s'\n", arg2); 1727*98bc733bSsthen free(nm); 17288b7325afSsthen return; 17298b7325afSsthen } 1730*98bc733bSsthen do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN, pc); 1731933707f3Ssthen 1732933707f3Ssthen free(nm); 1733933707f3Ssthen send_ok(ssl); 1734933707f3Ssthen } 1735933707f3Ssthen 1736933707f3Ssthen /** flush statistics */ 1737933707f3Ssthen static void 173820237c55Ssthen do_flush_stats(RES* ssl, struct worker* worker) 1739933707f3Ssthen { 1740933707f3Ssthen worker_stats_clear(worker); 1741933707f3Ssthen send_ok(ssl); 1742933707f3Ssthen } 1743933707f3Ssthen 1744933707f3Ssthen /** 1745933707f3Ssthen * Local info for deletion functions 1746933707f3Ssthen */ 1747933707f3Ssthen struct del_info { 1748933707f3Ssthen /** worker */ 1749933707f3Ssthen struct worker* worker; 1750933707f3Ssthen /** name to delete */ 1751933707f3Ssthen uint8_t* name; 1752933707f3Ssthen /** length */ 1753933707f3Ssthen size_t len; 1754933707f3Ssthen /** labels */ 1755933707f3Ssthen int labs; 1756933707f3Ssthen /** time to invalidate to */ 1757e9c7b4efSsthen time_t expired; 1758933707f3Ssthen /** number of rrsets removed */ 1759933707f3Ssthen size_t num_rrsets; 1760933707f3Ssthen /** number of msgs removed */ 1761933707f3Ssthen size_t num_msgs; 1762933707f3Ssthen /** number of key entries removed */ 1763933707f3Ssthen size_t num_keys; 1764933707f3Ssthen /** length of addr */ 1765933707f3Ssthen socklen_t addrlen; 1766933707f3Ssthen /** socket address for host deletion */ 1767933707f3Ssthen struct sockaddr_storage addr; 1768*98bc733bSsthen /** if cachedb information should be flushed too */ 1769*98bc733bSsthen int remcachedb; 1770933707f3Ssthen }; 1771933707f3Ssthen 1772933707f3Ssthen /** callback to delete hosts in infra cache */ 1773933707f3Ssthen static void 1774933707f3Ssthen infra_del_host(struct lruhash_entry* e, void* arg) 1775933707f3Ssthen { 1776933707f3Ssthen /* entry is locked */ 1777933707f3Ssthen struct del_info* inf = (struct del_info*)arg; 1778933707f3Ssthen struct infra_key* k = (struct infra_key*)e->key; 1779933707f3Ssthen if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) { 1780933707f3Ssthen struct infra_data* d = (struct infra_data*)e->data; 1781163a4143Ssthen d->probedelay = 0; 1782163a4143Ssthen d->timeout_A = 0; 1783163a4143Ssthen d->timeout_AAAA = 0; 1784163a4143Ssthen d->timeout_other = 0; 1785163a4143Ssthen rtt_init(&d->rtt); 1786a961b961Ssthen if(d->ttl > inf->expired) { 1787933707f3Ssthen d->ttl = inf->expired; 1788933707f3Ssthen inf->num_keys++; 1789933707f3Ssthen } 1790933707f3Ssthen } 1791933707f3Ssthen } 1792933707f3Ssthen 1793933707f3Ssthen /** flush infra cache */ 1794933707f3Ssthen static void 179520237c55Ssthen do_flush_infra(RES* ssl, struct worker* worker, char* arg) 1796933707f3Ssthen { 1797933707f3Ssthen struct sockaddr_storage addr; 1798933707f3Ssthen socklen_t len; 1799933707f3Ssthen struct del_info inf; 1800933707f3Ssthen if(strcmp(arg, "all") == 0) { 1801933707f3Ssthen slabhash_clear(worker->env.infra_cache->hosts); 1802933707f3Ssthen send_ok(ssl); 1803933707f3Ssthen return; 1804933707f3Ssthen } 1805933707f3Ssthen if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) { 1806933707f3Ssthen (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg); 1807933707f3Ssthen return; 1808933707f3Ssthen } 1809933707f3Ssthen /* delete all entries from cache */ 1810933707f3Ssthen /* what we do is to set them all expired */ 1811933707f3Ssthen inf.worker = worker; 1812933707f3Ssthen inf.name = 0; 1813933707f3Ssthen inf.len = 0; 1814933707f3Ssthen inf.labs = 0; 1815933707f3Ssthen inf.expired = *worker->env.now; 1816933707f3Ssthen inf.expired -= 3; /* handle 3 seconds skew between threads */ 1817933707f3Ssthen inf.num_rrsets = 0; 1818933707f3Ssthen inf.num_msgs = 0; 1819933707f3Ssthen inf.num_keys = 0; 1820933707f3Ssthen inf.addrlen = len; 1821*98bc733bSsthen inf.remcachedb = 0; 1822933707f3Ssthen memmove(&inf.addr, &addr, len); 1823933707f3Ssthen slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host, 1824933707f3Ssthen &inf); 1825933707f3Ssthen send_ok(ssl); 1826933707f3Ssthen } 1827933707f3Ssthen 1828933707f3Ssthen /** flush requestlist */ 1829933707f3Ssthen static void 183020237c55Ssthen do_flush_requestlist(RES* ssl, struct worker* worker) 1831933707f3Ssthen { 1832933707f3Ssthen mesh_delete_all(worker->env.mesh); 1833933707f3Ssthen send_ok(ssl); 1834933707f3Ssthen } 1835933707f3Ssthen 1836933707f3Ssthen /** callback to delete rrsets in a zone */ 1837933707f3Ssthen static void 1838933707f3Ssthen zone_del_rrset(struct lruhash_entry* e, void* arg) 1839933707f3Ssthen { 1840933707f3Ssthen /* entry is locked */ 1841933707f3Ssthen struct del_info* inf = (struct del_info*)arg; 1842933707f3Ssthen struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 1843933707f3Ssthen if(dname_subdomain_c(k->rk.dname, inf->name)) { 1844933707f3Ssthen struct packed_rrset_data* d = 1845933707f3Ssthen (struct packed_rrset_data*)e->data; 1846a961b961Ssthen if(d->ttl > inf->expired) { 1847933707f3Ssthen d->ttl = inf->expired; 1848933707f3Ssthen inf->num_rrsets++; 1849933707f3Ssthen } 1850933707f3Ssthen } 1851933707f3Ssthen } 1852933707f3Ssthen 1853933707f3Ssthen /** callback to delete messages in a zone */ 1854933707f3Ssthen static void 1855933707f3Ssthen zone_del_msg(struct lruhash_entry* e, void* arg) 1856933707f3Ssthen { 1857933707f3Ssthen /* entry is locked */ 1858933707f3Ssthen struct del_info* inf = (struct del_info*)arg; 1859933707f3Ssthen struct msgreply_entry* k = (struct msgreply_entry*)e->key; 1860933707f3Ssthen if(dname_subdomain_c(k->key.qname, inf->name)) { 1861933707f3Ssthen struct reply_info* d = (struct reply_info*)e->data; 1862a961b961Ssthen if(d->ttl > inf->expired) { 1863933707f3Ssthen d->ttl = inf->expired; 186420237c55Ssthen d->prefetch_ttl = inf->expired; 18652308e98cSsthen d->serve_expired_ttl = inf->expired; 1866933707f3Ssthen inf->num_msgs++; 1867933707f3Ssthen } 1868*98bc733bSsthen #ifdef USE_CACHEDB 1869*98bc733bSsthen if(inf->remcachedb && inf->worker->env.cachedb_enabled) 1870*98bc733bSsthen cachedb_msg_remove_qinfo(&inf->worker->env, &k->key); 1871*98bc733bSsthen #endif 1872933707f3Ssthen } 1873933707f3Ssthen } 1874933707f3Ssthen 1875933707f3Ssthen /** callback to delete keys in zone */ 1876933707f3Ssthen static void 1877933707f3Ssthen zone_del_kcache(struct lruhash_entry* e, void* arg) 1878933707f3Ssthen { 1879933707f3Ssthen /* entry is locked */ 1880933707f3Ssthen struct del_info* inf = (struct del_info*)arg; 1881933707f3Ssthen struct key_entry_key* k = (struct key_entry_key*)e->key; 1882933707f3Ssthen if(dname_subdomain_c(k->name, inf->name)) { 1883933707f3Ssthen struct key_entry_data* d = (struct key_entry_data*)e->data; 1884a961b961Ssthen if(d->ttl > inf->expired) { 1885933707f3Ssthen d->ttl = inf->expired; 1886933707f3Ssthen inf->num_keys++; 1887933707f3Ssthen } 1888933707f3Ssthen } 1889933707f3Ssthen } 1890933707f3Ssthen 1891933707f3Ssthen /** remove all rrsets and keys from zone from cache */ 1892933707f3Ssthen static void 189320237c55Ssthen do_flush_zone(RES* ssl, struct worker* worker, char* arg) 1894933707f3Ssthen { 1895933707f3Ssthen uint8_t* nm; 1896933707f3Ssthen int nmlabs; 1897933707f3Ssthen size_t nmlen; 1898933707f3Ssthen struct del_info inf; 1899*98bc733bSsthen int pc = 0; /* '+c' option */ 1900*98bc733bSsthen if(!parse_remcachedb(ssl, &arg, &pc)) 1901*98bc733bSsthen return; 1902933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1903933707f3Ssthen return; 1904933707f3Ssthen /* delete all RRs and key entries from zone */ 1905933707f3Ssthen /* what we do is to set them all expired */ 1906933707f3Ssthen inf.worker = worker; 1907933707f3Ssthen inf.name = nm; 1908933707f3Ssthen inf.len = nmlen; 1909933707f3Ssthen inf.labs = nmlabs; 1910933707f3Ssthen inf.expired = *worker->env.now; 1911933707f3Ssthen inf.expired -= 3; /* handle 3 seconds skew between threads */ 1912933707f3Ssthen inf.num_rrsets = 0; 1913933707f3Ssthen inf.num_msgs = 0; 1914933707f3Ssthen inf.num_keys = 0; 1915*98bc733bSsthen inf.remcachedb = pc; 1916933707f3Ssthen slabhash_traverse(&worker->env.rrset_cache->table, 1, 1917933707f3Ssthen &zone_del_rrset, &inf); 1918933707f3Ssthen 1919933707f3Ssthen slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf); 1920933707f3Ssthen 1921933707f3Ssthen /* and validator cache */ 1922933707f3Ssthen if(worker->env.key_cache) { 1923933707f3Ssthen slabhash_traverse(worker->env.key_cache->slab, 1, 1924933707f3Ssthen &zone_del_kcache, &inf); 1925933707f3Ssthen } 1926933707f3Ssthen 1927933707f3Ssthen free(nm); 1928933707f3Ssthen 1929e10d3884Sbrad (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1930e10d3884Sbrad "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1931e10d3884Sbrad (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1932933707f3Ssthen } 1933933707f3Ssthen 1934cebdf579Ssthen /** callback to delete bogus rrsets */ 1935cebdf579Ssthen static void 1936cebdf579Ssthen bogus_del_rrset(struct lruhash_entry* e, void* arg) 1937cebdf579Ssthen { 1938cebdf579Ssthen /* entry is locked */ 1939cebdf579Ssthen struct del_info* inf = (struct del_info*)arg; 1940cebdf579Ssthen struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 1941cebdf579Ssthen if(d->security == sec_status_bogus) { 1942cebdf579Ssthen d->ttl = inf->expired; 1943cebdf579Ssthen inf->num_rrsets++; 1944cebdf579Ssthen } 1945cebdf579Ssthen } 1946cebdf579Ssthen 1947cebdf579Ssthen /** callback to delete bogus messages */ 1948cebdf579Ssthen static void 1949cebdf579Ssthen bogus_del_msg(struct lruhash_entry* e, void* arg) 1950cebdf579Ssthen { 1951cebdf579Ssthen /* entry is locked */ 1952cebdf579Ssthen struct del_info* inf = (struct del_info*)arg; 1953cebdf579Ssthen struct reply_info* d = (struct reply_info*)e->data; 1954cebdf579Ssthen if(d->security == sec_status_bogus) { 1955cebdf579Ssthen d->ttl = inf->expired; 1956cebdf579Ssthen inf->num_msgs++; 1957*98bc733bSsthen #ifdef USE_CACHEDB 1958*98bc733bSsthen if(inf->remcachedb && inf->worker->env.cachedb_enabled) 1959*98bc733bSsthen cachedb_msg_remove_qinfo(&inf->worker->env, 1960*98bc733bSsthen &((struct msgreply_entry*)e->key)->key); 1961*98bc733bSsthen #endif 1962cebdf579Ssthen } 1963cebdf579Ssthen } 1964cebdf579Ssthen 1965cebdf579Ssthen /** callback to delete bogus keys */ 1966cebdf579Ssthen static void 1967cebdf579Ssthen bogus_del_kcache(struct lruhash_entry* e, void* arg) 1968cebdf579Ssthen { 1969cebdf579Ssthen /* entry is locked */ 1970cebdf579Ssthen struct del_info* inf = (struct del_info*)arg; 1971cebdf579Ssthen struct key_entry_data* d = (struct key_entry_data*)e->data; 1972cebdf579Ssthen if(d->isbad) { 1973cebdf579Ssthen d->ttl = inf->expired; 1974cebdf579Ssthen inf->num_keys++; 1975cebdf579Ssthen } 1976cebdf579Ssthen } 1977cebdf579Ssthen 1978e10d3884Sbrad /** remove all bogus rrsets, msgs and keys from cache */ 1979cebdf579Ssthen static void 1980*98bc733bSsthen do_flush_bogus(RES* ssl, struct worker* worker, char* arg) 1981cebdf579Ssthen { 1982cebdf579Ssthen struct del_info inf; 1983*98bc733bSsthen int pc = 0; /* '+c' option */ 1984*98bc733bSsthen if(!parse_remcachedb(ssl, &arg, &pc)) 1985*98bc733bSsthen return; 1986cebdf579Ssthen /* what we do is to set them all expired */ 1987cebdf579Ssthen inf.worker = worker; 1988cebdf579Ssthen inf.expired = *worker->env.now; 1989cebdf579Ssthen inf.expired -= 3; /* handle 3 seconds skew between threads */ 1990cebdf579Ssthen inf.num_rrsets = 0; 1991cebdf579Ssthen inf.num_msgs = 0; 1992cebdf579Ssthen inf.num_keys = 0; 1993*98bc733bSsthen inf.remcachedb = pc; 1994cebdf579Ssthen slabhash_traverse(&worker->env.rrset_cache->table, 1, 1995cebdf579Ssthen &bogus_del_rrset, &inf); 1996cebdf579Ssthen 1997cebdf579Ssthen slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf); 1998cebdf579Ssthen 1999cebdf579Ssthen /* and validator cache */ 2000cebdf579Ssthen if(worker->env.key_cache) { 2001cebdf579Ssthen slabhash_traverse(worker->env.key_cache->slab, 1, 2002cebdf579Ssthen &bogus_del_kcache, &inf); 2003cebdf579Ssthen } 2004cebdf579Ssthen 2005e10d3884Sbrad (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 2006e10d3884Sbrad "and %lu key entries\n", (unsigned long)inf.num_rrsets, 2007e10d3884Sbrad (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 2008e10d3884Sbrad } 2009e10d3884Sbrad 2010e10d3884Sbrad /** callback to delete negative and servfail rrsets */ 2011e10d3884Sbrad static void 2012e10d3884Sbrad negative_del_rrset(struct lruhash_entry* e, void* arg) 2013e10d3884Sbrad { 2014e10d3884Sbrad /* entry is locked */ 2015e10d3884Sbrad struct del_info* inf = (struct del_info*)arg; 2016e10d3884Sbrad struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 2017e10d3884Sbrad struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 2018e10d3884Sbrad /* delete the parentside negative cache rrsets, 2019bdfc4d55Sflorian * these are nameserver rrsets that failed lookup, rdata empty */ 2020e10d3884Sbrad if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 && 2021e10d3884Sbrad d->rrsig_count == 0 && d->rr_len[0] == 0) { 2022e10d3884Sbrad d->ttl = inf->expired; 2023e10d3884Sbrad inf->num_rrsets++; 2024e10d3884Sbrad } 2025e10d3884Sbrad } 2026e10d3884Sbrad 2027e10d3884Sbrad /** callback to delete negative and servfail messages */ 2028e10d3884Sbrad static void 2029e10d3884Sbrad negative_del_msg(struct lruhash_entry* e, void* arg) 2030e10d3884Sbrad { 2031e10d3884Sbrad /* entry is locked */ 2032e10d3884Sbrad struct del_info* inf = (struct del_info*)arg; 2033e10d3884Sbrad struct reply_info* d = (struct reply_info*)e->data; 2034e10d3884Sbrad /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error 2035e10d3884Sbrad * or NOERROR rcode with ANCOUNT==0: a NODATA answer */ 2036e10d3884Sbrad if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) { 2037e10d3884Sbrad d->ttl = inf->expired; 2038e10d3884Sbrad inf->num_msgs++; 2039*98bc733bSsthen #ifdef USE_CACHEDB 2040*98bc733bSsthen if(inf->remcachedb && inf->worker->env.cachedb_enabled) 2041*98bc733bSsthen cachedb_msg_remove_qinfo(&inf->worker->env, 2042*98bc733bSsthen &((struct msgreply_entry*)e->key)->key); 2043*98bc733bSsthen #endif 2044e10d3884Sbrad } 2045e10d3884Sbrad } 2046e10d3884Sbrad 2047e10d3884Sbrad /** callback to delete negative key entries */ 2048e10d3884Sbrad static void 2049e10d3884Sbrad negative_del_kcache(struct lruhash_entry* e, void* arg) 2050e10d3884Sbrad { 2051e10d3884Sbrad /* entry is locked */ 2052e10d3884Sbrad struct del_info* inf = (struct del_info*)arg; 2053e10d3884Sbrad struct key_entry_data* d = (struct key_entry_data*)e->data; 2054e10d3884Sbrad /* could be bad because of lookup failure on the DS, DNSKEY, which 2055e10d3884Sbrad * was nxdomain or servfail, and thus a result of negative lookups */ 2056e10d3884Sbrad if(d->isbad) { 2057e10d3884Sbrad d->ttl = inf->expired; 2058e10d3884Sbrad inf->num_keys++; 2059e10d3884Sbrad } 2060e10d3884Sbrad } 2061e10d3884Sbrad 2062e10d3884Sbrad /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */ 2063e10d3884Sbrad static void 2064*98bc733bSsthen do_flush_negative(RES* ssl, struct worker* worker, char* arg) 2065e10d3884Sbrad { 2066e10d3884Sbrad struct del_info inf; 2067*98bc733bSsthen int pc = 0; /* '+c' option */ 2068*98bc733bSsthen if(!parse_remcachedb(ssl, &arg, &pc)) 2069*98bc733bSsthen return; 2070e10d3884Sbrad /* what we do is to set them all expired */ 2071e10d3884Sbrad inf.worker = worker; 2072e10d3884Sbrad inf.expired = *worker->env.now; 2073e10d3884Sbrad inf.expired -= 3; /* handle 3 seconds skew between threads */ 2074e10d3884Sbrad inf.num_rrsets = 0; 2075e10d3884Sbrad inf.num_msgs = 0; 2076e10d3884Sbrad inf.num_keys = 0; 2077*98bc733bSsthen inf.remcachedb = pc; 2078e10d3884Sbrad slabhash_traverse(&worker->env.rrset_cache->table, 1, 2079e10d3884Sbrad &negative_del_rrset, &inf); 2080e10d3884Sbrad 2081e10d3884Sbrad slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf); 2082e10d3884Sbrad 2083e10d3884Sbrad /* and validator cache */ 2084e10d3884Sbrad if(worker->env.key_cache) { 2085e10d3884Sbrad slabhash_traverse(worker->env.key_cache->slab, 1, 2086e10d3884Sbrad &negative_del_kcache, &inf); 2087e10d3884Sbrad } 2088e10d3884Sbrad 2089e10d3884Sbrad (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 2090e10d3884Sbrad "and %lu key entries\n", (unsigned long)inf.num_rrsets, 2091e10d3884Sbrad (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 2092cebdf579Ssthen } 2093cebdf579Ssthen 2094933707f3Ssthen /** remove name rrset from cache */ 2095933707f3Ssthen static void 209620237c55Ssthen do_flush_name(RES* ssl, struct worker* w, char* arg) 2097933707f3Ssthen { 2098933707f3Ssthen uint8_t* nm; 2099933707f3Ssthen int nmlabs; 2100933707f3Ssthen size_t nmlen; 2101*98bc733bSsthen int pc = 0; /* '+c' option */ 2102*98bc733bSsthen if(!parse_remcachedb(ssl, &arg, &pc)) 2103*98bc733bSsthen return; 2104933707f3Ssthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2105933707f3Ssthen return; 2106*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN, pc); 2107*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN, pc); 2108*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN, pc); 2109*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN, pc); 2110*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN, pc); 2111*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN, pc); 2112*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN, pc); 2113*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN, pc); 2114*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN, pc); 2115*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN, pc); 2116*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SVCB, LDNS_RR_CLASS_IN, pc); 2117*98bc733bSsthen do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_HTTPS, LDNS_RR_CLASS_IN, pc); 2118933707f3Ssthen 2119933707f3Ssthen free(nm); 2120933707f3Ssthen send_ok(ssl); 2121933707f3Ssthen } 2122933707f3Ssthen 2123933707f3Ssthen /** printout a delegation point info */ 2124933707f3Ssthen static int 212520237c55Ssthen ssl_print_name_dp(RES* ssl, const char* str, uint8_t* nm, uint16_t dclass, 2126933707f3Ssthen struct delegpt* dp) 2127933707f3Ssthen { 2128933707f3Ssthen char buf[257]; 2129933707f3Ssthen struct delegpt_ns* ns; 2130933707f3Ssthen struct delegpt_addr* a; 2131933707f3Ssthen int f = 0; 2132933707f3Ssthen if(str) { /* print header for forward, stub */ 21330b68ff31Ssthen char* c = sldns_wire2str_class(dclass); 2134933707f3Ssthen dname_str(nm, buf); 2135e10d3884Sbrad if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) { 2136933707f3Ssthen free(c); 2137933707f3Ssthen return 0; 2138933707f3Ssthen } 2139933707f3Ssthen free(c); 2140933707f3Ssthen } 2141933707f3Ssthen for(ns = dp->nslist; ns; ns = ns->next) { 2142933707f3Ssthen dname_str(ns->name, buf); 2143933707f3Ssthen if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 2144933707f3Ssthen return 0; 2145933707f3Ssthen f = 1; 2146933707f3Ssthen } 2147933707f3Ssthen for(a = dp->target_list; a; a = a->next_target) { 2148933707f3Ssthen addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf)); 2149933707f3Ssthen if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 2150933707f3Ssthen return 0; 2151933707f3Ssthen f = 1; 2152933707f3Ssthen } 2153933707f3Ssthen return ssl_printf(ssl, "\n"); 2154933707f3Ssthen } 2155933707f3Ssthen 2156933707f3Ssthen 2157933707f3Ssthen /** print root forwards */ 2158933707f3Ssthen static int 215920237c55Ssthen print_root_fwds(RES* ssl, struct iter_forwards* fwds, uint8_t* root) 2160933707f3Ssthen { 2161933707f3Ssthen struct delegpt* dp; 21622bdc0ed1Ssthen int nolock = 0; 21632bdc0ed1Ssthen dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN, nolock); 21642bdc0ed1Ssthen if(!dp) { 2165933707f3Ssthen return ssl_printf(ssl, "off (using root hints)\n"); 21662bdc0ed1Ssthen } 2167933707f3Ssthen /* if dp is returned it must be the root */ 2168933707f3Ssthen log_assert(query_dname_compare(dp->name, root)==0); 21692bdc0ed1Ssthen if(!ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp)) { 21702bdc0ed1Ssthen lock_rw_unlock(&fwds->lock); 21712bdc0ed1Ssthen return 0; 21722bdc0ed1Ssthen } 21732bdc0ed1Ssthen lock_rw_unlock(&fwds->lock); 21742bdc0ed1Ssthen return 1; 2175933707f3Ssthen } 2176933707f3Ssthen 2177933707f3Ssthen /** parse args into delegpt */ 2178933707f3Ssthen static struct delegpt* 2179e21c60efSsthen parse_delegpt(RES* ssl, char* args, uint8_t* nm) 2180933707f3Ssthen { 2181933707f3Ssthen /* parse args and add in */ 2182933707f3Ssthen char* p = args; 2183933707f3Ssthen char* todo; 2184163a4143Ssthen struct delegpt* dp = delegpt_create_mlc(nm); 2185933707f3Ssthen struct sockaddr_storage addr; 2186933707f3Ssthen socklen_t addrlen; 218720237c55Ssthen char* auth_name; 2188163a4143Ssthen if(!dp) { 2189933707f3Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2190933707f3Ssthen return NULL; 2191933707f3Ssthen } 2192933707f3Ssthen while(p) { 2193933707f3Ssthen todo = p; 2194933707f3Ssthen p = strchr(p, ' '); /* find next spot, if any */ 2195933707f3Ssthen if(p) { 2196933707f3Ssthen *p++ = 0; /* end this spot */ 2197933707f3Ssthen p = skipwhite(p); /* position at next spot */ 2198933707f3Ssthen } 2199933707f3Ssthen /* parse address */ 220020237c55Ssthen if(!authextstrtoaddr(todo, &addr, &addrlen, &auth_name)) { 2201e21c60efSsthen uint8_t* dname= NULL; 2202e21c60efSsthen int port; 2203e21c60efSsthen dname = authextstrtodname(todo, &port, &auth_name); 2204e21c60efSsthen if(!dname) { 2205e21c60efSsthen (void)ssl_printf(ssl, "error cannot parse" 2206163a4143Ssthen " '%s'\n", todo); 2207163a4143Ssthen delegpt_free_mlc(dp); 2208933707f3Ssthen return NULL; 2209933707f3Ssthen } 2210e21c60efSsthen #if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST) 2211e21c60efSsthen if(auth_name) 2212e21c60efSsthen log_err("no name verification functionality in " 2213e21c60efSsthen "ssl library, ignored name for %s", todo); 2214e21c60efSsthen #endif 2215e21c60efSsthen if(!delegpt_add_ns_mlc(dp, dname, 0, auth_name, port)) { 2216933707f3Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2217e21c60efSsthen free(dname); 2218163a4143Ssthen delegpt_free_mlc(dp); 2219163a4143Ssthen return NULL; 2220163a4143Ssthen } 2221163a4143Ssthen } else { 2222c3b38330Ssthen #if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST) 22232308e98cSsthen if(auth_name) 22242308e98cSsthen log_err("no name verification functionality in " 22252308e98cSsthen "ssl library, ignored name for %s", todo); 22262308e98cSsthen #endif 2227163a4143Ssthen /* add address */ 222820237c55Ssthen if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0, 2229e21c60efSsthen auth_name, -1)) { 2230163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2231163a4143Ssthen delegpt_free_mlc(dp); 2232163a4143Ssthen return NULL; 2233163a4143Ssthen } 2234163a4143Ssthen } 2235933707f3Ssthen } 2236a961b961Ssthen dp->has_parent_side_NS = 1; 2237933707f3Ssthen return dp; 2238933707f3Ssthen } 2239933707f3Ssthen 2240*98bc733bSsthen /** do the forward command */ 2241933707f3Ssthen static void 224220237c55Ssthen do_forward(RES* ssl, struct worker* worker, char* args) 2243933707f3Ssthen { 2244933707f3Ssthen struct iter_forwards* fwd = worker->env.fwds; 2245933707f3Ssthen uint8_t* root = (uint8_t*)"\000"; 22462bdc0ed1Ssthen int nolock = 0; 2247933707f3Ssthen if(!fwd) { 2248933707f3Ssthen (void)ssl_printf(ssl, "error: structure not allocated\n"); 2249933707f3Ssthen return; 2250933707f3Ssthen } 2251933707f3Ssthen if(args == NULL || args[0] == 0) { 2252933707f3Ssthen (void)print_root_fwds(ssl, fwd, root); 2253933707f3Ssthen return; 2254933707f3Ssthen } 2255933707f3Ssthen /* set root forwards for this thread. since we are in remote control 2256933707f3Ssthen * the actual mesh is not running, so we can freely edit it. */ 2257933707f3Ssthen /* delete all the existing queries first */ 2258933707f3Ssthen mesh_delete_all(worker->env.mesh); 2259933707f3Ssthen if(strcmp(args, "off") == 0) { 22602bdc0ed1Ssthen forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root, nolock); 2261933707f3Ssthen } else { 2262933707f3Ssthen struct delegpt* dp; 2263e21c60efSsthen if(!(dp = parse_delegpt(ssl, args, root))) 2264933707f3Ssthen return; 22652bdc0ed1Ssthen if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp, nolock)) { 2266933707f3Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2267933707f3Ssthen return; 2268933707f3Ssthen } 2269933707f3Ssthen } 2270933707f3Ssthen send_ok(ssl); 2271933707f3Ssthen } 2272933707f3Ssthen 2273163a4143Ssthen static int 227420237c55Ssthen parse_fs_args(RES* ssl, char* args, uint8_t** nm, struct delegpt** dp, 22752bdc0ed1Ssthen int* insecure, int* prime, int* tls) 2276163a4143Ssthen { 2277163a4143Ssthen char* zonename; 2278163a4143Ssthen char* rest; 2279163a4143Ssthen size_t nmlen; 2280163a4143Ssthen int nmlabs; 2281163a4143Ssthen /* parse all -x args */ 2282163a4143Ssthen while(args[0] == '+') { 2283163a4143Ssthen if(!find_arg2(ssl, args, &rest)) 2284163a4143Ssthen return 0; 2285163a4143Ssthen while(*(++args) != 0) { 2286163a4143Ssthen if(*args == 'i' && insecure) 2287163a4143Ssthen *insecure = 1; 2288163a4143Ssthen else if(*args == 'p' && prime) 2289163a4143Ssthen *prime = 1; 22902bdc0ed1Ssthen else if(*args == 't' && tls) 22912bdc0ed1Ssthen *tls = 1; 2292163a4143Ssthen else { 2293163a4143Ssthen (void)ssl_printf(ssl, "error: unknown option %s\n", args); 2294163a4143Ssthen return 0; 2295163a4143Ssthen } 2296163a4143Ssthen } 2297163a4143Ssthen args = rest; 2298163a4143Ssthen } 2299163a4143Ssthen /* parse name */ 2300163a4143Ssthen if(dp) { 2301163a4143Ssthen if(!find_arg2(ssl, args, &rest)) 2302163a4143Ssthen return 0; 2303163a4143Ssthen zonename = args; 2304163a4143Ssthen args = rest; 2305163a4143Ssthen } else zonename = args; 2306163a4143Ssthen if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs)) 2307163a4143Ssthen return 0; 2308163a4143Ssthen 2309163a4143Ssthen /* parse dp */ 2310163a4143Ssthen if(dp) { 2311e21c60efSsthen if(!(*dp = parse_delegpt(ssl, args, *nm))) { 2312163a4143Ssthen free(*nm); 2313163a4143Ssthen return 0; 2314163a4143Ssthen } 2315163a4143Ssthen } 2316163a4143Ssthen return 1; 2317163a4143Ssthen } 2318163a4143Ssthen 2319163a4143Ssthen /** do the forward_add command */ 2320163a4143Ssthen static void 232120237c55Ssthen do_forward_add(RES* ssl, struct worker* worker, char* args) 2322163a4143Ssthen { 2323163a4143Ssthen struct iter_forwards* fwd = worker->env.fwds; 23242bdc0ed1Ssthen int insecure = 0, tls = 0; 2325163a4143Ssthen uint8_t* nm = NULL; 2326163a4143Ssthen struct delegpt* dp = NULL; 23272bdc0ed1Ssthen int nolock = 1; 23282bdc0ed1Ssthen if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL, &tls)) 2329163a4143Ssthen return; 23302bdc0ed1Ssthen if(tls) 23312bdc0ed1Ssthen dp->ssl_upstream = 1; 23322bdc0ed1Ssthen /* prelock forwarders for atomic operation with anchors */ 23332bdc0ed1Ssthen lock_rw_wrlock(&fwd->lock); 23340b68ff31Ssthen if(insecure && worker->env.anchors) { 2335163a4143Ssthen if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2336163a4143Ssthen nm)) { 23372bdc0ed1Ssthen lock_rw_unlock(&fwd->lock); 2338163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2339163a4143Ssthen delegpt_free_mlc(dp); 2340163a4143Ssthen free(nm); 2341163a4143Ssthen return; 2342163a4143Ssthen } 2343163a4143Ssthen } 23442bdc0ed1Ssthen if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp, nolock)) { 23452bdc0ed1Ssthen lock_rw_unlock(&fwd->lock); 2346163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2347163a4143Ssthen free(nm); 2348163a4143Ssthen return; 2349163a4143Ssthen } 23502bdc0ed1Ssthen lock_rw_unlock(&fwd->lock); 2351163a4143Ssthen free(nm); 2352163a4143Ssthen send_ok(ssl); 2353163a4143Ssthen } 2354163a4143Ssthen 2355163a4143Ssthen /** do the forward_remove command */ 2356163a4143Ssthen static void 235720237c55Ssthen do_forward_remove(RES* ssl, struct worker* worker, char* args) 2358163a4143Ssthen { 2359163a4143Ssthen struct iter_forwards* fwd = worker->env.fwds; 2360163a4143Ssthen int insecure = 0; 2361163a4143Ssthen uint8_t* nm = NULL; 23622bdc0ed1Ssthen int nolock = 1; 23632bdc0ed1Ssthen if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL, NULL)) 2364163a4143Ssthen return; 23652bdc0ed1Ssthen /* prelock forwarders for atomic operation with anchors */ 23662bdc0ed1Ssthen lock_rw_wrlock(&fwd->lock); 23670b68ff31Ssthen if(insecure && worker->env.anchors) 2368163a4143Ssthen anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2369163a4143Ssthen nm); 23702bdc0ed1Ssthen forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm, nolock); 23712bdc0ed1Ssthen lock_rw_unlock(&fwd->lock); 2372163a4143Ssthen free(nm); 2373163a4143Ssthen send_ok(ssl); 2374163a4143Ssthen } 2375163a4143Ssthen 2376163a4143Ssthen /** do the stub_add command */ 2377163a4143Ssthen static void 237820237c55Ssthen do_stub_add(RES* ssl, struct worker* worker, char* args) 2379163a4143Ssthen { 2380163a4143Ssthen struct iter_forwards* fwd = worker->env.fwds; 23812bdc0ed1Ssthen int insecure = 0, prime = 0, tls = 0; 2382163a4143Ssthen uint8_t* nm = NULL; 2383163a4143Ssthen struct delegpt* dp = NULL; 23842bdc0ed1Ssthen int nolock = 1; 23852bdc0ed1Ssthen if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime, &tls)) 2386163a4143Ssthen return; 23872bdc0ed1Ssthen if(tls) 23882bdc0ed1Ssthen dp->ssl_upstream = 1; 23892bdc0ed1Ssthen /* prelock forwarders and hints for atomic operation with anchors */ 23902bdc0ed1Ssthen lock_rw_wrlock(&fwd->lock); 23912bdc0ed1Ssthen lock_rw_wrlock(&worker->env.hints->lock); 23920b68ff31Ssthen if(insecure && worker->env.anchors) { 2393163a4143Ssthen if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2394163a4143Ssthen nm)) { 23952bdc0ed1Ssthen lock_rw_unlock(&fwd->lock); 23962bdc0ed1Ssthen lock_rw_unlock(&worker->env.hints->lock); 2397163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2398163a4143Ssthen delegpt_free_mlc(dp); 2399163a4143Ssthen free(nm); 2400163a4143Ssthen return; 2401163a4143Ssthen } 2402163a4143Ssthen } 24032bdc0ed1Ssthen if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm, nolock)) { 24040b68ff31Ssthen if(insecure && worker->env.anchors) 24050b68ff31Ssthen anchors_delete_insecure(worker->env.anchors, 2406163a4143Ssthen LDNS_RR_CLASS_IN, nm); 24072bdc0ed1Ssthen lock_rw_unlock(&fwd->lock); 24082bdc0ed1Ssthen lock_rw_unlock(&worker->env.hints->lock); 2409163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 2410163a4143Ssthen delegpt_free_mlc(dp); 2411163a4143Ssthen free(nm); 2412163a4143Ssthen return; 2413163a4143Ssthen } 24142bdc0ed1Ssthen if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime, 24152bdc0ed1Ssthen nolock)) { 2416163a4143Ssthen (void)ssl_printf(ssl, "error out of memory\n"); 24172bdc0ed1Ssthen forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm, nolock); 24180b68ff31Ssthen if(insecure && worker->env.anchors) 24190b68ff31Ssthen anchors_delete_insecure(worker->env.anchors, 2420163a4143Ssthen LDNS_RR_CLASS_IN, nm); 24212bdc0ed1Ssthen lock_rw_unlock(&fwd->lock); 24222bdc0ed1Ssthen lock_rw_unlock(&worker->env.hints->lock); 2423163a4143Ssthen free(nm); 2424163a4143Ssthen return; 2425163a4143Ssthen } 24262bdc0ed1Ssthen lock_rw_unlock(&fwd->lock); 24272bdc0ed1Ssthen lock_rw_unlock(&worker->env.hints->lock); 2428163a4143Ssthen free(nm); 2429163a4143Ssthen send_ok(ssl); 2430163a4143Ssthen } 2431163a4143Ssthen 2432163a4143Ssthen /** do the stub_remove command */ 2433163a4143Ssthen static void 243420237c55Ssthen do_stub_remove(RES* ssl, struct worker* worker, char* args) 2435163a4143Ssthen { 2436163a4143Ssthen struct iter_forwards* fwd = worker->env.fwds; 2437163a4143Ssthen int insecure = 0; 2438163a4143Ssthen uint8_t* nm = NULL; 24392bdc0ed1Ssthen int nolock = 1; 24402bdc0ed1Ssthen if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL, NULL)) 2441163a4143Ssthen return; 24422bdc0ed1Ssthen /* prelock forwarders and hints for atomic operation with anchors */ 24432bdc0ed1Ssthen lock_rw_wrlock(&fwd->lock); 24442bdc0ed1Ssthen lock_rw_wrlock(&worker->env.hints->lock); 24450b68ff31Ssthen if(insecure && worker->env.anchors) 2446163a4143Ssthen anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2447163a4143Ssthen nm); 24482bdc0ed1Ssthen forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm, nolock); 24492bdc0ed1Ssthen hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm, nolock); 24502bdc0ed1Ssthen lock_rw_unlock(&fwd->lock); 24512bdc0ed1Ssthen lock_rw_unlock(&worker->env.hints->lock); 2452163a4143Ssthen free(nm); 2453163a4143Ssthen send_ok(ssl); 2454163a4143Ssthen } 2455163a4143Ssthen 2456e9c7b4efSsthen /** do the insecure_add command */ 2457e9c7b4efSsthen static void 245820237c55Ssthen do_insecure_add(RES* ssl, struct worker* worker, char* arg) 2459e9c7b4efSsthen { 2460e9c7b4efSsthen size_t nmlen; 2461e9c7b4efSsthen int nmlabs; 2462e9c7b4efSsthen uint8_t* nm = NULL; 2463e9c7b4efSsthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2464e9c7b4efSsthen return; 24650b68ff31Ssthen if(worker->env.anchors) { 24660b68ff31Ssthen if(!anchors_add_insecure(worker->env.anchors, 24670b68ff31Ssthen LDNS_RR_CLASS_IN, nm)) { 2468e9c7b4efSsthen (void)ssl_printf(ssl, "error out of memory\n"); 2469e9c7b4efSsthen free(nm); 2470e9c7b4efSsthen return; 2471e9c7b4efSsthen } 24720b68ff31Ssthen } 2473e9c7b4efSsthen free(nm); 2474e9c7b4efSsthen send_ok(ssl); 2475e9c7b4efSsthen } 2476e9c7b4efSsthen 2477e9c7b4efSsthen /** do the insecure_remove command */ 2478e9c7b4efSsthen static void 247920237c55Ssthen do_insecure_remove(RES* ssl, struct worker* worker, char* arg) 2480e9c7b4efSsthen { 2481e9c7b4efSsthen size_t nmlen; 2482e9c7b4efSsthen int nmlabs; 2483e9c7b4efSsthen uint8_t* nm = NULL; 2484e9c7b4efSsthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2485e9c7b4efSsthen return; 24860b68ff31Ssthen if(worker->env.anchors) 24870b68ff31Ssthen anchors_delete_insecure(worker->env.anchors, 24880b68ff31Ssthen LDNS_RR_CLASS_IN, nm); 2489e9c7b4efSsthen free(nm); 2490e9c7b4efSsthen send_ok(ssl); 2491e9c7b4efSsthen } 2492e9c7b4efSsthen 2493a58bff56Ssthen static void 249420237c55Ssthen do_insecure_list(RES* ssl, struct worker* worker) 2495a58bff56Ssthen { 2496a58bff56Ssthen char buf[257]; 2497a58bff56Ssthen struct trust_anchor* a; 2498a58bff56Ssthen if(worker->env.anchors) { 2499a58bff56Ssthen RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) { 2500a58bff56Ssthen if(a->numDS == 0 && a->numDNSKEY == 0) { 2501a58bff56Ssthen dname_str(a->name, buf); 2502a58bff56Ssthen ssl_printf(ssl, "%s\n", buf); 2503a58bff56Ssthen } 2504a58bff56Ssthen } 2505a58bff56Ssthen } 2506a58bff56Ssthen } 2507a58bff56Ssthen 2508933707f3Ssthen /** do the status command */ 2509933707f3Ssthen static void 251020237c55Ssthen do_status(RES* ssl, struct worker* worker) 2511933707f3Ssthen { 2512933707f3Ssthen int i; 2513933707f3Ssthen time_t uptime; 2514933707f3Ssthen if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION)) 2515933707f3Ssthen return; 2516933707f3Ssthen if(!ssl_printf(ssl, "verbosity: %d\n", verbosity)) 2517933707f3Ssthen return; 2518933707f3Ssthen if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num)) 2519933707f3Ssthen return; 2520933707f3Ssthen if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num)) 2521933707f3Ssthen return; 2522933707f3Ssthen for(i=0; i<worker->daemon->mods.num; i++) { 2523933707f3Ssthen if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name)) 2524933707f3Ssthen return; 2525933707f3Ssthen } 2526933707f3Ssthen if(!ssl_printf(ssl, " ]\n")) 2527933707f3Ssthen return; 2528933707f3Ssthen uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec; 25290b68ff31Ssthen if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime)) 2530933707f3Ssthen return; 253120237c55Ssthen if(!ssl_printf(ssl, "options:%s%s%s%s\n" , 2532e10d3884Sbrad (worker->daemon->reuseport?" reuseport":""), 253320237c55Ssthen (worker->daemon->rc->accept_list?" control":""), 253420237c55Ssthen (worker->daemon->rc->accept_list && worker->daemon->rc->use_cert?"(ssl)":""), 253520237c55Ssthen (worker->daemon->rc->accept_list && worker->daemon->cfg->control_ifs.first && worker->daemon->cfg->control_ifs.first->str && worker->daemon->cfg->control_ifs.first->str[0] == '/'?"(namedpipe)":"") 253620237c55Ssthen )) 2537e10d3884Sbrad return; 2538933707f3Ssthen if(!ssl_printf(ssl, "unbound (pid %d) is running...\n", 2539933707f3Ssthen (int)getpid())) 2540933707f3Ssthen return; 2541933707f3Ssthen } 2542933707f3Ssthen 2543933707f3Ssthen /** get age for the mesh state */ 2544933707f3Ssthen static void 2545933707f3Ssthen get_mesh_age(struct mesh_state* m, char* buf, size_t len, 2546933707f3Ssthen struct module_env* env) 2547933707f3Ssthen { 2548933707f3Ssthen if(m->reply_list) { 2549933707f3Ssthen struct timeval d; 2550933707f3Ssthen struct mesh_reply* r = m->reply_list; 2551933707f3Ssthen /* last reply is the oldest */ 2552933707f3Ssthen while(r && r->next) 2553933707f3Ssthen r = r->next; 2554933707f3Ssthen timeval_subtract(&d, env->now_tv, &r->start_time); 25550b68ff31Ssthen snprintf(buf, len, ARG_LL "d.%6.6d", 25560b68ff31Ssthen (long long)d.tv_sec, (int)d.tv_usec); 2557933707f3Ssthen } else { 2558933707f3Ssthen snprintf(buf, len, "-"); 2559933707f3Ssthen } 2560933707f3Ssthen } 2561933707f3Ssthen 2562933707f3Ssthen /** get status of a mesh state */ 2563933707f3Ssthen static void 2564933707f3Ssthen get_mesh_status(struct mesh_area* mesh, struct mesh_state* m, 2565933707f3Ssthen char* buf, size_t len) 2566933707f3Ssthen { 2567933707f3Ssthen enum module_ext_state s = m->s.ext_state[m->s.curmod]; 2568933707f3Ssthen const char *modname = mesh->mods.mod[m->s.curmod]->name; 2569933707f3Ssthen size_t l; 2570933707f3Ssthen if(strcmp(modname, "iterator") == 0 && s == module_wait_reply && 2571933707f3Ssthen m->s.minfo[m->s.curmod]) { 2572933707f3Ssthen /* break into iterator to find out who its waiting for */ 2573933707f3Ssthen struct iter_qstate* qstate = (struct iter_qstate*) 2574933707f3Ssthen m->s.minfo[m->s.curmod]; 2575933707f3Ssthen struct outbound_list* ol = &qstate->outlist; 2576933707f3Ssthen struct outbound_entry* e; 2577933707f3Ssthen snprintf(buf, len, "%s wait for", modname); 2578933707f3Ssthen l = strlen(buf); 2579933707f3Ssthen buf += l; len -= l; 2580933707f3Ssthen if(ol->first == NULL) 2581933707f3Ssthen snprintf(buf, len, " (empty_list)"); 2582933707f3Ssthen for(e = ol->first; e; e = e->next) { 2583933707f3Ssthen snprintf(buf, len, " "); 2584933707f3Ssthen l = strlen(buf); 2585933707f3Ssthen buf += l; len -= l; 2586933707f3Ssthen addr_to_str(&e->qsent->addr, e->qsent->addrlen, 2587933707f3Ssthen buf, len); 2588933707f3Ssthen l = strlen(buf); 2589933707f3Ssthen buf += l; len -= l; 2590933707f3Ssthen } 2591933707f3Ssthen } else if(s == module_wait_subquery) { 2592933707f3Ssthen /* look in subs from mesh state to see what */ 2593933707f3Ssthen char nm[257]; 2594933707f3Ssthen struct mesh_state_ref* sub; 2595933707f3Ssthen snprintf(buf, len, "%s wants", modname); 2596933707f3Ssthen l = strlen(buf); 2597933707f3Ssthen buf += l; len -= l; 2598933707f3Ssthen if(m->sub_set.count == 0) 2599933707f3Ssthen snprintf(buf, len, " (empty_list)"); 2600933707f3Ssthen RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) { 26010b68ff31Ssthen char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype); 26020b68ff31Ssthen char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass); 2603933707f3Ssthen dname_str(sub->s->s.qinfo.qname, nm); 26040b68ff31Ssthen snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"), 26050b68ff31Ssthen (c?c:"CLASS??"), nm); 2606933707f3Ssthen l = strlen(buf); 2607933707f3Ssthen buf += l; len -= l; 2608933707f3Ssthen free(t); 2609933707f3Ssthen free(c); 2610933707f3Ssthen } 2611933707f3Ssthen } else { 2612933707f3Ssthen snprintf(buf, len, "%s is %s", modname, strextstate(s)); 2613933707f3Ssthen } 2614933707f3Ssthen } 2615933707f3Ssthen 2616933707f3Ssthen /** do the dump_requestlist command */ 2617933707f3Ssthen static void 261820237c55Ssthen do_dump_requestlist(RES* ssl, struct worker* worker) 2619933707f3Ssthen { 2620933707f3Ssthen struct mesh_area* mesh; 2621933707f3Ssthen struct mesh_state* m; 2622933707f3Ssthen int num = 0; 2623933707f3Ssthen char buf[257]; 2624933707f3Ssthen char timebuf[32]; 2625933707f3Ssthen char statbuf[10240]; 2626933707f3Ssthen if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num)) 2627933707f3Ssthen return; 2628933707f3Ssthen if(!ssl_printf(ssl, "# type cl name seconds module status\n")) 2629933707f3Ssthen return; 2630933707f3Ssthen /* show worker mesh contents */ 2631933707f3Ssthen mesh = worker->env.mesh; 2632933707f3Ssthen if(!mesh) return; 2633933707f3Ssthen RBTREE_FOR(m, struct mesh_state*, &mesh->all) { 26340b68ff31Ssthen char* t = sldns_wire2str_type(m->s.qinfo.qtype); 26350b68ff31Ssthen char* c = sldns_wire2str_class(m->s.qinfo.qclass); 2636933707f3Ssthen dname_str(m->s.qinfo.qname, buf); 2637933707f3Ssthen get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env); 2638933707f3Ssthen get_mesh_status(mesh, m, statbuf, sizeof(statbuf)); 2639933707f3Ssthen if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n", 26400b68ff31Ssthen num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf, 26410b68ff31Ssthen statbuf)) { 2642933707f3Ssthen free(t); 2643933707f3Ssthen free(c); 2644933707f3Ssthen return; 2645933707f3Ssthen } 2646933707f3Ssthen num++; 2647933707f3Ssthen free(t); 2648933707f3Ssthen free(c); 2649933707f3Ssthen } 2650933707f3Ssthen } 2651933707f3Ssthen 2652933707f3Ssthen /** structure for argument data for dump infra host */ 2653933707f3Ssthen struct infra_arg { 2654933707f3Ssthen /** the infra cache */ 2655933707f3Ssthen struct infra_cache* infra; 2656933707f3Ssthen /** the SSL connection */ 265720237c55Ssthen RES* ssl; 2658933707f3Ssthen /** the time now */ 2659e9c7b4efSsthen time_t now; 2660e10d3884Sbrad /** ssl failure? stop writing and skip the rest. If the tcp 2661e10d3884Sbrad * connection is broken, and writes fail, we then stop writing. */ 2662e10d3884Sbrad int ssl_failed; 2663933707f3Ssthen }; 2664933707f3Ssthen 2665933707f3Ssthen /** callback for every host element in the infra cache */ 2666933707f3Ssthen static void 2667933707f3Ssthen dump_infra_host(struct lruhash_entry* e, void* arg) 2668933707f3Ssthen { 2669933707f3Ssthen struct infra_arg* a = (struct infra_arg*)arg; 2670933707f3Ssthen struct infra_key* k = (struct infra_key*)e->key; 2671933707f3Ssthen struct infra_data* d = (struct infra_data*)e->data; 2672933707f3Ssthen char ip_str[1024]; 2673933707f3Ssthen char name[257]; 26747191de28Ssthen int port; 2675e10d3884Sbrad if(a->ssl_failed) 2676e10d3884Sbrad return; 2677933707f3Ssthen addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str)); 2678933707f3Ssthen dname_str(k->zonename, name); 26797191de28Ssthen port = (int)ntohs(((struct sockaddr_in*)&k->addr)->sin_port); 26807191de28Ssthen if(port != UNBOUND_DNS_PORT) { 26817191de28Ssthen snprintf(ip_str+strlen(ip_str), sizeof(ip_str)-strlen(ip_str), 26827191de28Ssthen "@%d", port); 26837191de28Ssthen } 2684933707f3Ssthen /* skip expired stuff (only backed off) */ 2685933707f3Ssthen if(d->ttl < a->now) { 2686933707f3Ssthen if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) { 2687933707f3Ssthen if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str, 2688e10d3884Sbrad name, d->rtt.rto)) { 2689e10d3884Sbrad a->ssl_failed = 1; 2690e10d3884Sbrad return; 2691e10d3884Sbrad } 2692933707f3Ssthen } 2693933707f3Ssthen return; 2694933707f3Ssthen } 2695e10d3884Sbrad if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d " 2696163a4143Ssthen "tA %d tAAAA %d tother %d " 2697933707f3Ssthen "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d " 2698e10d3884Sbrad "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now), 2699933707f3Ssthen d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto, 2700163a4143Ssthen d->timeout_A, d->timeout_AAAA, d->timeout_other, 2701933707f3Ssthen (int)d->edns_lame_known, (int)d->edns_version, 2702a58bff56Ssthen (int)(a->now<d->probedelay?(d->probedelay - a->now):0), 2703933707f3Ssthen (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A, 2704e10d3884Sbrad (int)d->lame_other)) { 2705e10d3884Sbrad a->ssl_failed = 1; 2706933707f3Ssthen return; 2707933707f3Ssthen } 2708e10d3884Sbrad } 2709933707f3Ssthen 2710933707f3Ssthen /** do the dump_infra command */ 2711933707f3Ssthen static void 271220237c55Ssthen do_dump_infra(RES* ssl, struct worker* worker) 2713933707f3Ssthen { 2714933707f3Ssthen struct infra_arg arg; 2715933707f3Ssthen arg.infra = worker->env.infra_cache; 2716933707f3Ssthen arg.ssl = ssl; 2717933707f3Ssthen arg.now = *worker->env.now; 2718e10d3884Sbrad arg.ssl_failed = 0; 2719933707f3Ssthen slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg); 2720933707f3Ssthen } 2721933707f3Ssthen 2722933707f3Ssthen /** do the log_reopen command */ 2723933707f3Ssthen static void 272420237c55Ssthen do_log_reopen(RES* ssl, struct worker* worker) 2725933707f3Ssthen { 2726933707f3Ssthen struct config_file* cfg = worker->env.cfg; 2727933707f3Ssthen send_ok(ssl); 2728933707f3Ssthen log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir); 2729933707f3Ssthen } 2730933707f3Ssthen 27312308e98cSsthen /** do the auth_zone_reload command */ 27322308e98cSsthen static void 27332308e98cSsthen do_auth_zone_reload(RES* ssl, struct worker* worker, char* arg) 27342308e98cSsthen { 27352308e98cSsthen size_t nmlen; 27362308e98cSsthen int nmlabs; 27372308e98cSsthen uint8_t* nm = NULL; 27382308e98cSsthen struct auth_zones* az = worker->env.auth_zones; 27392308e98cSsthen struct auth_zone* z = NULL; 2740191f22c6Ssthen struct auth_xfer* xfr = NULL; 2741191f22c6Ssthen char* reason = NULL; 27422308e98cSsthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 27432308e98cSsthen return; 27442308e98cSsthen if(az) { 27452308e98cSsthen lock_rw_rdlock(&az->lock); 27462308e98cSsthen z = auth_zone_find(az, nm, nmlen, LDNS_RR_CLASS_IN); 27472308e98cSsthen if(z) { 27482308e98cSsthen lock_rw_wrlock(&z->lock); 27492308e98cSsthen } 2750191f22c6Ssthen xfr = auth_xfer_find(az, nm, nmlen, LDNS_RR_CLASS_IN); 2751191f22c6Ssthen if(xfr) { 2752191f22c6Ssthen lock_basic_lock(&xfr->lock); 2753191f22c6Ssthen } 27542308e98cSsthen lock_rw_unlock(&az->lock); 27552308e98cSsthen } 27562308e98cSsthen free(nm); 27572308e98cSsthen if(!z) { 2758191f22c6Ssthen if(xfr) { 2759191f22c6Ssthen lock_basic_unlock(&xfr->lock); 2760191f22c6Ssthen } 27612308e98cSsthen (void)ssl_printf(ssl, "error no auth-zone %s\n", arg); 27622308e98cSsthen return; 27632308e98cSsthen } 27643150e5f6Ssthen if(!auth_zone_read_zonefile(z, worker->env.cfg)) { 27652308e98cSsthen lock_rw_unlock(&z->lock); 2766191f22c6Ssthen if(xfr) { 2767191f22c6Ssthen lock_basic_unlock(&xfr->lock); 2768191f22c6Ssthen } 27692308e98cSsthen (void)ssl_printf(ssl, "error failed to read %s\n", arg); 27702308e98cSsthen return; 27712308e98cSsthen } 2772191f22c6Ssthen 2773191f22c6Ssthen z->zone_expired = 0; 2774191f22c6Ssthen if(xfr) { 2775191f22c6Ssthen xfr->zone_expired = 0; 2776191f22c6Ssthen if(!xfr_find_soa(z, xfr)) { 2777191f22c6Ssthen if(z->data.count == 0) { 27782308e98cSsthen lock_rw_unlock(&z->lock); 2779191f22c6Ssthen lock_basic_unlock(&xfr->lock); 2780191f22c6Ssthen (void)ssl_printf(ssl, "zone %s has no contents\n", arg); 2781191f22c6Ssthen return; 2782191f22c6Ssthen } 2783191f22c6Ssthen lock_rw_unlock(&z->lock); 2784191f22c6Ssthen lock_basic_unlock(&xfr->lock); 2785191f22c6Ssthen (void)ssl_printf(ssl, "error: no SOA in zone after read %s\n", arg); 2786191f22c6Ssthen return; 2787191f22c6Ssthen } 2788191f22c6Ssthen if(xfr->have_zone) 2789191f22c6Ssthen xfr->lease_time = *worker->env.now; 2790191f22c6Ssthen lock_basic_unlock(&xfr->lock); 2791191f22c6Ssthen } 2792191f22c6Ssthen 2793191f22c6Ssthen auth_zone_verify_zonemd(z, &worker->env, &worker->env.mesh->mods, 2794191f22c6Ssthen &reason, 0, 0); 2795191f22c6Ssthen if(reason && z->zone_expired) { 2796191f22c6Ssthen lock_rw_unlock(&z->lock); 2797191f22c6Ssthen (void)ssl_printf(ssl, "error zonemd for %s failed: %s\n", 2798191f22c6Ssthen arg, reason); 2799191f22c6Ssthen free(reason); 2800191f22c6Ssthen return; 2801191f22c6Ssthen } else if(reason && strcmp(reason, "ZONEMD verification successful") 2802191f22c6Ssthen ==0) { 2803191f22c6Ssthen (void)ssl_printf(ssl, "%s: %s\n", arg, reason); 2804191f22c6Ssthen } 2805191f22c6Ssthen lock_rw_unlock(&z->lock); 2806191f22c6Ssthen free(reason); 28072308e98cSsthen send_ok(ssl); 28082308e98cSsthen } 28092308e98cSsthen 28102308e98cSsthen /** do the auth_zone_transfer command */ 28112308e98cSsthen static void 28122308e98cSsthen do_auth_zone_transfer(RES* ssl, struct worker* worker, char* arg) 28132308e98cSsthen { 28142308e98cSsthen size_t nmlen; 28152308e98cSsthen int nmlabs; 28162308e98cSsthen uint8_t* nm = NULL; 28172308e98cSsthen struct auth_zones* az = worker->env.auth_zones; 28182308e98cSsthen if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 28192308e98cSsthen return; 28202308e98cSsthen if(!az || !auth_zones_startprobesequence(az, &worker->env, nm, nmlen, 28212308e98cSsthen LDNS_RR_CLASS_IN)) { 28222308e98cSsthen (void)ssl_printf(ssl, "error zone xfr task not found %s\n", arg); 2823eaf2578eSsthen free(nm); 28242308e98cSsthen return; 28252308e98cSsthen } 2826eaf2578eSsthen free(nm); 28272308e98cSsthen send_ok(ssl); 28282308e98cSsthen } 28292308e98cSsthen 2830933707f3Ssthen /** do the set_option command */ 2831933707f3Ssthen static void 283220237c55Ssthen do_set_option(RES* ssl, struct worker* worker, char* arg) 2833933707f3Ssthen { 2834933707f3Ssthen char* arg2; 2835933707f3Ssthen if(!find_arg2(ssl, arg, &arg2)) 2836933707f3Ssthen return; 2837933707f3Ssthen if(!config_set_option(worker->env.cfg, arg, arg2)) { 2838933707f3Ssthen (void)ssl_printf(ssl, "error setting option\n"); 2839933707f3Ssthen return; 2840933707f3Ssthen } 284177079be7Ssthen /* effectuate some arguments */ 284277079be7Ssthen if(strcmp(arg, "val-override-date:") == 0) { 284377079be7Ssthen int m = modstack_find(&worker->env.mesh->mods, "validator"); 284477079be7Ssthen struct val_env* val_env = NULL; 284577079be7Ssthen if(m != -1) val_env = (struct val_env*)worker->env.modinfo[m]; 284677079be7Ssthen if(val_env) 284777079be7Ssthen val_env->date_override = worker->env.cfg->val_date_override; 284877079be7Ssthen } 2849933707f3Ssthen send_ok(ssl); 2850933707f3Ssthen } 2851933707f3Ssthen 2852933707f3Ssthen /* routine to printout option values over SSL */ 2853933707f3Ssthen void remote_get_opt_ssl(char* line, void* arg) 2854933707f3Ssthen { 285520237c55Ssthen RES* ssl = (RES*)arg; 2856933707f3Ssthen (void)ssl_printf(ssl, "%s\n", line); 2857933707f3Ssthen } 2858933707f3Ssthen 2859933707f3Ssthen /** do the get_option command */ 2860933707f3Ssthen static void 286120237c55Ssthen do_get_option(RES* ssl, struct worker* worker, char* arg) 2862933707f3Ssthen { 2863933707f3Ssthen int r; 2864933707f3Ssthen r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl); 2865933707f3Ssthen if(!r) { 2866933707f3Ssthen (void)ssl_printf(ssl, "error unknown option\n"); 2867933707f3Ssthen return; 2868933707f3Ssthen } 2869933707f3Ssthen } 2870933707f3Ssthen 2871933707f3Ssthen /** do the list_forwards command */ 2872933707f3Ssthen static void 287320237c55Ssthen do_list_forwards(RES* ssl, struct worker* worker) 2874933707f3Ssthen { 2875933707f3Ssthen /* since its a per-worker structure no locks needed */ 2876933707f3Ssthen struct iter_forwards* fwds = worker->env.fwds; 2877933707f3Ssthen struct iter_forward_zone* z; 2878e10d3884Sbrad struct trust_anchor* a; 2879e10d3884Sbrad int insecure; 28802bdc0ed1Ssthen lock_rw_rdlock(&fwds->lock); 2881933707f3Ssthen RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) { 2882933707f3Ssthen if(!z->dp) continue; /* skip empty marker for stub */ 2883e10d3884Sbrad 2884e10d3884Sbrad /* see if it is insecure */ 2885e10d3884Sbrad insecure = 0; 2886e10d3884Sbrad if(worker->env.anchors && 2887e10d3884Sbrad (a=anchor_find(worker->env.anchors, z->name, 2888e10d3884Sbrad z->namelabs, z->namelen, z->dclass))) { 2889e10d3884Sbrad if(!a->keylist && !a->numDS && !a->numDNSKEY) 2890e10d3884Sbrad insecure = 1; 2891e10d3884Sbrad lock_basic_unlock(&a->lock); 2892e10d3884Sbrad } 2893e10d3884Sbrad 2894e10d3884Sbrad if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"), 28952bdc0ed1Ssthen z->name, z->dclass, z->dp)) { 28962bdc0ed1Ssthen lock_rw_unlock(&fwds->lock); 2897933707f3Ssthen return; 2898933707f3Ssthen } 2899933707f3Ssthen } 29002bdc0ed1Ssthen lock_rw_unlock(&fwds->lock); 29012bdc0ed1Ssthen } 2902933707f3Ssthen 2903933707f3Ssthen /** do the list_stubs command */ 2904933707f3Ssthen static void 290520237c55Ssthen do_list_stubs(RES* ssl, struct worker* worker) 2906933707f3Ssthen { 2907933707f3Ssthen struct iter_hints_stub* z; 2908e10d3884Sbrad struct trust_anchor* a; 2909e10d3884Sbrad int insecure; 2910e10d3884Sbrad char str[32]; 29112bdc0ed1Ssthen lock_rw_rdlock(&worker->env.hints->lock); 2912163a4143Ssthen RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) { 2913e10d3884Sbrad 2914e10d3884Sbrad /* see if it is insecure */ 2915e10d3884Sbrad insecure = 0; 2916e10d3884Sbrad if(worker->env.anchors && 2917e10d3884Sbrad (a=anchor_find(worker->env.anchors, z->node.name, 2918e10d3884Sbrad z->node.labs, z->node.len, z->node.dclass))) { 2919e10d3884Sbrad if(!a->keylist && !a->numDS && !a->numDNSKEY) 2920e10d3884Sbrad insecure = 1; 2921e10d3884Sbrad lock_basic_unlock(&a->lock); 2922e10d3884Sbrad } 2923e10d3884Sbrad 2924e10d3884Sbrad snprintf(str, sizeof(str), "stub %sprime%s", 2925e10d3884Sbrad (z->noprime?"no":""), (insecure?" +i":"")); 2926e10d3884Sbrad if(!ssl_print_name_dp(ssl, str, z->node.name, 29272bdc0ed1Ssthen z->node.dclass, z->dp)) { 29282bdc0ed1Ssthen lock_rw_unlock(&worker->env.hints->lock); 2929933707f3Ssthen return; 2930933707f3Ssthen } 2931933707f3Ssthen } 29322bdc0ed1Ssthen lock_rw_unlock(&worker->env.hints->lock); 29332bdc0ed1Ssthen } 2934933707f3Ssthen 293520237c55Ssthen /** do the list_auth_zones command */ 293620237c55Ssthen static void 293720237c55Ssthen do_list_auth_zones(RES* ssl, struct auth_zones* az) 293820237c55Ssthen { 293920237c55Ssthen struct auth_zone* z; 294020237c55Ssthen char buf[257], buf2[256]; 294120237c55Ssthen lock_rw_rdlock(&az->lock); 294220237c55Ssthen RBTREE_FOR(z, struct auth_zone*, &az->ztree) { 294320237c55Ssthen lock_rw_rdlock(&z->lock); 294420237c55Ssthen dname_str(z->name, buf); 294520237c55Ssthen if(z->zone_expired) 294620237c55Ssthen snprintf(buf2, sizeof(buf2), "expired"); 294720237c55Ssthen else { 294820237c55Ssthen uint32_t serial = 0; 294920237c55Ssthen if(auth_zone_get_serial(z, &serial)) 295020237c55Ssthen snprintf(buf2, sizeof(buf2), "serial %u", 295120237c55Ssthen (unsigned)serial); 295220237c55Ssthen else snprintf(buf2, sizeof(buf2), "no serial"); 295320237c55Ssthen } 295420237c55Ssthen if(!ssl_printf(ssl, "%s\t%s\n", buf, buf2)) { 295520237c55Ssthen /* failure to print */ 295620237c55Ssthen lock_rw_unlock(&z->lock); 295720237c55Ssthen lock_rw_unlock(&az->lock); 295820237c55Ssthen return; 295920237c55Ssthen } 296020237c55Ssthen lock_rw_unlock(&z->lock); 296120237c55Ssthen } 296220237c55Ssthen lock_rw_unlock(&az->lock); 296320237c55Ssthen } 296420237c55Ssthen 2965933707f3Ssthen /** do the list_local_zones command */ 2966933707f3Ssthen static void 296720237c55Ssthen do_list_local_zones(RES* ssl, struct local_zones* zones) 2968933707f3Ssthen { 2969933707f3Ssthen struct local_zone* z; 2970933707f3Ssthen char buf[257]; 29710b68ff31Ssthen lock_rw_rdlock(&zones->lock); 2972933707f3Ssthen RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2973933707f3Ssthen lock_rw_rdlock(&z->lock); 2974933707f3Ssthen dname_str(z->name, buf); 2975e10d3884Sbrad if(!ssl_printf(ssl, "%s %s\n", buf, 2976e10d3884Sbrad local_zone_type2str(z->type))) { 2977e10d3884Sbrad /* failure to print */ 2978e10d3884Sbrad lock_rw_unlock(&z->lock); 2979e10d3884Sbrad lock_rw_unlock(&zones->lock); 2980e10d3884Sbrad return; 2981e10d3884Sbrad } 2982933707f3Ssthen lock_rw_unlock(&z->lock); 2983933707f3Ssthen } 29840b68ff31Ssthen lock_rw_unlock(&zones->lock); 2985933707f3Ssthen } 2986933707f3Ssthen 2987933707f3Ssthen /** do the list_local_data command */ 2988933707f3Ssthen static void 298920237c55Ssthen do_list_local_data(RES* ssl, struct worker* worker, struct local_zones* zones) 2990933707f3Ssthen { 2991933707f3Ssthen struct local_zone* z; 2992933707f3Ssthen struct local_data* d; 2993933707f3Ssthen struct local_rrset* p; 29940b68ff31Ssthen char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer); 29950b68ff31Ssthen size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer); 29960b68ff31Ssthen lock_rw_rdlock(&zones->lock); 2997933707f3Ssthen RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2998933707f3Ssthen lock_rw_rdlock(&z->lock); 2999933707f3Ssthen RBTREE_FOR(d, struct local_data*, &z->data) { 3000933707f3Ssthen for(p = d->rrsets; p; p = p->next) { 30010b68ff31Ssthen struct packed_rrset_data* d = 30020b68ff31Ssthen (struct packed_rrset_data*)p->rrset->entry.data; 30030b68ff31Ssthen size_t i; 30040b68ff31Ssthen for(i=0; i<d->count + d->rrsig_count; i++) { 30050b68ff31Ssthen if(!packed_rr_to_string(p->rrset, i, 30060b68ff31Ssthen 0, s, slen)) { 3007a961b961Ssthen if(!ssl_printf(ssl, "BADRR\n")) { 3008a961b961Ssthen lock_rw_unlock(&z->lock); 3009a961b961Ssthen lock_rw_unlock(&zones->lock); 30100b68ff31Ssthen return; 30110b68ff31Ssthen } 3012a961b961Ssthen } 3013a961b961Ssthen if(!ssl_printf(ssl, "%s\n", s)) { 3014a961b961Ssthen lock_rw_unlock(&z->lock); 3015a961b961Ssthen lock_rw_unlock(&zones->lock); 30160b68ff31Ssthen return; 30170b68ff31Ssthen } 3018933707f3Ssthen } 3019933707f3Ssthen } 3020a961b961Ssthen } 3021933707f3Ssthen lock_rw_unlock(&z->lock); 3022933707f3Ssthen } 30230b68ff31Ssthen lock_rw_unlock(&zones->lock); 3024933707f3Ssthen } 3025933707f3Ssthen 302677079be7Ssthen /** do the view_list_local_zones command */ 302777079be7Ssthen static void 302820237c55Ssthen do_view_list_local_zones(RES* ssl, struct worker* worker, char* arg) 302977079be7Ssthen { 303077079be7Ssthen struct view* v = views_find_view(worker->daemon->views, 303177079be7Ssthen arg, 0 /* get read lock*/); 303277079be7Ssthen if(!v) { 303377079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 303477079be7Ssthen return; 303577079be7Ssthen } 30362be9e038Ssthen if(v->local_zones) { 303777079be7Ssthen do_list_local_zones(ssl, v->local_zones); 30382be9e038Ssthen } 303977079be7Ssthen lock_rw_unlock(&v->lock); 304077079be7Ssthen } 304177079be7Ssthen 304277079be7Ssthen /** do the view_list_local_data command */ 304377079be7Ssthen static void 304420237c55Ssthen do_view_list_local_data(RES* ssl, struct worker* worker, char* arg) 304577079be7Ssthen { 304677079be7Ssthen struct view* v = views_find_view(worker->daemon->views, 304777079be7Ssthen arg, 0 /* get read lock*/); 304877079be7Ssthen if(!v) { 304977079be7Ssthen ssl_printf(ssl,"no view with name: %s\n", arg); 305077079be7Ssthen return; 305177079be7Ssthen } 30522be9e038Ssthen if(v->local_zones) { 305377079be7Ssthen do_list_local_data(ssl, worker, v->local_zones); 30542be9e038Ssthen } 305577079be7Ssthen lock_rw_unlock(&v->lock); 305677079be7Ssthen } 305777079be7Ssthen 3058a58bff56Ssthen /** struct for user arg ratelimit list */ 3059a58bff56Ssthen struct ratelimit_list_arg { 3060a58bff56Ssthen /** the infra cache */ 3061a58bff56Ssthen struct infra_cache* infra; 3062a58bff56Ssthen /** the SSL to print to */ 306320237c55Ssthen RES* ssl; 3064a58bff56Ssthen /** all or only ratelimited */ 3065a58bff56Ssthen int all; 3066a58bff56Ssthen /** current time */ 3067a58bff56Ssthen time_t now; 3068e21c60efSsthen /** if backoff is enabled */ 3069e21c60efSsthen int backoff; 3070a58bff56Ssthen }; 3071a58bff56Ssthen 307277079be7Ssthen #define ip_ratelimit_list_arg ratelimit_list_arg 307377079be7Ssthen 3074a58bff56Ssthen /** list items in the ratelimit table */ 3075a58bff56Ssthen static void 3076a58bff56Ssthen rate_list(struct lruhash_entry* e, void* arg) 3077a58bff56Ssthen { 3078a58bff56Ssthen struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg; 3079a58bff56Ssthen struct rate_key* k = (struct rate_key*)e->key; 3080a58bff56Ssthen struct rate_data* d = (struct rate_data*)e->data; 3081a58bff56Ssthen char buf[257]; 3082a58bff56Ssthen int lim = infra_find_ratelimit(a->infra, k->name, k->namelen); 3083e21c60efSsthen int max = infra_rate_max(d, a->now, a->backoff); 3084a58bff56Ssthen if(a->all == 0) { 3085a58bff56Ssthen if(max < lim) 3086a58bff56Ssthen return; 3087a58bff56Ssthen } 3088a58bff56Ssthen dname_str(k->name, buf); 3089a58bff56Ssthen ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim); 3090a58bff56Ssthen } 3091a58bff56Ssthen 309277079be7Ssthen /** list items in the ip_ratelimit table */ 309377079be7Ssthen static void 309477079be7Ssthen ip_rate_list(struct lruhash_entry* e, void* arg) 309577079be7Ssthen { 309677079be7Ssthen char ip[128]; 309777079be7Ssthen struct ip_ratelimit_list_arg* a = (struct ip_ratelimit_list_arg*)arg; 309877079be7Ssthen struct ip_rate_key* k = (struct ip_rate_key*)e->key; 309977079be7Ssthen struct ip_rate_data* d = (struct ip_rate_data*)e->data; 310077079be7Ssthen int lim = infra_ip_ratelimit; 3101e21c60efSsthen int max = infra_rate_max(d, a->now, a->backoff); 310277079be7Ssthen if(a->all == 0) { 310377079be7Ssthen if(max < lim) 310477079be7Ssthen return; 310577079be7Ssthen } 310677079be7Ssthen addr_to_str(&k->addr, k->addrlen, ip, sizeof(ip)); 310777079be7Ssthen ssl_printf(a->ssl, "%s %d limit %d\n", ip, max, lim); 310877079be7Ssthen } 310977079be7Ssthen 3110a58bff56Ssthen /** do the ratelimit_list command */ 3111a58bff56Ssthen static void 311220237c55Ssthen do_ratelimit_list(RES* ssl, struct worker* worker, char* arg) 3113a58bff56Ssthen { 3114a58bff56Ssthen struct ratelimit_list_arg a; 3115a58bff56Ssthen a.all = 0; 3116a58bff56Ssthen a.infra = worker->env.infra_cache; 3117a58bff56Ssthen a.now = *worker->env.now; 3118a58bff56Ssthen a.ssl = ssl; 3119e21c60efSsthen a.backoff = worker->env.cfg->ratelimit_backoff; 3120a58bff56Ssthen arg = skipwhite(arg); 3121a58bff56Ssthen if(strcmp(arg, "+a") == 0) 3122a58bff56Ssthen a.all = 1; 3123a58bff56Ssthen if(a.infra->domain_rates==NULL || 3124a58bff56Ssthen (a.all == 0 && infra_dp_ratelimit == 0)) 3125a58bff56Ssthen return; 3126a58bff56Ssthen slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a); 3127a58bff56Ssthen } 3128a58bff56Ssthen 312977079be7Ssthen /** do the ip_ratelimit_list command */ 313077079be7Ssthen static void 313120237c55Ssthen do_ip_ratelimit_list(RES* ssl, struct worker* worker, char* arg) 313277079be7Ssthen { 313377079be7Ssthen struct ip_ratelimit_list_arg a; 313477079be7Ssthen a.all = 0; 313577079be7Ssthen a.infra = worker->env.infra_cache; 313677079be7Ssthen a.now = *worker->env.now; 313777079be7Ssthen a.ssl = ssl; 3138e21c60efSsthen a.backoff = worker->env.cfg->ip_ratelimit_backoff; 313977079be7Ssthen arg = skipwhite(arg); 314077079be7Ssthen if(strcmp(arg, "+a") == 0) 314177079be7Ssthen a.all = 1; 314277079be7Ssthen if(a.infra->client_ip_rates==NULL || 314377079be7Ssthen (a.all == 0 && infra_ip_ratelimit == 0)) 314477079be7Ssthen return; 314577079be7Ssthen slabhash_traverse(a.infra->client_ip_rates, 0, ip_rate_list, &a); 314677079be7Ssthen } 314777079be7Ssthen 31489982a05dSsthen /** do the rpz_enable/disable command */ 31499982a05dSsthen static void 31509982a05dSsthen do_rpz_enable_disable(RES* ssl, struct worker* worker, char* arg, int enable) { 31519982a05dSsthen size_t nmlen; 31529982a05dSsthen int nmlabs; 31539982a05dSsthen uint8_t *nm = NULL; 31549982a05dSsthen struct auth_zones *az = worker->env.auth_zones; 31559982a05dSsthen struct auth_zone *z = NULL; 31569982a05dSsthen if (!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 31579982a05dSsthen return; 31589982a05dSsthen if (az) { 31599982a05dSsthen lock_rw_rdlock(&az->lock); 31609982a05dSsthen z = auth_zone_find(az, nm, nmlen, LDNS_RR_CLASS_IN); 31619982a05dSsthen if (z) { 31629982a05dSsthen lock_rw_wrlock(&z->lock); 31639982a05dSsthen } 31649982a05dSsthen lock_rw_unlock(&az->lock); 31659982a05dSsthen } 31669982a05dSsthen free(nm); 31679982a05dSsthen if (!z) { 31689982a05dSsthen (void) ssl_printf(ssl, "error no auth-zone %s\n", arg); 31699982a05dSsthen return; 31709982a05dSsthen } 31719982a05dSsthen if (!z->rpz) { 31729982a05dSsthen (void) ssl_printf(ssl, "error auth-zone %s not RPZ\n", arg); 31739982a05dSsthen lock_rw_unlock(&z->lock); 31749982a05dSsthen return; 31759982a05dSsthen } 31769982a05dSsthen if (enable) { 31779982a05dSsthen rpz_enable(z->rpz); 31789982a05dSsthen } else { 31799982a05dSsthen rpz_disable(z->rpz); 31809982a05dSsthen } 31819982a05dSsthen lock_rw_unlock(&z->lock); 31829982a05dSsthen send_ok(ssl); 31839982a05dSsthen } 31849982a05dSsthen 31859982a05dSsthen /** do the rpz_enable command */ 31869982a05dSsthen static void 31879982a05dSsthen do_rpz_enable(RES* ssl, struct worker* worker, char* arg) 31889982a05dSsthen { 31899982a05dSsthen do_rpz_enable_disable(ssl, worker, arg, 1); 31909982a05dSsthen } 31919982a05dSsthen 31929982a05dSsthen /** do the rpz_disable command */ 31939982a05dSsthen static void 31949982a05dSsthen do_rpz_disable(RES* ssl, struct worker* worker, char* arg) 31959982a05dSsthen { 31969982a05dSsthen do_rpz_enable_disable(ssl, worker, arg, 0); 31979982a05dSsthen } 31989982a05dSsthen 3199*98bc733bSsthen /** Write the cookie secrets to file, returns `0` on failure. 3200*98bc733bSsthen * Caller has to hold the lock. */ 3201*98bc733bSsthen static int 3202*98bc733bSsthen cookie_secret_file_dump(RES* ssl, struct worker* worker) { 3203*98bc733bSsthen char const* secret_file = worker->env.cfg->cookie_secret_file; 3204*98bc733bSsthen struct cookie_secrets* cookie_secrets = worker->daemon->cookie_secrets; 3205*98bc733bSsthen char secret_hex[UNBOUND_COOKIE_SECRET_SIZE * 2 + 1]; 3206*98bc733bSsthen FILE* f; 3207*98bc733bSsthen size_t i; 3208*98bc733bSsthen if(secret_file == NULL || secret_file[0]==0) { 3209*98bc733bSsthen (void)ssl_printf(ssl, "error: no cookie secret file configured\n"); 3210*98bc733bSsthen return 0; 3211*98bc733bSsthen } 3212*98bc733bSsthen log_assert( secret_file != NULL ); 3213*98bc733bSsthen 3214*98bc733bSsthen /* open write only and truncate */ 3215*98bc733bSsthen if((f = fopen(secret_file, "w")) == NULL ) { 3216*98bc733bSsthen (void)ssl_printf(ssl, "unable to open cookie secret file %s: %s", 3217*98bc733bSsthen secret_file, strerror(errno)); 3218*98bc733bSsthen return 0; 3219*98bc733bSsthen } 3220*98bc733bSsthen if(cookie_secrets == NULL) { 3221*98bc733bSsthen /* nothing to write */ 3222*98bc733bSsthen fclose(f); 3223*98bc733bSsthen return 1; 3224*98bc733bSsthen } 3225*98bc733bSsthen 3226*98bc733bSsthen for(i = 0; i < cookie_secrets->cookie_count; i++) { 3227*98bc733bSsthen struct cookie_secret const* cs = &cookie_secrets-> 3228*98bc733bSsthen cookie_secrets[i]; 3229*98bc733bSsthen ssize_t const len = hex_ntop(cs->cookie_secret, 3230*98bc733bSsthen UNBOUND_COOKIE_SECRET_SIZE, secret_hex, 3231*98bc733bSsthen sizeof(secret_hex)); 3232*98bc733bSsthen (void)len; /* silence unused variable warning with -DNDEBUG */ 3233*98bc733bSsthen log_assert( len == UNBOUND_COOKIE_SECRET_SIZE * 2 ); 3234*98bc733bSsthen secret_hex[UNBOUND_COOKIE_SECRET_SIZE * 2] = '\0'; 3235*98bc733bSsthen fprintf(f, "%s\n", secret_hex); 3236*98bc733bSsthen } 3237*98bc733bSsthen explicit_bzero(secret_hex, sizeof(secret_hex)); 3238*98bc733bSsthen fclose(f); 3239*98bc733bSsthen return 1; 3240*98bc733bSsthen } 3241*98bc733bSsthen 3242*98bc733bSsthen /** Activate cookie secret */ 3243933707f3Ssthen static void 3244*98bc733bSsthen do_activate_cookie_secret(RES* ssl, struct worker* worker) { 3245*98bc733bSsthen char const* secret_file = worker->env.cfg->cookie_secret_file; 3246*98bc733bSsthen struct cookie_secrets* cookie_secrets = worker->daemon->cookie_secrets; 3247*98bc733bSsthen 3248*98bc733bSsthen if(secret_file == NULL || secret_file[0] == 0) { 3249*98bc733bSsthen (void)ssl_printf(ssl, "error: no cookie secret file configured\n"); 3250*98bc733bSsthen return; 3251*98bc733bSsthen } 3252*98bc733bSsthen if(cookie_secrets == NULL) { 3253*98bc733bSsthen (void)ssl_printf(ssl, "error: there are no cookie_secrets."); 3254*98bc733bSsthen return; 3255*98bc733bSsthen } 3256*98bc733bSsthen lock_basic_lock(&cookie_secrets->lock); 3257*98bc733bSsthen 3258*98bc733bSsthen if(cookie_secrets->cookie_count <= 1 ) { 3259*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3260*98bc733bSsthen (void)ssl_printf(ssl, "error: no staging cookie secret to activate\n"); 3261*98bc733bSsthen return; 3262*98bc733bSsthen } 3263*98bc733bSsthen /* Only the worker 0 writes to file, the others update state. */ 3264*98bc733bSsthen if(worker->thread_num == 0 && !cookie_secret_file_dump(ssl, worker)) { 3265*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3266*98bc733bSsthen (void)ssl_printf(ssl, "error: writing to cookie secret file: \"%s\"\n", 3267*98bc733bSsthen secret_file); 3268*98bc733bSsthen return; 3269*98bc733bSsthen } 3270*98bc733bSsthen activate_cookie_secret(cookie_secrets); 3271*98bc733bSsthen if(worker->thread_num == 0) 3272*98bc733bSsthen (void)cookie_secret_file_dump(ssl, worker); 3273*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3274*98bc733bSsthen send_ok(ssl); 3275*98bc733bSsthen } 3276*98bc733bSsthen 3277*98bc733bSsthen /** Drop cookie secret */ 3278*98bc733bSsthen static void 3279*98bc733bSsthen do_drop_cookie_secret(RES* ssl, struct worker* worker) { 3280*98bc733bSsthen char const* secret_file = worker->env.cfg->cookie_secret_file; 3281*98bc733bSsthen struct cookie_secrets* cookie_secrets = worker->daemon->cookie_secrets; 3282*98bc733bSsthen 3283*98bc733bSsthen if(secret_file == NULL || secret_file[0] == 0) { 3284*98bc733bSsthen (void)ssl_printf(ssl, "error: no cookie secret file configured\n"); 3285*98bc733bSsthen return; 3286*98bc733bSsthen } 3287*98bc733bSsthen if(cookie_secrets == NULL) { 3288*98bc733bSsthen (void)ssl_printf(ssl, "error: there are no cookie_secrets."); 3289*98bc733bSsthen return; 3290*98bc733bSsthen } 3291*98bc733bSsthen lock_basic_lock(&cookie_secrets->lock); 3292*98bc733bSsthen 3293*98bc733bSsthen if(cookie_secrets->cookie_count <= 1 ) { 3294*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3295*98bc733bSsthen (void)ssl_printf(ssl, "error: can not drop the currently active cookie secret\n"); 3296*98bc733bSsthen return; 3297*98bc733bSsthen } 3298*98bc733bSsthen /* Only the worker 0 writes to file, the others update state. */ 3299*98bc733bSsthen if(worker->thread_num == 0 && !cookie_secret_file_dump(ssl, worker)) { 3300*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3301*98bc733bSsthen (void)ssl_printf(ssl, "error: writing to cookie secret file: \"%s\"\n", 3302*98bc733bSsthen secret_file); 3303*98bc733bSsthen return; 3304*98bc733bSsthen } 3305*98bc733bSsthen drop_cookie_secret(cookie_secrets); 3306*98bc733bSsthen if(worker->thread_num == 0) 3307*98bc733bSsthen (void)cookie_secret_file_dump(ssl, worker); 3308*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3309*98bc733bSsthen send_ok(ssl); 3310*98bc733bSsthen } 3311*98bc733bSsthen 3312*98bc733bSsthen /** Add cookie secret */ 3313*98bc733bSsthen static void 3314*98bc733bSsthen do_add_cookie_secret(RES* ssl, struct worker* worker, char* arg) { 3315*98bc733bSsthen uint8_t secret[UNBOUND_COOKIE_SECRET_SIZE]; 3316*98bc733bSsthen char const* secret_file = worker->env.cfg->cookie_secret_file; 3317*98bc733bSsthen struct cookie_secrets* cookie_secrets = worker->daemon->cookie_secrets; 3318*98bc733bSsthen 3319*98bc733bSsthen if(secret_file == NULL || secret_file[0] == 0) { 3320*98bc733bSsthen (void)ssl_printf(ssl, "error: no cookie secret file configured\n"); 3321*98bc733bSsthen return; 3322*98bc733bSsthen } 3323*98bc733bSsthen if(cookie_secrets == NULL) { 3324*98bc733bSsthen worker->daemon->cookie_secrets = cookie_secrets_create(); 3325*98bc733bSsthen if(!worker->daemon->cookie_secrets) { 3326*98bc733bSsthen (void)ssl_printf(ssl, "error: out of memory"); 3327*98bc733bSsthen return; 3328*98bc733bSsthen } 3329*98bc733bSsthen cookie_secrets = worker->daemon->cookie_secrets; 3330*98bc733bSsthen } 3331*98bc733bSsthen lock_basic_lock(&cookie_secrets->lock); 3332*98bc733bSsthen 3333*98bc733bSsthen if(*arg == '\0') { 3334*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3335*98bc733bSsthen (void)ssl_printf(ssl, "error: missing argument (cookie_secret)\n"); 3336*98bc733bSsthen return; 3337*98bc733bSsthen } 3338*98bc733bSsthen if(strlen(arg) != 32) { 3339*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3340*98bc733bSsthen explicit_bzero(arg, strlen(arg)); 3341*98bc733bSsthen (void)ssl_printf(ssl, "invalid cookie secret: invalid argument length\n"); 3342*98bc733bSsthen (void)ssl_printf(ssl, "please provide a 128bit hex encoded secret\n"); 3343*98bc733bSsthen return; 3344*98bc733bSsthen } 3345*98bc733bSsthen if(hex_pton(arg, secret, UNBOUND_COOKIE_SECRET_SIZE) != 3346*98bc733bSsthen UNBOUND_COOKIE_SECRET_SIZE ) { 3347*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3348*98bc733bSsthen explicit_bzero(secret, UNBOUND_COOKIE_SECRET_SIZE); 3349*98bc733bSsthen explicit_bzero(arg, strlen(arg)); 3350*98bc733bSsthen (void)ssl_printf(ssl, "invalid cookie secret: parse error\n"); 3351*98bc733bSsthen (void)ssl_printf(ssl, "please provide a 128bit hex encoded secret\n"); 3352*98bc733bSsthen return; 3353*98bc733bSsthen } 3354*98bc733bSsthen /* Only the worker 0 writes to file, the others update state. */ 3355*98bc733bSsthen if(worker->thread_num == 0 && !cookie_secret_file_dump(ssl, worker)) { 3356*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3357*98bc733bSsthen explicit_bzero(secret, UNBOUND_COOKIE_SECRET_SIZE); 3358*98bc733bSsthen explicit_bzero(arg, strlen(arg)); 3359*98bc733bSsthen (void)ssl_printf(ssl, "error: writing to cookie secret file: \"%s\"\n", 3360*98bc733bSsthen secret_file); 3361*98bc733bSsthen return; 3362*98bc733bSsthen } 3363*98bc733bSsthen add_cookie_secret(cookie_secrets, secret, UNBOUND_COOKIE_SECRET_SIZE); 3364*98bc733bSsthen explicit_bzero(secret, UNBOUND_COOKIE_SECRET_SIZE); 3365*98bc733bSsthen if(worker->thread_num == 0) 3366*98bc733bSsthen (void)cookie_secret_file_dump(ssl, worker); 3367*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3368*98bc733bSsthen explicit_bzero(arg, strlen(arg)); 3369*98bc733bSsthen send_ok(ssl); 3370*98bc733bSsthen } 3371*98bc733bSsthen 3372*98bc733bSsthen /** Print cookie secrets */ 3373*98bc733bSsthen static void 3374*98bc733bSsthen do_print_cookie_secrets(RES* ssl, struct worker* worker) { 3375*98bc733bSsthen struct cookie_secrets* cookie_secrets = worker->daemon->cookie_secrets; 3376*98bc733bSsthen char secret_hex[UNBOUND_COOKIE_SECRET_SIZE * 2 + 1]; 3377933707f3Ssthen int i; 3378*98bc733bSsthen 3379*98bc733bSsthen if(!cookie_secrets) 3380*98bc733bSsthen return; /* Output is empty. */ 3381*98bc733bSsthen lock_basic_lock(&cookie_secrets->lock); 3382*98bc733bSsthen for(i = 0; (size_t)i < cookie_secrets->cookie_count; i++) { 3383*98bc733bSsthen struct cookie_secret const* cs = &cookie_secrets-> 3384*98bc733bSsthen cookie_secrets[i]; 3385*98bc733bSsthen ssize_t const len = hex_ntop(cs->cookie_secret, 3386*98bc733bSsthen UNBOUND_COOKIE_SECRET_SIZE, secret_hex, 3387*98bc733bSsthen sizeof(secret_hex)); 3388*98bc733bSsthen (void)len; /* silence unused variable warning with -DNDEBUG */ 3389*98bc733bSsthen log_assert( len == UNBOUND_COOKIE_SECRET_SIZE * 2 ); 3390*98bc733bSsthen secret_hex[UNBOUND_COOKIE_SECRET_SIZE * 2] = '\0'; 3391*98bc733bSsthen if (i == 0) 3392*98bc733bSsthen (void)ssl_printf(ssl, "active : %s\n", secret_hex); 3393*98bc733bSsthen else if (cookie_secrets->cookie_count == 2) 3394*98bc733bSsthen (void)ssl_printf(ssl, "staging: %s\n", secret_hex); 3395*98bc733bSsthen else 3396*98bc733bSsthen (void)ssl_printf(ssl, "staging[%d]: %s\n", i, 3397*98bc733bSsthen secret_hex); 3398933707f3Ssthen } 3399*98bc733bSsthen lock_basic_unlock(&cookie_secrets->lock); 3400*98bc733bSsthen explicit_bzero(secret_hex, sizeof(secret_hex)); 3401933707f3Ssthen } 3402933707f3Ssthen 3403933707f3Ssthen /** check for name with end-of-string, space or tab after it */ 3404933707f3Ssthen static int 3405933707f3Ssthen cmdcmp(char* p, const char* cmd, size_t len) 3406933707f3Ssthen { 3407933707f3Ssthen return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t'); 3408933707f3Ssthen } 3409933707f3Ssthen 3410933707f3Ssthen /** execute a remote control command */ 3411933707f3Ssthen static void 341220237c55Ssthen execute_cmd(struct daemon_remote* rc, RES* ssl, char* cmd, 3413933707f3Ssthen struct worker* worker) 3414933707f3Ssthen { 3415933707f3Ssthen char* p = skipwhite(cmd); 3416933707f3Ssthen /* compare command */ 3417933707f3Ssthen if(cmdcmp(p, "stop", 4)) { 3418ebf5bb73Ssthen do_stop(ssl, worker); 3419933707f3Ssthen return; 34208b7325afSsthen } else if(cmdcmp(p, "reload_keep_cache", 17)) { 34218b7325afSsthen do_reload(ssl, worker, 1); 34228b7325afSsthen return; 3423933707f3Ssthen } else if(cmdcmp(p, "reload", 6)) { 34248b7325afSsthen do_reload(ssl, worker, 0); 3425933707f3Ssthen return; 3426933707f3Ssthen } else if(cmdcmp(p, "stats_noreset", 13)) { 3427ebf5bb73Ssthen do_stats(ssl, worker, 0); 3428933707f3Ssthen return; 3429933707f3Ssthen } else if(cmdcmp(p, "stats", 5)) { 3430ebf5bb73Ssthen do_stats(ssl, worker, 1); 3431933707f3Ssthen return; 3432933707f3Ssthen } else if(cmdcmp(p, "status", 6)) { 3433933707f3Ssthen do_status(ssl, worker); 3434933707f3Ssthen return; 3435933707f3Ssthen } else if(cmdcmp(p, "dump_cache", 10)) { 3436*98bc733bSsthen #ifdef THREADS_DISABLED 3437*98bc733bSsthen if(worker->daemon->num > 1) { 3438*98bc733bSsthen (void)ssl_printf(ssl, "dump_cache/load_cache is not " 3439*98bc733bSsthen "supported in multi-process operation\n"); 3440*98bc733bSsthen return; 3441*98bc733bSsthen } 3442*98bc733bSsthen #endif 3443933707f3Ssthen (void)dump_cache(ssl, worker); 3444933707f3Ssthen return; 3445933707f3Ssthen } else if(cmdcmp(p, "load_cache", 10)) { 3446*98bc733bSsthen #ifdef THREADS_DISABLED 3447*98bc733bSsthen if(worker->daemon->num > 1) { 3448*98bc733bSsthen /* The warning can't be printed when stdin is sending 3449*98bc733bSsthen * data; just return */ 3450*98bc733bSsthen return; 3451*98bc733bSsthen } 3452*98bc733bSsthen #endif 3453933707f3Ssthen if(load_cache(ssl, worker)) send_ok(ssl); 3454933707f3Ssthen return; 3455933707f3Ssthen } else if(cmdcmp(p, "list_forwards", 13)) { 3456933707f3Ssthen do_list_forwards(ssl, worker); 3457933707f3Ssthen return; 3458933707f3Ssthen } else if(cmdcmp(p, "list_stubs", 10)) { 3459933707f3Ssthen do_list_stubs(ssl, worker); 3460933707f3Ssthen return; 3461a58bff56Ssthen } else if(cmdcmp(p, "list_insecure", 13)) { 3462a58bff56Ssthen do_insecure_list(ssl, worker); 3463a58bff56Ssthen return; 3464933707f3Ssthen } else if(cmdcmp(p, "list_local_zones", 16)) { 346577079be7Ssthen do_list_local_zones(ssl, worker->daemon->local_zones); 3466933707f3Ssthen return; 3467933707f3Ssthen } else if(cmdcmp(p, "list_local_data", 15)) { 346877079be7Ssthen do_list_local_data(ssl, worker, worker->daemon->local_zones); 346977079be7Ssthen return; 347077079be7Ssthen } else if(cmdcmp(p, "view_list_local_zones", 21)) { 347177079be7Ssthen do_view_list_local_zones(ssl, worker, skipwhite(p+21)); 347277079be7Ssthen return; 347377079be7Ssthen } else if(cmdcmp(p, "view_list_local_data", 20)) { 347477079be7Ssthen do_view_list_local_data(ssl, worker, skipwhite(p+20)); 3475933707f3Ssthen return; 3476a58bff56Ssthen } else if(cmdcmp(p, "ratelimit_list", 14)) { 3477a58bff56Ssthen do_ratelimit_list(ssl, worker, p+14); 3478a58bff56Ssthen return; 347977079be7Ssthen } else if(cmdcmp(p, "ip_ratelimit_list", 17)) { 348077079be7Ssthen do_ip_ratelimit_list(ssl, worker, p+17); 348177079be7Ssthen return; 348220237c55Ssthen } else if(cmdcmp(p, "list_auth_zones", 15)) { 348320237c55Ssthen do_list_auth_zones(ssl, worker->env.auth_zones); 348420237c55Ssthen return; 34852308e98cSsthen } else if(cmdcmp(p, "auth_zone_reload", 16)) { 34862308e98cSsthen do_auth_zone_reload(ssl, worker, skipwhite(p+16)); 34872308e98cSsthen return; 34882308e98cSsthen } else if(cmdcmp(p, "auth_zone_transfer", 18)) { 34892308e98cSsthen do_auth_zone_transfer(ssl, worker, skipwhite(p+18)); 34902308e98cSsthen return; 3491e9c7b4efSsthen } else if(cmdcmp(p, "insecure_add", 12)) { 3492e9c7b4efSsthen /* must always distribute this cmd */ 3493e9c7b4efSsthen if(rc) distribute_cmd(rc, ssl, cmd); 3494e9c7b4efSsthen do_insecure_add(ssl, worker, skipwhite(p+12)); 3495e9c7b4efSsthen return; 3496e9c7b4efSsthen } else if(cmdcmp(p, "insecure_remove", 15)) { 3497e9c7b4efSsthen /* must always distribute this cmd */ 3498e9c7b4efSsthen if(rc) distribute_cmd(rc, ssl, cmd); 3499e9c7b4efSsthen do_insecure_remove(ssl, worker, skipwhite(p+15)); 3500e9c7b4efSsthen return; 3501933707f3Ssthen } else if(cmdcmp(p, "flush_stats", 11)) { 3502933707f3Ssthen /* must always distribute this cmd */ 3503933707f3Ssthen if(rc) distribute_cmd(rc, ssl, cmd); 3504933707f3Ssthen do_flush_stats(ssl, worker); 3505933707f3Ssthen return; 3506933707f3Ssthen } else if(cmdcmp(p, "flush_requestlist", 17)) { 3507933707f3Ssthen /* must always distribute this cmd */ 3508933707f3Ssthen if(rc) distribute_cmd(rc, ssl, cmd); 3509933707f3Ssthen do_flush_requestlist(ssl, worker); 3510933707f3Ssthen return; 3511933707f3Ssthen } else if(cmdcmp(p, "lookup", 6)) { 3512933707f3Ssthen do_lookup(ssl, worker, skipwhite(p+6)); 3513933707f3Ssthen return; 3514*98bc733bSsthen /* The following are commands that read stdin. 3515*98bc733bSsthen * Each line needs to be distributed if THREADS_DISABLED. 3516*98bc733bSsthen */ 3517*98bc733bSsthen } else if(cmdcmp(p, "local_zones_remove", 18)) { 3518*98bc733bSsthen do_zones_remove(rc, ssl, worker); 3519*98bc733bSsthen return; 3520*98bc733bSsthen } else if(cmdcmp(p, "local_zones", 11)) { 3521*98bc733bSsthen do_zones_add(rc, ssl, worker); 3522*98bc733bSsthen return; 3523*98bc733bSsthen } else if(cmdcmp(p, "local_datas_remove", 18)) { 3524*98bc733bSsthen do_datas_remove(rc, ssl, worker); 3525*98bc733bSsthen return; 3526*98bc733bSsthen } else if(cmdcmp(p, "local_datas", 11)) { 3527*98bc733bSsthen do_datas_add(rc, ssl, worker); 3528*98bc733bSsthen return; 3529*98bc733bSsthen } else if(cmdcmp(p, "view_local_datas_remove", 23)){ 3530*98bc733bSsthen do_view_datas_remove(rc, ssl, worker, skipwhite(p+23)); 3531*98bc733bSsthen return; 3532*98bc733bSsthen } else if(cmdcmp(p, "view_local_datas", 16)) { 3533*98bc733bSsthen do_view_datas_add(rc, ssl, worker, skipwhite(p+16)); 3534*98bc733bSsthen return; 3535*98bc733bSsthen } else if(cmdcmp(p, "print_cookie_secrets", 20)) { 3536*98bc733bSsthen do_print_cookie_secrets(ssl, worker); 3537*98bc733bSsthen return; 3538933707f3Ssthen } 3539933707f3Ssthen 3540933707f3Ssthen #ifdef THREADS_DISABLED 3541933707f3Ssthen /* other processes must execute the command as well */ 3542933707f3Ssthen /* commands that should not be distributed, returned above. */ 3543933707f3Ssthen if(rc) { /* only if this thread is the master (rc) thread */ 3544933707f3Ssthen /* done before the code below, which may split the string */ 3545933707f3Ssthen distribute_cmd(rc, ssl, cmd); 3546933707f3Ssthen } 3547933707f3Ssthen #endif 3548933707f3Ssthen if(cmdcmp(p, "verbosity", 9)) { 3549933707f3Ssthen do_verbosity(ssl, skipwhite(p+9)); 3550933707f3Ssthen } else if(cmdcmp(p, "local_zone_remove", 17)) { 355177079be7Ssthen do_zone_remove(ssl, worker->daemon->local_zones, skipwhite(p+17)); 3552933707f3Ssthen } else if(cmdcmp(p, "local_zone", 10)) { 355377079be7Ssthen do_zone_add(ssl, worker->daemon->local_zones, skipwhite(p+10)); 3554933707f3Ssthen } else if(cmdcmp(p, "local_data_remove", 17)) { 355577079be7Ssthen do_data_remove(ssl, worker->daemon->local_zones, skipwhite(p+17)); 3556933707f3Ssthen } else if(cmdcmp(p, "local_data", 10)) { 355777079be7Ssthen do_data_add(ssl, worker->daemon->local_zones, skipwhite(p+10)); 35582bdc0ed1Ssthen } else if(cmdcmp(p, "forward_add", 11)) { 35592bdc0ed1Ssthen do_forward_add(ssl, worker, skipwhite(p+11)); 35602bdc0ed1Ssthen } else if(cmdcmp(p, "forward_remove", 14)) { 35612bdc0ed1Ssthen do_forward_remove(ssl, worker, skipwhite(p+14)); 35622bdc0ed1Ssthen } else if(cmdcmp(p, "forward", 7)) { 35632bdc0ed1Ssthen do_forward(ssl, worker, skipwhite(p+7)); 35642bdc0ed1Ssthen } else if(cmdcmp(p, "stub_add", 8)) { 35652bdc0ed1Ssthen do_stub_add(ssl, worker, skipwhite(p+8)); 35662bdc0ed1Ssthen } else if(cmdcmp(p, "stub_remove", 11)) { 35672bdc0ed1Ssthen do_stub_remove(ssl, worker, skipwhite(p+11)); 356877079be7Ssthen } else if(cmdcmp(p, "view_local_zone_remove", 22)) { 356977079be7Ssthen do_view_zone_remove(ssl, worker, skipwhite(p+22)); 357077079be7Ssthen } else if(cmdcmp(p, "view_local_zone", 15)) { 357177079be7Ssthen do_view_zone_add(ssl, worker, skipwhite(p+15)); 357277079be7Ssthen } else if(cmdcmp(p, "view_local_data_remove", 22)) { 357377079be7Ssthen do_view_data_remove(ssl, worker, skipwhite(p+22)); 357477079be7Ssthen } else if(cmdcmp(p, "view_local_data", 15)) { 357577079be7Ssthen do_view_data_add(ssl, worker, skipwhite(p+15)); 3576933707f3Ssthen } else if(cmdcmp(p, "flush_zone", 10)) { 3577933707f3Ssthen do_flush_zone(ssl, worker, skipwhite(p+10)); 3578933707f3Ssthen } else if(cmdcmp(p, "flush_type", 10)) { 3579933707f3Ssthen do_flush_type(ssl, worker, skipwhite(p+10)); 3580933707f3Ssthen } else if(cmdcmp(p, "flush_infra", 11)) { 3581933707f3Ssthen do_flush_infra(ssl, worker, skipwhite(p+11)); 3582933707f3Ssthen } else if(cmdcmp(p, "flush", 5)) { 3583933707f3Ssthen do_flush_name(ssl, worker, skipwhite(p+5)); 3584933707f3Ssthen } else if(cmdcmp(p, "dump_requestlist", 16)) { 3585933707f3Ssthen do_dump_requestlist(ssl, worker); 3586933707f3Ssthen } else if(cmdcmp(p, "dump_infra", 10)) { 3587933707f3Ssthen do_dump_infra(ssl, worker); 3588933707f3Ssthen } else if(cmdcmp(p, "log_reopen", 10)) { 3589933707f3Ssthen do_log_reopen(ssl, worker); 3590933707f3Ssthen } else if(cmdcmp(p, "set_option", 10)) { 3591933707f3Ssthen do_set_option(ssl, worker, skipwhite(p+10)); 3592933707f3Ssthen } else if(cmdcmp(p, "get_option", 10)) { 3593933707f3Ssthen do_get_option(ssl, worker, skipwhite(p+10)); 3594cebdf579Ssthen } else if(cmdcmp(p, "flush_bogus", 11)) { 3595*98bc733bSsthen do_flush_bogus(ssl, worker, skipwhite(p+11)); 3596e10d3884Sbrad } else if(cmdcmp(p, "flush_negative", 14)) { 3597*98bc733bSsthen do_flush_negative(ssl, worker, skipwhite(p+14)); 35989982a05dSsthen } else if(cmdcmp(p, "rpz_enable", 10)) { 35999982a05dSsthen do_rpz_enable(ssl, worker, skipwhite(p+10)); 36009982a05dSsthen } else if(cmdcmp(p, "rpz_disable", 11)) { 36019982a05dSsthen do_rpz_disable(ssl, worker, skipwhite(p+11)); 3602*98bc733bSsthen } else if(cmdcmp(p, "add_cookie_secret", 17)) { 3603*98bc733bSsthen do_add_cookie_secret(ssl, worker, skipwhite(p+17)); 3604*98bc733bSsthen } else if(cmdcmp(p, "drop_cookie_secret", 18)) { 3605*98bc733bSsthen do_drop_cookie_secret(ssl, worker); 3606*98bc733bSsthen } else if(cmdcmp(p, "activate_cookie_secret", 22)) { 3607*98bc733bSsthen do_activate_cookie_secret(ssl, worker); 3608933707f3Ssthen } else { 3609933707f3Ssthen (void)ssl_printf(ssl, "error unknown command '%s'\n", p); 3610933707f3Ssthen } 3611933707f3Ssthen } 3612933707f3Ssthen 3613933707f3Ssthen void 3614933707f3Ssthen daemon_remote_exec(struct worker* worker) 3615933707f3Ssthen { 3616933707f3Ssthen /* read the cmd string */ 3617933707f3Ssthen uint8_t* msg = NULL; 3618933707f3Ssthen uint32_t len = 0; 3619933707f3Ssthen if(!tube_read_msg(worker->cmd, &msg, &len, 0)) { 3620933707f3Ssthen log_err("daemon_remote_exec: tube_read_msg failed"); 3621933707f3Ssthen return; 3622933707f3Ssthen } 3623933707f3Ssthen verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg); 3624933707f3Ssthen execute_cmd(NULL, NULL, (char*)msg, worker); 3625933707f3Ssthen free(msg); 3626933707f3Ssthen } 3627933707f3Ssthen 3628933707f3Ssthen /** handle remote control request */ 3629933707f3Ssthen static void 363020237c55Ssthen handle_req(struct daemon_remote* rc, struct rc_state* s, RES* res) 3631933707f3Ssthen { 3632933707f3Ssthen int r; 3633933707f3Ssthen char pre[10]; 3634933707f3Ssthen char magic[7]; 3635*98bc733bSsthen char buf[MAX_CMD_STRLINE]; 3636933707f3Ssthen #ifdef USE_WINSOCK 3637933707f3Ssthen /* makes it possible to set the socket blocking again. */ 3638933707f3Ssthen /* basically removes it from winsock_event ... */ 3639933707f3Ssthen WSAEventSelect(s->c->fd, NULL, 0); 3640933707f3Ssthen #endif 3641933707f3Ssthen fd_set_block(s->c->fd); 3642933707f3Ssthen 3643933707f3Ssthen /* try to read magic UBCT[version]_space_ string */ 364420237c55Ssthen if(res->ssl) { 3645933707f3Ssthen ERR_clear_error(); 364620237c55Ssthen if((r=SSL_read(res->ssl, magic, (int)sizeof(magic)-1)) <= 0) { 3647d896b962Ssthen int r2; 3648d896b962Ssthen if((r2=SSL_get_error(res->ssl, r)) == SSL_ERROR_ZERO_RETURN) 3649933707f3Ssthen return; 3650d896b962Ssthen log_crypto_err_io("could not SSL_read", r2); 3651933707f3Ssthen return; 3652933707f3Ssthen } 365320237c55Ssthen } else { 365420237c55Ssthen while(1) { 365520237c55Ssthen ssize_t rr = recv(res->fd, magic, sizeof(magic)-1, 0); 365620237c55Ssthen if(rr <= 0) { 365720237c55Ssthen if(rr == 0) return; 365820237c55Ssthen if(errno == EINTR || errno == EAGAIN) 365920237c55Ssthen continue; 36602c144df0Ssthen log_err("could not recv: %s", sock_strerror(errno)); 366120237c55Ssthen return; 366220237c55Ssthen } 366320237c55Ssthen r = (int)rr; 366420237c55Ssthen break; 366520237c55Ssthen } 366620237c55Ssthen } 3667933707f3Ssthen magic[6] = 0; 3668933707f3Ssthen if( r != 6 || strncmp(magic, "UBCT", 4) != 0) { 3669933707f3Ssthen verbose(VERB_QUERY, "control connection has bad magic string"); 3670933707f3Ssthen /* probably wrong tool connected, ignore it completely */ 3671933707f3Ssthen return; 3672933707f3Ssthen } 3673933707f3Ssthen 3674933707f3Ssthen /* read the command line */ 367520237c55Ssthen if(!ssl_read_line(res, buf, sizeof(buf))) { 3676933707f3Ssthen return; 3677933707f3Ssthen } 3678933707f3Ssthen snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION); 3679933707f3Ssthen if(strcmp(magic, pre) != 0) { 3680933707f3Ssthen verbose(VERB_QUERY, "control connection had bad " 3681933707f3Ssthen "version %s, cmd: %s", magic, buf); 368220237c55Ssthen ssl_printf(res, "error version mismatch\n"); 3683933707f3Ssthen return; 3684933707f3Ssthen } 3685933707f3Ssthen verbose(VERB_DETAIL, "control cmd: %s", buf); 3686933707f3Ssthen 3687933707f3Ssthen /* figure out what to do */ 368820237c55Ssthen execute_cmd(rc, res, buf, rc->worker); 3689933707f3Ssthen } 3690933707f3Ssthen 369120237c55Ssthen /** handle SSL_do_handshake changes to the file descriptor to wait for later */ 369220237c55Ssthen static int 369320237c55Ssthen remote_handshake_later(struct daemon_remote* rc, struct rc_state* s, 369420237c55Ssthen struct comm_point* c, int r, int r2) 3695933707f3Ssthen { 3696933707f3Ssthen if(r2 == SSL_ERROR_WANT_READ) { 3697933707f3Ssthen if(s->shake_state == rc_hs_read) { 3698933707f3Ssthen /* try again later */ 3699933707f3Ssthen return 0; 3700933707f3Ssthen } 3701933707f3Ssthen s->shake_state = rc_hs_read; 3702933707f3Ssthen comm_point_listen_for_rw(c, 1, 0); 3703933707f3Ssthen return 0; 3704933707f3Ssthen } else if(r2 == SSL_ERROR_WANT_WRITE) { 3705933707f3Ssthen if(s->shake_state == rc_hs_write) { 3706933707f3Ssthen /* try again later */ 3707933707f3Ssthen return 0; 3708933707f3Ssthen } 3709933707f3Ssthen s->shake_state = rc_hs_write; 3710933707f3Ssthen comm_point_listen_for_rw(c, 0, 1); 3711933707f3Ssthen return 0; 3712933707f3Ssthen } else { 3713933707f3Ssthen if(r == 0) 3714933707f3Ssthen log_err("remote control connection closed prematurely"); 3715ebf5bb73Ssthen log_addr(VERB_OPS, "failed connection from", 371645872187Ssthen &s->c->repinfo.remote_addr, s->c->repinfo.remote_addrlen); 3717d896b962Ssthen log_crypto_err_io("remote control failed ssl", r2); 3718933707f3Ssthen clean_point(rc, s); 371920237c55Ssthen } 3720933707f3Ssthen return 0; 3721933707f3Ssthen } 372220237c55Ssthen 372320237c55Ssthen int remote_control_callback(struct comm_point* c, void* arg, int err, 372420237c55Ssthen struct comm_reply* ATTR_UNUSED(rep)) 372520237c55Ssthen { 372620237c55Ssthen RES res; 372720237c55Ssthen struct rc_state* s = (struct rc_state*)arg; 372820237c55Ssthen struct daemon_remote* rc = s->rc; 372920237c55Ssthen int r; 373020237c55Ssthen if(err != NETEVENT_NOERROR) { 373120237c55Ssthen if(err==NETEVENT_TIMEOUT) 373220237c55Ssthen log_err("remote control timed out"); 373320237c55Ssthen clean_point(rc, s); 373420237c55Ssthen return 0; 373520237c55Ssthen } 373620237c55Ssthen if(s->ssl) { 373720237c55Ssthen /* (continue to) setup the SSL connection */ 373820237c55Ssthen ERR_clear_error(); 373920237c55Ssthen r = SSL_do_handshake(s->ssl); 374020237c55Ssthen if(r != 1) { 374120237c55Ssthen int r2 = SSL_get_error(s->ssl, r); 374220237c55Ssthen return remote_handshake_later(rc, s, c, r, r2); 3743933707f3Ssthen } 3744933707f3Ssthen s->shake_state = rc_none; 374520237c55Ssthen } 3746933707f3Ssthen 3747933707f3Ssthen /* once handshake has completed, check authentication */ 374831f127bbSsthen if (!rc->use_cert) { 374931f127bbSsthen verbose(VERB_ALGO, "unauthenticated remote control connection"); 375031f127bbSsthen } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) { 3751191f22c6Ssthen #ifdef HAVE_SSL_GET1_PEER_CERTIFICATE 3752191f22c6Ssthen X509* x = SSL_get1_peer_certificate(s->ssl); 3753191f22c6Ssthen #else 3754933707f3Ssthen X509* x = SSL_get_peer_certificate(s->ssl); 3755191f22c6Ssthen #endif 3756933707f3Ssthen if(!x) { 3757933707f3Ssthen verbose(VERB_DETAIL, "remote control connection " 3758933707f3Ssthen "provided no client certificate"); 3759933707f3Ssthen clean_point(rc, s); 3760933707f3Ssthen return 0; 3761933707f3Ssthen } 3762933707f3Ssthen verbose(VERB_ALGO, "remote control connection authenticated"); 3763933707f3Ssthen X509_free(x); 3764933707f3Ssthen } else { 3765933707f3Ssthen verbose(VERB_DETAIL, "remote control connection failed to " 3766933707f3Ssthen "authenticate with client certificate"); 3767933707f3Ssthen clean_point(rc, s); 3768933707f3Ssthen return 0; 3769933707f3Ssthen } 3770933707f3Ssthen 3771933707f3Ssthen /* if OK start to actually handle the request */ 377220237c55Ssthen res.ssl = s->ssl; 377320237c55Ssthen res.fd = c->fd; 377420237c55Ssthen handle_req(rc, s, &res); 3775933707f3Ssthen 3776933707f3Ssthen verbose(VERB_ALGO, "remote control operation completed"); 3777933707f3Ssthen clean_point(rc, s); 3778933707f3Ssthen return 0; 3779933707f3Ssthen } 3780