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>
50 #include <openssl/dh.h>
54 #include "daemon/remote.h"
55 #include "daemon/worker.h"
56 #include "daemon/daemon.h"
57 #include "daemon/stats.h"
58 #include "daemon/cachedump.h"
60 #include "util/config_file.h"
61 #include "util/net_help.h"
62 #include "util/module.h"
63 #include "services/listen_dnsport.h"
64 #include "services/cache/rrset.h"
65 #include "services/cache/infra.h"
66 #include "services/mesh.h"
67 #include "services/localzone.h"
68 #include "util/storage/slabhash.h"
69 #include "util/fptr_wlist.h"
70 #include "util/data/dname.h"
71 #include "validator/validator.h"
72 #include "validator/val_kcache.h"
73 #include "validator/val_kentry.h"
74 #include "validator/val_anchor.h"
75 #include "iterator/iterator.h"
76 #include "iterator/iter_fwd.h"
77 #include "iterator/iter_hints.h"
78 #include "iterator/iter_delegpt.h"
79 #include "services/outbound_list.h"
80 #include "services/outside_network.h"
81 #include "sldns/str2wire.h"
82 #include "sldns/parseutil.h"
83 #include "sldns/wire2str.h"
84 #include "sldns/sbuffer.h"
86 #ifdef HAVE_SYS_TYPES_H
87 # include <sys/types.h>
89 #ifdef HAVE_SYS_STAT_H
96 /* just for portability */
101 /** what to put on statistics lines between var and value, ": " or "=" */
103 /** if true, inhibits a lot of =0 lines from the stats output */
104 static const int inhibit_zero = 1;
106 /** subtract timers and the values do not overflow or become negative */
108 timeval_subtract(struct timeval* d, const struct timeval* end,
109 const struct timeval* start)
112 time_t end_usec = end->tv_usec;
113 d->tv_sec = end->tv_sec - start->tv_sec;
114 if(end_usec < start->tv_usec) {
118 d->tv_usec = end_usec - start->tv_usec;
122 /** divide sum of timers to get average */
124 timeval_divide(struct timeval* avg, const struct timeval* sum, size_t d)
133 avg->tv_sec = sum->tv_sec / d;
134 avg->tv_usec = sum->tv_usec / d;
135 /* handle fraction from seconds divide */
136 leftover = sum->tv_sec - avg->tv_sec*d;
137 avg->tv_usec += (leftover*1000000)/d;
142 * The following function was generated using the openssl utility, using
143 * the command : "openssl dhparam -dsaparam -C 1024"
144 * (some openssl versions reject DH that is 'too small', eg. 512).
149 static unsigned char dh1024_p[]={
150 0xB3,0x67,0x2E,0x3B,0x68,0xC5,0xDA,0x58,0x46,0xD6,0x2B,0xD3,
151 0x41,0x78,0x97,0xE4,0xE1,0x61,0x71,0x68,0xE6,0x0F,0x1D,0x78,
152 0x05,0xAA,0xF0,0xFF,0x30,0xDF,0xAC,0x49,0x7F,0xE0,0x90,0xFE,
153 0xB9,0x56,0x4E,0x3F,0xE2,0x98,0x8A,0xED,0xF5,0x28,0x39,0xEF,
154 0x2E,0xA6,0xB7,0x67,0xB2,0x43,0xE4,0x53,0xF8,0xEB,0x2C,0x1F,
155 0x06,0x77,0x3A,0x6F,0x62,0x98,0xC1,0x3B,0xF7,0xBA,0x4D,0x93,
156 0xF7,0xEB,0x5A,0xAD,0xC5,0x5F,0xF0,0xB7,0x24,0x35,0x81,0xF7,
157 0x7F,0x1F,0x24,0xC0,0xDF,0xD3,0xD8,0x40,0x72,0x7E,0xF3,0x19,
158 0x2B,0x26,0x27,0xF4,0xB6,0xB3,0xD4,0x7D,0x08,0x23,0xBE,0x68,
159 0x2B,0xCA,0xB4,0x46,0xA8,0x9E,0xDD,0x6C,0x3D,0x75,0xA6,0x48,
160 0xF7,0x44,0x43,0xBF,0x91,0xC2,0xB4,0x49,
162 static unsigned char dh1024_g[]={
163 0x5F,0x37,0xB5,0x80,0x4D,0xB4,0xC4,0xB2,0x37,0x12,0xD5,0x2F,
164 0x56,0x81,0xB0,0xDF,0x3D,0x27,0xA2,0x54,0xE7,0x14,0x65,0x2D,
165 0x72,0xA8,0x97,0xE0,0xA9,0x4A,0x09,0x5E,0x89,0xBE,0x34,0x9A,
166 0x90,0x98,0xC1,0xE8,0xBB,0x01,0x2B,0xC2,0x74,0x74,0x90,0x59,
167 0x0B,0x72,0x62,0x5C,0xFD,0x49,0x63,0x4B,0x38,0x91,0xF1,0x7F,
168 0x13,0x25,0xEB,0x52,0x50,0x47,0xA2,0x8C,0x32,0x28,0x42,0xAC,
169 0xBD,0x7A,0xCC,0x58,0xBE,0x36,0xDA,0x6A,0x24,0x06,0xC7,0xF1,
170 0xDA,0x8D,0x8A,0x3B,0x03,0xFA,0x6F,0x25,0xE5,0x20,0xA7,0xD6,
171 0x6F,0x74,0x61,0x53,0x14,0x81,0x29,0x04,0xB5,0x61,0x12,0x53,
172 0xA3,0xD6,0x09,0x98,0x0C,0x8F,0x1C,0xBB,0xD7,0x1C,0x2C,0xEE,
173 0x56,0x4B,0x74,0x8F,0x4A,0xF8,0xA9,0xD5,
177 if ((dh=DH_new()) == NULL) return(NULL);
178 dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
179 dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
180 if ((dh->p == NULL) || (dh->g == NULL))
181 { DH_free(dh); return(NULL); }
187 struct daemon_remote*
188 daemon_remote_create(struct config_file* cfg)
192 struct daemon_remote* rc = (struct daemon_remote*)calloc(1,
195 log_err("out of memory in daemon_remote_create");
200 if(!cfg->remote_control_enable) {
204 rc->ctx = SSL_CTX_new(SSLv23_server_method());
206 log_crypto_err("could not SSL_CTX_new");
210 /* no SSLv2, SSLv3 because has defects */
211 if((SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)
213 log_crypto_err("could not set SSL_OP_NO_SSLv2");
214 daemon_remote_delete(rc);
217 if((SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)
219 log_crypto_err("could not set SSL_OP_NO_SSLv3");
220 daemon_remote_delete(rc);
224 if (cfg->remote_control_use_cert == 0) {
225 /* No certificates are requested */
226 if(!SSL_CTX_set_cipher_list(rc->ctx, "aNULL")) {
227 log_crypto_err("Failed to set aNULL cipher list");
231 /* Since we have no certificates and hence no source of
232 * DH params, let's generate and set them
234 if(!SSL_CTX_set_tmp_dh(rc->ctx,get_dh1024())) {
235 log_crypto_err("Wanted to set DH param, but failed");
241 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
242 s_key = fname_after_chroot(cfg->server_key_file, cfg, 1);
243 if(!s_cert || !s_key) {
244 log_err("out of memory in remote control fname");
247 verbose(VERB_ALGO, "setup SSL certificates");
248 if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) {
249 log_err("Error for server-cert-file: %s", s_cert);
250 log_crypto_err("Error in SSL_CTX use_certificate_chain_file");
253 if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) {
254 log_err("Error for server-key-file: %s", s_key);
255 log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
258 if(!SSL_CTX_check_private_key(rc->ctx)) {
259 log_err("Error for server-key-file: %s", s_key);
260 log_crypto_err("Error in SSL_CTX check_private_key");
263 #if HAVE_DECL_SSL_CTX_SET_ECDH_AUTO
264 if(!SSL_CTX_set_ecdh_auto(rc->ctx,1)) {
265 log_crypto_err("Error in SSL_CTX_ecdh_auto, not enabling ECDHE");
267 #elif defined(USE_ECDSA)
269 EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
271 log_crypto_err("could not find p256, not enabling ECDHE");
273 if (1 != SSL_CTX_set_tmp_ecdh (rc->ctx, ecdh)) {
274 log_crypto_err("Error in SSL_CTX_set_tmp_ecdh, not enabling ECDHE");
280 if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) {
281 log_crypto_err("Error setting up SSL_CTX verify locations");
285 daemon_remote_delete(rc);
288 SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert));
289 SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL);
296 void daemon_remote_clear(struct daemon_remote* rc)
298 struct rc_state* p, *np;
300 /* but do not close the ports */
301 listen_list_delete(rc->accept_list);
302 rc->accept_list = NULL;
303 /* do close these sockets */
309 comm_point_delete(p->c);
313 rc->busy_list = NULL;
318 void daemon_remote_delete(struct daemon_remote* rc)
321 daemon_remote_clear(rc);
323 SSL_CTX_free(rc->ctx);
329 * Add and open a new control port
332 * @param list: list head
333 * @param noproto_is_err: if lack of protocol support is an error.
334 * @param cfg: config with username for chown of unix-sockets.
335 * @return false on failure.
338 add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err,
339 struct config_file* cfg)
341 struct addrinfo hints;
342 struct addrinfo* res;
343 struct listen_port* n;
347 snprintf(port, sizeof(port), "%d", nr);
348 port[sizeof(port)-1]=0;
349 memset(&hints, 0, sizeof(hints));
352 /* This looks like a local socket */
353 fd = create_local_accept_sock(ip, &noproto);
355 * Change socket ownership and permissions so users other
356 * than root can access it provided they are in the same
357 * group as the user we run as.
361 if (cfg->username && cfg->username[0] &&
362 cfg_uid != (uid_t)-1)
363 chown(ip, cfg_uid, cfg_gid);
364 chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
370 hints.ai_socktype = SOCK_STREAM;
371 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
372 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
374 if(!noproto_is_err && r == EAI_NONAME) {
375 /* tried to lookup the address as name */
376 return 1; /* return success, but do nothing */
378 #endif /* USE_WINSOCK */
379 log_err("control interface %s:%s getaddrinfo: %s %s",
380 ip?ip:"default", port, gai_strerror(r),
382 r==EAI_SYSTEM?(char*)strerror(errno):""
391 fd = create_tcp_accept_sock(res, 1, &noproto, 0,
392 cfg->ip_transparent);
396 if(fd == -1 && noproto) {
398 return 1; /* return success, but do nothing */
399 log_err("cannot open control interface %s %d : "
400 "protocol not supported", ip, nr);
404 log_err("cannot open control interface %s %d", ip, nr);
409 n = (struct listen_port*)calloc(1, sizeof(*n));
416 log_err("out of memory");
425 struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
427 struct listen_port* l = NULL;
428 log_assert(cfg->remote_control_enable && cfg->control_port);
429 if(cfg->control_ifs) {
430 struct config_strlist* p;
431 for(p = cfg->control_ifs; p; p = p->next) {
432 if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) {
433 listening_ports_free(l);
440 !add_open("::1", cfg->control_port, &l, 0, cfg)) {
441 listening_ports_free(l);
445 !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) {
446 listening_ports_free(l);
453 /** open accept commpoint */
455 accept_open(struct daemon_remote* rc, int fd)
457 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
459 log_err("out of memory");
462 n->next = rc->accept_list;
465 n->com = comm_point_create_raw(rc->worker->base, fd, 0,
466 &remote_accept_callback, rc);
469 /* keep this port open, its fd is kept in the rc portlist */
470 n->com->do_not_close = 1;
474 int daemon_remote_open_accept(struct daemon_remote* rc,
475 struct listen_port* ports, struct worker* worker)
477 struct listen_port* p;
479 for(p = ports; p; p = p->next) {
480 if(!accept_open(rc, p->fd)) {
481 log_err("could not create accept comm point");
488 void daemon_remote_stop_accept(struct daemon_remote* rc)
490 struct listen_list* p;
491 for(p=rc->accept_list; p; p=p->next) {
492 comm_point_stop_listening(p->com);
496 void daemon_remote_start_accept(struct daemon_remote* rc)
498 struct listen_list* p;
499 for(p=rc->accept_list; p; p=p->next) {
500 comm_point_start_listening(p->com, -1, -1);
504 int remote_accept_callback(struct comm_point* c, void* arg, int err,
505 struct comm_reply* ATTR_UNUSED(rep))
507 struct daemon_remote* rc = (struct daemon_remote*)arg;
508 struct sockaddr_storage addr;
512 if(err != NETEVENT_NOERROR) {
513 log_err("error %d on remote_accept_callback", err);
516 /* perform the accept */
517 newfd = comm_point_perform_accept(c, &addr, &addrlen);
520 /* create new commpoint unless we are servicing already */
521 if(rc->active >= rc->max_active) {
522 log_warn("drop incoming remote control: too many connections");
532 /* setup commpoint to service the remote control command */
533 n = (struct rc_state*)calloc(1, sizeof(*n));
535 log_err("out of memory");
538 /* start in reading state */
539 n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
540 &remote_control_callback, n);
542 log_err("out of memory");
546 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
547 n->c->do_not_close = 0;
548 comm_point_stop_listening(n->c);
549 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
550 memcpy(&n->c->repinfo.addr, &addr, addrlen);
551 n->c->repinfo.addrlen = addrlen;
552 n->shake_state = rc_hs_read;
553 n->ssl = SSL_new(rc->ctx);
555 log_crypto_err("could not SSL_new");
556 comm_point_delete(n->c);
560 SSL_set_accept_state(n->ssl);
561 (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY);
562 if(!SSL_set_fd(n->ssl, newfd)) {
563 log_crypto_err("could not SSL_set_fd");
565 comm_point_delete(n->c);
571 n->next = rc->busy_list;
575 /* perform the first nonblocking read already, for windows,
576 * so it can return wouldblock. could be faster too. */
577 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
581 /** delete from list */
583 state_list_remove_elem(struct rc_state** list, struct comm_point* c)
586 if( (*list)->c == c) {
587 *list = (*list)->next;
590 list = &(*list)->next;
594 /** decrease active count and remove commpoint from busy list */
596 clean_point(struct daemon_remote* rc, struct rc_state* s)
598 state_list_remove_elem(&rc->busy_list, s->c);
601 SSL_shutdown(s->ssl);
604 comm_point_delete(s->c);
609 ssl_print_text(SSL* ssl, const char* text)
615 if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) {
616 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
617 verbose(VERB_QUERY, "warning, in SSL_write, peer "
618 "closed connection");
621 log_crypto_err("could not SSL_write");
627 /** print text over the ssl connection */
629 ssl_print_vmsg(SSL* ssl, const char* format, va_list args)
632 vsnprintf(msg, sizeof(msg), format, args);
633 return ssl_print_text(ssl, msg);
636 /** printf style printing to the ssl connection */
637 int ssl_printf(SSL* ssl, const char* format, ...)
641 va_start(args, format);
642 ret = ssl_print_vmsg(ssl, format, args);
648 ssl_read_line(SSL* ssl, char* buf, size_t max)
656 if((r=SSL_read(ssl, buf+len, 1)) <= 0) {
657 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
661 log_crypto_err("could not SSL_read");
664 if(buf[len] == '\n') {
665 /* return string without \n */
672 log_err("control line too long (%d): %s", (int)max, buf);
676 /** skip whitespace, return new pointer into string */
680 /* EOS \0 is not a space */
681 while( isspace((unsigned char)*str) )
686 /** send the OK to the control client */
687 static void send_ok(SSL* ssl)
689 (void)ssl_printf(ssl, "ok\n");
692 /** do the stop command */
694 do_stop(SSL* ssl, struct daemon_remote* rc)
696 rc->worker->need_to_exit = 1;
697 comm_base_exit(rc->worker->base);
701 /** do the reload command */
703 do_reload(SSL* ssl, struct daemon_remote* rc)
705 rc->worker->need_to_exit = 0;
706 comm_base_exit(rc->worker->base);
710 /** do the verbosity command */
712 do_verbosity(SSL* ssl, char* str)
715 if(val == 0 && strcmp(str, "0") != 0) {
716 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
723 /** print stats from statinfo */
725 print_stats(SSL* ssl, const char* nm, struct stats_info* s)
728 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm,
729 (unsigned long)s->svr.num_queries)) return 0;
730 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm,
731 (unsigned long)(s->svr.num_queries
732 - s->svr.num_queries_missed_cache))) return 0;
733 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm,
734 (unsigned long)s->svr.num_queries_missed_cache)) return 0;
735 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm,
736 (unsigned long)s->svr.num_queries_prefetch)) return 0;
737 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm,
738 (unsigned long)s->mesh_replies_sent)) return 0;
739 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
740 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
741 (double)s->svr.sum_query_list_size/
742 (s->svr.num_queries_missed_cache+
743 s->svr.num_queries_prefetch) : 0.0)) return 0;
744 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm,
745 (unsigned long)s->svr.max_query_list_size)) return 0;
746 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm,
747 (unsigned long)s->mesh_jostled)) return 0;
748 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm,
749 (unsigned long)s->mesh_dropped)) return 0;
750 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm,
751 (unsigned long)s->mesh_num_states)) return 0;
752 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm,
753 (unsigned long)s->mesh_num_reply_states)) return 0;
754 timeval_divide(&avg, &s->mesh_replies_sum_wait, s->mesh_replies_sent);
755 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
756 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
757 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
758 s->mesh_time_median)) return 0;
759 if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm,
760 (unsigned long)s->svr.tcp_accept_usage)) return 0;
764 /** print stats for one thread */
766 print_thread_stats(SSL* ssl, int i, struct stats_info* s)
769 snprintf(nm, sizeof(nm), "thread%d", i);
771 return print_stats(ssl, nm, s);
774 /** print long number */
776 print_longnum(SSL* ssl, const char* desc, size_t x)
778 if(x > 1024*1024*1024) {
780 size_t front = x / (size_t)1000000;
781 size_t back = x % (size_t)1000000;
782 return ssl_printf(ssl, "%s%u%6.6u\n", desc,
783 (unsigned)front, (unsigned)back);
785 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x);
789 /** print mem stats */
791 print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon)
794 size_t msg, rrset, val, iter;
796 extern void* unbound_start_brk;
798 if(!print_longnum(ssl, "mem.total.sbrk"SQ,
799 (size_t)((char*)cur - (char*)unbound_start_brk))) return 0;
800 #endif /* HAVE_SBRK */
801 msg = slabhash_get_mem(daemon->env->msg_cache);
802 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
805 m = modstack_find(&worker->env.mesh->mods, "validator");
807 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
808 mods.mod[m]->get_mem));
809 val = (*worker->env.mesh->mods.mod[m]->get_mem)
812 m = modstack_find(&worker->env.mesh->mods, "iterator");
814 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
815 mods.mod[m]->get_mem));
816 iter = (*worker->env.mesh->mods.mod[m]->get_mem)
820 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
822 if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
824 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
826 if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
831 /** print uptime stats */
833 print_uptime(SSL* ssl, struct worker* worker, int reset)
835 struct timeval now = *worker->env.now_tv;
836 struct timeval up, dt;
837 timeval_subtract(&up, &now, &worker->daemon->time_boot);
838 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
840 worker->daemon->time_last_stat = now;
841 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
842 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
843 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
844 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
845 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
846 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
850 /** print extended histogram */
852 print_hist(SSL* ssl, struct stats_info* s)
854 struct timehist* hist;
856 hist = timehist_setup();
858 log_err("out of memory");
861 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
862 for(i=0; i<hist->num; i++) {
864 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
865 (int)hist->buckets[i].lower.tv_sec,
866 (int)hist->buckets[i].lower.tv_usec,
867 (int)hist->buckets[i].upper.tv_sec,
868 (int)hist->buckets[i].upper.tv_usec,
869 (unsigned long)hist->buckets[i].count)) {
870 timehist_delete(hist);
874 timehist_delete(hist);
878 /** print extended stats */
880 print_ext(SSL* ssl, struct stats_info* s)
884 const sldns_rr_descriptor* desc;
885 const sldns_lookup_table* lt;
887 for(i=0; i<STATS_QTYPE_NUM; i++) {
888 if(inhibit_zero && s->svr.qtype[i] == 0)
890 desc = sldns_rr_descript((uint16_t)i);
891 if(desc && desc->_name) {
892 snprintf(nm, sizeof(nm), "%s", desc->_name);
893 } else if (i == LDNS_RR_TYPE_IXFR) {
894 snprintf(nm, sizeof(nm), "IXFR");
895 } else if (i == LDNS_RR_TYPE_AXFR) {
896 snprintf(nm, sizeof(nm), "AXFR");
897 } else if (i == LDNS_RR_TYPE_MAILA) {
898 snprintf(nm, sizeof(nm), "MAILA");
899 } else if (i == LDNS_RR_TYPE_MAILB) {
900 snprintf(nm, sizeof(nm), "MAILB");
901 } else if (i == LDNS_RR_TYPE_ANY) {
902 snprintf(nm, sizeof(nm), "ANY");
904 snprintf(nm, sizeof(nm), "TYPE%d", i);
906 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n",
907 nm, (unsigned long)s->svr.qtype[i])) return 0;
909 if(!inhibit_zero || s->svr.qtype_big) {
910 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n",
911 (unsigned long)s->svr.qtype_big)) return 0;
914 for(i=0; i<STATS_QCLASS_NUM; i++) {
915 if(inhibit_zero && s->svr.qclass[i] == 0)
917 lt = sldns_lookup_by_id(sldns_rr_classes, i);
919 snprintf(nm, sizeof(nm), "%s", lt->name);
921 snprintf(nm, sizeof(nm), "CLASS%d", i);
923 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n",
924 nm, (unsigned long)s->svr.qclass[i])) return 0;
926 if(!inhibit_zero || s->svr.qclass_big) {
927 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n",
928 (unsigned long)s->svr.qclass_big)) return 0;
931 for(i=0; i<STATS_OPCODE_NUM; i++) {
932 if(inhibit_zero && s->svr.qopcode[i] == 0)
934 lt = sldns_lookup_by_id(sldns_opcodes, i);
936 snprintf(nm, sizeof(nm), "%s", lt->name);
938 snprintf(nm, sizeof(nm), "OPCODE%d", i);
940 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n",
941 nm, (unsigned long)s->svr.qopcode[i])) return 0;
944 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n",
945 (unsigned long)s->svr.qtcp)) return 0;
946 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n",
947 (unsigned long)s->svr.qtcp_outgoing)) return 0;
948 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n",
949 (unsigned long)s->svr.qipv6)) return 0;
951 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n",
952 (unsigned long)s->svr.qbit_QR)) return 0;
953 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n",
954 (unsigned long)s->svr.qbit_AA)) return 0;
955 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n",
956 (unsigned long)s->svr.qbit_TC)) return 0;
957 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n",
958 (unsigned long)s->svr.qbit_RD)) return 0;
959 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n",
960 (unsigned long)s->svr.qbit_RA)) return 0;
961 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n",
962 (unsigned long)s->svr.qbit_Z)) return 0;
963 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n",
964 (unsigned long)s->svr.qbit_AD)) return 0;
965 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n",
966 (unsigned long)s->svr.qbit_CD)) return 0;
967 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n",
968 (unsigned long)s->svr.qEDNS)) return 0;
969 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n",
970 (unsigned long)s->svr.qEDNS_DO)) return 0;
973 for(i=0; i<STATS_RCODE_NUM; i++) {
974 /* Always include RCODEs 0-5 */
975 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
977 lt = sldns_lookup_by_id(sldns_rcodes, i);
979 snprintf(nm, sizeof(nm), "%s", lt->name);
981 snprintf(nm, sizeof(nm), "RCODE%d", i);
983 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n",
984 nm, (unsigned long)s->svr.ans_rcode[i])) return 0;
986 if(!inhibit_zero || s->svr.ans_rcode_nodata) {
987 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n",
988 (unsigned long)s->svr.ans_rcode_nodata)) return 0;
991 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n",
992 (unsigned long)s->svr.ans_secure)) return 0;
993 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n",
994 (unsigned long)s->svr.ans_bogus)) return 0;
995 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n",
996 (unsigned long)s->svr.rrset_bogus)) return 0;
997 /* threat detection */
998 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n",
999 (unsigned long)s->svr.unwanted_queries)) return 0;
1000 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n",
1001 (unsigned long)s->svr.unwanted_replies)) return 0;
1003 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
1004 (unsigned)s->svr.msg_cache_count)) return 0;
1005 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
1006 (unsigned)s->svr.rrset_cache_count)) return 0;
1007 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
1008 (unsigned)s->svr.infra_cache_count)) return 0;
1009 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
1010 (unsigned)s->svr.key_cache_count)) return 0;
1014 /** do the stats command */
1016 do_stats(SSL* ssl, struct daemon_remote* rc, int reset)
1018 struct daemon* daemon = rc->worker->daemon;
1019 struct stats_info total;
1020 struct stats_info s;
1022 log_assert(daemon->num > 0);
1023 /* gather all thread statistics in one place */
1024 for(i=0; i<daemon->num; i++) {
1025 server_stats_obtain(rc->worker, daemon->workers[i], &s, reset);
1026 if(!print_thread_stats(ssl, i, &s))
1030 else server_stats_add(&total, &s);
1032 /* print the thread statistics */
1033 total.mesh_time_median /= (double)daemon->num;
1034 if(!print_stats(ssl, "total", &total))
1036 if(!print_uptime(ssl, rc->worker, reset))
1038 if(daemon->cfg->stat_extended) {
1039 if(!print_mem(ssl, rc->worker, daemon))
1041 if(!print_hist(ssl, &total))
1043 if(!print_ext(ssl, &total))
1048 /** parse commandline argument domain name */
1050 parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs)
1052 uint8_t nm[LDNS_MAX_DOMAINLEN+1];
1053 size_t nmlen = sizeof(nm);
1058 status = sldns_str2wire_dname_buf(str, nm, &nmlen);
1060 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
1061 LDNS_WIREPARSE_OFFSET(status),
1062 sldns_get_errorstr_parse(status));
1065 *res = memdup(nm, nmlen);
1067 ssl_printf(ssl, "error out of memory\n");
1070 *labs = dname_count_size_labels(*res, len);
1074 /** find second argument, modifies string */
1076 find_arg2(SSL* ssl, char* arg, char** arg2)
1078 char* as = strchr(arg, ' ');
1079 char* at = strchr(arg, '\t');
1084 *arg2 = skipwhite(as+1);
1087 *arg2 = skipwhite(as+1);
1090 *arg2 = skipwhite(at+1);
1092 ssl_printf(ssl, "error could not find next argument "
1099 /** Add a new zone */
1101 do_zone_add(SSL* ssl, struct worker* worker, char* arg)
1107 enum localzone_type t;
1108 struct local_zone* z;
1109 if(!find_arg2(ssl, arg, &arg2))
1111 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1113 if(!local_zone_str2type(arg2, &t)) {
1114 ssl_printf(ssl, "error not a zone type. %s\n", arg2);
1118 lock_rw_wrlock(&worker->daemon->local_zones->lock);
1119 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1120 nmlabs, LDNS_RR_CLASS_IN))) {
1121 /* already present in tree */
1122 lock_rw_wrlock(&z->lock);
1123 z->type = t; /* update type anyway */
1124 lock_rw_unlock(&z->lock);
1126 lock_rw_unlock(&worker->daemon->local_zones->lock);
1130 if(!local_zones_add_zone(worker->daemon->local_zones, nm, nmlen,
1131 nmlabs, LDNS_RR_CLASS_IN, t)) {
1132 lock_rw_unlock(&worker->daemon->local_zones->lock);
1133 ssl_printf(ssl, "error out of memory\n");
1136 lock_rw_unlock(&worker->daemon->local_zones->lock);
1140 /** Remove a zone */
1142 do_zone_remove(SSL* ssl, struct worker* worker, char* arg)
1147 struct local_zone* z;
1148 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1150 lock_rw_wrlock(&worker->daemon->local_zones->lock);
1151 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1152 nmlabs, LDNS_RR_CLASS_IN))) {
1153 /* present in tree */
1154 local_zones_del_zone(worker->daemon->local_zones, z);
1156 lock_rw_unlock(&worker->daemon->local_zones->lock);
1161 /** Add new RR data */
1163 do_data_add(SSL* ssl, struct worker* worker, char* arg)
1165 if(!local_zones_add_RR(worker->daemon->local_zones, arg)) {
1166 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1172 /** Remove RR data */
1174 do_data_remove(SSL* ssl, struct worker* worker, char* arg)
1179 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1181 local_zones_del_data(worker->daemon->local_zones, nm,
1182 nmlen, nmlabs, LDNS_RR_CLASS_IN);
1187 /** cache lookup of nameservers */
1189 do_lookup(SSL* ssl, struct worker* worker, char* arg)
1194 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1196 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1200 /** flush something from rrset and msg caches */
1202 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1203 uint16_t t, uint16_t c)
1206 struct query_info k;
1207 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1208 if(t == LDNS_RR_TYPE_SOA)
1209 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1210 PACKED_RRSET_SOA_NEG);
1212 k.qname_len = nmlen;
1215 h = query_info_hash(&k, 0);
1216 slabhash_remove(worker->env.msg_cache, h, &k);
1217 if(t == LDNS_RR_TYPE_AAAA) {
1218 /* for AAAA also flush dns64 bit_cd packet */
1219 h = query_info_hash(&k, BIT_CD);
1220 slabhash_remove(worker->env.msg_cache, h, &k);
1226 do_flush_type(SSL* ssl, struct worker* worker, char* arg)
1233 if(!find_arg2(ssl, arg, &arg2))
1235 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1237 t = sldns_get_rr_type_by_name(arg2);
1238 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1244 /** flush statistics */
1246 do_flush_stats(SSL* ssl, struct worker* worker)
1248 worker_stats_clear(worker);
1253 * Local info for deletion functions
1257 struct worker* worker;
1258 /** name to delete */
1264 /** time to invalidate to */
1266 /** number of rrsets removed */
1268 /** number of msgs removed */
1270 /** number of key entries removed */
1272 /** length of addr */
1274 /** socket address for host deletion */
1275 struct sockaddr_storage addr;
1278 /** callback to delete hosts in infra cache */
1280 infra_del_host(struct lruhash_entry* e, void* arg)
1282 /* entry is locked */
1283 struct del_info* inf = (struct del_info*)arg;
1284 struct infra_key* k = (struct infra_key*)e->key;
1285 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1286 struct infra_data* d = (struct infra_data*)e->data;
1289 d->timeout_AAAA = 0;
1290 d->timeout_other = 0;
1292 if(d->ttl > inf->expired) {
1293 d->ttl = inf->expired;
1299 /** flush infra cache */
1301 do_flush_infra(SSL* ssl, struct worker* worker, char* arg)
1303 struct sockaddr_storage addr;
1305 struct del_info inf;
1306 if(strcmp(arg, "all") == 0) {
1307 slabhash_clear(worker->env.infra_cache->hosts);
1311 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1312 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1315 /* delete all entries from cache */
1316 /* what we do is to set them all expired */
1317 inf.worker = worker;
1321 inf.expired = *worker->env.now;
1322 inf.expired -= 3; /* handle 3 seconds skew between threads */
1327 memmove(&inf.addr, &addr, len);
1328 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1333 /** flush requestlist */
1335 do_flush_requestlist(SSL* ssl, struct worker* worker)
1337 mesh_delete_all(worker->env.mesh);
1341 /** callback to delete rrsets in a zone */
1343 zone_del_rrset(struct lruhash_entry* e, void* arg)
1345 /* entry is locked */
1346 struct del_info* inf = (struct del_info*)arg;
1347 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1348 if(dname_subdomain_c(k->rk.dname, inf->name)) {
1349 struct packed_rrset_data* d =
1350 (struct packed_rrset_data*)e->data;
1351 if(d->ttl > inf->expired) {
1352 d->ttl = inf->expired;
1358 /** callback to delete messages in a zone */
1360 zone_del_msg(struct lruhash_entry* e, void* arg)
1362 /* entry is locked */
1363 struct del_info* inf = (struct del_info*)arg;
1364 struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1365 if(dname_subdomain_c(k->key.qname, inf->name)) {
1366 struct reply_info* d = (struct reply_info*)e->data;
1367 if(d->ttl > inf->expired) {
1368 d->ttl = inf->expired;
1374 /** callback to delete keys in zone */
1376 zone_del_kcache(struct lruhash_entry* e, void* arg)
1378 /* entry is locked */
1379 struct del_info* inf = (struct del_info*)arg;
1380 struct key_entry_key* k = (struct key_entry_key*)e->key;
1381 if(dname_subdomain_c(k->name, inf->name)) {
1382 struct key_entry_data* d = (struct key_entry_data*)e->data;
1383 if(d->ttl > inf->expired) {
1384 d->ttl = inf->expired;
1390 /** remove all rrsets and keys from zone from cache */
1392 do_flush_zone(SSL* ssl, struct worker* worker, char* arg)
1397 struct del_info inf;
1398 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1400 /* delete all RRs and key entries from zone */
1401 /* what we do is to set them all expired */
1402 inf.worker = worker;
1406 inf.expired = *worker->env.now;
1407 inf.expired -= 3; /* handle 3 seconds skew between threads */
1411 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1412 &zone_del_rrset, &inf);
1414 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1416 /* and validator cache */
1417 if(worker->env.key_cache) {
1418 slabhash_traverse(worker->env.key_cache->slab, 1,
1419 &zone_del_kcache, &inf);
1424 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1425 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1426 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1429 /** callback to delete bogus rrsets */
1431 bogus_del_rrset(struct lruhash_entry* e, void* arg)
1433 /* entry is locked */
1434 struct del_info* inf = (struct del_info*)arg;
1435 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1436 if(d->security == sec_status_bogus) {
1437 d->ttl = inf->expired;
1442 /** callback to delete bogus messages */
1444 bogus_del_msg(struct lruhash_entry* e, void* arg)
1446 /* entry is locked */
1447 struct del_info* inf = (struct del_info*)arg;
1448 struct reply_info* d = (struct reply_info*)e->data;
1449 if(d->security == sec_status_bogus) {
1450 d->ttl = inf->expired;
1455 /** callback to delete bogus keys */
1457 bogus_del_kcache(struct lruhash_entry* e, void* arg)
1459 /* entry is locked */
1460 struct del_info* inf = (struct del_info*)arg;
1461 struct key_entry_data* d = (struct key_entry_data*)e->data;
1463 d->ttl = inf->expired;
1468 /** remove all bogus rrsets, msgs and keys from cache */
1470 do_flush_bogus(SSL* ssl, struct worker* worker)
1472 struct del_info inf;
1473 /* what we do is to set them all expired */
1474 inf.worker = worker;
1475 inf.expired = *worker->env.now;
1476 inf.expired -= 3; /* handle 3 seconds skew between threads */
1480 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1481 &bogus_del_rrset, &inf);
1483 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1485 /* and validator cache */
1486 if(worker->env.key_cache) {
1487 slabhash_traverse(worker->env.key_cache->slab, 1,
1488 &bogus_del_kcache, &inf);
1491 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1492 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1493 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1496 /** callback to delete negative and servfail rrsets */
1498 negative_del_rrset(struct lruhash_entry* e, void* arg)
1500 /* entry is locked */
1501 struct del_info* inf = (struct del_info*)arg;
1502 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1503 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1504 /* delete the parentside negative cache rrsets,
1505 * these are namerserver rrsets that failed lookup, rdata empty */
1506 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 &&
1507 d->rrsig_count == 0 && d->rr_len[0] == 0) {
1508 d->ttl = inf->expired;
1513 /** callback to delete negative and servfail messages */
1515 negative_del_msg(struct lruhash_entry* e, void* arg)
1517 /* entry is locked */
1518 struct del_info* inf = (struct del_info*)arg;
1519 struct reply_info* d = (struct reply_info*)e->data;
1520 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1521 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1522 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) {
1523 d->ttl = inf->expired;
1528 /** callback to delete negative key entries */
1530 negative_del_kcache(struct lruhash_entry* e, void* arg)
1532 /* entry is locked */
1533 struct del_info* inf = (struct del_info*)arg;
1534 struct key_entry_data* d = (struct key_entry_data*)e->data;
1535 /* could be bad because of lookup failure on the DS, DNSKEY, which
1536 * was nxdomain or servfail, and thus a result of negative lookups */
1538 d->ttl = inf->expired;
1543 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1545 do_flush_negative(SSL* ssl, struct worker* worker)
1547 struct del_info inf;
1548 /* what we do is to set them all expired */
1549 inf.worker = worker;
1550 inf.expired = *worker->env.now;
1551 inf.expired -= 3; /* handle 3 seconds skew between threads */
1555 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1556 &negative_del_rrset, &inf);
1558 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf);
1560 /* and validator cache */
1561 if(worker->env.key_cache) {
1562 slabhash_traverse(worker->env.key_cache->slab, 1,
1563 &negative_del_kcache, &inf);
1566 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1567 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1568 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1571 /** remove name rrset from cache */
1573 do_flush_name(SSL* ssl, struct worker* w, char* arg)
1578 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1580 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1581 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1582 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1583 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1584 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1585 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1586 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1587 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1588 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1589 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1595 /** printout a delegation point info */
1597 ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1601 struct delegpt_ns* ns;
1602 struct delegpt_addr* a;
1604 if(str) { /* print header for forward, stub */
1605 char* c = sldns_wire2str_class(dclass);
1607 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) {
1613 for(ns = dp->nslist; ns; ns = ns->next) {
1614 dname_str(ns->name, buf);
1615 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1619 for(a = dp->target_list; a; a = a->next_target) {
1620 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1621 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1625 return ssl_printf(ssl, "\n");
1629 /** print root forwards */
1631 print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root)
1634 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1636 return ssl_printf(ssl, "off (using root hints)\n");
1637 /* if dp is returned it must be the root */
1638 log_assert(query_dname_compare(dp->name, root)==0);
1639 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1642 /** parse args into delegpt */
1643 static struct delegpt*
1644 parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names)
1646 /* parse args and add in */
1649 struct delegpt* dp = delegpt_create_mlc(nm);
1650 struct sockaddr_storage addr;
1653 (void)ssl_printf(ssl, "error out of memory\n");
1658 p = strchr(p, ' '); /* find next spot, if any */
1660 *p++ = 0; /* end this spot */
1661 p = skipwhite(p); /* position at next spot */
1664 if(!extstrtoaddr(todo, &addr, &addrlen)) {
1669 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
1670 (void)ssl_printf(ssl, "error cannot "
1671 "parse IP address or name "
1673 delegpt_free_mlc(dp);
1676 if(!delegpt_add_ns_mlc(dp, n, 0)) {
1677 (void)ssl_printf(ssl, "error out of memory\n");
1679 delegpt_free_mlc(dp);
1685 (void)ssl_printf(ssl, "error cannot parse"
1686 " IP address '%s'\n", todo);
1687 delegpt_free_mlc(dp);
1692 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) {
1693 (void)ssl_printf(ssl, "error out of memory\n");
1694 delegpt_free_mlc(dp);
1699 dp->has_parent_side_NS = 1;
1703 /** do the status command */
1705 do_forward(SSL* ssl, struct worker* worker, char* args)
1707 struct iter_forwards* fwd = worker->env.fwds;
1708 uint8_t* root = (uint8_t*)"\000";
1710 (void)ssl_printf(ssl, "error: structure not allocated\n");
1713 if(args == NULL || args[0] == 0) {
1714 (void)print_root_fwds(ssl, fwd, root);
1717 /* set root forwards for this thread. since we are in remote control
1718 * the actual mesh is not running, so we can freely edit it. */
1719 /* delete all the existing queries first */
1720 mesh_delete_all(worker->env.mesh);
1721 if(strcmp(args, "off") == 0) {
1722 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
1725 if(!(dp = parse_delegpt(ssl, args, root, 0)))
1727 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1728 (void)ssl_printf(ssl, "error out of memory\n");
1736 parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp,
1737 int* insecure, int* prime)
1743 /* parse all -x args */
1744 while(args[0] == '+') {
1745 if(!find_arg2(ssl, args, &rest))
1747 while(*(++args) != 0) {
1748 if(*args == 'i' && insecure)
1750 else if(*args == 'p' && prime)
1753 (void)ssl_printf(ssl, "error: unknown option %s\n", args);
1761 if(!find_arg2(ssl, args, &rest))
1765 } else zonename = args;
1766 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
1771 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
1779 /** do the forward_add command */
1781 do_forward_add(SSL* ssl, struct worker* worker, char* args)
1783 struct iter_forwards* fwd = worker->env.fwds;
1786 struct delegpt* dp = NULL;
1787 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
1789 if(insecure && worker->env.anchors) {
1790 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1792 (void)ssl_printf(ssl, "error out of memory\n");
1793 delegpt_free_mlc(dp);
1798 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1799 (void)ssl_printf(ssl, "error out of memory\n");
1807 /** do the forward_remove command */
1809 do_forward_remove(SSL* ssl, struct worker* worker, char* args)
1811 struct iter_forwards* fwd = worker->env.fwds;
1814 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1816 if(insecure && worker->env.anchors)
1817 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1819 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
1824 /** do the stub_add command */
1826 do_stub_add(SSL* ssl, struct worker* worker, char* args)
1828 struct iter_forwards* fwd = worker->env.fwds;
1829 int insecure = 0, prime = 0;
1831 struct delegpt* dp = NULL;
1832 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
1834 if(insecure && worker->env.anchors) {
1835 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1837 (void)ssl_printf(ssl, "error out of memory\n");
1838 delegpt_free_mlc(dp);
1843 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
1844 if(insecure && worker->env.anchors)
1845 anchors_delete_insecure(worker->env.anchors,
1846 LDNS_RR_CLASS_IN, nm);
1847 (void)ssl_printf(ssl, "error out of memory\n");
1848 delegpt_free_mlc(dp);
1852 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
1853 (void)ssl_printf(ssl, "error out of memory\n");
1854 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1855 if(insecure && worker->env.anchors)
1856 anchors_delete_insecure(worker->env.anchors,
1857 LDNS_RR_CLASS_IN, nm);
1865 /** do the stub_remove command */
1867 do_stub_remove(SSL* ssl, struct worker* worker, char* args)
1869 struct iter_forwards* fwd = worker->env.fwds;
1872 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1874 if(insecure && worker->env.anchors)
1875 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1877 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1878 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
1883 /** do the insecure_add command */
1885 do_insecure_add(SSL* ssl, struct worker* worker, char* arg)
1890 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1892 if(worker->env.anchors) {
1893 if(!anchors_add_insecure(worker->env.anchors,
1894 LDNS_RR_CLASS_IN, nm)) {
1895 (void)ssl_printf(ssl, "error out of memory\n");
1904 /** do the insecure_remove command */
1906 do_insecure_remove(SSL* ssl, struct worker* worker, char* arg)
1911 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1913 if(worker->env.anchors)
1914 anchors_delete_insecure(worker->env.anchors,
1915 LDNS_RR_CLASS_IN, nm);
1921 do_insecure_list(SSL* ssl, struct worker* worker)
1924 struct trust_anchor* a;
1925 if(worker->env.anchors) {
1926 RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) {
1927 if(a->numDS == 0 && a->numDNSKEY == 0) {
1928 dname_str(a->name, buf);
1929 ssl_printf(ssl, "%s\n", buf);
1935 /** do the status command */
1937 do_status(SSL* ssl, struct worker* worker)
1941 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
1943 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
1945 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
1947 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
1949 for(i=0; i<worker->daemon->mods.num; i++) {
1950 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
1953 if(!ssl_printf(ssl, " ]\n"))
1955 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
1956 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
1958 if(!ssl_printf(ssl, "options:%s%s\n" ,
1959 (worker->daemon->reuseport?" reuseport":""),
1960 (worker->daemon->rc->accept_list?" control(ssl)":"")))
1962 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
1967 /** get age for the mesh state */
1969 get_mesh_age(struct mesh_state* m, char* buf, size_t len,
1970 struct module_env* env)
1974 struct mesh_reply* r = m->reply_list;
1975 /* last reply is the oldest */
1978 timeval_subtract(&d, env->now_tv, &r->start_time);
1979 snprintf(buf, len, ARG_LL "d.%6.6d",
1980 (long long)d.tv_sec, (int)d.tv_usec);
1982 snprintf(buf, len, "-");
1986 /** get status of a mesh state */
1988 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
1989 char* buf, size_t len)
1991 enum module_ext_state s = m->s.ext_state[m->s.curmod];
1992 const char *modname = mesh->mods.mod[m->s.curmod]->name;
1994 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
1995 m->s.minfo[m->s.curmod]) {
1996 /* break into iterator to find out who its waiting for */
1997 struct iter_qstate* qstate = (struct iter_qstate*)
1998 m->s.minfo[m->s.curmod];
1999 struct outbound_list* ol = &qstate->outlist;
2000 struct outbound_entry* e;
2001 snprintf(buf, len, "%s wait for", modname);
2004 if(ol->first == NULL)
2005 snprintf(buf, len, " (empty_list)");
2006 for(e = ol->first; e; e = e->next) {
2007 snprintf(buf, len, " ");
2010 addr_to_str(&e->qsent->addr, e->qsent->addrlen,
2015 } else if(s == module_wait_subquery) {
2016 /* look in subs from mesh state to see what */
2018 struct mesh_state_ref* sub;
2019 snprintf(buf, len, "%s wants", modname);
2022 if(m->sub_set.count == 0)
2023 snprintf(buf, len, " (empty_list)");
2024 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
2025 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
2026 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
2027 dname_str(sub->s->s.qinfo.qname, nm);
2028 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
2029 (c?c:"CLASS??"), nm);
2036 snprintf(buf, len, "%s is %s", modname, strextstate(s));
2040 /** do the dump_requestlist command */
2042 do_dump_requestlist(SSL* ssl, struct worker* worker)
2044 struct mesh_area* mesh;
2045 struct mesh_state* m;
2049 char statbuf[10240];
2050 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
2052 if(!ssl_printf(ssl, "# type cl name seconds module status\n"))
2054 /* show worker mesh contents */
2055 mesh = worker->env.mesh;
2057 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
2058 char* t = sldns_wire2str_type(m->s.qinfo.qtype);
2059 char* c = sldns_wire2str_class(m->s.qinfo.qclass);
2060 dname_str(m->s.qinfo.qname, buf);
2061 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
2062 get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
2063 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
2064 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
2076 /** structure for argument data for dump infra host */
2078 /** the infra cache */
2079 struct infra_cache* infra;
2080 /** the SSL connection */
2084 /** ssl failure? stop writing and skip the rest. If the tcp
2085 * connection is broken, and writes fail, we then stop writing. */
2089 /** callback for every host element in the infra cache */
2091 dump_infra_host(struct lruhash_entry* e, void* arg)
2093 struct infra_arg* a = (struct infra_arg*)arg;
2094 struct infra_key* k = (struct infra_key*)e->key;
2095 struct infra_data* d = (struct infra_data*)e->data;
2100 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
2101 dname_str(k->zonename, name);
2102 /* skip expired stuff (only backed off) */
2103 if(d->ttl < a->now) {
2104 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
2105 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
2106 name, d->rtt.rto)) {
2113 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
2114 "tA %d tAAAA %d tother %d "
2115 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
2116 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now),
2117 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
2118 d->timeout_A, d->timeout_AAAA, d->timeout_other,
2119 (int)d->edns_lame_known, (int)d->edns_version,
2120 (int)(a->now<d->probedelay?(d->probedelay - a->now):0),
2121 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
2122 (int)d->lame_other)) {
2128 /** do the dump_infra command */
2130 do_dump_infra(SSL* ssl, struct worker* worker)
2132 struct infra_arg arg;
2133 arg.infra = worker->env.infra_cache;
2135 arg.now = *worker->env.now;
2137 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
2140 /** do the log_reopen command */
2142 do_log_reopen(SSL* ssl, struct worker* worker)
2144 struct config_file* cfg = worker->env.cfg;
2146 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
2149 /** do the set_option command */
2151 do_set_option(SSL* ssl, struct worker* worker, char* arg)
2154 if(!find_arg2(ssl, arg, &arg2))
2156 if(!config_set_option(worker->env.cfg, arg, arg2)) {
2157 (void)ssl_printf(ssl, "error setting option\n");
2163 /* routine to printout option values over SSL */
2164 void remote_get_opt_ssl(char* line, void* arg)
2166 SSL* ssl = (SSL*)arg;
2167 (void)ssl_printf(ssl, "%s\n", line);
2170 /** do the get_option command */
2172 do_get_option(SSL* ssl, struct worker* worker, char* arg)
2175 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
2177 (void)ssl_printf(ssl, "error unknown option\n");
2182 /** do the list_forwards command */
2184 do_list_forwards(SSL* ssl, struct worker* worker)
2186 /* since its a per-worker structure no locks needed */
2187 struct iter_forwards* fwds = worker->env.fwds;
2188 struct iter_forward_zone* z;
2189 struct trust_anchor* a;
2191 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
2192 if(!z->dp) continue; /* skip empty marker for stub */
2194 /* see if it is insecure */
2196 if(worker->env.anchors &&
2197 (a=anchor_find(worker->env.anchors, z->name,
2198 z->namelabs, z->namelen, z->dclass))) {
2199 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2201 lock_basic_unlock(&a->lock);
2204 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"),
2205 z->name, z->dclass, z->dp))
2210 /** do the list_stubs command */
2212 do_list_stubs(SSL* ssl, struct worker* worker)
2214 struct iter_hints_stub* z;
2215 struct trust_anchor* a;
2218 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
2220 /* see if it is insecure */
2222 if(worker->env.anchors &&
2223 (a=anchor_find(worker->env.anchors, z->node.name,
2224 z->node.labs, z->node.len, z->node.dclass))) {
2225 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2227 lock_basic_unlock(&a->lock);
2230 snprintf(str, sizeof(str), "stub %sprime%s",
2231 (z->noprime?"no":""), (insecure?" +i":""));
2232 if(!ssl_print_name_dp(ssl, str, z->node.name,
2233 z->node.dclass, z->dp))
2238 /** do the list_local_zones command */
2240 do_list_local_zones(SSL* ssl, struct worker* worker)
2242 struct local_zones* zones = worker->daemon->local_zones;
2243 struct local_zone* z;
2245 lock_rw_rdlock(&zones->lock);
2246 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2247 lock_rw_rdlock(&z->lock);
2248 dname_str(z->name, buf);
2249 if(!ssl_printf(ssl, "%s %s\n", buf,
2250 local_zone_type2str(z->type))) {
2251 /* failure to print */
2252 lock_rw_unlock(&z->lock);
2253 lock_rw_unlock(&zones->lock);
2256 lock_rw_unlock(&z->lock);
2258 lock_rw_unlock(&zones->lock);
2261 /** do the list_local_data command */
2263 do_list_local_data(SSL* ssl, struct worker* worker)
2265 struct local_zones* zones = worker->daemon->local_zones;
2266 struct local_zone* z;
2267 struct local_data* d;
2268 struct local_rrset* p;
2269 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
2270 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
2271 lock_rw_rdlock(&zones->lock);
2272 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2273 lock_rw_rdlock(&z->lock);
2274 RBTREE_FOR(d, struct local_data*, &z->data) {
2275 for(p = d->rrsets; p; p = p->next) {
2276 struct packed_rrset_data* d =
2277 (struct packed_rrset_data*)p->rrset->entry.data;
2279 for(i=0; i<d->count + d->rrsig_count; i++) {
2280 if(!packed_rr_to_string(p->rrset, i,
2282 if(!ssl_printf(ssl, "BADRR\n")) {
2283 lock_rw_unlock(&z->lock);
2284 lock_rw_unlock(&zones->lock);
2288 if(!ssl_printf(ssl, "%s\n", s)) {
2289 lock_rw_unlock(&z->lock);
2290 lock_rw_unlock(&zones->lock);
2296 lock_rw_unlock(&z->lock);
2298 lock_rw_unlock(&zones->lock);
2301 /** struct for user arg ratelimit list */
2302 struct ratelimit_list_arg {
2303 /** the infra cache */
2304 struct infra_cache* infra;
2305 /** the SSL to print to */
2307 /** all or only ratelimited */
2313 /** list items in the ratelimit table */
2315 rate_list(struct lruhash_entry* e, void* arg)
2317 struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg;
2318 struct rate_key* k = (struct rate_key*)e->key;
2319 struct rate_data* d = (struct rate_data*)e->data;
2321 int lim = infra_find_ratelimit(a->infra, k->name, k->namelen);
2322 int max = infra_rate_max(d, a->now);
2327 dname_str(k->name, buf);
2328 ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim);
2331 /** do the ratelimit_list command */
2333 do_ratelimit_list(SSL* ssl, struct worker* worker, char* arg)
2335 struct ratelimit_list_arg a;
2337 a.infra = worker->env.infra_cache;
2338 a.now = *worker->env.now;
2340 arg = skipwhite(arg);
2341 if(strcmp(arg, "+a") == 0)
2343 if(a.infra->domain_rates==NULL ||
2344 (a.all == 0 && infra_dp_ratelimit == 0))
2346 slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a);
2349 /** tell other processes to execute the command */
2351 distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd)
2356 /* skip i=0 which is me */
2357 for(i=1; i<rc->worker->daemon->num; i++) {
2358 worker_send_cmd(rc->worker->daemon->workers[i],
2360 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2361 (uint8_t*)cmd, strlen(cmd)+1, 0)) {
2362 ssl_printf(ssl, "error could not distribute cmd\n");
2368 /** check for name with end-of-string, space or tab after it */
2370 cmdcmp(char* p, const char* cmd, size_t len)
2372 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2375 /** execute a remote control command */
2377 execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd,
2378 struct worker* worker)
2380 char* p = skipwhite(cmd);
2381 /* compare command */
2382 if(cmdcmp(p, "stop", 4)) {
2385 } else if(cmdcmp(p, "reload", 6)) {
2388 } else if(cmdcmp(p, "stats_noreset", 13)) {
2389 do_stats(ssl, rc, 0);
2391 } else if(cmdcmp(p, "stats", 5)) {
2392 do_stats(ssl, rc, 1);
2394 } else if(cmdcmp(p, "status", 6)) {
2395 do_status(ssl, worker);
2397 } else if(cmdcmp(p, "dump_cache", 10)) {
2398 (void)dump_cache(ssl, worker);
2400 } else if(cmdcmp(p, "load_cache", 10)) {
2401 if(load_cache(ssl, worker)) send_ok(ssl);
2403 } else if(cmdcmp(p, "list_forwards", 13)) {
2404 do_list_forwards(ssl, worker);
2406 } else if(cmdcmp(p, "list_stubs", 10)) {
2407 do_list_stubs(ssl, worker);
2409 } else if(cmdcmp(p, "list_insecure", 13)) {
2410 do_insecure_list(ssl, worker);
2412 } else if(cmdcmp(p, "list_local_zones", 16)) {
2413 do_list_local_zones(ssl, worker);
2415 } else if(cmdcmp(p, "list_local_data", 15)) {
2416 do_list_local_data(ssl, worker);
2418 } else if(cmdcmp(p, "ratelimit_list", 14)) {
2419 do_ratelimit_list(ssl, worker, p+14);
2421 } else if(cmdcmp(p, "stub_add", 8)) {
2422 /* must always distribute this cmd */
2423 if(rc) distribute_cmd(rc, ssl, cmd);
2424 do_stub_add(ssl, worker, skipwhite(p+8));
2426 } else if(cmdcmp(p, "stub_remove", 11)) {
2427 /* must always distribute this cmd */
2428 if(rc) distribute_cmd(rc, ssl, cmd);
2429 do_stub_remove(ssl, worker, skipwhite(p+11));
2431 } else if(cmdcmp(p, "forward_add", 11)) {
2432 /* must always distribute this cmd */
2433 if(rc) distribute_cmd(rc, ssl, cmd);
2434 do_forward_add(ssl, worker, skipwhite(p+11));
2436 } else if(cmdcmp(p, "forward_remove", 14)) {
2437 /* must always distribute this cmd */
2438 if(rc) distribute_cmd(rc, ssl, cmd);
2439 do_forward_remove(ssl, worker, skipwhite(p+14));
2441 } else if(cmdcmp(p, "insecure_add", 12)) {
2442 /* must always distribute this cmd */
2443 if(rc) distribute_cmd(rc, ssl, cmd);
2444 do_insecure_add(ssl, worker, skipwhite(p+12));
2446 } else if(cmdcmp(p, "insecure_remove", 15)) {
2447 /* must always distribute this cmd */
2448 if(rc) distribute_cmd(rc, ssl, cmd);
2449 do_insecure_remove(ssl, worker, skipwhite(p+15));
2451 } else if(cmdcmp(p, "forward", 7)) {
2452 /* must always distribute this cmd */
2453 if(rc) distribute_cmd(rc, ssl, cmd);
2454 do_forward(ssl, worker, skipwhite(p+7));
2456 } else if(cmdcmp(p, "flush_stats", 11)) {
2457 /* must always distribute this cmd */
2458 if(rc) distribute_cmd(rc, ssl, cmd);
2459 do_flush_stats(ssl, worker);
2461 } else if(cmdcmp(p, "flush_requestlist", 17)) {
2462 /* must always distribute this cmd */
2463 if(rc) distribute_cmd(rc, ssl, cmd);
2464 do_flush_requestlist(ssl, worker);
2466 } else if(cmdcmp(p, "lookup", 6)) {
2467 do_lookup(ssl, worker, skipwhite(p+6));
2471 #ifdef THREADS_DISABLED
2472 /* other processes must execute the command as well */
2473 /* commands that should not be distributed, returned above. */
2474 if(rc) { /* only if this thread is the master (rc) thread */
2475 /* done before the code below, which may split the string */
2476 distribute_cmd(rc, ssl, cmd);
2479 if(cmdcmp(p, "verbosity", 9)) {
2480 do_verbosity(ssl, skipwhite(p+9));
2481 } else if(cmdcmp(p, "local_zone_remove", 17)) {
2482 do_zone_remove(ssl, worker, skipwhite(p+17));
2483 } else if(cmdcmp(p, "local_zone", 10)) {
2484 do_zone_add(ssl, worker, skipwhite(p+10));
2485 } else if(cmdcmp(p, "local_data_remove", 17)) {
2486 do_data_remove(ssl, worker, skipwhite(p+17));
2487 } else if(cmdcmp(p, "local_data", 10)) {
2488 do_data_add(ssl, worker, skipwhite(p+10));
2489 } else if(cmdcmp(p, "flush_zone", 10)) {
2490 do_flush_zone(ssl, worker, skipwhite(p+10));
2491 } else if(cmdcmp(p, "flush_type", 10)) {
2492 do_flush_type(ssl, worker, skipwhite(p+10));
2493 } else if(cmdcmp(p, "flush_infra", 11)) {
2494 do_flush_infra(ssl, worker, skipwhite(p+11));
2495 } else if(cmdcmp(p, "flush", 5)) {
2496 do_flush_name(ssl, worker, skipwhite(p+5));
2497 } else if(cmdcmp(p, "dump_requestlist", 16)) {
2498 do_dump_requestlist(ssl, worker);
2499 } else if(cmdcmp(p, "dump_infra", 10)) {
2500 do_dump_infra(ssl, worker);
2501 } else if(cmdcmp(p, "log_reopen", 10)) {
2502 do_log_reopen(ssl, worker);
2503 } else if(cmdcmp(p, "set_option", 10)) {
2504 do_set_option(ssl, worker, skipwhite(p+10));
2505 } else if(cmdcmp(p, "get_option", 10)) {
2506 do_get_option(ssl, worker, skipwhite(p+10));
2507 } else if(cmdcmp(p, "flush_bogus", 11)) {
2508 do_flush_bogus(ssl, worker);
2509 } else if(cmdcmp(p, "flush_negative", 14)) {
2510 do_flush_negative(ssl, worker);
2512 (void)ssl_printf(ssl, "error unknown command '%s'\n", p);
2517 daemon_remote_exec(struct worker* worker)
2519 /* read the cmd string */
2520 uint8_t* msg = NULL;
2522 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
2523 log_err("daemon_remote_exec: tube_read_msg failed");
2526 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
2527 execute_cmd(NULL, NULL, (char*)msg, worker);
2531 /** handle remote control request */
2533 handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl)
2540 /* makes it possible to set the socket blocking again. */
2541 /* basically removes it from winsock_event ... */
2542 WSAEventSelect(s->c->fd, NULL, 0);
2544 fd_set_block(s->c->fd);
2546 /* try to read magic UBCT[version]_space_ string */
2548 if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) {
2549 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN)
2551 log_crypto_err("could not SSL_read");
2555 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
2556 verbose(VERB_QUERY, "control connection has bad magic string");
2557 /* probably wrong tool connected, ignore it completely */
2561 /* read the command line */
2562 if(!ssl_read_line(ssl, buf, sizeof(buf))) {
2565 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
2566 if(strcmp(magic, pre) != 0) {
2567 verbose(VERB_QUERY, "control connection had bad "
2568 "version %s, cmd: %s", magic, buf);
2569 ssl_printf(ssl, "error version mismatch\n");
2572 verbose(VERB_DETAIL, "control cmd: %s", buf);
2574 /* figure out what to do */
2575 execute_cmd(rc, ssl, buf, rc->worker);
2578 int remote_control_callback(struct comm_point* c, void* arg, int err,
2579 struct comm_reply* ATTR_UNUSED(rep))
2581 struct rc_state* s = (struct rc_state*)arg;
2582 struct daemon_remote* rc = s->rc;
2584 if(err != NETEVENT_NOERROR) {
2585 if(err==NETEVENT_TIMEOUT)
2586 log_err("remote control timed out");
2590 /* (continue to) setup the SSL connection */
2592 r = SSL_do_handshake(s->ssl);
2594 int r2 = SSL_get_error(s->ssl, r);
2595 if(r2 == SSL_ERROR_WANT_READ) {
2596 if(s->shake_state == rc_hs_read) {
2597 /* try again later */
2600 s->shake_state = rc_hs_read;
2601 comm_point_listen_for_rw(c, 1, 0);
2603 } else if(r2 == SSL_ERROR_WANT_WRITE) {
2604 if(s->shake_state == rc_hs_write) {
2605 /* try again later */
2608 s->shake_state = rc_hs_write;
2609 comm_point_listen_for_rw(c, 0, 1);
2613 log_err("remote control connection closed prematurely");
2614 log_addr(1, "failed connection from",
2615 &s->c->repinfo.addr, s->c->repinfo.addrlen);
2616 log_crypto_err("remote control failed ssl");
2621 s->shake_state = rc_none;
2623 /* once handshake has completed, check authentication */
2624 if (!rc->use_cert) {
2625 verbose(VERB_ALGO, "unauthenticated remote control connection");
2626 } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
2627 X509* x = SSL_get_peer_certificate(s->ssl);
2629 verbose(VERB_DETAIL, "remote control connection "
2630 "provided no client certificate");
2634 verbose(VERB_ALGO, "remote control connection authenticated");
2637 verbose(VERB_DETAIL, "remote control connection failed to "
2638 "authenticate with client certificate");
2643 /* if OK start to actually handle the request */
2644 handle_req(rc, s, s->ssl);
2646 verbose(VERB_ALGO, "remote control operation completed");