Home | History | Annotate | Line # | Download | only in smallapp
unbound-control.c revision 1.1.1.10
      1       1.1  christos /*
      2   1.1.1.7  christos  * smallapp/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.1.8  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.1.8  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.1.8  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.1.8  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.1.8  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.8  christos #include "util/timeval_func.h"
     63   1.1.1.2  christos #include "daemon/stats.h"
     64   1.1.1.2  christos #include "sldns/wire2str.h"
     65   1.1.1.2  christos #include "sldns/pkthdr.h"
     66   1.1.1.6  christos #include "services/rpz.h"
     67   1.1.1.7  christos #include "services/listen_dnsport.h"
     68       1.1  christos 
     69   1.1.1.2  christos #ifdef HAVE_SYS_IPC_H
     70   1.1.1.2  christos #include "sys/ipc.h"
     71   1.1.1.2  christos #endif
     72   1.1.1.2  christos #ifdef HAVE_SYS_SHM_H
     73   1.1.1.2  christos #include "sys/shm.h"
     74   1.1.1.2  christos #endif
     75       1.1  christos #ifdef HAVE_SYS_UN_H
     76       1.1  christos #include <sys/un.h>
     77       1.1  christos #endif
     78       1.1  christos 
     79   1.1.1.6  christos #ifdef HAVE_TARGETCONDITIONALS_H
     80   1.1.1.6  christos #include <TargetConditionals.h>
     81   1.1.1.6  christos #endif
     82   1.1.1.6  christos 
     83   1.1.1.4  christos static void usage(void) ATTR_NORETURN;
     84   1.1.1.4  christos static void ssl_err(const char* s) ATTR_NORETURN;
     85   1.1.1.4  christos static void ssl_path_err(const char* s, const char *path) ATTR_NORETURN;
     86   1.1.1.4  christos 
     87   1.1.1.6  christos /** timeout to wait for connection over stream, in msec */
     88   1.1.1.6  christos #define UNBOUND_CONTROL_CONNECT_TIMEOUT 5000
     89   1.1.1.6  christos 
     90       1.1  christos /** Give unbound-control usage, and exit (1). */
     91       1.1  christos static void
     92   1.1.1.2  christos usage(void)
     93       1.1  christos {
     94       1.1  christos 	printf("Usage:	unbound-control [options] command\n");
     95       1.1  christos 	printf("	Remote control utility for unbound server.\n");
     96       1.1  christos 	printf("Options:\n");
     97       1.1  christos 	printf("  -c file	config file, default is %s\n", CONFIGFILE);
     98       1.1  christos 	printf("  -s ip[@port]	server address, if omitted config is used.\n");
     99       1.1  christos 	printf("  -q		quiet (don't print anything if it works ok).\n");
    100       1.1  christos 	printf("  -h		show this usage help.\n");
    101       1.1  christos 	printf("Commands:\n");
    102       1.1  christos 	printf("  start				start server; runs unbound(8)\n");
    103       1.1  christos 	printf("  stop				stops the server\n");
    104       1.1  christos 	printf("  reload			reloads the server\n");
    105       1.1  christos 	printf("  				(this flushes data, stats, requestlist)\n");
    106   1.1.1.8  christos 	printf("  reload_keep_cache		reloads the server but tries to\n");
    107   1.1.1.8  christos 	printf("  				keep the RRset and message cache\n");
    108   1.1.1.8  christos 	printf("  				if (re)configuration allows for it.\n");
    109   1.1.1.8  christos 	printf("  				That means the caches sizes and\n");
    110   1.1.1.8  christos 	printf("  				the number of threads must not\n");
    111   1.1.1.8  christos 	printf("  				change between reloads.\n");
    112   1.1.1.9  christos 	printf("  fast_reload [+dpv]		reloads the server but only briefly stops\n");
    113   1.1.1.9  christos 	printf("  				server processing, keeps cache, and changes\n");
    114   1.1.1.9  christos 	printf("  				most options; check unbound-control(8).\n");
    115   1.1.1.9  christos 	printf("  		+d		drops running queries to keep consistency\n");
    116   1.1.1.9  christos 	printf("  				on changed options while reloading.\n");
    117   1.1.1.9  christos 	printf("  		+p		does not pause threads for even faster\n");
    118   1.1.1.9  christos 	printf("  				reload but less options are supported\n");
    119   1.1.1.9  christos 	printf("  				; check unbound-control(8).\n");
    120   1.1.1.9  christos 	printf("  		+v		verbose output, it will include duration needed.\n");
    121   1.1.1.9  christos 	printf("  		+vv		more verbose output, it will include memory needed.\n");
    122       1.1  christos 	printf("  stats				print statistics\n");
    123       1.1  christos 	printf("  stats_noreset			peek at statistics\n");
    124   1.1.1.2  christos #ifdef HAVE_SHMGET
    125   1.1.1.2  christos 	printf("  stats_shm			print statistics using shm\n");
    126   1.1.1.2  christos #endif
    127       1.1  christos 	printf("  status			display status of server\n");
    128       1.1  christos 	printf("  verbosity <number>		change logging detail\n");
    129       1.1  christos 	printf("  log_reopen			close and open the logfile\n");
    130       1.1  christos 	printf("  local_zone <name> <type>	add new local zone\n");
    131       1.1  christos 	printf("  local_zone_remove <name>	remove local zone and its contents\n");
    132       1.1  christos 	printf("  local_data <RR data...>	add local data, for example\n");
    133       1.1  christos 	printf("				local_data www.example.com A 192.0.2.1\n");
    134       1.1  christos 	printf("  local_data_remove <name>	remove local RR data from name\n");
    135   1.1.1.9  christos 	printf("  local_zones,\n");
    136   1.1.1.9  christos 	printf("  local_zones_remove,\n");
    137   1.1.1.9  christos 	printf("  local_datas,\n");
    138   1.1.1.9  christos 	printf("  local_datas_remove		same, but read list from stdin\n");
    139   1.1.1.2  christos 	printf("  				(one entry per line).\n");
    140       1.1  christos 	printf("  dump_cache			print cache to stdout\n");
    141   1.1.1.9  christos 	printf("				(not supported in remote unbounds in\n");
    142   1.1.1.9  christos 	printf("				multi-process operation)\n");
    143       1.1  christos 	printf("  load_cache			load cache from stdin\n");
    144   1.1.1.9  christos 	printf("				(not supported in remote unbounds in\n");
    145   1.1.1.9  christos 	printf("				multi-process operation)\n");
    146  1.1.1.10  christos 	printf("  cache_lookup [+t] <names>	print rrsets and msgs at or under the names\n");
    147  1.1.1.10  christos 	printf("		+t		allow tld and root names.\n");
    148       1.1  christos 	printf("  lookup <name>			print nameservers for name\n");
    149   1.1.1.9  christos 	printf("  flush [+c] <name>			flushes common types for name from cache\n");
    150       1.1  christos 	printf("  				types:  A, AAAA, MX, PTR, NS,\n");
    151       1.1  christos 	printf("					SOA, CNAME, DNAME, SRV, NAPTR\n");
    152   1.1.1.9  christos 	printf("  flush_type [+c] <name> <type>	flush name, type from cache\n");
    153   1.1.1.9  christos 	printf("		+c		remove from cachedb too\n");
    154   1.1.1.9  christos 	printf("  flush_zone [+c] <name>	flush everything at or under name\n");
    155       1.1  christos 	printf("  				from rr and dnssec caches\n");
    156   1.1.1.9  christos 	printf("  flush_bogus [+c]		flush all bogus data\n");
    157   1.1.1.9  christos 	printf("  flush_negative [+c]		flush all negative data\n");
    158       1.1  christos 	printf("  flush_stats 			flush statistics, make zero\n");
    159       1.1  christos 	printf("  flush_requestlist 		drop queries that are worked on\n");
    160       1.1  christos 	printf("  dump_requestlist		show what is worked on by first thread\n");
    161       1.1  christos 	printf("  flush_infra [all | ip] 	remove ping, edns for one IP or all\n");
    162       1.1  christos 	printf("  dump_infra			show ping and edns entries\n");
    163       1.1  christos 	printf("  set_option opt: val		set option to value, no reload\n");
    164       1.1  christos 	printf("  get_option opt		get option value\n");
    165       1.1  christos 	printf("  list_stubs			list stub-zones and root hints in use\n");
    166       1.1  christos 	printf("  list_forwards			list forward-zones in use\n");
    167       1.1  christos 	printf("  list_insecure			list domain-insecure zones\n");
    168       1.1  christos 	printf("  list_local_zones		list local-zones in use\n");
    169       1.1  christos 	printf("  list_local_data		list local-data RRs in use\n");
    170       1.1  christos 	printf("  insecure_add zone 		add domain-insecure zone\n");
    171       1.1  christos 	printf("  insecure_remove zone		remove domain-insecure zone\n");
    172   1.1.1.9  christos 	printf("  forward_add [+it] zone addr..	add forward-zone with servers\n");
    173       1.1  christos 	printf("  forward_remove [+i] zone	remove forward zone\n");
    174   1.1.1.9  christos 	printf("  stub_add [+ipt] zone addr..	add stub-zone with servers\n");
    175       1.1  christos 	printf("  stub_remove [+i] zone		remove stub zone\n");
    176       1.1  christos 	printf("		+i		also do dnssec insecure point\n");
    177       1.1  christos 	printf("		+p		set stub to use priming\n");
    178   1.1.1.9  christos 	printf("		+t		set to use tls upstream\n");
    179       1.1  christos 	printf("  forward [off | addr ...]	without arg show forward setup\n");
    180       1.1  christos 	printf("				or off to turn off root forwarding\n");
    181       1.1  christos 	printf("				or give list of ip addresses\n");
    182       1.1  christos 	printf("  ratelimit_list [+a]		list ratelimited domains\n");
    183   1.1.1.2  christos 	printf("  ip_ratelimit_list [+a]	list ratelimited ip addresses\n");
    184       1.1  christos 	printf("		+a		list all, also not ratelimited\n");
    185   1.1.1.7  christos 	printf("  list_auth_zones		list auth zones (includes RPZ zones)\n");
    186   1.1.1.7  christos 	printf("  auth_zone_reload zone		reload auth zone (or RPZ zone) from zonefile\n");
    187   1.1.1.7  christos 	printf("  auth_zone_transfer zone	transfer auth zone (or RPZ zone) from master\n");
    188   1.1.1.2  christos 	printf("  view_list_local_zones	view	list local-zones in view\n");
    189   1.1.1.2  christos 	printf("  view_list_local_data	view	list local-data RRs in view\n");
    190   1.1.1.2  christos 	printf("  view_local_zone view name type  	add local-zone in view\n");
    191   1.1.1.2  christos 	printf("  view_local_zone_remove view name  	remove local-zone in view\n");
    192   1.1.1.2  christos 	printf("  view_local_data view RR...		add local-data in view\n");
    193   1.1.1.4  christos 	printf("  view_local_datas view 		add list of local-data to view\n");
    194   1.1.1.4  christos 	printf("  					one entry per line read from stdin\n");
    195   1.1.1.2  christos 	printf("  view_local_data_remove view name  	remove local-data in view\n");
    196   1.1.1.6  christos 	printf("  view_local_datas_remove view 		remove list of local-data from view\n");
    197   1.1.1.6  christos 	printf("  					one entry per line read from stdin\n");
    198   1.1.1.6  christos 	printf("  rpz_enable zone		Enable the RPZ zone if it had previously\n");
    199   1.1.1.6  christos 	printf("  				been disabled\n");
    200   1.1.1.6  christos 	printf("  rpz_disable zone		Disable the RPZ zone\n");
    201   1.1.1.9  christos 	printf("  add_cookie_secret <secret>	add (or replace) a new cookie secret <secret>\n");
    202   1.1.1.9  christos 	printf("  drop_cookie_secret		drop a staging cookie secret\n");
    203   1.1.1.9  christos 	printf("  activate_cookie_secret	make a staging cookie secret active\n");
    204   1.1.1.9  christos 	printf("  print_cookie_secrets		show all cookie secrets with their status\n");
    205       1.1  christos 	printf("Version %s\n", PACKAGE_VERSION);
    206       1.1  christos 	printf("BSD licensed, see LICENSE in source package for details.\n");
    207       1.1  christos 	printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
    208       1.1  christos 	exit(1);
    209       1.1  christos }
    210       1.1  christos 
    211   1.1.1.2  christos #ifdef HAVE_SHMGET
    212   1.1.1.2  christos /** what to put on statistics lines between var and value, ": " or "=" */
    213   1.1.1.2  christos #define SQ "="
    214   1.1.1.2  christos /** print unsigned long stats value */
    215   1.1.1.2  christos #define PR_UL_NM(str, var) printf("%s."str SQ"%lu\n", nm, (unsigned long)(var));
    216   1.1.1.2  christos #define PR_UL(str, var) printf(str SQ"%lu\n", (unsigned long)(var));
    217   1.1.1.2  christos #define PR_UL_SUB(str, nm, var) printf(str".%s"SQ"%lu\n", nm, (unsigned long)(var));
    218   1.1.1.2  christos #define PR_TIMEVAL(str, var) printf(str SQ ARG_LL "d.%6.6d\n", \
    219   1.1.1.2  christos 	(long long)var.tv_sec, (int)var.tv_usec);
    220   1.1.1.2  christos #define PR_STATSTIME(str, var) printf(str SQ ARG_LL "d.%6.6d\n", \
    221   1.1.1.2  christos 	(long long)var ## _sec, (int)var ## _usec);
    222   1.1.1.2  christos #define PR_LL(str, var) printf(str SQ ARG_LL"d\n", (long long)(var));
    223   1.1.1.2  christos 
    224   1.1.1.2  christos /** print stat block */
    225   1.1.1.2  christos static void pr_stats(const char* nm, struct ub_stats_info* s)
    226   1.1.1.2  christos {
    227   1.1.1.2  christos 	struct timeval sumwait, avg;
    228   1.1.1.2  christos 	PR_UL_NM("num.queries", s->svr.num_queries);
    229   1.1.1.8  christos 	PR_UL_NM("num.queries_ip_ratelimited",
    230   1.1.1.2  christos 		s->svr.num_queries_ip_ratelimited);
    231   1.1.1.8  christos 	PR_UL_NM("num.queries_cookie_valid",
    232   1.1.1.8  christos 		s->svr.num_queries_cookie_valid);
    233   1.1.1.8  christos 	PR_UL_NM("num.queries_cookie_client",
    234   1.1.1.8  christos 		s->svr.num_queries_cookie_client);
    235   1.1.1.8  christos 	PR_UL_NM("num.queries_cookie_invalid",
    236   1.1.1.8  christos 		s->svr.num_queries_cookie_invalid);
    237   1.1.1.9  christos 	PR_UL_NM("num.queries_discard_timeout",
    238   1.1.1.9  christos 		s->svr.num_queries_discard_timeout);
    239   1.1.1.9  christos 	PR_UL_NM("num.queries_wait_limit", s->svr.num_queries_wait_limit);
    240   1.1.1.2  christos 	PR_UL_NM("num.cachehits",
    241   1.1.1.2  christos 		s->svr.num_queries - s->svr.num_queries_missed_cache);
    242   1.1.1.2  christos 	PR_UL_NM("num.cachemiss", s->svr.num_queries_missed_cache);
    243   1.1.1.2  christos 	PR_UL_NM("num.prefetch", s->svr.num_queries_prefetch);
    244   1.1.1.8  christos 	PR_UL_NM("num.queries_timed_out", s->svr.num_queries_timed_out);
    245   1.1.1.8  christos 	PR_UL_NM("query.queue_time_us.max", s->svr.max_query_time_us);
    246   1.1.1.6  christos 	PR_UL_NM("num.expired", s->svr.ans_expired);
    247   1.1.1.2  christos 	PR_UL_NM("num.recursivereplies", s->mesh_replies_sent);
    248   1.1.1.2  christos #ifdef USE_DNSCRYPT
    249   1.1.1.9  christos 	PR_UL_NM("num.dnscrypt.crypted", s->svr.num_query_dnscrypt_crypted);
    250   1.1.1.9  christos 	PR_UL_NM("num.dnscrypt.cert", s->svr.num_query_dnscrypt_cert);
    251   1.1.1.9  christos 	PR_UL_NM("num.dnscrypt.cleartext", s->svr.num_query_dnscrypt_cleartext);
    252   1.1.1.9  christos 	PR_UL_NM("num.dnscrypt.malformed",
    253   1.1.1.9  christos 		s->svr.num_query_dnscrypt_crypted_malformed);
    254   1.1.1.2  christos #endif /* USE_DNSCRYPT */
    255   1.1.1.9  christos 	PR_UL_NM("num.dns_error_reports", s->svr.num_dns_error_reports);
    256   1.1.1.2  christos 	printf("%s.requestlist.avg"SQ"%g\n", nm,
    257   1.1.1.2  christos 		(s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)?
    258   1.1.1.2  christos 			(double)s->svr.sum_query_list_size/
    259   1.1.1.2  christos 			(double)(s->svr.num_queries_missed_cache+
    260   1.1.1.2  christos 			s->svr.num_queries_prefetch) : 0.0);
    261   1.1.1.2  christos 	PR_UL_NM("requestlist.max", s->svr.max_query_list_size);
    262   1.1.1.2  christos 	PR_UL_NM("requestlist.overwritten", s->mesh_jostled);
    263   1.1.1.2  christos 	PR_UL_NM("requestlist.exceeded", s->mesh_dropped);
    264   1.1.1.2  christos 	PR_UL_NM("requestlist.current.all", s->mesh_num_states);
    265   1.1.1.2  christos 	PR_UL_NM("requestlist.current.user", s->mesh_num_reply_states);
    266   1.1.1.2  christos #ifndef S_SPLINT_S
    267   1.1.1.2  christos 	sumwait.tv_sec = s->mesh_replies_sum_wait_sec;
    268   1.1.1.2  christos 	sumwait.tv_usec = s->mesh_replies_sum_wait_usec;
    269   1.1.1.2  christos #endif
    270   1.1.1.2  christos 	timeval_divide(&avg, &sumwait, s->mesh_replies_sent);
    271   1.1.1.2  christos 	printf("%s.", nm);
    272   1.1.1.2  christos 	PR_TIMEVAL("recursion.time.avg", avg);
    273   1.1.1.2  christos 	printf("%s.recursion.time.median"SQ"%g\n", nm, s->mesh_time_median);
    274   1.1.1.2  christos 	PR_UL_NM("tcpusage", s->svr.tcp_accept_usage);
    275   1.1.1.2  christos }
    276   1.1.1.2  christos 
    277   1.1.1.2  christos /** print uptime */
    278   1.1.1.2  christos static void print_uptime(struct ub_shm_stat_info* shm_stat)
    279   1.1.1.2  christos {
    280   1.1.1.2  christos 	PR_STATSTIME("time.now", shm_stat->time.now);
    281   1.1.1.2  christos 	PR_STATSTIME("time.up", shm_stat->time.up);
    282   1.1.1.2  christos 	PR_STATSTIME("time.elapsed", shm_stat->time.elapsed);
    283   1.1.1.2  christos }
    284   1.1.1.2  christos 
    285   1.1.1.2  christos /** print memory usage */
    286   1.1.1.4  christos static void print_mem(struct ub_shm_stat_info* shm_stat,
    287   1.1.1.4  christos 	struct ub_stats_info* s)
    288   1.1.1.2  christos {
    289   1.1.1.2  christos 	PR_LL("mem.cache.rrset", shm_stat->mem.rrset);
    290   1.1.1.2  christos 	PR_LL("mem.cache.message", shm_stat->mem.msg);
    291   1.1.1.2  christos 	PR_LL("mem.mod.iterator", shm_stat->mem.iter);
    292   1.1.1.2  christos 	PR_LL("mem.mod.validator", shm_stat->mem.val);
    293   1.1.1.2  christos 	PR_LL("mem.mod.respip", shm_stat->mem.respip);
    294   1.1.1.2  christos #ifdef CLIENT_SUBNET
    295   1.1.1.2  christos 	PR_LL("mem.mod.subnet", shm_stat->mem.subnet);
    296   1.1.1.2  christos #endif
    297   1.1.1.2  christos #ifdef USE_IPSECMOD
    298   1.1.1.2  christos 	PR_LL("mem.mod.ipsecmod", shm_stat->mem.ipsecmod);
    299   1.1.1.2  christos #endif
    300   1.1.1.6  christos #ifdef WITH_DYNLIBMODULE
    301   1.1.1.6  christos 	PR_LL("mem.mod.dynlib", shm_stat->mem.dynlib);
    302   1.1.1.6  christos #endif
    303   1.1.1.2  christos #ifdef USE_DNSCRYPT
    304   1.1.1.2  christos 	PR_LL("mem.cache.dnscrypt_shared_secret",
    305   1.1.1.2  christos 		shm_stat->mem.dnscrypt_shared_secret);
    306   1.1.1.2  christos 	PR_LL("mem.cache.dnscrypt_nonce",
    307   1.1.1.2  christos 		shm_stat->mem.dnscrypt_nonce);
    308   1.1.1.2  christos #endif
    309   1.1.1.4  christos 	PR_LL("mem.streamwait", s->svr.mem_stream_wait);
    310   1.1.1.6  christos 	PR_LL("mem.http.query_buffer", s->svr.mem_http2_query_buffer);
    311   1.1.1.6  christos 	PR_LL("mem.http.response_buffer", s->svr.mem_http2_response_buffer);
    312   1.1.1.9  christos #ifdef HAVE_NGTCP2
    313   1.1.1.9  christos 	PR_LL("mem.quic", s->svr.mem_quic);
    314   1.1.1.9  christos #endif
    315   1.1.1.2  christos }
    316   1.1.1.2  christos 
    317   1.1.1.2  christos /** print histogram */
    318   1.1.1.2  christos static void print_hist(struct ub_stats_info* s)
    319   1.1.1.2  christos {
    320   1.1.1.2  christos 	struct timehist* hist;
    321   1.1.1.2  christos 	size_t i;
    322   1.1.1.2  christos 	hist = timehist_setup();
    323   1.1.1.2  christos 	if(!hist)
    324   1.1.1.2  christos 		fatal_exit("out of memory");
    325   1.1.1.2  christos 	timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST);
    326   1.1.1.2  christos 	for(i=0; i<hist->num; i++) {
    327   1.1.1.2  christos 		printf("histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n",
    328   1.1.1.2  christos 			(int)hist->buckets[i].lower.tv_sec,
    329   1.1.1.2  christos 			(int)hist->buckets[i].lower.tv_usec,
    330   1.1.1.2  christos 			(int)hist->buckets[i].upper.tv_sec,
    331   1.1.1.2  christos 			(int)hist->buckets[i].upper.tv_usec,
    332   1.1.1.2  christos 			(unsigned long)hist->buckets[i].count);
    333   1.1.1.2  christos 	}
    334   1.1.1.2  christos 	timehist_delete(hist);
    335   1.1.1.2  christos }
    336   1.1.1.2  christos 
    337   1.1.1.2  christos /** print extended */
    338   1.1.1.8  christos static void print_extended(struct ub_stats_info* s, int inhibit_zero)
    339   1.1.1.2  christos {
    340   1.1.1.2  christos 	int i;
    341   1.1.1.2  christos 	char nm[16];
    342   1.1.1.2  christos 
    343   1.1.1.2  christos 	/* TYPE */
    344   1.1.1.2  christos 	for(i=0; i<UB_STATS_QTYPE_NUM; i++) {
    345   1.1.1.2  christos 		if(inhibit_zero && s->svr.qtype[i] == 0)
    346   1.1.1.2  christos 			continue;
    347   1.1.1.2  christos 		sldns_wire2str_type_buf((uint16_t)i, nm, sizeof(nm));
    348   1.1.1.2  christos 		PR_UL_SUB("num.query.type", nm, s->svr.qtype[i]);
    349   1.1.1.2  christos 	}
    350   1.1.1.2  christos 	if(!inhibit_zero || s->svr.qtype_big) {
    351   1.1.1.2  christos 		PR_UL("num.query.type.other", s->svr.qtype_big);
    352   1.1.1.2  christos 	}
    353   1.1.1.2  christos 
    354   1.1.1.2  christos 	/* CLASS */
    355   1.1.1.2  christos 	for(i=0; i<UB_STATS_QCLASS_NUM; i++) {
    356   1.1.1.2  christos 		if(inhibit_zero && s->svr.qclass[i] == 0)
    357   1.1.1.2  christos 			continue;
    358   1.1.1.2  christos 		sldns_wire2str_class_buf((uint16_t)i, nm, sizeof(nm));
    359   1.1.1.2  christos 		PR_UL_SUB("num.query.class", nm, s->svr.qclass[i]);
    360   1.1.1.2  christos 	}
    361   1.1.1.2  christos 	if(!inhibit_zero || s->svr.qclass_big) {
    362   1.1.1.2  christos 		PR_UL("num.query.class.other", s->svr.qclass_big);
    363   1.1.1.2  christos 	}
    364   1.1.1.2  christos 
    365   1.1.1.2  christos 	/* OPCODE */
    366   1.1.1.2  christos 	for(i=0; i<UB_STATS_OPCODE_NUM; i++) {
    367   1.1.1.2  christos 		if(inhibit_zero && s->svr.qopcode[i] == 0)
    368   1.1.1.2  christos 			continue;
    369   1.1.1.2  christos 		sldns_wire2str_opcode_buf(i, nm, sizeof(nm));
    370   1.1.1.2  christos 		PR_UL_SUB("num.query.opcode", nm, s->svr.qopcode[i]);
    371   1.1.1.2  christos 	}
    372   1.1.1.2  christos 
    373   1.1.1.2  christos 	/* transport */
    374   1.1.1.2  christos 	PR_UL("num.query.tcp", s->svr.qtcp);
    375   1.1.1.2  christos 	PR_UL("num.query.tcpout", s->svr.qtcp_outgoing);
    376   1.1.1.7  christos 	PR_UL("num.query.udpout", s->svr.qudp_outgoing);
    377   1.1.1.4  christos 	PR_UL("num.query.tls", s->svr.qtls);
    378   1.1.1.4  christos 	PR_UL("num.query.tls_resume", s->svr.qtls_resume);
    379   1.1.1.2  christos 	PR_UL("num.query.ipv6", s->svr.qipv6);
    380   1.1.1.6  christos 	PR_UL("num.query.https", s->svr.qhttps);
    381   1.1.1.9  christos #ifdef HAVE_NGTCP2
    382   1.1.1.9  christos 	PR_UL("num.query.quic", s->svr.qquic);
    383   1.1.1.9  christos #endif
    384   1.1.1.2  christos 
    385   1.1.1.2  christos 	/* flags */
    386   1.1.1.2  christos 	PR_UL("num.query.flags.QR", s->svr.qbit_QR);
    387   1.1.1.2  christos 	PR_UL("num.query.flags.AA", s->svr.qbit_AA);
    388   1.1.1.2  christos 	PR_UL("num.query.flags.TC", s->svr.qbit_TC);
    389   1.1.1.2  christos 	PR_UL("num.query.flags.RD", s->svr.qbit_RD);
    390   1.1.1.2  christos 	PR_UL("num.query.flags.RA", s->svr.qbit_RA);
    391   1.1.1.2  christos 	PR_UL("num.query.flags.Z", s->svr.qbit_Z);
    392   1.1.1.2  christos 	PR_UL("num.query.flags.AD", s->svr.qbit_AD);
    393   1.1.1.2  christos 	PR_UL("num.query.flags.CD", s->svr.qbit_CD);
    394   1.1.1.2  christos 	PR_UL("num.query.edns.present", s->svr.qEDNS);
    395   1.1.1.2  christos 	PR_UL("num.query.edns.DO", s->svr.qEDNS_DO);
    396   1.1.1.2  christos 
    397   1.1.1.2  christos 	/* RCODE */
    398   1.1.1.2  christos 	for(i=0; i<UB_STATS_RCODE_NUM; i++) {
    399   1.1.1.2  christos 		/* Always include RCODEs 0-5 */
    400   1.1.1.2  christos 		if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0)
    401   1.1.1.2  christos 			continue;
    402   1.1.1.2  christos 		sldns_wire2str_rcode_buf(i, nm, sizeof(nm));
    403   1.1.1.2  christos 		PR_UL_SUB("num.answer.rcode", nm, s->svr.ans_rcode[i]);
    404   1.1.1.2  christos 	}
    405   1.1.1.2  christos 	if(!inhibit_zero || s->svr.ans_rcode_nodata) {
    406   1.1.1.2  christos 		PR_UL("num.answer.rcode.nodata", s->svr.ans_rcode_nodata);
    407   1.1.1.2  christos 	}
    408   1.1.1.2  christos 	/* iteration */
    409   1.1.1.2  christos 	PR_UL("num.query.ratelimited", s->svr.queries_ratelimited);
    410   1.1.1.2  christos 	/* validation */
    411   1.1.1.2  christos 	PR_UL("num.answer.secure", s->svr.ans_secure);
    412   1.1.1.2  christos 	PR_UL("num.answer.bogus", s->svr.ans_bogus);
    413   1.1.1.2  christos 	PR_UL("num.rrset.bogus", s->svr.rrset_bogus);
    414  1.1.1.10  christos 	PR_UL("num.valops", s->svr.val_ops);
    415   1.1.1.3  christos 	PR_UL("num.query.aggressive.NOERROR", s->svr.num_neg_cache_noerror);
    416   1.1.1.3  christos 	PR_UL("num.query.aggressive.NXDOMAIN", s->svr.num_neg_cache_nxdomain);
    417   1.1.1.2  christos 	/* threat detection */
    418   1.1.1.2  christos 	PR_UL("unwanted.queries", s->svr.unwanted_queries);
    419   1.1.1.2  christos 	PR_UL("unwanted.replies", s->svr.unwanted_replies);
    420   1.1.1.2  christos 	/* cache counts */
    421   1.1.1.2  christos 	PR_UL("msg.cache.count", s->svr.msg_cache_count);
    422   1.1.1.2  christos 	PR_UL("rrset.cache.count", s->svr.rrset_cache_count);
    423   1.1.1.2  christos 	PR_UL("infra.cache.count", s->svr.infra_cache_count);
    424   1.1.1.2  christos 	PR_UL("key.cache.count", s->svr.key_cache_count);
    425   1.1.1.8  christos 	/* max collisions */
    426   1.1.1.8  christos 	PR_UL("msg.cache.max_collisions", s->svr.msg_cache_max_collisions);
    427   1.1.1.8  christos 	PR_UL("rrset.cache.max_collisions", s->svr.rrset_cache_max_collisions);
    428   1.1.1.6  christos 	/* applied RPZ actions */
    429   1.1.1.6  christos 	for(i=0; i<UB_STATS_RPZ_ACTION_NUM; i++) {
    430   1.1.1.6  christos 		if(i == RPZ_NO_OVERRIDE_ACTION)
    431   1.1.1.6  christos 			continue;
    432   1.1.1.6  christos 		if(inhibit_zero && s->svr.rpz_action[i] == 0)
    433   1.1.1.6  christos 			continue;
    434   1.1.1.6  christos 		PR_UL_SUB("num.rpz.action", rpz_action_to_string(i), s->svr.rpz_action[i]);
    435   1.1.1.6  christos 	}
    436   1.1.1.2  christos #ifdef USE_DNSCRYPT
    437   1.1.1.2  christos 	PR_UL("dnscrypt_shared_secret.cache.count",
    438   1.1.1.2  christos 			 s->svr.shared_secret_cache_count);
    439   1.1.1.2  christos 	PR_UL("num.query.dnscrypt.shared_secret.cachemiss",
    440   1.1.1.2  christos 			 s->svr.num_query_dnscrypt_secret_missed_cache);
    441   1.1.1.2  christos 	PR_UL("dnscrypt_nonce.cache.count", s->svr.nonce_cache_count);
    442   1.1.1.2  christos 	PR_UL("num.query.dnscrypt.replay",
    443   1.1.1.2  christos 			 s->svr.num_query_dnscrypt_replay);
    444   1.1.1.2  christos #endif /* USE_DNSCRYPT */
    445   1.1.1.3  christos 	PR_UL("num.query.authzone.up", s->svr.num_query_authzone_up);
    446   1.1.1.3  christos 	PR_UL("num.query.authzone.down", s->svr.num_query_authzone_down);
    447   1.1.1.4  christos #ifdef CLIENT_SUBNET
    448   1.1.1.4  christos 	PR_UL("num.query.subnet", s->svr.num_query_subnet);
    449   1.1.1.4  christos 	PR_UL("num.query.subnet_cache", s->svr.num_query_subnet_cache);
    450   1.1.1.4  christos #endif
    451   1.1.1.8  christos #ifdef USE_CACHEDB
    452   1.1.1.8  christos 	PR_UL("num.query.cachedb", s->svr.num_query_cachedb);
    453   1.1.1.8  christos #endif
    454   1.1.1.2  christos }
    455   1.1.1.2  christos 
    456   1.1.1.2  christos /** print statistics out of memory structures */
    457   1.1.1.2  christos static void do_stats_shm(struct config_file* cfg, struct ub_stats_info* stats,
    458   1.1.1.2  christos 	struct ub_shm_stat_info* shm_stat)
    459   1.1.1.2  christos {
    460   1.1.1.2  christos 	int i;
    461   1.1.1.2  christos 	char nm[32];
    462   1.1.1.2  christos 	for(i=0; i<cfg->num_threads; i++) {
    463   1.1.1.2  christos 		snprintf(nm, sizeof(nm), "thread%d", i);
    464   1.1.1.2  christos 		pr_stats(nm, &stats[i+1]);
    465   1.1.1.2  christos 	}
    466   1.1.1.2  christos 	pr_stats("total", &stats[0]);
    467   1.1.1.2  christos 	print_uptime(shm_stat);
    468   1.1.1.2  christos 	if(cfg->stat_extended) {
    469   1.1.1.4  christos 		print_mem(shm_stat, &stats[0]);
    470   1.1.1.2  christos 		print_hist(stats);
    471   1.1.1.8  christos 		print_extended(stats, cfg->stat_inhibit_zero);
    472   1.1.1.2  christos 	}
    473   1.1.1.2  christos }
    474   1.1.1.2  christos #endif /* HAVE_SHMGET */
    475   1.1.1.2  christos 
    476   1.1.1.2  christos /** print statistics from shm memory segment */
    477   1.1.1.7  christos static void print_stats_shm(const char* cfgfile, int quiet)
    478   1.1.1.2  christos {
    479   1.1.1.2  christos #ifdef HAVE_SHMGET
    480   1.1.1.2  christos 	struct config_file* cfg;
    481   1.1.1.2  christos 	struct ub_stats_info* stats;
    482   1.1.1.2  christos 	struct ub_shm_stat_info* shm_stat;
    483   1.1.1.2  christos 	int id_ctl, id_arr;
    484   1.1.1.2  christos 	/* read config */
    485   1.1.1.2  christos 	if(!(cfg = config_create()))
    486   1.1.1.2  christos 		fatal_exit("out of memory");
    487   1.1.1.2  christos 	if(!config_read(cfg, cfgfile, NULL))
    488   1.1.1.2  christos 		fatal_exit("could not read config file");
    489   1.1.1.2  christos 	/* get shm segments */
    490   1.1.1.5  christos 	id_ctl = shmget(cfg->shm_key, sizeof(int), SHM_R);
    491   1.1.1.2  christos 	if(id_ctl == -1) {
    492   1.1.1.2  christos 		fatal_exit("shmget(%d): %s", cfg->shm_key, strerror(errno));
    493   1.1.1.2  christos 	}
    494   1.1.1.5  christos 	id_arr = shmget(cfg->shm_key+1, sizeof(int), SHM_R);
    495   1.1.1.2  christos 	if(id_arr == -1) {
    496   1.1.1.2  christos 		fatal_exit("shmget(%d): %s", cfg->shm_key+1, strerror(errno));
    497   1.1.1.2  christos 	}
    498   1.1.1.5  christos 	shm_stat = (struct ub_shm_stat_info*)shmat(id_ctl, NULL, SHM_RDONLY);
    499   1.1.1.2  christos 	if(shm_stat == (void*)-1) {
    500   1.1.1.2  christos 		fatal_exit("shmat(%d): %s", id_ctl, strerror(errno));
    501   1.1.1.2  christos 	}
    502   1.1.1.5  christos 	stats = (struct ub_stats_info*)shmat(id_arr, NULL, SHM_RDONLY);
    503   1.1.1.2  christos 	if(stats == (void*)-1) {
    504   1.1.1.2  christos 		fatal_exit("shmat(%d): %s", id_arr, strerror(errno));
    505   1.1.1.2  christos 	}
    506   1.1.1.2  christos 
    507   1.1.1.7  christos 
    508   1.1.1.7  christos 	if(!quiet) {
    509   1.1.1.7  christos 		/* print the stats */
    510   1.1.1.7  christos 		do_stats_shm(cfg, stats, shm_stat);
    511   1.1.1.7  christos 	}
    512   1.1.1.2  christos 
    513   1.1.1.2  christos 	/* shutdown */
    514   1.1.1.2  christos 	shmdt(shm_stat);
    515   1.1.1.2  christos 	shmdt(stats);
    516   1.1.1.2  christos 	config_delete(cfg);
    517   1.1.1.2  christos #else
    518   1.1.1.2  christos 	(void)cfgfile;
    519   1.1.1.7  christos 	(void)quiet;
    520   1.1.1.2  christos #endif /* HAVE_SHMGET */
    521   1.1.1.2  christos }
    522   1.1.1.2  christos 
    523       1.1  christos /** exit with ssl error */
    524       1.1  christos static void ssl_err(const char* s)
    525       1.1  christos {
    526       1.1  christos 	fprintf(stderr, "error: %s\n", s);
    527       1.1  christos 	ERR_print_errors_fp(stderr);
    528       1.1  christos 	exit(1);
    529       1.1  christos }
    530       1.1  christos 
    531   1.1.1.4  christos /** exit with ssl error related to a file path */
    532   1.1.1.4  christos static void ssl_path_err(const char* s, const char *path)
    533   1.1.1.4  christos {
    534   1.1.1.4  christos 	unsigned long err;
    535   1.1.1.4  christos 	err = ERR_peek_error();
    536   1.1.1.7  christos 	if(ERR_GET_LIB(err) == ERR_LIB_SYS) {
    537   1.1.1.4  christos 		fprintf(stderr, "error: %s\n%s: %s\n",
    538   1.1.1.4  christos 			s, path, ERR_reason_error_string(err));
    539   1.1.1.4  christos 		exit(1);
    540   1.1.1.4  christos 	} else {
    541   1.1.1.4  christos 		ssl_err(s);
    542   1.1.1.4  christos 	}
    543   1.1.1.4  christos }
    544   1.1.1.4  christos 
    545       1.1  christos /** setup SSL context */
    546       1.1  christos static SSL_CTX*
    547       1.1  christos setup_ctx(struct config_file* cfg)
    548       1.1  christos {
    549       1.1  christos 	char* s_cert=NULL, *c_key=NULL, *c_cert=NULL;
    550       1.1  christos 	SSL_CTX* ctx;
    551       1.1  christos 
    552   1.1.1.3  christos 	if(!(options_remote_is_address(cfg) && cfg->control_use_cert))
    553   1.1.1.3  christos 		return NULL;
    554   1.1.1.3  christos 	s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
    555   1.1.1.3  christos 	c_key = fname_after_chroot(cfg->control_key_file, cfg, 1);
    556   1.1.1.3  christos 	c_cert = fname_after_chroot(cfg->control_cert_file, cfg, 1);
    557   1.1.1.3  christos 	if(!s_cert || !c_key || !c_cert)
    558   1.1.1.3  christos 		fatal_exit("out of memory");
    559   1.1.1.2  christos 	ctx = SSL_CTX_new(SSLv23_client_method());
    560       1.1  christos 	if(!ctx)
    561       1.1  christos 		ssl_err("could not allocate SSL_CTX pointer");
    562   1.1.1.6  christos #if SSL_OP_NO_SSLv2 != 0
    563   1.1.1.2  christos 	if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)
    564       1.1  christos 		!= SSL_OP_NO_SSLv2)
    565       1.1  christos 		ssl_err("could not set SSL_OP_NO_SSLv2");
    566   1.1.1.6  christos #endif
    567   1.1.1.3  christos 	if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)
    568   1.1.1.3  christos 		!= SSL_OP_NO_SSLv3)
    569   1.1.1.3  christos 		ssl_err("could not set SSL_OP_NO_SSLv3");
    570   1.1.1.5  christos #if defined(SSL_OP_NO_RENEGOTIATION)
    571   1.1.1.5  christos 	/* disable client renegotiation */
    572   1.1.1.5  christos 	if((SSL_CTX_set_options(ctx, SSL_OP_NO_RENEGOTIATION) &
    573   1.1.1.5  christos 		SSL_OP_NO_RENEGOTIATION) != SSL_OP_NO_RENEGOTIATION)
    574   1.1.1.5  christos 		ssl_err("could not set SSL_OP_NO_RENEGOTIATION");
    575   1.1.1.5  christos #endif
    576   1.1.1.4  christos 	if(!SSL_CTX_use_certificate_chain_file(ctx,c_cert))
    577   1.1.1.4  christos 		ssl_path_err("Error setting up SSL_CTX client cert", c_cert);
    578   1.1.1.7  christos 	if(!SSL_CTX_use_PrivateKey_file(ctx,c_key,SSL_FILETYPE_PEM))
    579   1.1.1.4  christos 		ssl_path_err("Error setting up SSL_CTX client key", c_key);
    580   1.1.1.7  christos 	if(!SSL_CTX_check_private_key(ctx))
    581   1.1.1.4  christos 		ssl_err("Error setting up SSL_CTX client key");
    582   1.1.1.7  christos 	if(SSL_CTX_load_verify_locations(ctx, s_cert, NULL) != 1)
    583   1.1.1.4  christos 		ssl_path_err("Error setting up SSL_CTX verify, server cert",
    584   1.1.1.4  christos 			     s_cert);
    585   1.1.1.3  christos 	SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
    586   1.1.1.3  christos 
    587   1.1.1.3  christos 	free(s_cert);
    588   1.1.1.3  christos 	free(c_key);
    589   1.1.1.3  christos 	free(c_cert);
    590       1.1  christos 	return ctx;
    591       1.1  christos }
    592       1.1  christos 
    593   1.1.1.6  christos /** check connect error */
    594   1.1.1.6  christos static void
    595   1.1.1.6  christos checkconnecterr(int err, const char* svr, struct sockaddr_storage* addr,
    596   1.1.1.6  christos 	socklen_t addrlen, int statuscmd, int useport)
    597   1.1.1.6  christos {
    598   1.1.1.6  christos #ifndef USE_WINSOCK
    599   1.1.1.6  christos 	if(!useport) log_err("connect: %s for %s", strerror(err), svr);
    600   1.1.1.6  christos 	else log_err_addr("connect", strerror(err), addr, addrlen);
    601   1.1.1.6  christos 	if(err == ECONNREFUSED && statuscmd) {
    602   1.1.1.6  christos 		printf("unbound is stopped\n");
    603   1.1.1.6  christos 		exit(3);
    604   1.1.1.6  christos 	}
    605   1.1.1.6  christos #else
    606   1.1.1.6  christos 	int wsaerr = err;
    607   1.1.1.6  christos 	if(!useport) log_err("connect: %s for %s", wsa_strerror(wsaerr), svr);
    608   1.1.1.6  christos 	else log_err_addr("connect", wsa_strerror(wsaerr), addr, addrlen);
    609   1.1.1.6  christos 	if(wsaerr == WSAECONNREFUSED && statuscmd) {
    610   1.1.1.6  christos 		printf("unbound is stopped\n");
    611   1.1.1.6  christos 		exit(3);
    612   1.1.1.6  christos 	}
    613   1.1.1.6  christos #endif
    614   1.1.1.6  christos 	exit(1);
    615   1.1.1.6  christos }
    616   1.1.1.6  christos 
    617       1.1  christos /** contact the server with TCP connect */
    618       1.1  christos static int
    619       1.1  christos contact_server(const char* svr, struct config_file* cfg, int statuscmd)
    620       1.1  christos {
    621       1.1  christos 	struct sockaddr_storage addr;
    622       1.1  christos 	socklen_t addrlen;
    623   1.1.1.3  christos 	int addrfamily = 0, proto = IPPROTO_TCP;
    624   1.1.1.3  christos 	int fd, useport = 1;
    625   1.1.1.7  christos 	char** rcif = NULL;
    626   1.1.1.7  christos 	int num_rcif = 0;
    627       1.1  christos 	/* use svr or the first config entry */
    628       1.1  christos 	if(!svr) {
    629   1.1.1.3  christos 		if(cfg->control_ifs.first) {
    630   1.1.1.7  christos 			struct sockaddr_storage addr2;
    631   1.1.1.7  christos 			socklen_t addrlen2;
    632   1.1.1.7  christos 			if(extstrtoaddr(cfg->control_ifs.first->str, &addr2,
    633   1.1.1.8  christos 				&addrlen2, UNBOUND_DNS_PORT)) {
    634   1.1.1.7  christos 				svr = cfg->control_ifs.first->str;
    635   1.1.1.7  christos 			} else {
    636   1.1.1.7  christos 				if(!resolve_interface_names(NULL, 0,
    637   1.1.1.7  christos 					cfg->control_ifs.first, &rcif,
    638   1.1.1.7  christos 					&num_rcif)) {
    639   1.1.1.7  christos 					fatal_exit("could not resolve interface names");
    640   1.1.1.7  christos 				}
    641   1.1.1.7  christos 				if(rcif == NULL || num_rcif == 0) {
    642   1.1.1.7  christos 					fatal_exit("no control interfaces");
    643   1.1.1.7  christos 				}
    644   1.1.1.7  christos 				svr = rcif[0];
    645   1.1.1.7  christos 			}
    646   1.1.1.2  christos 		} else if(cfg->do_ip4) {
    647   1.1.1.2  christos 			svr = "127.0.0.1";
    648   1.1.1.2  christos 		} else {
    649   1.1.1.2  christos 			svr = "::1";
    650   1.1.1.2  christos 		}
    651       1.1  christos 		/* config 0 addr (everything), means ask localhost */
    652       1.1  christos 		if(strcmp(svr, "0.0.0.0") == 0)
    653       1.1  christos 			svr = "127.0.0.1";
    654       1.1  christos 		else if(strcmp(svr, "::0") == 0 ||
    655       1.1  christos 			strcmp(svr, "0::0") == 0 ||
    656       1.1  christos 			strcmp(svr, "0::") == 0 ||
    657       1.1  christos 			strcmp(svr, "::") == 0)
    658       1.1  christos 			svr = "::1";
    659       1.1  christos 	}
    660       1.1  christos 	if(strchr(svr, '@')) {
    661   1.1.1.8  christos 		if(!extstrtoaddr(svr, &addr, &addrlen, UNBOUND_DNS_PORT))
    662       1.1  christos 			fatal_exit("could not parse IP@port: %s", svr);
    663       1.1  christos #ifdef HAVE_SYS_UN_H
    664       1.1  christos 	} else if(svr[0] == '/') {
    665       1.1  christos 		struct sockaddr_un* usock = (struct sockaddr_un *) &addr;
    666       1.1  christos 		usock->sun_family = AF_LOCAL;
    667       1.1  christos #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
    668   1.1.1.2  christos 		usock->sun_len = (unsigned)sizeof(usock);
    669       1.1  christos #endif
    670       1.1  christos 		(void)strlcpy(usock->sun_path, svr, sizeof(usock->sun_path));
    671       1.1  christos 		addrlen = (socklen_t)sizeof(struct sockaddr_un);
    672       1.1  christos 		addrfamily = AF_LOCAL;
    673   1.1.1.3  christos 		useport = 0;
    674   1.1.1.3  christos 		proto = 0;
    675       1.1  christos #endif
    676       1.1  christos 	} else {
    677       1.1  christos 		if(!ipstrtoaddr(svr, cfg->control_port, &addr, &addrlen))
    678       1.1  christos 			fatal_exit("could not parse IP: %s", svr);
    679       1.1  christos 	}
    680       1.1  christos 
    681       1.1  christos 	if(addrfamily == 0)
    682   1.1.1.3  christos 		addrfamily = addr_is_ip6(&addr, addrlen)?PF_INET6:PF_INET;
    683   1.1.1.3  christos 	fd = socket(addrfamily, SOCK_STREAM, proto);
    684       1.1  christos 	if(fd == -1) {
    685   1.1.1.6  christos 		fatal_exit("socket: %s", sock_strerror(errno));
    686       1.1  christos 	}
    687   1.1.1.6  christos 	fd_set_nonblock(fd);
    688       1.1  christos 	if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) {
    689       1.1  christos #ifndef USE_WINSOCK
    690   1.1.1.6  christos #ifdef EINPROGRESS
    691   1.1.1.6  christos 		if(errno != EINPROGRESS) {
    692   1.1.1.6  christos 			checkconnecterr(errno, svr, &addr,
    693   1.1.1.6  christos 				addrlen, statuscmd, useport);
    694       1.1  christos 		}
    695   1.1.1.6  christos #endif
    696       1.1  christos #else
    697   1.1.1.6  christos 		if(WSAGetLastError() != WSAEINPROGRESS &&
    698   1.1.1.6  christos 			WSAGetLastError() != WSAEWOULDBLOCK) {
    699   1.1.1.6  christos 			checkconnecterr(WSAGetLastError(), svr, &addr,
    700   1.1.1.6  christos 				addrlen, statuscmd, useport);
    701       1.1  christos 		}
    702       1.1  christos #endif
    703       1.1  christos 	}
    704   1.1.1.6  christos 	while(1) {
    705   1.1.1.6  christos 		fd_set rset, wset, eset;
    706   1.1.1.6  christos 		struct timeval tv;
    707   1.1.1.6  christos 		FD_ZERO(&rset);
    708   1.1.1.6  christos 		FD_SET(FD_SET_T fd, &rset);
    709   1.1.1.6  christos 		FD_ZERO(&wset);
    710   1.1.1.6  christos 		FD_SET(FD_SET_T fd, &wset);
    711   1.1.1.6  christos 		FD_ZERO(&eset);
    712   1.1.1.6  christos 		FD_SET(FD_SET_T fd, &eset);
    713   1.1.1.6  christos 		tv.tv_sec = UNBOUND_CONTROL_CONNECT_TIMEOUT/1000;
    714   1.1.1.6  christos 		tv.tv_usec= (UNBOUND_CONTROL_CONNECT_TIMEOUT%1000)*1000;
    715   1.1.1.6  christos 		if(select(fd+1, &rset, &wset, &eset, &tv) == -1) {
    716   1.1.1.6  christos 			fatal_exit("select: %s", sock_strerror(errno));
    717   1.1.1.6  christos 		}
    718   1.1.1.6  christos 		if(!FD_ISSET(fd, &rset) && !FD_ISSET(fd, &wset) &&
    719   1.1.1.6  christos 			!FD_ISSET(fd, &eset)) {
    720   1.1.1.6  christos 			fatal_exit("timeout: could not connect to server");
    721   1.1.1.6  christos 		} else {
    722   1.1.1.6  christos 			/* check nonblocking connect error */
    723   1.1.1.6  christos 			int error = 0;
    724   1.1.1.6  christos 			socklen_t len = (socklen_t)sizeof(error);
    725   1.1.1.6  christos 			if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
    726   1.1.1.6  christos 				&len) < 0) {
    727   1.1.1.6  christos #ifndef USE_WINSOCK
    728   1.1.1.6  christos 				error = errno; /* on solaris errno is error */
    729   1.1.1.6  christos #else
    730   1.1.1.6  christos 				error = WSAGetLastError();
    731   1.1.1.6  christos #endif
    732   1.1.1.6  christos 			}
    733   1.1.1.6  christos 			if(error != 0) {
    734   1.1.1.6  christos #ifndef USE_WINSOCK
    735   1.1.1.6  christos #ifdef EINPROGRESS
    736   1.1.1.6  christos 				if(error == EINPROGRESS)
    737   1.1.1.6  christos 					continue; /* try again later */
    738   1.1.1.6  christos #endif
    739   1.1.1.6  christos #ifdef EWOULDBLOCK
    740   1.1.1.6  christos 				if(error == EWOULDBLOCK)
    741   1.1.1.6  christos 					continue; /* try again later */
    742   1.1.1.6  christos #endif
    743   1.1.1.6  christos #else
    744   1.1.1.6  christos 				if(error == WSAEINPROGRESS)
    745   1.1.1.6  christos 					continue; /* try again later */
    746   1.1.1.6  christos 				if(error == WSAEWOULDBLOCK)
    747   1.1.1.6  christos 					continue; /* try again later */
    748   1.1.1.6  christos #endif
    749   1.1.1.6  christos 				checkconnecterr(error, svr, &addr, addrlen,
    750   1.1.1.6  christos 					statuscmd, useport);
    751   1.1.1.6  christos 			}
    752   1.1.1.6  christos 		}
    753   1.1.1.6  christos 		break;
    754   1.1.1.6  christos 	}
    755   1.1.1.6  christos 	fd_set_block(fd);
    756   1.1.1.7  christos 	config_del_strarray(rcif, num_rcif);
    757       1.1  christos 	return fd;
    758       1.1  christos }
    759       1.1  christos 
    760       1.1  christos /** setup SSL on the connection */
    761       1.1  christos static SSL*
    762   1.1.1.3  christos setup_ssl(SSL_CTX* ctx, int fd)
    763       1.1  christos {
    764       1.1  christos 	SSL* ssl;
    765       1.1  christos 	X509* x;
    766       1.1  christos 	int r;
    767       1.1  christos 
    768   1.1.1.3  christos 	if(!ctx) return NULL;
    769       1.1  christos 	ssl = SSL_new(ctx);
    770       1.1  christos 	if(!ssl)
    771       1.1  christos 		ssl_err("could not SSL_new");
    772       1.1  christos 	SSL_set_connect_state(ssl);
    773   1.1.1.5  christos 	(void)SSL_set_mode(ssl, (long)SSL_MODE_AUTO_RETRY);
    774       1.1  christos 	if(!SSL_set_fd(ssl, fd))
    775       1.1  christos 		ssl_err("could not SSL_set_fd");
    776       1.1  christos 	while(1) {
    777       1.1  christos 		ERR_clear_error();
    778       1.1  christos 		if( (r=SSL_do_handshake(ssl)) == 1)
    779       1.1  christos 			break;
    780       1.1  christos 		r = SSL_get_error(ssl, r);
    781       1.1  christos 		if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
    782       1.1  christos 			ssl_err("SSL handshake failed");
    783       1.1  christos 		/* wants to be called again */
    784       1.1  christos 	}
    785       1.1  christos 
    786       1.1  christos 	/* check authenticity of server */
    787       1.1  christos 	if(SSL_get_verify_result(ssl) != X509_V_OK)
    788       1.1  christos 		ssl_err("SSL verification failed");
    789   1.1.1.9  christos #ifdef HAVE_SSL_GET1_PEER_CERTIFICATE
    790   1.1.1.9  christos 	x = SSL_get1_peer_certificate(ssl);
    791   1.1.1.9  christos #else
    792   1.1.1.3  christos 	x = SSL_get_peer_certificate(ssl);
    793   1.1.1.9  christos #endif
    794   1.1.1.3  christos 	if(!x)
    795   1.1.1.3  christos 		ssl_err("Server presented no peer certificate");
    796   1.1.1.3  christos 	X509_free(x);
    797       1.1  christos 
    798       1.1  christos 	return ssl;
    799       1.1  christos }
    800       1.1  christos 
    801   1.1.1.3  christos /** read from ssl or fd, fatalexit on error, 0 EOF, 1 success */
    802   1.1.1.3  christos static int
    803   1.1.1.3  christos remote_read(SSL* ssl, int fd, char* buf, size_t len)
    804   1.1.1.3  christos {
    805   1.1.1.3  christos 	if(ssl) {
    806   1.1.1.3  christos 		int r;
    807   1.1.1.3  christos 		ERR_clear_error();
    808   1.1.1.3  christos 		if((r = SSL_read(ssl, buf, (int)len-1)) <= 0) {
    809   1.1.1.3  christos 			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
    810   1.1.1.3  christos 				/* EOF */
    811   1.1.1.3  christos 				return 0;
    812   1.1.1.3  christos 			}
    813   1.1.1.3  christos 			ssl_err("could not SSL_read");
    814   1.1.1.3  christos 		}
    815   1.1.1.3  christos 		buf[r] = 0;
    816   1.1.1.3  christos 	} else {
    817   1.1.1.3  christos 		ssize_t rr = recv(fd, buf, len-1, 0);
    818   1.1.1.3  christos 		if(rr <= 0) {
    819   1.1.1.3  christos 			if(rr == 0) {
    820   1.1.1.3  christos 				/* EOF */
    821   1.1.1.3  christos 				return 0;
    822   1.1.1.3  christos 			}
    823   1.1.1.6  christos 			fatal_exit("could not recv: %s", sock_strerror(errno));
    824   1.1.1.3  christos 		}
    825   1.1.1.3  christos 		buf[rr] = 0;
    826   1.1.1.3  christos 	}
    827   1.1.1.3  christos 	return 1;
    828   1.1.1.3  christos }
    829   1.1.1.3  christos 
    830   1.1.1.3  christos /** write to ssl or fd, fatalexit on error */
    831   1.1.1.3  christos static void
    832   1.1.1.3  christos remote_write(SSL* ssl, int fd, const char* buf, size_t len)
    833   1.1.1.3  christos {
    834   1.1.1.3  christos 	if(ssl) {
    835   1.1.1.3  christos 		if(SSL_write(ssl, buf, (int)len) <= 0)
    836   1.1.1.3  christos 			ssl_err("could not SSL_write");
    837   1.1.1.3  christos 	} else {
    838   1.1.1.3  christos 		if(send(fd, buf, len, 0) < (ssize_t)len) {
    839   1.1.1.6  christos 			fatal_exit("could not send: %s", sock_strerror(errno));
    840   1.1.1.3  christos 		}
    841   1.1.1.3  christos 	}
    842   1.1.1.3  christos }
    843   1.1.1.3  christos 
    844   1.1.1.5  christos /** check args, to see if too many args. Because when a file is sent it
    845   1.1.1.5  christos  * would wait for the terminal, and we can check for too many arguments,
    846   1.1.1.5  christos  * eg. user put arguments on the commandline. */
    847   1.1.1.5  christos static void
    848   1.1.1.5  christos check_args_for_listcmd(int argc, char* argv[])
    849   1.1.1.5  christos {
    850   1.1.1.5  christos 	if(argc >= 1 && (strcmp(argv[0], "local_zones") == 0 ||
    851   1.1.1.5  christos 		strcmp(argv[0], "local_zones_remove") == 0 ||
    852   1.1.1.5  christos 		strcmp(argv[0], "local_datas") == 0 ||
    853   1.1.1.5  christos 		strcmp(argv[0], "local_datas_remove") == 0) &&
    854   1.1.1.5  christos 		argc >= 2) {
    855   1.1.1.5  christos 		fatal_exit("too many arguments for command '%s', "
    856   1.1.1.5  christos 			"content is piped in from stdin", argv[0]);
    857   1.1.1.5  christos 	}
    858   1.1.1.6  christos 	if(argc >= 1 && (strcmp(argv[0], "view_local_datas") == 0 ||
    859   1.1.1.6  christos 		strcmp(argv[0], "view_local_datas_remove") == 0) &&
    860   1.1.1.5  christos 		argc >= 3) {
    861   1.1.1.5  christos 		fatal_exit("too many arguments for command '%s', "
    862   1.1.1.5  christos 			"content is piped in from stdin", argv[0]);
    863   1.1.1.5  christos 	}
    864   1.1.1.5  christos }
    865   1.1.1.5  christos 
    866       1.1  christos /** send stdin to server */
    867       1.1  christos static void
    868   1.1.1.3  christos send_file(SSL* ssl, int fd, FILE* in, char* buf, size_t sz)
    869       1.1  christos {
    870       1.1  christos 	while(fgets(buf, (int)sz, in)) {
    871   1.1.1.3  christos 		remote_write(ssl, fd, buf, strlen(buf));
    872       1.1  christos 	}
    873       1.1  christos }
    874       1.1  christos 
    875   1.1.1.2  christos /** send end-of-file marker to server */
    876   1.1.1.2  christos static void
    877   1.1.1.3  christos send_eof(SSL* ssl, int fd)
    878   1.1.1.2  christos {
    879   1.1.1.2  christos 	char e[] = {0x04, 0x0a};
    880   1.1.1.3  christos 	remote_write(ssl, fd, e, sizeof(e));
    881   1.1.1.2  christos }
    882   1.1.1.2  christos 
    883       1.1  christos /** send command and display result */
    884       1.1  christos static int
    885   1.1.1.3  christos go_cmd(SSL* ssl, int fd, int quiet, int argc, char* argv[])
    886       1.1  christos {
    887       1.1  christos 	char pre[10];
    888       1.1  christos 	const char* space=" ";
    889       1.1  christos 	const char* newline="\n";
    890       1.1  christos 	int was_error = 0, first_line = 1;
    891   1.1.1.3  christos 	int i;
    892       1.1  christos 	char buf[1024];
    893       1.1  christos 	snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
    894   1.1.1.3  christos 	remote_write(ssl, fd, pre, strlen(pre));
    895       1.1  christos 	for(i=0; i<argc; i++) {
    896   1.1.1.3  christos 		remote_write(ssl, fd, space, strlen(space));
    897   1.1.1.3  christos 		remote_write(ssl, fd, argv[i], strlen(argv[i]));
    898       1.1  christos 	}
    899   1.1.1.3  christos 	remote_write(ssl, fd, newline, strlen(newline));
    900       1.1  christos 
    901       1.1  christos 	if(argc == 1 && strcmp(argv[0], "load_cache") == 0) {
    902   1.1.1.3  christos 		send_file(ssl, fd, stdin, buf, sizeof(buf));
    903       1.1  christos 	}
    904   1.1.1.4  christos 	else if(argc >= 1 && (strcmp(argv[0], "local_zones") == 0 ||
    905   1.1.1.2  christos 		strcmp(argv[0], "local_zones_remove") == 0 ||
    906   1.1.1.2  christos 		strcmp(argv[0], "local_datas") == 0 ||
    907   1.1.1.4  christos 		strcmp(argv[0], "view_local_datas") == 0 ||
    908   1.1.1.6  christos 		strcmp(argv[0], "local_datas_remove") == 0 ||
    909   1.1.1.6  christos 		strcmp(argv[0], "view_local_datas_remove") == 0)) {
    910   1.1.1.3  christos 		send_file(ssl, fd, stdin, buf, sizeof(buf));
    911   1.1.1.3  christos 		send_eof(ssl, fd);
    912   1.1.1.2  christos 	}
    913       1.1  christos 
    914       1.1  christos 	while(1) {
    915   1.1.1.3  christos 		if(remote_read(ssl, fd, buf, sizeof(buf)) == 0) {
    916   1.1.1.3  christos 			break; /* EOF */
    917       1.1  christos 		}
    918       1.1  christos 		if(first_line && strncmp(buf, "error", 5) == 0) {
    919       1.1  christos 			printf("%s", buf);
    920       1.1  christos 			was_error = 1;
    921   1.1.1.7  christos 		} else if(!quiet) {
    922       1.1  christos 			printf("%s", buf);
    923   1.1.1.7  christos 		}
    924       1.1  christos 
    925       1.1  christos 		first_line = 0;
    926       1.1  christos 	}
    927       1.1  christos 	return was_error;
    928       1.1  christos }
    929       1.1  christos 
    930       1.1  christos /** go ahead and read config, contact server and perform command and display */
    931       1.1  christos static int
    932       1.1  christos go(const char* cfgfile, char* svr, int quiet, int argc, char* argv[])
    933       1.1  christos {
    934       1.1  christos 	struct config_file* cfg;
    935       1.1  christos 	int fd, ret;
    936       1.1  christos 	SSL_CTX* ctx;
    937       1.1  christos 	SSL* ssl;
    938       1.1  christos 
    939       1.1  christos 	/* read config */
    940       1.1  christos 	if(!(cfg = config_create()))
    941       1.1  christos 		fatal_exit("out of memory");
    942       1.1  christos 	if(!config_read(cfg, cfgfile, NULL))
    943       1.1  christos 		fatal_exit("could not read config file");
    944       1.1  christos 	if(!cfg->remote_control_enable)
    945       1.1  christos 		log_warn("control-enable is 'no' in the config file.");
    946       1.1  christos #ifdef UB_ON_WINDOWS
    947       1.1  christos 	w_config_adjust_directory(cfg);
    948       1.1  christos #endif
    949       1.1  christos 	ctx = setup_ctx(cfg);
    950       1.1  christos 
    951       1.1  christos 	/* contact server */
    952       1.1  christos 	fd = contact_server(svr, cfg, argc>0&&strcmp(argv[0],"status")==0);
    953   1.1.1.3  christos 	ssl = setup_ssl(ctx, fd);
    954       1.1  christos 
    955       1.1  christos 	/* send command */
    956   1.1.1.3  christos 	ret = go_cmd(ssl, fd, quiet, argc, argv);
    957       1.1  christos 
    958   1.1.1.3  christos 	if(ssl) SSL_free(ssl);
    959   1.1.1.6  christos 	sock_close(fd);
    960   1.1.1.3  christos 	if(ctx) SSL_CTX_free(ctx);
    961       1.1  christos 	config_delete(cfg);
    962       1.1  christos 	return ret;
    963       1.1  christos }
    964       1.1  christos 
    965       1.1  christos /** getopt global, in case header files fail to declare it. */
    966       1.1  christos extern int optind;
    967       1.1  christos /** getopt global, in case header files fail to declare it. */
    968       1.1  christos extern char* optarg;
    969       1.1  christos 
    970       1.1  christos /** Main routine for unbound-control */
    971       1.1  christos int main(int argc, char* argv[])
    972       1.1  christos {
    973       1.1  christos 	int c, ret;
    974       1.1  christos 	int quiet = 0;
    975       1.1  christos 	const char* cfgfile = CONFIGFILE;
    976       1.1  christos 	char* svr = NULL;
    977       1.1  christos #ifdef USE_WINSOCK
    978       1.1  christos 	int r;
    979       1.1  christos 	WSADATA wsa_data;
    980       1.1  christos #endif
    981       1.1  christos #ifdef USE_THREAD_DEBUG
    982   1.1.1.2  christos 	/* stop the file output from unbound-control, overwrites the servers */
    983       1.1  christos 	extern int check_locking_order;
    984       1.1  christos 	check_locking_order = 0;
    985       1.1  christos #endif /* USE_THREAD_DEBUG */
    986   1.1.1.7  christos 	checklock_start();
    987       1.1  christos 	log_ident_set("unbound-control");
    988       1.1  christos 	log_init(NULL, 0, NULL);
    989       1.1  christos #ifdef USE_WINSOCK
    990       1.1  christos 	/* use registry config file in preference to compiletime location */
    991       1.1  christos 	if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
    992       1.1  christos 		cfgfile = CONFIGFILE;
    993       1.1  christos #endif
    994       1.1  christos 	/* parse the options */
    995       1.1  christos 	while( (c=getopt(argc, argv, "c:s:qh")) != -1) {
    996       1.1  christos 		switch(c) {
    997       1.1  christos 		case 'c':
    998       1.1  christos 			cfgfile = optarg;
    999       1.1  christos 			break;
   1000       1.1  christos 		case 's':
   1001       1.1  christos 			svr = optarg;
   1002       1.1  christos 			break;
   1003       1.1  christos 		case 'q':
   1004       1.1  christos 			quiet = 1;
   1005       1.1  christos 			break;
   1006       1.1  christos 		case '?':
   1007       1.1  christos 		case 'h':
   1008       1.1  christos 		default:
   1009       1.1  christos 			usage();
   1010       1.1  christos 		}
   1011       1.1  christos 	}
   1012       1.1  christos 	argc -= optind;
   1013       1.1  christos 	argv += optind;
   1014       1.1  christos 	if(argc == 0)
   1015       1.1  christos 		usage();
   1016       1.1  christos 	if(argc >= 1 && strcmp(argv[0], "start")==0) {
   1017   1.1.1.6  christos #if (defined(TARGET_OS_TV) && TARGET_OS_TV) || (defined(TARGET_OS_WATCH) && TARGET_OS_WATCH)
   1018   1.1.1.6  christos 		fatal_exit("could not exec unbound: %s",
   1019   1.1.1.6  christos 			strerror(ENOSYS));
   1020   1.1.1.6  christos #else
   1021   1.1.1.8  christos 		if(execlp("unbound", "unbound", "-c", cfgfile,
   1022       1.1  christos 			(char*)NULL) < 0) {
   1023       1.1  christos 			fatal_exit("could not exec unbound: %s",
   1024       1.1  christos 				strerror(errno));
   1025       1.1  christos 		}
   1026   1.1.1.6  christos #endif
   1027       1.1  christos 	}
   1028   1.1.1.2  christos 	if(argc >= 1 && strcmp(argv[0], "stats_shm")==0) {
   1029   1.1.1.7  christos 		print_stats_shm(cfgfile, quiet);
   1030   1.1.1.2  christos 		return 0;
   1031   1.1.1.2  christos 	}
   1032   1.1.1.5  christos 	check_args_for_listcmd(argc, argv);
   1033   1.1.1.2  christos 
   1034   1.1.1.2  christos #ifdef USE_WINSOCK
   1035   1.1.1.2  christos 	if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0)
   1036   1.1.1.2  christos 		fatal_exit("WSAStartup failed: %s", wsa_strerror(r));
   1037   1.1.1.2  christos #endif
   1038   1.1.1.2  christos 
   1039   1.1.1.2  christos #ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
   1040   1.1.1.2  christos 	ERR_load_crypto_strings();
   1041   1.1.1.2  christos #endif
   1042   1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
   1043   1.1.1.2  christos 	ERR_load_SSL_strings();
   1044   1.1.1.2  christos #endif
   1045   1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
   1046   1.1.1.5  christos #  ifndef S_SPLINT_S
   1047   1.1.1.2  christos 	OpenSSL_add_all_algorithms();
   1048   1.1.1.5  christos #  endif
   1049   1.1.1.2  christos #else
   1050   1.1.1.2  christos 	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
   1051   1.1.1.2  christos 		| OPENSSL_INIT_ADD_ALL_DIGESTS
   1052   1.1.1.2  christos 		| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
   1053   1.1.1.2  christos #endif
   1054   1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
   1055   1.1.1.2  christos 	(void)SSL_library_init();
   1056   1.1.1.2  christos #else
   1057   1.1.1.2  christos 	(void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
   1058   1.1.1.2  christos #endif
   1059   1.1.1.2  christos 
   1060   1.1.1.2  christos 	if(!RAND_status()) {
   1061   1.1.1.2  christos 		/* try to seed it */
   1062   1.1.1.2  christos 		unsigned char buf[256];
   1063   1.1.1.2  christos 		unsigned int seed=(unsigned)time(NULL) ^ (unsigned)getpid();
   1064   1.1.1.2  christos 		unsigned int v = seed;
   1065   1.1.1.2  christos 		size_t i;
   1066   1.1.1.2  christos 		for(i=0; i<256/sizeof(v); i++) {
   1067   1.1.1.2  christos 			memmove(buf+i*sizeof(v), &v, sizeof(v));
   1068   1.1.1.2  christos 			v = v*seed + (unsigned int)i;
   1069   1.1.1.2  christos 		}
   1070   1.1.1.2  christos 		RAND_seed(buf, 256);
   1071   1.1.1.2  christos 		log_warn("no entropy, seeding openssl PRNG with time\n");
   1072   1.1.1.2  christos 	}
   1073       1.1  christos 
   1074       1.1  christos 	ret = go(cfgfile, svr, quiet, argc, argv);
   1075       1.1  christos 
   1076       1.1  christos #ifdef USE_WINSOCK
   1077   1.1.1.2  christos 	WSACleanup();
   1078       1.1  christos #endif
   1079       1.1  christos 	checklock_stop();
   1080       1.1  christos 	return ret;
   1081       1.1  christos }
   1082