xref: /openbsd/usr.sbin/unbound/daemon/remote.c (revision a58bff56)
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
4931f127bbSsthen #ifndef HEADER_DH_H
5031f127bbSsthen #include <openssl/dh.h>
5131f127bbSsthen #endif
5231f127bbSsthen 
53933707f3Ssthen #include <ctype.h>
54933707f3Ssthen #include "daemon/remote.h"
55933707f3Ssthen #include "daemon/worker.h"
56933707f3Ssthen #include "daemon/daemon.h"
57933707f3Ssthen #include "daemon/stats.h"
58933707f3Ssthen #include "daemon/cachedump.h"
59933707f3Ssthen #include "util/log.h"
60933707f3Ssthen #include "util/config_file.h"
61933707f3Ssthen #include "util/net_help.h"
62933707f3Ssthen #include "util/module.h"
63933707f3Ssthen #include "services/listen_dnsport.h"
64933707f3Ssthen #include "services/cache/rrset.h"
65933707f3Ssthen #include "services/cache/infra.h"
66933707f3Ssthen #include "services/mesh.h"
67933707f3Ssthen #include "services/localzone.h"
68933707f3Ssthen #include "util/storage/slabhash.h"
69933707f3Ssthen #include "util/fptr_wlist.h"
70933707f3Ssthen #include "util/data/dname.h"
71933707f3Ssthen #include "validator/validator.h"
72933707f3Ssthen #include "validator/val_kcache.h"
73933707f3Ssthen #include "validator/val_kentry.h"
74163a4143Ssthen #include "validator/val_anchor.h"
75933707f3Ssthen #include "iterator/iterator.h"
76933707f3Ssthen #include "iterator/iter_fwd.h"
77933707f3Ssthen #include "iterator/iter_hints.h"
78933707f3Ssthen #include "iterator/iter_delegpt.h"
79933707f3Ssthen #include "services/outbound_list.h"
80933707f3Ssthen #include "services/outside_network.h"
81*a58bff56Ssthen #include "sldns/str2wire.h"
82*a58bff56Ssthen #include "sldns/parseutil.h"
83*a58bff56Ssthen #include "sldns/wire2str.h"
84*a58bff56Ssthen #include "sldns/sbuffer.h"
85933707f3Ssthen 
86933707f3Ssthen #ifdef HAVE_SYS_TYPES_H
87933707f3Ssthen #  include <sys/types.h>
88933707f3Ssthen #endif
8931f127bbSsthen #ifdef HAVE_SYS_STAT_H
9031f127bbSsthen #include <sys/stat.h>
9131f127bbSsthen #endif
92933707f3Ssthen #ifdef HAVE_NETDB_H
93933707f3Ssthen #include <netdb.h>
94933707f3Ssthen #endif
95933707f3Ssthen 
96933707f3Ssthen /* just for portability */
97933707f3Ssthen #ifdef SQ
98933707f3Ssthen #undef SQ
99933707f3Ssthen #endif
100933707f3Ssthen 
101933707f3Ssthen /** what to put on statistics lines between var and value, ": " or "=" */
102933707f3Ssthen #define SQ "="
103933707f3Ssthen /** if true, inhibits a lot of =0 lines from the stats output */
104933707f3Ssthen static const int inhibit_zero = 1;
105933707f3Ssthen 
106933707f3Ssthen /** subtract timers and the values do not overflow or become negative */
107933707f3Ssthen static void
108933707f3Ssthen timeval_subtract(struct timeval* d, const struct timeval* end,
109933707f3Ssthen 	const struct timeval* start)
110933707f3Ssthen {
111933707f3Ssthen #ifndef S_SPLINT_S
112933707f3Ssthen 	time_t end_usec = end->tv_usec;
113933707f3Ssthen 	d->tv_sec = end->tv_sec - start->tv_sec;
114933707f3Ssthen 	if(end_usec < start->tv_usec) {
115933707f3Ssthen 		end_usec += 1000000;
116933707f3Ssthen 		d->tv_sec--;
117933707f3Ssthen 	}
118933707f3Ssthen 	d->tv_usec = end_usec - start->tv_usec;
119933707f3Ssthen #endif
120933707f3Ssthen }
121933707f3Ssthen 
122933707f3Ssthen /** divide sum of timers to get average */
123933707f3Ssthen static void
124933707f3Ssthen timeval_divide(struct timeval* avg, const struct timeval* sum, size_t d)
125933707f3Ssthen {
126933707f3Ssthen #ifndef S_SPLINT_S
127933707f3Ssthen 	size_t leftover;
128933707f3Ssthen 	if(d == 0) {
129933707f3Ssthen 		avg->tv_sec = 0;
130933707f3Ssthen 		avg->tv_usec = 0;
131933707f3Ssthen 		return;
132933707f3Ssthen 	}
133933707f3Ssthen 	avg->tv_sec = sum->tv_sec / d;
134933707f3Ssthen 	avg->tv_usec = sum->tv_usec / d;
135933707f3Ssthen 	/* handle fraction from seconds divide */
136933707f3Ssthen 	leftover = sum->tv_sec - avg->tv_sec*d;
137933707f3Ssthen 	avg->tv_usec += (leftover*1000000)/d;
138933707f3Ssthen #endif
139933707f3Ssthen }
140933707f3Ssthen 
14131f127bbSsthen /*
14231f127bbSsthen  * The following function was generated using the openssl utility, using
143*a58bff56Ssthen  * the command : "openssl dhparam -dsaparam -C 1024"
144*a58bff56Ssthen  * (some openssl versions reject DH that is 'too small', eg. 512).
14531f127bbSsthen  */
14631f127bbSsthen #ifndef S_SPLINT_S
147*a58bff56Ssthen DH *get_dh1024()
14831f127bbSsthen {
149*a58bff56Ssthen 	static unsigned char dh1024_p[]={
150*a58bff56Ssthen 		0xB3,0x67,0x2E,0x3B,0x68,0xC5,0xDA,0x58,0x46,0xD6,0x2B,0xD3,
151*a58bff56Ssthen 		0x41,0x78,0x97,0xE4,0xE1,0x61,0x71,0x68,0xE6,0x0F,0x1D,0x78,
152*a58bff56Ssthen 		0x05,0xAA,0xF0,0xFF,0x30,0xDF,0xAC,0x49,0x7F,0xE0,0x90,0xFE,
153*a58bff56Ssthen 		0xB9,0x56,0x4E,0x3F,0xE2,0x98,0x8A,0xED,0xF5,0x28,0x39,0xEF,
154*a58bff56Ssthen 		0x2E,0xA6,0xB7,0x67,0xB2,0x43,0xE4,0x53,0xF8,0xEB,0x2C,0x1F,
155*a58bff56Ssthen 		0x06,0x77,0x3A,0x6F,0x62,0x98,0xC1,0x3B,0xF7,0xBA,0x4D,0x93,
156*a58bff56Ssthen 		0xF7,0xEB,0x5A,0xAD,0xC5,0x5F,0xF0,0xB7,0x24,0x35,0x81,0xF7,
157*a58bff56Ssthen 		0x7F,0x1F,0x24,0xC0,0xDF,0xD3,0xD8,0x40,0x72,0x7E,0xF3,0x19,
158*a58bff56Ssthen 		0x2B,0x26,0x27,0xF4,0xB6,0xB3,0xD4,0x7D,0x08,0x23,0xBE,0x68,
159*a58bff56Ssthen 		0x2B,0xCA,0xB4,0x46,0xA8,0x9E,0xDD,0x6C,0x3D,0x75,0xA6,0x48,
160*a58bff56Ssthen 		0xF7,0x44,0x43,0xBF,0x91,0xC2,0xB4,0x49,
16131f127bbSsthen 		};
162*a58bff56Ssthen 	static unsigned char dh1024_g[]={
163*a58bff56Ssthen 		0x5F,0x37,0xB5,0x80,0x4D,0xB4,0xC4,0xB2,0x37,0x12,0xD5,0x2F,
164*a58bff56Ssthen 		0x56,0x81,0xB0,0xDF,0x3D,0x27,0xA2,0x54,0xE7,0x14,0x65,0x2D,
165*a58bff56Ssthen 		0x72,0xA8,0x97,0xE0,0xA9,0x4A,0x09,0x5E,0x89,0xBE,0x34,0x9A,
166*a58bff56Ssthen 		0x90,0x98,0xC1,0xE8,0xBB,0x01,0x2B,0xC2,0x74,0x74,0x90,0x59,
167*a58bff56Ssthen 		0x0B,0x72,0x62,0x5C,0xFD,0x49,0x63,0x4B,0x38,0x91,0xF1,0x7F,
168*a58bff56Ssthen 		0x13,0x25,0xEB,0x52,0x50,0x47,0xA2,0x8C,0x32,0x28,0x42,0xAC,
169*a58bff56Ssthen 		0xBD,0x7A,0xCC,0x58,0xBE,0x36,0xDA,0x6A,0x24,0x06,0xC7,0xF1,
170*a58bff56Ssthen 		0xDA,0x8D,0x8A,0x3B,0x03,0xFA,0x6F,0x25,0xE5,0x20,0xA7,0xD6,
171*a58bff56Ssthen 		0x6F,0x74,0x61,0x53,0x14,0x81,0x29,0x04,0xB5,0x61,0x12,0x53,
172*a58bff56Ssthen 		0xA3,0xD6,0x09,0x98,0x0C,0x8F,0x1C,0xBB,0xD7,0x1C,0x2C,0xEE,
173*a58bff56Ssthen 		0x56,0x4B,0x74,0x8F,0x4A,0xF8,0xA9,0xD5,
17431f127bbSsthen 		};
17531f127bbSsthen 	DH *dh;
17631f127bbSsthen 
17731f127bbSsthen 	if ((dh=DH_new()) == NULL) return(NULL);
178*a58bff56Ssthen 	dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
179*a58bff56Ssthen 	dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
18031f127bbSsthen 	if ((dh->p == NULL) || (dh->g == NULL))
18131f127bbSsthen 		{ DH_free(dh); return(NULL); }
18231f127bbSsthen 	dh->length = 160;
18331f127bbSsthen 	return(dh);
18431f127bbSsthen }
18531f127bbSsthen #endif /* SPLINT */
18631f127bbSsthen 
187933707f3Ssthen struct daemon_remote*
188933707f3Ssthen daemon_remote_create(struct config_file* cfg)
189933707f3Ssthen {
190933707f3Ssthen 	char* s_cert;
191933707f3Ssthen 	char* s_key;
192933707f3Ssthen 	struct daemon_remote* rc = (struct daemon_remote*)calloc(1,
193933707f3Ssthen 		sizeof(*rc));
194933707f3Ssthen 	if(!rc) {
195933707f3Ssthen 		log_err("out of memory in daemon_remote_create");
196933707f3Ssthen 		return NULL;
197933707f3Ssthen 	}
198933707f3Ssthen 	rc->max_active = 10;
199933707f3Ssthen 
200933707f3Ssthen 	if(!cfg->remote_control_enable) {
201933707f3Ssthen 		rc->ctx = NULL;
202933707f3Ssthen 		return rc;
203933707f3Ssthen 	}
204933707f3Ssthen 	rc->ctx = SSL_CTX_new(SSLv23_server_method());
205933707f3Ssthen 	if(!rc->ctx) {
206933707f3Ssthen 		log_crypto_err("could not SSL_CTX_new");
207933707f3Ssthen 		free(rc);
208933707f3Ssthen 		return NULL;
209933707f3Ssthen 	}
210e10d3884Sbrad 	/* no SSLv2, SSLv3 because has defects */
211933707f3Ssthen 	if(!(SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)){
212933707f3Ssthen 		log_crypto_err("could not set SSL_OP_NO_SSLv2");
213933707f3Ssthen 		daemon_remote_delete(rc);
214933707f3Ssthen 		return NULL;
215933707f3Ssthen 	}
216e10d3884Sbrad 	if(!(SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)){
217e10d3884Sbrad 		log_crypto_err("could not set SSL_OP_NO_SSLv3");
218e10d3884Sbrad 		daemon_remote_delete(rc);
219e10d3884Sbrad 		return NULL;
220e10d3884Sbrad 	}
22131f127bbSsthen 
22231f127bbSsthen 	if (cfg->remote_control_use_cert == 0) {
22331f127bbSsthen 		/* No certificates are requested */
22431f127bbSsthen 		if(!SSL_CTX_set_cipher_list(rc->ctx, "aNULL")) {
22531f127bbSsthen 			log_crypto_err("Failed to set aNULL cipher list");
22631f127bbSsthen 			return NULL;
22731f127bbSsthen 		}
22831f127bbSsthen 
22931f127bbSsthen 		/* Since we have no certificates and hence no source of
23031f127bbSsthen 		 * DH params, let's generate and set them
23131f127bbSsthen 		 */
232*a58bff56Ssthen 		if(!SSL_CTX_set_tmp_dh(rc->ctx,get_dh1024())) {
23331f127bbSsthen 			log_crypto_err("Wanted to set DH param, but failed");
23431f127bbSsthen 			return NULL;
23531f127bbSsthen 		}
23631f127bbSsthen 		return rc;
23731f127bbSsthen 	}
23831f127bbSsthen 	rc->use_cert = 1;
239933707f3Ssthen 	s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
240933707f3Ssthen 	s_key = fname_after_chroot(cfg->server_key_file, cfg, 1);
241933707f3Ssthen 	if(!s_cert || !s_key) {
242933707f3Ssthen 		log_err("out of memory in remote control fname");
243933707f3Ssthen 		goto setup_error;
244933707f3Ssthen 	}
245933707f3Ssthen 	verbose(VERB_ALGO, "setup SSL certificates");
246933707f3Ssthen 	if (!SSL_CTX_use_certificate_file(rc->ctx,s_cert,SSL_FILETYPE_PEM)) {
247933707f3Ssthen 		log_err("Error for server-cert-file: %s", s_cert);
248933707f3Ssthen 		log_crypto_err("Error in SSL_CTX use_certificate_file");
249933707f3Ssthen 		goto setup_error;
250933707f3Ssthen 	}
251933707f3Ssthen 	if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) {
252933707f3Ssthen 		log_err("Error for server-key-file: %s", s_key);
253933707f3Ssthen 		log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
254933707f3Ssthen 		goto setup_error;
255933707f3Ssthen 	}
256933707f3Ssthen 	if(!SSL_CTX_check_private_key(rc->ctx)) {
257933707f3Ssthen 		log_err("Error for server-key-file: %s", s_key);
258933707f3Ssthen 		log_crypto_err("Error in SSL_CTX check_private_key");
259933707f3Ssthen 		goto setup_error;
260933707f3Ssthen 	}
261933707f3Ssthen 	if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) {
262933707f3Ssthen 		log_crypto_err("Error setting up SSL_CTX verify locations");
263933707f3Ssthen 	setup_error:
264933707f3Ssthen 		free(s_cert);
265933707f3Ssthen 		free(s_key);
266933707f3Ssthen 		daemon_remote_delete(rc);
267933707f3Ssthen 		return NULL;
268933707f3Ssthen 	}
269933707f3Ssthen 	SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert));
270933707f3Ssthen 	SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL);
271933707f3Ssthen 	free(s_cert);
272933707f3Ssthen 	free(s_key);
273933707f3Ssthen 
274933707f3Ssthen 	return rc;
275933707f3Ssthen }
276933707f3Ssthen 
277933707f3Ssthen void daemon_remote_clear(struct daemon_remote* rc)
278933707f3Ssthen {
279933707f3Ssthen 	struct rc_state* p, *np;
280933707f3Ssthen 	if(!rc) return;
281933707f3Ssthen 	/* but do not close the ports */
282933707f3Ssthen 	listen_list_delete(rc->accept_list);
283933707f3Ssthen 	rc->accept_list = NULL;
284933707f3Ssthen 	/* do close these sockets */
285933707f3Ssthen 	p = rc->busy_list;
286933707f3Ssthen 	while(p) {
287933707f3Ssthen 		np = p->next;
288933707f3Ssthen 		if(p->ssl)
289933707f3Ssthen 			SSL_free(p->ssl);
290933707f3Ssthen 		comm_point_delete(p->c);
291933707f3Ssthen 		free(p);
292933707f3Ssthen 		p = np;
293933707f3Ssthen 	}
294933707f3Ssthen 	rc->busy_list = NULL;
295933707f3Ssthen 	rc->active = 0;
296933707f3Ssthen 	rc->worker = NULL;
297933707f3Ssthen }
298933707f3Ssthen 
299933707f3Ssthen void daemon_remote_delete(struct daemon_remote* rc)
300933707f3Ssthen {
301933707f3Ssthen 	if(!rc) return;
302933707f3Ssthen 	daemon_remote_clear(rc);
303933707f3Ssthen 	if(rc->ctx) {
304933707f3Ssthen 		SSL_CTX_free(rc->ctx);
305933707f3Ssthen 	}
306933707f3Ssthen 	free(rc);
307933707f3Ssthen }
308933707f3Ssthen 
309933707f3Ssthen /**
310933707f3Ssthen  * Add and open a new control port
311933707f3Ssthen  * @param ip: ip str
312933707f3Ssthen  * @param nr: port nr
313933707f3Ssthen  * @param list: list head
314933707f3Ssthen  * @param noproto_is_err: if lack of protocol support is an error.
31531f127bbSsthen  * @param cfg: config with username for chown of unix-sockets.
316933707f3Ssthen  * @return false on failure.
317933707f3Ssthen  */
318933707f3Ssthen static int
31931f127bbSsthen add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err,
32031f127bbSsthen 	struct config_file* cfg)
321933707f3Ssthen {
322933707f3Ssthen 	struct addrinfo hints;
323933707f3Ssthen 	struct addrinfo* res;
324933707f3Ssthen 	struct listen_port* n;
325933707f3Ssthen 	int noproto;
326933707f3Ssthen 	int fd, r;
327933707f3Ssthen 	char port[15];
328933707f3Ssthen 	snprintf(port, sizeof(port), "%d", nr);
329933707f3Ssthen 	port[sizeof(port)-1]=0;
330933707f3Ssthen 	memset(&hints, 0, sizeof(hints));
33131f127bbSsthen 
33231f127bbSsthen 	if(ip[0] == '/') {
33331f127bbSsthen 		/* This looks like a local socket */
33431f127bbSsthen 		fd = create_local_accept_sock(ip, &noproto);
33531f127bbSsthen 		/*
33631f127bbSsthen 		 * Change socket ownership and permissions so users other
33731f127bbSsthen 		 * than root can access it provided they are in the same
33831f127bbSsthen 		 * group as the user we run as.
33931f127bbSsthen 		 */
34031f127bbSsthen 		if(fd != -1) {
34131f127bbSsthen #ifdef HAVE_CHOWN
342*a58bff56Ssthen 			if (cfg->username && cfg->username[0] &&
343*a58bff56Ssthen 				cfg_uid != (uid_t)-1)
34447dfde74Sflorian 				chown(ip, cfg_uid, cfg_gid);
34531f127bbSsthen 			chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
34631f127bbSsthen #else
34731f127bbSsthen 			(void)cfg;
34831f127bbSsthen #endif
34931f127bbSsthen 		}
35031f127bbSsthen 	} else {
351933707f3Ssthen 		hints.ai_socktype = SOCK_STREAM;
352933707f3Ssthen 		hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
353933707f3Ssthen 		if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
354933707f3Ssthen #ifdef USE_WINSOCK
355933707f3Ssthen 			if(!noproto_is_err && r == EAI_NONAME) {
356933707f3Ssthen 				/* tried to lookup the address as name */
357933707f3Ssthen 				return 1; /* return success, but do nothing */
358933707f3Ssthen 			}
359933707f3Ssthen #endif /* USE_WINSOCK */
360933707f3Ssthen 			log_err("control interface %s:%s getaddrinfo: %s %s",
361933707f3Ssthen 				ip?ip:"default", port, gai_strerror(r),
362933707f3Ssthen #ifdef EAI_SYSTEM
363933707f3Ssthen 				r==EAI_SYSTEM?(char*)strerror(errno):""
364933707f3Ssthen #else
365933707f3Ssthen 				""
366933707f3Ssthen #endif
367933707f3Ssthen 			);
368933707f3Ssthen 			return 0;
369933707f3Ssthen 		}
370933707f3Ssthen 
371933707f3Ssthen 		/* open fd */
372*a58bff56Ssthen 		fd = create_tcp_accept_sock(res, 1, &noproto, 0,
373*a58bff56Ssthen 			cfg->ip_transparent);
374933707f3Ssthen 		freeaddrinfo(res);
37531f127bbSsthen 	}
37631f127bbSsthen 
377933707f3Ssthen 	if(fd == -1 && noproto) {
378933707f3Ssthen 		if(!noproto_is_err)
379933707f3Ssthen 			return 1; /* return success, but do nothing */
380933707f3Ssthen 		log_err("cannot open control interface %s %d : "
381933707f3Ssthen 			"protocol not supported", ip, nr);
382933707f3Ssthen 		return 0;
383933707f3Ssthen 	}
384933707f3Ssthen 	if(fd == -1) {
385933707f3Ssthen 		log_err("cannot open control interface %s %d", ip, nr);
386933707f3Ssthen 		return 0;
387933707f3Ssthen 	}
388933707f3Ssthen 
389933707f3Ssthen 	/* alloc */
390933707f3Ssthen 	n = (struct listen_port*)calloc(1, sizeof(*n));
391933707f3Ssthen 	if(!n) {
392933707f3Ssthen #ifndef USE_WINSOCK
393933707f3Ssthen 		close(fd);
394933707f3Ssthen #else
395933707f3Ssthen 		closesocket(fd);
396933707f3Ssthen #endif
397933707f3Ssthen 		log_err("out of memory");
398933707f3Ssthen 		return 0;
399933707f3Ssthen 	}
400933707f3Ssthen 	n->next = *list;
401933707f3Ssthen 	*list = n;
402933707f3Ssthen 	n->fd = fd;
403933707f3Ssthen 	return 1;
404933707f3Ssthen }
405933707f3Ssthen 
406933707f3Ssthen struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
407933707f3Ssthen {
408933707f3Ssthen 	struct listen_port* l = NULL;
409933707f3Ssthen 	log_assert(cfg->remote_control_enable && cfg->control_port);
410933707f3Ssthen 	if(cfg->control_ifs) {
411933707f3Ssthen 		struct config_strlist* p;
412933707f3Ssthen 		for(p = cfg->control_ifs; p; p = p->next) {
41331f127bbSsthen 			if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) {
414933707f3Ssthen 				listening_ports_free(l);
415933707f3Ssthen 				return NULL;
416933707f3Ssthen 			}
417933707f3Ssthen 		}
418933707f3Ssthen 	} else {
419933707f3Ssthen 		/* defaults */
420933707f3Ssthen 		if(cfg->do_ip6 &&
42131f127bbSsthen 			!add_open("::1", cfg->control_port, &l, 0, cfg)) {
422933707f3Ssthen 			listening_ports_free(l);
423933707f3Ssthen 			return NULL;
424933707f3Ssthen 		}
425933707f3Ssthen 		if(cfg->do_ip4 &&
42631f127bbSsthen 			!add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) {
427933707f3Ssthen 			listening_ports_free(l);
428933707f3Ssthen 			return NULL;
429933707f3Ssthen 		}
430933707f3Ssthen 	}
431933707f3Ssthen 	return l;
432933707f3Ssthen }
433933707f3Ssthen 
434933707f3Ssthen /** open accept commpoint */
435933707f3Ssthen static int
436933707f3Ssthen accept_open(struct daemon_remote* rc, int fd)
437933707f3Ssthen {
438933707f3Ssthen 	struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
439933707f3Ssthen 	if(!n) {
440933707f3Ssthen 		log_err("out of memory");
441933707f3Ssthen 		return 0;
442933707f3Ssthen 	}
443933707f3Ssthen 	n->next = rc->accept_list;
444933707f3Ssthen 	rc->accept_list = n;
445933707f3Ssthen 	/* open commpt */
446933707f3Ssthen 	n->com = comm_point_create_raw(rc->worker->base, fd, 0,
447933707f3Ssthen 		&remote_accept_callback, rc);
448933707f3Ssthen 	if(!n->com)
449933707f3Ssthen 		return 0;
450933707f3Ssthen 	/* keep this port open, its fd is kept in the rc portlist */
451933707f3Ssthen 	n->com->do_not_close = 1;
452933707f3Ssthen 	return 1;
453933707f3Ssthen }
454933707f3Ssthen 
455933707f3Ssthen int daemon_remote_open_accept(struct daemon_remote* rc,
456933707f3Ssthen 	struct listen_port* ports, struct worker* worker)
457933707f3Ssthen {
458933707f3Ssthen 	struct listen_port* p;
459933707f3Ssthen 	rc->worker = worker;
460933707f3Ssthen 	for(p = ports; p; p = p->next) {
461933707f3Ssthen 		if(!accept_open(rc, p->fd)) {
462933707f3Ssthen 			log_err("could not create accept comm point");
463933707f3Ssthen 			return 0;
464933707f3Ssthen 		}
465933707f3Ssthen 	}
466933707f3Ssthen 	return 1;
467933707f3Ssthen }
468933707f3Ssthen 
469af4988b1Ssthen void daemon_remote_stop_accept(struct daemon_remote* rc)
470af4988b1Ssthen {
471af4988b1Ssthen 	struct listen_list* p;
472af4988b1Ssthen 	for(p=rc->accept_list; p; p=p->next) {
473af4988b1Ssthen 		comm_point_stop_listening(p->com);
474af4988b1Ssthen 	}
475af4988b1Ssthen }
476af4988b1Ssthen 
477af4988b1Ssthen void daemon_remote_start_accept(struct daemon_remote* rc)
478af4988b1Ssthen {
479af4988b1Ssthen 	struct listen_list* p;
480af4988b1Ssthen 	for(p=rc->accept_list; p; p=p->next) {
481af4988b1Ssthen 		comm_point_start_listening(p->com, -1, -1);
482af4988b1Ssthen 	}
483af4988b1Ssthen }
484af4988b1Ssthen 
485933707f3Ssthen int remote_accept_callback(struct comm_point* c, void* arg, int err,
486933707f3Ssthen 	struct comm_reply* ATTR_UNUSED(rep))
487933707f3Ssthen {
488933707f3Ssthen 	struct daemon_remote* rc = (struct daemon_remote*)arg;
489933707f3Ssthen 	struct sockaddr_storage addr;
490933707f3Ssthen 	socklen_t addrlen;
491933707f3Ssthen 	int newfd;
492933707f3Ssthen 	struct rc_state* n;
493933707f3Ssthen 	if(err != NETEVENT_NOERROR) {
494933707f3Ssthen 		log_err("error %d on remote_accept_callback", err);
495933707f3Ssthen 		return 0;
496933707f3Ssthen 	}
497933707f3Ssthen 	/* perform the accept */
498933707f3Ssthen 	newfd = comm_point_perform_accept(c, &addr, &addrlen);
499933707f3Ssthen 	if(newfd == -1)
500933707f3Ssthen 		return 0;
501933707f3Ssthen 	/* create new commpoint unless we are servicing already */
502933707f3Ssthen 	if(rc->active >= rc->max_active) {
503933707f3Ssthen 		log_warn("drop incoming remote control: too many connections");
504933707f3Ssthen 	close_exit:
505933707f3Ssthen #ifndef USE_WINSOCK
506933707f3Ssthen 		close(newfd);
507933707f3Ssthen #else
508933707f3Ssthen 		closesocket(newfd);
509933707f3Ssthen #endif
510933707f3Ssthen 		return 0;
511933707f3Ssthen 	}
512933707f3Ssthen 
513933707f3Ssthen 	/* setup commpoint to service the remote control command */
514933707f3Ssthen 	n = (struct rc_state*)calloc(1, sizeof(*n));
515933707f3Ssthen 	if(!n) {
516933707f3Ssthen 		log_err("out of memory");
517933707f3Ssthen 		goto close_exit;
518933707f3Ssthen 	}
519933707f3Ssthen 	/* start in reading state */
520933707f3Ssthen 	n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
521933707f3Ssthen 		&remote_control_callback, n);
522933707f3Ssthen 	if(!n->c) {
523933707f3Ssthen 		log_err("out of memory");
524933707f3Ssthen 		free(n);
525933707f3Ssthen 		goto close_exit;
526933707f3Ssthen 	}
527933707f3Ssthen 	log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
528933707f3Ssthen 	n->c->do_not_close = 0;
529933707f3Ssthen 	comm_point_stop_listening(n->c);
530933707f3Ssthen 	comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
531933707f3Ssthen 	memcpy(&n->c->repinfo.addr, &addr, addrlen);
532933707f3Ssthen 	n->c->repinfo.addrlen = addrlen;
533933707f3Ssthen 	n->shake_state = rc_hs_read;
534933707f3Ssthen 	n->ssl = SSL_new(rc->ctx);
535933707f3Ssthen 	if(!n->ssl) {
536933707f3Ssthen 		log_crypto_err("could not SSL_new");
537933707f3Ssthen 		comm_point_delete(n->c);
538933707f3Ssthen 		free(n);
539933707f3Ssthen 		goto close_exit;
540933707f3Ssthen 	}
541933707f3Ssthen 	SSL_set_accept_state(n->ssl);
542933707f3Ssthen         (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY);
543933707f3Ssthen 	if(!SSL_set_fd(n->ssl, newfd)) {
544933707f3Ssthen 		log_crypto_err("could not SSL_set_fd");
545933707f3Ssthen 		SSL_free(n->ssl);
546933707f3Ssthen 		comm_point_delete(n->c);
547933707f3Ssthen 		free(n);
548933707f3Ssthen 		goto close_exit;
549933707f3Ssthen 	}
550933707f3Ssthen 
551933707f3Ssthen 	n->rc = rc;
552933707f3Ssthen 	n->next = rc->busy_list;
553933707f3Ssthen 	rc->busy_list = n;
554933707f3Ssthen 	rc->active ++;
555933707f3Ssthen 
556933707f3Ssthen 	/* perform the first nonblocking read already, for windows,
557933707f3Ssthen 	 * so it can return wouldblock. could be faster too. */
558933707f3Ssthen 	(void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
559933707f3Ssthen 	return 0;
560933707f3Ssthen }
561933707f3Ssthen 
562933707f3Ssthen /** delete from list */
563933707f3Ssthen static void
564933707f3Ssthen state_list_remove_elem(struct rc_state** list, struct comm_point* c)
565933707f3Ssthen {
566933707f3Ssthen 	while(*list) {
567933707f3Ssthen 		if( (*list)->c == c) {
568933707f3Ssthen 			*list = (*list)->next;
569933707f3Ssthen 			return;
570933707f3Ssthen 		}
571933707f3Ssthen 		list = &(*list)->next;
572933707f3Ssthen 	}
573933707f3Ssthen }
574933707f3Ssthen 
575933707f3Ssthen /** decrease active count and remove commpoint from busy list */
576933707f3Ssthen static void
577933707f3Ssthen clean_point(struct daemon_remote* rc, struct rc_state* s)
578933707f3Ssthen {
579933707f3Ssthen 	state_list_remove_elem(&rc->busy_list, s->c);
580933707f3Ssthen 	rc->active --;
581933707f3Ssthen 	if(s->ssl) {
582933707f3Ssthen 		SSL_shutdown(s->ssl);
583933707f3Ssthen 		SSL_free(s->ssl);
584933707f3Ssthen 	}
585933707f3Ssthen 	comm_point_delete(s->c);
586933707f3Ssthen 	free(s);
587933707f3Ssthen }
588933707f3Ssthen 
589933707f3Ssthen int
590933707f3Ssthen ssl_print_text(SSL* ssl, const char* text)
591933707f3Ssthen {
592933707f3Ssthen 	int r;
593933707f3Ssthen 	if(!ssl)
594933707f3Ssthen 		return 0;
595933707f3Ssthen 	ERR_clear_error();
596933707f3Ssthen 	if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) {
597933707f3Ssthen 		if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
598933707f3Ssthen 			verbose(VERB_QUERY, "warning, in SSL_write, peer "
599933707f3Ssthen 				"closed connection");
600933707f3Ssthen 			return 0;
601933707f3Ssthen 		}
602933707f3Ssthen 		log_crypto_err("could not SSL_write");
603933707f3Ssthen 		return 0;
604933707f3Ssthen 	}
605933707f3Ssthen 	return 1;
606933707f3Ssthen }
607933707f3Ssthen 
608933707f3Ssthen /** print text over the ssl connection */
609933707f3Ssthen static int
610933707f3Ssthen ssl_print_vmsg(SSL* ssl, const char* format, va_list args)
611933707f3Ssthen {
612933707f3Ssthen 	char msg[1024];
613933707f3Ssthen 	vsnprintf(msg, sizeof(msg), format, args);
614933707f3Ssthen 	return ssl_print_text(ssl, msg);
615933707f3Ssthen }
616933707f3Ssthen 
617933707f3Ssthen /** printf style printing to the ssl connection */
618933707f3Ssthen int ssl_printf(SSL* ssl, const char* format, ...)
619933707f3Ssthen {
620933707f3Ssthen 	va_list args;
621933707f3Ssthen 	int ret;
622933707f3Ssthen 	va_start(args, format);
623933707f3Ssthen 	ret = ssl_print_vmsg(ssl, format, args);
624933707f3Ssthen 	va_end(args);
625933707f3Ssthen 	return ret;
626933707f3Ssthen }
627933707f3Ssthen 
628933707f3Ssthen int
629933707f3Ssthen ssl_read_line(SSL* ssl, char* buf, size_t max)
630933707f3Ssthen {
631933707f3Ssthen 	int r;
632933707f3Ssthen 	size_t len = 0;
633933707f3Ssthen 	if(!ssl)
634933707f3Ssthen 		return 0;
635933707f3Ssthen 	while(len < max) {
636933707f3Ssthen 		ERR_clear_error();
637933707f3Ssthen 		if((r=SSL_read(ssl, buf+len, 1)) <= 0) {
638933707f3Ssthen 			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
639933707f3Ssthen 				buf[len] = 0;
640933707f3Ssthen 				return 1;
641933707f3Ssthen 			}
642933707f3Ssthen 			log_crypto_err("could not SSL_read");
643933707f3Ssthen 			return 0;
644933707f3Ssthen 		}
645933707f3Ssthen 		if(buf[len] == '\n') {
646933707f3Ssthen 			/* return string without \n */
647933707f3Ssthen 			buf[len] = 0;
648933707f3Ssthen 			return 1;
649933707f3Ssthen 		}
650933707f3Ssthen 		len++;
651933707f3Ssthen 	}
652933707f3Ssthen 	buf[max-1] = 0;
653933707f3Ssthen 	log_err("control line too long (%d): %s", (int)max, buf);
654933707f3Ssthen 	return 0;
655933707f3Ssthen }
656933707f3Ssthen 
657933707f3Ssthen /** skip whitespace, return new pointer into string */
658933707f3Ssthen static char*
659933707f3Ssthen skipwhite(char* str)
660933707f3Ssthen {
661933707f3Ssthen 	/* EOS \0 is not a space */
662e10d3884Sbrad 	while( isspace((unsigned char)*str) )
663933707f3Ssthen 		str++;
664933707f3Ssthen 	return str;
665933707f3Ssthen }
666933707f3Ssthen 
667933707f3Ssthen /** send the OK to the control client */
668933707f3Ssthen static void send_ok(SSL* ssl)
669933707f3Ssthen {
670933707f3Ssthen 	(void)ssl_printf(ssl, "ok\n");
671933707f3Ssthen }
672933707f3Ssthen 
673933707f3Ssthen /** do the stop command */
674933707f3Ssthen static void
675933707f3Ssthen do_stop(SSL* ssl, struct daemon_remote* rc)
676933707f3Ssthen {
677933707f3Ssthen 	rc->worker->need_to_exit = 1;
678933707f3Ssthen 	comm_base_exit(rc->worker->base);
679933707f3Ssthen 	send_ok(ssl);
680933707f3Ssthen }
681933707f3Ssthen 
682933707f3Ssthen /** do the reload command */
683933707f3Ssthen static void
684933707f3Ssthen do_reload(SSL* ssl, struct daemon_remote* rc)
685933707f3Ssthen {
686933707f3Ssthen 	rc->worker->need_to_exit = 0;
687933707f3Ssthen 	comm_base_exit(rc->worker->base);
688933707f3Ssthen 	send_ok(ssl);
689933707f3Ssthen }
690933707f3Ssthen 
691933707f3Ssthen /** do the verbosity command */
692933707f3Ssthen static void
693933707f3Ssthen do_verbosity(SSL* ssl, char* str)
694933707f3Ssthen {
695933707f3Ssthen 	int val = atoi(str);
696933707f3Ssthen 	if(val == 0 && strcmp(str, "0") != 0) {
697933707f3Ssthen 		ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
698933707f3Ssthen 		return;
699933707f3Ssthen 	}
700933707f3Ssthen 	verbosity = val;
701933707f3Ssthen 	send_ok(ssl);
702933707f3Ssthen }
703933707f3Ssthen 
704933707f3Ssthen /** print stats from statinfo */
705933707f3Ssthen static int
706933707f3Ssthen print_stats(SSL* ssl, const char* nm, struct stats_info* s)
707933707f3Ssthen {
708933707f3Ssthen 	struct timeval avg;
709e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm,
710e10d3884Sbrad 		(unsigned long)s->svr.num_queries)) return 0;
711e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm,
712e10d3884Sbrad 		(unsigned long)(s->svr.num_queries
713933707f3Ssthen 			- s->svr.num_queries_missed_cache))) return 0;
714e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm,
715e10d3884Sbrad 		(unsigned long)s->svr.num_queries_missed_cache)) return 0;
716e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm,
717e10d3884Sbrad 		(unsigned long)s->svr.num_queries_prefetch)) return 0;
718e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm,
719e10d3884Sbrad 		(unsigned long)s->mesh_replies_sent)) return 0;
720933707f3Ssthen 	if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
721933707f3Ssthen 		(s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
722933707f3Ssthen 			(double)s->svr.sum_query_list_size/
723933707f3Ssthen 			(s->svr.num_queries_missed_cache+
724933707f3Ssthen 			s->svr.num_queries_prefetch) : 0.0)) return 0;
725e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm,
726e10d3884Sbrad 		(unsigned long)s->svr.max_query_list_size)) return 0;
727e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm,
728e10d3884Sbrad 		(unsigned long)s->mesh_jostled)) return 0;
729e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm,
730e10d3884Sbrad 		(unsigned long)s->mesh_dropped)) return 0;
731e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm,
732e10d3884Sbrad 		(unsigned long)s->mesh_num_states)) return 0;
733e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm,
734e10d3884Sbrad 		(unsigned long)s->mesh_num_reply_states)) return 0;
735933707f3Ssthen 	timeval_divide(&avg, &s->mesh_replies_sum_wait, s->mesh_replies_sent);
7360b68ff31Ssthen 	if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
737e9c7b4efSsthen 		(long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
738933707f3Ssthen 	if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
739933707f3Ssthen 		s->mesh_time_median)) return 0;
740*a58bff56Ssthen 	if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm,
741*a58bff56Ssthen 		(unsigned long)s->svr.tcp_accept_usage)) return 0;
742933707f3Ssthen 	return 1;
743933707f3Ssthen }
744933707f3Ssthen 
745933707f3Ssthen /** print stats for one thread */
746933707f3Ssthen static int
747933707f3Ssthen print_thread_stats(SSL* ssl, int i, struct stats_info* s)
748933707f3Ssthen {
749933707f3Ssthen 	char nm[16];
750933707f3Ssthen 	snprintf(nm, sizeof(nm), "thread%d", i);
751933707f3Ssthen 	nm[sizeof(nm)-1]=0;
752933707f3Ssthen 	return print_stats(ssl, nm, s);
753933707f3Ssthen }
754933707f3Ssthen 
755933707f3Ssthen /** print long number */
756933707f3Ssthen static int
757e10d3884Sbrad print_longnum(SSL* ssl, const char* desc, size_t x)
758933707f3Ssthen {
759933707f3Ssthen 	if(x > 1024*1024*1024) {
760933707f3Ssthen 		/* more than a Gb */
761933707f3Ssthen 		size_t front = x / (size_t)1000000;
762933707f3Ssthen 		size_t back = x % (size_t)1000000;
763933707f3Ssthen 		return ssl_printf(ssl, "%s%u%6.6u\n", desc,
764933707f3Ssthen 			(unsigned)front, (unsigned)back);
765933707f3Ssthen 	} else {
766e10d3884Sbrad 		return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x);
767933707f3Ssthen 	}
768933707f3Ssthen }
769933707f3Ssthen 
770933707f3Ssthen /** print mem stats */
771933707f3Ssthen static int
772933707f3Ssthen print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon)
773933707f3Ssthen {
774933707f3Ssthen 	int m;
775933707f3Ssthen 	size_t msg, rrset, val, iter;
776933707f3Ssthen #ifdef HAVE_SBRK
777933707f3Ssthen 	extern void* unbound_start_brk;
778933707f3Ssthen 	void* cur = sbrk(0);
779933707f3Ssthen 	if(!print_longnum(ssl, "mem.total.sbrk"SQ,
780933707f3Ssthen 		(size_t)((char*)cur - (char*)unbound_start_brk))) return 0;
781933707f3Ssthen #endif /* HAVE_SBRK */
782933707f3Ssthen 	msg = slabhash_get_mem(daemon->env->msg_cache);
783933707f3Ssthen 	rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
784933707f3Ssthen 	val=0;
785933707f3Ssthen 	iter=0;
786933707f3Ssthen 	m = modstack_find(&worker->env.mesh->mods, "validator");
787933707f3Ssthen 	if(m != -1) {
788933707f3Ssthen 		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
789933707f3Ssthen 			mods.mod[m]->get_mem));
790933707f3Ssthen 		val = (*worker->env.mesh->mods.mod[m]->get_mem)
791933707f3Ssthen 			(&worker->env, m);
792933707f3Ssthen 	}
793933707f3Ssthen 	m = modstack_find(&worker->env.mesh->mods, "iterator");
794933707f3Ssthen 	if(m != -1) {
795933707f3Ssthen 		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
796933707f3Ssthen 			mods.mod[m]->get_mem));
797933707f3Ssthen 		iter = (*worker->env.mesh->mods.mod[m]->get_mem)
798933707f3Ssthen 			(&worker->env, m);
799933707f3Ssthen 	}
800933707f3Ssthen 
801933707f3Ssthen 	if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
802933707f3Ssthen 		return 0;
803933707f3Ssthen 	if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
804933707f3Ssthen 		return 0;
805933707f3Ssthen 	if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
806933707f3Ssthen 		return 0;
807933707f3Ssthen 	if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
808933707f3Ssthen 		return 0;
809933707f3Ssthen 	return 1;
810933707f3Ssthen }
811933707f3Ssthen 
812933707f3Ssthen /** print uptime stats */
813933707f3Ssthen static int
814933707f3Ssthen print_uptime(SSL* ssl, struct worker* worker, int reset)
815933707f3Ssthen {
816933707f3Ssthen 	struct timeval now = *worker->env.now_tv;
817933707f3Ssthen 	struct timeval up, dt;
818933707f3Ssthen 	timeval_subtract(&up, &now, &worker->daemon->time_boot);
819933707f3Ssthen 	timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
820933707f3Ssthen 	if(reset)
821933707f3Ssthen 		worker->daemon->time_last_stat = now;
8220b68ff31Ssthen 	if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
823e9c7b4efSsthen 		(long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
8240b68ff31Ssthen 	if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
825e9c7b4efSsthen 		(long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
8260b68ff31Ssthen 	if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
827e9c7b4efSsthen 		(long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
828933707f3Ssthen 	return 1;
829933707f3Ssthen }
830933707f3Ssthen 
831933707f3Ssthen /** print extended histogram */
832933707f3Ssthen static int
833933707f3Ssthen print_hist(SSL* ssl, struct stats_info* s)
834933707f3Ssthen {
835933707f3Ssthen 	struct timehist* hist;
836933707f3Ssthen 	size_t i;
837933707f3Ssthen 	hist = timehist_setup();
838933707f3Ssthen 	if(!hist) {
839933707f3Ssthen 		log_err("out of memory");
840933707f3Ssthen 		return 0;
841933707f3Ssthen 	}
842933707f3Ssthen 	timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
843933707f3Ssthen 	for(i=0; i<hist->num; i++) {
844933707f3Ssthen 		if(!ssl_printf(ssl,
845e10d3884Sbrad 			"histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
846933707f3Ssthen 			(int)hist->buckets[i].lower.tv_sec,
847933707f3Ssthen 			(int)hist->buckets[i].lower.tv_usec,
848933707f3Ssthen 			(int)hist->buckets[i].upper.tv_sec,
849933707f3Ssthen 			(int)hist->buckets[i].upper.tv_usec,
850e10d3884Sbrad 			(unsigned long)hist->buckets[i].count)) {
851933707f3Ssthen 			timehist_delete(hist);
852933707f3Ssthen 			return 0;
853933707f3Ssthen 		}
854933707f3Ssthen 	}
855933707f3Ssthen 	timehist_delete(hist);
856933707f3Ssthen 	return 1;
857933707f3Ssthen }
858933707f3Ssthen 
859933707f3Ssthen /** print extended stats */
860933707f3Ssthen static int
861933707f3Ssthen print_ext(SSL* ssl, struct stats_info* s)
862933707f3Ssthen {
863933707f3Ssthen 	int i;
864933707f3Ssthen 	char nm[16];
8650b68ff31Ssthen 	const sldns_rr_descriptor* desc;
8660b68ff31Ssthen 	const sldns_lookup_table* lt;
867933707f3Ssthen 	/* TYPE */
868933707f3Ssthen 	for(i=0; i<STATS_QTYPE_NUM; i++) {
869933707f3Ssthen 		if(inhibit_zero && s->svr.qtype[i] == 0)
870933707f3Ssthen 			continue;
8710b68ff31Ssthen 		desc = sldns_rr_descript((uint16_t)i);
872933707f3Ssthen 		if(desc && desc->_name) {
873933707f3Ssthen 			snprintf(nm, sizeof(nm), "%s", desc->_name);
874933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_IXFR) {
875933707f3Ssthen 			snprintf(nm, sizeof(nm), "IXFR");
876933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_AXFR) {
877933707f3Ssthen 			snprintf(nm, sizeof(nm), "AXFR");
878933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_MAILA) {
879933707f3Ssthen 			snprintf(nm, sizeof(nm), "MAILA");
880933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_MAILB) {
881933707f3Ssthen 			snprintf(nm, sizeof(nm), "MAILB");
882933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_ANY) {
883933707f3Ssthen 			snprintf(nm, sizeof(nm), "ANY");
884933707f3Ssthen 		} else {
885933707f3Ssthen 			snprintf(nm, sizeof(nm), "TYPE%d", i);
886933707f3Ssthen 		}
887e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n",
888e10d3884Sbrad 			nm, (unsigned long)s->svr.qtype[i])) return 0;
889933707f3Ssthen 	}
890933707f3Ssthen 	if(!inhibit_zero || s->svr.qtype_big) {
891e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n",
892e10d3884Sbrad 			(unsigned long)s->svr.qtype_big)) return 0;
893933707f3Ssthen 	}
894933707f3Ssthen 	/* CLASS */
895933707f3Ssthen 	for(i=0; i<STATS_QCLASS_NUM; i++) {
896933707f3Ssthen 		if(inhibit_zero && s->svr.qclass[i] == 0)
897933707f3Ssthen 			continue;
8980b68ff31Ssthen 		lt = sldns_lookup_by_id(sldns_rr_classes, i);
899933707f3Ssthen 		if(lt && lt->name) {
900933707f3Ssthen 			snprintf(nm, sizeof(nm), "%s", lt->name);
901933707f3Ssthen 		} else {
902933707f3Ssthen 			snprintf(nm, sizeof(nm), "CLASS%d", i);
903933707f3Ssthen 		}
904e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n",
905e10d3884Sbrad 			nm, (unsigned long)s->svr.qclass[i])) return 0;
906933707f3Ssthen 	}
907933707f3Ssthen 	if(!inhibit_zero || s->svr.qclass_big) {
908e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n",
909e10d3884Sbrad 			(unsigned long)s->svr.qclass_big)) return 0;
910933707f3Ssthen 	}
911933707f3Ssthen 	/* OPCODE */
912933707f3Ssthen 	for(i=0; i<STATS_OPCODE_NUM; i++) {
913933707f3Ssthen 		if(inhibit_zero && s->svr.qopcode[i] == 0)
914933707f3Ssthen 			continue;
9150b68ff31Ssthen 		lt = sldns_lookup_by_id(sldns_opcodes, i);
916933707f3Ssthen 		if(lt && lt->name) {
917933707f3Ssthen 			snprintf(nm, sizeof(nm), "%s", lt->name);
918933707f3Ssthen 		} else {
919933707f3Ssthen 			snprintf(nm, sizeof(nm), "OPCODE%d", i);
920933707f3Ssthen 		}
921e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n",
922e10d3884Sbrad 			nm, (unsigned long)s->svr.qopcode[i])) return 0;
923933707f3Ssthen 	}
924933707f3Ssthen 	/* transport */
925e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n",
926e10d3884Sbrad 		(unsigned long)s->svr.qtcp)) return 0;
927e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n",
928e10d3884Sbrad 		(unsigned long)s->svr.qtcp_outgoing)) return 0;
929e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n",
930e10d3884Sbrad 		(unsigned long)s->svr.qipv6)) return 0;
931933707f3Ssthen 	/* flags */
932e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n",
933e10d3884Sbrad 		(unsigned long)s->svr.qbit_QR)) return 0;
934e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n",
935e10d3884Sbrad 		(unsigned long)s->svr.qbit_AA)) return 0;
936e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n",
937e10d3884Sbrad 		(unsigned long)s->svr.qbit_TC)) return 0;
938e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n",
939e10d3884Sbrad 		(unsigned long)s->svr.qbit_RD)) return 0;
940e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n",
941e10d3884Sbrad 		(unsigned long)s->svr.qbit_RA)) return 0;
942e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n",
943e10d3884Sbrad 		(unsigned long)s->svr.qbit_Z)) return 0;
944e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n",
945e10d3884Sbrad 		(unsigned long)s->svr.qbit_AD)) return 0;
946e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n",
947e10d3884Sbrad 		(unsigned long)s->svr.qbit_CD)) return 0;
948e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n",
949e10d3884Sbrad 		(unsigned long)s->svr.qEDNS)) return 0;
950e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n",
951e10d3884Sbrad 		(unsigned long)s->svr.qEDNS_DO)) return 0;
952933707f3Ssthen 
953933707f3Ssthen 	/* RCODE */
954933707f3Ssthen 	for(i=0; i<STATS_RCODE_NUM; i++) {
955d50c774aSbrad 		/* Always include RCODEs 0-5 */
956d50c774aSbrad 		if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
957933707f3Ssthen 			continue;
9580b68ff31Ssthen 		lt = sldns_lookup_by_id(sldns_rcodes, i);
959933707f3Ssthen 		if(lt && lt->name) {
960933707f3Ssthen 			snprintf(nm, sizeof(nm), "%s", lt->name);
961933707f3Ssthen 		} else {
962933707f3Ssthen 			snprintf(nm, sizeof(nm), "RCODE%d", i);
963933707f3Ssthen 		}
964e10d3884Sbrad 		if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n",
965e10d3884Sbrad 			nm, (unsigned long)s->svr.ans_rcode[i])) return 0;
966933707f3Ssthen 	}
967933707f3Ssthen 	if(!inhibit_zero || s->svr.ans_rcode_nodata) {
968e10d3884Sbrad 		if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n",
969e10d3884Sbrad 			(unsigned long)s->svr.ans_rcode_nodata)) return 0;
970933707f3Ssthen 	}
971933707f3Ssthen 	/* validation */
972e10d3884Sbrad 	if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n",
973e10d3884Sbrad 		(unsigned long)s->svr.ans_secure)) return 0;
974e10d3884Sbrad 	if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n",
975e10d3884Sbrad 		(unsigned long)s->svr.ans_bogus)) return 0;
976e10d3884Sbrad 	if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n",
977e10d3884Sbrad 		(unsigned long)s->svr.rrset_bogus)) return 0;
978933707f3Ssthen 	/* threat detection */
979e10d3884Sbrad 	if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n",
980e10d3884Sbrad 		(unsigned long)s->svr.unwanted_queries)) return 0;
981e10d3884Sbrad 	if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n",
982e10d3884Sbrad 		(unsigned long)s->svr.unwanted_replies)) return 0;
983e10d3884Sbrad 	/* cache counts */
984e10d3884Sbrad 	if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
985e10d3884Sbrad 		(unsigned)s->svr.msg_cache_count)) return 0;
986e10d3884Sbrad 	if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
987e10d3884Sbrad 		(unsigned)s->svr.rrset_cache_count)) return 0;
988e10d3884Sbrad 	if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
989e10d3884Sbrad 		(unsigned)s->svr.infra_cache_count)) return 0;
990e10d3884Sbrad 	if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
991e10d3884Sbrad 		(unsigned)s->svr.key_cache_count)) return 0;
992933707f3Ssthen 	return 1;
993933707f3Ssthen }
994933707f3Ssthen 
995933707f3Ssthen /** do the stats command */
996933707f3Ssthen static void
997933707f3Ssthen do_stats(SSL* ssl, struct daemon_remote* rc, int reset)
998933707f3Ssthen {
999933707f3Ssthen 	struct daemon* daemon = rc->worker->daemon;
1000933707f3Ssthen 	struct stats_info total;
1001933707f3Ssthen 	struct stats_info s;
1002933707f3Ssthen 	int i;
1003933707f3Ssthen 	log_assert(daemon->num > 0);
1004933707f3Ssthen 	/* gather all thread statistics in one place */
1005933707f3Ssthen 	for(i=0; i<daemon->num; i++) {
1006933707f3Ssthen 		server_stats_obtain(rc->worker, daemon->workers[i], &s, reset);
1007933707f3Ssthen 		if(!print_thread_stats(ssl, i, &s))
1008933707f3Ssthen 			return;
1009933707f3Ssthen 		if(i == 0)
1010933707f3Ssthen 			total = s;
1011933707f3Ssthen 		else	server_stats_add(&total, &s);
1012933707f3Ssthen 	}
1013933707f3Ssthen 	/* print the thread statistics */
1014933707f3Ssthen 	total.mesh_time_median /= (double)daemon->num;
1015933707f3Ssthen 	if(!print_stats(ssl, "total", &total))
1016933707f3Ssthen 		return;
1017933707f3Ssthen 	if(!print_uptime(ssl, rc->worker, reset))
1018933707f3Ssthen 		return;
1019933707f3Ssthen 	if(daemon->cfg->stat_extended) {
1020933707f3Ssthen 		if(!print_mem(ssl, rc->worker, daemon))
1021933707f3Ssthen 			return;
1022933707f3Ssthen 		if(!print_hist(ssl, &total))
1023933707f3Ssthen 			return;
1024933707f3Ssthen 		if(!print_ext(ssl, &total))
1025933707f3Ssthen 			return;
1026933707f3Ssthen 	}
1027933707f3Ssthen }
1028933707f3Ssthen 
1029933707f3Ssthen /** parse commandline argument domain name */
1030933707f3Ssthen static int
1031933707f3Ssthen parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs)
1032933707f3Ssthen {
10330b68ff31Ssthen 	uint8_t nm[LDNS_MAX_DOMAINLEN+1];
10340b68ff31Ssthen 	size_t nmlen = sizeof(nm);
10350b68ff31Ssthen 	int status;
1036933707f3Ssthen 	*res = NULL;
1037933707f3Ssthen 	*len = 0;
1038933707f3Ssthen 	*labs = 0;
10390b68ff31Ssthen 	status = sldns_str2wire_dname_buf(str, nm, &nmlen);
10400b68ff31Ssthen 	if(status != 0) {
10410b68ff31Ssthen 		ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
10420b68ff31Ssthen 			LDNS_WIREPARSE_OFFSET(status),
10430b68ff31Ssthen 			sldns_get_errorstr_parse(status));
1044933707f3Ssthen 		return 0;
1045933707f3Ssthen 	}
10460b68ff31Ssthen 	*res = memdup(nm, nmlen);
1047933707f3Ssthen 	if(!*res) {
1048933707f3Ssthen 		ssl_printf(ssl, "error out of memory\n");
1049933707f3Ssthen 		return 0;
1050933707f3Ssthen 	}
1051933707f3Ssthen 	*labs = dname_count_size_labels(*res, len);
1052933707f3Ssthen 	return 1;
1053933707f3Ssthen }
1054933707f3Ssthen 
1055933707f3Ssthen /** find second argument, modifies string */
1056933707f3Ssthen static int
1057933707f3Ssthen find_arg2(SSL* ssl, char* arg, char** arg2)
1058933707f3Ssthen {
1059933707f3Ssthen 	char* as = strchr(arg, ' ');
1060933707f3Ssthen 	char* at = strchr(arg, '\t');
1061933707f3Ssthen 	if(as && at) {
1062933707f3Ssthen 		if(at < as)
1063933707f3Ssthen 			as = at;
1064933707f3Ssthen 		as[0]=0;
1065933707f3Ssthen 		*arg2 = skipwhite(as+1);
1066933707f3Ssthen 	} else if(as) {
1067933707f3Ssthen 		as[0]=0;
1068933707f3Ssthen 		*arg2 = skipwhite(as+1);
1069933707f3Ssthen 	} else if(at) {
1070933707f3Ssthen 		at[0]=0;
1071933707f3Ssthen 		*arg2 = skipwhite(at+1);
1072933707f3Ssthen 	} else {
1073933707f3Ssthen 		ssl_printf(ssl, "error could not find next argument "
1074933707f3Ssthen 			"after %s\n", arg);
1075933707f3Ssthen 		return 0;
1076933707f3Ssthen 	}
1077933707f3Ssthen 	return 1;
1078933707f3Ssthen }
1079933707f3Ssthen 
1080933707f3Ssthen /** Add a new zone */
1081933707f3Ssthen static void
1082933707f3Ssthen do_zone_add(SSL* ssl, struct worker* worker, char* arg)
1083933707f3Ssthen {
1084933707f3Ssthen 	uint8_t* nm;
1085933707f3Ssthen 	int nmlabs;
1086933707f3Ssthen 	size_t nmlen;
1087933707f3Ssthen 	char* arg2;
1088933707f3Ssthen 	enum localzone_type t;
1089933707f3Ssthen 	struct local_zone* z;
1090933707f3Ssthen 	if(!find_arg2(ssl, arg, &arg2))
1091933707f3Ssthen 		return;
1092933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1093933707f3Ssthen 		return;
1094933707f3Ssthen 	if(!local_zone_str2type(arg2, &t)) {
1095933707f3Ssthen 		ssl_printf(ssl, "error not a zone type. %s\n", arg2);
1096933707f3Ssthen 		free(nm);
1097933707f3Ssthen 		return;
1098933707f3Ssthen 	}
10990b68ff31Ssthen 	lock_rw_wrlock(&worker->daemon->local_zones->lock);
1100933707f3Ssthen 	if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1101933707f3Ssthen 		nmlabs, LDNS_RR_CLASS_IN))) {
1102933707f3Ssthen 		/* already present in tree */
1103933707f3Ssthen 		lock_rw_wrlock(&z->lock);
1104933707f3Ssthen 		z->type = t; /* update type anyway */
1105933707f3Ssthen 		lock_rw_unlock(&z->lock);
1106933707f3Ssthen 		free(nm);
11070b68ff31Ssthen 		lock_rw_unlock(&worker->daemon->local_zones->lock);
1108933707f3Ssthen 		send_ok(ssl);
1109933707f3Ssthen 		return;
1110933707f3Ssthen 	}
1111933707f3Ssthen 	if(!local_zones_add_zone(worker->daemon->local_zones, nm, nmlen,
1112933707f3Ssthen 		nmlabs, LDNS_RR_CLASS_IN, t)) {
11130b68ff31Ssthen 		lock_rw_unlock(&worker->daemon->local_zones->lock);
1114933707f3Ssthen 		ssl_printf(ssl, "error out of memory\n");
1115933707f3Ssthen 		return;
1116933707f3Ssthen 	}
11170b68ff31Ssthen 	lock_rw_unlock(&worker->daemon->local_zones->lock);
1118933707f3Ssthen 	send_ok(ssl);
1119933707f3Ssthen }
1120933707f3Ssthen 
1121933707f3Ssthen /** Remove a zone */
1122933707f3Ssthen static void
1123933707f3Ssthen do_zone_remove(SSL* ssl, struct worker* worker, char* arg)
1124933707f3Ssthen {
1125933707f3Ssthen 	uint8_t* nm;
1126933707f3Ssthen 	int nmlabs;
1127933707f3Ssthen 	size_t nmlen;
1128933707f3Ssthen 	struct local_zone* z;
1129933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1130933707f3Ssthen 		return;
11310b68ff31Ssthen 	lock_rw_wrlock(&worker->daemon->local_zones->lock);
1132933707f3Ssthen 	if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1133933707f3Ssthen 		nmlabs, LDNS_RR_CLASS_IN))) {
1134933707f3Ssthen 		/* present in tree */
1135933707f3Ssthen 		local_zones_del_zone(worker->daemon->local_zones, z);
1136933707f3Ssthen 	}
11370b68ff31Ssthen 	lock_rw_unlock(&worker->daemon->local_zones->lock);
1138933707f3Ssthen 	free(nm);
1139933707f3Ssthen 	send_ok(ssl);
1140933707f3Ssthen }
1141933707f3Ssthen 
1142933707f3Ssthen /** Add new RR data */
1143933707f3Ssthen static void
1144933707f3Ssthen do_data_add(SSL* ssl, struct worker* worker, char* arg)
1145933707f3Ssthen {
11460b68ff31Ssthen 	if(!local_zones_add_RR(worker->daemon->local_zones, arg)) {
1147933707f3Ssthen 		ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1148933707f3Ssthen 		return;
1149933707f3Ssthen 	}
1150933707f3Ssthen 	send_ok(ssl);
1151933707f3Ssthen }
1152933707f3Ssthen 
1153933707f3Ssthen /** Remove RR data */
1154933707f3Ssthen static void
1155933707f3Ssthen do_data_remove(SSL* ssl, struct worker* worker, char* arg)
1156933707f3Ssthen {
1157933707f3Ssthen 	uint8_t* nm;
1158933707f3Ssthen 	int nmlabs;
1159933707f3Ssthen 	size_t nmlen;
1160933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1161933707f3Ssthen 		return;
1162933707f3Ssthen 	local_zones_del_data(worker->daemon->local_zones, nm,
1163933707f3Ssthen 		nmlen, nmlabs, LDNS_RR_CLASS_IN);
1164933707f3Ssthen 	free(nm);
1165933707f3Ssthen 	send_ok(ssl);
1166933707f3Ssthen }
1167933707f3Ssthen 
1168933707f3Ssthen /** cache lookup of nameservers */
1169933707f3Ssthen static void
1170933707f3Ssthen do_lookup(SSL* ssl, struct worker* worker, char* arg)
1171933707f3Ssthen {
1172933707f3Ssthen 	uint8_t* nm;
1173933707f3Ssthen 	int nmlabs;
1174933707f3Ssthen 	size_t nmlen;
1175933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1176933707f3Ssthen 		return;
1177933707f3Ssthen 	(void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1178933707f3Ssthen 	free(nm);
1179933707f3Ssthen }
1180933707f3Ssthen 
1181933707f3Ssthen /** flush something from rrset and msg caches */
1182933707f3Ssthen static void
1183933707f3Ssthen do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1184933707f3Ssthen 	uint16_t t, uint16_t c)
1185933707f3Ssthen {
1186933707f3Ssthen 	hashvalue_t h;
1187933707f3Ssthen 	struct query_info k;
1188933707f3Ssthen 	rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1189933707f3Ssthen 	if(t == LDNS_RR_TYPE_SOA)
1190933707f3Ssthen 		rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1191933707f3Ssthen 			PACKED_RRSET_SOA_NEG);
1192933707f3Ssthen 	k.qname = nm;
1193933707f3Ssthen 	k.qname_len = nmlen;
1194933707f3Ssthen 	k.qtype = t;
1195933707f3Ssthen 	k.qclass = c;
119657dceb2aSbrad 	h = query_info_hash(&k, 0);
1197933707f3Ssthen 	slabhash_remove(worker->env.msg_cache, h, &k);
119857dceb2aSbrad 	if(t == LDNS_RR_TYPE_AAAA) {
119957dceb2aSbrad 		/* for AAAA also flush dns64 bit_cd packet */
120057dceb2aSbrad 		h = query_info_hash(&k, BIT_CD);
120157dceb2aSbrad 		slabhash_remove(worker->env.msg_cache, h, &k);
120257dceb2aSbrad 	}
1203933707f3Ssthen }
1204933707f3Ssthen 
1205933707f3Ssthen /** flush a type */
1206933707f3Ssthen static void
1207933707f3Ssthen do_flush_type(SSL* ssl, struct worker* worker, char* arg)
1208933707f3Ssthen {
1209933707f3Ssthen 	uint8_t* nm;
1210933707f3Ssthen 	int nmlabs;
1211933707f3Ssthen 	size_t nmlen;
1212933707f3Ssthen 	char* arg2;
1213933707f3Ssthen 	uint16_t t;
1214933707f3Ssthen 	if(!find_arg2(ssl, arg, &arg2))
1215933707f3Ssthen 		return;
1216933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1217933707f3Ssthen 		return;
12180b68ff31Ssthen 	t = sldns_get_rr_type_by_name(arg2);
1219933707f3Ssthen 	do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1220933707f3Ssthen 
1221933707f3Ssthen 	free(nm);
1222933707f3Ssthen 	send_ok(ssl);
1223933707f3Ssthen }
1224933707f3Ssthen 
1225933707f3Ssthen /** flush statistics */
1226933707f3Ssthen static void
1227933707f3Ssthen do_flush_stats(SSL* ssl, struct worker* worker)
1228933707f3Ssthen {
1229933707f3Ssthen 	worker_stats_clear(worker);
1230933707f3Ssthen 	send_ok(ssl);
1231933707f3Ssthen }
1232933707f3Ssthen 
1233933707f3Ssthen /**
1234933707f3Ssthen  * Local info for deletion functions
1235933707f3Ssthen  */
1236933707f3Ssthen struct del_info {
1237933707f3Ssthen 	/** worker */
1238933707f3Ssthen 	struct worker* worker;
1239933707f3Ssthen 	/** name to delete */
1240933707f3Ssthen 	uint8_t* name;
1241933707f3Ssthen 	/** length */
1242933707f3Ssthen 	size_t len;
1243933707f3Ssthen 	/** labels */
1244933707f3Ssthen 	int labs;
1245933707f3Ssthen 	/** now */
1246e9c7b4efSsthen 	time_t now;
1247933707f3Ssthen 	/** time to invalidate to */
1248e9c7b4efSsthen 	time_t expired;
1249933707f3Ssthen 	/** number of rrsets removed */
1250933707f3Ssthen 	size_t num_rrsets;
1251933707f3Ssthen 	/** number of msgs removed */
1252933707f3Ssthen 	size_t num_msgs;
1253933707f3Ssthen 	/** number of key entries removed */
1254933707f3Ssthen 	size_t num_keys;
1255933707f3Ssthen 	/** length of addr */
1256933707f3Ssthen 	socklen_t addrlen;
1257933707f3Ssthen 	/** socket address for host deletion */
1258933707f3Ssthen 	struct sockaddr_storage addr;
1259933707f3Ssthen };
1260933707f3Ssthen 
1261933707f3Ssthen /** callback to delete hosts in infra cache */
1262933707f3Ssthen static void
1263933707f3Ssthen infra_del_host(struct lruhash_entry* e, void* arg)
1264933707f3Ssthen {
1265933707f3Ssthen 	/* entry is locked */
1266933707f3Ssthen 	struct del_info* inf = (struct del_info*)arg;
1267933707f3Ssthen 	struct infra_key* k = (struct infra_key*)e->key;
1268933707f3Ssthen 	if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1269933707f3Ssthen 		struct infra_data* d = (struct infra_data*)e->data;
1270163a4143Ssthen 		d->probedelay = 0;
1271163a4143Ssthen 		d->timeout_A = 0;
1272163a4143Ssthen 		d->timeout_AAAA = 0;
1273163a4143Ssthen 		d->timeout_other = 0;
1274163a4143Ssthen 		rtt_init(&d->rtt);
1275933707f3Ssthen 		if(d->ttl >= inf->now) {
1276933707f3Ssthen 			d->ttl = inf->expired;
1277933707f3Ssthen 			inf->num_keys++;
1278933707f3Ssthen 		}
1279933707f3Ssthen 	}
1280933707f3Ssthen }
1281933707f3Ssthen 
1282933707f3Ssthen /** flush infra cache */
1283933707f3Ssthen static void
1284933707f3Ssthen do_flush_infra(SSL* ssl, struct worker* worker, char* arg)
1285933707f3Ssthen {
1286933707f3Ssthen 	struct sockaddr_storage addr;
1287933707f3Ssthen 	socklen_t len;
1288933707f3Ssthen 	struct del_info inf;
1289933707f3Ssthen 	if(strcmp(arg, "all") == 0) {
1290933707f3Ssthen 		slabhash_clear(worker->env.infra_cache->hosts);
1291933707f3Ssthen 		send_ok(ssl);
1292933707f3Ssthen 		return;
1293933707f3Ssthen 	}
1294933707f3Ssthen 	if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1295933707f3Ssthen 		(void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1296933707f3Ssthen 		return;
1297933707f3Ssthen 	}
1298933707f3Ssthen 	/* delete all entries from cache */
1299933707f3Ssthen 	/* what we do is to set them all expired */
1300933707f3Ssthen 	inf.worker = worker;
1301933707f3Ssthen 	inf.name = 0;
1302933707f3Ssthen 	inf.len = 0;
1303933707f3Ssthen 	inf.labs = 0;
1304933707f3Ssthen 	inf.now = *worker->env.now;
1305933707f3Ssthen 	inf.expired = *worker->env.now;
1306933707f3Ssthen 	inf.expired -= 3; /* handle 3 seconds skew between threads */
1307933707f3Ssthen 	inf.num_rrsets = 0;
1308933707f3Ssthen 	inf.num_msgs = 0;
1309933707f3Ssthen 	inf.num_keys = 0;
1310933707f3Ssthen 	inf.addrlen = len;
1311933707f3Ssthen 	memmove(&inf.addr, &addr, len);
1312933707f3Ssthen 	slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1313933707f3Ssthen 		&inf);
1314933707f3Ssthen 	send_ok(ssl);
1315933707f3Ssthen }
1316933707f3Ssthen 
1317933707f3Ssthen /** flush requestlist */
1318933707f3Ssthen static void
1319933707f3Ssthen do_flush_requestlist(SSL* ssl, struct worker* worker)
1320933707f3Ssthen {
1321933707f3Ssthen 	mesh_delete_all(worker->env.mesh);
1322933707f3Ssthen 	send_ok(ssl);
1323933707f3Ssthen }
1324933707f3Ssthen 
1325933707f3Ssthen /** callback to delete rrsets in a zone */
1326933707f3Ssthen static void
1327933707f3Ssthen zone_del_rrset(struct lruhash_entry* e, void* arg)
1328933707f3Ssthen {
1329933707f3Ssthen 	/* entry is locked */
1330933707f3Ssthen 	struct del_info* inf = (struct del_info*)arg;
1331933707f3Ssthen 	struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1332933707f3Ssthen 	if(dname_subdomain_c(k->rk.dname, inf->name)) {
1333933707f3Ssthen 		struct packed_rrset_data* d =
1334933707f3Ssthen 			(struct packed_rrset_data*)e->data;
1335933707f3Ssthen 		if(d->ttl >= inf->now) {
1336933707f3Ssthen 			d->ttl = inf->expired;
1337933707f3Ssthen 			inf->num_rrsets++;
1338933707f3Ssthen 		}
1339933707f3Ssthen 	}
1340933707f3Ssthen }
1341933707f3Ssthen 
1342933707f3Ssthen /** callback to delete messages in a zone */
1343933707f3Ssthen static void
1344933707f3Ssthen zone_del_msg(struct lruhash_entry* e, void* arg)
1345933707f3Ssthen {
1346933707f3Ssthen 	/* entry is locked */
1347933707f3Ssthen 	struct del_info* inf = (struct del_info*)arg;
1348933707f3Ssthen 	struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1349933707f3Ssthen 	if(dname_subdomain_c(k->key.qname, inf->name)) {
1350933707f3Ssthen 		struct reply_info* d = (struct reply_info*)e->data;
1351933707f3Ssthen 		if(d->ttl >= inf->now) {
1352933707f3Ssthen 			d->ttl = inf->expired;
1353933707f3Ssthen 			inf->num_msgs++;
1354933707f3Ssthen 		}
1355933707f3Ssthen 	}
1356933707f3Ssthen }
1357933707f3Ssthen 
1358933707f3Ssthen /** callback to delete keys in zone */
1359933707f3Ssthen static void
1360933707f3Ssthen zone_del_kcache(struct lruhash_entry* e, void* arg)
1361933707f3Ssthen {
1362933707f3Ssthen 	/* entry is locked */
1363933707f3Ssthen 	struct del_info* inf = (struct del_info*)arg;
1364933707f3Ssthen 	struct key_entry_key* k = (struct key_entry_key*)e->key;
1365933707f3Ssthen 	if(dname_subdomain_c(k->name, inf->name)) {
1366933707f3Ssthen 		struct key_entry_data* d = (struct key_entry_data*)e->data;
1367933707f3Ssthen 		if(d->ttl >= inf->now) {
1368933707f3Ssthen 			d->ttl = inf->expired;
1369933707f3Ssthen 			inf->num_keys++;
1370933707f3Ssthen 		}
1371933707f3Ssthen 	}
1372933707f3Ssthen }
1373933707f3Ssthen 
1374933707f3Ssthen /** remove all rrsets and keys from zone from cache */
1375933707f3Ssthen static void
1376933707f3Ssthen do_flush_zone(SSL* ssl, struct worker* worker, char* arg)
1377933707f3Ssthen {
1378933707f3Ssthen 	uint8_t* nm;
1379933707f3Ssthen 	int nmlabs;
1380933707f3Ssthen 	size_t nmlen;
1381933707f3Ssthen 	struct del_info inf;
1382933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1383933707f3Ssthen 		return;
1384933707f3Ssthen 	/* delete all RRs and key entries from zone */
1385933707f3Ssthen 	/* what we do is to set them all expired */
1386933707f3Ssthen 	inf.worker = worker;
1387933707f3Ssthen 	inf.name = nm;
1388933707f3Ssthen 	inf.len = nmlen;
1389933707f3Ssthen 	inf.labs = nmlabs;
1390933707f3Ssthen 	inf.now = *worker->env.now;
1391933707f3Ssthen 	inf.expired = *worker->env.now;
1392933707f3Ssthen 	inf.expired -= 3; /* handle 3 seconds skew between threads */
1393933707f3Ssthen 	inf.num_rrsets = 0;
1394933707f3Ssthen 	inf.num_msgs = 0;
1395933707f3Ssthen 	inf.num_keys = 0;
1396933707f3Ssthen 	slabhash_traverse(&worker->env.rrset_cache->table, 1,
1397933707f3Ssthen 		&zone_del_rrset, &inf);
1398933707f3Ssthen 
1399933707f3Ssthen 	slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1400933707f3Ssthen 
1401933707f3Ssthen 	/* and validator cache */
1402933707f3Ssthen 	if(worker->env.key_cache) {
1403933707f3Ssthen 		slabhash_traverse(worker->env.key_cache->slab, 1,
1404933707f3Ssthen 			&zone_del_kcache, &inf);
1405933707f3Ssthen 	}
1406933707f3Ssthen 
1407933707f3Ssthen 	free(nm);
1408933707f3Ssthen 
1409e10d3884Sbrad 	(void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1410e10d3884Sbrad 		"and %lu key entries\n", (unsigned long)inf.num_rrsets,
1411e10d3884Sbrad 		(unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1412933707f3Ssthen }
1413933707f3Ssthen 
1414cebdf579Ssthen /** callback to delete bogus rrsets */
1415cebdf579Ssthen static void
1416cebdf579Ssthen bogus_del_rrset(struct lruhash_entry* e, void* arg)
1417cebdf579Ssthen {
1418cebdf579Ssthen 	/* entry is locked */
1419cebdf579Ssthen 	struct del_info* inf = (struct del_info*)arg;
1420cebdf579Ssthen 	struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1421cebdf579Ssthen 	if(d->security == sec_status_bogus) {
1422cebdf579Ssthen 		d->ttl = inf->expired;
1423cebdf579Ssthen 		inf->num_rrsets++;
1424cebdf579Ssthen 	}
1425cebdf579Ssthen }
1426cebdf579Ssthen 
1427cebdf579Ssthen /** callback to delete bogus messages */
1428cebdf579Ssthen static void
1429cebdf579Ssthen bogus_del_msg(struct lruhash_entry* e, void* arg)
1430cebdf579Ssthen {
1431cebdf579Ssthen 	/* entry is locked */
1432cebdf579Ssthen 	struct del_info* inf = (struct del_info*)arg;
1433cebdf579Ssthen 	struct reply_info* d = (struct reply_info*)e->data;
1434cebdf579Ssthen 	if(d->security == sec_status_bogus) {
1435cebdf579Ssthen 		d->ttl = inf->expired;
1436cebdf579Ssthen 		inf->num_msgs++;
1437cebdf579Ssthen 	}
1438cebdf579Ssthen }
1439cebdf579Ssthen 
1440cebdf579Ssthen /** callback to delete bogus keys */
1441cebdf579Ssthen static void
1442cebdf579Ssthen bogus_del_kcache(struct lruhash_entry* e, void* arg)
1443cebdf579Ssthen {
1444cebdf579Ssthen 	/* entry is locked */
1445cebdf579Ssthen 	struct del_info* inf = (struct del_info*)arg;
1446cebdf579Ssthen 	struct key_entry_data* d = (struct key_entry_data*)e->data;
1447cebdf579Ssthen 	if(d->isbad) {
1448cebdf579Ssthen 		d->ttl = inf->expired;
1449cebdf579Ssthen 		inf->num_keys++;
1450cebdf579Ssthen 	}
1451cebdf579Ssthen }
1452cebdf579Ssthen 
1453e10d3884Sbrad /** remove all bogus rrsets, msgs and keys from cache */
1454cebdf579Ssthen static void
1455cebdf579Ssthen do_flush_bogus(SSL* ssl, struct worker* worker)
1456cebdf579Ssthen {
1457cebdf579Ssthen 	struct del_info inf;
1458cebdf579Ssthen 	/* what we do is to set them all expired */
1459cebdf579Ssthen 	inf.worker = worker;
1460cebdf579Ssthen 	inf.now = *worker->env.now;
1461cebdf579Ssthen 	inf.expired = *worker->env.now;
1462cebdf579Ssthen 	inf.expired -= 3; /* handle 3 seconds skew between threads */
1463cebdf579Ssthen 	inf.num_rrsets = 0;
1464cebdf579Ssthen 	inf.num_msgs = 0;
1465cebdf579Ssthen 	inf.num_keys = 0;
1466cebdf579Ssthen 	slabhash_traverse(&worker->env.rrset_cache->table, 1,
1467cebdf579Ssthen 		&bogus_del_rrset, &inf);
1468cebdf579Ssthen 
1469cebdf579Ssthen 	slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1470cebdf579Ssthen 
1471cebdf579Ssthen 	/* and validator cache */
1472cebdf579Ssthen 	if(worker->env.key_cache) {
1473cebdf579Ssthen 		slabhash_traverse(worker->env.key_cache->slab, 1,
1474cebdf579Ssthen 			&bogus_del_kcache, &inf);
1475cebdf579Ssthen 	}
1476cebdf579Ssthen 
1477e10d3884Sbrad 	(void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1478e10d3884Sbrad 		"and %lu key entries\n", (unsigned long)inf.num_rrsets,
1479e10d3884Sbrad 		(unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1480e10d3884Sbrad }
1481e10d3884Sbrad 
1482e10d3884Sbrad /** callback to delete negative and servfail rrsets */
1483e10d3884Sbrad static void
1484e10d3884Sbrad negative_del_rrset(struct lruhash_entry* e, void* arg)
1485e10d3884Sbrad {
1486e10d3884Sbrad 	/* entry is locked */
1487e10d3884Sbrad 	struct del_info* inf = (struct del_info*)arg;
1488e10d3884Sbrad 	struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1489e10d3884Sbrad 	struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1490e10d3884Sbrad 	/* delete the parentside negative cache rrsets,
1491e10d3884Sbrad 	 * these are namerserver rrsets that failed lookup, rdata empty */
1492e10d3884Sbrad 	if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 &&
1493e10d3884Sbrad 		d->rrsig_count == 0 && d->rr_len[0] == 0) {
1494e10d3884Sbrad 		d->ttl = inf->expired;
1495e10d3884Sbrad 		inf->num_rrsets++;
1496e10d3884Sbrad 	}
1497e10d3884Sbrad }
1498e10d3884Sbrad 
1499e10d3884Sbrad /** callback to delete negative and servfail messages */
1500e10d3884Sbrad static void
1501e10d3884Sbrad negative_del_msg(struct lruhash_entry* e, void* arg)
1502e10d3884Sbrad {
1503e10d3884Sbrad 	/* entry is locked */
1504e10d3884Sbrad 	struct del_info* inf = (struct del_info*)arg;
1505e10d3884Sbrad 	struct reply_info* d = (struct reply_info*)e->data;
1506e10d3884Sbrad 	/* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1507e10d3884Sbrad 	 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1508e10d3884Sbrad 	if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) {
1509e10d3884Sbrad 		d->ttl = inf->expired;
1510e10d3884Sbrad 		inf->num_msgs++;
1511e10d3884Sbrad 	}
1512e10d3884Sbrad }
1513e10d3884Sbrad 
1514e10d3884Sbrad /** callback to delete negative key entries */
1515e10d3884Sbrad static void
1516e10d3884Sbrad negative_del_kcache(struct lruhash_entry* e, void* arg)
1517e10d3884Sbrad {
1518e10d3884Sbrad 	/* entry is locked */
1519e10d3884Sbrad 	struct del_info* inf = (struct del_info*)arg;
1520e10d3884Sbrad 	struct key_entry_data* d = (struct key_entry_data*)e->data;
1521e10d3884Sbrad 	/* could be bad because of lookup failure on the DS, DNSKEY, which
1522e10d3884Sbrad 	 * was nxdomain or servfail, and thus a result of negative lookups */
1523e10d3884Sbrad 	if(d->isbad) {
1524e10d3884Sbrad 		d->ttl = inf->expired;
1525e10d3884Sbrad 		inf->num_keys++;
1526e10d3884Sbrad 	}
1527e10d3884Sbrad }
1528e10d3884Sbrad 
1529e10d3884Sbrad /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1530e10d3884Sbrad static void
1531e10d3884Sbrad do_flush_negative(SSL* ssl, struct worker* worker)
1532e10d3884Sbrad {
1533e10d3884Sbrad 	struct del_info inf;
1534e10d3884Sbrad 	/* what we do is to set them all expired */
1535e10d3884Sbrad 	inf.worker = worker;
1536e10d3884Sbrad 	inf.now = *worker->env.now;
1537e10d3884Sbrad 	inf.expired = *worker->env.now;
1538e10d3884Sbrad 	inf.expired -= 3; /* handle 3 seconds skew between threads */
1539e10d3884Sbrad 	inf.num_rrsets = 0;
1540e10d3884Sbrad 	inf.num_msgs = 0;
1541e10d3884Sbrad 	inf.num_keys = 0;
1542e10d3884Sbrad 	slabhash_traverse(&worker->env.rrset_cache->table, 1,
1543e10d3884Sbrad 		&negative_del_rrset, &inf);
1544e10d3884Sbrad 
1545e10d3884Sbrad 	slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf);
1546e10d3884Sbrad 
1547e10d3884Sbrad 	/* and validator cache */
1548e10d3884Sbrad 	if(worker->env.key_cache) {
1549e10d3884Sbrad 		slabhash_traverse(worker->env.key_cache->slab, 1,
1550e10d3884Sbrad 			&negative_del_kcache, &inf);
1551e10d3884Sbrad 	}
1552e10d3884Sbrad 
1553e10d3884Sbrad 	(void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1554e10d3884Sbrad 		"and %lu key entries\n", (unsigned long)inf.num_rrsets,
1555e10d3884Sbrad 		(unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1556cebdf579Ssthen }
1557cebdf579Ssthen 
1558933707f3Ssthen /** remove name rrset from cache */
1559933707f3Ssthen static void
1560933707f3Ssthen do_flush_name(SSL* ssl, struct worker* w, char* arg)
1561933707f3Ssthen {
1562933707f3Ssthen 	uint8_t* nm;
1563933707f3Ssthen 	int nmlabs;
1564933707f3Ssthen 	size_t nmlen;
1565933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1566933707f3Ssthen 		return;
1567933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1568933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1569933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1570933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1571933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1572933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1573933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1574933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1575933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1576933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1577933707f3Ssthen 
1578933707f3Ssthen 	free(nm);
1579933707f3Ssthen 	send_ok(ssl);
1580933707f3Ssthen }
1581933707f3Ssthen 
1582933707f3Ssthen /** printout a delegation point info */
1583933707f3Ssthen static int
1584e10d3884Sbrad ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1585933707f3Ssthen 	struct delegpt* dp)
1586933707f3Ssthen {
1587933707f3Ssthen 	char buf[257];
1588933707f3Ssthen 	struct delegpt_ns* ns;
1589933707f3Ssthen 	struct delegpt_addr* a;
1590933707f3Ssthen 	int f = 0;
1591933707f3Ssthen 	if(str) { /* print header for forward, stub */
15920b68ff31Ssthen 		char* c = sldns_wire2str_class(dclass);
1593933707f3Ssthen 		dname_str(nm, buf);
1594e10d3884Sbrad 		if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) {
1595933707f3Ssthen 			free(c);
1596933707f3Ssthen 			return 0;
1597933707f3Ssthen 		}
1598933707f3Ssthen 		free(c);
1599933707f3Ssthen 	}
1600933707f3Ssthen 	for(ns = dp->nslist; ns; ns = ns->next) {
1601933707f3Ssthen 		dname_str(ns->name, buf);
1602933707f3Ssthen 		if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1603933707f3Ssthen 			return 0;
1604933707f3Ssthen 		f = 1;
1605933707f3Ssthen 	}
1606933707f3Ssthen 	for(a = dp->target_list; a; a = a->next_target) {
1607933707f3Ssthen 		addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1608933707f3Ssthen 		if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1609933707f3Ssthen 			return 0;
1610933707f3Ssthen 		f = 1;
1611933707f3Ssthen 	}
1612933707f3Ssthen 	return ssl_printf(ssl, "\n");
1613933707f3Ssthen }
1614933707f3Ssthen 
1615933707f3Ssthen 
1616933707f3Ssthen /** print root forwards */
1617933707f3Ssthen static int
1618933707f3Ssthen print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root)
1619933707f3Ssthen {
1620933707f3Ssthen 	struct delegpt* dp;
1621933707f3Ssthen 	dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1622933707f3Ssthen 	if(!dp)
1623933707f3Ssthen 		return ssl_printf(ssl, "off (using root hints)\n");
1624933707f3Ssthen 	/* if dp is returned it must be the root */
1625933707f3Ssthen 	log_assert(query_dname_compare(dp->name, root)==0);
1626933707f3Ssthen 	return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1627933707f3Ssthen }
1628933707f3Ssthen 
1629933707f3Ssthen /** parse args into delegpt */
1630933707f3Ssthen static struct delegpt*
1631163a4143Ssthen parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names)
1632933707f3Ssthen {
1633933707f3Ssthen 	/* parse args and add in */
1634933707f3Ssthen 	char* p = args;
1635933707f3Ssthen 	char* todo;
1636163a4143Ssthen 	struct delegpt* dp = delegpt_create_mlc(nm);
1637933707f3Ssthen 	struct sockaddr_storage addr;
1638933707f3Ssthen 	socklen_t addrlen;
1639163a4143Ssthen 	if(!dp) {
1640933707f3Ssthen 		(void)ssl_printf(ssl, "error out of memory\n");
1641933707f3Ssthen 		return NULL;
1642933707f3Ssthen 	}
1643933707f3Ssthen 	while(p) {
1644933707f3Ssthen 		todo = p;
1645933707f3Ssthen 		p = strchr(p, ' '); /* find next spot, if any */
1646933707f3Ssthen 		if(p) {
1647933707f3Ssthen 			*p++ = 0;	/* end this spot */
1648933707f3Ssthen 			p = skipwhite(p); /* position at next spot */
1649933707f3Ssthen 		}
1650933707f3Ssthen 		/* parse address */
1651933707f3Ssthen 		if(!extstrtoaddr(todo, &addr, &addrlen)) {
1652163a4143Ssthen 			if(allow_names) {
1653163a4143Ssthen 				uint8_t* n = NULL;
1654163a4143Ssthen 				size_t ln;
1655163a4143Ssthen 				int lb;
1656163a4143Ssthen 				if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
1657163a4143Ssthen 					(void)ssl_printf(ssl, "error cannot "
1658163a4143Ssthen 						"parse IP address or name "
1659163a4143Ssthen 						"'%s'\n", todo);
1660163a4143Ssthen 					delegpt_free_mlc(dp);
1661933707f3Ssthen 					return NULL;
1662933707f3Ssthen 				}
1663163a4143Ssthen 				if(!delegpt_add_ns_mlc(dp, n, 0)) {
1664933707f3Ssthen 					(void)ssl_printf(ssl, "error out of memory\n");
1665cebdf579Ssthen 					free(n);
1666163a4143Ssthen 					delegpt_free_mlc(dp);
1667933707f3Ssthen 					return NULL;
1668933707f3Ssthen 				}
1669163a4143Ssthen 				free(n);
1670163a4143Ssthen 
1671163a4143Ssthen 			} else {
1672163a4143Ssthen 				(void)ssl_printf(ssl, "error cannot parse"
1673163a4143Ssthen 					" IP address '%s'\n", todo);
1674163a4143Ssthen 				delegpt_free_mlc(dp);
1675163a4143Ssthen 				return NULL;
1676163a4143Ssthen 			}
1677163a4143Ssthen 		} else {
1678163a4143Ssthen 			/* add address */
1679163a4143Ssthen 			if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) {
1680163a4143Ssthen 				(void)ssl_printf(ssl, "error out of memory\n");
1681163a4143Ssthen 				delegpt_free_mlc(dp);
1682163a4143Ssthen 				return NULL;
1683163a4143Ssthen 			}
1684163a4143Ssthen 		}
1685933707f3Ssthen 	}
1686933707f3Ssthen 	return dp;
1687933707f3Ssthen }
1688933707f3Ssthen 
1689933707f3Ssthen /** do the status command */
1690933707f3Ssthen static void
1691933707f3Ssthen do_forward(SSL* ssl, struct worker* worker, char* args)
1692933707f3Ssthen {
1693933707f3Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1694933707f3Ssthen 	uint8_t* root = (uint8_t*)"\000";
1695933707f3Ssthen 	if(!fwd) {
1696933707f3Ssthen 		(void)ssl_printf(ssl, "error: structure not allocated\n");
1697933707f3Ssthen 		return;
1698933707f3Ssthen 	}
1699933707f3Ssthen 	if(args == NULL || args[0] == 0) {
1700933707f3Ssthen 		(void)print_root_fwds(ssl, fwd, root);
1701933707f3Ssthen 		return;
1702933707f3Ssthen 	}
1703933707f3Ssthen 	/* set root forwards for this thread. since we are in remote control
1704933707f3Ssthen 	 * the actual mesh is not running, so we can freely edit it. */
1705933707f3Ssthen 	/* delete all the existing queries first */
1706933707f3Ssthen 	mesh_delete_all(worker->env.mesh);
1707933707f3Ssthen 	if(strcmp(args, "off") == 0) {
1708933707f3Ssthen 		forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
1709933707f3Ssthen 	} else {
1710933707f3Ssthen 		struct delegpt* dp;
1711163a4143Ssthen 		if(!(dp = parse_delegpt(ssl, args, root, 0)))
1712933707f3Ssthen 			return;
1713933707f3Ssthen 		if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1714933707f3Ssthen 			(void)ssl_printf(ssl, "error out of memory\n");
1715933707f3Ssthen 			return;
1716933707f3Ssthen 		}
1717933707f3Ssthen 	}
1718933707f3Ssthen 	send_ok(ssl);
1719933707f3Ssthen }
1720933707f3Ssthen 
1721163a4143Ssthen static int
1722163a4143Ssthen parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp,
1723163a4143Ssthen 	int* insecure, int* prime)
1724163a4143Ssthen {
1725163a4143Ssthen 	char* zonename;
1726163a4143Ssthen 	char* rest;
1727163a4143Ssthen 	size_t nmlen;
1728163a4143Ssthen 	int nmlabs;
1729163a4143Ssthen 	/* parse all -x args */
1730163a4143Ssthen 	while(args[0] == '+') {
1731163a4143Ssthen 		if(!find_arg2(ssl, args, &rest))
1732163a4143Ssthen 			return 0;
1733163a4143Ssthen 		while(*(++args) != 0) {
1734163a4143Ssthen 			if(*args == 'i' && insecure)
1735163a4143Ssthen 				*insecure = 1;
1736163a4143Ssthen 			else if(*args == 'p' && prime)
1737163a4143Ssthen 				*prime = 1;
1738163a4143Ssthen 			else {
1739163a4143Ssthen 				(void)ssl_printf(ssl, "error: unknown option %s\n", args);
1740163a4143Ssthen 				return 0;
1741163a4143Ssthen 			}
1742163a4143Ssthen 		}
1743163a4143Ssthen 		args = rest;
1744163a4143Ssthen 	}
1745163a4143Ssthen 	/* parse name */
1746163a4143Ssthen 	if(dp) {
1747163a4143Ssthen 		if(!find_arg2(ssl, args, &rest))
1748163a4143Ssthen 			return 0;
1749163a4143Ssthen 		zonename = args;
1750163a4143Ssthen 		args = rest;
1751163a4143Ssthen 	} else	zonename = args;
1752163a4143Ssthen 	if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
1753163a4143Ssthen 		return 0;
1754163a4143Ssthen 
1755163a4143Ssthen 	/* parse dp */
1756163a4143Ssthen 	if(dp) {
1757163a4143Ssthen 		if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
1758163a4143Ssthen 			free(*nm);
1759163a4143Ssthen 			return 0;
1760163a4143Ssthen 		}
1761163a4143Ssthen 	}
1762163a4143Ssthen 	return 1;
1763163a4143Ssthen }
1764163a4143Ssthen 
1765163a4143Ssthen /** do the forward_add command */
1766163a4143Ssthen static void
1767163a4143Ssthen do_forward_add(SSL* ssl, struct worker* worker, char* args)
1768163a4143Ssthen {
1769163a4143Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1770163a4143Ssthen 	int insecure = 0;
1771163a4143Ssthen 	uint8_t* nm = NULL;
1772163a4143Ssthen 	struct delegpt* dp = NULL;
1773163a4143Ssthen 	if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
1774163a4143Ssthen 		return;
17750b68ff31Ssthen 	if(insecure && worker->env.anchors) {
1776163a4143Ssthen 		if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1777163a4143Ssthen 			nm)) {
1778163a4143Ssthen 			(void)ssl_printf(ssl, "error out of memory\n");
1779163a4143Ssthen 			delegpt_free_mlc(dp);
1780163a4143Ssthen 			free(nm);
1781163a4143Ssthen 			return;
1782163a4143Ssthen 		}
1783163a4143Ssthen 	}
1784163a4143Ssthen 	if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1785163a4143Ssthen 		(void)ssl_printf(ssl, "error out of memory\n");
1786163a4143Ssthen 		free(nm);
1787163a4143Ssthen 		return;
1788163a4143Ssthen 	}
1789163a4143Ssthen 	free(nm);
1790163a4143Ssthen 	send_ok(ssl);
1791163a4143Ssthen }
1792163a4143Ssthen 
1793163a4143Ssthen /** do the forward_remove command */
1794163a4143Ssthen static void
1795163a4143Ssthen do_forward_remove(SSL* ssl, struct worker* worker, char* args)
1796163a4143Ssthen {
1797163a4143Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1798163a4143Ssthen 	int insecure = 0;
1799163a4143Ssthen 	uint8_t* nm = NULL;
1800163a4143Ssthen 	if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1801163a4143Ssthen 		return;
18020b68ff31Ssthen 	if(insecure && worker->env.anchors)
1803163a4143Ssthen 		anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1804163a4143Ssthen 			nm);
1805163a4143Ssthen 	forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
1806163a4143Ssthen 	free(nm);
1807163a4143Ssthen 	send_ok(ssl);
1808163a4143Ssthen }
1809163a4143Ssthen 
1810163a4143Ssthen /** do the stub_add command */
1811163a4143Ssthen static void
1812163a4143Ssthen do_stub_add(SSL* ssl, struct worker* worker, char* args)
1813163a4143Ssthen {
1814163a4143Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1815163a4143Ssthen 	int insecure = 0, prime = 0;
1816163a4143Ssthen 	uint8_t* nm = NULL;
1817163a4143Ssthen 	struct delegpt* dp = NULL;
1818163a4143Ssthen 	if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
1819163a4143Ssthen 		return;
18200b68ff31Ssthen 	if(insecure && worker->env.anchors) {
1821163a4143Ssthen 		if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1822163a4143Ssthen 			nm)) {
1823163a4143Ssthen 			(void)ssl_printf(ssl, "error out of memory\n");
1824163a4143Ssthen 			delegpt_free_mlc(dp);
1825163a4143Ssthen 			free(nm);
1826163a4143Ssthen 			return;
1827163a4143Ssthen 		}
1828163a4143Ssthen 	}
1829163a4143Ssthen 	if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
18300b68ff31Ssthen 		if(insecure && worker->env.anchors)
18310b68ff31Ssthen 			anchors_delete_insecure(worker->env.anchors,
1832163a4143Ssthen 				LDNS_RR_CLASS_IN, nm);
1833163a4143Ssthen 		(void)ssl_printf(ssl, "error out of memory\n");
1834163a4143Ssthen 		delegpt_free_mlc(dp);
1835163a4143Ssthen 		free(nm);
1836163a4143Ssthen 		return;
1837163a4143Ssthen 	}
1838163a4143Ssthen 	if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
1839163a4143Ssthen 		(void)ssl_printf(ssl, "error out of memory\n");
1840163a4143Ssthen 		forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
18410b68ff31Ssthen 		if(insecure && worker->env.anchors)
18420b68ff31Ssthen 			anchors_delete_insecure(worker->env.anchors,
1843163a4143Ssthen 				LDNS_RR_CLASS_IN, nm);
1844163a4143Ssthen 		free(nm);
1845163a4143Ssthen 		return;
1846163a4143Ssthen 	}
1847163a4143Ssthen 	free(nm);
1848163a4143Ssthen 	send_ok(ssl);
1849163a4143Ssthen }
1850163a4143Ssthen 
1851163a4143Ssthen /** do the stub_remove command */
1852163a4143Ssthen static void
1853163a4143Ssthen do_stub_remove(SSL* ssl, struct worker* worker, char* args)
1854163a4143Ssthen {
1855163a4143Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1856163a4143Ssthen 	int insecure = 0;
1857163a4143Ssthen 	uint8_t* nm = NULL;
1858163a4143Ssthen 	if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1859163a4143Ssthen 		return;
18600b68ff31Ssthen 	if(insecure && worker->env.anchors)
1861163a4143Ssthen 		anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1862163a4143Ssthen 			nm);
1863163a4143Ssthen 	forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1864163a4143Ssthen 	hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
1865163a4143Ssthen 	free(nm);
1866163a4143Ssthen 	send_ok(ssl);
1867163a4143Ssthen }
1868163a4143Ssthen 
1869e9c7b4efSsthen /** do the insecure_add command */
1870e9c7b4efSsthen static void
1871e9c7b4efSsthen do_insecure_add(SSL* ssl, struct worker* worker, char* arg)
1872e9c7b4efSsthen {
1873e9c7b4efSsthen 	size_t nmlen;
1874e9c7b4efSsthen 	int nmlabs;
1875e9c7b4efSsthen 	uint8_t* nm = NULL;
1876e9c7b4efSsthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1877e9c7b4efSsthen 		return;
18780b68ff31Ssthen 	if(worker->env.anchors) {
18790b68ff31Ssthen 		if(!anchors_add_insecure(worker->env.anchors,
18800b68ff31Ssthen 			LDNS_RR_CLASS_IN, nm)) {
1881e9c7b4efSsthen 			(void)ssl_printf(ssl, "error out of memory\n");
1882e9c7b4efSsthen 			free(nm);
1883e9c7b4efSsthen 			return;
1884e9c7b4efSsthen 		}
18850b68ff31Ssthen 	}
1886e9c7b4efSsthen 	free(nm);
1887e9c7b4efSsthen 	send_ok(ssl);
1888e9c7b4efSsthen }
1889e9c7b4efSsthen 
1890e9c7b4efSsthen /** do the insecure_remove command */
1891e9c7b4efSsthen static void
1892e9c7b4efSsthen do_insecure_remove(SSL* ssl, struct worker* worker, char* arg)
1893e9c7b4efSsthen {
1894e9c7b4efSsthen 	size_t nmlen;
1895e9c7b4efSsthen 	int nmlabs;
1896e9c7b4efSsthen 	uint8_t* nm = NULL;
1897e9c7b4efSsthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1898e9c7b4efSsthen 		return;
18990b68ff31Ssthen 	if(worker->env.anchors)
19000b68ff31Ssthen 		anchors_delete_insecure(worker->env.anchors,
19010b68ff31Ssthen 			LDNS_RR_CLASS_IN, nm);
1902e9c7b4efSsthen 	free(nm);
1903e9c7b4efSsthen 	send_ok(ssl);
1904e9c7b4efSsthen }
1905e9c7b4efSsthen 
1906*a58bff56Ssthen static void
1907*a58bff56Ssthen do_insecure_list(SSL* ssl, struct worker* worker)
1908*a58bff56Ssthen {
1909*a58bff56Ssthen 	char buf[257];
1910*a58bff56Ssthen 	struct trust_anchor* a;
1911*a58bff56Ssthen 	if(worker->env.anchors) {
1912*a58bff56Ssthen 		RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) {
1913*a58bff56Ssthen 			if(a->numDS == 0 && a->numDNSKEY == 0) {
1914*a58bff56Ssthen 				dname_str(a->name, buf);
1915*a58bff56Ssthen 				ssl_printf(ssl, "%s\n", buf);
1916*a58bff56Ssthen 			}
1917*a58bff56Ssthen 		}
1918*a58bff56Ssthen 	}
1919*a58bff56Ssthen }
1920*a58bff56Ssthen 
1921933707f3Ssthen /** do the status command */
1922933707f3Ssthen static void
1923933707f3Ssthen do_status(SSL* ssl, struct worker* worker)
1924933707f3Ssthen {
1925933707f3Ssthen 	int i;
1926933707f3Ssthen 	time_t uptime;
1927933707f3Ssthen 	if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
1928933707f3Ssthen 		return;
1929933707f3Ssthen 	if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
1930933707f3Ssthen 		return;
1931933707f3Ssthen 	if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
1932933707f3Ssthen 		return;
1933933707f3Ssthen 	if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
1934933707f3Ssthen 		return;
1935933707f3Ssthen 	for(i=0; i<worker->daemon->mods.num; i++) {
1936933707f3Ssthen 		if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
1937933707f3Ssthen 			return;
1938933707f3Ssthen 	}
1939933707f3Ssthen 	if(!ssl_printf(ssl, " ]\n"))
1940933707f3Ssthen 		return;
1941933707f3Ssthen 	uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
19420b68ff31Ssthen 	if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
1943933707f3Ssthen 		return;
1944e10d3884Sbrad 	if(!ssl_printf(ssl, "options:%s%s\n" ,
1945e10d3884Sbrad 		(worker->daemon->reuseport?" reuseport":""),
1946e10d3884Sbrad 		(worker->daemon->rc->accept_list?" control(ssl)":"")))
1947e10d3884Sbrad 		return;
1948933707f3Ssthen 	if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
1949933707f3Ssthen 		(int)getpid()))
1950933707f3Ssthen 		return;
1951933707f3Ssthen }
1952933707f3Ssthen 
1953933707f3Ssthen /** get age for the mesh state */
1954933707f3Ssthen static void
1955933707f3Ssthen get_mesh_age(struct mesh_state* m, char* buf, size_t len,
1956933707f3Ssthen 	struct module_env* env)
1957933707f3Ssthen {
1958933707f3Ssthen 	if(m->reply_list) {
1959933707f3Ssthen 		struct timeval d;
1960933707f3Ssthen 		struct mesh_reply* r = m->reply_list;
1961933707f3Ssthen 		/* last reply is the oldest */
1962933707f3Ssthen 		while(r && r->next)
1963933707f3Ssthen 			r = r->next;
1964933707f3Ssthen 		timeval_subtract(&d, env->now_tv, &r->start_time);
19650b68ff31Ssthen 		snprintf(buf, len, ARG_LL "d.%6.6d",
19660b68ff31Ssthen 			(long long)d.tv_sec, (int)d.tv_usec);
1967933707f3Ssthen 	} else {
1968933707f3Ssthen 		snprintf(buf, len, "-");
1969933707f3Ssthen 	}
1970933707f3Ssthen }
1971933707f3Ssthen 
1972933707f3Ssthen /** get status of a mesh state */
1973933707f3Ssthen static void
1974933707f3Ssthen get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
1975933707f3Ssthen 	char* buf, size_t len)
1976933707f3Ssthen {
1977933707f3Ssthen 	enum module_ext_state s = m->s.ext_state[m->s.curmod];
1978933707f3Ssthen 	const char *modname = mesh->mods.mod[m->s.curmod]->name;
1979933707f3Ssthen 	size_t l;
1980933707f3Ssthen 	if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
1981933707f3Ssthen 		m->s.minfo[m->s.curmod]) {
1982933707f3Ssthen 		/* break into iterator to find out who its waiting for */
1983933707f3Ssthen 		struct iter_qstate* qstate = (struct iter_qstate*)
1984933707f3Ssthen 			m->s.minfo[m->s.curmod];
1985933707f3Ssthen 		struct outbound_list* ol = &qstate->outlist;
1986933707f3Ssthen 		struct outbound_entry* e;
1987933707f3Ssthen 		snprintf(buf, len, "%s wait for", modname);
1988933707f3Ssthen 		l = strlen(buf);
1989933707f3Ssthen 		buf += l; len -= l;
1990933707f3Ssthen 		if(ol->first == NULL)
1991933707f3Ssthen 			snprintf(buf, len, " (empty_list)");
1992933707f3Ssthen 		for(e = ol->first; e; e = e->next) {
1993933707f3Ssthen 			snprintf(buf, len, " ");
1994933707f3Ssthen 			l = strlen(buf);
1995933707f3Ssthen 			buf += l; len -= l;
1996933707f3Ssthen 			addr_to_str(&e->qsent->addr, e->qsent->addrlen,
1997933707f3Ssthen 				buf, len);
1998933707f3Ssthen 			l = strlen(buf);
1999933707f3Ssthen 			buf += l; len -= l;
2000933707f3Ssthen 		}
2001933707f3Ssthen 	} else if(s == module_wait_subquery) {
2002933707f3Ssthen 		/* look in subs from mesh state to see what */
2003933707f3Ssthen 		char nm[257];
2004933707f3Ssthen 		struct mesh_state_ref* sub;
2005933707f3Ssthen 		snprintf(buf, len, "%s wants", modname);
2006933707f3Ssthen 		l = strlen(buf);
2007933707f3Ssthen 		buf += l; len -= l;
2008933707f3Ssthen 		if(m->sub_set.count == 0)
2009933707f3Ssthen 			snprintf(buf, len, " (empty_list)");
2010933707f3Ssthen 		RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
20110b68ff31Ssthen 			char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
20120b68ff31Ssthen 			char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
2013933707f3Ssthen 			dname_str(sub->s->s.qinfo.qname, nm);
20140b68ff31Ssthen 			snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
20150b68ff31Ssthen 				(c?c:"CLASS??"), nm);
2016933707f3Ssthen 			l = strlen(buf);
2017933707f3Ssthen 			buf += l; len -= l;
2018933707f3Ssthen 			free(t);
2019933707f3Ssthen 			free(c);
2020933707f3Ssthen 		}
2021933707f3Ssthen 	} else {
2022933707f3Ssthen 		snprintf(buf, len, "%s is %s", modname, strextstate(s));
2023933707f3Ssthen 	}
2024933707f3Ssthen }
2025933707f3Ssthen 
2026933707f3Ssthen /** do the dump_requestlist command */
2027933707f3Ssthen static void
2028933707f3Ssthen do_dump_requestlist(SSL* ssl, struct worker* worker)
2029933707f3Ssthen {
2030933707f3Ssthen 	struct mesh_area* mesh;
2031933707f3Ssthen 	struct mesh_state* m;
2032933707f3Ssthen 	int num = 0;
2033933707f3Ssthen 	char buf[257];
2034933707f3Ssthen 	char timebuf[32];
2035933707f3Ssthen 	char statbuf[10240];
2036933707f3Ssthen 	if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
2037933707f3Ssthen 		return;
2038933707f3Ssthen 	if(!ssl_printf(ssl, "#   type cl name    seconds    module status\n"))
2039933707f3Ssthen 		return;
2040933707f3Ssthen 	/* show worker mesh contents */
2041933707f3Ssthen 	mesh = worker->env.mesh;
2042933707f3Ssthen 	if(!mesh) return;
2043933707f3Ssthen 	RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
20440b68ff31Ssthen 		char* t = sldns_wire2str_type(m->s.qinfo.qtype);
20450b68ff31Ssthen 		char* c = sldns_wire2str_class(m->s.qinfo.qclass);
2046933707f3Ssthen 		dname_str(m->s.qinfo.qname, buf);
2047933707f3Ssthen 		get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
2048933707f3Ssthen 		get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
2049933707f3Ssthen 		if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
20500b68ff31Ssthen 			num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
20510b68ff31Ssthen 			statbuf)) {
2052933707f3Ssthen 			free(t);
2053933707f3Ssthen 			free(c);
2054933707f3Ssthen 			return;
2055933707f3Ssthen 		}
2056933707f3Ssthen 		num++;
2057933707f3Ssthen 		free(t);
2058933707f3Ssthen 		free(c);
2059933707f3Ssthen 	}
2060933707f3Ssthen }
2061933707f3Ssthen 
2062933707f3Ssthen /** structure for argument data for dump infra host */
2063933707f3Ssthen struct infra_arg {
2064933707f3Ssthen 	/** the infra cache */
2065933707f3Ssthen 	struct infra_cache* infra;
2066933707f3Ssthen 	/** the SSL connection */
2067933707f3Ssthen 	SSL* ssl;
2068933707f3Ssthen 	/** the time now */
2069e9c7b4efSsthen 	time_t now;
2070e10d3884Sbrad 	/** ssl failure? stop writing and skip the rest.  If the tcp
2071e10d3884Sbrad 	 * connection is broken, and writes fail, we then stop writing. */
2072e10d3884Sbrad 	int ssl_failed;
2073933707f3Ssthen };
2074933707f3Ssthen 
2075933707f3Ssthen /** callback for every host element in the infra cache */
2076933707f3Ssthen static void
2077933707f3Ssthen dump_infra_host(struct lruhash_entry* e, void* arg)
2078933707f3Ssthen {
2079933707f3Ssthen 	struct infra_arg* a = (struct infra_arg*)arg;
2080933707f3Ssthen 	struct infra_key* k = (struct infra_key*)e->key;
2081933707f3Ssthen 	struct infra_data* d = (struct infra_data*)e->data;
2082933707f3Ssthen 	char ip_str[1024];
2083933707f3Ssthen 	char name[257];
2084e10d3884Sbrad 	if(a->ssl_failed)
2085e10d3884Sbrad 		return;
2086933707f3Ssthen 	addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
2087933707f3Ssthen 	dname_str(k->zonename, name);
2088933707f3Ssthen 	/* skip expired stuff (only backed off) */
2089933707f3Ssthen 	if(d->ttl < a->now) {
2090933707f3Ssthen 		if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
2091933707f3Ssthen 			if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
2092e10d3884Sbrad 				name, d->rtt.rto))  {
2093e10d3884Sbrad 				a->ssl_failed = 1;
2094e10d3884Sbrad 				return;
2095e10d3884Sbrad 			}
2096933707f3Ssthen 		}
2097933707f3Ssthen 		return;
2098933707f3Ssthen 	}
2099e10d3884Sbrad 	if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
2100163a4143Ssthen 		"tA %d tAAAA %d tother %d "
2101933707f3Ssthen 		"ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
2102e10d3884Sbrad 		"other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now),
2103933707f3Ssthen 		d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
2104163a4143Ssthen 		d->timeout_A, d->timeout_AAAA, d->timeout_other,
2105933707f3Ssthen 		(int)d->edns_lame_known, (int)d->edns_version,
2106*a58bff56Ssthen 		(int)(a->now<d->probedelay?(d->probedelay - a->now):0),
2107933707f3Ssthen 		(int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
2108e10d3884Sbrad 		(int)d->lame_other)) {
2109e10d3884Sbrad 		a->ssl_failed = 1;
2110933707f3Ssthen 		return;
2111933707f3Ssthen 	}
2112e10d3884Sbrad }
2113933707f3Ssthen 
2114933707f3Ssthen /** do the dump_infra command */
2115933707f3Ssthen static void
2116933707f3Ssthen do_dump_infra(SSL* ssl, struct worker* worker)
2117933707f3Ssthen {
2118933707f3Ssthen 	struct infra_arg arg;
2119933707f3Ssthen 	arg.infra = worker->env.infra_cache;
2120933707f3Ssthen 	arg.ssl = ssl;
2121933707f3Ssthen 	arg.now = *worker->env.now;
2122e10d3884Sbrad 	arg.ssl_failed = 0;
2123933707f3Ssthen 	slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
2124933707f3Ssthen }
2125933707f3Ssthen 
2126933707f3Ssthen /** do the log_reopen command */
2127933707f3Ssthen static void
2128933707f3Ssthen do_log_reopen(SSL* ssl, struct worker* worker)
2129933707f3Ssthen {
2130933707f3Ssthen 	struct config_file* cfg = worker->env.cfg;
2131933707f3Ssthen 	send_ok(ssl);
2132933707f3Ssthen 	log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
2133933707f3Ssthen }
2134933707f3Ssthen 
2135933707f3Ssthen /** do the set_option command */
2136933707f3Ssthen static void
2137933707f3Ssthen do_set_option(SSL* ssl, struct worker* worker, char* arg)
2138933707f3Ssthen {
2139933707f3Ssthen 	char* arg2;
2140933707f3Ssthen 	if(!find_arg2(ssl, arg, &arg2))
2141933707f3Ssthen 		return;
2142933707f3Ssthen 	if(!config_set_option(worker->env.cfg, arg, arg2)) {
2143933707f3Ssthen 		(void)ssl_printf(ssl, "error setting option\n");
2144933707f3Ssthen 		return;
2145933707f3Ssthen 	}
2146933707f3Ssthen 	send_ok(ssl);
2147933707f3Ssthen }
2148933707f3Ssthen 
2149933707f3Ssthen /* routine to printout option values over SSL */
2150933707f3Ssthen void remote_get_opt_ssl(char* line, void* arg)
2151933707f3Ssthen {
2152933707f3Ssthen 	SSL* ssl = (SSL*)arg;
2153933707f3Ssthen 	(void)ssl_printf(ssl, "%s\n", line);
2154933707f3Ssthen }
2155933707f3Ssthen 
2156933707f3Ssthen /** do the get_option command */
2157933707f3Ssthen static void
2158933707f3Ssthen do_get_option(SSL* ssl, struct worker* worker, char* arg)
2159933707f3Ssthen {
2160933707f3Ssthen 	int r;
2161933707f3Ssthen 	r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
2162933707f3Ssthen 	if(!r) {
2163933707f3Ssthen 		(void)ssl_printf(ssl, "error unknown option\n");
2164933707f3Ssthen 		return;
2165933707f3Ssthen 	}
2166933707f3Ssthen }
2167933707f3Ssthen 
2168933707f3Ssthen /** do the list_forwards command */
2169933707f3Ssthen static void
2170933707f3Ssthen do_list_forwards(SSL* ssl, struct worker* worker)
2171933707f3Ssthen {
2172933707f3Ssthen 	/* since its a per-worker structure no locks needed */
2173933707f3Ssthen 	struct iter_forwards* fwds = worker->env.fwds;
2174933707f3Ssthen 	struct iter_forward_zone* z;
2175e10d3884Sbrad 	struct trust_anchor* a;
2176e10d3884Sbrad 	int insecure;
2177933707f3Ssthen 	RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
2178933707f3Ssthen 		if(!z->dp) continue; /* skip empty marker for stub */
2179e10d3884Sbrad 
2180e10d3884Sbrad 		/* see if it is insecure */
2181e10d3884Sbrad 		insecure = 0;
2182e10d3884Sbrad 		if(worker->env.anchors &&
2183e10d3884Sbrad 			(a=anchor_find(worker->env.anchors, z->name,
2184e10d3884Sbrad 			z->namelabs, z->namelen,  z->dclass))) {
2185e10d3884Sbrad 			if(!a->keylist && !a->numDS && !a->numDNSKEY)
2186e10d3884Sbrad 				insecure = 1;
2187e10d3884Sbrad 			lock_basic_unlock(&a->lock);
2188e10d3884Sbrad 		}
2189e10d3884Sbrad 
2190e10d3884Sbrad 		if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"),
2191e10d3884Sbrad 			z->name, z->dclass, z->dp))
2192933707f3Ssthen 			return;
2193933707f3Ssthen 	}
2194933707f3Ssthen }
2195933707f3Ssthen 
2196933707f3Ssthen /** do the list_stubs command */
2197933707f3Ssthen static void
2198933707f3Ssthen do_list_stubs(SSL* ssl, struct worker* worker)
2199933707f3Ssthen {
2200933707f3Ssthen 	struct iter_hints_stub* z;
2201e10d3884Sbrad 	struct trust_anchor* a;
2202e10d3884Sbrad 	int insecure;
2203e10d3884Sbrad 	char str[32];
2204163a4143Ssthen 	RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
2205e10d3884Sbrad 
2206e10d3884Sbrad 		/* see if it is insecure */
2207e10d3884Sbrad 		insecure = 0;
2208e10d3884Sbrad 		if(worker->env.anchors &&
2209e10d3884Sbrad 			(a=anchor_find(worker->env.anchors, z->node.name,
2210e10d3884Sbrad 			z->node.labs, z->node.len,  z->node.dclass))) {
2211e10d3884Sbrad 			if(!a->keylist && !a->numDS && !a->numDNSKEY)
2212e10d3884Sbrad 				insecure = 1;
2213e10d3884Sbrad 			lock_basic_unlock(&a->lock);
2214e10d3884Sbrad 		}
2215e10d3884Sbrad 
2216e10d3884Sbrad 		snprintf(str, sizeof(str), "stub %sprime%s",
2217e10d3884Sbrad 			(z->noprime?"no":""), (insecure?" +i":""));
2218e10d3884Sbrad 		if(!ssl_print_name_dp(ssl, str, z->node.name,
2219933707f3Ssthen 			z->node.dclass, z->dp))
2220933707f3Ssthen 			return;
2221933707f3Ssthen 	}
2222933707f3Ssthen }
2223933707f3Ssthen 
2224933707f3Ssthen /** do the list_local_zones command */
2225933707f3Ssthen static void
2226933707f3Ssthen do_list_local_zones(SSL* ssl, struct worker* worker)
2227933707f3Ssthen {
2228933707f3Ssthen 	struct local_zones* zones = worker->daemon->local_zones;
2229933707f3Ssthen 	struct local_zone* z;
2230933707f3Ssthen 	char buf[257];
22310b68ff31Ssthen 	lock_rw_rdlock(&zones->lock);
2232933707f3Ssthen 	RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2233933707f3Ssthen 		lock_rw_rdlock(&z->lock);
2234933707f3Ssthen 		dname_str(z->name, buf);
2235e10d3884Sbrad 		if(!ssl_printf(ssl, "%s %s\n", buf,
2236e10d3884Sbrad 			local_zone_type2str(z->type))) {
2237e10d3884Sbrad 			/* failure to print */
2238e10d3884Sbrad 			lock_rw_unlock(&z->lock);
2239e10d3884Sbrad 			lock_rw_unlock(&zones->lock);
2240e10d3884Sbrad 			return;
2241e10d3884Sbrad 		}
2242933707f3Ssthen 		lock_rw_unlock(&z->lock);
2243933707f3Ssthen 	}
22440b68ff31Ssthen 	lock_rw_unlock(&zones->lock);
2245933707f3Ssthen }
2246933707f3Ssthen 
2247933707f3Ssthen /** do the list_local_data command */
2248933707f3Ssthen static void
2249933707f3Ssthen do_list_local_data(SSL* ssl, struct worker* worker)
2250933707f3Ssthen {
2251933707f3Ssthen 	struct local_zones* zones = worker->daemon->local_zones;
2252933707f3Ssthen 	struct local_zone* z;
2253933707f3Ssthen 	struct local_data* d;
2254933707f3Ssthen 	struct local_rrset* p;
22550b68ff31Ssthen 	char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
22560b68ff31Ssthen 	size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
22570b68ff31Ssthen 	lock_rw_rdlock(&zones->lock);
2258933707f3Ssthen 	RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2259933707f3Ssthen 		lock_rw_rdlock(&z->lock);
2260933707f3Ssthen 		RBTREE_FOR(d, struct local_data*, &z->data) {
2261933707f3Ssthen 			for(p = d->rrsets; p; p = p->next) {
22620b68ff31Ssthen 				struct packed_rrset_data* d =
22630b68ff31Ssthen 					(struct packed_rrset_data*)p->rrset->entry.data;
22640b68ff31Ssthen 				size_t i;
22650b68ff31Ssthen 				for(i=0; i<d->count + d->rrsig_count; i++) {
22660b68ff31Ssthen 					if(!packed_rr_to_string(p->rrset, i,
22670b68ff31Ssthen 						0, s, slen)) {
22680b68ff31Ssthen 						if(!ssl_printf(ssl, "BADRR\n"))
22690b68ff31Ssthen 							return;
22700b68ff31Ssthen 					}
22710b68ff31Ssthen 				        if(!ssl_printf(ssl, "%s\n", s))
22720b68ff31Ssthen 						return;
22730b68ff31Ssthen 				}
2274933707f3Ssthen 			}
2275933707f3Ssthen 		}
2276933707f3Ssthen 		lock_rw_unlock(&z->lock);
2277933707f3Ssthen 	}
22780b68ff31Ssthen 	lock_rw_unlock(&zones->lock);
2279933707f3Ssthen }
2280933707f3Ssthen 
2281*a58bff56Ssthen /** struct for user arg ratelimit list */
2282*a58bff56Ssthen struct ratelimit_list_arg {
2283*a58bff56Ssthen 	/** the infra cache */
2284*a58bff56Ssthen 	struct infra_cache* infra;
2285*a58bff56Ssthen 	/** the SSL to print to */
2286*a58bff56Ssthen 	SSL* ssl;
2287*a58bff56Ssthen 	/** all or only ratelimited */
2288*a58bff56Ssthen 	int all;
2289*a58bff56Ssthen 	/** current time */
2290*a58bff56Ssthen 	time_t now;
2291*a58bff56Ssthen };
2292*a58bff56Ssthen 
2293*a58bff56Ssthen /** list items in the ratelimit table */
2294*a58bff56Ssthen static void
2295*a58bff56Ssthen rate_list(struct lruhash_entry* e, void* arg)
2296*a58bff56Ssthen {
2297*a58bff56Ssthen 	struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg;
2298*a58bff56Ssthen 	struct rate_key* k = (struct rate_key*)e->key;
2299*a58bff56Ssthen 	struct rate_data* d = (struct rate_data*)e->data;
2300*a58bff56Ssthen 	char buf[257];
2301*a58bff56Ssthen 	int lim = infra_find_ratelimit(a->infra, k->name, k->namelen);
2302*a58bff56Ssthen 	int max = infra_rate_max(d, a->now);
2303*a58bff56Ssthen 	if(a->all == 0) {
2304*a58bff56Ssthen 		if(max < lim)
2305*a58bff56Ssthen 			return;
2306*a58bff56Ssthen 	}
2307*a58bff56Ssthen 	dname_str(k->name, buf);
2308*a58bff56Ssthen 	ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim);
2309*a58bff56Ssthen }
2310*a58bff56Ssthen 
2311*a58bff56Ssthen /** do the ratelimit_list command */
2312*a58bff56Ssthen static void
2313*a58bff56Ssthen do_ratelimit_list(SSL* ssl, struct worker* worker, char* arg)
2314*a58bff56Ssthen {
2315*a58bff56Ssthen 	struct ratelimit_list_arg a;
2316*a58bff56Ssthen 	a.all = 0;
2317*a58bff56Ssthen 	a.infra = worker->env.infra_cache;
2318*a58bff56Ssthen 	a.now = *worker->env.now;
2319*a58bff56Ssthen 	a.ssl = ssl;
2320*a58bff56Ssthen 	arg = skipwhite(arg);
2321*a58bff56Ssthen 	if(strcmp(arg, "+a") == 0)
2322*a58bff56Ssthen 		a.all = 1;
2323*a58bff56Ssthen 	if(a.infra->domain_rates==NULL ||
2324*a58bff56Ssthen 		(a.all == 0 && infra_dp_ratelimit == 0))
2325*a58bff56Ssthen 		return;
2326*a58bff56Ssthen 	slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a);
2327*a58bff56Ssthen }
2328*a58bff56Ssthen 
2329933707f3Ssthen /** tell other processes to execute the command */
2330933707f3Ssthen static void
2331933707f3Ssthen distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd)
2332933707f3Ssthen {
2333933707f3Ssthen 	int i;
2334933707f3Ssthen 	if(!cmd || !ssl)
2335933707f3Ssthen 		return;
2336933707f3Ssthen 	/* skip i=0 which is me */
2337933707f3Ssthen 	for(i=1; i<rc->worker->daemon->num; i++) {
2338933707f3Ssthen 		worker_send_cmd(rc->worker->daemon->workers[i],
2339933707f3Ssthen 			worker_cmd_remote);
2340933707f3Ssthen 		if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2341933707f3Ssthen 			(uint8_t*)cmd, strlen(cmd)+1, 0)) {
2342933707f3Ssthen 			ssl_printf(ssl, "error could not distribute cmd\n");
2343933707f3Ssthen 			return;
2344933707f3Ssthen 		}
2345933707f3Ssthen 	}
2346933707f3Ssthen }
2347933707f3Ssthen 
2348933707f3Ssthen /** check for name with end-of-string, space or tab after it */
2349933707f3Ssthen static int
2350933707f3Ssthen cmdcmp(char* p, const char* cmd, size_t len)
2351933707f3Ssthen {
2352933707f3Ssthen 	return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2353933707f3Ssthen }
2354933707f3Ssthen 
2355933707f3Ssthen /** execute a remote control command */
2356933707f3Ssthen static void
2357933707f3Ssthen execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd,
2358933707f3Ssthen 	struct worker* worker)
2359933707f3Ssthen {
2360933707f3Ssthen 	char* p = skipwhite(cmd);
2361933707f3Ssthen 	/* compare command */
2362933707f3Ssthen 	if(cmdcmp(p, "stop", 4)) {
2363933707f3Ssthen 		do_stop(ssl, rc);
2364933707f3Ssthen 		return;
2365933707f3Ssthen 	} else if(cmdcmp(p, "reload", 6)) {
2366933707f3Ssthen 		do_reload(ssl, rc);
2367933707f3Ssthen 		return;
2368933707f3Ssthen 	} else if(cmdcmp(p, "stats_noreset", 13)) {
2369933707f3Ssthen 		do_stats(ssl, rc, 0);
2370933707f3Ssthen 		return;
2371933707f3Ssthen 	} else if(cmdcmp(p, "stats", 5)) {
2372933707f3Ssthen 		do_stats(ssl, rc, 1);
2373933707f3Ssthen 		return;
2374933707f3Ssthen 	} else if(cmdcmp(p, "status", 6)) {
2375933707f3Ssthen 		do_status(ssl, worker);
2376933707f3Ssthen 		return;
2377933707f3Ssthen 	} else if(cmdcmp(p, "dump_cache", 10)) {
2378933707f3Ssthen 		(void)dump_cache(ssl, worker);
2379933707f3Ssthen 		return;
2380933707f3Ssthen 	} else if(cmdcmp(p, "load_cache", 10)) {
2381933707f3Ssthen 		if(load_cache(ssl, worker)) send_ok(ssl);
2382933707f3Ssthen 		return;
2383933707f3Ssthen 	} else if(cmdcmp(p, "list_forwards", 13)) {
2384933707f3Ssthen 		do_list_forwards(ssl, worker);
2385933707f3Ssthen 		return;
2386933707f3Ssthen 	} else if(cmdcmp(p, "list_stubs", 10)) {
2387933707f3Ssthen 		do_list_stubs(ssl, worker);
2388933707f3Ssthen 		return;
2389*a58bff56Ssthen 	} else if(cmdcmp(p, "list_insecure", 13)) {
2390*a58bff56Ssthen 		do_insecure_list(ssl, worker);
2391*a58bff56Ssthen 		return;
2392933707f3Ssthen 	} else if(cmdcmp(p, "list_local_zones", 16)) {
2393933707f3Ssthen 		do_list_local_zones(ssl, worker);
2394933707f3Ssthen 		return;
2395933707f3Ssthen 	} else if(cmdcmp(p, "list_local_data", 15)) {
2396933707f3Ssthen 		do_list_local_data(ssl, worker);
2397933707f3Ssthen 		return;
2398*a58bff56Ssthen 	} else if(cmdcmp(p, "ratelimit_list", 14)) {
2399*a58bff56Ssthen 		do_ratelimit_list(ssl, worker, p+14);
2400*a58bff56Ssthen 		return;
2401163a4143Ssthen 	} else if(cmdcmp(p, "stub_add", 8)) {
2402163a4143Ssthen 		/* must always distribute this cmd */
2403163a4143Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2404163a4143Ssthen 		do_stub_add(ssl, worker, skipwhite(p+8));
2405163a4143Ssthen 		return;
2406163a4143Ssthen 	} else if(cmdcmp(p, "stub_remove", 11)) {
2407163a4143Ssthen 		/* must always distribute this cmd */
2408163a4143Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2409163a4143Ssthen 		do_stub_remove(ssl, worker, skipwhite(p+11));
2410163a4143Ssthen 		return;
2411163a4143Ssthen 	} else if(cmdcmp(p, "forward_add", 11)) {
2412163a4143Ssthen 		/* must always distribute this cmd */
2413163a4143Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2414163a4143Ssthen 		do_forward_add(ssl, worker, skipwhite(p+11));
2415163a4143Ssthen 		return;
2416163a4143Ssthen 	} else if(cmdcmp(p, "forward_remove", 14)) {
2417163a4143Ssthen 		/* must always distribute this cmd */
2418163a4143Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2419163a4143Ssthen 		do_forward_remove(ssl, worker, skipwhite(p+14));
2420163a4143Ssthen 		return;
2421e9c7b4efSsthen 	} else if(cmdcmp(p, "insecure_add", 12)) {
2422e9c7b4efSsthen 		/* must always distribute this cmd */
2423e9c7b4efSsthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2424e9c7b4efSsthen 		do_insecure_add(ssl, worker, skipwhite(p+12));
2425e9c7b4efSsthen 		return;
2426e9c7b4efSsthen 	} else if(cmdcmp(p, "insecure_remove", 15)) {
2427e9c7b4efSsthen 		/* must always distribute this cmd */
2428e9c7b4efSsthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2429e9c7b4efSsthen 		do_insecure_remove(ssl, worker, skipwhite(p+15));
2430e9c7b4efSsthen 		return;
2431933707f3Ssthen 	} else if(cmdcmp(p, "forward", 7)) {
2432933707f3Ssthen 		/* must always distribute this cmd */
2433933707f3Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2434933707f3Ssthen 		do_forward(ssl, worker, skipwhite(p+7));
2435933707f3Ssthen 		return;
2436933707f3Ssthen 	} else if(cmdcmp(p, "flush_stats", 11)) {
2437933707f3Ssthen 		/* must always distribute this cmd */
2438933707f3Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2439933707f3Ssthen 		do_flush_stats(ssl, worker);
2440933707f3Ssthen 		return;
2441933707f3Ssthen 	} else if(cmdcmp(p, "flush_requestlist", 17)) {
2442933707f3Ssthen 		/* must always distribute this cmd */
2443933707f3Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2444933707f3Ssthen 		do_flush_requestlist(ssl, worker);
2445933707f3Ssthen 		return;
2446933707f3Ssthen 	} else if(cmdcmp(p, "lookup", 6)) {
2447933707f3Ssthen 		do_lookup(ssl, worker, skipwhite(p+6));
2448933707f3Ssthen 		return;
2449933707f3Ssthen 	}
2450933707f3Ssthen 
2451933707f3Ssthen #ifdef THREADS_DISABLED
2452933707f3Ssthen 	/* other processes must execute the command as well */
2453933707f3Ssthen 	/* commands that should not be distributed, returned above. */
2454933707f3Ssthen 	if(rc) { /* only if this thread is the master (rc) thread */
2455933707f3Ssthen 		/* done before the code below, which may split the string */
2456933707f3Ssthen 		distribute_cmd(rc, ssl, cmd);
2457933707f3Ssthen 	}
2458933707f3Ssthen #endif
2459933707f3Ssthen 	if(cmdcmp(p, "verbosity", 9)) {
2460933707f3Ssthen 		do_verbosity(ssl, skipwhite(p+9));
2461933707f3Ssthen 	} else if(cmdcmp(p, "local_zone_remove", 17)) {
2462933707f3Ssthen 		do_zone_remove(ssl, worker, skipwhite(p+17));
2463933707f3Ssthen 	} else if(cmdcmp(p, "local_zone", 10)) {
2464933707f3Ssthen 		do_zone_add(ssl, worker, skipwhite(p+10));
2465933707f3Ssthen 	} else if(cmdcmp(p, "local_data_remove", 17)) {
2466933707f3Ssthen 		do_data_remove(ssl, worker, skipwhite(p+17));
2467933707f3Ssthen 	} else if(cmdcmp(p, "local_data", 10)) {
2468933707f3Ssthen 		do_data_add(ssl, worker, skipwhite(p+10));
2469933707f3Ssthen 	} else if(cmdcmp(p, "flush_zone", 10)) {
2470933707f3Ssthen 		do_flush_zone(ssl, worker, skipwhite(p+10));
2471933707f3Ssthen 	} else if(cmdcmp(p, "flush_type", 10)) {
2472933707f3Ssthen 		do_flush_type(ssl, worker, skipwhite(p+10));
2473933707f3Ssthen 	} else if(cmdcmp(p, "flush_infra", 11)) {
2474933707f3Ssthen 		do_flush_infra(ssl, worker, skipwhite(p+11));
2475933707f3Ssthen 	} else if(cmdcmp(p, "flush", 5)) {
2476933707f3Ssthen 		do_flush_name(ssl, worker, skipwhite(p+5));
2477933707f3Ssthen 	} else if(cmdcmp(p, "dump_requestlist", 16)) {
2478933707f3Ssthen 		do_dump_requestlist(ssl, worker);
2479933707f3Ssthen 	} else if(cmdcmp(p, "dump_infra", 10)) {
2480933707f3Ssthen 		do_dump_infra(ssl, worker);
2481933707f3Ssthen 	} else if(cmdcmp(p, "log_reopen", 10)) {
2482933707f3Ssthen 		do_log_reopen(ssl, worker);
2483933707f3Ssthen 	} else if(cmdcmp(p, "set_option", 10)) {
2484933707f3Ssthen 		do_set_option(ssl, worker, skipwhite(p+10));
2485933707f3Ssthen 	} else if(cmdcmp(p, "get_option", 10)) {
2486933707f3Ssthen 		do_get_option(ssl, worker, skipwhite(p+10));
2487cebdf579Ssthen 	} else if(cmdcmp(p, "flush_bogus", 11)) {
2488cebdf579Ssthen 		do_flush_bogus(ssl, worker);
2489e10d3884Sbrad 	} else if(cmdcmp(p, "flush_negative", 14)) {
2490e10d3884Sbrad 		do_flush_negative(ssl, worker);
2491933707f3Ssthen 	} else {
2492933707f3Ssthen 		(void)ssl_printf(ssl, "error unknown command '%s'\n", p);
2493933707f3Ssthen 	}
2494933707f3Ssthen }
2495933707f3Ssthen 
2496933707f3Ssthen void
2497933707f3Ssthen daemon_remote_exec(struct worker* worker)
2498933707f3Ssthen {
2499933707f3Ssthen 	/* read the cmd string */
2500933707f3Ssthen 	uint8_t* msg = NULL;
2501933707f3Ssthen 	uint32_t len = 0;
2502933707f3Ssthen 	if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
2503933707f3Ssthen 		log_err("daemon_remote_exec: tube_read_msg failed");
2504933707f3Ssthen 		return;
2505933707f3Ssthen 	}
2506933707f3Ssthen 	verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
2507933707f3Ssthen 	execute_cmd(NULL, NULL, (char*)msg, worker);
2508933707f3Ssthen 	free(msg);
2509933707f3Ssthen }
2510933707f3Ssthen 
2511933707f3Ssthen /** handle remote control request */
2512933707f3Ssthen static void
2513933707f3Ssthen handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl)
2514933707f3Ssthen {
2515933707f3Ssthen 	int r;
2516933707f3Ssthen 	char pre[10];
2517933707f3Ssthen 	char magic[7];
2518933707f3Ssthen 	char buf[1024];
2519933707f3Ssthen #ifdef USE_WINSOCK
2520933707f3Ssthen 	/* makes it possible to set the socket blocking again. */
2521933707f3Ssthen 	/* basically removes it from winsock_event ... */
2522933707f3Ssthen 	WSAEventSelect(s->c->fd, NULL, 0);
2523933707f3Ssthen #endif
2524933707f3Ssthen 	fd_set_block(s->c->fd);
2525933707f3Ssthen 
2526933707f3Ssthen 	/* try to read magic UBCT[version]_space_ string */
2527933707f3Ssthen 	ERR_clear_error();
2528933707f3Ssthen 	if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) {
2529933707f3Ssthen 		if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN)
2530933707f3Ssthen 			return;
2531933707f3Ssthen 		log_crypto_err("could not SSL_read");
2532933707f3Ssthen 		return;
2533933707f3Ssthen 	}
2534933707f3Ssthen 	magic[6] = 0;
2535933707f3Ssthen 	if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
2536933707f3Ssthen 		verbose(VERB_QUERY, "control connection has bad magic string");
2537933707f3Ssthen 		/* probably wrong tool connected, ignore it completely */
2538933707f3Ssthen 		return;
2539933707f3Ssthen 	}
2540933707f3Ssthen 
2541933707f3Ssthen 	/* read the command line */
2542933707f3Ssthen 	if(!ssl_read_line(ssl, buf, sizeof(buf))) {
2543933707f3Ssthen 		return;
2544933707f3Ssthen 	}
2545933707f3Ssthen 	snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
2546933707f3Ssthen 	if(strcmp(magic, pre) != 0) {
2547933707f3Ssthen 		verbose(VERB_QUERY, "control connection had bad "
2548933707f3Ssthen 			"version %s, cmd: %s", magic, buf);
2549933707f3Ssthen 		ssl_printf(ssl, "error version mismatch\n");
2550933707f3Ssthen 		return;
2551933707f3Ssthen 	}
2552933707f3Ssthen 	verbose(VERB_DETAIL, "control cmd: %s", buf);
2553933707f3Ssthen 
2554933707f3Ssthen 	/* figure out what to do */
2555933707f3Ssthen 	execute_cmd(rc, ssl, buf, rc->worker);
2556933707f3Ssthen }
2557933707f3Ssthen 
2558933707f3Ssthen int remote_control_callback(struct comm_point* c, void* arg, int err,
2559933707f3Ssthen 	struct comm_reply* ATTR_UNUSED(rep))
2560933707f3Ssthen {
2561933707f3Ssthen 	struct rc_state* s = (struct rc_state*)arg;
2562933707f3Ssthen 	struct daemon_remote* rc = s->rc;
2563933707f3Ssthen 	int r;
2564933707f3Ssthen 	if(err != NETEVENT_NOERROR) {
2565933707f3Ssthen 		if(err==NETEVENT_TIMEOUT)
2566933707f3Ssthen 			log_err("remote control timed out");
2567933707f3Ssthen 		clean_point(rc, s);
2568933707f3Ssthen 		return 0;
2569933707f3Ssthen 	}
2570933707f3Ssthen 	/* (continue to) setup the SSL connection */
2571933707f3Ssthen 	ERR_clear_error();
2572933707f3Ssthen 	r = SSL_do_handshake(s->ssl);
2573933707f3Ssthen 	if(r != 1) {
2574933707f3Ssthen 		int r2 = SSL_get_error(s->ssl, r);
2575933707f3Ssthen 		if(r2 == SSL_ERROR_WANT_READ) {
2576933707f3Ssthen 			if(s->shake_state == rc_hs_read) {
2577933707f3Ssthen 				/* try again later */
2578933707f3Ssthen 				return 0;
2579933707f3Ssthen 			}
2580933707f3Ssthen 			s->shake_state = rc_hs_read;
2581933707f3Ssthen 			comm_point_listen_for_rw(c, 1, 0);
2582933707f3Ssthen 			return 0;
2583933707f3Ssthen 		} else if(r2 == SSL_ERROR_WANT_WRITE) {
2584933707f3Ssthen 			if(s->shake_state == rc_hs_write) {
2585933707f3Ssthen 				/* try again later */
2586933707f3Ssthen 				return 0;
2587933707f3Ssthen 			}
2588933707f3Ssthen 			s->shake_state = rc_hs_write;
2589933707f3Ssthen 			comm_point_listen_for_rw(c, 0, 1);
2590933707f3Ssthen 			return 0;
2591933707f3Ssthen 		} else {
2592933707f3Ssthen 			if(r == 0)
2593933707f3Ssthen 				log_err("remote control connection closed prematurely");
2594933707f3Ssthen 			log_addr(1, "failed connection from",
2595933707f3Ssthen 				&s->c->repinfo.addr, s->c->repinfo.addrlen);
2596933707f3Ssthen 			log_crypto_err("remote control failed ssl");
2597933707f3Ssthen 			clean_point(rc, s);
2598933707f3Ssthen 			return 0;
2599933707f3Ssthen 		}
2600933707f3Ssthen 	}
2601933707f3Ssthen 	s->shake_state = rc_none;
2602933707f3Ssthen 
2603933707f3Ssthen 	/* once handshake has completed, check authentication */
260431f127bbSsthen 	if (!rc->use_cert) {
260531f127bbSsthen 		verbose(VERB_ALGO, "unauthenticated remote control connection");
260631f127bbSsthen 	} else if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
2607933707f3Ssthen 		X509* x = SSL_get_peer_certificate(s->ssl);
2608933707f3Ssthen 		if(!x) {
2609933707f3Ssthen 			verbose(VERB_DETAIL, "remote control connection "
2610933707f3Ssthen 				"provided no client certificate");
2611933707f3Ssthen 			clean_point(rc, s);
2612933707f3Ssthen 			return 0;
2613933707f3Ssthen 		}
2614933707f3Ssthen 		verbose(VERB_ALGO, "remote control connection authenticated");
2615933707f3Ssthen 		X509_free(x);
2616933707f3Ssthen 	} else {
2617933707f3Ssthen 		verbose(VERB_DETAIL, "remote control connection failed to "
2618933707f3Ssthen 			"authenticate with client certificate");
2619933707f3Ssthen 		clean_point(rc, s);
2620933707f3Ssthen 		return 0;
2621933707f3Ssthen 	}
2622933707f3Ssthen 
2623933707f3Ssthen 	/* if OK start to actually handle the request */
2624933707f3Ssthen 	handle_req(rc, s, s->ssl);
2625933707f3Ssthen 
2626933707f3Ssthen 	verbose(VERB_ALGO, "remote control operation completed");
2627933707f3Ssthen 	clean_point(rc, s);
2628933707f3Ssthen 	return 0;
2629933707f3Ssthen }
2630