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 * The following function was generated using the openssl utility, using
147 * the command : "openssl dhparam -C 2048"
148 * (some openssl versions reject DH that is 'too small', eg. 512).
150 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
152 static DH *get_dh2048(void)
154 static unsigned char dh2048_p[]={
155 0xE7,0x36,0x28,0x3B,0xE4,0xC3,0x32,0x1C,0x01,0xC3,0x67,0xD6,
156 0xF5,0xF3,0xDA,0xDC,0x71,0xC0,0x42,0x8B,0xE6,0xEB,0x8D,0x80,
157 0x35,0x7F,0x09,0x45,0x30,0xE5,0xB2,0x92,0x81,0x3F,0x08,0xCD,
158 0x36,0x5E,0x19,0x83,0x62,0xCC,0xAE,0x9B,0x81,0x66,0x24,0xEE,
159 0x16,0x6F,0xA9,0x9E,0xF4,0x82,0x1B,0xDD,0x46,0xC7,0x33,0x5D,
160 0xF4,0xCA,0xE6,0x8F,0xFC,0xD4,0xD8,0x58,0x94,0x24,0x5D,0xFF,
161 0x0A,0xE8,0xEF,0x3D,0xCE,0xBB,0x50,0x94,0xE0,0x5F,0xE8,0x41,
162 0xC3,0x35,0x30,0x37,0xD5,0xCB,0x8F,0x3D,0x95,0x15,0x1A,0x77,
163 0x42,0xB2,0x06,0x86,0xF6,0x09,0x66,0x0E,0x9A,0x25,0x94,0x3E,
164 0xD2,0x04,0x25,0x25,0x1D,0x23,0xEB,0xDC,0x4D,0x0C,0x83,0x28,
165 0x2E,0x15,0x81,0x2D,0xC1,0xAF,0x8D,0x36,0x64,0xE3,0x9A,0x83,
166 0x78,0xC2,0x8D,0xC0,0x9D,0xD9,0x3A,0x1C,0xC5,0x2B,0x50,0x68,
167 0x07,0xA9,0x4B,0x8C,0x07,0x57,0xD6,0x15,0x03,0x4E,0x9E,0x01,
168 0xF2,0x6F,0x35,0xAC,0x26,0x9C,0x92,0x68,0x61,0x13,0xFB,0x01,
169 0xBA,0x22,0x36,0x01,0x55,0xB6,0x62,0xD9,0xB2,0x98,0xCE,0x5D,
170 0x4B,0xA5,0x41,0xD6,0xE5,0x70,0x78,0x12,0x1F,0x64,0xB6,0x6F,
171 0xB0,0x91,0x51,0x91,0x92,0xC0,0x94,0x3A,0xD1,0x28,0x4D,0x30,
172 0x84,0x3E,0xE4,0xE4,0x7F,0x47,0x89,0xB1,0xB6,0x8C,0x8E,0x0E,
173 0x26,0xDB,0xCD,0x17,0x07,0x2A,0x21,0x7A,0xCC,0x68,0xE8,0x57,
174 0x94,0x9E,0x59,0x61,0xEC,0x20,0x34,0x26,0x0D,0x66,0x44,0xEB,
175 0x6F,0x02,0x58,0xE2,0xED,0xF6,0xF3,0x1B,0xBF,0x9E,0x45,0x52,
178 static unsigned char dh2048_g[]={
182 BIGNUM *p = NULL, *g = NULL;
185 p = BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL);
186 g = BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL);
190 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
194 if (!DH_set0_pqg(dh, p, NULL, g))
208 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000 */
210 struct daemon_remote*
211 daemon_remote_create(struct config_file* cfg)
215 struct daemon_remote* rc = (struct daemon_remote*)calloc(1,
218 log_err("out of memory in daemon_remote_create");
223 if(!cfg->remote_control_enable) {
227 rc->ctx = SSL_CTX_new(SSLv23_server_method());
229 log_crypto_err("could not SSL_CTX_new");
233 if(!listen_sslctx_setup(rc->ctx)) {
234 daemon_remote_delete(rc);
238 if (cfg->remote_control_use_cert == 0) {
239 /* No certificates are requested */
240 #if defined(SSL_OP_NO_TLSv1_3)
241 /* in openssl 1.1.1, negotiation code for tls 1.3 does
242 * not allow the unauthenticated aNULL and eNULL ciphers */
243 SSL_CTX_set_options(rc->ctx, SSL_OP_NO_TLSv1_3);
245 #ifdef HAVE_SSL_CTX_SET_SECURITY_LEVEL
246 SSL_CTX_set_security_level(rc->ctx, 0);
248 if(!SSL_CTX_set_cipher_list(rc->ctx, "aNULL:eNULL")) {
249 log_crypto_err("Failed to set aNULL cipher list");
250 daemon_remote_delete(rc);
254 /* in openssl 1.1, the securitylevel 0 allows eNULL, that
255 * does not need the DH */
256 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
257 /* Since we have no certificates and hence no source of
258 * DH params, let's generate and set them
260 if(!SSL_CTX_set_tmp_dh(rc->ctx,get_dh2048())) {
261 log_crypto_err("Wanted to set DH param, but failed");
262 daemon_remote_delete(rc);
269 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
270 s_key = fname_after_chroot(cfg->server_key_file, cfg, 1);
271 if(!s_cert || !s_key) {
272 log_err("out of memory in remote control fname");
275 verbose(VERB_ALGO, "setup SSL certificates");
276 if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) {
277 log_err("Error for server-cert-file: %s", s_cert);
278 log_crypto_err("Error in SSL_CTX use_certificate_chain_file");
281 if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) {
282 log_err("Error for server-key-file: %s", s_key);
283 log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
286 if(!SSL_CTX_check_private_key(rc->ctx)) {
287 log_err("Error for server-key-file: %s", s_key);
288 log_crypto_err("Error in SSL_CTX check_private_key");
291 listen_sslctx_setup_2(rc->ctx);
292 if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) {
293 log_crypto_err("Error setting up SSL_CTX verify locations");
297 daemon_remote_delete(rc);
300 SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert));
301 SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL);
308 void daemon_remote_clear(struct daemon_remote* rc)
310 struct rc_state* p, *np;
312 /* but do not close the ports */
313 listen_list_delete(rc->accept_list);
314 rc->accept_list = NULL;
315 /* do close these sockets */
321 comm_point_delete(p->c);
325 rc->busy_list = NULL;
330 void daemon_remote_delete(struct daemon_remote* rc)
333 daemon_remote_clear(rc);
335 SSL_CTX_free(rc->ctx);
341 * Add and open a new control port
344 * @param list: list head
345 * @param noproto_is_err: if lack of protocol support is an error.
346 * @param cfg: config with username for chown of unix-sockets.
347 * @return false on failure.
350 add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err,
351 struct config_file* cfg)
353 struct addrinfo hints;
354 struct addrinfo* res;
355 struct listen_port* n;
359 snprintf(port, sizeof(port), "%d", nr);
360 port[sizeof(port)-1]=0;
361 memset(&hints, 0, sizeof(hints));
364 /* This looks like a local socket */
365 fd = create_local_accept_sock(ip, &noproto, cfg->use_systemd);
367 * Change socket ownership and permissions so users other
368 * than root can access it provided they are in the same
369 * group as the user we run as.
373 if (cfg->username && cfg->username[0] &&
374 cfg_uid != (uid_t)-1) {
375 if(chown(ip, cfg_uid, cfg_gid) == -1)
376 verbose(VERB_QUERY, "cannot chown %u.%u %s: %s",
377 (unsigned)cfg_uid, (unsigned)cfg_gid,
378 ip, strerror(errno));
380 chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
386 hints.ai_socktype = SOCK_STREAM;
387 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
388 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) {
390 if(!noproto_is_err && r == EAI_NONAME) {
391 /* tried to lookup the address as name */
392 return 1; /* return success, but do nothing */
394 #endif /* USE_WINSOCK */
395 log_err("control interface %s:%s getaddrinfo: %s %s",
396 ip?ip:"default", port, gai_strerror(r),
398 r==EAI_SYSTEM?(char*)strerror(errno):""
407 fd = create_tcp_accept_sock(res, 1, &noproto, 0,
408 cfg->ip_transparent, 0, cfg->ip_freebind, cfg->use_systemd);
412 if(fd == -1 && noproto) {
414 return 1; /* return success, but do nothing */
415 log_err("cannot open control interface %s %d : "
416 "protocol not supported", ip, nr);
420 log_err("cannot open control interface %s %d", ip, nr);
425 n = (struct listen_port*)calloc(1, sizeof(*n));
432 log_err("out of memory");
441 struct listen_port* daemon_remote_open_ports(struct config_file* cfg)
443 struct listen_port* l = NULL;
444 log_assert(cfg->remote_control_enable && cfg->control_port);
445 if(cfg->control_ifs) {
446 struct config_strlist* p;
447 for(p = cfg->control_ifs; p; p = p->next) {
448 if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) {
449 listening_ports_free(l);
456 !add_open("::1", cfg->control_port, &l, 0, cfg)) {
457 listening_ports_free(l);
461 !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) {
462 listening_ports_free(l);
469 /** open accept commpoint */
471 accept_open(struct daemon_remote* rc, int fd)
473 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n));
475 log_err("out of memory");
478 n->next = rc->accept_list;
481 n->com = comm_point_create_raw(rc->worker->base, fd, 0,
482 &remote_accept_callback, rc);
485 /* keep this port open, its fd is kept in the rc portlist */
486 n->com->do_not_close = 1;
490 int daemon_remote_open_accept(struct daemon_remote* rc,
491 struct listen_port* ports, struct worker* worker)
493 struct listen_port* p;
495 for(p = ports; p; p = p->next) {
496 if(!accept_open(rc, p->fd)) {
497 log_err("could not create accept comm point");
504 void daemon_remote_stop_accept(struct daemon_remote* rc)
506 struct listen_list* p;
507 for(p=rc->accept_list; p; p=p->next) {
508 comm_point_stop_listening(p->com);
512 void daemon_remote_start_accept(struct daemon_remote* rc)
514 struct listen_list* p;
515 for(p=rc->accept_list; p; p=p->next) {
516 comm_point_start_listening(p->com, -1, -1);
520 int remote_accept_callback(struct comm_point* c, void* arg, int err,
521 struct comm_reply* ATTR_UNUSED(rep))
523 struct daemon_remote* rc = (struct daemon_remote*)arg;
524 struct sockaddr_storage addr;
528 if(err != NETEVENT_NOERROR) {
529 log_err("error %d on remote_accept_callback", err);
532 /* perform the accept */
533 newfd = comm_point_perform_accept(c, &addr, &addrlen);
536 /* create new commpoint unless we are servicing already */
537 if(rc->active >= rc->max_active) {
538 log_warn("drop incoming remote control: too many connections");
548 /* setup commpoint to service the remote control command */
549 n = (struct rc_state*)calloc(1, sizeof(*n));
551 log_err("out of memory");
554 /* start in reading state */
555 n->c = comm_point_create_raw(rc->worker->base, newfd, 0,
556 &remote_control_callback, n);
558 log_err("out of memory");
562 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen);
563 n->c->do_not_close = 0;
564 comm_point_stop_listening(n->c);
565 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT);
566 memcpy(&n->c->repinfo.addr, &addr, addrlen);
567 n->c->repinfo.addrlen = addrlen;
568 n->shake_state = rc_hs_read;
569 n->ssl = SSL_new(rc->ctx);
571 log_crypto_err("could not SSL_new");
572 comm_point_delete(n->c);
576 SSL_set_accept_state(n->ssl);
577 (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY);
578 if(!SSL_set_fd(n->ssl, newfd)) {
579 log_crypto_err("could not SSL_set_fd");
581 comm_point_delete(n->c);
587 n->next = rc->busy_list;
591 /* perform the first nonblocking read already, for windows,
592 * so it can return wouldblock. could be faster too. */
593 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL);
597 /** delete from list */
599 state_list_remove_elem(struct rc_state** list, struct comm_point* c)
602 if( (*list)->c == c) {
603 *list = (*list)->next;
606 list = &(*list)->next;
610 /** decrease active count and remove commpoint from busy list */
612 clean_point(struct daemon_remote* rc, struct rc_state* s)
614 state_list_remove_elem(&rc->busy_list, s->c);
617 SSL_shutdown(s->ssl);
620 comm_point_delete(s->c);
625 ssl_print_text(SSL* ssl, const char* text)
631 if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) {
632 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
633 verbose(VERB_QUERY, "warning, in SSL_write, peer "
634 "closed connection");
637 log_crypto_err("could not SSL_write");
643 /** print text over the ssl connection */
645 ssl_print_vmsg(SSL* ssl, const char* format, va_list args)
648 vsnprintf(msg, sizeof(msg), format, args);
649 return ssl_print_text(ssl, msg);
652 /** printf style printing to the ssl connection */
653 int ssl_printf(SSL* ssl, const char* format, ...)
657 va_start(args, format);
658 ret = ssl_print_vmsg(ssl, format, args);
664 ssl_read_line(SSL* ssl, char* buf, size_t max)
672 if((r=SSL_read(ssl, buf+len, 1)) <= 0) {
673 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
677 log_crypto_err("could not SSL_read");
680 if(buf[len] == '\n') {
681 /* return string without \n */
688 log_err("control line too long (%d): %s", (int)max, buf);
692 /** skip whitespace, return new pointer into string */
696 /* EOS \0 is not a space */
697 while( isspace((unsigned char)*str) )
702 /** send the OK to the control client */
703 static void send_ok(SSL* ssl)
705 (void)ssl_printf(ssl, "ok\n");
708 /** do the stop command */
710 do_stop(SSL* ssl, struct daemon_remote* rc)
712 rc->worker->need_to_exit = 1;
713 comm_base_exit(rc->worker->base);
717 /** do the reload command */
719 do_reload(SSL* ssl, struct daemon_remote* rc)
721 rc->worker->need_to_exit = 0;
722 comm_base_exit(rc->worker->base);
726 /** do the verbosity command */
728 do_verbosity(SSL* ssl, char* str)
731 if(val == 0 && strcmp(str, "0") != 0) {
732 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str);
739 /** print stats from statinfo */
741 print_stats(SSL* ssl, const char* nm, struct ub_stats_info* s)
743 struct timeval sumwait, avg;
744 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm,
745 (unsigned long)s->svr.num_queries)) return 0;
746 if(!ssl_printf(ssl, "%s.num.queries_ip_ratelimited"SQ"%lu\n", nm,
747 (unsigned long)s->svr.num_queries_ip_ratelimited)) return 0;
748 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm,
749 (unsigned long)(s->svr.num_queries
750 - s->svr.num_queries_missed_cache))) return 0;
751 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm,
752 (unsigned long)s->svr.num_queries_missed_cache)) return 0;
753 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm,
754 (unsigned long)s->svr.num_queries_prefetch)) return 0;
755 if(!ssl_printf(ssl, "%s.num.zero_ttl"SQ"%lu\n", nm,
756 (unsigned long)s->svr.zero_ttl_responses)) return 0;
757 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm,
758 (unsigned long)s->mesh_replies_sent)) return 0;
760 if(!ssl_printf(ssl, "%s.num.dnscrypt.crypted"SQ"%lu\n", nm,
761 (unsigned long)s->svr.num_query_dnscrypt_crypted)) return 0;
762 if(!ssl_printf(ssl, "%s.num.dnscrypt.cert"SQ"%lu\n", nm,
763 (unsigned long)s->svr.num_query_dnscrypt_cert)) return 0;
764 if(!ssl_printf(ssl, "%s.num.dnscrypt.cleartext"SQ"%lu\n", nm,
765 (unsigned long)s->svr.num_query_dnscrypt_cleartext)) return 0;
766 if(!ssl_printf(ssl, "%s.num.dnscrypt.malformed"SQ"%lu\n", nm,
767 (unsigned long)s->svr.num_query_dnscrypt_crypted_malformed)) return 0;
769 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm,
770 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
771 (double)s->svr.sum_query_list_size/
772 (double)(s->svr.num_queries_missed_cache+
773 s->svr.num_queries_prefetch) : 0.0)) return 0;
774 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm,
775 (unsigned long)s->svr.max_query_list_size)) return 0;
776 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm,
777 (unsigned long)s->mesh_jostled)) return 0;
778 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm,
779 (unsigned long)s->mesh_dropped)) return 0;
780 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm,
781 (unsigned long)s->mesh_num_states)) return 0;
782 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm,
783 (unsigned long)s->mesh_num_reply_states)) return 0;
785 sumwait.tv_sec = s->mesh_replies_sum_wait_sec;
786 sumwait.tv_usec = s->mesh_replies_sum_wait_usec;
788 timeval_divide(&avg, &sumwait, s->mesh_replies_sent);
789 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm,
790 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0;
791 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm,
792 s->mesh_time_median)) return 0;
793 if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm,
794 (unsigned long)s->svr.tcp_accept_usage)) return 0;
798 /** print stats for one thread */
800 print_thread_stats(SSL* ssl, int i, struct ub_stats_info* s)
803 snprintf(nm, sizeof(nm), "thread%d", i);
805 return print_stats(ssl, nm, s);
808 /** print long number */
810 print_longnum(SSL* ssl, const char* desc, size_t x)
812 if(x > 1024*1024*1024) {
814 size_t front = x / (size_t)1000000;
815 size_t back = x % (size_t)1000000;
816 return ssl_printf(ssl, "%s%u%6.6u\n", desc,
817 (unsigned)front, (unsigned)back);
819 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x);
823 /** print mem stats */
825 print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon)
827 size_t msg, rrset, val, iter, respip;
830 #endif /* CLIENT_SUBNET */
833 #endif /* USE_IPSECMOD */
835 size_t dnscrypt_shared_secret = 0;
836 size_t dnscrypt_nonce = 0;
837 #endif /* USE_DNSCRYPT */
838 msg = slabhash_get_mem(daemon->env->msg_cache);
839 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table);
840 val = mod_get_mem(&worker->env, "validator");
841 iter = mod_get_mem(&worker->env, "iterator");
842 respip = mod_get_mem(&worker->env, "respip");
844 subnet = mod_get_mem(&worker->env, "subnet");
845 #endif /* CLIENT_SUBNET */
847 ipsecmod = mod_get_mem(&worker->env, "ipsecmod");
848 #endif /* USE_IPSECMOD */
850 if(daemon->dnscenv) {
851 dnscrypt_shared_secret = slabhash_get_mem(
852 daemon->dnscenv->shared_secrets_cache);
853 dnscrypt_nonce = slabhash_get_mem(daemon->dnscenv->nonces_cache);
855 #endif /* USE_DNSCRYPT */
857 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset))
859 if(!print_longnum(ssl, "mem.cache.message"SQ, msg))
861 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter))
863 if(!print_longnum(ssl, "mem.mod.validator"SQ, val))
865 if(!print_longnum(ssl, "mem.mod.respip"SQ, respip))
868 if(!print_longnum(ssl, "mem.mod.subnet"SQ, subnet))
870 #endif /* CLIENT_SUBNET */
872 if(!print_longnum(ssl, "mem.mod.ipsecmod"SQ, ipsecmod))
874 #endif /* USE_IPSECMOD */
876 if(!print_longnum(ssl, "mem.cache.dnscrypt_shared_secret"SQ,
877 dnscrypt_shared_secret))
879 if(!print_longnum(ssl, "mem.cache.dnscrypt_nonce"SQ,
882 #endif /* USE_DNSCRYPT */
886 /** print uptime stats */
888 print_uptime(SSL* ssl, struct worker* worker, int reset)
890 struct timeval now = *worker->env.now_tv;
891 struct timeval up, dt;
892 timeval_subtract(&up, &now, &worker->daemon->time_boot);
893 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat);
895 worker->daemon->time_last_stat = now;
896 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n",
897 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0;
898 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n",
899 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0;
900 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n",
901 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0;
905 /** print extended histogram */
907 print_hist(SSL* ssl, struct ub_stats_info* s)
909 struct timehist* hist;
911 hist = timehist_setup();
913 log_err("out of memory");
916 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
917 for(i=0; i<hist->num; i++) {
919 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
920 (int)hist->buckets[i].lower.tv_sec,
921 (int)hist->buckets[i].lower.tv_usec,
922 (int)hist->buckets[i].upper.tv_sec,
923 (int)hist->buckets[i].upper.tv_usec,
924 (unsigned long)hist->buckets[i].count)) {
925 timehist_delete(hist);
929 timehist_delete(hist);
933 /** print extended stats */
935 print_ext(SSL* ssl, struct ub_stats_info* s)
939 const sldns_rr_descriptor* desc;
940 const sldns_lookup_table* lt;
942 for(i=0; i<UB_STATS_QTYPE_NUM; i++) {
943 if(inhibit_zero && s->svr.qtype[i] == 0)
945 desc = sldns_rr_descript((uint16_t)i);
946 if(desc && desc->_name) {
947 snprintf(nm, sizeof(nm), "%s", desc->_name);
948 } else if (i == LDNS_RR_TYPE_IXFR) {
949 snprintf(nm, sizeof(nm), "IXFR");
950 } else if (i == LDNS_RR_TYPE_AXFR) {
951 snprintf(nm, sizeof(nm), "AXFR");
952 } else if (i == LDNS_RR_TYPE_MAILA) {
953 snprintf(nm, sizeof(nm), "MAILA");
954 } else if (i == LDNS_RR_TYPE_MAILB) {
955 snprintf(nm, sizeof(nm), "MAILB");
956 } else if (i == LDNS_RR_TYPE_ANY) {
957 snprintf(nm, sizeof(nm), "ANY");
959 snprintf(nm, sizeof(nm), "TYPE%d", i);
961 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n",
962 nm, (unsigned long)s->svr.qtype[i])) return 0;
964 if(!inhibit_zero || s->svr.qtype_big) {
965 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n",
966 (unsigned long)s->svr.qtype_big)) return 0;
969 for(i=0; i<UB_STATS_QCLASS_NUM; i++) {
970 if(inhibit_zero && s->svr.qclass[i] == 0)
972 lt = sldns_lookup_by_id(sldns_rr_classes, i);
974 snprintf(nm, sizeof(nm), "%s", lt->name);
976 snprintf(nm, sizeof(nm), "CLASS%d", i);
978 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n",
979 nm, (unsigned long)s->svr.qclass[i])) return 0;
981 if(!inhibit_zero || s->svr.qclass_big) {
982 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n",
983 (unsigned long)s->svr.qclass_big)) return 0;
986 for(i=0; i<UB_STATS_OPCODE_NUM; i++) {
987 if(inhibit_zero && s->svr.qopcode[i] == 0)
989 lt = sldns_lookup_by_id(sldns_opcodes, i);
991 snprintf(nm, sizeof(nm), "%s", lt->name);
993 snprintf(nm, sizeof(nm), "OPCODE%d", i);
995 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n",
996 nm, (unsigned long)s->svr.qopcode[i])) return 0;
999 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n",
1000 (unsigned long)s->svr.qtcp)) return 0;
1001 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n",
1002 (unsigned long)s->svr.qtcp_outgoing)) return 0;
1003 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n",
1004 (unsigned long)s->svr.qipv6)) return 0;
1006 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n",
1007 (unsigned long)s->svr.qbit_QR)) return 0;
1008 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n",
1009 (unsigned long)s->svr.qbit_AA)) return 0;
1010 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n",
1011 (unsigned long)s->svr.qbit_TC)) return 0;
1012 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n",
1013 (unsigned long)s->svr.qbit_RD)) return 0;
1014 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n",
1015 (unsigned long)s->svr.qbit_RA)) return 0;
1016 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n",
1017 (unsigned long)s->svr.qbit_Z)) return 0;
1018 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n",
1019 (unsigned long)s->svr.qbit_AD)) return 0;
1020 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n",
1021 (unsigned long)s->svr.qbit_CD)) return 0;
1022 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n",
1023 (unsigned long)s->svr.qEDNS)) return 0;
1024 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n",
1025 (unsigned long)s->svr.qEDNS_DO)) return 0;
1028 for(i=0; i<UB_STATS_RCODE_NUM; i++) {
1029 /* Always include RCODEs 0-5 */
1030 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
1032 lt = sldns_lookup_by_id(sldns_rcodes, i);
1033 if(lt && lt->name) {
1034 snprintf(nm, sizeof(nm), "%s", lt->name);
1036 snprintf(nm, sizeof(nm), "RCODE%d", i);
1038 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n",
1039 nm, (unsigned long)s->svr.ans_rcode[i])) return 0;
1041 if(!inhibit_zero || s->svr.ans_rcode_nodata) {
1042 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n",
1043 (unsigned long)s->svr.ans_rcode_nodata)) return 0;
1046 if(!ssl_printf(ssl, "num.query.ratelimited"SQ"%lu\n",
1047 (unsigned long)s->svr.queries_ratelimited)) return 0;
1049 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n",
1050 (unsigned long)s->svr.ans_secure)) return 0;
1051 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n",
1052 (unsigned long)s->svr.ans_bogus)) return 0;
1053 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n",
1054 (unsigned long)s->svr.rrset_bogus)) return 0;
1055 if(!ssl_printf(ssl, "num.query.aggressive.NOERROR"SQ"%lu\n",
1056 (unsigned long)s->svr.num_neg_cache_noerror)) return 0;
1057 if(!ssl_printf(ssl, "num.query.aggressive.NXDOMAIN"SQ"%lu\n",
1058 (unsigned long)s->svr.num_neg_cache_nxdomain)) return 0;
1059 /* threat detection */
1060 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n",
1061 (unsigned long)s->svr.unwanted_queries)) return 0;
1062 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n",
1063 (unsigned long)s->svr.unwanted_replies)) return 0;
1065 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
1066 (unsigned)s->svr.msg_cache_count)) return 0;
1067 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
1068 (unsigned)s->svr.rrset_cache_count)) return 0;
1069 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
1070 (unsigned)s->svr.infra_cache_count)) return 0;
1071 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
1072 (unsigned)s->svr.key_cache_count)) return 0;
1074 if(!ssl_printf(ssl, "dnscrypt_shared_secret.cache.count"SQ"%u\n",
1075 (unsigned)s->svr.shared_secret_cache_count)) return 0;
1076 if(!ssl_printf(ssl, "dnscrypt_nonce.cache.count"SQ"%u\n",
1077 (unsigned)s->svr.nonce_cache_count)) return 0;
1078 if(!ssl_printf(ssl, "num.query.dnscrypt.shared_secret.cachemiss"SQ"%lu\n",
1079 (unsigned long)s->svr.num_query_dnscrypt_secret_missed_cache)) return 0;
1080 if(!ssl_printf(ssl, "num.query.dnscrypt.replay"SQ"%lu\n",
1081 (unsigned long)s->svr.num_query_dnscrypt_replay)) return 0;
1082 #endif /* USE_DNSCRYPT */
1083 if(!ssl_printf(ssl, "num.query.authzone.up"SQ"%lu\n",
1084 (unsigned long)s->svr.num_query_authzone_up)) return 0;
1085 if(!ssl_printf(ssl, "num.query.authzone.down"SQ"%lu\n",
1086 (unsigned long)s->svr.num_query_authzone_down)) return 0;
1090 /** do the stats command */
1092 do_stats(SSL* ssl, struct daemon_remote* rc, int reset)
1094 struct daemon* daemon = rc->worker->daemon;
1095 struct ub_stats_info total;
1096 struct ub_stats_info s;
1098 log_assert(daemon->num > 0);
1099 /* gather all thread statistics in one place */
1100 for(i=0; i<daemon->num; i++) {
1101 server_stats_obtain(rc->worker, daemon->workers[i], &s, reset);
1102 if(!print_thread_stats(ssl, i, &s))
1106 else server_stats_add(&total, &s);
1108 /* print the thread statistics */
1109 total.mesh_time_median /= (double)daemon->num;
1110 if(!print_stats(ssl, "total", &total))
1112 if(!print_uptime(ssl, rc->worker, reset))
1114 if(daemon->cfg->stat_extended) {
1115 if(!print_mem(ssl, rc->worker, daemon))
1117 if(!print_hist(ssl, &total))
1119 if(!print_ext(ssl, &total))
1124 /** parse commandline argument domain name */
1126 parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs)
1128 uint8_t nm[LDNS_MAX_DOMAINLEN+1];
1129 size_t nmlen = sizeof(nm);
1134 status = sldns_str2wire_dname_buf(str, nm, &nmlen);
1136 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str,
1137 LDNS_WIREPARSE_OFFSET(status),
1138 sldns_get_errorstr_parse(status));
1141 *res = memdup(nm, nmlen);
1143 ssl_printf(ssl, "error out of memory\n");
1146 *labs = dname_count_size_labels(*res, len);
1150 /** find second argument, modifies string */
1152 find_arg2(SSL* ssl, char* arg, char** arg2)
1154 char* as = strchr(arg, ' ');
1155 char* at = strchr(arg, '\t');
1160 *arg2 = skipwhite(as+1);
1163 *arg2 = skipwhite(as+1);
1166 *arg2 = skipwhite(at+1);
1168 ssl_printf(ssl, "error could not find next argument "
1175 /** Add a new zone */
1177 perform_zone_add(SSL* ssl, struct local_zones* zones, char* arg)
1183 enum localzone_type t;
1184 struct local_zone* z;
1185 if(!find_arg2(ssl, arg, &arg2))
1187 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1189 if(!local_zone_str2type(arg2, &t)) {
1190 ssl_printf(ssl, "error not a zone type. %s\n", arg2);
1194 lock_rw_wrlock(&zones->lock);
1195 if((z=local_zones_find(zones, nm, nmlen,
1196 nmlabs, LDNS_RR_CLASS_IN))) {
1197 /* already present in tree */
1198 lock_rw_wrlock(&z->lock);
1199 z->type = t; /* update type anyway */
1200 lock_rw_unlock(&z->lock);
1202 lock_rw_unlock(&zones->lock);
1205 if(!local_zones_add_zone(zones, nm, nmlen,
1206 nmlabs, LDNS_RR_CLASS_IN, t)) {
1207 lock_rw_unlock(&zones->lock);
1208 ssl_printf(ssl, "error out of memory\n");
1211 lock_rw_unlock(&zones->lock);
1215 /** Do the local_zone command */
1217 do_zone_add(SSL* ssl, struct local_zones* zones, char* arg)
1219 if(!perform_zone_add(ssl, zones, arg))
1224 /** Do the local_zones command */
1226 do_zones_add(SSL* ssl, struct local_zones* zones)
1230 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1231 if(buf[0] == 0x04 && buf[1] == 0)
1232 break; /* end of transmission */
1233 if(!perform_zone_add(ssl, zones, buf)) {
1234 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1240 (void)ssl_printf(ssl, "added %d zones\n", num);
1243 /** Remove a zone */
1245 perform_zone_remove(SSL* ssl, struct local_zones* zones, char* arg)
1250 struct local_zone* z;
1251 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1253 lock_rw_wrlock(&zones->lock);
1254 if((z=local_zones_find(zones, nm, nmlen,
1255 nmlabs, LDNS_RR_CLASS_IN))) {
1256 /* present in tree */
1257 local_zones_del_zone(zones, z);
1259 lock_rw_unlock(&zones->lock);
1264 /** Do the local_zone_remove command */
1266 do_zone_remove(SSL* ssl, struct local_zones* zones, char* arg)
1268 if(!perform_zone_remove(ssl, zones, arg))
1273 /** Do the local_zones_remove command */
1275 do_zones_remove(SSL* ssl, struct local_zones* zones)
1279 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1280 if(buf[0] == 0x04 && buf[1] == 0)
1281 break; /* end of transmission */
1282 if(!perform_zone_remove(ssl, zones, buf)) {
1283 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1289 (void)ssl_printf(ssl, "removed %d zones\n", num);
1292 /** Add new RR data */
1294 perform_data_add(SSL* ssl, struct local_zones* zones, char* arg)
1296 if(!local_zones_add_RR(zones, arg)) {
1297 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg);
1303 /** Do the local_data command */
1305 do_data_add(SSL* ssl, struct local_zones* zones, char* arg)
1307 if(!perform_data_add(ssl, zones, arg))
1312 /** Do the local_datas command */
1314 do_datas_add(SSL* ssl, struct local_zones* zones)
1318 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1319 if(buf[0] == 0x04 && buf[1] == 0)
1320 break; /* end of transmission */
1321 if(!perform_data_add(ssl, zones, buf)) {
1322 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1328 (void)ssl_printf(ssl, "added %d datas\n", num);
1331 /** Remove RR data */
1333 perform_data_remove(SSL* ssl, struct local_zones* zones, char* arg)
1338 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1340 local_zones_del_data(zones, nm,
1341 nmlen, nmlabs, LDNS_RR_CLASS_IN);
1346 /** Do the local_data_remove command */
1348 do_data_remove(SSL* ssl, struct local_zones* zones, char* arg)
1350 if(!perform_data_remove(ssl, zones, arg))
1355 /** Do the local_datas_remove command */
1357 do_datas_remove(SSL* ssl, struct local_zones* zones)
1361 while(ssl_read_line(ssl, buf, sizeof(buf))) {
1362 if(buf[0] == 0x04 && buf[1] == 0)
1363 break; /* end of transmission */
1364 if(!perform_data_remove(ssl, zones, buf)) {
1365 if(!ssl_printf(ssl, "error for input line: %s\n", buf))
1371 (void)ssl_printf(ssl, "removed %d datas\n", num);
1374 /** Add a new zone to view */
1376 do_view_zone_add(SSL* ssl, struct worker* worker, char* arg)
1380 if(!find_arg2(ssl, arg, &arg2))
1382 v = views_find_view(worker->daemon->views,
1383 arg, 1 /* get write lock*/);
1385 ssl_printf(ssl,"no view with name: %s\n", arg);
1388 if(!v->local_zones) {
1389 if(!(v->local_zones = local_zones_create())){
1390 lock_rw_unlock(&v->lock);
1391 ssl_printf(ssl,"error out of memory\n");
1395 /* Global local-zone is not used for this view,
1396 * therefore add defaults to this view-specic
1398 struct config_file lz_cfg;
1399 memset(&lz_cfg, 0, sizeof(lz_cfg));
1400 local_zone_enter_defaults(v->local_zones, &lz_cfg);
1403 do_zone_add(ssl, v->local_zones, arg2);
1404 lock_rw_unlock(&v->lock);
1407 /** Remove a zone from view */
1409 do_view_zone_remove(SSL* ssl, struct worker* worker, char* arg)
1413 if(!find_arg2(ssl, arg, &arg2))
1415 v = views_find_view(worker->daemon->views,
1416 arg, 1 /* get write lock*/);
1418 ssl_printf(ssl,"no view with name: %s\n", arg);
1421 if(!v->local_zones) {
1422 lock_rw_unlock(&v->lock);
1426 do_zone_remove(ssl, v->local_zones, arg2);
1427 lock_rw_unlock(&v->lock);
1430 /** Add new RR data to view */
1432 do_view_data_add(SSL* ssl, struct worker* worker, char* arg)
1436 if(!find_arg2(ssl, arg, &arg2))
1438 v = views_find_view(worker->daemon->views,
1439 arg, 1 /* get write lock*/);
1441 ssl_printf(ssl,"no view with name: %s\n", arg);
1444 if(!v->local_zones) {
1445 if(!(v->local_zones = local_zones_create())){
1446 lock_rw_unlock(&v->lock);
1447 ssl_printf(ssl,"error out of memory\n");
1451 do_data_add(ssl, v->local_zones, arg2);
1452 lock_rw_unlock(&v->lock);
1455 /** Remove RR data from view */
1457 do_view_data_remove(SSL* ssl, struct worker* worker, char* arg)
1461 if(!find_arg2(ssl, arg, &arg2))
1463 v = views_find_view(worker->daemon->views,
1464 arg, 1 /* get write lock*/);
1466 ssl_printf(ssl,"no view with name: %s\n", arg);
1469 if(!v->local_zones) {
1470 lock_rw_unlock(&v->lock);
1474 do_data_remove(ssl, v->local_zones, arg2);
1475 lock_rw_unlock(&v->lock);
1478 /** cache lookup of nameservers */
1480 do_lookup(SSL* ssl, struct worker* worker, char* arg)
1485 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1487 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs);
1491 /** flush something from rrset and msg caches */
1493 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen,
1494 uint16_t t, uint16_t c)
1497 struct query_info k;
1498 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0);
1499 if(t == LDNS_RR_TYPE_SOA)
1500 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c,
1501 PACKED_RRSET_SOA_NEG);
1503 k.qname_len = nmlen;
1506 k.local_alias = NULL;
1507 h = query_info_hash(&k, 0);
1508 slabhash_remove(worker->env.msg_cache, h, &k);
1509 if(t == LDNS_RR_TYPE_AAAA) {
1510 /* for AAAA also flush dns64 bit_cd packet */
1511 h = query_info_hash(&k, BIT_CD);
1512 slabhash_remove(worker->env.msg_cache, h, &k);
1518 do_flush_type(SSL* ssl, struct worker* worker, char* arg)
1525 if(!find_arg2(ssl, arg, &arg2))
1527 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1529 t = sldns_get_rr_type_by_name(arg2);
1530 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN);
1536 /** flush statistics */
1538 do_flush_stats(SSL* ssl, struct worker* worker)
1540 worker_stats_clear(worker);
1545 * Local info for deletion functions
1549 struct worker* worker;
1550 /** name to delete */
1556 /** time to invalidate to */
1558 /** number of rrsets removed */
1560 /** number of msgs removed */
1562 /** number of key entries removed */
1564 /** length of addr */
1566 /** socket address for host deletion */
1567 struct sockaddr_storage addr;
1570 /** callback to delete hosts in infra cache */
1572 infra_del_host(struct lruhash_entry* e, void* arg)
1574 /* entry is locked */
1575 struct del_info* inf = (struct del_info*)arg;
1576 struct infra_key* k = (struct infra_key*)e->key;
1577 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) {
1578 struct infra_data* d = (struct infra_data*)e->data;
1581 d->timeout_AAAA = 0;
1582 d->timeout_other = 0;
1584 if(d->ttl > inf->expired) {
1585 d->ttl = inf->expired;
1591 /** flush infra cache */
1593 do_flush_infra(SSL* ssl, struct worker* worker, char* arg)
1595 struct sockaddr_storage addr;
1597 struct del_info inf;
1598 if(strcmp(arg, "all") == 0) {
1599 slabhash_clear(worker->env.infra_cache->hosts);
1603 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) {
1604 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg);
1607 /* delete all entries from cache */
1608 /* what we do is to set them all expired */
1609 inf.worker = worker;
1613 inf.expired = *worker->env.now;
1614 inf.expired -= 3; /* handle 3 seconds skew between threads */
1619 memmove(&inf.addr, &addr, len);
1620 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host,
1625 /** flush requestlist */
1627 do_flush_requestlist(SSL* ssl, struct worker* worker)
1629 mesh_delete_all(worker->env.mesh);
1633 /** callback to delete rrsets in a zone */
1635 zone_del_rrset(struct lruhash_entry* e, void* arg)
1637 /* entry is locked */
1638 struct del_info* inf = (struct del_info*)arg;
1639 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1640 if(dname_subdomain_c(k->rk.dname, inf->name)) {
1641 struct packed_rrset_data* d =
1642 (struct packed_rrset_data*)e->data;
1643 if(d->ttl > inf->expired) {
1644 d->ttl = inf->expired;
1650 /** callback to delete messages in a zone */
1652 zone_del_msg(struct lruhash_entry* e, void* arg)
1654 /* entry is locked */
1655 struct del_info* inf = (struct del_info*)arg;
1656 struct msgreply_entry* k = (struct msgreply_entry*)e->key;
1657 if(dname_subdomain_c(k->key.qname, inf->name)) {
1658 struct reply_info* d = (struct reply_info*)e->data;
1659 if(d->ttl > inf->expired) {
1660 d->ttl = inf->expired;
1661 d->prefetch_ttl = inf->expired;
1667 /** callback to delete keys in zone */
1669 zone_del_kcache(struct lruhash_entry* e, void* arg)
1671 /* entry is locked */
1672 struct del_info* inf = (struct del_info*)arg;
1673 struct key_entry_key* k = (struct key_entry_key*)e->key;
1674 if(dname_subdomain_c(k->name, inf->name)) {
1675 struct key_entry_data* d = (struct key_entry_data*)e->data;
1676 if(d->ttl > inf->expired) {
1677 d->ttl = inf->expired;
1683 /** remove all rrsets and keys from zone from cache */
1685 do_flush_zone(SSL* ssl, struct worker* worker, char* arg)
1690 struct del_info inf;
1691 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1693 /* delete all RRs and key entries from zone */
1694 /* what we do is to set them all expired */
1695 inf.worker = worker;
1699 inf.expired = *worker->env.now;
1700 inf.expired -= 3; /* handle 3 seconds skew between threads */
1704 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1705 &zone_del_rrset, &inf);
1707 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf);
1709 /* and validator cache */
1710 if(worker->env.key_cache) {
1711 slabhash_traverse(worker->env.key_cache->slab, 1,
1712 &zone_del_kcache, &inf);
1717 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1718 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1719 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1722 /** callback to delete bogus rrsets */
1724 bogus_del_rrset(struct lruhash_entry* e, void* arg)
1726 /* entry is locked */
1727 struct del_info* inf = (struct del_info*)arg;
1728 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1729 if(d->security == sec_status_bogus) {
1730 d->ttl = inf->expired;
1735 /** callback to delete bogus messages */
1737 bogus_del_msg(struct lruhash_entry* e, void* arg)
1739 /* entry is locked */
1740 struct del_info* inf = (struct del_info*)arg;
1741 struct reply_info* d = (struct reply_info*)e->data;
1742 if(d->security == sec_status_bogus) {
1743 d->ttl = inf->expired;
1748 /** callback to delete bogus keys */
1750 bogus_del_kcache(struct lruhash_entry* e, void* arg)
1752 /* entry is locked */
1753 struct del_info* inf = (struct del_info*)arg;
1754 struct key_entry_data* d = (struct key_entry_data*)e->data;
1756 d->ttl = inf->expired;
1761 /** remove all bogus rrsets, msgs and keys from cache */
1763 do_flush_bogus(SSL* ssl, struct worker* worker)
1765 struct del_info inf;
1766 /* what we do is to set them all expired */
1767 inf.worker = worker;
1768 inf.expired = *worker->env.now;
1769 inf.expired -= 3; /* handle 3 seconds skew between threads */
1773 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1774 &bogus_del_rrset, &inf);
1776 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf);
1778 /* and validator cache */
1779 if(worker->env.key_cache) {
1780 slabhash_traverse(worker->env.key_cache->slab, 1,
1781 &bogus_del_kcache, &inf);
1784 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1785 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1786 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1789 /** callback to delete negative and servfail rrsets */
1791 negative_del_rrset(struct lruhash_entry* e, void* arg)
1793 /* entry is locked */
1794 struct del_info* inf = (struct del_info*)arg;
1795 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key;
1796 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data;
1797 /* delete the parentside negative cache rrsets,
1798 * these are nameserver rrsets that failed lookup, rdata empty */
1799 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 &&
1800 d->rrsig_count == 0 && d->rr_len[0] == 0) {
1801 d->ttl = inf->expired;
1806 /** callback to delete negative and servfail messages */
1808 negative_del_msg(struct lruhash_entry* e, void* arg)
1810 /* entry is locked */
1811 struct del_info* inf = (struct del_info*)arg;
1812 struct reply_info* d = (struct reply_info*)e->data;
1813 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1814 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1815 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) {
1816 d->ttl = inf->expired;
1821 /** callback to delete negative key entries */
1823 negative_del_kcache(struct lruhash_entry* e, void* arg)
1825 /* entry is locked */
1826 struct del_info* inf = (struct del_info*)arg;
1827 struct key_entry_data* d = (struct key_entry_data*)e->data;
1828 /* could be bad because of lookup failure on the DS, DNSKEY, which
1829 * was nxdomain or servfail, and thus a result of negative lookups */
1831 d->ttl = inf->expired;
1836 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1838 do_flush_negative(SSL* ssl, struct worker* worker)
1840 struct del_info inf;
1841 /* what we do is to set them all expired */
1842 inf.worker = worker;
1843 inf.expired = *worker->env.now;
1844 inf.expired -= 3; /* handle 3 seconds skew between threads */
1848 slabhash_traverse(&worker->env.rrset_cache->table, 1,
1849 &negative_del_rrset, &inf);
1851 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf);
1853 /* and validator cache */
1854 if(worker->env.key_cache) {
1855 slabhash_traverse(worker->env.key_cache->slab, 1,
1856 &negative_del_kcache, &inf);
1859 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages "
1860 "and %lu key entries\n", (unsigned long)inf.num_rrsets,
1861 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys);
1864 /** remove name rrset from cache */
1866 do_flush_name(SSL* ssl, struct worker* w, char* arg)
1871 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
1873 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN);
1874 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN);
1875 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1876 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN);
1877 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN);
1878 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN);
1879 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN);
1880 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN);
1881 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN);
1882 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN);
1888 /** printout a delegation point info */
1890 ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass,
1894 struct delegpt_ns* ns;
1895 struct delegpt_addr* a;
1897 if(str) { /* print header for forward, stub */
1898 char* c = sldns_wire2str_class(dclass);
1900 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) {
1906 for(ns = dp->nslist; ns; ns = ns->next) {
1907 dname_str(ns->name, buf);
1908 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1912 for(a = dp->target_list; a; a = a->next_target) {
1913 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf));
1914 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf))
1918 return ssl_printf(ssl, "\n");
1922 /** print root forwards */
1924 print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root)
1927 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN);
1929 return ssl_printf(ssl, "off (using root hints)\n");
1930 /* if dp is returned it must be the root */
1931 log_assert(query_dname_compare(dp->name, root)==0);
1932 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp);
1935 /** parse args into delegpt */
1936 static struct delegpt*
1937 parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names)
1939 /* parse args and add in */
1942 struct delegpt* dp = delegpt_create_mlc(nm);
1943 struct sockaddr_storage addr;
1947 (void)ssl_printf(ssl, "error out of memory\n");
1952 p = strchr(p, ' '); /* find next spot, if any */
1954 *p++ = 0; /* end this spot */
1955 p = skipwhite(p); /* position at next spot */
1958 if(!authextstrtoaddr(todo, &addr, &addrlen, &auth_name)) {
1963 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) {
1964 (void)ssl_printf(ssl, "error cannot "
1965 "parse IP address or name "
1967 delegpt_free_mlc(dp);
1970 if(!delegpt_add_ns_mlc(dp, n, 0)) {
1971 (void)ssl_printf(ssl, "error out of memory\n");
1973 delegpt_free_mlc(dp);
1979 (void)ssl_printf(ssl, "error cannot parse"
1980 " IP address '%s'\n", todo);
1981 delegpt_free_mlc(dp);
1986 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0,
1988 (void)ssl_printf(ssl, "error out of memory\n");
1989 delegpt_free_mlc(dp);
1994 dp->has_parent_side_NS = 1;
1998 /** do the status command */
2000 do_forward(SSL* ssl, struct worker* worker, char* args)
2002 struct iter_forwards* fwd = worker->env.fwds;
2003 uint8_t* root = (uint8_t*)"\000";
2005 (void)ssl_printf(ssl, "error: structure not allocated\n");
2008 if(args == NULL || args[0] == 0) {
2009 (void)print_root_fwds(ssl, fwd, root);
2012 /* set root forwards for this thread. since we are in remote control
2013 * the actual mesh is not running, so we can freely edit it. */
2014 /* delete all the existing queries first */
2015 mesh_delete_all(worker->env.mesh);
2016 if(strcmp(args, "off") == 0) {
2017 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root);
2020 if(!(dp = parse_delegpt(ssl, args, root, 0)))
2022 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
2023 (void)ssl_printf(ssl, "error out of memory\n");
2031 parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp,
2032 int* insecure, int* prime)
2038 /* parse all -x args */
2039 while(args[0] == '+') {
2040 if(!find_arg2(ssl, args, &rest))
2042 while(*(++args) != 0) {
2043 if(*args == 'i' && insecure)
2045 else if(*args == 'p' && prime)
2048 (void)ssl_printf(ssl, "error: unknown option %s\n", args);
2056 if(!find_arg2(ssl, args, &rest))
2060 } else zonename = args;
2061 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs))
2066 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) {
2074 /** do the forward_add command */
2076 do_forward_add(SSL* ssl, struct worker* worker, char* args)
2078 struct iter_forwards* fwd = worker->env.fwds;
2081 struct delegpt* dp = NULL;
2082 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL))
2084 if(insecure && worker->env.anchors) {
2085 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2087 (void)ssl_printf(ssl, "error out of memory\n");
2088 delegpt_free_mlc(dp);
2093 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) {
2094 (void)ssl_printf(ssl, "error out of memory\n");
2102 /** do the forward_remove command */
2104 do_forward_remove(SSL* ssl, struct worker* worker, char* args)
2106 struct iter_forwards* fwd = worker->env.fwds;
2109 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
2111 if(insecure && worker->env.anchors)
2112 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2114 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm);
2119 /** do the stub_add command */
2121 do_stub_add(SSL* ssl, struct worker* worker, char* args)
2123 struct iter_forwards* fwd = worker->env.fwds;
2124 int insecure = 0, prime = 0;
2126 struct delegpt* dp = NULL;
2127 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime))
2129 if(insecure && worker->env.anchors) {
2130 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2132 (void)ssl_printf(ssl, "error out of memory\n");
2133 delegpt_free_mlc(dp);
2138 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) {
2139 if(insecure && worker->env.anchors)
2140 anchors_delete_insecure(worker->env.anchors,
2141 LDNS_RR_CLASS_IN, nm);
2142 (void)ssl_printf(ssl, "error out of memory\n");
2143 delegpt_free_mlc(dp);
2147 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) {
2148 (void)ssl_printf(ssl, "error out of memory\n");
2149 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
2150 if(insecure && worker->env.anchors)
2151 anchors_delete_insecure(worker->env.anchors,
2152 LDNS_RR_CLASS_IN, nm);
2160 /** do the stub_remove command */
2162 do_stub_remove(SSL* ssl, struct worker* worker, char* args)
2164 struct iter_forwards* fwd = worker->env.fwds;
2167 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL))
2169 if(insecure && worker->env.anchors)
2170 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN,
2172 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm);
2173 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm);
2178 /** do the insecure_add command */
2180 do_insecure_add(SSL* ssl, struct worker* worker, char* arg)
2185 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2187 if(worker->env.anchors) {
2188 if(!anchors_add_insecure(worker->env.anchors,
2189 LDNS_RR_CLASS_IN, nm)) {
2190 (void)ssl_printf(ssl, "error out of memory\n");
2199 /** do the insecure_remove command */
2201 do_insecure_remove(SSL* ssl, struct worker* worker, char* arg)
2206 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs))
2208 if(worker->env.anchors)
2209 anchors_delete_insecure(worker->env.anchors,
2210 LDNS_RR_CLASS_IN, nm);
2216 do_insecure_list(SSL* ssl, struct worker* worker)
2219 struct trust_anchor* a;
2220 if(worker->env.anchors) {
2221 RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) {
2222 if(a->numDS == 0 && a->numDNSKEY == 0) {
2223 dname_str(a->name, buf);
2224 ssl_printf(ssl, "%s\n", buf);
2230 /** do the status command */
2232 do_status(SSL* ssl, struct worker* worker)
2236 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION))
2238 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity))
2240 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num))
2242 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num))
2244 for(i=0; i<worker->daemon->mods.num; i++) {
2245 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name))
2248 if(!ssl_printf(ssl, " ]\n"))
2250 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec;
2251 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime))
2253 if(!ssl_printf(ssl, "options:%s%s\n" ,
2254 (worker->daemon->reuseport?" reuseport":""),
2255 (worker->daemon->rc->accept_list?" control(ssl)":"")))
2257 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n",
2262 /** get age for the mesh state */
2264 get_mesh_age(struct mesh_state* m, char* buf, size_t len,
2265 struct module_env* env)
2269 struct mesh_reply* r = m->reply_list;
2270 /* last reply is the oldest */
2273 timeval_subtract(&d, env->now_tv, &r->start_time);
2274 snprintf(buf, len, ARG_LL "d.%6.6d",
2275 (long long)d.tv_sec, (int)d.tv_usec);
2277 snprintf(buf, len, "-");
2281 /** get status of a mesh state */
2283 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m,
2284 char* buf, size_t len)
2286 enum module_ext_state s = m->s.ext_state[m->s.curmod];
2287 const char *modname = mesh->mods.mod[m->s.curmod]->name;
2289 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply &&
2290 m->s.minfo[m->s.curmod]) {
2291 /* break into iterator to find out who its waiting for */
2292 struct iter_qstate* qstate = (struct iter_qstate*)
2293 m->s.minfo[m->s.curmod];
2294 struct outbound_list* ol = &qstate->outlist;
2295 struct outbound_entry* e;
2296 snprintf(buf, len, "%s wait for", modname);
2299 if(ol->first == NULL)
2300 snprintf(buf, len, " (empty_list)");
2301 for(e = ol->first; e; e = e->next) {
2302 snprintf(buf, len, " ");
2305 addr_to_str(&e->qsent->addr, e->qsent->addrlen,
2310 } else if(s == module_wait_subquery) {
2311 /* look in subs from mesh state to see what */
2313 struct mesh_state_ref* sub;
2314 snprintf(buf, len, "%s wants", modname);
2317 if(m->sub_set.count == 0)
2318 snprintf(buf, len, " (empty_list)");
2319 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) {
2320 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype);
2321 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass);
2322 dname_str(sub->s->s.qinfo.qname, nm);
2323 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"),
2324 (c?c:"CLASS??"), nm);
2331 snprintf(buf, len, "%s is %s", modname, strextstate(s));
2335 /** do the dump_requestlist command */
2337 do_dump_requestlist(SSL* ssl, struct worker* worker)
2339 struct mesh_area* mesh;
2340 struct mesh_state* m;
2344 char statbuf[10240];
2345 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num))
2347 if(!ssl_printf(ssl, "# type cl name seconds module status\n"))
2349 /* show worker mesh contents */
2350 mesh = worker->env.mesh;
2352 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
2353 char* t = sldns_wire2str_type(m->s.qinfo.qtype);
2354 char* c = sldns_wire2str_class(m->s.qinfo.qclass);
2355 dname_str(m->s.qinfo.qname, buf);
2356 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env);
2357 get_mesh_status(mesh, m, statbuf, sizeof(statbuf));
2358 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n",
2359 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf,
2371 /** structure for argument data for dump infra host */
2373 /** the infra cache */
2374 struct infra_cache* infra;
2375 /** the SSL connection */
2379 /** ssl failure? stop writing and skip the rest. If the tcp
2380 * connection is broken, and writes fail, we then stop writing. */
2384 /** callback for every host element in the infra cache */
2386 dump_infra_host(struct lruhash_entry* e, void* arg)
2388 struct infra_arg* a = (struct infra_arg*)arg;
2389 struct infra_key* k = (struct infra_key*)e->key;
2390 struct infra_data* d = (struct infra_data*)e->data;
2396 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str));
2397 dname_str(k->zonename, name);
2398 port = (int)ntohs(((struct sockaddr_in*)&k->addr)->sin_port);
2399 if(port != UNBOUND_DNS_PORT) {
2400 snprintf(ip_str+strlen(ip_str), sizeof(ip_str)-strlen(ip_str),
2403 /* skip expired stuff (only backed off) */
2404 if(d->ttl < a->now) {
2405 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) {
2406 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str,
2407 name, d->rtt.rto)) {
2414 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
2415 "tA %d tAAAA %d tother %d "
2416 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
2417 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now),
2418 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto,
2419 d->timeout_A, d->timeout_AAAA, d->timeout_other,
2420 (int)d->edns_lame_known, (int)d->edns_version,
2421 (int)(a->now<d->probedelay?(d->probedelay - a->now):0),
2422 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A,
2423 (int)d->lame_other)) {
2429 /** do the dump_infra command */
2431 do_dump_infra(SSL* ssl, struct worker* worker)
2433 struct infra_arg arg;
2434 arg.infra = worker->env.infra_cache;
2436 arg.now = *worker->env.now;
2438 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg);
2441 /** do the log_reopen command */
2443 do_log_reopen(SSL* ssl, struct worker* worker)
2445 struct config_file* cfg = worker->env.cfg;
2447 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
2450 /** do the set_option command */
2452 do_set_option(SSL* ssl, struct worker* worker, char* arg)
2455 if(!find_arg2(ssl, arg, &arg2))
2457 if(!config_set_option(worker->env.cfg, arg, arg2)) {
2458 (void)ssl_printf(ssl, "error setting option\n");
2461 /* effectuate some arguments */
2462 if(strcmp(arg, "val-override-date:") == 0) {
2463 int m = modstack_find(&worker->env.mesh->mods, "validator");
2464 struct val_env* val_env = NULL;
2465 if(m != -1) val_env = (struct val_env*)worker->env.modinfo[m];
2467 val_env->date_override = worker->env.cfg->val_date_override;
2472 /* routine to printout option values over SSL */
2473 void remote_get_opt_ssl(char* line, void* arg)
2475 SSL* ssl = (SSL*)arg;
2476 (void)ssl_printf(ssl, "%s\n", line);
2479 /** do the get_option command */
2481 do_get_option(SSL* ssl, struct worker* worker, char* arg)
2484 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl);
2486 (void)ssl_printf(ssl, "error unknown option\n");
2491 /** do the list_forwards command */
2493 do_list_forwards(SSL* ssl, struct worker* worker)
2495 /* since its a per-worker structure no locks needed */
2496 struct iter_forwards* fwds = worker->env.fwds;
2497 struct iter_forward_zone* z;
2498 struct trust_anchor* a;
2500 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) {
2501 if(!z->dp) continue; /* skip empty marker for stub */
2503 /* see if it is insecure */
2505 if(worker->env.anchors &&
2506 (a=anchor_find(worker->env.anchors, z->name,
2507 z->namelabs, z->namelen, z->dclass))) {
2508 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2510 lock_basic_unlock(&a->lock);
2513 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"),
2514 z->name, z->dclass, z->dp))
2519 /** do the list_stubs command */
2521 do_list_stubs(SSL* ssl, struct worker* worker)
2523 struct iter_hints_stub* z;
2524 struct trust_anchor* a;
2527 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) {
2529 /* see if it is insecure */
2531 if(worker->env.anchors &&
2532 (a=anchor_find(worker->env.anchors, z->node.name,
2533 z->node.labs, z->node.len, z->node.dclass))) {
2534 if(!a->keylist && !a->numDS && !a->numDNSKEY)
2536 lock_basic_unlock(&a->lock);
2539 snprintf(str, sizeof(str), "stub %sprime%s",
2540 (z->noprime?"no":""), (insecure?" +i":""));
2541 if(!ssl_print_name_dp(ssl, str, z->node.name,
2542 z->node.dclass, z->dp))
2547 /** do the list_auth_zones command */
2549 do_list_auth_zones(SSL* ssl, struct auth_zones* az)
2551 struct auth_zone* z;
2552 char buf[257], buf2[256];
2553 lock_rw_rdlock(&az->lock);
2554 RBTREE_FOR(z, struct auth_zone*, &az->ztree) {
2555 lock_rw_rdlock(&z->lock);
2556 dname_str(z->name, buf);
2558 snprintf(buf2, sizeof(buf2), "expired");
2560 uint32_t serial = 0;
2561 if(auth_zone_get_serial(z, &serial))
2562 snprintf(buf2, sizeof(buf2), "serial %u",
2564 else snprintf(buf2, sizeof(buf2), "no serial");
2566 if(!ssl_printf(ssl, "%s\t%s\n", buf, buf2)) {
2567 /* failure to print */
2568 lock_rw_unlock(&z->lock);
2569 lock_rw_unlock(&az->lock);
2572 lock_rw_unlock(&z->lock);
2574 lock_rw_unlock(&az->lock);
2577 /** do the list_local_zones command */
2579 do_list_local_zones(SSL* ssl, struct local_zones* zones)
2581 struct local_zone* z;
2583 lock_rw_rdlock(&zones->lock);
2584 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2585 lock_rw_rdlock(&z->lock);
2586 dname_str(z->name, buf);
2587 if(!ssl_printf(ssl, "%s %s\n", buf,
2588 local_zone_type2str(z->type))) {
2589 /* failure to print */
2590 lock_rw_unlock(&z->lock);
2591 lock_rw_unlock(&zones->lock);
2594 lock_rw_unlock(&z->lock);
2596 lock_rw_unlock(&zones->lock);
2599 /** do the list_local_data command */
2601 do_list_local_data(SSL* ssl, struct worker* worker, struct local_zones* zones)
2603 struct local_zone* z;
2604 struct local_data* d;
2605 struct local_rrset* p;
2606 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer);
2607 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer);
2608 lock_rw_rdlock(&zones->lock);
2609 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2610 lock_rw_rdlock(&z->lock);
2611 RBTREE_FOR(d, struct local_data*, &z->data) {
2612 for(p = d->rrsets; p; p = p->next) {
2613 struct packed_rrset_data* d =
2614 (struct packed_rrset_data*)p->rrset->entry.data;
2616 for(i=0; i<d->count + d->rrsig_count; i++) {
2617 if(!packed_rr_to_string(p->rrset, i,
2619 if(!ssl_printf(ssl, "BADRR\n")) {
2620 lock_rw_unlock(&z->lock);
2621 lock_rw_unlock(&zones->lock);
2625 if(!ssl_printf(ssl, "%s\n", s)) {
2626 lock_rw_unlock(&z->lock);
2627 lock_rw_unlock(&zones->lock);
2633 lock_rw_unlock(&z->lock);
2635 lock_rw_unlock(&zones->lock);
2638 /** do the view_list_local_zones command */
2640 do_view_list_local_zones(SSL* ssl, struct worker* worker, char* arg)
2642 struct view* v = views_find_view(worker->daemon->views,
2643 arg, 0 /* get read lock*/);
2645 ssl_printf(ssl,"no view with name: %s\n", arg);
2648 if(v->local_zones) {
2649 do_list_local_zones(ssl, v->local_zones);
2651 lock_rw_unlock(&v->lock);
2654 /** do the view_list_local_data command */
2656 do_view_list_local_data(SSL* ssl, struct worker* worker, char* arg)
2658 struct view* v = views_find_view(worker->daemon->views,
2659 arg, 0 /* get read lock*/);
2661 ssl_printf(ssl,"no view with name: %s\n", arg);
2664 if(v->local_zones) {
2665 do_list_local_data(ssl, worker, v->local_zones);
2667 lock_rw_unlock(&v->lock);
2670 /** struct for user arg ratelimit list */
2671 struct ratelimit_list_arg {
2672 /** the infra cache */
2673 struct infra_cache* infra;
2674 /** the SSL to print to */
2676 /** all or only ratelimited */
2682 #define ip_ratelimit_list_arg ratelimit_list_arg
2684 /** list items in the ratelimit table */
2686 rate_list(struct lruhash_entry* e, void* arg)
2688 struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg;
2689 struct rate_key* k = (struct rate_key*)e->key;
2690 struct rate_data* d = (struct rate_data*)e->data;
2692 int lim = infra_find_ratelimit(a->infra, k->name, k->namelen);
2693 int max = infra_rate_max(d, a->now);
2698 dname_str(k->name, buf);
2699 ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim);
2702 /** list items in the ip_ratelimit table */
2704 ip_rate_list(struct lruhash_entry* e, void* arg)
2707 struct ip_ratelimit_list_arg* a = (struct ip_ratelimit_list_arg*)arg;
2708 struct ip_rate_key* k = (struct ip_rate_key*)e->key;
2709 struct ip_rate_data* d = (struct ip_rate_data*)e->data;
2710 int lim = infra_ip_ratelimit;
2711 int max = infra_rate_max(d, a->now);
2716 addr_to_str(&k->addr, k->addrlen, ip, sizeof(ip));
2717 ssl_printf(a->ssl, "%s %d limit %d\n", ip, max, lim);
2720 /** do the ratelimit_list command */
2722 do_ratelimit_list(SSL* ssl, struct worker* worker, char* arg)
2724 struct ratelimit_list_arg a;
2726 a.infra = worker->env.infra_cache;
2727 a.now = *worker->env.now;
2729 arg = skipwhite(arg);
2730 if(strcmp(arg, "+a") == 0)
2732 if(a.infra->domain_rates==NULL ||
2733 (a.all == 0 && infra_dp_ratelimit == 0))
2735 slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a);
2738 /** do the ip_ratelimit_list command */
2740 do_ip_ratelimit_list(SSL* ssl, struct worker* worker, char* arg)
2742 struct ip_ratelimit_list_arg a;
2744 a.infra = worker->env.infra_cache;
2745 a.now = *worker->env.now;
2747 arg = skipwhite(arg);
2748 if(strcmp(arg, "+a") == 0)
2750 if(a.infra->client_ip_rates==NULL ||
2751 (a.all == 0 && infra_ip_ratelimit == 0))
2753 slabhash_traverse(a.infra->client_ip_rates, 0, ip_rate_list, &a);
2756 /** tell other processes to execute the command */
2758 distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd)
2763 /* skip i=0 which is me */
2764 for(i=1; i<rc->worker->daemon->num; i++) {
2765 worker_send_cmd(rc->worker->daemon->workers[i],
2767 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd,
2768 (uint8_t*)cmd, strlen(cmd)+1, 0)) {
2769 ssl_printf(ssl, "error could not distribute cmd\n");
2775 /** check for name with end-of-string, space or tab after it */
2777 cmdcmp(char* p, const char* cmd, size_t len)
2779 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t');
2782 /** execute a remote control command */
2784 execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd,
2785 struct worker* worker)
2787 char* p = skipwhite(cmd);
2788 /* compare command */
2789 if(cmdcmp(p, "stop", 4)) {
2792 } else if(cmdcmp(p, "reload", 6)) {
2795 } else if(cmdcmp(p, "stats_noreset", 13)) {
2796 do_stats(ssl, rc, 0);
2798 } else if(cmdcmp(p, "stats", 5)) {
2799 do_stats(ssl, rc, 1);
2801 } else if(cmdcmp(p, "status", 6)) {
2802 do_status(ssl, worker);
2804 } else if(cmdcmp(p, "dump_cache", 10)) {
2805 (void)dump_cache(ssl, worker);
2807 } else if(cmdcmp(p, "load_cache", 10)) {
2808 if(load_cache(ssl, worker)) send_ok(ssl);
2810 } else if(cmdcmp(p, "list_forwards", 13)) {
2811 do_list_forwards(ssl, worker);
2813 } else if(cmdcmp(p, "list_stubs", 10)) {
2814 do_list_stubs(ssl, worker);
2816 } else if(cmdcmp(p, "list_insecure", 13)) {
2817 do_insecure_list(ssl, worker);
2819 } else if(cmdcmp(p, "list_local_zones", 16)) {
2820 do_list_local_zones(ssl, worker->daemon->local_zones);
2822 } else if(cmdcmp(p, "list_local_data", 15)) {
2823 do_list_local_data(ssl, worker, worker->daemon->local_zones);
2825 } else if(cmdcmp(p, "view_list_local_zones", 21)) {
2826 do_view_list_local_zones(ssl, worker, skipwhite(p+21));
2828 } else if(cmdcmp(p, "view_list_local_data", 20)) {
2829 do_view_list_local_data(ssl, worker, skipwhite(p+20));
2831 } else if(cmdcmp(p, "ratelimit_list", 14)) {
2832 do_ratelimit_list(ssl, worker, p+14);
2834 } else if(cmdcmp(p, "ip_ratelimit_list", 17)) {
2835 do_ip_ratelimit_list(ssl, worker, p+17);
2837 } else if(cmdcmp(p, "list_auth_zones", 15)) {
2838 do_list_auth_zones(ssl, worker->env.auth_zones);
2840 } else if(cmdcmp(p, "stub_add", 8)) {
2841 /* must always distribute this cmd */
2842 if(rc) distribute_cmd(rc, ssl, cmd);
2843 do_stub_add(ssl, worker, skipwhite(p+8));
2845 } else if(cmdcmp(p, "stub_remove", 11)) {
2846 /* must always distribute this cmd */
2847 if(rc) distribute_cmd(rc, ssl, cmd);
2848 do_stub_remove(ssl, worker, skipwhite(p+11));
2850 } else if(cmdcmp(p, "forward_add", 11)) {
2851 /* must always distribute this cmd */
2852 if(rc) distribute_cmd(rc, ssl, cmd);
2853 do_forward_add(ssl, worker, skipwhite(p+11));
2855 } else if(cmdcmp(p, "forward_remove", 14)) {
2856 /* must always distribute this cmd */
2857 if(rc) distribute_cmd(rc, ssl, cmd);
2858 do_forward_remove(ssl, worker, skipwhite(p+14));
2860 } else if(cmdcmp(p, "insecure_add", 12)) {
2861 /* must always distribute this cmd */
2862 if(rc) distribute_cmd(rc, ssl, cmd);
2863 do_insecure_add(ssl, worker, skipwhite(p+12));
2865 } else if(cmdcmp(p, "insecure_remove", 15)) {
2866 /* must always distribute this cmd */
2867 if(rc) distribute_cmd(rc, ssl, cmd);
2868 do_insecure_remove(ssl, worker, skipwhite(p+15));
2870 } else if(cmdcmp(p, "forward", 7)) {
2871 /* must always distribute this cmd */
2872 if(rc) distribute_cmd(rc, ssl, cmd);
2873 do_forward(ssl, worker, skipwhite(p+7));
2875 } else if(cmdcmp(p, "flush_stats", 11)) {
2876 /* must always distribute this cmd */
2877 if(rc) distribute_cmd(rc, ssl, cmd);
2878 do_flush_stats(ssl, worker);
2880 } else if(cmdcmp(p, "flush_requestlist", 17)) {
2881 /* must always distribute this cmd */
2882 if(rc) distribute_cmd(rc, ssl, cmd);
2883 do_flush_requestlist(ssl, worker);
2885 } else if(cmdcmp(p, "lookup", 6)) {
2886 do_lookup(ssl, worker, skipwhite(p+6));
2890 #ifdef THREADS_DISABLED
2891 /* other processes must execute the command as well */
2892 /* commands that should not be distributed, returned above. */
2893 if(rc) { /* only if this thread is the master (rc) thread */
2894 /* done before the code below, which may split the string */
2895 distribute_cmd(rc, ssl, cmd);
2898 if(cmdcmp(p, "verbosity", 9)) {
2899 do_verbosity(ssl, skipwhite(p+9));
2900 } else if(cmdcmp(p, "local_zone_remove", 17)) {
2901 do_zone_remove(ssl, worker->daemon->local_zones, skipwhite(p+17));
2902 } else if(cmdcmp(p, "local_zones_remove", 18)) {
2903 do_zones_remove(ssl, worker->daemon->local_zones);
2904 } else if(cmdcmp(p, "local_zone", 10)) {
2905 do_zone_add(ssl, worker->daemon->local_zones, skipwhite(p+10));
2906 } else if(cmdcmp(p, "local_zones", 11)) {
2907 do_zones_add(ssl, worker->daemon->local_zones);
2908 } else if(cmdcmp(p, "local_data_remove", 17)) {
2909 do_data_remove(ssl, worker->daemon->local_zones, skipwhite(p+17));
2910 } else if(cmdcmp(p, "local_datas_remove", 18)) {
2911 do_datas_remove(ssl, worker->daemon->local_zones);
2912 } else if(cmdcmp(p, "local_data", 10)) {
2913 do_data_add(ssl, worker->daemon->local_zones, skipwhite(p+10));
2914 } else if(cmdcmp(p, "local_datas", 11)) {
2915 do_datas_add(ssl, worker->daemon->local_zones);
2916 } else if(cmdcmp(p, "view_local_zone_remove", 22)) {
2917 do_view_zone_remove(ssl, worker, skipwhite(p+22));
2918 } else if(cmdcmp(p, "view_local_zone", 15)) {
2919 do_view_zone_add(ssl, worker, skipwhite(p+15));
2920 } else if(cmdcmp(p, "view_local_data_remove", 22)) {
2921 do_view_data_remove(ssl, worker, skipwhite(p+22));
2922 } else if(cmdcmp(p, "view_local_data", 15)) {
2923 do_view_data_add(ssl, worker, skipwhite(p+15));
2924 } else if(cmdcmp(p, "flush_zone", 10)) {
2925 do_flush_zone(ssl, worker, skipwhite(p+10));
2926 } else if(cmdcmp(p, "flush_type", 10)) {
2927 do_flush_type(ssl, worker, skipwhite(p+10));
2928 } else if(cmdcmp(p, "flush_infra", 11)) {
2929 do_flush_infra(ssl, worker, skipwhite(p+11));
2930 } else if(cmdcmp(p, "flush", 5)) {
2931 do_flush_name(ssl, worker, skipwhite(p+5));
2932 } else if(cmdcmp(p, "dump_requestlist", 16)) {
2933 do_dump_requestlist(ssl, worker);
2934 } else if(cmdcmp(p, "dump_infra", 10)) {
2935 do_dump_infra(ssl, worker);
2936 } else if(cmdcmp(p, "log_reopen", 10)) {
2937 do_log_reopen(ssl, worker);
2938 } else if(cmdcmp(p, "set_option", 10)) {
2939 do_set_option(ssl, worker, skipwhite(p+10));
2940 } else if(cmdcmp(p, "get_option", 10)) {
2941 do_get_option(ssl, worker, skipwhite(p+10));
2942 } else if(cmdcmp(p, "flush_bogus", 11)) {
2943 do_flush_bogus(ssl, worker);
2944 } else if(cmdcmp(p, "flush_negative", 14)) {
2945 do_flush_negative(ssl, worker);
2947 (void)ssl_printf(ssl, "error unknown command '%s'\n", p);
2952 daemon_remote_exec(struct worker* worker)
2954 /* read the cmd string */
2955 uint8_t* msg = NULL;
2957 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) {
2958 log_err("daemon_remote_exec: tube_read_msg failed");
2961 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg);
2962 execute_cmd(NULL, NULL, (char*)msg, worker);
2966 /** handle remote control request */
2968 handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl)
2975 /* makes it possible to set the socket blocking again. */
2976 /* basically removes it from winsock_event ... */
2977 WSAEventSelect(s->c->fd, NULL, 0);
2979 fd_set_block(s->c->fd);
2981 /* try to read magic UBCT[version]_space_ string */
2983 if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) {
2984 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN)
2986 log_crypto_err("could not SSL_read");
2990 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) {
2991 verbose(VERB_QUERY, "control connection has bad magic string");
2992 /* probably wrong tool connected, ignore it completely */
2996 /* read the command line */
2997 if(!ssl_read_line(ssl, buf, sizeof(buf))) {
3000 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
3001 if(strcmp(magic, pre) != 0) {
3002 verbose(VERB_QUERY, "control connection had bad "
3003 "version %s, cmd: %s", magic, buf);
3004 ssl_printf(ssl, "error version mismatch\n");
3007 verbose(VERB_DETAIL, "control cmd: %s", buf);
3009 /* figure out what to do */
3010 execute_cmd(rc, ssl, buf, rc->worker);
3013 int remote_control_callback(struct comm_point* c, void* arg, int err,
3014 struct comm_reply* ATTR_UNUSED(rep))
3016 struct rc_state* s = (struct rc_state*)arg;
3017 struct daemon_remote* rc = s->rc;
3019 if(err != NETEVENT_NOERROR) {
3020 if(err==NETEVENT_TIMEOUT)
3021 log_err("remote control timed out");
3025 /* (continue to) setup the SSL connection */
3027 r = SSL_do_handshake(s->ssl);
3029 int r2 = SSL_get_error(s->ssl, r);
3030 if(r2 == SSL_ERROR_WANT_READ) {
3031 if(s->shake_state == rc_hs_read) {
3032 /* try again later */
3035 s->shake_state = rc_hs_read;
3036 comm_point_listen_for_rw(c, 1, 0);
3038 } else if(r2 == SSL_ERROR_WANT_WRITE) {
3039 if(s->shake_state == rc_hs_write) {
3040 /* try again later */
3043 s->shake_state = rc_hs_write;
3044 comm_point_listen_for_rw(c, 0, 1);
3048 log_err("remote control connection closed prematurely");
3049 log_addr(1, "failed connection from",
3050 &s->c->repinfo.addr, s->c->repinfo.addrlen);
3051 log_crypto_err("remote control failed ssl");
3056 s->shake_state = rc_none;
3058 /* once handshake has completed, check authentication */
3059 if (!rc->use_cert) {
3060 verbose(VERB_ALGO, "unauthenticated remote control connection");
3061 } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) {
3062 X509* x = SSL_get_peer_certificate(s->ssl);
3064 verbose(VERB_DETAIL, "remote control connection "
3065 "provided no client certificate");
3069 verbose(VERB_ALGO, "remote control connection authenticated");
3072 verbose(VERB_DETAIL, "remote control connection failed to "
3073 "authenticate with client certificate");
3078 /* if OK start to actually handle the request */
3079 handle_req(rc, s, s->ssl);
3081 verbose(VERB_ALGO, "remote control operation completed");