nsd-control.c revision 1.1.1.3 1 1.1 christos /*
2 1.1 christos * nsd-control.c - remote control utility for nsd.
3 1.1 christos *
4 1.1 christos * Copyright (c) 2011, 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 nsd 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_SSL
46 1.1 christos
47 1.1 christos #include <sys/types.h>
48 1.1 christos #include <unistd.h>
49 1.1 christos #include <string.h>
50 1.1 christos #ifdef HAVE_OPENSSL_SSL_H
51 1.1 christos #include <openssl/ssl.h>
52 1.1 christos #endif
53 1.1 christos #ifdef HAVE_OPENSSL_ERR_H
54 1.1 christos #include <openssl/err.h>
55 1.1 christos #endif
56 1.1 christos #ifdef HAVE_OPENSSL_RAND_H
57 1.1 christos #include <openssl/rand.h>
58 1.1 christos #endif
59 1.1.1.3 christos #ifdef HAVE_SYS_UN_H
60 1.1.1.3 christos #include <sys/un.h>
61 1.1.1.3 christos #endif
62 1.1 christos #include "util.h"
63 1.1 christos #include "tsig.h"
64 1.1 christos #include "options.h"
65 1.1 christos
66 1.1 christos /** Give nsd-control usage, and exit (1). */
67 1.1 christos static void
68 1.1 christos usage()
69 1.1 christos {
70 1.1 christos printf("Usage: nsd-control [options] command\n");
71 1.1 christos printf(" Remote control utility for nsd server.\n");
72 1.1 christos printf("Version %s. Report bugs to <%s>.\n",
73 1.1 christos PACKAGE_VERSION, PACKAGE_BUGREPORT);
74 1.1 christos printf("Options:\n");
75 1.1 christos printf(" -c file config file, default is %s\n", CONFIGFILE);
76 1.1 christos printf(" -s ip[@port] server address, if omitted config is used.\n");
77 1.1 christos printf(" -h show this usage help.\n");
78 1.1 christos printf("Commands:\n");
79 1.1 christos printf(" start start server; runs nsd(8)\n");
80 1.1 christos printf(" stop stops the server\n");
81 1.1 christos printf(" reload [<zone>] reload modified zonefiles from disk\n");
82 1.1 christos printf(" reconfig reload the config file\n");
83 1.1 christos printf(" repattern the same as reconfig\n");
84 1.1 christos printf(" log_reopen reopen logfile (for log rotate)\n");
85 1.1 christos printf(" status display status of server\n");
86 1.1 christos printf(" stats print statistics\n");
87 1.1 christos printf(" stats_noreset peek at statistics\n");
88 1.1 christos printf(" addzone <name> <pattern> add a new zone\n");
89 1.1 christos printf(" delzone <name> remove a zone\n");
90 1.1 christos printf(" addzones add zone list on stdin {name space pattern newline}\n");
91 1.1 christos printf(" delzones remove zone list on stdin {name newline}\n");
92 1.1 christos printf(" write [<zone>] write changed zonefiles to disk\n");
93 1.1 christos printf(" notify [<zone>] send NOTIFY messages to slave servers\n");
94 1.1 christos printf(" transfer [<zone>] try to update slave zones to newer serial\n");
95 1.1 christos printf(" force_transfer [<zone>] update slave zones with AXFR, no serial check\n");
96 1.1 christos printf(" zonestatus [<zone>] print state, serial, activity\n");
97 1.1 christos printf(" serverpid get pid of server process\n");
98 1.1 christos printf(" verbosity <number> change logging detail\n");
99 1.1 christos exit(1);
100 1.1 christos }
101 1.1 christos
102 1.1 christos /** exit with ssl error */
103 1.1 christos static void ssl_err(const char* s)
104 1.1 christos {
105 1.1 christos fprintf(stderr, "error: %s\n", s);
106 1.1 christos ERR_print_errors_fp(stderr);
107 1.1 christos exit(1);
108 1.1 christos }
109 1.1 christos
110 1.1 christos /** setup SSL context */
111 1.1 christos static SSL_CTX*
112 1.1.1.2 christos setup_ctx(struct nsd_options* cfg)
113 1.1 christos {
114 1.1 christos char* s_cert, *c_key, *c_cert;
115 1.1 christos SSL_CTX* ctx;
116 1.1 christos
117 1.1.1.3 christos if(!options_remote_is_address(cfg))
118 1.1.1.3 christos return NULL;
119 1.1 christos s_cert = cfg->server_cert_file;
120 1.1 christos c_key = cfg->control_key_file;
121 1.1 christos c_cert = cfg->control_cert_file;
122 1.1 christos
123 1.1 christos /* filenames may be relative to zonesdir */
124 1.1 christos if (cfg->zonesdir && cfg->zonesdir[0] &&
125 1.1 christos (s_cert[0] != '/' || c_key[0] != '/' || c_cert[0] != '/')) {
126 1.1 christos if(chdir(cfg->zonesdir))
127 1.1 christos ssl_err("could not chdir to zonesdir");
128 1.1 christos }
129 1.1 christos
130 1.1 christos ctx = SSL_CTX_new(SSLv23_client_method());
131 1.1 christos if(!ctx)
132 1.1 christos ssl_err("could not allocate SSL_CTX pointer");
133 1.1 christos if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)
134 1.1 christos != SSL_OP_NO_SSLv2)
135 1.1 christos ssl_err("could not set SSL_OP_NO_SSLv2");
136 1.1 christos if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)
137 1.1 christos != SSL_OP_NO_SSLv3)
138 1.1 christos ssl_err("could not set SSL_OP_NO_SSLv3");
139 1.1 christos if(!SSL_CTX_use_certificate_file(ctx,c_cert,SSL_FILETYPE_PEM) ||
140 1.1 christos !SSL_CTX_use_PrivateKey_file(ctx,c_key,SSL_FILETYPE_PEM)
141 1.1 christos || !SSL_CTX_check_private_key(ctx))
142 1.1 christos ssl_err("Error setting up SSL_CTX client key and cert");
143 1.1 christos if (SSL_CTX_load_verify_locations(ctx, s_cert, NULL) != 1)
144 1.1 christos ssl_err("Error setting up SSL_CTX verify, server cert");
145 1.1 christos SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
146 1.1 christos
147 1.1 christos return ctx;
148 1.1 christos }
149 1.1 christos
150 1.1 christos /** contact the server with TCP connect */
151 1.1 christos static int
152 1.1.1.2 christos contact_server(const char* svr, struct nsd_options* cfg, int statuscmd)
153 1.1 christos {
154 1.1 christos #ifdef INET6
155 1.1 christos struct sockaddr_storage addr;
156 1.1 christos #else
157 1.1 christos struct sockaddr_in addr;
158 1.1 christos #endif
159 1.1 christos socklen_t addrlen;
160 1.1 christos int fd;
161 1.1 christos int port = cfg->control_port;
162 1.1.1.3 christos int addrfamily = 0;
163 1.1 christos /* use svr or a config entry */
164 1.1 christos if(!svr) {
165 1.1.1.2 christos if(cfg->control_interface) {
166 1.1 christos svr = cfg->control_interface->address;
167 1.1.1.2 christos } else if(cfg->do_ip4) {
168 1.1.1.2 christos svr = "127.0.0.1";
169 1.1.1.2 christos } else {
170 1.1.1.2 christos svr = "::1";
171 1.1.1.2 christos }
172 1.1 christos /* config 0 addr (everything), means ask localhost */
173 1.1 christos if(strcmp(svr, "0.0.0.0") == 0)
174 1.1 christos svr = "127.0.0.1";
175 1.1 christos else if(strcmp(svr, "::0") == 0 ||
176 1.1 christos strcmp(svr, "0::0") == 0 ||
177 1.1 christos strcmp(svr, "0::") == 0 ||
178 1.1 christos strcmp(svr, "::") == 0)
179 1.1 christos svr = "::1";
180 1.1 christos }
181 1.1 christos if(strchr(svr, '@')) {
182 1.1 christos char* ps = strchr(svr, '@');
183 1.1 christos *ps++ = 0;
184 1.1 christos port = atoi(ps);
185 1.1 christos if(!port) {
186 1.1 christos fprintf(stderr, "could not parse port %s\n", ps);
187 1.1 christos exit(1);
188 1.1 christos }
189 1.1 christos }
190 1.1.1.3 christos if(svr[0] == '/') {
191 1.1.1.3 christos #ifdef HAVE_SYS_UN_H
192 1.1.1.3 christos struct sockaddr_un* usock = (struct sockaddr_un *) &addr;
193 1.1.1.3 christos usock->sun_family = AF_LOCAL;
194 1.1.1.3 christos #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
195 1.1.1.3 christos usock->sun_len = (unsigned)sizeof(usock);
196 1.1.1.3 christos #endif
197 1.1.1.3 christos (void)strlcpy(usock->sun_path, svr, sizeof(usock->sun_path));
198 1.1.1.3 christos addrlen = (socklen_t)sizeof(struct sockaddr_un);
199 1.1.1.3 christos addrfamily = AF_LOCAL;
200 1.1.1.3 christos port = 0;
201 1.1.1.3 christos #endif
202 1.1.1.3 christos } else if(strchr(svr, ':')) {
203 1.1 christos struct sockaddr_in6 sa;
204 1.1 christos addrlen = (socklen_t)sizeof(struct sockaddr_in6);
205 1.1 christos memset(&sa, 0, addrlen);
206 1.1 christos sa.sin6_family = AF_INET6;
207 1.1 christos sa.sin6_port = (in_port_t)htons((uint16_t)port);
208 1.1 christos if(inet_pton((int)sa.sin6_family, svr, &sa.sin6_addr) <= 0) {
209 1.1 christos fprintf(stderr, "could not parse IP: %s\n", svr);
210 1.1 christos exit(1);
211 1.1 christos }
212 1.1 christos memcpy(&addr, &sa, addrlen);
213 1.1.1.3 christos addrfamily = AF_INET6;
214 1.1 christos } else { /* ip4 */
215 1.1 christos struct sockaddr_in sa;
216 1.1 christos addrlen = (socklen_t)sizeof(struct sockaddr_in);
217 1.1 christos memset(&sa, 0, addrlen);
218 1.1 christos sa.sin_family = AF_INET;
219 1.1 christos sa.sin_port = (in_port_t)htons((uint16_t)port);
220 1.1 christos if(inet_pton((int)sa.sin_family, svr, &sa.sin_addr) <= 0) {
221 1.1 christos fprintf(stderr, "could not parse IP: %s\n", svr);
222 1.1 christos exit(1);
223 1.1 christos }
224 1.1 christos memcpy(&addr, &sa, addrlen);
225 1.1.1.3 christos addrfamily = AF_INET;
226 1.1 christos }
227 1.1 christos
228 1.1.1.3 christos fd = socket(addrfamily, SOCK_STREAM, 0);
229 1.1 christos if(fd == -1) {
230 1.1 christos fprintf(stderr, "socket: %s\n", strerror(errno));
231 1.1 christos exit(1);
232 1.1 christos }
233 1.1 christos if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) {
234 1.1.1.3 christos int err = errno;
235 1.1.1.3 christos if(!port) fprintf(stderr, "error: connect (%s): %s\n", svr,
236 1.1.1.3 christos strerror(err));
237 1.1.1.3 christos else fprintf(stderr, "error: connect (%s@%d): %s\n", svr, port,
238 1.1.1.3 christos strerror(err));
239 1.1.1.3 christos if(err == ECONNREFUSED && statuscmd) {
240 1.1 christos printf("nsd is stopped\n");
241 1.1 christos exit(3);
242 1.1 christos }
243 1.1 christos exit(1);
244 1.1 christos }
245 1.1 christos return fd;
246 1.1 christos }
247 1.1 christos
248 1.1 christos /** setup SSL on the connection */
249 1.1 christos static SSL*
250 1.1 christos setup_ssl(SSL_CTX* ctx, int fd)
251 1.1 christos {
252 1.1 christos SSL* ssl;
253 1.1 christos X509* x;
254 1.1 christos int r;
255 1.1 christos
256 1.1.1.3 christos if(!ctx) return NULL;
257 1.1 christos ssl = SSL_new(ctx);
258 1.1 christos if(!ssl)
259 1.1 christos ssl_err("could not SSL_new");
260 1.1 christos SSL_set_connect_state(ssl);
261 1.1 christos (void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
262 1.1 christos if(!SSL_set_fd(ssl, fd))
263 1.1 christos ssl_err("could not SSL_set_fd");
264 1.1 christos while(1) {
265 1.1 christos ERR_clear_error();
266 1.1 christos if( (r=SSL_do_handshake(ssl)) == 1)
267 1.1 christos break;
268 1.1 christos r = SSL_get_error(ssl, r);
269 1.1 christos if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
270 1.1 christos ssl_err("SSL handshake failed");
271 1.1 christos /* wants to be called again */
272 1.1 christos }
273 1.1 christos
274 1.1 christos /* check authenticity of server */
275 1.1 christos if(SSL_get_verify_result(ssl) != X509_V_OK)
276 1.1 christos ssl_err("SSL verification failed");
277 1.1 christos x = SSL_get_peer_certificate(ssl);
278 1.1 christos if(!x)
279 1.1 christos ssl_err("Server presented no peer certificate");
280 1.1 christos X509_free(x);
281 1.1 christos return ssl;
282 1.1 christos }
283 1.1 christos
284 1.1.1.3 christos /** read from ssl or fd, fatalexit on error, 0 EOF, 1 success */
285 1.1.1.3 christos static int
286 1.1.1.3 christos remote_read(SSL* ssl, int fd, char* buf, size_t len)
287 1.1.1.3 christos {
288 1.1.1.3 christos if(ssl) {
289 1.1.1.3 christos int r;
290 1.1.1.3 christos ERR_clear_error();
291 1.1.1.3 christos if((r = SSL_read(ssl, buf, (int)len-1)) <= 0) {
292 1.1.1.3 christos if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
293 1.1.1.3 christos /* EOF */
294 1.1.1.3 christos return 0;
295 1.1.1.3 christos }
296 1.1.1.3 christos ssl_err("could not SSL_read");
297 1.1.1.3 christos }
298 1.1.1.3 christos buf[r] = 0;
299 1.1.1.3 christos } else {
300 1.1.1.3 christos ssize_t rr = read(fd, buf, len-1);
301 1.1.1.3 christos if(rr <= 0) {
302 1.1.1.3 christos if(rr == 0) {
303 1.1.1.3 christos /* EOF */
304 1.1.1.3 christos return 0;
305 1.1.1.3 christos }
306 1.1.1.3 christos fprintf(stderr, "could not read: %s\n",
307 1.1.1.3 christos strerror(errno));
308 1.1.1.3 christos exit(1);
309 1.1.1.3 christos }
310 1.1.1.3 christos buf[rr] = 0;
311 1.1.1.3 christos }
312 1.1.1.3 christos return 1;
313 1.1.1.3 christos }
314 1.1.1.3 christos
315 1.1.1.3 christos /** write to ssl or fd, fatalexit on error */
316 1.1.1.3 christos static void
317 1.1.1.3 christos remote_write(SSL* ssl, int fd, const char* buf, size_t len)
318 1.1.1.3 christos {
319 1.1.1.3 christos if(ssl) {
320 1.1.1.3 christos if(SSL_write(ssl, buf, (int)len) <= 0)
321 1.1.1.3 christos ssl_err("could not SSL_write");
322 1.1.1.3 christos } else {
323 1.1.1.3 christos if(write(fd, buf, len) < (ssize_t)len) {
324 1.1.1.3 christos fprintf(stderr, "could not write: %s\n",
325 1.1.1.3 christos strerror(errno));
326 1.1.1.3 christos exit(1);
327 1.1.1.3 christos }
328 1.1.1.3 christos }
329 1.1.1.3 christos }
330 1.1.1.3 christos
331 1.1 christos /** send stdin to server */
332 1.1 christos static void
333 1.1.1.3 christos send_file(SSL* ssl, int fd, FILE* in, char* buf, size_t sz)
334 1.1 christos {
335 1.1 christos char e[] = {0x04, 0x0a};
336 1.1 christos while(fgets(buf, (int)sz, in)) {
337 1.1.1.3 christos remote_write(ssl, fd, buf, strlen(buf));
338 1.1 christos }
339 1.1 christos /* send end-of-file marker */
340 1.1.1.3 christos remote_write(ssl, fd, e, sizeof(e));
341 1.1 christos }
342 1.1 christos
343 1.1 christos /** send command and display result */
344 1.1 christos static int
345 1.1.1.3 christos go_cmd(SSL* ssl, int fd, int argc, char* argv[])
346 1.1 christos {
347 1.1 christos char pre[10];
348 1.1 christos const char* space=" ";
349 1.1 christos const char* newline="\n";
350 1.1 christos int was_error = 0, first_line = 1;
351 1.1.1.3 christos int i;
352 1.1 christos char buf[1024];
353 1.1 christos snprintf(pre, sizeof(pre), "NSDCT%d ", NSD_CONTROL_VERSION);
354 1.1.1.3 christos remote_write(ssl, fd, pre, strlen(pre));
355 1.1 christos for(i=0; i<argc; i++) {
356 1.1.1.3 christos remote_write(ssl, fd, space, strlen(space));
357 1.1.1.3 christos remote_write(ssl, fd, argv[i], strlen(argv[i]));
358 1.1 christos }
359 1.1.1.3 christos remote_write(ssl, fd, newline, strlen(newline));
360 1.1 christos
361 1.1 christos /* send contents to server */
362 1.1 christos if(argc == 1 && (strcmp(argv[0], "addzones") == 0 ||
363 1.1 christos strcmp(argv[0], "delzones") == 0)) {
364 1.1.1.3 christos send_file(ssl, fd, stdin, buf, sizeof(buf));
365 1.1 christos }
366 1.1 christos
367 1.1 christos while(1) {
368 1.1.1.3 christos if(remote_read(ssl, fd, buf, sizeof(buf)) == 0) {
369 1.1.1.3 christos break; /* EOF */
370 1.1 christos }
371 1.1 christos printf("%s", buf);
372 1.1 christos if(first_line && strncmp(buf, "error", 5) == 0)
373 1.1 christos was_error = 1;
374 1.1 christos first_line = 0;
375 1.1 christos }
376 1.1 christos return was_error;
377 1.1 christos }
378 1.1 christos
379 1.1 christos /** go ahead and read config, contact server and perform command and display */
380 1.1 christos static int
381 1.1 christos go(const char* cfgfile, char* svr, int argc, char* argv[])
382 1.1 christos {
383 1.1.1.2 christos struct nsd_options* opt;
384 1.1 christos int fd, ret;
385 1.1 christos SSL_CTX* ctx;
386 1.1 christos SSL* ssl;
387 1.1 christos
388 1.1 christos /* read config */
389 1.1 christos if(!(opt = nsd_options_create(region_create(xalloc, free)))) {
390 1.1 christos fprintf(stderr, "out of memory\n");
391 1.1 christos exit(1);
392 1.1 christos }
393 1.1 christos tsig_init(opt->region);
394 1.1 christos if(!parse_options_file(opt, cfgfile, NULL, NULL)) {
395 1.1 christos fprintf(stderr, "could not read config file\n");
396 1.1 christos exit(1);
397 1.1 christos }
398 1.1 christos if(!opt->control_enable)
399 1.1 christos fprintf(stderr, "warning: control-enable is 'no' in the config file.\n");
400 1.1 christos ctx = setup_ctx(opt);
401 1.1 christos
402 1.1 christos /* contact server */
403 1.1 christos fd = contact_server(svr, opt, argc>0&&strcmp(argv[0],"status")==0);
404 1.1 christos ssl = setup_ssl(ctx, fd);
405 1.1 christos
406 1.1 christos /* send command */
407 1.1.1.3 christos ret = go_cmd(ssl, fd, argc, argv);
408 1.1 christos
409 1.1.1.3 christos if(ssl) SSL_free(ssl);
410 1.1 christos close(fd);
411 1.1.1.3 christos if(ctx) SSL_CTX_free(ctx);
412 1.1 christos region_destroy(opt->region);
413 1.1 christos return ret;
414 1.1 christos }
415 1.1 christos
416 1.1 christos /** getopt global, in case header files fail to declare it. */
417 1.1 christos extern int optind;
418 1.1 christos /** getopt global, in case header files fail to declare it. */
419 1.1 christos extern char* optarg;
420 1.1 christos
421 1.1 christos /** Main routine for nsd-control */
422 1.1 christos int main(int argc, char* argv[])
423 1.1 christos {
424 1.1 christos int c;
425 1.1 christos const char* cfgfile = CONFIGFILE;
426 1.1 christos char* svr = NULL;
427 1.1 christos #ifdef USE_WINSOCK
428 1.1 christos int r;
429 1.1 christos WSADATA wsa_data;
430 1.1 christos #endif
431 1.1 christos log_init("nsd-control");
432 1.1 christos
433 1.1 christos #ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
434 1.1 christos ERR_load_crypto_strings();
435 1.1 christos #endif
436 1.1 christos ERR_load_SSL_strings();
437 1.1 christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
438 1.1 christos OpenSSL_add_all_algorithms();
439 1.1 christos #else
440 1.1 christos OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
441 1.1 christos | OPENSSL_INIT_ADD_ALL_DIGESTS
442 1.1 christos | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
443 1.1 christos #endif
444 1.1 christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
445 1.1 christos (void)SSL_library_init();
446 1.1 christos #else
447 1.1 christos OPENSSL_init_ssl(0, NULL);
448 1.1 christos #endif
449 1.1 christos
450 1.1 christos if(!RAND_status()) {
451 1.1 christos /* try to seed it */
452 1.1 christos unsigned char buf[256];
453 1.1 christos unsigned int v, seed=(unsigned)time(NULL) ^ (unsigned)getpid();
454 1.1 christos size_t i;
455 1.1 christos v = seed;
456 1.1 christos for(i=0; i<256/sizeof(v); i++) {
457 1.1 christos memmove(buf+i*sizeof(v), &v, sizeof(v));
458 1.1 christos v = v*seed + (unsigned int)i;
459 1.1 christos }
460 1.1 christos RAND_seed(buf, 256);
461 1.1 christos fprintf(stderr, "warning: no entropy, seeding openssl PRNG with time\n");
462 1.1 christos }
463 1.1 christos
464 1.1 christos /* parse the options */
465 1.1 christos while( (c=getopt(argc, argv, "c:s:h")) != -1) {
466 1.1 christos switch(c) {
467 1.1 christos case 'c':
468 1.1 christos cfgfile = optarg;
469 1.1 christos break;
470 1.1 christos case 's':
471 1.1 christos svr = optarg;
472 1.1 christos break;
473 1.1 christos case '?':
474 1.1 christos case 'h':
475 1.1 christos default:
476 1.1 christos usage();
477 1.1 christos }
478 1.1 christos }
479 1.1 christos argc -= optind;
480 1.1 christos argv += optind;
481 1.1 christos if(argc == 0)
482 1.1 christos usage();
483 1.1 christos if(argc >= 1 && strcmp(argv[0], "start")==0) {
484 1.1 christos if(execl(NSD_START_PATH, "nsd", "-c", cfgfile,
485 1.1 christos (char*)NULL) < 0) {
486 1.1 christos fprintf(stderr, "could not exec %s: %s\n",
487 1.1 christos NSD_START_PATH, strerror(errno));
488 1.1 christos exit(1);
489 1.1 christos }
490 1.1 christos }
491 1.1 christos
492 1.1 christos return go(cfgfile, svr, argc, argv);
493 1.1 christos }
494 1.1 christos
495 1.1 christos #else /* HAVE_SSL */
496 1.1 christos int main(void)
497 1.1 christos {
498 1.1 christos printf("error: NSD was compiled without SSL.\n");
499 1.1 christos return 1;
500 1.1 christos }
501 1.1 christos #endif /* HAVE_SSL */
502