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