2 * daemon/remote.c - remote control for the unbound daemon.
4 * Copyright (c) 2008, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
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 TLS capable web browser.
42 * The channel is secured using TLSv1, and certificates.
43 * Both the server and the client(control tool) have their own keys.
46 #ifdef HAVE_OPENSSL_ERR_H
47 #include <openssl/err.h>
49 #ifdef HAVE_OPENSSL_DH_H
50 #include <openssl/dh.h>
52 #ifdef HAVE_OPENSSL_BN_H
53 #include <openssl/bn.h>
57 #include "daemon/remote.h"
58 #include "daemon/worker.h"
59 #include "daemon/daemon.h"
60 #include "daemon/stats.h"
61 #include "daemon/cachedump.h"
63 #include "util/config_file.h"
64 #include "util/net_help.h"
65 #include "util/module.h"
66 #include "services/listen_dnsport.h"
67 #include "services/cache/rrset.h"
68 #include "services/cache/infra.h"
69 #include "services/mesh.h"
70 #include "services/localzone.h"
71 #include "util/storage/slabhash.h"
72 #include "util/fptr_wlist.h"
73 #include "util/data/dname.h"
74 #include "validator/validator.h"
75 #include "validator/val_kcache.h"
76 #include "validator/val_kentry.h"
77 #include "validator/val_anchor.h"
78 #include "iterator/iterator.h"
79 #include "iterator/iter_fwd.h"
80 #include "iterator/iter_hints.h"
81 #include "iterator/iter_delegpt.h"
82 #include "services/outbound_list.h"
83 #include "services/outside_network.h"
84 #include "sldns/str2wire.h"
85 #include "sldns/parseutil.h"
86 #include "sldns/wire2str.h"
87 #include "sldns/sbuffer.h"
89 #ifdef HAVE_SYS_TYPES_H
90 # include <sys/types.h>
92 #ifdef HAVE_SYS_STAT_H
99 /* just for portability */
104 /** what to put on statistics lines between var and value, ": " or "=" */
106 /** if true, inhibits a lot of =0 lines from the stats output */
107 static const int inhibit_zero = 1;
109 /** subtract timers and the values do not overflow or become negative */
111 timeval_subtract(struct timeval* d, const struct timeval* end,
112 const struct timeval* start)
115 time_t end_usec = end->tv_usec;
116 d->tv_sec = end->tv_sec - start->tv_sec;
117 if(end_usec < start->tv_usec) {
121 d->tv_usec = end_usec - start->tv_usec;
125 /** divide sum of timers to get average */
127 timeval_divide(struct timeval* avg, const struct timeval* sum, size_t d)
136 avg->tv_sec = sum->tv_sec / d;
137 avg->tv_usec = sum->tv_usec / d;
138 /* handle fraction from seconds divide */
139 leftover = sum->tv_sec - avg->tv_sec*d;
140 avg->tv_usec += (leftover*1000000)/d;
145 * The following function was generated using the openssl utility, using
146 * the command : "openssl dhparam -C 2048"
147 * (some openssl versions reject DH that is 'too small', eg. 512).
150 static DH *get_dh2048(void)
152 static unsigned char dh2048_p[]={
153 0xE7,0x36,0x28,0x3B,0xE4,0xC3,0x32,0x1C,0x01,0xC3,0x67,0xD6,
154 0xF5,0xF3,0xDA,0xDC,0x71,0xC0,0x42,0x8B,0xE6,0xEB,0x8D,0x80,
155 0x35,0x7F,0x09,0x45,0x30,0xE5,0xB2,0x92,0x81,0x3F,0x08,0xCD,
156 0x36,0x5E,0x19,0x83,0x62,0xCC,0xAE,0x9B,0x81,0x66,0x24,0xEE,
157 0x16,0x6F,0xA9,0x9E,0xF4,0x82,0x1B,0xDD,0x46,0xC7,0x33,0x5D,
158 0xF4,0xCA,0xE6,0x8F,0xFC,0xD4,0xD8,0x58,0x94,0x24,0x5D,0xFF,
159 0x0A,0xE8,0xEF,0x3D,0xCE,0xBB,0x50,0x94,0xE0,0x5F,0xE8,0x41,
160 0xC3,0x35,0x30,0x37,0xD5,0xCB,0x8F,0x3D,0x95,0x15,0x1A,0x77,
161 0x42,0xB2,0x06,0x86,0xF6,0x09,0x66,0x0E,0x9A,0x25,0x94,0x3E,
162 0xD2,0x04,0x25,0x25,0x1D,0x23,0xEB,0xDC,0x4D,0x0C,0x83,0x28,
163 0x2E,0x15,0x81,0x2D,0xC1,0xAF,0x8D,0x36,0x64,0xE3,0x9A,0x83,
164 0x78,0xC2,0x8D,0xC0,0x9D,0xD9,0x3A,0x1C,0xC5,0x2B,0x50,0x68,
165 0x07,0xA9,0x4B,0x8C,0x07,0x57,0xD6,0x15,0x03,0x4E,0x9E,0x01,
166 0xF2,0x6F,0x35,0xAC,0x26,0x9C,0x92,0x68,0x61,0x13,0xFB,0x01,
167 0xBA,0x22,0x36,0x01,0x55,0xB6,0x62,0xD9,0xB2,0x98,0xCE,0x5D,
168 0x4B,0xA5,0x41,0xD6,0xE5,0x70,0x78,0x12,0x1F,0x64,0xB6,0x6F,
169 0xB0,0x91,0x51,0x91,0x92,0xC0,0x94,0x3A,0xD1,0x28,0x4D,0x30,
170 0x84,0x3E,0xE4,0xE4,0x7F,0x47,0x89,0xB1,0xB6,0x8C,0x8E,0x0E,
171 0x26,0xDB,0xCD,0x17,0x07,0x2A,0x21,0x7A,0xCC,0x68,0xE8,0x57,
172 0x94,0x9E,0x59,0x61,0xEC,0x20,0x34,0x26,0x0D,0x66,0x44,0xEB,
173 0x6F,0x02,0x58,0xE2,0xED,0xF6,0xF3,0x1B,0xBF,0x9E,0x45,0x52,
176 static unsigned char dh2048_g[]={
180 BIGNUM *p = NULL, *g = NULL;
183 p = BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL);
184 g = BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL);
188 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
192 if (!DH_set0_pqg(dh, p, NULL, g))
207 struct daemon_remote*
208 daemon_remote_create(struct config_file* cfg)
212 struct daemon_remote* rc = (struct daemon_remote*)calloc(1,
215 log_err("out of memory in daemon_remote_create");
220 if(!cfg->remote_control_enable) {
224 rc->ctx = SSL_CTX_new(SSLv23_server_method());
226 log_crypto_err("could not SSL_CTX_new");
230 /* no SSLv2, SSLv3 because has defects */
231 if((SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)
233 log_crypto_err("could not set SSL_OP_NO_SSLv2");
234 daemon_remote_delete(rc);
237 if((SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)
239 log_crypto_err("could not set SSL_OP_NO_SSLv3");
240 daemon_remote_delete(rc);
244 if (cfg->remote_control_use_cert == 0) {
245 /* No certificates are requested */
246 if(!SSL_CTX_set_cipher_list(rc->ctx, "aNULL")) {
247 log_crypto_err("Failed to set aNULL cipher list");
248 daemon_remote_delete(rc);
252 /* Since we have no certificates and hence no source of
253 * DH params, let's generate and set them
255 if(!SSL_CTX_set_tmp_dh(rc->ctx,get_dh2048())) {
256 log_crypto_err("Wanted to set DH param, but failed");
257 daemon_remote_delete(rc);
263 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
264 s_key = fname_after_chroot(cfg->server_key_file, cfg, 1);
265 if(!s_cert || !s_key) {
266 log_err("out of memory in remote control fname");
269 verbose(VERB_ALGO, "setup SSL certificates");
270 if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) {
271 log_err("Error for server-cert-file: %s", s_cert);
272 log_crypto_err("Error in SSL_CTX use_certificate_chain_file");
275 if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) {
276 log_err("Error for server-key-file: %s", s_key);
277 log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
280 if(!SSL_CTX_check_private_key(rc->ctx)) {
281 log_err("Error for server-key-file: %s", s_key);
282 log_crypto_err("Error in SSL_CTX check_private_key");
285 #if HAVE_DECL_SSL_CTX_SET_ECDH_AUTO
286 if(!SSL_CTX_set_ecdh_auto(rc->ctx,1)) {
287 log_crypto_err("Error in SSL_CTX_ecdh_auto, not enabling ECDHE");
289 #elif defined(USE_ECDSA)
291 EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
293 log_crypto_err("could not find p256, not enabling ECDHE");
295 if (1 != SSL_CTX_set_tmp_ecdh (rc->ctx, ecdh)) {
296 log_crypto_err("Error in SSL_CTX_set_tmp_ecdh, not enabling ECDHE");
302 if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) {
303 log_crypto_err("Error setting up SSL_CTX verify locations");
307 daemon_remote_delete(rc);
310 SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert));
311 SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL);
318 void daemon_remote_clear(struct daemon_remote* rc)
320 struct rc_state* p, *np;
322 /* but do not close the ports */
323 listen_list_delete(rc->accept_list);
324 rc->accept_list = NULL;
325 /* do close these sockets */
331 comm_point_delete(p->c);
335 rc->busy_list = NULL;
340 void daemon_remote_delete(struct daemon_remote* rc)
343 daemon_remote_clear(rc);
345 SSL_CTX_free(rc->ctx);
351 * Add and open a new control port
354 * @param list: list head
355 * @param noproto_is_err: if lack of protocol support is an error.
356 * @param cfg: config with username for chown of unix-sockets.
357 * @return false on failure.
360 add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err,
361 struct config_file* cfg)
363 struct addrinfo hints;
364 struct addrinfo* res;
365 struct listen_port* n;
369 snprintf(port, sizeof(port), "%d", nr);
370 port[sizeof(port)-1]=0;
371 memset(&hints, 0, sizeof(hints));
374 /* This looks like a local socket */
375 fd = create_local_accept_sock(ip, &noproto);
377 * Change socket ownership and permissions so users other
378 * than root can access it provided they are in the same
379 * group as the user we run as.
383 if (cfg->username && cfg->username[0] &&
384 cfg_uid != (uid_t)-1) {
385 if(chown(ip, cfg_uid, cfg_gid) == -1)
386 log_err("cannot chown %u.%u %s: %s",
387 (unsigned)cfg_uid, (unsigned)cfg_gid,
388 ip, strerror(errno));
390 chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
396 hints.ai_socktype = SOCK_STREAM;
397 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
398 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
400 if(!noproto_is_err && r == EAI_NONAME) {
401 /* tried to lookup the address as name */
402 return 1; /* return success, but do nothing */
404 #endif /* USE_WINSOCK */
405 log_err("control interface %s:%s getaddrinfo: %s %s",
406 ip?ip:"default", port, gai_strerror(r),
408 r==EAI_SYSTEM?(char*)strerror(errno):""
417 fd = create_tcp_accept_sock(res, 1, &noproto, 0,
418 cfg->ip_transparent, 0, cfg->ip_freebind);
422 if(fd == -1 && noproto) {
424 return 1; /* return success, but do nothing */
425 log_err("cannot open control interface %s %d : "
426 "protocol not supported", ip, nr);
430 log_err("cannot open control interface %s %d", ip, nr);
435 n = (struct listen_port*)calloc(1, sizeof(*n));
442 log_err("out of memory");
451 struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
453 struct listen_port* l = NULL;
454 log_assert(cfg->remote_control_enable && cfg->control_port);
455 if(cfg->control_ifs) {
456 struct config_strlist* p;
457 for(p = cfg->control_ifs; p; p = p->next) {
458 if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) {
459 listening_ports_free(l);
466 !add_open("::1", cfg->control_port, &l, 0, cfg)) {
467 listening_ports_free(l);
471 !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) {
472 listening_ports_free(l);
479 /** open accept commpoint */
481 accept_open(struct daemon_remote* rc, int fd)
483 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
485 log_err("out of memory");
488 n->next = rc->accept_list;
491 n->com = comm_point_create_raw(rc->worker->base, fd, 0,
492 &remote_accept_callback, rc);
495 /* keep this port open, its fd is kept in the rc portlist */
496 n->com->do_not_close = 1;
500 int daemon_remote_open_accept(struct daemon_remote* rc,
501 struct listen_port* ports, struct worker* worker)
503 struct listen_port* p;
505 for(p = ports; p; p = p->next) {
506 if(!accept_open(rc, p->fd)) {
507 log_err("could not create accept comm point");
514 void daemon_remote_stop_accept(struct daemon_remote* rc)
516 struct listen_list* p;
517 for(p=rc->accept_list; p; p=p->next) {
518 comm_point_stop_listening(p->com);
522 void daemon_remote_start_accept(struct daemon_remote* rc)
524 struct listen_list* p;
525 for(p=rc->accept_list; p; p=p->next) {
526 comm_point_start_listening(p->com, -1, -1);
530 int remote_accept_callback(struct comm_point* c, void* arg, int err,
531 struct comm_reply* ATTR_UNUSED(rep))
533 struct daemon_remote* rc = (struct daemon_remote*)arg;
534 struct sockaddr_storage addr;
538 if(err != NETEVENT_NOERROR) {
539 log_err("error %d on remote_accept_callback", err);
542 /* perform the accept */
543 newfd = comm_point_perform_accept(c, &addr, &addrlen);
546 /* create new commpoint unless we are servicing already */
547 if(rc->active >= rc->max_active) {
548 log_warn("drop incoming remote control: too many connections");
558 /* setup commpoint to service the remote control command */
559 n = (struct rc_state*)calloc(1, sizeof(*n));
561 log_err("out of memory");
564 /* start in reading state */
565 n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
566 &remote_control_callback, n);
568 log_err("out of memory");
572 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
573 n->c->do_not_close = 0;
574 comm_point_stop_listening(n->c);
575 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
576 memcpy(&n->c->repinfo.addr, &addr, addrlen);
577 n->c->repinfo.addrlen = addrlen;
578 n->shake_state = rc_hs_read;
579 n->ssl = SSL_new(rc->ctx);
581 log_crypto_err("could not SSL_new");
582 comm_point_delete(n->c);
586 SSL_set_accept_state(n->ssl);
587 (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY);
588 if(!SSL_set_fd(n->ssl, newfd)) {
589 log_crypto_err("could not SSL_set_fd");
591 comm_point_delete(n->c);
597 n->next = rc->busy_list;
601 /* perform the first nonblocking read already, for windows,
602 * so it can return wouldblock. could be faster too. */
603 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
607 /** delete from list */
609 state_list_remove_elem(struct rc_state** list, struct comm_point* c)
612 if( (*list)->c == c) {
613 *list = (*list)->next;
616 list = &(*list)->next;
620 /** decrease active count and remove commpoint from busy list */
622 clean_point(struct daemon_remote* rc, struct rc_state* s)
624 state_list_remove_elem(&rc->busy_list, s->c);
627 SSL_shutdown(s->ssl);
630 comm_point_delete(s->c);
635 ssl_print_text(SSL* ssl, const char* text)
641 if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) {
642 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
643 verbose(VERB_QUERY, "warning, in SSL_write, peer "
644 "closed connection");
647 log_crypto_err("could not SSL_write");
653 /** print text over the ssl connection */
655 ssl_print_vmsg(SSL* ssl, const char* format, va_list args)
658 vsnprintf(msg, sizeof(msg), format, args);
659 return ssl_print_text(ssl, msg);
662 /** printf style printing to the ssl connection */
663 int ssl_printf(SSL* ssl, const char* format, ...)
667 va_start(args, format);
668 ret = ssl_print_vmsg(ssl, format, args);
674 ssl_read_line(SSL* ssl, char* buf, size_t max)
682 if((r=SSL_read(ssl, buf+len, 1)) <= 0) {
683 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
687 log_crypto_err("could not SSL_read");
690 if(buf[len] == '\n') {
691 /* return string without \n */
698 log_err("control line too long (%d): %s", (int)max, buf);
702 /** skip whitespace, return new pointer into string */
706 /* EOS \0 is not a space */
707 while( isspace((unsigned char)*str) )
712 /** send the OK to the control client */
713 static void send_ok(SSL* ssl)
715 (void)ssl_printf(ssl, "ok\n");
718 /** do the stop command */
720 do_stop(SSL* ssl, struct daemon_remote* rc)
722 rc->worker->need_to_exit = 1;
723 comm_base_exit(rc->worker->base);
727 /** do the reload command */
729 do_reload(SSL* ssl, struct daemon_remote* rc)
731 rc->worker->need_to_exit = 0;
732 comm_base_exit(rc->worker->base);
736 /** do the verbosity command */
738 do_verbosity(SSL* ssl, char* str)
741 if(val == 0 && strcmp(str, "0") != 0) {
742 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
749 /** print stats from statinfo */
751 print_stats(SSL* ssl, const char* nm, struct stats_info* s)
754 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm,
755 (unsigned long)s->svr.num_queries)) return 0;
756 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm,
757 (unsigned long)(s->svr.num_queries
758 - s->svr.num_queries_missed_cache))) return 0;
759 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm,
760 (unsigned long)s->svr.num_queries_missed_cache)) return 0;
761 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm,
762 (unsigned long)s->svr.num_queries_prefetch)) return 0;
763 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm,
764 (unsigned long)s->mesh_replies_sent)) return 0;
765 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
766 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
767 (double)s->svr.sum_query_list_size/
768 (s->svr.num_queries_missed_cache+
769 s->svr.num_queries_prefetch) : 0.0)) return 0;
770 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm,
771 (unsigned long)s->svr.max_query_list_size)) return 0;
772 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm,
773 (unsigned long)s->mesh_jostled)) return 0;
774 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm,
775 (unsigned long)s->mesh_dropped)) return 0;
776 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm,
777 (unsigned long)s->mesh_num_states)) return 0;
778 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm,
779 (unsigned long)s->mesh_num_reply_states)) return 0;
780 timeval_divide(&avg, &s->mesh_replies_sum_wait, s->mesh_replies_sent);
781 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
782 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
783 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
784 s->mesh_time_median)) return 0;
785 if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm,
786 (unsigned long)s->svr.tcp_accept_usage)) return 0;
790 /** print stats for one thread */
792 print_thread_stats(SSL* ssl, int i, struct stats_info* s)
795 snprintf(nm, sizeof(nm), "thread%d", i);
797 return print_stats(ssl, nm, s);
800 /** print long number */
802 print_longnum(SSL* ssl, const char* desc, size_t x)
804 if(x > 1024*1024*1024) {
806 size_t front = x / (size_t)1000000;
807 size_t back = x % (size_t)1000000;
808 return ssl_printf(ssl, "%s%u%6.6u\n", desc,
809 (unsigned)front, (unsigned)back);
811 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x);
815 /** print mem stats */
817 print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon)
820 size_t msg, rrset, val, iter;
822 extern void* unbound_start_brk;
824 if(!print_longnum(ssl, "mem.total.sbrk"SQ,
825 (size_t)((char*)cur - (char*)unbound_start_brk))) return 0;
826 #endif /* HAVE_SBRK */
827 msg = slabhash_get_mem(daemon->env->msg_cache);
828 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
831 m = modstack_find(&worker->env.mesh->mods, "validator");
833 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
834 mods.mod[m]->get_mem));
835 val = (*worker->env.mesh->mods.mod[m]->get_mem)
838 m = modstack_find(&worker->env.mesh->mods, "iterator");
840 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
841 mods.mod[m]->get_mem));
842 iter = (*worker->env.mesh->mods.mod[m]->get_mem)
846 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
848 if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
850 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
852 if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
857 /** print uptime stats */
859 print_uptime(SSL* ssl, struct worker* worker, int reset)
861 struct timeval now = *worker->env.now_tv;
862 struct timeval up, dt;
863 timeval_subtract(&up, &now, &worker->daemon->time_boot);
864 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
866 worker->daemon->time_last_stat = now;
867 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
868 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
869 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
870 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
871 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
872 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
876 /** print extended histogram */
878 print_hist(SSL* ssl, struct stats_info* s)
880 struct timehist* hist;
882 hist = timehist_setup();
884 log_err("out of memory");
887 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
888 for(i=0; i<hist->num; i++) {
890 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
891 (int)hist->buckets[i].lower.tv_sec,
892 (int)hist->buckets[i].lower.tv_usec,
893 (int)hist->buckets[i].upper.tv_sec,
894 (int)hist->buckets[i].upper.tv_usec,
895 (unsigned long)hist->buckets[i].count)) {
896 timehist_delete(hist);
900 timehist_delete(hist);
904 /** print extended stats */
906 print_ext(SSL* ssl, struct stats_info* s)
910 const sldns_rr_descriptor* desc;
911 const sldns_lookup_table* lt;
913 for(i=0; i<STATS_QTYPE_NUM; i++) {
914 if(inhibit_zero && s->svr.qtype[i] == 0)
916 desc = sldns_rr_descript((uint16_t)i);
917 if(desc && desc->_name) {
918 snprintf(nm, sizeof(nm), "%s", desc->_name);
919 } else if (i == LDNS_RR_TYPE_IXFR) {
920 snprintf(nm, sizeof(nm), "IXFR");
921 } else if (i == LDNS_RR_TYPE_AXFR) {
922 snprintf(nm, sizeof(nm), "AXFR");
923 } else if (i == LDNS_RR_TYPE_MAILA) {
924 snprintf(nm, sizeof(nm), "MAILA");
925 } else if (i == LDNS_RR_TYPE_MAILB) {
926 snprintf(nm, sizeof(nm), "MAILB");
927 } else if (i == LDNS_RR_TYPE_ANY) {
928 snprintf(nm, sizeof(nm), "ANY");
930 snprintf(nm, sizeof(nm), "TYPE%d", i);
932 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n",
933 nm, (unsigned long)s->svr.qtype[i])) return 0;
935 if(!inhibit_zero || s->svr.qtype_big) {
936 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n",
937 (unsigned long)s->svr.qtype_big)) return 0;
940 for(i=0; i<STATS_QCLASS_NUM; i++) {
941 if(inhibit_zero && s->svr.qclass[i] == 0)
943 lt = sldns_lookup_by_id(sldns_rr_classes, i);
945 snprintf(nm, sizeof(nm), "%s", lt->name);
947 snprintf(nm, sizeof(nm), "CLASS%d", i);
949 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n",
950 nm, (unsigned long)s->svr.qclass[i])) return 0;
952 if(!inhibit_zero || s->svr.qclass_big) {
953 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n",
954 (unsigned long)s->svr.qclass_big)) return 0;
957 for(i=0; i<STATS_OPCODE_NUM; i++) {
958 if(inhibit_zero && s->svr.qopcode[i] == 0)
960 lt = sldns_lookup_by_id(sldns_opcodes, i);
962 snprintf(nm, sizeof(nm), "%s", lt->name);
964 snprintf(nm, sizeof(nm), "OPCODE%d", i);
966 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n",
967 nm, (unsigned long)s->svr.qopcode[i])) return 0;
970 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n",
971 (unsigned long)s->svr.qtcp)) return 0;
972 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n",
973 (unsigned long)s->svr.qtcp_outgoing)) return 0;
974 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n",
975 (unsigned long)s->svr.qipv6)) return 0;
977 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n",
978 (unsigned long)s->svr.qbit_QR)) return 0;
979 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n",
980 (unsigned long)s->svr.qbit_AA)) return 0;
981 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n",
982 (unsigned long)s->svr.qbit_TC)) return 0;
983 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n",
984 (unsigned long)s->svr.qbit_RD)) return 0;
985 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n",
986 (unsigned long)s->svr.qbit_RA)) return 0;
987 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n",
988 (unsigned long)s->svr.qbit_Z)) return 0;
989 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n",
990 (unsigned long)s->svr.qbit_AD)) return 0;
991 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n",
992 (unsigned long)s->svr.qbit_CD)) return 0;
993 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n",
994 (unsigned long)s->svr.qEDNS)) return 0;
995 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n",
996 (unsigned long)s->svr.qEDNS_DO)) return 0;
999 for(i=0; i<STATS_RCODE_NUM; i++) {
1000 /* Always include RCODEs 0-5 */
1001 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
1003 lt = sldns_lookup_by_id(sldns_rcodes, i);
1004 if(lt && lt->name) {
1005 snprintf(nm, sizeof(nm), "%s", lt->name);
1007 snprintf(nm, sizeof(nm), "RCODE%d", i);
1009 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n",
1010 nm, (unsigned long)s->svr.ans_rcode[i])) return 0;
1012 if(!inhibit_zero || s->svr.ans_rcode_nodata) {
1013 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n",
1014 (unsigned long)s->svr.ans_rcode_nodata)) return 0;
1017 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n",
1018 (unsigned long)s->svr.ans_secure)) return 0;
1019 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n",
1020 (unsigned long)s->svr.ans_bogus)) return 0;
1021 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n",
1022 (unsigned long)s->svr.rrset_bogus)) return 0;
1023 /* threat detection */
1024 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n",
1025 (unsigned long)s->svr.unwanted_queries)) return 0;
1026 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n",
1027 (unsigned long)s->svr.unwanted_replies)) return 0;
1029 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
1030 (unsigned)s->svr.msg_cache_count)) return 0;
1031 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
1032 (unsigned)s->svr.rrset_cache_count)) return 0;
1033 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
1034 (unsigned)s->svr.infra_cache_count)) return 0;
1035 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
1036 (unsigned)s->svr.key_cache_count)) return 0;
1040 /** do the stats command */
1042 do_stats(SSL* ssl, struct daemon_remote* rc, int reset)
1044 struct daemon* daemon = rc->worker->daemon;
1045 struct stats_info total;
1046 struct stats_info s;
1048 log_assert(daemon->num > 0);
1049 /* gather all thread statistics in one place */
1050 for(i=0; i<daemon->num; i++) {
1051 server_stats_obtain(rc->worker, daemon->workers[i], &s, reset);
1052 if(!print_thread_stats(ssl, i, &s))
1056 else server_stats_add(&total, &s);
1058 /* print the thread statistics */
1059 total.mesh_time_median /= (double)daemon->num;
1060 if(!print_stats(ssl, "total", &total))
1062 if(!print_uptime(ssl, rc->worker, reset))
1064 if(daemon->cfg->stat_extended) {
1065 if(!print_mem(ssl, rc->worker, daemon))
1067 if(!print_hist(ssl, &total))
1069 if(!print_ext(ssl, &total))
1074 /** parse commandline argument domain name */
1076 parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs)
1078 uint8_t nm[LDNS_MAX_DOMAINLEN+1];
1079 size_t nmlen = sizeof(nm);
1084 status = sldns_str2wire_dname_buf(str, nm, &nmlen);
1086 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
1087 LDNS_WIREPARSE_OFFSET(status),
1088 sldns_get_errorstr_parse(status));
1091 *res = memdup(nm, nmlen);
1093 ssl_printf(ssl, "error out of memory\n");
1096 *labs = dname_count_size_labels(*res, len);
1100 /** find second argument, modifies string */
1102 find_arg2(SSL* ssl, char* arg, char** arg2)
1104 char* as = strchr(arg, ' ');
1105 char* at = strchr(arg, '\t');
1110 *arg2 = skipwhite(as+1);
1113 *arg2 = skipwhite(as+1);
1116 *arg2 = skipwhite(at+1);
1118 ssl_printf(ssl, "error could not find next argument "
1125 /** Add a new zone */
1127 do_zone_add(SSL* ssl, struct worker* worker, char* arg)
1133 enum localzone_type t;
1134 struct local_zone* z;
1135 if(!find_arg2(ssl, arg, &arg2))
1137 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1139 if(!local_zone_str2type(arg2, &t)) {
1140 ssl_printf(ssl, "error not a zone type. %s\n", arg2);
1144 lock_rw_wrlock(&worker->daemon->local_zones->lock);
1145 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1146 nmlabs, LDNS_RR_CLASS_IN))) {
1147 /* already present in tree */
1148 lock_rw_wrlock(&z->lock);
1149 z->type = t; /* update type anyway */
1150 lock_rw_unlock(&z->lock);
1152 lock_rw_unlock(&worker->daemon->local_zones->lock);
1156 if(!local_zones_add_zone(worker->daemon->local_zones, nm, nmlen,
1157 nmlabs, LDNS_RR_CLASS_IN, t)) {
1158 lock_rw_unlock(&worker->daemon->local_zones->lock);
1159 ssl_printf(ssl, "error out of memory\n");
1162 lock_rw_unlock(&worker->daemon->local_zones->lock);
1166 /** Remove a zone */
1168 do_zone_remove(SSL* ssl, struct worker* worker, char* arg)
1173 struct local_zone* z;
1174 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1176 lock_rw_wrlock(&worker->daemon->local_zones->lock);
1177 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1178 nmlabs, LDNS_RR_CLASS_IN))) {
1179 /* present in tree */
1180 local_zones_del_zone(worker->daemon->local_zones, z);
1182 lock_rw_unlock(&worker->daemon->local_zones->lock);
1187 /** Add new RR data */
1189 do_data_add(SSL* ssl, struct worker* worker, char* arg)
1191 if(!local_zones_add_RR(worker->daemon->local_zones, arg)) {
1192 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1198 /** Remove RR data */
1200 do_data_remove(SSL* ssl, struct worker* worker, char* arg)
1205 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1207 local_zones_del_data(worker->daemon->local_zones, nm,
1208 nmlen, nmlabs, LDNS_RR_CLASS_IN);
1213 /** cache lookup of nameservers */
1215 do_lookup(SSL* ssl, struct worker* worker, char* arg)
1220 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1222 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1226 /** flush something from rrset and msg caches */
1228 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1229 uint16_t t, uint16_t c)
1232 struct query_info k;
1233 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1234 if(t == LDNS_RR_TYPE_SOA)
1235 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1236 PACKED_RRSET_SOA_NEG);
1238 k.qname_len = nmlen;
1241 h = query_info_hash(&k, 0);
1242 slabhash_remove(worker->env.msg_cache, h, &k);
1243 if(t == LDNS_RR_TYPE_AAAA) {
1244 /* for AAAA also flush dns64 bit_cd packet */
1245 h = query_info_hash(&k, BIT_CD);
1246 slabhash_remove(worker->env.msg_cache, h, &k);
1252 do_flush_type(SSL* ssl, struct worker* worker, char* arg)
1259 if(!find_arg2(ssl, arg, &arg2))
1261 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1263 t = sldns_get_rr_type_by_name(arg2);
1264 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1270 /** flush statistics */
1272 do_flush_stats(SSL* ssl, struct worker* worker)
1274 worker_stats_clear(worker);
1279 * Local info for deletion functions
1283 struct worker* worker;
1284 /** name to delete */
1290 /** time to invalidate to */
1292 /** number of rrsets removed */
1294 /** number of msgs removed */
1296 /** number of key entries removed */
1298 /** length of addr */
1300 /** socket address for host deletion */
1301 struct sockaddr_storage addr;
1304 /** callback to delete hosts in infra cache */
1306 infra_del_host(struct lruhash_entry* e, void* arg)
1308 /* entry is locked */
1309 struct del_info* inf = (struct del_info*)arg;
1310 struct infra_key* k = (struct infra_key*)e->key;
1311 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1312 struct infra_data* d = (struct infra_data*)e->data;
1315 d->timeout_AAAA = 0;
1316 d->timeout_other = 0;
1318 if(d->ttl > inf->expired) {
1319 d->ttl = inf->expired;
1325 /** flush infra cache */
1327 do_flush_infra(SSL* ssl, struct worker* worker, char* arg)
1329 struct sockaddr_storage addr;
1331 struct del_info inf;
1332 if(strcmp(arg, "all") == 0) {
1333 slabhash_clear(worker->env.infra_cache->hosts);
1337 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1338 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1341 /* delete all entries from cache */
1342 /* what we do is to set them all expired */
1343 inf.worker = worker;
1347 inf.expired = *worker->env.now;
1348 inf.expired -= 3; /* handle 3 seconds skew between threads */
1353 memmove(&inf.addr, &addr, len);
1354 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1359 /** flush requestlist */
1361 do_flush_requestlist(SSL* ssl, struct worker* worker)
1363 mesh_delete_all(worker->env.mesh);
1367 /** callback to delete rrsets in a zone */
1369 zone_del_rrset(struct lruhash_entry* e, void* arg)
1371 /* entry is locked */
1372 struct del_info* inf = (struct del_info*)arg;
1373 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1374 if(dname_subdomain_c(k->rk.dname, inf->name)) {
1375 struct packed_rrset_data* d =
1376 (struct packed_rrset_data*)e->data;
1377 if(d->ttl > inf->expired) {
1378 d->ttl = inf->expired;
1384 /** callback to delete messages in a zone */
1386 zone_del_msg(struct lruhash_entry* e, void* arg)
1388 /* entry is locked */
1389 struct del_info* inf = (struct del_info*)arg;
1390 struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1391 if(dname_subdomain_c(k->key.qname, inf->name)) {
1392 struct reply_info* d = (struct reply_info*)e->data;
1393 if(d->ttl > inf->expired) {
1394 d->ttl = inf->expired;
1400 /** callback to delete keys in zone */
1402 zone_del_kcache(struct lruhash_entry* e, void* arg)
1404 /* entry is locked */
1405 struct del_info* inf = (struct del_info*)arg;
1406 struct key_entry_key* k = (struct key_entry_key*)e->key;
1407 if(dname_subdomain_c(k->name, inf->name)) {
1408 struct key_entry_data* d = (struct key_entry_data*)e->data;
1409 if(d->ttl > inf->expired) {
1410 d->ttl = inf->expired;
1416 /** remove all rrsets and keys from zone from cache */
1418 do_flush_zone(SSL* ssl, struct worker* worker, char* arg)
1423 struct del_info inf;
1424 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1426 /* delete all RRs and key entries from zone */
1427 /* what we do is to set them all expired */
1428 inf.worker = worker;
1432 inf.expired = *worker->env.now;
1433 inf.expired -= 3; /* handle 3 seconds skew between threads */
1437 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1438 &zone_del_rrset, &inf);
1440 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1442 /* and validator cache */
1443 if(worker->env.key_cache) {
1444 slabhash_traverse(worker->env.key_cache->slab, 1,
1445 &zone_del_kcache, &inf);
1450 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1451 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1452 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1455 /** callback to delete bogus rrsets */
1457 bogus_del_rrset(struct lruhash_entry* e, void* arg)
1459 /* entry is locked */
1460 struct del_info* inf = (struct del_info*)arg;
1461 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1462 if(d->security == sec_status_bogus) {
1463 d->ttl = inf->expired;
1468 /** callback to delete bogus messages */
1470 bogus_del_msg(struct lruhash_entry* e, void* arg)
1472 /* entry is locked */
1473 struct del_info* inf = (struct del_info*)arg;
1474 struct reply_info* d = (struct reply_info*)e->data;
1475 if(d->security == sec_status_bogus) {
1476 d->ttl = inf->expired;
1481 /** callback to delete bogus keys */
1483 bogus_del_kcache(struct lruhash_entry* e, void* arg)
1485 /* entry is locked */
1486 struct del_info* inf = (struct del_info*)arg;
1487 struct key_entry_data* d = (struct key_entry_data*)e->data;
1489 d->ttl = inf->expired;
1494 /** remove all bogus rrsets, msgs and keys from cache */
1496 do_flush_bogus(SSL* ssl, struct worker* worker)
1498 struct del_info inf;
1499 /* what we do is to set them all expired */
1500 inf.worker = worker;
1501 inf.expired = *worker->env.now;
1502 inf.expired -= 3; /* handle 3 seconds skew between threads */
1506 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1507 &bogus_del_rrset, &inf);
1509 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1511 /* and validator cache */
1512 if(worker->env.key_cache) {
1513 slabhash_traverse(worker->env.key_cache->slab, 1,
1514 &bogus_del_kcache, &inf);
1517 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1518 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1519 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1522 /** callback to delete negative and servfail rrsets */
1524 negative_del_rrset(struct lruhash_entry* e, void* arg)
1526 /* entry is locked */
1527 struct del_info* inf = (struct del_info*)arg;
1528 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1529 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1530 /* delete the parentside negative cache rrsets,
1531 * these are namerserver rrsets that failed lookup, rdata empty */
1532 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 &&
1533 d->rrsig_count == 0 && d->rr_len[0] == 0) {
1534 d->ttl = inf->expired;
1539 /** callback to delete negative and servfail messages */
1541 negative_del_msg(struct lruhash_entry* e, void* arg)
1543 /* entry is locked */
1544 struct del_info* inf = (struct del_info*)arg;
1545 struct reply_info* d = (struct reply_info*)e->data;
1546 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1547 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1548 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) {
1549 d->ttl = inf->expired;
1554 /** callback to delete negative key entries */
1556 negative_del_kcache(struct lruhash_entry* e, void* arg)
1558 /* entry is locked */
1559 struct del_info* inf = (struct del_info*)arg;
1560 struct key_entry_data* d = (struct key_entry_data*)e->data;
1561 /* could be bad because of lookup failure on the DS, DNSKEY, which
1562 * was nxdomain or servfail, and thus a result of negative lookups */
1564 d->ttl = inf->expired;
1569 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1571 do_flush_negative(SSL* ssl, struct worker* worker)
1573 struct del_info inf;
1574 /* what we do is to set them all expired */
1575 inf.worker = worker;
1576 inf.expired = *worker->env.now;
1577 inf.expired -= 3; /* handle 3 seconds skew between threads */
1581 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1582 &negative_del_rrset, &inf);
1584 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf);
1586 /* and validator cache */
1587 if(worker->env.key_cache) {
1588 slabhash_traverse(worker->env.key_cache->slab, 1,
1589 &negative_del_kcache, &inf);
1592 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1593 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1594 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1597 /** remove name rrset from cache */
1599 do_flush_name(SSL* ssl, struct worker* w, char* arg)
1604 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1606 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1607 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1608 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1609 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1610 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1611 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1612 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1613 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1614 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1615 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1621 /** printout a delegation point info */
1623 ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1627 struct delegpt_ns* ns;
1628 struct delegpt_addr* a;
1630 if(str) { /* print header for forward, stub */
1631 char* c = sldns_wire2str_class(dclass);
1633 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) {
1639 for(ns = dp->nslist; ns; ns = ns->next) {
1640 dname_str(ns->name, buf);
1641 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1645 for(a = dp->target_list; a; a = a->next_target) {
1646 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1647 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1651 return ssl_printf(ssl, "\n");
1655 /** print root forwards */
1657 print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root)
1660 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1662 return ssl_printf(ssl, "off (using root hints)\n");
1663 /* if dp is returned it must be the root */
1664 log_assert(query_dname_compare(dp->name, root)==0);
1665 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1668 /** parse args into delegpt */
1669 static struct delegpt*
1670 parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names)
1672 /* parse args and add in */
1675 struct delegpt* dp = delegpt_create_mlc(nm);
1676 struct sockaddr_storage addr;
1679 (void)ssl_printf(ssl, "error out of memory\n");
1684 p = strchr(p, ' '); /* find next spot, if any */
1686 *p++ = 0; /* end this spot */
1687 p = skipwhite(p); /* position at next spot */
1690 if(!extstrtoaddr(todo, &addr, &addrlen)) {
1695 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
1696 (void)ssl_printf(ssl, "error cannot "
1697 "parse IP address or name "
1699 delegpt_free_mlc(dp);
1702 if(!delegpt_add_ns_mlc(dp, n, 0)) {
1703 (void)ssl_printf(ssl, "error out of memory\n");
1705 delegpt_free_mlc(dp);
1711 (void)ssl_printf(ssl, "error cannot parse"
1712 " IP address '%s'\n", todo);
1713 delegpt_free_mlc(dp);
1718 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) {
1719 (void)ssl_printf(ssl, "error out of memory\n");
1720 delegpt_free_mlc(dp);
1725 dp->has_parent_side_NS = 1;
1729 /** do the status command */
1731 do_forward(SSL* ssl, struct worker* worker, char* args)
1733 struct iter_forwards* fwd = worker->env.fwds;
1734 uint8_t* root = (uint8_t*)"\000";
1736 (void)ssl_printf(ssl, "error: structure not allocated\n");
1739 if(args == NULL || args[0] == 0) {
1740 (void)print_root_fwds(ssl, fwd, root);
1743 /* set root forwards for this thread. since we are in remote control
1744 * the actual mesh is not running, so we can freely edit it. */
1745 /* delete all the existing queries first */
1746 mesh_delete_all(worker->env.mesh);
1747 if(strcmp(args, "off") == 0) {
1748 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
1751 if(!(dp = parse_delegpt(ssl, args, root, 0)))
1753 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1754 (void)ssl_printf(ssl, "error out of memory\n");
1762 parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp,
1763 int* insecure, int* prime)
1769 /* parse all -x args */
1770 while(args[0] == '+') {
1771 if(!find_arg2(ssl, args, &rest))
1773 while(*(++args) != 0) {
1774 if(*args == 'i' && insecure)
1776 else if(*args == 'p' && prime)
1779 (void)ssl_printf(ssl, "error: unknown option %s\n", args);
1787 if(!find_arg2(ssl, args, &rest))
1791 } else zonename = args;
1792 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
1797 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
1805 /** do the forward_add command */
1807 do_forward_add(SSL* ssl, struct worker* worker, char* args)
1809 struct iter_forwards* fwd = worker->env.fwds;
1812 struct delegpt* dp = NULL;
1813 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
1815 if(insecure && worker->env.anchors) {
1816 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1818 (void)ssl_printf(ssl, "error out of memory\n");
1819 delegpt_free_mlc(dp);
1824 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1825 (void)ssl_printf(ssl, "error out of memory\n");
1833 /** do the forward_remove command */
1835 do_forward_remove(SSL* ssl, struct worker* worker, char* args)
1837 struct iter_forwards* fwd = worker->env.fwds;
1840 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1842 if(insecure && worker->env.anchors)
1843 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1845 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
1850 /** do the stub_add command */
1852 do_stub_add(SSL* ssl, struct worker* worker, char* args)
1854 struct iter_forwards* fwd = worker->env.fwds;
1855 int insecure = 0, prime = 0;
1857 struct delegpt* dp = NULL;
1858 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
1860 if(insecure && worker->env.anchors) {
1861 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1863 (void)ssl_printf(ssl, "error out of memory\n");
1864 delegpt_free_mlc(dp);
1869 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
1870 if(insecure && worker->env.anchors)
1871 anchors_delete_insecure(worker->env.anchors,
1872 LDNS_RR_CLASS_IN, nm);
1873 (void)ssl_printf(ssl, "error out of memory\n");
1874 delegpt_free_mlc(dp);
1878 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
1879 (void)ssl_printf(ssl, "error out of memory\n");
1880 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1881 if(insecure && worker->env.anchors)
1882 anchors_delete_insecure(worker->env.anchors,
1883 LDNS_RR_CLASS_IN, nm);
1891 /** do the stub_remove command */
1893 do_stub_remove(SSL* ssl, struct worker* worker, char* args)
1895 struct iter_forwards* fwd = worker->env.fwds;
1898 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1900 if(insecure && worker->env.anchors)
1901 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1903 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1904 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
1909 /** do the insecure_add command */
1911 do_insecure_add(SSL* ssl, struct worker* worker, char* arg)
1916 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1918 if(worker->env.anchors) {
1919 if(!anchors_add_insecure(worker->env.anchors,
1920 LDNS_RR_CLASS_IN, nm)) {
1921 (void)ssl_printf(ssl, "error out of memory\n");
1930 /** do the insecure_remove command */
1932 do_insecure_remove(SSL* ssl, struct worker* worker, char* arg)
1937 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1939 if(worker->env.anchors)
1940 anchors_delete_insecure(worker->env.anchors,
1941 LDNS_RR_CLASS_IN, nm);
1947 do_insecure_list(SSL* ssl, struct worker* worker)
1950 struct trust_anchor* a;
1951 if(worker->env.anchors) {
1952 RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) {
1953 if(a->numDS == 0 && a->numDNSKEY == 0) {
1954 dname_str(a->name, buf);
1955 ssl_printf(ssl, "%s\n", buf);
1961 /** do the status command */
1963 do_status(SSL* ssl, struct worker* worker)
1967 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
1969 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
1971 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
1973 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
1975 for(i=0; i<worker->daemon->mods.num; i++) {
1976 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
1979 if(!ssl_printf(ssl, " ]\n"))
1981 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
1982 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
1984 if(!ssl_printf(ssl, "options:%s%s\n" ,
1985 (worker->daemon->reuseport?" reuseport":""),
1986 (worker->daemon->rc->accept_list?" control(ssl)":"")))
1988 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
1993 /** get age for the mesh state */
1995 get_mesh_age(struct mesh_state* m, char* buf, size_t len,
1996 struct module_env* env)
2000 struct mesh_reply* r = m->reply_list;
2001 /* last reply is the oldest */
2004 timeval_subtract(&d, env->now_tv, &r->start_time);
2005 snprintf(buf, len, ARG_LL "d.%6.6d",
2006 (long long)d.tv_sec, (int)d.tv_usec);
2008 snprintf(buf, len, "-");
2012 /** get status of a mesh state */
2014 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
2015 char* buf, size_t len)
2017 enum module_ext_state s = m->s.ext_state[m->s.curmod];
2018 const char *modname = mesh->mods.mod[m->s.curmod]->name;
2020 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
2021 m->s.minfo[m->s.curmod]) {
2022 /* break into iterator to find out who its waiting for */
2023 struct iter_qstate* qstate = (struct iter_qstate*)
2024 m->s.minfo[m->s.curmod];
2025 struct outbound_list* ol = &qstate->outlist;
2026 struct outbound_entry* e;
2027 snprintf(buf, len, "%s wait for", modname);
2030 if(ol->first == NULL)
2031 snprintf(buf, len, " (empty_list)");
2032 for(e = ol->first; e; e = e->next) {
2033 snprintf(buf, len, " ");
2036 addr_to_str(&e->qsent->addr, e->qsent->addrlen,
2041 } else if(s == module_wait_subquery) {
2042 /* look in subs from mesh state to see what */
2044 struct mesh_state_ref* sub;
2045 snprintf(buf, len, "%s wants", modname);
2048 if(m->sub_set.count == 0)
2049 snprintf(buf, len, " (empty_list)");
2050 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
2051 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
2052 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
2053 dname_str(sub->s->s.qinfo.qname, nm);
2054 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
2055 (c?c:"CLASS??"), nm);
2062 snprintf(buf, len, "%s is %s", modname, strextstate(s));
2066 /** do the dump_requestlist command */
2068 do_dump_requestlist(SSL* ssl, struct worker* worker)
2070 struct mesh_area* mesh;
2071 struct mesh_state* m;
2075 char statbuf[10240];
2076 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
2078 if(!ssl_printf(ssl, "# type cl name seconds module status\n"))
2080 /* show worker mesh contents */
2081 mesh = worker->env.mesh;
2083 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
2084 char* t = sldns_wire2str_type(m->s.qinfo.qtype);
2085 char* c = sldns_wire2str_class(m->s.qinfo.qclass);
2086 dname_str(m->s.qinfo.qname, buf);
2087 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
2088 get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
2089 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
2090 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
2102 /** structure for argument data for dump infra host */
2104 /** the infra cache */
2105 struct infra_cache* infra;
2106 /** the SSL connection */
2110 /** ssl failure? stop writing and skip the rest. If the tcp
2111 * connection is broken, and writes fail, we then stop writing. */
2115 /** callback for every host element in the infra cache */
2117 dump_infra_host(struct lruhash_entry* e, void* arg)
2119 struct infra_arg* a = (struct infra_arg*)arg;
2120 struct infra_key* k = (struct infra_key*)e->key;
2121 struct infra_data* d = (struct infra_data*)e->data;
2126 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
2127 dname_str(k->zonename, name);
2128 /* skip expired stuff (only backed off) */
2129 if(d->ttl < a->now) {
2130 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
2131 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
2132 name, d->rtt.rto)) {
2139 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
2140 "tA %d tAAAA %d tother %d "
2141 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
2142 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now),
2143 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
2144 d->timeout_A, d->timeout_AAAA, d->timeout_other,
2145 (int)d->edns_lame_known, (int)d->edns_version,
2146 (int)(a->now<d->probedelay?(d->probedelay - a->now):0),
2147 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
2148 (int)d->lame_other)) {
2154 /** do the dump_infra command */
2156 do_dump_infra(SSL* ssl, struct worker* worker)
2158 struct infra_arg arg;
2159 arg.infra = worker->env.infra_cache;
2161 arg.now = *worker->env.now;
2163 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
2166 /** do the log_reopen command */
2168 do_log_reopen(SSL* ssl, struct worker* worker)
2170 struct config_file* cfg = worker->env.cfg;
2172 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
2175 /** do the set_option command */
2177 do_set_option(SSL* ssl, struct worker* worker, char* arg)
2180 if(!find_arg2(ssl, arg, &arg2))
2182 if(!config_set_option(worker->env.cfg, arg, arg2)) {
2183 (void)ssl_printf(ssl, "error setting option\n");
2189 /* routine to printout option values over SSL */
2190 void remote_get_opt_ssl(char* line, void* arg)
2192 SSL* ssl = (SSL*)arg;
2193 (void)ssl_printf(ssl, "%s\n", line);
2196 /** do the get_option command */
2198 do_get_option(SSL* ssl, struct worker* worker, char* arg)
2201 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
2203 (void)ssl_printf(ssl, "error unknown option\n");
2208 /** do the list_forwards command */
2210 do_list_forwards(SSL* ssl, struct worker* worker)
2212 /* since its a per-worker structure no locks needed */
2213 struct iter_forwards* fwds = worker->env.fwds;
2214 struct iter_forward_zone* z;
2215 struct trust_anchor* a;
2217 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
2218 if(!z->dp) continue; /* skip empty marker for stub */
2220 /* see if it is insecure */
2222 if(worker->env.anchors &&
2223 (a=anchor_find(worker->env.anchors, z->name,
2224 z->namelabs, z->namelen, z->dclass))) {
2225 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2227 lock_basic_unlock(&a->lock);
2230 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"),
2231 z->name, z->dclass, z->dp))
2236 /** do the list_stubs command */
2238 do_list_stubs(SSL* ssl, struct worker* worker)
2240 struct iter_hints_stub* z;
2241 struct trust_anchor* a;
2244 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
2246 /* see if it is insecure */
2248 if(worker->env.anchors &&
2249 (a=anchor_find(worker->env.anchors, z->node.name,
2250 z->node.labs, z->node.len, z->node.dclass))) {
2251 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2253 lock_basic_unlock(&a->lock);
2256 snprintf(str, sizeof(str), "stub %sprime%s",
2257 (z->noprime?"no":""), (insecure?" +i":""));
2258 if(!ssl_print_name_dp(ssl, str, z->node.name,
2259 z->node.dclass, z->dp))
2264 /** do the list_local_zones command */
2266 do_list_local_zones(SSL* ssl, struct worker* worker)
2268 struct local_zones* zones = worker->daemon->local_zones;
2269 struct local_zone* z;
2271 lock_rw_rdlock(&zones->lock);
2272 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2273 lock_rw_rdlock(&z->lock);
2274 dname_str(z->name, buf);
2275 if(!ssl_printf(ssl, "%s %s\n", buf,
2276 local_zone_type2str(z->type))) {
2277 /* failure to print */
2278 lock_rw_unlock(&z->lock);
2279 lock_rw_unlock(&zones->lock);
2282 lock_rw_unlock(&z->lock);
2284 lock_rw_unlock(&zones->lock);
2287 /** do the list_local_data command */
2289 do_list_local_data(SSL* ssl, struct worker* worker)
2291 struct local_zones* zones = worker->daemon->local_zones;
2292 struct local_zone* z;
2293 struct local_data* d;
2294 struct local_rrset* p;
2295 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
2296 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
2297 lock_rw_rdlock(&zones->lock);
2298 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2299 lock_rw_rdlock(&z->lock);
2300 RBTREE_FOR(d, struct local_data*, &z->data) {
2301 for(p = d->rrsets; p; p = p->next) {
2302 struct packed_rrset_data* d =
2303 (struct packed_rrset_data*)p->rrset->entry.data;
2305 for(i=0; i<d->count + d->rrsig_count; i++) {
2306 if(!packed_rr_to_string(p->rrset, i,
2308 if(!ssl_printf(ssl, "BADRR\n")) {
2309 lock_rw_unlock(&z->lock);
2310 lock_rw_unlock(&zones->lock);
2314 if(!ssl_printf(ssl, "%s\n", s)) {
2315 lock_rw_unlock(&z->lock);
2316 lock_rw_unlock(&zones->lock);
2322 lock_rw_unlock(&z->lock);
2324 lock_rw_unlock(&zones->lock);
2327 /** struct for user arg ratelimit list */
2328 struct ratelimit_list_arg {
2329 /** the infra cache */
2330 struct infra_cache* infra;
2331 /** the SSL to print to */
2333 /** all or only ratelimited */
2339 /** list items in the ratelimit table */
2341 rate_list(struct lruhash_entry* e, void* arg)
2343 struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg;
2344 struct rate_key* k = (struct rate_key*)e->key;
2345 struct rate_data* d = (struct rate_data*)e->data;
2347 int lim = infra_find_ratelimit(a->infra, k->name, k->namelen);
2348 int max = infra_rate_max(d, a->now);
2353 dname_str(k->name, buf);
2354 ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim);
2357 /** do the ratelimit_list command */
2359 do_ratelimit_list(SSL* ssl, struct worker* worker, char* arg)
2361 struct ratelimit_list_arg a;
2363 a.infra = worker->env.infra_cache;
2364 a.now = *worker->env.now;
2366 arg = skipwhite(arg);
2367 if(strcmp(arg, "+a") == 0)
2369 if(a.infra->domain_rates==NULL ||
2370 (a.all == 0 && infra_dp_ratelimit == 0))
2372 slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a);
2375 /** tell other processes to execute the command */
2377 distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd)
2382 /* skip i=0 which is me */
2383 for(i=1; i<rc->worker->daemon->num; i++) {
2384 worker_send_cmd(rc->worker->daemon->workers[i],
2386 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2387 (uint8_t*)cmd, strlen(cmd)+1, 0)) {
2388 ssl_printf(ssl, "error could not distribute cmd\n");
2394 /** check for name with end-of-string, space or tab after it */
2396 cmdcmp(char* p, const char* cmd, size_t len)
2398 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2401 /** execute a remote control command */
2403 execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd,
2404 struct worker* worker)
2406 char* p = skipwhite(cmd);
2407 /* compare command */
2408 if(cmdcmp(p, "stop", 4)) {
2411 } else if(cmdcmp(p, "reload", 6)) {
2414 } else if(cmdcmp(p, "stats_noreset", 13)) {
2415 do_stats(ssl, rc, 0);
2417 } else if(cmdcmp(p, "stats", 5)) {
2418 do_stats(ssl, rc, 1);
2420 } else if(cmdcmp(p, "status", 6)) {
2421 do_status(ssl, worker);
2423 } else if(cmdcmp(p, "dump_cache", 10)) {
2424 (void)dump_cache(ssl, worker);
2426 } else if(cmdcmp(p, "load_cache", 10)) {
2427 if(load_cache(ssl, worker)) send_ok(ssl);
2429 } else if(cmdcmp(p, "list_forwards", 13)) {
2430 do_list_forwards(ssl, worker);
2432 } else if(cmdcmp(p, "list_stubs", 10)) {
2433 do_list_stubs(ssl, worker);
2435 } else if(cmdcmp(p, "list_insecure", 13)) {
2436 do_insecure_list(ssl, worker);
2438 } else if(cmdcmp(p, "list_local_zones", 16)) {
2439 do_list_local_zones(ssl, worker);
2441 } else if(cmdcmp(p, "list_local_data", 15)) {
2442 do_list_local_data(ssl, worker);
2444 } else if(cmdcmp(p, "ratelimit_list", 14)) {
2445 do_ratelimit_list(ssl, worker, p+14);
2447 } else if(cmdcmp(p, "stub_add", 8)) {
2448 /* must always distribute this cmd */
2449 if(rc) distribute_cmd(rc, ssl, cmd);
2450 do_stub_add(ssl, worker, skipwhite(p+8));
2452 } else if(cmdcmp(p, "stub_remove", 11)) {
2453 /* must always distribute this cmd */
2454 if(rc) distribute_cmd(rc, ssl, cmd);
2455 do_stub_remove(ssl, worker, skipwhite(p+11));
2457 } else if(cmdcmp(p, "forward_add", 11)) {
2458 /* must always distribute this cmd */
2459 if(rc) distribute_cmd(rc, ssl, cmd);
2460 do_forward_add(ssl, worker, skipwhite(p+11));
2462 } else if(cmdcmp(p, "forward_remove", 14)) {
2463 /* must always distribute this cmd */
2464 if(rc) distribute_cmd(rc, ssl, cmd);
2465 do_forward_remove(ssl, worker, skipwhite(p+14));
2467 } else if(cmdcmp(p, "insecure_add", 12)) {
2468 /* must always distribute this cmd */
2469 if(rc) distribute_cmd(rc, ssl, cmd);
2470 do_insecure_add(ssl, worker, skipwhite(p+12));
2472 } else if(cmdcmp(p, "insecure_remove", 15)) {
2473 /* must always distribute this cmd */
2474 if(rc) distribute_cmd(rc, ssl, cmd);
2475 do_insecure_remove(ssl, worker, skipwhite(p+15));
2477 } else if(cmdcmp(p, "forward", 7)) {
2478 /* must always distribute this cmd */
2479 if(rc) distribute_cmd(rc, ssl, cmd);
2480 do_forward(ssl, worker, skipwhite(p+7));
2482 } else if(cmdcmp(p, "flush_stats", 11)) {
2483 /* must always distribute this cmd */
2484 if(rc) distribute_cmd(rc, ssl, cmd);
2485 do_flush_stats(ssl, worker);
2487 } else if(cmdcmp(p, "flush_requestlist", 17)) {
2488 /* must always distribute this cmd */
2489 if(rc) distribute_cmd(rc, ssl, cmd);
2490 do_flush_requestlist(ssl, worker);
2492 } else if(cmdcmp(p, "lookup", 6)) {
2493 do_lookup(ssl, worker, skipwhite(p+6));
2497 #ifdef THREADS_DISABLED
2498 /* other processes must execute the command as well */
2499 /* commands that should not be distributed, returned above. */
2500 if(rc) { /* only if this thread is the master (rc) thread */
2501 /* done before the code below, which may split the string */
2502 distribute_cmd(rc, ssl, cmd);
2505 if(cmdcmp(p, "verbosity", 9)) {
2506 do_verbosity(ssl, skipwhite(p+9));
2507 } else if(cmdcmp(p, "local_zone_remove", 17)) {
2508 do_zone_remove(ssl, worker, skipwhite(p+17));
2509 } else if(cmdcmp(p, "local_zone", 10)) {
2510 do_zone_add(ssl, worker, skipwhite(p+10));
2511 } else if(cmdcmp(p, "local_data_remove", 17)) {
2512 do_data_remove(ssl, worker, skipwhite(p+17));
2513 } else if(cmdcmp(p, "local_data", 10)) {
2514 do_data_add(ssl, worker, skipwhite(p+10));
2515 } else if(cmdcmp(p, "flush_zone", 10)) {
2516 do_flush_zone(ssl, worker, skipwhite(p+10));
2517 } else if(cmdcmp(p, "flush_type", 10)) {
2518 do_flush_type(ssl, worker, skipwhite(p+10));
2519 } else if(cmdcmp(p, "flush_infra", 11)) {
2520 do_flush_infra(ssl, worker, skipwhite(p+11));
2521 } else if(cmdcmp(p, "flush", 5)) {
2522 do_flush_name(ssl, worker, skipwhite(p+5));
2523 } else if(cmdcmp(p, "dump_requestlist", 16)) {
2524 do_dump_requestlist(ssl, worker);
2525 } else if(cmdcmp(p, "dump_infra", 10)) {
2526 do_dump_infra(ssl, worker);
2527 } else if(cmdcmp(p, "log_reopen", 10)) {
2528 do_log_reopen(ssl, worker);
2529 } else if(cmdcmp(p, "set_option", 10)) {
2530 do_set_option(ssl, worker, skipwhite(p+10));
2531 } else if(cmdcmp(p, "get_option", 10)) {
2532 do_get_option(ssl, worker, skipwhite(p+10));
2533 } else if(cmdcmp(p, "flush_bogus", 11)) {
2534 do_flush_bogus(ssl, worker);
2535 } else if(cmdcmp(p, "flush_negative", 14)) {
2536 do_flush_negative(ssl, worker);
2538 (void)ssl_printf(ssl, "error unknown command '%s'\n", p);
2543 daemon_remote_exec(struct worker* worker)
2545 /* read the cmd string */
2546 uint8_t* msg = NULL;
2548 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
2549 log_err("daemon_remote_exec: tube_read_msg failed");
2552 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
2553 execute_cmd(NULL, NULL, (char*)msg, worker);
2557 /** handle remote control request */
2559 handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl)
2566 /* makes it possible to set the socket blocking again. */
2567 /* basically removes it from winsock_event ... */
2568 WSAEventSelect(s->c->fd, NULL, 0);
2570 fd_set_block(s->c->fd);
2572 /* try to read magic UBCT[version]_space_ string */
2574 if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) {
2575 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN)
2577 log_crypto_err("could not SSL_read");
2581 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
2582 verbose(VERB_QUERY, "control connection has bad magic string");
2583 /* probably wrong tool connected, ignore it completely */
2587 /* read the command line */
2588 if(!ssl_read_line(ssl, buf, sizeof(buf))) {
2591 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
2592 if(strcmp(magic, pre) != 0) {
2593 verbose(VERB_QUERY, "control connection had bad "
2594 "version %s, cmd: %s", magic, buf);
2595 ssl_printf(ssl, "error version mismatch\n");
2598 verbose(VERB_DETAIL, "control cmd: %s", buf);
2600 /* figure out what to do */
2601 execute_cmd(rc, ssl, buf, rc->worker);
2604 int remote_control_callback(struct comm_point* c, void* arg, int err,
2605 struct comm_reply* ATTR_UNUSED(rep))
2607 struct rc_state* s = (struct rc_state*)arg;
2608 struct daemon_remote* rc = s->rc;
2610 if(err != NETEVENT_NOERROR) {
2611 if(err==NETEVENT_TIMEOUT)
2612 log_err("remote control timed out");
2616 /* (continue to) setup the SSL connection */
2618 r = SSL_do_handshake(s->ssl);
2620 int r2 = SSL_get_error(s->ssl, r);
2621 if(r2 == SSL_ERROR_WANT_READ) {
2622 if(s->shake_state == rc_hs_read) {
2623 /* try again later */
2626 s->shake_state = rc_hs_read;
2627 comm_point_listen_for_rw(c, 1, 0);
2629 } else if(r2 == SSL_ERROR_WANT_WRITE) {
2630 if(s->shake_state == rc_hs_write) {
2631 /* try again later */
2634 s->shake_state = rc_hs_write;
2635 comm_point_listen_for_rw(c, 0, 1);
2639 log_err("remote control connection closed prematurely");
2640 log_addr(1, "failed connection from",
2641 &s->c->repinfo.addr, s->c->repinfo.addrlen);
2642 log_crypto_err("remote control failed ssl");
2647 s->shake_state = rc_none;
2649 /* once handshake has completed, check authentication */
2650 if (!rc->use_cert) {
2651 verbose(VERB_ALGO, "unauthenticated remote control connection");
2652 } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
2653 X509* x = SSL_get_peer_certificate(s->ssl);
2655 verbose(VERB_DETAIL, "remote control connection "
2656 "provided no client certificate");
2660 verbose(VERB_ALGO, "remote control connection authenticated");
2663 verbose(VERB_DETAIL, "remote control connection failed to "
2664 "authenticate with client certificate");
2669 /* if OK start to actually handle the request */
2670 handle_req(rc, s, s->ssl);
2672 verbose(VERB_ALGO, "remote control operation completed");