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 "util/storage/slabhash.h"
73 #include "util/fptr_wlist.h"
74 #include "util/data/dname.h"
75 #include "validator/validator.h"
76 #include "validator/val_kcache.h"
77 #include "validator/val_kentry.h"
78 #include "validator/val_anchor.h"
79 #include "iterator/iterator.h"
80 #include "iterator/iter_fwd.h"
81 #include "iterator/iter_hints.h"
82 #include "iterator/iter_delegpt.h"
83 #include "services/outbound_list.h"
84 #include "services/outside_network.h"
85 #include "sldns/str2wire.h"
86 #include "sldns/parseutil.h"
87 #include "sldns/wire2str.h"
88 #include "sldns/sbuffer.h"
90 #ifdef HAVE_SYS_TYPES_H
91 # include <sys/types.h>
93 #ifdef HAVE_SYS_STAT_H
100 /* just for portability */
105 /** what to put on statistics lines between var and value, ": " or "=" */
107 /** if true, inhibits a lot of =0 lines from the stats output */
108 static const int inhibit_zero = 1;
110 /** subtract timers and the values do not overflow or become negative */
112 timeval_subtract(struct timeval* d, const struct timeval* end,
113 const struct timeval* start)
116 time_t end_usec = end->tv_usec;
117 d->tv_sec = end->tv_sec - start->tv_sec;
118 if(end_usec < start->tv_usec) {
122 d->tv_usec = end_usec - start->tv_usec;
126 /** divide sum of timers to get average */
128 timeval_divide(struct timeval* avg, const struct timeval* sum, long long d)
137 avg->tv_sec = sum->tv_sec / d;
138 avg->tv_usec = sum->tv_usec / d;
139 /* handle fraction from seconds divide */
140 leftover = sum->tv_sec - avg->tv_sec*d;
141 avg->tv_usec += (leftover*1000000)/d;
146 remote_setup_ctx(struct daemon_remote* rc, struct config_file* cfg)
150 rc->ctx = SSL_CTX_new(SSLv23_server_method());
152 log_crypto_err("could not SSL_CTX_new");
155 if(!listen_sslctx_setup(rc->ctx)) {
159 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
160 s_key = fname_after_chroot(cfg->server_key_file, cfg, 1);
161 if(!s_cert || !s_key) {
162 log_err("out of memory in remote control fname");
165 verbose(VERB_ALGO, "setup SSL certificates");
166 if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) {
167 log_err("Error for server-cert-file: %s", s_cert);
168 log_crypto_err("Error in SSL_CTX use_certificate_chain_file");
171 if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) {
172 log_err("Error for server-key-file: %s", s_key);
173 log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
176 if(!SSL_CTX_check_private_key(rc->ctx)) {
177 log_err("Error for server-key-file: %s", s_key);
178 log_crypto_err("Error in SSL_CTX check_private_key");
181 listen_sslctx_setup_2(rc->ctx);
182 if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) {
183 log_crypto_err("Error setting up SSL_CTX verify locations");
189 SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert));
190 SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL);
196 struct daemon_remote*
197 daemon_remote_create(struct config_file* cfg)
199 struct daemon_remote* rc = (struct daemon_remote*)calloc(1,
202 log_err("out of memory in daemon_remote_create");
207 if(!cfg->remote_control_enable) {
211 if(options_remote_is_address(cfg) && cfg->control_use_cert) {
212 if(!remote_setup_ctx(rc, cfg)) {
213 daemon_remote_delete(rc);
218 struct config_strlist* p;
221 if(!options_remote_is_address(cfg))
222 for(p = cfg->control_ifs.first; p; p = p->next) {
223 if(p->str && p->str[0] != '/')
224 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);
230 void daemon_remote_clear(struct daemon_remote* rc)
232 struct rc_state* p, *np;
234 /* but do not close the ports */
235 listen_list_delete(rc->accept_list);
236 rc->accept_list = NULL;
237 /* do close these sockets */
243 comm_point_delete(p->c);
247 rc->busy_list = NULL;
252 void daemon_remote_delete(struct daemon_remote* rc)
255 daemon_remote_clear(rc);
257 SSL_CTX_free(rc->ctx);
263 * Add and open a new control port
266 * @param list: list head
267 * @param noproto_is_err: if lack of protocol support is an error.
268 * @param cfg: config with username for chown of unix-sockets.
269 * @return false on failure.
272 add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err,
273 struct config_file* cfg)
275 struct addrinfo hints;
276 struct addrinfo* res;
277 struct listen_port* n;
281 snprintf(port, sizeof(port), "%d", nr);
282 port[sizeof(port)-1]=0;
283 memset(&hints, 0, sizeof(hints));
287 /* This looks like a local socket */
288 fd = create_local_accept_sock(ip, &noproto, cfg->use_systemd);
290 * Change socket ownership and permissions so users other
291 * than root can access it provided they are in the same
292 * group as the user we run as.
296 if (cfg->username && cfg->username[0] &&
297 cfg_uid != (uid_t)-1) {
298 if(chown(ip, cfg_uid, cfg_gid) == -1)
299 verbose(VERB_QUERY, "cannot chown %u.%u %s: %s",
300 (unsigned)cfg_uid, (unsigned)cfg_gid,
301 ip, strerror(errno));
303 chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
309 hints.ai_socktype = SOCK_STREAM;
310 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
311 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
313 if(!noproto_is_err && r == EAI_NONAME) {
314 /* tried to lookup the address as name */
315 return 1; /* return success, but do nothing */
317 #endif /* USE_WINSOCK */
318 log_err("control interface %s:%s getaddrinfo: %s %s",
319 ip?ip:"default", port, gai_strerror(r),
321 r==EAI_SYSTEM?(char*)strerror(errno):""
330 fd = create_tcp_accept_sock(res, 1, &noproto, 0,
331 cfg->ip_transparent, 0, cfg->ip_freebind, cfg->use_systemd);
335 if(fd == -1 && noproto) {
337 return 1; /* return success, but do nothing */
338 log_err("cannot open control interface %s %d : "
339 "protocol not supported", ip, nr);
343 log_err("cannot open control interface %s %d", ip, nr);
348 n = (struct listen_port*)calloc(1, sizeof(*n));
355 log_err("out of memory");
364 struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
366 struct listen_port* l = NULL;
367 log_assert(cfg->remote_control_enable && cfg->control_port);
368 if(cfg->control_ifs.first) {
369 struct config_strlist* p;
370 for(p = cfg->control_ifs.first; p; p = p->next) {
371 if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) {
372 listening_ports_free(l);
379 !add_open("::1", cfg->control_port, &l, 0, cfg)) {
380 listening_ports_free(l);
384 !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) {
385 listening_ports_free(l);
392 /** open accept commpoint */
394 accept_open(struct daemon_remote* rc, int fd)
396 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
398 log_err("out of memory");
401 n->next = rc->accept_list;
404 n->com = comm_point_create_raw(rc->worker->base, fd, 0,
405 &remote_accept_callback, rc);
408 /* keep this port open, its fd is kept in the rc portlist */
409 n->com->do_not_close = 1;
413 int daemon_remote_open_accept(struct daemon_remote* rc,
414 struct listen_port* ports, struct worker* worker)
416 struct listen_port* p;
418 for(p = ports; p; p = p->next) {
419 if(!accept_open(rc, p->fd)) {
420 log_err("could not create accept comm point");
427 void daemon_remote_stop_accept(struct daemon_remote* rc)
429 struct listen_list* p;
430 for(p=rc->accept_list; p; p=p->next) {
431 comm_point_stop_listening(p->com);
435 void daemon_remote_start_accept(struct daemon_remote* rc)
437 struct listen_list* p;
438 for(p=rc->accept_list; p; p=p->next) {
439 comm_point_start_listening(p->com, -1, -1);
443 int remote_accept_callback(struct comm_point* c, void* arg, int err,
444 struct comm_reply* ATTR_UNUSED(rep))
446 struct daemon_remote* rc = (struct daemon_remote*)arg;
447 struct sockaddr_storage addr;
451 if(err != NETEVENT_NOERROR) {
452 log_err("error %d on remote_accept_callback", err);
455 /* perform the accept */
456 newfd = comm_point_perform_accept(c, &addr, &addrlen);
459 /* create new commpoint unless we are servicing already */
460 if(rc->active >= rc->max_active) {
461 log_warn("drop incoming remote control: too many connections");
471 /* setup commpoint to service the remote control command */
472 n = (struct rc_state*)calloc(1, sizeof(*n));
474 log_err("out of memory");
478 /* start in reading state */
479 n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
480 &remote_control_callback, n);
482 log_err("out of memory");
486 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
487 n->c->do_not_close = 0;
488 comm_point_stop_listening(n->c);
489 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
490 memcpy(&n->c->repinfo.addr, &addr, addrlen);
491 n->c->repinfo.addrlen = addrlen;
493 n->shake_state = rc_hs_read;
494 n->ssl = SSL_new(rc->ctx);
496 log_crypto_err("could not SSL_new");
497 comm_point_delete(n->c);
501 SSL_set_accept_state(n->ssl);
502 (void)SSL_set_mode(n->ssl, (long)SSL_MODE_AUTO_RETRY);
503 if(!SSL_set_fd(n->ssl, newfd)) {
504 log_crypto_err("could not SSL_set_fd");
506 comm_point_delete(n->c);
515 n->next = rc->busy_list;
519 /* perform the first nonblocking read already, for windows,
520 * so it can return wouldblock. could be faster too. */
521 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
525 /** delete from list */
527 state_list_remove_elem(struct rc_state** list, struct comm_point* c)
530 if( (*list)->c == c) {
531 *list = (*list)->next;
534 list = &(*list)->next;
538 /** decrease active count and remove commpoint from busy list */
540 clean_point(struct daemon_remote* rc, struct rc_state* s)
542 state_list_remove_elem(&rc->busy_list, s->c);
545 SSL_shutdown(s->ssl);
548 comm_point_delete(s->c);
553 ssl_print_text(RES* res, const char* text)
560 if((r=SSL_write(res->ssl, text, (int)strlen(text))) <= 0) {
561 if(SSL_get_error(res->ssl, r) == SSL_ERROR_ZERO_RETURN) {
562 verbose(VERB_QUERY, "warning, in SSL_write, peer "
563 "closed connection");
566 log_crypto_err("could not SSL_write");
571 while(at < strlen(text)) {
572 ssize_t r = send(res->fd, text+at, strlen(text)-at, 0);
574 if(errno == EAGAIN || errno == EINTR)
577 log_err("could not send: %s", strerror(errno));
579 log_err("could not send: %s", wsa_strerror(WSAGetLastError()));
589 /** print text over the ssl connection */
591 ssl_print_vmsg(RES* ssl, const char* format, va_list args)
594 vsnprintf(msg, sizeof(msg), format, args);
595 return ssl_print_text(ssl, msg);
598 /** printf style printing to the ssl connection */
599 int ssl_printf(RES* ssl, const char* format, ...)
603 va_start(args, format);
604 ret = ssl_print_vmsg(ssl, format, args);
610 ssl_read_line(RES* res, char* buf, size_t max)
619 if((r=SSL_read(res->ssl, buf+len, 1)) <= 0) {
620 if(SSL_get_error(res->ssl, r) == SSL_ERROR_ZERO_RETURN) {
624 log_crypto_err("could not SSL_read");
629 ssize_t rr = recv(res->fd, buf+len, 1, 0);
635 if(errno == EINTR || errno == EAGAIN)
638 log_err("could not recv: %s", strerror(errno));
640 log_err("could not recv: %s", wsa_strerror(WSAGetLastError()));
647 if(buf[len] == '\n') {
648 /* return string without \n */
655 log_err("control line too long (%d): %s", (int)max, buf);
659 /** skip whitespace, return new pointer into string */
663 /* EOS \0 is not a space */
664 while( isspace((unsigned char)*str) )
669 /** send the OK to the control client */
670 static void send_ok(RES* ssl)
672 (void)ssl_printf(ssl, "ok\n");
675 /** do the stop command */
677 do_stop(RES* ssl, struct worker* worker)
679 worker->need_to_exit = 1;
680 comm_base_exit(worker->base);
684 /** do the reload command */
686 do_reload(RES* ssl, struct worker* worker)
688 worker->need_to_exit = 0;
689 comm_base_exit(worker->base);
693 /** do the verbosity command */
695 do_verbosity(RES* ssl, char* str)
698 if(val == 0 && strcmp(str, "0") != 0) {
699 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
706 /** print stats from statinfo */
708 print_stats(RES* ssl, const char* nm, struct ub_stats_info* s)
710 struct timeval sumwait, avg;
711 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm,
712 (unsigned long)s->svr.num_queries)) return 0;
713 if(!ssl_printf(ssl, "%s.num.queries_ip_ratelimited"SQ"%lu\n", nm,
714 (unsigned long)s->svr.num_queries_ip_ratelimited)) return 0;
715 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm,
716 (unsigned long)(s->svr.num_queries
717 - s->svr.num_queries_missed_cache))) return 0;
718 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm,
719 (unsigned long)s->svr.num_queries_missed_cache)) return 0;
720 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm,
721 (unsigned long)s->svr.num_queries_prefetch)) return 0;
722 if(!ssl_printf(ssl, "%s.num.zero_ttl"SQ"%lu\n", nm,
723 (unsigned long)s->svr.zero_ttl_responses)) return 0;
724 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm,
725 (unsigned long)s->mesh_replies_sent)) return 0;
727 if(!ssl_printf(ssl, "%s.num.dnscrypt.crypted"SQ"%lu\n", nm,
728 (unsigned long)s->svr.num_query_dnscrypt_crypted)) return 0;
729 if(!ssl_printf(ssl, "%s.num.dnscrypt.cert"SQ"%lu\n", nm,
730 (unsigned long)s->svr.num_query_dnscrypt_cert)) return 0;
731 if(!ssl_printf(ssl, "%s.num.dnscrypt.cleartext"SQ"%lu\n", nm,
732 (unsigned long)s->svr.num_query_dnscrypt_cleartext)) return 0;
733 if(!ssl_printf(ssl, "%s.num.dnscrypt.malformed"SQ"%lu\n", nm,
734 (unsigned long)s->svr.num_query_dnscrypt_crypted_malformed)) return 0;
736 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
737 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
738 (double)s->svr.sum_query_list_size/
739 (double)(s->svr.num_queries_missed_cache+
740 s->svr.num_queries_prefetch) : 0.0)) return 0;
741 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm,
742 (unsigned long)s->svr.max_query_list_size)) return 0;
743 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm,
744 (unsigned long)s->mesh_jostled)) return 0;
745 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm,
746 (unsigned long)s->mesh_dropped)) return 0;
747 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm,
748 (unsigned long)s->mesh_num_states)) return 0;
749 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm,
750 (unsigned long)s->mesh_num_reply_states)) return 0;
752 sumwait.tv_sec = s->mesh_replies_sum_wait_sec;
753 sumwait.tv_usec = s->mesh_replies_sum_wait_usec;
755 timeval_divide(&avg, &sumwait, s->mesh_replies_sent);
756 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
757 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
758 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
759 s->mesh_time_median)) return 0;
760 if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm,
761 (unsigned long)s->svr.tcp_accept_usage)) return 0;
765 /** print stats for one thread */
767 print_thread_stats(RES* ssl, int i, struct ub_stats_info* s)
770 snprintf(nm, sizeof(nm), "thread%d", i);
772 return print_stats(ssl, nm, s);
775 /** print long number */
777 print_longnum(RES* ssl, const char* desc, size_t x)
779 if(x > 1024*1024*1024) {
781 size_t front = x / (size_t)1000000;
782 size_t back = x % (size_t)1000000;
783 return ssl_printf(ssl, "%s%u%6.6u\n", desc,
784 (unsigned)front, (unsigned)back);
786 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x);
790 /** print mem stats */
792 print_mem(RES* ssl, struct worker* worker, struct daemon* daemon,
793 struct ub_stats_info* s)
795 size_t msg, rrset, val, iter, respip;
798 #endif /* CLIENT_SUBNET */
801 #endif /* USE_IPSECMOD */
803 size_t dnscrypt_shared_secret = 0;
804 size_t dnscrypt_nonce = 0;
805 #endif /* USE_DNSCRYPT */
806 msg = slabhash_get_mem(daemon->env->msg_cache);
807 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
808 val = mod_get_mem(&worker->env, "validator");
809 iter = mod_get_mem(&worker->env, "iterator");
810 respip = mod_get_mem(&worker->env, "respip");
812 subnet = mod_get_mem(&worker->env, "subnet");
813 #endif /* CLIENT_SUBNET */
815 ipsecmod = mod_get_mem(&worker->env, "ipsecmod");
816 #endif /* USE_IPSECMOD */
818 if(daemon->dnscenv) {
819 dnscrypt_shared_secret = slabhash_get_mem(
820 daemon->dnscenv->shared_secrets_cache);
821 dnscrypt_nonce = slabhash_get_mem(daemon->dnscenv->nonces_cache);
823 #endif /* USE_DNSCRYPT */
825 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
827 if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
829 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
831 if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
833 if(!print_longnum(ssl, "mem.mod.respip"SQ, respip))
836 if(!print_longnum(ssl, "mem.mod.subnet"SQ, subnet))
838 #endif /* CLIENT_SUBNET */
840 if(!print_longnum(ssl, "mem.mod.ipsecmod"SQ, ipsecmod))
842 #endif /* USE_IPSECMOD */
844 if(!print_longnum(ssl, "mem.cache.dnscrypt_shared_secret"SQ,
845 dnscrypt_shared_secret))
847 if(!print_longnum(ssl, "mem.cache.dnscrypt_nonce"SQ,
850 #endif /* USE_DNSCRYPT */
851 if(!print_longnum(ssl, "mem.streamwait"SQ,
852 (size_t)s->svr.mem_stream_wait))
857 /** print uptime stats */
859 print_uptime(RES* ssl, struct worker* worker, int reset)
861 struct timeval now = *worker->env.now_tv;
862 struct timeval up, dt;
863 timeval_subtract(&up, &now, &worker->daemon->time_boot);
864 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
866 worker->daemon->time_last_stat = now;
867 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
868 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
869 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
870 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
871 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
872 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
876 /** print extended histogram */
878 print_hist(RES* ssl, struct ub_stats_info* s)
880 struct timehist* hist;
882 hist = timehist_setup();
884 log_err("out of memory");
887 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
888 for(i=0; i<hist->num; i++) {
890 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
891 (int)hist->buckets[i].lower.tv_sec,
892 (int)hist->buckets[i].lower.tv_usec,
893 (int)hist->buckets[i].upper.tv_sec,
894 (int)hist->buckets[i].upper.tv_usec,
895 (unsigned long)hist->buckets[i].count)) {
896 timehist_delete(hist);
900 timehist_delete(hist);
904 /** print extended stats */
906 print_ext(RES* ssl, struct ub_stats_info* s)
910 const sldns_rr_descriptor* desc;
911 const sldns_lookup_table* lt;
913 for(i=0; i<UB_STATS_QTYPE_NUM; i++) {
914 if(inhibit_zero && s->svr.qtype[i] == 0)
916 desc = sldns_rr_descript((uint16_t)i);
917 if(desc && desc->_name) {
918 snprintf(nm, sizeof(nm), "%s", desc->_name);
919 } else if (i == LDNS_RR_TYPE_IXFR) {
920 snprintf(nm, sizeof(nm), "IXFR");
921 } else if (i == LDNS_RR_TYPE_AXFR) {
922 snprintf(nm, sizeof(nm), "AXFR");
923 } else if (i == LDNS_RR_TYPE_MAILA) {
924 snprintf(nm, sizeof(nm), "MAILA");
925 } else if (i == LDNS_RR_TYPE_MAILB) {
926 snprintf(nm, sizeof(nm), "MAILB");
927 } else if (i == LDNS_RR_TYPE_ANY) {
928 snprintf(nm, sizeof(nm), "ANY");
930 snprintf(nm, sizeof(nm), "TYPE%d", i);
932 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n",
933 nm, (unsigned long)s->svr.qtype[i])) return 0;
935 if(!inhibit_zero || s->svr.qtype_big) {
936 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n",
937 (unsigned long)s->svr.qtype_big)) return 0;
940 for(i=0; i<UB_STATS_QCLASS_NUM; i++) {
941 if(inhibit_zero && s->svr.qclass[i] == 0)
943 lt = sldns_lookup_by_id(sldns_rr_classes, i);
945 snprintf(nm, sizeof(nm), "%s", lt->name);
947 snprintf(nm, sizeof(nm), "CLASS%d", i);
949 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n",
950 nm, (unsigned long)s->svr.qclass[i])) return 0;
952 if(!inhibit_zero || s->svr.qclass_big) {
953 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n",
954 (unsigned long)s->svr.qclass_big)) return 0;
957 for(i=0; i<UB_STATS_OPCODE_NUM; i++) {
958 if(inhibit_zero && s->svr.qopcode[i] == 0)
960 lt = sldns_lookup_by_id(sldns_opcodes, i);
962 snprintf(nm, sizeof(nm), "%s", lt->name);
964 snprintf(nm, sizeof(nm), "OPCODE%d", i);
966 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n",
967 nm, (unsigned long)s->svr.qopcode[i])) return 0;
970 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n",
971 (unsigned long)s->svr.qtcp)) return 0;
972 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n",
973 (unsigned long)s->svr.qtcp_outgoing)) return 0;
974 if(!ssl_printf(ssl, "num.query.tls"SQ"%lu\n",
975 (unsigned long)s->svr.qtls)) return 0;
976 if(!ssl_printf(ssl, "num.query.tls.resume"SQ"%lu\n",
977 (unsigned long)s->svr.qtls_resume)) return 0;
978 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n",
979 (unsigned long)s->svr.qipv6)) return 0;
981 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n",
982 (unsigned long)s->svr.qbit_QR)) return 0;
983 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n",
984 (unsigned long)s->svr.qbit_AA)) return 0;
985 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n",
986 (unsigned long)s->svr.qbit_TC)) return 0;
987 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n",
988 (unsigned long)s->svr.qbit_RD)) return 0;
989 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n",
990 (unsigned long)s->svr.qbit_RA)) return 0;
991 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n",
992 (unsigned long)s->svr.qbit_Z)) return 0;
993 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n",
994 (unsigned long)s->svr.qbit_AD)) return 0;
995 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n",
996 (unsigned long)s->svr.qbit_CD)) return 0;
997 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n",
998 (unsigned long)s->svr.qEDNS)) return 0;
999 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n",
1000 (unsigned long)s->svr.qEDNS_DO)) return 0;
1003 for(i=0; i<UB_STATS_RCODE_NUM; i++) {
1004 /* Always include RCODEs 0-5 */
1005 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
1007 lt = sldns_lookup_by_id(sldns_rcodes, i);
1008 if(lt && lt->name) {
1009 snprintf(nm, sizeof(nm), "%s", lt->name);
1011 snprintf(nm, sizeof(nm), "RCODE%d", i);
1013 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n",
1014 nm, (unsigned long)s->svr.ans_rcode[i])) return 0;
1016 if(!inhibit_zero || s->svr.ans_rcode_nodata) {
1017 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n",
1018 (unsigned long)s->svr.ans_rcode_nodata)) return 0;
1021 if(!ssl_printf(ssl, "num.query.ratelimited"SQ"%lu\n",
1022 (unsigned long)s->svr.queries_ratelimited)) return 0;
1024 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n",
1025 (unsigned long)s->svr.ans_secure)) return 0;
1026 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n",
1027 (unsigned long)s->svr.ans_bogus)) return 0;
1028 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n",
1029 (unsigned long)s->svr.rrset_bogus)) return 0;
1030 if(!ssl_printf(ssl, "num.query.aggressive.NOERROR"SQ"%lu\n",
1031 (unsigned long)s->svr.num_neg_cache_noerror)) return 0;
1032 if(!ssl_printf(ssl, "num.query.aggressive.NXDOMAIN"SQ"%lu\n",
1033 (unsigned long)s->svr.num_neg_cache_nxdomain)) return 0;
1034 /* threat detection */
1035 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n",
1036 (unsigned long)s->svr.unwanted_queries)) return 0;
1037 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n",
1038 (unsigned long)s->svr.unwanted_replies)) return 0;
1040 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
1041 (unsigned)s->svr.msg_cache_count)) return 0;
1042 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
1043 (unsigned)s->svr.rrset_cache_count)) return 0;
1044 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
1045 (unsigned)s->svr.infra_cache_count)) return 0;
1046 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
1047 (unsigned)s->svr.key_cache_count)) return 0;
1049 if(!ssl_printf(ssl, "dnscrypt_shared_secret.cache.count"SQ"%u\n",
1050 (unsigned)s->svr.shared_secret_cache_count)) return 0;
1051 if(!ssl_printf(ssl, "dnscrypt_nonce.cache.count"SQ"%u\n",
1052 (unsigned)s->svr.nonce_cache_count)) return 0;
1053 if(!ssl_printf(ssl, "num.query.dnscrypt.shared_secret.cachemiss"SQ"%lu\n",
1054 (unsigned long)s->svr.num_query_dnscrypt_secret_missed_cache)) return 0;
1055 if(!ssl_printf(ssl, "num.query.dnscrypt.replay"SQ"%lu\n",
1056 (unsigned long)s->svr.num_query_dnscrypt_replay)) return 0;
1057 #endif /* USE_DNSCRYPT */
1058 if(!ssl_printf(ssl, "num.query.authzone.up"SQ"%lu\n",
1059 (unsigned long)s->svr.num_query_authzone_up)) return 0;
1060 if(!ssl_printf(ssl, "num.query.authzone.down"SQ"%lu\n",
1061 (unsigned long)s->svr.num_query_authzone_down)) return 0;
1062 #ifdef CLIENT_SUBNET
1063 if(!ssl_printf(ssl, "num.query.subnet"SQ"%lu\n",
1064 (unsigned long)s->svr.num_query_subnet)) return 0;
1065 if(!ssl_printf(ssl, "num.query.subnet_cache"SQ"%lu\n",
1066 (unsigned long)s->svr.num_query_subnet_cache)) return 0;
1067 #endif /* CLIENT_SUBNET */
1071 /** do the stats command */
1073 do_stats(RES* ssl, struct worker* worker, int reset)
1075 struct daemon* daemon = worker->daemon;
1076 struct ub_stats_info total;
1077 struct ub_stats_info s;
1079 memset(&total, 0, sizeof(total));
1080 log_assert(daemon->num > 0);
1081 /* gather all thread statistics in one place */
1082 for(i=0; i<daemon->num; i++) {
1083 server_stats_obtain(worker, daemon->workers[i], &s, reset);
1084 if(!print_thread_stats(ssl, i, &s))
1088 else server_stats_add(&total, &s);
1090 /* print the thread statistics */
1091 total.mesh_time_median /= (double)daemon->num;
1092 if(!print_stats(ssl, "total", &total))
1094 if(!print_uptime(ssl, worker, reset))
1096 if(daemon->cfg->stat_extended) {
1097 if(!print_mem(ssl, worker, daemon, &total))
1099 if(!print_hist(ssl, &total))
1101 if(!print_ext(ssl, &total))
1106 /** parse commandline argument domain name */
1108 parse_arg_name(RES* ssl, char* str, uint8_t** res, size_t* len, int* labs)
1110 uint8_t nm[LDNS_MAX_DOMAINLEN+1];
1111 size_t nmlen = sizeof(nm);
1116 status = sldns_str2wire_dname_buf(str, nm, &nmlen);
1118 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
1119 LDNS_WIREPARSE_OFFSET(status),
1120 sldns_get_errorstr_parse(status));
1123 *res = memdup(nm, nmlen);
1125 ssl_printf(ssl, "error out of memory\n");
1128 *labs = dname_count_size_labels(*res, len);
1132 /** find second argument, modifies string */
1134 find_arg2(RES* ssl, char* arg, char** arg2)
1136 char* as = strchr(arg, ' ');
1137 char* at = strchr(arg, '\t');
1142 *arg2 = skipwhite(as+1);
1145 *arg2 = skipwhite(as+1);
1148 *arg2 = skipwhite(at+1);
1150 ssl_printf(ssl, "error could not find next argument "
1157 /** Add a new zone */
1159 perform_zone_add(RES* ssl, struct local_zones* zones, char* arg)
1165 enum localzone_type t;
1166 struct local_zone* z;
1167 if(!find_arg2(ssl, arg, &arg2))
1169 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1171 if(!local_zone_str2type(arg2, &t)) {
1172 ssl_printf(ssl, "error not a zone type. %s\n", arg2);
1176 lock_rw_wrlock(&zones->lock);
1177 if((z=local_zones_find(zones, nm, nmlen,
1178 nmlabs, LDNS_RR_CLASS_IN))) {
1179 /* already present in tree */
1180 lock_rw_wrlock(&z->lock);
1181 z->type = t; /* update type anyway */
1182 lock_rw_unlock(&z->lock);
1184 lock_rw_unlock(&zones->lock);
1187 if(!local_zones_add_zone(zones, nm, nmlen,
1188 nmlabs, LDNS_RR_CLASS_IN, t)) {
1189 lock_rw_unlock(&zones->lock);
1190 ssl_printf(ssl, "error out of memory\n");
1193 lock_rw_unlock(&zones->lock);
1197 /** Do the local_zone command */
1199 do_zone_add(RES* ssl, struct local_zones* zones, char* arg)
1201 if(!perform_zone_add(ssl, zones, arg))
1206 /** Do the local_zones command */
1208 do_zones_add(RES* ssl, struct local_zones* zones)
1212 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1213 if(buf[0] == 0x04 && buf[1] == 0)
1214 break; /* end of transmission */
1215 if(!perform_zone_add(ssl, zones, buf)) {
1216 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1222 (void)ssl_printf(ssl, "added %d zones\n", num);
1225 /** Remove a zone */
1227 perform_zone_remove(RES* ssl, struct local_zones* zones, char* arg)
1232 struct local_zone* z;
1233 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1235 lock_rw_wrlock(&zones->lock);
1236 if((z=local_zones_find(zones, nm, nmlen,
1237 nmlabs, LDNS_RR_CLASS_IN))) {
1238 /* present in tree */
1239 local_zones_del_zone(zones, z);
1241 lock_rw_unlock(&zones->lock);
1246 /** Do the local_zone_remove command */
1248 do_zone_remove(RES* ssl, struct local_zones* zones, char* arg)
1250 if(!perform_zone_remove(ssl, zones, arg))
1255 /** Do the local_zones_remove command */
1257 do_zones_remove(RES* ssl, struct local_zones* zones)
1261 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1262 if(buf[0] == 0x04 && buf[1] == 0)
1263 break; /* end of transmission */
1264 if(!perform_zone_remove(ssl, zones, buf)) {
1265 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1271 (void)ssl_printf(ssl, "removed %d zones\n", num);
1274 /** Add new RR data */
1276 perform_data_add(RES* ssl, struct local_zones* zones, char* arg)
1278 if(!local_zones_add_RR(zones, arg)) {
1279 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1285 /** Do the local_data command */
1287 do_data_add(RES* ssl, struct local_zones* zones, char* arg)
1289 if(!perform_data_add(ssl, zones, arg))
1294 /** Do the local_datas command */
1296 do_datas_add(RES* ssl, struct local_zones* zones)
1300 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1301 if(buf[0] == 0x04 && buf[1] == 0)
1302 break; /* end of transmission */
1303 if(!perform_data_add(ssl, zones, buf)) {
1304 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1310 (void)ssl_printf(ssl, "added %d datas\n", num);
1313 /** Remove RR data */
1315 perform_data_remove(RES* ssl, struct local_zones* zones, char* arg)
1320 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1322 local_zones_del_data(zones, nm,
1323 nmlen, nmlabs, LDNS_RR_CLASS_IN);
1328 /** Do the local_data_remove command */
1330 do_data_remove(RES* ssl, struct local_zones* zones, char* arg)
1332 if(!perform_data_remove(ssl, zones, arg))
1337 /** Do the local_datas_remove command */
1339 do_datas_remove(RES* ssl, struct local_zones* zones)
1343 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1344 if(buf[0] == 0x04 && buf[1] == 0)
1345 break; /* end of transmission */
1346 if(!perform_data_remove(ssl, zones, buf)) {
1347 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1353 (void)ssl_printf(ssl, "removed %d datas\n", num);
1356 /** Add a new zone to view */
1358 do_view_zone_add(RES* ssl, struct worker* worker, char* arg)
1362 if(!find_arg2(ssl, arg, &arg2))
1364 v = views_find_view(worker->daemon->views,
1365 arg, 1 /* get write lock*/);
1367 ssl_printf(ssl,"no view with name: %s\n", arg);
1370 if(!v->local_zones) {
1371 if(!(v->local_zones = local_zones_create())){
1372 lock_rw_unlock(&v->lock);
1373 ssl_printf(ssl,"error out of memory\n");
1377 /* Global local-zone is not used for this view,
1378 * therefore add defaults to this view-specic
1380 struct config_file lz_cfg;
1381 memset(&lz_cfg, 0, sizeof(lz_cfg));
1382 local_zone_enter_defaults(v->local_zones, &lz_cfg);
1385 do_zone_add(ssl, v->local_zones, arg2);
1386 lock_rw_unlock(&v->lock);
1389 /** Remove a zone from view */
1391 do_view_zone_remove(RES* ssl, struct worker* worker, char* arg)
1395 if(!find_arg2(ssl, arg, &arg2))
1397 v = views_find_view(worker->daemon->views,
1398 arg, 1 /* get write lock*/);
1400 ssl_printf(ssl,"no view with name: %s\n", arg);
1403 if(!v->local_zones) {
1404 lock_rw_unlock(&v->lock);
1408 do_zone_remove(ssl, v->local_zones, arg2);
1409 lock_rw_unlock(&v->lock);
1412 /** Add new RR data to view */
1414 do_view_data_add(RES* ssl, struct worker* worker, char* arg)
1418 if(!find_arg2(ssl, arg, &arg2))
1420 v = views_find_view(worker->daemon->views,
1421 arg, 1 /* get write lock*/);
1423 ssl_printf(ssl,"no view with name: %s\n", arg);
1426 if(!v->local_zones) {
1427 if(!(v->local_zones = local_zones_create())){
1428 lock_rw_unlock(&v->lock);
1429 ssl_printf(ssl,"error out of memory\n");
1433 do_data_add(ssl, v->local_zones, arg2);
1434 lock_rw_unlock(&v->lock);
1437 /** Add new RR data from stdin to view */
1439 do_view_datas_add(RES* ssl, struct worker* worker, char* arg)
1442 v = views_find_view(worker->daemon->views,
1443 arg, 1 /* get write lock*/);
1445 ssl_printf(ssl,"no view with name: %s\n", arg);
1448 if(!v->local_zones) {
1449 if(!(v->local_zones = local_zones_create())){
1450 lock_rw_unlock(&v->lock);
1451 ssl_printf(ssl,"error out of memory\n");
1455 do_datas_add(ssl, v->local_zones);
1456 lock_rw_unlock(&v->lock);
1459 /** Remove RR data from view */
1461 do_view_data_remove(RES* ssl, struct worker* worker, char* arg)
1465 if(!find_arg2(ssl, arg, &arg2))
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 lock_rw_unlock(&v->lock);
1478 do_data_remove(ssl, v->local_zones, arg2);
1479 lock_rw_unlock(&v->lock);
1482 /** cache lookup of nameservers */
1484 do_lookup(RES* ssl, struct worker* worker, char* arg)
1489 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1491 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1495 /** flush something from rrset and msg caches */
1497 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1498 uint16_t t, uint16_t c)
1501 struct query_info k;
1502 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1503 if(t == LDNS_RR_TYPE_SOA)
1504 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1505 PACKED_RRSET_SOA_NEG);
1507 k.qname_len = nmlen;
1510 k.local_alias = NULL;
1511 h = query_info_hash(&k, 0);
1512 slabhash_remove(worker->env.msg_cache, h, &k);
1513 if(t == LDNS_RR_TYPE_AAAA) {
1514 /* for AAAA also flush dns64 bit_cd packet */
1515 h = query_info_hash(&k, BIT_CD);
1516 slabhash_remove(worker->env.msg_cache, h, &k);
1522 do_flush_type(RES* ssl, struct worker* worker, char* arg)
1529 if(!find_arg2(ssl, arg, &arg2))
1531 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1533 t = sldns_get_rr_type_by_name(arg2);
1534 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1540 /** flush statistics */
1542 do_flush_stats(RES* ssl, struct worker* worker)
1544 worker_stats_clear(worker);
1549 * Local info for deletion functions
1553 struct worker* worker;
1554 /** name to delete */
1560 /** time to invalidate to */
1562 /** number of rrsets removed */
1564 /** number of msgs removed */
1566 /** number of key entries removed */
1568 /** length of addr */
1570 /** socket address for host deletion */
1571 struct sockaddr_storage addr;
1574 /** callback to delete hosts in infra cache */
1576 infra_del_host(struct lruhash_entry* e, void* arg)
1578 /* entry is locked */
1579 struct del_info* inf = (struct del_info*)arg;
1580 struct infra_key* k = (struct infra_key*)e->key;
1581 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1582 struct infra_data* d = (struct infra_data*)e->data;
1585 d->timeout_AAAA = 0;
1586 d->timeout_other = 0;
1588 if(d->ttl > inf->expired) {
1589 d->ttl = inf->expired;
1595 /** flush infra cache */
1597 do_flush_infra(RES* ssl, struct worker* worker, char* arg)
1599 struct sockaddr_storage addr;
1601 struct del_info inf;
1602 if(strcmp(arg, "all") == 0) {
1603 slabhash_clear(worker->env.infra_cache->hosts);
1607 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1608 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1611 /* delete all entries from cache */
1612 /* what we do is to set them all expired */
1613 inf.worker = worker;
1617 inf.expired = *worker->env.now;
1618 inf.expired -= 3; /* handle 3 seconds skew between threads */
1623 memmove(&inf.addr, &addr, len);
1624 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1629 /** flush requestlist */
1631 do_flush_requestlist(RES* ssl, struct worker* worker)
1633 mesh_delete_all(worker->env.mesh);
1637 /** callback to delete rrsets in a zone */
1639 zone_del_rrset(struct lruhash_entry* e, void* arg)
1641 /* entry is locked */
1642 struct del_info* inf = (struct del_info*)arg;
1643 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1644 if(dname_subdomain_c(k->rk.dname, inf->name)) {
1645 struct packed_rrset_data* d =
1646 (struct packed_rrset_data*)e->data;
1647 if(d->ttl > inf->expired) {
1648 d->ttl = inf->expired;
1654 /** callback to delete messages in a zone */
1656 zone_del_msg(struct lruhash_entry* e, void* arg)
1658 /* entry is locked */
1659 struct del_info* inf = (struct del_info*)arg;
1660 struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1661 if(dname_subdomain_c(k->key.qname, inf->name)) {
1662 struct reply_info* d = (struct reply_info*)e->data;
1663 if(d->ttl > inf->expired) {
1664 d->ttl = inf->expired;
1665 d->prefetch_ttl = inf->expired;
1666 d->serve_expired_ttl = inf->expired;
1672 /** callback to delete keys in zone */
1674 zone_del_kcache(struct lruhash_entry* e, void* arg)
1676 /* entry is locked */
1677 struct del_info* inf = (struct del_info*)arg;
1678 struct key_entry_key* k = (struct key_entry_key*)e->key;
1679 if(dname_subdomain_c(k->name, inf->name)) {
1680 struct key_entry_data* d = (struct key_entry_data*)e->data;
1681 if(d->ttl > inf->expired) {
1682 d->ttl = inf->expired;
1688 /** remove all rrsets and keys from zone from cache */
1690 do_flush_zone(RES* ssl, struct worker* worker, char* arg)
1695 struct del_info inf;
1696 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1698 /* delete all RRs and key entries from zone */
1699 /* what we do is to set them all expired */
1700 inf.worker = worker;
1704 inf.expired = *worker->env.now;
1705 inf.expired -= 3; /* handle 3 seconds skew between threads */
1709 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1710 &zone_del_rrset, &inf);
1712 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1714 /* and validator cache */
1715 if(worker->env.key_cache) {
1716 slabhash_traverse(worker->env.key_cache->slab, 1,
1717 &zone_del_kcache, &inf);
1722 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1723 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1724 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1727 /** callback to delete bogus rrsets */
1729 bogus_del_rrset(struct lruhash_entry* e, void* arg)
1731 /* entry is locked */
1732 struct del_info* inf = (struct del_info*)arg;
1733 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1734 if(d->security == sec_status_bogus) {
1735 d->ttl = inf->expired;
1740 /** callback to delete bogus messages */
1742 bogus_del_msg(struct lruhash_entry* e, void* arg)
1744 /* entry is locked */
1745 struct del_info* inf = (struct del_info*)arg;
1746 struct reply_info* d = (struct reply_info*)e->data;
1747 if(d->security == sec_status_bogus) {
1748 d->ttl = inf->expired;
1753 /** callback to delete bogus keys */
1755 bogus_del_kcache(struct lruhash_entry* e, void* arg)
1757 /* entry is locked */
1758 struct del_info* inf = (struct del_info*)arg;
1759 struct key_entry_data* d = (struct key_entry_data*)e->data;
1761 d->ttl = inf->expired;
1766 /** remove all bogus rrsets, msgs and keys from cache */
1768 do_flush_bogus(RES* ssl, struct worker* worker)
1770 struct del_info inf;
1771 /* what we do is to set them all expired */
1772 inf.worker = worker;
1773 inf.expired = *worker->env.now;
1774 inf.expired -= 3; /* handle 3 seconds skew between threads */
1778 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1779 &bogus_del_rrset, &inf);
1781 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1783 /* and validator cache */
1784 if(worker->env.key_cache) {
1785 slabhash_traverse(worker->env.key_cache->slab, 1,
1786 &bogus_del_kcache, &inf);
1789 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1790 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1791 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1794 /** callback to delete negative and servfail rrsets */
1796 negative_del_rrset(struct lruhash_entry* e, void* arg)
1798 /* entry is locked */
1799 struct del_info* inf = (struct del_info*)arg;
1800 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1801 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1802 /* delete the parentside negative cache rrsets,
1803 * these are nameserver rrsets that failed lookup, rdata empty */
1804 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 &&
1805 d->rrsig_count == 0 && d->rr_len[0] == 0) {
1806 d->ttl = inf->expired;
1811 /** callback to delete negative and servfail messages */
1813 negative_del_msg(struct lruhash_entry* e, void* arg)
1815 /* entry is locked */
1816 struct del_info* inf = (struct del_info*)arg;
1817 struct reply_info* d = (struct reply_info*)e->data;
1818 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1819 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1820 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) {
1821 d->ttl = inf->expired;
1826 /** callback to delete negative key entries */
1828 negative_del_kcache(struct lruhash_entry* e, void* arg)
1830 /* entry is locked */
1831 struct del_info* inf = (struct del_info*)arg;
1832 struct key_entry_data* d = (struct key_entry_data*)e->data;
1833 /* could be bad because of lookup failure on the DS, DNSKEY, which
1834 * was nxdomain or servfail, and thus a result of negative lookups */
1836 d->ttl = inf->expired;
1841 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1843 do_flush_negative(RES* ssl, struct worker* worker)
1845 struct del_info inf;
1846 /* what we do is to set them all expired */
1847 inf.worker = worker;
1848 inf.expired = *worker->env.now;
1849 inf.expired -= 3; /* handle 3 seconds skew between threads */
1853 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1854 &negative_del_rrset, &inf);
1856 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf);
1858 /* and validator cache */
1859 if(worker->env.key_cache) {
1860 slabhash_traverse(worker->env.key_cache->slab, 1,
1861 &negative_del_kcache, &inf);
1864 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1865 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1866 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1869 /** remove name rrset from cache */
1871 do_flush_name(RES* ssl, struct worker* w, char* arg)
1876 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1878 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1879 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1880 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1881 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1882 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1883 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1884 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1885 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1886 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1887 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1893 /** printout a delegation point info */
1895 ssl_print_name_dp(RES* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1899 struct delegpt_ns* ns;
1900 struct delegpt_addr* a;
1902 if(str) { /* print header for forward, stub */
1903 char* c = sldns_wire2str_class(dclass);
1905 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) {
1911 for(ns = dp->nslist; ns; ns = ns->next) {
1912 dname_str(ns->name, buf);
1913 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1917 for(a = dp->target_list; a; a = a->next_target) {
1918 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1919 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1923 return ssl_printf(ssl, "\n");
1927 /** print root forwards */
1929 print_root_fwds(RES* ssl, struct iter_forwards* fwds, uint8_t* root)
1932 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1934 return ssl_printf(ssl, "off (using root hints)\n");
1935 /* if dp is returned it must be the root */
1936 log_assert(query_dname_compare(dp->name, root)==0);
1937 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1940 /** parse args into delegpt */
1941 static struct delegpt*
1942 parse_delegpt(RES* ssl, char* args, uint8_t* nm, int allow_names)
1944 /* parse args and add in */
1947 struct delegpt* dp = delegpt_create_mlc(nm);
1948 struct sockaddr_storage addr;
1952 (void)ssl_printf(ssl, "error out of memory\n");
1957 p = strchr(p, ' '); /* find next spot, if any */
1959 *p++ = 0; /* end this spot */
1960 p = skipwhite(p); /* position at next spot */
1963 if(!authextstrtoaddr(todo, &addr, &addrlen, &auth_name)) {
1968 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
1969 (void)ssl_printf(ssl, "error cannot "
1970 "parse IP address or name "
1972 delegpt_free_mlc(dp);
1975 if(!delegpt_add_ns_mlc(dp, n, 0)) {
1976 (void)ssl_printf(ssl, "error out of memory\n");
1978 delegpt_free_mlc(dp);
1984 (void)ssl_printf(ssl, "error cannot parse"
1985 " IP address '%s'\n", todo);
1986 delegpt_free_mlc(dp);
1990 #if ! defined(HAVE_SSL_SET1_HOST) && ! defined(HAVE_X509_VERIFY_PARAM_SET1_HOST)
1992 log_err("no name verification functionality in "
1993 "ssl library, ignored name for %s", todo);
1996 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0,
1998 (void)ssl_printf(ssl, "error out of memory\n");
1999 delegpt_free_mlc(dp);
2004 dp->has_parent_side_NS = 1;
2008 /** do the status command */
2010 do_forward(RES* ssl, struct worker* worker, char* args)
2012 struct iter_forwards* fwd = worker->env.fwds;
2013 uint8_t* root = (uint8_t*)"\000";
2015 (void)ssl_printf(ssl, "error: structure not allocated\n");
2018 if(args == NULL || args[0] == 0) {
2019 (void)print_root_fwds(ssl, fwd, root);
2022 /* set root forwards for this thread. since we are in remote control
2023 * the actual mesh is not running, so we can freely edit it. */
2024 /* delete all the existing queries first */
2025 mesh_delete_all(worker->env.mesh);
2026 if(strcmp(args, "off") == 0) {
2027 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
2030 if(!(dp = parse_delegpt(ssl, args, root, 0)))
2032 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
2033 (void)ssl_printf(ssl, "error out of memory\n");
2041 parse_fs_args(RES* ssl, char* args, uint8_t** nm, struct delegpt** dp,
2042 int* insecure, int* prime)
2048 /* parse all -x args */
2049 while(args[0] == '+') {
2050 if(!find_arg2(ssl, args, &rest))
2052 while(*(++args) != 0) {
2053 if(*args == 'i' && insecure)
2055 else if(*args == 'p' && prime)
2058 (void)ssl_printf(ssl, "error: unknown option %s\n", args);
2066 if(!find_arg2(ssl, args, &rest))
2070 } else zonename = args;
2071 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
2076 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
2084 /** do the forward_add command */
2086 do_forward_add(RES* ssl, struct worker* worker, char* args)
2088 struct iter_forwards* fwd = worker->env.fwds;
2091 struct delegpt* dp = NULL;
2092 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
2094 if(insecure && worker->env.anchors) {
2095 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2097 (void)ssl_printf(ssl, "error out of memory\n");
2098 delegpt_free_mlc(dp);
2103 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
2104 (void)ssl_printf(ssl, "error out of memory\n");
2112 /** do the forward_remove command */
2114 do_forward_remove(RES* ssl, struct worker* worker, char* args)
2116 struct iter_forwards* fwd = worker->env.fwds;
2119 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
2121 if(insecure && worker->env.anchors)
2122 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2124 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
2129 /** do the stub_add command */
2131 do_stub_add(RES* ssl, struct worker* worker, char* args)
2133 struct iter_forwards* fwd = worker->env.fwds;
2134 int insecure = 0, prime = 0;
2136 struct delegpt* dp = NULL;
2137 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
2139 if(insecure && worker->env.anchors) {
2140 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2142 (void)ssl_printf(ssl, "error out of memory\n");
2143 delegpt_free_mlc(dp);
2148 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
2149 if(insecure && worker->env.anchors)
2150 anchors_delete_insecure(worker->env.anchors,
2151 LDNS_RR_CLASS_IN, nm);
2152 (void)ssl_printf(ssl, "error out of memory\n");
2153 delegpt_free_mlc(dp);
2157 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
2158 (void)ssl_printf(ssl, "error out of memory\n");
2159 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
2160 if(insecure && worker->env.anchors)
2161 anchors_delete_insecure(worker->env.anchors,
2162 LDNS_RR_CLASS_IN, nm);
2170 /** do the stub_remove command */
2172 do_stub_remove(RES* ssl, struct worker* worker, char* args)
2174 struct iter_forwards* fwd = worker->env.fwds;
2177 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
2179 if(insecure && worker->env.anchors)
2180 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2182 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
2183 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
2188 /** do the insecure_add command */
2190 do_insecure_add(RES* ssl, struct worker* worker, char* arg)
2195 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2197 if(worker->env.anchors) {
2198 if(!anchors_add_insecure(worker->env.anchors,
2199 LDNS_RR_CLASS_IN, nm)) {
2200 (void)ssl_printf(ssl, "error out of memory\n");
2209 /** do the insecure_remove command */
2211 do_insecure_remove(RES* ssl, struct worker* worker, char* arg)
2216 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2218 if(worker->env.anchors)
2219 anchors_delete_insecure(worker->env.anchors,
2220 LDNS_RR_CLASS_IN, nm);
2226 do_insecure_list(RES* ssl, struct worker* worker)
2229 struct trust_anchor* a;
2230 if(worker->env.anchors) {
2231 RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) {
2232 if(a->numDS == 0 && a->numDNSKEY == 0) {
2233 dname_str(a->name, buf);
2234 ssl_printf(ssl, "%s\n", buf);
2240 /** do the status command */
2242 do_status(RES* ssl, struct worker* worker)
2246 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
2248 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
2250 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
2252 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
2254 for(i=0; i<worker->daemon->mods.num; i++) {
2255 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
2258 if(!ssl_printf(ssl, " ]\n"))
2260 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
2261 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
2263 if(!ssl_printf(ssl, "options:%s%s%s%s\n" ,
2264 (worker->daemon->reuseport?" reuseport":""),
2265 (worker->daemon->rc->accept_list?" control":""),
2266 (worker->daemon->rc->accept_list && worker->daemon->rc->use_cert?"(ssl)":""),
2267 (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)":"")
2270 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
2275 /** get age for the mesh state */
2277 get_mesh_age(struct mesh_state* m, char* buf, size_t len,
2278 struct module_env* env)
2282 struct mesh_reply* r = m->reply_list;
2283 /* last reply is the oldest */
2286 timeval_subtract(&d, env->now_tv, &r->start_time);
2287 snprintf(buf, len, ARG_LL "d.%6.6d",
2288 (long long)d.tv_sec, (int)d.tv_usec);
2290 snprintf(buf, len, "-");
2294 /** get status of a mesh state */
2296 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
2297 char* buf, size_t len)
2299 enum module_ext_state s = m->s.ext_state[m->s.curmod];
2300 const char *modname = mesh->mods.mod[m->s.curmod]->name;
2302 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
2303 m->s.minfo[m->s.curmod]) {
2304 /* break into iterator to find out who its waiting for */
2305 struct iter_qstate* qstate = (struct iter_qstate*)
2306 m->s.minfo[m->s.curmod];
2307 struct outbound_list* ol = &qstate->outlist;
2308 struct outbound_entry* e;
2309 snprintf(buf, len, "%s wait for", modname);
2312 if(ol->first == NULL)
2313 snprintf(buf, len, " (empty_list)");
2314 for(e = ol->first; e; e = e->next) {
2315 snprintf(buf, len, " ");
2318 addr_to_str(&e->qsent->addr, e->qsent->addrlen,
2323 } else if(s == module_wait_subquery) {
2324 /* look in subs from mesh state to see what */
2326 struct mesh_state_ref* sub;
2327 snprintf(buf, len, "%s wants", modname);
2330 if(m->sub_set.count == 0)
2331 snprintf(buf, len, " (empty_list)");
2332 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
2333 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
2334 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
2335 dname_str(sub->s->s.qinfo.qname, nm);
2336 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
2337 (c?c:"CLASS??"), nm);
2344 snprintf(buf, len, "%s is %s", modname, strextstate(s));
2348 /** do the dump_requestlist command */
2350 do_dump_requestlist(RES* ssl, struct worker* worker)
2352 struct mesh_area* mesh;
2353 struct mesh_state* m;
2357 char statbuf[10240];
2358 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
2360 if(!ssl_printf(ssl, "# type cl name seconds module status\n"))
2362 /* show worker mesh contents */
2363 mesh = worker->env.mesh;
2365 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
2366 char* t = sldns_wire2str_type(m->s.qinfo.qtype);
2367 char* c = sldns_wire2str_class(m->s.qinfo.qclass);
2368 dname_str(m->s.qinfo.qname, buf);
2369 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
2370 get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
2371 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
2372 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
2384 /** structure for argument data for dump infra host */
2386 /** the infra cache */
2387 struct infra_cache* infra;
2388 /** the SSL connection */
2392 /** ssl failure? stop writing and skip the rest. If the tcp
2393 * connection is broken, and writes fail, we then stop writing. */
2397 /** callback for every host element in the infra cache */
2399 dump_infra_host(struct lruhash_entry* e, void* arg)
2401 struct infra_arg* a = (struct infra_arg*)arg;
2402 struct infra_key* k = (struct infra_key*)e->key;
2403 struct infra_data* d = (struct infra_data*)e->data;
2409 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
2410 dname_str(k->zonename, name);
2411 port = (int)ntohs(((struct sockaddr_in*)&k->addr)->sin_port);
2412 if(port != UNBOUND_DNS_PORT) {
2413 snprintf(ip_str+strlen(ip_str), sizeof(ip_str)-strlen(ip_str),
2416 /* skip expired stuff (only backed off) */
2417 if(d->ttl < a->now) {
2418 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
2419 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
2420 name, d->rtt.rto)) {
2427 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
2428 "tA %d tAAAA %d tother %d "
2429 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
2430 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now),
2431 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
2432 d->timeout_A, d->timeout_AAAA, d->timeout_other,
2433 (int)d->edns_lame_known, (int)d->edns_version,
2434 (int)(a->now<d->probedelay?(d->probedelay - a->now):0),
2435 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
2436 (int)d->lame_other)) {
2442 /** do the dump_infra command */
2444 do_dump_infra(RES* ssl, struct worker* worker)
2446 struct infra_arg arg;
2447 arg.infra = worker->env.infra_cache;
2449 arg.now = *worker->env.now;
2451 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
2454 /** do the log_reopen command */
2456 do_log_reopen(RES* ssl, struct worker* worker)
2458 struct config_file* cfg = worker->env.cfg;
2460 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
2463 /** do the auth_zone_reload command */
2465 do_auth_zone_reload(RES* ssl, struct worker* worker, char* arg)
2470 struct auth_zones* az = worker->env.auth_zones;
2471 struct auth_zone* z = NULL;
2472 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2475 lock_rw_rdlock(&az->lock);
2476 z = auth_zone_find(az, nm, nmlen, LDNS_RR_CLASS_IN);
2478 lock_rw_wrlock(&z->lock);
2480 lock_rw_unlock(&az->lock);
2484 (void)ssl_printf(ssl, "error no auth-zone %s\n", arg);
2487 if(!auth_zone_read_zonefile(z, worker->env.cfg)) {
2488 lock_rw_unlock(&z->lock);
2489 (void)ssl_printf(ssl, "error failed to read %s\n", arg);
2492 lock_rw_unlock(&z->lock);
2496 /** do the auth_zone_transfer command */
2498 do_auth_zone_transfer(RES* ssl, struct worker* worker, char* arg)
2503 struct auth_zones* az = worker->env.auth_zones;
2504 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2506 if(!az || !auth_zones_startprobesequence(az, &worker->env, nm, nmlen,
2507 LDNS_RR_CLASS_IN)) {
2508 (void)ssl_printf(ssl, "error zone xfr task not found %s\n", arg);
2514 /** do the set_option command */
2516 do_set_option(RES* ssl, struct worker* worker, char* arg)
2519 if(!find_arg2(ssl, arg, &arg2))
2521 if(!config_set_option(worker->env.cfg, arg, arg2)) {
2522 (void)ssl_printf(ssl, "error setting option\n");
2525 /* effectuate some arguments */
2526 if(strcmp(arg, "val-override-date:") == 0) {
2527 int m = modstack_find(&worker->env.mesh->mods, "validator");
2528 struct val_env* val_env = NULL;
2529 if(m != -1) val_env = (struct val_env*)worker->env.modinfo[m];
2531 val_env->date_override = worker->env.cfg->val_date_override;
2536 /* routine to printout option values over SSL */
2537 void remote_get_opt_ssl(char* line, void* arg)
2539 RES* ssl = (RES*)arg;
2540 (void)ssl_printf(ssl, "%s\n", line);
2543 /** do the get_option command */
2545 do_get_option(RES* ssl, struct worker* worker, char* arg)
2548 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
2550 (void)ssl_printf(ssl, "error unknown option\n");
2555 /** do the list_forwards command */
2557 do_list_forwards(RES* ssl, struct worker* worker)
2559 /* since its a per-worker structure no locks needed */
2560 struct iter_forwards* fwds = worker->env.fwds;
2561 struct iter_forward_zone* z;
2562 struct trust_anchor* a;
2564 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
2565 if(!z->dp) continue; /* skip empty marker for stub */
2567 /* see if it is insecure */
2569 if(worker->env.anchors &&
2570 (a=anchor_find(worker->env.anchors, z->name,
2571 z->namelabs, z->namelen, z->dclass))) {
2572 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2574 lock_basic_unlock(&a->lock);
2577 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"),
2578 z->name, z->dclass, z->dp))
2583 /** do the list_stubs command */
2585 do_list_stubs(RES* ssl, struct worker* worker)
2587 struct iter_hints_stub* z;
2588 struct trust_anchor* a;
2591 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
2593 /* see if it is insecure */
2595 if(worker->env.anchors &&
2596 (a=anchor_find(worker->env.anchors, z->node.name,
2597 z->node.labs, z->node.len, z->node.dclass))) {
2598 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2600 lock_basic_unlock(&a->lock);
2603 snprintf(str, sizeof(str), "stub %sprime%s",
2604 (z->noprime?"no":""), (insecure?" +i":""));
2605 if(!ssl_print_name_dp(ssl, str, z->node.name,
2606 z->node.dclass, z->dp))
2611 /** do the list_auth_zones command */
2613 do_list_auth_zones(RES* ssl, struct auth_zones* az)
2615 struct auth_zone* z;
2616 char buf[257], buf2[256];
2617 lock_rw_rdlock(&az->lock);
2618 RBTREE_FOR(z, struct auth_zone*, &az->ztree) {
2619 lock_rw_rdlock(&z->lock);
2620 dname_str(z->name, buf);
2622 snprintf(buf2, sizeof(buf2), "expired");
2624 uint32_t serial = 0;
2625 if(auth_zone_get_serial(z, &serial))
2626 snprintf(buf2, sizeof(buf2), "serial %u",
2628 else snprintf(buf2, sizeof(buf2), "no serial");
2630 if(!ssl_printf(ssl, "%s\t%s\n", buf, buf2)) {
2631 /* failure to print */
2632 lock_rw_unlock(&z->lock);
2633 lock_rw_unlock(&az->lock);
2636 lock_rw_unlock(&z->lock);
2638 lock_rw_unlock(&az->lock);
2641 /** do the list_local_zones command */
2643 do_list_local_zones(RES* ssl, struct local_zones* zones)
2645 struct local_zone* z;
2647 lock_rw_rdlock(&zones->lock);
2648 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2649 lock_rw_rdlock(&z->lock);
2650 dname_str(z->name, buf);
2651 if(!ssl_printf(ssl, "%s %s\n", buf,
2652 local_zone_type2str(z->type))) {
2653 /* failure to print */
2654 lock_rw_unlock(&z->lock);
2655 lock_rw_unlock(&zones->lock);
2658 lock_rw_unlock(&z->lock);
2660 lock_rw_unlock(&zones->lock);
2663 /** do the list_local_data command */
2665 do_list_local_data(RES* ssl, struct worker* worker, struct local_zones* zones)
2667 struct local_zone* z;
2668 struct local_data* d;
2669 struct local_rrset* p;
2670 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
2671 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
2672 lock_rw_rdlock(&zones->lock);
2673 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2674 lock_rw_rdlock(&z->lock);
2675 RBTREE_FOR(d, struct local_data*, &z->data) {
2676 for(p = d->rrsets; p; p = p->next) {
2677 struct packed_rrset_data* d =
2678 (struct packed_rrset_data*)p->rrset->entry.data;
2680 for(i=0; i<d->count + d->rrsig_count; i++) {
2681 if(!packed_rr_to_string(p->rrset, i,
2683 if(!ssl_printf(ssl, "BADRR\n")) {
2684 lock_rw_unlock(&z->lock);
2685 lock_rw_unlock(&zones->lock);
2689 if(!ssl_printf(ssl, "%s\n", s)) {
2690 lock_rw_unlock(&z->lock);
2691 lock_rw_unlock(&zones->lock);
2697 lock_rw_unlock(&z->lock);
2699 lock_rw_unlock(&zones->lock);
2702 /** do the view_list_local_zones command */
2704 do_view_list_local_zones(RES* ssl, struct worker* worker, char* arg)
2706 struct view* v = views_find_view(worker->daemon->views,
2707 arg, 0 /* get read lock*/);
2709 ssl_printf(ssl,"no view with name: %s\n", arg);
2712 if(v->local_zones) {
2713 do_list_local_zones(ssl, v->local_zones);
2715 lock_rw_unlock(&v->lock);
2718 /** do the view_list_local_data command */
2720 do_view_list_local_data(RES* ssl, struct worker* worker, char* arg)
2722 struct view* v = views_find_view(worker->daemon->views,
2723 arg, 0 /* get read lock*/);
2725 ssl_printf(ssl,"no view with name: %s\n", arg);
2728 if(v->local_zones) {
2729 do_list_local_data(ssl, worker, v->local_zones);
2731 lock_rw_unlock(&v->lock);
2734 /** struct for user arg ratelimit list */
2735 struct ratelimit_list_arg {
2736 /** the infra cache */
2737 struct infra_cache* infra;
2738 /** the SSL to print to */
2740 /** all or only ratelimited */
2746 #define ip_ratelimit_list_arg ratelimit_list_arg
2748 /** list items in the ratelimit table */
2750 rate_list(struct lruhash_entry* e, void* arg)
2752 struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg;
2753 struct rate_key* k = (struct rate_key*)e->key;
2754 struct rate_data* d = (struct rate_data*)e->data;
2756 int lim = infra_find_ratelimit(a->infra, k->name, k->namelen);
2757 int max = infra_rate_max(d, a->now);
2762 dname_str(k->name, buf);
2763 ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim);
2766 /** list items in the ip_ratelimit table */
2768 ip_rate_list(struct lruhash_entry* e, void* arg)
2771 struct ip_ratelimit_list_arg* a = (struct ip_ratelimit_list_arg*)arg;
2772 struct ip_rate_key* k = (struct ip_rate_key*)e->key;
2773 struct ip_rate_data* d = (struct ip_rate_data*)e->data;
2774 int lim = infra_ip_ratelimit;
2775 int max = infra_rate_max(d, a->now);
2780 addr_to_str(&k->addr, k->addrlen, ip, sizeof(ip));
2781 ssl_printf(a->ssl, "%s %d limit %d\n", ip, max, lim);
2784 /** do the ratelimit_list command */
2786 do_ratelimit_list(RES* ssl, struct worker* worker, char* arg)
2788 struct ratelimit_list_arg a;
2790 a.infra = worker->env.infra_cache;
2791 a.now = *worker->env.now;
2793 arg = skipwhite(arg);
2794 if(strcmp(arg, "+a") == 0)
2796 if(a.infra->domain_rates==NULL ||
2797 (a.all == 0 && infra_dp_ratelimit == 0))
2799 slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a);
2802 /** do the ip_ratelimit_list command */
2804 do_ip_ratelimit_list(RES* ssl, struct worker* worker, char* arg)
2806 struct ip_ratelimit_list_arg a;
2808 a.infra = worker->env.infra_cache;
2809 a.now = *worker->env.now;
2811 arg = skipwhite(arg);
2812 if(strcmp(arg, "+a") == 0)
2814 if(a.infra->client_ip_rates==NULL ||
2815 (a.all == 0 && infra_ip_ratelimit == 0))
2817 slabhash_traverse(a.infra->client_ip_rates, 0, ip_rate_list, &a);
2820 /** tell other processes to execute the command */
2822 distribute_cmd(struct daemon_remote* rc, RES* ssl, char* cmd)
2827 /* skip i=0 which is me */
2828 for(i=1; i<rc->worker->daemon->num; i++) {
2829 worker_send_cmd(rc->worker->daemon->workers[i],
2831 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2832 (uint8_t*)cmd, strlen(cmd)+1, 0)) {
2833 ssl_printf(ssl, "error could not distribute cmd\n");
2839 /** check for name with end-of-string, space or tab after it */
2841 cmdcmp(char* p, const char* cmd, size_t len)
2843 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2846 /** execute a remote control command */
2848 execute_cmd(struct daemon_remote* rc, RES* ssl, char* cmd,
2849 struct worker* worker)
2851 char* p = skipwhite(cmd);
2852 /* compare command */
2853 if(cmdcmp(p, "stop", 4)) {
2854 do_stop(ssl, worker);
2856 } else if(cmdcmp(p, "reload", 6)) {
2857 do_reload(ssl, worker);
2859 } else if(cmdcmp(p, "stats_noreset", 13)) {
2860 do_stats(ssl, worker, 0);
2862 } else if(cmdcmp(p, "stats", 5)) {
2863 do_stats(ssl, worker, 1);
2865 } else if(cmdcmp(p, "status", 6)) {
2866 do_status(ssl, worker);
2868 } else if(cmdcmp(p, "dump_cache", 10)) {
2869 (void)dump_cache(ssl, worker);
2871 } else if(cmdcmp(p, "load_cache", 10)) {
2872 if(load_cache(ssl, worker)) send_ok(ssl);
2874 } else if(cmdcmp(p, "list_forwards", 13)) {
2875 do_list_forwards(ssl, worker);
2877 } else if(cmdcmp(p, "list_stubs", 10)) {
2878 do_list_stubs(ssl, worker);
2880 } else if(cmdcmp(p, "list_insecure", 13)) {
2881 do_insecure_list(ssl, worker);
2883 } else if(cmdcmp(p, "list_local_zones", 16)) {
2884 do_list_local_zones(ssl, worker->daemon->local_zones);
2886 } else if(cmdcmp(p, "list_local_data", 15)) {
2887 do_list_local_data(ssl, worker, worker->daemon->local_zones);
2889 } else if(cmdcmp(p, "view_list_local_zones", 21)) {
2890 do_view_list_local_zones(ssl, worker, skipwhite(p+21));
2892 } else if(cmdcmp(p, "view_list_local_data", 20)) {
2893 do_view_list_local_data(ssl, worker, skipwhite(p+20));
2895 } else if(cmdcmp(p, "ratelimit_list", 14)) {
2896 do_ratelimit_list(ssl, worker, p+14);
2898 } else if(cmdcmp(p, "ip_ratelimit_list", 17)) {
2899 do_ip_ratelimit_list(ssl, worker, p+17);
2901 } else if(cmdcmp(p, "list_auth_zones", 15)) {
2902 do_list_auth_zones(ssl, worker->env.auth_zones);
2904 } else if(cmdcmp(p, "auth_zone_reload", 16)) {
2905 do_auth_zone_reload(ssl, worker, skipwhite(p+16));
2907 } else if(cmdcmp(p, "auth_zone_transfer", 18)) {
2908 do_auth_zone_transfer(ssl, worker, skipwhite(p+18));
2910 } else if(cmdcmp(p, "stub_add", 8)) {
2911 /* must always distribute this cmd */
2912 if(rc) distribute_cmd(rc, ssl, cmd);
2913 do_stub_add(ssl, worker, skipwhite(p+8));
2915 } else if(cmdcmp(p, "stub_remove", 11)) {
2916 /* must always distribute this cmd */
2917 if(rc) distribute_cmd(rc, ssl, cmd);
2918 do_stub_remove(ssl, worker, skipwhite(p+11));
2920 } else if(cmdcmp(p, "forward_add", 11)) {
2921 /* must always distribute this cmd */
2922 if(rc) distribute_cmd(rc, ssl, cmd);
2923 do_forward_add(ssl, worker, skipwhite(p+11));
2925 } else if(cmdcmp(p, "forward_remove", 14)) {
2926 /* must always distribute this cmd */
2927 if(rc) distribute_cmd(rc, ssl, cmd);
2928 do_forward_remove(ssl, worker, skipwhite(p+14));
2930 } else if(cmdcmp(p, "insecure_add", 12)) {
2931 /* must always distribute this cmd */
2932 if(rc) distribute_cmd(rc, ssl, cmd);
2933 do_insecure_add(ssl, worker, skipwhite(p+12));
2935 } else if(cmdcmp(p, "insecure_remove", 15)) {
2936 /* must always distribute this cmd */
2937 if(rc) distribute_cmd(rc, ssl, cmd);
2938 do_insecure_remove(ssl, worker, skipwhite(p+15));
2940 } else if(cmdcmp(p, "forward", 7)) {
2941 /* must always distribute this cmd */
2942 if(rc) distribute_cmd(rc, ssl, cmd);
2943 do_forward(ssl, worker, skipwhite(p+7));
2945 } else if(cmdcmp(p, "flush_stats", 11)) {
2946 /* must always distribute this cmd */
2947 if(rc) distribute_cmd(rc, ssl, cmd);
2948 do_flush_stats(ssl, worker);
2950 } else if(cmdcmp(p, "flush_requestlist", 17)) {
2951 /* must always distribute this cmd */
2952 if(rc) distribute_cmd(rc, ssl, cmd);
2953 do_flush_requestlist(ssl, worker);
2955 } else if(cmdcmp(p, "lookup", 6)) {
2956 do_lookup(ssl, worker, skipwhite(p+6));
2960 #ifdef THREADS_DISABLED
2961 /* other processes must execute the command as well */
2962 /* commands that should not be distributed, returned above. */
2963 if(rc) { /* only if this thread is the master (rc) thread */
2964 /* done before the code below, which may split the string */
2965 distribute_cmd(rc, ssl, cmd);
2968 if(cmdcmp(p, "verbosity", 9)) {
2969 do_verbosity(ssl, skipwhite(p+9));
2970 } else if(cmdcmp(p, "local_zone_remove", 17)) {
2971 do_zone_remove(ssl, worker->daemon->local_zones, skipwhite(p+17));
2972 } else if(cmdcmp(p, "local_zones_remove", 18)) {
2973 do_zones_remove(ssl, worker->daemon->local_zones);
2974 } else if(cmdcmp(p, "local_zone", 10)) {
2975 do_zone_add(ssl, worker->daemon->local_zones, skipwhite(p+10));
2976 } else if(cmdcmp(p, "local_zones", 11)) {
2977 do_zones_add(ssl, worker->daemon->local_zones);
2978 } else if(cmdcmp(p, "local_data_remove", 17)) {
2979 do_data_remove(ssl, worker->daemon->local_zones, skipwhite(p+17));
2980 } else if(cmdcmp(p, "local_datas_remove", 18)) {
2981 do_datas_remove(ssl, worker->daemon->local_zones);
2982 } else if(cmdcmp(p, "local_data", 10)) {
2983 do_data_add(ssl, worker->daemon->local_zones, skipwhite(p+10));
2984 } else if(cmdcmp(p, "local_datas", 11)) {
2985 do_datas_add(ssl, worker->daemon->local_zones);
2986 } else if(cmdcmp(p, "view_local_zone_remove", 22)) {
2987 do_view_zone_remove(ssl, worker, skipwhite(p+22));
2988 } else if(cmdcmp(p, "view_local_zone", 15)) {
2989 do_view_zone_add(ssl, worker, skipwhite(p+15));
2990 } else if(cmdcmp(p, "view_local_data_remove", 22)) {
2991 do_view_data_remove(ssl, worker, skipwhite(p+22));
2992 } else if(cmdcmp(p, "view_local_data", 15)) {
2993 do_view_data_add(ssl, worker, skipwhite(p+15));
2994 } else if(cmdcmp(p, "view_local_datas", 16)) {
2995 do_view_datas_add(ssl, worker, skipwhite(p+16));
2996 } else if(cmdcmp(p, "flush_zone", 10)) {
2997 do_flush_zone(ssl, worker, skipwhite(p+10));
2998 } else if(cmdcmp(p, "flush_type", 10)) {
2999 do_flush_type(ssl, worker, skipwhite(p+10));
3000 } else if(cmdcmp(p, "flush_infra", 11)) {
3001 do_flush_infra(ssl, worker, skipwhite(p+11));
3002 } else if(cmdcmp(p, "flush", 5)) {
3003 do_flush_name(ssl, worker, skipwhite(p+5));
3004 } else if(cmdcmp(p, "dump_requestlist", 16)) {
3005 do_dump_requestlist(ssl, worker);
3006 } else if(cmdcmp(p, "dump_infra", 10)) {
3007 do_dump_infra(ssl, worker);
3008 } else if(cmdcmp(p, "log_reopen", 10)) {
3009 do_log_reopen(ssl, worker);
3010 } else if(cmdcmp(p, "set_option", 10)) {
3011 do_set_option(ssl, worker, skipwhite(p+10));
3012 } else if(cmdcmp(p, "get_option", 10)) {
3013 do_get_option(ssl, worker, skipwhite(p+10));
3014 } else if(cmdcmp(p, "flush_bogus", 11)) {
3015 do_flush_bogus(ssl, worker);
3016 } else if(cmdcmp(p, "flush_negative", 14)) {
3017 do_flush_negative(ssl, worker);
3019 (void)ssl_printf(ssl, "error unknown command '%s'\n", p);
3024 daemon_remote_exec(struct worker* worker)
3026 /* read the cmd string */
3027 uint8_t* msg = NULL;
3029 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
3030 log_err("daemon_remote_exec: tube_read_msg failed");
3033 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
3034 execute_cmd(NULL, NULL, (char*)msg, worker);
3038 /** handle remote control request */
3040 handle_req(struct daemon_remote* rc, struct rc_state* s, RES* res)
3047 /* makes it possible to set the socket blocking again. */
3048 /* basically removes it from winsock_event ... */
3049 WSAEventSelect(s->c->fd, NULL, 0);
3051 fd_set_block(s->c->fd);
3053 /* try to read magic UBCT[version]_space_ string */
3056 if((r=SSL_read(res->ssl, magic, (int)sizeof(magic)-1)) <= 0) {
3057 if(SSL_get_error(res->ssl, r) == SSL_ERROR_ZERO_RETURN)
3059 log_crypto_err("could not SSL_read");
3064 ssize_t rr = recv(res->fd, magic, sizeof(magic)-1, 0);
3067 if(errno == EINTR || errno == EAGAIN)
3070 log_err("could not recv: %s", strerror(errno));
3072 log_err("could not recv: %s", wsa_strerror(WSAGetLastError()));
3081 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
3082 verbose(VERB_QUERY, "control connection has bad magic string");
3083 /* probably wrong tool connected, ignore it completely */
3087 /* read the command line */
3088 if(!ssl_read_line(res, buf, sizeof(buf))) {
3091 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
3092 if(strcmp(magic, pre) != 0) {
3093 verbose(VERB_QUERY, "control connection had bad "
3094 "version %s, cmd: %s", magic, buf);
3095 ssl_printf(res, "error version mismatch\n");
3098 verbose(VERB_DETAIL, "control cmd: %s", buf);
3100 /* figure out what to do */
3101 execute_cmd(rc, res, buf, rc->worker);
3104 /** handle SSL_do_handshake changes to the file descriptor to wait for later */
3106 remote_handshake_later(struct daemon_remote* rc, struct rc_state* s,
3107 struct comm_point* c, int r, int r2)
3109 if(r2 == SSL_ERROR_WANT_READ) {
3110 if(s->shake_state == rc_hs_read) {
3111 /* try again later */
3114 s->shake_state = rc_hs_read;
3115 comm_point_listen_for_rw(c, 1, 0);
3117 } else if(r2 == SSL_ERROR_WANT_WRITE) {
3118 if(s->shake_state == rc_hs_write) {
3119 /* try again later */
3122 s->shake_state = rc_hs_write;
3123 comm_point_listen_for_rw(c, 0, 1);
3127 log_err("remote control connection closed prematurely");
3128 log_addr(VERB_OPS, "failed connection from",
3129 &s->c->repinfo.addr, s->c->repinfo.addrlen);
3130 log_crypto_err("remote control failed ssl");
3136 int remote_control_callback(struct comm_point* c, void* arg, int err,
3137 struct comm_reply* ATTR_UNUSED(rep))
3140 struct rc_state* s = (struct rc_state*)arg;
3141 struct daemon_remote* rc = s->rc;
3143 if(err != NETEVENT_NOERROR) {
3144 if(err==NETEVENT_TIMEOUT)
3145 log_err("remote control timed out");
3150 /* (continue to) setup the SSL connection */
3152 r = SSL_do_handshake(s->ssl);
3154 int r2 = SSL_get_error(s->ssl, r);
3155 return remote_handshake_later(rc, s, c, r, r2);
3157 s->shake_state = rc_none;
3160 /* once handshake has completed, check authentication */
3161 if (!rc->use_cert) {
3162 verbose(VERB_ALGO, "unauthenticated remote control connection");
3163 } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
3164 X509* x = SSL_get_peer_certificate(s->ssl);
3166 verbose(VERB_DETAIL, "remote control connection "
3167 "provided no client certificate");
3171 verbose(VERB_ALGO, "remote control connection authenticated");
3174 verbose(VERB_DETAIL, "remote control connection failed to "
3175 "authenticate with client certificate");
3180 /* if OK start to actually handle the request */
3183 handle_req(rc, s, &res);
3185 verbose(VERB_ALGO, "remote control operation completed");