1 #if !defined(lint) && !defined(SABER)
2 static const char rcsid[] = "$Id: res_findzonecut.c,v 1.10 2005/10/11 00:10:16 marka Exp $";
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1999 by Internet Software Consortium.
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
27 #include "port_before.h"
29 #include <sys/param.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <arpa/nameser.h>
47 #include "port_after.h"
51 /* Data structures. */
54 LINK(struct rr_a) link;
55 union res_sockaddr_union addr;
57 typedef LIST(rr_a) rrset_a;
59 typedef struct rr_ns {
60 LINK(struct rr_ns) link;
65 typedef LIST(rr_ns) rrset_ns;
67 #define RR_NS_HAVE_V4 0x01
68 #define RR_NS_HAVE_V6 0x02
72 static int satisfy(res_state, const char *, rrset_ns *,
73 union res_sockaddr_union *, int);
74 static int add_addrs(res_state, rr_ns *,
75 union res_sockaddr_union *, int);
76 static int get_soa(res_state, const char *, ns_class, int,
77 char *, size_t, char *, size_t,
79 static int get_ns(res_state, const char *, ns_class, int, rrset_ns *);
80 static int get_glue(res_state, ns_class, int, rrset_ns *);
81 static int save_ns(res_state, ns_msg *, ns_sect,
82 const char *, ns_class, int, rrset_ns *);
83 static int save_a(res_state, ns_msg *, ns_sect,
84 const char *, ns_class, int, rr_ns *);
85 static void free_nsrrset(rrset_ns *);
86 static void free_nsrr(rrset_ns *, rr_ns *);
87 static rr_ns * find_ns(rrset_ns *, const char *);
88 static int do_query(res_state, const char *, ns_class, ns_type,
90 static void res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
94 #define DPRINTF(x) do {\
95 int save_errno = errno; \
96 if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
103 * find enclosing zone for a <dname,class>, and some server addresses
106 *\li res - resolver context to work within (is modified)
107 *\li dname - domain name whose enclosing zone is desired
108 *\li class - class of dname (and its enclosing zone)
109 *\li zname - found zone name
110 *\li zsize - allocated size of zname
111 *\li addrs - found server addresses
112 *\li naddrs - max number of addrs
115 *\li < 0 - an error occurred (check errno)
116 *\li = 0 - zname is now valid, but addrs[] wasn't changed
117 *\li > 0 - zname is now valid, and return value is number of addrs[] found
120 *\li this function calls res_nsend() which means it depends on correctly
121 * functioning recursive nameservers (usually defined in /etc/resolv.conf
122 * or its local equivalent).
124 *\li we start by asking for an SOA<dname,class>. if we get one as an
125 * answer, that just means <dname,class> is a zone top, which is fine.
126 * more than likely we'll be told to go pound sand, in the form of a
129 *\li note that we are not prepared to deal with referrals since that would
130 * only come from authority servers and our correctly functioning local
131 * recursive server would have followed the referral and got us something
134 *\li if the authority section contains an SOA, this SOA should also be the
135 * closest enclosing zone, since any intermediary zone cuts would've been
136 * returned as referrals and dealt with by our correctly functioning local
137 * recursive name server. but an SOA in the authority section should NOT
138 * match our dname (since that would have been returned in the answer
139 * section). an authority section SOA has to be "above" our dname.
141 *\li however, since authority section SOA's were once optional, it's
142 * possible that we'll have to go hunting for the enclosing SOA by
143 * ripping labels off the front of our dname -- this is known as "doing
146 *\li ultimately we want some server addresses, which are ideally the ones
147 * pertaining to the SOA.MNAME, but only if there is a matching NS RR.
148 * so the second phase (after we find an SOA) is to go looking for the
149 * NS RRset for that SOA's zone.
151 *\li no answer section processed by this code is allowed to contain CNAME
152 * or DNAME RR's. for the SOA query this means we strip a label and
153 * keep going. for the NS and A queries this means we just give up.
158 res_findzonecut(res_state statp, const char *dname, ns_class class, int opts,
159 char *zname, size_t zsize, struct in_addr *addrs, int naddrs)
162 union res_sockaddr_union *u;
165 opts |= RES_IPV4ONLY;
166 opts &= ~RES_IPV6ONLY;
168 u = calloc(naddrs, sizeof(*u));
172 result = res_findzonecut2(statp, dname, class, opts, zname, zsize,
175 for (i = 0; i < result; i++) {
176 addrs[i] = u[i].sin.sin_addr;
184 res_findzonecut2(res_state statp, const char *dname, ns_class class, int opts,
185 char *zname, size_t zsize, union res_sockaddr_union *addrs,
188 char mname[NS_MAXDNAME];
193 DPRINTF(("START dname='%s' class=%s, zsize=%ld, naddrs=%d",
194 dname, p_class(class), (long)zsize, naddrs));
195 save_pfcode = statp->pfcode;
196 statp->pfcode |= RES_PRF_HEAD2 | RES_PRF_HEAD1 | RES_PRF_HEADX |
197 RES_PRF_QUES | RES_PRF_ANS |
198 RES_PRF_AUTH | RES_PRF_ADD;
201 DPRINTF(("get the soa, and see if it has enough glue"));
202 if ((n = get_soa(statp, dname, class, opts, zname, zsize,
203 mname, sizeof mname, &nsrrs)) < 0 ||
204 ((opts & RES_EXHAUSTIVE) == 0 &&
205 (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
208 DPRINTF(("get the ns rrset and see if it has enough glue"));
209 if ((n = get_ns(statp, zname, class, opts, &nsrrs)) < 0 ||
210 ((opts & RES_EXHAUSTIVE) == 0 &&
211 (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
214 DPRINTF(("get the missing glue and see if it's finally enough"));
215 if ((n = get_glue(statp, class, opts, &nsrrs)) >= 0)
216 n = satisfy(statp, mname, &nsrrs, addrs, naddrs);
219 DPRINTF(("FINISH n=%d (%s)", n, (n < 0) ? strerror(errno) : "OK"));
220 free_nsrrset(&nsrrs);
221 statp->pfcode = save_pfcode;
228 satisfy(res_state statp, const char *mname, rrset_ns *nsrrsp,
229 union res_sockaddr_union *addrs, int naddrs)
235 nsrr = find_ns(nsrrsp, mname);
237 x = add_addrs(statp, nsrr, addrs, naddrs);
242 for (nsrr = HEAD(*nsrrsp);
243 nsrr != NULL && naddrs > 0;
244 nsrr = NEXT(nsrr, link))
245 if (ns_samename(nsrr->name, mname) != 1) {
246 x = add_addrs(statp, nsrr, addrs, naddrs);
251 DPRINTF(("satisfy(%s): %d", mname, n));
256 add_addrs(res_state statp, rr_ns *nsrr,
257 union res_sockaddr_union *addrs, int naddrs)
262 for (arr = HEAD(nsrr->addrs); arr != NULL; arr = NEXT(arr, link)) {
265 *addrs++ = arr->addr;
269 DPRINTF(("add_addrs: %d", n));
274 get_soa(res_state statp, const char *dname, ns_class class, int opts,
275 char *zname, size_t zsize, char *mname, size_t msize,
278 char tname[NS_MAXDNAME];
280 int n, i, ancount, nscount;
286 * Find closest enclosing SOA, even if it's for the root zone.
289 /* First canonicalize dname (exactly one unescaped trailing "."). */
290 if (ns_makecanon(dname, tname, sizeof tname) < 0)
294 resp = malloc(NS_MAXMSG);
298 /* Now grovel the subdomains, hunting for an SOA answer or auth. */
300 /* Leading or inter-label '.' are skipped here. */
301 while (*dname == '.')
304 /* Is there an SOA? */
305 n = do_query(statp, dname, class, ns_t_soa, resp, &msg);
307 DPRINTF(("get_soa: do_query('%s', %s) failed (%d)",
308 dname, p_class(class), n));
312 DPRINTF(("get_soa: CNAME or DNAME found"));
313 sect = ns_s_max, n = 0;
315 rcode = ns_msg_getflag(msg, ns_f_rcode);
316 ancount = ns_msg_count(msg, ns_s_an);
317 nscount = ns_msg_count(msg, ns_s_ns);
318 if (ancount > 0 && rcode == ns_r_noerror)
319 sect = ns_s_an, n = ancount;
320 else if (nscount > 0)
321 sect = ns_s_ns, n = nscount;
323 sect = ns_s_max, n = 0;
325 for (i = 0; i < n; i++) {
330 if (ns_parserr(&msg, sect, i, &rr) < 0) {
331 DPRINTF(("get_soa: ns_parserr(%s, %d) failed",
332 p_section(sect, ns_o_query), i));
335 if (ns_rr_type(rr) == ns_t_cname ||
336 ns_rr_type(rr) == ns_t_dname)
338 if (ns_rr_type(rr) != ns_t_soa ||
339 ns_rr_class(rr) != class)
344 if (ns_samedomain(dname, t) == 0) {
346 ("get_soa: ns_samedomain('%s', '%s') == 0",
354 if (ns_samename(dname, t) == 1 ||
355 ns_samedomain(dname, t) == 0) {
357 ("get_soa: ns_samename() || !ns_samedomain('%s', '%s')",
367 if (strlen(t) + 1 > zsize) {
368 DPRINTF(("get_soa: zname(%lu) too small (%lu)",
369 (unsigned long)zsize,
370 (unsigned long)strlen(t) + 1));
375 rdata = ns_rr_rdata(rr);
376 if (ns_name_uncompress(resp, ns_msg_end(msg), rdata,
378 DPRINTF(("get_soa: ns_name_uncompress failed")
382 if (save_ns(statp, &msg, ns_s_ns,
383 zname, class, opts, nsrrsp) < 0) {
384 DPRINTF(("get_soa: save_ns failed"));
391 /* If we're out of labels, then not even "." has an SOA! */
395 /* Find label-terminating "."; top of loop will skip it. */
396 while (*dname != '.') {
398 if (*++dname == '\0') {
405 DPRINTF(("get_soa: out of labels"));
406 errno = EDESTADDRREQ;
414 get_ns(res_state statp, const char *zname, ns_class class, int opts,
421 resp = malloc(NS_MAXMSG);
425 /* Go and get the NS RRs for this zone. */
426 n = do_query(statp, zname, class, ns_t_ns, resp, &msg);
428 DPRINTF(("get_ns: do_query('%s', %s) failed (%d)",
429 zname, p_class(class), n));
434 /* Remember the NS RRs and associated A RRs that came back. */
435 if (save_ns(statp, &msg, ns_s_an, zname, class, opts, nsrrsp) < 0) {
436 DPRINTF(("get_ns save_ns('%s', %s) failed",
437 zname, p_class(class)));
447 get_glue(res_state statp, ns_class class, int opts, rrset_ns *nsrrsp) {
448 rr_ns *nsrr, *nsrr_n;
451 resp = malloc(NS_MAXMSG);
455 /* Go and get the A RRs for each empty NS RR on our list. */
456 for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = nsrr_n) {
460 nsrr_n = NEXT(nsrr, link);
462 if ((nsrr->flags & RR_NS_HAVE_V4) == 0) {
463 n = do_query(statp, nsrr->name, class, ns_t_a,
467 ("get_glue: do_query('%s', %s') failed",
468 nsrr->name, p_class(class)));
473 "get_glue: do_query('%s', %s') CNAME or DNAME found",
474 nsrr->name, p_class(class)));
476 if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
478 DPRINTF(("get_glue: save_r('%s', %s) failed",
479 nsrr->name, p_class(class)));
484 if ((nsrr->flags & RR_NS_HAVE_V6) == 0) {
485 n = do_query(statp, nsrr->name, class, ns_t_aaaa,
489 ("get_glue: do_query('%s', %s') failed",
490 nsrr->name, p_class(class)));
495 "get_glue: do_query('%s', %s') CNAME or DNAME found",
496 nsrr->name, p_class(class)));
498 if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
500 DPRINTF(("get_glue: save_r('%s', %s) failed",
501 nsrr->name, p_class(class)));
506 /* If it's still empty, it's just chaff. */
507 if (EMPTY(nsrr->addrs)) {
508 DPRINTF(("get_glue: removing empty '%s' NS",
510 free_nsrr(nsrrsp, nsrr);
522 save_ns(res_state statp, ns_msg *msg, ns_sect sect,
523 const char *owner, ns_class class, int opts,
528 for (i = 0; i < ns_msg_count(*msg, sect); i++) {
529 char tname[MAXDNAME];
534 if (ns_parserr(msg, sect, i, &rr) < 0) {
535 DPRINTF(("save_ns: ns_parserr(%s, %d) failed",
536 p_section(sect, ns_o_query), i));
539 if (ns_rr_type(rr) != ns_t_ns ||
540 ns_rr_class(rr) != class ||
541 ns_samename(ns_rr_name(rr), owner) != 1)
543 nsrr = find_ns(nsrrsp, ns_rr_name(rr));
545 nsrr = malloc(sizeof *nsrr);
547 DPRINTF(("save_ns: malloc failed"));
550 rdata = ns_rr_rdata(rr);
551 if (ns_name_uncompress(ns_msg_base(*msg),
552 ns_msg_end(*msg), rdata,
553 tname, sizeof tname) < 0) {
554 DPRINTF(("save_ns: ns_name_uncompress failed")
559 nsrr->name = strdup(tname);
560 if (nsrr->name == NULL) {
561 DPRINTF(("save_ns: strdup failed"));
565 INIT_LINK(nsrr, link);
566 INIT_LIST(nsrr->addrs);
568 APPEND(*nsrrsp, nsrr, link);
570 if (save_a(statp, msg, ns_s_ar,
571 nsrr->name, class, opts, nsrr) < 0) {
572 DPRINTF(("save_ns: save_r('%s', %s) failed",
573 nsrr->name, p_class(class)));
581 save_a(res_state statp, ns_msg *msg, ns_sect sect,
582 const char *owner, ns_class class, int opts,
587 for (i = 0; i < ns_msg_count(*msg, sect); i++) {
591 if (ns_parserr(msg, sect, i, &rr) < 0) {
592 DPRINTF(("save_a: ns_parserr(%s, %d) failed",
593 p_section(sect, ns_o_query), i));
596 if ((ns_rr_type(rr) != ns_t_a &&
597 ns_rr_type(rr) != ns_t_aaaa) ||
598 ns_rr_class(rr) != class ||
599 ns_samename(ns_rr_name(rr), owner) != 1 ||
600 ns_rr_rdlen(rr) != NS_INADDRSZ)
602 if ((opts & RES_IPV6ONLY) != 0 && ns_rr_type(rr) != ns_t_aaaa)
604 if ((opts & RES_IPV4ONLY) != 0 && ns_rr_type(rr) != ns_t_a)
606 arr = malloc(sizeof *arr);
608 DPRINTF(("save_a: malloc failed"));
611 INIT_LINK(arr, link);
612 memset(&arr->addr, 0, sizeof(arr->addr));
613 switch (ns_rr_type(rr)) {
615 arr->addr.sin.sin_family = AF_INET;
617 arr->addr.sin.sin_len = sizeof(arr->addr.sin);
619 memcpy(&arr->addr.sin.sin_addr, ns_rr_rdata(rr),
621 arr->addr.sin.sin_port = htons(NAMESERVER_PORT);
622 nsrr->flags |= RR_NS_HAVE_V4;
625 arr->addr.sin6.sin6_family = AF_INET6;
627 arr->addr.sin6.sin6_len = sizeof(arr->addr.sin6);
629 memcpy(&arr->addr.sin6.sin6_addr, ns_rr_rdata(rr), 16);
630 arr->addr.sin.sin_port = htons(NAMESERVER_PORT);
631 nsrr->flags |= RR_NS_HAVE_V6;
636 APPEND(nsrr->addrs, arr, link);
642 free_nsrrset(rrset_ns *nsrrsp) {
645 while ((nsrr = HEAD(*nsrrsp)) != NULL)
646 free_nsrr(nsrrsp, nsrr);
650 free_nsrr(rrset_ns *nsrrsp, rr_ns *nsrr) {
654 while ((arr = HEAD(nsrr->addrs)) != NULL) {
655 UNLINK(nsrr->addrs, arr, link);
658 DE_CONST(nsrr->name, tmp);
660 UNLINK(*nsrrsp, nsrr, link);
665 find_ns(rrset_ns *nsrrsp, const char *dname) {
668 for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = NEXT(nsrr, link))
669 if (ns_samename(nsrr->name, dname) == 1)
675 do_query(res_state statp, const char *dname, ns_class class, ns_type qtype,
676 u_char *resp, ns_msg *msg)
678 u_char req[NS_PACKETSZ];
681 n = res_nmkquery(statp, ns_o_query, dname, class, qtype,
682 NULL, 0, NULL, req, NS_PACKETSZ);
684 DPRINTF(("do_query: res_nmkquery failed"));
687 n = res_nsend(statp, req, n, resp, NS_MAXMSG);
689 DPRINTF(("do_query: res_nsend failed"));
693 DPRINTF(("do_query: res_nsend returned 0"));
697 if (ns_initparse(resp, n, msg) < 0) {
698 DPRINTF(("do_query: ns_initparse failed"));
702 for (i = 0; i < ns_msg_count(*msg, ns_s_an); i++) {
705 if (ns_parserr(msg, ns_s_an, i, &rr) < 0) {
706 DPRINTF(("do_query: ns_parserr failed"));
709 n += (ns_rr_class(rr) == class &&
710 (ns_rr_type(rr) == ns_t_cname ||
711 ns_rr_type(rr) == ns_t_dname));
717 res_dprintf(const char *fmt, ...) {
721 fputs(";; res_findzonecut: ", stderr);
722 vfprintf(stderr, fmt, ap);