2 * daemon/remote.c - remote control for the unbound daemon.
4 * Copyright (c) 2008, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains the remote control functionality for the daemon.
40 * The remote control can be performed using either the commandline
41 * unbound-control tool, or a TLS capable web browser.
42 * The channel is secured using TLSv1, and certificates.
43 * Both the server and the client(control tool) have their own keys.
46 #ifdef HAVE_OPENSSL_ERR_H
47 #include <openssl/err.h>
49 #ifdef HAVE_OPENSSL_DH_H
50 #include <openssl/dh.h>
52 #ifdef HAVE_OPENSSL_BN_H
53 #include <openssl/bn.h>
57 #include "daemon/remote.h"
58 #include "daemon/worker.h"
59 #include "daemon/daemon.h"
60 #include "daemon/stats.h"
61 #include "daemon/cachedump.h"
63 #include "util/config_file.h"
64 #include "util/net_help.h"
65 #include "util/module.h"
66 #include "services/listen_dnsport.h"
67 #include "services/cache/rrset.h"
68 #include "services/cache/infra.h"
69 #include "services/mesh.h"
70 #include "services/localzone.h"
71 #include "services/authzone.h"
72 #include "services/rpz.h"
73 #include "util/storage/slabhash.h"
74 #include "util/fptr_wlist.h"
75 #include "util/data/dname.h"
76 #include "validator/validator.h"
77 #include "validator/val_kcache.h"
78 #include "validator/val_kentry.h"
79 #include "validator/val_anchor.h"
80 #include "iterator/iterator.h"
81 #include "iterator/iter_fwd.h"
82 #include "iterator/iter_hints.h"
83 #include "iterator/iter_delegpt.h"
84 #include "services/outbound_list.h"
85 #include "services/outside_network.h"
86 #include "sldns/str2wire.h"
87 #include "sldns/parseutil.h"
88 #include "sldns/wire2str.h"
89 #include "sldns/sbuffer.h"
91 #ifdef HAVE_SYS_TYPES_H
92 # include <sys/types.h>
94 #ifdef HAVE_SYS_STAT_H
101 /* just for portability */
106 /** what to put on statistics lines between var and value, ": " or "=" */
108 /** if true, inhibits a lot of =0 lines from the stats output */
109 static const int inhibit_zero = 1;
111 /** subtract timers and the values do not overflow or become negative */
113 timeval_subtract(struct timeval* d, const struct timeval* end,
114 const struct timeval* start)
117 time_t end_usec = end->tv_usec;
118 d->tv_sec = end->tv_sec - start->tv_sec;
119 if(end_usec < start->tv_usec) {
123 d->tv_usec = end_usec - start->tv_usec;
127 /** divide sum of timers to get average */
129 timeval_divide(struct timeval* avg, const struct timeval* sum, long long d)
138 avg->tv_sec = sum->tv_sec / d;
139 avg->tv_usec = sum->tv_usec / d;
140 /* handle fraction from seconds divide */
141 leftover = sum->tv_sec - avg->tv_sec*d;
142 avg->tv_usec += (leftover*1000000)/d;
147 remote_setup_ctx(struct daemon_remote* rc, struct config_file* cfg)
151 rc->ctx = SSL_CTX_new(SSLv23_server_method());
153 log_crypto_err("could not SSL_CTX_new");
156 if(!listen_sslctx_setup(rc->ctx)) {
160 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
161 s_key = fname_after_chroot(cfg->server_key_file, cfg, 1);
162 if(!s_cert || !s_key) {
163 log_err("out of memory in remote control fname");
166 verbose(VERB_ALGO, "setup SSL certificates");
167 if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) {
168 log_err("Error for server-cert-file: %s", s_cert);
169 log_crypto_err("Error in SSL_CTX use_certificate_chain_file");
172 if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) {
173 log_err("Error for server-key-file: %s", s_key);
174 log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
177 if(!SSL_CTX_check_private_key(rc->ctx)) {
178 log_err("Error for server-key-file: %s", s_key);
179 log_crypto_err("Error in SSL_CTX check_private_key");
182 listen_sslctx_setup_2(rc->ctx);
183 if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) {
184 log_crypto_err("Error setting up SSL_CTX verify locations");
190 SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert));
191 SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL);
197 struct daemon_remote*
198 daemon_remote_create(struct config_file* cfg)
200 struct daemon_remote* rc = (struct daemon_remote*)calloc(1,
203 log_err("out of memory in daemon_remote_create");
208 if(!cfg->remote_control_enable) {
212 if(options_remote_is_address(cfg) && cfg->control_use_cert) {
213 if(!remote_setup_ctx(rc, cfg)) {
214 daemon_remote_delete(rc);
219 struct config_strlist* p;
222 if(!options_remote_is_address(cfg))
223 for(p = cfg->control_ifs.first; p; p = p->next) {
224 if(p->str && p->str[0] != '/')
225 log_warn("control-interface %s is not using TLS, but plain transfer, because first control-interface in config file is a local socket (starts with a /).", p->str);
231 void daemon_remote_clear(struct daemon_remote* rc)
233 struct rc_state* p, *np;
235 /* but do not close the ports */
236 listen_list_delete(rc->accept_list);
237 rc->accept_list = NULL;
238 /* do close these sockets */
244 comm_point_delete(p->c);
248 rc->busy_list = NULL;
253 void daemon_remote_delete(struct daemon_remote* rc)
256 daemon_remote_clear(rc);
258 SSL_CTX_free(rc->ctx);
264 * Add and open a new control port
267 * @param list: list head
268 * @param noproto_is_err: if lack of protocol support is an error.
269 * @param cfg: config with username for chown of unix-sockets.
270 * @return false on failure.
273 add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err,
274 struct config_file* cfg)
276 struct addrinfo hints;
277 struct addrinfo* res;
278 struct listen_port* n;
282 snprintf(port, sizeof(port), "%d", nr);
283 port[sizeof(port)-1]=0;
284 memset(&hints, 0, sizeof(hints));
288 /* This looks like a local socket */
289 fd = create_local_accept_sock(ip, &noproto, cfg->use_systemd);
291 * Change socket ownership and permissions so users other
292 * than root can access it provided they are in the same
293 * group as the user we run as.
297 if (cfg->username && cfg->username[0] &&
298 cfg_uid != (uid_t)-1) {
299 if(chown(ip, cfg_uid, cfg_gid) == -1)
300 verbose(VERB_QUERY, "cannot chown %u.%u %s: %s",
301 (unsigned)cfg_uid, (unsigned)cfg_gid,
302 ip, strerror(errno));
304 chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
310 hints.ai_socktype = SOCK_STREAM;
311 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
312 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
314 if(!noproto_is_err && r == EAI_NONAME) {
315 /* tried to lookup the address as name */
316 return 1; /* return success, but do nothing */
318 #endif /* USE_WINSOCK */
319 log_err("control interface %s:%s getaddrinfo: %s %s",
320 ip?ip:"default", port, gai_strerror(r),
322 r==EAI_SYSTEM?(char*)strerror(errno):""
331 fd = create_tcp_accept_sock(res, 1, &noproto, 0,
332 cfg->ip_transparent, 0, cfg->ip_freebind, cfg->use_systemd, cfg->ip_dscp);
336 if(fd == -1 && noproto) {
338 return 1; /* return success, but do nothing */
339 log_err("cannot open control interface %s %d : "
340 "protocol not supported", ip, nr);
344 log_err("cannot open control interface %s %d", ip, nr);
349 n = (struct listen_port*)calloc(1, sizeof(*n));
356 log_err("out of memory");
365 struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
367 struct listen_port* l = NULL;
368 log_assert(cfg->remote_control_enable && cfg->control_port);
369 if(cfg->control_ifs.first) {
370 struct config_strlist* p;
371 for(p = cfg->control_ifs.first; p; p = p->next) {
372 if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) {
373 listening_ports_free(l);
380 !add_open("::1", cfg->control_port, &l, 0, cfg)) {
381 listening_ports_free(l);
385 !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) {
386 listening_ports_free(l);
393 /** open accept commpoint */
395 accept_open(struct daemon_remote* rc, int fd)
397 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
399 log_err("out of memory");
402 n->next = rc->accept_list;
405 n->com = comm_point_create_raw(rc->worker->base, fd, 0,
406 &remote_accept_callback, rc);
409 /* keep this port open, its fd is kept in the rc portlist */
410 n->com->do_not_close = 1;
414 int daemon_remote_open_accept(struct daemon_remote* rc,
415 struct listen_port* ports, struct worker* worker)
417 struct listen_port* p;
419 for(p = ports; p; p = p->next) {
420 if(!accept_open(rc, p->fd)) {
421 log_err("could not create accept comm point");
428 void daemon_remote_stop_accept(struct daemon_remote* rc)
430 struct listen_list* p;
431 for(p=rc->accept_list; p; p=p->next) {
432 comm_point_stop_listening(p->com);
436 void daemon_remote_start_accept(struct daemon_remote* rc)
438 struct listen_list* p;
439 for(p=rc->accept_list; p; p=p->next) {
440 comm_point_start_listening(p->com, -1, -1);
444 int remote_accept_callback(struct comm_point* c, void* arg, int err,
445 struct comm_reply* ATTR_UNUSED(rep))
447 struct daemon_remote* rc = (struct daemon_remote*)arg;
448 struct sockaddr_storage addr;
452 if(err != NETEVENT_NOERROR) {
453 log_err("error %d on remote_accept_callback", err);
456 /* perform the accept */
457 newfd = comm_point_perform_accept(c, &addr, &addrlen);
460 /* create new commpoint unless we are servicing already */
461 if(rc->active >= rc->max_active) {
462 log_warn("drop incoming remote control: too many connections");
472 /* setup commpoint to service the remote control command */
473 n = (struct rc_state*)calloc(1, sizeof(*n));
475 log_err("out of memory");
479 /* start in reading state */
480 n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
481 &remote_control_callback, n);
483 log_err("out of memory");
487 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
488 n->c->do_not_close = 0;
489 comm_point_stop_listening(n->c);
490 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
491 memcpy(&n->c->repinfo.addr, &addr, addrlen);
492 n->c->repinfo.addrlen = addrlen;
494 n->shake_state = rc_hs_read;
495 n->ssl = SSL_new(rc->ctx);
497 log_crypto_err("could not SSL_new");
498 comm_point_delete(n->c);
502 SSL_set_accept_state(n->ssl);
503 (void)SSL_set_mode(n->ssl, (long)SSL_MODE_AUTO_RETRY);
504 if(!SSL_set_fd(n->ssl, newfd)) {
505 log_crypto_err("could not SSL_set_fd");
507 comm_point_delete(n->c);
516 n->next = rc->busy_list;
520 /* perform the first nonblocking read already, for windows,
521 * so it can return wouldblock. could be faster too. */
522 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
526 /** delete from list */
528 state_list_remove_elem(struct rc_state** list, struct comm_point* c)
531 if( (*list)->c == c) {
532 *list = (*list)->next;
535 list = &(*list)->next;
539 /** decrease active count and remove commpoint from busy list */
541 clean_point(struct daemon_remote* rc, struct rc_state* s)
543 state_list_remove_elem(&rc->busy_list, s->c);
546 SSL_shutdown(s->ssl);
549 comm_point_delete(s->c);
554 ssl_print_text(RES* res, const char* text)
561 if((r=SSL_write(res->ssl, text, (int)strlen(text))) <= 0) {
562 if(SSL_get_error(res->ssl, r) == SSL_ERROR_ZERO_RETURN) {
563 verbose(VERB_QUERY, "warning, in SSL_write, peer "
564 "closed connection");
567 log_crypto_err("could not SSL_write");
572 while(at < strlen(text)) {
573 ssize_t r = send(res->fd, text+at, strlen(text)-at, 0);
575 if(errno == EAGAIN || errno == EINTR)
578 log_err("could not send: %s", strerror(errno));
580 log_err("could not send: %s", wsa_strerror(WSAGetLastError()));
590 /** print text over the ssl connection */
592 ssl_print_vmsg(RES* ssl, const char* format, va_list args)
595 vsnprintf(msg, sizeof(msg), format, args);
596 return ssl_print_text(ssl, msg);
599 /** printf style printing to the ssl connection */
600 int ssl_printf(RES* ssl, const char* format, ...)
604 va_start(args, format);
605 ret = ssl_print_vmsg(ssl, format, args);
611 ssl_read_line(RES* res, char* buf, size_t max)
620 if((r=SSL_read(res->ssl, buf+len, 1)) <= 0) {
621 if(SSL_get_error(res->ssl, r) == SSL_ERROR_ZERO_RETURN) {
625 log_crypto_err("could not SSL_read");
630 ssize_t rr = recv(res->fd, buf+len, 1, 0);
636 if(errno == EINTR || errno == EAGAIN)
639 log_err("could not recv: %s", strerror(errno));
641 log_err("could not recv: %s", wsa_strerror(WSAGetLastError()));
648 if(buf[len] == '\n') {
649 /* return string without \n */
656 log_err("control line too long (%d): %s", (int)max, buf);
660 /** skip whitespace, return new pointer into string */
664 /* EOS \0 is not a space */
665 while( isspace((unsigned char)*str) )
670 /** send the OK to the control client */
671 static void send_ok(RES* ssl)
673 (void)ssl_printf(ssl, "ok\n");
676 /** do the stop command */
678 do_stop(RES* ssl, struct worker* worker)
680 worker->need_to_exit = 1;
681 comm_base_exit(worker->base);
685 /** do the reload command */
687 do_reload(RES* ssl, struct worker* worker)
689 worker->need_to_exit = 0;
690 comm_base_exit(worker->base);
694 /** do the verbosity command */
696 do_verbosity(RES* ssl, char* str)
699 if(val == 0 && strcmp(str, "0") != 0) {
700 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
707 /** print stats from statinfo */
709 print_stats(RES* ssl, const char* nm, struct ub_stats_info* s)
711 struct timeval sumwait, avg;
712 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm,
713 (unsigned long)s->svr.num_queries)) return 0;
714 if(!ssl_printf(ssl, "%s.num.queries_ip_ratelimited"SQ"%lu\n", nm,
715 (unsigned long)s->svr.num_queries_ip_ratelimited)) return 0;
716 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm,
717 (unsigned long)(s->svr.num_queries
718 - s->svr.num_queries_missed_cache))) return 0;
719 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm,
720 (unsigned long)s->svr.num_queries_missed_cache)) return 0;
721 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm,
722 (unsigned long)s->svr.num_queries_prefetch)) return 0;
723 if(!ssl_printf(ssl, "%s.num.expired"SQ"%lu\n", nm,
724 (unsigned long)s->svr.ans_expired)) return 0;
725 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm,
726 (unsigned long)s->mesh_replies_sent)) return 0;
728 if(!ssl_printf(ssl, "%s.num.dnscrypt.crypted"SQ"%lu\n", nm,
729 (unsigned long)s->svr.num_query_dnscrypt_crypted)) return 0;
730 if(!ssl_printf(ssl, "%s.num.dnscrypt.cert"SQ"%lu\n", nm,
731 (unsigned long)s->svr.num_query_dnscrypt_cert)) return 0;
732 if(!ssl_printf(ssl, "%s.num.dnscrypt.cleartext"SQ"%lu\n", nm,
733 (unsigned long)s->svr.num_query_dnscrypt_cleartext)) return 0;
734 if(!ssl_printf(ssl, "%s.num.dnscrypt.malformed"SQ"%lu\n", nm,
735 (unsigned long)s->svr.num_query_dnscrypt_crypted_malformed)) return 0;
737 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
738 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
739 (double)s->svr.sum_query_list_size/
740 (double)(s->svr.num_queries_missed_cache+
741 s->svr.num_queries_prefetch) : 0.0)) return 0;
742 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm,
743 (unsigned long)s->svr.max_query_list_size)) return 0;
744 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm,
745 (unsigned long)s->mesh_jostled)) return 0;
746 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm,
747 (unsigned long)s->mesh_dropped)) return 0;
748 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm,
749 (unsigned long)s->mesh_num_states)) return 0;
750 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm,
751 (unsigned long)s->mesh_num_reply_states)) return 0;
753 sumwait.tv_sec = s->mesh_replies_sum_wait_sec;
754 sumwait.tv_usec = s->mesh_replies_sum_wait_usec;
756 timeval_divide(&avg, &sumwait, s->mesh_replies_sent);
757 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
758 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
759 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
760 s->mesh_time_median)) return 0;
761 if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm,
762 (unsigned long)s->svr.tcp_accept_usage)) return 0;
766 /** print stats for one thread */
768 print_thread_stats(RES* ssl, int i, struct ub_stats_info* s)
771 snprintf(nm, sizeof(nm), "thread%d", i);
773 return print_stats(ssl, nm, s);
776 /** print long number */
778 print_longnum(RES* ssl, const char* desc, size_t x)
780 if(x > 1024*1024*1024) {
782 size_t front = x / (size_t)1000000;
783 size_t back = x % (size_t)1000000;
784 return ssl_printf(ssl, "%s%u%6.6u\n", desc,
785 (unsigned)front, (unsigned)back);
787 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x);
791 /** print mem stats */
793 print_mem(RES* ssl, struct worker* worker, struct daemon* daemon,
794 struct ub_stats_info* s)
796 size_t msg, rrset, val, iter, respip;
799 #endif /* CLIENT_SUBNET */
802 #endif /* USE_IPSECMOD */
804 size_t dnscrypt_shared_secret = 0;
805 size_t dnscrypt_nonce = 0;
806 #endif /* USE_DNSCRYPT */
807 #ifdef WITH_DYNLIBMODULE
809 #endif /* WITH_DYNLIBMODULE */
810 msg = slabhash_get_mem(daemon->env->msg_cache);
811 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
812 val = mod_get_mem(&worker->env, "validator");
813 iter = mod_get_mem(&worker->env, "iterator");
814 respip = mod_get_mem(&worker->env, "respip");
816 subnet = mod_get_mem(&worker->env, "subnet");
817 #endif /* CLIENT_SUBNET */
819 ipsecmod = mod_get_mem(&worker->env, "ipsecmod");
820 #endif /* USE_IPSECMOD */
822 if(daemon->dnscenv) {
823 dnscrypt_shared_secret = slabhash_get_mem(
824 daemon->dnscenv->shared_secrets_cache);
825 dnscrypt_nonce = slabhash_get_mem(daemon->dnscenv->nonces_cache);
827 #endif /* USE_DNSCRYPT */
828 #ifdef WITH_DYNLIBMODULE
829 dynlib = mod_get_mem(&worker->env, "dynlib");
830 #endif /* WITH_DYNLIBMODULE */
832 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
834 if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
836 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
838 if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
840 if(!print_longnum(ssl, "mem.mod.respip"SQ, respip))
843 if(!print_longnum(ssl, "mem.mod.subnet"SQ, subnet))
845 #endif /* CLIENT_SUBNET */
847 if(!print_longnum(ssl, "mem.mod.ipsecmod"SQ, ipsecmod))
849 #endif /* USE_IPSECMOD */
851 if(!print_longnum(ssl, "mem.cache.dnscrypt_shared_secret"SQ,
852 dnscrypt_shared_secret))
854 if(!print_longnum(ssl, "mem.cache.dnscrypt_nonce"SQ,
857 #endif /* USE_DNSCRYPT */
858 #ifdef WITH_DYNLIBMODULE
859 if(!print_longnum(ssl, "mem.mod.dynlibmod"SQ, dynlib))
861 #endif /* WITH_DYNLIBMODULE */
862 if(!print_longnum(ssl, "mem.streamwait"SQ,
863 (size_t)s->svr.mem_stream_wait))
868 /** print uptime stats */
870 print_uptime(RES* ssl, struct worker* worker, int reset)
872 struct timeval now = *worker->env.now_tv;
873 struct timeval up, dt;
874 timeval_subtract(&up, &now, &worker->daemon->time_boot);
875 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
877 worker->daemon->time_last_stat = now;
878 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
879 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
880 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
881 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
882 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
883 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
887 /** print extended histogram */
889 print_hist(RES* ssl, struct ub_stats_info* s)
891 struct timehist* hist;
893 hist = timehist_setup();
895 log_err("out of memory");
898 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
899 for(i=0; i<hist->num; i++) {
901 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
902 (int)hist->buckets[i].lower.tv_sec,
903 (int)hist->buckets[i].lower.tv_usec,
904 (int)hist->buckets[i].upper.tv_sec,
905 (int)hist->buckets[i].upper.tv_usec,
906 (unsigned long)hist->buckets[i].count)) {
907 timehist_delete(hist);
911 timehist_delete(hist);
915 /** print extended stats */
917 print_ext(RES* ssl, struct ub_stats_info* s)
921 const sldns_rr_descriptor* desc;
922 const sldns_lookup_table* lt;
924 for(i=0; i<UB_STATS_QTYPE_NUM; i++) {
925 if(inhibit_zero && s->svr.qtype[i] == 0)
927 desc = sldns_rr_descript((uint16_t)i);
928 if(desc && desc->_name) {
929 snprintf(nm, sizeof(nm), "%s", desc->_name);
930 } else if (i == LDNS_RR_TYPE_IXFR) {
931 snprintf(nm, sizeof(nm), "IXFR");
932 } else if (i == LDNS_RR_TYPE_AXFR) {
933 snprintf(nm, sizeof(nm), "AXFR");
934 } else if (i == LDNS_RR_TYPE_MAILA) {
935 snprintf(nm, sizeof(nm), "MAILA");
936 } else if (i == LDNS_RR_TYPE_MAILB) {
937 snprintf(nm, sizeof(nm), "MAILB");
938 } else if (i == LDNS_RR_TYPE_ANY) {
939 snprintf(nm, sizeof(nm), "ANY");
941 snprintf(nm, sizeof(nm), "TYPE%d", i);
943 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n",
944 nm, (unsigned long)s->svr.qtype[i])) return 0;
946 if(!inhibit_zero || s->svr.qtype_big) {
947 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n",
948 (unsigned long)s->svr.qtype_big)) return 0;
951 for(i=0; i<UB_STATS_QCLASS_NUM; i++) {
952 if(inhibit_zero && s->svr.qclass[i] == 0)
954 lt = sldns_lookup_by_id(sldns_rr_classes, i);
956 snprintf(nm, sizeof(nm), "%s", lt->name);
958 snprintf(nm, sizeof(nm), "CLASS%d", i);
960 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n",
961 nm, (unsigned long)s->svr.qclass[i])) return 0;
963 if(!inhibit_zero || s->svr.qclass_big) {
964 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n",
965 (unsigned long)s->svr.qclass_big)) return 0;
968 for(i=0; i<UB_STATS_OPCODE_NUM; i++) {
969 if(inhibit_zero && s->svr.qopcode[i] == 0)
971 lt = sldns_lookup_by_id(sldns_opcodes, i);
973 snprintf(nm, sizeof(nm), "%s", lt->name);
975 snprintf(nm, sizeof(nm), "OPCODE%d", i);
977 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n",
978 nm, (unsigned long)s->svr.qopcode[i])) return 0;
981 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n",
982 (unsigned long)s->svr.qtcp)) return 0;
983 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n",
984 (unsigned long)s->svr.qtcp_outgoing)) return 0;
985 if(!ssl_printf(ssl, "num.query.tls"SQ"%lu\n",
986 (unsigned long)s->svr.qtls)) return 0;
987 if(!ssl_printf(ssl, "num.query.tls.resume"SQ"%lu\n",
988 (unsigned long)s->svr.qtls_resume)) return 0;
989 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n",
990 (unsigned long)s->svr.qipv6)) return 0;
992 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n",
993 (unsigned long)s->svr.qbit_QR)) return 0;
994 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n",
995 (unsigned long)s->svr.qbit_AA)) return 0;
996 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n",
997 (unsigned long)s->svr.qbit_TC)) return 0;
998 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n",
999 (unsigned long)s->svr.qbit_RD)) return 0;
1000 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n",
1001 (unsigned long)s->svr.qbit_RA)) return 0;
1002 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n",
1003 (unsigned long)s->svr.qbit_Z)) return 0;
1004 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n",
1005 (unsigned long)s->svr.qbit_AD)) return 0;
1006 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n",
1007 (unsigned long)s->svr.qbit_CD)) return 0;
1008 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n",
1009 (unsigned long)s->svr.qEDNS)) return 0;
1010 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n",
1011 (unsigned long)s->svr.qEDNS_DO)) return 0;
1014 for(i=0; i<UB_STATS_RCODE_NUM; i++) {
1015 /* Always include RCODEs 0-5 */
1016 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
1018 lt = sldns_lookup_by_id(sldns_rcodes, i);
1019 if(lt && lt->name) {
1020 snprintf(nm, sizeof(nm), "%s", lt->name);
1022 snprintf(nm, sizeof(nm), "RCODE%d", i);
1024 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n",
1025 nm, (unsigned long)s->svr.ans_rcode[i])) return 0;
1027 if(!inhibit_zero || s->svr.ans_rcode_nodata) {
1028 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n",
1029 (unsigned long)s->svr.ans_rcode_nodata)) return 0;
1032 if(!ssl_printf(ssl, "num.query.ratelimited"SQ"%lu\n",
1033 (unsigned long)s->svr.queries_ratelimited)) return 0;
1035 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n",
1036 (unsigned long)s->svr.ans_secure)) return 0;
1037 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n",
1038 (unsigned long)s->svr.ans_bogus)) return 0;
1039 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n",
1040 (unsigned long)s->svr.rrset_bogus)) return 0;
1041 if(!ssl_printf(ssl, "num.query.aggressive.NOERROR"SQ"%lu\n",
1042 (unsigned long)s->svr.num_neg_cache_noerror)) return 0;
1043 if(!ssl_printf(ssl, "num.query.aggressive.NXDOMAIN"SQ"%lu\n",
1044 (unsigned long)s->svr.num_neg_cache_nxdomain)) return 0;
1045 /* threat detection */
1046 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n",
1047 (unsigned long)s->svr.unwanted_queries)) return 0;
1048 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n",
1049 (unsigned long)s->svr.unwanted_replies)) return 0;
1051 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
1052 (unsigned)s->svr.msg_cache_count)) return 0;
1053 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
1054 (unsigned)s->svr.rrset_cache_count)) return 0;
1055 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
1056 (unsigned)s->svr.infra_cache_count)) return 0;
1057 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
1058 (unsigned)s->svr.key_cache_count)) return 0;
1059 /* applied RPZ actions */
1060 for(i=0; i<UB_STATS_RPZ_ACTION_NUM; i++) {
1061 if(i == RPZ_NO_OVERRIDE_ACTION)
1063 if(inhibit_zero && s->svr.rpz_action[i] == 0)
1065 if(!ssl_printf(ssl, "num.rpz.action.%s"SQ"%lu\n",
1066 rpz_action_to_string(i),
1067 (unsigned long)s->svr.rpz_action[i])) return 0;
1070 if(!ssl_printf(ssl, "dnscrypt_shared_secret.cache.count"SQ"%u\n",
1071 (unsigned)s->svr.shared_secret_cache_count)) return 0;
1072 if(!ssl_printf(ssl, "dnscrypt_nonce.cache.count"SQ"%u\n",
1073 (unsigned)s->svr.nonce_cache_count)) return 0;
1074 if(!ssl_printf(ssl, "num.query.dnscrypt.shared_secret.cachemiss"SQ"%lu\n",
1075 (unsigned long)s->svr.num_query_dnscrypt_secret_missed_cache)) return 0;
1076 if(!ssl_printf(ssl, "num.query.dnscrypt.replay"SQ"%lu\n",
1077 (unsigned long)s->svr.num_query_dnscrypt_replay)) return 0;
1078 #endif /* USE_DNSCRYPT */
1079 if(!ssl_printf(ssl, "num.query.authzone.up"SQ"%lu\n",
1080 (unsigned long)s->svr.num_query_authzone_up)) return 0;
1081 if(!ssl_printf(ssl, "num.query.authzone.down"SQ"%lu\n",
1082 (unsigned long)s->svr.num_query_authzone_down)) return 0;
1083 #ifdef CLIENT_SUBNET
1084 if(!ssl_printf(ssl, "num.query.subnet"SQ"%lu\n",
1085 (unsigned long)s->svr.num_query_subnet)) return 0;
1086 if(!ssl_printf(ssl, "num.query.subnet_cache"SQ"%lu\n",
1087 (unsigned long)s->svr.num_query_subnet_cache)) return 0;
1088 #endif /* CLIENT_SUBNET */
1092 /** do the stats command */
1094 do_stats(RES* ssl, struct worker* worker, int reset)
1096 struct daemon* daemon = worker->daemon;
1097 struct ub_stats_info total;
1098 struct ub_stats_info s;
1100 memset(&total, 0, sizeof(total));
1101 log_assert(daemon->num > 0);
1102 /* gather all thread statistics in one place */
1103 for(i=0; i<daemon->num; i++) {
1104 server_stats_obtain(worker, daemon->workers[i], &s, reset);
1105 if(!print_thread_stats(ssl, i, &s))
1109 else server_stats_add(&total, &s);
1111 /* print the thread statistics */
1112 total.mesh_time_median /= (double)daemon->num;
1113 if(!print_stats(ssl, "total", &total))
1115 if(!print_uptime(ssl, worker, reset))
1117 if(daemon->cfg->stat_extended) {
1118 if(!print_mem(ssl, worker, daemon, &total))
1120 if(!print_hist(ssl, &total))
1122 if(!print_ext(ssl, &total))
1127 /** parse commandline argument domain name */
1129 parse_arg_name(RES* ssl, char* str, uint8_t** res, size_t* len, int* labs)
1131 uint8_t nm[LDNS_MAX_DOMAINLEN+1];
1132 size_t nmlen = sizeof(nm);
1137 if(str[0] == '\0') {
1138 ssl_printf(ssl, "error: this option requires a domain name\n");
1141 status = sldns_str2wire_dname_buf(str, nm, &nmlen);
1143 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
1144 LDNS_WIREPARSE_OFFSET(status),
1145 sldns_get_errorstr_parse(status));
1148 *res = memdup(nm, nmlen);
1150 ssl_printf(ssl, "error out of memory\n");
1153 *labs = dname_count_size_labels(*res, len);
1157 /** find second argument, modifies string */
1159 find_arg2(RES* ssl, char* arg, char** arg2)
1161 char* as = strchr(arg, ' ');
1162 char* at = strchr(arg, '\t');
1167 *arg2 = skipwhite(as+1);
1170 *arg2 = skipwhite(as+1);
1173 *arg2 = skipwhite(at+1);
1175 ssl_printf(ssl, "error could not find next argument "
1182 /** Add a new zone */
1184 perform_zone_add(RES* ssl, struct local_zones* zones, char* arg)
1190 enum localzone_type t;
1191 struct local_zone* z;
1192 if(!find_arg2(ssl, arg, &arg2))
1194 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1196 if(!local_zone_str2type(arg2, &t)) {
1197 ssl_printf(ssl, "error not a zone type. %s\n", arg2);
1201 lock_rw_wrlock(&zones->lock);
1202 if((z=local_zones_find(zones, nm, nmlen,
1203 nmlabs, LDNS_RR_CLASS_IN))) {
1204 /* already present in tree */
1205 lock_rw_wrlock(&z->lock);
1206 z->type = t; /* update type anyway */
1207 lock_rw_unlock(&z->lock);
1209 lock_rw_unlock(&zones->lock);
1212 if(!local_zones_add_zone(zones, nm, nmlen,
1213 nmlabs, LDNS_RR_CLASS_IN, t)) {
1214 lock_rw_unlock(&zones->lock);
1215 ssl_printf(ssl, "error out of memory\n");
1218 lock_rw_unlock(&zones->lock);
1222 /** Do the local_zone command */
1224 do_zone_add(RES* ssl, struct local_zones* zones, char* arg)
1226 if(!perform_zone_add(ssl, zones, arg))
1231 /** Do the local_zones command */
1233 do_zones_add(RES* ssl, struct local_zones* zones)
1237 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1238 if(buf[0] == 0x04 && buf[1] == 0)
1239 break; /* end of transmission */
1240 if(!perform_zone_add(ssl, zones, buf)) {
1241 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1247 (void)ssl_printf(ssl, "added %d zones\n", num);
1250 /** Remove a zone */
1252 perform_zone_remove(RES* ssl, struct local_zones* zones, char* arg)
1257 struct local_zone* z;
1258 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1260 lock_rw_wrlock(&zones->lock);
1261 if((z=local_zones_find(zones, nm, nmlen,
1262 nmlabs, LDNS_RR_CLASS_IN))) {
1263 /* present in tree */
1264 local_zones_del_zone(zones, z);
1266 lock_rw_unlock(&zones->lock);
1271 /** Do the local_zone_remove command */
1273 do_zone_remove(RES* ssl, struct local_zones* zones, char* arg)
1275 if(!perform_zone_remove(ssl, zones, arg))
1280 /** Do the local_zones_remove command */
1282 do_zones_remove(RES* ssl, struct local_zones* zones)
1286 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1287 if(buf[0] == 0x04 && buf[1] == 0)
1288 break; /* end of transmission */
1289 if(!perform_zone_remove(ssl, zones, buf)) {
1290 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1296 (void)ssl_printf(ssl, "removed %d zones\n", num);
1299 /** Add new RR data */
1301 perform_data_add(RES* ssl, struct local_zones* zones, char* arg)
1303 if(!local_zones_add_RR(zones, arg)) {
1304 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1310 /** Do the local_data command */
1312 do_data_add(RES* ssl, struct local_zones* zones, char* arg)
1314 if(!perform_data_add(ssl, zones, arg))
1319 /** Do the local_datas command */
1321 do_datas_add(RES* ssl, struct local_zones* zones)
1325 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1326 if(buf[0] == 0x04 && buf[1] == 0)
1327 break; /* end of transmission */
1328 if(!perform_data_add(ssl, zones, buf)) {
1329 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1335 (void)ssl_printf(ssl, "added %d datas\n", num);
1338 /** Remove RR data */
1340 perform_data_remove(RES* ssl, struct local_zones* zones, char* arg)
1345 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1347 local_zones_del_data(zones, nm,
1348 nmlen, nmlabs, LDNS_RR_CLASS_IN);
1353 /** Do the local_data_remove command */
1355 do_data_remove(RES* ssl, struct local_zones* zones, char* arg)
1357 if(!perform_data_remove(ssl, zones, arg))
1362 /** Do the local_datas_remove command */
1364 do_datas_remove(RES* ssl, struct local_zones* zones)
1368 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1369 if(buf[0] == 0x04 && buf[1] == 0)
1370 break; /* end of transmission */
1371 if(!perform_data_remove(ssl, zones, buf)) {
1372 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1378 (void)ssl_printf(ssl, "removed %d datas\n", num);
1381 /** Add a new zone to view */
1383 do_view_zone_add(RES* ssl, struct worker* worker, char* arg)
1387 if(!find_arg2(ssl, arg, &arg2))
1389 v = views_find_view(worker->daemon->views,
1390 arg, 1 /* get write lock*/);
1392 ssl_printf(ssl,"no view with name: %s\n", arg);
1395 if(!v->local_zones) {
1396 if(!(v->local_zones = local_zones_create())){
1397 lock_rw_unlock(&v->lock);
1398 ssl_printf(ssl,"error out of memory\n");
1402 /* Global local-zone is not used for this view,
1403 * therefore add defaults to this view-specic
1405 struct config_file lz_cfg;
1406 memset(&lz_cfg, 0, sizeof(lz_cfg));
1407 local_zone_enter_defaults(v->local_zones, &lz_cfg);
1410 do_zone_add(ssl, v->local_zones, arg2);
1411 lock_rw_unlock(&v->lock);
1414 /** Remove a zone from view */
1416 do_view_zone_remove(RES* ssl, struct worker* worker, char* arg)
1420 if(!find_arg2(ssl, arg, &arg2))
1422 v = views_find_view(worker->daemon->views,
1423 arg, 1 /* get write lock*/);
1425 ssl_printf(ssl,"no view with name: %s\n", arg);
1428 if(!v->local_zones) {
1429 lock_rw_unlock(&v->lock);
1433 do_zone_remove(ssl, v->local_zones, arg2);
1434 lock_rw_unlock(&v->lock);
1437 /** Add new RR data to view */
1439 do_view_data_add(RES* ssl, struct worker* worker, char* arg)
1443 if(!find_arg2(ssl, arg, &arg2))
1445 v = views_find_view(worker->daemon->views,
1446 arg, 1 /* get write lock*/);
1448 ssl_printf(ssl,"no view with name: %s\n", arg);
1451 if(!v->local_zones) {
1452 if(!(v->local_zones = local_zones_create())){
1453 lock_rw_unlock(&v->lock);
1454 ssl_printf(ssl,"error out of memory\n");
1458 do_data_add(ssl, v->local_zones, arg2);
1459 lock_rw_unlock(&v->lock);
1462 /** Add new RR data from stdin to view */
1464 do_view_datas_add(RES* ssl, struct worker* worker, char* arg)
1467 v = views_find_view(worker->daemon->views,
1468 arg, 1 /* get write lock*/);
1470 ssl_printf(ssl,"no view with name: %s\n", arg);
1473 if(!v->local_zones) {
1474 if(!(v->local_zones = local_zones_create())){
1475 lock_rw_unlock(&v->lock);
1476 ssl_printf(ssl,"error out of memory\n");
1480 do_datas_add(ssl, v->local_zones);
1481 lock_rw_unlock(&v->lock);
1484 /** Remove RR data from view */
1486 do_view_data_remove(RES* ssl, struct worker* worker, char* arg)
1490 if(!find_arg2(ssl, arg, &arg2))
1492 v = views_find_view(worker->daemon->views,
1493 arg, 1 /* get write lock*/);
1495 ssl_printf(ssl,"no view with name: %s\n", arg);
1498 if(!v->local_zones) {
1499 lock_rw_unlock(&v->lock);
1503 do_data_remove(ssl, v->local_zones, arg2);
1504 lock_rw_unlock(&v->lock);
1507 /** Remove RR data from stdin from view */
1509 do_view_datas_remove(RES* ssl, struct worker* worker, char* arg)
1512 v = views_find_view(worker->daemon->views,
1513 arg, 1 /* get write lock*/);
1515 ssl_printf(ssl,"no view with name: %s\n", arg);
1518 if(!v->local_zones){
1519 lock_rw_unlock(&v->lock);
1520 ssl_printf(ssl, "removed 0 datas\n");
1524 do_datas_remove(ssl, v->local_zones);
1525 lock_rw_unlock(&v->lock);
1528 /** cache lookup of nameservers */
1530 do_lookup(RES* ssl, struct worker* worker, char* arg)
1535 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1537 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1541 /** flush something from rrset and msg caches */
1543 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1544 uint16_t t, uint16_t c)
1547 struct query_info k;
1548 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1549 if(t == LDNS_RR_TYPE_SOA)
1550 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1551 PACKED_RRSET_SOA_NEG);
1553 k.qname_len = nmlen;
1556 k.local_alias = NULL;
1557 h = query_info_hash(&k, 0);
1558 slabhash_remove(worker->env.msg_cache, h, &k);
1559 if(t == LDNS_RR_TYPE_AAAA) {
1560 /* for AAAA also flush dns64 bit_cd packet */
1561 h = query_info_hash(&k, BIT_CD);
1562 slabhash_remove(worker->env.msg_cache, h, &k);
1568 do_flush_type(RES* ssl, struct worker* worker, char* arg)
1575 if(!find_arg2(ssl, arg, &arg2))
1577 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1579 t = sldns_get_rr_type_by_name(arg2);
1580 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1586 /** flush statistics */
1588 do_flush_stats(RES* ssl, struct worker* worker)
1590 worker_stats_clear(worker);
1595 * Local info for deletion functions
1599 struct worker* worker;
1600 /** name to delete */
1606 /** time to invalidate to */
1608 /** number of rrsets removed */
1610 /** number of msgs removed */
1612 /** number of key entries removed */
1614 /** length of addr */
1616 /** socket address for host deletion */
1617 struct sockaddr_storage addr;
1620 /** callback to delete hosts in infra cache */
1622 infra_del_host(struct lruhash_entry* e, void* arg)
1624 /* entry is locked */
1625 struct del_info* inf = (struct del_info*)arg;
1626 struct infra_key* k = (struct infra_key*)e->key;
1627 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1628 struct infra_data* d = (struct infra_data*)e->data;
1631 d->timeout_AAAA = 0;
1632 d->timeout_other = 0;
1634 if(d->ttl > inf->expired) {
1635 d->ttl = inf->expired;
1641 /** flush infra cache */
1643 do_flush_infra(RES* ssl, struct worker* worker, char* arg)
1645 struct sockaddr_storage addr;
1647 struct del_info inf;
1648 if(strcmp(arg, "all") == 0) {
1649 slabhash_clear(worker->env.infra_cache->hosts);
1653 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1654 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1657 /* delete all entries from cache */
1658 /* what we do is to set them all expired */
1659 inf.worker = worker;
1663 inf.expired = *worker->env.now;
1664 inf.expired -= 3; /* handle 3 seconds skew between threads */
1669 memmove(&inf.addr, &addr, len);
1670 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1675 /** flush requestlist */
1677 do_flush_requestlist(RES* ssl, struct worker* worker)
1679 mesh_delete_all(worker->env.mesh);
1683 /** callback to delete rrsets in a zone */
1685 zone_del_rrset(struct lruhash_entry* e, void* arg)
1687 /* entry is locked */
1688 struct del_info* inf = (struct del_info*)arg;
1689 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1690 if(dname_subdomain_c(k->rk.dname, inf->name)) {
1691 struct packed_rrset_data* d =
1692 (struct packed_rrset_data*)e->data;
1693 if(d->ttl > inf->expired) {
1694 d->ttl = inf->expired;
1700 /** callback to delete messages in a zone */
1702 zone_del_msg(struct lruhash_entry* e, void* arg)
1704 /* entry is locked */
1705 struct del_info* inf = (struct del_info*)arg;
1706 struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1707 if(dname_subdomain_c(k->key.qname, inf->name)) {
1708 struct reply_info* d = (struct reply_info*)e->data;
1709 if(d->ttl > inf->expired) {
1710 d->ttl = inf->expired;
1711 d->prefetch_ttl = inf->expired;
1712 d->serve_expired_ttl = inf->expired;
1718 /** callback to delete keys in zone */
1720 zone_del_kcache(struct lruhash_entry* e, void* arg)
1722 /* entry is locked */
1723 struct del_info* inf = (struct del_info*)arg;
1724 struct key_entry_key* k = (struct key_entry_key*)e->key;
1725 if(dname_subdomain_c(k->name, inf->name)) {
1726 struct key_entry_data* d = (struct key_entry_data*)e->data;
1727 if(d->ttl > inf->expired) {
1728 d->ttl = inf->expired;
1734 /** remove all rrsets and keys from zone from cache */
1736 do_flush_zone(RES* ssl, struct worker* worker, char* arg)
1741 struct del_info inf;
1742 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1744 /* delete all RRs and key entries from zone */
1745 /* what we do is to set them all expired */
1746 inf.worker = worker;
1750 inf.expired = *worker->env.now;
1751 inf.expired -= 3; /* handle 3 seconds skew between threads */
1755 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1756 &zone_del_rrset, &inf);
1758 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1760 /* and validator cache */
1761 if(worker->env.key_cache) {
1762 slabhash_traverse(worker->env.key_cache->slab, 1,
1763 &zone_del_kcache, &inf);
1768 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1769 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1770 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1773 /** callback to delete bogus rrsets */
1775 bogus_del_rrset(struct lruhash_entry* e, void* arg)
1777 /* entry is locked */
1778 struct del_info* inf = (struct del_info*)arg;
1779 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1780 if(d->security == sec_status_bogus) {
1781 d->ttl = inf->expired;
1786 /** callback to delete bogus messages */
1788 bogus_del_msg(struct lruhash_entry* e, void* arg)
1790 /* entry is locked */
1791 struct del_info* inf = (struct del_info*)arg;
1792 struct reply_info* d = (struct reply_info*)e->data;
1793 if(d->security == sec_status_bogus) {
1794 d->ttl = inf->expired;
1799 /** callback to delete bogus keys */
1801 bogus_del_kcache(struct lruhash_entry* e, void* arg)
1803 /* entry is locked */
1804 struct del_info* inf = (struct del_info*)arg;
1805 struct key_entry_data* d = (struct key_entry_data*)e->data;
1807 d->ttl = inf->expired;
1812 /** remove all bogus rrsets, msgs and keys from cache */
1814 do_flush_bogus(RES* ssl, struct worker* worker)
1816 struct del_info inf;
1817 /* what we do is to set them all expired */
1818 inf.worker = worker;
1819 inf.expired = *worker->env.now;
1820 inf.expired -= 3; /* handle 3 seconds skew between threads */
1824 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1825 &bogus_del_rrset, &inf);
1827 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1829 /* and validator cache */
1830 if(worker->env.key_cache) {
1831 slabhash_traverse(worker->env.key_cache->slab, 1,
1832 &bogus_del_kcache, &inf);
1835 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1836 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1837 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1840 /** callback to delete negative and servfail rrsets */
1842 negative_del_rrset(struct lruhash_entry* e, void* arg)
1844 /* entry is locked */
1845 struct del_info* inf = (struct del_info*)arg;
1846 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1847 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1848 /* delete the parentside negative cache rrsets,
1849 * these are nameserver rrsets that failed lookup, rdata empty */
1850 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 &&
1851 d->rrsig_count == 0 && d->rr_len[0] == 0) {
1852 d->ttl = inf->expired;
1857 /** callback to delete negative and servfail messages */
1859 negative_del_msg(struct lruhash_entry* e, void* arg)
1861 /* entry is locked */
1862 struct del_info* inf = (struct del_info*)arg;
1863 struct reply_info* d = (struct reply_info*)e->data;
1864 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1865 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1866 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) {
1867 d->ttl = inf->expired;
1872 /** callback to delete negative key entries */
1874 negative_del_kcache(struct lruhash_entry* e, void* arg)
1876 /* entry is locked */
1877 struct del_info* inf = (struct del_info*)arg;
1878 struct key_entry_data* d = (struct key_entry_data*)e->data;
1879 /* could be bad because of lookup failure on the DS, DNSKEY, which
1880 * was nxdomain or servfail, and thus a result of negative lookups */
1882 d->ttl = inf->expired;
1887 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1889 do_flush_negative(RES* ssl, struct worker* worker)
1891 struct del_info inf;
1892 /* what we do is to set them all expired */
1893 inf.worker = worker;
1894 inf.expired = *worker->env.now;
1895 inf.expired -= 3; /* handle 3 seconds skew between threads */
1899 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1900 &negative_del_rrset, &inf);
1902 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf);
1904 /* and validator cache */
1905 if(worker->env.key_cache) {
1906 slabhash_traverse(worker->env.key_cache->slab, 1,
1907 &negative_del_kcache, &inf);
1910 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1911 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1912 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1915 /** remove name rrset from cache */
1917 do_flush_name(RES* ssl, struct worker* w, char* arg)
1922 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1924 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1925 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1926 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1927 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1928 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1929 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1930 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1931 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1932 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1933 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1939 /** printout a delegation point info */
1941 ssl_print_name_dp(RES* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1945 struct delegpt_ns* ns;
1946 struct delegpt_addr* a;
1948 if(str) { /* print header for forward, stub */
1949 char* c = sldns_wire2str_class(dclass);
1951 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) {
1957 for(ns = dp->nslist; ns; ns = ns->next) {
1958 dname_str(ns->name, buf);
1959 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1963 for(a = dp->target_list; a; a = a->next_target) {
1964 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1965 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1969 return ssl_printf(ssl, "\n");
1973 /** print root forwards */
1975 print_root_fwds(RES* ssl, struct iter_forwards* fwds, uint8_t* root)
1978 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1980 return ssl_printf(ssl, "off (using root hints)\n");
1981 /* if dp is returned it must be the root */
1982 log_assert(query_dname_compare(dp->name, root)==0);
1983 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1986 /** parse args into delegpt */
1987 static struct delegpt*
1988 parse_delegpt(RES* ssl, char* args, uint8_t* nm, int allow_names)
1990 /* parse args and add in */
1993 struct delegpt* dp = delegpt_create_mlc(nm);
1994 struct sockaddr_storage addr;
1998 (void)ssl_printf(ssl, "error out of memory\n");
2003 p = strchr(p, ' '); /* find next spot, if any */
2005 *p++ = 0; /* end this spot */
2006 p = skipwhite(p); /* position at next spot */
2009 if(!authextstrtoaddr(todo, &addr, &addrlen, &auth_name)) {
2014 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
2015 (void)ssl_printf(ssl, "error cannot "
2016 "parse IP address or name "
2018 delegpt_free_mlc(dp);
2021 if(!delegpt_add_ns_mlc(dp, n, 0)) {
2022 (void)ssl_printf(ssl, "error out of memory\n");
2024 delegpt_free_mlc(dp);
2030 (void)ssl_printf(ssl, "error cannot parse"
2031 " IP address '%s'\n", todo);
2032 delegpt_free_mlc(dp);
2036 #if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST)
2038 log_err("no name verification functionality in "
2039 "ssl library, ignored name for %s", todo);
2042 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0,
2044 (void)ssl_printf(ssl, "error out of memory\n");
2045 delegpt_free_mlc(dp);
2050 dp->has_parent_side_NS = 1;
2054 /** do the status command */
2056 do_forward(RES* ssl, struct worker* worker, char* args)
2058 struct iter_forwards* fwd = worker->env.fwds;
2059 uint8_t* root = (uint8_t*)"\000";
2061 (void)ssl_printf(ssl, "error: structure not allocated\n");
2064 if(args == NULL || args[0] == 0) {
2065 (void)print_root_fwds(ssl, fwd, root);
2068 /* set root forwards for this thread. since we are in remote control
2069 * the actual mesh is not running, so we can freely edit it. */
2070 /* delete all the existing queries first */
2071 mesh_delete_all(worker->env.mesh);
2072 if(strcmp(args, "off") == 0) {
2073 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
2076 if(!(dp = parse_delegpt(ssl, args, root, 0)))
2078 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
2079 (void)ssl_printf(ssl, "error out of memory\n");
2087 parse_fs_args(RES* ssl, char* args, uint8_t** nm, struct delegpt** dp,
2088 int* insecure, int* prime)
2094 /* parse all -x args */
2095 while(args[0] == '+') {
2096 if(!find_arg2(ssl, args, &rest))
2098 while(*(++args) != 0) {
2099 if(*args == 'i' && insecure)
2101 else if(*args == 'p' && prime)
2104 (void)ssl_printf(ssl, "error: unknown option %s\n", args);
2112 if(!find_arg2(ssl, args, &rest))
2116 } else zonename = args;
2117 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
2122 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
2130 /** do the forward_add command */
2132 do_forward_add(RES* ssl, struct worker* worker, char* args)
2134 struct iter_forwards* fwd = worker->env.fwds;
2137 struct delegpt* dp = NULL;
2138 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
2140 if(insecure && worker->env.anchors) {
2141 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2143 (void)ssl_printf(ssl, "error out of memory\n");
2144 delegpt_free_mlc(dp);
2149 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
2150 (void)ssl_printf(ssl, "error out of memory\n");
2158 /** do the forward_remove command */
2160 do_forward_remove(RES* ssl, struct worker* worker, char* args)
2162 struct iter_forwards* fwd = worker->env.fwds;
2165 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
2167 if(insecure && worker->env.anchors)
2168 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2170 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
2175 /** do the stub_add command */
2177 do_stub_add(RES* ssl, struct worker* worker, char* args)
2179 struct iter_forwards* fwd = worker->env.fwds;
2180 int insecure = 0, prime = 0;
2182 struct delegpt* dp = NULL;
2183 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
2185 if(insecure && worker->env.anchors) {
2186 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2188 (void)ssl_printf(ssl, "error out of memory\n");
2189 delegpt_free_mlc(dp);
2194 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
2195 if(insecure && worker->env.anchors)
2196 anchors_delete_insecure(worker->env.anchors,
2197 LDNS_RR_CLASS_IN, nm);
2198 (void)ssl_printf(ssl, "error out of memory\n");
2199 delegpt_free_mlc(dp);
2203 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
2204 (void)ssl_printf(ssl, "error out of memory\n");
2205 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
2206 if(insecure && worker->env.anchors)
2207 anchors_delete_insecure(worker->env.anchors,
2208 LDNS_RR_CLASS_IN, nm);
2216 /** do the stub_remove command */
2218 do_stub_remove(RES* ssl, struct worker* worker, char* args)
2220 struct iter_forwards* fwd = worker->env.fwds;
2223 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
2225 if(insecure && worker->env.anchors)
2226 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2228 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
2229 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
2234 /** do the insecure_add command */
2236 do_insecure_add(RES* ssl, struct worker* worker, char* arg)
2241 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2243 if(worker->env.anchors) {
2244 if(!anchors_add_insecure(worker->env.anchors,
2245 LDNS_RR_CLASS_IN, nm)) {
2246 (void)ssl_printf(ssl, "error out of memory\n");
2255 /** do the insecure_remove command */
2257 do_insecure_remove(RES* ssl, struct worker* worker, char* arg)
2262 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2264 if(worker->env.anchors)
2265 anchors_delete_insecure(worker->env.anchors,
2266 LDNS_RR_CLASS_IN, nm);
2272 do_insecure_list(RES* ssl, struct worker* worker)
2275 struct trust_anchor* a;
2276 if(worker->env.anchors) {
2277 RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) {
2278 if(a->numDS == 0 && a->numDNSKEY == 0) {
2279 dname_str(a->name, buf);
2280 ssl_printf(ssl, "%s\n", buf);
2286 /** do the status command */
2288 do_status(RES* ssl, struct worker* worker)
2292 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
2294 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
2296 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
2298 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
2300 for(i=0; i<worker->daemon->mods.num; i++) {
2301 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
2304 if(!ssl_printf(ssl, " ]\n"))
2306 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
2307 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
2309 if(!ssl_printf(ssl, "options:%s%s%s%s\n" ,
2310 (worker->daemon->reuseport?" reuseport":""),
2311 (worker->daemon->rc->accept_list?" control":""),
2312 (worker->daemon->rc->accept_list && worker->daemon->rc->use_cert?"(ssl)":""),
2313 (worker->daemon->rc->accept_list && worker->daemon->cfg->control_ifs.first && worker->daemon->cfg->control_ifs.first->str && worker->daemon->cfg->control_ifs.first->str[0] == '/'?"(namedpipe)":"")
2316 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
2321 /** get age for the mesh state */
2323 get_mesh_age(struct mesh_state* m, char* buf, size_t len,
2324 struct module_env* env)
2328 struct mesh_reply* r = m->reply_list;
2329 /* last reply is the oldest */
2332 timeval_subtract(&d, env->now_tv, &r->start_time);
2333 snprintf(buf, len, ARG_LL "d.%6.6d",
2334 (long long)d.tv_sec, (int)d.tv_usec);
2336 snprintf(buf, len, "-");
2340 /** get status of a mesh state */
2342 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
2343 char* buf, size_t len)
2345 enum module_ext_state s = m->s.ext_state[m->s.curmod];
2346 const char *modname = mesh->mods.mod[m->s.curmod]->name;
2348 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
2349 m->s.minfo[m->s.curmod]) {
2350 /* break into iterator to find out who its waiting for */
2351 struct iter_qstate* qstate = (struct iter_qstate*)
2352 m->s.minfo[m->s.curmod];
2353 struct outbound_list* ol = &qstate->outlist;
2354 struct outbound_entry* e;
2355 snprintf(buf, len, "%s wait for", modname);
2358 if(ol->first == NULL)
2359 snprintf(buf, len, " (empty_list)");
2360 for(e = ol->first; e; e = e->next) {
2361 snprintf(buf, len, " ");
2364 addr_to_str(&e->qsent->addr, e->qsent->addrlen,
2369 } else if(s == module_wait_subquery) {
2370 /* look in subs from mesh state to see what */
2372 struct mesh_state_ref* sub;
2373 snprintf(buf, len, "%s wants", modname);
2376 if(m->sub_set.count == 0)
2377 snprintf(buf, len, " (empty_list)");
2378 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
2379 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
2380 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
2381 dname_str(sub->s->s.qinfo.qname, nm);
2382 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
2383 (c?c:"CLASS??"), nm);
2390 snprintf(buf, len, "%s is %s", modname, strextstate(s));
2394 /** do the dump_requestlist command */
2396 do_dump_requestlist(RES* ssl, struct worker* worker)
2398 struct mesh_area* mesh;
2399 struct mesh_state* m;
2403 char statbuf[10240];
2404 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
2406 if(!ssl_printf(ssl, "# type cl name seconds module status\n"))
2408 /* show worker mesh contents */
2409 mesh = worker->env.mesh;
2411 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
2412 char* t = sldns_wire2str_type(m->s.qinfo.qtype);
2413 char* c = sldns_wire2str_class(m->s.qinfo.qclass);
2414 dname_str(m->s.qinfo.qname, buf);
2415 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
2416 get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
2417 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
2418 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
2430 /** structure for argument data for dump infra host */
2432 /** the infra cache */
2433 struct infra_cache* infra;
2434 /** the SSL connection */
2438 /** ssl failure? stop writing and skip the rest. If the tcp
2439 * connection is broken, and writes fail, we then stop writing. */
2443 /** callback for every host element in the infra cache */
2445 dump_infra_host(struct lruhash_entry* e, void* arg)
2447 struct infra_arg* a = (struct infra_arg*)arg;
2448 struct infra_key* k = (struct infra_key*)e->key;
2449 struct infra_data* d = (struct infra_data*)e->data;
2455 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
2456 dname_str(k->zonename, name);
2457 port = (int)ntohs(((struct sockaddr_in*)&k->addr)->sin_port);
2458 if(port != UNBOUND_DNS_PORT) {
2459 snprintf(ip_str+strlen(ip_str), sizeof(ip_str)-strlen(ip_str),
2462 /* skip expired stuff (only backed off) */
2463 if(d->ttl < a->now) {
2464 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
2465 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
2466 name, d->rtt.rto)) {
2473 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
2474 "tA %d tAAAA %d tother %d "
2475 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
2476 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now),
2477 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
2478 d->timeout_A, d->timeout_AAAA, d->timeout_other,
2479 (int)d->edns_lame_known, (int)d->edns_version,
2480 (int)(a->now<d->probedelay?(d->probedelay - a->now):0),
2481 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
2482 (int)d->lame_other)) {
2488 /** do the dump_infra command */
2490 do_dump_infra(RES* ssl, struct worker* worker)
2492 struct infra_arg arg;
2493 arg.infra = worker->env.infra_cache;
2495 arg.now = *worker->env.now;
2497 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
2500 /** do the log_reopen command */
2502 do_log_reopen(RES* ssl, struct worker* worker)
2504 struct config_file* cfg = worker->env.cfg;
2506 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
2509 /** do the auth_zone_reload command */
2511 do_auth_zone_reload(RES* ssl, struct worker* worker, char* arg)
2516 struct auth_zones* az = worker->env.auth_zones;
2517 struct auth_zone* z = NULL;
2518 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2521 lock_rw_rdlock(&az->lock);
2522 z = auth_zone_find(az, nm, nmlen, LDNS_RR_CLASS_IN);
2524 lock_rw_wrlock(&z->lock);
2526 lock_rw_unlock(&az->lock);
2530 (void)ssl_printf(ssl, "error no auth-zone %s\n", arg);
2533 if(!auth_zone_read_zonefile(z, worker->env.cfg)) {
2534 lock_rw_unlock(&z->lock);
2535 (void)ssl_printf(ssl, "error failed to read %s\n", arg);
2538 lock_rw_unlock(&z->lock);
2542 /** do the auth_zone_transfer command */
2544 do_auth_zone_transfer(RES* ssl, struct worker* worker, char* arg)
2549 struct auth_zones* az = worker->env.auth_zones;
2550 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2552 if(!az || !auth_zones_startprobesequence(az, &worker->env, nm, nmlen,
2553 LDNS_RR_CLASS_IN)) {
2554 (void)ssl_printf(ssl, "error zone xfr task not found %s\n", arg);
2562 /** do the set_option command */
2564 do_set_option(RES* ssl, struct worker* worker, char* arg)
2567 if(!find_arg2(ssl, arg, &arg2))
2569 if(!config_set_option(worker->env.cfg, arg, arg2)) {
2570 (void)ssl_printf(ssl, "error setting option\n");
2573 /* effectuate some arguments */
2574 if(strcmp(arg, "val-override-date:") == 0) {
2575 int m = modstack_find(&worker->env.mesh->mods, "validator");
2576 struct val_env* val_env = NULL;
2577 if(m != -1) val_env = (struct val_env*)worker->env.modinfo[m];
2579 val_env->date_override = worker->env.cfg->val_date_override;
2584 /* routine to printout option values over SSL */
2585 void remote_get_opt_ssl(char* line, void* arg)
2587 RES* ssl = (RES*)arg;
2588 (void)ssl_printf(ssl, "%s\n", line);
2591 /** do the get_option command */
2593 do_get_option(RES* ssl, struct worker* worker, char* arg)
2596 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
2598 (void)ssl_printf(ssl, "error unknown option\n");
2603 /** do the list_forwards command */
2605 do_list_forwards(RES* ssl, struct worker* worker)
2607 /* since its a per-worker structure no locks needed */
2608 struct iter_forwards* fwds = worker->env.fwds;
2609 struct iter_forward_zone* z;
2610 struct trust_anchor* a;
2612 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
2613 if(!z->dp) continue; /* skip empty marker for stub */
2615 /* see if it is insecure */
2617 if(worker->env.anchors &&
2618 (a=anchor_find(worker->env.anchors, z->name,
2619 z->namelabs, z->namelen, z->dclass))) {
2620 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2622 lock_basic_unlock(&a->lock);
2625 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"),
2626 z->name, z->dclass, z->dp))
2631 /** do the list_stubs command */
2633 do_list_stubs(RES* ssl, struct worker* worker)
2635 struct iter_hints_stub* z;
2636 struct trust_anchor* a;
2639 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
2641 /* see if it is insecure */
2643 if(worker->env.anchors &&
2644 (a=anchor_find(worker->env.anchors, z->node.name,
2645 z->node.labs, z->node.len, z->node.dclass))) {
2646 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2648 lock_basic_unlock(&a->lock);
2651 snprintf(str, sizeof(str), "stub %sprime%s",
2652 (z->noprime?"no":""), (insecure?" +i":""));
2653 if(!ssl_print_name_dp(ssl, str, z->node.name,
2654 z->node.dclass, z->dp))
2659 /** do the list_auth_zones command */
2661 do_list_auth_zones(RES* ssl, struct auth_zones* az)
2663 struct auth_zone* z;
2664 char buf[257], buf2[256];
2665 lock_rw_rdlock(&az->lock);
2666 RBTREE_FOR(z, struct auth_zone*, &az->ztree) {
2667 lock_rw_rdlock(&z->lock);
2668 dname_str(z->name, buf);
2670 snprintf(buf2, sizeof(buf2), "expired");
2672 uint32_t serial = 0;
2673 if(auth_zone_get_serial(z, &serial))
2674 snprintf(buf2, sizeof(buf2), "serial %u",
2676 else snprintf(buf2, sizeof(buf2), "no serial");
2678 if(!ssl_printf(ssl, "%s\t%s\n", buf, buf2)) {
2679 /* failure to print */
2680 lock_rw_unlock(&z->lock);
2681 lock_rw_unlock(&az->lock);
2684 lock_rw_unlock(&z->lock);
2686 lock_rw_unlock(&az->lock);
2689 /** do the list_local_zones command */
2691 do_list_local_zones(RES* ssl, struct local_zones* zones)
2693 struct local_zone* z;
2695 lock_rw_rdlock(&zones->lock);
2696 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2697 lock_rw_rdlock(&z->lock);
2698 dname_str(z->name, buf);
2699 if(!ssl_printf(ssl, "%s %s\n", buf,
2700 local_zone_type2str(z->type))) {
2701 /* failure to print */
2702 lock_rw_unlock(&z->lock);
2703 lock_rw_unlock(&zones->lock);
2706 lock_rw_unlock(&z->lock);
2708 lock_rw_unlock(&zones->lock);
2711 /** do the list_local_data command */
2713 do_list_local_data(RES* ssl, struct worker* worker, struct local_zones* zones)
2715 struct local_zone* z;
2716 struct local_data* d;
2717 struct local_rrset* p;
2718 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
2719 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
2720 lock_rw_rdlock(&zones->lock);
2721 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2722 lock_rw_rdlock(&z->lock);
2723 RBTREE_FOR(d, struct local_data*, &z->data) {
2724 for(p = d->rrsets; p; p = p->next) {
2725 struct packed_rrset_data* d =
2726 (struct packed_rrset_data*)p->rrset->entry.data;
2728 for(i=0; i<d->count + d->rrsig_count; i++) {
2729 if(!packed_rr_to_string(p->rrset, i,
2731 if(!ssl_printf(ssl, "BADRR\n")) {
2732 lock_rw_unlock(&z->lock);
2733 lock_rw_unlock(&zones->lock);
2737 if(!ssl_printf(ssl, "%s\n", s)) {
2738 lock_rw_unlock(&z->lock);
2739 lock_rw_unlock(&zones->lock);
2745 lock_rw_unlock(&z->lock);
2747 lock_rw_unlock(&zones->lock);
2750 /** do the view_list_local_zones command */
2752 do_view_list_local_zones(RES* ssl, struct worker* worker, char* arg)
2754 struct view* v = views_find_view(worker->daemon->views,
2755 arg, 0 /* get read lock*/);
2757 ssl_printf(ssl,"no view with name: %s\n", arg);
2760 if(v->local_zones) {
2761 do_list_local_zones(ssl, v->local_zones);
2763 lock_rw_unlock(&v->lock);
2766 /** do the view_list_local_data command */
2768 do_view_list_local_data(RES* ssl, struct worker* worker, char* arg)
2770 struct view* v = views_find_view(worker->daemon->views,
2771 arg, 0 /* get read lock*/);
2773 ssl_printf(ssl,"no view with name: %s\n", arg);
2776 if(v->local_zones) {
2777 do_list_local_data(ssl, worker, v->local_zones);
2779 lock_rw_unlock(&v->lock);
2782 /** struct for user arg ratelimit list */
2783 struct ratelimit_list_arg {
2784 /** the infra cache */
2785 struct infra_cache* infra;
2786 /** the SSL to print to */
2788 /** all or only ratelimited */
2794 #define ip_ratelimit_list_arg ratelimit_list_arg
2796 /** list items in the ratelimit table */
2798 rate_list(struct lruhash_entry* e, void* arg)
2800 struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg;
2801 struct rate_key* k = (struct rate_key*)e->key;
2802 struct rate_data* d = (struct rate_data*)e->data;
2804 int lim = infra_find_ratelimit(a->infra, k->name, k->namelen);
2805 int max = infra_rate_max(d, a->now);
2810 dname_str(k->name, buf);
2811 ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim);
2814 /** list items in the ip_ratelimit table */
2816 ip_rate_list(struct lruhash_entry* e, void* arg)
2819 struct ip_ratelimit_list_arg* a = (struct ip_ratelimit_list_arg*)arg;
2820 struct ip_rate_key* k = (struct ip_rate_key*)e->key;
2821 struct ip_rate_data* d = (struct ip_rate_data*)e->data;
2822 int lim = infra_ip_ratelimit;
2823 int max = infra_rate_max(d, a->now);
2828 addr_to_str(&k->addr, k->addrlen, ip, sizeof(ip));
2829 ssl_printf(a->ssl, "%s %d limit %d\n", ip, max, lim);
2832 /** do the ratelimit_list command */
2834 do_ratelimit_list(RES* ssl, struct worker* worker, char* arg)
2836 struct ratelimit_list_arg a;
2838 a.infra = worker->env.infra_cache;
2839 a.now = *worker->env.now;
2841 arg = skipwhite(arg);
2842 if(strcmp(arg, "+a") == 0)
2844 if(a.infra->domain_rates==NULL ||
2845 (a.all == 0 && infra_dp_ratelimit == 0))
2847 slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a);
2850 /** do the ip_ratelimit_list command */
2852 do_ip_ratelimit_list(RES* ssl, struct worker* worker, char* arg)
2854 struct ip_ratelimit_list_arg a;
2856 a.infra = worker->env.infra_cache;
2857 a.now = *worker->env.now;
2859 arg = skipwhite(arg);
2860 if(strcmp(arg, "+a") == 0)
2862 if(a.infra->client_ip_rates==NULL ||
2863 (a.all == 0 && infra_ip_ratelimit == 0))
2865 slabhash_traverse(a.infra->client_ip_rates, 0, ip_rate_list, &a);
2868 /** tell other processes to execute the command */
2870 distribute_cmd(struct daemon_remote* rc, RES* ssl, char* cmd)
2875 /* skip i=0 which is me */
2876 for(i=1; i<rc->worker->daemon->num; i++) {
2877 worker_send_cmd(rc->worker->daemon->workers[i],
2879 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2880 (uint8_t*)cmd, strlen(cmd)+1, 0)) {
2881 ssl_printf(ssl, "error could not distribute cmd\n");
2887 /** check for name with end-of-string, space or tab after it */
2889 cmdcmp(char* p, const char* cmd, size_t len)
2891 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2894 /** execute a remote control command */
2896 execute_cmd(struct daemon_remote* rc, RES* ssl, char* cmd,
2897 struct worker* worker)
2899 char* p = skipwhite(cmd);
2900 /* compare command */
2901 if(cmdcmp(p, "stop", 4)) {
2902 do_stop(ssl, worker);
2904 } else if(cmdcmp(p, "reload", 6)) {
2905 do_reload(ssl, worker);
2907 } else if(cmdcmp(p, "stats_noreset", 13)) {
2908 do_stats(ssl, worker, 0);
2910 } else if(cmdcmp(p, "stats", 5)) {
2911 do_stats(ssl, worker, 1);
2913 } else if(cmdcmp(p, "status", 6)) {
2914 do_status(ssl, worker);
2916 } else if(cmdcmp(p, "dump_cache", 10)) {
2917 (void)dump_cache(ssl, worker);
2919 } else if(cmdcmp(p, "load_cache", 10)) {
2920 if(load_cache(ssl, worker)) send_ok(ssl);
2922 } else if(cmdcmp(p, "list_forwards", 13)) {
2923 do_list_forwards(ssl, worker);
2925 } else if(cmdcmp(p, "list_stubs", 10)) {
2926 do_list_stubs(ssl, worker);
2928 } else if(cmdcmp(p, "list_insecure", 13)) {
2929 do_insecure_list(ssl, worker);
2931 } else if(cmdcmp(p, "list_local_zones", 16)) {
2932 do_list_local_zones(ssl, worker->daemon->local_zones);
2934 } else if(cmdcmp(p, "list_local_data", 15)) {
2935 do_list_local_data(ssl, worker, worker->daemon->local_zones);
2937 } else if(cmdcmp(p, "view_list_local_zones", 21)) {
2938 do_view_list_local_zones(ssl, worker, skipwhite(p+21));
2940 } else if(cmdcmp(p, "view_list_local_data", 20)) {
2941 do_view_list_local_data(ssl, worker, skipwhite(p+20));
2943 } else if(cmdcmp(p, "ratelimit_list", 14)) {
2944 do_ratelimit_list(ssl, worker, p+14);
2946 } else if(cmdcmp(p, "ip_ratelimit_list", 17)) {
2947 do_ip_ratelimit_list(ssl, worker, p+17);
2949 } else if(cmdcmp(p, "list_auth_zones", 15)) {
2950 do_list_auth_zones(ssl, worker->env.auth_zones);
2952 } else if(cmdcmp(p, "auth_zone_reload", 16)) {
2953 do_auth_zone_reload(ssl, worker, skipwhite(p+16));
2955 } else if(cmdcmp(p, "auth_zone_transfer", 18)) {
2956 do_auth_zone_transfer(ssl, worker, skipwhite(p+18));
2958 } else if(cmdcmp(p, "stub_add", 8)) {
2959 /* must always distribute this cmd */
2960 if(rc) distribute_cmd(rc, ssl, cmd);
2961 do_stub_add(ssl, worker, skipwhite(p+8));
2963 } else if(cmdcmp(p, "stub_remove", 11)) {
2964 /* must always distribute this cmd */
2965 if(rc) distribute_cmd(rc, ssl, cmd);
2966 do_stub_remove(ssl, worker, skipwhite(p+11));
2968 } else if(cmdcmp(p, "forward_add", 11)) {
2969 /* must always distribute this cmd */
2970 if(rc) distribute_cmd(rc, ssl, cmd);
2971 do_forward_add(ssl, worker, skipwhite(p+11));
2973 } else if(cmdcmp(p, "forward_remove", 14)) {
2974 /* must always distribute this cmd */
2975 if(rc) distribute_cmd(rc, ssl, cmd);
2976 do_forward_remove(ssl, worker, skipwhite(p+14));
2978 } else if(cmdcmp(p, "insecure_add", 12)) {
2979 /* must always distribute this cmd */
2980 if(rc) distribute_cmd(rc, ssl, cmd);
2981 do_insecure_add(ssl, worker, skipwhite(p+12));
2983 } else if(cmdcmp(p, "insecure_remove", 15)) {
2984 /* must always distribute this cmd */
2985 if(rc) distribute_cmd(rc, ssl, cmd);
2986 do_insecure_remove(ssl, worker, skipwhite(p+15));
2988 } else if(cmdcmp(p, "forward", 7)) {
2989 /* must always distribute this cmd */
2990 if(rc) distribute_cmd(rc, ssl, cmd);
2991 do_forward(ssl, worker, skipwhite(p+7));
2993 } else if(cmdcmp(p, "flush_stats", 11)) {
2994 /* must always distribute this cmd */
2995 if(rc) distribute_cmd(rc, ssl, cmd);
2996 do_flush_stats(ssl, worker);
2998 } else if(cmdcmp(p, "flush_requestlist", 17)) {
2999 /* must always distribute this cmd */
3000 if(rc) distribute_cmd(rc, ssl, cmd);
3001 do_flush_requestlist(ssl, worker);
3003 } else if(cmdcmp(p, "lookup", 6)) {
3004 do_lookup(ssl, worker, skipwhite(p+6));
3008 #ifdef THREADS_DISABLED
3009 /* other processes must execute the command as well */
3010 /* commands that should not be distributed, returned above. */
3011 if(rc) { /* only if this thread is the master (rc) thread */
3012 /* done before the code below, which may split the string */
3013 distribute_cmd(rc, ssl, cmd);
3016 if(cmdcmp(p, "verbosity", 9)) {
3017 do_verbosity(ssl, skipwhite(p+9));
3018 } else if(cmdcmp(p, "local_zone_remove", 17)) {
3019 do_zone_remove(ssl, worker->daemon->local_zones, skipwhite(p+17));
3020 } else if(cmdcmp(p, "local_zones_remove", 18)) {
3021 do_zones_remove(ssl, worker->daemon->local_zones);
3022 } else if(cmdcmp(p, "local_zone", 10)) {
3023 do_zone_add(ssl, worker->daemon->local_zones, skipwhite(p+10));
3024 } else if(cmdcmp(p, "local_zones", 11)) {
3025 do_zones_add(ssl, worker->daemon->local_zones);
3026 } else if(cmdcmp(p, "local_data_remove", 17)) {
3027 do_data_remove(ssl, worker->daemon->local_zones, skipwhite(p+17));
3028 } else if(cmdcmp(p, "local_datas_remove", 18)) {
3029 do_datas_remove(ssl, worker->daemon->local_zones);
3030 } else if(cmdcmp(p, "local_data", 10)) {
3031 do_data_add(ssl, worker->daemon->local_zones, skipwhite(p+10));
3032 } else if(cmdcmp(p, "local_datas", 11)) {
3033 do_datas_add(ssl, worker->daemon->local_zones);
3034 } else if(cmdcmp(p, "view_local_zone_remove", 22)) {
3035 do_view_zone_remove(ssl, worker, skipwhite(p+22));
3036 } else if(cmdcmp(p, "view_local_zone", 15)) {
3037 do_view_zone_add(ssl, worker, skipwhite(p+15));
3038 } else if(cmdcmp(p, "view_local_data_remove", 22)) {
3039 do_view_data_remove(ssl, worker, skipwhite(p+22));
3040 } else if(cmdcmp(p, "view_local_datas_remove", 23)){
3041 do_view_datas_remove(ssl, worker, skipwhite(p+23));
3042 } else if(cmdcmp(p, "view_local_data", 15)) {
3043 do_view_data_add(ssl, worker, skipwhite(p+15));
3044 } else if(cmdcmp(p, "view_local_datas", 16)) {
3045 do_view_datas_add(ssl, worker, skipwhite(p+16));
3046 } else if(cmdcmp(p, "flush_zone", 10)) {
3047 do_flush_zone(ssl, worker, skipwhite(p+10));
3048 } else if(cmdcmp(p, "flush_type", 10)) {
3049 do_flush_type(ssl, worker, skipwhite(p+10));
3050 } else if(cmdcmp(p, "flush_infra", 11)) {
3051 do_flush_infra(ssl, worker, skipwhite(p+11));
3052 } else if(cmdcmp(p, "flush", 5)) {
3053 do_flush_name(ssl, worker, skipwhite(p+5));
3054 } else if(cmdcmp(p, "dump_requestlist", 16)) {
3055 do_dump_requestlist(ssl, worker);
3056 } else if(cmdcmp(p, "dump_infra", 10)) {
3057 do_dump_infra(ssl, worker);
3058 } else if(cmdcmp(p, "log_reopen", 10)) {
3059 do_log_reopen(ssl, worker);
3060 } else if(cmdcmp(p, "set_option", 10)) {
3061 do_set_option(ssl, worker, skipwhite(p+10));
3062 } else if(cmdcmp(p, "get_option", 10)) {
3063 do_get_option(ssl, worker, skipwhite(p+10));
3064 } else if(cmdcmp(p, "flush_bogus", 11)) {
3065 do_flush_bogus(ssl, worker);
3066 } else if(cmdcmp(p, "flush_negative", 14)) {
3067 do_flush_negative(ssl, worker);
3069 (void)ssl_printf(ssl, "error unknown command '%s'\n", p);
3074 daemon_remote_exec(struct worker* worker)
3076 /* read the cmd string */
3077 uint8_t* msg = NULL;
3079 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
3080 log_err("daemon_remote_exec: tube_read_msg failed");
3083 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
3084 execute_cmd(NULL, NULL, (char*)msg, worker);
3088 /** handle remote control request */
3090 handle_req(struct daemon_remote* rc, struct rc_state* s, RES* res)
3097 /* makes it possible to set the socket blocking again. */
3098 /* basically removes it from winsock_event ... */
3099 WSAEventSelect(s->c->fd, NULL, 0);
3101 fd_set_block(s->c->fd);
3103 /* try to read magic UBCT[version]_space_ string */
3106 if((r=SSL_read(res->ssl, magic, (int)sizeof(magic)-1)) <= 0) {
3107 if(SSL_get_error(res->ssl, r) == SSL_ERROR_ZERO_RETURN)
3109 log_crypto_err("could not SSL_read");
3114 ssize_t rr = recv(res->fd, magic, sizeof(magic)-1, 0);
3117 if(errno == EINTR || errno == EAGAIN)
3120 log_err("could not recv: %s", strerror(errno));
3122 log_err("could not recv: %s", wsa_strerror(WSAGetLastError()));
3131 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
3132 verbose(VERB_QUERY, "control connection has bad magic string");
3133 /* probably wrong tool connected, ignore it completely */
3137 /* read the command line */
3138 if(!ssl_read_line(res, buf, sizeof(buf))) {
3141 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
3142 if(strcmp(magic, pre) != 0) {
3143 verbose(VERB_QUERY, "control connection had bad "
3144 "version %s, cmd: %s", magic, buf);
3145 ssl_printf(res, "error version mismatch\n");
3148 verbose(VERB_DETAIL, "control cmd: %s", buf);
3150 /* figure out what to do */
3151 execute_cmd(rc, res, buf, rc->worker);
3154 /** handle SSL_do_handshake changes to the file descriptor to wait for later */
3156 remote_handshake_later(struct daemon_remote* rc, struct rc_state* s,
3157 struct comm_point* c, int r, int r2)
3159 if(r2 == SSL_ERROR_WANT_READ) {
3160 if(s->shake_state == rc_hs_read) {
3161 /* try again later */
3164 s->shake_state = rc_hs_read;
3165 comm_point_listen_for_rw(c, 1, 0);
3167 } else if(r2 == SSL_ERROR_WANT_WRITE) {
3168 if(s->shake_state == rc_hs_write) {
3169 /* try again later */
3172 s->shake_state = rc_hs_write;
3173 comm_point_listen_for_rw(c, 0, 1);
3177 log_err("remote control connection closed prematurely");
3178 log_addr(VERB_OPS, "failed connection from",
3179 &s->c->repinfo.addr, s->c->repinfo.addrlen);
3180 log_crypto_err("remote control failed ssl");
3186 int remote_control_callback(struct comm_point* c, void* arg, int err,
3187 struct comm_reply* ATTR_UNUSED(rep))
3190 struct rc_state* s = (struct rc_state*)arg;
3191 struct daemon_remote* rc = s->rc;
3193 if(err != NETEVENT_NOERROR) {
3194 if(err==NETEVENT_TIMEOUT)
3195 log_err("remote control timed out");
3200 /* (continue to) setup the SSL connection */
3202 r = SSL_do_handshake(s->ssl);
3204 int r2 = SSL_get_error(s->ssl, r);
3205 return remote_handshake_later(rc, s, c, r, r2);
3207 s->shake_state = rc_none;
3210 /* once handshake has completed, check authentication */
3211 if (!rc->use_cert) {
3212 verbose(VERB_ALGO, "unauthenticated remote control connection");
3213 } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
3214 X509* x = SSL_get_peer_certificate(s->ssl);
3216 verbose(VERB_DETAIL, "remote control connection "
3217 "provided no client certificate");
3221 verbose(VERB_ALGO, "remote control connection authenticated");
3224 verbose(VERB_DETAIL, "remote control connection failed to "
3225 "authenticate with client certificate");
3230 /* if OK start to actually handle the request */
3233 handle_req(rc, s, &res);
3235 verbose(VERB_ALGO, "remote control operation completed");