1 /* 2 * checkconf/unbound-control.c - remote control utility for unbound. 3 * 4 * Copyright (c) 2008, 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 LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /** 37 * \file 38 * 39 * The remote control utility contacts the unbound 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_GETOPT_H 46 #include <getopt.h> 47 #endif 48 #ifdef HAVE_OPENSSL_SSL_H 49 #include <openssl/ssl.h> 50 #endif 51 #ifdef HAVE_OPENSSL_ERR_H 52 #include <openssl/err.h> 53 #endif 54 #ifdef HAVE_OPENSSL_RAND_H 55 #include <openssl/rand.h> 56 #endif 57 #include "util/log.h" 58 #include "util/config_file.h" 59 #include "util/locks.h" 60 #include "util/net_help.h" 61 62 /** Give unbound-control usage, and exit (1). */ 63 static void 64 usage() 65 { 66 printf("Usage: unbound-control [options] command\n"); 67 printf(" Remote control utility for unbound server.\n"); 68 printf("Options:\n"); 69 printf(" -c file config file, default is %s\n", CONFIGFILE); 70 printf(" -s ip[@port] server address, if omitted config is used.\n"); 71 printf(" -h show this usage help.\n"); 72 printf("Commands:\n"); 73 printf(" start start server; runs unbound(8)\n"); 74 printf(" stop stops the server\n"); 75 printf(" reload reloads the server\n"); 76 printf(" (this flushes data, stats, requestlist)\n"); 77 printf(" stats print statistics\n"); 78 printf(" stats_noreset peek at statistics\n"); 79 printf(" status display status of server\n"); 80 printf(" verbosity <number> change logging detail\n"); 81 printf(" log_reopen close and open the logfile\n"); 82 printf(" local_zone <name> <type> add new local zone\n"); 83 printf(" local_zone_remove <name> remove local zone and its contents\n"); 84 printf(" local_data <RR data...> add local data, for example\n"); 85 printf(" local_data www.example.com A 192.0.2.1\n"); 86 printf(" local_data_remove <name> remove local RR data from name\n"); 87 printf(" dump_cache print cache to stdout\n"); 88 printf(" load_cache load cache from stdin\n"); 89 printf(" lookup <name> print nameservers for name\n"); 90 printf(" flush <name> flushes common types for name from cache\n"); 91 printf(" types: A, AAAA, MX, PTR, NS,\n"); 92 printf(" SOA, CNAME, DNAME, SRV, NAPTR\n"); 93 printf(" flush_type <name> <type> flush name, type from cache\n"); 94 printf(" flush_zone <name> flush everything at or under name\n"); 95 printf(" from rr and dnssec caches\n"); 96 printf(" flush_stats flush statistics, make zero\n"); 97 printf(" flush_requestlist drop queries that are worked on\n"); 98 printf(" dump_requestlist show what is worked on\n"); 99 printf(" flush_infra [all | ip] remove ping, edns for one IP or all\n"); 100 printf(" dump_infra show ping and edns entries\n"); 101 printf(" set_option opt: val set option to value, no reload\n"); 102 printf(" get_option opt get option value\n"); 103 printf(" list_stubs list stub-zones and root hints in use\n"); 104 printf(" list_forwards list forward-zones in use\n"); 105 printf(" list_local_zones list local-zones in use\n"); 106 printf(" list_local_data list local-data RRs in use\n"); 107 printf(" forward [off | addr ...] without arg show forward setup\n"); 108 printf(" or off to turn off root forwarding\n"); 109 printf(" or give list of ip addresses\n"); 110 printf("Version %s\n", PACKAGE_VERSION); 111 printf("BSD licensed, see LICENSE in source package for details.\n"); 112 printf("Report bugs to %s\n", PACKAGE_BUGREPORT); 113 exit(1); 114 } 115 116 /** exit with ssl error */ 117 static void ssl_err(const char* s) 118 { 119 fprintf(stderr, "error: %s\n", s); 120 ERR_print_errors_fp(stderr); 121 exit(1); 122 } 123 124 /** setup SSL context */ 125 static SSL_CTX* 126 setup_ctx(struct config_file* cfg) 127 { 128 char* s_cert, *c_key, *c_cert; 129 SSL_CTX* ctx; 130 131 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1); 132 c_key = fname_after_chroot(cfg->control_key_file, cfg, 1); 133 c_cert = fname_after_chroot(cfg->control_cert_file, cfg, 1); 134 if(!s_cert || !c_key || !c_cert) 135 fatal_exit("out of memory"); 136 ctx = SSL_CTX_new(SSLv23_client_method()); 137 if(!ctx) 138 ssl_err("could not allocate SSL_CTX pointer"); 139 if(!(SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)) 140 ssl_err("could not set SSL_OP_NO_SSLv2"); 141 if(!SSL_CTX_use_certificate_file(ctx,c_cert,SSL_FILETYPE_PEM) || 142 !SSL_CTX_use_PrivateKey_file(ctx,c_key,SSL_FILETYPE_PEM) 143 || !SSL_CTX_check_private_key(ctx)) 144 ssl_err("Error setting up SSL_CTX client key and cert"); 145 if (SSL_CTX_load_verify_locations(ctx, s_cert, NULL) != 1) 146 ssl_err("Error setting up SSL_CTX verify, server cert"); 147 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); 148 149 free(s_cert); 150 free(c_key); 151 free(c_cert); 152 return ctx; 153 } 154 155 /** contact the server with TCP connect */ 156 static int 157 contact_server(const char* svr, struct config_file* cfg, int statuscmd) 158 { 159 struct sockaddr_storage addr; 160 socklen_t addrlen; 161 int fd; 162 /* use svr or the first config entry */ 163 if(!svr) { 164 if(cfg->control_ifs) 165 svr = cfg->control_ifs->str; 166 else svr = "127.0.0.1"; 167 /* config 0 addr (everything), means ask localhost */ 168 if(strcmp(svr, "0.0.0.0") == 0) 169 svr = "127.0.0.1"; 170 else if(strcmp(svr, "::0") == 0 || 171 strcmp(svr, "0::0") == 0 || 172 strcmp(svr, "0::") == 0 || 173 strcmp(svr, "::") == 0) 174 svr = "::1"; 175 } 176 if(strchr(svr, '@')) { 177 if(!extstrtoaddr(svr, &addr, &addrlen)) 178 fatal_exit("could not parse IP@port: %s", svr); 179 } else { 180 if(!ipstrtoaddr(svr, cfg->control_port, &addr, &addrlen)) 181 fatal_exit("could not parse IP: %s", svr); 182 } 183 fd = socket(addr_is_ip6(&addr, addrlen)?AF_INET6:AF_INET, 184 SOCK_STREAM, 0); 185 if(fd == -1) { 186 #ifndef USE_WINSOCK 187 fatal_exit("socket: %s", strerror(errno)); 188 #else 189 fatal_exit("socket: %s", wsa_strerror(WSAGetLastError())); 190 #endif 191 } 192 if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) { 193 log_addr(0, "address", &addr, addrlen); 194 #ifndef USE_WINSOCK 195 log_err("connect: %s", strerror(errno)); 196 if(errno == ECONNREFUSED && statuscmd) { 197 printf("unbound is stopped\n"); 198 exit(3); 199 } 200 #else 201 log_err("connect: %s", wsa_strerror(WSAGetLastError())); 202 if(WSAGetLastError() == WSAECONNREFUSED && statuscmd) { 203 printf("unbound is stopped\n"); 204 exit(3); 205 } 206 #endif 207 exit(1); 208 } 209 return fd; 210 } 211 212 /** setup SSL on the connection */ 213 static SSL* 214 setup_ssl(SSL_CTX* ctx, int fd) 215 { 216 SSL* ssl; 217 X509* x; 218 int r; 219 220 ssl = SSL_new(ctx); 221 if(!ssl) 222 ssl_err("could not SSL_new"); 223 SSL_set_connect_state(ssl); 224 (void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); 225 if(!SSL_set_fd(ssl, fd)) 226 ssl_err("could not SSL_set_fd"); 227 while(1) { 228 ERR_clear_error(); 229 if( (r=SSL_do_handshake(ssl)) == 1) 230 break; 231 r = SSL_get_error(ssl, r); 232 if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE) 233 ssl_err("SSL handshake failed"); 234 /* wants to be called again */ 235 } 236 237 /* check authenticity of server */ 238 if(SSL_get_verify_result(ssl) != X509_V_OK) 239 ssl_err("SSL verification failed"); 240 x = SSL_get_peer_certificate(ssl); 241 if(!x) 242 ssl_err("Server presented no peer certificate"); 243 X509_free(x); 244 return ssl; 245 } 246 247 /** send stdin to server */ 248 static void 249 send_file(SSL* ssl, FILE* in, char* buf, size_t sz) 250 { 251 while(fgets(buf, (int)sz, in)) { 252 if(SSL_write(ssl, buf, (int)strlen(buf)) <= 0) 253 ssl_err("could not SSL_write contents"); 254 } 255 } 256 257 /** send command and display result */ 258 static int 259 go_cmd(SSL* ssl, int argc, char* argv[]) 260 { 261 char pre[10]; 262 const char* space=" "; 263 const char* newline="\n"; 264 int was_error = 0, first_line = 1; 265 int r, i; 266 char buf[1024]; 267 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION); 268 if(SSL_write(ssl, pre, (int)strlen(pre)) <= 0) 269 ssl_err("could not SSL_write"); 270 for(i=0; i<argc; i++) { 271 if(SSL_write(ssl, space, (int)strlen(space)) <= 0) 272 ssl_err("could not SSL_write"); 273 if(SSL_write(ssl, argv[i], (int)strlen(argv[i])) <= 0) 274 ssl_err("could not SSL_write"); 275 } 276 if(SSL_write(ssl, newline, (int)strlen(newline)) <= 0) 277 ssl_err("could not SSL_write"); 278 279 if(argc == 1 && strcmp(argv[0], "load_cache") == 0) { 280 send_file(ssl, stdin, buf, sizeof(buf)); 281 } 282 283 while(1) { 284 ERR_clear_error(); 285 if((r = SSL_read(ssl, buf, (int)sizeof(buf)-1)) <= 0) { 286 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { 287 /* EOF */ 288 break; 289 } 290 ssl_err("could not SSL_read"); 291 } 292 buf[r] = 0; 293 printf("%s", buf); 294 if(first_line && strncmp(buf, "error", 5) == 0) 295 was_error = 1; 296 first_line = 0; 297 } 298 return was_error; 299 } 300 301 /** go ahead and read config, contact server and perform command and display */ 302 static int 303 go(const char* cfgfile, char* svr, int argc, char* argv[]) 304 { 305 struct config_file* cfg; 306 int fd, ret; 307 SSL_CTX* ctx; 308 SSL* ssl; 309 310 /* read config */ 311 if(!(cfg = config_create())) 312 fatal_exit("out of memory"); 313 if(!config_read(cfg, cfgfile, NULL)) 314 fatal_exit("could not read config file"); 315 if(!cfg->remote_control_enable) 316 log_warn("control-enable is 'no' in the config file."); 317 ctx = setup_ctx(cfg); 318 319 /* contact server */ 320 fd = contact_server(svr, cfg, argc>0&&strcmp(argv[0],"status")==0); 321 ssl = setup_ssl(ctx, fd); 322 323 /* send command */ 324 ret = go_cmd(ssl, argc, argv); 325 326 SSL_free(ssl); 327 #ifndef USE_WINSOCK 328 close(fd); 329 #else 330 closesocket(fd); 331 #endif 332 SSL_CTX_free(ctx); 333 config_delete(cfg); 334 return ret; 335 } 336 337 /** getopt global, in case header files fail to declare it. */ 338 extern int optind; 339 /** getopt global, in case header files fail to declare it. */ 340 extern char* optarg; 341 342 /** Main routine for unbound-control */ 343 int main(int argc, char* argv[]) 344 { 345 int c, ret; 346 const char* cfgfile = CONFIGFILE; 347 char* svr = NULL; 348 #ifdef USE_WINSOCK 349 int r; 350 WSADATA wsa_data; 351 #endif 352 #ifdef USE_THREAD_DEBUG 353 /* stop the file output from unbound-control, overwites the servers */ 354 extern int check_locking_order; 355 check_locking_order = 0; 356 #endif /* USE_THREAD_DEBUG */ 357 log_ident_set("unbound-control"); 358 log_init(NULL, 0, NULL); 359 checklock_start(); 360 #ifdef USE_WINSOCK 361 if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) 362 fatal_exit("WSAStartup failed: %s", wsa_strerror(r)); 363 #endif 364 365 ERR_load_crypto_strings(); 366 ERR_load_SSL_strings(); 367 OpenSSL_add_all_algorithms(); 368 (void)SSL_library_init(); 369 370 if(!RAND_status()) { 371 /* try to seed it */ 372 unsigned char buf[256]; 373 unsigned int v, seed=(unsigned)time(NULL) ^ (unsigned)getpid(); 374 size_t i; 375 for(i=0; i<256/sizeof(v); i++) { 376 memmove(buf+i*sizeof(v), &v, sizeof(v)); 377 v = v*seed + (unsigned int)i; 378 } 379 RAND_seed(buf, 256); 380 log_warn("no entropy, seeding openssl PRNG with time\n"); 381 } 382 383 /* parse the options */ 384 while( (c=getopt(argc, argv, "c:s:h")) != -1) { 385 switch(c) { 386 case 'c': 387 cfgfile = optarg; 388 break; 389 case 's': 390 svr = optarg; 391 break; 392 case '?': 393 case 'h': 394 default: 395 usage(); 396 } 397 } 398 argc -= optind; 399 argv += optind; 400 if(argc == 0) 401 usage(); 402 if(argc >= 1 && strcmp(argv[0], "start")==0) { 403 if(execlp("unbound", "unbound", "-c", cfgfile, 404 (char*)NULL) < 0) { 405 fatal_exit("could not exec unbound: %s", 406 strerror(errno)); 407 } 408 } 409 410 ret = go(cfgfile, svr, argc, argv); 411 412 #ifdef USE_WINSOCK 413 WSACleanup(); 414 #endif 415 checklock_stop(); 416 return ret; 417 } 418