about summary refs log tree commit diff
path: root/resolv/tst-resolv-search.c
blob: c5a4b9d891117563bccfd94a7b1f01092d735e6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* Test search/default domain name behavior.
   Copyright (C) 2016-2021 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <resolv.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/check_nss.h>
#include <support/resolv_test.h>
#include <support/support.h>
#include <support/xmemstream.h>

struct item
{
  const char *name;
  int response;
};

const struct item items[] =
  {
    {"hostname.usersys.example.com", 1},
    {"hostname.corp.example.com", 1},
    {"hostname.example.com", 1},

    {"mail.corp.example.com", 1},
    {"mail.example.com", 1},

    {"file.corp.example.com", 2},
    {"file.corp", 1},
    {"file.example.com", 1},
    {"servfail-usersys.usersys.example.com", -ns_r_servfail},
    {"servfail-usersys.corp.example.com", 1},
    {"servfail-usersys.example.com", 1},
    {"servfail-corp.usersys.example.com", 1},
    {"servfail-corp.corp.example.com", -ns_r_servfail},
    {"servfail-corp.example.com", 1},
    {"www.example.com", 1},
    {"large.example.com", 200},

    /* Test query amplification with a SERVFAIL response combined with
       a large RRset.  */
    {"large-servfail.usersys.example.com", -ns_r_servfail},
    {"large-servfail.example.com", 2000},
    {}
  };

enum
  {
    name_not_found = -1,
    name_no_data = -2
  };

static int
find_name (const char *name)
{
  for (int i = 0; items[i].name != NULL; ++i)
    {
      if (strcmp (name, items[i].name) == 0)
        return i;
    }
  if (strcmp (name, "example.com") == 0
      || strcmp (name, "usersys.example.com") == 0
      || strcmp (name, "corp.example.com") == 0)
    return name_no_data;
  return name_not_found;
}

static int rcode_override_server_index = -1;
static int rcode_override;

static void
response (const struct resolv_response_context *ctx,
          struct resolv_response_builder *b,
          const char *qname, uint16_t qclass, uint16_t qtype)
{
  if (ctx->server_index == rcode_override_server_index)
    {
      struct resolv_response_flags flags = {.rcode = rcode_override};
      resolv_response_init (b, flags);
      resolv_response_add_question (b, qname, qclass, qtype);
      return;
    }

  int index = find_name (qname);
  struct resolv_response_flags flags = {};
  if (index == name_not_found)
    flags.rcode = ns_r_nxdomain;
  else if (index >= 0 && items[index].response < 0)
    flags.rcode = -items[index].response;
  else if (index >= 0 && items[index].response > 5 && !ctx->tcp)
    /* Force TCP if more than 5 addresses where requested.  */
    flags.tc = true;
  resolv_response_init (b, flags);
  resolv_response_add_question (b, qname, qclass, qtype);

  if (flags.tc || index < 0 || items[index].response < 0)
    return;

  resolv_response_section (b, ns_s_an);

  for (int i = 0; i < items[index].response; ++i)
    {
      resolv_response_open_record (b, qname, qclass, qtype, 0);

      switch (qtype)
        {
        case T_A:
          {
            char addr[4] = {10, index, i >> 8, i};
            resolv_response_add_data (b, addr, sizeof (addr));
          }
          break;
        case T_AAAA:
          {
            char addr[16]
              = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, index + 1, (i + 1) >> 8, i + 1};
            resolv_response_add_data (b, addr, sizeof (addr));
          }
          break;
        default:
          support_record_failure ();
          printf ("error: unexpected QTYPE: %s/%u/%u\n",
                  qname, qclass, qtype);
        }
      resolv_response_close_record (b);
    }
}

enum output_format
  {
    format_get, format_gai
  };

static void
format_expected_1 (FILE *out, int family, enum output_format format, int index)
{
  for (int i = 0; i < items[index].response; ++i)
    {
      char address[200];
      switch (family)
        {
        case AF_INET:
          snprintf (address, sizeof (address), "10.%d.%d.%d",
                    index, (i >> 8) & 0xff, i & 0xff);
          break;
        case AF_INET6:
          snprintf (address, sizeof (address), "2001:db8::%x:%x",
                    index + 1, i + 1);
          break;
        default:
          FAIL_EXIT1 ("unreachable");
        }

      switch (format)
        {
        case format_get:
          fprintf (out, "address: %s\n", address);
          break;
        case format_gai:
          fprintf (out, "address: STREAM/TCP %s 80\n", address);
        }
    }
}

static char *
format_expected (const char *fqdn, int family, enum output_format format)
{
  int index = find_name (fqdn);
  TEST_VERIFY_EXIT (index >= 0);
  struct xmemstream stream;
  xopen_memstream (&stream);

  TEST_VERIFY_EXIT (items[index].response >= 0);
  if (format == format_get)
    fprintf (stream.out, "name: %s\n", items[index].name);
  if (family == AF_INET || family == AF_UNSPEC)
    format_expected_1 (stream.out, AF_INET, format, index);
  if (family == AF_INET6 || family == AF_UNSPEC)
    format_expected_1 (stream.out, AF_INET6, format, index);

  xfclose_memstream (&stream);
  return stream.buffer;
}

static void
do_get (const char *name, const char *fqdn, int family)
{
  char *expected = format_expected (fqdn, family, format_get);
  if (family == AF_INET)
    {
      char *query = xasprintf ("gethostbyname (\"%s\")", name);
      check_hostent (query, gethostbyname (name), expected);
      free (query);
    }
  char *query = xasprintf ("gethostbyname2 (\"%s\", %d)", name, family);
  check_hostent (query, gethostbyname2 (name, family), expected);

  /* Test res_search.  */
  int qtype;
  switch (family)
    {
    case AF_INET:
      qtype = T_A;
      break;
    case AF_INET6:
      qtype = T_AAAA;
      break;
    default:
      qtype = -1;
    }
  if (qtype >= 0)
    {
      int sz = 512;
      unsigned char *response = xmalloc (sz);
      int ret = res_search (name, C_IN, qtype, response, sz);
      TEST_VERIFY_EXIT (ret >= 0);
      if (ret > sz)
        {
          /* Truncation.  Retry with a larger buffer.  */
          sz = 65535;
          unsigned char *newptr = xrealloc (response, sz);
          response = newptr;

          ret = res_search (name, C_IN, qtype, response, sz);
          TEST_VERIFY_EXIT (ret >= 0);
          TEST_VERIFY_EXIT (ret < sz);
        }
      check_dns_packet (query, response, ret, expected);
      free (response);
    }

  free (query);
  free (expected);
}

static void
do_gai (const char *name, const char *fqdn, int family)
{
  struct addrinfo hints =
    {
      .ai_family = family,
      .ai_protocol = IPPROTO_TCP,
      .ai_socktype = SOCK_STREAM
    };
  struct addrinfo *ai;
  char *query = xasprintf ("%s:80 [%d]", name, family);
  int ret = getaddrinfo (name, "80", &hints, &ai);
  char *expected = format_expected (fqdn, family, format_gai);
  check_addrinfo (query, ai, ret, expected);
  if (ret == 0)
    freeaddrinfo (ai);
  free (expected);
  free (query);
}

static void
do_both (const char *name, const char *fqdn)
{
  do_get (name, fqdn, AF_INET);
  do_get (name, fqdn, AF_INET6);
  do_gai (name, fqdn, AF_INET);
  do_gai (name, fqdn, AF_INET6);
  do_gai (name, fqdn, AF_UNSPEC);
}

static void
do_test_all (bool unconnectable_server)
{
  struct resolv_redirect_config config =
    {
      .response_callback = response,
      .search = {"usersys.example.com", "corp.example.com", "example.com"},
    };
  struct resolv_test *obj = resolv_test_start (config);

  if (unconnectable_server)
    {
      /* 255.255.255.255 results in an immediate connect failure.  The
         next server will supply the answer instead.  This is a
         triggering condition for bug 19791.  */
      _res.nsaddr_list[0].sin_addr.s_addr = -1;
      _res.nsaddr_list[0].sin_port = htons (53);
    }

  do_both ("file", "file.corp.example.com");
  do_both ("www", "www.example.com");
  do_both ("servfail-usersys", "servfail-usersys.corp.example.com");
  do_both ("servfail-corp", "servfail-corp.usersys.example.com");
  do_both ("large", "large.example.com");
  do_both ("large-servfail", "large-servfail.example.com");
  do_both ("file.corp", "file.corp");

  /* Check that SERVFAIL and REFUSED responses do not alter the search
     path resolution.  */
  rcode_override_server_index = 0;
  rcode_override = ns_r_servfail;
  do_both ("hostname", "hostname.usersys.example.com");
  do_both ("large", "large.example.com");
  do_both ("large-servfail", "large-servfail.example.com");
  rcode_override = ns_r_refused;
  do_both ("hostname", "hostname.usersys.example.com");
  do_both ("large", "large.example.com");
  do_both ("large-servfail", "large-servfail.example.com");
  /* Likewise, but with an NXDOMAIN for the first search path
     entry.  */
  rcode_override = ns_r_servfail;
  do_both ("mail", "mail.corp.example.com");
  rcode_override = ns_r_refused;
  do_both ("mail", "mail.corp.example.com");
  /* Likewise, but with ndots handling.  */
  rcode_override = ns_r_servfail;
  do_both ("file.corp", "file.corp");
  rcode_override = ns_r_refused;
  do_both ("file.corp", "file.corp");

  resolv_test_end (obj);
}

static int
do_test (void)
{
  for (int unconnectable_server = 0; unconnectable_server < 2;
       ++unconnectable_server)
    do_test_all (unconnectable_server);
  return 0;
}

#include <support/test-driver.c>