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