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(nsd_options_t* 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, nsd_options_t* 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 svr = "127.0.0.1"; 162 /* config 0 addr (everything), means ask localhost */ 163 if(strcmp(svr, "0.0.0.0") == 0) 164 svr = "127.0.0.1"; 165 else if(strcmp(svr, "::0") == 0 || 166 strcmp(svr, "0::0") == 0 || 167 strcmp(svr, "0::") == 0 || 168 strcmp(svr, "::") == 0) 169 svr = "::1"; 170 } 171 if(strchr(svr, '@')) { 172 char* ps = strchr(svr, '@'); 173 *ps++ = 0; 174 port = atoi(ps); 175 if(!port) { 176 fprintf(stderr, "could not parse port %s\n", ps); 177 exit(1); 178 } 179 } 180 if(strchr(svr, ':')) { 181 struct sockaddr_in6 sa; 182 addrlen = (socklen_t)sizeof(struct sockaddr_in6); 183 memset(&sa, 0, addrlen); 184 sa.sin6_family = AF_INET6; 185 sa.sin6_port = (in_port_t)htons((uint16_t)port); 186 if(inet_pton((int)sa.sin6_family, svr, &sa.sin6_addr) <= 0) { 187 fprintf(stderr, "could not parse IP: %s\n", svr); 188 exit(1); 189 } 190 memcpy(&addr, &sa, addrlen); 191 } else { /* ip4 */ 192 struct sockaddr_in sa; 193 addrlen = (socklen_t)sizeof(struct sockaddr_in); 194 memset(&sa, 0, addrlen); 195 sa.sin_family = AF_INET; 196 sa.sin_port = (in_port_t)htons((uint16_t)port); 197 if(inet_pton((int)sa.sin_family, svr, &sa.sin_addr) <= 0) { 198 fprintf(stderr, "could not parse IP: %s\n", svr); 199 exit(1); 200 } 201 memcpy(&addr, &sa, addrlen); 202 } 203 204 fd = socket(strchr(svr, ':')?AF_INET6:AF_INET, SOCK_STREAM, 0); 205 if(fd == -1) { 206 fprintf(stderr, "socket: %s\n", strerror(errno)); 207 exit(1); 208 } 209 if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) { 210 fprintf(stderr, "error: connect (%s@%d): %s\n", svr, port, 211 strerror(errno)); 212 if(errno == ECONNREFUSED && statuscmd) { 213 printf("nsd is stopped\n"); 214 exit(3); 215 } 216 exit(1); 217 } 218 return fd; 219 } 220 221 /** setup SSL on the connection */ 222 static SSL* 223 setup_ssl(SSL_CTX* ctx, int fd) 224 { 225 SSL* ssl; 226 X509* x; 227 int r; 228 229 ssl = SSL_new(ctx); 230 if(!ssl) 231 ssl_err("could not SSL_new"); 232 SSL_set_connect_state(ssl); 233 (void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); 234 if(!SSL_set_fd(ssl, fd)) 235 ssl_err("could not SSL_set_fd"); 236 while(1) { 237 ERR_clear_error(); 238 if( (r=SSL_do_handshake(ssl)) == 1) 239 break; 240 r = SSL_get_error(ssl, r); 241 if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE) 242 ssl_err("SSL handshake failed"); 243 /* wants to be called again */ 244 } 245 246 /* check authenticity of server */ 247 if(SSL_get_verify_result(ssl) != X509_V_OK) 248 ssl_err("SSL verification failed"); 249 x = SSL_get_peer_certificate(ssl); 250 if(!x) 251 ssl_err("Server presented no peer certificate"); 252 X509_free(x); 253 return ssl; 254 } 255 256 /** send stdin to server */ 257 static void 258 send_file(SSL* ssl, FILE* in, char* buf, size_t sz) 259 { 260 char e[] = {0x04, 0x0a}; 261 while(fgets(buf, (int)sz, in)) { 262 if(SSL_write(ssl, buf, (int)strlen(buf)) <= 0) 263 ssl_err("could not SSL_write contents"); 264 } 265 /* send end-of-file marker */ 266 if(SSL_write(ssl, e, (int)sizeof(e)) <= 0) 267 ssl_err("could not SSL_write end-of-file marker"); 268 } 269 270 /** send command and display result */ 271 static int 272 go_cmd(SSL* ssl, int argc, char* argv[]) 273 { 274 char pre[10]; 275 const char* space=" "; 276 const char* newline="\n"; 277 int was_error = 0, first_line = 1; 278 int r, i; 279 char buf[1024]; 280 snprintf(pre, sizeof(pre), "NSDCT%d ", NSD_CONTROL_VERSION); 281 if(SSL_write(ssl, pre, (int)strlen(pre)) <= 0) 282 ssl_err("could not SSL_write"); 283 for(i=0; i<argc; i++) { 284 if(SSL_write(ssl, space, (int)strlen(space)) <= 0) 285 ssl_err("could not SSL_write"); 286 if(SSL_write(ssl, argv[i], (int)strlen(argv[i])) <= 0) 287 ssl_err("could not SSL_write"); 288 } 289 if(SSL_write(ssl, newline, (int)strlen(newline)) <= 0) 290 ssl_err("could not SSL_write"); 291 292 /* send contents to server */ 293 if(argc == 1 && (strcmp(argv[0], "addzones") == 0 || 294 strcmp(argv[0], "delzones") == 0)) { 295 send_file(ssl, stdin, buf, sizeof(buf)); 296 } 297 298 while(1) { 299 ERR_clear_error(); 300 if((r = SSL_read(ssl, buf, (int)sizeof(buf)-1)) <= 0) { 301 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { 302 /* EOF */ 303 break; 304 } 305 ssl_err("could not SSL_read"); 306 } 307 buf[r] = 0; 308 printf("%s", buf); 309 if(first_line && strncmp(buf, "error", 5) == 0) 310 was_error = 1; 311 first_line = 0; 312 } 313 return was_error; 314 } 315 316 /** go ahead and read config, contact server and perform command and display */ 317 static int 318 go(const char* cfgfile, char* svr, int argc, char* argv[]) 319 { 320 nsd_options_t* opt; 321 int fd, ret; 322 SSL_CTX* ctx; 323 SSL* ssl; 324 325 /* read config */ 326 if(!(opt = nsd_options_create(region_create(xalloc, free)))) { 327 fprintf(stderr, "out of memory\n"); 328 exit(1); 329 } 330 tsig_init(opt->region); 331 if(!parse_options_file(opt, cfgfile, NULL, NULL)) { 332 fprintf(stderr, "could not read config file\n"); 333 exit(1); 334 } 335 if(!opt->control_enable) 336 fprintf(stderr, "warning: control-enable is 'no' in the config file.\n"); 337 ctx = setup_ctx(opt); 338 339 /* contact server */ 340 fd = contact_server(svr, opt, argc>0&&strcmp(argv[0],"status")==0); 341 ssl = setup_ssl(ctx, fd); 342 343 /* send command */ 344 ret = go_cmd(ssl, argc, argv); 345 346 SSL_free(ssl); 347 close(fd); 348 SSL_CTX_free(ctx); 349 region_destroy(opt->region); 350 return ret; 351 } 352 353 /** getopt global, in case header files fail to declare it. */ 354 extern int optind; 355 /** getopt global, in case header files fail to declare it. */ 356 extern char* optarg; 357 358 /** Main routine for nsd-control */ 359 int main(int argc, char* argv[]) 360 { 361 int c; 362 const char* cfgfile = CONFIGFILE; 363 char* svr = NULL; 364 #ifdef USE_WINSOCK 365 int r; 366 WSADATA wsa_data; 367 #endif 368 log_init("nsd-control"); 369 370 ERR_load_crypto_strings(); 371 ERR_load_SSL_strings(); 372 OpenSSL_add_all_algorithms(); 373 (void)SSL_library_init(); 374 375 if(!RAND_status()) { 376 /* try to seed it */ 377 unsigned char buf[256]; 378 unsigned int v, seed=(unsigned)time(NULL) ^ (unsigned)getpid(); 379 size_t i; 380 v = seed; 381 for(i=0; i<256/sizeof(v); i++) { 382 memmove(buf+i*sizeof(v), &v, sizeof(v)); 383 v = v*seed + (unsigned int)i; 384 } 385 RAND_seed(buf, 256); 386 fprintf(stderr, "warning: no entropy, seeding openssl PRNG with time\n"); 387 } 388 389 /* parse the options */ 390 while( (c=getopt(argc, argv, "c:s:h")) != -1) { 391 switch(c) { 392 case 'c': 393 cfgfile = optarg; 394 break; 395 case 's': 396 svr = optarg; 397 break; 398 case '?': 399 case 'h': 400 default: 401 usage(); 402 } 403 } 404 argc -= optind; 405 argv += optind; 406 if(argc == 0) 407 usage(); 408 if(argc >= 1 && strcmp(argv[0], "start")==0) { 409 if(execl(NSD_START_PATH, "nsd", "-c", cfgfile, 410 (char*)NULL) < 0) { 411 fprintf(stderr, "could not exec %s: %s\n", 412 NSD_START_PATH, strerror(errno)); 413 exit(1); 414 } 415 } 416 417 return go(cfgfile, svr, argc, argv); 418 } 419 420 #else /* HAVE_SSL */ 421 int main(void) 422 { 423 printf("error: NSD was compiled without SSL.\n"); 424 return 1; 425 } 426 #endif /* HAVE_SSL */ 427