xref: /freebsd/lib/libcasper/services/cap_dns/tests/dns_test.c (revision 31d62a73c2e6ac0ff413a7a17700ffc7dce254ef)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Pawel Jakub Dawidek under sponsorship from
8  * the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/capsicum.h>
36 #include <sys/nv.h>
37 
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
40 
41 #include <assert.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <netdb.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include <libcasper.h>
51 
52 #include <casper/cap_dns.h>
53 
54 static int ntest = 1;
55 
56 #define CHECK(expr)     do {						\
57 	if ((expr))							\
58 		printf("ok %d # %s:%u\n", ntest, __FILE__, __LINE__);	\
59 	else								\
60 		printf("not ok %d # %s:%u\n", ntest, __FILE__, __LINE__); \
61 	fflush(stdout);							\
62 	ntest++;							\
63 } while (0)
64 #define CHECKX(expr)     do {						\
65 	if ((expr)) {							\
66 		printf("ok %d # %s:%u\n", ntest, __FILE__, __LINE__);	\
67 	} else {							\
68 		printf("not ok %d # %s:%u\n", ntest, __FILE__, __LINE__); \
69 		exit(1);						\
70 	}								\
71 	fflush(stdout);							\
72 	ntest++;							\
73 } while (0)
74 
75 #define	GETHOSTBYNAME			0x01
76 #define	GETHOSTBYNAME2_AF_INET		0x02
77 #define	GETHOSTBYNAME2_AF_INET6		0x04
78 #define	GETHOSTBYADDR_AF_INET		0x08
79 #define	GETHOSTBYADDR_AF_INET6		0x10
80 #define	GETADDRINFO_AF_UNSPEC		0x20
81 #define	GETADDRINFO_AF_INET		0x40
82 #define	GETADDRINFO_AF_INET6		0x80
83 
84 static bool
85 addrinfo_compare(struct addrinfo *ai0, struct addrinfo *ai1)
86 {
87 	struct addrinfo *at0, *at1;
88 
89 	if (ai0 == NULL && ai1 == NULL)
90 		return (true);
91 	if (ai0 == NULL || ai1 == NULL)
92 		return (false);
93 
94 	at0 = ai0;
95 	at1 = ai1;
96 	while (true) {
97 		if ((at0->ai_flags == at1->ai_flags) &&
98 		    (at0->ai_family == at1->ai_family) &&
99 		    (at0->ai_socktype == at1->ai_socktype) &&
100 		    (at0->ai_protocol == at1->ai_protocol) &&
101 		    (at0->ai_addrlen == at1->ai_addrlen) &&
102 		    (memcmp(at0->ai_addr, at1->ai_addr,
103 			at0->ai_addrlen) == 0)) {
104 			if (at0->ai_canonname != NULL &&
105 			    at1->ai_canonname != NULL) {
106 				if (strcmp(at0->ai_canonname,
107 				    at1->ai_canonname) != 0) {
108 					return (false);
109 				}
110 			}
111 
112 			if (at0->ai_canonname == NULL &&
113 			    at1->ai_canonname != NULL) {
114 				return (false);
115 			}
116 			if (at0->ai_canonname != NULL &&
117 			    at1->ai_canonname == NULL) {
118 				return (false);
119 			}
120 
121 			if (at0->ai_next == NULL && at1->ai_next == NULL)
122 				return (true);
123 			if (at0->ai_next == NULL || at1->ai_next == NULL)
124 				return (false);
125 
126 			at0 = at0->ai_next;
127 			at1 = at1->ai_next;
128 		} else {
129 			return (false);
130 		}
131 	}
132 
133 	/* NOTREACHED */
134 	fprintf(stderr, "Dead code reached in addrinfo_compare()\n");
135 	exit(1);
136 }
137 
138 static bool
139 hostent_aliases_compare(char **aliases0, char **aliases1)
140 {
141 	int i0, i1;
142 
143 	if (aliases0 == NULL && aliases1 == NULL)
144 		return (true);
145 	if (aliases0 == NULL || aliases1 == NULL)
146 		return (false);
147 
148 	for (i0 = 0; aliases0[i0] != NULL; i0++) {
149 		for (i1 = 0; aliases1[i1] != NULL; i1++) {
150 			if (strcmp(aliases0[i0], aliases1[i1]) == 0)
151 				break;
152 		}
153 		if (aliases1[i1] == NULL)
154 			return (false);
155 	}
156 
157 	return (true);
158 }
159 
160 static bool
161 hostent_addr_list_compare(char **addr_list0, char **addr_list1, int length)
162 {
163 	int i0, i1;
164 
165 	if (addr_list0 == NULL && addr_list1 == NULL)
166 		return (true);
167 	if (addr_list0 == NULL || addr_list1 == NULL)
168 		return (false);
169 
170 	for (i0 = 0; addr_list0[i0] != NULL; i0++) {
171 		for (i1 = 0; addr_list1[i1] != NULL; i1++) {
172 			if (memcmp(addr_list0[i0], addr_list1[i1], length) == 0)
173 				break;
174 		}
175 		if (addr_list1[i1] == NULL)
176 			return (false);
177 	}
178 
179 	return (true);
180 }
181 
182 static bool
183 hostent_compare(const struct hostent *hp0, const struct hostent *hp1)
184 {
185 
186 	if (hp0 == NULL && hp1 != NULL)
187 		return (true);
188 
189 	if (hp0 == NULL || hp1 == NULL)
190 		return (false);
191 
192 	if (hp0->h_name != NULL || hp1->h_name != NULL) {
193 		if (hp0->h_name == NULL || hp1->h_name == NULL)
194 			return (false);
195 		if (strcmp(hp0->h_name, hp1->h_name) != 0)
196 			return (false);
197 	}
198 
199 	if (!hostent_aliases_compare(hp0->h_aliases, hp1->h_aliases))
200 		return (false);
201 	if (!hostent_aliases_compare(hp1->h_aliases, hp0->h_aliases))
202 		return (false);
203 
204 	if (hp0->h_addrtype != hp1->h_addrtype)
205 		return (false);
206 
207 	if (hp0->h_length != hp1->h_length)
208 		return (false);
209 
210 	if (!hostent_addr_list_compare(hp0->h_addr_list, hp1->h_addr_list,
211 	    hp0->h_length)) {
212 		return (false);
213 	}
214 	if (!hostent_addr_list_compare(hp1->h_addr_list, hp0->h_addr_list,
215 	    hp0->h_length)) {
216 		return (false);
217 	}
218 
219 	return (true);
220 }
221 
222 static unsigned int
223 runtest(cap_channel_t *capdns)
224 {
225 	unsigned int result;
226 	struct addrinfo *ais, *aic, hints, *hintsp;
227 	struct hostent *hps, *hpc;
228 	struct in_addr ip4;
229 	struct in6_addr ip6;
230 
231 	result = 0;
232 
233 	hps = gethostbyname("example.com");
234 	if (hps == NULL)
235 		fprintf(stderr, "Unable to resolve %s IPv4.\n", "example.com");
236 	hpc = cap_gethostbyname(capdns, "example.com");
237 	if (hostent_compare(hps, hpc))
238 		result |= GETHOSTBYNAME;
239 
240 	hps = gethostbyname2("example.com", AF_INET);
241 	if (hps == NULL)
242 		fprintf(stderr, "Unable to resolve %s IPv4.\n", "example.com");
243 	hpc = cap_gethostbyname2(capdns, "example.com", AF_INET);
244 	if (hostent_compare(hps, hpc))
245 		result |= GETHOSTBYNAME2_AF_INET;
246 
247 	hps = gethostbyname2("example.com", AF_INET6);
248 	if (hps == NULL)
249 		fprintf(stderr, "Unable to resolve %s IPv6.\n", "example.com");
250 	hpc = cap_gethostbyname2(capdns, "example.com", AF_INET6);
251 	if (hostent_compare(hps, hpc))
252 		result |= GETHOSTBYNAME2_AF_INET6;
253 
254 	hints.ai_flags = 0;
255 	hints.ai_family = AF_UNSPEC;
256 	hints.ai_socktype = 0;
257 	hints.ai_protocol = 0;
258 	hints.ai_addrlen = 0;
259 	hints.ai_addr = NULL;
260 	hints.ai_canonname = NULL;
261 	hints.ai_next = NULL;
262 
263 	hintsp = &hints;
264 
265 	if (getaddrinfo("freebsd.org", "25", hintsp, &ais) != 0) {
266 		fprintf(stderr,
267 		    "Unable to issue [system] getaddrinfo() for AF_UNSPEC: %s\n",
268 		    gai_strerror(errno));
269 	}
270 	if (cap_getaddrinfo(capdns, "freebsd.org", "25", hintsp, &aic) == 0) {
271 		if (addrinfo_compare(ais, aic))
272 			result |= GETADDRINFO_AF_UNSPEC;
273 		freeaddrinfo(ais);
274 		freeaddrinfo(aic);
275 	}
276 
277 	hints.ai_family = AF_INET;
278 	if (getaddrinfo("freebsd.org", "25", hintsp, &ais) != 0) {
279 		fprintf(stderr,
280 		    "Unable to issue [system] getaddrinfo() for AF_UNSPEC: %s\n",
281 		    gai_strerror(errno));
282 	}
283 	if (cap_getaddrinfo(capdns, "freebsd.org", "25", hintsp, &aic) == 0) {
284 		if (addrinfo_compare(ais, aic))
285 			result |= GETADDRINFO_AF_INET;
286 		freeaddrinfo(ais);
287 		freeaddrinfo(aic);
288 	}
289 
290 	hints.ai_family = AF_INET6;
291 	if (getaddrinfo("freebsd.org", "25", hintsp, &ais) != 0) {
292 		fprintf(stderr,
293 		    "Unable to issue [system] getaddrinfo() for AF_UNSPEC: %s\n",
294 		    gai_strerror(errno));
295 	}
296 	if (cap_getaddrinfo(capdns, "freebsd.org", "25", hintsp, &aic) == 0) {
297 		if (addrinfo_compare(ais, aic))
298 			result |= GETADDRINFO_AF_INET6;
299 		freeaddrinfo(ais);
300 		freeaddrinfo(aic);
301 	}
302 
303 	/* XXX: hardcoded addresses for "google-public-dns-a.google.com". */
304 #define	GOOGLE_DNS_IPV4	"8.8.8.8"
305 #define	GOOGLE_DNS_IPV6	"2001:4860:4860::8888"
306 
307 	inet_pton(AF_INET, GOOGLE_DNS_IPV4, &ip4);
308 	hps = gethostbyaddr(&ip4, sizeof(ip4), AF_INET);
309 	if (hps == NULL)
310 		fprintf(stderr, "Unable to resolve %s.\n", GOOGLE_DNS_IPV4);
311 	hpc = cap_gethostbyaddr(capdns, &ip4, sizeof(ip4), AF_INET);
312 	if (hostent_compare(hps, hpc))
313 		result |= GETHOSTBYADDR_AF_INET;
314 
315 	inet_pton(AF_INET6, GOOGLE_DNS_IPV6, &ip6);
316 	hps = gethostbyaddr(&ip6, sizeof(ip6), AF_INET6);
317 	if (hps == NULL) {
318 		fprintf(stderr, "Unable to resolve %s.\n", GOOGLE_DNS_IPV6);
319 	}
320 	hpc = cap_gethostbyaddr(capdns, &ip6, sizeof(ip6), AF_INET6);
321 	if (hostent_compare(hps, hpc))
322 		result |= GETHOSTBYADDR_AF_INET6;
323 	return (result);
324 }
325 
326 int
327 main(void)
328 {
329 	cap_channel_t *capcas, *capdns, *origcapdns;
330 	const char *types[2];
331 	int families[2];
332 
333 	printf("1..91\n");
334 	fflush(stdout);
335 
336 	capcas = cap_init();
337 	CHECKX(capcas != NULL);
338 
339 	origcapdns = capdns = cap_service_open(capcas, "system.dns");
340 	CHECKX(capdns != NULL);
341 
342 	cap_close(capcas);
343 
344 	/* No limits set. */
345 
346 	CHECK(runtest(capdns) ==
347 	    (GETHOSTBYNAME | GETHOSTBYNAME2_AF_INET | GETHOSTBYNAME2_AF_INET6 |
348 	     GETHOSTBYADDR_AF_INET | GETHOSTBYADDR_AF_INET6 |
349 	     GETADDRINFO_AF_UNSPEC | GETADDRINFO_AF_INET | GETADDRINFO_AF_INET6));
350 
351 	/*
352 	 * Allow:
353 	 * type: NAME, ADDR
354 	 * family: AF_INET, AF_INET6
355 	 */
356 
357 	capdns = cap_clone(origcapdns);
358 	CHECK(capdns != NULL);
359 
360 	types[0] = "NAME";
361 	types[1] = "ADDR";
362 	CHECK(cap_dns_type_limit(capdns, types, 2) == 0);
363 	families[0] = AF_INET;
364 	families[1] = AF_INET6;
365 	CHECK(cap_dns_family_limit(capdns, families, 2) == 0);
366 
367 	CHECK(runtest(capdns) ==
368 	    (GETHOSTBYNAME | GETHOSTBYNAME2_AF_INET | GETHOSTBYNAME2_AF_INET6 |
369 	     GETHOSTBYADDR_AF_INET | GETHOSTBYADDR_AF_INET6 |
370 	     GETADDRINFO_AF_INET | GETADDRINFO_AF_INET6));
371 
372 	cap_close(capdns);
373 
374 	/*
375 	 * Allow:
376 	 * type: NAME
377 	 * family: AF_INET, AF_INET6
378 	 */
379 
380 	capdns = cap_clone(origcapdns);
381 	CHECK(capdns != NULL);
382 
383 	types[0] = "NAME";
384 	CHECK(cap_dns_type_limit(capdns, types, 1) == 0);
385 	types[1] = "ADDR";
386 	CHECK(cap_dns_type_limit(capdns, types, 2) == -1 &&
387 	    errno == ENOTCAPABLE);
388 	types[0] = "ADDR";
389 	CHECK(cap_dns_type_limit(capdns, types, 1) == -1 &&
390 	    errno == ENOTCAPABLE);
391 	families[0] = AF_INET;
392 	families[1] = AF_INET6;
393 	CHECK(cap_dns_family_limit(capdns, families, 2) == 0);
394 
395 	CHECK(runtest(capdns) ==
396 	    (GETHOSTBYNAME | GETHOSTBYNAME2_AF_INET | GETHOSTBYNAME2_AF_INET6));
397 
398 	cap_close(capdns);
399 
400 	/*
401 	 * Allow:
402 	 * type: ADDR
403 	 * family: AF_INET, AF_INET6
404 	 */
405 
406 	capdns = cap_clone(origcapdns);
407 	CHECK(capdns != NULL);
408 
409 	types[0] = "ADDR";
410 	CHECK(cap_dns_type_limit(capdns, types, 1) == 0);
411 	types[1] = "NAME";
412 	CHECK(cap_dns_type_limit(capdns, types, 2) == -1 &&
413 	    errno == ENOTCAPABLE);
414 	types[0] = "NAME";
415 	CHECK(cap_dns_type_limit(capdns, types, 1) == -1 &&
416 	    errno == ENOTCAPABLE);
417 	families[0] = AF_INET;
418 	families[1] = AF_INET6;
419 	CHECK(cap_dns_family_limit(capdns, families, 2) == 0);
420 
421 	CHECK(runtest(capdns) ==
422 	    (GETHOSTBYADDR_AF_INET | GETHOSTBYADDR_AF_INET6 |
423 	    GETADDRINFO_AF_INET | GETADDRINFO_AF_INET6));
424 
425 	cap_close(capdns);
426 
427 	/*
428 	 * Allow:
429 	 * type: NAME, ADDR
430 	 * family: AF_INET
431 	 */
432 
433 	capdns = cap_clone(origcapdns);
434 	CHECK(capdns != NULL);
435 
436 	types[0] = "NAME";
437 	types[1] = "ADDR";
438 	CHECK(cap_dns_type_limit(capdns, types, 2) == 0);
439 	families[0] = AF_INET;
440 	CHECK(cap_dns_family_limit(capdns, families, 1) == 0);
441 	families[1] = AF_INET6;
442 	CHECK(cap_dns_family_limit(capdns, families, 2) == -1 &&
443 	    errno == ENOTCAPABLE);
444 	families[0] = AF_INET6;
445 	CHECK(cap_dns_family_limit(capdns, families, 1) == -1 &&
446 	    errno == ENOTCAPABLE);
447 
448 	CHECK(runtest(capdns) ==
449 	    (GETHOSTBYNAME | GETHOSTBYNAME2_AF_INET | GETHOSTBYADDR_AF_INET |
450 	    GETADDRINFO_AF_INET));
451 
452 	cap_close(capdns);
453 
454 	/*
455 	 * Allow:
456 	 * type: NAME, ADDR
457 	 * family: AF_INET6
458 	 */
459 
460 	capdns = cap_clone(origcapdns);
461 	CHECK(capdns != NULL);
462 
463 	types[0] = "NAME";
464 	types[1] = "ADDR";
465 	CHECK(cap_dns_type_limit(capdns, types, 2) == 0);
466 	families[0] = AF_INET6;
467 	CHECK(cap_dns_family_limit(capdns, families, 1) == 0);
468 	families[1] = AF_INET;
469 	CHECK(cap_dns_family_limit(capdns, families, 2) == -1 &&
470 	    errno == ENOTCAPABLE);
471 	families[0] = AF_INET;
472 	CHECK(cap_dns_family_limit(capdns, families, 1) == -1 &&
473 	    errno == ENOTCAPABLE);
474 
475 	CHECK(runtest(capdns) ==
476 	    (GETHOSTBYNAME2_AF_INET6 | GETHOSTBYADDR_AF_INET6 |
477 	    GETADDRINFO_AF_INET6));
478 
479 	cap_close(capdns);
480 
481 	/* Below we also test further limiting capability. */
482 
483 	/*
484 	 * Allow:
485 	 * type: NAME
486 	 * family: AF_INET
487 	 */
488 
489 	capdns = cap_clone(origcapdns);
490 	CHECK(capdns != NULL);
491 
492 	types[0] = "NAME";
493 	types[1] = "ADDR";
494 	CHECK(cap_dns_type_limit(capdns, types, 2) == 0);
495 	families[0] = AF_INET;
496 	families[1] = AF_INET6;
497 	CHECK(cap_dns_family_limit(capdns, families, 2) == 0);
498 	types[0] = "NAME";
499 	CHECK(cap_dns_type_limit(capdns, types, 1) == 0);
500 	types[1] = "ADDR";
501 	CHECK(cap_dns_type_limit(capdns, types, 2) == -1 &&
502 	    errno == ENOTCAPABLE);
503 	types[0] = "ADDR";
504 	CHECK(cap_dns_type_limit(capdns, types, 1) == -1 &&
505 	    errno == ENOTCAPABLE);
506 	families[0] = AF_INET;
507 	CHECK(cap_dns_family_limit(capdns, families, 1) == 0);
508 	families[1] = AF_INET6;
509 	CHECK(cap_dns_family_limit(capdns, families, 2) == -1 &&
510 	    errno == ENOTCAPABLE);
511 	families[0] = AF_INET6;
512 	CHECK(cap_dns_family_limit(capdns, families, 1) == -1 &&
513 	    errno == ENOTCAPABLE);
514 
515 	CHECK(runtest(capdns) == (GETHOSTBYNAME | GETHOSTBYNAME2_AF_INET));
516 
517 	cap_close(capdns);
518 
519 	/*
520 	 * Allow:
521 	 * type: NAME
522 	 * family: AF_INET6
523 	 */
524 
525 	capdns = cap_clone(origcapdns);
526 	CHECK(capdns != NULL);
527 
528 	types[0] = "NAME";
529 	types[1] = "ADDR";
530 	CHECK(cap_dns_type_limit(capdns, types, 2) == 0);
531 	families[0] = AF_INET;
532 	families[1] = AF_INET6;
533 	CHECK(cap_dns_family_limit(capdns, families, 2) == 0);
534 	types[0] = "NAME";
535 	CHECK(cap_dns_type_limit(capdns, types, 1) == 0);
536 	types[1] = "ADDR";
537 	CHECK(cap_dns_type_limit(capdns, types, 2) == -1 &&
538 	    errno == ENOTCAPABLE);
539 	types[0] = "ADDR";
540 	CHECK(cap_dns_type_limit(capdns, types, 1) == -1 &&
541 	    errno == ENOTCAPABLE);
542 	families[0] = AF_INET6;
543 	CHECK(cap_dns_family_limit(capdns, families, 1) == 0);
544 	families[1] = AF_INET;
545 	CHECK(cap_dns_family_limit(capdns, families, 2) == -1 &&
546 	    errno == ENOTCAPABLE);
547 	families[0] = AF_INET;
548 	CHECK(cap_dns_family_limit(capdns, families, 1) == -1 &&
549 	    errno == ENOTCAPABLE);
550 
551 	CHECK(runtest(capdns) == GETHOSTBYNAME2_AF_INET6);
552 
553 	cap_close(capdns);
554 
555 	/*
556 	 * Allow:
557 	 * type: ADDR
558 	 * family: AF_INET
559 	 */
560 
561 	capdns = cap_clone(origcapdns);
562 	CHECK(capdns != NULL);
563 
564 	types[0] = "NAME";
565 	types[1] = "ADDR";
566 	CHECK(cap_dns_type_limit(capdns, types, 2) == 0);
567 	families[0] = AF_INET;
568 	families[1] = AF_INET6;
569 	CHECK(cap_dns_family_limit(capdns, families, 2) == 0);
570 	types[0] = "ADDR";
571 	CHECK(cap_dns_type_limit(capdns, types, 1) == 0);
572 	types[1] = "NAME";
573 	CHECK(cap_dns_type_limit(capdns, types, 2) == -1 &&
574 	    errno == ENOTCAPABLE);
575 	types[0] = "NAME";
576 	CHECK(cap_dns_type_limit(capdns, types, 1) == -1 &&
577 	    errno == ENOTCAPABLE);
578 	families[0] = AF_INET;
579 	CHECK(cap_dns_family_limit(capdns, families, 1) == 0);
580 	families[1] = AF_INET6;
581 	CHECK(cap_dns_family_limit(capdns, families, 2) == -1 &&
582 	    errno == ENOTCAPABLE);
583 	families[0] = AF_INET6;
584 	CHECK(cap_dns_family_limit(capdns, families, 1) == -1 &&
585 	    errno == ENOTCAPABLE);
586 
587 	CHECK(runtest(capdns) == (GETHOSTBYADDR_AF_INET | GETADDRINFO_AF_INET));
588 
589 	cap_close(capdns);
590 
591 	/*
592 	 * Allow:
593 	 * type: ADDR
594 	 * family: AF_INET6
595 	 */
596 
597 	capdns = cap_clone(origcapdns);
598 	CHECK(capdns != NULL);
599 
600 	types[0] = "NAME";
601 	types[1] = "ADDR";
602 	CHECK(cap_dns_type_limit(capdns, types, 2) == 0);
603 	families[0] = AF_INET;
604 	families[1] = AF_INET6;
605 	CHECK(cap_dns_family_limit(capdns, families, 2) == 0);
606 	types[0] = "ADDR";
607 	CHECK(cap_dns_type_limit(capdns, types, 1) == 0);
608 	types[1] = "NAME";
609 	CHECK(cap_dns_type_limit(capdns, types, 2) == -1 &&
610 	    errno == ENOTCAPABLE);
611 	types[0] = "NAME";
612 	CHECK(cap_dns_type_limit(capdns, types, 1) == -1 &&
613 	    errno == ENOTCAPABLE);
614 	families[0] = AF_INET6;
615 	CHECK(cap_dns_family_limit(capdns, families, 1) == 0);
616 	families[1] = AF_INET;
617 	CHECK(cap_dns_family_limit(capdns, families, 2) == -1 &&
618 	    errno == ENOTCAPABLE);
619 	families[0] = AF_INET;
620 	CHECK(cap_dns_family_limit(capdns, families, 1) == -1 &&
621 	    errno == ENOTCAPABLE);
622 
623 	CHECK(runtest(capdns) == (GETHOSTBYADDR_AF_INET6 |
624 	    GETADDRINFO_AF_INET6));
625 
626 	cap_close(capdns);
627 
628 	/* Trying to rise the limits. */
629 
630 	capdns = cap_clone(origcapdns);
631 	CHECK(capdns != NULL);
632 
633 	types[0] = "NAME";
634 	CHECK(cap_dns_type_limit(capdns, types, 1) == 0);
635 	families[0] = AF_INET;
636 	CHECK(cap_dns_family_limit(capdns, families, 1) == 0);
637 
638 	types[0] = "NAME";
639 	types[1] = "ADDR";
640 	CHECK(cap_dns_type_limit(capdns, types, 2) == -1 &&
641 	    errno == ENOTCAPABLE);
642 	families[0] = AF_INET;
643 	families[1] = AF_INET6;
644 	CHECK(cap_dns_family_limit(capdns, families, 2) == -1 &&
645 	    errno == ENOTCAPABLE);
646 
647 	types[0] = "ADDR";
648 	CHECK(cap_dns_type_limit(capdns, types, 1) == -1 &&
649 	    errno == ENOTCAPABLE);
650 	families[0] = AF_INET6;
651 	CHECK(cap_dns_family_limit(capdns, families, 1) == -1 &&
652 	    errno == ENOTCAPABLE);
653 
654 	CHECK(cap_dns_type_limit(capdns, NULL, 0) == -1 &&
655 	    errno == ENOTCAPABLE);
656 	CHECK(cap_dns_family_limit(capdns, NULL, 0) == -1 &&
657 	    errno == ENOTCAPABLE);
658 
659 	/* Do the limits still hold? */
660 	CHECK(runtest(capdns) == (GETHOSTBYNAME | GETHOSTBYNAME2_AF_INET));
661 
662 	cap_close(capdns);
663 
664 	capdns = cap_clone(origcapdns);
665 	CHECK(capdns != NULL);
666 
667 	types[0] = "ADDR";
668 	CHECK(cap_dns_type_limit(capdns, types, 1) == 0);
669 	families[0] = AF_INET6;
670 	CHECK(cap_dns_family_limit(capdns, families, 1) == 0);
671 
672 	types[0] = "NAME";
673 	types[1] = "ADDR";
674 	CHECK(cap_dns_type_limit(capdns, types, 2) == -1 &&
675 	    errno == ENOTCAPABLE);
676 	families[0] = AF_INET;
677 	families[1] = AF_INET6;
678 	CHECK(cap_dns_family_limit(capdns, families, 2) == -1 &&
679 	    errno == ENOTCAPABLE);
680 
681 	types[0] = "NAME";
682 	CHECK(cap_dns_type_limit(capdns, types, 1) == -1 &&
683 	    errno == ENOTCAPABLE);
684 	families[0] = AF_INET;
685 	CHECK(cap_dns_family_limit(capdns, families, 1) == -1 &&
686 	    errno == ENOTCAPABLE);
687 
688 	CHECK(cap_dns_type_limit(capdns, NULL, 0) == -1 &&
689 	    errno == ENOTCAPABLE);
690 	CHECK(cap_dns_family_limit(capdns, NULL, 0) == -1 &&
691 	    errno == ENOTCAPABLE);
692 
693 	/* Do the limits still hold? */
694 	CHECK(runtest(capdns) == (GETHOSTBYADDR_AF_INET6 |
695 	    GETADDRINFO_AF_INET6));
696 
697 	cap_close(capdns);
698 
699 	cap_close(origcapdns);
700 
701 	exit(0);
702 }
703