Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.8 KiB

  1. /* $OpenBSD: ntp_dns.c,v 1.3 2010/05/26 13:56:08 nicm Exp $ */
  2. /*
  3. * Copyright (c) 2003-2008 Henning Brauer <henning@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/param.h>
  18. #include <sys/time.h>
  19. #include <errno.h>
  20. #include <poll.h>
  21. #include <signal.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include "ntpd.h"
  26. volatile sig_atomic_t quit_dns = 0;
  27. struct imsgbuf *ibuf_dns;
  28. void sighdlr_dns(int);
  29. int dns_dispatch_imsg(void);
  30. void
  31. sighdlr_dns(int sig)
  32. {
  33. switch (sig) {
  34. case SIGTERM:
  35. case SIGINT:
  36. quit_dns = 1;
  37. break;
  38. }
  39. }
  40. pid_t
  41. ntp_dns(int pipe_ntp[2], struct ntpd_conf *nconf, struct passwd *pw)
  42. {
  43. pid_t pid;
  44. struct pollfd pfd[1];
  45. int nfds;
  46. switch (pid = fork()) {
  47. case -1:
  48. fatal("cannot fork");
  49. break;
  50. case 0:
  51. break;
  52. default:
  53. return (pid);
  54. }
  55. /* in this case the parent didn't init logging and didn't daemonize */
  56. if (nconf->settime && !nconf->debug) {
  57. log_init(nconf->debug);
  58. if (setsid() == -1)
  59. fatal("setsid");
  60. }
  61. setproctitle("dns engine");
  62. close(pipe_ntp[0]);
  63. if (setgroups(1, &pw->pw_gid) ||
  64. setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
  65. setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
  66. fatal("can't drop privileges");
  67. signal(SIGTERM, sighdlr_dns);
  68. signal(SIGINT, sighdlr_dns);
  69. signal(SIGHUP, sighdlr_dns);
  70. if ((ibuf_dns = malloc(sizeof(struct imsgbuf))) == NULL)
  71. fatal(NULL);
  72. imsg_init(ibuf_dns, pipe_ntp[1]);
  73. while (quit_dns == 0) {
  74. pfd[0].fd = ibuf_dns->fd;
  75. pfd[0].events = POLLIN;
  76. if (ibuf_dns->w.queued)
  77. pfd[0].events |= POLLOUT;
  78. if ((nfds = poll(pfd, 1, INFTIM)) == -1)
  79. if (errno != EINTR) {
  80. log_warn("poll error");
  81. quit_dns = 1;
  82. }
  83. if (nfds > 0 && (pfd[0].revents & POLLOUT))
  84. if (msgbuf_write(&ibuf_dns->w) < 0) {
  85. log_warn("pipe write error (to ntp engine)");
  86. quit_dns = 1;
  87. }
  88. if (nfds > 0 && pfd[0].revents & POLLIN) {
  89. nfds--;
  90. if (dns_dispatch_imsg() == -1)
  91. quit_dns = 1;
  92. }
  93. }
  94. msgbuf_clear(&ibuf_dns->w);
  95. free(ibuf_dns);
  96. _exit(0);
  97. }
  98. int
  99. dns_dispatch_imsg(void)
  100. {
  101. struct imsg imsg;
  102. int n, cnt;
  103. char *name;
  104. struct ntp_addr *h, *hn;
  105. struct ibuf *buf;
  106. if ((n = imsg_read(ibuf_dns)) == -1)
  107. return (-1);
  108. if (n == 0) { /* connection closed */
  109. log_warnx("dispatch_imsg in main: pipe closed");
  110. return (-1);
  111. }
  112. for (;;) {
  113. if ((n = imsg_get(ibuf_dns, &imsg)) == -1)
  114. return (-1);
  115. if (n == 0)
  116. break;
  117. switch (imsg.hdr.type) {
  118. case IMSG_HOST_DNS:
  119. name = imsg.data;
  120. if (imsg.hdr.len < 1 + IMSG_HEADER_SIZE)
  121. fatalx("invalid IMSG_HOST_DNS received");
  122. imsg.hdr.len -= 1 + IMSG_HEADER_SIZE;
  123. if (name[imsg.hdr.len] != '\0' ||
  124. strlen(name) != imsg.hdr.len)
  125. fatalx("invalid IMSG_HOST_DNS received");
  126. if ((cnt = host_dns(name, &hn)) == -1)
  127. break;
  128. buf = imsg_create(ibuf_dns, IMSG_HOST_DNS,
  129. imsg.hdr.peerid, 0,
  130. cnt * sizeof(struct sockaddr_storage));
  131. if (buf == NULL)
  132. break;
  133. if (cnt > 0)
  134. for (h = hn; h != NULL; h = h->next)
  135. imsg_add(buf, &h->ss, sizeof(h->ss));
  136. imsg_close(ibuf_dns, buf);
  137. break;
  138. default:
  139. break;
  140. }
  141. imsg_free(&imsg);
  142. }
  143. return (0);
  144. }