2 * daemon/remote.c - remote control for the unbound daemon.
4 * Copyright (c) 2008, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains the remote control functionality for the daemon.
40 * The remote control can be performed using either the commandline
41 * unbound-control tool, or a TLS capable web browser.
42 * The channel is secured using TLSv1, and certificates.
43 * Both the server and the client(control tool) have their own keys.
46 #ifdef HAVE_OPENSSL_ERR_H
47 #include <openssl/err.h>
50 #include "daemon/remote.h"
51 #include "daemon/worker.h"
52 #include "daemon/daemon.h"
53 #include "daemon/stats.h"
54 #include "daemon/cachedump.h"
56 #include "util/config_file.h"
57 #include "util/net_help.h"
58 #include "util/module.h"
59 #include "services/listen_dnsport.h"
60 #include "services/cache/rrset.h"
61 #include "services/cache/infra.h"
62 #include "services/mesh.h"
63 #include "services/localzone.h"
64 #include "util/storage/slabhash.h"
65 #include "util/fptr_wlist.h"
66 #include "util/data/dname.h"
67 #include "validator/validator.h"
68 #include "validator/val_kcache.h"
69 #include "validator/val_kentry.h"
70 #include "validator/val_anchor.h"
71 #include "iterator/iterator.h"
72 #include "iterator/iter_fwd.h"
73 #include "iterator/iter_hints.h"
74 #include "iterator/iter_delegpt.h"
75 #include "services/outbound_list.h"
76 #include "services/outside_network.h"
77 #include "ldns/str2wire.h"
78 #include "ldns/parseutil.h"
79 #include "ldns/wire2str.h"
80 #include "ldns/sbuffer.h"
82 #ifdef HAVE_SYS_TYPES_H
83 # include <sys/types.h>
89 /* just for portability */
94 /** what to put on statistics lines between var and value, ": " or "=" */
96 /** if true, inhibits a lot of =0 lines from the stats output */
97 static const int inhibit_zero
= 1;
99 /** subtract timers and the values do not overflow or become negative */
101 timeval_subtract(struct timeval
* d
, const struct timeval
* end
,
102 const struct timeval
* start
)
105 time_t end_usec
= end
->tv_usec
;
106 d
->tv_sec
= end
->tv_sec
- start
->tv_sec
;
107 if(end_usec
< start
->tv_usec
) {
111 d
->tv_usec
= end_usec
- start
->tv_usec
;
115 /** divide sum of timers to get average */
117 timeval_divide(struct timeval
* avg
, const struct timeval
* sum
, size_t d
)
126 avg
->tv_sec
= sum
->tv_sec
/ d
;
127 avg
->tv_usec
= sum
->tv_usec
/ d
;
128 /* handle fraction from seconds divide */
129 leftover
= sum
->tv_sec
- avg
->tv_sec
*d
;
130 avg
->tv_usec
+= (leftover
*1000000)/d
;
134 struct daemon_remote
*
135 daemon_remote_create(struct config_file
* cfg
)
139 struct daemon_remote
* rc
= (struct daemon_remote
*)calloc(1,
142 log_err("out of memory in daemon_remote_create");
147 if(!cfg
->remote_control_enable
) {
151 rc
->ctx
= SSL_CTX_new(SSLv23_server_method());
153 log_crypto_err("could not SSL_CTX_new");
157 /* no SSLv2, SSLv3 because has defects */
158 if(!(SSL_CTX_set_options(rc
->ctx
, SSL_OP_NO_SSLv2
) & SSL_OP_NO_SSLv2
)){
159 log_crypto_err("could not set SSL_OP_NO_SSLv2");
160 daemon_remote_delete(rc
);
163 if(!(SSL_CTX_set_options(rc
->ctx
, SSL_OP_NO_SSLv3
) & SSL_OP_NO_SSLv3
)){
164 log_crypto_err("could not set SSL_OP_NO_SSLv3");
165 daemon_remote_delete(rc
);
168 s_cert
= fname_after_chroot(cfg
->server_cert_file
, cfg
, 1);
169 s_key
= fname_after_chroot(cfg
->server_key_file
, cfg
, 1);
170 if(!s_cert
|| !s_key
) {
171 log_err("out of memory in remote control fname");
174 verbose(VERB_ALGO
, "setup SSL certificates");
175 if (!SSL_CTX_use_certificate_file(rc
->ctx
,s_cert
,SSL_FILETYPE_PEM
)) {
176 log_err("Error for server-cert-file: %s", s_cert
);
177 log_crypto_err("Error in SSL_CTX use_certificate_file");
180 if(!SSL_CTX_use_PrivateKey_file(rc
->ctx
,s_key
,SSL_FILETYPE_PEM
)) {
181 log_err("Error for server-key-file: %s", s_key
);
182 log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
185 if(!SSL_CTX_check_private_key(rc
->ctx
)) {
186 log_err("Error for server-key-file: %s", s_key
);
187 log_crypto_err("Error in SSL_CTX check_private_key");
190 if(!SSL_CTX_load_verify_locations(rc
->ctx
, s_cert
, NULL
)) {
191 log_crypto_err("Error setting up SSL_CTX verify locations");
195 daemon_remote_delete(rc
);
198 SSL_CTX_set_client_CA_list(rc
->ctx
, SSL_load_client_CA_file(s_cert
));
199 SSL_CTX_set_verify(rc
->ctx
, SSL_VERIFY_PEER
, NULL
);
206 void daemon_remote_clear(struct daemon_remote
* rc
)
208 struct rc_state
* p
, *np
;
210 /* but do not close the ports */
211 listen_list_delete(rc
->accept_list
);
212 rc
->accept_list
= NULL
;
213 /* do close these sockets */
219 comm_point_delete(p
->c
);
223 rc
->busy_list
= NULL
;
228 void daemon_remote_delete(struct daemon_remote
* rc
)
231 daemon_remote_clear(rc
);
233 SSL_CTX_free(rc
->ctx
);
239 * Add and open a new control port
242 * @param list: list head
243 * @param noproto_is_err: if lack of protocol support is an error.
244 * @return false on failure.
247 add_open(const char* ip
, int nr
, struct listen_port
** list
, int noproto_is_err
)
249 struct addrinfo hints
;
250 struct addrinfo
* res
;
251 struct listen_port
* n
;
255 snprintf(port
, sizeof(port
), "%d", nr
);
256 port
[sizeof(port
)-1]=0;
257 memset(&hints
, 0, sizeof(hints
));
258 hints
.ai_socktype
= SOCK_STREAM
;
259 hints
.ai_flags
= AI_PASSIVE
| AI_NUMERICHOST
;
260 if((r
= getaddrinfo(ip
, port
, &hints
, &res
)) != 0 || !res
) {
262 if(!noproto_is_err
&& r
== EAI_NONAME
) {
263 /* tried to lookup the address as name */
264 return 1; /* return success, but do nothing */
266 #endif /* USE_WINSOCK */
267 log_err("control interface %s:%s getaddrinfo: %s %s",
268 ip
?ip
:"default", port
, gai_strerror(r
),
270 r
==EAI_SYSTEM
?(char*)strerror(errno
):""
279 fd
= create_tcp_accept_sock(res
, 1, &noproto
, 0);
281 if(fd
== -1 && noproto
) {
283 return 1; /* return success, but do nothing */
284 log_err("cannot open control interface %s %d : "
285 "protocol not supported", ip
, nr
);
289 log_err("cannot open control interface %s %d", ip
, nr
);
294 n
= (struct listen_port
*)calloc(1, sizeof(*n
));
301 log_err("out of memory");
310 struct listen_port
* daemon_remote_open_ports(struct config_file
* cfg
)
312 struct listen_port
* l
= NULL
;
313 log_assert(cfg
->remote_control_enable
&& cfg
->control_port
);
314 if(cfg
->control_ifs
) {
315 struct config_strlist
* p
;
316 for(p
= cfg
->control_ifs
; p
; p
= p
->next
) {
317 if(!add_open(p
->str
, cfg
->control_port
, &l
, 1)) {
318 listening_ports_free(l
);
325 !add_open("::1", cfg
->control_port
, &l
, 0)) {
326 listening_ports_free(l
);
330 !add_open("127.0.0.1", cfg
->control_port
, &l
, 1)) {
331 listening_ports_free(l
);
338 /** open accept commpoint */
340 accept_open(struct daemon_remote
* rc
, int fd
)
342 struct listen_list
* n
= (struct listen_list
*)malloc(sizeof(*n
));
344 log_err("out of memory");
347 n
->next
= rc
->accept_list
;
350 n
->com
= comm_point_create_raw(rc
->worker
->base
, fd
, 0,
351 &remote_accept_callback
, rc
);
354 /* keep this port open, its fd is kept in the rc portlist */
355 n
->com
->do_not_close
= 1;
359 int daemon_remote_open_accept(struct daemon_remote
* rc
,
360 struct listen_port
* ports
, struct worker
* worker
)
362 struct listen_port
* p
;
364 for(p
= ports
; p
; p
= p
->next
) {
365 if(!accept_open(rc
, p
->fd
)) {
366 log_err("could not create accept comm point");
373 void daemon_remote_stop_accept(struct daemon_remote
* rc
)
375 struct listen_list
* p
;
376 for(p
=rc
->accept_list
; p
; p
=p
->next
) {
377 comm_point_stop_listening(p
->com
);
381 void daemon_remote_start_accept(struct daemon_remote
* rc
)
383 struct listen_list
* p
;
384 for(p
=rc
->accept_list
; p
; p
=p
->next
) {
385 comm_point_start_listening(p
->com
, -1, -1);
389 int remote_accept_callback(struct comm_point
* c
, void* arg
, int err
,
390 struct comm_reply
* ATTR_UNUSED(rep
))
392 struct daemon_remote
* rc
= (struct daemon_remote
*)arg
;
393 struct sockaddr_storage addr
;
397 if(err
!= NETEVENT_NOERROR
) {
398 log_err("error %d on remote_accept_callback", err
);
401 /* perform the accept */
402 newfd
= comm_point_perform_accept(c
, &addr
, &addrlen
);
405 /* create new commpoint unless we are servicing already */
406 if(rc
->active
>= rc
->max_active
) {
407 log_warn("drop incoming remote control: too many connections");
417 /* setup commpoint to service the remote control command */
418 n
= (struct rc_state
*)calloc(1, sizeof(*n
));
420 log_err("out of memory");
423 /* start in reading state */
424 n
->c
= comm_point_create_raw(rc
->worker
->base
, newfd
, 0,
425 &remote_control_callback
, n
);
427 log_err("out of memory");
431 log_addr(VERB_QUERY
, "new control connection from", &addr
, addrlen
);
432 n
->c
->do_not_close
= 0;
433 comm_point_stop_listening(n
->c
);
434 comm_point_start_listening(n
->c
, -1, REMOTE_CONTROL_TCP_TIMEOUT
);
435 memcpy(&n
->c
->repinfo
.addr
, &addr
, addrlen
);
436 n
->c
->repinfo
.addrlen
= addrlen
;
437 n
->shake_state
= rc_hs_read
;
438 n
->ssl
= SSL_new(rc
->ctx
);
440 log_crypto_err("could not SSL_new");
441 comm_point_delete(n
->c
);
445 SSL_set_accept_state(n
->ssl
);
446 (void)SSL_set_mode(n
->ssl
, SSL_MODE_AUTO_RETRY
);
447 if(!SSL_set_fd(n
->ssl
, newfd
)) {
448 log_crypto_err("could not SSL_set_fd");
450 comm_point_delete(n
->c
);
456 n
->next
= rc
->busy_list
;
460 /* perform the first nonblocking read already, for windows,
461 * so it can return wouldblock. could be faster too. */
462 (void)remote_control_callback(n
->c
, n
, NETEVENT_NOERROR
, NULL
);
466 /** delete from list */
468 state_list_remove_elem(struct rc_state
** list
, struct comm_point
* c
)
471 if( (*list
)->c
== c
) {
472 *list
= (*list
)->next
;
475 list
= &(*list
)->next
;
479 /** decrease active count and remove commpoint from busy list */
481 clean_point(struct daemon_remote
* rc
, struct rc_state
* s
)
483 state_list_remove_elem(&rc
->busy_list
, s
->c
);
486 SSL_shutdown(s
->ssl
);
489 comm_point_delete(s
->c
);
494 ssl_print_text(SSL
* ssl
, const char* text
)
500 if((r
=SSL_write(ssl
, text
, (int)strlen(text
))) <= 0) {
501 if(SSL_get_error(ssl
, r
) == SSL_ERROR_ZERO_RETURN
) {
502 verbose(VERB_QUERY
, "warning, in SSL_write, peer "
503 "closed connection");
506 log_crypto_err("could not SSL_write");
512 /** print text over the ssl connection */
514 ssl_print_vmsg(SSL
* ssl
, const char* format
, va_list args
)
517 vsnprintf(msg
, sizeof(msg
), format
, args
);
518 return ssl_print_text(ssl
, msg
);
521 /** printf style printing to the ssl connection */
522 int ssl_printf(SSL
* ssl
, const char* format
, ...)
526 va_start(args
, format
);
527 ret
= ssl_print_vmsg(ssl
, format
, args
);
533 ssl_read_line(SSL
* ssl
, char* buf
, size_t max
)
541 if((r
=SSL_read(ssl
, buf
+len
, 1)) <= 0) {
542 if(SSL_get_error(ssl
, r
) == SSL_ERROR_ZERO_RETURN
) {
546 log_crypto_err("could not SSL_read");
549 if(buf
[len
] == '\n') {
550 /* return string without \n */
557 log_err("control line too long (%d): %s", (int)max
, buf
);
561 /** skip whitespace, return new pointer into string */
565 /* EOS \0 is not a space */
566 while( isspace((unsigned char)*str
) )
571 /** send the OK to the control client */
572 static void send_ok(SSL
* ssl
)
574 (void)ssl_printf(ssl
, "ok\n");
577 /** do the stop command */
579 do_stop(SSL
* ssl
, struct daemon_remote
* rc
)
581 rc
->worker
->need_to_exit
= 1;
582 comm_base_exit(rc
->worker
->base
);
586 /** do the reload command */
588 do_reload(SSL
* ssl
, struct daemon_remote
* rc
)
590 rc
->worker
->need_to_exit
= 0;
591 comm_base_exit(rc
->worker
->base
);
595 /** do the verbosity command */
597 do_verbosity(SSL
* ssl
, char* str
)
600 if(val
== 0 && strcmp(str
, "0") != 0) {
601 ssl_printf(ssl
, "error in verbosity number syntax: %s\n", str
);
608 /** print stats from statinfo */
610 print_stats(SSL
* ssl
, const char* nm
, struct stats_info
* s
)
613 if(!ssl_printf(ssl
, "%s.num.queries"SQ
"%lu\n", nm
,
614 (unsigned long)s
->svr
.num_queries
)) return 0;
615 if(!ssl_printf(ssl
, "%s.num.cachehits"SQ
"%lu\n", nm
,
616 (unsigned long)(s
->svr
.num_queries
617 - s
->svr
.num_queries_missed_cache
))) return 0;
618 if(!ssl_printf(ssl
, "%s.num.cachemiss"SQ
"%lu\n", nm
,
619 (unsigned long)s
->svr
.num_queries_missed_cache
)) return 0;
620 if(!ssl_printf(ssl
, "%s.num.prefetch"SQ
"%lu\n", nm
,
621 (unsigned long)s
->svr
.num_queries_prefetch
)) return 0;
622 if(!ssl_printf(ssl
, "%s.num.recursivereplies"SQ
"%lu\n", nm
,
623 (unsigned long)s
->mesh_replies_sent
)) return 0;
624 if(!ssl_printf(ssl
, "%s.requestlist.avg"SQ
"%g\n", nm
,
625 (s
->svr
.num_queries_missed_cache
+s
->svr
.num_queries_prefetch
)?
626 (double)s
->svr
.sum_query_list_size
/
627 (s
->svr
.num_queries_missed_cache
+
628 s
->svr
.num_queries_prefetch
) : 0.0)) return 0;
629 if(!ssl_printf(ssl
, "%s.requestlist.max"SQ
"%lu\n", nm
,
630 (unsigned long)s
->svr
.max_query_list_size
)) return 0;
631 if(!ssl_printf(ssl
, "%s.requestlist.overwritten"SQ
"%lu\n", nm
,
632 (unsigned long)s
->mesh_jostled
)) return 0;
633 if(!ssl_printf(ssl
, "%s.requestlist.exceeded"SQ
"%lu\n", nm
,
634 (unsigned long)s
->mesh_dropped
)) return 0;
635 if(!ssl_printf(ssl
, "%s.requestlist.current.all"SQ
"%lu\n", nm
,
636 (unsigned long)s
->mesh_num_states
)) return 0;
637 if(!ssl_printf(ssl
, "%s.requestlist.current.user"SQ
"%lu\n", nm
,
638 (unsigned long)s
->mesh_num_reply_states
)) return 0;
639 timeval_divide(&avg
, &s
->mesh_replies_sum_wait
, s
->mesh_replies_sent
);
640 if(!ssl_printf(ssl
, "%s.recursion.time.avg"SQ ARG_LL
"d.%6.6d\n", nm
,
641 (long long)avg
.tv_sec
, (int)avg
.tv_usec
)) return 0;
642 if(!ssl_printf(ssl
, "%s.recursion.time.median"SQ
"%g\n", nm
,
643 s
->mesh_time_median
)) return 0;
647 /** print stats for one thread */
649 print_thread_stats(SSL
* ssl
, int i
, struct stats_info
* s
)
652 snprintf(nm
, sizeof(nm
), "thread%d", i
);
654 return print_stats(ssl
, nm
, s
);
657 /** print long number */
659 print_longnum(SSL
* ssl
, const char* desc
, size_t x
)
661 if(x
> 1024*1024*1024) {
663 size_t front
= x
/ (size_t)1000000;
664 size_t back
= x
% (size_t)1000000;
665 return ssl_printf(ssl
, "%s%u%6.6u\n", desc
,
666 (unsigned)front
, (unsigned)back
);
668 return ssl_printf(ssl
, "%s%lu\n", desc
, (unsigned long)x
);
672 /** print mem stats */
674 print_mem(SSL
* ssl
, struct worker
* worker
, struct daemon
* daemon
)
677 size_t msg
, rrset
, val
, iter
;
679 extern void* unbound_start_brk
;
681 if(!print_longnum(ssl
, "mem.total.sbrk"SQ
,
682 (size_t)((char*)cur
- (char*)unbound_start_brk
))) return 0;
683 #endif /* HAVE_SBRK */
684 msg
= slabhash_get_mem(daemon
->env
->msg_cache
);
685 rrset
= slabhash_get_mem(&daemon
->env
->rrset_cache
->table
);
688 m
= modstack_find(&worker
->env
.mesh
->mods
, "validator");
690 fptr_ok(fptr_whitelist_mod_get_mem(worker
->env
.mesh
->
691 mods
.mod
[m
]->get_mem
));
692 val
= (*worker
->env
.mesh
->mods
.mod
[m
]->get_mem
)
695 m
= modstack_find(&worker
->env
.mesh
->mods
, "iterator");
697 fptr_ok(fptr_whitelist_mod_get_mem(worker
->env
.mesh
->
698 mods
.mod
[m
]->get_mem
));
699 iter
= (*worker
->env
.mesh
->mods
.mod
[m
]->get_mem
)
703 if(!print_longnum(ssl
, "mem.cache.rrset"SQ
, rrset
))
705 if(!print_longnum(ssl
, "mem.cache.message"SQ
, msg
))
707 if(!print_longnum(ssl
, "mem.mod.iterator"SQ
, iter
))
709 if(!print_longnum(ssl
, "mem.mod.validator"SQ
, val
))
714 /** print uptime stats */
716 print_uptime(SSL
* ssl
, struct worker
* worker
, int reset
)
718 struct timeval now
= *worker
->env
.now_tv
;
719 struct timeval up
, dt
;
720 timeval_subtract(&up
, &now
, &worker
->daemon
->time_boot
);
721 timeval_subtract(&dt
, &now
, &worker
->daemon
->time_last_stat
);
723 worker
->daemon
->time_last_stat
= now
;
724 if(!ssl_printf(ssl
, "time.now"SQ ARG_LL
"d.%6.6d\n",
725 (long long)now
.tv_sec
, (unsigned)now
.tv_usec
)) return 0;
726 if(!ssl_printf(ssl
, "time.up"SQ ARG_LL
"d.%6.6d\n",
727 (long long)up
.tv_sec
, (unsigned)up
.tv_usec
)) return 0;
728 if(!ssl_printf(ssl
, "time.elapsed"SQ ARG_LL
"d.%6.6d\n",
729 (long long)dt
.tv_sec
, (unsigned)dt
.tv_usec
)) return 0;
733 /** print extended histogram */
735 print_hist(SSL
* ssl
, struct stats_info
* s
)
737 struct timehist
* hist
;
739 hist
= timehist_setup();
741 log_err("out of memory");
744 timehist_import(hist
, s
->svr
.hist
, NUM_BUCKETS_HIST
);
745 for(i
=0; i
<hist
->num
; i
++) {
747 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
748 (int)hist
->buckets
[i
].lower
.tv_sec
,
749 (int)hist
->buckets
[i
].lower
.tv_usec
,
750 (int)hist
->buckets
[i
].upper
.tv_sec
,
751 (int)hist
->buckets
[i
].upper
.tv_usec
,
752 (unsigned long)hist
->buckets
[i
].count
)) {
753 timehist_delete(hist
);
757 timehist_delete(hist
);
761 /** print extended stats */
763 print_ext(SSL
* ssl
, struct stats_info
* s
)
767 const sldns_rr_descriptor
* desc
;
768 const sldns_lookup_table
* lt
;
770 for(i
=0; i
<STATS_QTYPE_NUM
; i
++) {
771 if(inhibit_zero
&& s
->svr
.qtype
[i
] == 0)
773 desc
= sldns_rr_descript((uint16_t)i
);
774 if(desc
&& desc
->_name
) {
775 snprintf(nm
, sizeof(nm
), "%s", desc
->_name
);
776 } else if (i
== LDNS_RR_TYPE_IXFR
) {
777 snprintf(nm
, sizeof(nm
), "IXFR");
778 } else if (i
== LDNS_RR_TYPE_AXFR
) {
779 snprintf(nm
, sizeof(nm
), "AXFR");
780 } else if (i
== LDNS_RR_TYPE_MAILA
) {
781 snprintf(nm
, sizeof(nm
), "MAILA");
782 } else if (i
== LDNS_RR_TYPE_MAILB
) {
783 snprintf(nm
, sizeof(nm
), "MAILB");
784 } else if (i
== LDNS_RR_TYPE_ANY
) {
785 snprintf(nm
, sizeof(nm
), "ANY");
787 snprintf(nm
, sizeof(nm
), "TYPE%d", i
);
789 if(!ssl_printf(ssl
, "num.query.type.%s"SQ
"%lu\n",
790 nm
, (unsigned long)s
->svr
.qtype
[i
])) return 0;
792 if(!inhibit_zero
|| s
->svr
.qtype_big
) {
793 if(!ssl_printf(ssl
, "num.query.type.other"SQ
"%lu\n",
794 (unsigned long)s
->svr
.qtype_big
)) return 0;
797 for(i
=0; i
<STATS_QCLASS_NUM
; i
++) {
798 if(inhibit_zero
&& s
->svr
.qclass
[i
] == 0)
800 lt
= sldns_lookup_by_id(sldns_rr_classes
, i
);
802 snprintf(nm
, sizeof(nm
), "%s", lt
->name
);
804 snprintf(nm
, sizeof(nm
), "CLASS%d", i
);
806 if(!ssl_printf(ssl
, "num.query.class.%s"SQ
"%lu\n",
807 nm
, (unsigned long)s
->svr
.qclass
[i
])) return 0;
809 if(!inhibit_zero
|| s
->svr
.qclass_big
) {
810 if(!ssl_printf(ssl
, "num.query.class.other"SQ
"%lu\n",
811 (unsigned long)s
->svr
.qclass_big
)) return 0;
814 for(i
=0; i
<STATS_OPCODE_NUM
; i
++) {
815 if(inhibit_zero
&& s
->svr
.qopcode
[i
] == 0)
817 lt
= sldns_lookup_by_id(sldns_opcodes
, i
);
819 snprintf(nm
, sizeof(nm
), "%s", lt
->name
);
821 snprintf(nm
, sizeof(nm
), "OPCODE%d", i
);
823 if(!ssl_printf(ssl
, "num.query.opcode.%s"SQ
"%lu\n",
824 nm
, (unsigned long)s
->svr
.qopcode
[i
])) return 0;
827 if(!ssl_printf(ssl
, "num.query.tcp"SQ
"%lu\n",
828 (unsigned long)s
->svr
.qtcp
)) return 0;
829 if(!ssl_printf(ssl
, "num.query.tcpout"SQ
"%lu\n",
830 (unsigned long)s
->svr
.qtcp_outgoing
)) return 0;
831 if(!ssl_printf(ssl
, "num.query.ipv6"SQ
"%lu\n",
832 (unsigned long)s
->svr
.qipv6
)) return 0;
834 if(!ssl_printf(ssl
, "num.query.flags.QR"SQ
"%lu\n",
835 (unsigned long)s
->svr
.qbit_QR
)) return 0;
836 if(!ssl_printf(ssl
, "num.query.flags.AA"SQ
"%lu\n",
837 (unsigned long)s
->svr
.qbit_AA
)) return 0;
838 if(!ssl_printf(ssl
, "num.query.flags.TC"SQ
"%lu\n",
839 (unsigned long)s
->svr
.qbit_TC
)) return 0;
840 if(!ssl_printf(ssl
, "num.query.flags.RD"SQ
"%lu\n",
841 (unsigned long)s
->svr
.qbit_RD
)) return 0;
842 if(!ssl_printf(ssl
, "num.query.flags.RA"SQ
"%lu\n",
843 (unsigned long)s
->svr
.qbit_RA
)) return 0;
844 if(!ssl_printf(ssl
, "num.query.flags.Z"SQ
"%lu\n",
845 (unsigned long)s
->svr
.qbit_Z
)) return 0;
846 if(!ssl_printf(ssl
, "num.query.flags.AD"SQ
"%lu\n",
847 (unsigned long)s
->svr
.qbit_AD
)) return 0;
848 if(!ssl_printf(ssl
, "num.query.flags.CD"SQ
"%lu\n",
849 (unsigned long)s
->svr
.qbit_CD
)) return 0;
850 if(!ssl_printf(ssl
, "num.query.edns.present"SQ
"%lu\n",
851 (unsigned long)s
->svr
.qEDNS
)) return 0;
852 if(!ssl_printf(ssl
, "num.query.edns.DO"SQ
"%lu\n",
853 (unsigned long)s
->svr
.qEDNS_DO
)) return 0;
856 for(i
=0; i
<STATS_RCODE_NUM
; i
++) {
857 /* Always include RCODEs 0-5 */
858 if(inhibit_zero
&& i
> LDNS_RCODE_REFUSED
&& s
->svr
.ans_rcode
[i
] == 0)
860 lt
= sldns_lookup_by_id(sldns_rcodes
, i
);
862 snprintf(nm
, sizeof(nm
), "%s", lt
->name
);
864 snprintf(nm
, sizeof(nm
), "RCODE%d", i
);
866 if(!ssl_printf(ssl
, "num.answer.rcode.%s"SQ
"%lu\n",
867 nm
, (unsigned long)s
->svr
.ans_rcode
[i
])) return 0;
869 if(!inhibit_zero
|| s
->svr
.ans_rcode_nodata
) {
870 if(!ssl_printf(ssl
, "num.answer.rcode.nodata"SQ
"%lu\n",
871 (unsigned long)s
->svr
.ans_rcode_nodata
)) return 0;
874 if(!ssl_printf(ssl
, "num.answer.secure"SQ
"%lu\n",
875 (unsigned long)s
->svr
.ans_secure
)) return 0;
876 if(!ssl_printf(ssl
, "num.answer.bogus"SQ
"%lu\n",
877 (unsigned long)s
->svr
.ans_bogus
)) return 0;
878 if(!ssl_printf(ssl
, "num.rrset.bogus"SQ
"%lu\n",
879 (unsigned long)s
->svr
.rrset_bogus
)) return 0;
880 /* threat detection */
881 if(!ssl_printf(ssl
, "unwanted.queries"SQ
"%lu\n",
882 (unsigned long)s
->svr
.unwanted_queries
)) return 0;
883 if(!ssl_printf(ssl
, "unwanted.replies"SQ
"%lu\n",
884 (unsigned long)s
->svr
.unwanted_replies
)) return 0;
886 if(!ssl_printf(ssl
, "msg.cache.count"SQ
"%u\n",
887 (unsigned)s
->svr
.msg_cache_count
)) return 0;
888 if(!ssl_printf(ssl
, "rrset.cache.count"SQ
"%u\n",
889 (unsigned)s
->svr
.rrset_cache_count
)) return 0;
890 if(!ssl_printf(ssl
, "infra.cache.count"SQ
"%u\n",
891 (unsigned)s
->svr
.infra_cache_count
)) return 0;
892 if(!ssl_printf(ssl
, "key.cache.count"SQ
"%u\n",
893 (unsigned)s
->svr
.key_cache_count
)) return 0;
897 /** do the stats command */
899 do_stats(SSL
* ssl
, struct daemon_remote
* rc
, int reset
)
901 struct daemon
* daemon
= rc
->worker
->daemon
;
902 struct stats_info total
;
905 log_assert(daemon
->num
> 0);
906 /* gather all thread statistics in one place */
907 for(i
=0; i
<daemon
->num
; i
++) {
908 server_stats_obtain(rc
->worker
, daemon
->workers
[i
], &s
, reset
);
909 if(!print_thread_stats(ssl
, i
, &s
))
913 else server_stats_add(&total
, &s
);
915 /* print the thread statistics */
916 total
.mesh_time_median
/= (double)daemon
->num
;
917 if(!print_stats(ssl
, "total", &total
))
919 if(!print_uptime(ssl
, rc
->worker
, reset
))
921 if(daemon
->cfg
->stat_extended
) {
922 if(!print_mem(ssl
, rc
->worker
, daemon
))
924 if(!print_hist(ssl
, &total
))
926 if(!print_ext(ssl
, &total
))
931 /** parse commandline argument domain name */
933 parse_arg_name(SSL
* ssl
, char* str
, uint8_t** res
, size_t* len
, int* labs
)
935 uint8_t nm
[LDNS_MAX_DOMAINLEN
+1];
936 size_t nmlen
= sizeof(nm
);
941 status
= sldns_str2wire_dname_buf(str
, nm
, &nmlen
);
943 ssl_printf(ssl
, "error cannot parse name %s at %d: %s\n", str
,
944 LDNS_WIREPARSE_OFFSET(status
),
945 sldns_get_errorstr_parse(status
));
948 *res
= memdup(nm
, nmlen
);
950 ssl_printf(ssl
, "error out of memory\n");
953 *labs
= dname_count_size_labels(*res
, len
);
957 /** find second argument, modifies string */
959 find_arg2(SSL
* ssl
, char* arg
, char** arg2
)
961 char* as
= strchr(arg
, ' ');
962 char* at
= strchr(arg
, '\t');
967 *arg2
= skipwhite(as
+1);
970 *arg2
= skipwhite(as
+1);
973 *arg2
= skipwhite(at
+1);
975 ssl_printf(ssl
, "error could not find next argument "
982 /** Add a new zone */
984 do_zone_add(SSL
* ssl
, struct worker
* worker
, char* arg
)
990 enum localzone_type t
;
991 struct local_zone
* z
;
992 if(!find_arg2(ssl
, arg
, &arg2
))
994 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
996 if(!local_zone_str2type(arg2
, &t
)) {
997 ssl_printf(ssl
, "error not a zone type. %s\n", arg2
);
1001 lock_rw_wrlock(&worker
->daemon
->local_zones
->lock
);
1002 if((z
=local_zones_find(worker
->daemon
->local_zones
, nm
, nmlen
,
1003 nmlabs
, LDNS_RR_CLASS_IN
))) {
1004 /* already present in tree */
1005 lock_rw_wrlock(&z
->lock
);
1006 z
->type
= t
; /* update type anyway */
1007 lock_rw_unlock(&z
->lock
);
1009 lock_rw_unlock(&worker
->daemon
->local_zones
->lock
);
1013 if(!local_zones_add_zone(worker
->daemon
->local_zones
, nm
, nmlen
,
1014 nmlabs
, LDNS_RR_CLASS_IN
, t
)) {
1015 lock_rw_unlock(&worker
->daemon
->local_zones
->lock
);
1016 ssl_printf(ssl
, "error out of memory\n");
1019 lock_rw_unlock(&worker
->daemon
->local_zones
->lock
);
1023 /** Remove a zone */
1025 do_zone_remove(SSL
* ssl
, struct worker
* worker
, char* arg
)
1030 struct local_zone
* z
;
1031 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1033 lock_rw_wrlock(&worker
->daemon
->local_zones
->lock
);
1034 if((z
=local_zones_find(worker
->daemon
->local_zones
, nm
, nmlen
,
1035 nmlabs
, LDNS_RR_CLASS_IN
))) {
1036 /* present in tree */
1037 local_zones_del_zone(worker
->daemon
->local_zones
, z
);
1039 lock_rw_unlock(&worker
->daemon
->local_zones
->lock
);
1044 /** Add new RR data */
1046 do_data_add(SSL
* ssl
, struct worker
* worker
, char* arg
)
1048 if(!local_zones_add_RR(worker
->daemon
->local_zones
, arg
)) {
1049 ssl_printf(ssl
,"error in syntax or out of memory, %s\n", arg
);
1055 /** Remove RR data */
1057 do_data_remove(SSL
* ssl
, struct worker
* worker
, char* arg
)
1062 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1064 local_zones_del_data(worker
->daemon
->local_zones
, nm
,
1065 nmlen
, nmlabs
, LDNS_RR_CLASS_IN
);
1070 /** cache lookup of nameservers */
1072 do_lookup(SSL
* ssl
, struct worker
* worker
, char* arg
)
1077 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1079 (void)print_deleg_lookup(ssl
, worker
, nm
, nmlen
, nmlabs
);
1083 /** flush something from rrset and msg caches */
1085 do_cache_remove(struct worker
* worker
, uint8_t* nm
, size_t nmlen
,
1086 uint16_t t
, uint16_t c
)
1089 struct query_info k
;
1090 rrset_cache_remove(worker
->env
.rrset_cache
, nm
, nmlen
, t
, c
, 0);
1091 if(t
== LDNS_RR_TYPE_SOA
)
1092 rrset_cache_remove(worker
->env
.rrset_cache
, nm
, nmlen
, t
, c
,
1093 PACKED_RRSET_SOA_NEG
);
1095 k
.qname_len
= nmlen
;
1098 h
= query_info_hash(&k
, 0);
1099 slabhash_remove(worker
->env
.msg_cache
, h
, &k
);
1100 if(t
== LDNS_RR_TYPE_AAAA
) {
1101 /* for AAAA also flush dns64 bit_cd packet */
1102 h
= query_info_hash(&k
, BIT_CD
);
1103 slabhash_remove(worker
->env
.msg_cache
, h
, &k
);
1109 do_flush_type(SSL
* ssl
, struct worker
* worker
, char* arg
)
1116 if(!find_arg2(ssl
, arg
, &arg2
))
1118 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1120 t
= sldns_get_rr_type_by_name(arg2
);
1121 do_cache_remove(worker
, nm
, nmlen
, t
, LDNS_RR_CLASS_IN
);
1127 /** flush statistics */
1129 do_flush_stats(SSL
* ssl
, struct worker
* worker
)
1131 worker_stats_clear(worker
);
1136 * Local info for deletion functions
1140 struct worker
* worker
;
1141 /** name to delete */
1149 /** time to invalidate to */
1151 /** number of rrsets removed */
1153 /** number of msgs removed */
1155 /** number of key entries removed */
1157 /** length of addr */
1159 /** socket address for host deletion */
1160 struct sockaddr_storage addr
;
1163 /** callback to delete hosts in infra cache */
1165 infra_del_host(struct lruhash_entry
* e
, void* arg
)
1167 /* entry is locked */
1168 struct del_info
* inf
= (struct del_info
*)arg
;
1169 struct infra_key
* k
= (struct infra_key
*)e
->key
;
1170 if(sockaddr_cmp(&inf
->addr
, inf
->addrlen
, &k
->addr
, k
->addrlen
) == 0) {
1171 struct infra_data
* d
= (struct infra_data
*)e
->data
;
1174 d
->timeout_AAAA
= 0;
1175 d
->timeout_other
= 0;
1177 if(d
->ttl
>= inf
->now
) {
1178 d
->ttl
= inf
->expired
;
1184 /** flush infra cache */
1186 do_flush_infra(SSL
* ssl
, struct worker
* worker
, char* arg
)
1188 struct sockaddr_storage addr
;
1190 struct del_info inf
;
1191 if(strcmp(arg
, "all") == 0) {
1192 slabhash_clear(worker
->env
.infra_cache
->hosts
);
1196 if(!ipstrtoaddr(arg
, UNBOUND_DNS_PORT
, &addr
, &len
)) {
1197 (void)ssl_printf(ssl
, "error parsing ip addr: '%s'\n", arg
);
1200 /* delete all entries from cache */
1201 /* what we do is to set them all expired */
1202 inf
.worker
= worker
;
1206 inf
.now
= *worker
->env
.now
;
1207 inf
.expired
= *worker
->env
.now
;
1208 inf
.expired
-= 3; /* handle 3 seconds skew between threads */
1213 memmove(&inf
.addr
, &addr
, len
);
1214 slabhash_traverse(worker
->env
.infra_cache
->hosts
, 1, &infra_del_host
,
1219 /** flush requestlist */
1221 do_flush_requestlist(SSL
* ssl
, struct worker
* worker
)
1223 mesh_delete_all(worker
->env
.mesh
);
1227 /** callback to delete rrsets in a zone */
1229 zone_del_rrset(struct lruhash_entry
* e
, void* arg
)
1231 /* entry is locked */
1232 struct del_info
* inf
= (struct del_info
*)arg
;
1233 struct ub_packed_rrset_key
* k
= (struct ub_packed_rrset_key
*)e
->key
;
1234 if(dname_subdomain_c(k
->rk
.dname
, inf
->name
)) {
1235 struct packed_rrset_data
* d
=
1236 (struct packed_rrset_data
*)e
->data
;
1237 if(d
->ttl
>= inf
->now
) {
1238 d
->ttl
= inf
->expired
;
1244 /** callback to delete messages in a zone */
1246 zone_del_msg(struct lruhash_entry
* e
, void* arg
)
1248 /* entry is locked */
1249 struct del_info
* inf
= (struct del_info
*)arg
;
1250 struct msgreply_entry
* k
= (struct msgreply_entry
*)e
->key
;
1251 if(dname_subdomain_c(k
->key
.qname
, inf
->name
)) {
1252 struct reply_info
* d
= (struct reply_info
*)e
->data
;
1253 if(d
->ttl
>= inf
->now
) {
1254 d
->ttl
= inf
->expired
;
1260 /** callback to delete keys in zone */
1262 zone_del_kcache(struct lruhash_entry
* e
, void* arg
)
1264 /* entry is locked */
1265 struct del_info
* inf
= (struct del_info
*)arg
;
1266 struct key_entry_key
* k
= (struct key_entry_key
*)e
->key
;
1267 if(dname_subdomain_c(k
->name
, inf
->name
)) {
1268 struct key_entry_data
* d
= (struct key_entry_data
*)e
->data
;
1269 if(d
->ttl
>= inf
->now
) {
1270 d
->ttl
= inf
->expired
;
1276 /** remove all rrsets and keys from zone from cache */
1278 do_flush_zone(SSL
* ssl
, struct worker
* worker
, char* arg
)
1283 struct del_info inf
;
1284 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1286 /* delete all RRs and key entries from zone */
1287 /* what we do is to set them all expired */
1288 inf
.worker
= worker
;
1292 inf
.now
= *worker
->env
.now
;
1293 inf
.expired
= *worker
->env
.now
;
1294 inf
.expired
-= 3; /* handle 3 seconds skew between threads */
1298 slabhash_traverse(&worker
->env
.rrset_cache
->table
, 1,
1299 &zone_del_rrset
, &inf
);
1301 slabhash_traverse(worker
->env
.msg_cache
, 1, &zone_del_msg
, &inf
);
1303 /* and validator cache */
1304 if(worker
->env
.key_cache
) {
1305 slabhash_traverse(worker
->env
.key_cache
->slab
, 1,
1306 &zone_del_kcache
, &inf
);
1311 (void)ssl_printf(ssl
, "ok removed %lu rrsets, %lu messages "
1312 "and %lu key entries\n", (unsigned long)inf
.num_rrsets
,
1313 (unsigned long)inf
.num_msgs
, (unsigned long)inf
.num_keys
);
1316 /** callback to delete bogus rrsets */
1318 bogus_del_rrset(struct lruhash_entry
* e
, void* arg
)
1320 /* entry is locked */
1321 struct del_info
* inf
= (struct del_info
*)arg
;
1322 struct packed_rrset_data
* d
= (struct packed_rrset_data
*)e
->data
;
1323 if(d
->security
== sec_status_bogus
) {
1324 d
->ttl
= inf
->expired
;
1329 /** callback to delete bogus messages */
1331 bogus_del_msg(struct lruhash_entry
* e
, void* arg
)
1333 /* entry is locked */
1334 struct del_info
* inf
= (struct del_info
*)arg
;
1335 struct reply_info
* d
= (struct reply_info
*)e
->data
;
1336 if(d
->security
== sec_status_bogus
) {
1337 d
->ttl
= inf
->expired
;
1342 /** callback to delete bogus keys */
1344 bogus_del_kcache(struct lruhash_entry
* e
, void* arg
)
1346 /* entry is locked */
1347 struct del_info
* inf
= (struct del_info
*)arg
;
1348 struct key_entry_data
* d
= (struct key_entry_data
*)e
->data
;
1350 d
->ttl
= inf
->expired
;
1355 /** remove all bogus rrsets, msgs and keys from cache */
1357 do_flush_bogus(SSL
* ssl
, struct worker
* worker
)
1359 struct del_info inf
;
1360 /* what we do is to set them all expired */
1361 inf
.worker
= worker
;
1362 inf
.now
= *worker
->env
.now
;
1363 inf
.expired
= *worker
->env
.now
;
1364 inf
.expired
-= 3; /* handle 3 seconds skew between threads */
1368 slabhash_traverse(&worker
->env
.rrset_cache
->table
, 1,
1369 &bogus_del_rrset
, &inf
);
1371 slabhash_traverse(worker
->env
.msg_cache
, 1, &bogus_del_msg
, &inf
);
1373 /* and validator cache */
1374 if(worker
->env
.key_cache
) {
1375 slabhash_traverse(worker
->env
.key_cache
->slab
, 1,
1376 &bogus_del_kcache
, &inf
);
1379 (void)ssl_printf(ssl
, "ok removed %lu rrsets, %lu messages "
1380 "and %lu key entries\n", (unsigned long)inf
.num_rrsets
,
1381 (unsigned long)inf
.num_msgs
, (unsigned long)inf
.num_keys
);
1384 /** callback to delete negative and servfail rrsets */
1386 negative_del_rrset(struct lruhash_entry
* e
, void* arg
)
1388 /* entry is locked */
1389 struct del_info
* inf
= (struct del_info
*)arg
;
1390 struct ub_packed_rrset_key
* k
= (struct ub_packed_rrset_key
*)e
->key
;
1391 struct packed_rrset_data
* d
= (struct packed_rrset_data
*)e
->data
;
1392 /* delete the parentside negative cache rrsets,
1393 * these are namerserver rrsets that failed lookup, rdata empty */
1394 if((k
->rk
.flags
& PACKED_RRSET_PARENT_SIDE
) && d
->count
== 1 &&
1395 d
->rrsig_count
== 0 && d
->rr_len
[0] == 0) {
1396 d
->ttl
= inf
->expired
;
1401 /** callback to delete negative and servfail messages */
1403 negative_del_msg(struct lruhash_entry
* e
, void* arg
)
1405 /* entry is locked */
1406 struct del_info
* inf
= (struct del_info
*)arg
;
1407 struct reply_info
* d
= (struct reply_info
*)e
->data
;
1408 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1409 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1410 if(FLAGS_GET_RCODE(d
->flags
) != 0 || d
->an_numrrsets
== 0) {
1411 d
->ttl
= inf
->expired
;
1416 /** callback to delete negative key entries */
1418 negative_del_kcache(struct lruhash_entry
* e
, void* arg
)
1420 /* entry is locked */
1421 struct del_info
* inf
= (struct del_info
*)arg
;
1422 struct key_entry_data
* d
= (struct key_entry_data
*)e
->data
;
1423 /* could be bad because of lookup failure on the DS, DNSKEY, which
1424 * was nxdomain or servfail, and thus a result of negative lookups */
1426 d
->ttl
= inf
->expired
;
1431 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1433 do_flush_negative(SSL
* ssl
, struct worker
* worker
)
1435 struct del_info inf
;
1436 /* what we do is to set them all expired */
1437 inf
.worker
= worker
;
1438 inf
.now
= *worker
->env
.now
;
1439 inf
.expired
= *worker
->env
.now
;
1440 inf
.expired
-= 3; /* handle 3 seconds skew between threads */
1444 slabhash_traverse(&worker
->env
.rrset_cache
->table
, 1,
1445 &negative_del_rrset
, &inf
);
1447 slabhash_traverse(worker
->env
.msg_cache
, 1, &negative_del_msg
, &inf
);
1449 /* and validator cache */
1450 if(worker
->env
.key_cache
) {
1451 slabhash_traverse(worker
->env
.key_cache
->slab
, 1,
1452 &negative_del_kcache
, &inf
);
1455 (void)ssl_printf(ssl
, "ok removed %lu rrsets, %lu messages "
1456 "and %lu key entries\n", (unsigned long)inf
.num_rrsets
,
1457 (unsigned long)inf
.num_msgs
, (unsigned long)inf
.num_keys
);
1460 /** remove name rrset from cache */
1462 do_flush_name(SSL
* ssl
, struct worker
* w
, char* arg
)
1467 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1469 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_A
, LDNS_RR_CLASS_IN
);
1470 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_AAAA
, LDNS_RR_CLASS_IN
);
1471 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_NS
, LDNS_RR_CLASS_IN
);
1472 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_SOA
, LDNS_RR_CLASS_IN
);
1473 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_CNAME
, LDNS_RR_CLASS_IN
);
1474 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_DNAME
, LDNS_RR_CLASS_IN
);
1475 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_MX
, LDNS_RR_CLASS_IN
);
1476 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_PTR
, LDNS_RR_CLASS_IN
);
1477 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_SRV
, LDNS_RR_CLASS_IN
);
1478 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_NAPTR
, LDNS_RR_CLASS_IN
);
1484 /** printout a delegation point info */
1486 ssl_print_name_dp(SSL
* ssl
, const char* str
, uint8_t* nm
, uint16_t dclass
,
1490 struct delegpt_ns
* ns
;
1491 struct delegpt_addr
* a
;
1493 if(str
) { /* print header for forward, stub */
1494 char* c
= sldns_wire2str_class(dclass
);
1496 if(!ssl_printf(ssl
, "%s %s %s ", buf
, (c
?c
:"CLASS??"), str
)) {
1502 for(ns
= dp
->nslist
; ns
; ns
= ns
->next
) {
1503 dname_str(ns
->name
, buf
);
1504 if(!ssl_printf(ssl
, "%s%s", (f
?" ":""), buf
))
1508 for(a
= dp
->target_list
; a
; a
= a
->next_target
) {
1509 addr_to_str(&a
->addr
, a
->addrlen
, buf
, sizeof(buf
));
1510 if(!ssl_printf(ssl
, "%s%s", (f
?" ":""), buf
))
1514 return ssl_printf(ssl
, "\n");
1518 /** print root forwards */
1520 print_root_fwds(SSL
* ssl
, struct iter_forwards
* fwds
, uint8_t* root
)
1523 dp
= forwards_lookup(fwds
, root
, LDNS_RR_CLASS_IN
);
1525 return ssl_printf(ssl
, "off (using root hints)\n");
1526 /* if dp is returned it must be the root */
1527 log_assert(query_dname_compare(dp
->name
, root
)==0);
1528 return ssl_print_name_dp(ssl
, NULL
, root
, LDNS_RR_CLASS_IN
, dp
);
1531 /** parse args into delegpt */
1532 static struct delegpt
*
1533 parse_delegpt(SSL
* ssl
, char* args
, uint8_t* nm
, int allow_names
)
1535 /* parse args and add in */
1538 struct delegpt
* dp
= delegpt_create_mlc(nm
);
1539 struct sockaddr_storage addr
;
1542 (void)ssl_printf(ssl
, "error out of memory\n");
1547 p
= strchr(p
, ' '); /* find next spot, if any */
1549 *p
++ = 0; /* end this spot */
1550 p
= skipwhite(p
); /* position at next spot */
1553 if(!extstrtoaddr(todo
, &addr
, &addrlen
)) {
1558 if(!parse_arg_name(ssl
, todo
, &n
, &ln
, &lb
)) {
1559 (void)ssl_printf(ssl
, "error cannot "
1560 "parse IP address or name "
1562 delegpt_free_mlc(dp
);
1565 if(!delegpt_add_ns_mlc(dp
, n
, 0)) {
1566 (void)ssl_printf(ssl
, "error out of memory\n");
1568 delegpt_free_mlc(dp
);
1574 (void)ssl_printf(ssl
, "error cannot parse"
1575 " IP address '%s'\n", todo
);
1576 delegpt_free_mlc(dp
);
1581 if(!delegpt_add_addr_mlc(dp
, &addr
, addrlen
, 0, 0)) {
1582 (void)ssl_printf(ssl
, "error out of memory\n");
1583 delegpt_free_mlc(dp
);
1591 /** do the status command */
1593 do_forward(SSL
* ssl
, struct worker
* worker
, char* args
)
1595 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1596 uint8_t* root
= (uint8_t*)"\000";
1598 (void)ssl_printf(ssl
, "error: structure not allocated\n");
1601 if(args
== NULL
|| args
[0] == 0) {
1602 (void)print_root_fwds(ssl
, fwd
, root
);
1605 /* set root forwards for this thread. since we are in remote control
1606 * the actual mesh is not running, so we can freely edit it. */
1607 /* delete all the existing queries first */
1608 mesh_delete_all(worker
->env
.mesh
);
1609 if(strcmp(args
, "off") == 0) {
1610 forwards_delete_zone(fwd
, LDNS_RR_CLASS_IN
, root
);
1613 if(!(dp
= parse_delegpt(ssl
, args
, root
, 0)))
1615 if(!forwards_add_zone(fwd
, LDNS_RR_CLASS_IN
, dp
)) {
1616 (void)ssl_printf(ssl
, "error out of memory\n");
1624 parse_fs_args(SSL
* ssl
, char* args
, uint8_t** nm
, struct delegpt
** dp
,
1625 int* insecure
, int* prime
)
1631 /* parse all -x args */
1632 while(args
[0] == '+') {
1633 if(!find_arg2(ssl
, args
, &rest
))
1635 while(*(++args
) != 0) {
1636 if(*args
== 'i' && insecure
)
1638 else if(*args
== 'p' && prime
)
1641 (void)ssl_printf(ssl
, "error: unknown option %s\n", args
);
1649 if(!find_arg2(ssl
, args
, &rest
))
1653 } else zonename
= args
;
1654 if(!parse_arg_name(ssl
, zonename
, nm
, &nmlen
, &nmlabs
))
1659 if(!(*dp
= parse_delegpt(ssl
, args
, *nm
, 1))) {
1667 /** do the forward_add command */
1669 do_forward_add(SSL
* ssl
, struct worker
* worker
, char* args
)
1671 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1674 struct delegpt
* dp
= NULL
;
1675 if(!parse_fs_args(ssl
, args
, &nm
, &dp
, &insecure
, NULL
))
1677 if(insecure
&& worker
->env
.anchors
) {
1678 if(!anchors_add_insecure(worker
->env
.anchors
, LDNS_RR_CLASS_IN
,
1680 (void)ssl_printf(ssl
, "error out of memory\n");
1681 delegpt_free_mlc(dp
);
1686 if(!forwards_add_zone(fwd
, LDNS_RR_CLASS_IN
, dp
)) {
1687 (void)ssl_printf(ssl
, "error out of memory\n");
1695 /** do the forward_remove command */
1697 do_forward_remove(SSL
* ssl
, struct worker
* worker
, char* args
)
1699 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1702 if(!parse_fs_args(ssl
, args
, &nm
, NULL
, &insecure
, NULL
))
1704 if(insecure
&& worker
->env
.anchors
)
1705 anchors_delete_insecure(worker
->env
.anchors
, LDNS_RR_CLASS_IN
,
1707 forwards_delete_zone(fwd
, LDNS_RR_CLASS_IN
, nm
);
1712 /** do the stub_add command */
1714 do_stub_add(SSL
* ssl
, struct worker
* worker
, char* args
)
1716 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1717 int insecure
= 0, prime
= 0;
1719 struct delegpt
* dp
= NULL
;
1720 if(!parse_fs_args(ssl
, args
, &nm
, &dp
, &insecure
, &prime
))
1722 if(insecure
&& worker
->env
.anchors
) {
1723 if(!anchors_add_insecure(worker
->env
.anchors
, LDNS_RR_CLASS_IN
,
1725 (void)ssl_printf(ssl
, "error out of memory\n");
1726 delegpt_free_mlc(dp
);
1731 if(!forwards_add_stub_hole(fwd
, LDNS_RR_CLASS_IN
, nm
)) {
1732 if(insecure
&& worker
->env
.anchors
)
1733 anchors_delete_insecure(worker
->env
.anchors
,
1734 LDNS_RR_CLASS_IN
, nm
);
1735 (void)ssl_printf(ssl
, "error out of memory\n");
1736 delegpt_free_mlc(dp
);
1740 if(!hints_add_stub(worker
->env
.hints
, LDNS_RR_CLASS_IN
, dp
, !prime
)) {
1741 (void)ssl_printf(ssl
, "error out of memory\n");
1742 forwards_delete_stub_hole(fwd
, LDNS_RR_CLASS_IN
, nm
);
1743 if(insecure
&& worker
->env
.anchors
)
1744 anchors_delete_insecure(worker
->env
.anchors
,
1745 LDNS_RR_CLASS_IN
, nm
);
1753 /** do the stub_remove command */
1755 do_stub_remove(SSL
* ssl
, struct worker
* worker
, char* args
)
1757 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1760 if(!parse_fs_args(ssl
, args
, &nm
, NULL
, &insecure
, NULL
))
1762 if(insecure
&& worker
->env
.anchors
)
1763 anchors_delete_insecure(worker
->env
.anchors
, LDNS_RR_CLASS_IN
,
1765 forwards_delete_stub_hole(fwd
, LDNS_RR_CLASS_IN
, nm
);
1766 hints_delete_stub(worker
->env
.hints
, LDNS_RR_CLASS_IN
, nm
);
1771 /** do the insecure_add command */
1773 do_insecure_add(SSL
* ssl
, struct worker
* worker
, char* arg
)
1778 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1780 if(worker
->env
.anchors
) {
1781 if(!anchors_add_insecure(worker
->env
.anchors
,
1782 LDNS_RR_CLASS_IN
, nm
)) {
1783 (void)ssl_printf(ssl
, "error out of memory\n");
1792 /** do the insecure_remove command */
1794 do_insecure_remove(SSL
* ssl
, struct worker
* worker
, char* arg
)
1799 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1801 if(worker
->env
.anchors
)
1802 anchors_delete_insecure(worker
->env
.anchors
,
1803 LDNS_RR_CLASS_IN
, nm
);
1808 /** do the status command */
1810 do_status(SSL
* ssl
, struct worker
* worker
)
1814 if(!ssl_printf(ssl
, "version: %s\n", PACKAGE_VERSION
))
1816 if(!ssl_printf(ssl
, "verbosity: %d\n", verbosity
))
1818 if(!ssl_printf(ssl
, "threads: %d\n", worker
->daemon
->num
))
1820 if(!ssl_printf(ssl
, "modules: %d [", worker
->daemon
->mods
.num
))
1822 for(i
=0; i
<worker
->daemon
->mods
.num
; i
++) {
1823 if(!ssl_printf(ssl
, " %s", worker
->daemon
->mods
.mod
[i
]->name
))
1826 if(!ssl_printf(ssl
, " ]\n"))
1828 uptime
= (time_t)time(NULL
) - (time_t)worker
->daemon
->time_boot
.tv_sec
;
1829 if(!ssl_printf(ssl
, "uptime: " ARG_LL
"d seconds\n", (long long)uptime
))
1831 if(!ssl_printf(ssl
, "options:%s%s\n" ,
1832 (worker
->daemon
->reuseport
?" reuseport":""),
1833 (worker
->daemon
->rc
->accept_list
?" control(ssl)":"")))
1835 if(!ssl_printf(ssl
, "unbound (pid %d) is running...\n",
1840 /** get age for the mesh state */
1842 get_mesh_age(struct mesh_state
* m
, char* buf
, size_t len
,
1843 struct module_env
* env
)
1847 struct mesh_reply
* r
= m
->reply_list
;
1848 /* last reply is the oldest */
1851 timeval_subtract(&d
, env
->now_tv
, &r
->start_time
);
1852 snprintf(buf
, len
, ARG_LL
"d.%6.6d",
1853 (long long)d
.tv_sec
, (int)d
.tv_usec
);
1855 snprintf(buf
, len
, "-");
1859 /** get status of a mesh state */
1861 get_mesh_status(struct mesh_area
* mesh
, struct mesh_state
* m
,
1862 char* buf
, size_t len
)
1864 enum module_ext_state s
= m
->s
.ext_state
[m
->s
.curmod
];
1865 const char *modname
= mesh
->mods
.mod
[m
->s
.curmod
]->name
;
1867 if(strcmp(modname
, "iterator") == 0 && s
== module_wait_reply
&&
1868 m
->s
.minfo
[m
->s
.curmod
]) {
1869 /* break into iterator to find out who its waiting for */
1870 struct iter_qstate
* qstate
= (struct iter_qstate
*)
1871 m
->s
.minfo
[m
->s
.curmod
];
1872 struct outbound_list
* ol
= &qstate
->outlist
;
1873 struct outbound_entry
* e
;
1874 snprintf(buf
, len
, "%s wait for", modname
);
1877 if(ol
->first
== NULL
)
1878 snprintf(buf
, len
, " (empty_list)");
1879 for(e
= ol
->first
; e
; e
= e
->next
) {
1880 snprintf(buf
, len
, " ");
1883 addr_to_str(&e
->qsent
->addr
, e
->qsent
->addrlen
,
1888 } else if(s
== module_wait_subquery
) {
1889 /* look in subs from mesh state to see what */
1891 struct mesh_state_ref
* sub
;
1892 snprintf(buf
, len
, "%s wants", modname
);
1895 if(m
->sub_set
.count
== 0)
1896 snprintf(buf
, len
, " (empty_list)");
1897 RBTREE_FOR(sub
, struct mesh_state_ref
*, &m
->sub_set
) {
1898 char* t
= sldns_wire2str_type(sub
->s
->s
.qinfo
.qtype
);
1899 char* c
= sldns_wire2str_class(sub
->s
->s
.qinfo
.qclass
);
1900 dname_str(sub
->s
->s
.qinfo
.qname
, nm
);
1901 snprintf(buf
, len
, " %s %s %s", (t
?t
:"TYPE??"),
1902 (c
?c
:"CLASS??"), nm
);
1909 snprintf(buf
, len
, "%s is %s", modname
, strextstate(s
));
1913 /** do the dump_requestlist command */
1915 do_dump_requestlist(SSL
* ssl
, struct worker
* worker
)
1917 struct mesh_area
* mesh
;
1918 struct mesh_state
* m
;
1922 char statbuf
[10240];
1923 if(!ssl_printf(ssl
, "thread #%d\n", worker
->thread_num
))
1925 if(!ssl_printf(ssl
, "# type cl name seconds module status\n"))
1927 /* show worker mesh contents */
1928 mesh
= worker
->env
.mesh
;
1930 RBTREE_FOR(m
, struct mesh_state
*, &mesh
->all
) {
1931 char* t
= sldns_wire2str_type(m
->s
.qinfo
.qtype
);
1932 char* c
= sldns_wire2str_class(m
->s
.qinfo
.qclass
);
1933 dname_str(m
->s
.qinfo
.qname
, buf
);
1934 get_mesh_age(m
, timebuf
, sizeof(timebuf
), &worker
->env
);
1935 get_mesh_status(mesh
, m
, statbuf
, sizeof(statbuf
));
1936 if(!ssl_printf(ssl
, "%3d %4s %2s %s %s %s\n",
1937 num
, (t
?t
:"TYPE??"), (c
?c
:"CLASS??"), buf
, timebuf
,
1949 /** structure for argument data for dump infra host */
1951 /** the infra cache */
1952 struct infra_cache
* infra
;
1953 /** the SSL connection */
1957 /** ssl failure? stop writing and skip the rest. If the tcp
1958 * connection is broken, and writes fail, we then stop writing. */
1962 /** callback for every host element in the infra cache */
1964 dump_infra_host(struct lruhash_entry
* e
, void* arg
)
1966 struct infra_arg
* a
= (struct infra_arg
*)arg
;
1967 struct infra_key
* k
= (struct infra_key
*)e
->key
;
1968 struct infra_data
* d
= (struct infra_data
*)e
->data
;
1973 addr_to_str(&k
->addr
, k
->addrlen
, ip_str
, sizeof(ip_str
));
1974 dname_str(k
->zonename
, name
);
1975 /* skip expired stuff (only backed off) */
1976 if(d
->ttl
< a
->now
) {
1977 if(d
->rtt
.rto
>= USEFUL_SERVER_TOP_TIMEOUT
) {
1978 if(!ssl_printf(a
->ssl
, "%s %s expired rto %d\n", ip_str
,
1979 name
, d
->rtt
.rto
)) {
1986 if(!ssl_printf(a
->ssl
, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
1987 "tA %d tAAAA %d tother %d "
1988 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
1989 "other %d\n", ip_str
, name
, (unsigned long)(d
->ttl
- a
->now
),
1990 d
->rtt
.srtt
, d
->rtt
.rttvar
, rtt_notimeout(&d
->rtt
), d
->rtt
.rto
,
1991 d
->timeout_A
, d
->timeout_AAAA
, d
->timeout_other
,
1992 (int)d
->edns_lame_known
, (int)d
->edns_version
,
1993 (int)(a
->now
<d
->probedelay
?d
->probedelay
-a
->now
:0),
1994 (int)d
->isdnsseclame
, (int)d
->rec_lame
, (int)d
->lame_type_A
,
1995 (int)d
->lame_other
)) {
2001 /** do the dump_infra command */
2003 do_dump_infra(SSL
* ssl
, struct worker
* worker
)
2005 struct infra_arg arg
;
2006 arg
.infra
= worker
->env
.infra_cache
;
2008 arg
.now
= *worker
->env
.now
;
2010 slabhash_traverse(arg
.infra
->hosts
, 0, &dump_infra_host
, (void*)&arg
);
2013 /** do the log_reopen command */
2015 do_log_reopen(SSL
* ssl
, struct worker
* worker
)
2017 struct config_file
* cfg
= worker
->env
.cfg
;
2019 log_init(cfg
->logfile
, cfg
->use_syslog
, cfg
->chrootdir
);
2022 /** do the set_option command */
2024 do_set_option(SSL
* ssl
, struct worker
* worker
, char* arg
)
2027 if(!find_arg2(ssl
, arg
, &arg2
))
2029 if(!config_set_option(worker
->env
.cfg
, arg
, arg2
)) {
2030 (void)ssl_printf(ssl
, "error setting option\n");
2036 /* routine to printout option values over SSL */
2037 void remote_get_opt_ssl(char* line
, void* arg
)
2039 SSL
* ssl
= (SSL
*)arg
;
2040 (void)ssl_printf(ssl
, "%s\n", line
);
2043 /** do the get_option command */
2045 do_get_option(SSL
* ssl
, struct worker
* worker
, char* arg
)
2048 r
= config_get_option(worker
->env
.cfg
, arg
, remote_get_opt_ssl
, ssl
);
2050 (void)ssl_printf(ssl
, "error unknown option\n");
2055 /** do the list_forwards command */
2057 do_list_forwards(SSL
* ssl
, struct worker
* worker
)
2059 /* since its a per-worker structure no locks needed */
2060 struct iter_forwards
* fwds
= worker
->env
.fwds
;
2061 struct iter_forward_zone
* z
;
2062 struct trust_anchor
* a
;
2064 RBTREE_FOR(z
, struct iter_forward_zone
*, fwds
->tree
) {
2065 if(!z
->dp
) continue; /* skip empty marker for stub */
2067 /* see if it is insecure */
2069 if(worker
->env
.anchors
&&
2070 (a
=anchor_find(worker
->env
.anchors
, z
->name
,
2071 z
->namelabs
, z
->namelen
, z
->dclass
))) {
2072 if(!a
->keylist
&& !a
->numDS
&& !a
->numDNSKEY
)
2074 lock_basic_unlock(&a
->lock
);
2077 if(!ssl_print_name_dp(ssl
, (insecure
?"forward +i":"forward"),
2078 z
->name
, z
->dclass
, z
->dp
))
2083 /** do the list_stubs command */
2085 do_list_stubs(SSL
* ssl
, struct worker
* worker
)
2087 struct iter_hints_stub
* z
;
2088 struct trust_anchor
* a
;
2091 RBTREE_FOR(z
, struct iter_hints_stub
*, &worker
->env
.hints
->tree
) {
2093 /* see if it is insecure */
2095 if(worker
->env
.anchors
&&
2096 (a
=anchor_find(worker
->env
.anchors
, z
->node
.name
,
2097 z
->node
.labs
, z
->node
.len
, z
->node
.dclass
))) {
2098 if(!a
->keylist
&& !a
->numDS
&& !a
->numDNSKEY
)
2100 lock_basic_unlock(&a
->lock
);
2103 snprintf(str
, sizeof(str
), "stub %sprime%s",
2104 (z
->noprime
?"no":""), (insecure
?" +i":""));
2105 if(!ssl_print_name_dp(ssl
, str
, z
->node
.name
,
2106 z
->node
.dclass
, z
->dp
))
2111 /** do the list_local_zones command */
2113 do_list_local_zones(SSL
* ssl
, struct worker
* worker
)
2115 struct local_zones
* zones
= worker
->daemon
->local_zones
;
2116 struct local_zone
* z
;
2118 lock_rw_rdlock(&zones
->lock
);
2119 RBTREE_FOR(z
, struct local_zone
*, &zones
->ztree
) {
2120 lock_rw_rdlock(&z
->lock
);
2121 dname_str(z
->name
, buf
);
2122 if(!ssl_printf(ssl
, "%s %s\n", buf
,
2123 local_zone_type2str(z
->type
))) {
2124 /* failure to print */
2125 lock_rw_unlock(&z
->lock
);
2126 lock_rw_unlock(&zones
->lock
);
2129 lock_rw_unlock(&z
->lock
);
2131 lock_rw_unlock(&zones
->lock
);
2134 /** do the list_local_data command */
2136 do_list_local_data(SSL
* ssl
, struct worker
* worker
)
2138 struct local_zones
* zones
= worker
->daemon
->local_zones
;
2139 struct local_zone
* z
;
2140 struct local_data
* d
;
2141 struct local_rrset
* p
;
2142 char* s
= (char*)sldns_buffer_begin(worker
->env
.scratch_buffer
);
2143 size_t slen
= sldns_buffer_capacity(worker
->env
.scratch_buffer
);
2144 lock_rw_rdlock(&zones
->lock
);
2145 RBTREE_FOR(z
, struct local_zone
*, &zones
->ztree
) {
2146 lock_rw_rdlock(&z
->lock
);
2147 RBTREE_FOR(d
, struct local_data
*, &z
->data
) {
2148 for(p
= d
->rrsets
; p
; p
= p
->next
) {
2149 struct packed_rrset_data
* d
=
2150 (struct packed_rrset_data
*)p
->rrset
->entry
.data
;
2152 for(i
=0; i
<d
->count
+ d
->rrsig_count
; i
++) {
2153 if(!packed_rr_to_string(p
->rrset
, i
,
2155 if(!ssl_printf(ssl
, "BADRR\n"))
2158 if(!ssl_printf(ssl
, "%s\n", s
))
2163 lock_rw_unlock(&z
->lock
);
2165 lock_rw_unlock(&zones
->lock
);
2168 /** tell other processes to execute the command */
2170 distribute_cmd(struct daemon_remote
* rc
, SSL
* ssl
, char* cmd
)
2175 /* skip i=0 which is me */
2176 for(i
=1; i
<rc
->worker
->daemon
->num
; i
++) {
2177 worker_send_cmd(rc
->worker
->daemon
->workers
[i
],
2179 if(!tube_write_msg(rc
->worker
->daemon
->workers
[i
]->cmd
,
2180 (uint8_t*)cmd
, strlen(cmd
)+1, 0)) {
2181 ssl_printf(ssl
, "error could not distribute cmd\n");
2187 /** check for name with end-of-string, space or tab after it */
2189 cmdcmp(char* p
, const char* cmd
, size_t len
)
2191 return strncmp(p
,cmd
,len
)==0 && (p
[len
]==0||p
[len
]==' '||p
[len
]=='\t');
2194 /** execute a remote control command */
2196 execute_cmd(struct daemon_remote
* rc
, SSL
* ssl
, char* cmd
,
2197 struct worker
* worker
)
2199 char* p
= skipwhite(cmd
);
2200 /* compare command */
2201 if(cmdcmp(p
, "stop", 4)) {
2204 } else if(cmdcmp(p
, "reload", 6)) {
2207 } else if(cmdcmp(p
, "stats_noreset", 13)) {
2208 do_stats(ssl
, rc
, 0);
2210 } else if(cmdcmp(p
, "stats", 5)) {
2211 do_stats(ssl
, rc
, 1);
2213 } else if(cmdcmp(p
, "status", 6)) {
2214 do_status(ssl
, worker
);
2216 } else if(cmdcmp(p
, "dump_cache", 10)) {
2217 (void)dump_cache(ssl
, worker
);
2219 } else if(cmdcmp(p
, "load_cache", 10)) {
2220 if(load_cache(ssl
, worker
)) send_ok(ssl
);
2222 } else if(cmdcmp(p
, "list_forwards", 13)) {
2223 do_list_forwards(ssl
, worker
);
2225 } else if(cmdcmp(p
, "list_stubs", 10)) {
2226 do_list_stubs(ssl
, worker
);
2228 } else if(cmdcmp(p
, "list_local_zones", 16)) {
2229 do_list_local_zones(ssl
, worker
);
2231 } else if(cmdcmp(p
, "list_local_data", 15)) {
2232 do_list_local_data(ssl
, worker
);
2234 } else if(cmdcmp(p
, "stub_add", 8)) {
2235 /* must always distribute this cmd */
2236 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2237 do_stub_add(ssl
, worker
, skipwhite(p
+8));
2239 } else if(cmdcmp(p
, "stub_remove", 11)) {
2240 /* must always distribute this cmd */
2241 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2242 do_stub_remove(ssl
, worker
, skipwhite(p
+11));
2244 } else if(cmdcmp(p
, "forward_add", 11)) {
2245 /* must always distribute this cmd */
2246 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2247 do_forward_add(ssl
, worker
, skipwhite(p
+11));
2249 } else if(cmdcmp(p
, "forward_remove", 14)) {
2250 /* must always distribute this cmd */
2251 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2252 do_forward_remove(ssl
, worker
, skipwhite(p
+14));
2254 } else if(cmdcmp(p
, "insecure_add", 12)) {
2255 /* must always distribute this cmd */
2256 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2257 do_insecure_add(ssl
, worker
, skipwhite(p
+12));
2259 } else if(cmdcmp(p
, "insecure_remove", 15)) {
2260 /* must always distribute this cmd */
2261 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2262 do_insecure_remove(ssl
, worker
, skipwhite(p
+15));
2264 } else if(cmdcmp(p
, "forward", 7)) {
2265 /* must always distribute this cmd */
2266 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2267 do_forward(ssl
, worker
, skipwhite(p
+7));
2269 } else if(cmdcmp(p
, "flush_stats", 11)) {
2270 /* must always distribute this cmd */
2271 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2272 do_flush_stats(ssl
, worker
);
2274 } else if(cmdcmp(p
, "flush_requestlist", 17)) {
2275 /* must always distribute this cmd */
2276 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2277 do_flush_requestlist(ssl
, worker
);
2279 } else if(cmdcmp(p
, "lookup", 6)) {
2280 do_lookup(ssl
, worker
, skipwhite(p
+6));
2284 #ifdef THREADS_DISABLED
2285 /* other processes must execute the command as well */
2286 /* commands that should not be distributed, returned above. */
2287 if(rc
) { /* only if this thread is the master (rc) thread */
2288 /* done before the code below, which may split the string */
2289 distribute_cmd(rc
, ssl
, cmd
);
2292 if(cmdcmp(p
, "verbosity", 9)) {
2293 do_verbosity(ssl
, skipwhite(p
+9));
2294 } else if(cmdcmp(p
, "local_zone_remove", 17)) {
2295 do_zone_remove(ssl
, worker
, skipwhite(p
+17));
2296 } else if(cmdcmp(p
, "local_zone", 10)) {
2297 do_zone_add(ssl
, worker
, skipwhite(p
+10));
2298 } else if(cmdcmp(p
, "local_data_remove", 17)) {
2299 do_data_remove(ssl
, worker
, skipwhite(p
+17));
2300 } else if(cmdcmp(p
, "local_data", 10)) {
2301 do_data_add(ssl
, worker
, skipwhite(p
+10));
2302 } else if(cmdcmp(p
, "flush_zone", 10)) {
2303 do_flush_zone(ssl
, worker
, skipwhite(p
+10));
2304 } else if(cmdcmp(p
, "flush_type", 10)) {
2305 do_flush_type(ssl
, worker
, skipwhite(p
+10));
2306 } else if(cmdcmp(p
, "flush_infra", 11)) {
2307 do_flush_infra(ssl
, worker
, skipwhite(p
+11));
2308 } else if(cmdcmp(p
, "flush", 5)) {
2309 do_flush_name(ssl
, worker
, skipwhite(p
+5));
2310 } else if(cmdcmp(p
, "dump_requestlist", 16)) {
2311 do_dump_requestlist(ssl
, worker
);
2312 } else if(cmdcmp(p
, "dump_infra", 10)) {
2313 do_dump_infra(ssl
, worker
);
2314 } else if(cmdcmp(p
, "log_reopen", 10)) {
2315 do_log_reopen(ssl
, worker
);
2316 } else if(cmdcmp(p
, "set_option", 10)) {
2317 do_set_option(ssl
, worker
, skipwhite(p
+10));
2318 } else if(cmdcmp(p
, "get_option", 10)) {
2319 do_get_option(ssl
, worker
, skipwhite(p
+10));
2320 } else if(cmdcmp(p
, "flush_bogus", 11)) {
2321 do_flush_bogus(ssl
, worker
);
2322 } else if(cmdcmp(p
, "flush_negative", 14)) {
2323 do_flush_negative(ssl
, worker
);
2325 (void)ssl_printf(ssl
, "error unknown command '%s'\n", p
);
2330 daemon_remote_exec(struct worker
* worker
)
2332 /* read the cmd string */
2333 uint8_t* msg
= NULL
;
2335 if(!tube_read_msg(worker
->cmd
, &msg
, &len
, 0)) {
2336 log_err("daemon_remote_exec: tube_read_msg failed");
2339 verbose(VERB_ALGO
, "remote exec distributed: %s", (char*)msg
);
2340 execute_cmd(NULL
, NULL
, (char*)msg
, worker
);
2344 /** handle remote control request */
2346 handle_req(struct daemon_remote
* rc
, struct rc_state
* s
, SSL
* ssl
)
2353 /* makes it possible to set the socket blocking again. */
2354 /* basically removes it from winsock_event ... */
2355 WSAEventSelect(s
->c
->fd
, NULL
, 0);
2357 fd_set_block(s
->c
->fd
);
2359 /* try to read magic UBCT[version]_space_ string */
2361 if((r
=SSL_read(ssl
, magic
, (int)sizeof(magic
)-1)) <= 0) {
2362 if(SSL_get_error(ssl
, r
) == SSL_ERROR_ZERO_RETURN
)
2364 log_crypto_err("could not SSL_read");
2368 if( r
!= 6 || strncmp(magic
, "UBCT", 4) != 0) {
2369 verbose(VERB_QUERY
, "control connection has bad magic string");
2370 /* probably wrong tool connected, ignore it completely */
2374 /* read the command line */
2375 if(!ssl_read_line(ssl
, buf
, sizeof(buf
))) {
2378 snprintf(pre
, sizeof(pre
), "UBCT%d ", UNBOUND_CONTROL_VERSION
);
2379 if(strcmp(magic
, pre
) != 0) {
2380 verbose(VERB_QUERY
, "control connection had bad "
2381 "version %s, cmd: %s", magic
, buf
);
2382 ssl_printf(ssl
, "error version mismatch\n");
2385 verbose(VERB_DETAIL
, "control cmd: %s", buf
);
2387 /* figure out what to do */
2388 execute_cmd(rc
, ssl
, buf
, rc
->worker
);
2391 int remote_control_callback(struct comm_point
* c
, void* arg
, int err
,
2392 struct comm_reply
* ATTR_UNUSED(rep
))
2394 struct rc_state
* s
= (struct rc_state
*)arg
;
2395 struct daemon_remote
* rc
= s
->rc
;
2397 if(err
!= NETEVENT_NOERROR
) {
2398 if(err
==NETEVENT_TIMEOUT
)
2399 log_err("remote control timed out");
2403 /* (continue to) setup the SSL connection */
2405 r
= SSL_do_handshake(s
->ssl
);
2407 int r2
= SSL_get_error(s
->ssl
, r
);
2408 if(r2
== SSL_ERROR_WANT_READ
) {
2409 if(s
->shake_state
== rc_hs_read
) {
2410 /* try again later */
2413 s
->shake_state
= rc_hs_read
;
2414 comm_point_listen_for_rw(c
, 1, 0);
2416 } else if(r2
== SSL_ERROR_WANT_WRITE
) {
2417 if(s
->shake_state
== rc_hs_write
) {
2418 /* try again later */
2421 s
->shake_state
= rc_hs_write
;
2422 comm_point_listen_for_rw(c
, 0, 1);
2426 log_err("remote control connection closed prematurely");
2427 log_addr(1, "failed connection from",
2428 &s
->c
->repinfo
.addr
, s
->c
->repinfo
.addrlen
);
2429 log_crypto_err("remote control failed ssl");
2434 s
->shake_state
= rc_none
;
2436 /* once handshake has completed, check authentication */
2437 if(SSL_get_verify_result(s
->ssl
) == X509_V_OK
) {
2438 X509
* x
= SSL_get_peer_certificate(s
->ssl
);
2440 verbose(VERB_DETAIL
, "remote control connection "
2441 "provided no client certificate");
2445 verbose(VERB_ALGO
, "remote control connection authenticated");
2448 verbose(VERB_DETAIL
, "remote control connection failed to "
2449 "authenticate with client certificate");
2454 /* if OK start to actually handle the request */
2455 handle_req(rc
, s
, s
->ssl
);
2457 verbose(VERB_ALGO
, "remote control operation completed");