Home | History | Annotate | Line # | Download | only in smallapp
unbound-control.c revision 1.1.1.2
      1      1.1  christos /*
      2      1.1  christos  * checkconf/unbound-control.c - remote control utility for unbound.
      3      1.1  christos  *
      4      1.1  christos  * Copyright (c) 2008, NLnet Labs. All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * This software is open source.
      7      1.1  christos  *
      8      1.1  christos  * Redistribution and use in source and binary forms, with or without
      9      1.1  christos  * modification, are permitted provided that the following conditions
     10      1.1  christos  * are met:
     11      1.1  christos  *
     12      1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13      1.1  christos  * this list of conditions and the following disclaimer.
     14      1.1  christos  *
     15      1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16      1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17      1.1  christos  * and/or other materials provided with the distribution.
     18      1.1  christos  *
     19      1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20      1.1  christos  * be used to endorse or promote products derived from this software without
     21      1.1  christos  * specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25      1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26      1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27      1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28      1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29      1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30      1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31      1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32      1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33      1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34      1.1  christos  */
     35      1.1  christos 
     36      1.1  christos /**
     37      1.1  christos  * \file
     38      1.1  christos  *
     39      1.1  christos  * The remote control utility contacts the unbound server over ssl and
     40      1.1  christos  * sends the command, receives the answer, and displays the result
     41      1.1  christos  * from the commandline.
     42      1.1  christos  */
     43      1.1  christos 
     44      1.1  christos #include "config.h"
     45      1.1  christos #ifdef HAVE_GETOPT_H
     46      1.1  christos #include <getopt.h>
     47      1.1  christos #endif
     48      1.1  christos #ifdef HAVE_OPENSSL_SSL_H
     49      1.1  christos #include <openssl/ssl.h>
     50      1.1  christos #endif
     51      1.1  christos #ifdef HAVE_OPENSSL_ERR_H
     52      1.1  christos #include <openssl/err.h>
     53      1.1  christos #endif
     54      1.1  christos #ifdef HAVE_OPENSSL_RAND_H
     55      1.1  christos #include <openssl/rand.h>
     56      1.1  christos #endif
     57      1.1  christos #include "util/log.h"
     58      1.1  christos #include "util/config_file.h"
     59      1.1  christos #include "util/locks.h"
     60      1.1  christos #include "util/net_help.h"
     61  1.1.1.2  christos #include "util/shm_side/shm_main.h"
     62  1.1.1.2  christos #include "daemon/stats.h"
     63  1.1.1.2  christos #include "sldns/wire2str.h"
     64  1.1.1.2  christos #include "sldns/pkthdr.h"
     65      1.1  christos 
     66  1.1.1.2  christos #ifdef HAVE_SYS_IPC_H
     67  1.1.1.2  christos #include "sys/ipc.h"
     68  1.1.1.2  christos #endif
     69  1.1.1.2  christos #ifdef HAVE_SYS_SHM_H
     70  1.1.1.2  christos #include "sys/shm.h"
     71  1.1.1.2  christos #endif
     72      1.1  christos #ifdef HAVE_SYS_UN_H
     73      1.1  christos #include <sys/un.h>
     74      1.1  christos #endif
     75      1.1  christos 
     76      1.1  christos /** Give unbound-control usage, and exit (1). */
     77      1.1  christos static void
     78  1.1.1.2  christos usage(void)
     79      1.1  christos {
     80      1.1  christos 	printf("Usage:	unbound-control [options] command\n");
     81      1.1  christos 	printf("	Remote control utility for unbound server.\n");
     82      1.1  christos 	printf("Options:\n");
     83      1.1  christos 	printf("  -c file	config file, default is %s\n", CONFIGFILE);
     84      1.1  christos 	printf("  -s ip[@port]	server address, if omitted config is used.\n");
     85      1.1  christos 	printf("  -q		quiet (don't print anything if it works ok).\n");
     86      1.1  christos 	printf("  -h		show this usage help.\n");
     87      1.1  christos 	printf("Commands:\n");
     88      1.1  christos 	printf("  start				start server; runs unbound(8)\n");
     89      1.1  christos 	printf("  stop				stops the server\n");
     90      1.1  christos 	printf("  reload			reloads the server\n");
     91      1.1  christos 	printf("  				(this flushes data, stats, requestlist)\n");
     92      1.1  christos 	printf("  stats				print statistics\n");
     93      1.1  christos 	printf("  stats_noreset			peek at statistics\n");
     94  1.1.1.2  christos #ifdef HAVE_SHMGET
     95  1.1.1.2  christos 	printf("  stats_shm			print statistics using shm\n");
     96  1.1.1.2  christos #endif
     97      1.1  christos 	printf("  status			display status of server\n");
     98      1.1  christos 	printf("  verbosity <number>		change logging detail\n");
     99      1.1  christos 	printf("  log_reopen			close and open the logfile\n");
    100      1.1  christos 	printf("  local_zone <name> <type>	add new local zone\n");
    101      1.1  christos 	printf("  local_zone_remove <name>	remove local zone and its contents\n");
    102      1.1  christos 	printf("  local_data <RR data...>	add local data, for example\n");
    103      1.1  christos 	printf("				local_data www.example.com A 192.0.2.1\n");
    104      1.1  christos 	printf("  local_data_remove <name>	remove local RR data from name\n");
    105  1.1.1.2  christos 	printf("  local_zones, local_zones_remove, local_datas, local_datas_remove\n");
    106  1.1.1.2  christos 	printf("  				same, but read list from stdin\n");
    107  1.1.1.2  christos 	printf("  				(one entry per line).\n");
    108      1.1  christos 	printf("  dump_cache			print cache to stdout\n");
    109      1.1  christos 	printf("  load_cache			load cache from stdin\n");
    110      1.1  christos 	printf("  lookup <name>			print nameservers for name\n");
    111      1.1  christos 	printf("  flush <name>			flushes common types for name from cache\n");
    112      1.1  christos 	printf("  				types:  A, AAAA, MX, PTR, NS,\n");
    113      1.1  christos 	printf("					SOA, CNAME, DNAME, SRV, NAPTR\n");
    114      1.1  christos 	printf("  flush_type <name> <type>	flush name, type from cache\n");
    115      1.1  christos 	printf("  flush_zone <name>		flush everything at or under name\n");
    116      1.1  christos 	printf("  				from rr and dnssec caches\n");
    117      1.1  christos 	printf("  flush_bogus			flush all bogus data\n");
    118      1.1  christos 	printf("  flush_negative		flush all negative data\n");
    119      1.1  christos 	printf("  flush_stats 			flush statistics, make zero\n");
    120      1.1  christos 	printf("  flush_requestlist 		drop queries that are worked on\n");
    121      1.1  christos 	printf("  dump_requestlist		show what is worked on by first thread\n");
    122      1.1  christos 	printf("  flush_infra [all | ip] 	remove ping, edns for one IP or all\n");
    123      1.1  christos 	printf("  dump_infra			show ping and edns entries\n");
    124      1.1  christos 	printf("  set_option opt: val		set option to value, no reload\n");
    125      1.1  christos 	printf("  get_option opt		get option value\n");
    126      1.1  christos 	printf("  list_stubs			list stub-zones and root hints in use\n");
    127      1.1  christos 	printf("  list_forwards			list forward-zones in use\n");
    128      1.1  christos 	printf("  list_insecure			list domain-insecure zones\n");
    129      1.1  christos 	printf("  list_local_zones		list local-zones in use\n");
    130      1.1  christos 	printf("  list_local_data		list local-data RRs in use\n");
    131      1.1  christos 	printf("  insecure_add zone 		add domain-insecure zone\n");
    132      1.1  christos 	printf("  insecure_remove zone		remove domain-insecure zone\n");
    133      1.1  christos 	printf("  forward_add [+i] zone addr..	add forward-zone with servers\n");
    134      1.1  christos 	printf("  forward_remove [+i] zone	remove forward zone\n");
    135      1.1  christos 	printf("  stub_add [+ip] zone addr..	add stub-zone with servers\n");
    136      1.1  christos 	printf("  stub_remove [+i] zone		remove stub zone\n");
    137      1.1  christos 	printf("		+i		also do dnssec insecure point\n");
    138      1.1  christos 	printf("		+p		set stub to use priming\n");
    139      1.1  christos 	printf("  forward [off | addr ...]	without arg show forward setup\n");
    140      1.1  christos 	printf("				or off to turn off root forwarding\n");
    141      1.1  christos 	printf("				or give list of ip addresses\n");
    142      1.1  christos 	printf("  ratelimit_list [+a]		list ratelimited domains\n");
    143  1.1.1.2  christos 	printf("  ip_ratelimit_list [+a]	list ratelimited ip addresses\n");
    144      1.1  christos 	printf("		+a		list all, also not ratelimited\n");
    145  1.1.1.2  christos 	printf("  view_list_local_zones	view	list local-zones in view\n");
    146  1.1.1.2  christos 	printf("  view_list_local_data	view	list local-data RRs in view\n");
    147  1.1.1.2  christos 	printf("  view_local_zone view name type  	add local-zone in view\n");
    148  1.1.1.2  christos 	printf("  view_local_zone_remove view name  	remove local-zone in view\n");
    149  1.1.1.2  christos 	printf("  view_local_data view RR...		add local-data in view\n");
    150  1.1.1.2  christos 	printf("  view_local_data_remove view name  	remove local-data in view\n");
    151      1.1  christos 	printf("Version %s\n", PACKAGE_VERSION);
    152      1.1  christos 	printf("BSD licensed, see LICENSE in source package for details.\n");
    153      1.1  christos 	printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
    154      1.1  christos 	exit(1);
    155      1.1  christos }
    156      1.1  christos 
    157  1.1.1.2  christos #ifdef HAVE_SHMGET
    158  1.1.1.2  christos /** what to put on statistics lines between var and value, ": " or "=" */
    159  1.1.1.2  christos #define SQ "="
    160  1.1.1.2  christos /** if true, inhibits a lot of =0 lines from the stats output */
    161  1.1.1.2  christos static const int inhibit_zero = 1;
    162  1.1.1.2  christos /** divide sum of timers to get average */
    163  1.1.1.2  christos static void
    164  1.1.1.2  christos timeval_divide(struct timeval* avg, const struct timeval* sum, long long d)
    165  1.1.1.2  christos {
    166  1.1.1.2  christos #ifndef S_SPLINT_S
    167  1.1.1.2  christos 	size_t leftover;
    168  1.1.1.2  christos 	if(d == 0) {
    169  1.1.1.2  christos 		avg->tv_sec = 0;
    170  1.1.1.2  christos 		avg->tv_usec = 0;
    171  1.1.1.2  christos 		return;
    172  1.1.1.2  christos 	}
    173  1.1.1.2  christos 	avg->tv_sec = sum->tv_sec / d;
    174  1.1.1.2  christos 	avg->tv_usec = sum->tv_usec / d;
    175  1.1.1.2  christos 	/* handle fraction from seconds divide */
    176  1.1.1.2  christos 	leftover = sum->tv_sec - avg->tv_sec*d;
    177  1.1.1.2  christos 	avg->tv_usec += (leftover*1000000)/d;
    178  1.1.1.2  christos #endif
    179  1.1.1.2  christos }
    180  1.1.1.2  christos 
    181  1.1.1.2  christos /** print unsigned long stats value */
    182  1.1.1.2  christos #define PR_UL_NM(str, var) printf("%s."str SQ"%lu\n", nm, (unsigned long)(var));
    183  1.1.1.2  christos #define PR_UL(str, var) printf(str SQ"%lu\n", (unsigned long)(var));
    184  1.1.1.2  christos #define PR_UL_SUB(str, nm, var) printf(str".%s"SQ"%lu\n", nm, (unsigned long)(var));
    185  1.1.1.2  christos #define PR_TIMEVAL(str, var) printf(str SQ ARG_LL "d.%6.6d\n", \
    186  1.1.1.2  christos 	(long long)var.tv_sec, (int)var.tv_usec);
    187  1.1.1.2  christos #define PR_STATSTIME(str, var) printf(str SQ ARG_LL "d.%6.6d\n", \
    188  1.1.1.2  christos 	(long long)var ## _sec, (int)var ## _usec);
    189  1.1.1.2  christos #define PR_LL(str, var) printf(str SQ ARG_LL"d\n", (long long)(var));
    190  1.1.1.2  christos 
    191  1.1.1.2  christos /** print stat block */
    192  1.1.1.2  christos static void pr_stats(const char* nm, struct ub_stats_info* s)
    193  1.1.1.2  christos {
    194  1.1.1.2  christos 	struct timeval sumwait, avg;
    195  1.1.1.2  christos 	PR_UL_NM("num.queries", s->svr.num_queries);
    196  1.1.1.2  christos 	PR_UL_NM("num.queries_ip_ratelimited",
    197  1.1.1.2  christos 		s->svr.num_queries_ip_ratelimited);
    198  1.1.1.2  christos 	PR_UL_NM("num.cachehits",
    199  1.1.1.2  christos 		s->svr.num_queries - s->svr.num_queries_missed_cache);
    200  1.1.1.2  christos 	PR_UL_NM("num.cachemiss", s->svr.num_queries_missed_cache);
    201  1.1.1.2  christos 	PR_UL_NM("num.prefetch", s->svr.num_queries_prefetch);
    202  1.1.1.2  christos 	PR_UL_NM("num.zero_ttl", s->svr.zero_ttl_responses);
    203  1.1.1.2  christos 	PR_UL_NM("num.recursivereplies", s->mesh_replies_sent);
    204  1.1.1.2  christos #ifdef USE_DNSCRYPT
    205  1.1.1.2  christos     PR_UL_NM("num.dnscrypt.crypted", s->svr.num_query_dnscrypt_crypted);
    206  1.1.1.2  christos     PR_UL_NM("num.dnscrypt.cert", s->svr.num_query_dnscrypt_cert);
    207  1.1.1.2  christos     PR_UL_NM("num.dnscrypt.cleartext", s->svr.num_query_dnscrypt_cleartext);
    208  1.1.1.2  christos     PR_UL_NM("num.dnscrypt.malformed",
    209  1.1.1.2  christos              s->svr.num_query_dnscrypt_crypted_malformed);
    210  1.1.1.2  christos #endif /* USE_DNSCRYPT */
    211  1.1.1.2  christos 	printf("%s.requestlist.avg"SQ"%g\n", nm,
    212  1.1.1.2  christos 		(s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
    213  1.1.1.2  christos 			(double)s->svr.sum_query_list_size/
    214  1.1.1.2  christos 			(double)(s->svr.num_queries_missed_cache+
    215  1.1.1.2  christos 			s->svr.num_queries_prefetch) : 0.0);
    216  1.1.1.2  christos 	PR_UL_NM("requestlist.max", s->svr.max_query_list_size);
    217  1.1.1.2  christos 	PR_UL_NM("requestlist.overwritten", s->mesh_jostled);
    218  1.1.1.2  christos 	PR_UL_NM("requestlist.exceeded", s->mesh_dropped);
    219  1.1.1.2  christos 	PR_UL_NM("requestlist.current.all", s->mesh_num_states);
    220  1.1.1.2  christos 	PR_UL_NM("requestlist.current.user", s->mesh_num_reply_states);
    221  1.1.1.2  christos #ifndef S_SPLINT_S
    222  1.1.1.2  christos 	sumwait.tv_sec = s->mesh_replies_sum_wait_sec;
    223  1.1.1.2  christos 	sumwait.tv_usec = s->mesh_replies_sum_wait_usec;
    224  1.1.1.2  christos #endif
    225  1.1.1.2  christos 	timeval_divide(&avg, &sumwait, s->mesh_replies_sent);
    226  1.1.1.2  christos 	printf("%s.", nm);
    227  1.1.1.2  christos 	PR_TIMEVAL("recursion.time.avg", avg);
    228  1.1.1.2  christos 	printf("%s.recursion.time.median"SQ"%g\n", nm, s->mesh_time_median);
    229  1.1.1.2  christos 	PR_UL_NM("tcpusage", s->svr.tcp_accept_usage);
    230  1.1.1.2  christos }
    231  1.1.1.2  christos 
    232  1.1.1.2  christos /** print uptime */
    233  1.1.1.2  christos static void print_uptime(struct ub_shm_stat_info* shm_stat)
    234  1.1.1.2  christos {
    235  1.1.1.2  christos 	PR_STATSTIME("time.now", shm_stat->time.now);
    236  1.1.1.2  christos 	PR_STATSTIME("time.up", shm_stat->time.up);
    237  1.1.1.2  christos 	PR_STATSTIME("time.elapsed", shm_stat->time.elapsed);
    238  1.1.1.2  christos }
    239  1.1.1.2  christos 
    240  1.1.1.2  christos /** print memory usage */
    241  1.1.1.2  christos static void print_mem(struct ub_shm_stat_info* shm_stat)
    242  1.1.1.2  christos {
    243  1.1.1.2  christos 	PR_LL("mem.cache.rrset", shm_stat->mem.rrset);
    244  1.1.1.2  christos 	PR_LL("mem.cache.message", shm_stat->mem.msg);
    245  1.1.1.2  christos 	PR_LL("mem.mod.iterator", shm_stat->mem.iter);
    246  1.1.1.2  christos 	PR_LL("mem.mod.validator", shm_stat->mem.val);
    247  1.1.1.2  christos 	PR_LL("mem.mod.respip", shm_stat->mem.respip);
    248  1.1.1.2  christos #ifdef CLIENT_SUBNET
    249  1.1.1.2  christos 	PR_LL("mem.mod.subnet", shm_stat->mem.subnet);
    250  1.1.1.2  christos #endif
    251  1.1.1.2  christos #ifdef USE_IPSECMOD
    252  1.1.1.2  christos 	PR_LL("mem.mod.ipsecmod", shm_stat->mem.ipsecmod);
    253  1.1.1.2  christos #endif
    254  1.1.1.2  christos #ifdef USE_DNSCRYPT
    255  1.1.1.2  christos 	PR_LL("mem.cache.dnscrypt_shared_secret",
    256  1.1.1.2  christos 		shm_stat->mem.dnscrypt_shared_secret);
    257  1.1.1.2  christos 	PR_LL("mem.cache.dnscrypt_nonce",
    258  1.1.1.2  christos 		shm_stat->mem.dnscrypt_nonce);
    259  1.1.1.2  christos #endif
    260  1.1.1.2  christos }
    261  1.1.1.2  christos 
    262  1.1.1.2  christos /** print histogram */
    263  1.1.1.2  christos static void print_hist(struct ub_stats_info* s)
    264  1.1.1.2  christos {
    265  1.1.1.2  christos 	struct timehist* hist;
    266  1.1.1.2  christos 	size_t i;
    267  1.1.1.2  christos 	hist = timehist_setup();
    268  1.1.1.2  christos 	if(!hist)
    269  1.1.1.2  christos 		fatal_exit("out of memory");
    270  1.1.1.2  christos 	timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
    271  1.1.1.2  christos 	for(i=0; i<hist->num; i++) {
    272  1.1.1.2  christos 		printf("histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
    273  1.1.1.2  christos 			(int)hist->buckets[i].lower.tv_sec,
    274  1.1.1.2  christos 			(int)hist->buckets[i].lower.tv_usec,
    275  1.1.1.2  christos 			(int)hist->buckets[i].upper.tv_sec,
    276  1.1.1.2  christos 			(int)hist->buckets[i].upper.tv_usec,
    277  1.1.1.2  christos 			(unsigned long)hist->buckets[i].count);
    278  1.1.1.2  christos 	}
    279  1.1.1.2  christos 	timehist_delete(hist);
    280  1.1.1.2  christos }
    281  1.1.1.2  christos 
    282  1.1.1.2  christos /** print extended */
    283  1.1.1.2  christos static void print_extended(struct ub_stats_info* s)
    284  1.1.1.2  christos {
    285  1.1.1.2  christos 	int i;
    286  1.1.1.2  christos 	char nm[16];
    287  1.1.1.2  christos 
    288  1.1.1.2  christos 	/* TYPE */
    289  1.1.1.2  christos 	for(i=0; i<UB_STATS_QTYPE_NUM; i++) {
    290  1.1.1.2  christos 		if(inhibit_zero && s->svr.qtype[i] == 0)
    291  1.1.1.2  christos 			continue;
    292  1.1.1.2  christos 		sldns_wire2str_type_buf((uint16_t)i, nm, sizeof(nm));
    293  1.1.1.2  christos 		PR_UL_SUB("num.query.type", nm, s->svr.qtype[i]);
    294  1.1.1.2  christos 	}
    295  1.1.1.2  christos 	if(!inhibit_zero || s->svr.qtype_big) {
    296  1.1.1.2  christos 		PR_UL("num.query.type.other", s->svr.qtype_big);
    297  1.1.1.2  christos 	}
    298  1.1.1.2  christos 
    299  1.1.1.2  christos 	/* CLASS */
    300  1.1.1.2  christos 	for(i=0; i<UB_STATS_QCLASS_NUM; i++) {
    301  1.1.1.2  christos 		if(inhibit_zero && s->svr.qclass[i] == 0)
    302  1.1.1.2  christos 			continue;
    303  1.1.1.2  christos 		sldns_wire2str_class_buf((uint16_t)i, nm, sizeof(nm));
    304  1.1.1.2  christos 		PR_UL_SUB("num.query.class", nm, s->svr.qclass[i]);
    305  1.1.1.2  christos 	}
    306  1.1.1.2  christos 	if(!inhibit_zero || s->svr.qclass_big) {
    307  1.1.1.2  christos 		PR_UL("num.query.class.other", s->svr.qclass_big);
    308  1.1.1.2  christos 	}
    309  1.1.1.2  christos 
    310  1.1.1.2  christos 	/* OPCODE */
    311  1.1.1.2  christos 	for(i=0; i<UB_STATS_OPCODE_NUM; i++) {
    312  1.1.1.2  christos 		if(inhibit_zero && s->svr.qopcode[i] == 0)
    313  1.1.1.2  christos 			continue;
    314  1.1.1.2  christos 		sldns_wire2str_opcode_buf(i, nm, sizeof(nm));
    315  1.1.1.2  christos 		PR_UL_SUB("num.query.opcode", nm, s->svr.qopcode[i]);
    316  1.1.1.2  christos 	}
    317  1.1.1.2  christos 
    318  1.1.1.2  christos 	/* transport */
    319  1.1.1.2  christos 	PR_UL("num.query.tcp", s->svr.qtcp);
    320  1.1.1.2  christos 	PR_UL("num.query.tcpout", s->svr.qtcp_outgoing);
    321  1.1.1.2  christos 	PR_UL("num.query.ipv6", s->svr.qipv6);
    322  1.1.1.2  christos 
    323  1.1.1.2  christos 	/* flags */
    324  1.1.1.2  christos 	PR_UL("num.query.flags.QR", s->svr.qbit_QR);
    325  1.1.1.2  christos 	PR_UL("num.query.flags.AA", s->svr.qbit_AA);
    326  1.1.1.2  christos 	PR_UL("num.query.flags.TC", s->svr.qbit_TC);
    327  1.1.1.2  christos 	PR_UL("num.query.flags.RD", s->svr.qbit_RD);
    328  1.1.1.2  christos 	PR_UL("num.query.flags.RA", s->svr.qbit_RA);
    329  1.1.1.2  christos 	PR_UL("num.query.flags.Z", s->svr.qbit_Z);
    330  1.1.1.2  christos 	PR_UL("num.query.flags.AD", s->svr.qbit_AD);
    331  1.1.1.2  christos 	PR_UL("num.query.flags.CD", s->svr.qbit_CD);
    332  1.1.1.2  christos 	PR_UL("num.query.edns.present", s->svr.qEDNS);
    333  1.1.1.2  christos 	PR_UL("num.query.edns.DO", s->svr.qEDNS_DO);
    334  1.1.1.2  christos 
    335  1.1.1.2  christos 	/* RCODE */
    336  1.1.1.2  christos 	for(i=0; i<UB_STATS_RCODE_NUM; i++) {
    337  1.1.1.2  christos 		/* Always include RCODEs 0-5 */
    338  1.1.1.2  christos 		if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
    339  1.1.1.2  christos 			continue;
    340  1.1.1.2  christos 		sldns_wire2str_rcode_buf(i, nm, sizeof(nm));
    341  1.1.1.2  christos 		PR_UL_SUB("num.answer.rcode", nm, s->svr.ans_rcode[i]);
    342  1.1.1.2  christos 	}
    343  1.1.1.2  christos 	if(!inhibit_zero || s->svr.ans_rcode_nodata) {
    344  1.1.1.2  christos 		PR_UL("num.answer.rcode.nodata", s->svr.ans_rcode_nodata);
    345  1.1.1.2  christos 	}
    346  1.1.1.2  christos 	/* iteration */
    347  1.1.1.2  christos 	PR_UL("num.query.ratelimited", s->svr.queries_ratelimited);
    348  1.1.1.2  christos 	/* validation */
    349  1.1.1.2  christos 	PR_UL("num.answer.secure", s->svr.ans_secure);
    350  1.1.1.2  christos 	PR_UL("num.answer.bogus", s->svr.ans_bogus);
    351  1.1.1.2  christos 	PR_UL("num.rrset.bogus", s->svr.rrset_bogus);
    352  1.1.1.2  christos 	/* threat detection */
    353  1.1.1.2  christos 	PR_UL("unwanted.queries", s->svr.unwanted_queries);
    354  1.1.1.2  christos 	PR_UL("unwanted.replies", s->svr.unwanted_replies);
    355  1.1.1.2  christos 	/* cache counts */
    356  1.1.1.2  christos 	PR_UL("msg.cache.count", s->svr.msg_cache_count);
    357  1.1.1.2  christos 	PR_UL("rrset.cache.count", s->svr.rrset_cache_count);
    358  1.1.1.2  christos 	PR_UL("infra.cache.count", s->svr.infra_cache_count);
    359  1.1.1.2  christos 	PR_UL("key.cache.count", s->svr.key_cache_count);
    360  1.1.1.2  christos #ifdef USE_DNSCRYPT
    361  1.1.1.2  christos 	PR_UL("dnscrypt_shared_secret.cache.count",
    362  1.1.1.2  christos 			 s->svr.shared_secret_cache_count);
    363  1.1.1.2  christos 	PR_UL("num.query.dnscrypt.shared_secret.cachemiss",
    364  1.1.1.2  christos 			 s->svr.num_query_dnscrypt_secret_missed_cache);
    365  1.1.1.2  christos 	PR_UL("dnscrypt_nonce.cache.count", s->svr.nonce_cache_count);
    366  1.1.1.2  christos 	PR_UL("num.query.dnscrypt.replay",
    367  1.1.1.2  christos 			 s->svr.num_query_dnscrypt_replay);
    368  1.1.1.2  christos #endif /* USE_DNSCRYPT */
    369  1.1.1.2  christos }
    370  1.1.1.2  christos 
    371  1.1.1.2  christos /** print statistics out of memory structures */
    372  1.1.1.2  christos static void do_stats_shm(struct config_file* cfg, struct ub_stats_info* stats,
    373  1.1.1.2  christos 	struct ub_shm_stat_info* shm_stat)
    374  1.1.1.2  christos {
    375  1.1.1.2  christos 	int i;
    376  1.1.1.2  christos 	char nm[32];
    377  1.1.1.2  christos 	for(i=0; i<cfg->num_threads; i++) {
    378  1.1.1.2  christos 		snprintf(nm, sizeof(nm), "thread%d", i);
    379  1.1.1.2  christos 		pr_stats(nm, &stats[i+1]);
    380  1.1.1.2  christos 	}
    381  1.1.1.2  christos 	pr_stats("total", &stats[0]);
    382  1.1.1.2  christos 	print_uptime(shm_stat);
    383  1.1.1.2  christos 	if(cfg->stat_extended) {
    384  1.1.1.2  christos 		print_mem(shm_stat);
    385  1.1.1.2  christos 		print_hist(stats);
    386  1.1.1.2  christos 		print_extended(stats);
    387  1.1.1.2  christos 	}
    388  1.1.1.2  christos }
    389  1.1.1.2  christos #endif /* HAVE_SHMGET */
    390  1.1.1.2  christos 
    391  1.1.1.2  christos /** print statistics from shm memory segment */
    392  1.1.1.2  christos static void print_stats_shm(const char* cfgfile)
    393  1.1.1.2  christos {
    394  1.1.1.2  christos #ifdef HAVE_SHMGET
    395  1.1.1.2  christos 	struct config_file* cfg;
    396  1.1.1.2  christos 	struct ub_stats_info* stats;
    397  1.1.1.2  christos 	struct ub_shm_stat_info* shm_stat;
    398  1.1.1.2  christos 	int id_ctl, id_arr;
    399  1.1.1.2  christos 	/* read config */
    400  1.1.1.2  christos 	if(!(cfg = config_create()))
    401  1.1.1.2  christos 		fatal_exit("out of memory");
    402  1.1.1.2  christos 	if(!config_read(cfg, cfgfile, NULL))
    403  1.1.1.2  christos 		fatal_exit("could not read config file");
    404  1.1.1.2  christos 	/* get shm segments */
    405  1.1.1.2  christos 	id_ctl = shmget(cfg->shm_key, sizeof(int), SHM_R|SHM_W);
    406  1.1.1.2  christos 	if(id_ctl == -1) {
    407  1.1.1.2  christos 		fatal_exit("shmget(%d): %s", cfg->shm_key, strerror(errno));
    408  1.1.1.2  christos 	}
    409  1.1.1.2  christos 	id_arr = shmget(cfg->shm_key+1, sizeof(int), SHM_R|SHM_W);
    410  1.1.1.2  christos 	if(id_arr == -1) {
    411  1.1.1.2  christos 		fatal_exit("shmget(%d): %s", cfg->shm_key+1, strerror(errno));
    412  1.1.1.2  christos 	}
    413  1.1.1.2  christos 	shm_stat = (struct ub_shm_stat_info*)shmat(id_ctl, NULL, 0);
    414  1.1.1.2  christos 	if(shm_stat == (void*)-1) {
    415  1.1.1.2  christos 		fatal_exit("shmat(%d): %s", id_ctl, strerror(errno));
    416  1.1.1.2  christos 	}
    417  1.1.1.2  christos 	stats = (struct ub_stats_info*)shmat(id_arr, NULL, 0);
    418  1.1.1.2  christos 	if(stats == (void*)-1) {
    419  1.1.1.2  christos 		fatal_exit("shmat(%d): %s", id_arr, strerror(errno));
    420  1.1.1.2  christos 	}
    421  1.1.1.2  christos 
    422  1.1.1.2  christos 	/* print the stats */
    423  1.1.1.2  christos 	do_stats_shm(cfg, stats, shm_stat);
    424  1.1.1.2  christos 
    425  1.1.1.2  christos 	/* shutdown */
    426  1.1.1.2  christos 	shmdt(shm_stat);
    427  1.1.1.2  christos 	shmdt(stats);
    428  1.1.1.2  christos 	config_delete(cfg);
    429  1.1.1.2  christos #else
    430  1.1.1.2  christos 	(void)cfgfile;
    431  1.1.1.2  christos #endif /* HAVE_SHMGET */
    432  1.1.1.2  christos }
    433  1.1.1.2  christos 
    434      1.1  christos /** exit with ssl error */
    435      1.1  christos static void ssl_err(const char* s)
    436      1.1  christos {
    437      1.1  christos 	fprintf(stderr, "error: %s\n", s);
    438      1.1  christos 	ERR_print_errors_fp(stderr);
    439      1.1  christos 	exit(1);
    440      1.1  christos }
    441      1.1  christos 
    442      1.1  christos /** setup SSL context */
    443      1.1  christos static SSL_CTX*
    444      1.1  christos setup_ctx(struct config_file* cfg)
    445      1.1  christos {
    446      1.1  christos 	char* s_cert=NULL, *c_key=NULL, *c_cert=NULL;
    447      1.1  christos 	SSL_CTX* ctx;
    448      1.1  christos 
    449      1.1  christos 	if(cfg->remote_control_use_cert) {
    450      1.1  christos 		s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
    451      1.1  christos 		c_key = fname_after_chroot(cfg->control_key_file, cfg, 1);
    452      1.1  christos 		c_cert = fname_after_chroot(cfg->control_cert_file, cfg, 1);
    453      1.1  christos 		if(!s_cert || !c_key || !c_cert)
    454      1.1  christos 			fatal_exit("out of memory");
    455      1.1  christos 	}
    456  1.1.1.2  christos 	ctx = SSL_CTX_new(SSLv23_client_method());
    457      1.1  christos 	if(!ctx)
    458      1.1  christos 		ssl_err("could not allocate SSL_CTX pointer");
    459  1.1.1.2  christos 	if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)
    460      1.1  christos 		!= SSL_OP_NO_SSLv2)
    461      1.1  christos 		ssl_err("could not set SSL_OP_NO_SSLv2");
    462  1.1.1.2  christos 	if(cfg->remote_control_use_cert) {
    463      1.1  christos 		if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)
    464      1.1  christos 			!= SSL_OP_NO_SSLv3)
    465      1.1  christos 			ssl_err("could not set SSL_OP_NO_SSLv3");
    466      1.1  christos 		if(!SSL_CTX_use_certificate_chain_file(ctx,c_cert) ||
    467      1.1  christos 		    !SSL_CTX_use_PrivateKey_file(ctx,c_key,SSL_FILETYPE_PEM)
    468      1.1  christos 		    || !SSL_CTX_check_private_key(ctx))
    469      1.1  christos 			ssl_err("Error setting up SSL_CTX client key and cert");
    470      1.1  christos 		if (SSL_CTX_load_verify_locations(ctx, s_cert, NULL) != 1)
    471      1.1  christos 			ssl_err("Error setting up SSL_CTX verify, server cert");
    472      1.1  christos 		SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
    473      1.1  christos 
    474      1.1  christos 		free(s_cert);
    475      1.1  christos 		free(c_key);
    476      1.1  christos 		free(c_cert);
    477      1.1  christos 	} else {
    478      1.1  christos 		/* Use ciphers that don't require authentication  */
    479  1.1.1.2  christos #ifdef HAVE_SSL_CTX_SET_SECURITY_LEVEL
    480  1.1.1.2  christos 		SSL_CTX_set_security_level(ctx, 0);
    481  1.1.1.2  christos #endif
    482  1.1.1.2  christos 		if(!SSL_CTX_set_cipher_list(ctx, "aNULL, eNULL"))
    483      1.1  christos 			ssl_err("Error setting NULL cipher!");
    484      1.1  christos 	}
    485      1.1  christos 	return ctx;
    486      1.1  christos }
    487      1.1  christos 
    488      1.1  christos /** contact the server with TCP connect */
    489      1.1  christos static int
    490      1.1  christos contact_server(const char* svr, struct config_file* cfg, int statuscmd)
    491      1.1  christos {
    492      1.1  christos 	struct sockaddr_storage addr;
    493      1.1  christos 	socklen_t addrlen;
    494      1.1  christos 	int addrfamily = 0;
    495      1.1  christos 	int fd;
    496      1.1  christos 	/* use svr or the first config entry */
    497      1.1  christos 	if(!svr) {
    498  1.1.1.2  christos 		if(cfg->control_ifs) {
    499      1.1  christos 			svr = cfg->control_ifs->str;
    500  1.1.1.2  christos 		} else if(cfg->do_ip4) {
    501  1.1.1.2  christos 			svr = "127.0.0.1";
    502  1.1.1.2  christos 		} else {
    503  1.1.1.2  christos 			svr = "::1";
    504  1.1.1.2  christos 		}
    505      1.1  christos 		/* config 0 addr (everything), means ask localhost */
    506      1.1  christos 		if(strcmp(svr, "0.0.0.0") == 0)
    507      1.1  christos 			svr = "127.0.0.1";
    508      1.1  christos 		else if(strcmp(svr, "::0") == 0 ||
    509      1.1  christos 			strcmp(svr, "0::0") == 0 ||
    510      1.1  christos 			strcmp(svr, "0::") == 0 ||
    511      1.1  christos 			strcmp(svr, "::") == 0)
    512      1.1  christos 			svr = "::1";
    513      1.1  christos 	}
    514      1.1  christos 	if(strchr(svr, '@')) {
    515      1.1  christos 		if(!extstrtoaddr(svr, &addr, &addrlen))
    516      1.1  christos 			fatal_exit("could not parse IP@port: %s", svr);
    517      1.1  christos #ifdef HAVE_SYS_UN_H
    518      1.1  christos 	} else if(svr[0] == '/') {
    519      1.1  christos 		struct sockaddr_un* usock = (struct sockaddr_un *) &addr;
    520      1.1  christos 		usock->sun_family = AF_LOCAL;
    521      1.1  christos #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
    522  1.1.1.2  christos 		usock->sun_len = (unsigned)sizeof(usock);
    523      1.1  christos #endif
    524      1.1  christos 		(void)strlcpy(usock->sun_path, svr, sizeof(usock->sun_path));
    525      1.1  christos 		addrlen = (socklen_t)sizeof(struct sockaddr_un);
    526      1.1  christos 		addrfamily = AF_LOCAL;
    527      1.1  christos #endif
    528      1.1  christos 	} else {
    529      1.1  christos 		if(!ipstrtoaddr(svr, cfg->control_port, &addr, &addrlen))
    530      1.1  christos 			fatal_exit("could not parse IP: %s", svr);
    531      1.1  christos 	}
    532      1.1  christos 
    533      1.1  christos 	if(addrfamily == 0)
    534      1.1  christos 		addrfamily = addr_is_ip6(&addr, addrlen)?AF_INET6:AF_INET;
    535      1.1  christos 	fd = socket(addrfamily, SOCK_STREAM, 0);
    536      1.1  christos 	if(fd == -1) {
    537      1.1  christos #ifndef USE_WINSOCK
    538      1.1  christos 		fatal_exit("socket: %s", strerror(errno));
    539      1.1  christos #else
    540      1.1  christos 		fatal_exit("socket: %s", wsa_strerror(WSAGetLastError()));
    541      1.1  christos #endif
    542      1.1  christos 	}
    543      1.1  christos 	if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) {
    544      1.1  christos #ifndef USE_WINSOCK
    545      1.1  christos 		log_err_addr("connect", strerror(errno), &addr, addrlen);
    546      1.1  christos 		if(errno == ECONNREFUSED && statuscmd) {
    547      1.1  christos 			printf("unbound is stopped\n");
    548      1.1  christos 			exit(3);
    549      1.1  christos 		}
    550      1.1  christos #else
    551      1.1  christos 		log_err_addr("connect", wsa_strerror(WSAGetLastError()), &addr, addrlen);
    552      1.1  christos 		if(WSAGetLastError() == WSAECONNREFUSED && statuscmd) {
    553      1.1  christos 			printf("unbound is stopped\n");
    554      1.1  christos 			exit(3);
    555      1.1  christos 		}
    556      1.1  christos #endif
    557      1.1  christos 		exit(1);
    558      1.1  christos 	}
    559      1.1  christos 	return fd;
    560      1.1  christos }
    561      1.1  christos 
    562      1.1  christos /** setup SSL on the connection */
    563      1.1  christos static SSL*
    564      1.1  christos setup_ssl(SSL_CTX* ctx, int fd, struct config_file* cfg)
    565      1.1  christos {
    566      1.1  christos 	SSL* ssl;
    567      1.1  christos 	X509* x;
    568      1.1  christos 	int r;
    569      1.1  christos 
    570      1.1  christos 	ssl = SSL_new(ctx);
    571      1.1  christos 	if(!ssl)
    572      1.1  christos 		ssl_err("could not SSL_new");
    573      1.1  christos 	SSL_set_connect_state(ssl);
    574      1.1  christos 	(void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
    575      1.1  christos 	if(!SSL_set_fd(ssl, fd))
    576      1.1  christos 		ssl_err("could not SSL_set_fd");
    577      1.1  christos 	while(1) {
    578      1.1  christos 		ERR_clear_error();
    579      1.1  christos 		if( (r=SSL_do_handshake(ssl)) == 1)
    580      1.1  christos 			break;
    581      1.1  christos 		r = SSL_get_error(ssl, r);
    582      1.1  christos 		if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
    583      1.1  christos 			ssl_err("SSL handshake failed");
    584      1.1  christos 		/* wants to be called again */
    585      1.1  christos 	}
    586      1.1  christos 
    587      1.1  christos 	/* check authenticity of server */
    588      1.1  christos 	if(SSL_get_verify_result(ssl) != X509_V_OK)
    589      1.1  christos 		ssl_err("SSL verification failed");
    590      1.1  christos 	if(cfg->remote_control_use_cert) {
    591      1.1  christos 		x = SSL_get_peer_certificate(ssl);
    592      1.1  christos 		if(!x)
    593      1.1  christos 			ssl_err("Server presented no peer certificate");
    594      1.1  christos 		X509_free(x);
    595      1.1  christos 	}
    596      1.1  christos 
    597      1.1  christos 	return ssl;
    598      1.1  christos }
    599      1.1  christos 
    600      1.1  christos /** send stdin to server */
    601      1.1  christos static void
    602      1.1  christos send_file(SSL* ssl, FILE* in, char* buf, size_t sz)
    603      1.1  christos {
    604      1.1  christos 	while(fgets(buf, (int)sz, in)) {
    605      1.1  christos 		if(SSL_write(ssl, buf, (int)strlen(buf)) <= 0)
    606      1.1  christos 			ssl_err("could not SSL_write contents");
    607      1.1  christos 	}
    608      1.1  christos }
    609      1.1  christos 
    610  1.1.1.2  christos /** send end-of-file marker to server */
    611  1.1.1.2  christos static void
    612  1.1.1.2  christos send_eof(SSL* ssl)
    613  1.1.1.2  christos {
    614  1.1.1.2  christos 	char e[] = {0x04, 0x0a};
    615  1.1.1.2  christos 	if(SSL_write(ssl, e, (int)sizeof(e)) <= 0)
    616  1.1.1.2  christos 		ssl_err("could not SSL_write end-of-file marker");
    617  1.1.1.2  christos }
    618  1.1.1.2  christos 
    619      1.1  christos /** send command and display result */
    620      1.1  christos static int
    621      1.1  christos go_cmd(SSL* ssl, int quiet, int argc, char* argv[])
    622      1.1  christos {
    623      1.1  christos 	char pre[10];
    624      1.1  christos 	const char* space=" ";
    625      1.1  christos 	const char* newline="\n";
    626      1.1  christos 	int was_error = 0, first_line = 1;
    627      1.1  christos 	int r, i;
    628      1.1  christos 	char buf[1024];
    629      1.1  christos 	snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
    630      1.1  christos 	if(SSL_write(ssl, pre, (int)strlen(pre)) <= 0)
    631      1.1  christos 		ssl_err("could not SSL_write");
    632      1.1  christos 	for(i=0; i<argc; i++) {
    633      1.1  christos 		if(SSL_write(ssl, space, (int)strlen(space)) <= 0)
    634      1.1  christos 			ssl_err("could not SSL_write");
    635      1.1  christos 		if(SSL_write(ssl, argv[i], (int)strlen(argv[i])) <= 0)
    636      1.1  christos 			ssl_err("could not SSL_write");
    637      1.1  christos 	}
    638      1.1  christos 	if(SSL_write(ssl, newline, (int)strlen(newline)) <= 0)
    639      1.1  christos 		ssl_err("could not SSL_write");
    640      1.1  christos 
    641      1.1  christos 	if(argc == 1 && strcmp(argv[0], "load_cache") == 0) {
    642      1.1  christos 		send_file(ssl, stdin, buf, sizeof(buf));
    643      1.1  christos 	}
    644  1.1.1.2  christos 	else if(argc == 1 && (strcmp(argv[0], "local_zones") == 0 ||
    645  1.1.1.2  christos 		strcmp(argv[0], "local_zones_remove") == 0 ||
    646  1.1.1.2  christos 		strcmp(argv[0], "local_datas") == 0 ||
    647  1.1.1.2  christos 		strcmp(argv[0], "local_datas_remove") == 0)) {
    648  1.1.1.2  christos 		send_file(ssl, stdin, buf, sizeof(buf));
    649  1.1.1.2  christos 		send_eof(ssl);
    650  1.1.1.2  christos 	}
    651      1.1  christos 
    652      1.1  christos 	while(1) {
    653      1.1  christos 		ERR_clear_error();
    654      1.1  christos 		if((r = SSL_read(ssl, buf, (int)sizeof(buf)-1)) <= 0) {
    655      1.1  christos 			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
    656      1.1  christos 				/* EOF */
    657      1.1  christos 				break;
    658      1.1  christos 			}
    659      1.1  christos 			ssl_err("could not SSL_read");
    660      1.1  christos 		}
    661      1.1  christos 		buf[r] = 0;
    662      1.1  christos 		if(first_line && strncmp(buf, "error", 5) == 0) {
    663      1.1  christos 			printf("%s", buf);
    664      1.1  christos 			was_error = 1;
    665      1.1  christos 		} else if (!quiet)
    666      1.1  christos 			printf("%s", buf);
    667      1.1  christos 
    668      1.1  christos 		first_line = 0;
    669      1.1  christos 	}
    670      1.1  christos 	return was_error;
    671      1.1  christos }
    672      1.1  christos 
    673      1.1  christos /** go ahead and read config, contact server and perform command and display */
    674      1.1  christos static int
    675      1.1  christos go(const char* cfgfile, char* svr, int quiet, int argc, char* argv[])
    676      1.1  christos {
    677      1.1  christos 	struct config_file* cfg;
    678      1.1  christos 	int fd, ret;
    679      1.1  christos 	SSL_CTX* ctx;
    680      1.1  christos 	SSL* ssl;
    681      1.1  christos 
    682      1.1  christos 	/* read config */
    683      1.1  christos 	if(!(cfg = config_create()))
    684      1.1  christos 		fatal_exit("out of memory");
    685      1.1  christos 	if(!config_read(cfg, cfgfile, NULL))
    686      1.1  christos 		fatal_exit("could not read config file");
    687      1.1  christos 	if(!cfg->remote_control_enable)
    688      1.1  christos 		log_warn("control-enable is 'no' in the config file.");
    689      1.1  christos #ifdef UB_ON_WINDOWS
    690      1.1  christos 	w_config_adjust_directory(cfg);
    691      1.1  christos #endif
    692      1.1  christos 	ctx = setup_ctx(cfg);
    693      1.1  christos 
    694      1.1  christos 	/* contact server */
    695      1.1  christos 	fd = contact_server(svr, cfg, argc>0&&strcmp(argv[0],"status")==0);
    696      1.1  christos 	ssl = setup_ssl(ctx, fd, cfg);
    697      1.1  christos 
    698      1.1  christos 	/* send command */
    699      1.1  christos 	ret = go_cmd(ssl, quiet, argc, argv);
    700      1.1  christos 
    701      1.1  christos 	SSL_free(ssl);
    702      1.1  christos #ifndef USE_WINSOCK
    703      1.1  christos 	close(fd);
    704      1.1  christos #else
    705      1.1  christos 	closesocket(fd);
    706      1.1  christos #endif
    707      1.1  christos 	SSL_CTX_free(ctx);
    708      1.1  christos 	config_delete(cfg);
    709      1.1  christos 	return ret;
    710      1.1  christos }
    711      1.1  christos 
    712      1.1  christos /** getopt global, in case header files fail to declare it. */
    713      1.1  christos extern int optind;
    714      1.1  christos /** getopt global, in case header files fail to declare it. */
    715      1.1  christos extern char* optarg;
    716      1.1  christos 
    717      1.1  christos /** Main routine for unbound-control */
    718      1.1  christos int main(int argc, char* argv[])
    719      1.1  christos {
    720      1.1  christos 	int c, ret;
    721      1.1  christos 	int quiet = 0;
    722      1.1  christos 	const char* cfgfile = CONFIGFILE;
    723      1.1  christos 	char* svr = NULL;
    724      1.1  christos #ifdef USE_WINSOCK
    725      1.1  christos 	int r;
    726      1.1  christos 	WSADATA wsa_data;
    727      1.1  christos #endif
    728      1.1  christos #ifdef USE_THREAD_DEBUG
    729  1.1.1.2  christos 	/* stop the file output from unbound-control, overwrites the servers */
    730      1.1  christos 	extern int check_locking_order;
    731      1.1  christos 	check_locking_order = 0;
    732      1.1  christos #endif /* USE_THREAD_DEBUG */
    733      1.1  christos 	log_ident_set("unbound-control");
    734      1.1  christos 	log_init(NULL, 0, NULL);
    735      1.1  christos 	checklock_start();
    736      1.1  christos #ifdef USE_WINSOCK
    737      1.1  christos 	/* use registry config file in preference to compiletime location */
    738      1.1  christos 	if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
    739      1.1  christos 		cfgfile = CONFIGFILE;
    740      1.1  christos #endif
    741      1.1  christos 	/* parse the options */
    742      1.1  christos 	while( (c=getopt(argc, argv, "c:s:qh")) != -1) {
    743      1.1  christos 		switch(c) {
    744      1.1  christos 		case 'c':
    745      1.1  christos 			cfgfile = optarg;
    746      1.1  christos 			break;
    747      1.1  christos 		case 's':
    748      1.1  christos 			svr = optarg;
    749      1.1  christos 			break;
    750      1.1  christos 		case 'q':
    751      1.1  christos 			quiet = 1;
    752      1.1  christos 			break;
    753      1.1  christos 		case '?':
    754      1.1  christos 		case 'h':
    755      1.1  christos 		default:
    756      1.1  christos 			usage();
    757      1.1  christos 		}
    758      1.1  christos 	}
    759      1.1  christos 	argc -= optind;
    760      1.1  christos 	argv += optind;
    761      1.1  christos 	if(argc == 0)
    762      1.1  christos 		usage();
    763      1.1  christos 	if(argc >= 1 && strcmp(argv[0], "start")==0) {
    764      1.1  christos 		if(execlp("unbound", "unbound", "-c", cfgfile,
    765      1.1  christos 			(char*)NULL) < 0) {
    766      1.1  christos 			fatal_exit("could not exec unbound: %s",
    767      1.1  christos 				strerror(errno));
    768      1.1  christos 		}
    769      1.1  christos 	}
    770  1.1.1.2  christos 	if(argc >= 1 && strcmp(argv[0], "stats_shm")==0) {
    771  1.1.1.2  christos 		print_stats_shm(cfgfile);
    772  1.1.1.2  christos 		return 0;
    773  1.1.1.2  christos 	}
    774  1.1.1.2  christos 
    775  1.1.1.2  christos #ifdef USE_WINSOCK
    776  1.1.1.2  christos 	if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0)
    777  1.1.1.2  christos 		fatal_exit("WSAStartup failed: %s", wsa_strerror(r));
    778  1.1.1.2  christos #endif
    779  1.1.1.2  christos 
    780  1.1.1.2  christos #ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
    781  1.1.1.2  christos 	ERR_load_crypto_strings();
    782  1.1.1.2  christos #endif
    783  1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
    784  1.1.1.2  christos 	ERR_load_SSL_strings();
    785  1.1.1.2  christos #endif
    786  1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
    787  1.1.1.2  christos 	OpenSSL_add_all_algorithms();
    788  1.1.1.2  christos #else
    789  1.1.1.2  christos 	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
    790  1.1.1.2  christos 		| OPENSSL_INIT_ADD_ALL_DIGESTS
    791  1.1.1.2  christos 		| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
    792  1.1.1.2  christos #endif
    793  1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
    794  1.1.1.2  christos 	(void)SSL_library_init();
    795  1.1.1.2  christos #else
    796  1.1.1.2  christos 	(void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
    797  1.1.1.2  christos #endif
    798  1.1.1.2  christos 
    799  1.1.1.2  christos 	if(!RAND_status()) {
    800  1.1.1.2  christos 		/* try to seed it */
    801  1.1.1.2  christos 		unsigned char buf[256];
    802  1.1.1.2  christos 		unsigned int seed=(unsigned)time(NULL) ^ (unsigned)getpid();
    803  1.1.1.2  christos 		unsigned int v = seed;
    804  1.1.1.2  christos 		size_t i;
    805  1.1.1.2  christos 		for(i=0; i<256/sizeof(v); i++) {
    806  1.1.1.2  christos 			memmove(buf+i*sizeof(v), &v, sizeof(v));
    807  1.1.1.2  christos 			v = v*seed + (unsigned int)i;
    808  1.1.1.2  christos 		}
    809  1.1.1.2  christos 		RAND_seed(buf, 256);
    810  1.1.1.2  christos 		log_warn("no entropy, seeding openssl PRNG with time\n");
    811  1.1.1.2  christos 	}
    812      1.1  christos 
    813      1.1  christos 	ret = go(cfgfile, svr, quiet, argc, argv);
    814      1.1  christos 
    815      1.1  christos #ifdef USE_WINSOCK
    816  1.1.1.2  christos 	WSACleanup();
    817      1.1  christos #endif
    818      1.1  christos 	checklock_stop();
    819      1.1  christos 	return ret;
    820      1.1  christos }
    821