1feee8
commit 1a3afdfe319a142228498f7a4ee82ac3917d97e8
1feee8
Author: Florian Weimer <fweimer@redhat.com>
1feee8
Date:   Tue Aug 30 10:02:49 2022 +0200
1feee8
1feee8
    resolv: Add tst-resolv-byaddr for testing reverse lookup
1feee8
    
1feee8
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
1feee8
    (cherry picked from commit 0b99828d54e5d1fc8f5ad3edf5ba262ad2e9c5b0)
1feee8
1feee8
diff --git a/resolv/Makefile b/resolv/Makefile
1feee8
index e8269dcb5bcf216b..78165eb99e98b525 100644
1feee8
--- a/resolv/Makefile
1feee8
+++ b/resolv/Makefile
1feee8
@@ -92,6 +92,7 @@ tests += \
1feee8
   tst-res_hnok \
1feee8
   tst-resolv-basic \
1feee8
   tst-resolv-binary \
1feee8
+  tst-resolv-byaddr \
1feee8
   tst-resolv-edns \
1feee8
   tst-resolv-network \
1feee8
   tst-resolv-noaaaa \
1feee8
@@ -251,6 +252,7 @@ $(objpfx)tst-resolv-ai_idn-nolibidn2.out: \
1feee8
   $(gen-locales) $(objpfx)tst-no-libidn2.so
1feee8
 $(objpfx)tst-resolv-basic: $(objpfx)libresolv.so $(shared-thread-library)
1feee8
 $(objpfx)tst-resolv-binary: $(objpfx)libresolv.so $(shared-thread-library)
1feee8
+$(objpfx)tst-resolv-byaddr: $(objpfx)libresolv.so $(shared-thread-library)
1feee8
 $(objpfx)tst-resolv-edns: $(objpfx)libresolv.so $(shared-thread-library)
1feee8
 $(objpfx)tst-resolv-network: $(objpfx)libresolv.so $(shared-thread-library)
1feee8
 $(objpfx)tst-resolv-res_init: $(objpfx)libresolv.so
1feee8
diff --git a/resolv/tst-resolv-byaddr.c b/resolv/tst-resolv-byaddr.c
1feee8
new file mode 100644
1feee8
index 0000000000000000..6299e89837da58c6
1feee8
--- /dev/null
1feee8
+++ b/resolv/tst-resolv-byaddr.c
1feee8
@@ -0,0 +1,326 @@
1feee8
+/* Test reverse DNS lookup.
1feee8
+   Copyright (C) 2022 Free Software Foundation, Inc.
1feee8
+   This file is part of the GNU C Library.
1feee8
+
1feee8
+   The GNU C Library is free software; you can redistribute it and/or
1feee8
+   modify it under the terms of the GNU Lesser General Public
1feee8
+   License as published by the Free Software Foundation; either
1feee8
+   version 2.1 of the License, or (at your option) any later version.
1feee8
+
1feee8
+   The GNU C Library is distributed in the hope that it will be useful,
1feee8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
1feee8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1feee8
+   Lesser General Public License for more details.
1feee8
+
1feee8
+   You should have received a copy of the GNU Lesser General Public
1feee8
+   License along with the GNU C Library; if not, see
1feee8
+   <https://www.gnu.org/licenses/>.  */
1feee8
+
1feee8
+#include <arpa/inet.h>
1feee8
+#include <errno.h>
1feee8
+#include <netdb.h>
1feee8
+#include <stdbool.h>
1feee8
+#include <stdio.h>
1feee8
+#include <stdlib.h>
1feee8
+#include <string.h>
1feee8
+#include <support/check.h>
1feee8
+#include <support/check_nss.h>
1feee8
+#include <support/next_to_fault.h>
1feee8
+#include <support/resolv_test.h>
1feee8
+#include <support/support.h>
1feee8
+
1feee8
+#include "tst-resolv-maybe_insert_sig.h"
1feee8
+
1feee8
+/* QNAME format:
1feee8
+
1feee8
+   ADDRESSES.CNAMES...(lots of 0s)...8.b.d.0.1.0.0.2.ip6.arpa.
1feee8
+   CNAMES|ADDRESSES.2.0.192.in-addr-arpa.
1feee8
+
1feee8
+   For the IPv4 reverse lookup, the address count is in the lower
1feee8
+   bits.
1feee8
+
1feee8
+   CNAMES is the length of the CNAME chain, ADDRESSES is the number of
1feee8
+   addresses in the response.  The special value 15 means that there
1feee8
+   are no addresses, and the RCODE is NXDOMAIN.  */
1feee8
+static void
1feee8
+response (const struct resolv_response_context *ctx,
1feee8
+          struct resolv_response_builder *b,
1feee8
+          const char *qname, uint16_t qclass, uint16_t qtype)
1feee8
+{
1feee8
+  TEST_COMPARE (qclass, C_IN);
1feee8
+  TEST_COMPARE (qtype, T_PTR);
1feee8
+
1feee8
+  unsigned int addresses, cnames, bits;
1feee8
+  char *tail;
1feee8
+  if (strstr (qname, "ip6.arpa") != NULL
1feee8
+      && sscanf (qname, "%x.%x.%ms", &addresses, &cnames, &tail) == 3)
1feee8
+    TEST_COMPARE_STRING (tail, "\
1feee8
+0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa");
1feee8
+  else if (sscanf (qname, "%u.%ms", &bits, &tail) == 2)
1feee8
+    {
1feee8
+      TEST_COMPARE_STRING (tail, "2.0.192.in-addr.arpa");
1feee8
+      addresses = bits & 0x0f;
1feee8
+      cnames = bits >> 4;
1feee8
+    }
1feee8
+  else
1feee8
+    FAIL_EXIT1 ("invalid QNAME: %s", qname);
1feee8
+  free (tail);
1feee8
+
1feee8
+  int rcode;
1feee8
+  if (addresses == 15)
1feee8
+    {
1feee8
+      /* Special case: Use no addresses with NXDOMAIN response.  */
1feee8
+      rcode = ns_r_nxdomain;
1feee8
+      addresses = 0;
1feee8
+    }
1feee8
+  else
1feee8
+    rcode = 0;
1feee8
+
1feee8
+  struct resolv_response_flags flags = { .rcode = rcode };
1feee8
+  resolv_response_init (b, flags);
1feee8
+  resolv_response_add_question (b, qname, qclass, qtype);
1feee8
+  resolv_response_section (b, ns_s_an);
1feee8
+  maybe_insert_sig (b, qname);
1feee8
+
1feee8
+  /* Provide the requested number of CNAME records.  */
1feee8
+  char *previous_name = (char *) qname;
1feee8
+  for (int unique = 0; unique < cnames; ++unique)
1feee8
+    {
1feee8
+      resolv_response_open_record (b, previous_name, qclass, T_CNAME, 60);
1feee8
+      char *new_name = xasprintf ("%d.alias.example", unique);
1feee8
+      resolv_response_add_name (b, new_name);
1feee8
+      resolv_response_close_record (b);
1feee8
+
1feee8
+      maybe_insert_sig (b, qname);
1feee8
+
1feee8
+      if (previous_name != qname)
1feee8
+        free (previous_name);
1feee8
+      previous_name = new_name;
1feee8
+    }
1feee8
+
1feee8
+  for (int unique = 0; unique < addresses; ++unique)
1feee8
+    {
1feee8
+      resolv_response_open_record (b, previous_name, qclass, T_PTR, 60);
1feee8
+      char *ptr = xasprintf ("unique-%d.cnames-%u.addresses-%u.example",
1feee8
+                             unique, cnames, addresses);
1feee8
+      resolv_response_add_name (b, ptr);
1feee8
+      free (ptr);
1feee8
+      resolv_response_close_record (b);
1feee8
+    }
1feee8
+
1feee8
+  if (previous_name != qname)
1feee8
+    free (previous_name);
1feee8
+}
1feee8
+
1feee8
+/* Used to check that gethostbyaddr_r does not write past the buffer
1feee8
+   end.  */
1feee8
+static struct support_next_to_fault ntf;
1feee8
+
1feee8
+/* Perform a gethostbyaddr call and check the result.  */
1feee8
+static void
1feee8
+check_gethostbyaddr (const char *address, const char *expected)
1feee8
+{
1feee8
+  unsigned char bytes[16];
1feee8
+  unsigned int byteslen;
1feee8
+  int family;
1feee8
+  if (strchr (address, ':') != NULL)
1feee8
+    {
1feee8
+      family = AF_INET6;
1feee8
+      byteslen = 16;
1feee8
+    }
1feee8
+  else
1feee8
+    {
1feee8
+      family = AF_INET;
1feee8
+      byteslen = 4;
1feee8
+    }
1feee8
+  TEST_COMPARE (inet_pton (family, address, bytes), 1);
1feee8
+
1feee8
+  struct hostent *e = gethostbyaddr (bytes, byteslen, family);
1feee8
+  check_hostent (address, e, expected);
1feee8
+
1feee8
+  if (e == NULL)
1feee8
+    return;
1feee8
+
1feee8
+  /* Try gethostbyaddr_r with increasing sizes until success.  First
1feee8
+     compute a reasonable minimum buffer size, to avoid many pointless
1feee8
+     attempts.  */
1feee8
+  size_t minimum_size = strlen (e->h_name);
1feee8
+  for (int i = 0; e->h_addr_list[i] != NULL; ++i)
1feee8
+    minimum_size += e->h_length + sizeof (char *);
1feee8
+  for (int i = 0; e->h_aliases[i] != NULL; ++i)
1feee8
+    minimum_size += strlen (e->h_aliases[i]) + 1 + sizeof (char *);
1feee8
+
1feee8
+  /* Gradually increase the size until success.  */
1feee8
+  for (size_t size = minimum_size; size < ntf.length; ++size)
1feee8
+    {
1feee8
+      struct hostent result;
1feee8
+      int herrno;
1feee8
+      int ret = gethostbyaddr_r (bytes, byteslen, family, &result,
1feee8
+                                 ntf.buffer + ntf.length - size, size,
1feee8
+                                 &e, &herrno);
1feee8
+      if (ret == ERANGE)
1feee8
+        /* Retry with larger size.  */
1feee8
+        TEST_COMPARE (herrno, NETDB_INTERNAL);
1feee8
+      else if (ret == 0)
1feee8
+        {
1feee8
+         TEST_VERIFY (size > minimum_size);
1feee8
+         check_hostent (address, e, expected);
1feee8
+         return;
1feee8
+        }
1feee8
+      else
1feee8
+        FAIL_EXIT1 ("Unexpected gethostbyaddr_r failure: %d", ret);
1feee8
+    }
1feee8
+
1feee8
+  FAIL_EXIT1 ("gethostbyaddr_r always failed for: %s", address);
1feee8
+}
1feee8
+
1feee8
+/* Perform a getnameinfo call and check the result.  */
1feee8
+static void
1feee8
+check_getnameinfo (const char *address, const char *expected)
1feee8
+{
1feee8
+  struct sockaddr_in sin = { };
1feee8
+  struct sockaddr_in6 sin6 = { };
1feee8
+  void *sa;
1feee8
+  socklen_t salen;
1feee8
+  if (strchr (address, ':') != NULL)
1feee8
+    {
1feee8
+      sin6.sin6_family = AF_INET6;
1feee8
+      TEST_COMPARE (inet_pton (AF_INET6, address, &sin6.sin6_addr), 1);
1feee8
+      sin6.sin6_port = htons (80);
1feee8
+      sa = &sin;;
1feee8
+      salen = sizeof (sin6);
1feee8
+    }
1feee8
+  else
1feee8
+    {
1feee8
+      sin.sin_family = AF_INET;
1feee8
+      TEST_COMPARE (inet_pton (AF_INET, address, &sin.sin_addr), 1);
1feee8
+      sin.sin_port = htons (80);
1feee8
+      sa = &sin;
1feee8
+      salen = sizeof (sin);
1feee8
+    }
1feee8
+
1feee8
+  char host[64];
1feee8
+  char service[64];
1feee8
+  int ret = getnameinfo (sa, salen, host,
1feee8
+                         sizeof (host), service, sizeof (service),
1feee8
+                         NI_NAMEREQD | NI_NUMERICSERV);
1feee8
+  switch (ret)
1feee8
+    {
1feee8
+    case 0:
1feee8
+      TEST_COMPARE_STRING (host, expected);
1feee8
+      TEST_COMPARE_STRING (service, "80");
1feee8
+      break;
1feee8
+    case EAI_SYSTEM:
1feee8
+      TEST_COMPARE_STRING (strerror (errno), expected);
1feee8
+      break;
1feee8
+    default:
1feee8
+      TEST_COMPARE_STRING (gai_strerror (ret), expected);
1feee8
+    }
1feee8
+}
1feee8
+
1feee8
+static int
1feee8
+do_test (void)
1feee8
+{
1feee8
+  /* Some reasonably upper bound for the maximum response size.  */
1feee8
+  ntf = support_next_to_fault_allocate (4096);
1feee8
+
1feee8
+  struct resolv_test *obj = resolv_test_start
1feee8
+    ((struct resolv_redirect_config)
1feee8
+     {
1feee8
+       .response_callback = response
1feee8
+     });
1feee8
+
1feee8
+  for (int do_insert_sig = 0; do_insert_sig < 2; ++do_insert_sig)
1feee8
+    {
1feee8
+      insert_sig = do_insert_sig;
1feee8
+
1feee8
+      /* No PTR record, RCODE=0.  */
1feee8
+      check_gethostbyaddr ("192.0.2.0", "error: NO_RECOVERY\n");
1feee8
+      check_getnameinfo ("192.0.2.0", "Name or service not known");
1feee8
+      check_gethostbyaddr ("192.0.2.16", "error: NO_RECOVERY\n");
1feee8
+      check_getnameinfo ("192.0.2.16", "Name or service not known");
1feee8
+      check_gethostbyaddr ("192.0.2.32", "error: NO_RECOVERY\n");
1feee8
+      check_getnameinfo ("192.0.2.32", "Name or service not known");
1feee8
+      check_gethostbyaddr ("2001:db8::", "error: NO_RECOVERY\n");
1feee8
+      check_getnameinfo ("2001:db8::", "Name or service not known");
1feee8
+      check_gethostbyaddr ("2001:db8::10", "error: NO_RECOVERY\n");
1feee8
+      check_getnameinfo ("2001:db8::10", "Name or service not known");
1feee8
+      check_gethostbyaddr ("2001:db8::20", "error: NO_RECOVERY\n");
1feee8
+      check_getnameinfo ("2001:db8::20", "Name or service not known");
1feee8
+
1feee8
+      /* No PTR record, NXDOMAIN.  */
1feee8
+      check_gethostbyaddr ("192.0.2.15", "error: HOST_NOT_FOUND\n");
1feee8
+      check_getnameinfo ("192.0.2.15", "Name or service not known");
1feee8
+      check_gethostbyaddr ("192.0.2.31", "error: HOST_NOT_FOUND\n");
1feee8
+      check_getnameinfo ("192.0.2.31", "Name or service not known");
1feee8
+      check_gethostbyaddr ("192.0.2.47", "error: HOST_NOT_FOUND\n");
1feee8
+      check_getnameinfo ("192.0.2.47", "Name or service not known");
1feee8
+      check_gethostbyaddr ("2001:db8::f", "error: HOST_NOT_FOUND\n");
1feee8
+      check_getnameinfo ("2001:db8::f", "Name or service not known");
1feee8
+      check_gethostbyaddr ("2001:db8::1f", "error: HOST_NOT_FOUND\n");
1feee8
+      check_getnameinfo ("2001:db8::1f", "Name or service not known");
1feee8
+      check_gethostbyaddr ("2001:db8::2f", "error: HOST_NOT_FOUND\n");
1feee8
+      check_getnameinfo ("2001:db8::2f", "Name or service not known");
1feee8
+
1feee8
+      /* Actual response data.  Only the first PTR record is returned.  */
1feee8
+      check_gethostbyaddr ("192.0.2.1",
1feee8
+                           "name: unique-0.cnames-0.addresses-1.example\n"
1feee8
+                           "address: 192.0.2.1\n");
1feee8
+      check_getnameinfo ("192.0.2.1",
1feee8
+                         "unique-0.cnames-0.addresses-1.example");
1feee8
+      check_gethostbyaddr ("192.0.2.17",
1feee8
+                           "name: unique-0.cnames-1.addresses-1.example\n"
1feee8
+                           "address: 192.0.2.17\n");
1feee8
+      check_getnameinfo ("192.0.2.17",
1feee8
+                         "unique-0.cnames-1.addresses-1.example");
1feee8
+      check_gethostbyaddr ("192.0.2.18",
1feee8
+                           "name: unique-0.cnames-1.addresses-2.example\n"
1feee8
+                           "address: 192.0.2.18\n");
1feee8
+      check_getnameinfo ("192.0.2.18",
1feee8
+                         "unique-0.cnames-1.addresses-2.example");
1feee8
+      check_gethostbyaddr ("192.0.2.33",
1feee8
+                           "name: unique-0.cnames-2.addresses-1.example\n"
1feee8
+                           "address: 192.0.2.33\n");
1feee8
+      check_getnameinfo ("192.0.2.33",
1feee8
+                         "unique-0.cnames-2.addresses-1.example");
1feee8
+      check_gethostbyaddr ("192.0.2.34",
1feee8
+                           "name: unique-0.cnames-2.addresses-2.example\n"
1feee8
+                           "address: 192.0.2.34\n");
1feee8
+      check_getnameinfo ("192.0.2.34",
1feee8
+                         "unique-0.cnames-2.addresses-2.example");
1feee8
+
1feee8
+      /* Same for IPv6 addresses.  */
1feee8
+      check_gethostbyaddr ("2001:db8::1",
1feee8
+                           "name: unique-0.cnames-0.addresses-1.example\n"
1feee8
+                           "address: 2001:db8::1\n");
1feee8
+      check_getnameinfo ("2001:db8::1",
1feee8
+                         "unique-0.cnames-0.addresses-1.example");
1feee8
+      check_gethostbyaddr ("2001:db8::11",
1feee8
+                           "name: unique-0.cnames-1.addresses-1.example\n"
1feee8
+                           "address: 2001:db8::11\n");
1feee8
+      check_getnameinfo ("2001:db8::11",
1feee8
+                         "unique-0.cnames-1.addresses-1.example");
1feee8
+      check_gethostbyaddr ("2001:db8::12",
1feee8
+                           "name: unique-0.cnames-1.addresses-2.example\n"
1feee8
+                           "address: 2001:db8::12\n");
1feee8
+      check_getnameinfo ("2001:db8::12",
1feee8
+                         "unique-0.cnames-1.addresses-2.example");
1feee8
+      check_gethostbyaddr ("2001:db8::21",
1feee8
+                           "name: unique-0.cnames-2.addresses-1.example\n"
1feee8
+                           "address: 2001:db8::21\n");
1feee8
+      check_getnameinfo ("2001:db8::21",
1feee8
+                         "unique-0.cnames-2.addresses-1.example");
1feee8
+      check_gethostbyaddr ("2001:db8::22",
1feee8
+                           "name: unique-0.cnames-2.addresses-2.example\n"
1feee8
+                           "address: 2001:db8::22\n");
1feee8
+      check_getnameinfo ("2001:db8::22",
1feee8
+                         "unique-0.cnames-2.addresses-2.example");
1feee8
+    }
1feee8
+
1feee8
+  resolv_test_end (obj);
1feee8
+
1feee8
+  support_next_to_fault_free (&ntf;;
1feee8
+  return 0;
1feee8
+}
1feee8
+
1feee8
+#include <support/test-driver.c>
1feee8
diff --git a/resolv/tst-resolv-maybe_insert_sig.h b/resolv/tst-resolv-maybe_insert_sig.h
1feee8
new file mode 100644
1feee8
index 0000000000000000..05725225af0818cb
1feee8
--- /dev/null
1feee8
+++ b/resolv/tst-resolv-maybe_insert_sig.h
1feee8
@@ -0,0 +1,32 @@
1feee8
+/* Code snippet for optionally inserting ignored SIG records in resolver tests.
1feee8
+   Copyright (C) 2022 Free Software Foundation, Inc.
1feee8
+   This file is part of the GNU C Library.
1feee8
+
1feee8
+   The GNU C Library is free software; you can redistribute it and/or
1feee8
+   modify it under the terms of the GNU Lesser General Public
1feee8
+   License as published by the Free Software Foundation; either
1feee8
+   version 2.1 of the License, or (at your option) any later version.
1feee8
+
1feee8
+   The GNU C Library is distributed in the hope that it will be useful,
1feee8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
1feee8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1feee8
+   Lesser General Public License for more details.
1feee8
+
1feee8
+   You should have received a copy of the GNU Lesser General Public
1feee8
+   License along with the GNU C Library; if not, see
1feee8
+   <https://www.gnu.org/licenses/>.  */
1feee8
+
1feee8
+/* Set to true for an alternative pass that inserts (ignored) SIG
1feee8
+   records.  This does not alter the response, so this property is not
1feee8
+   encoded in the QNAME.  The variable needs to be volatile because
1feee8
+   leaf attributes tell GCC that the response function is not
1feee8
+   called.  */
1feee8
+static volatile bool insert_sig;
1feee8
+
1feee8
+static void
1feee8
+maybe_insert_sig (struct resolv_response_builder *b, const char *owner)
1feee8
+{
1feee8
+  resolv_response_open_record (b, owner, C_IN, T_SIG, 60);
1feee8
+  resolv_response_add_data (b, "", 1);
1feee8
+  resolv_response_close_record (b);
1feee8
+}