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