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