xref: /openbsd-src/usr.sbin/nsd/nsd-control.c (revision bf87c3c07c3ad89262e2b8cae09f17e70aa9e1ee)
1d3fecca9Ssthen /*
2d3fecca9Ssthen  * nsd-control.c - remote control utility for nsd.
3d3fecca9Ssthen  *
4d3fecca9Ssthen  * Copyright (c) 2011, NLnet Labs. All rights reserved.
5d3fecca9Ssthen  *
6d3fecca9Ssthen  * This software is open source.
7d3fecca9Ssthen  *
8d3fecca9Ssthen  * Redistribution and use in source and binary forms, with or without
9d3fecca9Ssthen  * modification, are permitted provided that the following conditions
10d3fecca9Ssthen  * are met:
11d3fecca9Ssthen  *
12d3fecca9Ssthen  * Redistributions of source code must retain the above copyright notice,
13d3fecca9Ssthen  * this list of conditions and the following disclaimer.
14d3fecca9Ssthen  *
15d3fecca9Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16d3fecca9Ssthen  * this list of conditions and the following disclaimer in the documentation
17d3fecca9Ssthen  * and/or other materials provided with the distribution.
18d3fecca9Ssthen  *
19d3fecca9Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20d3fecca9Ssthen  * be used to endorse or promote products derived from this software without
21d3fecca9Ssthen  * specific prior written permission.
22d3fecca9Ssthen  *
23d3fecca9Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24cbbc2d6cSbrad  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25cbbc2d6cSbrad  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26cbbc2d6cSbrad  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27cbbc2d6cSbrad  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28cbbc2d6cSbrad  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29cbbc2d6cSbrad  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30cbbc2d6cSbrad  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31cbbc2d6cSbrad  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32cbbc2d6cSbrad  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33cbbc2d6cSbrad  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34d3fecca9Ssthen  */
35d3fecca9Ssthen 
36d3fecca9Ssthen /**
37d3fecca9Ssthen  * \file
38d3fecca9Ssthen  *
39d3fecca9Ssthen  * The remote control utility contacts the nsd server over ssl and
40d3fecca9Ssthen  * sends the command, receives the answer, and displays the result
41d3fecca9Ssthen  * from the commandline.
42d3fecca9Ssthen  */
43d3fecca9Ssthen 
44d3fecca9Ssthen #include "config.h"
45eab1363eSsthen #include <stdio.h>
463f21e8ccSflorian #include <stdlib.h>
473efee2e1Sflorian 
483efee2e1Sflorian struct region;
493efee2e1Sflorian struct domain_table;
503efee2e1Sflorian struct zone;
513efee2e1Sflorian struct domain;
zonec_parse_string(struct region * ATTR_UNUSED (region),struct domain_table * ATTR_UNUSED (domains),struct zone * ATTR_UNUSED (zone),char * ATTR_UNUSED (str),struct domain ** ATTR_UNUSED (parsed),int * ATTR_UNUSED (num_rrs))523efee2e1Sflorian int zonec_parse_string(struct region* ATTR_UNUSED(region),
533efee2e1Sflorian 	struct domain_table* ATTR_UNUSED(domains),
543efee2e1Sflorian 	struct zone* ATTR_UNUSED(zone), char* ATTR_UNUSED(str),
553efee2e1Sflorian 	struct domain** ATTR_UNUSED(parsed), int* ATTR_UNUSED(num_rrs))
563efee2e1Sflorian {
573efee2e1Sflorian 	return 0;
583efee2e1Sflorian }
593efee2e1Sflorian 
60d3fecca9Ssthen #include <sys/types.h>
61d3fecca9Ssthen #include <unistd.h>
62d3fecca9Ssthen #include <string.h>
633efee2e1Sflorian #include <errno.h>
643efee2e1Sflorian #ifdef HAVE_SSL
65d3fecca9Ssthen #ifdef HAVE_OPENSSL_SSL_H
66d3fecca9Ssthen #include <openssl/ssl.h>
67d3fecca9Ssthen #endif
68d3fecca9Ssthen #ifdef HAVE_OPENSSL_ERR_H
69d3fecca9Ssthen #include <openssl/err.h>
70d3fecca9Ssthen #endif
71d3fecca9Ssthen #ifdef HAVE_OPENSSL_RAND_H
72d3fecca9Ssthen #include <openssl/rand.h>
73d3fecca9Ssthen #endif
743efee2e1Sflorian #endif /* HAVE_SSL */
7518e77612Sflorian #ifdef HAVE_SYS_UN_H
7618e77612Sflorian #include <sys/un.h>
7718e77612Sflorian #endif
788d298c9fSsthen #include <fcntl.h>
798d298c9fSsthen #ifndef AF_LOCAL
808d298c9fSsthen #define AF_LOCAL AF_UNIX
818d298c9fSsthen #endif
82d3fecca9Ssthen #include "util.h"
83d3fecca9Ssthen #include "tsig.h"
84d3fecca9Ssthen #include "options.h"
854564029fSflorian #include "zonec.h"
86d3fecca9Ssthen 
873b24e79eSsthen static void usage(void) ATTR_NORETURN;
883efee2e1Sflorian #ifdef HAVE_SSL
894b6a9f59Sflorian static void ssl_err(const char* s) ATTR_NORETURN;
904b6a9f59Sflorian static void ssl_path_err(const char* s, const char *path) ATTR_NORETURN;
913efee2e1Sflorian #else
923efee2e1Sflorian /* define SSL to use as a boolean to turn it off in function calls. */
933efee2e1Sflorian #define SSL int
943efee2e1Sflorian #endif
954b6a9f59Sflorian 
968d298c9fSsthen /** timeout to wait for connection over stream, in msec */
978d298c9fSsthen #define NSD_CONTROL_CONNECT_TIMEOUT 5000
988d298c9fSsthen 
99d3fecca9Ssthen /** Give nsd-control usage, and exit (1). */
100d3fecca9Ssthen static void
usage()101d3fecca9Ssthen usage()
102d3fecca9Ssthen {
103d3fecca9Ssthen 	printf("Usage:	nsd-control [options] command\n");
104d3fecca9Ssthen 	printf("	Remote control utility for nsd server.\n");
105d3fecca9Ssthen 	printf("Version %s. Report bugs to <%s>.\n",
106d3fecca9Ssthen 		PACKAGE_VERSION, PACKAGE_BUGREPORT);
107d3fecca9Ssthen 	printf("Options:\n");
108d3fecca9Ssthen 	printf("  -c file	config file, default is %s\n", CONFIGFILE);
109d3fecca9Ssthen 	printf("  -s ip[@port]	server address, if omitted config is used.\n");
110d3fecca9Ssthen 	printf("  -h		show this usage help.\n");
111d3fecca9Ssthen 	printf("Commands:\n");
112d3fecca9Ssthen 	printf("  start				start server; runs nsd(8)\n");
113d3fecca9Ssthen 	printf("  stop				stops the server\n");
114d3fecca9Ssthen 	printf("  reload [<zone>]		reload modified zonefiles from disk\n");
115d3fecca9Ssthen 	printf("  reconfig			reload the config file\n");
116d3fecca9Ssthen 	printf("  repattern			the same as reconfig\n");
117d3fecca9Ssthen 	printf("  log_reopen			reopen logfile (for log rotate)\n");
118d3fecca9Ssthen 	printf("  status			display status of server\n");
119d3fecca9Ssthen 	printf("  stats				print statistics\n");
120d3fecca9Ssthen 	printf("  stats_noreset			peek at statistics\n");
121d3fecca9Ssthen 	printf("  addzone <name> <pattern>	add a new zone\n");
122d3fecca9Ssthen 	printf("  delzone <name>		remove a zone\n");
123e02bc0dfSflorian 	printf("  changezone <name> <pattern>	change zone to use pattern\n");
124c939baa4Ssthen 	printf("  addzones			add zone list on stdin {name space pattern newline}\n");
125c939baa4Ssthen 	printf("  delzones			remove zone list on stdin {name newline}\n");
126d3fecca9Ssthen 	printf("  write [<zone>]		write changed zonefiles to disk\n");
127*bf87c3c0Sflorian 	printf("  notify [<zone>]		send NOTIFY messages to secondary servers\n");
128*bf87c3c0Sflorian 	printf("  transfer [<zone>]		try to update secondary zones to newer serial\n");
129*bf87c3c0Sflorian 	printf("  force_transfer [<zone>]	update secondary zones with AXFR, no serial check\n");
130d3fecca9Ssthen 	printf("  zonestatus [<zone>]		print state, serial, activity\n");
131d3fecca9Ssthen 	printf("  serverpid			get pid of server process\n");
132d3fecca9Ssthen 	printf("  verbosity <number>		change logging detail\n");
133a1bac035Sflorian 	printf("  print_tsig [<key_name>]	print tsig with <name> the secret and algo\n");
134a1bac035Sflorian 	printf("  update_tsig <name> <secret>	change existing tsig with <name> to a new <secret>\n");
135a1bac035Sflorian 	printf("  add_tsig <name> <secret> [algo] add new key with the given parameters\n");
136a1bac035Sflorian 	printf("  assoc_tsig <zone> <key_name>	associate <zone> with given tsig <key_name> name\n");
137a1bac035Sflorian 	printf("  del_tsig <key_name>		delete tsig <key_name> from configuration\n");
138063644e9Sflorian 	printf("  add_cookie_secret <secret>	add (or replace) a new cookie secret <secret>\n");
139063644e9Sflorian 	printf("  drop_cookie_secret		drop a staging cookie secret\n");
140063644e9Sflorian 	printf("  activate_cookie_secret	make a staging cookie secret active\n");
141063644e9Sflorian 	printf("  print_cookie_secrets		show all cookie secrets with their status\n");
142d3fecca9Ssthen 	exit(1);
143d3fecca9Ssthen }
144d3fecca9Ssthen 
1453efee2e1Sflorian #ifdef HAVE_SSL
146d3fecca9Ssthen /** exit with ssl error */
ssl_err(const char * s)147d3fecca9Ssthen static void ssl_err(const char* s)
148d3fecca9Ssthen {
149d3fecca9Ssthen 	fprintf(stderr, "error: %s\n", s);
150d3fecca9Ssthen 	ERR_print_errors_fp(stderr);
151d3fecca9Ssthen 	exit(1);
152d3fecca9Ssthen }
153d3fecca9Ssthen 
1544b6a9f59Sflorian /** exit with ssl error related to a file path */
ssl_path_err(const char * s,const char * path)1554b6a9f59Sflorian static void ssl_path_err(const char* s, const char *path)
1564b6a9f59Sflorian {
1574b6a9f59Sflorian 	unsigned long err;
1584b6a9f59Sflorian 	err = ERR_peek_error();
159a904e103Sflorian 	if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
1604b6a9f59Sflorian 		fprintf(stderr, "error: %s\n%s: %s\n",
1614b6a9f59Sflorian 			s, path, ERR_reason_error_string(err));
1624b6a9f59Sflorian 		exit(1);
1634b6a9f59Sflorian 	} else {
1644b6a9f59Sflorian 		ssl_err(s);
1654b6a9f59Sflorian 	}
1664b6a9f59Sflorian }
1674b6a9f59Sflorian 
168d3fecca9Ssthen /** setup SSL context */
169d3fecca9Ssthen static SSL_CTX*
setup_ctx(struct nsd_options * cfg)170fe5fe5f6Sflorian setup_ctx(struct nsd_options* cfg)
171d3fecca9Ssthen {
172d3fecca9Ssthen 	char* s_cert, *c_key, *c_cert;
173d3fecca9Ssthen 	SSL_CTX* ctx;
174d3fecca9Ssthen 
17518e77612Sflorian 	if(!options_remote_is_address(cfg))
17618e77612Sflorian 		return NULL;
177d3fecca9Ssthen 	s_cert = cfg->server_cert_file;
178d3fecca9Ssthen 	c_key = cfg->control_key_file;
179d3fecca9Ssthen 	c_cert = cfg->control_cert_file;
180d3fecca9Ssthen 
181d3fecca9Ssthen 	/* filenames may be relative to zonesdir */
182d3fecca9Ssthen 	if (cfg->zonesdir && cfg->zonesdir[0] &&
183d3fecca9Ssthen 		(s_cert[0] != '/' || c_key[0] != '/' || c_cert[0] != '/')) {
184d3fecca9Ssthen 		if(chdir(cfg->zonesdir))
1854b6a9f59Sflorian 			error("could not chdir to zonesdir: %s %s",
1864b6a9f59Sflorian 				cfg->zonesdir, strerror(errno));
187d3fecca9Ssthen 	}
188d3fecca9Ssthen 
189d3fecca9Ssthen         ctx = SSL_CTX_new(SSLv23_client_method());
190d3fecca9Ssthen 	if(!ctx)
191d3fecca9Ssthen 		ssl_err("could not allocate SSL_CTX pointer");
192308d2509Sflorian #if SSL_OP_NO_SSLv2 != 0
1933c667526Sdoug         if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)
1943c667526Sdoug 		!= SSL_OP_NO_SSLv2)
195d3fecca9Ssthen 		ssl_err("could not set SSL_OP_NO_SSLv2");
196308d2509Sflorian #endif
1973c667526Sdoug         if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)
1983c667526Sdoug 		!= SSL_OP_NO_SSLv3)
1999e506f0aSbrad 		ssl_err("could not set SSL_OP_NO_SSLv3");
200eab1363eSsthen #if defined(SSL_OP_NO_RENEGOTIATION)
201eab1363eSsthen 	/* disable client renegotiation */
202eab1363eSsthen 	if((SSL_CTX_set_options(ctx, SSL_OP_NO_RENEGOTIATION) &
203eab1363eSsthen 		SSL_OP_NO_RENEGOTIATION) != SSL_OP_NO_RENEGOTIATION)
204eab1363eSsthen 		ssl_err("could not set SSL_OP_NO_RENEGOTIATION");
205eab1363eSsthen #endif
2064b6a9f59Sflorian 	if(!SSL_CTX_use_certificate_file(ctx,c_cert,SSL_FILETYPE_PEM))
2074b6a9f59Sflorian 		ssl_path_err("Error setting up SSL_CTX client cert", c_cert);
2084b6a9f59Sflorian 	if(!SSL_CTX_use_PrivateKey_file(ctx,c_key,SSL_FILETYPE_PEM))
2094b6a9f59Sflorian 		ssl_path_err("Error setting up SSL_CTX client key", c_key);
2104b6a9f59Sflorian 	if(!SSL_CTX_check_private_key(ctx))
2114b6a9f59Sflorian 		ssl_err("Error setting up SSL_CTX client key");
212d3fecca9Ssthen 	if (SSL_CTX_load_verify_locations(ctx, s_cert, NULL) != 1)
2134b6a9f59Sflorian 		ssl_path_err("Error setting up SSL_CTX verify, server cert",
2144b6a9f59Sflorian 			s_cert);
215d3fecca9Ssthen 	SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
216d3fecca9Ssthen 
217d3fecca9Ssthen 	return ctx;
218d3fecca9Ssthen }
2193efee2e1Sflorian #endif /* HAVE_SSL */
220d3fecca9Ssthen 
2218d298c9fSsthen /** check connect error */
2228d298c9fSsthen static void
checkconnecterr(int err,const char * svr,int port,int statuscmd)2238d298c9fSsthen checkconnecterr(int err, const char* svr, int port, int statuscmd)
2248d298c9fSsthen {
2258d298c9fSsthen 	if(!port) fprintf(stderr, "error: connect (%s): %s\n", svr,
2268d298c9fSsthen 		strerror(err));
2278d298c9fSsthen 	else fprintf(stderr, "error: connect (%s@%d): %s\n", svr, port,
2288d298c9fSsthen 		strerror(err));
2298d298c9fSsthen 	if(err == ECONNREFUSED && statuscmd) {
2308d298c9fSsthen 		printf("nsd is stopped\n");
2318d298c9fSsthen 		exit(3);
2328d298c9fSsthen 	}
2338d298c9fSsthen 	exit(1);
2348d298c9fSsthen }
2358d298c9fSsthen 
236d3fecca9Ssthen /** contact the server with TCP connect */
237d3fecca9Ssthen static int
contact_server(const char * svr,struct nsd_options * cfg,int statuscmd)238fe5fe5f6Sflorian contact_server(const char* svr, struct nsd_options* cfg, int statuscmd)
239d3fecca9Ssthen {
240d3fecca9Ssthen #ifdef INET6
241d3fecca9Ssthen 	struct sockaddr_storage addr;
242d3fecca9Ssthen #else
243d3fecca9Ssthen 	struct sockaddr_in addr;
244d3fecca9Ssthen #endif
245d3fecca9Ssthen 	socklen_t addrlen;
246d3fecca9Ssthen 	int fd;
247d3fecca9Ssthen 	int port = cfg->control_port;
24818e77612Sflorian 	int addrfamily = 0;
249d3fecca9Ssthen 	/* use svr or a config entry */
250d3fecca9Ssthen 	if(!svr) {
251fe5fe5f6Sflorian 		if(cfg->control_interface) {
252d3fecca9Ssthen 			svr = cfg->control_interface->address;
253fe5fe5f6Sflorian 		} else if(cfg->do_ip4) {
254fe5fe5f6Sflorian 			svr = "127.0.0.1";
255fe5fe5f6Sflorian 		} else {
256fe5fe5f6Sflorian 			svr = "::1";
257fe5fe5f6Sflorian 		}
258d3fecca9Ssthen 		/* config 0 addr (everything), means ask localhost */
259d3fecca9Ssthen 		if(strcmp(svr, "0.0.0.0") == 0)
260d3fecca9Ssthen 			svr = "127.0.0.1";
261d3fecca9Ssthen 		else if(strcmp(svr, "::0") == 0 ||
262d3fecca9Ssthen 			strcmp(svr, "0::0") == 0 ||
263d3fecca9Ssthen 			strcmp(svr, "0::") == 0 ||
264d3fecca9Ssthen 			strcmp(svr, "::") == 0)
265d3fecca9Ssthen 			svr = "::1";
266d3fecca9Ssthen 	}
267d3fecca9Ssthen 	if(strchr(svr, '@')) {
268d3fecca9Ssthen 		char* ps = strchr(svr, '@');
269d3fecca9Ssthen 		*ps++ = 0;
270d3fecca9Ssthen 		port = atoi(ps);
271d3fecca9Ssthen 		if(!port) {
272d3fecca9Ssthen 			fprintf(stderr, "could not parse port %s\n", ps);
273d3fecca9Ssthen 			exit(1);
274d3fecca9Ssthen 		}
275d3fecca9Ssthen 	}
27618e77612Sflorian 	if(svr[0] == '/') {
27718e77612Sflorian #ifdef HAVE_SYS_UN_H
27818e77612Sflorian 		struct sockaddr_un* usock = (struct sockaddr_un *) &addr;
27918e77612Sflorian 		usock->sun_family = AF_LOCAL;
28018e77612Sflorian #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
28118e77612Sflorian 		usock->sun_len = (unsigned)sizeof(usock);
28218e77612Sflorian #endif
28318e77612Sflorian 		(void)strlcpy(usock->sun_path, svr, sizeof(usock->sun_path));
28418e77612Sflorian 		addrlen = (socklen_t)sizeof(struct sockaddr_un);
28518e77612Sflorian 		addrfamily = AF_LOCAL;
28618e77612Sflorian 		port = 0;
28718e77612Sflorian #endif
2885435475dSsthen #ifdef INET6
28918e77612Sflorian 	} else if(strchr(svr, ':')) {
290d3fecca9Ssthen 		struct sockaddr_in6 sa;
291d3fecca9Ssthen 		addrlen = (socklen_t)sizeof(struct sockaddr_in6);
292d3fecca9Ssthen 		memset(&sa, 0, addrlen);
293d3fecca9Ssthen 		sa.sin6_family = AF_INET6;
294d3fecca9Ssthen 		sa.sin6_port = (in_port_t)htons((uint16_t)port);
295d3fecca9Ssthen 		if(inet_pton((int)sa.sin6_family, svr, &sa.sin6_addr) <= 0) {
296d3fecca9Ssthen 			fprintf(stderr, "could not parse IP: %s\n", svr);
297d3fecca9Ssthen 			exit(1);
298d3fecca9Ssthen 		}
299d3fecca9Ssthen 		memcpy(&addr, &sa, addrlen);
30018e77612Sflorian 		addrfamily = AF_INET6;
3015435475dSsthen #endif
302d3fecca9Ssthen 	} else { /* ip4 */
303d3fecca9Ssthen 		struct sockaddr_in sa;
304d3fecca9Ssthen 		addrlen = (socklen_t)sizeof(struct sockaddr_in);
305d3fecca9Ssthen 		memset(&sa, 0, addrlen);
306d3fecca9Ssthen 		sa.sin_family = AF_INET;
307d3fecca9Ssthen 		sa.sin_port = (in_port_t)htons((uint16_t)port);
308d3fecca9Ssthen 		if(inet_pton((int)sa.sin_family, svr, &sa.sin_addr) <= 0) {
309d3fecca9Ssthen 			fprintf(stderr, "could not parse IP: %s\n", svr);
310d3fecca9Ssthen 			exit(1);
311d3fecca9Ssthen 		}
312d3fecca9Ssthen 		memcpy(&addr, &sa, addrlen);
31318e77612Sflorian 		addrfamily = AF_INET;
314d3fecca9Ssthen 	}
315d3fecca9Ssthen 
31618e77612Sflorian 	fd = socket(addrfamily, SOCK_STREAM, 0);
317d3fecca9Ssthen 	if(fd == -1) {
318d3fecca9Ssthen 		fprintf(stderr, "socket: %s\n", strerror(errno));
319d3fecca9Ssthen 		exit(1);
320d3fecca9Ssthen 	}
3218d298c9fSsthen 	if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
3228d298c9fSsthen 		fprintf(stderr, "error: set nonblocking: fcntl: %s",
3238d298c9fSsthen 			strerror(errno));
324d3fecca9Ssthen 	}
3258d298c9fSsthen 	if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) {
3268d298c9fSsthen 		if(errno != EINPROGRESS) {
3278d298c9fSsthen 			checkconnecterr(errno, svr, port, statuscmd);
3288d298c9fSsthen 		}
3298d298c9fSsthen 	}
3308d298c9fSsthen 	while(1) {
3318d298c9fSsthen 		fd_set rset, wset, eset;
3328d298c9fSsthen 		struct timeval tv;
3338d298c9fSsthen 		FD_ZERO(&rset);
3348d298c9fSsthen 		FD_SET(fd, &rset);
3358d298c9fSsthen 		FD_ZERO(&wset);
3368d298c9fSsthen 		FD_SET(fd, &wset);
3378d298c9fSsthen 		FD_ZERO(&eset);
3388d298c9fSsthen 		FD_SET(fd, &eset);
3398d298c9fSsthen 		tv.tv_sec = NSD_CONTROL_CONNECT_TIMEOUT/1000;
3408d298c9fSsthen 		tv.tv_usec= (NSD_CONTROL_CONNECT_TIMEOUT%1000)*1000;
3418d298c9fSsthen 		if(select(fd+1, &rset, &wset, &eset, &tv) == -1) {
3428d298c9fSsthen 			fprintf(stderr, "select: %s\n", strerror(errno));
343d3fecca9Ssthen 			exit(1);
344d3fecca9Ssthen 		}
3458d298c9fSsthen 		if(!FD_ISSET(fd, &rset) && !FD_ISSET(fd, &wset) &&
3468d298c9fSsthen 			!FD_ISSET(fd, &eset)) {
3478d298c9fSsthen 			fprintf(stderr, "timeout: could not connect to server\n");
3488d298c9fSsthen 			exit(1);
3498d298c9fSsthen 		} else {
3508d298c9fSsthen 			/* check nonblocking connect error */
3518d298c9fSsthen 			int error = 0;
3528d298c9fSsthen 			socklen_t len = (socklen_t)sizeof(error);
3538d298c9fSsthen 			if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
3548d298c9fSsthen 				&len) < 0) {
3558d298c9fSsthen 				error = errno; /* on solaris errno is error */
3568d298c9fSsthen 			}
3578d298c9fSsthen 			if(error != 0) {
3588d298c9fSsthen 				if(error == EINPROGRESS || error == EWOULDBLOCK)
3598d298c9fSsthen 					continue; /* try again later */
3608d298c9fSsthen 				checkconnecterr(error, svr, port, statuscmd);
3618d298c9fSsthen 			}
3628d298c9fSsthen 		}
3638d298c9fSsthen 		break;
3648d298c9fSsthen 	}
3658d298c9fSsthen 	if(fcntl(fd, F_SETFL, 0) == -1) {
3668d298c9fSsthen 		fprintf(stderr, "error: set blocking: fcntl: %s",
3678d298c9fSsthen 			strerror(errno));
3688d298c9fSsthen 	}
369d3fecca9Ssthen 	return fd;
370d3fecca9Ssthen }
371d3fecca9Ssthen 
3723efee2e1Sflorian #ifdef HAVE_SSL
373d3fecca9Ssthen /** setup SSL on the connection */
374d3fecca9Ssthen static SSL*
setup_ssl(SSL_CTX * ctx,int fd)375d3fecca9Ssthen setup_ssl(SSL_CTX* ctx, int fd)
376d3fecca9Ssthen {
377d3fecca9Ssthen 	SSL* ssl;
378d3fecca9Ssthen 	X509* x;
379d3fecca9Ssthen 	int r;
380d3fecca9Ssthen 
38118e77612Sflorian 	if(!ctx) return NULL;
382d3fecca9Ssthen 	ssl = SSL_new(ctx);
383d3fecca9Ssthen 	if(!ssl)
384d3fecca9Ssthen 		ssl_err("could not SSL_new");
385d3fecca9Ssthen 	SSL_set_connect_state(ssl);
386d3fecca9Ssthen 	(void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
387d3fecca9Ssthen 	if(!SSL_set_fd(ssl, fd))
388d3fecca9Ssthen 		ssl_err("could not SSL_set_fd");
389d3fecca9Ssthen 	while(1) {
390d3fecca9Ssthen 		ERR_clear_error();
391d3fecca9Ssthen 		if( (r=SSL_do_handshake(ssl)) == 1)
392d3fecca9Ssthen 			break;
393d3fecca9Ssthen 		r = SSL_get_error(ssl, r);
394d3fecca9Ssthen 		if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
395d3fecca9Ssthen 			ssl_err("SSL handshake failed");
396d3fecca9Ssthen 		/* wants to be called again */
397d3fecca9Ssthen 	}
398d3fecca9Ssthen 
399d3fecca9Ssthen 	/* check authenticity of server */
400d3fecca9Ssthen 	if(SSL_get_verify_result(ssl) != X509_V_OK)
401d3fecca9Ssthen 		ssl_err("SSL verification failed");
402d3fecca9Ssthen 	x = SSL_get_peer_certificate(ssl);
403d3fecca9Ssthen 	if(!x)
404d3fecca9Ssthen 		ssl_err("Server presented no peer certificate");
405d3fecca9Ssthen 	X509_free(x);
406d3fecca9Ssthen 	return ssl;
407d3fecca9Ssthen }
4083efee2e1Sflorian #endif /* HAVE_SSL */
409d3fecca9Ssthen 
41018e77612Sflorian /** read from ssl or fd, fatalexit on error, 0 EOF, 1 success */
41118e77612Sflorian static int
remote_read(SSL * ssl,int fd,char * buf,size_t len)41218e77612Sflorian remote_read(SSL* ssl, int fd, char* buf, size_t len)
41318e77612Sflorian {
41418e77612Sflorian 	if(ssl) {
4153efee2e1Sflorian #ifdef HAVE_SSL
41618e77612Sflorian 		int r;
41718e77612Sflorian 		ERR_clear_error();
41818e77612Sflorian 		if((r = SSL_read(ssl, buf, (int)len-1)) <= 0) {
41918e77612Sflorian 			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
42018e77612Sflorian 				/* EOF */
42118e77612Sflorian 				return 0;
42218e77612Sflorian 			}
42318e77612Sflorian 			ssl_err("could not SSL_read");
42418e77612Sflorian 		}
42518e77612Sflorian 		buf[r] = 0;
4263efee2e1Sflorian #endif /* HAVE_SSL */
42718e77612Sflorian 	} else {
42818e77612Sflorian 		ssize_t rr = read(fd, buf, len-1);
42918e77612Sflorian 		if(rr <= 0) {
43018e77612Sflorian 			if(rr == 0) {
43118e77612Sflorian 				/* EOF */
43218e77612Sflorian 				return 0;
43318e77612Sflorian 			}
43418e77612Sflorian 			fprintf(stderr, "could not read: %s\n",
43518e77612Sflorian 				strerror(errno));
43618e77612Sflorian 			exit(1);
43718e77612Sflorian 		}
43818e77612Sflorian 		buf[rr] = 0;
43918e77612Sflorian 	}
44018e77612Sflorian 	return 1;
44118e77612Sflorian }
44218e77612Sflorian 
44318e77612Sflorian /** write to ssl or fd, fatalexit on error */
44418e77612Sflorian static void
remote_write(SSL * ssl,int fd,const char * buf,size_t len)44518e77612Sflorian remote_write(SSL* ssl, int fd, const char* buf, size_t len)
44618e77612Sflorian {
44718e77612Sflorian 	if(ssl) {
4483efee2e1Sflorian #ifdef HAVE_SSL
44918e77612Sflorian 		if(SSL_write(ssl, buf, (int)len) <= 0)
45018e77612Sflorian 			ssl_err("could not SSL_write");
4513efee2e1Sflorian #endif /* HAVE_SSL */
45218e77612Sflorian 	} else {
45318e77612Sflorian 		if(write(fd, buf, len) < (ssize_t)len) {
45418e77612Sflorian 			fprintf(stderr, "could not write: %s\n",
45518e77612Sflorian 				strerror(errno));
45618e77612Sflorian 			exit(1);
45718e77612Sflorian 		}
45818e77612Sflorian 	}
45918e77612Sflorian }
46018e77612Sflorian 
461d3fecca9Ssthen /** send stdin to server */
462d3fecca9Ssthen static void
send_file(SSL * ssl,int fd,FILE * in,char * buf,size_t sz)46318e77612Sflorian send_file(SSL* ssl, int fd, FILE* in, char* buf, size_t sz)
464d3fecca9Ssthen {
465c939baa4Ssthen 	char e[] = {0x04, 0x0a};
466d3fecca9Ssthen 	while(fgets(buf, (int)sz, in)) {
46718e77612Sflorian 		remote_write(ssl, fd, buf, strlen(buf));
468d3fecca9Ssthen 	}
469c939baa4Ssthen 	/* send end-of-file marker */
47018e77612Sflorian 	remote_write(ssl, fd, e, sizeof(e));
471d3fecca9Ssthen }
472d3fecca9Ssthen 
473d3fecca9Ssthen /** send command and display result */
474d3fecca9Ssthen static int
go_cmd(SSL * ssl,int fd,int argc,char * argv[])47518e77612Sflorian go_cmd(SSL* ssl, int fd, int argc, char* argv[])
476d3fecca9Ssthen {
477d3fecca9Ssthen 	char pre[10];
478d3fecca9Ssthen 	const char* space=" ";
479d3fecca9Ssthen 	const char* newline="\n";
480d3fecca9Ssthen 	int was_error = 0, first_line = 1;
48118e77612Sflorian 	int i;
482d3fecca9Ssthen 	char buf[1024];
483d3fecca9Ssthen 	snprintf(pre, sizeof(pre), "NSDCT%d ", NSD_CONTROL_VERSION);
48418e77612Sflorian 	remote_write(ssl, fd, pre, strlen(pre));
485d3fecca9Ssthen 	for(i=0; i<argc; i++) {
48618e77612Sflorian 		remote_write(ssl, fd, space, strlen(space));
48718e77612Sflorian 		remote_write(ssl, fd, argv[i], strlen(argv[i]));
488d3fecca9Ssthen 	}
48918e77612Sflorian 	remote_write(ssl, fd, newline, strlen(newline));
490d3fecca9Ssthen 
491c939baa4Ssthen 	/* send contents to server */
492c939baa4Ssthen 	if(argc == 1 && (strcmp(argv[0], "addzones") == 0 ||
493c939baa4Ssthen 		strcmp(argv[0], "delzones") == 0)) {
49418e77612Sflorian 		send_file(ssl, fd, stdin, buf, sizeof(buf));
495d3fecca9Ssthen 	}
496d3fecca9Ssthen 
497d3fecca9Ssthen 	while(1) {
49818e77612Sflorian 		if(remote_read(ssl, fd, buf, sizeof(buf)) == 0) {
49918e77612Sflorian 			break; /* EOF */
500d3fecca9Ssthen 		}
501d3fecca9Ssthen 		printf("%s", buf);
502d3fecca9Ssthen 		if(first_line && strncmp(buf, "error", 5) == 0)
503d3fecca9Ssthen 			was_error = 1;
504d3fecca9Ssthen 		first_line = 0;
505d3fecca9Ssthen 	}
506d3fecca9Ssthen 	return was_error;
507d3fecca9Ssthen }
508d3fecca9Ssthen 
509d3fecca9Ssthen /** go ahead and read config, contact server and perform command and display */
510d3fecca9Ssthen static int
go(const char * cfgfile,char * svr,int argc,char * argv[])511d3fecca9Ssthen go(const char* cfgfile, char* svr, int argc, char* argv[])
512d3fecca9Ssthen {
513fe5fe5f6Sflorian 	struct nsd_options* opt;
514d3fecca9Ssthen 	int fd, ret;
5153efee2e1Sflorian #ifdef HAVE_SSL
5163efee2e1Sflorian 	SSL_CTX* ctx = NULL;
5173efee2e1Sflorian #endif
5183efee2e1Sflorian 	SSL* ssl = NULL;
519d3fecca9Ssthen 
520d3fecca9Ssthen 	/* read config */
521d3fecca9Ssthen 	if(!(opt = nsd_options_create(region_create(xalloc, free)))) {
522d3fecca9Ssthen 		fprintf(stderr, "out of memory\n");
523d3fecca9Ssthen 		exit(1);
524d3fecca9Ssthen 	}
525d3fecca9Ssthen 	tsig_init(opt->region);
526*bf87c3c0Sflorian 	if(!parse_options_file(opt, cfgfile, NULL, NULL, NULL)) {
527d3fecca9Ssthen 		fprintf(stderr, "could not read config file\n");
528d3fecca9Ssthen 		exit(1);
529d3fecca9Ssthen 	}
530d3fecca9Ssthen 	if(!opt->control_enable)
531d3fecca9Ssthen 		fprintf(stderr, "warning: control-enable is 'no' in the config file.\n");
5328d298c9fSsthen 	resolve_interface_names(opt);
5333efee2e1Sflorian #ifdef HAVE_SSL
534d3fecca9Ssthen 	ctx = setup_ctx(opt);
5353efee2e1Sflorian #else
5363efee2e1Sflorian 	if(options_remote_is_address(opt)) {
5373efee2e1Sflorian 		fprintf(stderr, "error: NSD was compiled without SSL.\n");
5383efee2e1Sflorian 		exit(1);
5393efee2e1Sflorian 	}
5403efee2e1Sflorian #endif /* HAVE_SSL */
541d3fecca9Ssthen 
542d3fecca9Ssthen 	/* contact server */
543d3fecca9Ssthen 	fd = contact_server(svr, opt, argc>0&&strcmp(argv[0],"status")==0);
5443efee2e1Sflorian #ifdef HAVE_SSL
545d3fecca9Ssthen 	ssl = setup_ssl(ctx, fd);
5463efee2e1Sflorian #endif
547d3fecca9Ssthen 
548d3fecca9Ssthen 	/* send command */
54918e77612Sflorian 	ret = go_cmd(ssl, fd, argc, argv);
550d3fecca9Ssthen 
5513efee2e1Sflorian #ifdef HAVE_SSL
55218e77612Sflorian 	if(ssl) SSL_free(ssl);
5533efee2e1Sflorian #endif
554d3fecca9Ssthen 	close(fd);
5553efee2e1Sflorian #ifdef HAVE_SSL
55618e77612Sflorian 	if(ctx) SSL_CTX_free(ctx);
5573efee2e1Sflorian #endif
558d3fecca9Ssthen 	region_destroy(opt->region);
559d3fecca9Ssthen 	return ret;
560d3fecca9Ssthen }
561d3fecca9Ssthen 
562d3fecca9Ssthen /** getopt global, in case header files fail to declare it. */
563d3fecca9Ssthen extern int optind;
564d3fecca9Ssthen /** getopt global, in case header files fail to declare it. */
565d3fecca9Ssthen extern char* optarg;
566d3fecca9Ssthen 
567d3fecca9Ssthen /** Main routine for nsd-control */
main(int argc,char * argv[])568d3fecca9Ssthen int main(int argc, char* argv[])
569d3fecca9Ssthen {
570d3fecca9Ssthen 	int c;
571d3fecca9Ssthen 	const char* cfgfile = CONFIGFILE;
572d3fecca9Ssthen 	char* svr = NULL;
573d3fecca9Ssthen 	log_init("nsd-control");
574d3fecca9Ssthen 
5753efee2e1Sflorian #ifdef HAVE_SSL
576c1e73312Sflorian #ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
577d3fecca9Ssthen 	ERR_load_crypto_strings();
578c1e73312Sflorian #endif
579a904e103Sflorian #if defined(HAVE_ERR_LOAD_SSL_STRINGS) && !defined(DEPRECATED_ERR_LOAD_SSL_STRINGS)
580d3fecca9Ssthen 	ERR_load_SSL_strings();
581a904e103Sflorian #endif
582c1e73312Sflorian #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
583d3fecca9Ssthen 	OpenSSL_add_all_algorithms();
584c1e73312Sflorian #else
585c1e73312Sflorian 	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
586c1e73312Sflorian 		| OPENSSL_INIT_ADD_ALL_DIGESTS
587c1e73312Sflorian 		| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
588c1e73312Sflorian #endif
589c1e73312Sflorian #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
590d3fecca9Ssthen 	(void)SSL_library_init();
591c1e73312Sflorian #else
592c1e73312Sflorian 	OPENSSL_init_ssl(0, NULL);
593c1e73312Sflorian #endif
594d3fecca9Ssthen 
595d3fecca9Ssthen 	if(!RAND_status()) {
596d3fecca9Ssthen                 /* try to seed it */
597d3fecca9Ssthen                 unsigned char buf[256];
598d3fecca9Ssthen                 unsigned int v, seed=(unsigned)time(NULL) ^ (unsigned)getpid();
599d3fecca9Ssthen                 size_t i;
600d3fecca9Ssthen 		v = seed;
601d3fecca9Ssthen                 for(i=0; i<256/sizeof(v); i++) {
602d3fecca9Ssthen                         memmove(buf+i*sizeof(v), &v, sizeof(v));
603d3fecca9Ssthen                         v = v*seed + (unsigned int)i;
604d3fecca9Ssthen                 }
605d3fecca9Ssthen                 RAND_seed(buf, 256);
606d3fecca9Ssthen 		fprintf(stderr, "warning: no entropy, seeding openssl PRNG with time\n");
607d3fecca9Ssthen 	}
6083efee2e1Sflorian #endif /* HAVE_SSL */
609d3fecca9Ssthen 
610d3fecca9Ssthen 	/* parse the options */
611d3fecca9Ssthen 	while( (c=getopt(argc, argv, "c:s:h")) != -1) {
612d3fecca9Ssthen 		switch(c) {
613d3fecca9Ssthen 		case 'c':
614d3fecca9Ssthen 			cfgfile = optarg;
615d3fecca9Ssthen 			break;
616d3fecca9Ssthen 		case 's':
617d3fecca9Ssthen 			svr = optarg;
618d3fecca9Ssthen 			break;
619d3fecca9Ssthen 		case '?':
620d3fecca9Ssthen 		case 'h':
621d3fecca9Ssthen 		default:
622d3fecca9Ssthen 			usage();
623d3fecca9Ssthen 		}
624d3fecca9Ssthen 	}
625d3fecca9Ssthen 	argc -= optind;
626d3fecca9Ssthen 	argv += optind;
627d3fecca9Ssthen 	if(argc == 0)
628d3fecca9Ssthen 		usage();
629d3fecca9Ssthen 	if(argc >= 1 && strcmp(argv[0], "start")==0) {
6303f21e8ccSflorian 		const char *path;
6313f21e8ccSflorian 		if((path = getenv("NSD_PATH")) == NULL) {
6323f21e8ccSflorian 			path = NSD_START_PATH;
6333f21e8ccSflorian 		}
6343f21e8ccSflorian 		if(execl(path, "nsd", "-c", cfgfile, (char*)NULL) < 0) {
635d3fecca9Ssthen 			fprintf(stderr, "could not exec %s: %s\n",
636d3fecca9Ssthen 				NSD_START_PATH, strerror(errno));
637d3fecca9Ssthen 			exit(1);
638d3fecca9Ssthen 		}
639d3fecca9Ssthen 	}
640d3fecca9Ssthen 
641d3fecca9Ssthen 	return go(cfgfile, svr, argc, argv);
642d3fecca9Ssthen }
643