digits_dots.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* Copyright (C) 1997-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <ctype.h>
  19. #include <wctype.h>
  20. #include <resolv/resolv-internal.h>
  21. #include <resolv/resolv_context.h>
  22. #include <netdb.h>
  23. #include <arpa/inet.h>
  24. #include "nsswitch.h"
  25. #ifdef USE_NSCD
  26. # include <nscd/nscd_proto.h>
  27. #endif
  28. int
  29. __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
  30. char **buffer, size_t *buffer_size,
  31. size_t buflen, struct hostent **result,
  32. enum nss_status *status, int af, int *h_errnop)
  33. {
  34. /* We have to test for the use of IPv6 which can only be done by
  35. examining `_res'. */
  36. struct resolv_context *ctx = __resolv_context_get ();
  37. if (ctx == NULL)
  38. {
  39. if (h_errnop)
  40. *h_errnop = NETDB_INTERNAL;
  41. if (buffer_size == NULL)
  42. *status = NSS_STATUS_TRYAGAIN;
  43. else
  44. *result = NULL;
  45. return -1;
  46. }
  47. int ret = __nss_hostname_digits_dots_context
  48. (ctx, name, resbuf, buffer, buffer_size, buflen,
  49. result, status, af, h_errnop);
  50. __resolv_context_put (ctx);
  51. return ret;
  52. }
  53. int
  54. __nss_hostname_digits_dots_context (struct resolv_context *ctx,
  55. const char *name, struct hostent *resbuf,
  56. char **buffer, size_t *buffer_size,
  57. size_t buflen, struct hostent **result,
  58. enum nss_status *status, int af, int *h_errnop)
  59. {
  60. int save;
  61. /*
  62. * disallow names consisting only of digits/dots, unless
  63. * they end in a dot.
  64. */
  65. if (isdigit (name[0]) || isxdigit (name[0]) || name[0] == ':')
  66. {
  67. const char *cp;
  68. char *hostname;
  69. typedef unsigned char host_addr_t[16];
  70. host_addr_t *host_addr;
  71. typedef char *host_addr_list_t[2];
  72. host_addr_list_t *h_addr_ptrs;
  73. char **h_alias_ptr;
  74. size_t size_needed;
  75. int addr_size;
  76. switch (af)
  77. {
  78. case AF_INET:
  79. addr_size = INADDRSZ;
  80. break;
  81. case AF_INET6:
  82. addr_size = IN6ADDRSZ;
  83. break;
  84. default:
  85. af = res_use_inet6 () ? AF_INET6 : AF_INET;
  86. addr_size = af == AF_INET6 ? IN6ADDRSZ : INADDRSZ;
  87. break;
  88. }
  89. size_needed = (sizeof (*host_addr)
  90. + sizeof (*h_addr_ptrs)
  91. + sizeof (*h_alias_ptr) + strlen (name) + 1);
  92. if (buffer_size == NULL)
  93. {
  94. if (buflen < size_needed)
  95. {
  96. *status = NSS_STATUS_TRYAGAIN;
  97. if (h_errnop != NULL)
  98. *h_errnop = NETDB_INTERNAL;
  99. __set_errno (ERANGE);
  100. goto done;
  101. }
  102. }
  103. else if (buffer_size != NULL && *buffer_size < size_needed)
  104. {
  105. char *new_buf;
  106. *buffer_size = size_needed;
  107. new_buf = (char *) realloc (*buffer, *buffer_size);
  108. if (new_buf == NULL)
  109. {
  110. save = errno;
  111. free (*buffer);
  112. *buffer = NULL;
  113. *buffer_size = 0;
  114. __set_errno (save);
  115. if (h_errnop != NULL)
  116. *h_errnop = NETDB_INTERNAL;
  117. *result = NULL;
  118. goto done;
  119. }
  120. *buffer = new_buf;
  121. }
  122. memset (*buffer, '\0', size_needed);
  123. host_addr = (host_addr_t *) *buffer;
  124. h_addr_ptrs = (host_addr_list_t *)
  125. ((char *) host_addr + sizeof (*host_addr));
  126. h_alias_ptr = (char **) ((char *) h_addr_ptrs + sizeof (*h_addr_ptrs));
  127. hostname = (char *) h_alias_ptr + sizeof (*h_alias_ptr);
  128. if (isdigit (name[0]))
  129. {
  130. for (cp = name;; ++cp)
  131. {
  132. if (*cp == '\0')
  133. {
  134. int ok;
  135. if (*--cp == '.')
  136. break;
  137. /* All-numeric, no dot at the end. Fake up a hostent as if
  138. we'd actually done a lookup. What if someone types
  139. 255.255.255.255? The test below will succeed
  140. spuriously... ??? */
  141. if (af == AF_INET)
  142. ok = __inet_aton_exact (name, (struct in_addr *) host_addr);
  143. else
  144. {
  145. assert (af == AF_INET6);
  146. ok = inet_pton (af, name, host_addr) > 0;
  147. }
  148. if (! ok)
  149. {
  150. *h_errnop = HOST_NOT_FOUND;
  151. if (buffer_size == NULL)
  152. *status = NSS_STATUS_NOTFOUND;
  153. else
  154. *result = NULL;
  155. goto done;
  156. }
  157. resbuf->h_name = strcpy (hostname, name);
  158. h_alias_ptr[0] = NULL;
  159. resbuf->h_aliases = h_alias_ptr;
  160. (*h_addr_ptrs)[0] = (char *) host_addr;
  161. (*h_addr_ptrs)[1] = NULL;
  162. resbuf->h_addr_list = *h_addr_ptrs;
  163. if (af == AF_INET && res_use_inet6 ())
  164. {
  165. /* We need to change the IP v4 address into the
  166. IP v6 address. */
  167. char tmp[INADDRSZ];
  168. char *p = (char *) host_addr;
  169. int i;
  170. /* Save a copy of the IP v4 address. */
  171. memcpy (tmp, host_addr, INADDRSZ);
  172. /* Mark this ipv6 addr as a mapped ipv4. */
  173. for (i = 0; i < 10; i++)
  174. *p++ = 0x00;
  175. *p++ = 0xff;
  176. *p++ = 0xff;
  177. /* Copy the IP v4 address. */
  178. memcpy (p, tmp, INADDRSZ);
  179. resbuf->h_addrtype = AF_INET6;
  180. resbuf->h_length = IN6ADDRSZ;
  181. }
  182. else
  183. {
  184. resbuf->h_addrtype = af;
  185. resbuf->h_length = addr_size;
  186. }
  187. if (h_errnop != NULL)
  188. *h_errnop = NETDB_SUCCESS;
  189. if (buffer_size == NULL)
  190. *status = NSS_STATUS_SUCCESS;
  191. else
  192. *result = resbuf;
  193. goto done;
  194. }
  195. if (!isdigit (*cp) && *cp != '.')
  196. break;
  197. }
  198. }
  199. if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':')
  200. {
  201. switch (af)
  202. {
  203. default:
  204. af = res_use_inet6 () ? AF_INET6 : AF_INET;
  205. if (af == AF_INET6)
  206. {
  207. addr_size = IN6ADDRSZ;
  208. break;
  209. }
  210. [[fallthrough]];
  211. case AF_INET:
  212. /* This is not possible. We cannot represent an IPv6 address
  213. in an `struct in_addr' variable. */
  214. *h_errnop = HOST_NOT_FOUND;
  215. if (buffer_size == NULL)
  216. *status = NSS_STATUS_NOTFOUND;
  217. else
  218. *result = NULL;
  219. goto done;
  220. case AF_INET6:
  221. addr_size = IN6ADDRSZ;
  222. break;
  223. }
  224. for (cp = name;; ++cp)
  225. {
  226. if (!*cp)
  227. {
  228. if (*--cp == '.')
  229. break;
  230. /* All-IPv6-legal, no dot at the end. Fake up a
  231. hostent as if we'd actually done a lookup. */
  232. if (inet_pton (AF_INET6, name, host_addr) <= 0)
  233. {
  234. *h_errnop = HOST_NOT_FOUND;
  235. if (buffer_size == NULL)
  236. *status = NSS_STATUS_NOTFOUND;
  237. else
  238. *result = NULL;
  239. goto done;
  240. }
  241. resbuf->h_name = strcpy (hostname, name);
  242. h_alias_ptr[0] = NULL;
  243. resbuf->h_aliases = h_alias_ptr;
  244. (*h_addr_ptrs)[0] = (char *) host_addr;
  245. (*h_addr_ptrs)[1] = (char *) 0;
  246. resbuf->h_addr_list = *h_addr_ptrs;
  247. resbuf->h_addrtype = AF_INET6;
  248. resbuf->h_length = addr_size;
  249. *h_errnop = NETDB_SUCCESS;
  250. if (buffer_size == NULL)
  251. *status = NSS_STATUS_SUCCESS;
  252. else
  253. *result = resbuf;
  254. goto done;
  255. }
  256. if (!isxdigit (*cp) && *cp != ':' && *cp != '.')
  257. break;
  258. }
  259. }
  260. }
  261. return 0;
  262. done:
  263. return 1;
  264. }
  265. libc_hidden_def (__nss_hostname_digits_dots)