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.

183 lines
4.0 KiB

  1. /* $OpenBSD: config.c,v 1.30 2019/05/28 06:49:46 otto Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 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 USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <sys/stat.h>
  20. #include <netinet/in.h>
  21. #include <errno.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <resolv.h>
  25. #include <unistd.h>
  26. #include "ntpd.h"
  27. struct ntp_addr *host_ip(const char *);
  28. int host_dns1(const char *, struct ntp_addr **, int);
  29. static u_int32_t maxid = 0;
  30. static u_int32_t constraint_maxid = 0;
  31. void
  32. host(const char *s, struct ntp_addr **hn)
  33. {
  34. struct ntp_addr *h;
  35. if (!strcmp(s, "*")) {
  36. if ((h = calloc(1, sizeof(*h))) == NULL)
  37. fatal(NULL);
  38. } else {
  39. if ((h = host_ip(s)) == NULL)
  40. return;
  41. }
  42. *hn = h;
  43. }
  44. struct ntp_addr *
  45. host_ip(const char *s)
  46. {
  47. struct addrinfo hints, *res;
  48. struct ntp_addr *h = NULL;
  49. memset(&hints, 0, sizeof(hints));
  50. hints.ai_family = AF_UNSPEC;
  51. hints.ai_socktype = SOCK_DGRAM; /*dummy*/
  52. hints.ai_flags = AI_NUMERICHOST;
  53. if (getaddrinfo(s, "0", &hints, &res) == 0) {
  54. if (res->ai_family == AF_INET ||
  55. res->ai_family == AF_INET6) {
  56. if ((h = calloc(1, sizeof(*h))) == NULL)
  57. fatal(NULL);
  58. memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
  59. }
  60. freeaddrinfo(res);
  61. }
  62. return (h);
  63. }
  64. void
  65. host_dns_free(struct ntp_addr *hn)
  66. {
  67. struct ntp_addr *h = hn, *tmp;
  68. while (h) {
  69. tmp = h;
  70. h = h->next;
  71. free(tmp);
  72. }
  73. }
  74. int
  75. host_dns1(const char *s, struct ntp_addr **hn, int notauth)
  76. {
  77. struct addrinfo hints, *res0, *res;
  78. int error, cnt = 0;
  79. struct ntp_addr *h, *hh = NULL;
  80. memset(&hints, 0, sizeof(hints));
  81. hints.ai_family = AF_UNSPEC;
  82. hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
  83. /* ntpd MUST NOT use AI_ADDRCONFIG here */
  84. error = getaddrinfo(s, NULL, &hints, &res0);
  85. if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
  86. return (0);
  87. if (error) {
  88. log_warnx("could not parse \"%s\": %s", s,
  89. gai_strerror(error));
  90. return (-1);
  91. }
  92. for (res = res0; res && cnt < MAX_SERVERS_DNS; res = res->ai_next) {
  93. if (res->ai_family != AF_INET &&
  94. res->ai_family != AF_INET6)
  95. continue;
  96. if ((h = calloc(1, sizeof(*h))) == NULL)
  97. fatal(NULL);
  98. memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
  99. h->notauth = notauth;
  100. h->next = hh;
  101. hh = h;
  102. cnt++;
  103. }
  104. freeaddrinfo(res0);
  105. *hn = hh;
  106. return (cnt);
  107. }
  108. int
  109. host_dns(const char *s, struct ntp_addr **hn)
  110. {
  111. int error, save_opts;
  112. log_debug("trying to resolve %s", s);
  113. error = host_dns1(s, hn, 0);
  114. if (error <= 0) {
  115. log_debug("no luck, trying to resolve %s without checking", s);
  116. save_opts = _res.options;
  117. _res.options |= RES_USE_CD;
  118. error = host_dns1(s, hn, 1);
  119. _res.options = save_opts;
  120. }
  121. log_debug("resolve %s done: %d", s, error);
  122. return error;
  123. }
  124. struct ntp_peer *
  125. new_peer(void)
  126. {
  127. struct ntp_peer *p;
  128. if ((p = calloc(1, sizeof(struct ntp_peer))) == NULL)
  129. fatal("new_peer calloc");
  130. p->id = ++maxid;
  131. return (p);
  132. }
  133. struct ntp_conf_sensor *
  134. new_sensor(char *device)
  135. {
  136. struct ntp_conf_sensor *s;
  137. if ((s = calloc(1, sizeof(struct ntp_conf_sensor))) == NULL)
  138. fatal("new_sensor calloc");
  139. if ((s->device = strdup(device)) == NULL)
  140. fatal("new_sensor strdup");
  141. return (s);
  142. }
  143. struct constraint *
  144. new_constraint(void)
  145. {
  146. struct constraint *p;
  147. if ((p = calloc(1, sizeof(struct constraint))) == NULL)
  148. fatal("new_constraint calloc");
  149. p->id = ++constraint_maxid;
  150. p->fd = -1;
  151. return (p);
  152. }