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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
/* Test basic nss_dns functionality and the resolver test harness itself.
Copyright (C) 2016-2017 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
<http://www.gnu.org/licenses/>. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/check_nss.h>
#include <support/format_nss.h>
#include <support/resolv_test.h>
#include <support/support.h>
#define LONG_NAME \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax." \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay." \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz." \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaat"
static void
response (const struct resolv_response_context *ctx,
struct resolv_response_builder *b,
const char *qname, uint16_t qclass, uint16_t qtype)
{
TEST_VERIFY_EXIT (qname != NULL);
/* The "t." prefix can be used to request TCP fallback. */
bool force_tcp;
if (strncmp ("t.", qname, 2) == 0)
force_tcp = true;
else
force_tcp = false;
const char *qname_compare;
if (force_tcp)
qname_compare = qname + 2;
else
qname_compare = qname;
enum {www, alias, nxdomain, long_name, nodata} requested_qname;
if (strcmp (qname_compare, "www.example") == 0)
requested_qname = www;
else if (strcmp (qname_compare, "alias.example") == 0)
requested_qname = alias;
else if (strcmp (qname_compare, "nxdomain.example") == 0)
requested_qname = nxdomain;
else if (strcmp (qname_compare, LONG_NAME) == 0)
requested_qname = long_name;
else if (strcmp (qname_compare, "nodata.example") == 0)
requested_qname = nodata;
else
{
support_record_failure ();
printf ("error: unexpected QNAME: %s\n", qname);
return;
}
TEST_VERIFY_EXIT (qclass == C_IN);
struct resolv_response_flags flags = {.tc = force_tcp && !ctx->tcp};
if (requested_qname == nxdomain)
flags.rcode = 3; /* NXDOMAIN */
resolv_response_init (b, flags);
resolv_response_add_question (b, qname, qclass, qtype);
if (requested_qname == nxdomain || flags.tc)
return;
resolv_response_section (b, ns_s_an);
switch (requested_qname)
{
case www:
case long_name:
resolv_response_open_record (b, qname, qclass, qtype, 0);
break;
case alias:
resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
resolv_response_add_name (b, "www.example");
resolv_response_close_record (b);
resolv_response_open_record (b, "www.example", qclass, qtype, 0);
break;
case nodata:
return;
case nxdomain:
FAIL_EXIT1 ("unreachable");
}
switch (qtype)
{
case T_A:
{
char ipv4[4] = {192, 0, 2, 17};
ipv4[3] += requested_qname + 2 * ctx->tcp + 4 * ctx->server_index;
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, 0, 1};
ipv6[15] += requested_qname + 2 * ctx->tcp + 4 * ctx->server_index;
resolv_response_add_data (b, &ipv6, sizeof (ipv6));
}
break;
default:
support_record_failure ();
printf ("error: unexpected QTYPE: %s/%u/%u\n",
qname, qclass, qtype);
}
resolv_response_close_record (b);
}
static void
check_h (const char *name, int family, const char *expected)
{
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);
free (query);
}
bool too_small = true;
for (unsigned int offset = 0; offset < 8; ++offset)
for (unsigned int size = 1; too_small; ++size)
{
char *buf = xmalloc (offset + size);
too_small = false;
struct hostent hostbuf;
struct hostent *result;
int herror;
if (family == AF_INET)
{
char *query = xasprintf ("gethostbyname (\"%s\") %u/%u",
name, offset, size);
int ret = gethostbyname_r
(name, &hostbuf, buf + offset, size, &result, &herror);
if (ret == 0)
{
h_errno = herror;
check_hostent (query, result, expected);
}
else if (ret == ERANGE)
too_small = true;
else
{
errno = ret;
FAIL_EXIT1 ("gethostbyname_r: %m");
}
free (query);
memset (buf, 0, offset + size);
}
char *query = xasprintf ("gethostbyname2 (\"%s\", %d) %u/%u",
name, family, offset, size);
int ret = gethostbyname2_r
(name, family, &hostbuf, buf + offset, size, &result, &herror);
if (ret == 0)
{
h_errno = herror;
check_hostent (query, result, expected);
}
else if (ret == ERANGE)
too_small = true;
else
{
errno = ret;
FAIL_EXIT1 ("gethostbyname_r: %m");
}
free (buf);
free (query);
}
}
static void
check_ai_hints (const char *name, const char *service,
struct addrinfo hints, const char *expected)
{
struct addrinfo *ai;
char *query = xasprintf ("%s:%s [%d]/0x%x", name, service,
hints.ai_family, hints.ai_flags);
int ret = getaddrinfo (name, service, &hints, &ai);
check_addrinfo (query, ai, ret, expected);
if (ret == 0)
freeaddrinfo (ai);
free (query);
}
static void
check_ai (const char *name, const char *service,
int family, const char *expected)
{
return check_ai_hints (name, service,
(struct addrinfo) { .ai_family = family, },
expected);
}
/* Test for bug 21295: getaddrinfo used to discard address information
instead of merging it. */
static void
test_bug_21295 (void)
{
/* The address order is unpredictable. There are two factors which
contribute to that: The stub resolver does not perform proper
response matching for A/AAAA queries (an A response could be
associated with an AAAA query and vice versa), and without
namespaces, system configuration could affect address
ordering. */
for (int do_tcp = 0; do_tcp < 2; ++do_tcp)
{
const struct addrinfo hints =
{
.ai_family = AF_INET6,
.ai_socktype = SOCK_STREAM,
.ai_flags = AI_V4MAPPED | AI_ALL,
};
const char *qname;
if (do_tcp)
qname = "t.www.example";
else
qname = "www.example";
struct addrinfo *ai = NULL;
int ret = getaddrinfo (qname, "80", &hints, &ai);
TEST_VERIFY_EXIT (ret == 0);
const char *expected_a;
const char *expected_b;
if (do_tcp)
{
expected_a = "flags: AI_V4MAPPED AI_ALL\n"
"address: STREAM/TCP 2001:db8::3 80\n"
"address: STREAM/TCP ::ffff:192.0.2.19 80\n";
expected_b = "flags: AI_V4MAPPED AI_ALL\n"
"address: STREAM/TCP ::ffff:192.0.2.19 80\n"
"address: STREAM/TCP 2001:db8::3 80\n";
}
else
{
expected_a = "flags: AI_V4MAPPED AI_ALL\n"
"address: STREAM/TCP 2001:db8::1 80\n"
"address: STREAM/TCP ::ffff:192.0.2.17 80\n";
expected_b = "flags: AI_V4MAPPED AI_ALL\n"
"address: STREAM/TCP ::ffff:192.0.2.17 80\n"
"address: STREAM/TCP 2001:db8::1 80\n";
}
char *actual = support_format_addrinfo (ai, ret);
if (!(strcmp (actual, expected_a) == 0
|| strcmp (actual, expected_b) == 0))
{
support_record_failure ();
printf ("error: %s: unexpected response (TCP: %d):\n%s\n",
__func__, do_tcp, actual);
}
free (actual);
freeaddrinfo (ai);
}
}
/* Run tests which do not expect any data. */
static void
test_nodata_nxdomain (void)
{
/* Iterate through different address families. */
int families[] = { AF_UNSPEC, AF_INET, AF_INET6, -1 };
for (int i = 0; families[i] >= 0; ++i)
/* If do_tcp, prepend "t." to the name to trigger TCP
fallback. */
for (int do_tcp = 0; do_tcp < 2; ++do_tcp)
/* If do_nxdomain, trigger an NXDOMAIN error (DNS failure),
otherwise use a NODATA response (empty but successful
answer). */
for (int do_nxdomain = 0; do_nxdomain < 2; ++do_nxdomain)
{
int family = families[i];
char *name = xasprintf ("%s%s.example",
do_tcp ? "t." : "",
do_nxdomain ? "nxdomain" : "nodata");
if (family != AF_UNSPEC)
{
if (do_nxdomain)
check_h (name, family, "error: HOST_NOT_FOUND\n");
else
check_h (name, family, "error: NO_ADDRESS\n");
}
const char *expected;
if (do_nxdomain)
expected = "error: Name or service not known\n";
else
expected = "error: No address associated with hostname\n";
check_ai (name, "80", family, expected);
struct addrinfo hints =
{
.ai_family = family,
.ai_flags = AI_V4MAPPED | AI_ALL,
};
check_ai_hints (name, "80", hints, expected);
hints.ai_flags |= AI_CANONNAME;
check_ai_hints (name, "80", hints, expected);
free (name);
}
}
static int
do_test (void)
{
struct resolv_test *aux = resolv_test_start
((struct resolv_redirect_config)
{
.response_callback = response,
});
check_h ("www.example", AF_INET,
"name: www.example\n"
"address: 192.0.2.17\n");
check_h ("alias.example", AF_INET,
"name: www.example\n"
"alias: alias.example\n"
"address: 192.0.2.18\n");
check_h ("www.example", AF_INET6,
"name: www.example\n"
"address: 2001:db8::1\n");
check_h ("alias.example", AF_INET6,
"name: www.example\n"
"alias: alias.example\n"
"address: 2001:db8::2\n");
check_h (LONG_NAME, AF_INET,
"name: " LONG_NAME "\n"
"address: 192.0.2.20\n");
check_ai ("www.example", "80", AF_UNSPEC,
"address: STREAM/TCP 192.0.2.17 80\n"
"address: DGRAM/UDP 192.0.2.17 80\n"
"address: RAW/IP 192.0.2.17 80\n"
"address: STREAM/TCP 2001:db8::1 80\n"
"address: DGRAM/UDP 2001:db8::1 80\n"
"address: RAW/IP 2001:db8::1 80\n");
check_ai_hints ("www.example", "80",
(struct addrinfo) { .ai_family = AF_UNSPEC,
.ai_flags = AI_CANONNAME, },
"flags: AI_CANONNAME\n"
"canonname: www.example\n"
"address: STREAM/TCP 192.0.2.17 80\n"
"address: DGRAM/UDP 192.0.2.17 80\n"
"address: RAW/IP 192.0.2.17 80\n"
"address: STREAM/TCP 2001:db8::1 80\n"
"address: DGRAM/UDP 2001:db8::1 80\n"
"address: RAW/IP 2001:db8::1 80\n");
check_ai ("alias.example", "80", AF_UNSPEC,
"address: STREAM/TCP 192.0.2.18 80\n"
"address: DGRAM/UDP 192.0.2.18 80\n"
"address: RAW/IP 192.0.2.18 80\n"
"address: STREAM/TCP 2001:db8::2 80\n"
"address: DGRAM/UDP 2001:db8::2 80\n"
"address: RAW/IP 2001:db8::2 80\n");
check_ai_hints ("alias.example", "80",
(struct addrinfo) { .ai_family = AF_UNSPEC,
.ai_flags = AI_CANONNAME, },
"flags: AI_CANONNAME\n"
"canonname: www.example\n"
"address: STREAM/TCP 192.0.2.18 80\n"
"address: DGRAM/UDP 192.0.2.18 80\n"
"address: RAW/IP 192.0.2.18 80\n"
"address: STREAM/TCP 2001:db8::2 80\n"
"address: DGRAM/UDP 2001:db8::2 80\n"
"address: RAW/IP 2001:db8::2 80\n");
check_ai (LONG_NAME, "80", AF_UNSPEC,
"address: STREAM/TCP 192.0.2.20 80\n"
"address: DGRAM/UDP 192.0.2.20 80\n"
"address: RAW/IP 192.0.2.20 80\n"
"address: STREAM/TCP 2001:db8::4 80\n"
"address: DGRAM/UDP 2001:db8::4 80\n"
"address: RAW/IP 2001:db8::4 80\n");
check_ai ("www.example", "80", AF_INET,
"address: STREAM/TCP 192.0.2.17 80\n"
"address: DGRAM/UDP 192.0.2.17 80\n"
"address: RAW/IP 192.0.2.17 80\n");
check_ai_hints ("www.example", "80",
(struct addrinfo) { .ai_family = AF_INET,
.ai_flags = AI_CANONNAME, },
"flags: AI_CANONNAME\n"
"canonname: www.example\n"
"address: STREAM/TCP 192.0.2.17 80\n"
"address: DGRAM/UDP 192.0.2.17 80\n"
"address: RAW/IP 192.0.2.17 80\n");
check_ai ("alias.example", "80", AF_INET,
"address: STREAM/TCP 192.0.2.18 80\n"
"address: DGRAM/UDP 192.0.2.18 80\n"
"address: RAW/IP 192.0.2.18 80\n");
check_ai_hints ("alias.example", "80",
(struct addrinfo) { .ai_family = AF_INET,
.ai_flags = AI_CANONNAME, },
"flags: AI_CANONNAME\n"
"canonname: www.example\n"
"address: STREAM/TCP 192.0.2.18 80\n"
"address: DGRAM/UDP 192.0.2.18 80\n"
"address: RAW/IP 192.0.2.18 80\n");
check_ai (LONG_NAME, "80", AF_INET,
"address: STREAM/TCP 192.0.2.20 80\n"
"address: DGRAM/UDP 192.0.2.20 80\n"
"address: RAW/IP 192.0.2.20 80\n");
check_ai ("www.example", "80", AF_INET6,
"address: STREAM/TCP 2001:db8::1 80\n"
"address: DGRAM/UDP 2001:db8::1 80\n"
"address: RAW/IP 2001:db8::1 80\n");
check_ai_hints ("www.example", "80",
(struct addrinfo) { .ai_family = AF_INET6,
.ai_flags = AI_CANONNAME, },
"flags: AI_CANONNAME\n"
"canonname: www.example\n"
"address: STREAM/TCP 2001:db8::1 80\n"
"address: DGRAM/UDP 2001:db8::1 80\n"
"address: RAW/IP 2001:db8::1 80\n");
check_ai ("alias.example", "80", AF_INET6,
"address: STREAM/TCP 2001:db8::2 80\n"
"address: DGRAM/UDP 2001:db8::2 80\n"
"address: RAW/IP 2001:db8::2 80\n");
check_ai_hints ("alias.example", "80",
(struct addrinfo) { .ai_family = AF_INET6,
.ai_flags = AI_CANONNAME, },
"flags: AI_CANONNAME\n"
"canonname: www.example\n"
"address: STREAM/TCP 2001:db8::2 80\n"
"address: DGRAM/UDP 2001:db8::2 80\n"
"address: RAW/IP 2001:db8::2 80\n");
check_ai (LONG_NAME, "80", AF_INET6,
"address: STREAM/TCP 2001:db8::4 80\n"
"address: DGRAM/UDP 2001:db8::4 80\n"
"address: RAW/IP 2001:db8::4 80\n");
check_h ("t.www.example", AF_INET,
"name: t.www.example\n"
"address: 192.0.2.19\n");
check_h ("t.alias.example", AF_INET,
"name: www.example\n"
"alias: t.alias.example\n"
"address: 192.0.2.20\n");
check_h ("t.www.example", AF_INET6,
"name: t.www.example\n"
"address: 2001:db8::3\n");
check_h ("t.alias.example", AF_INET6,
"name: www.example\n"
"alias: t.alias.example\n"
"address: 2001:db8::4\n");
check_ai ("t.www.example", "80", AF_UNSPEC,
"address: STREAM/TCP 192.0.2.19 80\n"
"address: DGRAM/UDP 192.0.2.19 80\n"
"address: RAW/IP 192.0.2.19 80\n"
"address: STREAM/TCP 2001:db8::3 80\n"
"address: DGRAM/UDP 2001:db8::3 80\n"
"address: RAW/IP 2001:db8::3 80\n");
check_ai ("t.alias.example", "80", AF_UNSPEC,
"address: STREAM/TCP 192.0.2.20 80\n"
"address: DGRAM/UDP 192.0.2.20 80\n"
"address: RAW/IP 192.0.2.20 80\n"
"address: STREAM/TCP 2001:db8::4 80\n"
"address: DGRAM/UDP 2001:db8::4 80\n"
"address: RAW/IP 2001:db8::4 80\n");
check_ai ("t.www.example", "80", AF_INET,
"address: STREAM/TCP 192.0.2.19 80\n"
"address: DGRAM/UDP 192.0.2.19 80\n"
"address: RAW/IP 192.0.2.19 80\n");
check_ai ("t.alias.example", "80", AF_INET,
"address: STREAM/TCP 192.0.2.20 80\n"
"address: DGRAM/UDP 192.0.2.20 80\n"
"address: RAW/IP 192.0.2.20 80\n");
check_ai ("t.www.example", "80", AF_INET6,
"address: STREAM/TCP 2001:db8::3 80\n"
"address: DGRAM/UDP 2001:db8::3 80\n"
"address: RAW/IP 2001:db8::3 80\n");
check_ai ("t.alias.example", "80", AF_INET6,
"address: STREAM/TCP 2001:db8::4 80\n"
"address: DGRAM/UDP 2001:db8::4 80\n"
"address: RAW/IP 2001:db8::4 80\n");
test_bug_21295 ();
test_nodata_nxdomain ();
resolv_test_end (aux);
return 0;
}
#include <support/test-driver.c>
|