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 cfg_uid != (uid_t)-1)
333 chown(ip, cfg_uid, cfg_gid);
334 chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
340 hints.ai_socktype = SOCK_STREAM;
341 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
342 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
344 if(!noproto_is_err && r == EAI_NONAME) {
345 /* tried to lookup the address as name */
346 return 1; /* return success, but do nothing */
348 #endif /* USE_WINSOCK */
349 log_err("control interface %s:%s getaddrinfo: %s %s",
350 ip?ip:"default", port, gai_strerror(r),
352 r==EAI_SYSTEM?(char*)strerror(errno):""
361 fd = create_tcp_accept_sock(res, 1, &noproto, 0);
365 if(fd == -1 && noproto) {
367 return 1; /* return success, but do nothing */
368 log_err("cannot open control interface %s %d : "
369 "protocol not supported", ip, nr);
373 log_err("cannot open control interface %s %d", ip, nr);
378 n = (struct listen_port*)calloc(1, sizeof(*n));
385 log_err("out of memory");
394 struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
396 struct listen_port* l = NULL;
397 log_assert(cfg->remote_control_enable && cfg->control_port);
398 if(cfg->control_ifs) {
399 struct config_strlist* p;
400 for(p = cfg->control_ifs; p; p = p->next) {
401 if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) {
402 listening_ports_free(l);
409 !add_open("::1", cfg->control_port, &l, 0, cfg)) {
410 listening_ports_free(l);
414 !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) {
415 listening_ports_free(l);
422 /** open accept commpoint */
424 accept_open(struct daemon_remote* rc, int fd)
426 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
428 log_err("out of memory");
431 n->next = rc->accept_list;
434 n->com = comm_point_create_raw(rc->worker->base, fd, 0,
435 &remote_accept_callback, rc);
438 /* keep this port open, its fd is kept in the rc portlist */
439 n->com->do_not_close = 1;
443 int daemon_remote_open_accept(struct daemon_remote* rc,
444 struct listen_port* ports, struct worker* worker)
446 struct listen_port* p;
448 for(p = ports; p; p = p->next) {
449 if(!accept_open(rc, p->fd)) {
450 log_err("could not create accept comm point");
457 void daemon_remote_stop_accept(struct daemon_remote* rc)
459 struct listen_list* p;
460 for(p=rc->accept_list; p; p=p->next) {
461 comm_point_stop_listening(p->com);
465 void daemon_remote_start_accept(struct daemon_remote* rc)
467 struct listen_list* p;
468 for(p=rc->accept_list; p; p=p->next) {
469 comm_point_start_listening(p->com, -1, -1);
473 int remote_accept_callback(struct comm_point* c, void* arg, int err,
474 struct comm_reply* ATTR_UNUSED(rep))
476 struct daemon_remote* rc = (struct daemon_remote*)arg;
477 struct sockaddr_storage addr;
481 if(err != NETEVENT_NOERROR) {
482 log_err("error %d on remote_accept_callback", err);
485 /* perform the accept */
486 newfd = comm_point_perform_accept(c, &addr, &addrlen);
489 /* create new commpoint unless we are servicing already */
490 if(rc->active >= rc->max_active) {
491 log_warn("drop incoming remote control: too many connections");
501 /* setup commpoint to service the remote control command */
502 n = (struct rc_state*)calloc(1, sizeof(*n));
504 log_err("out of memory");
507 /* start in reading state */
508 n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
509 &remote_control_callback, n);
511 log_err("out of memory");
515 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
516 n->c->do_not_close = 0;
517 comm_point_stop_listening(n->c);
518 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
519 memcpy(&n->c->repinfo.addr, &addr, addrlen);
520 n->c->repinfo.addrlen = addrlen;
521 n->shake_state = rc_hs_read;
522 n->ssl = SSL_new(rc->ctx);
524 log_crypto_err("could not SSL_new");
525 comm_point_delete(n->c);
529 SSL_set_accept_state(n->ssl);
530 (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY);
531 if(!SSL_set_fd(n->ssl, newfd)) {
532 log_crypto_err("could not SSL_set_fd");
534 comm_point_delete(n->c);
540 n->next = rc->busy_list;
544 /* perform the first nonblocking read already, for windows,
545 * so it can return wouldblock. could be faster too. */
546 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
550 /** delete from list */
552 state_list_remove_elem(struct rc_state** list, struct comm_point* c)
555 if( (*list)->c == c) {
556 *list = (*list)->next;
559 list = &(*list)->next;
563 /** decrease active count and remove commpoint from busy list */
565 clean_point(struct daemon_remote* rc, struct rc_state* s)
567 state_list_remove_elem(&rc->busy_list, s->c);
570 SSL_shutdown(s->ssl);
573 comm_point_delete(s->c);
578 ssl_print_text(SSL* ssl, const char* text)
584 if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) {
585 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
586 verbose(VERB_QUERY, "warning, in SSL_write, peer "
587 "closed connection");
590 log_crypto_err("could not SSL_write");
596 /** print text over the ssl connection */
598 ssl_print_vmsg(SSL* ssl, const char* format, va_list args)
601 vsnprintf(msg, sizeof(msg), format, args);
602 return ssl_print_text(ssl, msg);
605 /** printf style printing to the ssl connection */
606 int ssl_printf(SSL* ssl, const char* format, ...)
610 va_start(args, format);
611 ret = ssl_print_vmsg(ssl, format, args);
617 ssl_read_line(SSL* ssl, char* buf, size_t max)
625 if((r=SSL_read(ssl, buf+len, 1)) <= 0) {
626 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
630 log_crypto_err("could not SSL_read");
633 if(buf[len] == '\n') {
634 /* return string without \n */
641 log_err("control line too long (%d): %s", (int)max, buf);
645 /** skip whitespace, return new pointer into string */
649 /* EOS \0 is not a space */
650 while( isspace((unsigned char)*str) )
655 /** send the OK to the control client */
656 static void send_ok(SSL* ssl)
658 (void)ssl_printf(ssl, "ok\n");
661 /** do the stop command */
663 do_stop(SSL* ssl, struct daemon_remote* rc)
665 rc->worker->need_to_exit = 1;
666 comm_base_exit(rc->worker->base);
670 /** do the reload command */
672 do_reload(SSL* ssl, struct daemon_remote* rc)
674 rc->worker->need_to_exit = 0;
675 comm_base_exit(rc->worker->base);
679 /** do the verbosity command */
681 do_verbosity(SSL* ssl, char* str)
684 if(val == 0 && strcmp(str, "0") != 0) {
685 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
692 /** print stats from statinfo */
694 print_stats(SSL* ssl, const char* nm, struct stats_info* s)
697 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm,
698 (unsigned long)s->svr.num_queries)) return 0;
699 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm,
700 (unsigned long)(s->svr.num_queries
701 - s->svr.num_queries_missed_cache))) return 0;
702 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm,
703 (unsigned long)s->svr.num_queries_missed_cache)) return 0;
704 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm,
705 (unsigned long)s->svr.num_queries_prefetch)) return 0;
706 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm,
707 (unsigned long)s->mesh_replies_sent)) return 0;
708 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
709 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
710 (double)s->svr.sum_query_list_size/
711 (s->svr.num_queries_missed_cache+
712 s->svr.num_queries_prefetch) : 0.0)) return 0;
713 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm,
714 (unsigned long)s->svr.max_query_list_size)) return 0;
715 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm,
716 (unsigned long)s->mesh_jostled)) return 0;
717 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm,
718 (unsigned long)s->mesh_dropped)) return 0;
719 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm,
720 (unsigned long)s->mesh_num_states)) return 0;
721 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm,
722 (unsigned long)s->mesh_num_reply_states)) return 0;
723 timeval_divide(&avg, &s->mesh_replies_sum_wait, s->mesh_replies_sent);
724 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
725 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
726 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
727 s->mesh_time_median)) return 0;
731 /** print stats for one thread */
733 print_thread_stats(SSL* ssl, int i, struct stats_info* s)
736 snprintf(nm, sizeof(nm), "thread%d", i);
738 return print_stats(ssl, nm, s);
741 /** print long number */
743 print_longnum(SSL* ssl, const char* desc, size_t x)
745 if(x > 1024*1024*1024) {
747 size_t front = x / (size_t)1000000;
748 size_t back = x % (size_t)1000000;
749 return ssl_printf(ssl, "%s%u%6.6u\n", desc,
750 (unsigned)front, (unsigned)back);
752 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x);
756 /** print mem stats */
758 print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon)
761 size_t msg, rrset, val, iter;
763 extern void* unbound_start_brk;
765 if(!print_longnum(ssl, "mem.total.sbrk"SQ,
766 (size_t)((char*)cur - (char*)unbound_start_brk))) return 0;
767 #endif /* HAVE_SBRK */
768 msg = slabhash_get_mem(daemon->env->msg_cache);
769 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
772 m = modstack_find(&worker->env.mesh->mods, "validator");
774 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
775 mods.mod[m]->get_mem));
776 val = (*worker->env.mesh->mods.mod[m]->get_mem)
779 m = modstack_find(&worker->env.mesh->mods, "iterator");
781 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
782 mods.mod[m]->get_mem));
783 iter = (*worker->env.mesh->mods.mod[m]->get_mem)
787 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
789 if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
791 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
793 if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
798 /** print uptime stats */
800 print_uptime(SSL* ssl, struct worker* worker, int reset)
802 struct timeval now = *worker->env.now_tv;
803 struct timeval up, dt;
804 timeval_subtract(&up, &now, &worker->daemon->time_boot);
805 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
807 worker->daemon->time_last_stat = now;
808 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
809 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
810 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
811 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
812 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
813 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
817 /** print extended histogram */
819 print_hist(SSL* ssl, struct stats_info* s)
821 struct timehist* hist;
823 hist = timehist_setup();
825 log_err("out of memory");
828 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
829 for(i=0; i<hist->num; i++) {
831 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
832 (int)hist->buckets[i].lower.tv_sec,
833 (int)hist->buckets[i].lower.tv_usec,
834 (int)hist->buckets[i].upper.tv_sec,
835 (int)hist->buckets[i].upper.tv_usec,
836 (unsigned long)hist->buckets[i].count)) {
837 timehist_delete(hist);
841 timehist_delete(hist);
845 /** print extended stats */
847 print_ext(SSL* ssl, struct stats_info* s)
851 const sldns_rr_descriptor* desc;
852 const sldns_lookup_table* lt;
854 for(i=0; i<STATS_QTYPE_NUM; i++) {
855 if(inhibit_zero && s->svr.qtype[i] == 0)
857 desc = sldns_rr_descript((uint16_t)i);
858 if(desc && desc->_name) {
859 snprintf(nm, sizeof(nm), "%s", desc->_name);
860 } else if (i == LDNS_RR_TYPE_IXFR) {
861 snprintf(nm, sizeof(nm), "IXFR");
862 } else if (i == LDNS_RR_TYPE_AXFR) {
863 snprintf(nm, sizeof(nm), "AXFR");
864 } else if (i == LDNS_RR_TYPE_MAILA) {
865 snprintf(nm, sizeof(nm), "MAILA");
866 } else if (i == LDNS_RR_TYPE_MAILB) {
867 snprintf(nm, sizeof(nm), "MAILB");
868 } else if (i == LDNS_RR_TYPE_ANY) {
869 snprintf(nm, sizeof(nm), "ANY");
871 snprintf(nm, sizeof(nm), "TYPE%d", i);
873 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n",
874 nm, (unsigned long)s->svr.qtype[i])) return 0;
876 if(!inhibit_zero || s->svr.qtype_big) {
877 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n",
878 (unsigned long)s->svr.qtype_big)) return 0;
881 for(i=0; i<STATS_QCLASS_NUM; i++) {
882 if(inhibit_zero && s->svr.qclass[i] == 0)
884 lt = sldns_lookup_by_id(sldns_rr_classes, i);
886 snprintf(nm, sizeof(nm), "%s", lt->name);
888 snprintf(nm, sizeof(nm), "CLASS%d", i);
890 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n",
891 nm, (unsigned long)s->svr.qclass[i])) return 0;
893 if(!inhibit_zero || s->svr.qclass_big) {
894 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n",
895 (unsigned long)s->svr.qclass_big)) return 0;
898 for(i=0; i<STATS_OPCODE_NUM; i++) {
899 if(inhibit_zero && s->svr.qopcode[i] == 0)
901 lt = sldns_lookup_by_id(sldns_opcodes, i);
903 snprintf(nm, sizeof(nm), "%s", lt->name);
905 snprintf(nm, sizeof(nm), "OPCODE%d", i);
907 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n",
908 nm, (unsigned long)s->svr.qopcode[i])) return 0;
911 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n",
912 (unsigned long)s->svr.qtcp)) return 0;
913 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n",
914 (unsigned long)s->svr.qtcp_outgoing)) return 0;
915 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n",
916 (unsigned long)s->svr.qipv6)) return 0;
918 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n",
919 (unsigned long)s->svr.qbit_QR)) return 0;
920 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n",
921 (unsigned long)s->svr.qbit_AA)) return 0;
922 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n",
923 (unsigned long)s->svr.qbit_TC)) return 0;
924 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n",
925 (unsigned long)s->svr.qbit_RD)) return 0;
926 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n",
927 (unsigned long)s->svr.qbit_RA)) return 0;
928 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n",
929 (unsigned long)s->svr.qbit_Z)) return 0;
930 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n",
931 (unsigned long)s->svr.qbit_AD)) return 0;
932 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n",
933 (unsigned long)s->svr.qbit_CD)) return 0;
934 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n",
935 (unsigned long)s->svr.qEDNS)) return 0;
936 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n",
937 (unsigned long)s->svr.qEDNS_DO)) return 0;
940 for(i=0; i<STATS_RCODE_NUM; i++) {
941 /* Always include RCODEs 0-5 */
942 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
944 lt = sldns_lookup_by_id(sldns_rcodes, i);
946 snprintf(nm, sizeof(nm), "%s", lt->name);
948 snprintf(nm, sizeof(nm), "RCODE%d", i);
950 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n",
951 nm, (unsigned long)s->svr.ans_rcode[i])) return 0;
953 if(!inhibit_zero || s->svr.ans_rcode_nodata) {
954 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n",
955 (unsigned long)s->svr.ans_rcode_nodata)) return 0;
958 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n",
959 (unsigned long)s->svr.ans_secure)) return 0;
960 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n",
961 (unsigned long)s->svr.ans_bogus)) return 0;
962 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n",
963 (unsigned long)s->svr.rrset_bogus)) return 0;
964 /* threat detection */
965 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n",
966 (unsigned long)s->svr.unwanted_queries)) return 0;
967 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n",
968 (unsigned long)s->svr.unwanted_replies)) return 0;
970 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
971 (unsigned)s->svr.msg_cache_count)) return 0;
972 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
973 (unsigned)s->svr.rrset_cache_count)) return 0;
974 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
975 (unsigned)s->svr.infra_cache_count)) return 0;
976 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
977 (unsigned)s->svr.key_cache_count)) return 0;
981 /** do the stats command */
983 do_stats(SSL* ssl, struct daemon_remote* rc, int reset)
985 struct daemon* daemon = rc->worker->daemon;
986 struct stats_info total;
989 log_assert(daemon->num > 0);
990 /* gather all thread statistics in one place */
991 for(i=0; i<daemon->num; i++) {
992 server_stats_obtain(rc->worker, daemon->workers[i], &s, reset);
993 if(!print_thread_stats(ssl, i, &s))
997 else server_stats_add(&total, &s);
999 /* print the thread statistics */
1000 total.mesh_time_median /= (double)daemon->num;
1001 if(!print_stats(ssl, "total", &total))
1003 if(!print_uptime(ssl, rc->worker, reset))
1005 if(daemon->cfg->stat_extended) {
1006 if(!print_mem(ssl, rc->worker, daemon))
1008 if(!print_hist(ssl, &total))
1010 if(!print_ext(ssl, &total))
1015 /** parse commandline argument domain name */
1017 parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs)
1019 uint8_t nm[LDNS_MAX_DOMAINLEN+1];
1020 size_t nmlen = sizeof(nm);
1025 status = sldns_str2wire_dname_buf(str, nm, &nmlen);
1027 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
1028 LDNS_WIREPARSE_OFFSET(status),
1029 sldns_get_errorstr_parse(status));
1032 *res = memdup(nm, nmlen);
1034 ssl_printf(ssl, "error out of memory\n");
1037 *labs = dname_count_size_labels(*res, len);
1041 /** find second argument, modifies string */
1043 find_arg2(SSL* ssl, char* arg, char** arg2)
1045 char* as = strchr(arg, ' ');
1046 char* at = strchr(arg, '\t');
1051 *arg2 = skipwhite(as+1);
1054 *arg2 = skipwhite(as+1);
1057 *arg2 = skipwhite(at+1);
1059 ssl_printf(ssl, "error could not find next argument "
1066 /** Add a new zone */
1068 do_zone_add(SSL* ssl, struct worker* worker, char* arg)
1074 enum localzone_type t;
1075 struct local_zone* z;
1076 if(!find_arg2(ssl, arg, &arg2))
1078 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1080 if(!local_zone_str2type(arg2, &t)) {
1081 ssl_printf(ssl, "error not a zone type. %s\n", arg2);
1085 lock_rw_wrlock(&worker->daemon->local_zones->lock);
1086 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1087 nmlabs, LDNS_RR_CLASS_IN))) {
1088 /* already present in tree */
1089 lock_rw_wrlock(&z->lock);
1090 z->type = t; /* update type anyway */
1091 lock_rw_unlock(&z->lock);
1093 lock_rw_unlock(&worker->daemon->local_zones->lock);
1097 if(!local_zones_add_zone(worker->daemon->local_zones, nm, nmlen,
1098 nmlabs, LDNS_RR_CLASS_IN, t)) {
1099 lock_rw_unlock(&worker->daemon->local_zones->lock);
1100 ssl_printf(ssl, "error out of memory\n");
1103 lock_rw_unlock(&worker->daemon->local_zones->lock);
1107 /** Remove a zone */
1109 do_zone_remove(SSL* ssl, struct worker* worker, char* arg)
1114 struct local_zone* z;
1115 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1117 lock_rw_wrlock(&worker->daemon->local_zones->lock);
1118 if((z=local_zones_find(worker->daemon->local_zones, nm, nmlen,
1119 nmlabs, LDNS_RR_CLASS_IN))) {
1120 /* present in tree */
1121 local_zones_del_zone(worker->daemon->local_zones, z);
1123 lock_rw_unlock(&worker->daemon->local_zones->lock);
1128 /** Add new RR data */
1130 do_data_add(SSL* ssl, struct worker* worker, char* arg)
1132 if(!local_zones_add_RR(worker->daemon->local_zones, arg)) {
1133 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1139 /** Remove RR data */
1141 do_data_remove(SSL* ssl, struct worker* worker, char* arg)
1146 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1148 local_zones_del_data(worker->daemon->local_zones, nm,
1149 nmlen, nmlabs, LDNS_RR_CLASS_IN);
1154 /** cache lookup of nameservers */
1156 do_lookup(SSL* ssl, struct worker* worker, char* arg)
1161 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1163 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1167 /** flush something from rrset and msg caches */
1169 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1170 uint16_t t, uint16_t c)
1173 struct query_info k;
1174 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1175 if(t == LDNS_RR_TYPE_SOA)
1176 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1177 PACKED_RRSET_SOA_NEG);
1179 k.qname_len = nmlen;
1182 h = query_info_hash(&k, 0);
1183 slabhash_remove(worker->env.msg_cache, h, &k);
1184 if(t == LDNS_RR_TYPE_AAAA) {
1185 /* for AAAA also flush dns64 bit_cd packet */
1186 h = query_info_hash(&k, BIT_CD);
1187 slabhash_remove(worker->env.msg_cache, h, &k);
1193 do_flush_type(SSL* ssl, struct worker* worker, char* arg)
1200 if(!find_arg2(ssl, arg, &arg2))
1202 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1204 t = sldns_get_rr_type_by_name(arg2);
1205 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1211 /** flush statistics */
1213 do_flush_stats(SSL* ssl, struct worker* worker)
1215 worker_stats_clear(worker);
1220 * Local info for deletion functions
1224 struct worker* worker;
1225 /** name to delete */
1233 /** time to invalidate to */
1235 /** number of rrsets removed */
1237 /** number of msgs removed */
1239 /** number of key entries removed */
1241 /** length of addr */
1243 /** socket address for host deletion */
1244 struct sockaddr_storage addr;
1247 /** callback to delete hosts in infra cache */
1249 infra_del_host(struct lruhash_entry* e, void* arg)
1251 /* entry is locked */
1252 struct del_info* inf = (struct del_info*)arg;
1253 struct infra_key* k = (struct infra_key*)e->key;
1254 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1255 struct infra_data* d = (struct infra_data*)e->data;
1258 d->timeout_AAAA = 0;
1259 d->timeout_other = 0;
1261 if(d->ttl >= inf->now) {
1262 d->ttl = inf->expired;
1268 /** flush infra cache */
1270 do_flush_infra(SSL* ssl, struct worker* worker, char* arg)
1272 struct sockaddr_storage addr;
1274 struct del_info inf;
1275 if(strcmp(arg, "all") == 0) {
1276 slabhash_clear(worker->env.infra_cache->hosts);
1280 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1281 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1284 /* delete all entries from cache */
1285 /* what we do is to set them all expired */
1286 inf.worker = worker;
1290 inf.now = *worker->env.now;
1291 inf.expired = *worker->env.now;
1292 inf.expired -= 3; /* handle 3 seconds skew between threads */
1297 memmove(&inf.addr, &addr, len);
1298 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1303 /** flush requestlist */
1305 do_flush_requestlist(SSL* ssl, struct worker* worker)
1307 mesh_delete_all(worker->env.mesh);
1311 /** callback to delete rrsets in a zone */
1313 zone_del_rrset(struct lruhash_entry* e, void* arg)
1315 /* entry is locked */
1316 struct del_info* inf = (struct del_info*)arg;
1317 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1318 if(dname_subdomain_c(k->rk.dname, inf->name)) {
1319 struct packed_rrset_data* d =
1320 (struct packed_rrset_data*)e->data;
1321 if(d->ttl >= inf->now) {
1322 d->ttl = inf->expired;
1328 /** callback to delete messages in a zone */
1330 zone_del_msg(struct lruhash_entry* e, void* arg)
1332 /* entry is locked */
1333 struct del_info* inf = (struct del_info*)arg;
1334 struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1335 if(dname_subdomain_c(k->key.qname, inf->name)) {
1336 struct reply_info* d = (struct reply_info*)e->data;
1337 if(d->ttl >= inf->now) {
1338 d->ttl = inf->expired;
1344 /** callback to delete keys in zone */
1346 zone_del_kcache(struct lruhash_entry* e, void* arg)
1348 /* entry is locked */
1349 struct del_info* inf = (struct del_info*)arg;
1350 struct key_entry_key* k = (struct key_entry_key*)e->key;
1351 if(dname_subdomain_c(k->name, inf->name)) {
1352 struct key_entry_data* d = (struct key_entry_data*)e->data;
1353 if(d->ttl >= inf->now) {
1354 d->ttl = inf->expired;
1360 /** remove all rrsets and keys from zone from cache */
1362 do_flush_zone(SSL* ssl, struct worker* worker, char* arg)
1367 struct del_info inf;
1368 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1370 /* delete all RRs and key entries from zone */
1371 /* what we do is to set them all expired */
1372 inf.worker = worker;
1376 inf.now = *worker->env.now;
1377 inf.expired = *worker->env.now;
1378 inf.expired -= 3; /* handle 3 seconds skew between threads */
1382 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1383 &zone_del_rrset, &inf);
1385 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1387 /* and validator cache */
1388 if(worker->env.key_cache) {
1389 slabhash_traverse(worker->env.key_cache->slab, 1,
1390 &zone_del_kcache, &inf);
1395 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1396 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1397 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1400 /** callback to delete bogus rrsets */
1402 bogus_del_rrset(struct lruhash_entry* e, void* arg)
1404 /* entry is locked */
1405 struct del_info* inf = (struct del_info*)arg;
1406 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1407 if(d->security == sec_status_bogus) {
1408 d->ttl = inf->expired;
1413 /** callback to delete bogus messages */
1415 bogus_del_msg(struct lruhash_entry* e, void* arg)
1417 /* entry is locked */
1418 struct del_info* inf = (struct del_info*)arg;
1419 struct reply_info* d = (struct reply_info*)e->data;
1420 if(d->security == sec_status_bogus) {
1421 d->ttl = inf->expired;
1426 /** callback to delete bogus keys */
1428 bogus_del_kcache(struct lruhash_entry* e, void* arg)
1430 /* entry is locked */
1431 struct del_info* inf = (struct del_info*)arg;
1432 struct key_entry_data* d = (struct key_entry_data*)e->data;
1434 d->ttl = inf->expired;
1439 /** remove all bogus rrsets, msgs and keys from cache */
1441 do_flush_bogus(SSL* ssl, struct worker* worker)
1443 struct del_info inf;
1444 /* what we do is to set them all expired */
1445 inf.worker = worker;
1446 inf.now = *worker->env.now;
1447 inf.expired = *worker->env.now;
1448 inf.expired -= 3; /* handle 3 seconds skew between threads */
1452 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1453 &bogus_del_rrset, &inf);
1455 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1457 /* and validator cache */
1458 if(worker->env.key_cache) {
1459 slabhash_traverse(worker->env.key_cache->slab, 1,
1460 &bogus_del_kcache, &inf);
1463 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1464 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1465 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1468 /** callback to delete negative and servfail rrsets */
1470 negative_del_rrset(struct lruhash_entry* e, void* arg)
1472 /* entry is locked */
1473 struct del_info* inf = (struct del_info*)arg;
1474 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1475 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1476 /* delete the parentside negative cache rrsets,
1477 * these are namerserver rrsets that failed lookup, rdata empty */
1478 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 &&
1479 d->rrsig_count == 0 && d->rr_len[0] == 0) {
1480 d->ttl = inf->expired;
1485 /** callback to delete negative and servfail messages */
1487 negative_del_msg(struct lruhash_entry* e, void* arg)
1489 /* entry is locked */
1490 struct del_info* inf = (struct del_info*)arg;
1491 struct reply_info* d = (struct reply_info*)e->data;
1492 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1493 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1494 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) {
1495 d->ttl = inf->expired;
1500 /** callback to delete negative key entries */
1502 negative_del_kcache(struct lruhash_entry* e, void* arg)
1504 /* entry is locked */
1505 struct del_info* inf = (struct del_info*)arg;
1506 struct key_entry_data* d = (struct key_entry_data*)e->data;
1507 /* could be bad because of lookup failure on the DS, DNSKEY, which
1508 * was nxdomain or servfail, and thus a result of negative lookups */
1510 d->ttl = inf->expired;
1515 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1517 do_flush_negative(SSL* ssl, struct worker* worker)
1519 struct del_info inf;
1520 /* what we do is to set them all expired */
1521 inf.worker = worker;
1522 inf.now = *worker->env.now;
1523 inf.expired = *worker->env.now;
1524 inf.expired -= 3; /* handle 3 seconds skew between threads */
1528 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1529 &negative_del_rrset, &inf);
1531 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf);
1533 /* and validator cache */
1534 if(worker->env.key_cache) {
1535 slabhash_traverse(worker->env.key_cache->slab, 1,
1536 &negative_del_kcache, &inf);
1539 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1540 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1541 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1544 /** remove name rrset from cache */
1546 do_flush_name(SSL* ssl, struct worker* w, char* arg)
1551 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1553 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1554 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1555 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1556 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1557 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1558 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1559 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1560 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1561 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1562 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1568 /** printout a delegation point info */
1570 ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1574 struct delegpt_ns* ns;
1575 struct delegpt_addr* a;
1577 if(str) { /* print header for forward, stub */
1578 char* c = sldns_wire2str_class(dclass);
1580 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) {
1586 for(ns = dp->nslist; ns; ns = ns->next) {
1587 dname_str(ns->name, buf);
1588 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1592 for(a = dp->target_list; a; a = a->next_target) {
1593 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1594 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1598 return ssl_printf(ssl, "\n");
1602 /** print root forwards */
1604 print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root)
1607 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1609 return ssl_printf(ssl, "off (using root hints)\n");
1610 /* if dp is returned it must be the root */
1611 log_assert(query_dname_compare(dp->name, root)==0);
1612 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1615 /** parse args into delegpt */
1616 static struct delegpt*
1617 parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names)
1619 /* parse args and add in */
1622 struct delegpt* dp = delegpt_create_mlc(nm);
1623 struct sockaddr_storage addr;
1626 (void)ssl_printf(ssl, "error out of memory\n");
1631 p = strchr(p, ' '); /* find next spot, if any */
1633 *p++ = 0; /* end this spot */
1634 p = skipwhite(p); /* position at next spot */
1637 if(!extstrtoaddr(todo, &addr, &addrlen)) {
1642 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
1643 (void)ssl_printf(ssl, "error cannot "
1644 "parse IP address or name "
1646 delegpt_free_mlc(dp);
1649 if(!delegpt_add_ns_mlc(dp, n, 0)) {
1650 (void)ssl_printf(ssl, "error out of memory\n");
1652 delegpt_free_mlc(dp);
1658 (void)ssl_printf(ssl, "error cannot parse"
1659 " IP address '%s'\n", todo);
1660 delegpt_free_mlc(dp);
1665 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) {
1666 (void)ssl_printf(ssl, "error out of memory\n");
1667 delegpt_free_mlc(dp);
1675 /** do the status command */
1677 do_forward(SSL* ssl, struct worker* worker, char* args)
1679 struct iter_forwards* fwd = worker->env.fwds;
1680 uint8_t* root = (uint8_t*)"\000";
1682 (void)ssl_printf(ssl, "error: structure not allocated\n");
1685 if(args == NULL || args[0] == 0) {
1686 (void)print_root_fwds(ssl, fwd, root);
1689 /* set root forwards for this thread. since we are in remote control
1690 * the actual mesh is not running, so we can freely edit it. */
1691 /* delete all the existing queries first */
1692 mesh_delete_all(worker->env.mesh);
1693 if(strcmp(args, "off") == 0) {
1694 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
1697 if(!(dp = parse_delegpt(ssl, args, root, 0)))
1699 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1700 (void)ssl_printf(ssl, "error out of memory\n");
1708 parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp,
1709 int* insecure, int* prime)
1715 /* parse all -x args */
1716 while(args[0] == '+') {
1717 if(!find_arg2(ssl, args, &rest))
1719 while(*(++args) != 0) {
1720 if(*args == 'i' && insecure)
1722 else if(*args == 'p' && prime)
1725 (void)ssl_printf(ssl, "error: unknown option %s\n", args);
1733 if(!find_arg2(ssl, args, &rest))
1737 } else zonename = args;
1738 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
1743 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
1751 /** do the forward_add command */
1753 do_forward_add(SSL* ssl, struct worker* worker, char* args)
1755 struct iter_forwards* fwd = worker->env.fwds;
1758 struct delegpt* dp = NULL;
1759 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
1761 if(insecure && worker->env.anchors) {
1762 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1764 (void)ssl_printf(ssl, "error out of memory\n");
1765 delegpt_free_mlc(dp);
1770 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
1771 (void)ssl_printf(ssl, "error out of memory\n");
1779 /** do the forward_remove command */
1781 do_forward_remove(SSL* ssl, struct worker* worker, char* args)
1783 struct iter_forwards* fwd = worker->env.fwds;
1786 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1788 if(insecure && worker->env.anchors)
1789 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1791 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
1796 /** do the stub_add command */
1798 do_stub_add(SSL* ssl, struct worker* worker, char* args)
1800 struct iter_forwards* fwd = worker->env.fwds;
1801 int insecure = 0, prime = 0;
1803 struct delegpt* dp = NULL;
1804 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
1806 if(insecure && worker->env.anchors) {
1807 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1809 (void)ssl_printf(ssl, "error out of memory\n");
1810 delegpt_free_mlc(dp);
1815 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
1816 if(insecure && worker->env.anchors)
1817 anchors_delete_insecure(worker->env.anchors,
1818 LDNS_RR_CLASS_IN, nm);
1819 (void)ssl_printf(ssl, "error out of memory\n");
1820 delegpt_free_mlc(dp);
1824 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
1825 (void)ssl_printf(ssl, "error out of memory\n");
1826 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1827 if(insecure && worker->env.anchors)
1828 anchors_delete_insecure(worker->env.anchors,
1829 LDNS_RR_CLASS_IN, nm);
1837 /** do the stub_remove command */
1839 do_stub_remove(SSL* ssl, struct worker* worker, char* args)
1841 struct iter_forwards* fwd = worker->env.fwds;
1844 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
1846 if(insecure && worker->env.anchors)
1847 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
1849 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
1850 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
1855 /** do the insecure_add command */
1857 do_insecure_add(SSL* ssl, struct worker* worker, char* arg)
1862 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1864 if(worker->env.anchors) {
1865 if(!anchors_add_insecure(worker->env.anchors,
1866 LDNS_RR_CLASS_IN, nm)) {
1867 (void)ssl_printf(ssl, "error out of memory\n");
1876 /** do the insecure_remove command */
1878 do_insecure_remove(SSL* ssl, struct worker* worker, char* arg)
1883 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1885 if(worker->env.anchors)
1886 anchors_delete_insecure(worker->env.anchors,
1887 LDNS_RR_CLASS_IN, nm);
1892 /** do the status command */
1894 do_status(SSL* ssl, struct worker* worker)
1898 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
1900 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
1902 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
1904 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
1906 for(i=0; i<worker->daemon->mods.num; i++) {
1907 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
1910 if(!ssl_printf(ssl, " ]\n"))
1912 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
1913 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
1915 if(!ssl_printf(ssl, "options:%s%s\n" ,
1916 (worker->daemon->reuseport?" reuseport":""),
1917 (worker->daemon->rc->accept_list?" control(ssl)":"")))
1919 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
1924 /** get age for the mesh state */
1926 get_mesh_age(struct mesh_state* m, char* buf, size_t len,
1927 struct module_env* env)
1931 struct mesh_reply* r = m->reply_list;
1932 /* last reply is the oldest */
1935 timeval_subtract(&d, env->now_tv, &r->start_time);
1936 snprintf(buf, len, ARG_LL "d.%6.6d",
1937 (long long)d.tv_sec, (int)d.tv_usec);
1939 snprintf(buf, len, "-");
1943 /** get status of a mesh state */
1945 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
1946 char* buf, size_t len)
1948 enum module_ext_state s = m->s.ext_state[m->s.curmod];
1949 const char *modname = mesh->mods.mod[m->s.curmod]->name;
1951 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
1952 m->s.minfo[m->s.curmod]) {
1953 /* break into iterator to find out who its waiting for */
1954 struct iter_qstate* qstate = (struct iter_qstate*)
1955 m->s.minfo[m->s.curmod];
1956 struct outbound_list* ol = &qstate->outlist;
1957 struct outbound_entry* e;
1958 snprintf(buf, len, "%s wait for", modname);
1961 if(ol->first == NULL)
1962 snprintf(buf, len, " (empty_list)");
1963 for(e = ol->first; e; e = e->next) {
1964 snprintf(buf, len, " ");
1967 addr_to_str(&e->qsent->addr, e->qsent->addrlen,
1972 } else if(s == module_wait_subquery) {
1973 /* look in subs from mesh state to see what */
1975 struct mesh_state_ref* sub;
1976 snprintf(buf, len, "%s wants", modname);
1979 if(m->sub_set.count == 0)
1980 snprintf(buf, len, " (empty_list)");
1981 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
1982 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
1983 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
1984 dname_str(sub->s->s.qinfo.qname, nm);
1985 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
1986 (c?c:"CLASS??"), nm);
1993 snprintf(buf, len, "%s is %s", modname, strextstate(s));
1997 /** do the dump_requestlist command */
1999 do_dump_requestlist(SSL* ssl, struct worker* worker)
2001 struct mesh_area* mesh;
2002 struct mesh_state* m;
2006 char statbuf[10240];
2007 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
2009 if(!ssl_printf(ssl, "# type cl name seconds module status\n"))
2011 /* show worker mesh contents */
2012 mesh = worker->env.mesh;
2014 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
2015 char* t = sldns_wire2str_type(m->s.qinfo.qtype);
2016 char* c = sldns_wire2str_class(m->s.qinfo.qclass);
2017 dname_str(m->s.qinfo.qname, buf);
2018 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
2019 get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
2020 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
2021 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
2033 /** structure for argument data for dump infra host */
2035 /** the infra cache */
2036 struct infra_cache* infra;
2037 /** the SSL connection */
2041 /** ssl failure? stop writing and skip the rest. If the tcp
2042 * connection is broken, and writes fail, we then stop writing. */
2046 /** callback for every host element in the infra cache */
2048 dump_infra_host(struct lruhash_entry* e, void* arg)
2050 struct infra_arg* a = (struct infra_arg*)arg;
2051 struct infra_key* k = (struct infra_key*)e->key;
2052 struct infra_data* d = (struct infra_data*)e->data;
2057 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
2058 dname_str(k->zonename, name);
2059 /* skip expired stuff (only backed off) */
2060 if(d->ttl < a->now) {
2061 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
2062 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
2063 name, d->rtt.rto)) {
2070 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
2071 "tA %d tAAAA %d tother %d "
2072 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
2073 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now),
2074 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
2075 d->timeout_A, d->timeout_AAAA, d->timeout_other,
2076 (int)d->edns_lame_known, (int)d->edns_version,
2077 (int)(a->now<d->probedelay?d->probedelay-a->now:0),
2078 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
2079 (int)d->lame_other)) {
2085 /** do the dump_infra command */
2087 do_dump_infra(SSL* ssl, struct worker* worker)
2089 struct infra_arg arg;
2090 arg.infra = worker->env.infra_cache;
2092 arg.now = *worker->env.now;
2094 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
2097 /** do the log_reopen command */
2099 do_log_reopen(SSL* ssl, struct worker* worker)
2101 struct config_file* cfg = worker->env.cfg;
2103 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
2106 /** do the set_option command */
2108 do_set_option(SSL* ssl, struct worker* worker, char* arg)
2111 if(!find_arg2(ssl, arg, &arg2))
2113 if(!config_set_option(worker->env.cfg, arg, arg2)) {
2114 (void)ssl_printf(ssl, "error setting option\n");
2120 /* routine to printout option values over SSL */
2121 void remote_get_opt_ssl(char* line, void* arg)
2123 SSL* ssl = (SSL*)arg;
2124 (void)ssl_printf(ssl, "%s\n", line);
2127 /** do the get_option command */
2129 do_get_option(SSL* ssl, struct worker* worker, char* arg)
2132 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
2134 (void)ssl_printf(ssl, "error unknown option\n");
2139 /** do the list_forwards command */
2141 do_list_forwards(SSL* ssl, struct worker* worker)
2143 /* since its a per-worker structure no locks needed */
2144 struct iter_forwards* fwds = worker->env.fwds;
2145 struct iter_forward_zone* z;
2146 struct trust_anchor* a;
2148 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
2149 if(!z->dp) continue; /* skip empty marker for stub */
2151 /* see if it is insecure */
2153 if(worker->env.anchors &&
2154 (a=anchor_find(worker->env.anchors, z->name,
2155 z->namelabs, z->namelen, z->dclass))) {
2156 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2158 lock_basic_unlock(&a->lock);
2161 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"),
2162 z->name, z->dclass, z->dp))
2167 /** do the list_stubs command */
2169 do_list_stubs(SSL* ssl, struct worker* worker)
2171 struct iter_hints_stub* z;
2172 struct trust_anchor* a;
2175 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
2177 /* see if it is insecure */
2179 if(worker->env.anchors &&
2180 (a=anchor_find(worker->env.anchors, z->node.name,
2181 z->node.labs, z->node.len, z->node.dclass))) {
2182 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2184 lock_basic_unlock(&a->lock);
2187 snprintf(str, sizeof(str), "stub %sprime%s",
2188 (z->noprime?"no":""), (insecure?" +i":""));
2189 if(!ssl_print_name_dp(ssl, str, z->node.name,
2190 z->node.dclass, z->dp))
2195 /** do the list_local_zones command */
2197 do_list_local_zones(SSL* ssl, struct worker* worker)
2199 struct local_zones* zones = worker->daemon->local_zones;
2200 struct local_zone* z;
2202 lock_rw_rdlock(&zones->lock);
2203 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2204 lock_rw_rdlock(&z->lock);
2205 dname_str(z->name, buf);
2206 if(!ssl_printf(ssl, "%s %s\n", buf,
2207 local_zone_type2str(z->type))) {
2208 /* failure to print */
2209 lock_rw_unlock(&z->lock);
2210 lock_rw_unlock(&zones->lock);
2213 lock_rw_unlock(&z->lock);
2215 lock_rw_unlock(&zones->lock);
2218 /** do the list_local_data command */
2220 do_list_local_data(SSL* ssl, struct worker* worker)
2222 struct local_zones* zones = worker->daemon->local_zones;
2223 struct local_zone* z;
2224 struct local_data* d;
2225 struct local_rrset* p;
2226 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
2227 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
2228 lock_rw_rdlock(&zones->lock);
2229 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2230 lock_rw_rdlock(&z->lock);
2231 RBTREE_FOR(d, struct local_data*, &z->data) {
2232 for(p = d->rrsets; p; p = p->next) {
2233 struct packed_rrset_data* d =
2234 (struct packed_rrset_data*)p->rrset->entry.data;
2236 for(i=0; i<d->count + d->rrsig_count; i++) {
2237 if(!packed_rr_to_string(p->rrset, i,
2239 if(!ssl_printf(ssl, "BADRR\n"))
2242 if(!ssl_printf(ssl, "%s\n", s))
2247 lock_rw_unlock(&z->lock);
2249 lock_rw_unlock(&zones->lock);
2252 /** tell other processes to execute the command */
2254 distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd)
2259 /* skip i=0 which is me */
2260 for(i=1; i<rc->worker->daemon->num; i++) {
2261 worker_send_cmd(rc->worker->daemon->workers[i],
2263 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2264 (uint8_t*)cmd, strlen(cmd)+1, 0)) {
2265 ssl_printf(ssl, "error could not distribute cmd\n");
2271 /** check for name with end-of-string, space or tab after it */
2273 cmdcmp(char* p, const char* cmd, size_t len)
2275 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2278 /** execute a remote control command */
2280 execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd,
2281 struct worker* worker)
2283 char* p = skipwhite(cmd);
2284 /* compare command */
2285 if(cmdcmp(p, "stop", 4)) {
2288 } else if(cmdcmp(p, "reload", 6)) {
2291 } else if(cmdcmp(p, "stats_noreset", 13)) {
2292 do_stats(ssl, rc, 0);
2294 } else if(cmdcmp(p, "stats", 5)) {
2295 do_stats(ssl, rc, 1);
2297 } else if(cmdcmp(p, "status", 6)) {
2298 do_status(ssl, worker);
2300 } else if(cmdcmp(p, "dump_cache", 10)) {
2301 (void)dump_cache(ssl, worker);
2303 } else if(cmdcmp(p, "load_cache", 10)) {
2304 if(load_cache(ssl, worker)) send_ok(ssl);
2306 } else if(cmdcmp(p, "list_forwards", 13)) {
2307 do_list_forwards(ssl, worker);
2309 } else if(cmdcmp(p, "list_stubs", 10)) {
2310 do_list_stubs(ssl, worker);
2312 } else if(cmdcmp(p, "list_local_zones", 16)) {
2313 do_list_local_zones(ssl, worker);
2315 } else if(cmdcmp(p, "list_local_data", 15)) {
2316 do_list_local_data(ssl, worker);
2318 } else if(cmdcmp(p, "stub_add", 8)) {
2319 /* must always distribute this cmd */
2320 if(rc) distribute_cmd(rc, ssl, cmd);
2321 do_stub_add(ssl, worker, skipwhite(p+8));
2323 } else if(cmdcmp(p, "stub_remove", 11)) {
2324 /* must always distribute this cmd */
2325 if(rc) distribute_cmd(rc, ssl, cmd);
2326 do_stub_remove(ssl, worker, skipwhite(p+11));
2328 } else if(cmdcmp(p, "forward_add", 11)) {
2329 /* must always distribute this cmd */
2330 if(rc) distribute_cmd(rc, ssl, cmd);
2331 do_forward_add(ssl, worker, skipwhite(p+11));
2333 } else if(cmdcmp(p, "forward_remove", 14)) {
2334 /* must always distribute this cmd */
2335 if(rc) distribute_cmd(rc, ssl, cmd);
2336 do_forward_remove(ssl, worker, skipwhite(p+14));
2338 } else if(cmdcmp(p, "insecure_add", 12)) {
2339 /* must always distribute this cmd */
2340 if(rc) distribute_cmd(rc, ssl, cmd);
2341 do_insecure_add(ssl, worker, skipwhite(p+12));
2343 } else if(cmdcmp(p, "insecure_remove", 15)) {
2344 /* must always distribute this cmd */
2345 if(rc) distribute_cmd(rc, ssl, cmd);
2346 do_insecure_remove(ssl, worker, skipwhite(p+15));
2348 } else if(cmdcmp(p, "forward", 7)) {
2349 /* must always distribute this cmd */
2350 if(rc) distribute_cmd(rc, ssl, cmd);
2351 do_forward(ssl, worker, skipwhite(p+7));
2353 } else if(cmdcmp(p, "flush_stats", 11)) {
2354 /* must always distribute this cmd */
2355 if(rc) distribute_cmd(rc, ssl, cmd);
2356 do_flush_stats(ssl, worker);
2358 } else if(cmdcmp(p, "flush_requestlist", 17)) {
2359 /* must always distribute this cmd */
2360 if(rc) distribute_cmd(rc, ssl, cmd);
2361 do_flush_requestlist(ssl, worker);
2363 } else if(cmdcmp(p, "lookup", 6)) {
2364 do_lookup(ssl, worker, skipwhite(p+6));
2368 #ifdef THREADS_DISABLED
2369 /* other processes must execute the command as well */
2370 /* commands that should not be distributed, returned above. */
2371 if(rc) { /* only if this thread is the master (rc) thread */
2372 /* done before the code below, which may split the string */
2373 distribute_cmd(rc, ssl, cmd);
2376 if(cmdcmp(p, "verbosity", 9)) {
2377 do_verbosity(ssl, skipwhite(p+9));
2378 } else if(cmdcmp(p, "local_zone_remove", 17)) {
2379 do_zone_remove(ssl, worker, skipwhite(p+17));
2380 } else if(cmdcmp(p, "local_zone", 10)) {
2381 do_zone_add(ssl, worker, skipwhite(p+10));
2382 } else if(cmdcmp(p, "local_data_remove", 17)) {
2383 do_data_remove(ssl, worker, skipwhite(p+17));
2384 } else if(cmdcmp(p, "local_data", 10)) {
2385 do_data_add(ssl, worker, skipwhite(p+10));
2386 } else if(cmdcmp(p, "flush_zone", 10)) {
2387 do_flush_zone(ssl, worker, skipwhite(p+10));
2388 } else if(cmdcmp(p, "flush_type", 10)) {
2389 do_flush_type(ssl, worker, skipwhite(p+10));
2390 } else if(cmdcmp(p, "flush_infra", 11)) {
2391 do_flush_infra(ssl, worker, skipwhite(p+11));
2392 } else if(cmdcmp(p, "flush", 5)) {
2393 do_flush_name(ssl, worker, skipwhite(p+5));
2394 } else if(cmdcmp(p, "dump_requestlist", 16)) {
2395 do_dump_requestlist(ssl, worker);
2396 } else if(cmdcmp(p, "dump_infra", 10)) {
2397 do_dump_infra(ssl, worker);
2398 } else if(cmdcmp(p, "log_reopen", 10)) {
2399 do_log_reopen(ssl, worker);
2400 } else if(cmdcmp(p, "set_option", 10)) {
2401 do_set_option(ssl, worker, skipwhite(p+10));
2402 } else if(cmdcmp(p, "get_option", 10)) {
2403 do_get_option(ssl, worker, skipwhite(p+10));
2404 } else if(cmdcmp(p, "flush_bogus", 11)) {
2405 do_flush_bogus(ssl, worker);
2406 } else if(cmdcmp(p, "flush_negative", 14)) {
2407 do_flush_negative(ssl, worker);
2409 (void)ssl_printf(ssl, "error unknown command '%s'\n", p);
2414 daemon_remote_exec(struct worker* worker)
2416 /* read the cmd string */
2417 uint8_t* msg = NULL;
2419 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
2420 log_err("daemon_remote_exec: tube_read_msg failed");
2423 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
2424 execute_cmd(NULL, NULL, (char*)msg, worker);
2428 /** handle remote control request */
2430 handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl)
2437 /* makes it possible to set the socket blocking again. */
2438 /* basically removes it from winsock_event ... */
2439 WSAEventSelect(s->c->fd, NULL, 0);
2441 fd_set_block(s->c->fd);
2443 /* try to read magic UBCT[version]_space_ string */
2445 if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) {
2446 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN)
2448 log_crypto_err("could not SSL_read");
2452 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
2453 verbose(VERB_QUERY, "control connection has bad magic string");
2454 /* probably wrong tool connected, ignore it completely */
2458 /* read the command line */
2459 if(!ssl_read_line(ssl, buf, sizeof(buf))) {
2462 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
2463 if(strcmp(magic, pre) != 0) {
2464 verbose(VERB_QUERY, "control connection had bad "
2465 "version %s, cmd: %s", magic, buf);
2466 ssl_printf(ssl, "error version mismatch\n");
2469 verbose(VERB_DETAIL, "control cmd: %s", buf);
2471 /* figure out what to do */
2472 execute_cmd(rc, ssl, buf, rc->worker);
2475 int remote_control_callback(struct comm_point* c, void* arg, int err,
2476 struct comm_reply* ATTR_UNUSED(rep))
2478 struct rc_state* s = (struct rc_state*)arg;
2479 struct daemon_remote* rc = s->rc;
2481 if(err != NETEVENT_NOERROR) {
2482 if(err==NETEVENT_TIMEOUT)
2483 log_err("remote control timed out");
2487 /* (continue to) setup the SSL connection */
2489 r = SSL_do_handshake(s->ssl);
2491 int r2 = SSL_get_error(s->ssl, r);
2492 if(r2 == SSL_ERROR_WANT_READ) {
2493 if(s->shake_state == rc_hs_read) {
2494 /* try again later */
2497 s->shake_state = rc_hs_read;
2498 comm_point_listen_for_rw(c, 1, 0);
2500 } else if(r2 == SSL_ERROR_WANT_WRITE) {
2501 if(s->shake_state == rc_hs_write) {
2502 /* try again later */
2505 s->shake_state = rc_hs_write;
2506 comm_point_listen_for_rw(c, 0, 1);
2510 log_err("remote control connection closed prematurely");
2511 log_addr(1, "failed connection from",
2512 &s->c->repinfo.addr, s->c->repinfo.addrlen);
2513 log_crypto_err("remote control failed ssl");
2518 s->shake_state = rc_none;
2520 /* once handshake has completed, check authentication */
2521 if (!rc->use_cert) {
2522 verbose(VERB_ALGO, "unauthenticated remote control connection");
2523 } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
2524 X509* x = SSL_get_peer_certificate(s->ssl);
2526 verbose(VERB_DETAIL, "remote control connection "
2527 "provided no client certificate");
2531 verbose(VERB_ALGO, "remote control connection authenticated");
2534 verbose(VERB_DETAIL, "remote control connection failed to "
2535 "authenticate with client certificate");
2540 /* if OK start to actually handle the request */
2541 handle_req(rc, s, s->ssl);
2543 verbose(VERB_ALGO, "remote control operation completed");