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 s_cert
= fname_after_chroot(cfg
->server_cert_file
, cfg
, 1);
158 s_key
= fname_after_chroot(cfg
->server_key_file
, cfg
, 1);
159 if(!s_cert
|| !s_key
) {
160 log_err("out of memory in remote control fname");
163 verbose(VERB_ALGO
, "setup SSL certificates");
164 if (!SSL_CTX_use_certificate_file(rc
->ctx
,s_cert
,SSL_FILETYPE_PEM
)) {
165 log_err("Error for server-cert-file: %s", s_cert
);
166 log_crypto_err("Error in SSL_CTX use_certificate_file");
169 if(!SSL_CTX_use_PrivateKey_file(rc
->ctx
,s_key
,SSL_FILETYPE_PEM
)) {
170 log_err("Error for server-key-file: %s", s_key
);
171 log_crypto_err("Error in SSL_CTX use_PrivateKey_file");
174 if(!SSL_CTX_check_private_key(rc
->ctx
)) {
175 log_err("Error for server-key-file: %s", s_key
);
176 log_crypto_err("Error in SSL_CTX check_private_key");
179 if(!SSL_CTX_load_verify_locations(rc
->ctx
, s_cert
, NULL
)) {
180 log_crypto_err("Error setting up SSL_CTX verify locations");
184 daemon_remote_delete(rc
);
187 SSL_CTX_set_client_CA_list(rc
->ctx
, SSL_load_client_CA_file(s_cert
));
188 SSL_CTX_set_verify(rc
->ctx
, SSL_VERIFY_PEER
, NULL
);
195 void daemon_remote_clear(struct daemon_remote
* rc
)
197 struct rc_state
* p
, *np
;
199 /* but do not close the ports */
200 listen_list_delete(rc
->accept_list
);
201 rc
->accept_list
= NULL
;
202 /* do close these sockets */
208 comm_point_delete(p
->c
);
212 rc
->busy_list
= NULL
;
217 void daemon_remote_delete(struct daemon_remote
* rc
)
220 daemon_remote_clear(rc
);
222 SSL_CTX_free(rc
->ctx
);
228 * Add and open a new control port
231 * @param list: list head
232 * @param noproto_is_err: if lack of protocol support is an error.
233 * @return false on failure.
236 add_open(const char* ip
, int nr
, struct listen_port
** list
, int noproto_is_err
)
238 struct addrinfo hints
;
239 struct addrinfo
* res
;
240 struct listen_port
* n
;
244 snprintf(port
, sizeof(port
), "%d", nr
);
245 port
[sizeof(port
)-1]=0;
246 memset(&hints
, 0, sizeof(hints
));
247 hints
.ai_socktype
= SOCK_STREAM
;
248 hints
.ai_flags
= AI_PASSIVE
| AI_NUMERICHOST
;
249 if((r
= getaddrinfo(ip
, port
, &hints
, &res
)) != 0 || !res
) {
251 if(!noproto_is_err
&& r
== EAI_NONAME
) {
252 /* tried to lookup the address as name */
253 return 1; /* return success, but do nothing */
255 #endif /* USE_WINSOCK */
256 log_err("control interface %s:%s getaddrinfo: %s %s",
257 ip
?ip
:"default", port
, gai_strerror(r
),
259 r
==EAI_SYSTEM
?(char*)strerror(errno
):""
268 fd
= create_tcp_accept_sock(res
, 1, &noproto
, 0);
270 if(fd
== -1 && noproto
) {
272 return 1; /* return success, but do nothing */
273 log_err("cannot open control interface %s %d : "
274 "protocol not supported", ip
, nr
);
278 log_err("cannot open control interface %s %d", ip
, nr
);
283 n
= (struct listen_port
*)calloc(1, sizeof(*n
));
290 log_err("out of memory");
299 struct listen_port
* daemon_remote_open_ports(struct config_file
* cfg
)
301 struct listen_port
* l
= NULL
;
302 log_assert(cfg
->remote_control_enable
&& cfg
->control_port
);
303 if(cfg
->control_ifs
) {
304 struct config_strlist
* p
;
305 for(p
= cfg
->control_ifs
; p
; p
= p
->next
) {
306 if(!add_open(p
->str
, cfg
->control_port
, &l
, 1)) {
307 listening_ports_free(l
);
314 !add_open("::1", cfg
->control_port
, &l
, 0)) {
315 listening_ports_free(l
);
319 !add_open("127.0.0.1", cfg
->control_port
, &l
, 1)) {
320 listening_ports_free(l
);
327 /** open accept commpoint */
329 accept_open(struct daemon_remote
* rc
, int fd
)
331 struct listen_list
* n
= (struct listen_list
*)malloc(sizeof(*n
));
333 log_err("out of memory");
336 n
->next
= rc
->accept_list
;
339 n
->com
= comm_point_create_raw(rc
->worker
->base
, fd
, 0,
340 &remote_accept_callback
, rc
);
343 /* keep this port open, its fd is kept in the rc portlist */
344 n
->com
->do_not_close
= 1;
348 int daemon_remote_open_accept(struct daemon_remote
* rc
,
349 struct listen_port
* ports
, struct worker
* worker
)
351 struct listen_port
* p
;
353 for(p
= ports
; p
; p
= p
->next
) {
354 if(!accept_open(rc
, p
->fd
)) {
355 log_err("could not create accept comm point");
362 void daemon_remote_stop_accept(struct daemon_remote
* rc
)
364 struct listen_list
* p
;
365 for(p
=rc
->accept_list
; p
; p
=p
->next
) {
366 comm_point_stop_listening(p
->com
);
370 void daemon_remote_start_accept(struct daemon_remote
* rc
)
372 struct listen_list
* p
;
373 for(p
=rc
->accept_list
; p
; p
=p
->next
) {
374 comm_point_start_listening(p
->com
, -1, -1);
378 int remote_accept_callback(struct comm_point
* c
, void* arg
, int err
,
379 struct comm_reply
* ATTR_UNUSED(rep
))
381 struct daemon_remote
* rc
= (struct daemon_remote
*)arg
;
382 struct sockaddr_storage addr
;
386 if(err
!= NETEVENT_NOERROR
) {
387 log_err("error %d on remote_accept_callback", err
);
390 /* perform the accept */
391 newfd
= comm_point_perform_accept(c
, &addr
, &addrlen
);
394 /* create new commpoint unless we are servicing already */
395 if(rc
->active
>= rc
->max_active
) {
396 log_warn("drop incoming remote control: too many connections");
406 /* setup commpoint to service the remote control command */
407 n
= (struct rc_state
*)calloc(1, sizeof(*n
));
409 log_err("out of memory");
412 /* start in reading state */
413 n
->c
= comm_point_create_raw(rc
->worker
->base
, newfd
, 0,
414 &remote_control_callback
, n
);
416 log_err("out of memory");
420 log_addr(VERB_QUERY
, "new control connection from", &addr
, addrlen
);
421 n
->c
->do_not_close
= 0;
422 comm_point_stop_listening(n
->c
);
423 comm_point_start_listening(n
->c
, -1, REMOTE_CONTROL_TCP_TIMEOUT
);
424 memcpy(&n
->c
->repinfo
.addr
, &addr
, addrlen
);
425 n
->c
->repinfo
.addrlen
= addrlen
;
426 n
->shake_state
= rc_hs_read
;
427 n
->ssl
= SSL_new(rc
->ctx
);
429 log_crypto_err("could not SSL_new");
430 comm_point_delete(n
->c
);
434 SSL_set_accept_state(n
->ssl
);
435 (void)SSL_set_mode(n
->ssl
, SSL_MODE_AUTO_RETRY
);
436 if(!SSL_set_fd(n
->ssl
, newfd
)) {
437 log_crypto_err("could not SSL_set_fd");
439 comm_point_delete(n
->c
);
445 n
->next
= rc
->busy_list
;
449 /* perform the first nonblocking read already, for windows,
450 * so it can return wouldblock. could be faster too. */
451 (void)remote_control_callback(n
->c
, n
, NETEVENT_NOERROR
, NULL
);
455 /** delete from list */
457 state_list_remove_elem(struct rc_state
** list
, struct comm_point
* c
)
460 if( (*list
)->c
== c
) {
461 *list
= (*list
)->next
;
464 list
= &(*list
)->next
;
468 /** decrease active count and remove commpoint from busy list */
470 clean_point(struct daemon_remote
* rc
, struct rc_state
* s
)
472 state_list_remove_elem(&rc
->busy_list
, s
->c
);
475 SSL_shutdown(s
->ssl
);
478 comm_point_delete(s
->c
);
483 ssl_print_text(SSL
* ssl
, const char* text
)
489 if((r
=SSL_write(ssl
, text
, (int)strlen(text
))) <= 0) {
490 if(SSL_get_error(ssl
, r
) == SSL_ERROR_ZERO_RETURN
) {
491 verbose(VERB_QUERY
, "warning, in SSL_write, peer "
492 "closed connection");
495 log_crypto_err("could not SSL_write");
501 /** print text over the ssl connection */
503 ssl_print_vmsg(SSL
* ssl
, const char* format
, va_list args
)
506 vsnprintf(msg
, sizeof(msg
), format
, args
);
507 return ssl_print_text(ssl
, msg
);
510 /** printf style printing to the ssl connection */
511 int ssl_printf(SSL
* ssl
, const char* format
, ...)
515 va_start(args
, format
);
516 ret
= ssl_print_vmsg(ssl
, format
, args
);
522 ssl_read_line(SSL
* ssl
, char* buf
, size_t max
)
530 if((r
=SSL_read(ssl
, buf
+len
, 1)) <= 0) {
531 if(SSL_get_error(ssl
, r
) == SSL_ERROR_ZERO_RETURN
) {
535 log_crypto_err("could not SSL_read");
538 if(buf
[len
] == '\n') {
539 /* return string without \n */
546 log_err("control line too long (%d): %s", (int)max
, buf
);
550 /** skip whitespace, return new pointer into string */
554 /* EOS \0 is not a space */
555 while( isspace((unsigned char)*str
) )
560 /** send the OK to the control client */
561 static void send_ok(SSL
* ssl
)
563 (void)ssl_printf(ssl
, "ok\n");
566 /** do the stop command */
568 do_stop(SSL
* ssl
, struct daemon_remote
* rc
)
570 rc
->worker
->need_to_exit
= 1;
571 comm_base_exit(rc
->worker
->base
);
575 /** do the reload command */
577 do_reload(SSL
* ssl
, struct daemon_remote
* rc
)
579 rc
->worker
->need_to_exit
= 0;
580 comm_base_exit(rc
->worker
->base
);
584 /** do the verbosity command */
586 do_verbosity(SSL
* ssl
, char* str
)
589 if(val
== 0 && strcmp(str
, "0") != 0) {
590 ssl_printf(ssl
, "error in verbosity number syntax: %s\n", str
);
597 /** print stats from statinfo */
599 print_stats(SSL
* ssl
, const char* nm
, struct stats_info
* s
)
602 if(!ssl_printf(ssl
, "%s.num.queries"SQ
"%lu\n", nm
,
603 (unsigned long)s
->svr
.num_queries
)) return 0;
604 if(!ssl_printf(ssl
, "%s.num.cachehits"SQ
"%lu\n", nm
,
605 (unsigned long)(s
->svr
.num_queries
606 - s
->svr
.num_queries_missed_cache
))) return 0;
607 if(!ssl_printf(ssl
, "%s.num.cachemiss"SQ
"%lu\n", nm
,
608 (unsigned long)s
->svr
.num_queries_missed_cache
)) return 0;
609 if(!ssl_printf(ssl
, "%s.num.prefetch"SQ
"%lu\n", nm
,
610 (unsigned long)s
->svr
.num_queries_prefetch
)) return 0;
611 if(!ssl_printf(ssl
, "%s.num.recursivereplies"SQ
"%lu\n", nm
,
612 (unsigned long)s
->mesh_replies_sent
)) return 0;
613 if(!ssl_printf(ssl
, "%s.requestlist.avg"SQ
"%g\n", nm
,
614 (s
->svr
.num_queries_missed_cache
+s
->svr
.num_queries_prefetch
)?
615 (double)s
->svr
.sum_query_list_size
/
616 (s
->svr
.num_queries_missed_cache
+
617 s
->svr
.num_queries_prefetch
) : 0.0)) return 0;
618 if(!ssl_printf(ssl
, "%s.requestlist.max"SQ
"%lu\n", nm
,
619 (unsigned long)s
->svr
.max_query_list_size
)) return 0;
620 if(!ssl_printf(ssl
, "%s.requestlist.overwritten"SQ
"%lu\n", nm
,
621 (unsigned long)s
->mesh_jostled
)) return 0;
622 if(!ssl_printf(ssl
, "%s.requestlist.exceeded"SQ
"%lu\n", nm
,
623 (unsigned long)s
->mesh_dropped
)) return 0;
624 if(!ssl_printf(ssl
, "%s.requestlist.current.all"SQ
"%lu\n", nm
,
625 (unsigned long)s
->mesh_num_states
)) return 0;
626 if(!ssl_printf(ssl
, "%s.requestlist.current.user"SQ
"%lu\n", nm
,
627 (unsigned long)s
->mesh_num_reply_states
)) return 0;
628 timeval_divide(&avg
, &s
->mesh_replies_sum_wait
, s
->mesh_replies_sent
);
629 if(!ssl_printf(ssl
, "%s.recursion.time.avg"SQ ARG_LL
"d.%6.6d\n", nm
,
630 (long long)avg
.tv_sec
, (int)avg
.tv_usec
)) return 0;
631 if(!ssl_printf(ssl
, "%s.recursion.time.median"SQ
"%g\n", nm
,
632 s
->mesh_time_median
)) return 0;
636 /** print stats for one thread */
638 print_thread_stats(SSL
* ssl
, int i
, struct stats_info
* s
)
641 snprintf(nm
, sizeof(nm
), "thread%d", i
);
643 return print_stats(ssl
, nm
, s
);
646 /** print long number */
648 print_longnum(SSL
* ssl
, const char* desc
, size_t x
)
650 if(x
> 1024*1024*1024) {
652 size_t front
= x
/ (size_t)1000000;
653 size_t back
= x
% (size_t)1000000;
654 return ssl_printf(ssl
, "%s%u%6.6u\n", desc
,
655 (unsigned)front
, (unsigned)back
);
657 return ssl_printf(ssl
, "%s%lu\n", desc
, (unsigned long)x
);
661 /** print mem stats */
663 print_mem(SSL
* ssl
, struct worker
* worker
, struct daemon
* daemon
)
666 size_t msg
, rrset
, val
, iter
;
668 extern void* unbound_start_brk
;
670 if(!print_longnum(ssl
, "mem.total.sbrk"SQ
,
671 (size_t)((char*)cur
- (char*)unbound_start_brk
))) return 0;
672 #endif /* HAVE_SBRK */
673 msg
= slabhash_get_mem(daemon
->env
->msg_cache
);
674 rrset
= slabhash_get_mem(&daemon
->env
->rrset_cache
->table
);
677 m
= modstack_find(&worker
->env
.mesh
->mods
, "validator");
679 fptr_ok(fptr_whitelist_mod_get_mem(worker
->env
.mesh
->
680 mods
.mod
[m
]->get_mem
));
681 val
= (*worker
->env
.mesh
->mods
.mod
[m
]->get_mem
)
684 m
= modstack_find(&worker
->env
.mesh
->mods
, "iterator");
686 fptr_ok(fptr_whitelist_mod_get_mem(worker
->env
.mesh
->
687 mods
.mod
[m
]->get_mem
));
688 iter
= (*worker
->env
.mesh
->mods
.mod
[m
]->get_mem
)
692 if(!print_longnum(ssl
, "mem.cache.rrset"SQ
, rrset
))
694 if(!print_longnum(ssl
, "mem.cache.message"SQ
, msg
))
696 if(!print_longnum(ssl
, "mem.mod.iterator"SQ
, iter
))
698 if(!print_longnum(ssl
, "mem.mod.validator"SQ
, val
))
703 /** print uptime stats */
705 print_uptime(SSL
* ssl
, struct worker
* worker
, int reset
)
707 struct timeval now
= *worker
->env
.now_tv
;
708 struct timeval up
, dt
;
709 timeval_subtract(&up
, &now
, &worker
->daemon
->time_boot
);
710 timeval_subtract(&dt
, &now
, &worker
->daemon
->time_last_stat
);
712 worker
->daemon
->time_last_stat
= now
;
713 if(!ssl_printf(ssl
, "time.now"SQ ARG_LL
"d.%6.6d\n",
714 (long long)now
.tv_sec
, (unsigned)now
.tv_usec
)) return 0;
715 if(!ssl_printf(ssl
, "time.up"SQ ARG_LL
"d.%6.6d\n",
716 (long long)up
.tv_sec
, (unsigned)up
.tv_usec
)) return 0;
717 if(!ssl_printf(ssl
, "time.elapsed"SQ ARG_LL
"d.%6.6d\n",
718 (long long)dt
.tv_sec
, (unsigned)dt
.tv_usec
)) return 0;
722 /** print extended histogram */
724 print_hist(SSL
* ssl
, struct stats_info
* s
)
726 struct timehist
* hist
;
728 hist
= timehist_setup();
730 log_err("out of memory");
733 timehist_import(hist
, s
->svr
.hist
, NUM_BUCKETS_HIST
);
734 for(i
=0; i
<hist
->num
; i
++) {
736 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
737 (int)hist
->buckets
[i
].lower
.tv_sec
,
738 (int)hist
->buckets
[i
].lower
.tv_usec
,
739 (int)hist
->buckets
[i
].upper
.tv_sec
,
740 (int)hist
->buckets
[i
].upper
.tv_usec
,
741 (unsigned long)hist
->buckets
[i
].count
)) {
742 timehist_delete(hist
);
746 timehist_delete(hist
);
750 /** print extended stats */
752 print_ext(SSL
* ssl
, struct stats_info
* s
)
756 const sldns_rr_descriptor
* desc
;
757 const sldns_lookup_table
* lt
;
759 for(i
=0; i
<STATS_QTYPE_NUM
; i
++) {
760 if(inhibit_zero
&& s
->svr
.qtype
[i
] == 0)
762 desc
= sldns_rr_descript((uint16_t)i
);
763 if(desc
&& desc
->_name
) {
764 snprintf(nm
, sizeof(nm
), "%s", desc
->_name
);
765 } else if (i
== LDNS_RR_TYPE_IXFR
) {
766 snprintf(nm
, sizeof(nm
), "IXFR");
767 } else if (i
== LDNS_RR_TYPE_AXFR
) {
768 snprintf(nm
, sizeof(nm
), "AXFR");
769 } else if (i
== LDNS_RR_TYPE_MAILA
) {
770 snprintf(nm
, sizeof(nm
), "MAILA");
771 } else if (i
== LDNS_RR_TYPE_MAILB
) {
772 snprintf(nm
, sizeof(nm
), "MAILB");
773 } else if (i
== LDNS_RR_TYPE_ANY
) {
774 snprintf(nm
, sizeof(nm
), "ANY");
776 snprintf(nm
, sizeof(nm
), "TYPE%d", i
);
778 if(!ssl_printf(ssl
, "num.query.type.%s"SQ
"%lu\n",
779 nm
, (unsigned long)s
->svr
.qtype
[i
])) return 0;
781 if(!inhibit_zero
|| s
->svr
.qtype_big
) {
782 if(!ssl_printf(ssl
, "num.query.type.other"SQ
"%lu\n",
783 (unsigned long)s
->svr
.qtype_big
)) return 0;
786 for(i
=0; i
<STATS_QCLASS_NUM
; i
++) {
787 if(inhibit_zero
&& s
->svr
.qclass
[i
] == 0)
789 lt
= sldns_lookup_by_id(sldns_rr_classes
, i
);
791 snprintf(nm
, sizeof(nm
), "%s", lt
->name
);
793 snprintf(nm
, sizeof(nm
), "CLASS%d", i
);
795 if(!ssl_printf(ssl
, "num.query.class.%s"SQ
"%lu\n",
796 nm
, (unsigned long)s
->svr
.qclass
[i
])) return 0;
798 if(!inhibit_zero
|| s
->svr
.qclass_big
) {
799 if(!ssl_printf(ssl
, "num.query.class.other"SQ
"%lu\n",
800 (unsigned long)s
->svr
.qclass_big
)) return 0;
803 for(i
=0; i
<STATS_OPCODE_NUM
; i
++) {
804 if(inhibit_zero
&& s
->svr
.qopcode
[i
] == 0)
806 lt
= sldns_lookup_by_id(sldns_opcodes
, i
);
808 snprintf(nm
, sizeof(nm
), "%s", lt
->name
);
810 snprintf(nm
, sizeof(nm
), "OPCODE%d", i
);
812 if(!ssl_printf(ssl
, "num.query.opcode.%s"SQ
"%lu\n",
813 nm
, (unsigned long)s
->svr
.qopcode
[i
])) return 0;
816 if(!ssl_printf(ssl
, "num.query.tcp"SQ
"%lu\n",
817 (unsigned long)s
->svr
.qtcp
)) return 0;
818 if(!ssl_printf(ssl
, "num.query.tcpout"SQ
"%lu\n",
819 (unsigned long)s
->svr
.qtcp_outgoing
)) return 0;
820 if(!ssl_printf(ssl
, "num.query.ipv6"SQ
"%lu\n",
821 (unsigned long)s
->svr
.qipv6
)) return 0;
823 if(!ssl_printf(ssl
, "num.query.flags.QR"SQ
"%lu\n",
824 (unsigned long)s
->svr
.qbit_QR
)) return 0;
825 if(!ssl_printf(ssl
, "num.query.flags.AA"SQ
"%lu\n",
826 (unsigned long)s
->svr
.qbit_AA
)) return 0;
827 if(!ssl_printf(ssl
, "num.query.flags.TC"SQ
"%lu\n",
828 (unsigned long)s
->svr
.qbit_TC
)) return 0;
829 if(!ssl_printf(ssl
, "num.query.flags.RD"SQ
"%lu\n",
830 (unsigned long)s
->svr
.qbit_RD
)) return 0;
831 if(!ssl_printf(ssl
, "num.query.flags.RA"SQ
"%lu\n",
832 (unsigned long)s
->svr
.qbit_RA
)) return 0;
833 if(!ssl_printf(ssl
, "num.query.flags.Z"SQ
"%lu\n",
834 (unsigned long)s
->svr
.qbit_Z
)) return 0;
835 if(!ssl_printf(ssl
, "num.query.flags.AD"SQ
"%lu\n",
836 (unsigned long)s
->svr
.qbit_AD
)) return 0;
837 if(!ssl_printf(ssl
, "num.query.flags.CD"SQ
"%lu\n",
838 (unsigned long)s
->svr
.qbit_CD
)) return 0;
839 if(!ssl_printf(ssl
, "num.query.edns.present"SQ
"%lu\n",
840 (unsigned long)s
->svr
.qEDNS
)) return 0;
841 if(!ssl_printf(ssl
, "num.query.edns.DO"SQ
"%lu\n",
842 (unsigned long)s
->svr
.qEDNS_DO
)) return 0;
845 for(i
=0; i
<STATS_RCODE_NUM
; i
++) {
846 /* Always include RCODEs 0-5 */
847 if(inhibit_zero
&& i
> LDNS_RCODE_REFUSED
&& s
->svr
.ans_rcode
[i
] == 0)
849 lt
= sldns_lookup_by_id(sldns_rcodes
, i
);
851 snprintf(nm
, sizeof(nm
), "%s", lt
->name
);
853 snprintf(nm
, sizeof(nm
), "RCODE%d", i
);
855 if(!ssl_printf(ssl
, "num.answer.rcode.%s"SQ
"%lu\n",
856 nm
, (unsigned long)s
->svr
.ans_rcode
[i
])) return 0;
858 if(!inhibit_zero
|| s
->svr
.ans_rcode_nodata
) {
859 if(!ssl_printf(ssl
, "num.answer.rcode.nodata"SQ
"%lu\n",
860 (unsigned long)s
->svr
.ans_rcode_nodata
)) return 0;
863 if(!ssl_printf(ssl
, "num.answer.secure"SQ
"%lu\n",
864 (unsigned long)s
->svr
.ans_secure
)) return 0;
865 if(!ssl_printf(ssl
, "num.answer.bogus"SQ
"%lu\n",
866 (unsigned long)s
->svr
.ans_bogus
)) return 0;
867 if(!ssl_printf(ssl
, "num.rrset.bogus"SQ
"%lu\n",
868 (unsigned long)s
->svr
.rrset_bogus
)) return 0;
869 /* threat detection */
870 if(!ssl_printf(ssl
, "unwanted.queries"SQ
"%lu\n",
871 (unsigned long)s
->svr
.unwanted_queries
)) return 0;
872 if(!ssl_printf(ssl
, "unwanted.replies"SQ
"%lu\n",
873 (unsigned long)s
->svr
.unwanted_replies
)) return 0;
875 if(!ssl_printf(ssl
, "msg.cache.count"SQ
"%u\n",
876 (unsigned)s
->svr
.msg_cache_count
)) return 0;
877 if(!ssl_printf(ssl
, "rrset.cache.count"SQ
"%u\n",
878 (unsigned)s
->svr
.rrset_cache_count
)) return 0;
879 if(!ssl_printf(ssl
, "infra.cache.count"SQ
"%u\n",
880 (unsigned)s
->svr
.infra_cache_count
)) return 0;
881 if(!ssl_printf(ssl
, "key.cache.count"SQ
"%u\n",
882 (unsigned)s
->svr
.key_cache_count
)) return 0;
886 /** do the stats command */
888 do_stats(SSL
* ssl
, struct daemon_remote
* rc
, int reset
)
890 struct daemon
* daemon
= rc
->worker
->daemon
;
891 struct stats_info total
;
894 log_assert(daemon
->num
> 0);
895 /* gather all thread statistics in one place */
896 for(i
=0; i
<daemon
->num
; i
++) {
897 server_stats_obtain(rc
->worker
, daemon
->workers
[i
], &s
, reset
);
898 if(!print_thread_stats(ssl
, i
, &s
))
902 else server_stats_add(&total
, &s
);
904 /* print the thread statistics */
905 total
.mesh_time_median
/= (double)daemon
->num
;
906 if(!print_stats(ssl
, "total", &total
))
908 if(!print_uptime(ssl
, rc
->worker
, reset
))
910 if(daemon
->cfg
->stat_extended
) {
911 if(!print_mem(ssl
, rc
->worker
, daemon
))
913 if(!print_hist(ssl
, &total
))
915 if(!print_ext(ssl
, &total
))
920 /** parse commandline argument domain name */
922 parse_arg_name(SSL
* ssl
, char* str
, uint8_t** res
, size_t* len
, int* labs
)
924 uint8_t nm
[LDNS_MAX_DOMAINLEN
+1];
925 size_t nmlen
= sizeof(nm
);
930 status
= sldns_str2wire_dname_buf(str
, nm
, &nmlen
);
932 ssl_printf(ssl
, "error cannot parse name %s at %d: %s\n", str
,
933 LDNS_WIREPARSE_OFFSET(status
),
934 sldns_get_errorstr_parse(status
));
937 *res
= memdup(nm
, nmlen
);
939 ssl_printf(ssl
, "error out of memory\n");
942 *labs
= dname_count_size_labels(*res
, len
);
946 /** find second argument, modifies string */
948 find_arg2(SSL
* ssl
, char* arg
, char** arg2
)
950 char* as
= strchr(arg
, ' ');
951 char* at
= strchr(arg
, '\t');
956 *arg2
= skipwhite(as
+1);
959 *arg2
= skipwhite(as
+1);
962 *arg2
= skipwhite(at
+1);
964 ssl_printf(ssl
, "error could not find next argument "
971 /** Add a new zone */
973 do_zone_add(SSL
* ssl
, struct worker
* worker
, char* arg
)
979 enum localzone_type t
;
980 struct local_zone
* z
;
981 if(!find_arg2(ssl
, arg
, &arg2
))
983 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
985 if(!local_zone_str2type(arg2
, &t
)) {
986 ssl_printf(ssl
, "error not a zone type. %s\n", arg2
);
990 lock_rw_wrlock(&worker
->daemon
->local_zones
->lock
);
991 if((z
=local_zones_find(worker
->daemon
->local_zones
, nm
, nmlen
,
992 nmlabs
, LDNS_RR_CLASS_IN
))) {
993 /* already present in tree */
994 lock_rw_wrlock(&z
->lock
);
995 z
->type
= t
; /* update type anyway */
996 lock_rw_unlock(&z
->lock
);
998 lock_rw_unlock(&worker
->daemon
->local_zones
->lock
);
1002 if(!local_zones_add_zone(worker
->daemon
->local_zones
, nm
, nmlen
,
1003 nmlabs
, LDNS_RR_CLASS_IN
, t
)) {
1004 lock_rw_unlock(&worker
->daemon
->local_zones
->lock
);
1005 ssl_printf(ssl
, "error out of memory\n");
1008 lock_rw_unlock(&worker
->daemon
->local_zones
->lock
);
1012 /** Remove a zone */
1014 do_zone_remove(SSL
* ssl
, struct worker
* worker
, char* arg
)
1019 struct local_zone
* z
;
1020 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1022 lock_rw_wrlock(&worker
->daemon
->local_zones
->lock
);
1023 if((z
=local_zones_find(worker
->daemon
->local_zones
, nm
, nmlen
,
1024 nmlabs
, LDNS_RR_CLASS_IN
))) {
1025 /* present in tree */
1026 local_zones_del_zone(worker
->daemon
->local_zones
, z
);
1028 lock_rw_unlock(&worker
->daemon
->local_zones
->lock
);
1033 /** Add new RR data */
1035 do_data_add(SSL
* ssl
, struct worker
* worker
, char* arg
)
1037 if(!local_zones_add_RR(worker
->daemon
->local_zones
, arg
)) {
1038 ssl_printf(ssl
,"error in syntax or out of memory, %s\n", arg
);
1044 /** Remove RR data */
1046 do_data_remove(SSL
* ssl
, struct worker
* worker
, char* arg
)
1051 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1053 local_zones_del_data(worker
->daemon
->local_zones
, nm
,
1054 nmlen
, nmlabs
, LDNS_RR_CLASS_IN
);
1059 /** cache lookup of nameservers */
1061 do_lookup(SSL
* ssl
, struct worker
* worker
, char* arg
)
1066 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1068 (void)print_deleg_lookup(ssl
, worker
, nm
, nmlen
, nmlabs
);
1072 /** flush something from rrset and msg caches */
1074 do_cache_remove(struct worker
* worker
, uint8_t* nm
, size_t nmlen
,
1075 uint16_t t
, uint16_t c
)
1078 struct query_info k
;
1079 rrset_cache_remove(worker
->env
.rrset_cache
, nm
, nmlen
, t
, c
, 0);
1080 if(t
== LDNS_RR_TYPE_SOA
)
1081 rrset_cache_remove(worker
->env
.rrset_cache
, nm
, nmlen
, t
, c
,
1082 PACKED_RRSET_SOA_NEG
);
1084 k
.qname_len
= nmlen
;
1087 h
= query_info_hash(&k
, 0);
1088 slabhash_remove(worker
->env
.msg_cache
, h
, &k
);
1089 if(t
== LDNS_RR_TYPE_AAAA
) {
1090 /* for AAAA also flush dns64 bit_cd packet */
1091 h
= query_info_hash(&k
, BIT_CD
);
1092 slabhash_remove(worker
->env
.msg_cache
, h
, &k
);
1098 do_flush_type(SSL
* ssl
, struct worker
* worker
, char* arg
)
1105 if(!find_arg2(ssl
, arg
, &arg2
))
1107 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1109 t
= sldns_get_rr_type_by_name(arg2
);
1110 do_cache_remove(worker
, nm
, nmlen
, t
, LDNS_RR_CLASS_IN
);
1116 /** flush statistics */
1118 do_flush_stats(SSL
* ssl
, struct worker
* worker
)
1120 worker_stats_clear(worker
);
1125 * Local info for deletion functions
1129 struct worker
* worker
;
1130 /** name to delete */
1138 /** time to invalidate to */
1140 /** number of rrsets removed */
1142 /** number of msgs removed */
1144 /** number of key entries removed */
1146 /** length of addr */
1148 /** socket address for host deletion */
1149 struct sockaddr_storage addr
;
1152 /** callback to delete hosts in infra cache */
1154 infra_del_host(struct lruhash_entry
* e
, void* arg
)
1156 /* entry is locked */
1157 struct del_info
* inf
= (struct del_info
*)arg
;
1158 struct infra_key
* k
= (struct infra_key
*)e
->key
;
1159 if(sockaddr_cmp(&inf
->addr
, inf
->addrlen
, &k
->addr
, k
->addrlen
) == 0) {
1160 struct infra_data
* d
= (struct infra_data
*)e
->data
;
1163 d
->timeout_AAAA
= 0;
1164 d
->timeout_other
= 0;
1166 if(d
->ttl
>= inf
->now
) {
1167 d
->ttl
= inf
->expired
;
1173 /** flush infra cache */
1175 do_flush_infra(SSL
* ssl
, struct worker
* worker
, char* arg
)
1177 struct sockaddr_storage addr
;
1179 struct del_info inf
;
1180 if(strcmp(arg
, "all") == 0) {
1181 slabhash_clear(worker
->env
.infra_cache
->hosts
);
1185 if(!ipstrtoaddr(arg
, UNBOUND_DNS_PORT
, &addr
, &len
)) {
1186 (void)ssl_printf(ssl
, "error parsing ip addr: '%s'\n", arg
);
1189 /* delete all entries from cache */
1190 /* what we do is to set them all expired */
1191 inf
.worker
= worker
;
1195 inf
.now
= *worker
->env
.now
;
1196 inf
.expired
= *worker
->env
.now
;
1197 inf
.expired
-= 3; /* handle 3 seconds skew between threads */
1202 memmove(&inf
.addr
, &addr
, len
);
1203 slabhash_traverse(worker
->env
.infra_cache
->hosts
, 1, &infra_del_host
,
1208 /** flush requestlist */
1210 do_flush_requestlist(SSL
* ssl
, struct worker
* worker
)
1212 mesh_delete_all(worker
->env
.mesh
);
1216 /** callback to delete rrsets in a zone */
1218 zone_del_rrset(struct lruhash_entry
* e
, void* arg
)
1220 /* entry is locked */
1221 struct del_info
* inf
= (struct del_info
*)arg
;
1222 struct ub_packed_rrset_key
* k
= (struct ub_packed_rrset_key
*)e
->key
;
1223 if(dname_subdomain_c(k
->rk
.dname
, inf
->name
)) {
1224 struct packed_rrset_data
* d
=
1225 (struct packed_rrset_data
*)e
->data
;
1226 if(d
->ttl
>= inf
->now
) {
1227 d
->ttl
= inf
->expired
;
1233 /** callback to delete messages in a zone */
1235 zone_del_msg(struct lruhash_entry
* e
, void* arg
)
1237 /* entry is locked */
1238 struct del_info
* inf
= (struct del_info
*)arg
;
1239 struct msgreply_entry
* k
= (struct msgreply_entry
*)e
->key
;
1240 if(dname_subdomain_c(k
->key
.qname
, inf
->name
)) {
1241 struct reply_info
* d
= (struct reply_info
*)e
->data
;
1242 if(d
->ttl
>= inf
->now
) {
1243 d
->ttl
= inf
->expired
;
1249 /** callback to delete keys in zone */
1251 zone_del_kcache(struct lruhash_entry
* e
, void* arg
)
1253 /* entry is locked */
1254 struct del_info
* inf
= (struct del_info
*)arg
;
1255 struct key_entry_key
* k
= (struct key_entry_key
*)e
->key
;
1256 if(dname_subdomain_c(k
->name
, inf
->name
)) {
1257 struct key_entry_data
* d
= (struct key_entry_data
*)e
->data
;
1258 if(d
->ttl
>= inf
->now
) {
1259 d
->ttl
= inf
->expired
;
1265 /** remove all rrsets and keys from zone from cache */
1267 do_flush_zone(SSL
* ssl
, struct worker
* worker
, char* arg
)
1272 struct del_info inf
;
1273 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1275 /* delete all RRs and key entries from zone */
1276 /* what we do is to set them all expired */
1277 inf
.worker
= worker
;
1281 inf
.now
= *worker
->env
.now
;
1282 inf
.expired
= *worker
->env
.now
;
1283 inf
.expired
-= 3; /* handle 3 seconds skew between threads */
1287 slabhash_traverse(&worker
->env
.rrset_cache
->table
, 1,
1288 &zone_del_rrset
, &inf
);
1290 slabhash_traverse(worker
->env
.msg_cache
, 1, &zone_del_msg
, &inf
);
1292 /* and validator cache */
1293 if(worker
->env
.key_cache
) {
1294 slabhash_traverse(worker
->env
.key_cache
->slab
, 1,
1295 &zone_del_kcache
, &inf
);
1300 (void)ssl_printf(ssl
, "ok removed %lu rrsets, %lu messages "
1301 "and %lu key entries\n", (unsigned long)inf
.num_rrsets
,
1302 (unsigned long)inf
.num_msgs
, (unsigned long)inf
.num_keys
);
1305 /** callback to delete bogus rrsets */
1307 bogus_del_rrset(struct lruhash_entry
* e
, void* arg
)
1309 /* entry is locked */
1310 struct del_info
* inf
= (struct del_info
*)arg
;
1311 struct packed_rrset_data
* d
= (struct packed_rrset_data
*)e
->data
;
1312 if(d
->security
== sec_status_bogus
) {
1313 d
->ttl
= inf
->expired
;
1318 /** callback to delete bogus messages */
1320 bogus_del_msg(struct lruhash_entry
* e
, void* arg
)
1322 /* entry is locked */
1323 struct del_info
* inf
= (struct del_info
*)arg
;
1324 struct reply_info
* d
= (struct reply_info
*)e
->data
;
1325 if(d
->security
== sec_status_bogus
) {
1326 d
->ttl
= inf
->expired
;
1331 /** callback to delete bogus keys */
1333 bogus_del_kcache(struct lruhash_entry
* e
, void* arg
)
1335 /* entry is locked */
1336 struct del_info
* inf
= (struct del_info
*)arg
;
1337 struct key_entry_data
* d
= (struct key_entry_data
*)e
->data
;
1339 d
->ttl
= inf
->expired
;
1344 /** remove all bogus rrsets, msgs and keys from cache */
1346 do_flush_bogus(SSL
* ssl
, struct worker
* worker
)
1348 struct del_info inf
;
1349 /* what we do is to set them all expired */
1350 inf
.worker
= worker
;
1351 inf
.now
= *worker
->env
.now
;
1352 inf
.expired
= *worker
->env
.now
;
1353 inf
.expired
-= 3; /* handle 3 seconds skew between threads */
1357 slabhash_traverse(&worker
->env
.rrset_cache
->table
, 1,
1358 &bogus_del_rrset
, &inf
);
1360 slabhash_traverse(worker
->env
.msg_cache
, 1, &bogus_del_msg
, &inf
);
1362 /* and validator cache */
1363 if(worker
->env
.key_cache
) {
1364 slabhash_traverse(worker
->env
.key_cache
->slab
, 1,
1365 &bogus_del_kcache
, &inf
);
1368 (void)ssl_printf(ssl
, "ok removed %lu rrsets, %lu messages "
1369 "and %lu key entries\n", (unsigned long)inf
.num_rrsets
,
1370 (unsigned long)inf
.num_msgs
, (unsigned long)inf
.num_keys
);
1373 /** callback to delete negative and servfail rrsets */
1375 negative_del_rrset(struct lruhash_entry
* e
, void* arg
)
1377 /* entry is locked */
1378 struct del_info
* inf
= (struct del_info
*)arg
;
1379 struct ub_packed_rrset_key
* k
= (struct ub_packed_rrset_key
*)e
->key
;
1380 struct packed_rrset_data
* d
= (struct packed_rrset_data
*)e
->data
;
1381 /* delete the parentside negative cache rrsets,
1382 * these are namerserver rrsets that failed lookup, rdata empty */
1383 if((k
->rk
.flags
& PACKED_RRSET_PARENT_SIDE
) && d
->count
== 1 &&
1384 d
->rrsig_count
== 0 && d
->rr_len
[0] == 0) {
1385 d
->ttl
= inf
->expired
;
1390 /** callback to delete negative and servfail messages */
1392 negative_del_msg(struct lruhash_entry
* e
, void* arg
)
1394 /* entry is locked */
1395 struct del_info
* inf
= (struct del_info
*)arg
;
1396 struct reply_info
* d
= (struct reply_info
*)e
->data
;
1397 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error
1398 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */
1399 if(FLAGS_GET_RCODE(d
->flags
) != 0 || d
->an_numrrsets
== 0) {
1400 d
->ttl
= inf
->expired
;
1405 /** callback to delete negative key entries */
1407 negative_del_kcache(struct lruhash_entry
* e
, void* arg
)
1409 /* entry is locked */
1410 struct del_info
* inf
= (struct del_info
*)arg
;
1411 struct key_entry_data
* d
= (struct key_entry_data
*)e
->data
;
1412 /* could be bad because of lookup failure on the DS, DNSKEY, which
1413 * was nxdomain or servfail, and thus a result of negative lookups */
1415 d
->ttl
= inf
->expired
;
1420 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */
1422 do_flush_negative(SSL
* ssl
, struct worker
* worker
)
1424 struct del_info inf
;
1425 /* what we do is to set them all expired */
1426 inf
.worker
= worker
;
1427 inf
.now
= *worker
->env
.now
;
1428 inf
.expired
= *worker
->env
.now
;
1429 inf
.expired
-= 3; /* handle 3 seconds skew between threads */
1433 slabhash_traverse(&worker
->env
.rrset_cache
->table
, 1,
1434 &negative_del_rrset
, &inf
);
1436 slabhash_traverse(worker
->env
.msg_cache
, 1, &negative_del_msg
, &inf
);
1438 /* and validator cache */
1439 if(worker
->env
.key_cache
) {
1440 slabhash_traverse(worker
->env
.key_cache
->slab
, 1,
1441 &negative_del_kcache
, &inf
);
1444 (void)ssl_printf(ssl
, "ok removed %lu rrsets, %lu messages "
1445 "and %lu key entries\n", (unsigned long)inf
.num_rrsets
,
1446 (unsigned long)inf
.num_msgs
, (unsigned long)inf
.num_keys
);
1449 /** remove name rrset from cache */
1451 do_flush_name(SSL
* ssl
, struct worker
* w
, char* arg
)
1456 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1458 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_A
, LDNS_RR_CLASS_IN
);
1459 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_AAAA
, LDNS_RR_CLASS_IN
);
1460 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_NS
, LDNS_RR_CLASS_IN
);
1461 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_SOA
, LDNS_RR_CLASS_IN
);
1462 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_CNAME
, LDNS_RR_CLASS_IN
);
1463 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_DNAME
, LDNS_RR_CLASS_IN
);
1464 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_MX
, LDNS_RR_CLASS_IN
);
1465 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_PTR
, LDNS_RR_CLASS_IN
);
1466 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_SRV
, LDNS_RR_CLASS_IN
);
1467 do_cache_remove(w
, nm
, nmlen
, LDNS_RR_TYPE_NAPTR
, LDNS_RR_CLASS_IN
);
1473 /** printout a delegation point info */
1475 ssl_print_name_dp(SSL
* ssl
, const char* str
, uint8_t* nm
, uint16_t dclass
,
1479 struct delegpt_ns
* ns
;
1480 struct delegpt_addr
* a
;
1482 if(str
) { /* print header for forward, stub */
1483 char* c
= sldns_wire2str_class(dclass
);
1485 if(!ssl_printf(ssl
, "%s %s %s ", buf
, (c
?c
:"CLASS??"), str
)) {
1491 for(ns
= dp
->nslist
; ns
; ns
= ns
->next
) {
1492 dname_str(ns
->name
, buf
);
1493 if(!ssl_printf(ssl
, "%s%s", (f
?" ":""), buf
))
1497 for(a
= dp
->target_list
; a
; a
= a
->next_target
) {
1498 addr_to_str(&a
->addr
, a
->addrlen
, buf
, sizeof(buf
));
1499 if(!ssl_printf(ssl
, "%s%s", (f
?" ":""), buf
))
1503 return ssl_printf(ssl
, "\n");
1507 /** print root forwards */
1509 print_root_fwds(SSL
* ssl
, struct iter_forwards
* fwds
, uint8_t* root
)
1512 dp
= forwards_lookup(fwds
, root
, LDNS_RR_CLASS_IN
);
1514 return ssl_printf(ssl
, "off (using root hints)\n");
1515 /* if dp is returned it must be the root */
1516 log_assert(query_dname_compare(dp
->name
, root
)==0);
1517 return ssl_print_name_dp(ssl
, NULL
, root
, LDNS_RR_CLASS_IN
, dp
);
1520 /** parse args into delegpt */
1521 static struct delegpt
*
1522 parse_delegpt(SSL
* ssl
, char* args
, uint8_t* nm
, int allow_names
)
1524 /* parse args and add in */
1527 struct delegpt
* dp
= delegpt_create_mlc(nm
);
1528 struct sockaddr_storage addr
;
1531 (void)ssl_printf(ssl
, "error out of memory\n");
1536 p
= strchr(p
, ' '); /* find next spot, if any */
1538 *p
++ = 0; /* end this spot */
1539 p
= skipwhite(p
); /* position at next spot */
1542 if(!extstrtoaddr(todo
, &addr
, &addrlen
)) {
1547 if(!parse_arg_name(ssl
, todo
, &n
, &ln
, &lb
)) {
1548 (void)ssl_printf(ssl
, "error cannot "
1549 "parse IP address or name "
1551 delegpt_free_mlc(dp
);
1554 if(!delegpt_add_ns_mlc(dp
, n
, 0)) {
1555 (void)ssl_printf(ssl
, "error out of memory\n");
1557 delegpt_free_mlc(dp
);
1563 (void)ssl_printf(ssl
, "error cannot parse"
1564 " IP address '%s'\n", todo
);
1565 delegpt_free_mlc(dp
);
1570 if(!delegpt_add_addr_mlc(dp
, &addr
, addrlen
, 0, 0)) {
1571 (void)ssl_printf(ssl
, "error out of memory\n");
1572 delegpt_free_mlc(dp
);
1580 /** do the status command */
1582 do_forward(SSL
* ssl
, struct worker
* worker
, char* args
)
1584 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1585 uint8_t* root
= (uint8_t*)"\000";
1587 (void)ssl_printf(ssl
, "error: structure not allocated\n");
1590 if(args
== NULL
|| args
[0] == 0) {
1591 (void)print_root_fwds(ssl
, fwd
, root
);
1594 /* set root forwards for this thread. since we are in remote control
1595 * the actual mesh is not running, so we can freely edit it. */
1596 /* delete all the existing queries first */
1597 mesh_delete_all(worker
->env
.mesh
);
1598 if(strcmp(args
, "off") == 0) {
1599 forwards_delete_zone(fwd
, LDNS_RR_CLASS_IN
, root
);
1602 if(!(dp
= parse_delegpt(ssl
, args
, root
, 0)))
1604 if(!forwards_add_zone(fwd
, LDNS_RR_CLASS_IN
, dp
)) {
1605 (void)ssl_printf(ssl
, "error out of memory\n");
1613 parse_fs_args(SSL
* ssl
, char* args
, uint8_t** nm
, struct delegpt
** dp
,
1614 int* insecure
, int* prime
)
1620 /* parse all -x args */
1621 while(args
[0] == '+') {
1622 if(!find_arg2(ssl
, args
, &rest
))
1624 while(*(++args
) != 0) {
1625 if(*args
== 'i' && insecure
)
1627 else if(*args
== 'p' && prime
)
1630 (void)ssl_printf(ssl
, "error: unknown option %s\n", args
);
1638 if(!find_arg2(ssl
, args
, &rest
))
1642 } else zonename
= args
;
1643 if(!parse_arg_name(ssl
, zonename
, nm
, &nmlen
, &nmlabs
))
1648 if(!(*dp
= parse_delegpt(ssl
, args
, *nm
, 1))) {
1656 /** do the forward_add command */
1658 do_forward_add(SSL
* ssl
, struct worker
* worker
, char* args
)
1660 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1663 struct delegpt
* dp
= NULL
;
1664 if(!parse_fs_args(ssl
, args
, &nm
, &dp
, &insecure
, NULL
))
1666 if(insecure
&& worker
->env
.anchors
) {
1667 if(!anchors_add_insecure(worker
->env
.anchors
, LDNS_RR_CLASS_IN
,
1669 (void)ssl_printf(ssl
, "error out of memory\n");
1670 delegpt_free_mlc(dp
);
1675 if(!forwards_add_zone(fwd
, LDNS_RR_CLASS_IN
, dp
)) {
1676 (void)ssl_printf(ssl
, "error out of memory\n");
1684 /** do the forward_remove command */
1686 do_forward_remove(SSL
* ssl
, struct worker
* worker
, char* args
)
1688 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1691 if(!parse_fs_args(ssl
, args
, &nm
, NULL
, &insecure
, NULL
))
1693 if(insecure
&& worker
->env
.anchors
)
1694 anchors_delete_insecure(worker
->env
.anchors
, LDNS_RR_CLASS_IN
,
1696 forwards_delete_zone(fwd
, LDNS_RR_CLASS_IN
, nm
);
1701 /** do the stub_add command */
1703 do_stub_add(SSL
* ssl
, struct worker
* worker
, char* args
)
1705 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1706 int insecure
= 0, prime
= 0;
1708 struct delegpt
* dp
= NULL
;
1709 if(!parse_fs_args(ssl
, args
, &nm
, &dp
, &insecure
, &prime
))
1711 if(insecure
&& worker
->env
.anchors
) {
1712 if(!anchors_add_insecure(worker
->env
.anchors
, LDNS_RR_CLASS_IN
,
1714 (void)ssl_printf(ssl
, "error out of memory\n");
1715 delegpt_free_mlc(dp
);
1720 if(!forwards_add_stub_hole(fwd
, LDNS_RR_CLASS_IN
, nm
)) {
1721 if(insecure
&& worker
->env
.anchors
)
1722 anchors_delete_insecure(worker
->env
.anchors
,
1723 LDNS_RR_CLASS_IN
, nm
);
1724 (void)ssl_printf(ssl
, "error out of memory\n");
1725 delegpt_free_mlc(dp
);
1729 if(!hints_add_stub(worker
->env
.hints
, LDNS_RR_CLASS_IN
, dp
, !prime
)) {
1730 (void)ssl_printf(ssl
, "error out of memory\n");
1731 forwards_delete_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
);
1742 /** do the stub_remove command */
1744 do_stub_remove(SSL
* ssl
, struct worker
* worker
, char* args
)
1746 struct iter_forwards
* fwd
= worker
->env
.fwds
;
1749 if(!parse_fs_args(ssl
, args
, &nm
, NULL
, &insecure
, NULL
))
1751 if(insecure
&& worker
->env
.anchors
)
1752 anchors_delete_insecure(worker
->env
.anchors
, LDNS_RR_CLASS_IN
,
1754 forwards_delete_stub_hole(fwd
, LDNS_RR_CLASS_IN
, nm
);
1755 hints_delete_stub(worker
->env
.hints
, LDNS_RR_CLASS_IN
, nm
);
1760 /** do the insecure_add command */
1762 do_insecure_add(SSL
* ssl
, struct worker
* worker
, char* arg
)
1767 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1769 if(worker
->env
.anchors
) {
1770 if(!anchors_add_insecure(worker
->env
.anchors
,
1771 LDNS_RR_CLASS_IN
, nm
)) {
1772 (void)ssl_printf(ssl
, "error out of memory\n");
1781 /** do the insecure_remove command */
1783 do_insecure_remove(SSL
* ssl
, struct worker
* worker
, char* arg
)
1788 if(!parse_arg_name(ssl
, arg
, &nm
, &nmlen
, &nmlabs
))
1790 if(worker
->env
.anchors
)
1791 anchors_delete_insecure(worker
->env
.anchors
,
1792 LDNS_RR_CLASS_IN
, nm
);
1797 /** do the status command */
1799 do_status(SSL
* ssl
, struct worker
* worker
)
1803 if(!ssl_printf(ssl
, "version: %s\n", PACKAGE_VERSION
))
1805 if(!ssl_printf(ssl
, "verbosity: %d\n", verbosity
))
1807 if(!ssl_printf(ssl
, "threads: %d\n", worker
->daemon
->num
))
1809 if(!ssl_printf(ssl
, "modules: %d [", worker
->daemon
->mods
.num
))
1811 for(i
=0; i
<worker
->daemon
->mods
.num
; i
++) {
1812 if(!ssl_printf(ssl
, " %s", worker
->daemon
->mods
.mod
[i
]->name
))
1815 if(!ssl_printf(ssl
, " ]\n"))
1817 uptime
= (time_t)time(NULL
) - (time_t)worker
->daemon
->time_boot
.tv_sec
;
1818 if(!ssl_printf(ssl
, "uptime: " ARG_LL
"d seconds\n", (long long)uptime
))
1820 if(!ssl_printf(ssl
, "options:%s%s\n" ,
1821 (worker
->daemon
->reuseport
?" reuseport":""),
1822 (worker
->daemon
->rc
->accept_list
?" control(ssl)":"")))
1824 if(!ssl_printf(ssl
, "unbound (pid %d) is running...\n",
1829 /** get age for the mesh state */
1831 get_mesh_age(struct mesh_state
* m
, char* buf
, size_t len
,
1832 struct module_env
* env
)
1836 struct mesh_reply
* r
= m
->reply_list
;
1837 /* last reply is the oldest */
1840 timeval_subtract(&d
, env
->now_tv
, &r
->start_time
);
1841 snprintf(buf
, len
, ARG_LL
"d.%6.6d",
1842 (long long)d
.tv_sec
, (int)d
.tv_usec
);
1844 snprintf(buf
, len
, "-");
1848 /** get status of a mesh state */
1850 get_mesh_status(struct mesh_area
* mesh
, struct mesh_state
* m
,
1851 char* buf
, size_t len
)
1853 enum module_ext_state s
= m
->s
.ext_state
[m
->s
.curmod
];
1854 const char *modname
= mesh
->mods
.mod
[m
->s
.curmod
]->name
;
1856 if(strcmp(modname
, "iterator") == 0 && s
== module_wait_reply
&&
1857 m
->s
.minfo
[m
->s
.curmod
]) {
1858 /* break into iterator to find out who its waiting for */
1859 struct iter_qstate
* qstate
= (struct iter_qstate
*)
1860 m
->s
.minfo
[m
->s
.curmod
];
1861 struct outbound_list
* ol
= &qstate
->outlist
;
1862 struct outbound_entry
* e
;
1863 snprintf(buf
, len
, "%s wait for", modname
);
1866 if(ol
->first
== NULL
)
1867 snprintf(buf
, len
, " (empty_list)");
1868 for(e
= ol
->first
; e
; e
= e
->next
) {
1869 snprintf(buf
, len
, " ");
1872 addr_to_str(&e
->qsent
->addr
, e
->qsent
->addrlen
,
1877 } else if(s
== module_wait_subquery
) {
1878 /* look in subs from mesh state to see what */
1880 struct mesh_state_ref
* sub
;
1881 snprintf(buf
, len
, "%s wants", modname
);
1884 if(m
->sub_set
.count
== 0)
1885 snprintf(buf
, len
, " (empty_list)");
1886 RBTREE_FOR(sub
, struct mesh_state_ref
*, &m
->sub_set
) {
1887 char* t
= sldns_wire2str_type(sub
->s
->s
.qinfo
.qtype
);
1888 char* c
= sldns_wire2str_class(sub
->s
->s
.qinfo
.qclass
);
1889 dname_str(sub
->s
->s
.qinfo
.qname
, nm
);
1890 snprintf(buf
, len
, " %s %s %s", (t
?t
:"TYPE??"),
1891 (c
?c
:"CLASS??"), nm
);
1898 snprintf(buf
, len
, "%s is %s", modname
, strextstate(s
));
1902 /** do the dump_requestlist command */
1904 do_dump_requestlist(SSL
* ssl
, struct worker
* worker
)
1906 struct mesh_area
* mesh
;
1907 struct mesh_state
* m
;
1911 char statbuf
[10240];
1912 if(!ssl_printf(ssl
, "thread #%d\n", worker
->thread_num
))
1914 if(!ssl_printf(ssl
, "# type cl name seconds module status\n"))
1916 /* show worker mesh contents */
1917 mesh
= worker
->env
.mesh
;
1919 RBTREE_FOR(m
, struct mesh_state
*, &mesh
->all
) {
1920 char* t
= sldns_wire2str_type(m
->s
.qinfo
.qtype
);
1921 char* c
= sldns_wire2str_class(m
->s
.qinfo
.qclass
);
1922 dname_str(m
->s
.qinfo
.qname
, buf
);
1923 get_mesh_age(m
, timebuf
, sizeof(timebuf
), &worker
->env
);
1924 get_mesh_status(mesh
, m
, statbuf
, sizeof(statbuf
));
1925 if(!ssl_printf(ssl
, "%3d %4s %2s %s %s %s\n",
1926 num
, (t
?t
:"TYPE??"), (c
?c
:"CLASS??"), buf
, timebuf
,
1938 /** structure for argument data for dump infra host */
1940 /** the infra cache */
1941 struct infra_cache
* infra
;
1942 /** the SSL connection */
1946 /** ssl failure? stop writing and skip the rest. If the tcp
1947 * connection is broken, and writes fail, we then stop writing. */
1951 /** callback for every host element in the infra cache */
1953 dump_infra_host(struct lruhash_entry
* e
, void* arg
)
1955 struct infra_arg
* a
= (struct infra_arg
*)arg
;
1956 struct infra_key
* k
= (struct infra_key
*)e
->key
;
1957 struct infra_data
* d
= (struct infra_data
*)e
->data
;
1962 addr_to_str(&k
->addr
, k
->addrlen
, ip_str
, sizeof(ip_str
));
1963 dname_str(k
->zonename
, name
);
1964 /* skip expired stuff (only backed off) */
1965 if(d
->ttl
< a
->now
) {
1966 if(d
->rtt
.rto
>= USEFUL_SERVER_TOP_TIMEOUT
) {
1967 if(!ssl_printf(a
->ssl
, "%s %s expired rto %d\n", ip_str
,
1968 name
, d
->rtt
.rto
)) {
1975 if(!ssl_printf(a
->ssl
, "%s %s ttl %lu ping %d var %d rtt %d rto %d "
1976 "tA %d tAAAA %d tother %d "
1977 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d "
1978 "other %d\n", ip_str
, name
, (unsigned long)(d
->ttl
- a
->now
),
1979 d
->rtt
.srtt
, d
->rtt
.rttvar
, rtt_notimeout(&d
->rtt
), d
->rtt
.rto
,
1980 d
->timeout_A
, d
->timeout_AAAA
, d
->timeout_other
,
1981 (int)d
->edns_lame_known
, (int)d
->edns_version
,
1982 (int)(a
->now
<d
->probedelay
?d
->probedelay
-a
->now
:0),
1983 (int)d
->isdnsseclame
, (int)d
->rec_lame
, (int)d
->lame_type_A
,
1984 (int)d
->lame_other
)) {
1990 /** do the dump_infra command */
1992 do_dump_infra(SSL
* ssl
, struct worker
* worker
)
1994 struct infra_arg arg
;
1995 arg
.infra
= worker
->env
.infra_cache
;
1997 arg
.now
= *worker
->env
.now
;
1999 slabhash_traverse(arg
.infra
->hosts
, 0, &dump_infra_host
, (void*)&arg
);
2002 /** do the log_reopen command */
2004 do_log_reopen(SSL
* ssl
, struct worker
* worker
)
2006 struct config_file
* cfg
= worker
->env
.cfg
;
2008 log_init(cfg
->logfile
, cfg
->use_syslog
, cfg
->chrootdir
);
2011 /** do the set_option command */
2013 do_set_option(SSL
* ssl
, struct worker
* worker
, char* arg
)
2016 if(!find_arg2(ssl
, arg
, &arg2
))
2018 if(!config_set_option(worker
->env
.cfg
, arg
, arg2
)) {
2019 (void)ssl_printf(ssl
, "error setting option\n");
2025 /* routine to printout option values over SSL */
2026 void remote_get_opt_ssl(char* line
, void* arg
)
2028 SSL
* ssl
= (SSL
*)arg
;
2029 (void)ssl_printf(ssl
, "%s\n", line
);
2032 /** do the get_option command */
2034 do_get_option(SSL
* ssl
, struct worker
* worker
, char* arg
)
2037 r
= config_get_option(worker
->env
.cfg
, arg
, remote_get_opt_ssl
, ssl
);
2039 (void)ssl_printf(ssl
, "error unknown option\n");
2044 /** do the list_forwards command */
2046 do_list_forwards(SSL
* ssl
, struct worker
* worker
)
2048 /* since its a per-worker structure no locks needed */
2049 struct iter_forwards
* fwds
= worker
->env
.fwds
;
2050 struct iter_forward_zone
* z
;
2051 struct trust_anchor
* a
;
2053 RBTREE_FOR(z
, struct iter_forward_zone
*, fwds
->tree
) {
2054 if(!z
->dp
) continue; /* skip empty marker for stub */
2056 /* see if it is insecure */
2058 if(worker
->env
.anchors
&&
2059 (a
=anchor_find(worker
->env
.anchors
, z
->name
,
2060 z
->namelabs
, z
->namelen
, z
->dclass
))) {
2061 if(!a
->keylist
&& !a
->numDS
&& !a
->numDNSKEY
)
2063 lock_basic_unlock(&a
->lock
);
2066 if(!ssl_print_name_dp(ssl
, (insecure
?"forward +i":"forward"),
2067 z
->name
, z
->dclass
, z
->dp
))
2072 /** do the list_stubs command */
2074 do_list_stubs(SSL
* ssl
, struct worker
* worker
)
2076 struct iter_hints_stub
* z
;
2077 struct trust_anchor
* a
;
2080 RBTREE_FOR(z
, struct iter_hints_stub
*, &worker
->env
.hints
->tree
) {
2082 /* see if it is insecure */
2084 if(worker
->env
.anchors
&&
2085 (a
=anchor_find(worker
->env
.anchors
, z
->node
.name
,
2086 z
->node
.labs
, z
->node
.len
, z
->node
.dclass
))) {
2087 if(!a
->keylist
&& !a
->numDS
&& !a
->numDNSKEY
)
2089 lock_basic_unlock(&a
->lock
);
2092 snprintf(str
, sizeof(str
), "stub %sprime%s",
2093 (z
->noprime
?"no":""), (insecure
?" +i":""));
2094 if(!ssl_print_name_dp(ssl
, str
, z
->node
.name
,
2095 z
->node
.dclass
, z
->dp
))
2100 /** do the list_local_zones command */
2102 do_list_local_zones(SSL
* ssl
, struct worker
* worker
)
2104 struct local_zones
* zones
= worker
->daemon
->local_zones
;
2105 struct local_zone
* z
;
2107 lock_rw_rdlock(&zones
->lock
);
2108 RBTREE_FOR(z
, struct local_zone
*, &zones
->ztree
) {
2109 lock_rw_rdlock(&z
->lock
);
2110 dname_str(z
->name
, buf
);
2111 if(!ssl_printf(ssl
, "%s %s\n", buf
,
2112 local_zone_type2str(z
->type
))) {
2113 /* failure to print */
2114 lock_rw_unlock(&z
->lock
);
2115 lock_rw_unlock(&zones
->lock
);
2118 lock_rw_unlock(&z
->lock
);
2120 lock_rw_unlock(&zones
->lock
);
2123 /** do the list_local_data command */
2125 do_list_local_data(SSL
* ssl
, struct worker
* worker
)
2127 struct local_zones
* zones
= worker
->daemon
->local_zones
;
2128 struct local_zone
* z
;
2129 struct local_data
* d
;
2130 struct local_rrset
* p
;
2131 char* s
= (char*)sldns_buffer_begin(worker
->env
.scratch_buffer
);
2132 size_t slen
= sldns_buffer_capacity(worker
->env
.scratch_buffer
);
2133 lock_rw_rdlock(&zones
->lock
);
2134 RBTREE_FOR(z
, struct local_zone
*, &zones
->ztree
) {
2135 lock_rw_rdlock(&z
->lock
);
2136 RBTREE_FOR(d
, struct local_data
*, &z
->data
) {
2137 for(p
= d
->rrsets
; p
; p
= p
->next
) {
2138 struct packed_rrset_data
* d
=
2139 (struct packed_rrset_data
*)p
->rrset
->entry
.data
;
2141 for(i
=0; i
<d
->count
+ d
->rrsig_count
; i
++) {
2142 if(!packed_rr_to_string(p
->rrset
, i
,
2144 if(!ssl_printf(ssl
, "BADRR\n"))
2147 if(!ssl_printf(ssl
, "%s\n", s
))
2152 lock_rw_unlock(&z
->lock
);
2154 lock_rw_unlock(&zones
->lock
);
2157 /** tell other processes to execute the command */
2159 distribute_cmd(struct daemon_remote
* rc
, SSL
* ssl
, char* cmd
)
2164 /* skip i=0 which is me */
2165 for(i
=1; i
<rc
->worker
->daemon
->num
; i
++) {
2166 worker_send_cmd(rc
->worker
->daemon
->workers
[i
],
2168 if(!tube_write_msg(rc
->worker
->daemon
->workers
[i
]->cmd
,
2169 (uint8_t*)cmd
, strlen(cmd
)+1, 0)) {
2170 ssl_printf(ssl
, "error could not distribute cmd\n");
2176 /** check for name with end-of-string, space or tab after it */
2178 cmdcmp(char* p
, const char* cmd
, size_t len
)
2180 return strncmp(p
,cmd
,len
)==0 && (p
[len
]==0||p
[len
]==' '||p
[len
]=='\t');
2183 /** execute a remote control command */
2185 execute_cmd(struct daemon_remote
* rc
, SSL
* ssl
, char* cmd
,
2186 struct worker
* worker
)
2188 char* p
= skipwhite(cmd
);
2189 /* compare command */
2190 if(cmdcmp(p
, "stop", 4)) {
2193 } else if(cmdcmp(p
, "reload", 6)) {
2196 } else if(cmdcmp(p
, "stats_noreset", 13)) {
2197 do_stats(ssl
, rc
, 0);
2199 } else if(cmdcmp(p
, "stats", 5)) {
2200 do_stats(ssl
, rc
, 1);
2202 } else if(cmdcmp(p
, "status", 6)) {
2203 do_status(ssl
, worker
);
2205 } else if(cmdcmp(p
, "dump_cache", 10)) {
2206 (void)dump_cache(ssl
, worker
);
2208 } else if(cmdcmp(p
, "load_cache", 10)) {
2209 if(load_cache(ssl
, worker
)) send_ok(ssl
);
2211 } else if(cmdcmp(p
, "list_forwards", 13)) {
2212 do_list_forwards(ssl
, worker
);
2214 } else if(cmdcmp(p
, "list_stubs", 10)) {
2215 do_list_stubs(ssl
, worker
);
2217 } else if(cmdcmp(p
, "list_local_zones", 16)) {
2218 do_list_local_zones(ssl
, worker
);
2220 } else if(cmdcmp(p
, "list_local_data", 15)) {
2221 do_list_local_data(ssl
, worker
);
2223 } else if(cmdcmp(p
, "stub_add", 8)) {
2224 /* must always distribute this cmd */
2225 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2226 do_stub_add(ssl
, worker
, skipwhite(p
+8));
2228 } else if(cmdcmp(p
, "stub_remove", 11)) {
2229 /* must always distribute this cmd */
2230 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2231 do_stub_remove(ssl
, worker
, skipwhite(p
+11));
2233 } else if(cmdcmp(p
, "forward_add", 11)) {
2234 /* must always distribute this cmd */
2235 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2236 do_forward_add(ssl
, worker
, skipwhite(p
+11));
2238 } else if(cmdcmp(p
, "forward_remove", 14)) {
2239 /* must always distribute this cmd */
2240 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2241 do_forward_remove(ssl
, worker
, skipwhite(p
+14));
2243 } else if(cmdcmp(p
, "insecure_add", 12)) {
2244 /* must always distribute this cmd */
2245 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2246 do_insecure_add(ssl
, worker
, skipwhite(p
+12));
2248 } else if(cmdcmp(p
, "insecure_remove", 15)) {
2249 /* must always distribute this cmd */
2250 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2251 do_insecure_remove(ssl
, worker
, skipwhite(p
+15));
2253 } else if(cmdcmp(p
, "forward", 7)) {
2254 /* must always distribute this cmd */
2255 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2256 do_forward(ssl
, worker
, skipwhite(p
+7));
2258 } else if(cmdcmp(p
, "flush_stats", 11)) {
2259 /* must always distribute this cmd */
2260 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2261 do_flush_stats(ssl
, worker
);
2263 } else if(cmdcmp(p
, "flush_requestlist", 17)) {
2264 /* must always distribute this cmd */
2265 if(rc
) distribute_cmd(rc
, ssl
, cmd
);
2266 do_flush_requestlist(ssl
, worker
);
2268 } else if(cmdcmp(p
, "lookup", 6)) {
2269 do_lookup(ssl
, worker
, skipwhite(p
+6));
2273 #ifdef THREADS_DISABLED
2274 /* other processes must execute the command as well */
2275 /* commands that should not be distributed, returned above. */
2276 if(rc
) { /* only if this thread is the master (rc) thread */
2277 /* done before the code below, which may split the string */
2278 distribute_cmd(rc
, ssl
, cmd
);
2281 if(cmdcmp(p
, "verbosity", 9)) {
2282 do_verbosity(ssl
, skipwhite(p
+9));
2283 } else if(cmdcmp(p
, "local_zone_remove", 17)) {
2284 do_zone_remove(ssl
, worker
, skipwhite(p
+17));
2285 } else if(cmdcmp(p
, "local_zone", 10)) {
2286 do_zone_add(ssl
, worker
, skipwhite(p
+10));
2287 } else if(cmdcmp(p
, "local_data_remove", 17)) {
2288 do_data_remove(ssl
, worker
, skipwhite(p
+17));
2289 } else if(cmdcmp(p
, "local_data", 10)) {
2290 do_data_add(ssl
, worker
, skipwhite(p
+10));
2291 } else if(cmdcmp(p
, "flush_zone", 10)) {
2292 do_flush_zone(ssl
, worker
, skipwhite(p
+10));
2293 } else if(cmdcmp(p
, "flush_type", 10)) {
2294 do_flush_type(ssl
, worker
, skipwhite(p
+10));
2295 } else if(cmdcmp(p
, "flush_infra", 11)) {
2296 do_flush_infra(ssl
, worker
, skipwhite(p
+11));
2297 } else if(cmdcmp(p
, "flush", 5)) {
2298 do_flush_name(ssl
, worker
, skipwhite(p
+5));
2299 } else if(cmdcmp(p
, "dump_requestlist", 16)) {
2300 do_dump_requestlist(ssl
, worker
);
2301 } else if(cmdcmp(p
, "dump_infra", 10)) {
2302 do_dump_infra(ssl
, worker
);
2303 } else if(cmdcmp(p
, "log_reopen", 10)) {
2304 do_log_reopen(ssl
, worker
);
2305 } else if(cmdcmp(p
, "set_option", 10)) {
2306 do_set_option(ssl
, worker
, skipwhite(p
+10));
2307 } else if(cmdcmp(p
, "get_option", 10)) {
2308 do_get_option(ssl
, worker
, skipwhite(p
+10));
2309 } else if(cmdcmp(p
, "flush_bogus", 11)) {
2310 do_flush_bogus(ssl
, worker
);
2311 } else if(cmdcmp(p
, "flush_negative", 14)) {
2312 do_flush_negative(ssl
, worker
);
2314 (void)ssl_printf(ssl
, "error unknown command '%s'\n", p
);
2319 daemon_remote_exec(struct worker
* worker
)
2321 /* read the cmd string */
2322 uint8_t* msg
= NULL
;
2324 if(!tube_read_msg(worker
->cmd
, &msg
, &len
, 0)) {
2325 log_err("daemon_remote_exec: tube_read_msg failed");
2328 verbose(VERB_ALGO
, "remote exec distributed: %s", (char*)msg
);
2329 execute_cmd(NULL
, NULL
, (char*)msg
, worker
);
2333 /** handle remote control request */
2335 handle_req(struct daemon_remote
* rc
, struct rc_state
* s
, SSL
* ssl
)
2342 /* makes it possible to set the socket blocking again. */
2343 /* basically removes it from winsock_event ... */
2344 WSAEventSelect(s
->c
->fd
, NULL
, 0);
2346 fd_set_block(s
->c
->fd
);
2348 /* try to read magic UBCT[version]_space_ string */
2350 if((r
=SSL_read(ssl
, magic
, (int)sizeof(magic
)-1)) <= 0) {
2351 if(SSL_get_error(ssl
, r
) == SSL_ERROR_ZERO_RETURN
)
2353 log_crypto_err("could not SSL_read");
2357 if( r
!= 6 || strncmp(magic
, "UBCT", 4) != 0) {
2358 verbose(VERB_QUERY
, "control connection has bad magic string");
2359 /* probably wrong tool connected, ignore it completely */
2363 /* read the command line */
2364 if(!ssl_read_line(ssl
, buf
, sizeof(buf
))) {
2367 snprintf(pre
, sizeof(pre
), "UBCT%d ", UNBOUND_CONTROL_VERSION
);
2368 if(strcmp(magic
, pre
) != 0) {
2369 verbose(VERB_QUERY
, "control connection had bad "
2370 "version %s, cmd: %s", magic
, buf
);
2371 ssl_printf(ssl
, "error version mismatch\n");
2374 verbose(VERB_DETAIL
, "control cmd: %s", buf
);
2376 /* figure out what to do */
2377 execute_cmd(rc
, ssl
, buf
, rc
->worker
);
2380 int remote_control_callback(struct comm_point
* c
, void* arg
, int err
,
2381 struct comm_reply
* ATTR_UNUSED(rep
))
2383 struct rc_state
* s
= (struct rc_state
*)arg
;
2384 struct daemon_remote
* rc
= s
->rc
;
2386 if(err
!= NETEVENT_NOERROR
) {
2387 if(err
==NETEVENT_TIMEOUT
)
2388 log_err("remote control timed out");
2392 /* (continue to) setup the SSL connection */
2394 r
= SSL_do_handshake(s
->ssl
);
2396 int r2
= SSL_get_error(s
->ssl
, r
);
2397 if(r2
== SSL_ERROR_WANT_READ
) {
2398 if(s
->shake_state
== rc_hs_read
) {
2399 /* try again later */
2402 s
->shake_state
= rc_hs_read
;
2403 comm_point_listen_for_rw(c
, 1, 0);
2405 } else if(r2
== SSL_ERROR_WANT_WRITE
) {
2406 if(s
->shake_state
== rc_hs_write
) {
2407 /* try again later */
2410 s
->shake_state
= rc_hs_write
;
2411 comm_point_listen_for_rw(c
, 0, 1);
2415 log_err("remote control connection closed prematurely");
2416 log_addr(1, "failed connection from",
2417 &s
->c
->repinfo
.addr
, s
->c
->repinfo
.addrlen
);
2418 log_crypto_err("remote control failed ssl");
2423 s
->shake_state
= rc_none
;
2425 /* once handshake has completed, check authentication */
2426 if(SSL_get_verify_result(s
->ssl
) == X509_V_OK
) {
2427 X509
* x
= SSL_get_peer_certificate(s
->ssl
);
2429 verbose(VERB_DETAIL
, "remote control connection "
2430 "provided no client certificate");
2434 verbose(VERB_ALGO
, "remote control connection authenticated");
2437 verbose(VERB_DETAIL
, "remote control connection failed to "
2438 "authenticate with client certificate");
2443 /* if OK start to actually handle the request */
2444 handle_req(rc
, s
, s
->ssl
);
2446 verbose(VERB_ALGO
, "remote control operation completed");