remote.c revision 269257
1/*
2 * daemon/remote.c - remote control for the unbound daemon.
3 *
4 * Copyright (c) 2008, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/**
37 * \file
38 *
39 * This file contains the remote control functionality for the daemon.
40 * The remote control can be performed using either the commandline
41 * unbound-control tool, or a SSLv3/TLS capable web browser.
42 * The channel is secured using SSLv3 or TLSv1, and certificates.
43 * Both the server and the client(control tool) have their own keys.
44 */
45#include "config.h"
46#ifdef HAVE_OPENSSL_ERR_H
47#include <openssl/err.h>
48#endif
49#include <ctype.h>
50#include "daemon/remote.h"
51#include "daemon/worker.h"
52#include "daemon/daemon.h"
53#include "daemon/stats.h"
54#include "daemon/cachedump.h"
55#include "util/log.h"
56#include "util/config_file.h"
57#include "util/net_help.h"
58#include "util/module.h"
59#include "services/listen_dnsport.h"
60#include "services/cache/rrset.h"
61#include "services/cache/infra.h"
62#include "services/mesh.h"
63#include "services/localzone.h"
64#include "util/storage/slabhash.h"
65#include "util/fptr_wlist.h"
66#include "util/data/dname.h"
67#include "validator/validator.h"
68#include "validator/val_kcache.h"
69#include "validator/val_kentry.h"
70#include "validator/val_anchor.h"
71#include "iterator/iterator.h"
72#include "iterator/iter_fwd.h"
73#include "iterator/iter_hints.h"
74#include "iterator/iter_delegpt.h"
75#include "services/outbound_list.h"
76#include "services/outside_network.h"
77#include "ldns/str2wire.h"
78#include "ldns/parseutil.h"
79#include "ldns/wire2str.h"
80#include "ldns/sbuffer.h"
81
82#ifdef HAVE_SYS_TYPES_H
83#  include <sys/types.h>
84#endif
85#ifdef HAVE_NETDB_H
86#include <netdb.h>
87#endif
88
89/* just for portability */
90#ifdef SQ
91#undef SQ
92#endif
93
94/** what to put on statistics lines between var and value, ": " or "=" */
95#define SQ "="
96/** if true, inhibits a lot of =0 lines from the stats output */
97static const int inhibit_zero = 1;
98
99/** subtract timers and the values do not overflow or become negative */
100static void
101timeval_subtract(struct timeval* d, const struct timeval* end,
102	const struct timeval* start)
103{
104#ifndef S_SPLINT_S
105	time_t end_usec = end->tv_usec;
106	d->tv_sec = end->tv_sec - start->tv_sec;
107	if(end_usec < start->tv_usec) {
108		end_usec += 1000000;
109		d->tv_sec--;
110	}
111	d->tv_usec = end_usec - start->tv_usec;
112#endif
113}
114
115/** divide sum of timers to get average */
116static void
117timeval_divide(struct timeval* avg, const struct timeval* sum, size_t d)
118{
119#ifndef S_SPLINT_S
120	size_t leftover;
121	if(d == 0) {
122		avg->tv_sec = 0;
123		avg->tv_usec = 0;
124		return;
125	}
126	avg->tv_sec = sum->tv_sec / d;
127	avg->tv_usec = sum->tv_usec / d;
128	/* handle fraction from seconds divide */
129	leftover = sum->tv_sec - avg->tv_sec*d;
130	avg->tv_usec += (leftover*1000000)/d;
131#endif
132}
133
134struct daemon_remote*
135daemon_remote_create(struct config_file* cfg)
136{
137	char* s_cert;
138	char* s_key;
139	struct daemon_remote* rc = (struct daemon_remote*)calloc(1,
140		sizeof(*rc));
141	if(!rc) {
142		log_err("out of memory in daemon_remote_create");
143		return NULL;
144	}
145	rc->max_active = 10;
146
147	if(!cfg->remote_control_enable) {
148		rc->ctx = NULL;
149		return rc;
150	}
151	rc->ctx = SSL_CTX_new(SSLv23_server_method());
152	if(!rc->ctx) {
153		log_crypto_err("could not SSL_CTX_new");
154		free(rc);
155		return NULL;
156	}
157	/* no SSLv2 because has defects */
158	if(!(SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)){
159		log_crypto_err("could not set SSL_OP_NO_SSLv2");
160		daemon_remote_delete(rc);
161		return NULL;
162	}
163	s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
164	s_key = fname_after_chroot(cfg->server_key_file, cfg, 1);
165	if(!s_cert || !s_key) {
166		log_err("out of memory in remote control fname");
167		goto setup_error;
168	}
169	verbose(VERB_ALGO, "setup SSL certificates");
170	if (!SSL_CTX_use_certificate_file(rc->ctx,s_cert,SSL_FILETYPE_PEM)) {
171		log_err("Error for server-cert-file: %s", s_cert);
172		log_crypto_err("Error in SSL_CTX use_certificate_file");
173		goto setup_error;
174	}
175	if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) {
176		log_err("Error for server-key-file: %s", s_key);
177		log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
178		goto setup_error;
179	}
180	if(!SSL_CTX_check_private_key(rc->ctx)) {
181		log_err("Error for server-key-file: %s", s_key);
182		log_crypto_err("Error in SSL_CTX check_private_key");
183		goto setup_error;
184	}
185	if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) {
186		log_crypto_err("Error setting up SSL_CTX verify locations");
187	setup_error:
188		free(s_cert);
189		free(s_key);
190		daemon_remote_delete(rc);
191		return NULL;
192	}
193	SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert));
194	SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL);
195	free(s_cert);
196	free(s_key);
197
198	return rc;
199}
200
201void daemon_remote_clear(struct daemon_remote* rc)
202{
203	struct rc_state* p, *np;
204	if(!rc) return;
205	/* but do not close the ports */
206	listen_list_delete(rc->accept_list);
207	rc->accept_list = NULL;
208	/* do close these sockets */
209	p = rc->busy_list;
210	while(p) {
211		np = p->next;
212		if(p->ssl)
213			SSL_free(p->ssl);
214		comm_point_delete(p->c);
215		free(p);
216		p = np;
217	}
218	rc->busy_list = NULL;
219	rc->active = 0;
220	rc->worker = NULL;
221}
222
223void daemon_remote_delete(struct daemon_remote* rc)
224{
225	if(!rc) return;
226	daemon_remote_clear(rc);
227	if(rc->ctx) {
228		SSL_CTX_free(rc->ctx);
229	}
230	free(rc);
231}
232
233/**
234 * Add and open a new control port
235 * @param ip: ip str
236 * @param nr: port nr
237 * @param list: list head
238 * @param noproto_is_err: if lack of protocol support is an error.
239 * @return false on failure.
240 */
241static int
242add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err)
243{
244	struct addrinfo hints;
245	struct addrinfo* res;
246	struct listen_port* n;
247	int noproto;
248	int fd, r;
249	char port[15];
250	snprintf(port, sizeof(port), "%d", nr);
251	port[sizeof(port)-1]=0;
252	memset(&hints, 0, sizeof(hints));
253	hints.ai_socktype = SOCK_STREAM;
254	hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
255	if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
256#ifdef USE_WINSOCK
257		if(!noproto_is_err && r == EAI_NONAME) {
258			/* tried to lookup the address as name */
259			return 1; /* return success, but do nothing */
260		}
261#endif /* USE_WINSOCK */
262                log_err("control interface %s:%s getaddrinfo: %s %s",
263			ip?ip:"default", port, gai_strerror(r),
264#ifdef EAI_SYSTEM
265			r==EAI_SYSTEM?(char*)strerror(errno):""
266#else
267			""
268#endif
269			);
270		return 0;
271	}
272
273	/* open fd */
274	fd = create_tcp_accept_sock(res, 1, &noproto, 0);
275	freeaddrinfo(res);
276	if(fd == -1 && noproto) {
277		if(!noproto_is_err)
278			return 1; /* return success, but do nothing */
279		log_err("cannot open control interface %s %d : "
280			"protocol not supported", ip, nr);
281		return 0;
282	}
283	if(fd == -1) {
284		log_err("cannot open control interface %s %d", ip, nr);
285		return 0;
286	}
287
288	/* alloc */
289	n = (struct listen_port*)calloc(1, sizeof(*n));
290	if(!n) {
291#ifndef USE_WINSOCK
292		close(fd);
293#else
294		closesocket(fd);
295#endif
296		log_err("out of memory");
297		return 0;
298	}
299	n->next = *list;
300	*list = n;
301	n->fd = fd;
302	return 1;
303}
304
305struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
306{
307	struct listen_port* l = NULL;
308	log_assert(cfg->remote_control_enable && cfg->control_port);
309	if(cfg->control_ifs) {
310		struct config_strlist* p;
311		for(p = cfg->control_ifs; p; p = p->next) {
312			if(!add_open(p->str, cfg->control_port, &l, 1)) {
313				listening_ports_free(l);
314				return NULL;
315			}
316		}
317	} else {
318		/* defaults */
319		if(cfg->do_ip6 &&
320			!add_open("::1", cfg->control_port, &l, 0)) {
321			listening_ports_free(l);
322			return NULL;
323		}
324		if(cfg->do_ip4 &&
325			!add_open("127.0.0.1", cfg->control_port, &l, 1)) {
326			listening_ports_free(l);
327			return NULL;
328		}
329	}
330	return l;
331}
332
333/** open accept commpoint */
334static int
335accept_open(struct daemon_remote* rc, int fd)
336{
337	struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
338	if(!n) {
339		log_err("out of memory");
340		return 0;
341	}
342	n->next = rc->accept_list;
343	rc->accept_list = n;
344	/* open commpt */
345	n->com = comm_point_create_raw(rc->worker->base, fd, 0,
346		&remote_accept_callback, rc);
347	if(!n->com)
348		return 0;
349	/* keep this port open, its fd is kept in the rc portlist */
350	n->com->do_not_close = 1;
351	return 1;
352}
353
354int daemon_remote_open_accept(struct daemon_remote* rc,
355	struct listen_port* ports, struct worker* worker)
356{
357	struct listen_port* p;
358	rc->worker = worker;
359	for(p = ports; p; p = p->next) {
360		if(!accept_open(rc, p->fd)) {
361			log_err("could not create accept comm point");
362			return 0;
363		}
364	}
365	return 1;
366}
367
368void daemon_remote_stop_accept(struct daemon_remote* rc)
369{
370	struct listen_list* p;
371	for(p=rc->accept_list; p; p=p->next) {
372		comm_point_stop_listening(p->com);
373	}
374}
375
376void daemon_remote_start_accept(struct daemon_remote* rc)
377{
378	struct listen_list* p;
379	for(p=rc->accept_list; p; p=p->next) {
380		comm_point_start_listening(p->com, -1, -1);
381	}
382}
383
384int remote_accept_callback(struct comm_point* c, void* arg, int err,
385	struct comm_reply* ATTR_UNUSED(rep))
386{
387	struct daemon_remote* rc = (struct daemon_remote*)arg;
388	struct sockaddr_storage addr;
389	socklen_t addrlen;
390	int newfd;
391	struct rc_state* n;
392	if(err != NETEVENT_NOERROR) {
393		log_err("error %d on remote_accept_callback", err);
394		return 0;
395	}
396	/* perform the accept */
397	newfd = comm_point_perform_accept(c, &addr, &addrlen);
398	if(newfd == -1)
399		return 0;
400	/* create new commpoint unless we are servicing already */
401	if(rc->active >= rc->max_active) {
402		log_warn("drop incoming remote control: too many connections");
403	close_exit:
404#ifndef USE_WINSOCK
405		close(newfd);
406#else
407		closesocket(newfd);
408#endif
409		return 0;
410	}
411
412	/* setup commpoint to service the remote control command */
413	n = (struct rc_state*)calloc(1, sizeof(*n));
414	if(!n) {
415		log_err("out of memory");
416		goto close_exit;
417	}
418	/* start in reading state */
419	n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
420		&remote_control_callback, n);
421	if(!n->c) {
422		log_err("out of memory");
423		free(n);
424		goto close_exit;
425	}
426	log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
427	n->c->do_not_close = 0;
428	comm_point_stop_listening(n->c);
429	comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
430	memcpy(&n->c->repinfo.addr, &addr, addrlen);
431	n->c->repinfo.addrlen = addrlen;
432	n->shake_state = rc_hs_read;
433	n->ssl = SSL_new(rc->ctx);
434	if(!n->ssl) {
435		log_crypto_err("could not SSL_new");
436		comm_point_delete(n->c);
437		free(n);
438		goto close_exit;
439	}
440	SSL_set_accept_state(n->ssl);
441        (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY);
442	if(!SSL_set_fd(n->ssl, newfd)) {
443		log_crypto_err("could not SSL_set_fd");
444		SSL_free(n->ssl);
445		comm_point_delete(n->c);
446		free(n);
447		goto close_exit;
448	}
449
450	n->rc = rc;
451	n->next = rc->busy_list;
452	rc->busy_list = n;
453	rc->active ++;
454
455	/* perform the first nonblocking read already, for windows,
456	 * so it can return wouldblock. could be faster too. */
457	(void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
458	return 0;
459}
460
461/** delete from list */
462static void
463state_list_remove_elem(struct rc_state** list, struct comm_point* c)
464{
465	while(*list) {
466		if( (*list)->c == c) {
467			*list = (*list)->next;
468			return;
469		}
470		list = &(*list)->next;
471	}
472}
473
474/** decrease active count and remove commpoint from busy list */
475static void
476clean_point(struct daemon_remote* rc, struct rc_state* s)
477{
478	state_list_remove_elem(&rc->busy_list, s->c);
479	rc->active --;
480	if(s->ssl) {
481		SSL_shutdown(s->ssl);
482		SSL_free(s->ssl);
483	}
484	comm_point_delete(s->c);
485	free(s);
486}
487
488int
489ssl_print_text(SSL* ssl, const char* text)
490{
491	int r;
492	if(!ssl)
493		return 0;
494	ERR_clear_error();
495	if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) {
496		if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
497			verbose(VERB_QUERY, "warning, in SSL_write, peer "
498				"closed connection");
499			return 0;
500		}
501		log_crypto_err("could not SSL_write");
502		return 0;
503	}
504	return 1;
505}
506
507/** print text over the ssl connection */
508static int
509ssl_print_vmsg(SSL* ssl, const char* format, va_list args)
510{
511	char msg[1024];
512	vsnprintf(msg, sizeof(msg), format, args);
513	return ssl_print_text(ssl, msg);
514}
515
516/** printf style printing to the ssl connection */
517int ssl_printf(SSL* ssl, const char* format, ...)
518{
519	va_list args;
520	int ret;
521	va_start(args, format);
522	ret = ssl_print_vmsg(ssl, format, args);
523	va_end(args);
524	return ret;
525}
526
527int
528ssl_read_line(SSL* ssl, char* buf, size_t max)
529{
530	int r;
531	size_t len = 0;
532	if(!ssl)
533		return 0;
534	while(len < max) {
535		ERR_clear_error();
536		if((r=SSL_read(ssl, buf+len, 1)) <= 0) {
537			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
538				buf[len] = 0;
539				return 1;
540			}
541			log_crypto_err("could not SSL_read");
542			return 0;
543		}
544		if(buf[len] == '\n') {
545			/* return string without \n */
546			buf[len] = 0;
547			return 1;
548		}
549		len++;
550	}
551	buf[max-1] = 0;
552	log_err("control line too long (%d): %s", (int)max, buf);
553	return 0;
554}
555
556/** skip whitespace, return new pointer into string */
557static char*
558skipwhite(char* str)
559{
560	/* EOS \0 is not a space */
561	while( isspace(*str) )
562		str++;
563	return str;
564}
565
566/** send the OK to the control client */
567static void send_ok(SSL* ssl)
568{
569	(void)ssl_printf(ssl, "ok\n");
570}
571
572/** do the stop command */
573static void
574do_stop(SSL* ssl, struct daemon_remote* rc)
575{
576	rc->worker->need_to_exit = 1;
577	comm_base_exit(rc->worker->base);
578	send_ok(ssl);
579}
580
581/** do the reload command */
582static void
583do_reload(SSL* ssl, struct daemon_remote* rc)
584{
585	rc->worker->need_to_exit = 0;
586	comm_base_exit(rc->worker->base);
587	send_ok(ssl);
588}
589
590/** do the verbosity command */
591static void
592do_verbosity(SSL* ssl, char* str)
593{
594	int val = atoi(str);
595	if(val == 0 && strcmp(str, "0") != 0) {
596		ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
597		return;
598	}
599	verbosity = val;
600	send_ok(ssl);
601}
602
603/** print stats from statinfo */
604static int
605print_stats(SSL* ssl, const char* nm, struct stats_info* s)
606{
607	struct timeval avg;
608	if(!ssl_printf(ssl, "%s.num.queries"SQ"%u\n", nm,
609		(unsigned)s->svr.num_queries)) return 0;
610	if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%u\n", nm,
611		(unsigned)(s->svr.num_queries
612			- s->svr.num_queries_missed_cache))) return 0;
613	if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%u\n", nm,
614		(unsigned)s->svr.num_queries_missed_cache)) return 0;
615	if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%u\n", nm,
616		(unsigned)s->svr.num_queries_prefetch)) return 0;
617	if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%u\n", nm,
618		(unsigned)s->mesh_replies_sent)) return 0;
619	if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
620		(s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
621			(double)s->svr.sum_query_list_size/
622			(s->svr.num_queries_missed_cache+
623			s->svr.num_queries_prefetch) : 0.0)) return 0;
624	if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%u\n", nm,
625		(unsigned)s->svr.max_query_list_size)) return 0;
626	if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%u\n", nm,
627		(unsigned)s->mesh_jostled)) return 0;
628	if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%u\n", nm,
629		(unsigned)s->mesh_dropped)) return 0;
630	if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%u\n", nm,
631		(unsigned)s->mesh_num_states)) return 0;
632	if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%u\n", nm,
633		(unsigned)s->mesh_num_reply_states)) return 0;
634	timeval_divide(&avg, &s->mesh_replies_sum_wait, s->mesh_replies_sent);
635	if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
636		(long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
637	if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
638		s->mesh_time_median)) return 0;
639	return 1;
640}
641
642/** print stats for one thread */
643static int
644print_thread_stats(SSL* ssl, int i, struct stats_info* s)
645{
646	char nm[16];
647	snprintf(nm, sizeof(nm), "thread%d", i);
648	nm[sizeof(nm)-1]=0;
649	return print_stats(ssl, nm, s);
650}
651
652/** print long number */
653static int
654print_longnum(SSL* ssl, const char* desc, size_t x)
655{
656	if(x > 1024*1024*1024) {
657		/* more than a Gb */
658		size_t front = x / (size_t)1000000;
659		size_t back = x % (size_t)1000000;
660		return ssl_printf(ssl, "%s%u%6.6u\n", desc,
661			(unsigned)front, (unsigned)back);
662	} else {
663		return ssl_printf(ssl, "%s%u\n", desc, (unsigned)x);
664	}
665}
666
667/** print mem stats */
668static int
669print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon)
670{
671	int m;
672	size_t msg, rrset, val, iter;
673#ifdef HAVE_SBRK
674	extern void* unbound_start_brk;
675	void* cur = sbrk(0);
676	if(!print_longnum(ssl, "mem.total.sbrk"SQ,
677		(size_t)((char*)cur - (char*)unbound_start_brk))) return 0;
678#endif /* HAVE_SBRK */
679	msg = slabhash_get_mem(daemon->env->msg_cache);
680	rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
681	val=0;
682	iter=0;
683	m = modstack_find(&worker->env.mesh->mods, "validator");
684	if(m != -1) {
685		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
686			mods.mod[m]->get_mem));
687		val = (*worker->env.mesh->mods.mod[m]->get_mem)
688			(&worker->env, m);
689	}
690	m = modstack_find(&worker->env.mesh->mods, "iterator");
691	if(m != -1) {
692		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
693			mods.mod[m]->get_mem));
694		iter = (*worker->env.mesh->mods.mod[m]->get_mem)
695			(&worker->env, m);
696	}
697
698	if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
699		return 0;
700	if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
701		return 0;
702	if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
703		return 0;
704	if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
705		return 0;
706	return 1;
707}
708
709/** print uptime stats */
710static int
711print_uptime(SSL* ssl, struct worker* worker, int reset)
712{
713	struct timeval now = *worker->env.now_tv;
714	struct timeval up, dt;
715	timeval_subtract(&up, &now, &worker->daemon->time_boot);
716	timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
717	if(reset)
718		worker->daemon->time_last_stat = now;
719	if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
720		(long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
721	if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
722		(long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
723	if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
724		(long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
725	return 1;
726}
727
728/** print extended histogram */
729static int
730print_hist(SSL* ssl, struct stats_info* s)
731{
732	struct timehist* hist;
733	size_t i;
734	hist = timehist_setup();
735	if(!hist) {
736		log_err("out of memory");
737		return 0;
738	}
739	timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
740	for(i=0; i<hist->num; i++) {
741		if(!ssl_printf(ssl,
742			"histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%u\n",
743			(int)hist->buckets[i].lower.tv_sec,
744			(int)hist->buckets[i].lower.tv_usec,
745			(int)hist->buckets[i].upper.tv_sec,
746			(int)hist->buckets[i].upper.tv_usec,
747			(unsigned)hist->buckets[i].count)) {
748			timehist_delete(hist);
749			return 0;
750		}
751	}
752	timehist_delete(hist);
753	return 1;
754}
755
756/** print extended stats */
757static int
758print_ext(SSL* ssl, struct stats_info* s)
759{
760	int i;
761	char nm[16];
762	const sldns_rr_descriptor* desc;
763	const sldns_lookup_table* lt;
764	/* TYPE */
765	for(i=0; i<STATS_QTYPE_NUM; i++) {
766		if(inhibit_zero && s->svr.qtype[i] == 0)
767			continue;
768		desc = sldns_rr_descript((uint16_t)i);
769		if(desc && desc->_name) {
770			snprintf(nm, sizeof(nm), "%s", desc->_name);
771		} else if (i == LDNS_RR_TYPE_IXFR) {
772			snprintf(nm, sizeof(nm), "IXFR");
773		} else if (i == LDNS_RR_TYPE_AXFR) {
774			snprintf(nm, sizeof(nm), "AXFR");
775		} else if (i == LDNS_RR_TYPE_MAILA) {
776			snprintf(nm, sizeof(nm), "MAILA");
777		} else if (i == LDNS_RR_TYPE_MAILB) {
778			snprintf(nm, sizeof(nm), "MAILB");
779		} else if (i == LDNS_RR_TYPE_ANY) {
780			snprintf(nm, sizeof(nm), "ANY");
781		} else {
782			snprintf(nm, sizeof(nm), "TYPE%d", i);
783		}
784		if(!ssl_printf(ssl, "num.query.type.%s"SQ"%u\n",
785			nm, (unsigned)s->svr.qtype[i])) return 0;
786	}
787	if(!inhibit_zero || s->svr.qtype_big) {
788		if(!ssl_printf(ssl, "num.query.type.other"SQ"%u\n",
789			(unsigned)s->svr.qtype_big)) return 0;
790	}
791	/* CLASS */
792	for(i=0; i<STATS_QCLASS_NUM; i++) {
793		if(inhibit_zero && s->svr.qclass[i] == 0)
794			continue;
795		lt = sldns_lookup_by_id(sldns_rr_classes, i);
796		if(lt && lt->name) {
797			snprintf(nm, sizeof(nm), "%s", lt->name);
798		} else {
799			snprintf(nm, sizeof(nm), "CLASS%d", i);
800		}
801		if(!ssl_printf(ssl, "num.query.class.%s"SQ"%u\n",
802			nm, (unsigned)s->svr.qclass[i])) return 0;
803	}
804	if(!inhibit_zero || s->svr.qclass_big) {
805		if(!ssl_printf(ssl, "num.query.class.other"SQ"%u\n",
806			(unsigned)s->svr.qclass_big)) return 0;
807	}
808	/* OPCODE */
809	for(i=0; i<STATS_OPCODE_NUM; i++) {
810		if(inhibit_zero && s->svr.qopcode[i] == 0)
811			continue;
812		lt = sldns_lookup_by_id(sldns_opcodes, i);
813		if(lt && lt->name) {
814			snprintf(nm, sizeof(nm), "%s", lt->name);
815		} else {
816			snprintf(nm, sizeof(nm), "OPCODE%d", i);
817		}
818		if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%u\n",
819			nm, (unsigned)s->svr.qopcode[i])) return 0;
820	}
821	/* transport */
822	if(!ssl_printf(ssl, "num.query.tcp"SQ"%u\n",
823		(unsigned)s->svr.qtcp)) return 0;
824	if(!ssl_printf(ssl, "num.query.ipv6"SQ"%u\n",
825		(unsigned)s->svr.qipv6)) return 0;
826	/* flags */
827	if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%u\n",
828		(unsigned)s->svr.qbit_QR)) return 0;
829	if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%u\n",
830		(unsigned)s->svr.qbit_AA)) return 0;
831	if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%u\n",
832		(unsigned)s->svr.qbit_TC)) return 0;
833	if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%u\n",
834		(unsigned)s->svr.qbit_RD)) return 0;
835	if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%u\n",
836		(unsigned)s->svr.qbit_RA)) return 0;
837	if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%u\n",
838		(unsigned)s->svr.qbit_Z)) return 0;
839	if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%u\n",
840		(unsigned)s->svr.qbit_AD)) return 0;
841	if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%u\n",
842		(unsigned)s->svr.qbit_CD)) return 0;
843	if(!ssl_printf(ssl, "num.query.edns.present"SQ"%u\n",
844		(unsigned)s->svr.qEDNS)) return 0;
845	if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%u\n",
846		(unsigned)s->svr.qEDNS_DO)) return 0;
847
848	/* RCODE */
849	for(i=0; i<STATS_RCODE_NUM; i++) {
850		if(inhibit_zero && s->svr.ans_rcode[i] == 0)
851			continue;
852		lt = sldns_lookup_by_id(sldns_rcodes, i);
853		if(lt && lt->name) {
854			snprintf(nm, sizeof(nm), "%s", lt->name);
855		} else {
856			snprintf(nm, sizeof(nm), "RCODE%d", i);
857		}
858		if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%u\n",
859			nm, (unsigned)s->svr.ans_rcode[i])) return 0;
860	}
861	if(!inhibit_zero || s->svr.ans_rcode_nodata) {
862		if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%u\n",
863			(unsigned)s->svr.ans_rcode_nodata)) return 0;
864	}
865	/* validation */
866	if(!ssl_printf(ssl, "num.answer.secure"SQ"%u\n",
867		(unsigned)s->svr.ans_secure)) return 0;
868	if(!ssl_printf(ssl, "num.answer.bogus"SQ"%u\n",
869		(unsigned)s->svr.ans_bogus)) return 0;
870	if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%u\n",
871		(unsigned)s->svr.rrset_bogus)) return 0;
872	/* threat detection */
873	if(!ssl_printf(ssl, "unwanted.queries"SQ"%u\n",
874		(unsigned)s->svr.unwanted_queries)) return 0;
875	if(!ssl_printf(ssl, "unwanted.replies"SQ"%u\n",
876		(unsigned)s->svr.unwanted_replies)) return 0;
877	return 1;
878}
879
880/** do the stats command */
881static void
882do_stats(SSL* ssl, struct daemon_remote* rc, int reset)
883{
884	struct daemon* daemon = rc->worker->daemon;
885	struct stats_info total;
886	struct stats_info s;
887	int i;
888	log_assert(daemon->num > 0);
889	/* gather all thread statistics in one place */
890	for(i=0; i<daemon->num; i++) {
891		server_stats_obtain(rc->worker, daemon->workers[i], &s, reset);
892		if(!print_thread_stats(ssl, i, &s))
893			return;
894		if(i == 0)
895			total = s;
896		else	server_stats_add(&total, &s);
897	}
898	/* print the thread statistics */
899	total.mesh_time_median /= (double)daemon->num;
900	if(!print_stats(ssl, "total", &total))
901		return;
902	if(!print_uptime(ssl, rc->worker, reset))
903		return;
904	if(daemon->cfg->stat_extended) {
905		if(!print_mem(ssl, rc->worker, daemon))
906			return;
907		if(!print_hist(ssl, &total))
908			return;
909		if(!print_ext(ssl, &total))
910			return;
911	}
912}
913
914/** parse commandline argument domain name */
915static int
916parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs)
917{
918	uint8_t nm[LDNS_MAX_DOMAINLEN+1];
919	size_t nmlen = sizeof(nm);
920	int status;
921	*res = NULL;
922	*len = 0;
923	*labs = 0;
924	status = sldns_str2wire_dname_buf(str, nm, &nmlen);
925	if(status != 0) {
926		ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
927			LDNS_WIREPARSE_OFFSET(status),
928			sldns_get_errorstr_parse(status));
929		return 0;
930	}
931	*res = memdup(nm, nmlen);
932	if(!*res) {
933		ssl_printf(ssl, "error out of memory\n");
934		return 0;
935	}
936	*labs = dname_count_size_labels(*res, len);
937	return 1;
938}
939
940/** find second argument, modifies string */
941static int
942find_arg2(SSL* ssl, char* arg, char** arg2)
943{
944	char* as = strchr(arg, ' ');
945	char* at = strchr(arg, '\t');
946	if(as && at) {
947		if(at < as)
948			as = at;
949		as[0]=0;
950		*arg2 = skipwhite(as+1);
951	} else if(as) {
952		as[0]=0;
953		*arg2 = skipwhite(as+1);
954	} else if(at) {
955		at[0]=0;
956		*arg2 = skipwhite(at+1);
957	} else {
958		ssl_printf(ssl, "error could not find next argument "
959			"after %s\n", arg);
960		return 0;
961	}
962	return 1;
963}
964
965/** Add a new zone */
966static void
967do_zone_add(SSL* ssl, struct worker* worker, char* arg)
968{
969	uint8_t* nm;
970	int nmlabs;
971	size_t nmlen;
972	char* arg2;
973	enum localzone_type t;
974	struct local_zone* z;
975	if(!find_arg2(ssl, arg, &arg2))
976		return;
977	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
978		return;
979	if(!local_zone_str2type(arg2, &t)) {
980		ssl_printf(ssl, "error not a zone type. %s\n", arg2);
981		free(nm);
982		return;
983	}
984	lock_rw_wrlock(&worker->daemon->local_zones->lock);
985	if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
986		nmlabs, LDNS_RR_CLASS_IN))) {
987		/* already present in tree */
988		lock_rw_wrlock(&z->lock);
989		z->type = t; /* update type anyway */
990		lock_rw_unlock(&z->lock);
991		free(nm);
992		lock_rw_unlock(&worker->daemon->local_zones->lock);
993		send_ok(ssl);
994		return;
995	}
996	if(!local_zones_add_zone(worker->daemon->local_zones, nm, nmlen,
997		nmlabs, LDNS_RR_CLASS_IN, t)) {
998		lock_rw_unlock(&worker->daemon->local_zones->lock);
999		ssl_printf(ssl, "error out of memory\n");
1000		return;
1001	}
1002	lock_rw_unlock(&worker->daemon->local_zones->lock);
1003	send_ok(ssl);
1004}
1005
1006/** Remove a zone */
1007static void
1008do_zone_remove(SSL* ssl, struct worker* worker, char* arg)
1009{
1010	uint8_t* nm;
1011	int nmlabs;
1012	size_t nmlen;
1013	struct local_zone* z;
1014	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1015		return;
1016	lock_rw_wrlock(&worker->daemon->local_zones->lock);
1017	if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1018		nmlabs, LDNS_RR_CLASS_IN))) {
1019		/* present in tree */
1020		local_zones_del_zone(worker->daemon->local_zones, z);
1021	}
1022	lock_rw_unlock(&worker->daemon->local_zones->lock);
1023	free(nm);
1024	send_ok(ssl);
1025}
1026
1027/** Add new RR data */
1028static void
1029do_data_add(SSL* ssl, struct worker* worker, char* arg)
1030{
1031	if(!local_zones_add_RR(worker->daemon->local_zones, arg)) {
1032		ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1033		return;
1034	}
1035	send_ok(ssl);
1036}
1037
1038/** Remove RR data */
1039static void
1040do_data_remove(SSL* ssl, struct worker* worker, char* arg)
1041{
1042	uint8_t* nm;
1043	int nmlabs;
1044	size_t nmlen;
1045	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1046		return;
1047	local_zones_del_data(worker->daemon->local_zones, nm,
1048		nmlen, nmlabs, LDNS_RR_CLASS_IN);
1049	free(nm);
1050	send_ok(ssl);
1051}
1052
1053/** cache lookup of nameservers */
1054static void
1055do_lookup(SSL* ssl, struct worker* worker, char* arg)
1056{
1057	uint8_t* nm;
1058	int nmlabs;
1059	size_t nmlen;
1060	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1061		return;
1062	(void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1063	free(nm);
1064}
1065
1066/** flush something from rrset and msg caches */
1067static void
1068do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1069	uint16_t t, uint16_t c)
1070{
1071	hashvalue_t h;
1072	struct query_info k;
1073	rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1074	if(t == LDNS_RR_TYPE_SOA)
1075		rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1076			PACKED_RRSET_SOA_NEG);
1077	k.qname = nm;
1078	k.qname_len = nmlen;
1079	k.qtype = t;
1080	k.qclass = c;
1081	h = query_info_hash(&k);
1082	slabhash_remove(worker->env.msg_cache, h, &k);
1083}
1084
1085/** flush a type */
1086static void
1087do_flush_type(SSL* ssl, struct worker* worker, char* arg)
1088{
1089	uint8_t* nm;
1090	int nmlabs;
1091	size_t nmlen;
1092	char* arg2;
1093	uint16_t t;
1094	if(!find_arg2(ssl, arg, &arg2))
1095		return;
1096	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1097		return;
1098	t = sldns_get_rr_type_by_name(arg2);
1099	do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1100
1101	free(nm);
1102	send_ok(ssl);
1103}
1104
1105/** flush statistics */
1106static void
1107do_flush_stats(SSL* ssl, struct worker* worker)
1108{
1109	worker_stats_clear(worker);
1110	send_ok(ssl);
1111}
1112
1113/**
1114 * Local info for deletion functions
1115 */
1116struct del_info {
1117	/** worker */
1118	struct worker* worker;
1119	/** name to delete */
1120	uint8_t* name;
1121	/** length */
1122	size_t len;
1123	/** labels */
1124	int labs;
1125	/** now */
1126	time_t now;
1127	/** time to invalidate to */
1128	time_t expired;
1129	/** number of rrsets removed */
1130	size_t num_rrsets;
1131	/** number of msgs removed */
1132	size_t num_msgs;
1133	/** number of key entries removed */
1134	size_t num_keys;
1135	/** length of addr */
1136	socklen_t addrlen;
1137	/** socket address for host deletion */
1138	struct sockaddr_storage addr;
1139};
1140
1141/** callback to delete hosts in infra cache */
1142static void
1143infra_del_host(struct lruhash_entry* e, void* arg)
1144{
1145	/* entry is locked */
1146	struct del_info* inf = (struct del_info*)arg;
1147	struct infra_key* k = (struct infra_key*)e->key;
1148	if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1149		struct infra_data* d = (struct infra_data*)e->data;
1150		d->probedelay = 0;
1151		d->timeout_A = 0;
1152		d->timeout_AAAA = 0;
1153		d->timeout_other = 0;
1154		rtt_init(&d->rtt);
1155		if(d->ttl >= inf->now) {
1156			d->ttl = inf->expired;
1157			inf->num_keys++;
1158		}
1159	}
1160}
1161
1162/** flush infra cache */
1163static void
1164do_flush_infra(SSL* ssl, struct worker* worker, char* arg)
1165{
1166	struct sockaddr_storage addr;
1167	socklen_t len;
1168	struct del_info inf;
1169	if(strcmp(arg, "all") == 0) {
1170		slabhash_clear(worker->env.infra_cache->hosts);
1171		send_ok(ssl);
1172		return;
1173	}
1174	if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1175		(void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1176		return;
1177	}
1178	/* delete all entries from cache */
1179	/* what we do is to set them all expired */
1180	inf.worker = worker;
1181	inf.name = 0;
1182	inf.len = 0;
1183	inf.labs = 0;
1184	inf.now = *worker->env.now;
1185	inf.expired = *worker->env.now;
1186	inf.expired -= 3; /* handle 3 seconds skew between threads */
1187	inf.num_rrsets = 0;
1188	inf.num_msgs = 0;
1189	inf.num_keys = 0;
1190	inf.addrlen = len;
1191	memmove(&inf.addr, &addr, len);
1192	slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1193		&inf);
1194	send_ok(ssl);
1195}
1196
1197/** flush requestlist */
1198static void
1199do_flush_requestlist(SSL* ssl, struct worker* worker)
1200{
1201	mesh_delete_all(worker->env.mesh);
1202	send_ok(ssl);
1203}
1204
1205/** callback to delete rrsets in a zone */
1206static void
1207zone_del_rrset(struct lruhash_entry* e, void* arg)
1208{
1209	/* entry is locked */
1210	struct del_info* inf = (struct del_info*)arg;
1211	struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1212	if(dname_subdomain_c(k->rk.dname, inf->name)) {
1213		struct packed_rrset_data* d =
1214			(struct packed_rrset_data*)e->data;
1215		if(d->ttl >= inf->now) {
1216			d->ttl = inf->expired;
1217			inf->num_rrsets++;
1218		}
1219	}
1220}
1221
1222/** callback to delete messages in a zone */
1223static void
1224zone_del_msg(struct lruhash_entry* e, void* arg)
1225{
1226	/* entry is locked */
1227	struct del_info* inf = (struct del_info*)arg;
1228	struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1229	if(dname_subdomain_c(k->key.qname, inf->name)) {
1230		struct reply_info* d = (struct reply_info*)e->data;
1231		if(d->ttl >= inf->now) {
1232			d->ttl = inf->expired;
1233			inf->num_msgs++;
1234		}
1235	}
1236}
1237
1238/** callback to delete keys in zone */
1239static void
1240zone_del_kcache(struct lruhash_entry* e, void* arg)
1241{
1242	/* entry is locked */
1243	struct del_info* inf = (struct del_info*)arg;
1244	struct key_entry_key* k = (struct key_entry_key*)e->key;
1245	if(dname_subdomain_c(k->name, inf->name)) {
1246		struct key_entry_data* d = (struct key_entry_data*)e->data;
1247		if(d->ttl >= inf->now) {
1248			d->ttl = inf->expired;
1249			inf->num_keys++;
1250		}
1251	}
1252}
1253
1254/** remove all rrsets and keys from zone from cache */
1255static void
1256do_flush_zone(SSL* ssl, struct worker* worker, char* arg)
1257{
1258	uint8_t* nm;
1259	int nmlabs;
1260	size_t nmlen;
1261	struct del_info inf;
1262	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1263		return;
1264	/* delete all RRs and key entries from zone */
1265	/* what we do is to set them all expired */
1266	inf.worker = worker;
1267	inf.name = nm;
1268	inf.len = nmlen;
1269	inf.labs = nmlabs;
1270	inf.now = *worker->env.now;
1271	inf.expired = *worker->env.now;
1272	inf.expired -= 3; /* handle 3 seconds skew between threads */
1273	inf.num_rrsets = 0;
1274	inf.num_msgs = 0;
1275	inf.num_keys = 0;
1276	slabhash_traverse(&worker->env.rrset_cache->table, 1,
1277		&zone_del_rrset, &inf);
1278
1279	slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1280
1281	/* and validator cache */
1282	if(worker->env.key_cache) {
1283		slabhash_traverse(worker->env.key_cache->slab, 1,
1284			&zone_del_kcache, &inf);
1285	}
1286
1287	free(nm);
1288
1289	(void)ssl_printf(ssl, "ok removed %u rrsets, %u messages "
1290		"and %u key entries\n", (unsigned)inf.num_rrsets,
1291		(unsigned)inf.num_msgs, (unsigned)inf.num_keys);
1292}
1293
1294/** callback to delete bogus rrsets */
1295static void
1296bogus_del_rrset(struct lruhash_entry* e, void* arg)
1297{
1298	/* entry is locked */
1299	struct del_info* inf = (struct del_info*)arg;
1300	struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1301	if(d->security == sec_status_bogus) {
1302		d->ttl = inf->expired;
1303		inf->num_rrsets++;
1304	}
1305}
1306
1307/** callback to delete bogus messages */
1308static void
1309bogus_del_msg(struct lruhash_entry* e, void* arg)
1310{
1311	/* entry is locked */
1312	struct del_info* inf = (struct del_info*)arg;
1313	struct reply_info* d = (struct reply_info*)e->data;
1314	if(d->security == sec_status_bogus) {
1315		d->ttl = inf->expired;
1316		inf->num_msgs++;
1317	}
1318}
1319
1320/** callback to delete bogus keys */
1321static void
1322bogus_del_kcache(struct lruhash_entry* e, void* arg)
1323{
1324	/* entry is locked */
1325	struct del_info* inf = (struct del_info*)arg;
1326	struct key_entry_data* d = (struct key_entry_data*)e->data;
1327	if(d->isbad) {
1328		d->ttl = inf->expired;
1329		inf->num_keys++;
1330	}
1331}
1332
1333/** remove all rrsets and keys from zone from cache */
1334static void
1335do_flush_bogus(SSL* ssl, struct worker* worker)
1336{
1337	struct del_info inf;
1338	/* what we do is to set them all expired */
1339	inf.worker = worker;
1340	inf.now = *worker->env.now;
1341	inf.expired = *worker->env.now;
1342	inf.expired -= 3; /* handle 3 seconds skew between threads */
1343	inf.num_rrsets = 0;
1344	inf.num_msgs = 0;
1345	inf.num_keys = 0;
1346	slabhash_traverse(&worker->env.rrset_cache->table, 1,
1347		&bogus_del_rrset, &inf);
1348
1349	slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1350
1351	/* and validator cache */
1352	if(worker->env.key_cache) {
1353		slabhash_traverse(worker->env.key_cache->slab, 1,
1354			&bogus_del_kcache, &inf);
1355	}
1356
1357	(void)ssl_printf(ssl, "ok removed %u rrsets, %u messages "
1358		"and %u key entries\n", (unsigned)inf.num_rrsets,
1359		(unsigned)inf.num_msgs, (unsigned)inf.num_keys);
1360}
1361
1362/** remove name rrset from cache */
1363static void
1364do_flush_name(SSL* ssl, struct worker* w, char* arg)
1365{
1366	uint8_t* nm;
1367	int nmlabs;
1368	size_t nmlen;
1369	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1370		return;
1371	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1372	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1373	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1374	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1375	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1376	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1377	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1378	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1379	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1380	do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1381
1382	free(nm);
1383	send_ok(ssl);
1384}
1385
1386/** printout a delegation point info */
1387static int
1388ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1389	struct delegpt* dp)
1390{
1391	char buf[257];
1392	struct delegpt_ns* ns;
1393	struct delegpt_addr* a;
1394	int f = 0;
1395	if(str) { /* print header for forward, stub */
1396		char* c = sldns_wire2str_class(dclass);
1397		dname_str(nm, buf);
1398		if(!ssl_printf(ssl, "%s %s %s: ", buf, (c?c:"CLASS??"), str)) {
1399			free(c);
1400			return 0;
1401		}
1402		free(c);
1403	}
1404	for(ns = dp->nslist; ns; ns = ns->next) {
1405		dname_str(ns->name, buf);
1406		if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1407			return 0;
1408		f = 1;
1409	}
1410	for(a = dp->target_list; a; a = a->next_target) {
1411		addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1412		if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1413			return 0;
1414		f = 1;
1415	}
1416	return ssl_printf(ssl, "\n");
1417}
1418
1419
1420/** print root forwards */
1421static int
1422print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root)
1423{
1424	struct delegpt* dp;
1425	dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1426	if(!dp)
1427		return ssl_printf(ssl, "off (using root hints)\n");
1428	/* if dp is returned it must be the root */
1429	log_assert(query_dname_compare(dp->name, root)==0);
1430	return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1431}
1432
1433/** parse args into delegpt */
1434static struct delegpt*
1435parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names)
1436{
1437	/* parse args and add in */
1438	char* p = args;
1439	char* todo;
1440	struct delegpt* dp = delegpt_create_mlc(nm);
1441	struct sockaddr_storage addr;
1442	socklen_t addrlen;
1443	if(!dp) {
1444		(void)ssl_printf(ssl, "error out of memory\n");
1445		return NULL;
1446	}
1447	while(p) {
1448		todo = p;
1449		p = strchr(p, ' '); /* find next spot, if any */
1450		if(p) {
1451			*p++ = 0;	/* end this spot */
1452			p = skipwhite(p); /* position at next spot */
1453		}
1454		/* parse address */
1455		if(!extstrtoaddr(todo, &addr, &addrlen)) {
1456			if(allow_names) {
1457				uint8_t* n = NULL;
1458				size_t ln;
1459				int lb;
1460				if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
1461					(void)ssl_printf(ssl, "error cannot "
1462						"parse IP address or name "
1463						"'%s'\n", todo);
1464					delegpt_free_mlc(dp);
1465					return NULL;
1466				}
1467				if(!delegpt_add_ns_mlc(dp, n, 0)) {
1468					(void)ssl_printf(ssl, "error out of memory\n");
1469					free(n);
1470					delegpt_free_mlc(dp);
1471					return NULL;
1472				}
1473				free(n);
1474
1475			} else {
1476				(void)ssl_printf(ssl, "error cannot parse"
1477					" IP address '%s'\n", todo);
1478				delegpt_free_mlc(dp);
1479				return NULL;
1480			}
1481		} else {
1482			/* add address */
1483			if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) {
1484				(void)ssl_printf(ssl, "error out of memory\n");
1485				delegpt_free_mlc(dp);
1486				return NULL;
1487			}
1488		}
1489	}
1490	return dp;
1491}
1492
1493/** do the status command */
1494static void
1495do_forward(SSL* ssl, struct worker* worker, char* args)
1496{
1497	struct iter_forwards* fwd = worker->env.fwds;
1498	uint8_t* root = (uint8_t*)"\000";
1499	if(!fwd) {
1500		(void)ssl_printf(ssl, "error: structure not allocated\n");
1501		return;
1502	}
1503	if(args == NULL || args[0] == 0) {
1504		(void)print_root_fwds(ssl, fwd, root);
1505		return;
1506	}
1507	/* set root forwards for this thread. since we are in remote control
1508	 * the actual mesh is not running, so we can freely edit it. */
1509	/* delete all the existing queries first */
1510	mesh_delete_all(worker->env.mesh);
1511	if(strcmp(args, "off") == 0) {
1512		forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
1513	} else {
1514		struct delegpt* dp;
1515		if(!(dp = parse_delegpt(ssl, args, root, 0)))
1516			return;
1517		if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1518			(void)ssl_printf(ssl, "error out of memory\n");
1519			return;
1520		}
1521	}
1522	send_ok(ssl);
1523}
1524
1525static int
1526parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp,
1527	int* insecure, int* prime)
1528{
1529	char* zonename;
1530	char* rest;
1531	size_t nmlen;
1532	int nmlabs;
1533	/* parse all -x args */
1534	while(args[0] == '+') {
1535		if(!find_arg2(ssl, args, &rest))
1536			return 0;
1537		while(*(++args) != 0) {
1538			if(*args == 'i' && insecure)
1539				*insecure = 1;
1540			else if(*args == 'p' && prime)
1541				*prime = 1;
1542			else {
1543				(void)ssl_printf(ssl, "error: unknown option %s\n", args);
1544				return 0;
1545			}
1546		}
1547		args = rest;
1548	}
1549	/* parse name */
1550	if(dp) {
1551		if(!find_arg2(ssl, args, &rest))
1552			return 0;
1553		zonename = args;
1554		args = rest;
1555	} else	zonename = args;
1556	if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
1557		return 0;
1558
1559	/* parse dp */
1560	if(dp) {
1561		if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
1562			free(*nm);
1563			return 0;
1564		}
1565	}
1566	return 1;
1567}
1568
1569/** do the forward_add command */
1570static void
1571do_forward_add(SSL* ssl, struct worker* worker, char* args)
1572{
1573	struct iter_forwards* fwd = worker->env.fwds;
1574	int insecure = 0;
1575	uint8_t* nm = NULL;
1576	struct delegpt* dp = NULL;
1577	if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
1578		return;
1579	if(insecure && worker->env.anchors) {
1580		if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1581			nm)) {
1582			(void)ssl_printf(ssl, "error out of memory\n");
1583			delegpt_free_mlc(dp);
1584			free(nm);
1585			return;
1586		}
1587	}
1588	if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1589		(void)ssl_printf(ssl, "error out of memory\n");
1590		free(nm);
1591		return;
1592	}
1593	free(nm);
1594	send_ok(ssl);
1595}
1596
1597/** do the forward_remove command */
1598static void
1599do_forward_remove(SSL* ssl, struct worker* worker, char* args)
1600{
1601	struct iter_forwards* fwd = worker->env.fwds;
1602	int insecure = 0;
1603	uint8_t* nm = NULL;
1604	if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1605		return;
1606	if(insecure && worker->env.anchors)
1607		anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1608			nm);
1609	forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
1610	free(nm);
1611	send_ok(ssl);
1612}
1613
1614/** do the stub_add command */
1615static void
1616do_stub_add(SSL* ssl, struct worker* worker, char* args)
1617{
1618	struct iter_forwards* fwd = worker->env.fwds;
1619	int insecure = 0, prime = 0;
1620	uint8_t* nm = NULL;
1621	struct delegpt* dp = NULL;
1622	if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
1623		return;
1624	if(insecure && worker->env.anchors) {
1625		if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1626			nm)) {
1627			(void)ssl_printf(ssl, "error out of memory\n");
1628			delegpt_free_mlc(dp);
1629			free(nm);
1630			return;
1631		}
1632	}
1633	if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
1634		if(insecure && worker->env.anchors)
1635			anchors_delete_insecure(worker->env.anchors,
1636				LDNS_RR_CLASS_IN, nm);
1637		(void)ssl_printf(ssl, "error out of memory\n");
1638		delegpt_free_mlc(dp);
1639		free(nm);
1640		return;
1641	}
1642	if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
1643		(void)ssl_printf(ssl, "error out of memory\n");
1644		forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1645		if(insecure && worker->env.anchors)
1646			anchors_delete_insecure(worker->env.anchors,
1647				LDNS_RR_CLASS_IN, nm);
1648		free(nm);
1649		return;
1650	}
1651	free(nm);
1652	send_ok(ssl);
1653}
1654
1655/** do the stub_remove command */
1656static void
1657do_stub_remove(SSL* ssl, struct worker* worker, char* args)
1658{
1659	struct iter_forwards* fwd = worker->env.fwds;
1660	int insecure = 0;
1661	uint8_t* nm = NULL;
1662	if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1663		return;
1664	if(insecure && worker->env.anchors)
1665		anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1666			nm);
1667	forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1668	hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
1669	free(nm);
1670	send_ok(ssl);
1671}
1672
1673/** do the insecure_add command */
1674static void
1675do_insecure_add(SSL* ssl, struct worker* worker, char* arg)
1676{
1677	size_t nmlen;
1678	int nmlabs;
1679	uint8_t* nm = NULL;
1680	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1681		return;
1682	if(worker->env.anchors) {
1683		if(!anchors_add_insecure(worker->env.anchors,
1684			LDNS_RR_CLASS_IN, nm)) {
1685			(void)ssl_printf(ssl, "error out of memory\n");
1686			free(nm);
1687			return;
1688		}
1689	}
1690	free(nm);
1691	send_ok(ssl);
1692}
1693
1694/** do the insecure_remove command */
1695static void
1696do_insecure_remove(SSL* ssl, struct worker* worker, char* arg)
1697{
1698	size_t nmlen;
1699	int nmlabs;
1700	uint8_t* nm = NULL;
1701	if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1702		return;
1703	if(worker->env.anchors)
1704		anchors_delete_insecure(worker->env.anchors,
1705			LDNS_RR_CLASS_IN, nm);
1706	free(nm);
1707	send_ok(ssl);
1708}
1709
1710/** do the status command */
1711static void
1712do_status(SSL* ssl, struct worker* worker)
1713{
1714	int i;
1715	time_t uptime;
1716	if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
1717		return;
1718	if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
1719		return;
1720	if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
1721		return;
1722	if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
1723		return;
1724	for(i=0; i<worker->daemon->mods.num; i++) {
1725		if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
1726			return;
1727	}
1728	if(!ssl_printf(ssl, " ]\n"))
1729		return;
1730	uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
1731	if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
1732		return;
1733	if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
1734		(int)getpid()))
1735		return;
1736}
1737
1738/** get age for the mesh state */
1739static void
1740get_mesh_age(struct mesh_state* m, char* buf, size_t len,
1741	struct module_env* env)
1742{
1743	if(m->reply_list) {
1744		struct timeval d;
1745		struct mesh_reply* r = m->reply_list;
1746		/* last reply is the oldest */
1747		while(r && r->next)
1748			r = r->next;
1749		timeval_subtract(&d, env->now_tv, &r->start_time);
1750		snprintf(buf, len, ARG_LL "d.%6.6d",
1751			(long long)d.tv_sec, (int)d.tv_usec);
1752	} else {
1753		snprintf(buf, len, "-");
1754	}
1755}
1756
1757/** get status of a mesh state */
1758static void
1759get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
1760	char* buf, size_t len)
1761{
1762	enum module_ext_state s = m->s.ext_state[m->s.curmod];
1763	const char *modname = mesh->mods.mod[m->s.curmod]->name;
1764	size_t l;
1765	if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
1766		m->s.minfo[m->s.curmod]) {
1767		/* break into iterator to find out who its waiting for */
1768		struct iter_qstate* qstate = (struct iter_qstate*)
1769			m->s.minfo[m->s.curmod];
1770		struct outbound_list* ol = &qstate->outlist;
1771		struct outbound_entry* e;
1772		snprintf(buf, len, "%s wait for", modname);
1773		l = strlen(buf);
1774		buf += l; len -= l;
1775		if(ol->first == NULL)
1776			snprintf(buf, len, " (empty_list)");
1777		for(e = ol->first; e; e = e->next) {
1778			snprintf(buf, len, " ");
1779			l = strlen(buf);
1780			buf += l; len -= l;
1781			addr_to_str(&e->qsent->addr, e->qsent->addrlen,
1782				buf, len);
1783			l = strlen(buf);
1784			buf += l; len -= l;
1785		}
1786	} else if(s == module_wait_subquery) {
1787		/* look in subs from mesh state to see what */
1788		char nm[257];
1789		struct mesh_state_ref* sub;
1790		snprintf(buf, len, "%s wants", modname);
1791		l = strlen(buf);
1792		buf += l; len -= l;
1793		if(m->sub_set.count == 0)
1794			snprintf(buf, len, " (empty_list)");
1795		RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
1796			char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
1797			char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
1798			dname_str(sub->s->s.qinfo.qname, nm);
1799			snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
1800				(c?c:"CLASS??"), nm);
1801			l = strlen(buf);
1802			buf += l; len -= l;
1803			free(t);
1804			free(c);
1805		}
1806	} else {
1807		snprintf(buf, len, "%s is %s", modname, strextstate(s));
1808	}
1809}
1810
1811/** do the dump_requestlist command */
1812static void
1813do_dump_requestlist(SSL* ssl, struct worker* worker)
1814{
1815	struct mesh_area* mesh;
1816	struct mesh_state* m;
1817	int num = 0;
1818	char buf[257];
1819	char timebuf[32];
1820	char statbuf[10240];
1821	if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
1822		return;
1823	if(!ssl_printf(ssl, "#   type cl name    seconds    module status\n"))
1824		return;
1825	/* show worker mesh contents */
1826	mesh = worker->env.mesh;
1827	if(!mesh) return;
1828	RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
1829		char* t = sldns_wire2str_type(m->s.qinfo.qtype);
1830		char* c = sldns_wire2str_class(m->s.qinfo.qclass);
1831		dname_str(m->s.qinfo.qname, buf);
1832		get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
1833		get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
1834		if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
1835			num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
1836			statbuf)) {
1837			free(t);
1838			free(c);
1839			return;
1840		}
1841		num++;
1842		free(t);
1843		free(c);
1844	}
1845}
1846
1847/** structure for argument data for dump infra host */
1848struct infra_arg {
1849	/** the infra cache */
1850	struct infra_cache* infra;
1851	/** the SSL connection */
1852	SSL* ssl;
1853	/** the time now */
1854	time_t now;
1855};
1856
1857/** callback for every host element in the infra cache */
1858static void
1859dump_infra_host(struct lruhash_entry* e, void* arg)
1860{
1861	struct infra_arg* a = (struct infra_arg*)arg;
1862	struct infra_key* k = (struct infra_key*)e->key;
1863	struct infra_data* d = (struct infra_data*)e->data;
1864	char ip_str[1024];
1865	char name[257];
1866	addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
1867	dname_str(k->zonename, name);
1868	/* skip expired stuff (only backed off) */
1869	if(d->ttl < a->now) {
1870		if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
1871			if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
1872				name, d->rtt.rto)) return;
1873		}
1874		return;
1875	}
1876	if(!ssl_printf(a->ssl, "%s %s ttl %d ping %d var %d rtt %d rto %d "
1877		"tA %d tAAAA %d tother %d "
1878		"ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
1879		"other %d\n", ip_str, name, (int)(d->ttl - a->now),
1880		d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
1881		d->timeout_A, d->timeout_AAAA, d->timeout_other,
1882		(int)d->edns_lame_known, (int)d->edns_version,
1883		(int)(a->now<d->probedelay?d->probedelay-a->now:0),
1884		(int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
1885		(int)d->lame_other))
1886		return;
1887}
1888
1889/** do the dump_infra command */
1890static void
1891do_dump_infra(SSL* ssl, struct worker* worker)
1892{
1893	struct infra_arg arg;
1894	arg.infra = worker->env.infra_cache;
1895	arg.ssl = ssl;
1896	arg.now = *worker->env.now;
1897	slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
1898}
1899
1900/** do the log_reopen command */
1901static void
1902do_log_reopen(SSL* ssl, struct worker* worker)
1903{
1904	struct config_file* cfg = worker->env.cfg;
1905	send_ok(ssl);
1906	log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
1907}
1908
1909/** do the set_option command */
1910static void
1911do_set_option(SSL* ssl, struct worker* worker, char* arg)
1912{
1913	char* arg2;
1914	if(!find_arg2(ssl, arg, &arg2))
1915		return;
1916	if(!config_set_option(worker->env.cfg, arg, arg2)) {
1917		(void)ssl_printf(ssl, "error setting option\n");
1918		return;
1919	}
1920	send_ok(ssl);
1921}
1922
1923/* routine to printout option values over SSL */
1924void remote_get_opt_ssl(char* line, void* arg)
1925{
1926	SSL* ssl = (SSL*)arg;
1927	(void)ssl_printf(ssl, "%s\n", line);
1928}
1929
1930/** do the get_option command */
1931static void
1932do_get_option(SSL* ssl, struct worker* worker, char* arg)
1933{
1934	int r;
1935	r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
1936	if(!r) {
1937		(void)ssl_printf(ssl, "error unknown option\n");
1938		return;
1939	}
1940}
1941
1942/** do the list_forwards command */
1943static void
1944do_list_forwards(SSL* ssl, struct worker* worker)
1945{
1946	/* since its a per-worker structure no locks needed */
1947	struct iter_forwards* fwds = worker->env.fwds;
1948	struct iter_forward_zone* z;
1949	RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
1950		if(!z->dp) continue; /* skip empty marker for stub */
1951		if(!ssl_print_name_dp(ssl, "forward", z->name, z->dclass,
1952			z->dp))
1953			return;
1954	}
1955}
1956
1957/** do the list_stubs command */
1958static void
1959do_list_stubs(SSL* ssl, struct worker* worker)
1960{
1961	struct iter_hints_stub* z;
1962	RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
1963		if(!ssl_print_name_dp(ssl,
1964			z->noprime?"stub noprime":"stub prime", z->node.name,
1965			z->node.dclass, z->dp))
1966			return;
1967	}
1968}
1969
1970/** do the list_local_zones command */
1971static void
1972do_list_local_zones(SSL* ssl, struct worker* worker)
1973{
1974	struct local_zones* zones = worker->daemon->local_zones;
1975	struct local_zone* z;
1976	char buf[257];
1977	lock_rw_rdlock(&zones->lock);
1978	RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
1979		lock_rw_rdlock(&z->lock);
1980		dname_str(z->name, buf);
1981		(void)ssl_printf(ssl, "%s %s\n", buf,
1982			local_zone_type2str(z->type));
1983		lock_rw_unlock(&z->lock);
1984	}
1985	lock_rw_unlock(&zones->lock);
1986}
1987
1988/** do the list_local_data command */
1989static void
1990do_list_local_data(SSL* ssl, struct worker* worker)
1991{
1992	struct local_zones* zones = worker->daemon->local_zones;
1993	struct local_zone* z;
1994	struct local_data* d;
1995	struct local_rrset* p;
1996	char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
1997	size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
1998	lock_rw_rdlock(&zones->lock);
1999	RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2000		lock_rw_rdlock(&z->lock);
2001		RBTREE_FOR(d, struct local_data*, &z->data) {
2002			for(p = d->rrsets; p; p = p->next) {
2003				struct packed_rrset_data* d =
2004					(struct packed_rrset_data*)p->rrset->entry.data;
2005				size_t i;
2006				for(i=0; i<d->count + d->rrsig_count; i++) {
2007					if(!packed_rr_to_string(p->rrset, i,
2008						0, s, slen)) {
2009						if(!ssl_printf(ssl, "BADRR\n"))
2010							return;
2011					}
2012				        if(!ssl_printf(ssl, "%s\n", s))
2013						return;
2014				}
2015			}
2016		}
2017		lock_rw_unlock(&z->lock);
2018	}
2019	lock_rw_unlock(&zones->lock);
2020}
2021
2022/** tell other processes to execute the command */
2023static void
2024distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd)
2025{
2026	int i;
2027	if(!cmd || !ssl)
2028		return;
2029	/* skip i=0 which is me */
2030	for(i=1; i<rc->worker->daemon->num; i++) {
2031		worker_send_cmd(rc->worker->daemon->workers[i],
2032			worker_cmd_remote);
2033		if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2034			(uint8_t*)cmd, strlen(cmd)+1, 0)) {
2035			ssl_printf(ssl, "error could not distribute cmd\n");
2036			return;
2037		}
2038	}
2039}
2040
2041/** check for name with end-of-string, space or tab after it */
2042static int
2043cmdcmp(char* p, const char* cmd, size_t len)
2044{
2045	return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2046}
2047
2048/** execute a remote control command */
2049static void
2050execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd,
2051	struct worker* worker)
2052{
2053	char* p = skipwhite(cmd);
2054	/* compare command */
2055	if(cmdcmp(p, "stop", 4)) {
2056		do_stop(ssl, rc);
2057		return;
2058	} else if(cmdcmp(p, "reload", 6)) {
2059		do_reload(ssl, rc);
2060		return;
2061	} else if(cmdcmp(p, "stats_noreset", 13)) {
2062		do_stats(ssl, rc, 0);
2063		return;
2064	} else if(cmdcmp(p, "stats", 5)) {
2065		do_stats(ssl, rc, 1);
2066		return;
2067	} else if(cmdcmp(p, "status", 6)) {
2068		do_status(ssl, worker);
2069		return;
2070	} else if(cmdcmp(p, "dump_cache", 10)) {
2071		(void)dump_cache(ssl, worker);
2072		return;
2073	} else if(cmdcmp(p, "load_cache", 10)) {
2074		if(load_cache(ssl, worker)) send_ok(ssl);
2075		return;
2076	} else if(cmdcmp(p, "list_forwards", 13)) {
2077		do_list_forwards(ssl, worker);
2078		return;
2079	} else if(cmdcmp(p, "list_stubs", 10)) {
2080		do_list_stubs(ssl, worker);
2081		return;
2082	} else if(cmdcmp(p, "list_local_zones", 16)) {
2083		do_list_local_zones(ssl, worker);
2084		return;
2085	} else if(cmdcmp(p, "list_local_data", 15)) {
2086		do_list_local_data(ssl, worker);
2087		return;
2088	} else if(cmdcmp(p, "stub_add", 8)) {
2089		/* must always distribute this cmd */
2090		if(rc) distribute_cmd(rc, ssl, cmd);
2091		do_stub_add(ssl, worker, skipwhite(p+8));
2092		return;
2093	} else if(cmdcmp(p, "stub_remove", 11)) {
2094		/* must always distribute this cmd */
2095		if(rc) distribute_cmd(rc, ssl, cmd);
2096		do_stub_remove(ssl, worker, skipwhite(p+11));
2097		return;
2098	} else if(cmdcmp(p, "forward_add", 11)) {
2099		/* must always distribute this cmd */
2100		if(rc) distribute_cmd(rc, ssl, cmd);
2101		do_forward_add(ssl, worker, skipwhite(p+11));
2102		return;
2103	} else if(cmdcmp(p, "forward_remove", 14)) {
2104		/* must always distribute this cmd */
2105		if(rc) distribute_cmd(rc, ssl, cmd);
2106		do_forward_remove(ssl, worker, skipwhite(p+14));
2107		return;
2108	} else if(cmdcmp(p, "insecure_add", 12)) {
2109		/* must always distribute this cmd */
2110		if(rc) distribute_cmd(rc, ssl, cmd);
2111		do_insecure_add(ssl, worker, skipwhite(p+12));
2112		return;
2113	} else if(cmdcmp(p, "insecure_remove", 15)) {
2114		/* must always distribute this cmd */
2115		if(rc) distribute_cmd(rc, ssl, cmd);
2116		do_insecure_remove(ssl, worker, skipwhite(p+15));
2117		return;
2118	} else if(cmdcmp(p, "forward", 7)) {
2119		/* must always distribute this cmd */
2120		if(rc) distribute_cmd(rc, ssl, cmd);
2121		do_forward(ssl, worker, skipwhite(p+7));
2122		return;
2123	} else if(cmdcmp(p, "flush_stats", 11)) {
2124		/* must always distribute this cmd */
2125		if(rc) distribute_cmd(rc, ssl, cmd);
2126		do_flush_stats(ssl, worker);
2127		return;
2128	} else if(cmdcmp(p, "flush_requestlist", 17)) {
2129		/* must always distribute this cmd */
2130		if(rc) distribute_cmd(rc, ssl, cmd);
2131		do_flush_requestlist(ssl, worker);
2132		return;
2133	} else if(cmdcmp(p, "lookup", 6)) {
2134		do_lookup(ssl, worker, skipwhite(p+6));
2135		return;
2136	}
2137
2138#ifdef THREADS_DISABLED
2139	/* other processes must execute the command as well */
2140	/* commands that should not be distributed, returned above. */
2141	if(rc) { /* only if this thread is the master (rc) thread */
2142		/* done before the code below, which may split the string */
2143		distribute_cmd(rc, ssl, cmd);
2144	}
2145#endif
2146	if(cmdcmp(p, "verbosity", 9)) {
2147		do_verbosity(ssl, skipwhite(p+9));
2148	} else if(cmdcmp(p, "local_zone_remove", 17)) {
2149		do_zone_remove(ssl, worker, skipwhite(p+17));
2150	} else if(cmdcmp(p, "local_zone", 10)) {
2151		do_zone_add(ssl, worker, skipwhite(p+10));
2152	} else if(cmdcmp(p, "local_data_remove", 17)) {
2153		do_data_remove(ssl, worker, skipwhite(p+17));
2154	} else if(cmdcmp(p, "local_data", 10)) {
2155		do_data_add(ssl, worker, skipwhite(p+10));
2156	} else if(cmdcmp(p, "flush_zone", 10)) {
2157		do_flush_zone(ssl, worker, skipwhite(p+10));
2158	} else if(cmdcmp(p, "flush_type", 10)) {
2159		do_flush_type(ssl, worker, skipwhite(p+10));
2160	} else if(cmdcmp(p, "flush_infra", 11)) {
2161		do_flush_infra(ssl, worker, skipwhite(p+11));
2162	} else if(cmdcmp(p, "flush", 5)) {
2163		do_flush_name(ssl, worker, skipwhite(p+5));
2164	} else if(cmdcmp(p, "dump_requestlist", 16)) {
2165		do_dump_requestlist(ssl, worker);
2166	} else if(cmdcmp(p, "dump_infra", 10)) {
2167		do_dump_infra(ssl, worker);
2168	} else if(cmdcmp(p, "log_reopen", 10)) {
2169		do_log_reopen(ssl, worker);
2170	} else if(cmdcmp(p, "set_option", 10)) {
2171		do_set_option(ssl, worker, skipwhite(p+10));
2172	} else if(cmdcmp(p, "get_option", 10)) {
2173		do_get_option(ssl, worker, skipwhite(p+10));
2174	} else if(cmdcmp(p, "flush_bogus", 11)) {
2175		do_flush_bogus(ssl, worker);
2176	} else {
2177		(void)ssl_printf(ssl, "error unknown command '%s'\n", p);
2178	}
2179}
2180
2181void
2182daemon_remote_exec(struct worker* worker)
2183{
2184	/* read the cmd string */
2185	uint8_t* msg = NULL;
2186	uint32_t len = 0;
2187	if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
2188		log_err("daemon_remote_exec: tube_read_msg failed");
2189		return;
2190	}
2191	verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
2192	execute_cmd(NULL, NULL, (char*)msg, worker);
2193	free(msg);
2194}
2195
2196/** handle remote control request */
2197static void
2198handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl)
2199{
2200	int r;
2201	char pre[10];
2202	char magic[7];
2203	char buf[1024];
2204#ifdef USE_WINSOCK
2205	/* makes it possible to set the socket blocking again. */
2206	/* basically removes it from winsock_event ... */
2207	WSAEventSelect(s->c->fd, NULL, 0);
2208#endif
2209	fd_set_block(s->c->fd);
2210
2211	/* try to read magic UBCT[version]_space_ string */
2212	ERR_clear_error();
2213	if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) {
2214		if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN)
2215			return;
2216		log_crypto_err("could not SSL_read");
2217		return;
2218	}
2219	magic[6] = 0;
2220	if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
2221		verbose(VERB_QUERY, "control connection has bad magic string");
2222		/* probably wrong tool connected, ignore it completely */
2223		return;
2224	}
2225
2226	/* read the command line */
2227	if(!ssl_read_line(ssl, buf, sizeof(buf))) {
2228		return;
2229	}
2230	snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
2231	if(strcmp(magic, pre) != 0) {
2232		verbose(VERB_QUERY, "control connection had bad "
2233			"version %s, cmd: %s", magic, buf);
2234		ssl_printf(ssl, "error version mismatch\n");
2235		return;
2236	}
2237	verbose(VERB_DETAIL, "control cmd: %s", buf);
2238
2239	/* figure out what to do */
2240	execute_cmd(rc, ssl, buf, rc->worker);
2241}
2242
2243int remote_control_callback(struct comm_point* c, void* arg, int err,
2244	struct comm_reply* ATTR_UNUSED(rep))
2245{
2246	struct rc_state* s = (struct rc_state*)arg;
2247	struct daemon_remote* rc = s->rc;
2248	int r;
2249	if(err != NETEVENT_NOERROR) {
2250		if(err==NETEVENT_TIMEOUT)
2251			log_err("remote control timed out");
2252		clean_point(rc, s);
2253		return 0;
2254	}
2255	/* (continue to) setup the SSL connection */
2256	ERR_clear_error();
2257	r = SSL_do_handshake(s->ssl);
2258	if(r != 1) {
2259		int r2 = SSL_get_error(s->ssl, r);
2260		if(r2 == SSL_ERROR_WANT_READ) {
2261			if(s->shake_state == rc_hs_read) {
2262				/* try again later */
2263				return 0;
2264			}
2265			s->shake_state = rc_hs_read;
2266			comm_point_listen_for_rw(c, 1, 0);
2267			return 0;
2268		} else if(r2 == SSL_ERROR_WANT_WRITE) {
2269			if(s->shake_state == rc_hs_write) {
2270				/* try again later */
2271				return 0;
2272			}
2273			s->shake_state = rc_hs_write;
2274			comm_point_listen_for_rw(c, 0, 1);
2275			return 0;
2276		} else {
2277			if(r == 0)
2278				log_err("remote control connection closed prematurely");
2279			log_addr(1, "failed connection from",
2280				&s->c->repinfo.addr, s->c->repinfo.addrlen);
2281			log_crypto_err("remote control failed ssl");
2282			clean_point(rc, s);
2283			return 0;
2284		}
2285	}
2286	s->shake_state = rc_none;
2287
2288	/* once handshake has completed, check authentication */
2289	if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
2290		X509* x = SSL_get_peer_certificate(s->ssl);
2291		if(!x) {
2292			verbose(VERB_DETAIL, "remote control connection "
2293				"provided no client certificate");
2294			clean_point(rc, s);
2295			return 0;
2296		}
2297		verbose(VERB_ALGO, "remote control connection authenticated");
2298		X509_free(x);
2299	} else {
2300		verbose(VERB_DETAIL, "remote control connection failed to "
2301			"authenticate with client certificate");
2302		clean_point(rc, s);
2303		return 0;
2304	}
2305
2306	/* if OK start to actually handle the request */
2307	handle_req(rc, s, s->ssl);
2308
2309	verbose(VERB_ALGO, "remote control operation completed");
2310	clean_point(rc, s);
2311	return 0;
2312}
2313