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.

187 lines
4.5 KiB

  1. /* $OpenBSD: config.c,v 1.19 2006/05/27 17:01:07 henning 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 <arpa/nameser.h>
  22. #include <errno.h>
  23. #include <resolv.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include "ntpd.h"
  28. struct ntp_addr *host_v4(const char *);
  29. struct ntp_addr *host_v6(const char *);
  30. static u_int32_t maxid = 0;
  31. int
  32. host(const char *s, struct ntp_addr **hn)
  33. {
  34. struct ntp_addr *h = NULL;
  35. if (!strcmp(s, "*"))
  36. if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
  37. fatal(NULL);
  38. /* IPv4 address? */
  39. if (h == NULL)
  40. h = host_v4(s);
  41. /* IPv6 address? */
  42. if (h == NULL)
  43. h = host_v6(s);
  44. if (h == NULL)
  45. return (0);
  46. *hn = h;
  47. return (1);
  48. }
  49. struct ntp_addr *
  50. host_v4(const char *s)
  51. {
  52. struct in_addr ina;
  53. struct sockaddr_in *sa_in;
  54. struct ntp_addr *h;
  55. bzero(&ina, sizeof(struct in_addr));
  56. if (inet_pton(AF_INET, s, &ina) != 1)
  57. return (NULL);
  58. if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
  59. fatal(NULL);
  60. sa_in = (struct sockaddr_in *)&h->ss;
  61. sa_in->sin_len = sizeof(struct sockaddr_in);
  62. sa_in->sin_family = AF_INET;
  63. sa_in->sin_addr.s_addr = ina.s_addr;
  64. return (h);
  65. }
  66. struct ntp_addr *
  67. host_v6(const char *s)
  68. {
  69. struct addrinfo hints, *res;
  70. struct sockaddr_in6 *sa_in6;
  71. struct ntp_addr *h = NULL;
  72. bzero(&hints, sizeof(hints));
  73. hints.ai_family = AF_INET6;
  74. hints.ai_socktype = SOCK_DGRAM; /*dummy*/
  75. hints.ai_flags = AI_NUMERICHOST;
  76. if (getaddrinfo(s, "0", &hints, &res) == 0) {
  77. if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
  78. fatal(NULL);
  79. sa_in6 = (struct sockaddr_in6 *)&h->ss;
  80. sa_in6->sin6_len = sizeof(struct sockaddr_in6);
  81. sa_in6->sin6_family = AF_INET6;
  82. memcpy(&sa_in6->sin6_addr,
  83. &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
  84. sizeof(sa_in6->sin6_addr));
  85. sa_in6->sin6_scope_id =
  86. ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
  87. freeaddrinfo(res);
  88. }
  89. return (h);
  90. }
  91. int
  92. host_dns(const char *s, struct ntp_addr **hn)
  93. {
  94. struct addrinfo hints, *res0, *res;
  95. int error, cnt = 0;
  96. struct sockaddr_in *sa_in;
  97. struct sockaddr_in6 *sa_in6;
  98. struct ntp_addr *h, *hh = NULL;
  99. bzero(&hints, sizeof(hints));
  100. hints.ai_family = PF_UNSPEC;
  101. hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
  102. error = getaddrinfo(s, NULL, &hints, &res0);
  103. if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
  104. return (0);
  105. if (error) {
  106. log_warnx("could not parse \"%s\": %s", s,
  107. gai_strerror(error));
  108. return (-1);
  109. }
  110. for (res = res0; res && cnt < MAX_SERVERS_DNS; res = res->ai_next) {
  111. if (res->ai_family != AF_INET &&
  112. res->ai_family != AF_INET6)
  113. continue;
  114. if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL)
  115. fatal(NULL);
  116. h->ss.ss_family = res->ai_family;
  117. if (res->ai_family == AF_INET) {
  118. sa_in = (struct sockaddr_in *)&h->ss;
  119. sa_in->sin_len = sizeof(struct sockaddr_in);
  120. sa_in->sin_addr.s_addr = ((struct sockaddr_in *)
  121. res->ai_addr)->sin_addr.s_addr;
  122. } else {
  123. sa_in6 = (struct sockaddr_in6 *)&h->ss;
  124. sa_in6->sin6_len = sizeof(struct sockaddr_in6);
  125. memcpy(&sa_in6->sin6_addr, &((struct sockaddr_in6 *)
  126. res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
  127. }
  128. h->next = hh;
  129. hh = h;
  130. cnt++;
  131. }
  132. freeaddrinfo(res0);
  133. *hn = hh;
  134. return (cnt);
  135. }
  136. struct ntp_peer *
  137. new_peer(void)
  138. {
  139. struct ntp_peer *p;
  140. if ((p = calloc(1, sizeof(struct ntp_peer))) == NULL)
  141. fatal("new_peer calloc");
  142. p->id = ++maxid;
  143. return (p);
  144. }
  145. struct ntp_conf_sensor *
  146. new_sensor(char *device)
  147. {
  148. struct ntp_conf_sensor *s;
  149. if ((s = calloc(1, sizeof(struct ntp_conf_sensor))) == NULL)
  150. fatal("new_sensor calloc");
  151. if ((s->device = strdup(device)) == NULL)
  152. fatal("new_sensor strdup");
  153. return (s);
  154. }