xref: /openbsd-src/usr.sbin/nsd/nsd-control.c (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
1 /*
2  * nsd-control.c - remote control utility for nsd.
3  *
4  * Copyright (c) 2011, 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
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * The remote control utility contacts the nsd 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 #include <stdio.h>
46 #include <stdlib.h>
47 #ifdef HAVE_SSL
48 #include <sys/types.h>
49 #include <unistd.h>
50 #include <string.h>
51 #ifdef HAVE_OPENSSL_SSL_H
52 #include <openssl/ssl.h>
53 #endif
54 #ifdef HAVE_OPENSSL_ERR_H
55 #include <openssl/err.h>
56 #endif
57 #ifdef HAVE_OPENSSL_RAND_H
58 #include <openssl/rand.h>
59 #endif
60 #ifdef HAVE_SYS_UN_H
61 #include <sys/un.h>
62 #endif
63 #include <fcntl.h>
64 #ifndef AF_LOCAL
65 #define AF_LOCAL AF_UNIX
66 #endif
67 #include "util.h"
68 #include "tsig.h"
69 #include "options.h"
70 #include "zonec.h"
71 
72 static void usage(void) ATTR_NORETURN;
73 static void ssl_err(const char* s) ATTR_NORETURN;
74 static void ssl_path_err(const char* s, const char *path) ATTR_NORETURN;
75 
76 /** timeout to wait for connection over stream, in msec */
77 #define NSD_CONTROL_CONNECT_TIMEOUT 5000
78 
79 int zonec_parse_string(region_type* ATTR_UNUSED(region),
80 	domain_table_type* ATTR_UNUSED(domains), zone_type* ATTR_UNUSED(zone),
81 	char* ATTR_UNUSED(str), domain_type** ATTR_UNUSED(parsed),
82 	int* ATTR_UNUSED(num_rrs))
83 {
84 	return 0;
85 }
86 
87 /** Give nsd-control usage, and exit (1). */
88 static void
89 usage()
90 {
91 	printf("Usage:	nsd-control [options] command\n");
92 	printf("	Remote control utility for nsd server.\n");
93 	printf("Version %s. Report bugs to <%s>.\n",
94 		PACKAGE_VERSION, PACKAGE_BUGREPORT);
95 	printf("Options:\n");
96 	printf("  -c file	config file, default is %s\n", CONFIGFILE);
97 	printf("  -s ip[@port]	server address, if omitted config is used.\n");
98 	printf("  -h		show this usage help.\n");
99 	printf("Commands:\n");
100 	printf("  start				start server; runs nsd(8)\n");
101 	printf("  stop				stops the server\n");
102 	printf("  reload [<zone>]		reload modified zonefiles from disk\n");
103 	printf("  reconfig			reload the config file\n");
104 	printf("  repattern			the same as reconfig\n");
105 	printf("  log_reopen			reopen logfile (for log rotate)\n");
106 	printf("  status			display status of server\n");
107 	printf("  stats				print statistics\n");
108 	printf("  stats_noreset			peek at statistics\n");
109 	printf("  addzone <name> <pattern>	add a new zone\n");
110 	printf("  delzone <name>		remove a zone\n");
111 	printf("  changezone <name> <pattern>	change zone to use pattern\n");
112 	printf("  addzones			add zone list on stdin {name space pattern newline}\n");
113 	printf("  delzones			remove zone list on stdin {name newline}\n");
114 	printf("  write [<zone>]		write changed zonefiles to disk\n");
115 	printf("  notify [<zone>]		send NOTIFY messages to slave servers\n");
116 	printf("  transfer [<zone>]		try to update slave zones to newer serial\n");
117 	printf("  force_transfer [<zone>]	update slave zones with AXFR, no serial check\n");
118 	printf("  zonestatus [<zone>]		print state, serial, activity\n");
119 	printf("  serverpid			get pid of server process\n");
120 	printf("  verbosity <number>		change logging detail\n");
121 	printf("  print_tsig [<key_name>]	print tsig with <name> the secret and algo\n");
122 	printf("  update_tsig <name> <secret>	change existing tsig with <name> to a new <secret>\n");
123 	printf("  add_tsig <name> <secret> [algo] add new key with the given parameters\n");
124 	printf("  assoc_tsig <zone> <key_name>	associate <zone> with given tsig <key_name> name\n");
125 	printf("  del_tsig <key_name>		delete tsig <key_name> from configuration\n");
126 	printf("  add_cookie_secret <secret>	add (or replace) a new cookie secret <secret>\n");
127 	printf("  drop_cookie_secret		drop a staging cookie secret\n");
128 	printf("  activate_cookie_secret	make a staging cookie secret active\n");
129 	printf("  print_cookie_secrets		show all cookie secrets with their status\n");
130 	exit(1);
131 }
132 
133 /** exit with ssl error */
134 static void ssl_err(const char* s)
135 {
136 	fprintf(stderr, "error: %s\n", s);
137 	ERR_print_errors_fp(stderr);
138 	exit(1);
139 }
140 
141 /** exit with ssl error related to a file path */
142 static void ssl_path_err(const char* s, const char *path)
143 {
144 	unsigned long err;
145 	err = ERR_peek_error();
146 	if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
147 		fprintf(stderr, "error: %s\n%s: %s\n",
148 			s, path, ERR_reason_error_string(err));
149 		exit(1);
150 	} else {
151 		ssl_err(s);
152 	}
153 }
154 
155 /** setup SSL context */
156 static SSL_CTX*
157 setup_ctx(struct nsd_options* cfg)
158 {
159 	char* s_cert, *c_key, *c_cert;
160 	SSL_CTX* ctx;
161 
162 	if(!options_remote_is_address(cfg))
163 		return NULL;
164 	s_cert = cfg->server_cert_file;
165 	c_key = cfg->control_key_file;
166 	c_cert = cfg->control_cert_file;
167 
168 	/* filenames may be relative to zonesdir */
169 	if (cfg->zonesdir && cfg->zonesdir[0] &&
170 		(s_cert[0] != '/' || c_key[0] != '/' || c_cert[0] != '/')) {
171 		if(chdir(cfg->zonesdir))
172 			error("could not chdir to zonesdir: %s %s",
173 				cfg->zonesdir, strerror(errno));
174 	}
175 
176         ctx = SSL_CTX_new(SSLv23_client_method());
177 	if(!ctx)
178 		ssl_err("could not allocate SSL_CTX pointer");
179 #if SSL_OP_NO_SSLv2 != 0
180         if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)
181 		!= SSL_OP_NO_SSLv2)
182 		ssl_err("could not set SSL_OP_NO_SSLv2");
183 #endif
184         if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)
185 		!= SSL_OP_NO_SSLv3)
186 		ssl_err("could not set SSL_OP_NO_SSLv3");
187 #if defined(SSL_OP_NO_RENEGOTIATION)
188 	/* disable client renegotiation */
189 	if((SSL_CTX_set_options(ctx, SSL_OP_NO_RENEGOTIATION) &
190 		SSL_OP_NO_RENEGOTIATION) != SSL_OP_NO_RENEGOTIATION)
191 		ssl_err("could not set SSL_OP_NO_RENEGOTIATION");
192 #endif
193 	if(!SSL_CTX_use_certificate_file(ctx,c_cert,SSL_FILETYPE_PEM))
194 		ssl_path_err("Error setting up SSL_CTX client cert", c_cert);
195 	if(!SSL_CTX_use_PrivateKey_file(ctx,c_key,SSL_FILETYPE_PEM))
196 		ssl_path_err("Error setting up SSL_CTX client key", c_key);
197 	if(!SSL_CTX_check_private_key(ctx))
198 		ssl_err("Error setting up SSL_CTX client key");
199 	if (SSL_CTX_load_verify_locations(ctx, s_cert, NULL) != 1)
200 		ssl_path_err("Error setting up SSL_CTX verify, server cert",
201 			s_cert);
202 	SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
203 
204 	return ctx;
205 }
206 
207 /** check connect error */
208 static void
209 checkconnecterr(int err, const char* svr, int port, int statuscmd)
210 {
211 	if(!port) fprintf(stderr, "error: connect (%s): %s\n", svr,
212 		strerror(err));
213 	else fprintf(stderr, "error: connect (%s@%d): %s\n", svr, port,
214 		strerror(err));
215 	if(err == ECONNREFUSED && statuscmd) {
216 		printf("nsd is stopped\n");
217 		exit(3);
218 	}
219 	exit(1);
220 }
221 
222 /** contact the server with TCP connect */
223 static int
224 contact_server(const char* svr, struct nsd_options* cfg, int statuscmd)
225 {
226 #ifdef INET6
227 	struct sockaddr_storage addr;
228 #else
229 	struct sockaddr_in addr;
230 #endif
231 	socklen_t addrlen;
232 	int fd;
233 	int port = cfg->control_port;
234 	int addrfamily = 0;
235 	/* use svr or a config entry */
236 	if(!svr) {
237 		if(cfg->control_interface) {
238 			svr = cfg->control_interface->address;
239 		} else if(cfg->do_ip4) {
240 			svr = "127.0.0.1";
241 		} else {
242 			svr = "::1";
243 		}
244 		/* config 0 addr (everything), means ask localhost */
245 		if(strcmp(svr, "0.0.0.0") == 0)
246 			svr = "127.0.0.1";
247 		else if(strcmp(svr, "::0") == 0 ||
248 			strcmp(svr, "0::0") == 0 ||
249 			strcmp(svr, "0::") == 0 ||
250 			strcmp(svr, "::") == 0)
251 			svr = "::1";
252 	}
253 	if(strchr(svr, '@')) {
254 		char* ps = strchr(svr, '@');
255 		*ps++ = 0;
256 		port = atoi(ps);
257 		if(!port) {
258 			fprintf(stderr, "could not parse port %s\n", ps);
259 			exit(1);
260 		}
261 	}
262 	if(svr[0] == '/') {
263 #ifdef HAVE_SYS_UN_H
264 		struct sockaddr_un* usock = (struct sockaddr_un *) &addr;
265 		usock->sun_family = AF_LOCAL;
266 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
267 		usock->sun_len = (unsigned)sizeof(usock);
268 #endif
269 		(void)strlcpy(usock->sun_path, svr, sizeof(usock->sun_path));
270 		addrlen = (socklen_t)sizeof(struct sockaddr_un);
271 		addrfamily = AF_LOCAL;
272 		port = 0;
273 #endif
274 #ifdef INET6
275 	} else if(strchr(svr, ':')) {
276 		struct sockaddr_in6 sa;
277 		addrlen = (socklen_t)sizeof(struct sockaddr_in6);
278 		memset(&sa, 0, addrlen);
279 		sa.sin6_family = AF_INET6;
280 		sa.sin6_port = (in_port_t)htons((uint16_t)port);
281 		if(inet_pton((int)sa.sin6_family, svr, &sa.sin6_addr) <= 0) {
282 			fprintf(stderr, "could not parse IP: %s\n", svr);
283 			exit(1);
284 		}
285 		memcpy(&addr, &sa, addrlen);
286 		addrfamily = AF_INET6;
287 #endif
288 	} else { /* ip4 */
289 		struct sockaddr_in sa;
290 		addrlen = (socklen_t)sizeof(struct sockaddr_in);
291 		memset(&sa, 0, addrlen);
292 		sa.sin_family = AF_INET;
293 		sa.sin_port = (in_port_t)htons((uint16_t)port);
294 		if(inet_pton((int)sa.sin_family, svr, &sa.sin_addr) <= 0) {
295 			fprintf(stderr, "could not parse IP: %s\n", svr);
296 			exit(1);
297 		}
298 		memcpy(&addr, &sa, addrlen);
299 		addrfamily = AF_INET;
300 	}
301 
302 	fd = socket(addrfamily, SOCK_STREAM, 0);
303 	if(fd == -1) {
304 		fprintf(stderr, "socket: %s\n", strerror(errno));
305 		exit(1);
306 	}
307 	if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
308 		fprintf(stderr, "error: set nonblocking: fcntl: %s",
309 			strerror(errno));
310 	}
311 	if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) {
312 		if(errno != EINPROGRESS) {
313 			checkconnecterr(errno, svr, port, statuscmd);
314 		}
315 	}
316 	while(1) {
317 		fd_set rset, wset, eset;
318 		struct timeval tv;
319 		FD_ZERO(&rset);
320 		FD_SET(fd, &rset);
321 		FD_ZERO(&wset);
322 		FD_SET(fd, &wset);
323 		FD_ZERO(&eset);
324 		FD_SET(fd, &eset);
325 		tv.tv_sec = NSD_CONTROL_CONNECT_TIMEOUT/1000;
326 		tv.tv_usec= (NSD_CONTROL_CONNECT_TIMEOUT%1000)*1000;
327 		if(select(fd+1, &rset, &wset, &eset, &tv) == -1) {
328 			fprintf(stderr, "select: %s\n", strerror(errno));
329 			exit(1);
330 		}
331 		if(!FD_ISSET(fd, &rset) && !FD_ISSET(fd, &wset) &&
332 			!FD_ISSET(fd, &eset)) {
333 			fprintf(stderr, "timeout: could not connect to server\n");
334 			exit(1);
335 		} else {
336 			/* check nonblocking connect error */
337 			int error = 0;
338 			socklen_t len = (socklen_t)sizeof(error);
339 			if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
340 				&len) < 0) {
341 				error = errno; /* on solaris errno is error */
342 			}
343 			if(error != 0) {
344 				if(error == EINPROGRESS || error == EWOULDBLOCK)
345 					continue; /* try again later */
346 				checkconnecterr(error, svr, port, statuscmd);
347 			}
348 		}
349 		break;
350 	}
351 	if(fcntl(fd, F_SETFL, 0) == -1) {
352 		fprintf(stderr, "error: set blocking: fcntl: %s",
353 			strerror(errno));
354 	}
355 	return fd;
356 }
357 
358 /** setup SSL on the connection */
359 static SSL*
360 setup_ssl(SSL_CTX* ctx, int fd)
361 {
362 	SSL* ssl;
363 	X509* x;
364 	int r;
365 
366 	if(!ctx) return NULL;
367 	ssl = SSL_new(ctx);
368 	if(!ssl)
369 		ssl_err("could not SSL_new");
370 	SSL_set_connect_state(ssl);
371 	(void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
372 	if(!SSL_set_fd(ssl, fd))
373 		ssl_err("could not SSL_set_fd");
374 	while(1) {
375 		ERR_clear_error();
376 		if( (r=SSL_do_handshake(ssl)) == 1)
377 			break;
378 		r = SSL_get_error(ssl, r);
379 		if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
380 			ssl_err("SSL handshake failed");
381 		/* wants to be called again */
382 	}
383 
384 	/* check authenticity of server */
385 	if(SSL_get_verify_result(ssl) != X509_V_OK)
386 		ssl_err("SSL verification failed");
387 	x = SSL_get_peer_certificate(ssl);
388 	if(!x)
389 		ssl_err("Server presented no peer certificate");
390 	X509_free(x);
391 	return ssl;
392 }
393 
394 /** read from ssl or fd, fatalexit on error, 0 EOF, 1 success */
395 static int
396 remote_read(SSL* ssl, int fd, char* buf, size_t len)
397 {
398 	if(ssl) {
399 		int r;
400 		ERR_clear_error();
401 		if((r = SSL_read(ssl, buf, (int)len-1)) <= 0) {
402 			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
403 				/* EOF */
404 				return 0;
405 			}
406 			ssl_err("could not SSL_read");
407 		}
408 		buf[r] = 0;
409 	} else {
410 		ssize_t rr = read(fd, buf, len-1);
411 		if(rr <= 0) {
412 			if(rr == 0) {
413 				/* EOF */
414 				return 0;
415 			}
416 			fprintf(stderr, "could not read: %s\n",
417 				strerror(errno));
418 			exit(1);
419 		}
420 		buf[rr] = 0;
421 	}
422 	return 1;
423 }
424 
425 /** write to ssl or fd, fatalexit on error */
426 static void
427 remote_write(SSL* ssl, int fd, const char* buf, size_t len)
428 {
429 	if(ssl) {
430 		if(SSL_write(ssl, buf, (int)len) <= 0)
431 			ssl_err("could not SSL_write");
432 	} else {
433 		if(write(fd, buf, len) < (ssize_t)len) {
434 			fprintf(stderr, "could not write: %s\n",
435 				strerror(errno));
436 			exit(1);
437 		}
438 	}
439 }
440 
441 /** send stdin to server */
442 static void
443 send_file(SSL* ssl, int fd, FILE* in, char* buf, size_t sz)
444 {
445 	char e[] = {0x04, 0x0a};
446 	while(fgets(buf, (int)sz, in)) {
447 		remote_write(ssl, fd, buf, strlen(buf));
448 	}
449 	/* send end-of-file marker */
450 	remote_write(ssl, fd, e, sizeof(e));
451 }
452 
453 /** send command and display result */
454 static int
455 go_cmd(SSL* ssl, int fd, int argc, char* argv[])
456 {
457 	char pre[10];
458 	const char* space=" ";
459 	const char* newline="\n";
460 	int was_error = 0, first_line = 1;
461 	int i;
462 	char buf[1024];
463 	snprintf(pre, sizeof(pre), "NSDCT%d ", NSD_CONTROL_VERSION);
464 	remote_write(ssl, fd, pre, strlen(pre));
465 	for(i=0; i<argc; i++) {
466 		remote_write(ssl, fd, space, strlen(space));
467 		remote_write(ssl, fd, argv[i], strlen(argv[i]));
468 	}
469 	remote_write(ssl, fd, newline, strlen(newline));
470 
471 	/* send contents to server */
472 	if(argc == 1 && (strcmp(argv[0], "addzones") == 0 ||
473 		strcmp(argv[0], "delzones") == 0)) {
474 		send_file(ssl, fd, stdin, buf, sizeof(buf));
475 	}
476 
477 	while(1) {
478 		if(remote_read(ssl, fd, buf, sizeof(buf)) == 0) {
479 			break; /* EOF */
480 		}
481 		printf("%s", buf);
482 		if(first_line && strncmp(buf, "error", 5) == 0)
483 			was_error = 1;
484 		first_line = 0;
485 	}
486 	return was_error;
487 }
488 
489 /** go ahead and read config, contact server and perform command and display */
490 static int
491 go(const char* cfgfile, char* svr, int argc, char* argv[])
492 {
493 	struct nsd_options* opt;
494 	int fd, ret;
495 	SSL_CTX* ctx;
496 	SSL* ssl;
497 
498 	/* read config */
499 	if(!(opt = nsd_options_create(region_create(xalloc, free)))) {
500 		fprintf(stderr, "out of memory\n");
501 		exit(1);
502 	}
503 	tsig_init(opt->region);
504 	if(!parse_options_file(opt, cfgfile, NULL, NULL)) {
505 		fprintf(stderr, "could not read config file\n");
506 		exit(1);
507 	}
508 	if(!opt->control_enable)
509 		fprintf(stderr, "warning: control-enable is 'no' in the config file.\n");
510 	resolve_interface_names(opt);
511 	ctx = setup_ctx(opt);
512 
513 	/* contact server */
514 	fd = contact_server(svr, opt, argc>0&&strcmp(argv[0],"status")==0);
515 	ssl = setup_ssl(ctx, fd);
516 
517 	/* send command */
518 	ret = go_cmd(ssl, fd, argc, argv);
519 
520 	if(ssl) SSL_free(ssl);
521 	close(fd);
522 	if(ctx) SSL_CTX_free(ctx);
523 	region_destroy(opt->region);
524 	return ret;
525 }
526 
527 /** getopt global, in case header files fail to declare it. */
528 extern int optind;
529 /** getopt global, in case header files fail to declare it. */
530 extern char* optarg;
531 
532 /** Main routine for nsd-control */
533 int main(int argc, char* argv[])
534 {
535 	int c;
536 	const char* cfgfile = CONFIGFILE;
537 	char* svr = NULL;
538 	log_init("nsd-control");
539 
540 #ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
541 	ERR_load_crypto_strings();
542 #endif
543 #if defined(HAVE_ERR_LOAD_SSL_STRINGS) && !defined(DEPRECATED_ERR_LOAD_SSL_STRINGS)
544 	ERR_load_SSL_strings();
545 #endif
546 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
547 	OpenSSL_add_all_algorithms();
548 #else
549 	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
550 		| OPENSSL_INIT_ADD_ALL_DIGESTS
551 		| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
552 #endif
553 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
554 	(void)SSL_library_init();
555 #else
556 	OPENSSL_init_ssl(0, NULL);
557 #endif
558 
559 	if(!RAND_status()) {
560                 /* try to seed it */
561                 unsigned char buf[256];
562                 unsigned int v, seed=(unsigned)time(NULL) ^ (unsigned)getpid();
563                 size_t i;
564 		v = seed;
565                 for(i=0; i<256/sizeof(v); i++) {
566                         memmove(buf+i*sizeof(v), &v, sizeof(v));
567                         v = v*seed + (unsigned int)i;
568                 }
569                 RAND_seed(buf, 256);
570 		fprintf(stderr, "warning: no entropy, seeding openssl PRNG with time\n");
571 	}
572 
573 	/* parse the options */
574 	while( (c=getopt(argc, argv, "c:s:h")) != -1) {
575 		switch(c) {
576 		case 'c':
577 			cfgfile = optarg;
578 			break;
579 		case 's':
580 			svr = optarg;
581 			break;
582 		case '?':
583 		case 'h':
584 		default:
585 			usage();
586 		}
587 	}
588 	argc -= optind;
589 	argv += optind;
590 	if(argc == 0)
591 		usage();
592 	if(argc >= 1 && strcmp(argv[0], "start")==0) {
593 		const char *path;
594 		if((path = getenv("NSD_PATH")) == NULL) {
595 			path = NSD_START_PATH;
596 		}
597 		if(execl(path, "nsd", "-c", cfgfile, (char*)NULL) < 0) {
598 			fprintf(stderr, "could not exec %s: %s\n",
599 				NSD_START_PATH, strerror(errno));
600 			exit(1);
601 		}
602 	}
603 
604 	return go(cfgfile, svr, argc, argv);
605 }
606 
607 #else /* HAVE_SSL */
608 int main(void)
609 {
610 	printf("error: NSD was compiled without SSL.\n");
611 	return 1;
612 }
613 #endif /* HAVE_SSL */
614