xref: /illumos-gate/usr/src/lib/libresolv2/common/resolv/res_findzonecut.c (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1999 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* Import. */
19 
20 #include "port_before.h"
21 
22 #include <sys/param.h>
23 #include <sys/socket.h>
24 #include <sys/time.h>
25 
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <arpa/nameser.h>
29 
30 #include <errno.h>
31 #include <limits.h>
32 #include <netdb.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #include <isc/list.h>
39 
40 #include "port_after.h"
41 
42 #include <resolv.h>
43 
44 /* Data structures. */
45 
46 typedef struct rr_a {
47 	LINK(struct rr_a)	link;
48 	union res_sockaddr_union addr;
49 } rr_a;
50 typedef LIST(rr_a) rrset_a;
51 
52 typedef struct rr_ns {
53 	LINK(struct rr_ns)	link;
54 	const char *		name;
55 	unsigned int		flags;
56 	rrset_a			addrs;
57 } rr_ns;
58 typedef LIST(rr_ns) rrset_ns;
59 
60 #define	RR_NS_HAVE_V4		0x01
61 #define	RR_NS_HAVE_V6		0x02
62 
63 /* Forward. */
64 
65 static int	satisfy(res_state, const char *, rrset_ns *,
66 			union res_sockaddr_union *, int);
67 static int	add_addrs(res_state, rr_ns *,
68 			  union res_sockaddr_union *, int);
69 static int	get_soa(res_state, const char *, ns_class, int,
70 			char *, size_t, char *, size_t,
71 			rrset_ns *);
72 static int	get_ns(res_state, const char *, ns_class, int, rrset_ns *);
73 static int	get_glue(res_state, ns_class, int, rrset_ns *);
74 static int	save_ns(res_state, ns_msg *, ns_sect,
75 			const char *, ns_class, int, rrset_ns *);
76 static int	save_a(res_state, ns_msg *, ns_sect,
77 		       const char *, ns_class, int, rr_ns *);
78 static void	free_nsrrset(rrset_ns *);
79 static void	free_nsrr(rrset_ns *, rr_ns *);
80 static rr_ns *	find_ns(rrset_ns *, const char *);
81 static int	do_query(res_state, const char *, ns_class, ns_type,
82 			 u_char *, ns_msg *);
83 static void	res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
84 
85 /* Macros. */
86 
87 #define DPRINTF(x) do {\
88 		int save_errno = errno; \
89 		if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
90 		errno = save_errno; \
91 	} while (0)
92 
93 /* Public. */
94 
95 /*%
96  *	find enclosing zone for a <dname,class>, and some server addresses
97  *
98  * parameters:
99  *\li	res - resolver context to work within (is modified)
100  *\li	dname - domain name whose enclosing zone is desired
101  *\li	class - class of dname (and its enclosing zone)
102  *\li	zname - found zone name
103  *\li	zsize - allocated size of zname
104  *\li	addrs - found server addresses
105  *\li	naddrs - max number of addrs
106  *
107  * return values:
108  *\li	< 0 - an error occurred (check errno)
109  *\li	= 0 - zname is now valid, but addrs[] wasn't changed
110  *\li	> 0 - zname is now valid, and return value is number of addrs[] found
111  *
112  * notes:
113  *\li	this function calls res_nsend() which means it depends on correctly
114  *	functioning recursive nameservers (usually defined in /etc/resolv.conf
115  *	or its local equivilent).
116  *
117  *\li	we start by asking for an SOA<dname,class>.  if we get one as an
118  *	answer, that just means <dname,class> is a zone top, which is fine.
119  *	more than likely we'll be told to go pound sand, in the form of a
120  *	negative answer.
121  *
122  *\li	note that we are not prepared to deal with referrals since that would
123  *	only come from authority servers and our correctly functioning local
124  *	recursive server would have followed the referral and got us something
125  *	more definite.
126  *
127  *\li	if the authority section contains an SOA, this SOA should also be the
128  *	closest enclosing zone, since any intermediary zone cuts would've been
129  *	returned as referrals and dealt with by our correctly functioning local
130  *	recursive name server.  but an SOA in the authority section should NOT
131  *	match our dname (since that would have been returned in the answer
132  *	section).  an authority section SOA has to be "above" our dname.
133  *
134  *\li	however, since authority section SOA's were once optional, it's
135  *	possible that we'll have to go hunting for the enclosing SOA by
136  *	ripping labels off the front of our dname -- this is known as "doing
137  *	it the hard way."
138  *
139  *\li	ultimately we want some server addresses, which are ideally the ones
140  *	pertaining to the SOA.MNAME, but only if there is a matching NS RR.
141  *	so the second phase (after we find an SOA) is to go looking for the
142  *	NS RRset for that SOA's zone.
143  *
144  *\li	no answer section processed by this code is allowed to contain CNAME
145  *	or DNAME RR's.  for the SOA query this means we strip a label and
146  *	keep going.  for the NS and A queries this means we just give up.
147  */
148 
149 int
150 res_findzonecut(res_state statp, const char *dname, ns_class class, int opts,
151 		char *zname, size_t zsize, struct in_addr *addrs, int naddrs)
152 {
153 	int result, i;
154 	union res_sockaddr_union *u;
155 
156 
157 	opts |= RES_IPV4ONLY;
158 	opts &= ~RES_IPV6ONLY;
159 
160 	u = calloc(naddrs, sizeof(*u));
161 	if (u == NULL)
162 		return(-1);
163 
164 	result = res_findzonecut2(statp, dname, class, opts, zname, zsize,
165 				  u, naddrs);
166 
167 	for (i = 0; i < result; i++) {
168 		addrs[i] = u[i].sin.sin_addr;
169 	}
170 	free(u);
171 	return (result);
172 }
173 
174 int
175 res_findzonecut2(res_state statp, const char *dname, ns_class class, int opts,
176 		 char *zname, size_t zsize, union res_sockaddr_union *addrs,
177 		 int naddrs)
178 {
179 	char mname[NS_MAXDNAME];
180 	u_long save_pfcode;
181 	rrset_ns nsrrs;
182 	int n;
183 
184 	DPRINTF(("START dname='%s' class=%s, zsize=%ld, naddrs=%d",
185 		 dname, p_class(class), (long)zsize, naddrs));
186 	save_pfcode = statp->pfcode;
187 	statp->pfcode |= RES_PRF_HEAD2 | RES_PRF_HEAD1 | RES_PRF_HEADX |
188 			 RES_PRF_QUES | RES_PRF_ANS |
189 			 RES_PRF_AUTH | RES_PRF_ADD;
190 	INIT_LIST(nsrrs);
191 
192 	DPRINTF(("get the soa, and see if it has enough glue"));
193 	if ((n = get_soa(statp, dname, class, opts, zname, zsize,
194 			 mname, sizeof mname, &nsrrs)) < 0 ||
195 	    ((opts & RES_EXHAUSTIVE) == 0 &&
196 	     (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
197 		goto done;
198 
199 	DPRINTF(("get the ns rrset and see if it has enough glue"));
200 	if ((n = get_ns(statp, zname, class, opts, &nsrrs)) < 0 ||
201 	    ((opts & RES_EXHAUSTIVE) == 0 &&
202 	     (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
203 		goto done;
204 
205 	DPRINTF(("get the missing glue and see if it's finally enough"));
206 	if ((n = get_glue(statp, class, opts, &nsrrs)) >= 0)
207 		n = satisfy(statp, mname, &nsrrs, addrs, naddrs);
208 
209  done:
210 	DPRINTF(("FINISH n=%d (%s)", n, (n < 0) ? strerror(errno) : "OK"));
211 	free_nsrrset(&nsrrs);
212 	statp->pfcode = save_pfcode;
213 	return (n);
214 }
215 
216 /* Private. */
217 
218 static int
219 satisfy(res_state statp, const char *mname, rrset_ns *nsrrsp,
220 	union res_sockaddr_union *addrs, int naddrs)
221 {
222 	rr_ns *nsrr;
223 	int n, x;
224 
225 	n = 0;
226 	nsrr = find_ns(nsrrsp, mname);
227 	if (nsrr != NULL) {
228 		x = add_addrs(statp, nsrr, addrs, naddrs);
229 		addrs += x;
230 		naddrs -= x;
231 		n += x;
232 	}
233 	for (nsrr = HEAD(*nsrrsp);
234 	     nsrr != NULL && naddrs > 0;
235 	     nsrr = NEXT(nsrr, link))
236 		if (ns_samename(nsrr->name, mname) != 1) {
237 			x = add_addrs(statp, nsrr, addrs, naddrs);
238 			addrs += x;
239 			naddrs -= x;
240 			n += x;
241 		}
242 	DPRINTF(("satisfy(%s): %d", mname, n));
243 	return (n);
244 }
245 
246 static int
247 add_addrs(res_state statp, rr_ns *nsrr,
248 	  union res_sockaddr_union *addrs, int naddrs)
249 {
250 	rr_a *arr;
251 	int n = 0;
252 
253 	for (arr = HEAD(nsrr->addrs); arr != NULL; arr = NEXT(arr, link)) {
254 		if (naddrs <= 0)
255 			return (0);
256 		*addrs++ = arr->addr;
257 		naddrs--;
258 		n++;
259 	}
260 	DPRINTF(("add_addrs: %d", n));
261 	return (n);
262 }
263 
264 static int
265 get_soa(res_state statp, const char *dname, ns_class class, int opts,
266 	char *zname, size_t zsize, char *mname, size_t msize,
267 	rrset_ns *nsrrsp)
268 {
269 	char tname[NS_MAXDNAME];
270 	u_char *resp = NULL;
271 	int n, i, ancount, nscount;
272 	ns_sect sect;
273 	ns_msg msg;
274 	u_int rcode;
275 
276 	/*
277 	 * Find closest enclosing SOA, even if it's for the root zone.
278 	 */
279 
280 	/* First canonicalize dname (exactly one unescaped trailing "."). */
281 	if (ns_makecanon(dname, tname, sizeof tname) < 0)
282 		goto cleanup;
283 	dname = tname;
284 
285 	resp = malloc(NS_MAXMSG);
286 	if (resp == NULL)
287 		goto cleanup;
288 
289 	/* Now grovel the subdomains, hunting for an SOA answer or auth. */
290 	for (;;) {
291 		/* Leading or inter-label '.' are skipped here. */
292 		while (*dname == '.')
293 			dname++;
294 
295 		/* Is there an SOA? */
296 		n = do_query(statp, dname, class, ns_t_soa, resp, &msg);
297 		if (n < 0) {
298 			DPRINTF(("get_soa: do_query('%s', %s) failed (%d)",
299 				 dname, p_class(class), n));
300 			goto cleanup;
301 		}
302 		if (n > 0) {
303 			DPRINTF(("get_soa: CNAME or DNAME found"));
304 			sect = ns_s_max, n = 0;
305 		} else {
306 			rcode = ns_msg_getflag(msg, ns_f_rcode);
307 			ancount = ns_msg_count(msg, ns_s_an);
308 			nscount = ns_msg_count(msg, ns_s_ns);
309 			if (ancount > 0 && rcode == ns_r_noerror)
310 				sect = ns_s_an, n = ancount;
311 			else if (nscount > 0)
312 				sect = ns_s_ns, n = nscount;
313 			else
314 				sect = ns_s_max, n = 0;
315 		}
316 		for (i = 0; i < n; i++) {
317 			const char *t;
318 			const u_char *rdata;
319 			ns_rr rr;
320 
321 			if (ns_parserr(&msg, sect, i, &rr) < 0) {
322 				DPRINTF(("get_soa: ns_parserr(%s, %d) failed",
323 					 p_section(sect, ns_o_query), i));
324 				goto cleanup;
325 			}
326 			if (ns_rr_type(rr) == ns_t_cname ||
327 			    ns_rr_type(rr) == ns_t_dname)
328 				break;
329 			if (ns_rr_type(rr) != ns_t_soa ||
330 			    ns_rr_class(rr) != class)
331 				continue;
332 			t = ns_rr_name(rr);
333 			switch (sect) {
334 			case ns_s_an:
335 				if (ns_samedomain(dname, t) == 0) {
336 					DPRINTF(
337 				    ("get_soa: ns_samedomain('%s', '%s') == 0",
338 						dname, t)
339 						);
340 					errno = EPROTOTYPE;
341 					goto cleanup;
342 				}
343 				break;
344 			case ns_s_ns:
345 				if (ns_samename(dname, t) == 1 ||
346 				    ns_samedomain(dname, t) == 0) {
347 					DPRINTF(
348 		       ("get_soa: ns_samename() || !ns_samedomain('%s', '%s')",
349 						dname, t)
350 						);
351 					errno = EPROTOTYPE;
352 					goto cleanup;
353 				}
354 				break;
355 			default:
356 				abort();
357 			}
358 			if (strlen(t) + 1 > zsize) {
359 				DPRINTF(("get_soa: zname(%lu) too small (%lu)",
360 					 (unsigned long)zsize,
361 					 (unsigned long)strlen(t) + 1));
362 				errno = EMSGSIZE;
363 				goto cleanup;
364 			}
365 			strcpy(zname, t);
366 			rdata = ns_rr_rdata(rr);
367 			if (ns_name_uncompress(resp, ns_msg_end(msg), rdata,
368 					       mname, msize) < 0) {
369 				DPRINTF(("get_soa: ns_name_uncompress failed")
370 					);
371 				goto cleanup;
372 			}
373 			if (save_ns(statp, &msg, ns_s_ns,
374 				    zname, class, opts, nsrrsp) < 0) {
375 				DPRINTF(("get_soa: save_ns failed"));
376 				goto cleanup;
377 			}
378 			free(resp);
379 			return (0);
380 		}
381 
382 		/* If we're out of labels, then not even "." has an SOA! */
383 		if (*dname == '\0')
384 			break;
385 
386 		/* Find label-terminating "."; top of loop will skip it. */
387 		while (*dname != '.') {
388 			if (*dname == '\\')
389 				if (*++dname == '\0') {
390 					errno = EMSGSIZE;
391 					goto cleanup;
392 				}
393 			dname++;
394 		}
395 	}
396 	DPRINTF(("get_soa: out of labels"));
397 	errno = EDESTADDRREQ;
398  cleanup:
399 	if (resp != NULL)
400 		free(resp);
401 	return (-1);
402 }
403 
404 static int
405 get_ns(res_state statp, const char *zname, ns_class class, int opts,
406       rrset_ns *nsrrsp)
407 {
408 	u_char *resp;
409 	ns_msg msg;
410 	int n;
411 
412 	resp = malloc(NS_MAXMSG);
413 	if (resp == NULL)
414 		return (-1);
415 
416 	/* Go and get the NS RRs for this zone. */
417 	n = do_query(statp, zname, class, ns_t_ns, resp, &msg);
418 	if (n != 0) {
419 		DPRINTF(("get_ns: do_query('%s', %s) failed (%d)",
420 			 zname, p_class(class), n));
421 		free(resp);
422 		return (-1);
423 	}
424 
425 	/* Remember the NS RRs and associated A RRs that came back. */
426 	if (save_ns(statp, &msg, ns_s_an, zname, class, opts, nsrrsp) < 0) {
427 		DPRINTF(("get_ns save_ns('%s', %s) failed",
428 			 zname, p_class(class)));
429 		free(resp);
430 		return (-1);
431 	}
432 
433 	free(resp);
434 	return (0);
435 }
436 
437 static int
438 get_glue(res_state statp, ns_class class, int opts, rrset_ns *nsrrsp) {
439 	rr_ns *nsrr, *nsrr_n;
440 	u_char *resp;
441 
442 	resp = malloc(NS_MAXMSG);
443 	if (resp == NULL)
444 		return(-1);
445 
446 	/* Go and get the A RRs for each empty NS RR on our list. */
447 	for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = nsrr_n) {
448 		ns_msg msg;
449 		int n;
450 
451 		nsrr_n = NEXT(nsrr, link);
452 
453 		if ((nsrr->flags & RR_NS_HAVE_V4) == 0) {
454 			n = do_query(statp, nsrr->name, class, ns_t_a,
455 				     resp, &msg);
456 			if (n < 0) {
457 				DPRINTF(
458 				       ("get_glue: do_query('%s', %s') failed",
459 					nsrr->name, p_class(class)));
460 				goto cleanup;
461 			}
462 			if (n > 0) {
463 				DPRINTF((
464 			"get_glue: do_query('%s', %s') CNAME or DNAME found",
465 					 nsrr->name, p_class(class)));
466 			}
467 			if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
468 				   opts, nsrr) < 0) {
469 				DPRINTF(("get_glue: save_r('%s', %s) failed",
470 					 nsrr->name, p_class(class)));
471 				goto cleanup;
472 			}
473 		}
474 
475 		if ((nsrr->flags & RR_NS_HAVE_V6) == 0) {
476 			n = do_query(statp, nsrr->name, class, ns_t_aaaa,
477 				     resp, &msg);
478 			if (n < 0) {
479 				DPRINTF(
480 				       ("get_glue: do_query('%s', %s') failed",
481 					nsrr->name, p_class(class)));
482 				goto cleanup;
483 			}
484 			if (n > 0) {
485 				DPRINTF((
486 			"get_glue: do_query('%s', %s') CNAME or DNAME found",
487 					 nsrr->name, p_class(class)));
488 			}
489 			if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
490 				   opts, nsrr) < 0) {
491 				DPRINTF(("get_glue: save_r('%s', %s) failed",
492 					 nsrr->name, p_class(class)));
493 				goto cleanup;
494 			}
495 		}
496 
497 		/* If it's still empty, it's just chaff. */
498 		if (EMPTY(nsrr->addrs)) {
499 			DPRINTF(("get_glue: removing empty '%s' NS",
500 				 nsrr->name));
501 			free_nsrr(nsrrsp, nsrr);
502 		}
503 	}
504 	free(resp);
505 	return (0);
506 
507  cleanup:
508 	free(resp);
509 	return (-1);
510 }
511 
512 static int
513 save_ns(res_state statp, ns_msg *msg, ns_sect sect,
514 	const char *owner, ns_class class, int opts,
515 	rrset_ns *nsrrsp)
516 {
517 	int i;
518 
519 	for (i = 0; i < ns_msg_count(*msg, sect); i++) {
520 		char tname[MAXDNAME];
521 		const u_char *rdata;
522 		rr_ns *nsrr;
523 		ns_rr rr;
524 
525 		if (ns_parserr(msg, sect, i, &rr) < 0) {
526 			DPRINTF(("save_ns: ns_parserr(%s, %d) failed",
527 				 p_section(sect, ns_o_query), i));
528 			return (-1);
529 		}
530 		if (ns_rr_type(rr) != ns_t_ns ||
531 		    ns_rr_class(rr) != class ||
532 		    ns_samename(ns_rr_name(rr), owner) != 1)
533 			continue;
534 		nsrr = find_ns(nsrrsp, ns_rr_name(rr));
535 		if (nsrr == NULL) {
536 			nsrr = malloc(sizeof *nsrr);
537 			if (nsrr == NULL) {
538 				DPRINTF(("save_ns: malloc failed"));
539 				return (-1);
540 			}
541 			rdata = ns_rr_rdata(rr);
542 			if (ns_name_uncompress(ns_msg_base(*msg),
543 					       ns_msg_end(*msg), rdata,
544 					       tname, sizeof tname) < 0) {
545 				DPRINTF(("save_ns: ns_name_uncompress failed")
546 					);
547 				free(nsrr);
548 				return (-1);
549 			}
550 			nsrr->name = strdup(tname);
551 			if (nsrr->name == NULL) {
552 				DPRINTF(("save_ns: strdup failed"));
553 				free(nsrr);
554 				return (-1);
555 			}
556 			INIT_LINK(nsrr, link);
557 			INIT_LIST(nsrr->addrs);
558 			nsrr->flags = 0;
559 			APPEND(*nsrrsp, nsrr, link);
560 		}
561 		if (save_a(statp, msg, ns_s_ar,
562 			   nsrr->name, class, opts, nsrr) < 0) {
563 			DPRINTF(("save_ns: save_r('%s', %s) failed",
564 				 nsrr->name, p_class(class)));
565 			return (-1);
566 		}
567 	}
568 	return (0);
569 }
570 
571 static int
572 save_a(res_state statp, ns_msg *msg, ns_sect sect,
573        const char *owner, ns_class class, int opts,
574        rr_ns *nsrr)
575 {
576 	int i;
577 
578 	for (i = 0; i < ns_msg_count(*msg, sect); i++) {
579 		ns_rr rr;
580 		rr_a *arr;
581 
582 		if (ns_parserr(msg, sect, i, &rr) < 0) {
583 			DPRINTF(("save_a: ns_parserr(%s, %d) failed",
584 				 p_section(sect, ns_o_query), i));
585 			return (-1);
586 		}
587 		if ((ns_rr_type(rr) != ns_t_a &&
588 		     ns_rr_type(rr) != ns_t_aaaa) ||
589 		    ns_rr_class(rr) != class ||
590 		    ns_samename(ns_rr_name(rr), owner) != 1 ||
591 		    ns_rr_rdlen(rr) != NS_INADDRSZ)
592 			continue;
593 		if ((opts & RES_IPV6ONLY) != 0 && ns_rr_type(rr) != ns_t_aaaa)
594 			continue;
595 		if ((opts & RES_IPV4ONLY) != 0 && ns_rr_type(rr) != ns_t_a)
596 			continue;
597 		arr = malloc(sizeof *arr);
598 		if (arr == NULL) {
599 			DPRINTF(("save_a: malloc failed"));
600 			return (-1);
601 		}
602 		INIT_LINK(arr, link);
603 		memset(&arr->addr, 0, sizeof(arr->addr));
604 		switch (ns_rr_type(rr)) {
605 		case ns_t_a:
606 			arr->addr.sin.sin_family = AF_INET;
607 #ifdef HAVE_SA_LEN
608 			arr->addr.sin.sin_len = sizeof(arr->addr.sin);
609 #endif
610 			memcpy(&arr->addr.sin.sin_addr, ns_rr_rdata(rr),
611 			       NS_INADDRSZ);
612 			arr->addr.sin.sin_port = htons(NAMESERVER_PORT);
613 			nsrr->flags |= RR_NS_HAVE_V4;
614 			break;
615 		case ns_t_aaaa:
616 			arr->addr.sin6.sin6_family = AF_INET6;
617 #ifdef HAVE_SA_LEN
618 			arr->addr.sin6.sin6_len = sizeof(arr->addr.sin6);
619 #endif
620 			memcpy(&arr->addr.sin6.sin6_addr, ns_rr_rdata(rr), 16);
621 			arr->addr.sin.sin_port = htons(NAMESERVER_PORT);
622 			nsrr->flags |= RR_NS_HAVE_V6;
623 			break;
624 		default:
625 			abort();
626 		}
627 		APPEND(nsrr->addrs, arr, link);
628 	}
629 	return (0);
630 }
631 
632 static void
633 free_nsrrset(rrset_ns *nsrrsp) {
634 	rr_ns *nsrr;
635 
636 	while ((nsrr = HEAD(*nsrrsp)) != NULL)
637 		free_nsrr(nsrrsp, nsrr);
638 }
639 
640 static void
641 free_nsrr(rrset_ns *nsrrsp, rr_ns *nsrr) {
642 	rr_a *arr;
643 	char *tmp;
644 
645 	while ((arr = HEAD(nsrr->addrs)) != NULL) {
646 		UNLINK(nsrr->addrs, arr, link);
647 		free(arr);
648 	}
649 	DE_CONST(nsrr->name, tmp);
650 	free(tmp);
651 	UNLINK(*nsrrsp, nsrr, link);
652 	free(nsrr);
653 }
654 
655 static rr_ns *
656 find_ns(rrset_ns *nsrrsp, const char *dname) {
657 	rr_ns *nsrr;
658 
659 	for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = NEXT(nsrr, link))
660 		if (ns_samename(nsrr->name, dname) == 1)
661 			return (nsrr);
662 	return (NULL);
663 }
664 
665 static int
666 do_query(res_state statp, const char *dname, ns_class class, ns_type qtype,
667 	 u_char *resp, ns_msg *msg)
668 {
669 	u_char req[NS_PACKETSZ];
670 	int i, n;
671 
672 	n = res_nmkquery(statp, ns_o_query, dname, class, qtype,
673 			 NULL, 0, NULL, req, NS_PACKETSZ);
674 	if (n < 0) {
675 		DPRINTF(("do_query: res_nmkquery failed"));
676 		return (-1);
677 	}
678 	n = res_nsend(statp, req, n, resp, NS_MAXMSG);
679 	if (n < 0) {
680 		DPRINTF(("do_query: res_nsend failed"));
681 		return (-1);
682 	}
683 	if (n == 0) {
684 		DPRINTF(("do_query: res_nsend returned 0"));
685 		errno = EMSGSIZE;
686 		return (-1);
687 	}
688 	if (ns_initparse(resp, n, msg) < 0) {
689 		DPRINTF(("do_query: ns_initparse failed"));
690 		return (-1);
691 	}
692 	n = 0;
693 	for (i = 0; i < ns_msg_count(*msg, ns_s_an); i++) {
694 		ns_rr rr;
695 
696 		if (ns_parserr(msg, ns_s_an, i, &rr) < 0) {
697 			DPRINTF(("do_query: ns_parserr failed"));
698 			return (-1);
699 		}
700 		n += (ns_rr_class(rr) == class &&
701 		      (ns_rr_type(rr) == ns_t_cname ||
702 		       ns_rr_type(rr) == ns_t_dname));
703 	}
704 	return (n);
705 }
706 
707 static void
708 res_dprintf(const char *fmt, ...) {
709 	va_list ap;
710 
711 	va_start(ap, fmt);
712 	fputs(";; res_findzonecut: ", stderr);
713 	vfprintf(stderr, fmt, ap);
714 	fputc('\n', stderr);
715 	va_end(ap);
716 }
717 
718 /*! \file */
719