1 /* $OpenBSD: unwind.c,v 1.47 2020/05/25 16:52:15 florian Exp $ */ 2 3 /* 4 * Copyright (c) 2018 Florian Obser <florian@openbsd.org> 5 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 6 * Copyright (c) 2004 Esben Norby <norby@openbsd.org> 7 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 #include <sys/types.h> 22 #include <sys/queue.h> 23 #include <sys/socket.h> 24 #include <sys/stat.h> 25 #include <sys/syslog.h> 26 #include <sys/wait.h> 27 28 #include <net/if.h> 29 #include <net/route.h> 30 31 #include <err.h> 32 #include <errno.h> 33 #include <event.h> 34 #include <fcntl.h> 35 #include <imsg.h> 36 #include <netdb.h> 37 #include <asr.h> 38 #include <pwd.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <signal.h> 43 #include <unistd.h> 44 45 #include "log.h" 46 #include "unwind.h" 47 #include "frontend.h" 48 #include "resolver.h" 49 #include "control.h" 50 51 #define TRUST_ANCHOR_FILE "/var/db/unwind.key" 52 53 __dead void usage(void); 54 __dead void main_shutdown(void); 55 56 void main_sig_handler(int, short, void *); 57 58 static pid_t start_child(int, char *, int, int, int); 59 60 void main_dispatch_frontend(int, short, void *); 61 void main_dispatch_resolver(int, short, void *); 62 63 static int main_imsg_send_ipc_sockets(struct imsgbuf *, struct imsgbuf *); 64 static int main_imsg_send_config(struct uw_conf *); 65 66 int main_reload(void); 67 int main_sendall(enum imsg_type, void *, uint16_t); 68 void open_ports(void); 69 void solicit_dns_proposals(void); 70 void send_blocklist_fd(void); 71 72 struct uw_conf *main_conf; 73 struct imsgev *iev_frontend; 74 struct imsgev *iev_resolver; 75 char *conffile; 76 77 pid_t frontend_pid; 78 pid_t resolver_pid; 79 80 uint32_t cmd_opts; 81 82 int routesock; 83 84 void 85 main_sig_handler(int sig, short event, void *arg) 86 { 87 /* 88 * Normal signal handler rules don't apply because libevent 89 * decouples for us. 90 */ 91 92 switch (sig) { 93 case SIGTERM: 94 case SIGINT: 95 main_shutdown(); 96 break; 97 case SIGHUP: 98 if (main_reload() == -1) 99 log_warnx("configuration reload failed"); 100 else 101 log_debug("configuration reloaded"); 102 break; 103 default: 104 fatalx("unexpected signal"); 105 } 106 } 107 108 __dead void 109 usage(void) 110 { 111 extern char *__progname; 112 113 fprintf(stderr, "usage: %s [-dnv] [-f file] [-s socket]\n", 114 __progname); 115 exit(1); 116 } 117 118 int 119 main(int argc, char *argv[]) 120 { 121 struct event ev_sigint, ev_sigterm, ev_sighup; 122 int ch, debug = 0, resolver_flag = 0, frontend_flag = 0; 123 int frontend_routesock, rtfilter; 124 int pipe_main2frontend[2], pipe_main2resolver[2]; 125 int control_fd, ta_fd; 126 char *csock, *saved_argv0; 127 128 csock = UNWIND_SOCKET; 129 130 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */ 131 log_setverbose(1); 132 133 saved_argv0 = argv[0]; 134 if (saved_argv0 == NULL) 135 saved_argv0 = "unwind"; 136 137 while ((ch = getopt(argc, argv, "dEFf:ns:v")) != -1) { 138 switch (ch) { 139 case 'd': 140 debug = 1; 141 break; 142 case 'E': 143 resolver_flag = 1; 144 break; 145 case 'F': 146 frontend_flag = 1; 147 break; 148 case 'f': 149 conffile = optarg; 150 break; 151 case 'n': 152 cmd_opts |= OPT_NOACTION; 153 break; 154 case 's': 155 csock = optarg; 156 break; 157 case 'v': 158 if (cmd_opts & OPT_VERBOSE2) 159 cmd_opts |= OPT_VERBOSE3; 160 if (cmd_opts & OPT_VERBOSE) 161 cmd_opts |= OPT_VERBOSE2; 162 cmd_opts |= OPT_VERBOSE; 163 break; 164 default: 165 usage(); 166 } 167 } 168 169 argc -= optind; 170 argv += optind; 171 if (argc > 0 || (resolver_flag && frontend_flag)) 172 usage(); 173 174 if (resolver_flag) 175 resolver(debug, cmd_opts & (OPT_VERBOSE | OPT_VERBOSE2 | 176 OPT_VERBOSE3)); 177 else if (frontend_flag) 178 frontend(debug, cmd_opts & (OPT_VERBOSE | OPT_VERBOSE2 | 179 OPT_VERBOSE3)); 180 181 if ((main_conf = parse_config(conffile)) == NULL) 182 exit(1); 183 184 if (cmd_opts & OPT_NOACTION) { 185 if (cmd_opts & OPT_VERBOSE) 186 print_config(main_conf); 187 else 188 fprintf(stderr, "configuration OK\n"); 189 exit(0); 190 } 191 192 /* Check for root privileges. */ 193 if (geteuid()) 194 errx(1, "need root privileges"); 195 196 /* Check for assigned daemon user */ 197 if (getpwnam(UNWIND_USER) == NULL) 198 errx(1, "unknown user %s", UNWIND_USER); 199 200 log_init(debug, LOG_DAEMON); 201 log_setverbose(cmd_opts & (OPT_VERBOSE | OPT_VERBOSE2 | OPT_VERBOSE3)); 202 203 if (!debug) 204 daemon(1, 0); 205 206 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 207 PF_UNSPEC, pipe_main2frontend) == -1) 208 fatal("main2frontend socketpair"); 209 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 210 PF_UNSPEC, pipe_main2resolver) == -1) 211 fatal("main2resolver socketpair"); 212 213 /* Start children. */ 214 resolver_pid = start_child(PROC_RESOLVER, saved_argv0, 215 pipe_main2resolver[1], debug, cmd_opts & (OPT_VERBOSE | 216 OPT_VERBOSE2 | OPT_VERBOSE3)); 217 frontend_pid = start_child(PROC_FRONTEND, saved_argv0, 218 pipe_main2frontend[1], debug, cmd_opts & (OPT_VERBOSE | 219 OPT_VERBOSE2 | OPT_VERBOSE3)); 220 221 uw_process = PROC_MAIN; 222 log_procinit(log_procnames[uw_process]); 223 224 event_init(); 225 226 /* Setup signal handler. */ 227 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL); 228 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL); 229 signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL); 230 signal_add(&ev_sigint, NULL); 231 signal_add(&ev_sigterm, NULL); 232 signal_add(&ev_sighup, NULL); 233 signal(SIGPIPE, SIG_IGN); 234 235 /* Setup pipes to children. */ 236 237 if ((iev_frontend = malloc(sizeof(struct imsgev))) == NULL || 238 (iev_resolver = malloc(sizeof(struct imsgev))) == NULL) 239 fatal(NULL); 240 imsg_init(&iev_frontend->ibuf, pipe_main2frontend[0]); 241 iev_frontend->handler = main_dispatch_frontend; 242 imsg_init(&iev_resolver->ibuf, pipe_main2resolver[0]); 243 iev_resolver->handler = main_dispatch_resolver; 244 245 /* Setup event handlers for pipes. */ 246 iev_frontend->events = EV_READ; 247 event_set(&iev_frontend->ev, iev_frontend->ibuf.fd, 248 iev_frontend->events, iev_frontend->handler, iev_frontend); 249 event_add(&iev_frontend->ev, NULL); 250 251 iev_resolver->events = EV_READ; 252 event_set(&iev_resolver->ev, iev_resolver->ibuf.fd, 253 iev_resolver->events, iev_resolver->handler, iev_resolver); 254 event_add(&iev_resolver->ev, NULL); 255 256 if (main_imsg_send_ipc_sockets(&iev_frontend->ibuf, 257 &iev_resolver->ibuf)) 258 fatal("could not establish imsg links"); 259 260 open_ports(); 261 262 if ((control_fd = control_init(csock)) == -1) 263 fatalx("control socket setup failed"); 264 265 if ((frontend_routesock = socket(AF_ROUTE, SOCK_RAW | SOCK_CLOEXEC, 266 AF_INET)) == -1) 267 fatal("route socket"); 268 269 rtfilter = ROUTE_FILTER(RTM_IFINFO) | ROUTE_FILTER(RTM_PROPOSAL); 270 if (setsockopt(frontend_routesock, AF_ROUTE, ROUTE_MSGFILTER, 271 &rtfilter, sizeof(rtfilter)) == -1) 272 fatal("setsockopt(ROUTE_MSGFILTER)"); 273 274 if ((routesock = socket(AF_ROUTE, SOCK_RAW | SOCK_CLOEXEC | 275 SOCK_NONBLOCK, AF_INET6)) == -1) 276 fatal("route socket"); 277 shutdown(SHUT_RD, routesock); 278 279 if ((ta_fd = open(TRUST_ANCHOR_FILE, O_RDWR | O_CREAT, 0644)) == -1) 280 log_warn("%s", TRUST_ANCHOR_FILE); 281 282 /* receiver handles failed open correctly */ 283 main_imsg_compose_frontend_fd(IMSG_TAFD, 0, ta_fd); 284 285 main_imsg_compose_frontend_fd(IMSG_CONTROLFD, 0, control_fd); 286 main_imsg_compose_frontend_fd(IMSG_ROUTESOCK, 0, frontend_routesock); 287 main_imsg_send_config(main_conf); 288 289 if (main_conf->blocklist_file != NULL) 290 send_blocklist_fd(); 291 292 if (pledge("stdio rpath sendfd", NULL) == -1) 293 fatal("pledge"); 294 295 main_imsg_compose_frontend(IMSG_STARTUP, 0, NULL, 0); 296 main_imsg_compose_resolver(IMSG_STARTUP, 0, NULL, 0); 297 298 event_dispatch(); 299 300 main_shutdown(); 301 return (0); 302 } 303 304 __dead void 305 main_shutdown(void) 306 { 307 pid_t pid; 308 int status; 309 310 /* Close pipes. */ 311 msgbuf_clear(&iev_frontend->ibuf.w); 312 close(iev_frontend->ibuf.fd); 313 msgbuf_clear(&iev_resolver->ibuf.w); 314 close(iev_resolver->ibuf.fd); 315 316 config_clear(main_conf); 317 318 log_debug("waiting for children to terminate"); 319 do { 320 pid = wait(&status); 321 if (pid == -1) { 322 if (errno != EINTR && errno != ECHILD) 323 fatal("wait"); 324 } else if (WIFSIGNALED(status)) 325 log_warnx("%s terminated; signal %d", 326 (pid == resolver_pid) ? "resolver" : 327 "frontend", WTERMSIG(status)); 328 } while (pid != -1 || (pid == -1 && errno == EINTR)); 329 330 free(iev_frontend); 331 free(iev_resolver); 332 333 log_info("terminating"); 334 exit(0); 335 } 336 337 static pid_t 338 start_child(int p, char *argv0, int fd, int debug, int verbose) 339 { 340 char *argv[7]; 341 int argc = 0; 342 pid_t pid; 343 344 switch (pid = fork()) { 345 case -1: 346 fatal("cannot fork"); 347 case 0: 348 break; 349 default: 350 close(fd); 351 return (pid); 352 } 353 354 if (fd != 3) { 355 if (dup2(fd, 3) == -1) 356 fatal("cannot setup imsg fd"); 357 } else if (fcntl(fd, F_SETFD, 0) == -1) 358 fatal("cannot setup imsg fd"); 359 360 argv[argc++] = argv0; 361 switch (p) { 362 case PROC_MAIN: 363 fatalx("Can not start main process"); 364 case PROC_RESOLVER: 365 argv[argc++] = "-E"; 366 break; 367 case PROC_FRONTEND: 368 argv[argc++] = "-F"; 369 break; 370 } 371 if (debug) 372 argv[argc++] = "-d"; 373 if (verbose & OPT_VERBOSE) 374 argv[argc++] = "-v"; 375 if (verbose & OPT_VERBOSE2) 376 argv[argc++] = "-v"; 377 if (verbose & OPT_VERBOSE3) 378 argv[argc++] = "-v"; 379 argv[argc++] = NULL; 380 381 execvp(argv0, argv); 382 fatal("execvp"); 383 } 384 385 void 386 main_dispatch_frontend(int fd, short event, void *bula) 387 { 388 struct imsgev *iev = bula; 389 struct imsgbuf *ibuf; 390 struct imsg imsg; 391 ssize_t n; 392 int shut = 0, verbose; 393 394 ibuf = &iev->ibuf; 395 396 if (event & EV_READ) { 397 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 398 fatal("imsg_read error"); 399 if (n == 0) /* Connection closed. */ 400 shut = 1; 401 } 402 if (event & EV_WRITE) { 403 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 404 fatal("msgbuf_write"); 405 if (n == 0) /* Connection closed. */ 406 shut = 1; 407 } 408 409 for (;;) { 410 if ((n = imsg_get(ibuf, &imsg)) == -1) 411 fatal("imsg_get"); 412 if (n == 0) /* No more messages. */ 413 break; 414 415 switch (imsg.hdr.type) { 416 case IMSG_STARTUP_DONE: 417 solicit_dns_proposals(); 418 break; 419 case IMSG_CTL_RELOAD: 420 if (main_reload() == -1) 421 log_warnx("configuration reload failed"); 422 else 423 log_warnx("configuration reloaded"); 424 break; 425 case IMSG_CTL_LOG_VERBOSE: 426 if (IMSG_DATA_SIZE(imsg) != sizeof(verbose)) 427 fatalx("%s: IMSG_CTL_LOG_VERBOSE wrong length: " 428 "%lu", __func__, IMSG_DATA_SIZE(imsg)); 429 memcpy(&verbose, imsg.data, sizeof(verbose)); 430 log_setverbose(verbose); 431 break; 432 default: 433 log_debug("%s: error handling imsg %d", __func__, 434 imsg.hdr.type); 435 break; 436 } 437 imsg_free(&imsg); 438 } 439 if (!shut) 440 imsg_event_add(iev); 441 else { 442 /* This pipe is dead. Remove its event handler */ 443 event_del(&iev->ev); 444 event_loopexit(NULL); 445 } 446 } 447 448 void 449 main_dispatch_resolver(int fd, short event, void *bula) 450 { 451 struct imsgev *iev = bula; 452 struct imsgbuf *ibuf; 453 struct imsg imsg; 454 ssize_t n; 455 int shut = 0; 456 457 ibuf = &iev->ibuf; 458 459 if (event & EV_READ) { 460 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 461 fatal("imsg_read error"); 462 if (n == 0) /* Connection closed. */ 463 shut = 1; 464 } 465 if (event & EV_WRITE) { 466 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 467 fatal("msgbuf_write"); 468 if (n == 0) /* Connection closed. */ 469 shut = 1; 470 } 471 472 for (;;) { 473 if ((n = imsg_get(ibuf, &imsg)) == -1) 474 fatal("imsg_get"); 475 if (n == 0) /* No more messages. */ 476 break; 477 478 switch (imsg.hdr.type) { 479 default: 480 log_debug("%s: error handling imsg %d", __func__, 481 imsg.hdr.type); 482 break; 483 } 484 imsg_free(&imsg); 485 } 486 if (!shut) 487 imsg_event_add(iev); 488 else { 489 /* This pipe is dead. Remove its event handler. */ 490 event_del(&iev->ev); 491 event_loopexit(NULL); 492 } 493 } 494 495 void 496 main_imsg_compose_frontend(int type, pid_t pid, void *data, uint16_t datalen) 497 { 498 if (iev_frontend) 499 imsg_compose_event(iev_frontend, type, 0, pid, -1, data, 500 datalen); 501 } 502 503 void 504 main_imsg_compose_frontend_fd(int type, pid_t pid, int fd) 505 { 506 if (iev_frontend) 507 imsg_compose_event(iev_frontend, type, 0, pid, fd, NULL, 0); 508 } 509 510 void 511 main_imsg_compose_resolver(int type, pid_t pid, void *data, uint16_t datalen) 512 { 513 if (iev_resolver) 514 imsg_compose_event(iev_resolver, type, 0, pid, -1, data, 515 datalen); 516 } 517 518 void 519 imsg_event_add(struct imsgev *iev) 520 { 521 iev->events = EV_READ; 522 if (iev->ibuf.w.queued) 523 iev->events |= EV_WRITE; 524 525 event_del(&iev->ev); 526 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev); 527 event_add(&iev->ev, NULL); 528 } 529 530 int 531 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid, 532 pid_t pid, int fd, void *data, uint16_t datalen) 533 { 534 int ret; 535 536 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, 537 datalen)) != -1) 538 imsg_event_add(iev); 539 540 return (ret); 541 } 542 543 static int 544 main_imsg_send_ipc_sockets(struct imsgbuf *frontend_buf, 545 struct imsgbuf *resolver_buf) 546 { 547 int pipe_frontend2resolver[2]; 548 549 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 550 PF_UNSPEC, pipe_frontend2resolver) == -1) 551 return (-1); 552 553 if (imsg_compose(frontend_buf, IMSG_SOCKET_IPC_RESOLVER, 0, 0, 554 pipe_frontend2resolver[0], NULL, 0) == -1) 555 return (-1); 556 if (imsg_compose(resolver_buf, IMSG_SOCKET_IPC_FRONTEND, 0, 0, 557 pipe_frontend2resolver[1], NULL, 0) == -1) 558 return (-1); 559 560 return (0); 561 } 562 563 int 564 main_reload(void) 565 { 566 struct uw_conf *xconf; 567 568 if ((xconf = parse_config(conffile)) == NULL) 569 return (-1); 570 571 if (main_imsg_send_config(xconf) == -1) 572 return (-1); 573 574 merge_config(main_conf, xconf); 575 576 if (main_conf->blocklist_file != NULL) 577 send_blocklist_fd(); 578 579 return (0); 580 } 581 582 int 583 main_imsg_send_config(struct uw_conf *xconf) 584 { 585 struct uw_forwarder *uw_forwarder; 586 struct force_tree_entry *force_entry; 587 588 /* Send fixed part of config to children. */ 589 if (main_sendall(IMSG_RECONF_CONF, xconf, sizeof(*xconf)) == -1) 590 return (-1); 591 592 if (xconf->blocklist_file != NULL) { 593 if (main_sendall(IMSG_RECONF_BLOCKLIST_FILE, 594 xconf->blocklist_file, strlen(xconf->blocklist_file) + 1) 595 == -1) 596 return (-1); 597 } 598 599 /* send static forwarders to children */ 600 TAILQ_FOREACH(uw_forwarder, &xconf->uw_forwarder_list, entry) { 601 if (main_sendall(IMSG_RECONF_FORWARDER, uw_forwarder, 602 sizeof(*uw_forwarder)) == -1) 603 return (-1); 604 } 605 606 /* send static DoT forwarders to children */ 607 TAILQ_FOREACH(uw_forwarder, &xconf->uw_dot_forwarder_list, 608 entry) { 609 if (main_sendall(IMSG_RECONF_DOT_FORWARDER, uw_forwarder, 610 sizeof(*uw_forwarder)) == -1) 611 return (-1); 612 } 613 RB_FOREACH(force_entry, force_tree, &xconf->force) { 614 if (main_sendall(IMSG_RECONF_FORCE, force_entry, 615 sizeof(*force_entry)) == -1) 616 return (-1); 617 } 618 619 /* Tell children the revised config is now complete. */ 620 if (main_sendall(IMSG_RECONF_END, NULL, 0) == -1) 621 return (-1); 622 623 return (0); 624 } 625 626 int 627 main_sendall(enum imsg_type type, void *buf, uint16_t len) 628 { 629 if (imsg_compose_event(iev_frontend, type, 0, 0, -1, buf, len) == -1) 630 return (-1); 631 if (imsg_compose_event(iev_resolver, type, 0, 0, -1, buf, len) == -1) 632 return (-1); 633 return (0); 634 } 635 636 void 637 merge_config(struct uw_conf *conf, struct uw_conf *xconf) 638 { 639 struct uw_forwarder *uw_forwarder; 640 struct force_tree_entry *n, *nxt; 641 642 /* Remove & discard existing forwarders. */ 643 while ((uw_forwarder = TAILQ_FIRST(&conf->uw_forwarder_list)) != 644 NULL) { 645 TAILQ_REMOVE(&conf->uw_forwarder_list, uw_forwarder, entry); 646 free(uw_forwarder); 647 } 648 while ((uw_forwarder = TAILQ_FIRST(&conf->uw_dot_forwarder_list)) != 649 NULL) { 650 TAILQ_REMOVE(&conf->uw_dot_forwarder_list, uw_forwarder, entry); 651 free(uw_forwarder); 652 } 653 654 /* Remove & discard existing force tree. */ 655 for (n = RB_MIN(force_tree, &conf->force); n != NULL; n = nxt) { 656 nxt = RB_NEXT(force_tree, &conf->force, n); 657 RB_REMOVE(force_tree, &conf->force, n); 658 free(n); 659 } 660 661 memcpy(&conf->res_pref, &xconf->res_pref, 662 sizeof(conf->res_pref)); 663 664 free(conf->blocklist_file); 665 conf->blocklist_file = xconf->blocklist_file; 666 conf->blocklist_log = xconf->blocklist_log; 667 668 /* Add new forwarders. */ 669 TAILQ_CONCAT(&conf->uw_forwarder_list, &xconf->uw_forwarder_list, 670 entry); 671 TAILQ_CONCAT(&conf->uw_dot_forwarder_list, 672 &xconf->uw_dot_forwarder_list, entry); 673 674 for (n = RB_MIN(force_tree, &xconf->force); n != NULL; n = nxt) { 675 nxt = RB_NEXT(force_tree, &xconf->force, n); 676 RB_REMOVE(force_tree, &xconf->force, n); 677 RB_INSERT(force_tree, &conf->force, n); 678 } 679 680 free(xconf); 681 } 682 683 struct uw_conf * 684 config_new_empty(void) 685 { 686 static enum uw_resolver_type default_res_pref[] = { 687 UW_RES_DOT, 688 UW_RES_ODOT_FORWARDER, 689 UW_RES_FORWARDER, 690 UW_RES_RECURSOR, 691 UW_RES_ODOT_DHCP, 692 UW_RES_DHCP, 693 UW_RES_ASR}; 694 struct uw_conf *xconf; 695 696 xconf = calloc(1, sizeof(*xconf)); 697 if (xconf == NULL) 698 fatal(NULL); 699 700 memcpy(&xconf->res_pref.types, &default_res_pref, 701 sizeof(default_res_pref)); 702 xconf->res_pref.len = nitems(default_res_pref); 703 704 TAILQ_INIT(&xconf->uw_forwarder_list); 705 TAILQ_INIT(&xconf->uw_dot_forwarder_list); 706 707 RB_INIT(&xconf->force); 708 709 return (xconf); 710 } 711 712 void 713 config_clear(struct uw_conf *conf) 714 { 715 struct uw_conf *xconf; 716 717 /* Merge current config with an empty config. */ 718 xconf = config_new_empty(); 719 merge_config(conf, xconf); 720 721 free(conf); 722 } 723 724 void 725 open_ports(void) 726 { 727 struct addrinfo hints, *res0; 728 int udp4sock = -1, udp6sock = -1, error; 729 730 memset(&hints, 0, sizeof(hints)); 731 hints.ai_family = AF_INET; 732 hints.ai_socktype = SOCK_DGRAM; 733 hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE; 734 735 error = getaddrinfo("127.0.0.1", "domain", &hints, &res0); 736 if (!error && res0) { 737 if ((udp4sock = socket(res0->ai_family, res0->ai_socktype, 738 res0->ai_protocol)) != -1) { 739 if (bind(udp4sock, res0->ai_addr, res0->ai_addrlen) 740 == -1) { 741 close(udp4sock); 742 udp4sock = -1; 743 } 744 } 745 } 746 if (res0) 747 freeaddrinfo(res0); 748 749 hints.ai_family = AF_INET6; 750 error = getaddrinfo("::1", "domain", &hints, &res0); 751 if (!error && res0) { 752 if ((udp6sock = socket(res0->ai_family, res0->ai_socktype, 753 res0->ai_protocol)) != -1) { 754 if (bind(udp6sock, res0->ai_addr, res0->ai_addrlen) 755 == -1) { 756 close(udp6sock); 757 udp6sock = -1; 758 } 759 } 760 } 761 if (res0) 762 freeaddrinfo(res0); 763 764 if (udp4sock == -1 && udp6sock == -1) 765 fatal("could not bind to 127.0.0.1 or ::1 on port 53"); 766 767 if (udp4sock != -1) 768 main_imsg_compose_frontend_fd(IMSG_UDP4SOCK, 0, udp4sock); 769 if (udp6sock != -1) 770 main_imsg_compose_frontend_fd(IMSG_UDP6SOCK, 0, udp6sock); 771 } 772 773 void 774 solicit_dns_proposals(void) 775 { 776 struct rt_msghdr rtm; 777 struct iovec iov[1]; 778 int iovcnt = 0; 779 780 memset(&rtm, 0, sizeof(rtm)); 781 782 rtm.rtm_version = RTM_VERSION; 783 rtm.rtm_type = RTM_PROPOSAL; 784 rtm.rtm_msglen = sizeof(rtm); 785 rtm.rtm_tableid = 0; 786 rtm.rtm_index = 0; 787 rtm.rtm_seq = arc4random(); 788 rtm.rtm_priority = RTP_PROPOSAL_SOLICIT; 789 790 iov[iovcnt].iov_base = &rtm; 791 iov[iovcnt++].iov_len = sizeof(rtm); 792 793 if (writev(routesock, iov, iovcnt) == -1) 794 log_warn("failed to send solicitation"); 795 } 796 797 void 798 send_blocklist_fd(void) 799 { 800 int bl_fd; 801 802 if ((bl_fd = open(main_conf->blocklist_file, O_RDONLY)) != -1) 803 main_imsg_compose_frontend_fd(IMSG_BLFD, 0, bl_fd); 804 else 805 log_warn("%s", main_conf->blocklist_file); 806 } 807 808 void 809 imsg_receive_config(struct imsg *imsg, struct uw_conf **xconf) 810 { 811 struct uw_conf *nconf; 812 struct uw_forwarder *uw_forwarder; 813 struct force_tree_entry *force_entry; 814 815 nconf = *xconf; 816 817 switch (imsg->hdr.type) { 818 case IMSG_RECONF_CONF: 819 if (nconf != NULL) 820 fatalx("%s: IMSG_RECONF_CONF already in " 821 "progress", __func__); 822 if (IMSG_DATA_SIZE(*imsg) != sizeof(struct uw_conf)) 823 fatalx("%s: IMSG_RECONF_CONF wrong length: %lu", 824 __func__, IMSG_DATA_SIZE(*imsg)); 825 if ((*xconf = malloc(sizeof(struct uw_conf))) == NULL) 826 fatal(NULL); 827 nconf = *xconf; 828 memcpy(nconf, imsg->data, sizeof(struct uw_conf)); 829 TAILQ_INIT(&nconf->uw_forwarder_list); 830 TAILQ_INIT(&nconf->uw_dot_forwarder_list); 831 RB_INIT(&nconf->force); 832 break; 833 case IMSG_RECONF_BLOCKLIST_FILE: 834 /* make sure this is a string */ 835 ((char *)imsg->data)[IMSG_DATA_SIZE(*imsg) - 1] = '\0'; 836 if ((nconf->blocklist_file = strdup(imsg->data)) == 837 NULL) 838 fatal("%s: strdup", __func__); 839 break; 840 case IMSG_RECONF_FORWARDER: 841 if (IMSG_DATA_SIZE(*imsg) != sizeof(struct uw_forwarder)) 842 fatalx("%s: IMSG_RECONF_FORWARDER wrong length:" 843 " %lu", __func__, IMSG_DATA_SIZE(*imsg)); 844 if ((uw_forwarder = malloc(sizeof(struct 845 uw_forwarder))) == NULL) 846 fatal(NULL); 847 memcpy(uw_forwarder, imsg->data, sizeof(struct 848 uw_forwarder)); 849 TAILQ_INSERT_TAIL(&nconf->uw_forwarder_list, 850 uw_forwarder, entry); 851 break; 852 case IMSG_RECONF_DOT_FORWARDER: 853 if (IMSG_DATA_SIZE(*imsg) != sizeof(struct uw_forwarder)) 854 fatalx("%s: IMSG_RECONF_DOT_FORWARDER wrong " 855 "length: %lu", __func__, 856 IMSG_DATA_SIZE(*imsg)); 857 if ((uw_forwarder = malloc(sizeof(struct 858 uw_forwarder))) == NULL) 859 fatal(NULL); 860 memcpy(uw_forwarder, imsg->data, sizeof(struct 861 uw_forwarder)); 862 TAILQ_INSERT_TAIL(&nconf->uw_dot_forwarder_list, 863 uw_forwarder, entry); 864 break; 865 case IMSG_RECONF_FORCE: 866 if (IMSG_DATA_SIZE(*imsg) != sizeof(struct force_tree_entry)) 867 fatalx("%s: IMSG_RECONF_FORCE wrong " 868 "length: %lu", __func__, 869 IMSG_DATA_SIZE(*imsg)); 870 if ((force_entry = malloc(sizeof(struct 871 force_tree_entry))) == NULL) 872 fatal(NULL); 873 memcpy(force_entry, imsg->data, sizeof(struct 874 force_tree_entry)); 875 RB_INSERT(force_tree, &nconf->force, force_entry); 876 break; 877 default: 878 log_debug("%s: error handling imsg %d", __func__, 879 imsg->hdr.type); 880 break; 881 } 882 } 883