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