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 "ldns/str2wire.h"
82 #include "ldns/parseutil.h"
83 #include "ldns/wire2str.h"
84 #include "ldns/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 512"
148 static unsigned char dh512_p[]={
149 0xC9,0xD7,0x05,0xDA,0x5F,0xAB,0x14,0xE8,0x11,0x56,0x77,0x85,
150 0xB1,0x24,0x2C,0x95,0x60,0xEA,0xE2,0x10,0x6F,0x0F,0x84,0xEC,
151 0xF4,0x45,0xE8,0x90,0x7A,0xA7,0x03,0xFF,0x5B,0x88,0x53,0xDE,
152 0xC4,0xDE,0xBC,0x42,0x78,0x71,0x23,0x7E,0x24,0xA5,0x5E,0x4E,
153 0xEF,0x6F,0xFF,0x5F,0xAF,0xBE,0x8A,0x77,0x62,0xB4,0x65,0x82,
156 static unsigned char dh512_g[]={
157 0x8D,0x3A,0x52,0xBC,0x8A,0x71,0x94,0x33,0x2F,0xE1,0xE8,0x4C,
158 0x73,0x47,0x03,0x4E,0x7D,0x40,0xE5,0x84,0xA0,0xB5,0x6D,0x10,
159 0x6F,0x90,0x43,0x05,0x1A,0xF9,0x0B,0x6A,0xD1,0x2A,0x9C,0x25,
160 0x0A,0xB9,0xD1,0x14,0xDC,0x35,0x1C,0x48,0x7C,0xC6,0x0C,0x6D,
161 0x32,0x1D,0xD3,0xC8,0x10,0xA8,0x82,0x14,0xA2,0x1C,0xF4,0x53,
166 if ((dh=DH_new()) == NULL) return(NULL);
167 dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
168 dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
169 if ((dh->p == NULL) || (dh->g == NULL))
170 { DH_free(dh); return(NULL); }
176 struct daemon_remote*
177 daemon_remote_create(struct config_file* cfg)
181 struct daemon_remote* rc = (struct daemon_remote*)calloc(1,
184 log_err("out of memory in daemon_remote_create");
189 if(!cfg->remote_control_enable) {
193 rc->ctx = SSL_CTX_new(SSLv23_server_method());
195 log_crypto_err("could not SSL_CTX_new");
199 /* no SSLv2, SSLv3 because has defects */
200 if(!(SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)){
201 log_crypto_err("could not set SSL_OP_NO_SSLv2");
202 daemon_remote_delete(rc);
205 if(!(SSL_CTX_set_options(rc->ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)){
206 log_crypto_err("could not set SSL_OP_NO_SSLv3");
207 daemon_remote_delete(rc);
211 if (cfg->remote_control_use_cert == 0) {
212 /* No certificates are requested */
213 if(!SSL_CTX_set_cipher_list(rc->ctx, "aNULL")) {
214 log_crypto_err("Failed to set aNULL cipher list");
218 /* Since we have no certificates and hence no source of
219 * DH params, let's generate and set them
221 if(!SSL_CTX_set_tmp_dh(rc->ctx,get_dh512())) {
222 log_crypto_err("Wanted to set DH param, but failed");
228 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
229 s_key = fname_after_chroot(cfg->server_key_file, cfg, 1);
230 if(!s_cert || !s_key) {
231 log_err("out of memory in remote control fname");
234 verbose(VERB_ALGO, "setup SSL certificates");
235 if (!SSL_CTX_use_certificate_file(rc->ctx,s_cert,SSL_FILETYPE_PEM)) {
236 log_err("Error for server-cert-file: %s", s_cert);
237 log_crypto_err("Error in SSL_CTX use_certificate_file");
240 if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) {
241 log_err("Error for server-key-file: %s", s_key);
242 log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
245 if(!SSL_CTX_check_private_key(rc->ctx)) {
246 log_err("Error for server-key-file: %s", s_key);
247 log_crypto_err("Error in SSL_CTX check_private_key");
250 if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) {
251 log_crypto_err("Error setting up SSL_CTX verify locations");
255 daemon_remote_delete(rc);
258 SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert));
259 SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL);
266 void daemon_remote_clear(struct daemon_remote* rc)
268 struct rc_state* p, *np;
270 /* but do not close the ports */
271 listen_list_delete(rc->accept_list);
272 rc->accept_list = NULL;
273 /* do close these sockets */
279 comm_point_delete(p->c);
283 rc->busy_list = NULL;
288 void daemon_remote_delete(struct daemon_remote* rc)
291 daemon_remote_clear(rc);
293 SSL_CTX_free(rc->ctx);
299 * Add and open a new control port
302 * @param list: list head
303 * @param noproto_is_err: if lack of protocol support is an error.
304 * @param cfg: config with username for chown of unix-sockets.
305 * @return false on failure.
308 add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err,
309 struct config_file* cfg)
311 struct addrinfo hints;
312 struct addrinfo* res;
313 struct listen_port* n;
317 snprintf(port, sizeof(port), "%d", nr);
318 port[sizeof(port)-1]=0;
319 memset(&hints, 0, sizeof(hints));
322 /* This looks like a local socket */
323 fd = create_local_accept_sock(ip, &noproto);
325 * Change socket ownership and permissions so users other
326 * than root can access it provided they are in the same
327 * group as the user we run as.
331 if (cfg->username && cfg->username[0])
332 chown(ip, cfg_uid, cfg_gid);
333 chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
339 hints.ai_socktype = SOCK_STREAM;
340 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
341 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
343 if(!noproto_is_err && r == EAI_NONAME) {
344 /* tried to lookup the address as name */
345 return 1; /* return success, but do nothing */
347 #endif /* USE_WINSOCK */
348 log_err("control interface %s:%s getaddrinfo: %s %s",
349 ip?ip:"default", port, gai_strerror(r),
351 r==EAI_SYSTEM?(char*)strerror(errno):""
360 fd = create_tcp_accept_sock(res, 1, &noproto, 0);
364 if(fd == -1 && noproto) {
366 return 1; /* return success, but do nothing */
367 log_err("cannot open control interface %s %d : "
368 "protocol not supported", ip, nr);
372 log_err("cannot open control interface %s %d", ip, nr);
377 n = (struct listen_port*)calloc(1, sizeof(*n));
384 log_err("out of memory");
393 struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
395 struct listen_port* l = NULL;
396 log_assert(cfg->remote_control_enable && cfg->control_port);
397 if(cfg->control_ifs) {
398 struct config_strlist* p;
399 for(p = cfg->control_ifs; p; p = p->next) {
400 if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) {
401 listening_ports_free(l);
408 !add_open("::1", cfg->control_port, &l, 0, cfg)) {
409 listening_ports_free(l);
413 !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) {
414 listening_ports_free(l);
421 /** open accept commpoint */
423 accept_open(struct daemon_remote* rc, int fd)
425 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
427 log_err("out of memory");
430 n->next = rc->accept_list;
433 n->com = comm_point_create_raw(rc->worker->base, fd, 0,
434 &remote_accept_callback, rc);
437 /* keep this port open, its fd is kept in the rc portlist */
438 n->com->do_not_close = 1;
442 int daemon_remote_open_accept(struct daemon_remote* rc,
443 struct listen_port* ports, struct worker* worker)
445 struct listen_port* p;
447 for(p = ports; p; p = p->next) {
448 if(!accept_open(rc, p->fd)) {
449 log_err("could not create accept comm point");
456 void daemon_remote_stop_accept(struct daemon_remote* rc)
458 struct listen_list* p;
459 for(p=rc->accept_list; p; p=p->next) {
460 comm_point_stop_listening(p->com);
464 void daemon_remote_start_accept(struct daemon_remote* rc)
466 struct listen_list* p;
467 for(p=rc->accept_list; p; p=p->next) {
468 comm_point_start_listening(p->com, -1, -1);
472 int remote_accept_callback(struct comm_point* c, void* arg, int err,
473 struct comm_reply* ATTR_UNUSED(rep))
475 struct daemon_remote* rc = (struct daemon_remote*)arg;
476 struct sockaddr_storage addr;
480 if(err != NETEVENT_NOERROR) {
481 log_err("error %d on remote_accept_callback", err);
484 /* perform the accept */
485 newfd = comm_point_perform_accept(c, &addr, &addrlen);
488 /* create new commpoint unless we are servicing already */
489 if(rc->active >= rc->max_active) {
490 log_warn("drop incoming remote control: too many connections");
500 /* setup commpoint to service the remote control command */
501 n = (struct rc_state*)calloc(1, sizeof(*n));
503 log_err("out of memory");
506 /* start in reading state */
507 n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
508 &remote_control_callback, n);
510 log_err("out of memory");
514 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
515 n->c->do_not_close = 0;
516 comm_point_stop_listening(n->c);
517 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
518 memcpy(&n->c->repinfo.addr, &addr, addrlen);
519 n->c->repinfo.addrlen = addrlen;
520 n->shake_state = rc_hs_read;
521 n->ssl = SSL_new(rc->ctx);
523 log_crypto_err("could not SSL_new");
524 comm_point_delete(n->c);
528 SSL_set_accept_state(n->ssl);
529 (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY);
530 if(!SSL_set_fd(n->ssl, newfd)) {
531 log_crypto_err("could not SSL_set_fd");
533 comm_point_delete(n->c);
539 n->next = rc->busy_list;
543 /* perform the first nonblocking read already, for windows,
544 * so it can return wouldblock. could be faster too. */
545 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
549 /** delete from list */
551 state_list_remove_elem(struct rc_state** list, struct comm_point* c)
554 if( (*list)->c == c) {
555 *list = (*list)->next;
558 list = &(*list)->next;
562 /** decrease active count and remove commpoint from busy list */
564 clean_point(struct daemon_remote* rc, struct rc_state* s)
566 state_list_remove_elem(&rc->busy_list, s->c);
569 SSL_shutdown(s->ssl);
572 comm_point_delete(s->c);
577 ssl_print_text(SSL* ssl, const char* text)
583 if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) {
584 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
585 verbose(VERB_QUERY, "warning, in SSL_write, peer "
586 "closed connection");
589 log_crypto_err("could not SSL_write");
595 /** print text over the ssl connection */
597 ssl_print_vmsg(SSL* ssl, const char* format, va_list args)
600 vsnprintf(msg, sizeof(msg), format, args);
601 return ssl_print_text(ssl, msg);
604 /** printf style printing to the ssl connection */
605 int ssl_printf(SSL* ssl, const char* format, ...)
609 va_start(args, format);
610 ret = ssl_print_vmsg(ssl, format, args);
616 ssl_read_line(SSL* ssl, char* buf, size_t max)
624 if((r=SSL_read(ssl, buf+len, 1)) <= 0) {
625 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
629 log_crypto_err("could not SSL_read");
632 if(buf[len] == '\n') {
633 /* return string without \n */
640 log_err("control line too long (%d): %s", (int)max, buf);
644 /** skip whitespace, return new pointer into string */
648 /* EOS \0 is not a space */
649 while( isspace((unsigned char)*str) )
654 /** send the OK to the control client */
655 static void send_ok(SSL* ssl)
657 (void)ssl_printf(ssl, "ok\n");
660 /** do the stop command */
662 do_stop(SSL* ssl, struct daemon_remote* rc)
664 rc->worker->need_to_exit = 1;
665 comm_base_exit(rc->worker->base);
669 /** do the reload command */
671 do_reload(SSL* ssl, struct daemon_remote* rc)
673 rc->worker->need_to_exit = 0;
674 comm_base_exit(rc->worker->base);
678 /** do the verbosity command */
680 do_verbosity(SSL* ssl, char* str)
683 if(val == 0 && strcmp(str, "0") != 0) {
684 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
691 /** print stats from statinfo */
693 print_stats(SSL* ssl, const char* nm, struct stats_info* s)
696 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm,
697 (unsigned long)s->svr.num_queries)) return 0;
698 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm,
699 (unsigned long)(s->svr.num_queries
700 - s->svr.num_queries_missed_cache))) return 0;
701 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm,
702 (unsigned long)s->svr.num_queries_missed_cache)) return 0;
703 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm,
704 (unsigned long)s->svr.num_queries_prefetch)) return 0;
705 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm,
706 (unsigned long)s->mesh_replies_sent)) return 0;
707 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
708 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
709 (double)s->svr.sum_query_list_size/
710 (s->svr.num_queries_missed_cache+
711 s->svr.num_queries_prefetch) : 0.0)) return 0;
712 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm,
713 (unsigned long)s->svr.max_query_list_size)) return 0;
714 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm,
715 (unsigned long)s->mesh_jostled)) return 0;
716 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm,
717 (unsigned long)s->mesh_dropped)) return 0;
718 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm,
719 (unsigned long)s->mesh_num_states)) return 0;
720 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm,
721 (unsigned long)s->mesh_num_reply_states)) return 0;
722 timeval_divide(&avg, &s->mesh_replies_sum_wait, s->mesh_replies_sent);
723 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
724 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
725 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
726 s->mesh_time_median)) return 0;
730 /** print stats for one thread */
732 print_thread_stats(SSL* ssl, int i, struct stats_info* s)
735 snprintf(nm, sizeof(nm), "thread%d", i);
737 return print_stats(ssl, nm, s);
740 /** print long number */
742 print_longnum(SSL* ssl, const char* desc, size_t x)
744 if(x > 1024*1024*1024) {
746 size_t front = x / (size_t)1000000;
747 size_t back = x % (size_t)1000000;
748 return ssl_printf(ssl, "%s%u%6.6u\n", desc,
749 (unsigned)front, (unsigned)back);
751 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x);
755 /** print mem stats */
757 print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon)
760 size_t msg, rrset, val, iter;
762 extern void* unbound_start_brk;
764 if(!print_longnum(ssl, "mem.total.sbrk"SQ,
765 (size_t)((char*)cur - (char*)unbound_start_brk))) return 0;
766 #endif /* HAVE_SBRK */
767 msg = slabhash_get_mem(daemon->env->msg_cache);
768 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
771 m = modstack_find(&worker->env.mesh->mods, "validator");
773 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
774 mods.mod[m]->get_mem));
775 val = (*worker->env.mesh->mods.mod[m]->get_mem)
778 m = modstack_find(&worker->env.mesh->mods, "iterator");
780 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
781 mods.mod[m]->get_mem));
782 iter = (*worker->env.mesh->mods.mod[m]->get_mem)
786 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
788 if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
790 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
792 if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
797 /** print uptime stats */
799 print_uptime(SSL* ssl, struct worker* worker, int reset)
801 struct timeval now = *worker->env.now_tv;
802 struct timeval up, dt;
803 timeval_subtract(&up, &now, &worker->daemon->time_boot);
804 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
806 worker->daemon->time_last_stat = now;
807 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
808 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
809 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
810 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
811 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
812 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
816 /** print extended histogram */
818 print_hist(SSL* ssl, struct stats_info* s)
820 struct timehist* hist;
822 hist = timehist_setup();
824 log_err("out of memory");
827 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
828 for(i=0; i<hist->num; i++) {
830 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
831 (int)hist->buckets[i].lower.tv_sec,
832 (int)hist->buckets[i].lower.tv_usec,
833 (int)hist->buckets[i].upper.tv_sec,
834 (int)hist->buckets[i].upper.tv_usec,
835 (unsigned long)hist->buckets[i].count)) {
836 timehist_delete(hist);
840 timehist_delete(hist);
844 /** print extended stats */
846 print_ext(SSL* ssl, struct stats_info* s)
850 const sldns_rr_descriptor* desc;
851 const sldns_lookup_table* lt;
853 for(i=0; i<STATS_QTYPE_NUM; i++) {
854 if(inhibit_zero && s->svr.qtype[i] == 0)
856 desc = sldns_rr_descript((uint16_t)i);
857 if(desc && desc->_name) {
858 snprintf(nm, sizeof(nm), "%s", desc->_name);
859 } else if (i == LDNS_RR_TYPE_IXFR) {
860 snprintf(nm, sizeof(nm), "IXFR");
861 } else if (i == LDNS_RR_TYPE_AXFR) {
862 snprintf(nm, sizeof(nm), "AXFR");
863 } else if (i == LDNS_RR_TYPE_MAILA) {
864 snprintf(nm, sizeof(nm), "MAILA");
865 } else if (i == LDNS_RR_TYPE_MAILB) {
866 snprintf(nm, sizeof(nm), "MAILB");
867 } else if (i == LDNS_RR_TYPE_ANY) {
868 snprintf(nm, sizeof(nm), "ANY");
870 snprintf(nm, sizeof(nm), "TYPE%d", i);
872 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n",
873 nm, (unsigned long)s->svr.qtype[i])) return 0;
875 if(!inhibit_zero || s->svr.qtype_big) {
876 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n",
877 (unsigned long)s->svr.qtype_big)) return 0;
880 for(i=0; i<STATS_QCLASS_NUM; i++) {
881 if(inhibit_zero && s->svr.qclass[i] == 0)
883 lt = sldns_lookup_by_id(sldns_rr_classes, i);
885 snprintf(nm, sizeof(nm), "%s", lt->name);
887 snprintf(nm, sizeof(nm), "CLASS%d", i);
889 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n",
890 nm, (unsigned long)s->svr.qclass[i])) return 0;
892 if(!inhibit_zero || s->svr.qclass_big) {
893 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n",
894 (unsigned long)s->svr.qclass_big)) return 0;
897 for(i=0; i<STATS_OPCODE_NUM; i++) {
898 if(inhibit_zero && s->svr.qopcode[i] == 0)
900 lt = sldns_lookup_by_id(sldns_opcodes, i);
902 snprintf(nm, sizeof(nm), "%s", lt->name);
904 snprintf(nm, sizeof(nm), "OPCODE%d", i);
906 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n",
907 nm, (unsigned long)s->svr.qopcode[i])) return 0;
910 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n",
911 (unsigned long)s->svr.qtcp)) return 0;
912 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n",
913 (unsigned long)s->svr.qtcp_outgoing)) return 0;
914 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n",
915 (unsigned long)s->svr.qipv6)) return 0;
917 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n",
918 (unsigned long)s->svr.qbit_QR)) return 0;
919 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n",
920 (unsigned long)s->svr.qbit_AA)) return 0;
921 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n",
922 (unsigned long)s->svr.qbit_TC)) return 0;
923 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n",
924 (unsigned long)s->svr.qbit_RD)) return 0;
925 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n",
926 (unsigned long)s->svr.qbit_RA)) return 0;
927 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n",
928 (unsigned long)s->svr.qbit_Z)) return 0;
929 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n",
930 (unsigned long)s->svr.qbit_AD)) return 0;
931 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n",
932 (unsigned long)s->svr.qbit_CD)) return 0;
933 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n",
934 (unsigned long)s->svr.qEDNS)) return 0;
935 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n",
936 (unsigned long)s->svr.qEDNS_DO)) return 0;
939 for(i=0; i<STATS_RCODE_NUM; i++) {
940 /* Always include RCODEs 0-5 */
941 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
943 lt = sldns_lookup_by_id(sldns_rcodes, i);
945 snprintf(nm, sizeof(nm), "%s", lt->name);
947 snprintf(nm, sizeof(nm), "RCODE%d", i);
949 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n",
950 nm, (unsigned long)s->svr.ans_rcode[i])) return 0;
952 if(!inhibit_zero || s->svr.ans_rcode_nodata) {
953 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n",
954 (unsigned long)s->svr.ans_rcode_nodata)) return 0;
957 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n",
958 (unsigned long)s->svr.ans_secure)) return 0;
959 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n",
960 (unsigned long)s->svr.ans_bogus)) return 0;
961 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n",
962 (unsigned long)s->svr.rrset_bogus)) return 0;
963 /* threat detection */
964 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n",
965 (unsigned long)s->svr.unwanted_queries)) return 0;
966 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n",
967 (unsigned long)s->svr.unwanted_replies)) return 0;
969 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
970 (unsigned)s->svr.msg_cache_count)) return 0;
971 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
972 (unsigned)s->svr.rrset_cache_count)) return 0;
973 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
974 (unsigned)s->svr.infra_cache_count)) return 0;
975 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
976 (unsigned)s->svr.key_cache_count)) return 0;
980 /** do the stats command */
982 do_stats(SSL* ssl, struct daemon_remote* rc, int reset)
984 struct daemon* daemon = rc->worker->daemon;
985 struct stats_info total;
988 log_assert(daemon->num > 0);
989 /* gather all thread statistics in one place */
990 for(i=0; i<daemon->num; i++) {
991 server_stats_obtain(rc->worker, daemon->workers[i], &s, reset);
992 if(!print_thread_stats(ssl, i, &s))
996 else server_stats_add(&total, &s);
998 /* print the thread statistics */
999 total.mesh_time_median /= (double)daemon->num;
1000 if(!print_stats(ssl, "total", &total))
1002 if(!print_uptime(ssl, rc->worker, reset))
1004 if(daemon->cfg->stat_extended) {
1005 if(!print_mem(ssl, rc->worker, daemon))
1007 if(!print_hist(ssl, &total))
1009 if(!print_ext(ssl, &total))
1014 /** parse commandline argument domain name */
1016 parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs)
1018 uint8_t nm[LDNS_MAX_DOMAINLEN+1];
1019 size_t nmlen = sizeof(nm);
1024 status = sldns_str2wire_dname_buf(str, nm, &nmlen);
1026 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
1027 LDNS_WIREPARSE_OFFSET(status),
1028 sldns_get_errorstr_parse(status));
1031 *res = memdup(nm, nmlen);
1033 ssl_printf(ssl, "error out of memory\n");
1036 *labs = dname_count_size_labels(*res, len);
1040 /** find second argument, modifies string */
1042 find_arg2(SSL* ssl, char* arg, char** arg2)
1044 char* as = strchr(arg, ' ');
1045 char* at = strchr(arg, '\t');
1050 *arg2 = skipwhite(as+1);
1053 *arg2 = skipwhite(as+1);
1056 *arg2 = skipwhite(at+1);
1058 ssl_printf(ssl, "error could not find next argument "
1065 /** Add a new zone */
1067 do_zone_add(SSL* ssl, struct worker* worker, char* arg)
1073 enum localzone_type t;
1074 struct local_zone* z;
1075 if(!find_arg2(ssl, arg, &arg2))
1077 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1079 if(!local_zone_str2type(arg2, &t)) {
1080 ssl_printf(ssl, "error not a zone type. %s\n", arg2);
1084 lock_rw_wrlock(&worker->daemon->local_zones->lock);
1085 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1086 nmlabs, LDNS_RR_CLASS_IN))) {
1087 /* already present in tree */
1088 lock_rw_wrlock(&z->lock);
1089 z->type = t; /* update type anyway */
1090 lock_rw_unlock(&z->lock);
1092 lock_rw_unlock(&worker->daemon->local_zones->lock);
1096 if(!local_zones_add_zone(worker->daemon->local_zones, nm, nmlen,
1097 nmlabs, LDNS_RR_CLASS_IN, t)) {
1098 lock_rw_unlock(&worker->daemon->local_zones->lock);
1099 ssl_printf(ssl, "error out of memory\n");
1102 lock_rw_unlock(&worker->daemon->local_zones->lock);
1106 /** Remove a zone */
1108 do_zone_remove(SSL* ssl, struct worker* worker, char* arg)
1113 struct local_zone* z;
1114 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1116 lock_rw_wrlock(&worker->daemon->local_zones->lock);
1117 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1118 nmlabs, LDNS_RR_CLASS_IN))) {
1119 /* present in tree */
1120 local_zones_del_zone(worker->daemon->local_zones, z);
1122 lock_rw_unlock(&worker->daemon->local_zones->lock);
1127 /** Add new RR data */
1129 do_data_add(SSL* ssl, struct worker* worker, char* arg)
1131 if(!local_zones_add_RR(worker->daemon->local_zones, arg)) {
1132 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1138 /** Remove RR data */
1140 do_data_remove(SSL* ssl, struct worker* worker, char* arg)
1145 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1147 local_zones_del_data(worker->daemon->local_zones, nm,
1148 nmlen, nmlabs, LDNS_RR_CLASS_IN);
1153 /** cache lookup of nameservers */
1155 do_lookup(SSL* ssl, struct worker* worker, char* arg)
1160 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1162 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1166 /** flush something from rrset and msg caches */
1168 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1169 uint16_t t, uint16_t c)
1172 struct query_info k;
1173 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1174 if(t == LDNS_RR_TYPE_SOA)
1175 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1176 PACKED_RRSET_SOA_NEG);
1178 k.qname_len = nmlen;
1181 h = query_info_hash(&k, 0);
1182 slabhash_remove(worker->env.msg_cache, h, &k);
1183 if(t == LDNS_RR_TYPE_AAAA) {
1184 /* for AAAA also flush dns64 bit_cd packet */
1185 h = query_info_hash(&k, BIT_CD);
1186 slabhash_remove(worker->env.msg_cache, h, &k);
1192 do_flush_type(SSL* ssl, struct worker* worker, char* arg)
1199 if(!find_arg2(ssl, arg, &arg2))
1201 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1203 t = sldns_get_rr_type_by_name(arg2);
1204 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1210 /** flush statistics */
1212 do_flush_stats(SSL* ssl, struct worker* worker)
1214 worker_stats_clear(worker);
1219 * Local info for deletion functions
1223 struct worker* worker;
1224 /** name to delete */
1232 /** time to invalidate to */
1234 /** number of rrsets removed */
1236 /** number of msgs removed */
1238 /** number of key entries removed */
1240 /** length of addr */
1242 /** socket address for host deletion */
1243 struct sockaddr_storage addr;
1246 /** callback to delete hosts in infra cache */
1248 infra_del_host(struct lruhash_entry* e, void* arg)
1250 /* entry is locked */
1251 struct del_info* inf = (struct del_info*)arg;
1252 struct infra_key* k = (struct infra_key*)e->key;
1253 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1254 struct infra_data* d = (struct infra_data*)e->data;
1257 d->timeout_AAAA = 0;
1258 d->timeout_other = 0;
1260 if(d->ttl >= inf->now) {
1261 d->ttl = inf->expired;
1267 /** flush infra cache */
1269 do_flush_infra(SSL* ssl, struct worker* worker, char* arg)
1271 struct sockaddr_storage addr;
1273 struct del_info inf;
1274 if(strcmp(arg, "all") == 0) {
1275 slabhash_clear(worker->env.infra_cache->hosts);
1279 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1280 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1283 /* delete all entries from cache */
1284 /* what we do is to set them all expired */
1285 inf.worker = worker;
1289 inf.now = *worker->env.now;
1290 inf.expired = *worker->env.now;
1291 inf.expired -= 3; /* handle 3 seconds skew between threads */
1296 memmove(&inf.addr, &addr, len);
1297 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1302 /** flush requestlist */
1304 do_flush_requestlist(SSL* ssl, struct worker* worker)
1306 mesh_delete_all(worker->env.mesh);
1310 /** callback to delete rrsets in a zone */
1312 zone_del_rrset(struct lruhash_entry* e, void* arg)
1314 /* entry is locked */
1315 struct del_info* inf = (struct del_info*)arg;
1316 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1317 if(dname_subdomain_c(k->rk.dname, inf->name)) {
1318 struct packed_rrset_data* d =
1319 (struct packed_rrset_data*)e->data;
1320 if(d->ttl >= inf->now) {
1321 d->ttl = inf->expired;
1327 /** callback to delete messages in a zone */
1329 zone_del_msg(struct lruhash_entry* e, void* arg)
1331 /* entry is locked */
1332 struct del_info* inf = (struct del_info*)arg;
1333 struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1334 if(dname_subdomain_c(k->key.qname, inf->name)) {
1335 struct reply_info* d = (struct reply_info*)e->data;
1336 if(d->ttl >= inf->now) {
1337 d->ttl = inf->expired;
1343 /** callback to delete keys in zone */
1345 zone_del_kcache(struct lruhash_entry* e, void* arg)
1347 /* entry is locked */
1348 struct del_info* inf = (struct del_info*)arg;
1349 struct key_entry_key* k = (struct key_entry_key*)e->key;
1350 if(dname_subdomain_c(k->name, inf->name)) {
1351 struct key_entry_data* d = (struct key_entry_data*)e->data;
1352 if(d->ttl >= inf->now) {
1353 d->ttl = inf->expired;
1359 /** remove all rrsets and keys from zone from cache */
1361 do_flush_zone(SSL* ssl, struct worker* worker, char* arg)
1366 struct del_info inf;
1367 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1369 /* delete all RRs and key entries from zone */
1370 /* what we do is to set them all expired */
1371 inf.worker = worker;
1375 inf.now = *worker->env.now;
1376 inf.expired = *worker->env.now;
1377 inf.expired -= 3; /* handle 3 seconds skew between threads */
1381 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1382 &zone_del_rrset, &inf);
1384 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1386 /* and validator cache */
1387 if(worker->env.key_cache) {
1388 slabhash_traverse(worker->env.key_cache->slab, 1,
1389 &zone_del_kcache, &inf);
1394 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1395 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1396 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1399 /** callback to delete bogus rrsets */
1401 bogus_del_rrset(struct lruhash_entry* e, void* arg)
1403 /* entry is locked */
1404 struct del_info* inf = (struct del_info*)arg;
1405 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1406 if(d->security == sec_status_bogus) {
1407 d->ttl = inf->expired;
1412 /** callback to delete bogus messages */
1414 bogus_del_msg(struct lruhash_entry* e, void* arg)
1416 /* entry is locked */
1417 struct del_info* inf = (struct del_info*)arg;
1418 struct reply_info* d = (struct reply_info*)e->data;
1419 if(d->security == sec_status_bogus) {
1420 d->ttl = inf->expired;
1425 /** callback to delete bogus keys */
1427 bogus_del_kcache(struct lruhash_entry* e, void* arg)
1429 /* entry is locked */
1430 struct del_info* inf = (struct del_info*)arg;
1431 struct key_entry_data* d = (struct key_entry_data*)e->data;
1433 d->ttl = inf->expired;
1438 /** remove all bogus rrsets, msgs and keys from cache */
1440 do_flush_bogus(SSL* ssl, struct worker* worker)
1442 struct del_info inf;
1443 /* what we do is to set them all expired */
1444 inf.worker = worker;
1445 inf.now = *worker->env.now;
1446 inf.expired = *worker->env.now;
1447 inf.expired -= 3; /* handle 3 seconds skew between threads */
1451 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1452 &bogus_del_rrset, &inf);
1454 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1456 /* and validator cache */
1457 if(worker->env.key_cache) {
1458 slabhash_traverse(worker->env.key_cache->slab, 1,
1459 &bogus_del_kcache, &inf);
1462 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1463 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1464 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1467 /** callback to delete negative and servfail rrsets */
1469 negative_del_rrset(struct lruhash_entry* e, void* arg)
1471 /* entry is locked */
1472 struct del_info* inf = (struct del_info*)arg;
1473 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1474 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1475 /* delete the parentside negative cache rrsets,
1476 * these are namerserver rrsets that failed lookup, rdata empty */
1477 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 &&
1478 d->rrsig_count == 0 && d->rr_len[0] == 0) {
1479 d->ttl = inf->expired;
1484 /** callback to delete negative and servfail messages */
1486 negative_del_msg(struct lruhash_entry* e, void* arg)
1488 /* entry is locked */
1489 struct del_info* inf = (struct del_info*)arg;
1490 struct reply_info* d = (struct reply_info*)e->data;
1491 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1492 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1493 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) {
1494 d->ttl = inf->expired;
1499 /** callback to delete negative key entries */
1501 negative_del_kcache(struct lruhash_entry* e, void* arg)
1503 /* entry is locked */
1504 struct del_info* inf = (struct del_info*)arg;
1505 struct key_entry_data* d = (struct key_entry_data*)e->data;
1506 /* could be bad because of lookup failure on the DS, DNSKEY, which
1507 * was nxdomain or servfail, and thus a result of negative lookups */
1509 d->ttl = inf->expired;
1514 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1516 do_flush_negative(SSL* ssl, struct worker* worker)
1518 struct del_info inf;
1519 /* what we do is to set them all expired */
1520 inf.worker = worker;
1521 inf.now = *worker->env.now;
1522 inf.expired = *worker->env.now;
1523 inf.expired -= 3; /* handle 3 seconds skew between threads */
1527 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1528 &negative_del_rrset, &inf);
1530 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf);
1532 /* and validator cache */
1533 if(worker->env.key_cache) {
1534 slabhash_traverse(worker->env.key_cache->slab, 1,
1535 &negative_del_kcache, &inf);
1538 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1539 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1540 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1543 /** remove name rrset from cache */
1545 do_flush_name(SSL* ssl, struct worker* w, char* arg)
1550 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1552 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1553 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1554 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1555 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1556 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1557 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1558 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1559 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1560 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1561 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1567 /** printout a delegation point info */
1569 ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1573 struct delegpt_ns* ns;
1574 struct delegpt_addr* a;
1576 if(str) { /* print header for forward, stub */
1577 char* c = sldns_wire2str_class(dclass);
1579 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) {
1585 for(ns = dp->nslist; ns; ns = ns->next) {
1586 dname_str(ns->name, buf);
1587 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1591 for(a = dp->target_list; a; a = a->next_target) {
1592 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1593 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1597 return ssl_printf(ssl, "\n");
1601 /** print root forwards */
1603 print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root)
1606 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1608 return ssl_printf(ssl, "off (using root hints)\n");
1609 /* if dp is returned it must be the root */
1610 log_assert(query_dname_compare(dp->name, root)==0);
1611 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1614 /** parse args into delegpt */
1615 static struct delegpt*
1616 parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names)
1618 /* parse args and add in */
1621 struct delegpt* dp = delegpt_create_mlc(nm);
1622 struct sockaddr_storage addr;
1625 (void)ssl_printf(ssl, "error out of memory\n");
1630 p = strchr(p, ' '); /* find next spot, if any */
1632 *p++ = 0; /* end this spot */
1633 p = skipwhite(p); /* position at next spot */
1636 if(!extstrtoaddr(todo, &addr, &addrlen)) {
1641 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
1642 (void)ssl_printf(ssl, "error cannot "
1643 "parse IP address or name "
1645 delegpt_free_mlc(dp);
1648 if(!delegpt_add_ns_mlc(dp, n, 0)) {
1649 (void)ssl_printf(ssl, "error out of memory\n");
1651 delegpt_free_mlc(dp);
1657 (void)ssl_printf(ssl, "error cannot parse"
1658 " IP address '%s'\n", todo);
1659 delegpt_free_mlc(dp);
1664 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) {
1665 (void)ssl_printf(ssl, "error out of memory\n");
1666 delegpt_free_mlc(dp);
1674 /** do the status command */
1676 do_forward(SSL* ssl, struct worker* worker, char* args)
1678 struct iter_forwards* fwd = worker->env.fwds;
1679 uint8_t* root = (uint8_t*)"\000";
1681 (void)ssl_printf(ssl, "error: structure not allocated\n");
1684 if(args == NULL || args[0] == 0) {
1685 (void)print_root_fwds(ssl, fwd, root);
1688 /* set root forwards for this thread. since we are in remote control
1689 * the actual mesh is not running, so we can freely edit it. */
1690 /* delete all the existing queries first */
1691 mesh_delete_all(worker->env.mesh);
1692 if(strcmp(args, "off") == 0) {
1693 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
1696 if(!(dp = parse_delegpt(ssl, args, root, 0)))
1698 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1699 (void)ssl_printf(ssl, "error out of memory\n");
1707 parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp,
1708 int* insecure, int* prime)
1714 /* parse all -x args */
1715 while(args[0] == '+') {
1716 if(!find_arg2(ssl, args, &rest))
1718 while(*(++args) != 0) {
1719 if(*args == 'i' && insecure)
1721 else if(*args == 'p' && prime)
1724 (void)ssl_printf(ssl, "error: unknown option %s\n", args);
1732 if(!find_arg2(ssl, args, &rest))
1736 } else zonename = args;
1737 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
1742 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
1750 /** do the forward_add command */
1752 do_forward_add(SSL* ssl, struct worker* worker, char* args)
1754 struct iter_forwards* fwd = worker->env.fwds;
1757 struct delegpt* dp = NULL;
1758 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
1760 if(insecure && worker->env.anchors) {
1761 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1763 (void)ssl_printf(ssl, "error out of memory\n");
1764 delegpt_free_mlc(dp);
1769 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1770 (void)ssl_printf(ssl, "error out of memory\n");
1778 /** do the forward_remove command */
1780 do_forward_remove(SSL* ssl, struct worker* worker, char* args)
1782 struct iter_forwards* fwd = worker->env.fwds;
1785 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1787 if(insecure && worker->env.anchors)
1788 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1790 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
1795 /** do the stub_add command */
1797 do_stub_add(SSL* ssl, struct worker* worker, char* args)
1799 struct iter_forwards* fwd = worker->env.fwds;
1800 int insecure = 0, prime = 0;
1802 struct delegpt* dp = NULL;
1803 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
1805 if(insecure && worker->env.anchors) {
1806 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1808 (void)ssl_printf(ssl, "error out of memory\n");
1809 delegpt_free_mlc(dp);
1814 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
1815 if(insecure && worker->env.anchors)
1816 anchors_delete_insecure(worker->env.anchors,
1817 LDNS_RR_CLASS_IN, nm);
1818 (void)ssl_printf(ssl, "error out of memory\n");
1819 delegpt_free_mlc(dp);
1823 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
1824 (void)ssl_printf(ssl, "error out of memory\n");
1825 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1826 if(insecure && worker->env.anchors)
1827 anchors_delete_insecure(worker->env.anchors,
1828 LDNS_RR_CLASS_IN, nm);
1836 /** do the stub_remove command */
1838 do_stub_remove(SSL* ssl, struct worker* worker, char* args)
1840 struct iter_forwards* fwd = worker->env.fwds;
1843 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1845 if(insecure && worker->env.anchors)
1846 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1848 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1849 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
1854 /** do the insecure_add command */
1856 do_insecure_add(SSL* ssl, struct worker* worker, char* arg)
1861 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1863 if(worker->env.anchors) {
1864 if(!anchors_add_insecure(worker->env.anchors,
1865 LDNS_RR_CLASS_IN, nm)) {
1866 (void)ssl_printf(ssl, "error out of memory\n");
1875 /** do the insecure_remove command */
1877 do_insecure_remove(SSL* ssl, struct worker* worker, char* arg)
1882 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1884 if(worker->env.anchors)
1885 anchors_delete_insecure(worker->env.anchors,
1886 LDNS_RR_CLASS_IN, nm);
1891 /** do the status command */
1893 do_status(SSL* ssl, struct worker* worker)
1897 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
1899 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
1901 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
1903 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
1905 for(i=0; i<worker->daemon->mods.num; i++) {
1906 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
1909 if(!ssl_printf(ssl, " ]\n"))
1911 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
1912 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
1914 if(!ssl_printf(ssl, "options:%s%s\n" ,
1915 (worker->daemon->reuseport?" reuseport":""),
1916 (worker->daemon->rc->accept_list?" control(ssl)":"")))
1918 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
1923 /** get age for the mesh state */
1925 get_mesh_age(struct mesh_state* m, char* buf, size_t len,
1926 struct module_env* env)
1930 struct mesh_reply* r = m->reply_list;
1931 /* last reply is the oldest */
1934 timeval_subtract(&d, env->now_tv, &r->start_time);
1935 snprintf(buf, len, ARG_LL "d.%6.6d",
1936 (long long)d.tv_sec, (int)d.tv_usec);
1938 snprintf(buf, len, "-");
1942 /** get status of a mesh state */
1944 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
1945 char* buf, size_t len)
1947 enum module_ext_state s = m->s.ext_state[m->s.curmod];
1948 const char *modname = mesh->mods.mod[m->s.curmod]->name;
1950 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
1951 m->s.minfo[m->s.curmod]) {
1952 /* break into iterator to find out who its waiting for */
1953 struct iter_qstate* qstate = (struct iter_qstate*)
1954 m->s.minfo[m->s.curmod];
1955 struct outbound_list* ol = &qstate->outlist;
1956 struct outbound_entry* e;
1957 snprintf(buf, len, "%s wait for", modname);
1960 if(ol->first == NULL)
1961 snprintf(buf, len, " (empty_list)");
1962 for(e = ol->first; e; e = e->next) {
1963 snprintf(buf, len, " ");
1966 addr_to_str(&e->qsent->addr, e->qsent->addrlen,
1971 } else if(s == module_wait_subquery) {
1972 /* look in subs from mesh state to see what */
1974 struct mesh_state_ref* sub;
1975 snprintf(buf, len, "%s wants", modname);
1978 if(m->sub_set.count == 0)
1979 snprintf(buf, len, " (empty_list)");
1980 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
1981 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
1982 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
1983 dname_str(sub->s->s.qinfo.qname, nm);
1984 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
1985 (c?c:"CLASS??"), nm);
1992 snprintf(buf, len, "%s is %s", modname, strextstate(s));
1996 /** do the dump_requestlist command */
1998 do_dump_requestlist(SSL* ssl, struct worker* worker)
2000 struct mesh_area* mesh;
2001 struct mesh_state* m;
2005 char statbuf[10240];
2006 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
2008 if(!ssl_printf(ssl, "# type cl name seconds module status\n"))
2010 /* show worker mesh contents */
2011 mesh = worker->env.mesh;
2013 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
2014 char* t = sldns_wire2str_type(m->s.qinfo.qtype);
2015 char* c = sldns_wire2str_class(m->s.qinfo.qclass);
2016 dname_str(m->s.qinfo.qname, buf);
2017 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
2018 get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
2019 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
2020 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
2032 /** structure for argument data for dump infra host */
2034 /** the infra cache */
2035 struct infra_cache* infra;
2036 /** the SSL connection */
2040 /** ssl failure? stop writing and skip the rest. If the tcp
2041 * connection is broken, and writes fail, we then stop writing. */
2045 /** callback for every host element in the infra cache */
2047 dump_infra_host(struct lruhash_entry* e, void* arg)
2049 struct infra_arg* a = (struct infra_arg*)arg;
2050 struct infra_key* k = (struct infra_key*)e->key;
2051 struct infra_data* d = (struct infra_data*)e->data;
2056 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
2057 dname_str(k->zonename, name);
2058 /* skip expired stuff (only backed off) */
2059 if(d->ttl < a->now) {
2060 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
2061 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
2062 name, d->rtt.rto)) {
2069 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
2070 "tA %d tAAAA %d tother %d "
2071 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
2072 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now),
2073 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
2074 d->timeout_A, d->timeout_AAAA, d->timeout_other,
2075 (int)d->edns_lame_known, (int)d->edns_version,
2076 (int)(a->now<d->probedelay?d->probedelay-a->now:0),
2077 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
2078 (int)d->lame_other)) {
2084 /** do the dump_infra command */
2086 do_dump_infra(SSL* ssl, struct worker* worker)
2088 struct infra_arg arg;
2089 arg.infra = worker->env.infra_cache;
2091 arg.now = *worker->env.now;
2093 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
2096 /** do the log_reopen command */
2098 do_log_reopen(SSL* ssl, struct worker* worker)
2100 struct config_file* cfg = worker->env.cfg;
2102 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
2105 /** do the set_option command */
2107 do_set_option(SSL* ssl, struct worker* worker, char* arg)
2110 if(!find_arg2(ssl, arg, &arg2))
2112 if(!config_set_option(worker->env.cfg, arg, arg2)) {
2113 (void)ssl_printf(ssl, "error setting option\n");
2119 /* routine to printout option values over SSL */
2120 void remote_get_opt_ssl(char* line, void* arg)
2122 SSL* ssl = (SSL*)arg;
2123 (void)ssl_printf(ssl, "%s\n", line);
2126 /** do the get_option command */
2128 do_get_option(SSL* ssl, struct worker* worker, char* arg)
2131 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
2133 (void)ssl_printf(ssl, "error unknown option\n");
2138 /** do the list_forwards command */
2140 do_list_forwards(SSL* ssl, struct worker* worker)
2142 /* since its a per-worker structure no locks needed */
2143 struct iter_forwards* fwds = worker->env.fwds;
2144 struct iter_forward_zone* z;
2145 struct trust_anchor* a;
2147 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
2148 if(!z->dp) continue; /* skip empty marker for stub */
2150 /* see if it is insecure */
2152 if(worker->env.anchors &&
2153 (a=anchor_find(worker->env.anchors, z->name,
2154 z->namelabs, z->namelen, z->dclass))) {
2155 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2157 lock_basic_unlock(&a->lock);
2160 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"),
2161 z->name, z->dclass, z->dp))
2166 /** do the list_stubs command */
2168 do_list_stubs(SSL* ssl, struct worker* worker)
2170 struct iter_hints_stub* z;
2171 struct trust_anchor* a;
2174 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
2176 /* see if it is insecure */
2178 if(worker->env.anchors &&
2179 (a=anchor_find(worker->env.anchors, z->node.name,
2180 z->node.labs, z->node.len, z->node.dclass))) {
2181 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2183 lock_basic_unlock(&a->lock);
2186 snprintf(str, sizeof(str), "stub %sprime%s",
2187 (z->noprime?"no":""), (insecure?" +i":""));
2188 if(!ssl_print_name_dp(ssl, str, z->node.name,
2189 z->node.dclass, z->dp))
2194 /** do the list_local_zones command */
2196 do_list_local_zones(SSL* ssl, struct worker* worker)
2198 struct local_zones* zones = worker->daemon->local_zones;
2199 struct local_zone* z;
2201 lock_rw_rdlock(&zones->lock);
2202 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2203 lock_rw_rdlock(&z->lock);
2204 dname_str(z->name, buf);
2205 if(!ssl_printf(ssl, "%s %s\n", buf,
2206 local_zone_type2str(z->type))) {
2207 /* failure to print */
2208 lock_rw_unlock(&z->lock);
2209 lock_rw_unlock(&zones->lock);
2212 lock_rw_unlock(&z->lock);
2214 lock_rw_unlock(&zones->lock);
2217 /** do the list_local_data command */
2219 do_list_local_data(SSL* ssl, struct worker* worker)
2221 struct local_zones* zones = worker->daemon->local_zones;
2222 struct local_zone* z;
2223 struct local_data* d;
2224 struct local_rrset* p;
2225 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
2226 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
2227 lock_rw_rdlock(&zones->lock);
2228 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2229 lock_rw_rdlock(&z->lock);
2230 RBTREE_FOR(d, struct local_data*, &z->data) {
2231 for(p = d->rrsets; p; p = p->next) {
2232 struct packed_rrset_data* d =
2233 (struct packed_rrset_data*)p->rrset->entry.data;
2235 for(i=0; i<d->count + d->rrsig_count; i++) {
2236 if(!packed_rr_to_string(p->rrset, i,
2238 if(!ssl_printf(ssl, "BADRR\n"))
2241 if(!ssl_printf(ssl, "%s\n", s))
2246 lock_rw_unlock(&z->lock);
2248 lock_rw_unlock(&zones->lock);
2251 /** tell other processes to execute the command */
2253 distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd)
2258 /* skip i=0 which is me */
2259 for(i=1; i<rc->worker->daemon->num; i++) {
2260 worker_send_cmd(rc->worker->daemon->workers[i],
2262 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2263 (uint8_t*)cmd, strlen(cmd)+1, 0)) {
2264 ssl_printf(ssl, "error could not distribute cmd\n");
2270 /** check for name with end-of-string, space or tab after it */
2272 cmdcmp(char* p, const char* cmd, size_t len)
2274 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2277 /** execute a remote control command */
2279 execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd,
2280 struct worker* worker)
2282 char* p = skipwhite(cmd);
2283 /* compare command */
2284 if(cmdcmp(p, "stop", 4)) {
2287 } else if(cmdcmp(p, "reload", 6)) {
2290 } else if(cmdcmp(p, "stats_noreset", 13)) {
2291 do_stats(ssl, rc, 0);
2293 } else if(cmdcmp(p, "stats", 5)) {
2294 do_stats(ssl, rc, 1);
2296 } else if(cmdcmp(p, "status", 6)) {
2297 do_status(ssl, worker);
2299 } else if(cmdcmp(p, "dump_cache", 10)) {
2300 (void)dump_cache(ssl, worker);
2302 } else if(cmdcmp(p, "load_cache", 10)) {
2303 if(load_cache(ssl, worker)) send_ok(ssl);
2305 } else if(cmdcmp(p, "list_forwards", 13)) {
2306 do_list_forwards(ssl, worker);
2308 } else if(cmdcmp(p, "list_stubs", 10)) {
2309 do_list_stubs(ssl, worker);
2311 } else if(cmdcmp(p, "list_local_zones", 16)) {
2312 do_list_local_zones(ssl, worker);
2314 } else if(cmdcmp(p, "list_local_data", 15)) {
2315 do_list_local_data(ssl, worker);
2317 } else if(cmdcmp(p, "stub_add", 8)) {
2318 /* must always distribute this cmd */
2319 if(rc) distribute_cmd(rc, ssl, cmd);
2320 do_stub_add(ssl, worker, skipwhite(p+8));
2322 } else if(cmdcmp(p, "stub_remove", 11)) {
2323 /* must always distribute this cmd */
2324 if(rc) distribute_cmd(rc, ssl, cmd);
2325 do_stub_remove(ssl, worker, skipwhite(p+11));
2327 } else if(cmdcmp(p, "forward_add", 11)) {
2328 /* must always distribute this cmd */
2329 if(rc) distribute_cmd(rc, ssl, cmd);
2330 do_forward_add(ssl, worker, skipwhite(p+11));
2332 } else if(cmdcmp(p, "forward_remove", 14)) {
2333 /* must always distribute this cmd */
2334 if(rc) distribute_cmd(rc, ssl, cmd);
2335 do_forward_remove(ssl, worker, skipwhite(p+14));
2337 } else if(cmdcmp(p, "insecure_add", 12)) {
2338 /* must always distribute this cmd */
2339 if(rc) distribute_cmd(rc, ssl, cmd);
2340 do_insecure_add(ssl, worker, skipwhite(p+12));
2342 } else if(cmdcmp(p, "insecure_remove", 15)) {
2343 /* must always distribute this cmd */
2344 if(rc) distribute_cmd(rc, ssl, cmd);
2345 do_insecure_remove(ssl, worker, skipwhite(p+15));
2347 } else if(cmdcmp(p, "forward", 7)) {
2348 /* must always distribute this cmd */
2349 if(rc) distribute_cmd(rc, ssl, cmd);
2350 do_forward(ssl, worker, skipwhite(p+7));
2352 } else if(cmdcmp(p, "flush_stats", 11)) {
2353 /* must always distribute this cmd */
2354 if(rc) distribute_cmd(rc, ssl, cmd);
2355 do_flush_stats(ssl, worker);
2357 } else if(cmdcmp(p, "flush_requestlist", 17)) {
2358 /* must always distribute this cmd */
2359 if(rc) distribute_cmd(rc, ssl, cmd);
2360 do_flush_requestlist(ssl, worker);
2362 } else if(cmdcmp(p, "lookup", 6)) {
2363 do_lookup(ssl, worker, skipwhite(p+6));
2367 #ifdef THREADS_DISABLED
2368 /* other processes must execute the command as well */
2369 /* commands that should not be distributed, returned above. */
2370 if(rc) { /* only if this thread is the master (rc) thread */
2371 /* done before the code below, which may split the string */
2372 distribute_cmd(rc, ssl, cmd);
2375 if(cmdcmp(p, "verbosity", 9)) {
2376 do_verbosity(ssl, skipwhite(p+9));
2377 } else if(cmdcmp(p, "local_zone_remove", 17)) {
2378 do_zone_remove(ssl, worker, skipwhite(p+17));
2379 } else if(cmdcmp(p, "local_zone", 10)) {
2380 do_zone_add(ssl, worker, skipwhite(p+10));
2381 } else if(cmdcmp(p, "local_data_remove", 17)) {
2382 do_data_remove(ssl, worker, skipwhite(p+17));
2383 } else if(cmdcmp(p, "local_data", 10)) {
2384 do_data_add(ssl, worker, skipwhite(p+10));
2385 } else if(cmdcmp(p, "flush_zone", 10)) {
2386 do_flush_zone(ssl, worker, skipwhite(p+10));
2387 } else if(cmdcmp(p, "flush_type", 10)) {
2388 do_flush_type(ssl, worker, skipwhite(p+10));
2389 } else if(cmdcmp(p, "flush_infra", 11)) {
2390 do_flush_infra(ssl, worker, skipwhite(p+11));
2391 } else if(cmdcmp(p, "flush", 5)) {
2392 do_flush_name(ssl, worker, skipwhite(p+5));
2393 } else if(cmdcmp(p, "dump_requestlist", 16)) {
2394 do_dump_requestlist(ssl, worker);
2395 } else if(cmdcmp(p, "dump_infra", 10)) {
2396 do_dump_infra(ssl, worker);
2397 } else if(cmdcmp(p, "log_reopen", 10)) {
2398 do_log_reopen(ssl, worker);
2399 } else if(cmdcmp(p, "set_option", 10)) {
2400 do_set_option(ssl, worker, skipwhite(p+10));
2401 } else if(cmdcmp(p, "get_option", 10)) {
2402 do_get_option(ssl, worker, skipwhite(p+10));
2403 } else if(cmdcmp(p, "flush_bogus", 11)) {
2404 do_flush_bogus(ssl, worker);
2405 } else if(cmdcmp(p, "flush_negative", 14)) {
2406 do_flush_negative(ssl, worker);
2408 (void)ssl_printf(ssl, "error unknown command '%s'\n", p);
2413 daemon_remote_exec(struct worker* worker)
2415 /* read the cmd string */
2416 uint8_t* msg = NULL;
2418 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
2419 log_err("daemon_remote_exec: tube_read_msg failed");
2422 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
2423 execute_cmd(NULL, NULL, (char*)msg, worker);
2427 /** handle remote control request */
2429 handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl)
2436 /* makes it possible to set the socket blocking again. */
2437 /* basically removes it from winsock_event ... */
2438 WSAEventSelect(s->c->fd, NULL, 0);
2440 fd_set_block(s->c->fd);
2442 /* try to read magic UBCT[version]_space_ string */
2444 if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) {
2445 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN)
2447 log_crypto_err("could not SSL_read");
2451 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
2452 verbose(VERB_QUERY, "control connection has bad magic string");
2453 /* probably wrong tool connected, ignore it completely */
2457 /* read the command line */
2458 if(!ssl_read_line(ssl, buf, sizeof(buf))) {
2461 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
2462 if(strcmp(magic, pre) != 0) {
2463 verbose(VERB_QUERY, "control connection had bad "
2464 "version %s, cmd: %s", magic, buf);
2465 ssl_printf(ssl, "error version mismatch\n");
2468 verbose(VERB_DETAIL, "control cmd: %s", buf);
2470 /* figure out what to do */
2471 execute_cmd(rc, ssl, buf, rc->worker);
2474 int remote_control_callback(struct comm_point* c, void* arg, int err,
2475 struct comm_reply* ATTR_UNUSED(rep))
2477 struct rc_state* s = (struct rc_state*)arg;
2478 struct daemon_remote* rc = s->rc;
2480 if(err != NETEVENT_NOERROR) {
2481 if(err==NETEVENT_TIMEOUT)
2482 log_err("remote control timed out");
2486 /* (continue to) setup the SSL connection */
2488 r = SSL_do_handshake(s->ssl);
2490 int r2 = SSL_get_error(s->ssl, r);
2491 if(r2 == SSL_ERROR_WANT_READ) {
2492 if(s->shake_state == rc_hs_read) {
2493 /* try again later */
2496 s->shake_state = rc_hs_read;
2497 comm_point_listen_for_rw(c, 1, 0);
2499 } else if(r2 == SSL_ERROR_WANT_WRITE) {
2500 if(s->shake_state == rc_hs_write) {
2501 /* try again later */
2504 s->shake_state = rc_hs_write;
2505 comm_point_listen_for_rw(c, 0, 1);
2509 log_err("remote control connection closed prematurely");
2510 log_addr(1, "failed connection from",
2511 &s->c->repinfo.addr, s->c->repinfo.addrlen);
2512 log_crypto_err("remote control failed ssl");
2517 s->shake_state = rc_none;
2519 /* once handshake has completed, check authentication */
2520 if (!rc->use_cert) {
2521 verbose(VERB_ALGO, "unauthenticated remote control connection");
2522 } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
2523 X509* x = SSL_get_peer_certificate(s->ssl);
2525 verbose(VERB_DETAIL, "remote control connection "
2526 "provided no client certificate");
2530 verbose(VERB_ALGO, "remote control connection authenticated");
2533 verbose(VERB_DETAIL, "remote control connection failed to "
2534 "authenticate with client certificate");
2539 /* if OK start to actually handle the request */
2540 handle_req(rc, s, s->ssl);
2542 verbose(VERB_ALGO, "remote control operation completed");