aboutsummaryrefslogtreecommitdiff
path: root/resolv/tst-resolv-invalid-cname.c
blob: ae2d4419b1978c02ec5838b13e99d2f115f56de6 (plain)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* Test handling of CNAMEs with non-host domain names (bug 12154).
   Copyright (C) 2022 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 <errno.h>
#include <netdb.h>
#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>

/* Query strings describe the CNAME chain in the response.  They have
   the format "bitsBITS.countCOUNT.example.", where BITS and COUNT are
   replaced by unsigned decimal numbers.  COUNT is the number of CNAME
   records in the response.  BITS has two bits for each CNAME record,
   describing a special prefix that is added to that CNAME.

   0: No special leading label.
   1: Starting with "*.".
   2: Starting with "-x.".
   3: Starting with "star.*.".

   The first CNAME in the response using the two least significant
   bits.

   For PTR queries, the QNAME format is different, it is either
   COUNT.BITS.168.192.in-addr.arpa. (with BITS and COUNT still
   decimal), or:

COUNT.BITS0.BITS1.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.

   where BITS and COUNT are hexadecimal.  */

static void
response (const struct resolv_response_context *ctx,
          struct resolv_response_builder *b,
          const char *qname, uint16_t qclass, uint16_t qtype)
{
  TEST_COMPARE (qclass, C_IN);

  /* The only other query type besides A is PTR.  */
  if (qtype != T_A && qtype != T_AAAA)
    TEST_COMPARE (qtype, T_PTR);

  unsigned int bits, bits1, count;
  char *tail = NULL;
  if (sscanf (qname, "bits%u.count%u.%ms", &bits, &count, &tail) == 3)
    TEST_COMPARE_STRING (tail, "example");
  else if (strstr (qname, "in-addr.arpa") != NULL
           && sscanf (qname, "%u.%u.%ms", &bits, &count, &tail) == 3)
    TEST_COMPARE_STRING (tail, "168.192.in-addr.arpa");
  else if (sscanf (qname, "%x.%x.%x.%ms", &bits, &bits1, &count, &tail) == 4)
    {
      TEST_COMPARE_STRING (tail, "\
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");
      bits |= bits1 << 4;
    }
  else
    FAIL_EXIT1 ("invalid QNAME: %s\n", qname);
  free (tail);

  struct resolv_response_flags flags = {};
  resolv_response_init (b, flags);
  resolv_response_add_question (b, qname, qclass, qtype);
  resolv_response_section (b, ns_s_an);

  /* Provide the requested number of CNAME records.  */
  char *previous_name = (char *) qname;
  unsigned int original_bits = bits;
  for (int unique = 0; unique < count; ++unique)
    {
      resolv_response_open_record (b, previous_name, qclass, T_CNAME, 60);

      static const char bits_to_prefix[4][8] = { "", "*.", "-x.", "star.*." };
      char *new_name = xasprintf ("%sunique%d.example",
                                  bits_to_prefix[bits & 3], unique);
      bits >>= 2;
      resolv_response_add_name (b, new_name);
      resolv_response_close_record (b);

      if (previous_name != qname)
        free (previous_name);
      previous_name = new_name;
    }

  /* Actual answer record.  */
  resolv_response_open_record (b, previous_name, qclass, qtype, 60);
  switch (qtype)
    {
    case T_A:
      {
        char ipv4[4] = {192, 168, count, original_bits};
        resolv_response_add_data (b, &ipv4, sizeof (ipv4));
      }
      break;
    case T_AAAA:
      {
        char ipv6[16] =
          {
            0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            count, original_bits
          };
        resolv_response_add_data (b, &ipv6, sizeof (ipv6));
      }
      break;

    case T_PTR:
      {
        char *name = xasprintf ("bits%u.count%u.example",
                                original_bits, count);
        resolv_response_add_name (b, name);
        free (name);
      }
      break;
    }
  resolv_response_close_record (b);

  if (previous_name != qname)
    free (previous_name);
}

/* Controls which name resolution function is invoked.  */
enum test_mode
  {
    byname,                     /* gethostbyname.  */
    byname2,                    /* gethostbyname2.  */
    gai,                        /* getaddrinfo without AI_CANONNAME.  */
    gai_canon,                  /* getaddrinfo with AI_CANONNAME.  */

    test_mode_num               /* Number of enum values.  */
  };

static const char *
test_mode_to_string (enum test_mode mode)
{
  switch (mode)
    {
    case byname:
      return "byname";
    case byname2:
      return "byname2";
    case gai:
      return "gai";
    case gai_canon:
      return "gai_canon";
    case test_mode_num:
      /* Report error below.  */
    }
  FAIL_EXIT1 ("invalid test_mode: %d", mode);
}

/* Append the name and aliases to OUT.  */
static void
append_names (FILE *out, const char *qname, int bits, int count,
              enum test_mode mode)
{
  /* Largest valid index which has a corresponding zero in bits
     (meaning a syntactically valid CNAME).  */
  int last_valid_cname = -1;

  for (int i = 0; i < count; ++i)
    if ((bits & (3 << (i * 2))) == 0)
      last_valid_cname = i;

  if (mode != gai)
    {
      const char *label;
      if (mode == gai_canon)
        label = "canonname";
      else
        label = "name";
      if (last_valid_cname >= 0)
        fprintf (out, "%s: unique%d.example\n", label, last_valid_cname);
      else
        fprintf (out, "%s: %s\n", label, qname);
    }

  if (mode == byname || mode == byname2)
    {
      if (last_valid_cname >= 0)
        fprintf (out, "alias: %s\n", qname);
      for (int i = 0; i < count; ++i)
        {
          if ((bits & (3 << (i * 2))) == 0 && i != last_valid_cname)
            fprintf (out, "alias: unique%d.example\n", i);
        }
    }
}

/* Append the address information to OUT.  */
static void
append_addresses (FILE *out, int af, int bits, int count, enum test_mode mode)
{
  int last = count * 256 + bits;
  if (mode == gai || mode == gai_canon)
    {
      if (af == AF_INET || af == AF_UNSPEC)
        fprintf (out, "address: STREAM/TCP 192.168.%d.%d 80\n", count, bits);
      if (af == AF_INET6 || af == AF_UNSPEC)
        {
          if (last == 0)
            fprintf (out, "address: STREAM/TCP 2001:db8:: 80\n");
          else
            fprintf (out, "address: STREAM/TCP 2001:db8::%x 80\n", last);
        }
    }
  else
    {
      TEST_VERIFY (af != AF_UNSPEC);
      if (af == AF_INET)
        fprintf (out, "address: 192.168.%d.%d\n", count, bits);
      if (af == AF_INET6)
        {
          if (last == 0)
            fprintf (out, "address: 2001:db8::\n");
          else
            fprintf (out, "address: 2001:db8::%x\n", last);
        }
    }
}

/* Perform one test using a forward lookup.  */
static void
check_forward (int af, int bits, int count, enum test_mode mode)
{
  char *qname = xasprintf ("bits%d.count%d.example", bits, count);
  char *label = xasprintf ("af=%d bits=%d count=%d mode=%s qname=%s",
                           af, bits, count, test_mode_to_string (mode), qname);

  struct xmemstream expected;
  xopen_memstream (&expected);
  if (mode == gai_canon)
    fprintf (expected.out, "flags: AI_CANONNAME\n");
  append_names (expected.out, qname, bits, count, mode);
  append_addresses (expected.out, af, bits, count, mode);
  xfclose_memstream (&expected);

  if (mode == gai || mode == gai_canon)
    {
      struct addrinfo *ai;
      struct addrinfo hints =
        {
          .ai_family = af,
          .ai_socktype = SOCK_STREAM,
        };
      if (mode == gai_canon)
        hints.ai_flags |= AI_CANONNAME;
      int ret = getaddrinfo (qname, "80", &hints, &ai);
      check_addrinfo (label, ai, ret, expected.buffer);
      if (ret == 0)
        freeaddrinfo (ai);
    }
  else
    {
      struct hostent *e;
      if (mode == gai)
        {
          TEST_COMPARE (af, AF_INET);
          e = gethostbyname (qname);
        }
      else
        {
          if (af != AF_INET)
            TEST_COMPARE (af, AF_INET6);
          e = gethostbyname2 (qname, af);
        }
      check_hostent (label, e, expected.buffer);
    }

  free (expected.buffer);
  free (label);
  free (qname);
}

/* Perform one check using a reverse lookup.  */

static void
check_reverse (int af, int bits, int count)
{
  TEST_VERIFY (af == AF_INET || af == AF_INET6);

  char *label = xasprintf ("af=%d bits=%d count=%d", af, bits, count);
  char *fqdn = xasprintf ("bits%d.count%d.example", bits, count);

  struct xmemstream expected;
  xopen_memstream (&expected);
  fprintf (expected.out, "name: %s\n", fqdn);
  append_addresses (expected.out, af, bits, count, byname);
  xfclose_memstream (&expected);

  char addr[16] = { 0 };
  socklen_t addrlen;
  if (af == AF_INET)
    {
      addr[0] = 192;
      addr[1] = 168;
      addr[2] = count;
      addr[3] = bits;
      addrlen = 4;
    }
  else
    {
      addr[0] = 0x20;
      addr[1] = 0x01;
      addr[2] = 0x0d;
      addr[3] = 0xb8;
      addr[14] = count;
      addr[15] = bits;
      addrlen = 16;
    }

  struct hostent *e = gethostbyaddr (addr, addrlen, af);
  check_hostent (label, e, expected.buffer);

  /* getnameinfo check is different.  There is no generic check_*
     function for it.  */
  {
    struct sockaddr_in sin = { };
    struct sockaddr_in6 sin6 = { };
    void *sa;
    socklen_t salen;
    if (af == AF_INET)
      {
        sin.sin_family = AF_INET;
        memcpy (&sin.sin_addr, addr, addrlen);
        sin.sin_port = htons (80);
        sa = &sin;
        salen = sizeof (sin);
      }
    else
      {
        sin6.sin6_family = AF_INET6;
        memcpy (&sin6.sin6_addr, addr, addrlen);
        sin6.sin6_port = htons (80);
        sa = &sin6;
        salen = sizeof (sin6);
      }

    char host[64];
    char service[64];
    int ret = getnameinfo (sa, salen, host,
                           sizeof (host), service, sizeof (service),
                           NI_NAMEREQD | NI_NUMERICSERV);
    TEST_COMPARE (ret, 0);
    TEST_COMPARE_STRING (host, fqdn);
    TEST_COMPARE_STRING (service, "80");
  }

  free (expected.buffer);
  free (fqdn);
  free (label);
}

static int
do_test (void)
{
  struct resolv_test *obj = resolv_test_start
    ((struct resolv_redirect_config)
     {
       .response_callback = response
     });

  for (int count = 0; count <= 3; ++count)
    for (int bits = 0; bits <= 1 << (count * 2); ++bits)
      {
        if (count > 0 && bits == count)
          /* The last bits value is only checked if count == 0.  */
          continue;

        for (enum test_mode mode = 0; mode < test_mode_num; ++mode)
          {
            check_forward (AF_INET, bits, count, mode);
            if (mode != byname)
              check_forward (AF_INET6, bits, count, mode);
            if (mode == gai || mode == gai_canon)
              check_forward (AF_UNSPEC, bits, count, mode);
          }

        check_reverse (AF_INET, bits, count);
        check_reverse (AF_INET6, bits, count);
      }

  resolv_test_end (obj);

  return 0;
}

#include <support/test-driver.c>