xref: /openbsd/usr.sbin/unbound/daemon/remote.c (revision a961b961)
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"
81a58bff56Ssthen #include "sldns/str2wire.h"
82a58bff56Ssthen #include "sldns/parseutil.h"
83a58bff56Ssthen #include "sldns/wire2str.h"
84a58bff56Ssthen #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
143a58bff56Ssthen  * the command : "openssl dhparam -dsaparam -C 1024"
144a58bff56Ssthen  * (some openssl versions reject DH that is 'too small', eg. 512).
14531f127bbSsthen  */
14631f127bbSsthen #ifndef S_SPLINT_S
147a58bff56Ssthen DH *get_dh1024()
14831f127bbSsthen {
149a58bff56Ssthen 	static unsigned char dh1024_p[]={
150a58bff56Ssthen 		0xB3,0x67,0x2E,0x3B,0x68,0xC5,0xDA,0x58,0x46,0xD6,0x2B,0xD3,
151a58bff56Ssthen 		0x41,0x78,0x97,0xE4,0xE1,0x61,0x71,0x68,0xE6,0x0F,0x1D,0x78,
152a58bff56Ssthen 		0x05,0xAA,0xF0,0xFF,0x30,0xDF,0xAC,0x49,0x7F,0xE0,0x90,0xFE,
153a58bff56Ssthen 		0xB9,0x56,0x4E,0x3F,0xE2,0x98,0x8A,0xED,0xF5,0x28,0x39,0xEF,
154a58bff56Ssthen 		0x2E,0xA6,0xB7,0x67,0xB2,0x43,0xE4,0x53,0xF8,0xEB,0x2C,0x1F,
155a58bff56Ssthen 		0x06,0x77,0x3A,0x6F,0x62,0x98,0xC1,0x3B,0xF7,0xBA,0x4D,0x93,
156a58bff56Ssthen 		0xF7,0xEB,0x5A,0xAD,0xC5,0x5F,0xF0,0xB7,0x24,0x35,0x81,0xF7,
157a58bff56Ssthen 		0x7F,0x1F,0x24,0xC0,0xDF,0xD3,0xD8,0x40,0x72,0x7E,0xF3,0x19,
158a58bff56Ssthen 		0x2B,0x26,0x27,0xF4,0xB6,0xB3,0xD4,0x7D,0x08,0x23,0xBE,0x68,
159a58bff56Ssthen 		0x2B,0xCA,0xB4,0x46,0xA8,0x9E,0xDD,0x6C,0x3D,0x75,0xA6,0x48,
160a58bff56Ssthen 		0xF7,0x44,0x43,0xBF,0x91,0xC2,0xB4,0x49,
16131f127bbSsthen 		};
162a58bff56Ssthen 	static unsigned char dh1024_g[]={
163a58bff56Ssthen 		0x5F,0x37,0xB5,0x80,0x4D,0xB4,0xC4,0xB2,0x37,0x12,0xD5,0x2F,
164a58bff56Ssthen 		0x56,0x81,0xB0,0xDF,0x3D,0x27,0xA2,0x54,0xE7,0x14,0x65,0x2D,
165a58bff56Ssthen 		0x72,0xA8,0x97,0xE0,0xA9,0x4A,0x09,0x5E,0x89,0xBE,0x34,0x9A,
166a58bff56Ssthen 		0x90,0x98,0xC1,0xE8,0xBB,0x01,0x2B,0xC2,0x74,0x74,0x90,0x59,
167a58bff56Ssthen 		0x0B,0x72,0x62,0x5C,0xFD,0x49,0x63,0x4B,0x38,0x91,0xF1,0x7F,
168a58bff56Ssthen 		0x13,0x25,0xEB,0x52,0x50,0x47,0xA2,0x8C,0x32,0x28,0x42,0xAC,
169a58bff56Ssthen 		0xBD,0x7A,0xCC,0x58,0xBE,0x36,0xDA,0x6A,0x24,0x06,0xC7,0xF1,
170a58bff56Ssthen 		0xDA,0x8D,0x8A,0x3B,0x03,0xFA,0x6F,0x25,0xE5,0x20,0xA7,0xD6,
171a58bff56Ssthen 		0x6F,0x74,0x61,0x53,0x14,0x81,0x29,0x04,0xB5,0x61,0x12,0x53,
172a58bff56Ssthen 		0xA3,0xD6,0x09,0x98,0x0C,0x8F,0x1C,0xBB,0xD7,0x1C,0x2C,0xEE,
173a58bff56Ssthen 		0x56,0x4B,0x74,0x8F,0x4A,0xF8,0xA9,0xD5,
17431f127bbSsthen 		};
17531f127bbSsthen 	DH *dh;
17631f127bbSsthen 
17731f127bbSsthen 	if ((dh=DH_new()) == NULL) return(NULL);
178a58bff56Ssthen 	dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
179a58bff56Ssthen 	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 */
2113c667526Sdoug 	if((SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)
2123c667526Sdoug 		!= SSL_OP_NO_SSLv2){
213933707f3Ssthen 		log_crypto_err("could not set SSL_OP_NO_SSLv2");
214933707f3Ssthen 		daemon_remote_delete(rc);
215933707f3Ssthen 		return NULL;
216933707f3Ssthen 	}
2173c667526Sdoug 	if((SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)
2183c667526Sdoug 		!= SSL_OP_NO_SSLv3){
219e10d3884Sbrad 		log_crypto_err("could not set SSL_OP_NO_SSLv3");
220e10d3884Sbrad 		daemon_remote_delete(rc);
221e10d3884Sbrad 		return NULL;
222e10d3884Sbrad 	}
22331f127bbSsthen 
22431f127bbSsthen 	if (cfg->remote_control_use_cert == 0) {
22531f127bbSsthen 		/* No certificates are requested */
22631f127bbSsthen 		if(!SSL_CTX_set_cipher_list(rc->ctx, "aNULL")) {
22731f127bbSsthen 			log_crypto_err("Failed to set aNULL cipher list");
22831f127bbSsthen 			return NULL;
22931f127bbSsthen 		}
23031f127bbSsthen 
23131f127bbSsthen 		/* Since we have no certificates and hence no source of
23231f127bbSsthen 		 * DH params, let's generate and set them
23331f127bbSsthen 		 */
234a58bff56Ssthen 		if(!SSL_CTX_set_tmp_dh(rc->ctx,get_dh1024())) {
23531f127bbSsthen 			log_crypto_err("Wanted to set DH param, but failed");
23631f127bbSsthen 			return NULL;
23731f127bbSsthen 		}
23831f127bbSsthen 		return rc;
23931f127bbSsthen 	}
24031f127bbSsthen 	rc->use_cert = 1;
241933707f3Ssthen 	s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
242933707f3Ssthen 	s_key = fname_after_chroot(cfg->server_key_file, cfg, 1);
243933707f3Ssthen 	if(!s_cert || !s_key) {
244933707f3Ssthen 		log_err("out of memory in remote control fname");
245933707f3Ssthen 		goto setup_error;
246933707f3Ssthen 	}
247933707f3Ssthen 	verbose(VERB_ALGO, "setup SSL certificates");
248*a961b961Ssthen 	if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) {
249933707f3Ssthen 		log_err("Error for server-cert-file: %s", s_cert);
250*a961b961Ssthen 		log_crypto_err("Error in SSL_CTX use_certificate_chain_file");
251933707f3Ssthen 		goto setup_error;
252933707f3Ssthen 	}
253933707f3Ssthen 	if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) {
254933707f3Ssthen 		log_err("Error for server-key-file: %s", s_key);
255933707f3Ssthen 		log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
256933707f3Ssthen 		goto setup_error;
257933707f3Ssthen 	}
258933707f3Ssthen 	if(!SSL_CTX_check_private_key(rc->ctx)) {
259933707f3Ssthen 		log_err("Error for server-key-file: %s", s_key);
260933707f3Ssthen 		log_crypto_err("Error in SSL_CTX check_private_key");
261933707f3Ssthen 		goto setup_error;
262933707f3Ssthen 	}
263*a961b961Ssthen #if HAVE_DECL_SSL_CTX_SET_ECDH_AUTO
264*a961b961Ssthen 	if(!SSL_CTX_set_ecdh_auto(rc->ctx,1)) {
265*a961b961Ssthen 		log_crypto_err("Error in SSL_CTX_ecdh_auto, not enabling ECDHE");
266*a961b961Ssthen 	}
267*a961b961Ssthen #elif defined(USE_ECDSA)
268*a961b961Ssthen 	if(1) {
269*a961b961Ssthen 		EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
270*a961b961Ssthen 		if (!ecdh) {
271*a961b961Ssthen 			log_crypto_err("could not find p256, not enabling ECDHE");
272*a961b961Ssthen 		} else {
273*a961b961Ssthen 			if (1 != SSL_CTX_set_tmp_ecdh (rc->ctx, ecdh)) {
274*a961b961Ssthen 				log_crypto_err("Error in SSL_CTX_set_tmp_ecdh, not enabling ECDHE");
275*a961b961Ssthen 			}
276*a961b961Ssthen 			EC_KEY_free (ecdh);
277*a961b961Ssthen 		}
278*a961b961Ssthen 	}
279*a961b961Ssthen #endif
280933707f3Ssthen 	if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) {
281933707f3Ssthen 		log_crypto_err("Error setting up SSL_CTX verify locations");
282933707f3Ssthen 	setup_error:
283933707f3Ssthen 		free(s_cert);
284933707f3Ssthen 		free(s_key);
285933707f3Ssthen 		daemon_remote_delete(rc);
286933707f3Ssthen 		return NULL;
287933707f3Ssthen 	}
288933707f3Ssthen 	SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert));
289933707f3Ssthen 	SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL);
290933707f3Ssthen 	free(s_cert);
291933707f3Ssthen 	free(s_key);
292933707f3Ssthen 
293933707f3Ssthen 	return rc;
294933707f3Ssthen }
295933707f3Ssthen 
296933707f3Ssthen void daemon_remote_clear(struct daemon_remote* rc)
297933707f3Ssthen {
298933707f3Ssthen 	struct rc_state* p, *np;
299933707f3Ssthen 	if(!rc) return;
300933707f3Ssthen 	/* but do not close the ports */
301933707f3Ssthen 	listen_list_delete(rc->accept_list);
302933707f3Ssthen 	rc->accept_list = NULL;
303933707f3Ssthen 	/* do close these sockets */
304933707f3Ssthen 	p = rc->busy_list;
305933707f3Ssthen 	while(p) {
306933707f3Ssthen 		np = p->next;
307933707f3Ssthen 		if(p->ssl)
308933707f3Ssthen 			SSL_free(p->ssl);
309933707f3Ssthen 		comm_point_delete(p->c);
310933707f3Ssthen 		free(p);
311933707f3Ssthen 		p = np;
312933707f3Ssthen 	}
313933707f3Ssthen 	rc->busy_list = NULL;
314933707f3Ssthen 	rc->active = 0;
315933707f3Ssthen 	rc->worker = NULL;
316933707f3Ssthen }
317933707f3Ssthen 
318933707f3Ssthen void daemon_remote_delete(struct daemon_remote* rc)
319933707f3Ssthen {
320933707f3Ssthen 	if(!rc) return;
321933707f3Ssthen 	daemon_remote_clear(rc);
322933707f3Ssthen 	if(rc->ctx) {
323933707f3Ssthen 		SSL_CTX_free(rc->ctx);
324933707f3Ssthen 	}
325933707f3Ssthen 	free(rc);
326933707f3Ssthen }
327933707f3Ssthen 
328933707f3Ssthen /**
329933707f3Ssthen  * Add and open a new control port
330933707f3Ssthen  * @param ip: ip str
331933707f3Ssthen  * @param nr: port nr
332933707f3Ssthen  * @param list: list head
333933707f3Ssthen  * @param noproto_is_err: if lack of protocol support is an error.
33431f127bbSsthen  * @param cfg: config with username for chown of unix-sockets.
335933707f3Ssthen  * @return false on failure.
336933707f3Ssthen  */
337933707f3Ssthen static int
33831f127bbSsthen add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err,
33931f127bbSsthen 	struct config_file* cfg)
340933707f3Ssthen {
341933707f3Ssthen 	struct addrinfo hints;
342933707f3Ssthen 	struct addrinfo* res;
343933707f3Ssthen 	struct listen_port* n;
344933707f3Ssthen 	int noproto;
345933707f3Ssthen 	int fd, r;
346933707f3Ssthen 	char port[15];
347933707f3Ssthen 	snprintf(port, sizeof(port), "%d", nr);
348933707f3Ssthen 	port[sizeof(port)-1]=0;
349933707f3Ssthen 	memset(&hints, 0, sizeof(hints));
35031f127bbSsthen 
35131f127bbSsthen 	if(ip[0] == '/') {
35231f127bbSsthen 		/* This looks like a local socket */
35331f127bbSsthen 		fd = create_local_accept_sock(ip, &noproto);
35431f127bbSsthen 		/*
35531f127bbSsthen 		 * Change socket ownership and permissions so users other
35631f127bbSsthen 		 * than root can access it provided they are in the same
35731f127bbSsthen 		 * group as the user we run as.
35831f127bbSsthen 		 */
35931f127bbSsthen 		if(fd != -1) {
36031f127bbSsthen #ifdef HAVE_CHOWN
361a58bff56Ssthen 			if (cfg->username && cfg->username[0] &&
362a58bff56Ssthen 				cfg_uid != (uid_t)-1)
36347dfde74Sflorian 				chown(ip, cfg_uid, cfg_gid);
36431f127bbSsthen 			chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
36531f127bbSsthen #else
36631f127bbSsthen 			(void)cfg;
36731f127bbSsthen #endif
36831f127bbSsthen 		}
36931f127bbSsthen 	} else {
370933707f3Ssthen 		hints.ai_socktype = SOCK_STREAM;
371933707f3Ssthen 		hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
372933707f3Ssthen 		if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
373933707f3Ssthen #ifdef USE_WINSOCK
374933707f3Ssthen 			if(!noproto_is_err && r == EAI_NONAME) {
375933707f3Ssthen 				/* tried to lookup the address as name */
376933707f3Ssthen 				return 1; /* return success, but do nothing */
377933707f3Ssthen 			}
378933707f3Ssthen #endif /* USE_WINSOCK */
379933707f3Ssthen 			log_err("control interface %s:%s getaddrinfo: %s %s",
380933707f3Ssthen 				ip?ip:"default", port, gai_strerror(r),
381933707f3Ssthen #ifdef EAI_SYSTEM
382933707f3Ssthen 				r==EAI_SYSTEM?(char*)strerror(errno):""
383933707f3Ssthen #else
384933707f3Ssthen 				""
385933707f3Ssthen #endif
386933707f3Ssthen 			);
387933707f3Ssthen 			return 0;
388933707f3Ssthen 		}
389933707f3Ssthen 
390933707f3Ssthen 		/* open fd */
391a58bff56Ssthen 		fd = create_tcp_accept_sock(res, 1, &noproto, 0,
392a58bff56Ssthen 			cfg->ip_transparent);
393933707f3Ssthen 		freeaddrinfo(res);
39431f127bbSsthen 	}
39531f127bbSsthen 
396933707f3Ssthen 	if(fd == -1 && noproto) {
397933707f3Ssthen 		if(!noproto_is_err)
398933707f3Ssthen 			return 1; /* return success, but do nothing */
399933707f3Ssthen 		log_err("cannot open control interface %s %d : "
400933707f3Ssthen 			"protocol not supported", ip, nr);
401933707f3Ssthen 		return 0;
402933707f3Ssthen 	}
403933707f3Ssthen 	if(fd == -1) {
404933707f3Ssthen 		log_err("cannot open control interface %s %d", ip, nr);
405933707f3Ssthen 		return 0;
406933707f3Ssthen 	}
407933707f3Ssthen 
408933707f3Ssthen 	/* alloc */
409933707f3Ssthen 	n = (struct listen_port*)calloc(1, sizeof(*n));
410933707f3Ssthen 	if(!n) {
411933707f3Ssthen #ifndef USE_WINSOCK
412933707f3Ssthen 		close(fd);
413933707f3Ssthen #else
414933707f3Ssthen 		closesocket(fd);
415933707f3Ssthen #endif
416933707f3Ssthen 		log_err("out of memory");
417933707f3Ssthen 		return 0;
418933707f3Ssthen 	}
419933707f3Ssthen 	n->next = *list;
420933707f3Ssthen 	*list = n;
421933707f3Ssthen 	n->fd = fd;
422933707f3Ssthen 	return 1;
423933707f3Ssthen }
424933707f3Ssthen 
425933707f3Ssthen struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
426933707f3Ssthen {
427933707f3Ssthen 	struct listen_port* l = NULL;
428933707f3Ssthen 	log_assert(cfg->remote_control_enable && cfg->control_port);
429933707f3Ssthen 	if(cfg->control_ifs) {
430933707f3Ssthen 		struct config_strlist* p;
431933707f3Ssthen 		for(p = cfg->control_ifs; p; p = p->next) {
43231f127bbSsthen 			if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) {
433933707f3Ssthen 				listening_ports_free(l);
434933707f3Ssthen 				return NULL;
435933707f3Ssthen 			}
436933707f3Ssthen 		}
437933707f3Ssthen 	} else {
438933707f3Ssthen 		/* defaults */
439933707f3Ssthen 		if(cfg->do_ip6 &&
44031f127bbSsthen 			!add_open("::1", cfg->control_port, &l, 0, cfg)) {
441933707f3Ssthen 			listening_ports_free(l);
442933707f3Ssthen 			return NULL;
443933707f3Ssthen 		}
444933707f3Ssthen 		if(cfg->do_ip4 &&
44531f127bbSsthen 			!add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) {
446933707f3Ssthen 			listening_ports_free(l);
447933707f3Ssthen 			return NULL;
448933707f3Ssthen 		}
449933707f3Ssthen 	}
450933707f3Ssthen 	return l;
451933707f3Ssthen }
452933707f3Ssthen 
453933707f3Ssthen /** open accept commpoint */
454933707f3Ssthen static int
455933707f3Ssthen accept_open(struct daemon_remote* rc, int fd)
456933707f3Ssthen {
457933707f3Ssthen 	struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
458933707f3Ssthen 	if(!n) {
459933707f3Ssthen 		log_err("out of memory");
460933707f3Ssthen 		return 0;
461933707f3Ssthen 	}
462933707f3Ssthen 	n->next = rc->accept_list;
463933707f3Ssthen 	rc->accept_list = n;
464933707f3Ssthen 	/* open commpt */
465933707f3Ssthen 	n->com = comm_point_create_raw(rc->worker->base, fd, 0,
466933707f3Ssthen 		&remote_accept_callback, rc);
467933707f3Ssthen 	if(!n->com)
468933707f3Ssthen 		return 0;
469933707f3Ssthen 	/* keep this port open, its fd is kept in the rc portlist */
470933707f3Ssthen 	n->com->do_not_close = 1;
471933707f3Ssthen 	return 1;
472933707f3Ssthen }
473933707f3Ssthen 
474933707f3Ssthen int daemon_remote_open_accept(struct daemon_remote* rc,
475933707f3Ssthen 	struct listen_port* ports, struct worker* worker)
476933707f3Ssthen {
477933707f3Ssthen 	struct listen_port* p;
478933707f3Ssthen 	rc->worker = worker;
479933707f3Ssthen 	for(p = ports; p; p = p->next) {
480933707f3Ssthen 		if(!accept_open(rc, p->fd)) {
481933707f3Ssthen 			log_err("could not create accept comm point");
482933707f3Ssthen 			return 0;
483933707f3Ssthen 		}
484933707f3Ssthen 	}
485933707f3Ssthen 	return 1;
486933707f3Ssthen }
487933707f3Ssthen 
488af4988b1Ssthen void daemon_remote_stop_accept(struct daemon_remote* rc)
489af4988b1Ssthen {
490af4988b1Ssthen 	struct listen_list* p;
491af4988b1Ssthen 	for(p=rc->accept_list; p; p=p->next) {
492af4988b1Ssthen 		comm_point_stop_listening(p->com);
493af4988b1Ssthen 	}
494af4988b1Ssthen }
495af4988b1Ssthen 
496af4988b1Ssthen void daemon_remote_start_accept(struct daemon_remote* rc)
497af4988b1Ssthen {
498af4988b1Ssthen 	struct listen_list* p;
499af4988b1Ssthen 	for(p=rc->accept_list; p; p=p->next) {
500af4988b1Ssthen 		comm_point_start_listening(p->com, -1, -1);
501af4988b1Ssthen 	}
502af4988b1Ssthen }
503af4988b1Ssthen 
504933707f3Ssthen int remote_accept_callback(struct comm_point* c, void* arg, int err,
505933707f3Ssthen 	struct comm_reply* ATTR_UNUSED(rep))
506933707f3Ssthen {
507933707f3Ssthen 	struct daemon_remote* rc = (struct daemon_remote*)arg;
508933707f3Ssthen 	struct sockaddr_storage addr;
509933707f3Ssthen 	socklen_t addrlen;
510933707f3Ssthen 	int newfd;
511933707f3Ssthen 	struct rc_state* n;
512933707f3Ssthen 	if(err != NETEVENT_NOERROR) {
513933707f3Ssthen 		log_err("error %d on remote_accept_callback", err);
514933707f3Ssthen 		return 0;
515933707f3Ssthen 	}
516933707f3Ssthen 	/* perform the accept */
517933707f3Ssthen 	newfd = comm_point_perform_accept(c, &addr, &addrlen);
518933707f3Ssthen 	if(newfd == -1)
519933707f3Ssthen 		return 0;
520933707f3Ssthen 	/* create new commpoint unless we are servicing already */
521933707f3Ssthen 	if(rc->active >= rc->max_active) {
522933707f3Ssthen 		log_warn("drop incoming remote control: too many connections");
523933707f3Ssthen 	close_exit:
524933707f3Ssthen #ifndef USE_WINSOCK
525933707f3Ssthen 		close(newfd);
526933707f3Ssthen #else
527933707f3Ssthen 		closesocket(newfd);
528933707f3Ssthen #endif
529933707f3Ssthen 		return 0;
530933707f3Ssthen 	}
531933707f3Ssthen 
532933707f3Ssthen 	/* setup commpoint to service the remote control command */
533933707f3Ssthen 	n = (struct rc_state*)calloc(1, sizeof(*n));
534933707f3Ssthen 	if(!n) {
535933707f3Ssthen 		log_err("out of memory");
536933707f3Ssthen 		goto close_exit;
537933707f3Ssthen 	}
538933707f3Ssthen 	/* start in reading state */
539933707f3Ssthen 	n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
540933707f3Ssthen 		&remote_control_callback, n);
541933707f3Ssthen 	if(!n->c) {
542933707f3Ssthen 		log_err("out of memory");
543933707f3Ssthen 		free(n);
544933707f3Ssthen 		goto close_exit;
545933707f3Ssthen 	}
546933707f3Ssthen 	log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
547933707f3Ssthen 	n->c->do_not_close = 0;
548933707f3Ssthen 	comm_point_stop_listening(n->c);
549933707f3Ssthen 	comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
550933707f3Ssthen 	memcpy(&n->c->repinfo.addr, &addr, addrlen);
551933707f3Ssthen 	n->c->repinfo.addrlen = addrlen;
552933707f3Ssthen 	n->shake_state = rc_hs_read;
553933707f3Ssthen 	n->ssl = SSL_new(rc->ctx);
554933707f3Ssthen 	if(!n->ssl) {
555933707f3Ssthen 		log_crypto_err("could not SSL_new");
556933707f3Ssthen 		comm_point_delete(n->c);
557933707f3Ssthen 		free(n);
558933707f3Ssthen 		goto close_exit;
559933707f3Ssthen 	}
560933707f3Ssthen 	SSL_set_accept_state(n->ssl);
561933707f3Ssthen         (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY);
562933707f3Ssthen 	if(!SSL_set_fd(n->ssl, newfd)) {
563933707f3Ssthen 		log_crypto_err("could not SSL_set_fd");
564933707f3Ssthen 		SSL_free(n->ssl);
565933707f3Ssthen 		comm_point_delete(n->c);
566933707f3Ssthen 		free(n);
567933707f3Ssthen 		goto close_exit;
568933707f3Ssthen 	}
569933707f3Ssthen 
570933707f3Ssthen 	n->rc = rc;
571933707f3Ssthen 	n->next = rc->busy_list;
572933707f3Ssthen 	rc->busy_list = n;
573933707f3Ssthen 	rc->active ++;
574933707f3Ssthen 
575933707f3Ssthen 	/* perform the first nonblocking read already, for windows,
576933707f3Ssthen 	 * so it can return wouldblock. could be faster too. */
577933707f3Ssthen 	(void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
578933707f3Ssthen 	return 0;
579933707f3Ssthen }
580933707f3Ssthen 
581933707f3Ssthen /** delete from list */
582933707f3Ssthen static void
583933707f3Ssthen state_list_remove_elem(struct rc_state** list, struct comm_point* c)
584933707f3Ssthen {
585933707f3Ssthen 	while(*list) {
586933707f3Ssthen 		if( (*list)->c == c) {
587933707f3Ssthen 			*list = (*list)->next;
588933707f3Ssthen 			return;
589933707f3Ssthen 		}
590933707f3Ssthen 		list = &(*list)->next;
591933707f3Ssthen 	}
592933707f3Ssthen }
593933707f3Ssthen 
594933707f3Ssthen /** decrease active count and remove commpoint from busy list */
595933707f3Ssthen static void
596933707f3Ssthen clean_point(struct daemon_remote* rc, struct rc_state* s)
597933707f3Ssthen {
598933707f3Ssthen 	state_list_remove_elem(&rc->busy_list, s->c);
599933707f3Ssthen 	rc->active --;
600933707f3Ssthen 	if(s->ssl) {
601933707f3Ssthen 		SSL_shutdown(s->ssl);
602933707f3Ssthen 		SSL_free(s->ssl);
603933707f3Ssthen 	}
604933707f3Ssthen 	comm_point_delete(s->c);
605933707f3Ssthen 	free(s);
606933707f3Ssthen }
607933707f3Ssthen 
608933707f3Ssthen int
609933707f3Ssthen ssl_print_text(SSL* ssl, const char* text)
610933707f3Ssthen {
611933707f3Ssthen 	int r;
612933707f3Ssthen 	if(!ssl)
613933707f3Ssthen 		return 0;
614933707f3Ssthen 	ERR_clear_error();
615933707f3Ssthen 	if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) {
616933707f3Ssthen 		if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
617933707f3Ssthen 			verbose(VERB_QUERY, "warning, in SSL_write, peer "
618933707f3Ssthen 				"closed connection");
619933707f3Ssthen 			return 0;
620933707f3Ssthen 		}
621933707f3Ssthen 		log_crypto_err("could not SSL_write");
622933707f3Ssthen 		return 0;
623933707f3Ssthen 	}
624933707f3Ssthen 	return 1;
625933707f3Ssthen }
626933707f3Ssthen 
627933707f3Ssthen /** print text over the ssl connection */
628933707f3Ssthen static int
629933707f3Ssthen ssl_print_vmsg(SSL* ssl, const char* format, va_list args)
630933707f3Ssthen {
631933707f3Ssthen 	char msg[1024];
632933707f3Ssthen 	vsnprintf(msg, sizeof(msg), format, args);
633933707f3Ssthen 	return ssl_print_text(ssl, msg);
634933707f3Ssthen }
635933707f3Ssthen 
636933707f3Ssthen /** printf style printing to the ssl connection */
637933707f3Ssthen int ssl_printf(SSL* ssl, const char* format, ...)
638933707f3Ssthen {
639933707f3Ssthen 	va_list args;
640933707f3Ssthen 	int ret;
641933707f3Ssthen 	va_start(args, format);
642933707f3Ssthen 	ret = ssl_print_vmsg(ssl, format, args);
643933707f3Ssthen 	va_end(args);
644933707f3Ssthen 	return ret;
645933707f3Ssthen }
646933707f3Ssthen 
647933707f3Ssthen int
648933707f3Ssthen ssl_read_line(SSL* ssl, char* buf, size_t max)
649933707f3Ssthen {
650933707f3Ssthen 	int r;
651933707f3Ssthen 	size_t len = 0;
652933707f3Ssthen 	if(!ssl)
653933707f3Ssthen 		return 0;
654933707f3Ssthen 	while(len < max) {
655933707f3Ssthen 		ERR_clear_error();
656933707f3Ssthen 		if((r=SSL_read(ssl, buf+len, 1)) <= 0) {
657933707f3Ssthen 			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
658933707f3Ssthen 				buf[len] = 0;
659933707f3Ssthen 				return 1;
660933707f3Ssthen 			}
661933707f3Ssthen 			log_crypto_err("could not SSL_read");
662933707f3Ssthen 			return 0;
663933707f3Ssthen 		}
664933707f3Ssthen 		if(buf[len] == '\n') {
665933707f3Ssthen 			/* return string without \n */
666933707f3Ssthen 			buf[len] = 0;
667933707f3Ssthen 			return 1;
668933707f3Ssthen 		}
669933707f3Ssthen 		len++;
670933707f3Ssthen 	}
671933707f3Ssthen 	buf[max-1] = 0;
672933707f3Ssthen 	log_err("control line too long (%d): %s", (int)max, buf);
673933707f3Ssthen 	return 0;
674933707f3Ssthen }
675933707f3Ssthen 
676933707f3Ssthen /** skip whitespace, return new pointer into string */
677933707f3Ssthen static char*
678933707f3Ssthen skipwhite(char* str)
679933707f3Ssthen {
680933707f3Ssthen 	/* EOS \0 is not a space */
681e10d3884Sbrad 	while( isspace((unsigned char)*str) )
682933707f3Ssthen 		str++;
683933707f3Ssthen 	return str;
684933707f3Ssthen }
685933707f3Ssthen 
686933707f3Ssthen /** send the OK to the control client */
687933707f3Ssthen static void send_ok(SSL* ssl)
688933707f3Ssthen {
689933707f3Ssthen 	(void)ssl_printf(ssl, "ok\n");
690933707f3Ssthen }
691933707f3Ssthen 
692933707f3Ssthen /** do the stop command */
693933707f3Ssthen static void
694933707f3Ssthen do_stop(SSL* ssl, struct daemon_remote* rc)
695933707f3Ssthen {
696933707f3Ssthen 	rc->worker->need_to_exit = 1;
697933707f3Ssthen 	comm_base_exit(rc->worker->base);
698933707f3Ssthen 	send_ok(ssl);
699933707f3Ssthen }
700933707f3Ssthen 
701933707f3Ssthen /** do the reload command */
702933707f3Ssthen static void
703933707f3Ssthen do_reload(SSL* ssl, struct daemon_remote* rc)
704933707f3Ssthen {
705933707f3Ssthen 	rc->worker->need_to_exit = 0;
706933707f3Ssthen 	comm_base_exit(rc->worker->base);
707933707f3Ssthen 	send_ok(ssl);
708933707f3Ssthen }
709933707f3Ssthen 
710933707f3Ssthen /** do the verbosity command */
711933707f3Ssthen static void
712933707f3Ssthen do_verbosity(SSL* ssl, char* str)
713933707f3Ssthen {
714933707f3Ssthen 	int val = atoi(str);
715933707f3Ssthen 	if(val == 0 && strcmp(str, "0") != 0) {
716933707f3Ssthen 		ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
717933707f3Ssthen 		return;
718933707f3Ssthen 	}
719933707f3Ssthen 	verbosity = val;
720933707f3Ssthen 	send_ok(ssl);
721933707f3Ssthen }
722933707f3Ssthen 
723933707f3Ssthen /** print stats from statinfo */
724933707f3Ssthen static int
725933707f3Ssthen print_stats(SSL* ssl, const char* nm, struct stats_info* s)
726933707f3Ssthen {
727933707f3Ssthen 	struct timeval avg;
728e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm,
729e10d3884Sbrad 		(unsigned long)s->svr.num_queries)) return 0;
730e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm,
731e10d3884Sbrad 		(unsigned long)(s->svr.num_queries
732933707f3Ssthen 			- s->svr.num_queries_missed_cache))) return 0;
733e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm,
734e10d3884Sbrad 		(unsigned long)s->svr.num_queries_missed_cache)) return 0;
735e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm,
736e10d3884Sbrad 		(unsigned long)s->svr.num_queries_prefetch)) return 0;
737e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm,
738e10d3884Sbrad 		(unsigned long)s->mesh_replies_sent)) return 0;
739933707f3Ssthen 	if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
740933707f3Ssthen 		(s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
741933707f3Ssthen 			(double)s->svr.sum_query_list_size/
742933707f3Ssthen 			(s->svr.num_queries_missed_cache+
743933707f3Ssthen 			s->svr.num_queries_prefetch) : 0.0)) return 0;
744e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm,
745e10d3884Sbrad 		(unsigned long)s->svr.max_query_list_size)) return 0;
746e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm,
747e10d3884Sbrad 		(unsigned long)s->mesh_jostled)) return 0;
748e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm,
749e10d3884Sbrad 		(unsigned long)s->mesh_dropped)) return 0;
750e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm,
751e10d3884Sbrad 		(unsigned long)s->mesh_num_states)) return 0;
752e10d3884Sbrad 	if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm,
753e10d3884Sbrad 		(unsigned long)s->mesh_num_reply_states)) return 0;
754933707f3Ssthen 	timeval_divide(&avg, &s->mesh_replies_sum_wait, s->mesh_replies_sent);
7550b68ff31Ssthen 	if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
756e9c7b4efSsthen 		(long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
757933707f3Ssthen 	if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
758933707f3Ssthen 		s->mesh_time_median)) return 0;
759a58bff56Ssthen 	if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm,
760a58bff56Ssthen 		(unsigned long)s->svr.tcp_accept_usage)) return 0;
761933707f3Ssthen 	return 1;
762933707f3Ssthen }
763933707f3Ssthen 
764933707f3Ssthen /** print stats for one thread */
765933707f3Ssthen static int
766933707f3Ssthen print_thread_stats(SSL* ssl, int i, struct stats_info* s)
767933707f3Ssthen {
768933707f3Ssthen 	char nm[16];
769933707f3Ssthen 	snprintf(nm, sizeof(nm), "thread%d", i);
770933707f3Ssthen 	nm[sizeof(nm)-1]=0;
771933707f3Ssthen 	return print_stats(ssl, nm, s);
772933707f3Ssthen }
773933707f3Ssthen 
774933707f3Ssthen /** print long number */
775933707f3Ssthen static int
776e10d3884Sbrad print_longnum(SSL* ssl, const char* desc, size_t x)
777933707f3Ssthen {
778933707f3Ssthen 	if(x > 1024*1024*1024) {
779933707f3Ssthen 		/* more than a Gb */
780933707f3Ssthen 		size_t front = x / (size_t)1000000;
781933707f3Ssthen 		size_t back = x % (size_t)1000000;
782933707f3Ssthen 		return ssl_printf(ssl, "%s%u%6.6u\n", desc,
783933707f3Ssthen 			(unsigned)front, (unsigned)back);
784933707f3Ssthen 	} else {
785e10d3884Sbrad 		return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x);
786933707f3Ssthen 	}
787933707f3Ssthen }
788933707f3Ssthen 
789933707f3Ssthen /** print mem stats */
790933707f3Ssthen static int
791933707f3Ssthen print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon)
792933707f3Ssthen {
793933707f3Ssthen 	int m;
794933707f3Ssthen 	size_t msg, rrset, val, iter;
795933707f3Ssthen #ifdef HAVE_SBRK
796933707f3Ssthen 	extern void* unbound_start_brk;
797933707f3Ssthen 	void* cur = sbrk(0);
798933707f3Ssthen 	if(!print_longnum(ssl, "mem.total.sbrk"SQ,
799933707f3Ssthen 		(size_t)((char*)cur - (char*)unbound_start_brk))) return 0;
800933707f3Ssthen #endif /* HAVE_SBRK */
801933707f3Ssthen 	msg = slabhash_get_mem(daemon->env->msg_cache);
802933707f3Ssthen 	rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
803933707f3Ssthen 	val=0;
804933707f3Ssthen 	iter=0;
805933707f3Ssthen 	m = modstack_find(&worker->env.mesh->mods, "validator");
806933707f3Ssthen 	if(m != -1) {
807933707f3Ssthen 		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
808933707f3Ssthen 			mods.mod[m]->get_mem));
809933707f3Ssthen 		val = (*worker->env.mesh->mods.mod[m]->get_mem)
810933707f3Ssthen 			(&worker->env, m);
811933707f3Ssthen 	}
812933707f3Ssthen 	m = modstack_find(&worker->env.mesh->mods, "iterator");
813933707f3Ssthen 	if(m != -1) {
814933707f3Ssthen 		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
815933707f3Ssthen 			mods.mod[m]->get_mem));
816933707f3Ssthen 		iter = (*worker->env.mesh->mods.mod[m]->get_mem)
817933707f3Ssthen 			(&worker->env, m);
818933707f3Ssthen 	}
819933707f3Ssthen 
820933707f3Ssthen 	if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
821933707f3Ssthen 		return 0;
822933707f3Ssthen 	if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
823933707f3Ssthen 		return 0;
824933707f3Ssthen 	if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
825933707f3Ssthen 		return 0;
826933707f3Ssthen 	if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
827933707f3Ssthen 		return 0;
828933707f3Ssthen 	return 1;
829933707f3Ssthen }
830933707f3Ssthen 
831933707f3Ssthen /** print uptime stats */
832933707f3Ssthen static int
833933707f3Ssthen print_uptime(SSL* ssl, struct worker* worker, int reset)
834933707f3Ssthen {
835933707f3Ssthen 	struct timeval now = *worker->env.now_tv;
836933707f3Ssthen 	struct timeval up, dt;
837933707f3Ssthen 	timeval_subtract(&up, &now, &worker->daemon->time_boot);
838933707f3Ssthen 	timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
839933707f3Ssthen 	if(reset)
840933707f3Ssthen 		worker->daemon->time_last_stat = now;
8410b68ff31Ssthen 	if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
842e9c7b4efSsthen 		(long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
8430b68ff31Ssthen 	if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
844e9c7b4efSsthen 		(long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
8450b68ff31Ssthen 	if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
846e9c7b4efSsthen 		(long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
847933707f3Ssthen 	return 1;
848933707f3Ssthen }
849933707f3Ssthen 
850933707f3Ssthen /** print extended histogram */
851933707f3Ssthen static int
852933707f3Ssthen print_hist(SSL* ssl, struct stats_info* s)
853933707f3Ssthen {
854933707f3Ssthen 	struct timehist* hist;
855933707f3Ssthen 	size_t i;
856933707f3Ssthen 	hist = timehist_setup();
857933707f3Ssthen 	if(!hist) {
858933707f3Ssthen 		log_err("out of memory");
859933707f3Ssthen 		return 0;
860933707f3Ssthen 	}
861933707f3Ssthen 	timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
862933707f3Ssthen 	for(i=0; i<hist->num; i++) {
863933707f3Ssthen 		if(!ssl_printf(ssl,
864e10d3884Sbrad 			"histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
865933707f3Ssthen 			(int)hist->buckets[i].lower.tv_sec,
866933707f3Ssthen 			(int)hist->buckets[i].lower.tv_usec,
867933707f3Ssthen 			(int)hist->buckets[i].upper.tv_sec,
868933707f3Ssthen 			(int)hist->buckets[i].upper.tv_usec,
869e10d3884Sbrad 			(unsigned long)hist->buckets[i].count)) {
870933707f3Ssthen 			timehist_delete(hist);
871933707f3Ssthen 			return 0;
872933707f3Ssthen 		}
873933707f3Ssthen 	}
874933707f3Ssthen 	timehist_delete(hist);
875933707f3Ssthen 	return 1;
876933707f3Ssthen }
877933707f3Ssthen 
878933707f3Ssthen /** print extended stats */
879933707f3Ssthen static int
880933707f3Ssthen print_ext(SSL* ssl, struct stats_info* s)
881933707f3Ssthen {
882933707f3Ssthen 	int i;
883933707f3Ssthen 	char nm[16];
8840b68ff31Ssthen 	const sldns_rr_descriptor* desc;
8850b68ff31Ssthen 	const sldns_lookup_table* lt;
886933707f3Ssthen 	/* TYPE */
887933707f3Ssthen 	for(i=0; i<STATS_QTYPE_NUM; i++) {
888933707f3Ssthen 		if(inhibit_zero && s->svr.qtype[i] == 0)
889933707f3Ssthen 			continue;
8900b68ff31Ssthen 		desc = sldns_rr_descript((uint16_t)i);
891933707f3Ssthen 		if(desc && desc->_name) {
892933707f3Ssthen 			snprintf(nm, sizeof(nm), "%s", desc->_name);
893933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_IXFR) {
894933707f3Ssthen 			snprintf(nm, sizeof(nm), "IXFR");
895933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_AXFR) {
896933707f3Ssthen 			snprintf(nm, sizeof(nm), "AXFR");
897933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_MAILA) {
898933707f3Ssthen 			snprintf(nm, sizeof(nm), "MAILA");
899933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_MAILB) {
900933707f3Ssthen 			snprintf(nm, sizeof(nm), "MAILB");
901933707f3Ssthen 		} else if (i == LDNS_RR_TYPE_ANY) {
902933707f3Ssthen 			snprintf(nm, sizeof(nm), "ANY");
903933707f3Ssthen 		} else {
904933707f3Ssthen 			snprintf(nm, sizeof(nm), "TYPE%d", i);
905933707f3Ssthen 		}
906e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n",
907e10d3884Sbrad 			nm, (unsigned long)s->svr.qtype[i])) return 0;
908933707f3Ssthen 	}
909933707f3Ssthen 	if(!inhibit_zero || s->svr.qtype_big) {
910e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n",
911e10d3884Sbrad 			(unsigned long)s->svr.qtype_big)) return 0;
912933707f3Ssthen 	}
913933707f3Ssthen 	/* CLASS */
914933707f3Ssthen 	for(i=0; i<STATS_QCLASS_NUM; i++) {
915933707f3Ssthen 		if(inhibit_zero && s->svr.qclass[i] == 0)
916933707f3Ssthen 			continue;
9170b68ff31Ssthen 		lt = sldns_lookup_by_id(sldns_rr_classes, i);
918933707f3Ssthen 		if(lt && lt->name) {
919933707f3Ssthen 			snprintf(nm, sizeof(nm), "%s", lt->name);
920933707f3Ssthen 		} else {
921933707f3Ssthen 			snprintf(nm, sizeof(nm), "CLASS%d", i);
922933707f3Ssthen 		}
923e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n",
924e10d3884Sbrad 			nm, (unsigned long)s->svr.qclass[i])) return 0;
925933707f3Ssthen 	}
926933707f3Ssthen 	if(!inhibit_zero || s->svr.qclass_big) {
927e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n",
928e10d3884Sbrad 			(unsigned long)s->svr.qclass_big)) return 0;
929933707f3Ssthen 	}
930933707f3Ssthen 	/* OPCODE */
931933707f3Ssthen 	for(i=0; i<STATS_OPCODE_NUM; i++) {
932933707f3Ssthen 		if(inhibit_zero && s->svr.qopcode[i] == 0)
933933707f3Ssthen 			continue;
9340b68ff31Ssthen 		lt = sldns_lookup_by_id(sldns_opcodes, i);
935933707f3Ssthen 		if(lt && lt->name) {
936933707f3Ssthen 			snprintf(nm, sizeof(nm), "%s", lt->name);
937933707f3Ssthen 		} else {
938933707f3Ssthen 			snprintf(nm, sizeof(nm), "OPCODE%d", i);
939933707f3Ssthen 		}
940e10d3884Sbrad 		if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n",
941e10d3884Sbrad 			nm, (unsigned long)s->svr.qopcode[i])) return 0;
942933707f3Ssthen 	}
943933707f3Ssthen 	/* transport */
944e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n",
945e10d3884Sbrad 		(unsigned long)s->svr.qtcp)) return 0;
946e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n",
947e10d3884Sbrad 		(unsigned long)s->svr.qtcp_outgoing)) return 0;
948e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n",
949e10d3884Sbrad 		(unsigned long)s->svr.qipv6)) return 0;
950933707f3Ssthen 	/* flags */
951e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n",
952e10d3884Sbrad 		(unsigned long)s->svr.qbit_QR)) return 0;
953e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n",
954e10d3884Sbrad 		(unsigned long)s->svr.qbit_AA)) return 0;
955e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n",
956e10d3884Sbrad 		(unsigned long)s->svr.qbit_TC)) return 0;
957e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n",
958e10d3884Sbrad 		(unsigned long)s->svr.qbit_RD)) return 0;
959e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n",
960e10d3884Sbrad 		(unsigned long)s->svr.qbit_RA)) return 0;
961e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n",
962e10d3884Sbrad 		(unsigned long)s->svr.qbit_Z)) return 0;
963e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n",
964e10d3884Sbrad 		(unsigned long)s->svr.qbit_AD)) return 0;
965e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n",
966e10d3884Sbrad 		(unsigned long)s->svr.qbit_CD)) return 0;
967e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n",
968e10d3884Sbrad 		(unsigned long)s->svr.qEDNS)) return 0;
969e10d3884Sbrad 	if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n",
970e10d3884Sbrad 		(unsigned long)s->svr.qEDNS_DO)) return 0;
971933707f3Ssthen 
972933707f3Ssthen 	/* RCODE */
973933707f3Ssthen 	for(i=0; i<STATS_RCODE_NUM; i++) {
974d50c774aSbrad 		/* Always include RCODEs 0-5 */
975d50c774aSbrad 		if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
976933707f3Ssthen 			continue;
9770b68ff31Ssthen 		lt = sldns_lookup_by_id(sldns_rcodes, i);
978933707f3Ssthen 		if(lt && lt->name) {
979933707f3Ssthen 			snprintf(nm, sizeof(nm), "%s", lt->name);
980933707f3Ssthen 		} else {
981933707f3Ssthen 			snprintf(nm, sizeof(nm), "RCODE%d", i);
982933707f3Ssthen 		}
983e10d3884Sbrad 		if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n",
984e10d3884Sbrad 			nm, (unsigned long)s->svr.ans_rcode[i])) return 0;
985933707f3Ssthen 	}
986933707f3Ssthen 	if(!inhibit_zero || s->svr.ans_rcode_nodata) {
987e10d3884Sbrad 		if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n",
988e10d3884Sbrad 			(unsigned long)s->svr.ans_rcode_nodata)) return 0;
989933707f3Ssthen 	}
990933707f3Ssthen 	/* validation */
991e10d3884Sbrad 	if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n",
992e10d3884Sbrad 		(unsigned long)s->svr.ans_secure)) return 0;
993e10d3884Sbrad 	if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n",
994e10d3884Sbrad 		(unsigned long)s->svr.ans_bogus)) return 0;
995e10d3884Sbrad 	if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n",
996e10d3884Sbrad 		(unsigned long)s->svr.rrset_bogus)) return 0;
997933707f3Ssthen 	/* threat detection */
998e10d3884Sbrad 	if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n",
999e10d3884Sbrad 		(unsigned long)s->svr.unwanted_queries)) return 0;
1000e10d3884Sbrad 	if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n",
1001e10d3884Sbrad 		(unsigned long)s->svr.unwanted_replies)) return 0;
1002e10d3884Sbrad 	/* cache counts */
1003e10d3884Sbrad 	if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
1004e10d3884Sbrad 		(unsigned)s->svr.msg_cache_count)) return 0;
1005e10d3884Sbrad 	if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
1006e10d3884Sbrad 		(unsigned)s->svr.rrset_cache_count)) return 0;
1007e10d3884Sbrad 	if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
1008e10d3884Sbrad 		(unsigned)s->svr.infra_cache_count)) return 0;
1009e10d3884Sbrad 	if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
1010e10d3884Sbrad 		(unsigned)s->svr.key_cache_count)) return 0;
1011933707f3Ssthen 	return 1;
1012933707f3Ssthen }
1013933707f3Ssthen 
1014933707f3Ssthen /** do the stats command */
1015933707f3Ssthen static void
1016933707f3Ssthen do_stats(SSL* ssl, struct daemon_remote* rc, int reset)
1017933707f3Ssthen {
1018933707f3Ssthen 	struct daemon* daemon = rc->worker->daemon;
1019933707f3Ssthen 	struct stats_info total;
1020933707f3Ssthen 	struct stats_info s;
1021933707f3Ssthen 	int i;
1022933707f3Ssthen 	log_assert(daemon->num > 0);
1023933707f3Ssthen 	/* gather all thread statistics in one place */
1024933707f3Ssthen 	for(i=0; i<daemon->num; i++) {
1025933707f3Ssthen 		server_stats_obtain(rc->worker, daemon->workers[i], &s, reset);
1026933707f3Ssthen 		if(!print_thread_stats(ssl, i, &s))
1027933707f3Ssthen 			return;
1028933707f3Ssthen 		if(i == 0)
1029933707f3Ssthen 			total = s;
1030933707f3Ssthen 		else	server_stats_add(&total, &s);
1031933707f3Ssthen 	}
1032933707f3Ssthen 	/* print the thread statistics */
1033933707f3Ssthen 	total.mesh_time_median /= (double)daemon->num;
1034933707f3Ssthen 	if(!print_stats(ssl, "total", &total))
1035933707f3Ssthen 		return;
1036933707f3Ssthen 	if(!print_uptime(ssl, rc->worker, reset))
1037933707f3Ssthen 		return;
1038933707f3Ssthen 	if(daemon->cfg->stat_extended) {
1039933707f3Ssthen 		if(!print_mem(ssl, rc->worker, daemon))
1040933707f3Ssthen 			return;
1041933707f3Ssthen 		if(!print_hist(ssl, &total))
1042933707f3Ssthen 			return;
1043933707f3Ssthen 		if(!print_ext(ssl, &total))
1044933707f3Ssthen 			return;
1045933707f3Ssthen 	}
1046933707f3Ssthen }
1047933707f3Ssthen 
1048933707f3Ssthen /** parse commandline argument domain name */
1049933707f3Ssthen static int
1050933707f3Ssthen parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs)
1051933707f3Ssthen {
10520b68ff31Ssthen 	uint8_t nm[LDNS_MAX_DOMAINLEN+1];
10530b68ff31Ssthen 	size_t nmlen = sizeof(nm);
10540b68ff31Ssthen 	int status;
1055933707f3Ssthen 	*res = NULL;
1056933707f3Ssthen 	*len = 0;
1057933707f3Ssthen 	*labs = 0;
10580b68ff31Ssthen 	status = sldns_str2wire_dname_buf(str, nm, &nmlen);
10590b68ff31Ssthen 	if(status != 0) {
10600b68ff31Ssthen 		ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
10610b68ff31Ssthen 			LDNS_WIREPARSE_OFFSET(status),
10620b68ff31Ssthen 			sldns_get_errorstr_parse(status));
1063933707f3Ssthen 		return 0;
1064933707f3Ssthen 	}
10650b68ff31Ssthen 	*res = memdup(nm, nmlen);
1066933707f3Ssthen 	if(!*res) {
1067933707f3Ssthen 		ssl_printf(ssl, "error out of memory\n");
1068933707f3Ssthen 		return 0;
1069933707f3Ssthen 	}
1070933707f3Ssthen 	*labs = dname_count_size_labels(*res, len);
1071933707f3Ssthen 	return 1;
1072933707f3Ssthen }
1073933707f3Ssthen 
1074933707f3Ssthen /** find second argument, modifies string */
1075933707f3Ssthen static int
1076933707f3Ssthen find_arg2(SSL* ssl, char* arg, char** arg2)
1077933707f3Ssthen {
1078933707f3Ssthen 	char* as = strchr(arg, ' ');
1079933707f3Ssthen 	char* at = strchr(arg, '\t');
1080933707f3Ssthen 	if(as && at) {
1081933707f3Ssthen 		if(at < as)
1082933707f3Ssthen 			as = at;
1083933707f3Ssthen 		as[0]=0;
1084933707f3Ssthen 		*arg2 = skipwhite(as+1);
1085933707f3Ssthen 	} else if(as) {
1086933707f3Ssthen 		as[0]=0;
1087933707f3Ssthen 		*arg2 = skipwhite(as+1);
1088933707f3Ssthen 	} else if(at) {
1089933707f3Ssthen 		at[0]=0;
1090933707f3Ssthen 		*arg2 = skipwhite(at+1);
1091933707f3Ssthen 	} else {
1092933707f3Ssthen 		ssl_printf(ssl, "error could not find next argument "
1093933707f3Ssthen 			"after %s\n", arg);
1094933707f3Ssthen 		return 0;
1095933707f3Ssthen 	}
1096933707f3Ssthen 	return 1;
1097933707f3Ssthen }
1098933707f3Ssthen 
1099933707f3Ssthen /** Add a new zone */
1100933707f3Ssthen static void
1101933707f3Ssthen do_zone_add(SSL* ssl, struct worker* worker, char* arg)
1102933707f3Ssthen {
1103933707f3Ssthen 	uint8_t* nm;
1104933707f3Ssthen 	int nmlabs;
1105933707f3Ssthen 	size_t nmlen;
1106933707f3Ssthen 	char* arg2;
1107933707f3Ssthen 	enum localzone_type t;
1108933707f3Ssthen 	struct local_zone* z;
1109933707f3Ssthen 	if(!find_arg2(ssl, arg, &arg2))
1110933707f3Ssthen 		return;
1111933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1112933707f3Ssthen 		return;
1113933707f3Ssthen 	if(!local_zone_str2type(arg2, &t)) {
1114933707f3Ssthen 		ssl_printf(ssl, "error not a zone type. %s\n", arg2);
1115933707f3Ssthen 		free(nm);
1116933707f3Ssthen 		return;
1117933707f3Ssthen 	}
11180b68ff31Ssthen 	lock_rw_wrlock(&worker->daemon->local_zones->lock);
1119933707f3Ssthen 	if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1120933707f3Ssthen 		nmlabs, LDNS_RR_CLASS_IN))) {
1121933707f3Ssthen 		/* already present in tree */
1122933707f3Ssthen 		lock_rw_wrlock(&z->lock);
1123933707f3Ssthen 		z->type = t; /* update type anyway */
1124933707f3Ssthen 		lock_rw_unlock(&z->lock);
1125933707f3Ssthen 		free(nm);
11260b68ff31Ssthen 		lock_rw_unlock(&worker->daemon->local_zones->lock);
1127933707f3Ssthen 		send_ok(ssl);
1128933707f3Ssthen 		return;
1129933707f3Ssthen 	}
1130933707f3Ssthen 	if(!local_zones_add_zone(worker->daemon->local_zones, nm, nmlen,
1131933707f3Ssthen 		nmlabs, LDNS_RR_CLASS_IN, t)) {
11320b68ff31Ssthen 		lock_rw_unlock(&worker->daemon->local_zones->lock);
1133933707f3Ssthen 		ssl_printf(ssl, "error out of memory\n");
1134933707f3Ssthen 		return;
1135933707f3Ssthen 	}
11360b68ff31Ssthen 	lock_rw_unlock(&worker->daemon->local_zones->lock);
1137933707f3Ssthen 	send_ok(ssl);
1138933707f3Ssthen }
1139933707f3Ssthen 
1140933707f3Ssthen /** Remove a zone */
1141933707f3Ssthen static void
1142933707f3Ssthen do_zone_remove(SSL* ssl, struct worker* worker, char* arg)
1143933707f3Ssthen {
1144933707f3Ssthen 	uint8_t* nm;
1145933707f3Ssthen 	int nmlabs;
1146933707f3Ssthen 	size_t nmlen;
1147933707f3Ssthen 	struct local_zone* z;
1148933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1149933707f3Ssthen 		return;
11500b68ff31Ssthen 	lock_rw_wrlock(&worker->daemon->local_zones->lock);
1151933707f3Ssthen 	if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1152933707f3Ssthen 		nmlabs, LDNS_RR_CLASS_IN))) {
1153933707f3Ssthen 		/* present in tree */
1154933707f3Ssthen 		local_zones_del_zone(worker->daemon->local_zones, z);
1155933707f3Ssthen 	}
11560b68ff31Ssthen 	lock_rw_unlock(&worker->daemon->local_zones->lock);
1157933707f3Ssthen 	free(nm);
1158933707f3Ssthen 	send_ok(ssl);
1159933707f3Ssthen }
1160933707f3Ssthen 
1161933707f3Ssthen /** Add new RR data */
1162933707f3Ssthen static void
1163933707f3Ssthen do_data_add(SSL* ssl, struct worker* worker, char* arg)
1164933707f3Ssthen {
11650b68ff31Ssthen 	if(!local_zones_add_RR(worker->daemon->local_zones, arg)) {
1166933707f3Ssthen 		ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1167933707f3Ssthen 		return;
1168933707f3Ssthen 	}
1169933707f3Ssthen 	send_ok(ssl);
1170933707f3Ssthen }
1171933707f3Ssthen 
1172933707f3Ssthen /** Remove RR data */
1173933707f3Ssthen static void
1174933707f3Ssthen do_data_remove(SSL* ssl, struct worker* worker, char* arg)
1175933707f3Ssthen {
1176933707f3Ssthen 	uint8_t* nm;
1177933707f3Ssthen 	int nmlabs;
1178933707f3Ssthen 	size_t nmlen;
1179933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1180933707f3Ssthen 		return;
1181933707f3Ssthen 	local_zones_del_data(worker->daemon->local_zones, nm,
1182933707f3Ssthen 		nmlen, nmlabs, LDNS_RR_CLASS_IN);
1183933707f3Ssthen 	free(nm);
1184933707f3Ssthen 	send_ok(ssl);
1185933707f3Ssthen }
1186933707f3Ssthen 
1187933707f3Ssthen /** cache lookup of nameservers */
1188933707f3Ssthen static void
1189933707f3Ssthen do_lookup(SSL* ssl, struct worker* worker, char* arg)
1190933707f3Ssthen {
1191933707f3Ssthen 	uint8_t* nm;
1192933707f3Ssthen 	int nmlabs;
1193933707f3Ssthen 	size_t nmlen;
1194933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1195933707f3Ssthen 		return;
1196933707f3Ssthen 	(void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1197933707f3Ssthen 	free(nm);
1198933707f3Ssthen }
1199933707f3Ssthen 
1200933707f3Ssthen /** flush something from rrset and msg caches */
1201933707f3Ssthen static void
1202933707f3Ssthen do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1203933707f3Ssthen 	uint16_t t, uint16_t c)
1204933707f3Ssthen {
1205933707f3Ssthen 	hashvalue_t h;
1206933707f3Ssthen 	struct query_info k;
1207933707f3Ssthen 	rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1208933707f3Ssthen 	if(t == LDNS_RR_TYPE_SOA)
1209933707f3Ssthen 		rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1210933707f3Ssthen 			PACKED_RRSET_SOA_NEG);
1211933707f3Ssthen 	k.qname = nm;
1212933707f3Ssthen 	k.qname_len = nmlen;
1213933707f3Ssthen 	k.qtype = t;
1214933707f3Ssthen 	k.qclass = c;
121557dceb2aSbrad 	h = query_info_hash(&k, 0);
1216933707f3Ssthen 	slabhash_remove(worker->env.msg_cache, h, &k);
121757dceb2aSbrad 	if(t == LDNS_RR_TYPE_AAAA) {
121857dceb2aSbrad 		/* for AAAA also flush dns64 bit_cd packet */
121957dceb2aSbrad 		h = query_info_hash(&k, BIT_CD);
122057dceb2aSbrad 		slabhash_remove(worker->env.msg_cache, h, &k);
122157dceb2aSbrad 	}
1222933707f3Ssthen }
1223933707f3Ssthen 
1224933707f3Ssthen /** flush a type */
1225933707f3Ssthen static void
1226933707f3Ssthen do_flush_type(SSL* ssl, struct worker* worker, char* arg)
1227933707f3Ssthen {
1228933707f3Ssthen 	uint8_t* nm;
1229933707f3Ssthen 	int nmlabs;
1230933707f3Ssthen 	size_t nmlen;
1231933707f3Ssthen 	char* arg2;
1232933707f3Ssthen 	uint16_t t;
1233933707f3Ssthen 	if(!find_arg2(ssl, arg, &arg2))
1234933707f3Ssthen 		return;
1235933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1236933707f3Ssthen 		return;
12370b68ff31Ssthen 	t = sldns_get_rr_type_by_name(arg2);
1238933707f3Ssthen 	do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1239933707f3Ssthen 
1240933707f3Ssthen 	free(nm);
1241933707f3Ssthen 	send_ok(ssl);
1242933707f3Ssthen }
1243933707f3Ssthen 
1244933707f3Ssthen /** flush statistics */
1245933707f3Ssthen static void
1246933707f3Ssthen do_flush_stats(SSL* ssl, struct worker* worker)
1247933707f3Ssthen {
1248933707f3Ssthen 	worker_stats_clear(worker);
1249933707f3Ssthen 	send_ok(ssl);
1250933707f3Ssthen }
1251933707f3Ssthen 
1252933707f3Ssthen /**
1253933707f3Ssthen  * Local info for deletion functions
1254933707f3Ssthen  */
1255933707f3Ssthen struct del_info {
1256933707f3Ssthen 	/** worker */
1257933707f3Ssthen 	struct worker* worker;
1258933707f3Ssthen 	/** name to delete */
1259933707f3Ssthen 	uint8_t* name;
1260933707f3Ssthen 	/** length */
1261933707f3Ssthen 	size_t len;
1262933707f3Ssthen 	/** labels */
1263933707f3Ssthen 	int labs;
1264933707f3Ssthen 	/** time to invalidate to */
1265e9c7b4efSsthen 	time_t expired;
1266933707f3Ssthen 	/** number of rrsets removed */
1267933707f3Ssthen 	size_t num_rrsets;
1268933707f3Ssthen 	/** number of msgs removed */
1269933707f3Ssthen 	size_t num_msgs;
1270933707f3Ssthen 	/** number of key entries removed */
1271933707f3Ssthen 	size_t num_keys;
1272933707f3Ssthen 	/** length of addr */
1273933707f3Ssthen 	socklen_t addrlen;
1274933707f3Ssthen 	/** socket address for host deletion */
1275933707f3Ssthen 	struct sockaddr_storage addr;
1276933707f3Ssthen };
1277933707f3Ssthen 
1278933707f3Ssthen /** callback to delete hosts in infra cache */
1279933707f3Ssthen static void
1280933707f3Ssthen infra_del_host(struct lruhash_entry* e, void* arg)
1281933707f3Ssthen {
1282933707f3Ssthen 	/* entry is locked */
1283933707f3Ssthen 	struct del_info* inf = (struct del_info*)arg;
1284933707f3Ssthen 	struct infra_key* k = (struct infra_key*)e->key;
1285933707f3Ssthen 	if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1286933707f3Ssthen 		struct infra_data* d = (struct infra_data*)e->data;
1287163a4143Ssthen 		d->probedelay = 0;
1288163a4143Ssthen 		d->timeout_A = 0;
1289163a4143Ssthen 		d->timeout_AAAA = 0;
1290163a4143Ssthen 		d->timeout_other = 0;
1291163a4143Ssthen 		rtt_init(&d->rtt);
1292*a961b961Ssthen 		if(d->ttl > inf->expired) {
1293933707f3Ssthen 			d->ttl = inf->expired;
1294933707f3Ssthen 			inf->num_keys++;
1295933707f3Ssthen 		}
1296933707f3Ssthen 	}
1297933707f3Ssthen }
1298933707f3Ssthen 
1299933707f3Ssthen /** flush infra cache */
1300933707f3Ssthen static void
1301933707f3Ssthen do_flush_infra(SSL* ssl, struct worker* worker, char* arg)
1302933707f3Ssthen {
1303933707f3Ssthen 	struct sockaddr_storage addr;
1304933707f3Ssthen 	socklen_t len;
1305933707f3Ssthen 	struct del_info inf;
1306933707f3Ssthen 	if(strcmp(arg, "all") == 0) {
1307933707f3Ssthen 		slabhash_clear(worker->env.infra_cache->hosts);
1308933707f3Ssthen 		send_ok(ssl);
1309933707f3Ssthen 		return;
1310933707f3Ssthen 	}
1311933707f3Ssthen 	if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1312933707f3Ssthen 		(void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1313933707f3Ssthen 		return;
1314933707f3Ssthen 	}
1315933707f3Ssthen 	/* delete all entries from cache */
1316933707f3Ssthen 	/* what we do is to set them all expired */
1317933707f3Ssthen 	inf.worker = worker;
1318933707f3Ssthen 	inf.name = 0;
1319933707f3Ssthen 	inf.len = 0;
1320933707f3Ssthen 	inf.labs = 0;
1321933707f3Ssthen 	inf.expired = *worker->env.now;
1322933707f3Ssthen 	inf.expired -= 3; /* handle 3 seconds skew between threads */
1323933707f3Ssthen 	inf.num_rrsets = 0;
1324933707f3Ssthen 	inf.num_msgs = 0;
1325933707f3Ssthen 	inf.num_keys = 0;
1326933707f3Ssthen 	inf.addrlen = len;
1327933707f3Ssthen 	memmove(&inf.addr, &addr, len);
1328933707f3Ssthen 	slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1329933707f3Ssthen 		&inf);
1330933707f3Ssthen 	send_ok(ssl);
1331933707f3Ssthen }
1332933707f3Ssthen 
1333933707f3Ssthen /** flush requestlist */
1334933707f3Ssthen static void
1335933707f3Ssthen do_flush_requestlist(SSL* ssl, struct worker* worker)
1336933707f3Ssthen {
1337933707f3Ssthen 	mesh_delete_all(worker->env.mesh);
1338933707f3Ssthen 	send_ok(ssl);
1339933707f3Ssthen }
1340933707f3Ssthen 
1341933707f3Ssthen /** callback to delete rrsets in a zone */
1342933707f3Ssthen static void
1343933707f3Ssthen zone_del_rrset(struct lruhash_entry* e, void* arg)
1344933707f3Ssthen {
1345933707f3Ssthen 	/* entry is locked */
1346933707f3Ssthen 	struct del_info* inf = (struct del_info*)arg;
1347933707f3Ssthen 	struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1348933707f3Ssthen 	if(dname_subdomain_c(k->rk.dname, inf->name)) {
1349933707f3Ssthen 		struct packed_rrset_data* d =
1350933707f3Ssthen 			(struct packed_rrset_data*)e->data;
1351*a961b961Ssthen 		if(d->ttl > inf->expired) {
1352933707f3Ssthen 			d->ttl = inf->expired;
1353933707f3Ssthen 			inf->num_rrsets++;
1354933707f3Ssthen 		}
1355933707f3Ssthen 	}
1356933707f3Ssthen }
1357933707f3Ssthen 
1358933707f3Ssthen /** callback to delete messages in a zone */
1359933707f3Ssthen static void
1360933707f3Ssthen zone_del_msg(struct lruhash_entry* e, void* arg)
1361933707f3Ssthen {
1362933707f3Ssthen 	/* entry is locked */
1363933707f3Ssthen 	struct del_info* inf = (struct del_info*)arg;
1364933707f3Ssthen 	struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1365933707f3Ssthen 	if(dname_subdomain_c(k->key.qname, inf->name)) {
1366933707f3Ssthen 		struct reply_info* d = (struct reply_info*)e->data;
1367*a961b961Ssthen 		if(d->ttl > inf->expired) {
1368933707f3Ssthen 			d->ttl = inf->expired;
1369933707f3Ssthen 			inf->num_msgs++;
1370933707f3Ssthen 		}
1371933707f3Ssthen 	}
1372933707f3Ssthen }
1373933707f3Ssthen 
1374933707f3Ssthen /** callback to delete keys in zone */
1375933707f3Ssthen static void
1376933707f3Ssthen zone_del_kcache(struct lruhash_entry* e, void* arg)
1377933707f3Ssthen {
1378933707f3Ssthen 	/* entry is locked */
1379933707f3Ssthen 	struct del_info* inf = (struct del_info*)arg;
1380933707f3Ssthen 	struct key_entry_key* k = (struct key_entry_key*)e->key;
1381933707f3Ssthen 	if(dname_subdomain_c(k->name, inf->name)) {
1382933707f3Ssthen 		struct key_entry_data* d = (struct key_entry_data*)e->data;
1383*a961b961Ssthen 		if(d->ttl > inf->expired) {
1384933707f3Ssthen 			d->ttl = inf->expired;
1385933707f3Ssthen 			inf->num_keys++;
1386933707f3Ssthen 		}
1387933707f3Ssthen 	}
1388933707f3Ssthen }
1389933707f3Ssthen 
1390933707f3Ssthen /** remove all rrsets and keys from zone from cache */
1391933707f3Ssthen static void
1392933707f3Ssthen do_flush_zone(SSL* ssl, struct worker* worker, char* arg)
1393933707f3Ssthen {
1394933707f3Ssthen 	uint8_t* nm;
1395933707f3Ssthen 	int nmlabs;
1396933707f3Ssthen 	size_t nmlen;
1397933707f3Ssthen 	struct del_info inf;
1398933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1399933707f3Ssthen 		return;
1400933707f3Ssthen 	/* delete all RRs and key entries from zone */
1401933707f3Ssthen 	/* what we do is to set them all expired */
1402933707f3Ssthen 	inf.worker = worker;
1403933707f3Ssthen 	inf.name = nm;
1404933707f3Ssthen 	inf.len = nmlen;
1405933707f3Ssthen 	inf.labs = nmlabs;
1406933707f3Ssthen 	inf.expired = *worker->env.now;
1407933707f3Ssthen 	inf.expired -= 3; /* handle 3 seconds skew between threads */
1408933707f3Ssthen 	inf.num_rrsets = 0;
1409933707f3Ssthen 	inf.num_msgs = 0;
1410933707f3Ssthen 	inf.num_keys = 0;
1411933707f3Ssthen 	slabhash_traverse(&worker->env.rrset_cache->table, 1,
1412933707f3Ssthen 		&zone_del_rrset, &inf);
1413933707f3Ssthen 
1414933707f3Ssthen 	slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1415933707f3Ssthen 
1416933707f3Ssthen 	/* and validator cache */
1417933707f3Ssthen 	if(worker->env.key_cache) {
1418933707f3Ssthen 		slabhash_traverse(worker->env.key_cache->slab, 1,
1419933707f3Ssthen 			&zone_del_kcache, &inf);
1420933707f3Ssthen 	}
1421933707f3Ssthen 
1422933707f3Ssthen 	free(nm);
1423933707f3Ssthen 
1424e10d3884Sbrad 	(void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1425e10d3884Sbrad 		"and %lu key entries\n", (unsigned long)inf.num_rrsets,
1426e10d3884Sbrad 		(unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1427933707f3Ssthen }
1428933707f3Ssthen 
1429cebdf579Ssthen /** callback to delete bogus rrsets */
1430cebdf579Ssthen static void
1431cebdf579Ssthen bogus_del_rrset(struct lruhash_entry* e, void* arg)
1432cebdf579Ssthen {
1433cebdf579Ssthen 	/* entry is locked */
1434cebdf579Ssthen 	struct del_info* inf = (struct del_info*)arg;
1435cebdf579Ssthen 	struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1436cebdf579Ssthen 	if(d->security == sec_status_bogus) {
1437cebdf579Ssthen 		d->ttl = inf->expired;
1438cebdf579Ssthen 		inf->num_rrsets++;
1439cebdf579Ssthen 	}
1440cebdf579Ssthen }
1441cebdf579Ssthen 
1442cebdf579Ssthen /** callback to delete bogus messages */
1443cebdf579Ssthen static void
1444cebdf579Ssthen bogus_del_msg(struct lruhash_entry* e, void* arg)
1445cebdf579Ssthen {
1446cebdf579Ssthen 	/* entry is locked */
1447cebdf579Ssthen 	struct del_info* inf = (struct del_info*)arg;
1448cebdf579Ssthen 	struct reply_info* d = (struct reply_info*)e->data;
1449cebdf579Ssthen 	if(d->security == sec_status_bogus) {
1450cebdf579Ssthen 		d->ttl = inf->expired;
1451cebdf579Ssthen 		inf->num_msgs++;
1452cebdf579Ssthen 	}
1453cebdf579Ssthen }
1454cebdf579Ssthen 
1455cebdf579Ssthen /** callback to delete bogus keys */
1456cebdf579Ssthen static void
1457cebdf579Ssthen bogus_del_kcache(struct lruhash_entry* e, void* arg)
1458cebdf579Ssthen {
1459cebdf579Ssthen 	/* entry is locked */
1460cebdf579Ssthen 	struct del_info* inf = (struct del_info*)arg;
1461cebdf579Ssthen 	struct key_entry_data* d = (struct key_entry_data*)e->data;
1462cebdf579Ssthen 	if(d->isbad) {
1463cebdf579Ssthen 		d->ttl = inf->expired;
1464cebdf579Ssthen 		inf->num_keys++;
1465cebdf579Ssthen 	}
1466cebdf579Ssthen }
1467cebdf579Ssthen 
1468e10d3884Sbrad /** remove all bogus rrsets, msgs and keys from cache */
1469cebdf579Ssthen static void
1470cebdf579Ssthen do_flush_bogus(SSL* ssl, struct worker* worker)
1471cebdf579Ssthen {
1472cebdf579Ssthen 	struct del_info inf;
1473cebdf579Ssthen 	/* what we do is to set them all expired */
1474cebdf579Ssthen 	inf.worker = worker;
1475cebdf579Ssthen 	inf.expired = *worker->env.now;
1476cebdf579Ssthen 	inf.expired -= 3; /* handle 3 seconds skew between threads */
1477cebdf579Ssthen 	inf.num_rrsets = 0;
1478cebdf579Ssthen 	inf.num_msgs = 0;
1479cebdf579Ssthen 	inf.num_keys = 0;
1480cebdf579Ssthen 	slabhash_traverse(&worker->env.rrset_cache->table, 1,
1481cebdf579Ssthen 		&bogus_del_rrset, &inf);
1482cebdf579Ssthen 
1483cebdf579Ssthen 	slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1484cebdf579Ssthen 
1485cebdf579Ssthen 	/* and validator cache */
1486cebdf579Ssthen 	if(worker->env.key_cache) {
1487cebdf579Ssthen 		slabhash_traverse(worker->env.key_cache->slab, 1,
1488cebdf579Ssthen 			&bogus_del_kcache, &inf);
1489cebdf579Ssthen 	}
1490cebdf579Ssthen 
1491e10d3884Sbrad 	(void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1492e10d3884Sbrad 		"and %lu key entries\n", (unsigned long)inf.num_rrsets,
1493e10d3884Sbrad 		(unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1494e10d3884Sbrad }
1495e10d3884Sbrad 
1496e10d3884Sbrad /** callback to delete negative and servfail rrsets */
1497e10d3884Sbrad static void
1498e10d3884Sbrad negative_del_rrset(struct lruhash_entry* e, void* arg)
1499e10d3884Sbrad {
1500e10d3884Sbrad 	/* entry is locked */
1501e10d3884Sbrad 	struct del_info* inf = (struct del_info*)arg;
1502e10d3884Sbrad 	struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1503e10d3884Sbrad 	struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1504e10d3884Sbrad 	/* delete the parentside negative cache rrsets,
1505e10d3884Sbrad 	 * these are namerserver rrsets that failed lookup, rdata empty */
1506e10d3884Sbrad 	if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 &&
1507e10d3884Sbrad 		d->rrsig_count == 0 && d->rr_len[0] == 0) {
1508e10d3884Sbrad 		d->ttl = inf->expired;
1509e10d3884Sbrad 		inf->num_rrsets++;
1510e10d3884Sbrad 	}
1511e10d3884Sbrad }
1512e10d3884Sbrad 
1513e10d3884Sbrad /** callback to delete negative and servfail messages */
1514e10d3884Sbrad static void
1515e10d3884Sbrad negative_del_msg(struct lruhash_entry* e, void* arg)
1516e10d3884Sbrad {
1517e10d3884Sbrad 	/* entry is locked */
1518e10d3884Sbrad 	struct del_info* inf = (struct del_info*)arg;
1519e10d3884Sbrad 	struct reply_info* d = (struct reply_info*)e->data;
1520e10d3884Sbrad 	/* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1521e10d3884Sbrad 	 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1522e10d3884Sbrad 	if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) {
1523e10d3884Sbrad 		d->ttl = inf->expired;
1524e10d3884Sbrad 		inf->num_msgs++;
1525e10d3884Sbrad 	}
1526e10d3884Sbrad }
1527e10d3884Sbrad 
1528e10d3884Sbrad /** callback to delete negative key entries */
1529e10d3884Sbrad static void
1530e10d3884Sbrad negative_del_kcache(struct lruhash_entry* e, void* arg)
1531e10d3884Sbrad {
1532e10d3884Sbrad 	/* entry is locked */
1533e10d3884Sbrad 	struct del_info* inf = (struct del_info*)arg;
1534e10d3884Sbrad 	struct key_entry_data* d = (struct key_entry_data*)e->data;
1535e10d3884Sbrad 	/* could be bad because of lookup failure on the DS, DNSKEY, which
1536e10d3884Sbrad 	 * was nxdomain or servfail, and thus a result of negative lookups */
1537e10d3884Sbrad 	if(d->isbad) {
1538e10d3884Sbrad 		d->ttl = inf->expired;
1539e10d3884Sbrad 		inf->num_keys++;
1540e10d3884Sbrad 	}
1541e10d3884Sbrad }
1542e10d3884Sbrad 
1543e10d3884Sbrad /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1544e10d3884Sbrad static void
1545e10d3884Sbrad do_flush_negative(SSL* ssl, struct worker* worker)
1546e10d3884Sbrad {
1547e10d3884Sbrad 	struct del_info inf;
1548e10d3884Sbrad 	/* what we do is to set them all expired */
1549e10d3884Sbrad 	inf.worker = worker;
1550e10d3884Sbrad 	inf.expired = *worker->env.now;
1551e10d3884Sbrad 	inf.expired -= 3; /* handle 3 seconds skew between threads */
1552e10d3884Sbrad 	inf.num_rrsets = 0;
1553e10d3884Sbrad 	inf.num_msgs = 0;
1554e10d3884Sbrad 	inf.num_keys = 0;
1555e10d3884Sbrad 	slabhash_traverse(&worker->env.rrset_cache->table, 1,
1556e10d3884Sbrad 		&negative_del_rrset, &inf);
1557e10d3884Sbrad 
1558e10d3884Sbrad 	slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf);
1559e10d3884Sbrad 
1560e10d3884Sbrad 	/* and validator cache */
1561e10d3884Sbrad 	if(worker->env.key_cache) {
1562e10d3884Sbrad 		slabhash_traverse(worker->env.key_cache->slab, 1,
1563e10d3884Sbrad 			&negative_del_kcache, &inf);
1564e10d3884Sbrad 	}
1565e10d3884Sbrad 
1566e10d3884Sbrad 	(void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1567e10d3884Sbrad 		"and %lu key entries\n", (unsigned long)inf.num_rrsets,
1568e10d3884Sbrad 		(unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1569cebdf579Ssthen }
1570cebdf579Ssthen 
1571933707f3Ssthen /** remove name rrset from cache */
1572933707f3Ssthen static void
1573933707f3Ssthen do_flush_name(SSL* ssl, struct worker* w, char* arg)
1574933707f3Ssthen {
1575933707f3Ssthen 	uint8_t* nm;
1576933707f3Ssthen 	int nmlabs;
1577933707f3Ssthen 	size_t nmlen;
1578933707f3Ssthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1579933707f3Ssthen 		return;
1580933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1581933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1582933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1583933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1584933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1585933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1586933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1587933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1588933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1589933707f3Ssthen 	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1590933707f3Ssthen 
1591933707f3Ssthen 	free(nm);
1592933707f3Ssthen 	send_ok(ssl);
1593933707f3Ssthen }
1594933707f3Ssthen 
1595933707f3Ssthen /** printout a delegation point info */
1596933707f3Ssthen static int
1597e10d3884Sbrad ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1598933707f3Ssthen 	struct delegpt* dp)
1599933707f3Ssthen {
1600933707f3Ssthen 	char buf[257];
1601933707f3Ssthen 	struct delegpt_ns* ns;
1602933707f3Ssthen 	struct delegpt_addr* a;
1603933707f3Ssthen 	int f = 0;
1604933707f3Ssthen 	if(str) { /* print header for forward, stub */
16050b68ff31Ssthen 		char* c = sldns_wire2str_class(dclass);
1606933707f3Ssthen 		dname_str(nm, buf);
1607e10d3884Sbrad 		if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) {
1608933707f3Ssthen 			free(c);
1609933707f3Ssthen 			return 0;
1610933707f3Ssthen 		}
1611933707f3Ssthen 		free(c);
1612933707f3Ssthen 	}
1613933707f3Ssthen 	for(ns = dp->nslist; ns; ns = ns->next) {
1614933707f3Ssthen 		dname_str(ns->name, buf);
1615933707f3Ssthen 		if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1616933707f3Ssthen 			return 0;
1617933707f3Ssthen 		f = 1;
1618933707f3Ssthen 	}
1619933707f3Ssthen 	for(a = dp->target_list; a; a = a->next_target) {
1620933707f3Ssthen 		addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1621933707f3Ssthen 		if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1622933707f3Ssthen 			return 0;
1623933707f3Ssthen 		f = 1;
1624933707f3Ssthen 	}
1625933707f3Ssthen 	return ssl_printf(ssl, "\n");
1626933707f3Ssthen }
1627933707f3Ssthen 
1628933707f3Ssthen 
1629933707f3Ssthen /** print root forwards */
1630933707f3Ssthen static int
1631933707f3Ssthen print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root)
1632933707f3Ssthen {
1633933707f3Ssthen 	struct delegpt* dp;
1634933707f3Ssthen 	dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1635933707f3Ssthen 	if(!dp)
1636933707f3Ssthen 		return ssl_printf(ssl, "off (using root hints)\n");
1637933707f3Ssthen 	/* if dp is returned it must be the root */
1638933707f3Ssthen 	log_assert(query_dname_compare(dp->name, root)==0);
1639933707f3Ssthen 	return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1640933707f3Ssthen }
1641933707f3Ssthen 
1642933707f3Ssthen /** parse args into delegpt */
1643933707f3Ssthen static struct delegpt*
1644163a4143Ssthen parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names)
1645933707f3Ssthen {
1646933707f3Ssthen 	/* parse args and add in */
1647933707f3Ssthen 	char* p = args;
1648933707f3Ssthen 	char* todo;
1649163a4143Ssthen 	struct delegpt* dp = delegpt_create_mlc(nm);
1650933707f3Ssthen 	struct sockaddr_storage addr;
1651933707f3Ssthen 	socklen_t addrlen;
1652163a4143Ssthen 	if(!dp) {
1653933707f3Ssthen 		(void)ssl_printf(ssl, "error out of memory\n");
1654933707f3Ssthen 		return NULL;
1655933707f3Ssthen 	}
1656933707f3Ssthen 	while(p) {
1657933707f3Ssthen 		todo = p;
1658933707f3Ssthen 		p = strchr(p, ' '); /* find next spot, if any */
1659933707f3Ssthen 		if(p) {
1660933707f3Ssthen 			*p++ = 0;	/* end this spot */
1661933707f3Ssthen 			p = skipwhite(p); /* position at next spot */
1662933707f3Ssthen 		}
1663933707f3Ssthen 		/* parse address */
1664933707f3Ssthen 		if(!extstrtoaddr(todo, &addr, &addrlen)) {
1665163a4143Ssthen 			if(allow_names) {
1666163a4143Ssthen 				uint8_t* n = NULL;
1667163a4143Ssthen 				size_t ln;
1668163a4143Ssthen 				int lb;
1669163a4143Ssthen 				if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
1670163a4143Ssthen 					(void)ssl_printf(ssl, "error cannot "
1671163a4143Ssthen 						"parse IP address or name "
1672163a4143Ssthen 						"'%s'\n", todo);
1673163a4143Ssthen 					delegpt_free_mlc(dp);
1674933707f3Ssthen 					return NULL;
1675933707f3Ssthen 				}
1676163a4143Ssthen 				if(!delegpt_add_ns_mlc(dp, n, 0)) {
1677933707f3Ssthen 					(void)ssl_printf(ssl, "error out of memory\n");
1678cebdf579Ssthen 					free(n);
1679163a4143Ssthen 					delegpt_free_mlc(dp);
1680933707f3Ssthen 					return NULL;
1681933707f3Ssthen 				}
1682163a4143Ssthen 				free(n);
1683163a4143Ssthen 
1684163a4143Ssthen 			} else {
1685163a4143Ssthen 				(void)ssl_printf(ssl, "error cannot parse"
1686163a4143Ssthen 					" IP address '%s'\n", todo);
1687163a4143Ssthen 				delegpt_free_mlc(dp);
1688163a4143Ssthen 				return NULL;
1689163a4143Ssthen 			}
1690163a4143Ssthen 		} else {
1691163a4143Ssthen 			/* add address */
1692163a4143Ssthen 			if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) {
1693163a4143Ssthen 				(void)ssl_printf(ssl, "error out of memory\n");
1694163a4143Ssthen 				delegpt_free_mlc(dp);
1695163a4143Ssthen 				return NULL;
1696163a4143Ssthen 			}
1697163a4143Ssthen 		}
1698933707f3Ssthen 	}
1699*a961b961Ssthen 	dp->has_parent_side_NS = 1;
1700933707f3Ssthen 	return dp;
1701933707f3Ssthen }
1702933707f3Ssthen 
1703933707f3Ssthen /** do the status command */
1704933707f3Ssthen static void
1705933707f3Ssthen do_forward(SSL* ssl, struct worker* worker, char* args)
1706933707f3Ssthen {
1707933707f3Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1708933707f3Ssthen 	uint8_t* root = (uint8_t*)"\000";
1709933707f3Ssthen 	if(!fwd) {
1710933707f3Ssthen 		(void)ssl_printf(ssl, "error: structure not allocated\n");
1711933707f3Ssthen 		return;
1712933707f3Ssthen 	}
1713933707f3Ssthen 	if(args == NULL || args[0] == 0) {
1714933707f3Ssthen 		(void)print_root_fwds(ssl, fwd, root);
1715933707f3Ssthen 		return;
1716933707f3Ssthen 	}
1717933707f3Ssthen 	/* set root forwards for this thread. since we are in remote control
1718933707f3Ssthen 	 * the actual mesh is not running, so we can freely edit it. */
1719933707f3Ssthen 	/* delete all the existing queries first */
1720933707f3Ssthen 	mesh_delete_all(worker->env.mesh);
1721933707f3Ssthen 	if(strcmp(args, "off") == 0) {
1722933707f3Ssthen 		forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
1723933707f3Ssthen 	} else {
1724933707f3Ssthen 		struct delegpt* dp;
1725163a4143Ssthen 		if(!(dp = parse_delegpt(ssl, args, root, 0)))
1726933707f3Ssthen 			return;
1727933707f3Ssthen 		if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1728933707f3Ssthen 			(void)ssl_printf(ssl, "error out of memory\n");
1729933707f3Ssthen 			return;
1730933707f3Ssthen 		}
1731933707f3Ssthen 	}
1732933707f3Ssthen 	send_ok(ssl);
1733933707f3Ssthen }
1734933707f3Ssthen 
1735163a4143Ssthen static int
1736163a4143Ssthen parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp,
1737163a4143Ssthen 	int* insecure, int* prime)
1738163a4143Ssthen {
1739163a4143Ssthen 	char* zonename;
1740163a4143Ssthen 	char* rest;
1741163a4143Ssthen 	size_t nmlen;
1742163a4143Ssthen 	int nmlabs;
1743163a4143Ssthen 	/* parse all -x args */
1744163a4143Ssthen 	while(args[0] == '+') {
1745163a4143Ssthen 		if(!find_arg2(ssl, args, &rest))
1746163a4143Ssthen 			return 0;
1747163a4143Ssthen 		while(*(++args) != 0) {
1748163a4143Ssthen 			if(*args == 'i' && insecure)
1749163a4143Ssthen 				*insecure = 1;
1750163a4143Ssthen 			else if(*args == 'p' && prime)
1751163a4143Ssthen 				*prime = 1;
1752163a4143Ssthen 			else {
1753163a4143Ssthen 				(void)ssl_printf(ssl, "error: unknown option %s\n", args);
1754163a4143Ssthen 				return 0;
1755163a4143Ssthen 			}
1756163a4143Ssthen 		}
1757163a4143Ssthen 		args = rest;
1758163a4143Ssthen 	}
1759163a4143Ssthen 	/* parse name */
1760163a4143Ssthen 	if(dp) {
1761163a4143Ssthen 		if(!find_arg2(ssl, args, &rest))
1762163a4143Ssthen 			return 0;
1763163a4143Ssthen 		zonename = args;
1764163a4143Ssthen 		args = rest;
1765163a4143Ssthen 	} else	zonename = args;
1766163a4143Ssthen 	if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
1767163a4143Ssthen 		return 0;
1768163a4143Ssthen 
1769163a4143Ssthen 	/* parse dp */
1770163a4143Ssthen 	if(dp) {
1771163a4143Ssthen 		if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
1772163a4143Ssthen 			free(*nm);
1773163a4143Ssthen 			return 0;
1774163a4143Ssthen 		}
1775163a4143Ssthen 	}
1776163a4143Ssthen 	return 1;
1777163a4143Ssthen }
1778163a4143Ssthen 
1779163a4143Ssthen /** do the forward_add command */
1780163a4143Ssthen static void
1781163a4143Ssthen do_forward_add(SSL* ssl, struct worker* worker, char* args)
1782163a4143Ssthen {
1783163a4143Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1784163a4143Ssthen 	int insecure = 0;
1785163a4143Ssthen 	uint8_t* nm = NULL;
1786163a4143Ssthen 	struct delegpt* dp = NULL;
1787163a4143Ssthen 	if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
1788163a4143Ssthen 		return;
17890b68ff31Ssthen 	if(insecure && worker->env.anchors) {
1790163a4143Ssthen 		if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1791163a4143Ssthen 			nm)) {
1792163a4143Ssthen 			(void)ssl_printf(ssl, "error out of memory\n");
1793163a4143Ssthen 			delegpt_free_mlc(dp);
1794163a4143Ssthen 			free(nm);
1795163a4143Ssthen 			return;
1796163a4143Ssthen 		}
1797163a4143Ssthen 	}
1798163a4143Ssthen 	if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1799163a4143Ssthen 		(void)ssl_printf(ssl, "error out of memory\n");
1800163a4143Ssthen 		free(nm);
1801163a4143Ssthen 		return;
1802163a4143Ssthen 	}
1803163a4143Ssthen 	free(nm);
1804163a4143Ssthen 	send_ok(ssl);
1805163a4143Ssthen }
1806163a4143Ssthen 
1807163a4143Ssthen /** do the forward_remove command */
1808163a4143Ssthen static void
1809163a4143Ssthen do_forward_remove(SSL* ssl, struct worker* worker, char* args)
1810163a4143Ssthen {
1811163a4143Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1812163a4143Ssthen 	int insecure = 0;
1813163a4143Ssthen 	uint8_t* nm = NULL;
1814163a4143Ssthen 	if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1815163a4143Ssthen 		return;
18160b68ff31Ssthen 	if(insecure && worker->env.anchors)
1817163a4143Ssthen 		anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1818163a4143Ssthen 			nm);
1819163a4143Ssthen 	forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
1820163a4143Ssthen 	free(nm);
1821163a4143Ssthen 	send_ok(ssl);
1822163a4143Ssthen }
1823163a4143Ssthen 
1824163a4143Ssthen /** do the stub_add command */
1825163a4143Ssthen static void
1826163a4143Ssthen do_stub_add(SSL* ssl, struct worker* worker, char* args)
1827163a4143Ssthen {
1828163a4143Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1829163a4143Ssthen 	int insecure = 0, prime = 0;
1830163a4143Ssthen 	uint8_t* nm = NULL;
1831163a4143Ssthen 	struct delegpt* dp = NULL;
1832163a4143Ssthen 	if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
1833163a4143Ssthen 		return;
18340b68ff31Ssthen 	if(insecure && worker->env.anchors) {
1835163a4143Ssthen 		if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1836163a4143Ssthen 			nm)) {
1837163a4143Ssthen 			(void)ssl_printf(ssl, "error out of memory\n");
1838163a4143Ssthen 			delegpt_free_mlc(dp);
1839163a4143Ssthen 			free(nm);
1840163a4143Ssthen 			return;
1841163a4143Ssthen 		}
1842163a4143Ssthen 	}
1843163a4143Ssthen 	if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
18440b68ff31Ssthen 		if(insecure && worker->env.anchors)
18450b68ff31Ssthen 			anchors_delete_insecure(worker->env.anchors,
1846163a4143Ssthen 				LDNS_RR_CLASS_IN, nm);
1847163a4143Ssthen 		(void)ssl_printf(ssl, "error out of memory\n");
1848163a4143Ssthen 		delegpt_free_mlc(dp);
1849163a4143Ssthen 		free(nm);
1850163a4143Ssthen 		return;
1851163a4143Ssthen 	}
1852163a4143Ssthen 	if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
1853163a4143Ssthen 		(void)ssl_printf(ssl, "error out of memory\n");
1854163a4143Ssthen 		forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
18550b68ff31Ssthen 		if(insecure && worker->env.anchors)
18560b68ff31Ssthen 			anchors_delete_insecure(worker->env.anchors,
1857163a4143Ssthen 				LDNS_RR_CLASS_IN, nm);
1858163a4143Ssthen 		free(nm);
1859163a4143Ssthen 		return;
1860163a4143Ssthen 	}
1861163a4143Ssthen 	free(nm);
1862163a4143Ssthen 	send_ok(ssl);
1863163a4143Ssthen }
1864163a4143Ssthen 
1865163a4143Ssthen /** do the stub_remove command */
1866163a4143Ssthen static void
1867163a4143Ssthen do_stub_remove(SSL* ssl, struct worker* worker, char* args)
1868163a4143Ssthen {
1869163a4143Ssthen 	struct iter_forwards* fwd = worker->env.fwds;
1870163a4143Ssthen 	int insecure = 0;
1871163a4143Ssthen 	uint8_t* nm = NULL;
1872163a4143Ssthen 	if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1873163a4143Ssthen 		return;
18740b68ff31Ssthen 	if(insecure && worker->env.anchors)
1875163a4143Ssthen 		anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1876163a4143Ssthen 			nm);
1877163a4143Ssthen 	forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1878163a4143Ssthen 	hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
1879163a4143Ssthen 	free(nm);
1880163a4143Ssthen 	send_ok(ssl);
1881163a4143Ssthen }
1882163a4143Ssthen 
1883e9c7b4efSsthen /** do the insecure_add command */
1884e9c7b4efSsthen static void
1885e9c7b4efSsthen do_insecure_add(SSL* ssl, struct worker* worker, char* arg)
1886e9c7b4efSsthen {
1887e9c7b4efSsthen 	size_t nmlen;
1888e9c7b4efSsthen 	int nmlabs;
1889e9c7b4efSsthen 	uint8_t* nm = NULL;
1890e9c7b4efSsthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1891e9c7b4efSsthen 		return;
18920b68ff31Ssthen 	if(worker->env.anchors) {
18930b68ff31Ssthen 		if(!anchors_add_insecure(worker->env.anchors,
18940b68ff31Ssthen 			LDNS_RR_CLASS_IN, nm)) {
1895e9c7b4efSsthen 			(void)ssl_printf(ssl, "error out of memory\n");
1896e9c7b4efSsthen 			free(nm);
1897e9c7b4efSsthen 			return;
1898e9c7b4efSsthen 		}
18990b68ff31Ssthen 	}
1900e9c7b4efSsthen 	free(nm);
1901e9c7b4efSsthen 	send_ok(ssl);
1902e9c7b4efSsthen }
1903e9c7b4efSsthen 
1904e9c7b4efSsthen /** do the insecure_remove command */
1905e9c7b4efSsthen static void
1906e9c7b4efSsthen do_insecure_remove(SSL* ssl, struct worker* worker, char* arg)
1907e9c7b4efSsthen {
1908e9c7b4efSsthen 	size_t nmlen;
1909e9c7b4efSsthen 	int nmlabs;
1910e9c7b4efSsthen 	uint8_t* nm = NULL;
1911e9c7b4efSsthen 	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1912e9c7b4efSsthen 		return;
19130b68ff31Ssthen 	if(worker->env.anchors)
19140b68ff31Ssthen 		anchors_delete_insecure(worker->env.anchors,
19150b68ff31Ssthen 			LDNS_RR_CLASS_IN, nm);
1916e9c7b4efSsthen 	free(nm);
1917e9c7b4efSsthen 	send_ok(ssl);
1918e9c7b4efSsthen }
1919e9c7b4efSsthen 
1920a58bff56Ssthen static void
1921a58bff56Ssthen do_insecure_list(SSL* ssl, struct worker* worker)
1922a58bff56Ssthen {
1923a58bff56Ssthen 	char buf[257];
1924a58bff56Ssthen 	struct trust_anchor* a;
1925a58bff56Ssthen 	if(worker->env.anchors) {
1926a58bff56Ssthen 		RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) {
1927a58bff56Ssthen 			if(a->numDS == 0 && a->numDNSKEY == 0) {
1928a58bff56Ssthen 				dname_str(a->name, buf);
1929a58bff56Ssthen 				ssl_printf(ssl, "%s\n", buf);
1930a58bff56Ssthen 			}
1931a58bff56Ssthen 		}
1932a58bff56Ssthen 	}
1933a58bff56Ssthen }
1934a58bff56Ssthen 
1935933707f3Ssthen /** do the status command */
1936933707f3Ssthen static void
1937933707f3Ssthen do_status(SSL* ssl, struct worker* worker)
1938933707f3Ssthen {
1939933707f3Ssthen 	int i;
1940933707f3Ssthen 	time_t uptime;
1941933707f3Ssthen 	if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
1942933707f3Ssthen 		return;
1943933707f3Ssthen 	if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
1944933707f3Ssthen 		return;
1945933707f3Ssthen 	if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
1946933707f3Ssthen 		return;
1947933707f3Ssthen 	if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
1948933707f3Ssthen 		return;
1949933707f3Ssthen 	for(i=0; i<worker->daemon->mods.num; i++) {
1950933707f3Ssthen 		if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
1951933707f3Ssthen 			return;
1952933707f3Ssthen 	}
1953933707f3Ssthen 	if(!ssl_printf(ssl, " ]\n"))
1954933707f3Ssthen 		return;
1955933707f3Ssthen 	uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
19560b68ff31Ssthen 	if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
1957933707f3Ssthen 		return;
1958e10d3884Sbrad 	if(!ssl_printf(ssl, "options:%s%s\n" ,
1959e10d3884Sbrad 		(worker->daemon->reuseport?" reuseport":""),
1960e10d3884Sbrad 		(worker->daemon->rc->accept_list?" control(ssl)":"")))
1961e10d3884Sbrad 		return;
1962933707f3Ssthen 	if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
1963933707f3Ssthen 		(int)getpid()))
1964933707f3Ssthen 		return;
1965933707f3Ssthen }
1966933707f3Ssthen 
1967933707f3Ssthen /** get age for the mesh state */
1968933707f3Ssthen static void
1969933707f3Ssthen get_mesh_age(struct mesh_state* m, char* buf, size_t len,
1970933707f3Ssthen 	struct module_env* env)
1971933707f3Ssthen {
1972933707f3Ssthen 	if(m->reply_list) {
1973933707f3Ssthen 		struct timeval d;
1974933707f3Ssthen 		struct mesh_reply* r = m->reply_list;
1975933707f3Ssthen 		/* last reply is the oldest */
1976933707f3Ssthen 		while(r && r->next)
1977933707f3Ssthen 			r = r->next;
1978933707f3Ssthen 		timeval_subtract(&d, env->now_tv, &r->start_time);
19790b68ff31Ssthen 		snprintf(buf, len, ARG_LL "d.%6.6d",
19800b68ff31Ssthen 			(long long)d.tv_sec, (int)d.tv_usec);
1981933707f3Ssthen 	} else {
1982933707f3Ssthen 		snprintf(buf, len, "-");
1983933707f3Ssthen 	}
1984933707f3Ssthen }
1985933707f3Ssthen 
1986933707f3Ssthen /** get status of a mesh state */
1987933707f3Ssthen static void
1988933707f3Ssthen get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
1989933707f3Ssthen 	char* buf, size_t len)
1990933707f3Ssthen {
1991933707f3Ssthen 	enum module_ext_state s = m->s.ext_state[m->s.curmod];
1992933707f3Ssthen 	const char *modname = mesh->mods.mod[m->s.curmod]->name;
1993933707f3Ssthen 	size_t l;
1994933707f3Ssthen 	if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
1995933707f3Ssthen 		m->s.minfo[m->s.curmod]) {
1996933707f3Ssthen 		/* break into iterator to find out who its waiting for */
1997933707f3Ssthen 		struct iter_qstate* qstate = (struct iter_qstate*)
1998933707f3Ssthen 			m->s.minfo[m->s.curmod];
1999933707f3Ssthen 		struct outbound_list* ol = &qstate->outlist;
2000933707f3Ssthen 		struct outbound_entry* e;
2001933707f3Ssthen 		snprintf(buf, len, "%s wait for", modname);
2002933707f3Ssthen 		l = strlen(buf);
2003933707f3Ssthen 		buf += l; len -= l;
2004933707f3Ssthen 		if(ol->first == NULL)
2005933707f3Ssthen 			snprintf(buf, len, " (empty_list)");
2006933707f3Ssthen 		for(e = ol->first; e; e = e->next) {
2007933707f3Ssthen 			snprintf(buf, len, " ");
2008933707f3Ssthen 			l = strlen(buf);
2009933707f3Ssthen 			buf += l; len -= l;
2010933707f3Ssthen 			addr_to_str(&e->qsent->addr, e->qsent->addrlen,
2011933707f3Ssthen 				buf, len);
2012933707f3Ssthen 			l = strlen(buf);
2013933707f3Ssthen 			buf += l; len -= l;
2014933707f3Ssthen 		}
2015933707f3Ssthen 	} else if(s == module_wait_subquery) {
2016933707f3Ssthen 		/* look in subs from mesh state to see what */
2017933707f3Ssthen 		char nm[257];
2018933707f3Ssthen 		struct mesh_state_ref* sub;
2019933707f3Ssthen 		snprintf(buf, len, "%s wants", modname);
2020933707f3Ssthen 		l = strlen(buf);
2021933707f3Ssthen 		buf += l; len -= l;
2022933707f3Ssthen 		if(m->sub_set.count == 0)
2023933707f3Ssthen 			snprintf(buf, len, " (empty_list)");
2024933707f3Ssthen 		RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
20250b68ff31Ssthen 			char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
20260b68ff31Ssthen 			char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
2027933707f3Ssthen 			dname_str(sub->s->s.qinfo.qname, nm);
20280b68ff31Ssthen 			snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
20290b68ff31Ssthen 				(c?c:"CLASS??"), nm);
2030933707f3Ssthen 			l = strlen(buf);
2031933707f3Ssthen 			buf += l; len -= l;
2032933707f3Ssthen 			free(t);
2033933707f3Ssthen 			free(c);
2034933707f3Ssthen 		}
2035933707f3Ssthen 	} else {
2036933707f3Ssthen 		snprintf(buf, len, "%s is %s", modname, strextstate(s));
2037933707f3Ssthen 	}
2038933707f3Ssthen }
2039933707f3Ssthen 
2040933707f3Ssthen /** do the dump_requestlist command */
2041933707f3Ssthen static void
2042933707f3Ssthen do_dump_requestlist(SSL* ssl, struct worker* worker)
2043933707f3Ssthen {
2044933707f3Ssthen 	struct mesh_area* mesh;
2045933707f3Ssthen 	struct mesh_state* m;
2046933707f3Ssthen 	int num = 0;
2047933707f3Ssthen 	char buf[257];
2048933707f3Ssthen 	char timebuf[32];
2049933707f3Ssthen 	char statbuf[10240];
2050933707f3Ssthen 	if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
2051933707f3Ssthen 		return;
2052933707f3Ssthen 	if(!ssl_printf(ssl, "#   type cl name    seconds    module status\n"))
2053933707f3Ssthen 		return;
2054933707f3Ssthen 	/* show worker mesh contents */
2055933707f3Ssthen 	mesh = worker->env.mesh;
2056933707f3Ssthen 	if(!mesh) return;
2057933707f3Ssthen 	RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
20580b68ff31Ssthen 		char* t = sldns_wire2str_type(m->s.qinfo.qtype);
20590b68ff31Ssthen 		char* c = sldns_wire2str_class(m->s.qinfo.qclass);
2060933707f3Ssthen 		dname_str(m->s.qinfo.qname, buf);
2061933707f3Ssthen 		get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
2062933707f3Ssthen 		get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
2063933707f3Ssthen 		if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
20640b68ff31Ssthen 			num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
20650b68ff31Ssthen 			statbuf)) {
2066933707f3Ssthen 			free(t);
2067933707f3Ssthen 			free(c);
2068933707f3Ssthen 			return;
2069933707f3Ssthen 		}
2070933707f3Ssthen 		num++;
2071933707f3Ssthen 		free(t);
2072933707f3Ssthen 		free(c);
2073933707f3Ssthen 	}
2074933707f3Ssthen }
2075933707f3Ssthen 
2076933707f3Ssthen /** structure for argument data for dump infra host */
2077933707f3Ssthen struct infra_arg {
2078933707f3Ssthen 	/** the infra cache */
2079933707f3Ssthen 	struct infra_cache* infra;
2080933707f3Ssthen 	/** the SSL connection */
2081933707f3Ssthen 	SSL* ssl;
2082933707f3Ssthen 	/** the time now */
2083e9c7b4efSsthen 	time_t now;
2084e10d3884Sbrad 	/** ssl failure? stop writing and skip the rest.  If the tcp
2085e10d3884Sbrad 	 * connection is broken, and writes fail, we then stop writing. */
2086e10d3884Sbrad 	int ssl_failed;
2087933707f3Ssthen };
2088933707f3Ssthen 
2089933707f3Ssthen /** callback for every host element in the infra cache */
2090933707f3Ssthen static void
2091933707f3Ssthen dump_infra_host(struct lruhash_entry* e, void* arg)
2092933707f3Ssthen {
2093933707f3Ssthen 	struct infra_arg* a = (struct infra_arg*)arg;
2094933707f3Ssthen 	struct infra_key* k = (struct infra_key*)e->key;
2095933707f3Ssthen 	struct infra_data* d = (struct infra_data*)e->data;
2096933707f3Ssthen 	char ip_str[1024];
2097933707f3Ssthen 	char name[257];
2098e10d3884Sbrad 	if(a->ssl_failed)
2099e10d3884Sbrad 		return;
2100933707f3Ssthen 	addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
2101933707f3Ssthen 	dname_str(k->zonename, name);
2102933707f3Ssthen 	/* skip expired stuff (only backed off) */
2103933707f3Ssthen 	if(d->ttl < a->now) {
2104933707f3Ssthen 		if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
2105933707f3Ssthen 			if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
2106e10d3884Sbrad 				name, d->rtt.rto))  {
2107e10d3884Sbrad 				a->ssl_failed = 1;
2108e10d3884Sbrad 				return;
2109e10d3884Sbrad 			}
2110933707f3Ssthen 		}
2111933707f3Ssthen 		return;
2112933707f3Ssthen 	}
2113e10d3884Sbrad 	if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
2114163a4143Ssthen 		"tA %d tAAAA %d tother %d "
2115933707f3Ssthen 		"ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
2116e10d3884Sbrad 		"other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now),
2117933707f3Ssthen 		d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
2118163a4143Ssthen 		d->timeout_A, d->timeout_AAAA, d->timeout_other,
2119933707f3Ssthen 		(int)d->edns_lame_known, (int)d->edns_version,
2120a58bff56Ssthen 		(int)(a->now<d->probedelay?(d->probedelay - a->now):0),
2121933707f3Ssthen 		(int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
2122e10d3884Sbrad 		(int)d->lame_other)) {
2123e10d3884Sbrad 		a->ssl_failed = 1;
2124933707f3Ssthen 		return;
2125933707f3Ssthen 	}
2126e10d3884Sbrad }
2127933707f3Ssthen 
2128933707f3Ssthen /** do the dump_infra command */
2129933707f3Ssthen static void
2130933707f3Ssthen do_dump_infra(SSL* ssl, struct worker* worker)
2131933707f3Ssthen {
2132933707f3Ssthen 	struct infra_arg arg;
2133933707f3Ssthen 	arg.infra = worker->env.infra_cache;
2134933707f3Ssthen 	arg.ssl = ssl;
2135933707f3Ssthen 	arg.now = *worker->env.now;
2136e10d3884Sbrad 	arg.ssl_failed = 0;
2137933707f3Ssthen 	slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
2138933707f3Ssthen }
2139933707f3Ssthen 
2140933707f3Ssthen /** do the log_reopen command */
2141933707f3Ssthen static void
2142933707f3Ssthen do_log_reopen(SSL* ssl, struct worker* worker)
2143933707f3Ssthen {
2144933707f3Ssthen 	struct config_file* cfg = worker->env.cfg;
2145933707f3Ssthen 	send_ok(ssl);
2146933707f3Ssthen 	log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
2147933707f3Ssthen }
2148933707f3Ssthen 
2149933707f3Ssthen /** do the set_option command */
2150933707f3Ssthen static void
2151933707f3Ssthen do_set_option(SSL* ssl, struct worker* worker, char* arg)
2152933707f3Ssthen {
2153933707f3Ssthen 	char* arg2;
2154933707f3Ssthen 	if(!find_arg2(ssl, arg, &arg2))
2155933707f3Ssthen 		return;
2156933707f3Ssthen 	if(!config_set_option(worker->env.cfg, arg, arg2)) {
2157933707f3Ssthen 		(void)ssl_printf(ssl, "error setting option\n");
2158933707f3Ssthen 		return;
2159933707f3Ssthen 	}
2160933707f3Ssthen 	send_ok(ssl);
2161933707f3Ssthen }
2162933707f3Ssthen 
2163933707f3Ssthen /* routine to printout option values over SSL */
2164933707f3Ssthen void remote_get_opt_ssl(char* line, void* arg)
2165933707f3Ssthen {
2166933707f3Ssthen 	SSL* ssl = (SSL*)arg;
2167933707f3Ssthen 	(void)ssl_printf(ssl, "%s\n", line);
2168933707f3Ssthen }
2169933707f3Ssthen 
2170933707f3Ssthen /** do the get_option command */
2171933707f3Ssthen static void
2172933707f3Ssthen do_get_option(SSL* ssl, struct worker* worker, char* arg)
2173933707f3Ssthen {
2174933707f3Ssthen 	int r;
2175933707f3Ssthen 	r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
2176933707f3Ssthen 	if(!r) {
2177933707f3Ssthen 		(void)ssl_printf(ssl, "error unknown option\n");
2178933707f3Ssthen 		return;
2179933707f3Ssthen 	}
2180933707f3Ssthen }
2181933707f3Ssthen 
2182933707f3Ssthen /** do the list_forwards command */
2183933707f3Ssthen static void
2184933707f3Ssthen do_list_forwards(SSL* ssl, struct worker* worker)
2185933707f3Ssthen {
2186933707f3Ssthen 	/* since its a per-worker structure no locks needed */
2187933707f3Ssthen 	struct iter_forwards* fwds = worker->env.fwds;
2188933707f3Ssthen 	struct iter_forward_zone* z;
2189e10d3884Sbrad 	struct trust_anchor* a;
2190e10d3884Sbrad 	int insecure;
2191933707f3Ssthen 	RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
2192933707f3Ssthen 		if(!z->dp) continue; /* skip empty marker for stub */
2193e10d3884Sbrad 
2194e10d3884Sbrad 		/* see if it is insecure */
2195e10d3884Sbrad 		insecure = 0;
2196e10d3884Sbrad 		if(worker->env.anchors &&
2197e10d3884Sbrad 			(a=anchor_find(worker->env.anchors, z->name,
2198e10d3884Sbrad 			z->namelabs, z->namelen,  z->dclass))) {
2199e10d3884Sbrad 			if(!a->keylist && !a->numDS && !a->numDNSKEY)
2200e10d3884Sbrad 				insecure = 1;
2201e10d3884Sbrad 			lock_basic_unlock(&a->lock);
2202e10d3884Sbrad 		}
2203e10d3884Sbrad 
2204e10d3884Sbrad 		if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"),
2205e10d3884Sbrad 			z->name, z->dclass, z->dp))
2206933707f3Ssthen 			return;
2207933707f3Ssthen 	}
2208933707f3Ssthen }
2209933707f3Ssthen 
2210933707f3Ssthen /** do the list_stubs command */
2211933707f3Ssthen static void
2212933707f3Ssthen do_list_stubs(SSL* ssl, struct worker* worker)
2213933707f3Ssthen {
2214933707f3Ssthen 	struct iter_hints_stub* z;
2215e10d3884Sbrad 	struct trust_anchor* a;
2216e10d3884Sbrad 	int insecure;
2217e10d3884Sbrad 	char str[32];
2218163a4143Ssthen 	RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
2219e10d3884Sbrad 
2220e10d3884Sbrad 		/* see if it is insecure */
2221e10d3884Sbrad 		insecure = 0;
2222e10d3884Sbrad 		if(worker->env.anchors &&
2223e10d3884Sbrad 			(a=anchor_find(worker->env.anchors, z->node.name,
2224e10d3884Sbrad 			z->node.labs, z->node.len,  z->node.dclass))) {
2225e10d3884Sbrad 			if(!a->keylist && !a->numDS && !a->numDNSKEY)
2226e10d3884Sbrad 				insecure = 1;
2227e10d3884Sbrad 			lock_basic_unlock(&a->lock);
2228e10d3884Sbrad 		}
2229e10d3884Sbrad 
2230e10d3884Sbrad 		snprintf(str, sizeof(str), "stub %sprime%s",
2231e10d3884Sbrad 			(z->noprime?"no":""), (insecure?" +i":""));
2232e10d3884Sbrad 		if(!ssl_print_name_dp(ssl, str, z->node.name,
2233933707f3Ssthen 			z->node.dclass, z->dp))
2234933707f3Ssthen 			return;
2235933707f3Ssthen 	}
2236933707f3Ssthen }
2237933707f3Ssthen 
2238933707f3Ssthen /** do the list_local_zones command */
2239933707f3Ssthen static void
2240933707f3Ssthen do_list_local_zones(SSL* ssl, struct worker* worker)
2241933707f3Ssthen {
2242933707f3Ssthen 	struct local_zones* zones = worker->daemon->local_zones;
2243933707f3Ssthen 	struct local_zone* z;
2244933707f3Ssthen 	char buf[257];
22450b68ff31Ssthen 	lock_rw_rdlock(&zones->lock);
2246933707f3Ssthen 	RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2247933707f3Ssthen 		lock_rw_rdlock(&z->lock);
2248933707f3Ssthen 		dname_str(z->name, buf);
2249e10d3884Sbrad 		if(!ssl_printf(ssl, "%s %s\n", buf,
2250e10d3884Sbrad 			local_zone_type2str(z->type))) {
2251e10d3884Sbrad 			/* failure to print */
2252e10d3884Sbrad 			lock_rw_unlock(&z->lock);
2253e10d3884Sbrad 			lock_rw_unlock(&zones->lock);
2254e10d3884Sbrad 			return;
2255e10d3884Sbrad 		}
2256933707f3Ssthen 		lock_rw_unlock(&z->lock);
2257933707f3Ssthen 	}
22580b68ff31Ssthen 	lock_rw_unlock(&zones->lock);
2259933707f3Ssthen }
2260933707f3Ssthen 
2261933707f3Ssthen /** do the list_local_data command */
2262933707f3Ssthen static void
2263933707f3Ssthen do_list_local_data(SSL* ssl, struct worker* worker)
2264933707f3Ssthen {
2265933707f3Ssthen 	struct local_zones* zones = worker->daemon->local_zones;
2266933707f3Ssthen 	struct local_zone* z;
2267933707f3Ssthen 	struct local_data* d;
2268933707f3Ssthen 	struct local_rrset* p;
22690b68ff31Ssthen 	char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
22700b68ff31Ssthen 	size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
22710b68ff31Ssthen 	lock_rw_rdlock(&zones->lock);
2272933707f3Ssthen 	RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2273933707f3Ssthen 		lock_rw_rdlock(&z->lock);
2274933707f3Ssthen 		RBTREE_FOR(d, struct local_data*, &z->data) {
2275933707f3Ssthen 			for(p = d->rrsets; p; p = p->next) {
22760b68ff31Ssthen 				struct packed_rrset_data* d =
22770b68ff31Ssthen 					(struct packed_rrset_data*)p->rrset->entry.data;
22780b68ff31Ssthen 				size_t i;
22790b68ff31Ssthen 				for(i=0; i<d->count + d->rrsig_count; i++) {
22800b68ff31Ssthen 					if(!packed_rr_to_string(p->rrset, i,
22810b68ff31Ssthen 						0, s, slen)) {
2282*a961b961Ssthen 						if(!ssl_printf(ssl, "BADRR\n")) {
2283*a961b961Ssthen 							lock_rw_unlock(&z->lock);
2284*a961b961Ssthen 							lock_rw_unlock(&zones->lock);
22850b68ff31Ssthen 							return;
22860b68ff31Ssthen 						}
2287*a961b961Ssthen 					}
2288*a961b961Ssthen 				        if(!ssl_printf(ssl, "%s\n", s)) {
2289*a961b961Ssthen 						lock_rw_unlock(&z->lock);
2290*a961b961Ssthen 						lock_rw_unlock(&zones->lock);
22910b68ff31Ssthen 						return;
22920b68ff31Ssthen 					}
2293933707f3Ssthen 				}
2294933707f3Ssthen 			}
2295*a961b961Ssthen 		}
2296933707f3Ssthen 		lock_rw_unlock(&z->lock);
2297933707f3Ssthen 	}
22980b68ff31Ssthen 	lock_rw_unlock(&zones->lock);
2299933707f3Ssthen }
2300933707f3Ssthen 
2301a58bff56Ssthen /** struct for user arg ratelimit list */
2302a58bff56Ssthen struct ratelimit_list_arg {
2303a58bff56Ssthen 	/** the infra cache */
2304a58bff56Ssthen 	struct infra_cache* infra;
2305a58bff56Ssthen 	/** the SSL to print to */
2306a58bff56Ssthen 	SSL* ssl;
2307a58bff56Ssthen 	/** all or only ratelimited */
2308a58bff56Ssthen 	int all;
2309a58bff56Ssthen 	/** current time */
2310a58bff56Ssthen 	time_t now;
2311a58bff56Ssthen };
2312a58bff56Ssthen 
2313a58bff56Ssthen /** list items in the ratelimit table */
2314a58bff56Ssthen static void
2315a58bff56Ssthen rate_list(struct lruhash_entry* e, void* arg)
2316a58bff56Ssthen {
2317a58bff56Ssthen 	struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg;
2318a58bff56Ssthen 	struct rate_key* k = (struct rate_key*)e->key;
2319a58bff56Ssthen 	struct rate_data* d = (struct rate_data*)e->data;
2320a58bff56Ssthen 	char buf[257];
2321a58bff56Ssthen 	int lim = infra_find_ratelimit(a->infra, k->name, k->namelen);
2322a58bff56Ssthen 	int max = infra_rate_max(d, a->now);
2323a58bff56Ssthen 	if(a->all == 0) {
2324a58bff56Ssthen 		if(max < lim)
2325a58bff56Ssthen 			return;
2326a58bff56Ssthen 	}
2327a58bff56Ssthen 	dname_str(k->name, buf);
2328a58bff56Ssthen 	ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim);
2329a58bff56Ssthen }
2330a58bff56Ssthen 
2331a58bff56Ssthen /** do the ratelimit_list command */
2332a58bff56Ssthen static void
2333a58bff56Ssthen do_ratelimit_list(SSL* ssl, struct worker* worker, char* arg)
2334a58bff56Ssthen {
2335a58bff56Ssthen 	struct ratelimit_list_arg a;
2336a58bff56Ssthen 	a.all = 0;
2337a58bff56Ssthen 	a.infra = worker->env.infra_cache;
2338a58bff56Ssthen 	a.now = *worker->env.now;
2339a58bff56Ssthen 	a.ssl = ssl;
2340a58bff56Ssthen 	arg = skipwhite(arg);
2341a58bff56Ssthen 	if(strcmp(arg, "+a") == 0)
2342a58bff56Ssthen 		a.all = 1;
2343a58bff56Ssthen 	if(a.infra->domain_rates==NULL ||
2344a58bff56Ssthen 		(a.all == 0 && infra_dp_ratelimit == 0))
2345a58bff56Ssthen 		return;
2346a58bff56Ssthen 	slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a);
2347a58bff56Ssthen }
2348a58bff56Ssthen 
2349933707f3Ssthen /** tell other processes to execute the command */
2350933707f3Ssthen static void
2351933707f3Ssthen distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd)
2352933707f3Ssthen {
2353933707f3Ssthen 	int i;
2354933707f3Ssthen 	if(!cmd || !ssl)
2355933707f3Ssthen 		return;
2356933707f3Ssthen 	/* skip i=0 which is me */
2357933707f3Ssthen 	for(i=1; i<rc->worker->daemon->num; i++) {
2358933707f3Ssthen 		worker_send_cmd(rc->worker->daemon->workers[i],
2359933707f3Ssthen 			worker_cmd_remote);
2360933707f3Ssthen 		if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2361933707f3Ssthen 			(uint8_t*)cmd, strlen(cmd)+1, 0)) {
2362933707f3Ssthen 			ssl_printf(ssl, "error could not distribute cmd\n");
2363933707f3Ssthen 			return;
2364933707f3Ssthen 		}
2365933707f3Ssthen 	}
2366933707f3Ssthen }
2367933707f3Ssthen 
2368933707f3Ssthen /** check for name with end-of-string, space or tab after it */
2369933707f3Ssthen static int
2370933707f3Ssthen cmdcmp(char* p, const char* cmd, size_t len)
2371933707f3Ssthen {
2372933707f3Ssthen 	return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2373933707f3Ssthen }
2374933707f3Ssthen 
2375933707f3Ssthen /** execute a remote control command */
2376933707f3Ssthen static void
2377933707f3Ssthen execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd,
2378933707f3Ssthen 	struct worker* worker)
2379933707f3Ssthen {
2380933707f3Ssthen 	char* p = skipwhite(cmd);
2381933707f3Ssthen 	/* compare command */
2382933707f3Ssthen 	if(cmdcmp(p, "stop", 4)) {
2383933707f3Ssthen 		do_stop(ssl, rc);
2384933707f3Ssthen 		return;
2385933707f3Ssthen 	} else if(cmdcmp(p, "reload", 6)) {
2386933707f3Ssthen 		do_reload(ssl, rc);
2387933707f3Ssthen 		return;
2388933707f3Ssthen 	} else if(cmdcmp(p, "stats_noreset", 13)) {
2389933707f3Ssthen 		do_stats(ssl, rc, 0);
2390933707f3Ssthen 		return;
2391933707f3Ssthen 	} else if(cmdcmp(p, "stats", 5)) {
2392933707f3Ssthen 		do_stats(ssl, rc, 1);
2393933707f3Ssthen 		return;
2394933707f3Ssthen 	} else if(cmdcmp(p, "status", 6)) {
2395933707f3Ssthen 		do_status(ssl, worker);
2396933707f3Ssthen 		return;
2397933707f3Ssthen 	} else if(cmdcmp(p, "dump_cache", 10)) {
2398933707f3Ssthen 		(void)dump_cache(ssl, worker);
2399933707f3Ssthen 		return;
2400933707f3Ssthen 	} else if(cmdcmp(p, "load_cache", 10)) {
2401933707f3Ssthen 		if(load_cache(ssl, worker)) send_ok(ssl);
2402933707f3Ssthen 		return;
2403933707f3Ssthen 	} else if(cmdcmp(p, "list_forwards", 13)) {
2404933707f3Ssthen 		do_list_forwards(ssl, worker);
2405933707f3Ssthen 		return;
2406933707f3Ssthen 	} else if(cmdcmp(p, "list_stubs", 10)) {
2407933707f3Ssthen 		do_list_stubs(ssl, worker);
2408933707f3Ssthen 		return;
2409a58bff56Ssthen 	} else if(cmdcmp(p, "list_insecure", 13)) {
2410a58bff56Ssthen 		do_insecure_list(ssl, worker);
2411a58bff56Ssthen 		return;
2412933707f3Ssthen 	} else if(cmdcmp(p, "list_local_zones", 16)) {
2413933707f3Ssthen 		do_list_local_zones(ssl, worker);
2414933707f3Ssthen 		return;
2415933707f3Ssthen 	} else if(cmdcmp(p, "list_local_data", 15)) {
2416933707f3Ssthen 		do_list_local_data(ssl, worker);
2417933707f3Ssthen 		return;
2418a58bff56Ssthen 	} else if(cmdcmp(p, "ratelimit_list", 14)) {
2419a58bff56Ssthen 		do_ratelimit_list(ssl, worker, p+14);
2420a58bff56Ssthen 		return;
2421163a4143Ssthen 	} else if(cmdcmp(p, "stub_add", 8)) {
2422163a4143Ssthen 		/* must always distribute this cmd */
2423163a4143Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2424163a4143Ssthen 		do_stub_add(ssl, worker, skipwhite(p+8));
2425163a4143Ssthen 		return;
2426163a4143Ssthen 	} else if(cmdcmp(p, "stub_remove", 11)) {
2427163a4143Ssthen 		/* must always distribute this cmd */
2428163a4143Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2429163a4143Ssthen 		do_stub_remove(ssl, worker, skipwhite(p+11));
2430163a4143Ssthen 		return;
2431163a4143Ssthen 	} else if(cmdcmp(p, "forward_add", 11)) {
2432163a4143Ssthen 		/* must always distribute this cmd */
2433163a4143Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2434163a4143Ssthen 		do_forward_add(ssl, worker, skipwhite(p+11));
2435163a4143Ssthen 		return;
2436163a4143Ssthen 	} else if(cmdcmp(p, "forward_remove", 14)) {
2437163a4143Ssthen 		/* must always distribute this cmd */
2438163a4143Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2439163a4143Ssthen 		do_forward_remove(ssl, worker, skipwhite(p+14));
2440163a4143Ssthen 		return;
2441e9c7b4efSsthen 	} else if(cmdcmp(p, "insecure_add", 12)) {
2442e9c7b4efSsthen 		/* must always distribute this cmd */
2443e9c7b4efSsthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2444e9c7b4efSsthen 		do_insecure_add(ssl, worker, skipwhite(p+12));
2445e9c7b4efSsthen 		return;
2446e9c7b4efSsthen 	} else if(cmdcmp(p, "insecure_remove", 15)) {
2447e9c7b4efSsthen 		/* must always distribute this cmd */
2448e9c7b4efSsthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2449e9c7b4efSsthen 		do_insecure_remove(ssl, worker, skipwhite(p+15));
2450e9c7b4efSsthen 		return;
2451933707f3Ssthen 	} else if(cmdcmp(p, "forward", 7)) {
2452933707f3Ssthen 		/* must always distribute this cmd */
2453933707f3Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2454933707f3Ssthen 		do_forward(ssl, worker, skipwhite(p+7));
2455933707f3Ssthen 		return;
2456933707f3Ssthen 	} else if(cmdcmp(p, "flush_stats", 11)) {
2457933707f3Ssthen 		/* must always distribute this cmd */
2458933707f3Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2459933707f3Ssthen 		do_flush_stats(ssl, worker);
2460933707f3Ssthen 		return;
2461933707f3Ssthen 	} else if(cmdcmp(p, "flush_requestlist", 17)) {
2462933707f3Ssthen 		/* must always distribute this cmd */
2463933707f3Ssthen 		if(rc) distribute_cmd(rc, ssl, cmd);
2464933707f3Ssthen 		do_flush_requestlist(ssl, worker);
2465933707f3Ssthen 		return;
2466933707f3Ssthen 	} else if(cmdcmp(p, "lookup", 6)) {
2467933707f3Ssthen 		do_lookup(ssl, worker, skipwhite(p+6));
2468933707f3Ssthen 		return;
2469933707f3Ssthen 	}
2470933707f3Ssthen 
2471933707f3Ssthen #ifdef THREADS_DISABLED
2472933707f3Ssthen 	/* other processes must execute the command as well */
2473933707f3Ssthen 	/* commands that should not be distributed, returned above. */
2474933707f3Ssthen 	if(rc) { /* only if this thread is the master (rc) thread */
2475933707f3Ssthen 		/* done before the code below, which may split the string */
2476933707f3Ssthen 		distribute_cmd(rc, ssl, cmd);
2477933707f3Ssthen 	}
2478933707f3Ssthen #endif
2479933707f3Ssthen 	if(cmdcmp(p, "verbosity", 9)) {
2480933707f3Ssthen 		do_verbosity(ssl, skipwhite(p+9));
2481933707f3Ssthen 	} else if(cmdcmp(p, "local_zone_remove", 17)) {
2482933707f3Ssthen 		do_zone_remove(ssl, worker, skipwhite(p+17));
2483933707f3Ssthen 	} else if(cmdcmp(p, "local_zone", 10)) {
2484933707f3Ssthen 		do_zone_add(ssl, worker, skipwhite(p+10));
2485933707f3Ssthen 	} else if(cmdcmp(p, "local_data_remove", 17)) {
2486933707f3Ssthen 		do_data_remove(ssl, worker, skipwhite(p+17));
2487933707f3Ssthen 	} else if(cmdcmp(p, "local_data", 10)) {
2488933707f3Ssthen 		do_data_add(ssl, worker, skipwhite(p+10));
2489933707f3Ssthen 	} else if(cmdcmp(p, "flush_zone", 10)) {
2490933707f3Ssthen 		do_flush_zone(ssl, worker, skipwhite(p+10));
2491933707f3Ssthen 	} else if(cmdcmp(p, "flush_type", 10)) {
2492933707f3Ssthen 		do_flush_type(ssl, worker, skipwhite(p+10));
2493933707f3Ssthen 	} else if(cmdcmp(p, "flush_infra", 11)) {
2494933707f3Ssthen 		do_flush_infra(ssl, worker, skipwhite(p+11));
2495933707f3Ssthen 	} else if(cmdcmp(p, "flush", 5)) {
2496933707f3Ssthen 		do_flush_name(ssl, worker, skipwhite(p+5));
2497933707f3Ssthen 	} else if(cmdcmp(p, "dump_requestlist", 16)) {
2498933707f3Ssthen 		do_dump_requestlist(ssl, worker);
2499933707f3Ssthen 	} else if(cmdcmp(p, "dump_infra", 10)) {
2500933707f3Ssthen 		do_dump_infra(ssl, worker);
2501933707f3Ssthen 	} else if(cmdcmp(p, "log_reopen", 10)) {
2502933707f3Ssthen 		do_log_reopen(ssl, worker);
2503933707f3Ssthen 	} else if(cmdcmp(p, "set_option", 10)) {
2504933707f3Ssthen 		do_set_option(ssl, worker, skipwhite(p+10));
2505933707f3Ssthen 	} else if(cmdcmp(p, "get_option", 10)) {
2506933707f3Ssthen 		do_get_option(ssl, worker, skipwhite(p+10));
2507cebdf579Ssthen 	} else if(cmdcmp(p, "flush_bogus", 11)) {
2508cebdf579Ssthen 		do_flush_bogus(ssl, worker);
2509e10d3884Sbrad 	} else if(cmdcmp(p, "flush_negative", 14)) {
2510e10d3884Sbrad 		do_flush_negative(ssl, worker);
2511933707f3Ssthen 	} else {
2512933707f3Ssthen 		(void)ssl_printf(ssl, "error unknown command '%s'\n", p);
2513933707f3Ssthen 	}
2514933707f3Ssthen }
2515933707f3Ssthen 
2516933707f3Ssthen void
2517933707f3Ssthen daemon_remote_exec(struct worker* worker)
2518933707f3Ssthen {
2519933707f3Ssthen 	/* read the cmd string */
2520933707f3Ssthen 	uint8_t* msg = NULL;
2521933707f3Ssthen 	uint32_t len = 0;
2522933707f3Ssthen 	if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
2523933707f3Ssthen 		log_err("daemon_remote_exec: tube_read_msg failed");
2524933707f3Ssthen 		return;
2525933707f3Ssthen 	}
2526933707f3Ssthen 	verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
2527933707f3Ssthen 	execute_cmd(NULL, NULL, (char*)msg, worker);
2528933707f3Ssthen 	free(msg);
2529933707f3Ssthen }
2530933707f3Ssthen 
2531933707f3Ssthen /** handle remote control request */
2532933707f3Ssthen static void
2533933707f3Ssthen handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl)
2534933707f3Ssthen {
2535933707f3Ssthen 	int r;
2536933707f3Ssthen 	char pre[10];
2537933707f3Ssthen 	char magic[7];
2538933707f3Ssthen 	char buf[1024];
2539933707f3Ssthen #ifdef USE_WINSOCK
2540933707f3Ssthen 	/* makes it possible to set the socket blocking again. */
2541933707f3Ssthen 	/* basically removes it from winsock_event ... */
2542933707f3Ssthen 	WSAEventSelect(s->c->fd, NULL, 0);
2543933707f3Ssthen #endif
2544933707f3Ssthen 	fd_set_block(s->c->fd);
2545933707f3Ssthen 
2546933707f3Ssthen 	/* try to read magic UBCT[version]_space_ string */
2547933707f3Ssthen 	ERR_clear_error();
2548933707f3Ssthen 	if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) {
2549933707f3Ssthen 		if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN)
2550933707f3Ssthen 			return;
2551933707f3Ssthen 		log_crypto_err("could not SSL_read");
2552933707f3Ssthen 		return;
2553933707f3Ssthen 	}
2554933707f3Ssthen 	magic[6] = 0;
2555933707f3Ssthen 	if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
2556933707f3Ssthen 		verbose(VERB_QUERY, "control connection has bad magic string");
2557933707f3Ssthen 		/* probably wrong tool connected, ignore it completely */
2558933707f3Ssthen 		return;
2559933707f3Ssthen 	}
2560933707f3Ssthen 
2561933707f3Ssthen 	/* read the command line */
2562933707f3Ssthen 	if(!ssl_read_line(ssl, buf, sizeof(buf))) {
2563933707f3Ssthen 		return;
2564933707f3Ssthen 	}
2565933707f3Ssthen 	snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
2566933707f3Ssthen 	if(strcmp(magic, pre) != 0) {
2567933707f3Ssthen 		verbose(VERB_QUERY, "control connection had bad "
2568933707f3Ssthen 			"version %s, cmd: %s", magic, buf);
2569933707f3Ssthen 		ssl_printf(ssl, "error version mismatch\n");
2570933707f3Ssthen 		return;
2571933707f3Ssthen 	}
2572933707f3Ssthen 	verbose(VERB_DETAIL, "control cmd: %s", buf);
2573933707f3Ssthen 
2574933707f3Ssthen 	/* figure out what to do */
2575933707f3Ssthen 	execute_cmd(rc, ssl, buf, rc->worker);
2576933707f3Ssthen }
2577933707f3Ssthen 
2578933707f3Ssthen int remote_control_callback(struct comm_point* c, void* arg, int err,
2579933707f3Ssthen 	struct comm_reply* ATTR_UNUSED(rep))
2580933707f3Ssthen {
2581933707f3Ssthen 	struct rc_state* s = (struct rc_state*)arg;
2582933707f3Ssthen 	struct daemon_remote* rc = s->rc;
2583933707f3Ssthen 	int r;
2584933707f3Ssthen 	if(err != NETEVENT_NOERROR) {
2585933707f3Ssthen 		if(err==NETEVENT_TIMEOUT)
2586933707f3Ssthen 			log_err("remote control timed out");
2587933707f3Ssthen 		clean_point(rc, s);
2588933707f3Ssthen 		return 0;
2589933707f3Ssthen 	}
2590933707f3Ssthen 	/* (continue to) setup the SSL connection */
2591933707f3Ssthen 	ERR_clear_error();
2592933707f3Ssthen 	r = SSL_do_handshake(s->ssl);
2593933707f3Ssthen 	if(r != 1) {
2594933707f3Ssthen 		int r2 = SSL_get_error(s->ssl, r);
2595933707f3Ssthen 		if(r2 == SSL_ERROR_WANT_READ) {
2596933707f3Ssthen 			if(s->shake_state == rc_hs_read) {
2597933707f3Ssthen 				/* try again later */
2598933707f3Ssthen 				return 0;
2599933707f3Ssthen 			}
2600933707f3Ssthen 			s->shake_state = rc_hs_read;
2601933707f3Ssthen 			comm_point_listen_for_rw(c, 1, 0);
2602933707f3Ssthen 			return 0;
2603933707f3Ssthen 		} else if(r2 == SSL_ERROR_WANT_WRITE) {
2604933707f3Ssthen 			if(s->shake_state == rc_hs_write) {
2605933707f3Ssthen 				/* try again later */
2606933707f3Ssthen 				return 0;
2607933707f3Ssthen 			}
2608933707f3Ssthen 			s->shake_state = rc_hs_write;
2609933707f3Ssthen 			comm_point_listen_for_rw(c, 0, 1);
2610933707f3Ssthen 			return 0;
2611933707f3Ssthen 		} else {
2612933707f3Ssthen 			if(r == 0)
2613933707f3Ssthen 				log_err("remote control connection closed prematurely");
2614933707f3Ssthen 			log_addr(1, "failed connection from",
2615933707f3Ssthen 				&s->c->repinfo.addr, s->c->repinfo.addrlen);
2616933707f3Ssthen 			log_crypto_err("remote control failed ssl");
2617933707f3Ssthen 			clean_point(rc, s);
2618933707f3Ssthen 			return 0;
2619933707f3Ssthen 		}
2620933707f3Ssthen 	}
2621933707f3Ssthen 	s->shake_state = rc_none;
2622933707f3Ssthen 
2623933707f3Ssthen 	/* once handshake has completed, check authentication */
262431f127bbSsthen 	if (!rc->use_cert) {
262531f127bbSsthen 		verbose(VERB_ALGO, "unauthenticated remote control connection");
262631f127bbSsthen 	} else if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
2627933707f3Ssthen 		X509* x = SSL_get_peer_certificate(s->ssl);
2628933707f3Ssthen 		if(!x) {
2629933707f3Ssthen 			verbose(VERB_DETAIL, "remote control connection "
2630933707f3Ssthen 				"provided no client certificate");
2631933707f3Ssthen 			clean_point(rc, s);
2632933707f3Ssthen 			return 0;
2633933707f3Ssthen 		}
2634933707f3Ssthen 		verbose(VERB_ALGO, "remote control connection authenticated");
2635933707f3Ssthen 		X509_free(x);
2636933707f3Ssthen 	} else {
2637933707f3Ssthen 		verbose(VERB_DETAIL, "remote control connection failed to "
2638933707f3Ssthen 			"authenticate with client certificate");
2639933707f3Ssthen 		clean_point(rc, s);
2640933707f3Ssthen 		return 0;
2641933707f3Ssthen 	}
2642933707f3Ssthen 
2643933707f3Ssthen 	/* if OK start to actually handle the request */
2644933707f3Ssthen 	handle_req(rc, s, s->ssl);
2645933707f3Ssthen 
2646933707f3Ssthen 	verbose(VERB_ALGO, "remote control operation completed");
2647933707f3Ssthen 	clean_point(rc, s);
2648933707f3Ssthen 	return 0;
2649933707f3Ssthen }
2650