1 /* 2 * daemon/remote.c - remote control for the unbound daemon. 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 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 * This file contains the remote control functionality for the daemon. 40 * The remote control can be performed using either the commandline 41 * unbound-control tool, or a TLS capable web browser. 42 * The channel is secured using TLSv1, and certificates. 43 * Both the server and the client(control tool) have their own keys. 44 */ 45 #include "config.h" 46 #ifdef HAVE_OPENSSL_ERR_H 47 #include <openssl/err.h> 48 #endif 49 #ifdef HAVE_OPENSSL_DH_H 50 #include <openssl/dh.h> 51 #endif 52 #ifdef HAVE_OPENSSL_BN_H 53 #include <openssl/bn.h> 54 #endif 55 56 #include <ctype.h> 57 #include "daemon/remote.h" 58 #include "daemon/worker.h" 59 #include "daemon/daemon.h" 60 #include "daemon/stats.h" 61 #include "daemon/cachedump.h" 62 #include "util/log.h" 63 #include "util/config_file.h" 64 #include "util/net_help.h" 65 #include "util/module.h" 66 #include "services/listen_dnsport.h" 67 #include "services/cache/rrset.h" 68 #include "services/cache/infra.h" 69 #include "services/mesh.h" 70 #include "services/localzone.h" 71 #include "services/authzone.h" 72 #include "util/storage/slabhash.h" 73 #include "util/fptr_wlist.h" 74 #include "util/data/dname.h" 75 #include "validator/validator.h" 76 #include "validator/val_kcache.h" 77 #include "validator/val_kentry.h" 78 #include "validator/val_anchor.h" 79 #include "iterator/iterator.h" 80 #include "iterator/iter_fwd.h" 81 #include "iterator/iter_hints.h" 82 #include "iterator/iter_delegpt.h" 83 #include "services/outbound_list.h" 84 #include "services/outside_network.h" 85 #include "sldns/str2wire.h" 86 #include "sldns/parseutil.h" 87 #include "sldns/wire2str.h" 88 #include "sldns/sbuffer.h" 89 90 #ifdef HAVE_SYS_TYPES_H 91 # include <sys/types.h> 92 #endif 93 #ifdef HAVE_SYS_STAT_H 94 #include <sys/stat.h> 95 #endif 96 #ifdef HAVE_NETDB_H 97 #include <netdb.h> 98 #endif 99 100 /* just for portability */ 101 #ifdef SQ 102 #undef SQ 103 #endif 104 105 /** what to put on statistics lines between var and value, ": " or "=" */ 106 #define SQ "=" 107 /** if true, inhibits a lot of =0 lines from the stats output */ 108 static const int inhibit_zero = 1; 109 110 /** subtract timers and the values do not overflow or become negative */ 111 static void 112 timeval_subtract(struct timeval* d, const struct timeval* end, 113 const struct timeval* start) 114 { 115 #ifndef S_SPLINT_S 116 time_t end_usec = end->tv_usec; 117 d->tv_sec = end->tv_sec - start->tv_sec; 118 if(end_usec < start->tv_usec) { 119 end_usec += 1000000; 120 d->tv_sec--; 121 } 122 d->tv_usec = end_usec - start->tv_usec; 123 #endif 124 } 125 126 /** divide sum of timers to get average */ 127 static void 128 timeval_divide(struct timeval* avg, const struct timeval* sum, long long d) 129 { 130 #ifndef S_SPLINT_S 131 size_t leftover; 132 if(d == 0) { 133 avg->tv_sec = 0; 134 avg->tv_usec = 0; 135 return; 136 } 137 avg->tv_sec = sum->tv_sec / d; 138 avg->tv_usec = sum->tv_usec / d; 139 /* handle fraction from seconds divide */ 140 leftover = sum->tv_sec - avg->tv_sec*d; 141 avg->tv_usec += (leftover*1000000)/d; 142 #endif 143 } 144 145 /* 146 * The following function was generated using the openssl utility, using 147 * the command : "openssl dhparam -C 2048" 148 * (some openssl versions reject DH that is 'too small', eg. 512). 149 */ 150 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL) 151 #ifndef S_SPLINT_S 152 static DH *get_dh2048(void) 153 { 154 static unsigned char dh2048_p[]={ 155 0xE7,0x36,0x28,0x3B,0xE4,0xC3,0x32,0x1C,0x01,0xC3,0x67,0xD6, 156 0xF5,0xF3,0xDA,0xDC,0x71,0xC0,0x42,0x8B,0xE6,0xEB,0x8D,0x80, 157 0x35,0x7F,0x09,0x45,0x30,0xE5,0xB2,0x92,0x81,0x3F,0x08,0xCD, 158 0x36,0x5E,0x19,0x83,0x62,0xCC,0xAE,0x9B,0x81,0x66,0x24,0xEE, 159 0x16,0x6F,0xA9,0x9E,0xF4,0x82,0x1B,0xDD,0x46,0xC7,0x33,0x5D, 160 0xF4,0xCA,0xE6,0x8F,0xFC,0xD4,0xD8,0x58,0x94,0x24,0x5D,0xFF, 161 0x0A,0xE8,0xEF,0x3D,0xCE,0xBB,0x50,0x94,0xE0,0x5F,0xE8,0x41, 162 0xC3,0x35,0x30,0x37,0xD5,0xCB,0x8F,0x3D,0x95,0x15,0x1A,0x77, 163 0x42,0xB2,0x06,0x86,0xF6,0x09,0x66,0x0E,0x9A,0x25,0x94,0x3E, 164 0xD2,0x04,0x25,0x25,0x1D,0x23,0xEB,0xDC,0x4D,0x0C,0x83,0x28, 165 0x2E,0x15,0x81,0x2D,0xC1,0xAF,0x8D,0x36,0x64,0xE3,0x9A,0x83, 166 0x78,0xC2,0x8D,0xC0,0x9D,0xD9,0x3A,0x1C,0xC5,0x2B,0x50,0x68, 167 0x07,0xA9,0x4B,0x8C,0x07,0x57,0xD6,0x15,0x03,0x4E,0x9E,0x01, 168 0xF2,0x6F,0x35,0xAC,0x26,0x9C,0x92,0x68,0x61,0x13,0xFB,0x01, 169 0xBA,0x22,0x36,0x01,0x55,0xB6,0x62,0xD9,0xB2,0x98,0xCE,0x5D, 170 0x4B,0xA5,0x41,0xD6,0xE5,0x70,0x78,0x12,0x1F,0x64,0xB6,0x6F, 171 0xB0,0x91,0x51,0x91,0x92,0xC0,0x94,0x3A,0xD1,0x28,0x4D,0x30, 172 0x84,0x3E,0xE4,0xE4,0x7F,0x47,0x89,0xB1,0xB6,0x8C,0x8E,0x0E, 173 0x26,0xDB,0xCD,0x17,0x07,0x2A,0x21,0x7A,0xCC,0x68,0xE8,0x57, 174 0x94,0x9E,0x59,0x61,0xEC,0x20,0x34,0x26,0x0D,0x66,0x44,0xEB, 175 0x6F,0x02,0x58,0xE2,0xED,0xF6,0xF3,0x1B,0xBF,0x9E,0x45,0x52, 176 0x5A,0x49,0xA1,0x5B, 177 }; 178 static unsigned char dh2048_g[]={ 179 0x02, 180 }; 181 DH *dh = NULL; 182 BIGNUM *p = NULL, *g = NULL; 183 184 dh = DH_new(); 185 p = BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL); 186 g = BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL); 187 if (!dh || !p || !g) 188 goto err; 189 190 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL) 191 dh->p = p; 192 dh->g = g; 193 #else 194 if (!DH_set0_pqg(dh, p, NULL, g)) 195 goto err; 196 #endif 197 return dh; 198 err: 199 if (p) 200 BN_free(p); 201 if (g) 202 BN_free(g); 203 if (dh) 204 DH_free(dh); 205 return NULL; 206 } 207 #endif /* SPLINT */ 208 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000 */ 209 210 struct daemon_remote* 211 daemon_remote_create(struct config_file* cfg) 212 { 213 char* s_cert; 214 char* s_key; 215 struct daemon_remote* rc = (struct daemon_remote*)calloc(1, 216 sizeof(*rc)); 217 if(!rc) { 218 log_err("out of memory in daemon_remote_create"); 219 return NULL; 220 } 221 rc->max_active = 10; 222 223 if(!cfg->remote_control_enable) { 224 rc->ctx = NULL; 225 return rc; 226 } 227 rc->ctx = SSL_CTX_new(SSLv23_server_method()); 228 if(!rc->ctx) { 229 log_crypto_err("could not SSL_CTX_new"); 230 free(rc); 231 return NULL; 232 } 233 if(!listen_sslctx_setup(rc->ctx)) { 234 daemon_remote_delete(rc); 235 return NULL; 236 } 237 238 if (cfg->remote_control_use_cert == 0) { 239 /* No certificates are requested */ 240 #if defined(SSL_OP_NO_TLSv1_3) 241 /* in openssl 1.1.1, negotiation code for tls 1.3 does 242 * not allow the unauthenticated aNULL and eNULL ciphers */ 243 SSL_CTX_set_options(rc->ctx, SSL_OP_NO_TLSv1_3); 244 #endif 245 #ifdef HAVE_SSL_CTX_SET_SECURITY_LEVEL 246 SSL_CTX_set_security_level(rc->ctx, 0); 247 #endif 248 if(!SSL_CTX_set_cipher_list(rc->ctx, "aNULL:eNULL")) { 249 log_crypto_err("Failed to set aNULL cipher list"); 250 daemon_remote_delete(rc); 251 return NULL; 252 } 253 254 /* in openssl 1.1, the securitylevel 0 allows eNULL, that 255 * does not need the DH */ 256 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL) 257 /* Since we have no certificates and hence no source of 258 * DH params, let's generate and set them 259 */ 260 if(!SSL_CTX_set_tmp_dh(rc->ctx,get_dh2048())) { 261 log_crypto_err("Wanted to set DH param, but failed"); 262 daemon_remote_delete(rc); 263 return NULL; 264 } 265 #endif 266 return rc; 267 } 268 rc->use_cert = 1; 269 s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1); 270 s_key = fname_after_chroot(cfg->server_key_file, cfg, 1); 271 if(!s_cert || !s_key) { 272 log_err("out of memory in remote control fname"); 273 goto setup_error; 274 } 275 verbose(VERB_ALGO, "setup SSL certificates"); 276 if (!SSL_CTX_use_certificate_chain_file(rc->ctx,s_cert)) { 277 log_err("Error for server-cert-file: %s", s_cert); 278 log_crypto_err("Error in SSL_CTX use_certificate_chain_file"); 279 goto setup_error; 280 } 281 if(!SSL_CTX_use_PrivateKey_file(rc->ctx,s_key,SSL_FILETYPE_PEM)) { 282 log_err("Error for server-key-file: %s", s_key); 283 log_crypto_err("Error in SSL_CTX use_PrivateKey_file"); 284 goto setup_error; 285 } 286 if(!SSL_CTX_check_private_key(rc->ctx)) { 287 log_err("Error for server-key-file: %s", s_key); 288 log_crypto_err("Error in SSL_CTX check_private_key"); 289 goto setup_error; 290 } 291 listen_sslctx_setup_2(rc->ctx); 292 if(!SSL_CTX_load_verify_locations(rc->ctx, s_cert, NULL)) { 293 log_crypto_err("Error setting up SSL_CTX verify locations"); 294 setup_error: 295 free(s_cert); 296 free(s_key); 297 daemon_remote_delete(rc); 298 return NULL; 299 } 300 SSL_CTX_set_client_CA_list(rc->ctx, SSL_load_client_CA_file(s_cert)); 301 SSL_CTX_set_verify(rc->ctx, SSL_VERIFY_PEER, NULL); 302 free(s_cert); 303 free(s_key); 304 305 return rc; 306 } 307 308 void daemon_remote_clear(struct daemon_remote* rc) 309 { 310 struct rc_state* p, *np; 311 if(!rc) return; 312 /* but do not close the ports */ 313 listen_list_delete(rc->accept_list); 314 rc->accept_list = NULL; 315 /* do close these sockets */ 316 p = rc->busy_list; 317 while(p) { 318 np = p->next; 319 if(p->ssl) 320 SSL_free(p->ssl); 321 comm_point_delete(p->c); 322 free(p); 323 p = np; 324 } 325 rc->busy_list = NULL; 326 rc->active = 0; 327 rc->worker = NULL; 328 } 329 330 void daemon_remote_delete(struct daemon_remote* rc) 331 { 332 if(!rc) return; 333 daemon_remote_clear(rc); 334 if(rc->ctx) { 335 SSL_CTX_free(rc->ctx); 336 } 337 free(rc); 338 } 339 340 /** 341 * Add and open a new control port 342 * @param ip: ip str 343 * @param nr: port nr 344 * @param list: list head 345 * @param noproto_is_err: if lack of protocol support is an error. 346 * @param cfg: config with username for chown of unix-sockets. 347 * @return false on failure. 348 */ 349 static int 350 add_open(const char* ip, int nr, struct listen_port** list, int noproto_is_err, 351 struct config_file* cfg) 352 { 353 struct addrinfo hints; 354 struct addrinfo* res; 355 struct listen_port* n; 356 int noproto; 357 int fd, r; 358 char port[15]; 359 snprintf(port, sizeof(port), "%d", nr); 360 port[sizeof(port)-1]=0; 361 memset(&hints, 0, sizeof(hints)); 362 363 if(ip[0] == '/') { 364 /* This looks like a local socket */ 365 fd = create_local_accept_sock(ip, &noproto, cfg->use_systemd); 366 /* 367 * Change socket ownership and permissions so users other 368 * than root can access it provided they are in the same 369 * group as the user we run as. 370 */ 371 if(fd != -1) { 372 #ifdef HAVE_CHOWN 373 if (cfg->username && cfg->username[0] && 374 cfg_uid != (uid_t)-1) { 375 if(chown(ip, cfg_uid, cfg_gid) == -1) 376 verbose(VERB_QUERY, "cannot chown %u.%u %s: %s", 377 (unsigned)cfg_uid, (unsigned)cfg_gid, 378 ip, strerror(errno)); 379 } 380 chmod(ip, (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)); 381 #else 382 (void)cfg; 383 #endif 384 } 385 } else { 386 hints.ai_socktype = SOCK_STREAM; 387 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 388 if((r = getaddrinfo(ip, port, &hints, &res)) != 0 || !res) { 389 #ifdef USE_WINSOCK 390 if(!noproto_is_err && r == EAI_NONAME) { 391 /* tried to lookup the address as name */ 392 return 1; /* return success, but do nothing */ 393 } 394 #endif /* USE_WINSOCK */ 395 log_err("control interface %s:%s getaddrinfo: %s %s", 396 ip?ip:"default", port, gai_strerror(r), 397 #ifdef EAI_SYSTEM 398 r==EAI_SYSTEM?(char*)strerror(errno):"" 399 #else 400 "" 401 #endif 402 ); 403 return 0; 404 } 405 406 /* open fd */ 407 fd = create_tcp_accept_sock(res, 1, &noproto, 0, 408 cfg->ip_transparent, 0, cfg->ip_freebind, cfg->use_systemd); 409 freeaddrinfo(res); 410 } 411 412 if(fd == -1 && noproto) { 413 if(!noproto_is_err) 414 return 1; /* return success, but do nothing */ 415 log_err("cannot open control interface %s %d : " 416 "protocol not supported", ip, nr); 417 return 0; 418 } 419 if(fd == -1) { 420 log_err("cannot open control interface %s %d", ip, nr); 421 return 0; 422 } 423 424 /* alloc */ 425 n = (struct listen_port*)calloc(1, sizeof(*n)); 426 if(!n) { 427 #ifndef USE_WINSOCK 428 close(fd); 429 #else 430 closesocket(fd); 431 #endif 432 log_err("out of memory"); 433 return 0; 434 } 435 n->next = *list; 436 *list = n; 437 n->fd = fd; 438 return 1; 439 } 440 441 struct listen_port* daemon_remote_open_ports(struct config_file* cfg) 442 { 443 struct listen_port* l = NULL; 444 log_assert(cfg->remote_control_enable && cfg->control_port); 445 if(cfg->control_ifs) { 446 struct config_strlist* p; 447 for(p = cfg->control_ifs; p; p = p->next) { 448 if(!add_open(p->str, cfg->control_port, &l, 1, cfg)) { 449 listening_ports_free(l); 450 return NULL; 451 } 452 } 453 } else { 454 /* defaults */ 455 if(cfg->do_ip6 && 456 !add_open("::1", cfg->control_port, &l, 0, cfg)) { 457 listening_ports_free(l); 458 return NULL; 459 } 460 if(cfg->do_ip4 && 461 !add_open("127.0.0.1", cfg->control_port, &l, 1, cfg)) { 462 listening_ports_free(l); 463 return NULL; 464 } 465 } 466 return l; 467 } 468 469 /** open accept commpoint */ 470 static int 471 accept_open(struct daemon_remote* rc, int fd) 472 { 473 struct listen_list* n = (struct listen_list*)malloc(sizeof(*n)); 474 if(!n) { 475 log_err("out of memory"); 476 return 0; 477 } 478 n->next = rc->accept_list; 479 rc->accept_list = n; 480 /* open commpt */ 481 n->com = comm_point_create_raw(rc->worker->base, fd, 0, 482 &remote_accept_callback, rc); 483 if(!n->com) 484 return 0; 485 /* keep this port open, its fd is kept in the rc portlist */ 486 n->com->do_not_close = 1; 487 return 1; 488 } 489 490 int daemon_remote_open_accept(struct daemon_remote* rc, 491 struct listen_port* ports, struct worker* worker) 492 { 493 struct listen_port* p; 494 rc->worker = worker; 495 for(p = ports; p; p = p->next) { 496 if(!accept_open(rc, p->fd)) { 497 log_err("could not create accept comm point"); 498 return 0; 499 } 500 } 501 return 1; 502 } 503 504 void daemon_remote_stop_accept(struct daemon_remote* rc) 505 { 506 struct listen_list* p; 507 for(p=rc->accept_list; p; p=p->next) { 508 comm_point_stop_listening(p->com); 509 } 510 } 511 512 void daemon_remote_start_accept(struct daemon_remote* rc) 513 { 514 struct listen_list* p; 515 for(p=rc->accept_list; p; p=p->next) { 516 comm_point_start_listening(p->com, -1, -1); 517 } 518 } 519 520 int remote_accept_callback(struct comm_point* c, void* arg, int err, 521 struct comm_reply* ATTR_UNUSED(rep)) 522 { 523 struct daemon_remote* rc = (struct daemon_remote*)arg; 524 struct sockaddr_storage addr; 525 socklen_t addrlen; 526 int newfd; 527 struct rc_state* n; 528 if(err != NETEVENT_NOERROR) { 529 log_err("error %d on remote_accept_callback", err); 530 return 0; 531 } 532 /* perform the accept */ 533 newfd = comm_point_perform_accept(c, &addr, &addrlen); 534 if(newfd == -1) 535 return 0; 536 /* create new commpoint unless we are servicing already */ 537 if(rc->active >= rc->max_active) { 538 log_warn("drop incoming remote control: too many connections"); 539 close_exit: 540 #ifndef USE_WINSOCK 541 close(newfd); 542 #else 543 closesocket(newfd); 544 #endif 545 return 0; 546 } 547 548 /* setup commpoint to service the remote control command */ 549 n = (struct rc_state*)calloc(1, sizeof(*n)); 550 if(!n) { 551 log_err("out of memory"); 552 goto close_exit; 553 } 554 /* start in reading state */ 555 n->c = comm_point_create_raw(rc->worker->base, newfd, 0, 556 &remote_control_callback, n); 557 if(!n->c) { 558 log_err("out of memory"); 559 free(n); 560 goto close_exit; 561 } 562 log_addr(VERB_QUERY, "new control connection from", &addr, addrlen); 563 n->c->do_not_close = 0; 564 comm_point_stop_listening(n->c); 565 comm_point_start_listening(n->c, -1, REMOTE_CONTROL_TCP_TIMEOUT); 566 memcpy(&n->c->repinfo.addr, &addr, addrlen); 567 n->c->repinfo.addrlen = addrlen; 568 n->shake_state = rc_hs_read; 569 n->ssl = SSL_new(rc->ctx); 570 if(!n->ssl) { 571 log_crypto_err("could not SSL_new"); 572 comm_point_delete(n->c); 573 free(n); 574 goto close_exit; 575 } 576 SSL_set_accept_state(n->ssl); 577 (void)SSL_set_mode(n->ssl, SSL_MODE_AUTO_RETRY); 578 if(!SSL_set_fd(n->ssl, newfd)) { 579 log_crypto_err("could not SSL_set_fd"); 580 SSL_free(n->ssl); 581 comm_point_delete(n->c); 582 free(n); 583 goto close_exit; 584 } 585 586 n->rc = rc; 587 n->next = rc->busy_list; 588 rc->busy_list = n; 589 rc->active ++; 590 591 /* perform the first nonblocking read already, for windows, 592 * so it can return wouldblock. could be faster too. */ 593 (void)remote_control_callback(n->c, n, NETEVENT_NOERROR, NULL); 594 return 0; 595 } 596 597 /** delete from list */ 598 static void 599 state_list_remove_elem(struct rc_state** list, struct comm_point* c) 600 { 601 while(*list) { 602 if( (*list)->c == c) { 603 *list = (*list)->next; 604 return; 605 } 606 list = &(*list)->next; 607 } 608 } 609 610 /** decrease active count and remove commpoint from busy list */ 611 static void 612 clean_point(struct daemon_remote* rc, struct rc_state* s) 613 { 614 state_list_remove_elem(&rc->busy_list, s->c); 615 rc->active --; 616 if(s->ssl) { 617 SSL_shutdown(s->ssl); 618 SSL_free(s->ssl); 619 } 620 comm_point_delete(s->c); 621 free(s); 622 } 623 624 int 625 ssl_print_text(SSL* ssl, const char* text) 626 { 627 int r; 628 if(!ssl) 629 return 0; 630 ERR_clear_error(); 631 if((r=SSL_write(ssl, text, (int)strlen(text))) <= 0) { 632 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { 633 verbose(VERB_QUERY, "warning, in SSL_write, peer " 634 "closed connection"); 635 return 0; 636 } 637 log_crypto_err("could not SSL_write"); 638 return 0; 639 } 640 return 1; 641 } 642 643 /** print text over the ssl connection */ 644 static int 645 ssl_print_vmsg(SSL* ssl, const char* format, va_list args) 646 { 647 char msg[1024]; 648 vsnprintf(msg, sizeof(msg), format, args); 649 return ssl_print_text(ssl, msg); 650 } 651 652 /** printf style printing to the ssl connection */ 653 int ssl_printf(SSL* ssl, const char* format, ...) 654 { 655 va_list args; 656 int ret; 657 va_start(args, format); 658 ret = ssl_print_vmsg(ssl, format, args); 659 va_end(args); 660 return ret; 661 } 662 663 int 664 ssl_read_line(SSL* ssl, char* buf, size_t max) 665 { 666 int r; 667 size_t len = 0; 668 if(!ssl) 669 return 0; 670 while(len < max) { 671 ERR_clear_error(); 672 if((r=SSL_read(ssl, buf+len, 1)) <= 0) { 673 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { 674 buf[len] = 0; 675 return 1; 676 } 677 log_crypto_err("could not SSL_read"); 678 return 0; 679 } 680 if(buf[len] == '\n') { 681 /* return string without \n */ 682 buf[len] = 0; 683 return 1; 684 } 685 len++; 686 } 687 buf[max-1] = 0; 688 log_err("control line too long (%d): %s", (int)max, buf); 689 return 0; 690 } 691 692 /** skip whitespace, return new pointer into string */ 693 static char* 694 skipwhite(char* str) 695 { 696 /* EOS \0 is not a space */ 697 while( isspace((unsigned char)*str) ) 698 str++; 699 return str; 700 } 701 702 /** send the OK to the control client */ 703 static void send_ok(SSL* ssl) 704 { 705 (void)ssl_printf(ssl, "ok\n"); 706 } 707 708 /** do the stop command */ 709 static void 710 do_stop(SSL* ssl, struct daemon_remote* rc) 711 { 712 rc->worker->need_to_exit = 1; 713 comm_base_exit(rc->worker->base); 714 send_ok(ssl); 715 } 716 717 /** do the reload command */ 718 static void 719 do_reload(SSL* ssl, struct daemon_remote* rc) 720 { 721 rc->worker->need_to_exit = 0; 722 comm_base_exit(rc->worker->base); 723 send_ok(ssl); 724 } 725 726 /** do the verbosity command */ 727 static void 728 do_verbosity(SSL* ssl, char* str) 729 { 730 int val = atoi(str); 731 if(val == 0 && strcmp(str, "0") != 0) { 732 ssl_printf(ssl, "error in verbosity number syntax: %s\n", str); 733 return; 734 } 735 verbosity = val; 736 send_ok(ssl); 737 } 738 739 /** print stats from statinfo */ 740 static int 741 print_stats(SSL* ssl, const char* nm, struct ub_stats_info* s) 742 { 743 struct timeval sumwait, avg; 744 if(!ssl_printf(ssl, "%s.num.queries"SQ"%lu\n", nm, 745 (unsigned long)s->svr.num_queries)) return 0; 746 if(!ssl_printf(ssl, "%s.num.queries_ip_ratelimited"SQ"%lu\n", nm, 747 (unsigned long)s->svr.num_queries_ip_ratelimited)) return 0; 748 if(!ssl_printf(ssl, "%s.num.cachehits"SQ"%lu\n", nm, 749 (unsigned long)(s->svr.num_queries 750 - s->svr.num_queries_missed_cache))) return 0; 751 if(!ssl_printf(ssl, "%s.num.cachemiss"SQ"%lu\n", nm, 752 (unsigned long)s->svr.num_queries_missed_cache)) return 0; 753 if(!ssl_printf(ssl, "%s.num.prefetch"SQ"%lu\n", nm, 754 (unsigned long)s->svr.num_queries_prefetch)) return 0; 755 if(!ssl_printf(ssl, "%s.num.zero_ttl"SQ"%lu\n", nm, 756 (unsigned long)s->svr.zero_ttl_responses)) return 0; 757 if(!ssl_printf(ssl, "%s.num.recursivereplies"SQ"%lu\n", nm, 758 (unsigned long)s->mesh_replies_sent)) return 0; 759 #ifdef USE_DNSCRYPT 760 if(!ssl_printf(ssl, "%s.num.dnscrypt.crypted"SQ"%lu\n", nm, 761 (unsigned long)s->svr.num_query_dnscrypt_crypted)) return 0; 762 if(!ssl_printf(ssl, "%s.num.dnscrypt.cert"SQ"%lu\n", nm, 763 (unsigned long)s->svr.num_query_dnscrypt_cert)) return 0; 764 if(!ssl_printf(ssl, "%s.num.dnscrypt.cleartext"SQ"%lu\n", nm, 765 (unsigned long)s->svr.num_query_dnscrypt_cleartext)) return 0; 766 if(!ssl_printf(ssl, "%s.num.dnscrypt.malformed"SQ"%lu\n", nm, 767 (unsigned long)s->svr.num_query_dnscrypt_crypted_malformed)) return 0; 768 #endif 769 if(!ssl_printf(ssl, "%s.requestlist.avg"SQ"%g\n", nm, 770 (s->svr.num_queries_missed_cache+s->svr.num_queries_prefetch)? 771 (double)s->svr.sum_query_list_size/ 772 (double)(s->svr.num_queries_missed_cache+ 773 s->svr.num_queries_prefetch) : 0.0)) return 0; 774 if(!ssl_printf(ssl, "%s.requestlist.max"SQ"%lu\n", nm, 775 (unsigned long)s->svr.max_query_list_size)) return 0; 776 if(!ssl_printf(ssl, "%s.requestlist.overwritten"SQ"%lu\n", nm, 777 (unsigned long)s->mesh_jostled)) return 0; 778 if(!ssl_printf(ssl, "%s.requestlist.exceeded"SQ"%lu\n", nm, 779 (unsigned long)s->mesh_dropped)) return 0; 780 if(!ssl_printf(ssl, "%s.requestlist.current.all"SQ"%lu\n", nm, 781 (unsigned long)s->mesh_num_states)) return 0; 782 if(!ssl_printf(ssl, "%s.requestlist.current.user"SQ"%lu\n", nm, 783 (unsigned long)s->mesh_num_reply_states)) return 0; 784 #ifndef S_SPLINT_S 785 sumwait.tv_sec = s->mesh_replies_sum_wait_sec; 786 sumwait.tv_usec = s->mesh_replies_sum_wait_usec; 787 #endif 788 timeval_divide(&avg, &sumwait, s->mesh_replies_sent); 789 if(!ssl_printf(ssl, "%s.recursion.time.avg"SQ ARG_LL "d.%6.6d\n", nm, 790 (long long)avg.tv_sec, (int)avg.tv_usec)) return 0; 791 if(!ssl_printf(ssl, "%s.recursion.time.median"SQ"%g\n", nm, 792 s->mesh_time_median)) return 0; 793 if(!ssl_printf(ssl, "%s.tcpusage"SQ"%lu\n", nm, 794 (unsigned long)s->svr.tcp_accept_usage)) return 0; 795 return 1; 796 } 797 798 /** print stats for one thread */ 799 static int 800 print_thread_stats(SSL* ssl, int i, struct ub_stats_info* s) 801 { 802 char nm[32]; 803 snprintf(nm, sizeof(nm), "thread%d", i); 804 nm[sizeof(nm)-1]=0; 805 return print_stats(ssl, nm, s); 806 } 807 808 /** print long number */ 809 static int 810 print_longnum(SSL* ssl, const char* desc, size_t x) 811 { 812 if(x > 1024*1024*1024) { 813 /* more than a Gb */ 814 size_t front = x / (size_t)1000000; 815 size_t back = x % (size_t)1000000; 816 return ssl_printf(ssl, "%s%u%6.6u\n", desc, 817 (unsigned)front, (unsigned)back); 818 } else { 819 return ssl_printf(ssl, "%s%lu\n", desc, (unsigned long)x); 820 } 821 } 822 823 /** print mem stats */ 824 static int 825 print_mem(SSL* ssl, struct worker* worker, struct daemon* daemon) 826 { 827 size_t msg, rrset, val, iter, respip; 828 #ifdef CLIENT_SUBNET 829 size_t subnet = 0; 830 #endif /* CLIENT_SUBNET */ 831 #ifdef USE_IPSECMOD 832 size_t ipsecmod = 0; 833 #endif /* USE_IPSECMOD */ 834 #ifdef USE_DNSCRYPT 835 size_t dnscrypt_shared_secret = 0; 836 size_t dnscrypt_nonce = 0; 837 #endif /* USE_DNSCRYPT */ 838 msg = slabhash_get_mem(daemon->env->msg_cache); 839 rrset = slabhash_get_mem(&daemon->env->rrset_cache->table); 840 val = mod_get_mem(&worker->env, "validator"); 841 iter = mod_get_mem(&worker->env, "iterator"); 842 respip = mod_get_mem(&worker->env, "respip"); 843 #ifdef CLIENT_SUBNET 844 subnet = mod_get_mem(&worker->env, "subnet"); 845 #endif /* CLIENT_SUBNET */ 846 #ifdef USE_IPSECMOD 847 ipsecmod = mod_get_mem(&worker->env, "ipsecmod"); 848 #endif /* USE_IPSECMOD */ 849 #ifdef USE_DNSCRYPT 850 if(daemon->dnscenv) { 851 dnscrypt_shared_secret = slabhash_get_mem( 852 daemon->dnscenv->shared_secrets_cache); 853 dnscrypt_nonce = slabhash_get_mem(daemon->dnscenv->nonces_cache); 854 } 855 #endif /* USE_DNSCRYPT */ 856 857 if(!print_longnum(ssl, "mem.cache.rrset"SQ, rrset)) 858 return 0; 859 if(!print_longnum(ssl, "mem.cache.message"SQ, msg)) 860 return 0; 861 if(!print_longnum(ssl, "mem.mod.iterator"SQ, iter)) 862 return 0; 863 if(!print_longnum(ssl, "mem.mod.validator"SQ, val)) 864 return 0; 865 if(!print_longnum(ssl, "mem.mod.respip"SQ, respip)) 866 return 0; 867 #ifdef CLIENT_SUBNET 868 if(!print_longnum(ssl, "mem.mod.subnet"SQ, subnet)) 869 return 0; 870 #endif /* CLIENT_SUBNET */ 871 #ifdef USE_IPSECMOD 872 if(!print_longnum(ssl, "mem.mod.ipsecmod"SQ, ipsecmod)) 873 return 0; 874 #endif /* USE_IPSECMOD */ 875 #ifdef USE_DNSCRYPT 876 if(!print_longnum(ssl, "mem.cache.dnscrypt_shared_secret"SQ, 877 dnscrypt_shared_secret)) 878 return 0; 879 if(!print_longnum(ssl, "mem.cache.dnscrypt_nonce"SQ, 880 dnscrypt_nonce)) 881 return 0; 882 #endif /* USE_DNSCRYPT */ 883 return 1; 884 } 885 886 /** print uptime stats */ 887 static int 888 print_uptime(SSL* ssl, struct worker* worker, int reset) 889 { 890 struct timeval now = *worker->env.now_tv; 891 struct timeval up, dt; 892 timeval_subtract(&up, &now, &worker->daemon->time_boot); 893 timeval_subtract(&dt, &now, &worker->daemon->time_last_stat); 894 if(reset) 895 worker->daemon->time_last_stat = now; 896 if(!ssl_printf(ssl, "time.now"SQ ARG_LL "d.%6.6d\n", 897 (long long)now.tv_sec, (unsigned)now.tv_usec)) return 0; 898 if(!ssl_printf(ssl, "time.up"SQ ARG_LL "d.%6.6d\n", 899 (long long)up.tv_sec, (unsigned)up.tv_usec)) return 0; 900 if(!ssl_printf(ssl, "time.elapsed"SQ ARG_LL "d.%6.6d\n", 901 (long long)dt.tv_sec, (unsigned)dt.tv_usec)) return 0; 902 return 1; 903 } 904 905 /** print extended histogram */ 906 static int 907 print_hist(SSL* ssl, struct ub_stats_info* s) 908 { 909 struct timehist* hist; 910 size_t i; 911 hist = timehist_setup(); 912 if(!hist) { 913 log_err("out of memory"); 914 return 0; 915 } 916 timehist_import(hist, s->svr.hist, NUM_BUCKETS_HIST); 917 for(i=0; i<hist->num; i++) { 918 if(!ssl_printf(ssl, 919 "histogram.%6.6d.%6.6d.to.%6.6d.%6.6d=%lu\n", 920 (int)hist->buckets[i].lower.tv_sec, 921 (int)hist->buckets[i].lower.tv_usec, 922 (int)hist->buckets[i].upper.tv_sec, 923 (int)hist->buckets[i].upper.tv_usec, 924 (unsigned long)hist->buckets[i].count)) { 925 timehist_delete(hist); 926 return 0; 927 } 928 } 929 timehist_delete(hist); 930 return 1; 931 } 932 933 /** print extended stats */ 934 static int 935 print_ext(SSL* ssl, struct ub_stats_info* s) 936 { 937 int i; 938 char nm[16]; 939 const sldns_rr_descriptor* desc; 940 const sldns_lookup_table* lt; 941 /* TYPE */ 942 for(i=0; i<UB_STATS_QTYPE_NUM; i++) { 943 if(inhibit_zero && s->svr.qtype[i] == 0) 944 continue; 945 desc = sldns_rr_descript((uint16_t)i); 946 if(desc && desc->_name) { 947 snprintf(nm, sizeof(nm), "%s", desc->_name); 948 } else if (i == LDNS_RR_TYPE_IXFR) { 949 snprintf(nm, sizeof(nm), "IXFR"); 950 } else if (i == LDNS_RR_TYPE_AXFR) { 951 snprintf(nm, sizeof(nm), "AXFR"); 952 } else if (i == LDNS_RR_TYPE_MAILA) { 953 snprintf(nm, sizeof(nm), "MAILA"); 954 } else if (i == LDNS_RR_TYPE_MAILB) { 955 snprintf(nm, sizeof(nm), "MAILB"); 956 } else if (i == LDNS_RR_TYPE_ANY) { 957 snprintf(nm, sizeof(nm), "ANY"); 958 } else { 959 snprintf(nm, sizeof(nm), "TYPE%d", i); 960 } 961 if(!ssl_printf(ssl, "num.query.type.%s"SQ"%lu\n", 962 nm, (unsigned long)s->svr.qtype[i])) return 0; 963 } 964 if(!inhibit_zero || s->svr.qtype_big) { 965 if(!ssl_printf(ssl, "num.query.type.other"SQ"%lu\n", 966 (unsigned long)s->svr.qtype_big)) return 0; 967 } 968 /* CLASS */ 969 for(i=0; i<UB_STATS_QCLASS_NUM; i++) { 970 if(inhibit_zero && s->svr.qclass[i] == 0) 971 continue; 972 lt = sldns_lookup_by_id(sldns_rr_classes, i); 973 if(lt && lt->name) { 974 snprintf(nm, sizeof(nm), "%s", lt->name); 975 } else { 976 snprintf(nm, sizeof(nm), "CLASS%d", i); 977 } 978 if(!ssl_printf(ssl, "num.query.class.%s"SQ"%lu\n", 979 nm, (unsigned long)s->svr.qclass[i])) return 0; 980 } 981 if(!inhibit_zero || s->svr.qclass_big) { 982 if(!ssl_printf(ssl, "num.query.class.other"SQ"%lu\n", 983 (unsigned long)s->svr.qclass_big)) return 0; 984 } 985 /* OPCODE */ 986 for(i=0; i<UB_STATS_OPCODE_NUM; i++) { 987 if(inhibit_zero && s->svr.qopcode[i] == 0) 988 continue; 989 lt = sldns_lookup_by_id(sldns_opcodes, i); 990 if(lt && lt->name) { 991 snprintf(nm, sizeof(nm), "%s", lt->name); 992 } else { 993 snprintf(nm, sizeof(nm), "OPCODE%d", i); 994 } 995 if(!ssl_printf(ssl, "num.query.opcode.%s"SQ"%lu\n", 996 nm, (unsigned long)s->svr.qopcode[i])) return 0; 997 } 998 /* transport */ 999 if(!ssl_printf(ssl, "num.query.tcp"SQ"%lu\n", 1000 (unsigned long)s->svr.qtcp)) return 0; 1001 if(!ssl_printf(ssl, "num.query.tcpout"SQ"%lu\n", 1002 (unsigned long)s->svr.qtcp_outgoing)) return 0; 1003 if(!ssl_printf(ssl, "num.query.ipv6"SQ"%lu\n", 1004 (unsigned long)s->svr.qipv6)) return 0; 1005 /* flags */ 1006 if(!ssl_printf(ssl, "num.query.flags.QR"SQ"%lu\n", 1007 (unsigned long)s->svr.qbit_QR)) return 0; 1008 if(!ssl_printf(ssl, "num.query.flags.AA"SQ"%lu\n", 1009 (unsigned long)s->svr.qbit_AA)) return 0; 1010 if(!ssl_printf(ssl, "num.query.flags.TC"SQ"%lu\n", 1011 (unsigned long)s->svr.qbit_TC)) return 0; 1012 if(!ssl_printf(ssl, "num.query.flags.RD"SQ"%lu\n", 1013 (unsigned long)s->svr.qbit_RD)) return 0; 1014 if(!ssl_printf(ssl, "num.query.flags.RA"SQ"%lu\n", 1015 (unsigned long)s->svr.qbit_RA)) return 0; 1016 if(!ssl_printf(ssl, "num.query.flags.Z"SQ"%lu\n", 1017 (unsigned long)s->svr.qbit_Z)) return 0; 1018 if(!ssl_printf(ssl, "num.query.flags.AD"SQ"%lu\n", 1019 (unsigned long)s->svr.qbit_AD)) return 0; 1020 if(!ssl_printf(ssl, "num.query.flags.CD"SQ"%lu\n", 1021 (unsigned long)s->svr.qbit_CD)) return 0; 1022 if(!ssl_printf(ssl, "num.query.edns.present"SQ"%lu\n", 1023 (unsigned long)s->svr.qEDNS)) return 0; 1024 if(!ssl_printf(ssl, "num.query.edns.DO"SQ"%lu\n", 1025 (unsigned long)s->svr.qEDNS_DO)) return 0; 1026 1027 /* RCODE */ 1028 for(i=0; i<UB_STATS_RCODE_NUM; i++) { 1029 /* Always include RCODEs 0-5 */ 1030 if(inhibit_zero && i > LDNS_RCODE_REFUSED && s->svr.ans_rcode[i] == 0) 1031 continue; 1032 lt = sldns_lookup_by_id(sldns_rcodes, i); 1033 if(lt && lt->name) { 1034 snprintf(nm, sizeof(nm), "%s", lt->name); 1035 } else { 1036 snprintf(nm, sizeof(nm), "RCODE%d", i); 1037 } 1038 if(!ssl_printf(ssl, "num.answer.rcode.%s"SQ"%lu\n", 1039 nm, (unsigned long)s->svr.ans_rcode[i])) return 0; 1040 } 1041 if(!inhibit_zero || s->svr.ans_rcode_nodata) { 1042 if(!ssl_printf(ssl, "num.answer.rcode.nodata"SQ"%lu\n", 1043 (unsigned long)s->svr.ans_rcode_nodata)) return 0; 1044 } 1045 /* iteration */ 1046 if(!ssl_printf(ssl, "num.query.ratelimited"SQ"%lu\n", 1047 (unsigned long)s->svr.queries_ratelimited)) return 0; 1048 /* validation */ 1049 if(!ssl_printf(ssl, "num.answer.secure"SQ"%lu\n", 1050 (unsigned long)s->svr.ans_secure)) return 0; 1051 if(!ssl_printf(ssl, "num.answer.bogus"SQ"%lu\n", 1052 (unsigned long)s->svr.ans_bogus)) return 0; 1053 if(!ssl_printf(ssl, "num.rrset.bogus"SQ"%lu\n", 1054 (unsigned long)s->svr.rrset_bogus)) return 0; 1055 if(!ssl_printf(ssl, "num.query.aggressive.NOERROR"SQ"%lu\n", 1056 (unsigned long)s->svr.num_neg_cache_noerror)) return 0; 1057 if(!ssl_printf(ssl, "num.query.aggressive.NXDOMAIN"SQ"%lu\n", 1058 (unsigned long)s->svr.num_neg_cache_nxdomain)) return 0; 1059 /* threat detection */ 1060 if(!ssl_printf(ssl, "unwanted.queries"SQ"%lu\n", 1061 (unsigned long)s->svr.unwanted_queries)) return 0; 1062 if(!ssl_printf(ssl, "unwanted.replies"SQ"%lu\n", 1063 (unsigned long)s->svr.unwanted_replies)) return 0; 1064 /* cache counts */ 1065 if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n", 1066 (unsigned)s->svr.msg_cache_count)) return 0; 1067 if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n", 1068 (unsigned)s->svr.rrset_cache_count)) return 0; 1069 if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n", 1070 (unsigned)s->svr.infra_cache_count)) return 0; 1071 if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n", 1072 (unsigned)s->svr.key_cache_count)) return 0; 1073 #ifdef USE_DNSCRYPT 1074 if(!ssl_printf(ssl, "dnscrypt_shared_secret.cache.count"SQ"%u\n", 1075 (unsigned)s->svr.shared_secret_cache_count)) return 0; 1076 if(!ssl_printf(ssl, "dnscrypt_nonce.cache.count"SQ"%u\n", 1077 (unsigned)s->svr.nonce_cache_count)) return 0; 1078 if(!ssl_printf(ssl, "num.query.dnscrypt.shared_secret.cachemiss"SQ"%lu\n", 1079 (unsigned long)s->svr.num_query_dnscrypt_secret_missed_cache)) return 0; 1080 if(!ssl_printf(ssl, "num.query.dnscrypt.replay"SQ"%lu\n", 1081 (unsigned long)s->svr.num_query_dnscrypt_replay)) return 0; 1082 #endif /* USE_DNSCRYPT */ 1083 if(!ssl_printf(ssl, "num.query.authzone.up"SQ"%lu\n", 1084 (unsigned long)s->svr.num_query_authzone_up)) return 0; 1085 if(!ssl_printf(ssl, "num.query.authzone.down"SQ"%lu\n", 1086 (unsigned long)s->svr.num_query_authzone_down)) return 0; 1087 return 1; 1088 } 1089 1090 /** do the stats command */ 1091 static void 1092 do_stats(SSL* ssl, struct daemon_remote* rc, int reset) 1093 { 1094 struct daemon* daemon = rc->worker->daemon; 1095 struct ub_stats_info total; 1096 struct ub_stats_info s; 1097 int i; 1098 log_assert(daemon->num > 0); 1099 /* gather all thread statistics in one place */ 1100 for(i=0; i<daemon->num; i++) { 1101 server_stats_obtain(rc->worker, daemon->workers[i], &s, reset); 1102 if(!print_thread_stats(ssl, i, &s)) 1103 return; 1104 if(i == 0) 1105 total = s; 1106 else server_stats_add(&total, &s); 1107 } 1108 /* print the thread statistics */ 1109 total.mesh_time_median /= (double)daemon->num; 1110 if(!print_stats(ssl, "total", &total)) 1111 return; 1112 if(!print_uptime(ssl, rc->worker, reset)) 1113 return; 1114 if(daemon->cfg->stat_extended) { 1115 if(!print_mem(ssl, rc->worker, daemon)) 1116 return; 1117 if(!print_hist(ssl, &total)) 1118 return; 1119 if(!print_ext(ssl, &total)) 1120 return; 1121 } 1122 } 1123 1124 /** parse commandline argument domain name */ 1125 static int 1126 parse_arg_name(SSL* ssl, char* str, uint8_t** res, size_t* len, int* labs) 1127 { 1128 uint8_t nm[LDNS_MAX_DOMAINLEN+1]; 1129 size_t nmlen = sizeof(nm); 1130 int status; 1131 *res = NULL; 1132 *len = 0; 1133 *labs = 0; 1134 status = sldns_str2wire_dname_buf(str, nm, &nmlen); 1135 if(status != 0) { 1136 ssl_printf(ssl, "error cannot parse name %s at %d: %s\n", str, 1137 LDNS_WIREPARSE_OFFSET(status), 1138 sldns_get_errorstr_parse(status)); 1139 return 0; 1140 } 1141 *res = memdup(nm, nmlen); 1142 if(!*res) { 1143 ssl_printf(ssl, "error out of memory\n"); 1144 return 0; 1145 } 1146 *labs = dname_count_size_labels(*res, len); 1147 return 1; 1148 } 1149 1150 /** find second argument, modifies string */ 1151 static int 1152 find_arg2(SSL* ssl, char* arg, char** arg2) 1153 { 1154 char* as = strchr(arg, ' '); 1155 char* at = strchr(arg, '\t'); 1156 if(as && at) { 1157 if(at < as) 1158 as = at; 1159 as[0]=0; 1160 *arg2 = skipwhite(as+1); 1161 } else if(as) { 1162 as[0]=0; 1163 *arg2 = skipwhite(as+1); 1164 } else if(at) { 1165 at[0]=0; 1166 *arg2 = skipwhite(at+1); 1167 } else { 1168 ssl_printf(ssl, "error could not find next argument " 1169 "after %s\n", arg); 1170 return 0; 1171 } 1172 return 1; 1173 } 1174 1175 /** Add a new zone */ 1176 static int 1177 perform_zone_add(SSL* ssl, struct local_zones* zones, char* arg) 1178 { 1179 uint8_t* nm; 1180 int nmlabs; 1181 size_t nmlen; 1182 char* arg2; 1183 enum localzone_type t; 1184 struct local_zone* z; 1185 if(!find_arg2(ssl, arg, &arg2)) 1186 return 0; 1187 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1188 return 0; 1189 if(!local_zone_str2type(arg2, &t)) { 1190 ssl_printf(ssl, "error not a zone type. %s\n", arg2); 1191 free(nm); 1192 return 0; 1193 } 1194 lock_rw_wrlock(&zones->lock); 1195 if((z=local_zones_find(zones, nm, nmlen, 1196 nmlabs, LDNS_RR_CLASS_IN))) { 1197 /* already present in tree */ 1198 lock_rw_wrlock(&z->lock); 1199 z->type = t; /* update type anyway */ 1200 lock_rw_unlock(&z->lock); 1201 free(nm); 1202 lock_rw_unlock(&zones->lock); 1203 return 1; 1204 } 1205 if(!local_zones_add_zone(zones, nm, nmlen, 1206 nmlabs, LDNS_RR_CLASS_IN, t)) { 1207 lock_rw_unlock(&zones->lock); 1208 ssl_printf(ssl, "error out of memory\n"); 1209 return 0; 1210 } 1211 lock_rw_unlock(&zones->lock); 1212 return 1; 1213 } 1214 1215 /** Do the local_zone command */ 1216 static void 1217 do_zone_add(SSL* ssl, struct local_zones* zones, char* arg) 1218 { 1219 if(!perform_zone_add(ssl, zones, arg)) 1220 return; 1221 send_ok(ssl); 1222 } 1223 1224 /** Do the local_zones command */ 1225 static void 1226 do_zones_add(SSL* ssl, struct local_zones* zones) 1227 { 1228 char buf[2048]; 1229 int num = 0; 1230 while(ssl_read_line(ssl, buf, sizeof(buf))) { 1231 if(buf[0] == 0x04 && buf[1] == 0) 1232 break; /* end of transmission */ 1233 if(!perform_zone_add(ssl, zones, buf)) { 1234 if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 1235 return; 1236 } 1237 else 1238 num++; 1239 } 1240 (void)ssl_printf(ssl, "added %d zones\n", num); 1241 } 1242 1243 /** Remove a zone */ 1244 static int 1245 perform_zone_remove(SSL* ssl, struct local_zones* zones, char* arg) 1246 { 1247 uint8_t* nm; 1248 int nmlabs; 1249 size_t nmlen; 1250 struct local_zone* z; 1251 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1252 return 0; 1253 lock_rw_wrlock(&zones->lock); 1254 if((z=local_zones_find(zones, nm, nmlen, 1255 nmlabs, LDNS_RR_CLASS_IN))) { 1256 /* present in tree */ 1257 local_zones_del_zone(zones, z); 1258 } 1259 lock_rw_unlock(&zones->lock); 1260 free(nm); 1261 return 1; 1262 } 1263 1264 /** Do the local_zone_remove command */ 1265 static void 1266 do_zone_remove(SSL* ssl, struct local_zones* zones, char* arg) 1267 { 1268 if(!perform_zone_remove(ssl, zones, arg)) 1269 return; 1270 send_ok(ssl); 1271 } 1272 1273 /** Do the local_zones_remove command */ 1274 static void 1275 do_zones_remove(SSL* ssl, struct local_zones* zones) 1276 { 1277 char buf[2048]; 1278 int num = 0; 1279 while(ssl_read_line(ssl, buf, sizeof(buf))) { 1280 if(buf[0] == 0x04 && buf[1] == 0) 1281 break; /* end of transmission */ 1282 if(!perform_zone_remove(ssl, zones, buf)) { 1283 if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 1284 return; 1285 } 1286 else 1287 num++; 1288 } 1289 (void)ssl_printf(ssl, "removed %d zones\n", num); 1290 } 1291 1292 /** Add new RR data */ 1293 static int 1294 perform_data_add(SSL* ssl, struct local_zones* zones, char* arg) 1295 { 1296 if(!local_zones_add_RR(zones, arg)) { 1297 ssl_printf(ssl,"error in syntax or out of memory, %s\n", arg); 1298 return 0; 1299 } 1300 return 1; 1301 } 1302 1303 /** Do the local_data command */ 1304 static void 1305 do_data_add(SSL* ssl, struct local_zones* zones, char* arg) 1306 { 1307 if(!perform_data_add(ssl, zones, arg)) 1308 return; 1309 send_ok(ssl); 1310 } 1311 1312 /** Do the local_datas command */ 1313 static void 1314 do_datas_add(SSL* ssl, struct local_zones* zones) 1315 { 1316 char buf[2048]; 1317 int num = 0; 1318 while(ssl_read_line(ssl, buf, sizeof(buf))) { 1319 if(buf[0] == 0x04 && buf[1] == 0) 1320 break; /* end of transmission */ 1321 if(!perform_data_add(ssl, zones, buf)) { 1322 if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 1323 return; 1324 } 1325 else 1326 num++; 1327 } 1328 (void)ssl_printf(ssl, "added %d datas\n", num); 1329 } 1330 1331 /** Remove RR data */ 1332 static int 1333 perform_data_remove(SSL* ssl, struct local_zones* zones, char* arg) 1334 { 1335 uint8_t* nm; 1336 int nmlabs; 1337 size_t nmlen; 1338 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1339 return 0; 1340 local_zones_del_data(zones, nm, 1341 nmlen, nmlabs, LDNS_RR_CLASS_IN); 1342 free(nm); 1343 return 1; 1344 } 1345 1346 /** Do the local_data_remove command */ 1347 static void 1348 do_data_remove(SSL* ssl, struct local_zones* zones, char* arg) 1349 { 1350 if(!perform_data_remove(ssl, zones, arg)) 1351 return; 1352 send_ok(ssl); 1353 } 1354 1355 /** Do the local_datas_remove command */ 1356 static void 1357 do_datas_remove(SSL* ssl, struct local_zones* zones) 1358 { 1359 char buf[2048]; 1360 int num = 0; 1361 while(ssl_read_line(ssl, buf, sizeof(buf))) { 1362 if(buf[0] == 0x04 && buf[1] == 0) 1363 break; /* end of transmission */ 1364 if(!perform_data_remove(ssl, zones, buf)) { 1365 if(!ssl_printf(ssl, "error for input line: %s\n", buf)) 1366 return; 1367 } 1368 else 1369 num++; 1370 } 1371 (void)ssl_printf(ssl, "removed %d datas\n", num); 1372 } 1373 1374 /** Add a new zone to view */ 1375 static void 1376 do_view_zone_add(SSL* ssl, struct worker* worker, char* arg) 1377 { 1378 char* arg2; 1379 struct view* v; 1380 if(!find_arg2(ssl, arg, &arg2)) 1381 return; 1382 v = views_find_view(worker->daemon->views, 1383 arg, 1 /* get write lock*/); 1384 if(!v) { 1385 ssl_printf(ssl,"no view with name: %s\n", arg); 1386 return; 1387 } 1388 if(!v->local_zones) { 1389 if(!(v->local_zones = local_zones_create())){ 1390 lock_rw_unlock(&v->lock); 1391 ssl_printf(ssl,"error out of memory\n"); 1392 return; 1393 } 1394 if(!v->isfirst) { 1395 /* Global local-zone is not used for this view, 1396 * therefore add defaults to this view-specic 1397 * local-zone. */ 1398 struct config_file lz_cfg; 1399 memset(&lz_cfg, 0, sizeof(lz_cfg)); 1400 local_zone_enter_defaults(v->local_zones, &lz_cfg); 1401 } 1402 } 1403 do_zone_add(ssl, v->local_zones, arg2); 1404 lock_rw_unlock(&v->lock); 1405 } 1406 1407 /** Remove a zone from view */ 1408 static void 1409 do_view_zone_remove(SSL* ssl, struct worker* worker, char* arg) 1410 { 1411 char* arg2; 1412 struct view* v; 1413 if(!find_arg2(ssl, arg, &arg2)) 1414 return; 1415 v = views_find_view(worker->daemon->views, 1416 arg, 1 /* get write lock*/); 1417 if(!v) { 1418 ssl_printf(ssl,"no view with name: %s\n", arg); 1419 return; 1420 } 1421 if(!v->local_zones) { 1422 lock_rw_unlock(&v->lock); 1423 send_ok(ssl); 1424 return; 1425 } 1426 do_zone_remove(ssl, v->local_zones, arg2); 1427 lock_rw_unlock(&v->lock); 1428 } 1429 1430 /** Add new RR data to view */ 1431 static void 1432 do_view_data_add(SSL* ssl, struct worker* worker, char* arg) 1433 { 1434 char* arg2; 1435 struct view* v; 1436 if(!find_arg2(ssl, arg, &arg2)) 1437 return; 1438 v = views_find_view(worker->daemon->views, 1439 arg, 1 /* get write lock*/); 1440 if(!v) { 1441 ssl_printf(ssl,"no view with name: %s\n", arg); 1442 return; 1443 } 1444 if(!v->local_zones) { 1445 if(!(v->local_zones = local_zones_create())){ 1446 lock_rw_unlock(&v->lock); 1447 ssl_printf(ssl,"error out of memory\n"); 1448 return; 1449 } 1450 } 1451 do_data_add(ssl, v->local_zones, arg2); 1452 lock_rw_unlock(&v->lock); 1453 } 1454 1455 /** Remove RR data from view */ 1456 static void 1457 do_view_data_remove(SSL* ssl, struct worker* worker, char* arg) 1458 { 1459 char* arg2; 1460 struct view* v; 1461 if(!find_arg2(ssl, arg, &arg2)) 1462 return; 1463 v = views_find_view(worker->daemon->views, 1464 arg, 1 /* get write lock*/); 1465 if(!v) { 1466 ssl_printf(ssl,"no view with name: %s\n", arg); 1467 return; 1468 } 1469 if(!v->local_zones) { 1470 lock_rw_unlock(&v->lock); 1471 send_ok(ssl); 1472 return; 1473 } 1474 do_data_remove(ssl, v->local_zones, arg2); 1475 lock_rw_unlock(&v->lock); 1476 } 1477 1478 /** cache lookup of nameservers */ 1479 static void 1480 do_lookup(SSL* ssl, struct worker* worker, char* arg) 1481 { 1482 uint8_t* nm; 1483 int nmlabs; 1484 size_t nmlen; 1485 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1486 return; 1487 (void)print_deleg_lookup(ssl, worker, nm, nmlen, nmlabs); 1488 free(nm); 1489 } 1490 1491 /** flush something from rrset and msg caches */ 1492 static void 1493 do_cache_remove(struct worker* worker, uint8_t* nm, size_t nmlen, 1494 uint16_t t, uint16_t c) 1495 { 1496 hashvalue_type h; 1497 struct query_info k; 1498 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 0); 1499 if(t == LDNS_RR_TYPE_SOA) 1500 rrset_cache_remove(worker->env.rrset_cache, nm, nmlen, t, c, 1501 PACKED_RRSET_SOA_NEG); 1502 k.qname = nm; 1503 k.qname_len = nmlen; 1504 k.qtype = t; 1505 k.qclass = c; 1506 k.local_alias = NULL; 1507 h = query_info_hash(&k, 0); 1508 slabhash_remove(worker->env.msg_cache, h, &k); 1509 if(t == LDNS_RR_TYPE_AAAA) { 1510 /* for AAAA also flush dns64 bit_cd packet */ 1511 h = query_info_hash(&k, BIT_CD); 1512 slabhash_remove(worker->env.msg_cache, h, &k); 1513 } 1514 } 1515 1516 /** flush a type */ 1517 static void 1518 do_flush_type(SSL* ssl, struct worker* worker, char* arg) 1519 { 1520 uint8_t* nm; 1521 int nmlabs; 1522 size_t nmlen; 1523 char* arg2; 1524 uint16_t t; 1525 if(!find_arg2(ssl, arg, &arg2)) 1526 return; 1527 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1528 return; 1529 t = sldns_get_rr_type_by_name(arg2); 1530 do_cache_remove(worker, nm, nmlen, t, LDNS_RR_CLASS_IN); 1531 1532 free(nm); 1533 send_ok(ssl); 1534 } 1535 1536 /** flush statistics */ 1537 static void 1538 do_flush_stats(SSL* ssl, struct worker* worker) 1539 { 1540 worker_stats_clear(worker); 1541 send_ok(ssl); 1542 } 1543 1544 /** 1545 * Local info for deletion functions 1546 */ 1547 struct del_info { 1548 /** worker */ 1549 struct worker* worker; 1550 /** name to delete */ 1551 uint8_t* name; 1552 /** length */ 1553 size_t len; 1554 /** labels */ 1555 int labs; 1556 /** time to invalidate to */ 1557 time_t expired; 1558 /** number of rrsets removed */ 1559 size_t num_rrsets; 1560 /** number of msgs removed */ 1561 size_t num_msgs; 1562 /** number of key entries removed */ 1563 size_t num_keys; 1564 /** length of addr */ 1565 socklen_t addrlen; 1566 /** socket address for host deletion */ 1567 struct sockaddr_storage addr; 1568 }; 1569 1570 /** callback to delete hosts in infra cache */ 1571 static void 1572 infra_del_host(struct lruhash_entry* e, void* arg) 1573 { 1574 /* entry is locked */ 1575 struct del_info* inf = (struct del_info*)arg; 1576 struct infra_key* k = (struct infra_key*)e->key; 1577 if(sockaddr_cmp(&inf->addr, inf->addrlen, &k->addr, k->addrlen) == 0) { 1578 struct infra_data* d = (struct infra_data*)e->data; 1579 d->probedelay = 0; 1580 d->timeout_A = 0; 1581 d->timeout_AAAA = 0; 1582 d->timeout_other = 0; 1583 rtt_init(&d->rtt); 1584 if(d->ttl > inf->expired) { 1585 d->ttl = inf->expired; 1586 inf->num_keys++; 1587 } 1588 } 1589 } 1590 1591 /** flush infra cache */ 1592 static void 1593 do_flush_infra(SSL* ssl, struct worker* worker, char* arg) 1594 { 1595 struct sockaddr_storage addr; 1596 socklen_t len; 1597 struct del_info inf; 1598 if(strcmp(arg, "all") == 0) { 1599 slabhash_clear(worker->env.infra_cache->hosts); 1600 send_ok(ssl); 1601 return; 1602 } 1603 if(!ipstrtoaddr(arg, UNBOUND_DNS_PORT, &addr, &len)) { 1604 (void)ssl_printf(ssl, "error parsing ip addr: '%s'\n", arg); 1605 return; 1606 } 1607 /* delete all entries from cache */ 1608 /* what we do is to set them all expired */ 1609 inf.worker = worker; 1610 inf.name = 0; 1611 inf.len = 0; 1612 inf.labs = 0; 1613 inf.expired = *worker->env.now; 1614 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1615 inf.num_rrsets = 0; 1616 inf.num_msgs = 0; 1617 inf.num_keys = 0; 1618 inf.addrlen = len; 1619 memmove(&inf.addr, &addr, len); 1620 slabhash_traverse(worker->env.infra_cache->hosts, 1, &infra_del_host, 1621 &inf); 1622 send_ok(ssl); 1623 } 1624 1625 /** flush requestlist */ 1626 static void 1627 do_flush_requestlist(SSL* ssl, struct worker* worker) 1628 { 1629 mesh_delete_all(worker->env.mesh); 1630 send_ok(ssl); 1631 } 1632 1633 /** callback to delete rrsets in a zone */ 1634 static void 1635 zone_del_rrset(struct lruhash_entry* e, void* arg) 1636 { 1637 /* entry is locked */ 1638 struct del_info* inf = (struct del_info*)arg; 1639 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 1640 if(dname_subdomain_c(k->rk.dname, inf->name)) { 1641 struct packed_rrset_data* d = 1642 (struct packed_rrset_data*)e->data; 1643 if(d->ttl > inf->expired) { 1644 d->ttl = inf->expired; 1645 inf->num_rrsets++; 1646 } 1647 } 1648 } 1649 1650 /** callback to delete messages in a zone */ 1651 static void 1652 zone_del_msg(struct lruhash_entry* e, void* arg) 1653 { 1654 /* entry is locked */ 1655 struct del_info* inf = (struct del_info*)arg; 1656 struct msgreply_entry* k = (struct msgreply_entry*)e->key; 1657 if(dname_subdomain_c(k->key.qname, inf->name)) { 1658 struct reply_info* d = (struct reply_info*)e->data; 1659 if(d->ttl > inf->expired) { 1660 d->ttl = inf->expired; 1661 d->prefetch_ttl = inf->expired; 1662 inf->num_msgs++; 1663 } 1664 } 1665 } 1666 1667 /** callback to delete keys in zone */ 1668 static void 1669 zone_del_kcache(struct lruhash_entry* e, void* arg) 1670 { 1671 /* entry is locked */ 1672 struct del_info* inf = (struct del_info*)arg; 1673 struct key_entry_key* k = (struct key_entry_key*)e->key; 1674 if(dname_subdomain_c(k->name, inf->name)) { 1675 struct key_entry_data* d = (struct key_entry_data*)e->data; 1676 if(d->ttl > inf->expired) { 1677 d->ttl = inf->expired; 1678 inf->num_keys++; 1679 } 1680 } 1681 } 1682 1683 /** remove all rrsets and keys from zone from cache */ 1684 static void 1685 do_flush_zone(SSL* ssl, struct worker* worker, char* arg) 1686 { 1687 uint8_t* nm; 1688 int nmlabs; 1689 size_t nmlen; 1690 struct del_info inf; 1691 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1692 return; 1693 /* delete all RRs and key entries from zone */ 1694 /* what we do is to set them all expired */ 1695 inf.worker = worker; 1696 inf.name = nm; 1697 inf.len = nmlen; 1698 inf.labs = nmlabs; 1699 inf.expired = *worker->env.now; 1700 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1701 inf.num_rrsets = 0; 1702 inf.num_msgs = 0; 1703 inf.num_keys = 0; 1704 slabhash_traverse(&worker->env.rrset_cache->table, 1, 1705 &zone_del_rrset, &inf); 1706 1707 slabhash_traverse(worker->env.msg_cache, 1, &zone_del_msg, &inf); 1708 1709 /* and validator cache */ 1710 if(worker->env.key_cache) { 1711 slabhash_traverse(worker->env.key_cache->slab, 1, 1712 &zone_del_kcache, &inf); 1713 } 1714 1715 free(nm); 1716 1717 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1718 "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1719 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1720 } 1721 1722 /** callback to delete bogus rrsets */ 1723 static void 1724 bogus_del_rrset(struct lruhash_entry* e, void* arg) 1725 { 1726 /* entry is locked */ 1727 struct del_info* inf = (struct del_info*)arg; 1728 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 1729 if(d->security == sec_status_bogus) { 1730 d->ttl = inf->expired; 1731 inf->num_rrsets++; 1732 } 1733 } 1734 1735 /** callback to delete bogus messages */ 1736 static void 1737 bogus_del_msg(struct lruhash_entry* e, void* arg) 1738 { 1739 /* entry is locked */ 1740 struct del_info* inf = (struct del_info*)arg; 1741 struct reply_info* d = (struct reply_info*)e->data; 1742 if(d->security == sec_status_bogus) { 1743 d->ttl = inf->expired; 1744 inf->num_msgs++; 1745 } 1746 } 1747 1748 /** callback to delete bogus keys */ 1749 static void 1750 bogus_del_kcache(struct lruhash_entry* e, void* arg) 1751 { 1752 /* entry is locked */ 1753 struct del_info* inf = (struct del_info*)arg; 1754 struct key_entry_data* d = (struct key_entry_data*)e->data; 1755 if(d->isbad) { 1756 d->ttl = inf->expired; 1757 inf->num_keys++; 1758 } 1759 } 1760 1761 /** remove all bogus rrsets, msgs and keys from cache */ 1762 static void 1763 do_flush_bogus(SSL* ssl, struct worker* worker) 1764 { 1765 struct del_info inf; 1766 /* what we do is to set them all expired */ 1767 inf.worker = worker; 1768 inf.expired = *worker->env.now; 1769 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1770 inf.num_rrsets = 0; 1771 inf.num_msgs = 0; 1772 inf.num_keys = 0; 1773 slabhash_traverse(&worker->env.rrset_cache->table, 1, 1774 &bogus_del_rrset, &inf); 1775 1776 slabhash_traverse(worker->env.msg_cache, 1, &bogus_del_msg, &inf); 1777 1778 /* and validator cache */ 1779 if(worker->env.key_cache) { 1780 slabhash_traverse(worker->env.key_cache->slab, 1, 1781 &bogus_del_kcache, &inf); 1782 } 1783 1784 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1785 "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1786 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1787 } 1788 1789 /** callback to delete negative and servfail rrsets */ 1790 static void 1791 negative_del_rrset(struct lruhash_entry* e, void* arg) 1792 { 1793 /* entry is locked */ 1794 struct del_info* inf = (struct del_info*)arg; 1795 struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; 1796 struct packed_rrset_data* d = (struct packed_rrset_data*)e->data; 1797 /* delete the parentside negative cache rrsets, 1798 * these are nameserver rrsets that failed lookup, rdata empty */ 1799 if((k->rk.flags & PACKED_RRSET_PARENT_SIDE) && d->count == 1 && 1800 d->rrsig_count == 0 && d->rr_len[0] == 0) { 1801 d->ttl = inf->expired; 1802 inf->num_rrsets++; 1803 } 1804 } 1805 1806 /** callback to delete negative and servfail messages */ 1807 static void 1808 negative_del_msg(struct lruhash_entry* e, void* arg) 1809 { 1810 /* entry is locked */ 1811 struct del_info* inf = (struct del_info*)arg; 1812 struct reply_info* d = (struct reply_info*)e->data; 1813 /* rcode not NOERROR: NXDOMAIN, SERVFAIL, ..: an nxdomain or error 1814 * or NOERROR rcode with ANCOUNT==0: a NODATA answer */ 1815 if(FLAGS_GET_RCODE(d->flags) != 0 || d->an_numrrsets == 0) { 1816 d->ttl = inf->expired; 1817 inf->num_msgs++; 1818 } 1819 } 1820 1821 /** callback to delete negative key entries */ 1822 static void 1823 negative_del_kcache(struct lruhash_entry* e, void* arg) 1824 { 1825 /* entry is locked */ 1826 struct del_info* inf = (struct del_info*)arg; 1827 struct key_entry_data* d = (struct key_entry_data*)e->data; 1828 /* could be bad because of lookup failure on the DS, DNSKEY, which 1829 * was nxdomain or servfail, and thus a result of negative lookups */ 1830 if(d->isbad) { 1831 d->ttl = inf->expired; 1832 inf->num_keys++; 1833 } 1834 } 1835 1836 /** remove all negative(NODATA,NXDOMAIN), and servfail messages from cache */ 1837 static void 1838 do_flush_negative(SSL* ssl, struct worker* worker) 1839 { 1840 struct del_info inf; 1841 /* what we do is to set them all expired */ 1842 inf.worker = worker; 1843 inf.expired = *worker->env.now; 1844 inf.expired -= 3; /* handle 3 seconds skew between threads */ 1845 inf.num_rrsets = 0; 1846 inf.num_msgs = 0; 1847 inf.num_keys = 0; 1848 slabhash_traverse(&worker->env.rrset_cache->table, 1, 1849 &negative_del_rrset, &inf); 1850 1851 slabhash_traverse(worker->env.msg_cache, 1, &negative_del_msg, &inf); 1852 1853 /* and validator cache */ 1854 if(worker->env.key_cache) { 1855 slabhash_traverse(worker->env.key_cache->slab, 1, 1856 &negative_del_kcache, &inf); 1857 } 1858 1859 (void)ssl_printf(ssl, "ok removed %lu rrsets, %lu messages " 1860 "and %lu key entries\n", (unsigned long)inf.num_rrsets, 1861 (unsigned long)inf.num_msgs, (unsigned long)inf.num_keys); 1862 } 1863 1864 /** remove name rrset from cache */ 1865 static void 1866 do_flush_name(SSL* ssl, struct worker* w, char* arg) 1867 { 1868 uint8_t* nm; 1869 int nmlabs; 1870 size_t nmlen; 1871 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 1872 return; 1873 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN); 1874 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN); 1875 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN); 1876 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SOA, LDNS_RR_CLASS_IN); 1877 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_CNAME, LDNS_RR_CLASS_IN); 1878 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_DNAME, LDNS_RR_CLASS_IN); 1879 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_MX, LDNS_RR_CLASS_IN); 1880 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_PTR, LDNS_RR_CLASS_IN); 1881 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_SRV, LDNS_RR_CLASS_IN); 1882 do_cache_remove(w, nm, nmlen, LDNS_RR_TYPE_NAPTR, LDNS_RR_CLASS_IN); 1883 1884 free(nm); 1885 send_ok(ssl); 1886 } 1887 1888 /** printout a delegation point info */ 1889 static int 1890 ssl_print_name_dp(SSL* ssl, const char* str, uint8_t* nm, uint16_t dclass, 1891 struct delegpt* dp) 1892 { 1893 char buf[257]; 1894 struct delegpt_ns* ns; 1895 struct delegpt_addr* a; 1896 int f = 0; 1897 if(str) { /* print header for forward, stub */ 1898 char* c = sldns_wire2str_class(dclass); 1899 dname_str(nm, buf); 1900 if(!ssl_printf(ssl, "%s %s %s ", buf, (c?c:"CLASS??"), str)) { 1901 free(c); 1902 return 0; 1903 } 1904 free(c); 1905 } 1906 for(ns = dp->nslist; ns; ns = ns->next) { 1907 dname_str(ns->name, buf); 1908 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 1909 return 0; 1910 f = 1; 1911 } 1912 for(a = dp->target_list; a; a = a->next_target) { 1913 addr_to_str(&a->addr, a->addrlen, buf, sizeof(buf)); 1914 if(!ssl_printf(ssl, "%s%s", (f?" ":""), buf)) 1915 return 0; 1916 f = 1; 1917 } 1918 return ssl_printf(ssl, "\n"); 1919 } 1920 1921 1922 /** print root forwards */ 1923 static int 1924 print_root_fwds(SSL* ssl, struct iter_forwards* fwds, uint8_t* root) 1925 { 1926 struct delegpt* dp; 1927 dp = forwards_lookup(fwds, root, LDNS_RR_CLASS_IN); 1928 if(!dp) 1929 return ssl_printf(ssl, "off (using root hints)\n"); 1930 /* if dp is returned it must be the root */ 1931 log_assert(query_dname_compare(dp->name, root)==0); 1932 return ssl_print_name_dp(ssl, NULL, root, LDNS_RR_CLASS_IN, dp); 1933 } 1934 1935 /** parse args into delegpt */ 1936 static struct delegpt* 1937 parse_delegpt(SSL* ssl, char* args, uint8_t* nm, int allow_names) 1938 { 1939 /* parse args and add in */ 1940 char* p = args; 1941 char* todo; 1942 struct delegpt* dp = delegpt_create_mlc(nm); 1943 struct sockaddr_storage addr; 1944 socklen_t addrlen; 1945 char* auth_name; 1946 if(!dp) { 1947 (void)ssl_printf(ssl, "error out of memory\n"); 1948 return NULL; 1949 } 1950 while(p) { 1951 todo = p; 1952 p = strchr(p, ' '); /* find next spot, if any */ 1953 if(p) { 1954 *p++ = 0; /* end this spot */ 1955 p = skipwhite(p); /* position at next spot */ 1956 } 1957 /* parse address */ 1958 if(!authextstrtoaddr(todo, &addr, &addrlen, &auth_name)) { 1959 if(allow_names) { 1960 uint8_t* n = NULL; 1961 size_t ln; 1962 int lb; 1963 if(!parse_arg_name(ssl, todo, &n, &ln, &lb)) { 1964 (void)ssl_printf(ssl, "error cannot " 1965 "parse IP address or name " 1966 "'%s'\n", todo); 1967 delegpt_free_mlc(dp); 1968 return NULL; 1969 } 1970 if(!delegpt_add_ns_mlc(dp, n, 0)) { 1971 (void)ssl_printf(ssl, "error out of memory\n"); 1972 free(n); 1973 delegpt_free_mlc(dp); 1974 return NULL; 1975 } 1976 free(n); 1977 1978 } else { 1979 (void)ssl_printf(ssl, "error cannot parse" 1980 " IP address '%s'\n", todo); 1981 delegpt_free_mlc(dp); 1982 return NULL; 1983 } 1984 } else { 1985 /* add address */ 1986 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0, 1987 auth_name)) { 1988 (void)ssl_printf(ssl, "error out of memory\n"); 1989 delegpt_free_mlc(dp); 1990 return NULL; 1991 } 1992 } 1993 } 1994 dp->has_parent_side_NS = 1; 1995 return dp; 1996 } 1997 1998 /** do the status command */ 1999 static void 2000 do_forward(SSL* ssl, struct worker* worker, char* args) 2001 { 2002 struct iter_forwards* fwd = worker->env.fwds; 2003 uint8_t* root = (uint8_t*)"\000"; 2004 if(!fwd) { 2005 (void)ssl_printf(ssl, "error: structure not allocated\n"); 2006 return; 2007 } 2008 if(args == NULL || args[0] == 0) { 2009 (void)print_root_fwds(ssl, fwd, root); 2010 return; 2011 } 2012 /* set root forwards for this thread. since we are in remote control 2013 * the actual mesh is not running, so we can freely edit it. */ 2014 /* delete all the existing queries first */ 2015 mesh_delete_all(worker->env.mesh); 2016 if(strcmp(args, "off") == 0) { 2017 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, root); 2018 } else { 2019 struct delegpt* dp; 2020 if(!(dp = parse_delegpt(ssl, args, root, 0))) 2021 return; 2022 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) { 2023 (void)ssl_printf(ssl, "error out of memory\n"); 2024 return; 2025 } 2026 } 2027 send_ok(ssl); 2028 } 2029 2030 static int 2031 parse_fs_args(SSL* ssl, char* args, uint8_t** nm, struct delegpt** dp, 2032 int* insecure, int* prime) 2033 { 2034 char* zonename; 2035 char* rest; 2036 size_t nmlen; 2037 int nmlabs; 2038 /* parse all -x args */ 2039 while(args[0] == '+') { 2040 if(!find_arg2(ssl, args, &rest)) 2041 return 0; 2042 while(*(++args) != 0) { 2043 if(*args == 'i' && insecure) 2044 *insecure = 1; 2045 else if(*args == 'p' && prime) 2046 *prime = 1; 2047 else { 2048 (void)ssl_printf(ssl, "error: unknown option %s\n", args); 2049 return 0; 2050 } 2051 } 2052 args = rest; 2053 } 2054 /* parse name */ 2055 if(dp) { 2056 if(!find_arg2(ssl, args, &rest)) 2057 return 0; 2058 zonename = args; 2059 args = rest; 2060 } else zonename = args; 2061 if(!parse_arg_name(ssl, zonename, nm, &nmlen, &nmlabs)) 2062 return 0; 2063 2064 /* parse dp */ 2065 if(dp) { 2066 if(!(*dp = parse_delegpt(ssl, args, *nm, 1))) { 2067 free(*nm); 2068 return 0; 2069 } 2070 } 2071 return 1; 2072 } 2073 2074 /** do the forward_add command */ 2075 static void 2076 do_forward_add(SSL* ssl, struct worker* worker, char* args) 2077 { 2078 struct iter_forwards* fwd = worker->env.fwds; 2079 int insecure = 0; 2080 uint8_t* nm = NULL; 2081 struct delegpt* dp = NULL; 2082 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, NULL)) 2083 return; 2084 if(insecure && worker->env.anchors) { 2085 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2086 nm)) { 2087 (void)ssl_printf(ssl, "error out of memory\n"); 2088 delegpt_free_mlc(dp); 2089 free(nm); 2090 return; 2091 } 2092 } 2093 if(!forwards_add_zone(fwd, LDNS_RR_CLASS_IN, dp)) { 2094 (void)ssl_printf(ssl, "error out of memory\n"); 2095 free(nm); 2096 return; 2097 } 2098 free(nm); 2099 send_ok(ssl); 2100 } 2101 2102 /** do the forward_remove command */ 2103 static void 2104 do_forward_remove(SSL* ssl, struct worker* worker, char* args) 2105 { 2106 struct iter_forwards* fwd = worker->env.fwds; 2107 int insecure = 0; 2108 uint8_t* nm = NULL; 2109 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL)) 2110 return; 2111 if(insecure && worker->env.anchors) 2112 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2113 nm); 2114 forwards_delete_zone(fwd, LDNS_RR_CLASS_IN, nm); 2115 free(nm); 2116 send_ok(ssl); 2117 } 2118 2119 /** do the stub_add command */ 2120 static void 2121 do_stub_add(SSL* ssl, struct worker* worker, char* args) 2122 { 2123 struct iter_forwards* fwd = worker->env.fwds; 2124 int insecure = 0, prime = 0; 2125 uint8_t* nm = NULL; 2126 struct delegpt* dp = NULL; 2127 if(!parse_fs_args(ssl, args, &nm, &dp, &insecure, &prime)) 2128 return; 2129 if(insecure && worker->env.anchors) { 2130 if(!anchors_add_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2131 nm)) { 2132 (void)ssl_printf(ssl, "error out of memory\n"); 2133 delegpt_free_mlc(dp); 2134 free(nm); 2135 return; 2136 } 2137 } 2138 if(!forwards_add_stub_hole(fwd, LDNS_RR_CLASS_IN, nm)) { 2139 if(insecure && worker->env.anchors) 2140 anchors_delete_insecure(worker->env.anchors, 2141 LDNS_RR_CLASS_IN, nm); 2142 (void)ssl_printf(ssl, "error out of memory\n"); 2143 delegpt_free_mlc(dp); 2144 free(nm); 2145 return; 2146 } 2147 if(!hints_add_stub(worker->env.hints, LDNS_RR_CLASS_IN, dp, !prime)) { 2148 (void)ssl_printf(ssl, "error out of memory\n"); 2149 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm); 2150 if(insecure && worker->env.anchors) 2151 anchors_delete_insecure(worker->env.anchors, 2152 LDNS_RR_CLASS_IN, nm); 2153 free(nm); 2154 return; 2155 } 2156 free(nm); 2157 send_ok(ssl); 2158 } 2159 2160 /** do the stub_remove command */ 2161 static void 2162 do_stub_remove(SSL* ssl, struct worker* worker, char* args) 2163 { 2164 struct iter_forwards* fwd = worker->env.fwds; 2165 int insecure = 0; 2166 uint8_t* nm = NULL; 2167 if(!parse_fs_args(ssl, args, &nm, NULL, &insecure, NULL)) 2168 return; 2169 if(insecure && worker->env.anchors) 2170 anchors_delete_insecure(worker->env.anchors, LDNS_RR_CLASS_IN, 2171 nm); 2172 forwards_delete_stub_hole(fwd, LDNS_RR_CLASS_IN, nm); 2173 hints_delete_stub(worker->env.hints, LDNS_RR_CLASS_IN, nm); 2174 free(nm); 2175 send_ok(ssl); 2176 } 2177 2178 /** do the insecure_add command */ 2179 static void 2180 do_insecure_add(SSL* ssl, struct worker* worker, char* arg) 2181 { 2182 size_t nmlen; 2183 int nmlabs; 2184 uint8_t* nm = NULL; 2185 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2186 return; 2187 if(worker->env.anchors) { 2188 if(!anchors_add_insecure(worker->env.anchors, 2189 LDNS_RR_CLASS_IN, nm)) { 2190 (void)ssl_printf(ssl, "error out of memory\n"); 2191 free(nm); 2192 return; 2193 } 2194 } 2195 free(nm); 2196 send_ok(ssl); 2197 } 2198 2199 /** do the insecure_remove command */ 2200 static void 2201 do_insecure_remove(SSL* ssl, struct worker* worker, char* arg) 2202 { 2203 size_t nmlen; 2204 int nmlabs; 2205 uint8_t* nm = NULL; 2206 if(!parse_arg_name(ssl, arg, &nm, &nmlen, &nmlabs)) 2207 return; 2208 if(worker->env.anchors) 2209 anchors_delete_insecure(worker->env.anchors, 2210 LDNS_RR_CLASS_IN, nm); 2211 free(nm); 2212 send_ok(ssl); 2213 } 2214 2215 static void 2216 do_insecure_list(SSL* ssl, struct worker* worker) 2217 { 2218 char buf[257]; 2219 struct trust_anchor* a; 2220 if(worker->env.anchors) { 2221 RBTREE_FOR(a, struct trust_anchor*, worker->env.anchors->tree) { 2222 if(a->numDS == 0 && a->numDNSKEY == 0) { 2223 dname_str(a->name, buf); 2224 ssl_printf(ssl, "%s\n", buf); 2225 } 2226 } 2227 } 2228 } 2229 2230 /** do the status command */ 2231 static void 2232 do_status(SSL* ssl, struct worker* worker) 2233 { 2234 int i; 2235 time_t uptime; 2236 if(!ssl_printf(ssl, "version: %s\n", PACKAGE_VERSION)) 2237 return; 2238 if(!ssl_printf(ssl, "verbosity: %d\n", verbosity)) 2239 return; 2240 if(!ssl_printf(ssl, "threads: %d\n", worker->daemon->num)) 2241 return; 2242 if(!ssl_printf(ssl, "modules: %d [", worker->daemon->mods.num)) 2243 return; 2244 for(i=0; i<worker->daemon->mods.num; i++) { 2245 if(!ssl_printf(ssl, " %s", worker->daemon->mods.mod[i]->name)) 2246 return; 2247 } 2248 if(!ssl_printf(ssl, " ]\n")) 2249 return; 2250 uptime = (time_t)time(NULL) - (time_t)worker->daemon->time_boot.tv_sec; 2251 if(!ssl_printf(ssl, "uptime: " ARG_LL "d seconds\n", (long long)uptime)) 2252 return; 2253 if(!ssl_printf(ssl, "options:%s%s\n" , 2254 (worker->daemon->reuseport?" reuseport":""), 2255 (worker->daemon->rc->accept_list?" control(ssl)":""))) 2256 return; 2257 if(!ssl_printf(ssl, "unbound (pid %d) is running...\n", 2258 (int)getpid())) 2259 return; 2260 } 2261 2262 /** get age for the mesh state */ 2263 static void 2264 get_mesh_age(struct mesh_state* m, char* buf, size_t len, 2265 struct module_env* env) 2266 { 2267 if(m->reply_list) { 2268 struct timeval d; 2269 struct mesh_reply* r = m->reply_list; 2270 /* last reply is the oldest */ 2271 while(r && r->next) 2272 r = r->next; 2273 timeval_subtract(&d, env->now_tv, &r->start_time); 2274 snprintf(buf, len, ARG_LL "d.%6.6d", 2275 (long long)d.tv_sec, (int)d.tv_usec); 2276 } else { 2277 snprintf(buf, len, "-"); 2278 } 2279 } 2280 2281 /** get status of a mesh state */ 2282 static void 2283 get_mesh_status(struct mesh_area* mesh, struct mesh_state* m, 2284 char* buf, size_t len) 2285 { 2286 enum module_ext_state s = m->s.ext_state[m->s.curmod]; 2287 const char *modname = mesh->mods.mod[m->s.curmod]->name; 2288 size_t l; 2289 if(strcmp(modname, "iterator") == 0 && s == module_wait_reply && 2290 m->s.minfo[m->s.curmod]) { 2291 /* break into iterator to find out who its waiting for */ 2292 struct iter_qstate* qstate = (struct iter_qstate*) 2293 m->s.minfo[m->s.curmod]; 2294 struct outbound_list* ol = &qstate->outlist; 2295 struct outbound_entry* e; 2296 snprintf(buf, len, "%s wait for", modname); 2297 l = strlen(buf); 2298 buf += l; len -= l; 2299 if(ol->first == NULL) 2300 snprintf(buf, len, " (empty_list)"); 2301 for(e = ol->first; e; e = e->next) { 2302 snprintf(buf, len, " "); 2303 l = strlen(buf); 2304 buf += l; len -= l; 2305 addr_to_str(&e->qsent->addr, e->qsent->addrlen, 2306 buf, len); 2307 l = strlen(buf); 2308 buf += l; len -= l; 2309 } 2310 } else if(s == module_wait_subquery) { 2311 /* look in subs from mesh state to see what */ 2312 char nm[257]; 2313 struct mesh_state_ref* sub; 2314 snprintf(buf, len, "%s wants", modname); 2315 l = strlen(buf); 2316 buf += l; len -= l; 2317 if(m->sub_set.count == 0) 2318 snprintf(buf, len, " (empty_list)"); 2319 RBTREE_FOR(sub, struct mesh_state_ref*, &m->sub_set) { 2320 char* t = sldns_wire2str_type(sub->s->s.qinfo.qtype); 2321 char* c = sldns_wire2str_class(sub->s->s.qinfo.qclass); 2322 dname_str(sub->s->s.qinfo.qname, nm); 2323 snprintf(buf, len, " %s %s %s", (t?t:"TYPE??"), 2324 (c?c:"CLASS??"), nm); 2325 l = strlen(buf); 2326 buf += l; len -= l; 2327 free(t); 2328 free(c); 2329 } 2330 } else { 2331 snprintf(buf, len, "%s is %s", modname, strextstate(s)); 2332 } 2333 } 2334 2335 /** do the dump_requestlist command */ 2336 static void 2337 do_dump_requestlist(SSL* ssl, struct worker* worker) 2338 { 2339 struct mesh_area* mesh; 2340 struct mesh_state* m; 2341 int num = 0; 2342 char buf[257]; 2343 char timebuf[32]; 2344 char statbuf[10240]; 2345 if(!ssl_printf(ssl, "thread #%d\n", worker->thread_num)) 2346 return; 2347 if(!ssl_printf(ssl, "# type cl name seconds module status\n")) 2348 return; 2349 /* show worker mesh contents */ 2350 mesh = worker->env.mesh; 2351 if(!mesh) return; 2352 RBTREE_FOR(m, struct mesh_state*, &mesh->all) { 2353 char* t = sldns_wire2str_type(m->s.qinfo.qtype); 2354 char* c = sldns_wire2str_class(m->s.qinfo.qclass); 2355 dname_str(m->s.qinfo.qname, buf); 2356 get_mesh_age(m, timebuf, sizeof(timebuf), &worker->env); 2357 get_mesh_status(mesh, m, statbuf, sizeof(statbuf)); 2358 if(!ssl_printf(ssl, "%3d %4s %2s %s %s %s\n", 2359 num, (t?t:"TYPE??"), (c?c:"CLASS??"), buf, timebuf, 2360 statbuf)) { 2361 free(t); 2362 free(c); 2363 return; 2364 } 2365 num++; 2366 free(t); 2367 free(c); 2368 } 2369 } 2370 2371 /** structure for argument data for dump infra host */ 2372 struct infra_arg { 2373 /** the infra cache */ 2374 struct infra_cache* infra; 2375 /** the SSL connection */ 2376 SSL* ssl; 2377 /** the time now */ 2378 time_t now; 2379 /** ssl failure? stop writing and skip the rest. If the tcp 2380 * connection is broken, and writes fail, we then stop writing. */ 2381 int ssl_failed; 2382 }; 2383 2384 /** callback for every host element in the infra cache */ 2385 static void 2386 dump_infra_host(struct lruhash_entry* e, void* arg) 2387 { 2388 struct infra_arg* a = (struct infra_arg*)arg; 2389 struct infra_key* k = (struct infra_key*)e->key; 2390 struct infra_data* d = (struct infra_data*)e->data; 2391 char ip_str[1024]; 2392 char name[257]; 2393 int port; 2394 if(a->ssl_failed) 2395 return; 2396 addr_to_str(&k->addr, k->addrlen, ip_str, sizeof(ip_str)); 2397 dname_str(k->zonename, name); 2398 port = (int)ntohs(((struct sockaddr_in*)&k->addr)->sin_port); 2399 if(port != UNBOUND_DNS_PORT) { 2400 snprintf(ip_str+strlen(ip_str), sizeof(ip_str)-strlen(ip_str), 2401 "@%d", port); 2402 } 2403 /* skip expired stuff (only backed off) */ 2404 if(d->ttl < a->now) { 2405 if(d->rtt.rto >= USEFUL_SERVER_TOP_TIMEOUT) { 2406 if(!ssl_printf(a->ssl, "%s %s expired rto %d\n", ip_str, 2407 name, d->rtt.rto)) { 2408 a->ssl_failed = 1; 2409 return; 2410 } 2411 } 2412 return; 2413 } 2414 if(!ssl_printf(a->ssl, "%s %s ttl %lu ping %d var %d rtt %d rto %d " 2415 "tA %d tAAAA %d tother %d " 2416 "ednsknown %d edns %d delay %d lame dnssec %d rec %d A %d " 2417 "other %d\n", ip_str, name, (unsigned long)(d->ttl - a->now), 2418 d->rtt.srtt, d->rtt.rttvar, rtt_notimeout(&d->rtt), d->rtt.rto, 2419 d->timeout_A, d->timeout_AAAA, d->timeout_other, 2420 (int)d->edns_lame_known, (int)d->edns_version, 2421 (int)(a->now<d->probedelay?(d->probedelay - a->now):0), 2422 (int)d->isdnsseclame, (int)d->rec_lame, (int)d->lame_type_A, 2423 (int)d->lame_other)) { 2424 a->ssl_failed = 1; 2425 return; 2426 } 2427 } 2428 2429 /** do the dump_infra command */ 2430 static void 2431 do_dump_infra(SSL* ssl, struct worker* worker) 2432 { 2433 struct infra_arg arg; 2434 arg.infra = worker->env.infra_cache; 2435 arg.ssl = ssl; 2436 arg.now = *worker->env.now; 2437 arg.ssl_failed = 0; 2438 slabhash_traverse(arg.infra->hosts, 0, &dump_infra_host, (void*)&arg); 2439 } 2440 2441 /** do the log_reopen command */ 2442 static void 2443 do_log_reopen(SSL* ssl, struct worker* worker) 2444 { 2445 struct config_file* cfg = worker->env.cfg; 2446 send_ok(ssl); 2447 log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir); 2448 } 2449 2450 /** do the set_option command */ 2451 static void 2452 do_set_option(SSL* ssl, struct worker* worker, char* arg) 2453 { 2454 char* arg2; 2455 if(!find_arg2(ssl, arg, &arg2)) 2456 return; 2457 if(!config_set_option(worker->env.cfg, arg, arg2)) { 2458 (void)ssl_printf(ssl, "error setting option\n"); 2459 return; 2460 } 2461 /* effectuate some arguments */ 2462 if(strcmp(arg, "val-override-date:") == 0) { 2463 int m = modstack_find(&worker->env.mesh->mods, "validator"); 2464 struct val_env* val_env = NULL; 2465 if(m != -1) val_env = (struct val_env*)worker->env.modinfo[m]; 2466 if(val_env) 2467 val_env->date_override = worker->env.cfg->val_date_override; 2468 } 2469 send_ok(ssl); 2470 } 2471 2472 /* routine to printout option values over SSL */ 2473 void remote_get_opt_ssl(char* line, void* arg) 2474 { 2475 SSL* ssl = (SSL*)arg; 2476 (void)ssl_printf(ssl, "%s\n", line); 2477 } 2478 2479 /** do the get_option command */ 2480 static void 2481 do_get_option(SSL* ssl, struct worker* worker, char* arg) 2482 { 2483 int r; 2484 r = config_get_option(worker->env.cfg, arg, remote_get_opt_ssl, ssl); 2485 if(!r) { 2486 (void)ssl_printf(ssl, "error unknown option\n"); 2487 return; 2488 } 2489 } 2490 2491 /** do the list_forwards command */ 2492 static void 2493 do_list_forwards(SSL* ssl, struct worker* worker) 2494 { 2495 /* since its a per-worker structure no locks needed */ 2496 struct iter_forwards* fwds = worker->env.fwds; 2497 struct iter_forward_zone* z; 2498 struct trust_anchor* a; 2499 int insecure; 2500 RBTREE_FOR(z, struct iter_forward_zone*, fwds->tree) { 2501 if(!z->dp) continue; /* skip empty marker for stub */ 2502 2503 /* see if it is insecure */ 2504 insecure = 0; 2505 if(worker->env.anchors && 2506 (a=anchor_find(worker->env.anchors, z->name, 2507 z->namelabs, z->namelen, z->dclass))) { 2508 if(!a->keylist && !a->numDS && !a->numDNSKEY) 2509 insecure = 1; 2510 lock_basic_unlock(&a->lock); 2511 } 2512 2513 if(!ssl_print_name_dp(ssl, (insecure?"forward +i":"forward"), 2514 z->name, z->dclass, z->dp)) 2515 return; 2516 } 2517 } 2518 2519 /** do the list_stubs command */ 2520 static void 2521 do_list_stubs(SSL* ssl, struct worker* worker) 2522 { 2523 struct iter_hints_stub* z; 2524 struct trust_anchor* a; 2525 int insecure; 2526 char str[32]; 2527 RBTREE_FOR(z, struct iter_hints_stub*, &worker->env.hints->tree) { 2528 2529 /* see if it is insecure */ 2530 insecure = 0; 2531 if(worker->env.anchors && 2532 (a=anchor_find(worker->env.anchors, z->node.name, 2533 z->node.labs, z->node.len, z->node.dclass))) { 2534 if(!a->keylist && !a->numDS && !a->numDNSKEY) 2535 insecure = 1; 2536 lock_basic_unlock(&a->lock); 2537 } 2538 2539 snprintf(str, sizeof(str), "stub %sprime%s", 2540 (z->noprime?"no":""), (insecure?" +i":"")); 2541 if(!ssl_print_name_dp(ssl, str, z->node.name, 2542 z->node.dclass, z->dp)) 2543 return; 2544 } 2545 } 2546 2547 /** do the list_auth_zones command */ 2548 static void 2549 do_list_auth_zones(SSL* ssl, struct auth_zones* az) 2550 { 2551 struct auth_zone* z; 2552 char buf[257], buf2[256]; 2553 lock_rw_rdlock(&az->lock); 2554 RBTREE_FOR(z, struct auth_zone*, &az->ztree) { 2555 lock_rw_rdlock(&z->lock); 2556 dname_str(z->name, buf); 2557 if(z->zone_expired) 2558 snprintf(buf2, sizeof(buf2), "expired"); 2559 else { 2560 uint32_t serial = 0; 2561 if(auth_zone_get_serial(z, &serial)) 2562 snprintf(buf2, sizeof(buf2), "serial %u", 2563 (unsigned)serial); 2564 else snprintf(buf2, sizeof(buf2), "no serial"); 2565 } 2566 if(!ssl_printf(ssl, "%s\t%s\n", buf, buf2)) { 2567 /* failure to print */ 2568 lock_rw_unlock(&z->lock); 2569 lock_rw_unlock(&az->lock); 2570 return; 2571 } 2572 lock_rw_unlock(&z->lock); 2573 } 2574 lock_rw_unlock(&az->lock); 2575 } 2576 2577 /** do the list_local_zones command */ 2578 static void 2579 do_list_local_zones(SSL* ssl, struct local_zones* zones) 2580 { 2581 struct local_zone* z; 2582 char buf[257]; 2583 lock_rw_rdlock(&zones->lock); 2584 RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2585 lock_rw_rdlock(&z->lock); 2586 dname_str(z->name, buf); 2587 if(!ssl_printf(ssl, "%s %s\n", buf, 2588 local_zone_type2str(z->type))) { 2589 /* failure to print */ 2590 lock_rw_unlock(&z->lock); 2591 lock_rw_unlock(&zones->lock); 2592 return; 2593 } 2594 lock_rw_unlock(&z->lock); 2595 } 2596 lock_rw_unlock(&zones->lock); 2597 } 2598 2599 /** do the list_local_data command */ 2600 static void 2601 do_list_local_data(SSL* ssl, struct worker* worker, struct local_zones* zones) 2602 { 2603 struct local_zone* z; 2604 struct local_data* d; 2605 struct local_rrset* p; 2606 char* s = (char*)sldns_buffer_begin(worker->env.scratch_buffer); 2607 size_t slen = sldns_buffer_capacity(worker->env.scratch_buffer); 2608 lock_rw_rdlock(&zones->lock); 2609 RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 2610 lock_rw_rdlock(&z->lock); 2611 RBTREE_FOR(d, struct local_data*, &z->data) { 2612 for(p = d->rrsets; p; p = p->next) { 2613 struct packed_rrset_data* d = 2614 (struct packed_rrset_data*)p->rrset->entry.data; 2615 size_t i; 2616 for(i=0; i<d->count + d->rrsig_count; i++) { 2617 if(!packed_rr_to_string(p->rrset, i, 2618 0, s, slen)) { 2619 if(!ssl_printf(ssl, "BADRR\n")) { 2620 lock_rw_unlock(&z->lock); 2621 lock_rw_unlock(&zones->lock); 2622 return; 2623 } 2624 } 2625 if(!ssl_printf(ssl, "%s\n", s)) { 2626 lock_rw_unlock(&z->lock); 2627 lock_rw_unlock(&zones->lock); 2628 return; 2629 } 2630 } 2631 } 2632 } 2633 lock_rw_unlock(&z->lock); 2634 } 2635 lock_rw_unlock(&zones->lock); 2636 } 2637 2638 /** do the view_list_local_zones command */ 2639 static void 2640 do_view_list_local_zones(SSL* ssl, struct worker* worker, char* arg) 2641 { 2642 struct view* v = views_find_view(worker->daemon->views, 2643 arg, 0 /* get read lock*/); 2644 if(!v) { 2645 ssl_printf(ssl,"no view with name: %s\n", arg); 2646 return; 2647 } 2648 if(v->local_zones) { 2649 do_list_local_zones(ssl, v->local_zones); 2650 } 2651 lock_rw_unlock(&v->lock); 2652 } 2653 2654 /** do the view_list_local_data command */ 2655 static void 2656 do_view_list_local_data(SSL* ssl, struct worker* worker, char* arg) 2657 { 2658 struct view* v = views_find_view(worker->daemon->views, 2659 arg, 0 /* get read lock*/); 2660 if(!v) { 2661 ssl_printf(ssl,"no view with name: %s\n", arg); 2662 return; 2663 } 2664 if(v->local_zones) { 2665 do_list_local_data(ssl, worker, v->local_zones); 2666 } 2667 lock_rw_unlock(&v->lock); 2668 } 2669 2670 /** struct for user arg ratelimit list */ 2671 struct ratelimit_list_arg { 2672 /** the infra cache */ 2673 struct infra_cache* infra; 2674 /** the SSL to print to */ 2675 SSL* ssl; 2676 /** all or only ratelimited */ 2677 int all; 2678 /** current time */ 2679 time_t now; 2680 }; 2681 2682 #define ip_ratelimit_list_arg ratelimit_list_arg 2683 2684 /** list items in the ratelimit table */ 2685 static void 2686 rate_list(struct lruhash_entry* e, void* arg) 2687 { 2688 struct ratelimit_list_arg* a = (struct ratelimit_list_arg*)arg; 2689 struct rate_key* k = (struct rate_key*)e->key; 2690 struct rate_data* d = (struct rate_data*)e->data; 2691 char buf[257]; 2692 int lim = infra_find_ratelimit(a->infra, k->name, k->namelen); 2693 int max = infra_rate_max(d, a->now); 2694 if(a->all == 0) { 2695 if(max < lim) 2696 return; 2697 } 2698 dname_str(k->name, buf); 2699 ssl_printf(a->ssl, "%s %d limit %d\n", buf, max, lim); 2700 } 2701 2702 /** list items in the ip_ratelimit table */ 2703 static void 2704 ip_rate_list(struct lruhash_entry* e, void* arg) 2705 { 2706 char ip[128]; 2707 struct ip_ratelimit_list_arg* a = (struct ip_ratelimit_list_arg*)arg; 2708 struct ip_rate_key* k = (struct ip_rate_key*)e->key; 2709 struct ip_rate_data* d = (struct ip_rate_data*)e->data; 2710 int lim = infra_ip_ratelimit; 2711 int max = infra_rate_max(d, a->now); 2712 if(a->all == 0) { 2713 if(max < lim) 2714 return; 2715 } 2716 addr_to_str(&k->addr, k->addrlen, ip, sizeof(ip)); 2717 ssl_printf(a->ssl, "%s %d limit %d\n", ip, max, lim); 2718 } 2719 2720 /** do the ratelimit_list command */ 2721 static void 2722 do_ratelimit_list(SSL* ssl, struct worker* worker, char* arg) 2723 { 2724 struct ratelimit_list_arg a; 2725 a.all = 0; 2726 a.infra = worker->env.infra_cache; 2727 a.now = *worker->env.now; 2728 a.ssl = ssl; 2729 arg = skipwhite(arg); 2730 if(strcmp(arg, "+a") == 0) 2731 a.all = 1; 2732 if(a.infra->domain_rates==NULL || 2733 (a.all == 0 && infra_dp_ratelimit == 0)) 2734 return; 2735 slabhash_traverse(a.infra->domain_rates, 0, rate_list, &a); 2736 } 2737 2738 /** do the ip_ratelimit_list command */ 2739 static void 2740 do_ip_ratelimit_list(SSL* ssl, struct worker* worker, char* arg) 2741 { 2742 struct ip_ratelimit_list_arg a; 2743 a.all = 0; 2744 a.infra = worker->env.infra_cache; 2745 a.now = *worker->env.now; 2746 a.ssl = ssl; 2747 arg = skipwhite(arg); 2748 if(strcmp(arg, "+a") == 0) 2749 a.all = 1; 2750 if(a.infra->client_ip_rates==NULL || 2751 (a.all == 0 && infra_ip_ratelimit == 0)) 2752 return; 2753 slabhash_traverse(a.infra->client_ip_rates, 0, ip_rate_list, &a); 2754 } 2755 2756 /** tell other processes to execute the command */ 2757 static void 2758 distribute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd) 2759 { 2760 int i; 2761 if(!cmd || !ssl) 2762 return; 2763 /* skip i=0 which is me */ 2764 for(i=1; i<rc->worker->daemon->num; i++) { 2765 worker_send_cmd(rc->worker->daemon->workers[i], 2766 worker_cmd_remote); 2767 if(!tube_write_msg(rc->worker->daemon->workers[i]->cmd, 2768 (uint8_t*)cmd, strlen(cmd)+1, 0)) { 2769 ssl_printf(ssl, "error could not distribute cmd\n"); 2770 return; 2771 } 2772 } 2773 } 2774 2775 /** check for name with end-of-string, space or tab after it */ 2776 static int 2777 cmdcmp(char* p, const char* cmd, size_t len) 2778 { 2779 return strncmp(p,cmd,len)==0 && (p[len]==0||p[len]==' '||p[len]=='\t'); 2780 } 2781 2782 /** execute a remote control command */ 2783 static void 2784 execute_cmd(struct daemon_remote* rc, SSL* ssl, char* cmd, 2785 struct worker* worker) 2786 { 2787 char* p = skipwhite(cmd); 2788 /* compare command */ 2789 if(cmdcmp(p, "stop", 4)) { 2790 do_stop(ssl, rc); 2791 return; 2792 } else if(cmdcmp(p, "reload", 6)) { 2793 do_reload(ssl, rc); 2794 return; 2795 } else if(cmdcmp(p, "stats_noreset", 13)) { 2796 do_stats(ssl, rc, 0); 2797 return; 2798 } else if(cmdcmp(p, "stats", 5)) { 2799 do_stats(ssl, rc, 1); 2800 return; 2801 } else if(cmdcmp(p, "status", 6)) { 2802 do_status(ssl, worker); 2803 return; 2804 } else if(cmdcmp(p, "dump_cache", 10)) { 2805 (void)dump_cache(ssl, worker); 2806 return; 2807 } else if(cmdcmp(p, "load_cache", 10)) { 2808 if(load_cache(ssl, worker)) send_ok(ssl); 2809 return; 2810 } else if(cmdcmp(p, "list_forwards", 13)) { 2811 do_list_forwards(ssl, worker); 2812 return; 2813 } else if(cmdcmp(p, "list_stubs", 10)) { 2814 do_list_stubs(ssl, worker); 2815 return; 2816 } else if(cmdcmp(p, "list_insecure", 13)) { 2817 do_insecure_list(ssl, worker); 2818 return; 2819 } else if(cmdcmp(p, "list_local_zones", 16)) { 2820 do_list_local_zones(ssl, worker->daemon->local_zones); 2821 return; 2822 } else if(cmdcmp(p, "list_local_data", 15)) { 2823 do_list_local_data(ssl, worker, worker->daemon->local_zones); 2824 return; 2825 } else if(cmdcmp(p, "view_list_local_zones", 21)) { 2826 do_view_list_local_zones(ssl, worker, skipwhite(p+21)); 2827 return; 2828 } else if(cmdcmp(p, "view_list_local_data", 20)) { 2829 do_view_list_local_data(ssl, worker, skipwhite(p+20)); 2830 return; 2831 } else if(cmdcmp(p, "ratelimit_list", 14)) { 2832 do_ratelimit_list(ssl, worker, p+14); 2833 return; 2834 } else if(cmdcmp(p, "ip_ratelimit_list", 17)) { 2835 do_ip_ratelimit_list(ssl, worker, p+17); 2836 return; 2837 } else if(cmdcmp(p, "list_auth_zones", 15)) { 2838 do_list_auth_zones(ssl, worker->env.auth_zones); 2839 return; 2840 } else if(cmdcmp(p, "stub_add", 8)) { 2841 /* must always distribute this cmd */ 2842 if(rc) distribute_cmd(rc, ssl, cmd); 2843 do_stub_add(ssl, worker, skipwhite(p+8)); 2844 return; 2845 } else if(cmdcmp(p, "stub_remove", 11)) { 2846 /* must always distribute this cmd */ 2847 if(rc) distribute_cmd(rc, ssl, cmd); 2848 do_stub_remove(ssl, worker, skipwhite(p+11)); 2849 return; 2850 } else if(cmdcmp(p, "forward_add", 11)) { 2851 /* must always distribute this cmd */ 2852 if(rc) distribute_cmd(rc, ssl, cmd); 2853 do_forward_add(ssl, worker, skipwhite(p+11)); 2854 return; 2855 } else if(cmdcmp(p, "forward_remove", 14)) { 2856 /* must always distribute this cmd */ 2857 if(rc) distribute_cmd(rc, ssl, cmd); 2858 do_forward_remove(ssl, worker, skipwhite(p+14)); 2859 return; 2860 } else if(cmdcmp(p, "insecure_add", 12)) { 2861 /* must always distribute this cmd */ 2862 if(rc) distribute_cmd(rc, ssl, cmd); 2863 do_insecure_add(ssl, worker, skipwhite(p+12)); 2864 return; 2865 } else if(cmdcmp(p, "insecure_remove", 15)) { 2866 /* must always distribute this cmd */ 2867 if(rc) distribute_cmd(rc, ssl, cmd); 2868 do_insecure_remove(ssl, worker, skipwhite(p+15)); 2869 return; 2870 } else if(cmdcmp(p, "forward", 7)) { 2871 /* must always distribute this cmd */ 2872 if(rc) distribute_cmd(rc, ssl, cmd); 2873 do_forward(ssl, worker, skipwhite(p+7)); 2874 return; 2875 } else if(cmdcmp(p, "flush_stats", 11)) { 2876 /* must always distribute this cmd */ 2877 if(rc) distribute_cmd(rc, ssl, cmd); 2878 do_flush_stats(ssl, worker); 2879 return; 2880 } else if(cmdcmp(p, "flush_requestlist", 17)) { 2881 /* must always distribute this cmd */ 2882 if(rc) distribute_cmd(rc, ssl, cmd); 2883 do_flush_requestlist(ssl, worker); 2884 return; 2885 } else if(cmdcmp(p, "lookup", 6)) { 2886 do_lookup(ssl, worker, skipwhite(p+6)); 2887 return; 2888 } 2889 2890 #ifdef THREADS_DISABLED 2891 /* other processes must execute the command as well */ 2892 /* commands that should not be distributed, returned above. */ 2893 if(rc) { /* only if this thread is the master (rc) thread */ 2894 /* done before the code below, which may split the string */ 2895 distribute_cmd(rc, ssl, cmd); 2896 } 2897 #endif 2898 if(cmdcmp(p, "verbosity", 9)) { 2899 do_verbosity(ssl, skipwhite(p+9)); 2900 } else if(cmdcmp(p, "local_zone_remove", 17)) { 2901 do_zone_remove(ssl, worker->daemon->local_zones, skipwhite(p+17)); 2902 } else if(cmdcmp(p, "local_zones_remove", 18)) { 2903 do_zones_remove(ssl, worker->daemon->local_zones); 2904 } else if(cmdcmp(p, "local_zone", 10)) { 2905 do_zone_add(ssl, worker->daemon->local_zones, skipwhite(p+10)); 2906 } else if(cmdcmp(p, "local_zones", 11)) { 2907 do_zones_add(ssl, worker->daemon->local_zones); 2908 } else if(cmdcmp(p, "local_data_remove", 17)) { 2909 do_data_remove(ssl, worker->daemon->local_zones, skipwhite(p+17)); 2910 } else if(cmdcmp(p, "local_datas_remove", 18)) { 2911 do_datas_remove(ssl, worker->daemon->local_zones); 2912 } else if(cmdcmp(p, "local_data", 10)) { 2913 do_data_add(ssl, worker->daemon->local_zones, skipwhite(p+10)); 2914 } else if(cmdcmp(p, "local_datas", 11)) { 2915 do_datas_add(ssl, worker->daemon->local_zones); 2916 } else if(cmdcmp(p, "view_local_zone_remove", 22)) { 2917 do_view_zone_remove(ssl, worker, skipwhite(p+22)); 2918 } else if(cmdcmp(p, "view_local_zone", 15)) { 2919 do_view_zone_add(ssl, worker, skipwhite(p+15)); 2920 } else if(cmdcmp(p, "view_local_data_remove", 22)) { 2921 do_view_data_remove(ssl, worker, skipwhite(p+22)); 2922 } else if(cmdcmp(p, "view_local_data", 15)) { 2923 do_view_data_add(ssl, worker, skipwhite(p+15)); 2924 } else if(cmdcmp(p, "flush_zone", 10)) { 2925 do_flush_zone(ssl, worker, skipwhite(p+10)); 2926 } else if(cmdcmp(p, "flush_type", 10)) { 2927 do_flush_type(ssl, worker, skipwhite(p+10)); 2928 } else if(cmdcmp(p, "flush_infra", 11)) { 2929 do_flush_infra(ssl, worker, skipwhite(p+11)); 2930 } else if(cmdcmp(p, "flush", 5)) { 2931 do_flush_name(ssl, worker, skipwhite(p+5)); 2932 } else if(cmdcmp(p, "dump_requestlist", 16)) { 2933 do_dump_requestlist(ssl, worker); 2934 } else if(cmdcmp(p, "dump_infra", 10)) { 2935 do_dump_infra(ssl, worker); 2936 } else if(cmdcmp(p, "log_reopen", 10)) { 2937 do_log_reopen(ssl, worker); 2938 } else if(cmdcmp(p, "set_option", 10)) { 2939 do_set_option(ssl, worker, skipwhite(p+10)); 2940 } else if(cmdcmp(p, "get_option", 10)) { 2941 do_get_option(ssl, worker, skipwhite(p+10)); 2942 } else if(cmdcmp(p, "flush_bogus", 11)) { 2943 do_flush_bogus(ssl, worker); 2944 } else if(cmdcmp(p, "flush_negative", 14)) { 2945 do_flush_negative(ssl, worker); 2946 } else { 2947 (void)ssl_printf(ssl, "error unknown command '%s'\n", p); 2948 } 2949 } 2950 2951 void 2952 daemon_remote_exec(struct worker* worker) 2953 { 2954 /* read the cmd string */ 2955 uint8_t* msg = NULL; 2956 uint32_t len = 0; 2957 if(!tube_read_msg(worker->cmd, &msg, &len, 0)) { 2958 log_err("daemon_remote_exec: tube_read_msg failed"); 2959 return; 2960 } 2961 verbose(VERB_ALGO, "remote exec distributed: %s", (char*)msg); 2962 execute_cmd(NULL, NULL, (char*)msg, worker); 2963 free(msg); 2964 } 2965 2966 /** handle remote control request */ 2967 static void 2968 handle_req(struct daemon_remote* rc, struct rc_state* s, SSL* ssl) 2969 { 2970 int r; 2971 char pre[10]; 2972 char magic[7]; 2973 char buf[1024]; 2974 #ifdef USE_WINSOCK 2975 /* makes it possible to set the socket blocking again. */ 2976 /* basically removes it from winsock_event ... */ 2977 WSAEventSelect(s->c->fd, NULL, 0); 2978 #endif 2979 fd_set_block(s->c->fd); 2980 2981 /* try to read magic UBCT[version]_space_ string */ 2982 ERR_clear_error(); 2983 if((r=SSL_read(ssl, magic, (int)sizeof(magic)-1)) <= 0) { 2984 if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) 2985 return; 2986 log_crypto_err("could not SSL_read"); 2987 return; 2988 } 2989 magic[6] = 0; 2990 if( r != 6 || strncmp(magic, "UBCT", 4) != 0) { 2991 verbose(VERB_QUERY, "control connection has bad magic string"); 2992 /* probably wrong tool connected, ignore it completely */ 2993 return; 2994 } 2995 2996 /* read the command line */ 2997 if(!ssl_read_line(ssl, buf, sizeof(buf))) { 2998 return; 2999 } 3000 snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION); 3001 if(strcmp(magic, pre) != 0) { 3002 verbose(VERB_QUERY, "control connection had bad " 3003 "version %s, cmd: %s", magic, buf); 3004 ssl_printf(ssl, "error version mismatch\n"); 3005 return; 3006 } 3007 verbose(VERB_DETAIL, "control cmd: %s", buf); 3008 3009 /* figure out what to do */ 3010 execute_cmd(rc, ssl, buf, rc->worker); 3011 } 3012 3013 int remote_control_callback(struct comm_point* c, void* arg, int err, 3014 struct comm_reply* ATTR_UNUSED(rep)) 3015 { 3016 struct rc_state* s = (struct rc_state*)arg; 3017 struct daemon_remote* rc = s->rc; 3018 int r; 3019 if(err != NETEVENT_NOERROR) { 3020 if(err==NETEVENT_TIMEOUT) 3021 log_err("remote control timed out"); 3022 clean_point(rc, s); 3023 return 0; 3024 } 3025 /* (continue to) setup the SSL connection */ 3026 ERR_clear_error(); 3027 r = SSL_do_handshake(s->ssl); 3028 if(r != 1) { 3029 int r2 = SSL_get_error(s->ssl, r); 3030 if(r2 == SSL_ERROR_WANT_READ) { 3031 if(s->shake_state == rc_hs_read) { 3032 /* try again later */ 3033 return 0; 3034 } 3035 s->shake_state = rc_hs_read; 3036 comm_point_listen_for_rw(c, 1, 0); 3037 return 0; 3038 } else if(r2 == SSL_ERROR_WANT_WRITE) { 3039 if(s->shake_state == rc_hs_write) { 3040 /* try again later */ 3041 return 0; 3042 } 3043 s->shake_state = rc_hs_write; 3044 comm_point_listen_for_rw(c, 0, 1); 3045 return 0; 3046 } else { 3047 if(r == 0) 3048 log_err("remote control connection closed prematurely"); 3049 log_addr(1, "failed connection from", 3050 &s->c->repinfo.addr, s->c->repinfo.addrlen); 3051 log_crypto_err("remote control failed ssl"); 3052 clean_point(rc, s); 3053 return 0; 3054 } 3055 } 3056 s->shake_state = rc_none; 3057 3058 /* once handshake has completed, check authentication */ 3059 if (!rc->use_cert) { 3060 verbose(VERB_ALGO, "unauthenticated remote control connection"); 3061 } else if(SSL_get_verify_result(s->ssl) == X509_V_OK) { 3062 X509* x = SSL_get_peer_certificate(s->ssl); 3063 if(!x) { 3064 verbose(VERB_DETAIL, "remote control connection " 3065 "provided no client certificate"); 3066 clean_point(rc, s); 3067 return 0; 3068 } 3069 verbose(VERB_ALGO, "remote control connection authenticated"); 3070 X509_free(x); 3071 } else { 3072 verbose(VERB_DETAIL, "remote control connection failed to " 3073 "authenticate with client certificate"); 3074 clean_point(rc, s); 3075 return 0; 3076 } 3077 3078 /* if OK start to actually handle the request */ 3079 handle_req(rc, s, s->ssl); 3080 3081 verbose(VERB_ALGO, "remote control operation completed"); 3082 clean_point(rc, s); 3083 return 0; 3084 } 3085