unbound-control.c revision 249141
1/*
2 * checkconf/unbound-control.c - remote control utility for unbound.
3 *
4 * Copyright (c) 2008, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/**
37 * \file
38 *
39 * The remote control utility contacts the unbound server over ssl and
40 * sends the command, receives the answer, and displays the result
41 * from the commandline.
42 */
43
44#include "config.h"
45#ifdef HAVE_GETOPT_H
46#include <getopt.h>
47#endif
48#ifdef HAVE_OPENSSL_SSL_H
49#include <openssl/ssl.h>
50#endif
51#ifdef HAVE_OPENSSL_ERR_H
52#include <openssl/err.h>
53#endif
54#ifdef HAVE_OPENSSL_RAND_H
55#include <openssl/rand.h>
56#endif
57#include "util/log.h"
58#include "util/config_file.h"
59#include "util/locks.h"
60#include "util/net_help.h"
61
62/** Give unbound-control usage, and exit (1). */
63static void
64usage()
65{
66	printf("Usage:	unbound-control [options] command\n");
67	printf("	Remote control utility for unbound server.\n");
68	printf("Options:\n");
69	printf("  -c file	config file, default is %s\n", CONFIGFILE);
70	printf("  -s ip[@port]	server address, if omitted config is used.\n");
71	printf("  -q		quiet (don't print anything if it works ok).\n");
72	printf("  -h		show this usage help.\n");
73	printf("Commands:\n");
74	printf("  start				start server; runs unbound(8)\n");
75	printf("  stop				stops the server\n");
76	printf("  reload			reloads the server\n");
77	printf("  				(this flushes data, stats, requestlist)\n");
78	printf("  stats				print statistics\n");
79	printf("  stats_noreset			peek at statistics\n");
80	printf("  status			display status of server\n");
81	printf("  verbosity <number>		change logging detail\n");
82	printf("  log_reopen			close and open the logfile\n");
83	printf("  local_zone <name> <type>	add new local zone\n");
84	printf("  local_zone_remove <name>	remove local zone and its contents\n");
85	printf("  local_data <RR data...>	add local data, for example\n");
86	printf("				local_data www.example.com A 192.0.2.1\n");
87	printf("  local_data_remove <name>	remove local RR data from name\n");
88	printf("  dump_cache			print cache to stdout\n");
89	printf("  load_cache			load cache from stdin\n");
90	printf("  lookup <name>			print nameservers for name\n");
91	printf("  flush <name>			flushes common types for name from cache\n");
92	printf("  				types:  A, AAAA, MX, PTR, NS,\n");
93	printf("					SOA, CNAME, DNAME, SRV, NAPTR\n");
94	printf("  flush_type <name> <type>	flush name, type from cache\n");
95	printf("  flush_zone <name>		flush everything at or under name\n");
96	printf("  				from rr and dnssec caches\n");
97	printf("  flush_bogus			flush all bogus data\n");
98	printf("  flush_stats 			flush statistics, make zero\n");
99	printf("  flush_requestlist 		drop queries that are worked on\n");
100	printf("  dump_requestlist		show what is worked on\n");
101	printf("  flush_infra [all | ip] 	remove ping, edns for one IP or all\n");
102	printf("  dump_infra			show ping and edns entries\n");
103	printf("  set_option opt: val		set option to value, no reload\n");
104	printf("  get_option opt		get option value\n");
105	printf("  list_stubs			list stub-zones and root hints in use\n");
106	printf("  list_forwards			list forward-zones in use\n");
107	printf("  list_local_zones		list local-zones in use\n");
108	printf("  list_local_data		list local-data RRs in use\n");
109	printf("  forward_add [+i] zone addr..	add forward-zone with servers\n");
110	printf("  forward_remove [+i] zone	remove forward zone\n");
111	printf("  stub_add [+ip] zone addr..	add stub-zone with servers\n");
112	printf("  stub_remove [+i] zone		remove stub zone\n");
113	printf("		+i		also do dnssec insecure point\n");
114	printf("		+p		set stub to use priming\n");
115	printf("  forward [off | addr ...]	without arg show forward setup\n");
116	printf("				or off to turn off root forwarding\n");
117	printf("				or give list of ip addresses\n");
118	printf("Version %s\n", PACKAGE_VERSION);
119	printf("BSD licensed, see LICENSE in source package for details.\n");
120	printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
121	exit(1);
122}
123
124/** exit with ssl error */
125static void ssl_err(const char* s)
126{
127	fprintf(stderr, "error: %s\n", s);
128	ERR_print_errors_fp(stderr);
129	exit(1);
130}
131
132/** setup SSL context */
133static SSL_CTX*
134setup_ctx(struct config_file* cfg)
135{
136	char* s_cert, *c_key, *c_cert;
137	SSL_CTX* ctx;
138
139	s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
140	c_key = fname_after_chroot(cfg->control_key_file, cfg, 1);
141	c_cert = fname_after_chroot(cfg->control_cert_file, cfg, 1);
142	if(!s_cert || !c_key || !c_cert)
143		fatal_exit("out of memory");
144        ctx = SSL_CTX_new(SSLv23_client_method());
145	if(!ctx)
146		ssl_err("could not allocate SSL_CTX pointer");
147        if(!(SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2))
148		ssl_err("could not set SSL_OP_NO_SSLv2");
149	if(!SSL_CTX_use_certificate_file(ctx,c_cert,SSL_FILETYPE_PEM) ||
150		!SSL_CTX_use_PrivateKey_file(ctx,c_key,SSL_FILETYPE_PEM)
151		|| !SSL_CTX_check_private_key(ctx))
152		ssl_err("Error setting up SSL_CTX client key and cert");
153	if (SSL_CTX_load_verify_locations(ctx, s_cert, NULL) != 1)
154		ssl_err("Error setting up SSL_CTX verify, server cert");
155	SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
156
157	free(s_cert);
158	free(c_key);
159	free(c_cert);
160	return ctx;
161}
162
163/** contact the server with TCP connect */
164static int
165contact_server(const char* svr, struct config_file* cfg, int statuscmd)
166{
167	struct sockaddr_storage addr;
168	socklen_t addrlen;
169	int fd;
170	/* use svr or the first config entry */
171	if(!svr) {
172		if(cfg->control_ifs)
173			svr = cfg->control_ifs->str;
174		else	svr = "127.0.0.1";
175		/* config 0 addr (everything), means ask localhost */
176		if(strcmp(svr, "0.0.0.0") == 0)
177			svr = "127.0.0.1";
178		else if(strcmp(svr, "::0") == 0 ||
179			strcmp(svr, "0::0") == 0 ||
180			strcmp(svr, "0::") == 0 ||
181			strcmp(svr, "::") == 0)
182			svr = "::1";
183	}
184	if(strchr(svr, '@')) {
185		if(!extstrtoaddr(svr, &addr, &addrlen))
186			fatal_exit("could not parse IP@port: %s", svr);
187	} else {
188		if(!ipstrtoaddr(svr, cfg->control_port, &addr, &addrlen))
189			fatal_exit("could not parse IP: %s", svr);
190	}
191	fd = socket(addr_is_ip6(&addr, addrlen)?AF_INET6:AF_INET,
192		SOCK_STREAM, 0);
193	if(fd == -1) {
194#ifndef USE_WINSOCK
195		fatal_exit("socket: %s", strerror(errno));
196#else
197		fatal_exit("socket: %s", wsa_strerror(WSAGetLastError()));
198#endif
199	}
200	if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) {
201		log_addr(0, "address", &addr, addrlen);
202#ifndef USE_WINSOCK
203		log_err("connect: %s", strerror(errno));
204		if(errno == ECONNREFUSED && statuscmd) {
205			printf("unbound is stopped\n");
206			exit(3);
207		}
208#else
209		log_err("connect: %s", wsa_strerror(WSAGetLastError()));
210		if(WSAGetLastError() == WSAECONNREFUSED && statuscmd) {
211			printf("unbound is stopped\n");
212			exit(3);
213		}
214#endif
215		exit(1);
216	}
217	return fd;
218}
219
220/** setup SSL on the connection */
221static SSL*
222setup_ssl(SSL_CTX* ctx, int fd)
223{
224	SSL* ssl;
225	X509* x;
226	int r;
227
228	ssl = SSL_new(ctx);
229	if(!ssl)
230		ssl_err("could not SSL_new");
231	SSL_set_connect_state(ssl);
232	(void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
233	if(!SSL_set_fd(ssl, fd))
234		ssl_err("could not SSL_set_fd");
235	while(1) {
236		ERR_clear_error();
237		if( (r=SSL_do_handshake(ssl)) == 1)
238			break;
239		r = SSL_get_error(ssl, r);
240		if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
241			ssl_err("SSL handshake failed");
242		/* wants to be called again */
243	}
244
245	/* check authenticity of server */
246	if(SSL_get_verify_result(ssl) != X509_V_OK)
247		ssl_err("SSL verification failed");
248	x = SSL_get_peer_certificate(ssl);
249	if(!x)
250		ssl_err("Server presented no peer certificate");
251	X509_free(x);
252	return ssl;
253}
254
255/** send stdin to server */
256static void
257send_file(SSL* ssl, FILE* in, char* buf, size_t sz)
258{
259	while(fgets(buf, (int)sz, in)) {
260		if(SSL_write(ssl, buf, (int)strlen(buf)) <= 0)
261			ssl_err("could not SSL_write contents");
262	}
263}
264
265/** send command and display result */
266static int
267go_cmd(SSL* ssl, int quiet, int argc, char* argv[])
268{
269	char pre[10];
270	const char* space=" ";
271	const char* newline="\n";
272	int was_error = 0, first_line = 1;
273	int r, i;
274	char buf[1024];
275	snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
276	if(SSL_write(ssl, pre, (int)strlen(pre)) <= 0)
277		ssl_err("could not SSL_write");
278	for(i=0; i<argc; i++) {
279		if(SSL_write(ssl, space, (int)strlen(space)) <= 0)
280			ssl_err("could not SSL_write");
281		if(SSL_write(ssl, argv[i], (int)strlen(argv[i])) <= 0)
282			ssl_err("could not SSL_write");
283	}
284	if(SSL_write(ssl, newline, (int)strlen(newline)) <= 0)
285		ssl_err("could not SSL_write");
286
287	if(argc == 1 && strcmp(argv[0], "load_cache") == 0) {
288		send_file(ssl, stdin, buf, sizeof(buf));
289	}
290
291	while(1) {
292		ERR_clear_error();
293		if((r = SSL_read(ssl, buf, (int)sizeof(buf)-1)) <= 0) {
294			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
295				/* EOF */
296				break;
297			}
298			ssl_err("could not SSL_read");
299		}
300		buf[r] = 0;
301		if(first_line && strncmp(buf, "error", 5) == 0) {
302			printf("%s", buf);
303			was_error = 1;
304		} else if (!quiet)
305			printf("%s", buf);
306
307		first_line = 0;
308	}
309	return was_error;
310}
311
312/** go ahead and read config, contact server and perform command and display */
313static int
314go(const char* cfgfile, char* svr, int quiet, int argc, char* argv[])
315{
316	struct config_file* cfg;
317	int fd, ret;
318	SSL_CTX* ctx;
319	SSL* ssl;
320
321	/* read config */
322	if(!(cfg = config_create()))
323		fatal_exit("out of memory");
324	if(!config_read(cfg, cfgfile, NULL))
325		fatal_exit("could not read config file");
326	if(!cfg->remote_control_enable)
327		log_warn("control-enable is 'no' in the config file.");
328	ctx = setup_ctx(cfg);
329
330	/* contact server */
331	fd = contact_server(svr, cfg, argc>0&&strcmp(argv[0],"status")==0);
332	ssl = setup_ssl(ctx, fd);
333
334	/* send command */
335	ret = go_cmd(ssl, quiet, argc, argv);
336
337	SSL_free(ssl);
338#ifndef USE_WINSOCK
339	close(fd);
340#else
341	closesocket(fd);
342#endif
343	SSL_CTX_free(ctx);
344	config_delete(cfg);
345	return ret;
346}
347
348/** getopt global, in case header files fail to declare it. */
349extern int optind;
350/** getopt global, in case header files fail to declare it. */
351extern char* optarg;
352
353/** Main routine for unbound-control */
354int main(int argc, char* argv[])
355{
356	int c, ret;
357	int quiet = 0;
358	const char* cfgfile = CONFIGFILE;
359	char* svr = NULL;
360#ifdef USE_WINSOCK
361	int r;
362	WSADATA wsa_data;
363#endif
364#ifdef USE_THREAD_DEBUG
365	/* stop the file output from unbound-control, overwites the servers */
366	extern int check_locking_order;
367	check_locking_order = 0;
368#endif /* USE_THREAD_DEBUG */
369	log_ident_set("unbound-control");
370	log_init(NULL, 0, NULL);
371	checklock_start();
372#ifdef USE_WINSOCK
373	if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0)
374		fatal_exit("WSAStartup failed: %s", wsa_strerror(r));
375	/* use registry config file in preference to compiletime location */
376	if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
377		cfgfile = CONFIGFILE;
378#endif
379
380	ERR_load_crypto_strings();
381	ERR_load_SSL_strings();
382	OpenSSL_add_all_algorithms();
383	(void)SSL_library_init();
384
385	if(!RAND_status()) {
386                /* try to seed it */
387                unsigned char buf[256];
388                unsigned int seed=(unsigned)time(NULL) ^ (unsigned)getpid();
389		unsigned int v = seed;
390                size_t i;
391                for(i=0; i<256/sizeof(v); i++) {
392                        memmove(buf+i*sizeof(v), &v, sizeof(v));
393                        v = v*seed + (unsigned int)i;
394                }
395                RAND_seed(buf, 256);
396		log_warn("no entropy, seeding openssl PRNG with time\n");
397	}
398
399	/* parse the options */
400	while( (c=getopt(argc, argv, "c:s:qh")) != -1) {
401		switch(c) {
402		case 'c':
403			cfgfile = optarg;
404			break;
405		case 's':
406			svr = optarg;
407			break;
408		case 'q':
409			quiet = 1;
410			break;
411		case '?':
412		case 'h':
413		default:
414			usage();
415		}
416	}
417	argc -= optind;
418	argv += optind;
419	if(argc == 0)
420		usage();
421	if(argc >= 1 && strcmp(argv[0], "start")==0) {
422		if(execlp("unbound", "unbound", "-c", cfgfile,
423			(char*)NULL) < 0) {
424			fatal_exit("could not exec unbound: %s",
425				strerror(errno));
426		}
427	}
428
429	ret = go(cfgfile, svr, quiet, argc, argv);
430
431#ifdef USE_WINSOCK
432        WSACleanup();
433#endif
434	checklock_stop();
435	return ret;
436}
437