tst-resolv-aliases.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Test alias handling (mainly for gethostbyname).
  2. Copyright (C) 2022-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <array_length.h>
  16. #include <arpa/inet.h>
  17. #include <netdb.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <support/check.h>
  23. #include <support/check_nss.h>
  24. #include <support/resolv_test.h>
  25. #include <support/support.h>
  26. #include "tst-resolv-maybe_insert_sig.h"
  27. /* QNAME format:
  28. aADDRESSES-cCNAMES.example.net
  29. CNAMES is the length of the CNAME chain, ADDRESSES is the number of
  30. addresses in the response. The special value 255 means that there
  31. are no addresses, and the RCODE is NXDOMAIN. */
  32. static void
  33. response (const struct resolv_response_context *ctx,
  34. struct resolv_response_builder *b,
  35. const char *qname, uint16_t qclass, uint16_t qtype)
  36. {
  37. TEST_COMPARE (qclass, C_IN);
  38. if (qtype != T_A)
  39. TEST_COMPARE (qtype, T_AAAA);
  40. unsigned int addresses, cnames;
  41. char *tail;
  42. if (sscanf (qname, "a%u-c%u%ms", &addresses, &cnames, &tail) == 3)
  43. {
  44. if (strcmp (tail, ".example.com") == 0
  45. || strcmp (tail, ".example.net.example.net") == 0
  46. || strcmp (tail, ".example.net.example.com") == 0)
  47. /* These only happen after NXDOMAIN. */
  48. TEST_VERIFY (addresses == 255);
  49. else if (strcmp (tail, ".example.net") != 0)
  50. FAIL_EXIT1 ("invalid QNAME: %s", qname);
  51. }
  52. free (tail);
  53. int rcode;
  54. if (addresses == 255)
  55. {
  56. /* Special case: Use no addresses with NXDOMAIN response. */
  57. rcode = ns_r_nxdomain;
  58. addresses = 0;
  59. }
  60. else
  61. rcode = 0;
  62. struct resolv_response_flags flags = { .rcode = rcode };
  63. resolv_response_init (b, flags);
  64. resolv_response_add_question (b, qname, qclass, qtype);
  65. resolv_response_section (b, ns_s_an);
  66. maybe_insert_sig (b, qname);
  67. /* Provide the requested number of CNAME records. */
  68. char *previous_name = (char *) qname;
  69. for (int unique = 0; unique < cnames; ++unique)
  70. {
  71. resolv_response_open_record (b, previous_name, qclass, T_CNAME, 60);
  72. char *new_name = xasprintf ("%d.alias.example", unique);
  73. resolv_response_add_name (b, new_name);
  74. resolv_response_close_record (b);
  75. maybe_insert_sig (b, qname);
  76. if (previous_name != qname)
  77. free (previous_name);
  78. previous_name = new_name;
  79. }
  80. for (int unique = 0; unique < addresses; ++unique)
  81. {
  82. resolv_response_open_record (b, previous_name, qclass, qtype, 60);
  83. if (qtype == T_A)
  84. {
  85. char ipv4[4] = {192, 0, 2, 1 + unique};
  86. resolv_response_add_data (b, &ipv4, sizeof (ipv4));
  87. }
  88. else if (qtype == T_AAAA)
  89. {
  90. char ipv6[16] =
  91. {
  92. 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  93. 1 + unique
  94. };
  95. resolv_response_add_data (b, &ipv6, sizeof (ipv6));
  96. }
  97. resolv_response_close_record (b);
  98. }
  99. if (previous_name != qname)
  100. free (previous_name);
  101. }
  102. static char *
  103. make_qname (bool do_search, int cnames, int addresses)
  104. {
  105. return xasprintf ("a%d-c%d%s",
  106. addresses, cnames, do_search ? "" : ".example.net");
  107. }
  108. static void
  109. check_cnames_failure (int af, bool do_search, int cnames, int addresses)
  110. {
  111. char *qname = make_qname (do_search, cnames, addresses);
  112. struct hostent *e;
  113. if (af == AF_UNSPEC)
  114. e = gethostbyname (qname);
  115. else
  116. e = gethostbyname2 (qname, af);
  117. if (addresses == 0)
  118. check_hostent (qname, e, "error: NO_RECOVERY\n");
  119. else
  120. check_hostent (qname, e, "error: HOST_NOT_FOUND\n");
  121. free (qname);
  122. }
  123. static void
  124. check (int af, bool do_search, int cnames, int addresses)
  125. {
  126. char *qname = make_qname (do_search, cnames, addresses);
  127. char *fqdn = make_qname (false, cnames, addresses);
  128. struct hostent *e;
  129. if (af == AF_UNSPEC)
  130. e = gethostbyname (qname);
  131. else
  132. e = gethostbyname2 (qname, af);
  133. if (e == NULL)
  134. FAIL_EXIT1 ("unexpected failure for %d, %d, %d", af, cnames, addresses);
  135. if (af == AF_UNSPEC || af == AF_INET)
  136. {
  137. TEST_COMPARE (e->h_addrtype, AF_INET);
  138. TEST_COMPARE (e->h_length, 4);
  139. }
  140. else
  141. {
  142. TEST_COMPARE (e->h_addrtype, AF_INET6);
  143. TEST_COMPARE (e->h_length, 16);
  144. }
  145. for (int i = 0; i < addresses; ++i)
  146. {
  147. char ipv4[4] = {192, 0, 2, 1 + i};
  148. char ipv6[16] =
  149. { 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 + i };
  150. char *expected = e->h_addrtype == AF_INET ? ipv4 : ipv6;
  151. TEST_COMPARE_BLOB (e->h_addr_list[i], e->h_length,
  152. expected, e->h_length);
  153. }
  154. TEST_VERIFY (e->h_addr_list[addresses] == NULL);
  155. if (cnames == 0)
  156. {
  157. /* QNAME is fully qualified. */
  158. TEST_COMPARE_STRING (e->h_name, fqdn);
  159. TEST_VERIFY (e->h_aliases[0] == NULL);
  160. }
  161. else
  162. {
  163. /* Fully-qualified QNAME is demoted to an aliases. */
  164. TEST_COMPARE_STRING (e->h_aliases[0], fqdn);
  165. for (int i = 1; i <= cnames; ++i)
  166. {
  167. char *expected = xasprintf ("%d.alias.example", i - 1);
  168. if (i == cnames)
  169. TEST_COMPARE_STRING (e->h_name, expected);
  170. else
  171. TEST_COMPARE_STRING (e->h_aliases[i], expected);
  172. free (expected);
  173. }
  174. TEST_VERIFY (e->h_aliases[cnames] == NULL);
  175. }
  176. free (fqdn);
  177. free (qname);
  178. }
  179. static int
  180. do_test (void)
  181. {
  182. struct resolv_test *obj = resolv_test_start
  183. ((struct resolv_redirect_config)
  184. {
  185. .response_callback = response,
  186. .search = { "example.net", "example.com" },
  187. });
  188. static const int families[] = { AF_UNSPEC, AF_INET, AF_INET6 };
  189. for (int do_insert_sig = 0; do_insert_sig < 2; ++do_insert_sig)
  190. {
  191. insert_sig = do_insert_sig;
  192. /* If do_search is true, a bare host name (for example, a1-c1)
  193. is used. This exercises search path processing and FQDN
  194. qualification. */
  195. for (int do_search = 0; do_search < 2; ++do_search)
  196. for (const int *paf = families; paf != array_end (families); ++paf)
  197. {
  198. for (int cnames = 0; cnames <= 100; ++cnames)
  199. {
  200. check_cnames_failure (*paf, do_search, cnames, 0);
  201. /* Now with NXDOMAIN responses. */
  202. check_cnames_failure (*paf, do_search, cnames, 255);
  203. }
  204. for (int cnames = 0; cnames <= 10; ++cnames)
  205. for (int addresses = 1; addresses <= 10; ++addresses)
  206. check (*paf, do_search, cnames, addresses);
  207. /* The current implementation is limited to 47 aliases.
  208. Addresses do not have such a limit. */
  209. check (*paf, do_search, 47, 60);
  210. }
  211. }
  212. resolv_test_end (obj);
  213. return 0;
  214. }
  215. #include <support/test-driver.c>