1 #if !defined(lint) && !defined(SABER)
2 static const char rcsid[] = "$Id: res_findzonecut.c,v 1.2.2.5 2004/03/16 12:35:34 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.
24 #include "port_before.h"
26 #include <sys/param.h>
27 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <arpa/nameser.h>
44 #include "port_after.h"
48 /* Data structures. */
51 LINK(struct rr_a) link;
52 union res_sockaddr_union addr;
54 typedef LIST(rr_a) rrset_a;
56 typedef struct rr_ns {
57 LINK(struct rr_ns) link;
62 typedef LIST(rr_ns) rrset_ns;
64 #define RR_NS_HAVE_V4 0x01
65 #define RR_NS_HAVE_V6 0x02
69 static int satisfy(res_state, const char *, rrset_ns *,
70 union res_sockaddr_union *, int);
71 static int add_addrs(res_state, rr_ns *,
72 union res_sockaddr_union *, int);
73 static int get_soa(res_state, const char *, ns_class, int,
74 char *, size_t, char *, size_t,
76 static int get_ns(res_state, const char *, ns_class, int, rrset_ns *);
77 static int get_glue(res_state, ns_class, int, rrset_ns *);
78 static int save_ns(res_state, ns_msg *, ns_sect,
79 const char *, ns_class, int, rrset_ns *);
80 static int save_a(res_state, ns_msg *, ns_sect,
81 const char *, ns_class, int, rr_ns *);
82 static void free_nsrrset(rrset_ns *);
83 static void free_nsrr(rrset_ns *, rr_ns *);
84 static rr_ns * find_ns(rrset_ns *, const char *);
85 static int do_query(res_state, const char *, ns_class, ns_type,
87 static void res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
91 #define DPRINTF(x) do {\
92 int save_errno = errno; \
93 if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
101 * res_findzonecut(res, dname, class, zname, zsize, addrs, naddrs)
102 * find enclosing zone for a <dname,class>, and some server addresses
104 * res - resolver context to work within (is modified)
105 * dname - domain name whose enclosing zone is desired
106 * class - class of dname (and its enclosing zone)
107 * zname - found zone name
108 * zsize - allocated size of zname
109 * addrs - found server addresses
110 * naddrs - max number of addrs
112 * < 0 - an error occurred (check errno)
113 * = 0 - zname is now valid, but addrs[] wasn't changed
114 * > 0 - zname is now valid, and return value is number of addrs[] found
116 * this function calls res_nsend() which means it depends on correctly
117 * functioning recursive nameservers (usually defined in /etc/resolv.conf
118 * or its local equivilent).
120 * we start by asking for an SOA<dname,class>. if we get one as an
121 * answer, that just means <dname,class> is a zone top, which is fine.
122 * more than likely we'll be told to go pound sand, in the form of a
125 * note that we are not prepared to deal with referrals since that would
126 * only come from authority servers and our correctly functioning local
127 * recursive server would have followed the referral and got us something
130 * if the authority section contains an SOA, this SOA should also be the
131 * closest enclosing zone, since any intermediary zone cuts would've been
132 * returned as referrals and dealt with by our correctly functioning local
133 * recursive name server. but an SOA in the authority section should NOT
134 * match our dname (since that would have been returned in the answer
135 * section). an authority section SOA has to be "above" our dname.
137 * however, since authority section SOA's were once optional, it's
138 * possible that we'll have to go hunting for the enclosing SOA by
139 * ripping labels off the front of our dname -- this is known as "doing
142 * ultimately we want some server addresses, which are ideally the ones
143 * pertaining to the SOA.MNAME, but only if there is a matching NS RR.
144 * so the second phase (after we find an SOA) is to go looking for the
145 * NS RRset for that SOA's zone.
147 * no answer section processed by this code is allowed to contain CNAME
148 * or DNAME RR's. for the SOA query this means we strip a label and
149 * keep going. for the NS and A queries this means we just give up.
153 res_findzonecut(res_state statp, const char *dname, ns_class class, int opts,
154 char *zname, size_t zsize, struct in_addr *addrs, int naddrs)
157 union res_sockaddr_union *u;
160 opts |= RES_IPV4ONLY;
161 opts &= ~RES_IPV6ONLY;
163 u = calloc(naddrs, sizeof(*u));
167 result = res_findzonecut2(statp, dname, class, opts, zname, zsize,
170 for (i = 0; i < result; i++) {
171 addrs[i] = u[i].sin.sin_addr;
178 res_findzonecut2(res_state statp, const char *dname, ns_class class, int opts,
179 char *zname, size_t zsize, union res_sockaddr_union *addrs,
182 char mname[NS_MAXDNAME];
187 DPRINTF(("START dname='%s' class=%s, zsize=%ld, naddrs=%d",
188 dname, p_class(class), (long)zsize, naddrs));
189 save_pfcode = statp->pfcode;
190 statp->pfcode |= RES_PRF_HEAD2 | RES_PRF_HEAD1 | RES_PRF_HEADX |
191 RES_PRF_QUES | RES_PRF_ANS |
192 RES_PRF_AUTH | RES_PRF_ADD;
195 DPRINTF(("get the soa, and see if it has enough glue"));
196 if ((n = get_soa(statp, dname, class, opts, zname, zsize,
197 mname, sizeof mname, &nsrrs)) < 0 ||
198 ((opts & RES_EXHAUSTIVE) == 0 &&
199 (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
202 DPRINTF(("get the ns rrset and see if it has enough glue"));
203 if ((n = get_ns(statp, zname, class, opts, &nsrrs)) < 0 ||
204 ((opts & RES_EXHAUSTIVE) == 0 &&
205 (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
208 DPRINTF(("get the missing glue and see if it's finally enough"));
209 if ((n = get_glue(statp, class, opts, &nsrrs)) >= 0)
210 n = satisfy(statp, mname, &nsrrs, addrs, naddrs);
213 DPRINTF(("FINISH n=%d (%s)", n, (n < 0) ? strerror(errno) : "OK"));
214 free_nsrrset(&nsrrs);
215 statp->pfcode = save_pfcode;
222 satisfy(res_state statp, const char *mname, rrset_ns *nsrrsp,
223 union res_sockaddr_union *addrs, int naddrs)
229 nsrr = find_ns(nsrrsp, mname);
231 x = add_addrs(statp, nsrr, addrs, naddrs);
236 for (nsrr = HEAD(*nsrrsp);
237 nsrr != NULL && naddrs > 0;
238 nsrr = NEXT(nsrr, link))
239 if (ns_samename(nsrr->name, mname) != 1) {
240 x = add_addrs(statp, nsrr, addrs, naddrs);
245 DPRINTF(("satisfy(%s): %d", mname, n));
250 add_addrs(res_state statp, rr_ns *nsrr,
251 union res_sockaddr_union *addrs, int naddrs)
256 for (arr = HEAD(nsrr->addrs); arr != NULL; arr = NEXT(arr, link)) {
259 *addrs++ = arr->addr;
263 DPRINTF(("add_addrs: %d", n));
268 get_soa(res_state statp, const char *dname, ns_class class, int opts,
269 char *zname, size_t zsize, char *mname, size_t msize,
272 char tname[NS_MAXDNAME];
274 int n, i, ancount, nscount;
280 * Find closest enclosing SOA, even if it's for the root zone.
283 /* First canonicalize dname (exactly one unescaped trailing "."). */
284 if (ns_makecanon(dname, tname, sizeof tname) < 0)
288 resp = malloc(NS_MAXMSG);
292 /* Now grovel the subdomains, hunting for an SOA answer or auth. */
294 /* Leading or inter-label '.' are skipped here. */
295 while (*dname == '.')
298 /* Is there an SOA? */
299 n = do_query(statp, dname, class, ns_t_soa, resp, &msg);
301 DPRINTF(("get_soa: do_query('%s', %s) failed (%d)",
302 dname, p_class(class), n));
306 DPRINTF(("get_soa: CNAME or DNAME found"));
307 sect = ns_s_max, n = 0;
309 rcode = ns_msg_getflag(msg, ns_f_rcode);
310 ancount = ns_msg_count(msg, ns_s_an);
311 nscount = ns_msg_count(msg, ns_s_ns);
312 if (ancount > 0 && rcode == ns_r_noerror)
313 sect = ns_s_an, n = ancount;
314 else if (nscount > 0)
315 sect = ns_s_ns, n = nscount;
317 sect = ns_s_max, n = 0;
319 for (i = 0; i < n; i++) {
325 if (ns_parserr(&msg, sect, i, &rr) < 0) {
326 DPRINTF(("get_soa: ns_parserr(%s, %d) failed",
327 p_section(sect, ns_o_query), i));
330 if (ns_rr_type(rr) == ns_t_cname ||
331 ns_rr_type(rr) == ns_t_dname)
333 if (ns_rr_type(rr) != ns_t_soa ||
334 ns_rr_class(rr) != class)
339 if (ns_samedomain(dname, t) == 0) {
341 ("get_soa: ns_samedomain('%s', '%s') == 0",
349 if (ns_samename(dname, t) == 1 ||
350 ns_samedomain(dname, t) == 0) {
352 ("get_soa: ns_samename() || !ns_samedomain('%s', '%s')",
362 if (strlen(t) + 1 > zsize) {
363 DPRINTF(("get_soa: zname(%d) too small (%d)",
364 zsize, strlen(t) + 1));
369 rdata = ns_rr_rdata(rr);
370 rdlen = ns_rr_rdlen(rr);
371 if (ns_name_uncompress(resp, ns_msg_end(msg), rdata,
373 DPRINTF(("get_soa: ns_name_uncompress failed")
377 if (save_ns(statp, &msg, ns_s_ns,
378 zname, class, opts, nsrrsp) < 0) {
379 DPRINTF(("get_soa: save_ns failed"));
386 /* If we're out of labels, then not even "." has an SOA! */
390 /* Find label-terminating "."; top of loop will skip it. */
391 while (*dname != '.') {
393 if (*++dname == '\0') {
400 DPRINTF(("get_soa: out of labels"));
401 errno = EDESTADDRREQ;
409 get_ns(res_state statp, const char *zname, ns_class class, int opts,
416 resp = malloc(NS_MAXMSG);
420 /* Go and get the NS RRs for this zone. */
421 n = do_query(statp, zname, class, ns_t_ns, resp, &msg);
423 DPRINTF(("get_ns: do_query('%s', %s) failed (%d)",
424 zname, p_class(class), n));
429 /* Remember the NS RRs and associated A RRs that came back. */
430 if (save_ns(statp, &msg, ns_s_an, zname, class, opts, nsrrsp) < 0) {
431 DPRINTF(("get_ns save_ns('%s', %s) failed",
432 zname, p_class(class)));
442 get_glue(res_state statp, ns_class class, int opts, rrset_ns *nsrrsp) {
443 rr_ns *nsrr, *nsrr_n;
446 resp = malloc(NS_MAXMSG);
450 /* Go and get the A RRs for each empty NS RR on our list. */
451 for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = nsrr_n) {
455 nsrr_n = NEXT(nsrr, link);
457 if ((nsrr->flags & RR_NS_HAVE_V4) == 0) {
458 n = do_query(statp, nsrr->name, class, ns_t_a,
462 ("get_glue: do_query('%s', %s') failed",
463 nsrr->name, p_class(class)));
468 "get_glue: do_query('%s', %s') CNAME or DNAME found",
469 nsrr->name, p_class(class)));
471 if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
473 DPRINTF(("get_glue: save_r('%s', %s) failed",
474 nsrr->name, p_class(class)));
479 if ((nsrr->flags & RR_NS_HAVE_V6) == 0) {
480 n = do_query(statp, nsrr->name, class, ns_t_aaaa,
484 ("get_glue: do_query('%s', %s') failed",
485 nsrr->name, p_class(class)));
490 "get_glue: do_query('%s', %s') CNAME or DNAME found",
491 nsrr->name, p_class(class)));
493 if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
495 DPRINTF(("get_glue: save_r('%s', %s) failed",
496 nsrr->name, p_class(class)));
501 /* If it's still empty, it's just chaff. */
502 if (EMPTY(nsrr->addrs)) {
503 DPRINTF(("get_glue: removing empty '%s' NS",
505 free_nsrr(nsrrsp, nsrr);
517 save_ns(res_state statp, ns_msg *msg, ns_sect sect,
518 const char *owner, ns_class class, int opts,
523 for (i = 0; i < ns_msg_count(*msg, sect); i++) {
524 char tname[MAXDNAME];
530 if (ns_parserr(msg, sect, i, &rr) < 0) {
531 DPRINTF(("save_ns: ns_parserr(%s, %d) failed",
532 p_section(sect, ns_o_query), i));
535 if (ns_rr_type(rr) != ns_t_ns ||
536 ns_rr_class(rr) != class ||
537 ns_samename(ns_rr_name(rr), owner) != 1)
539 nsrr = find_ns(nsrrsp, ns_rr_name(rr));
541 nsrr = malloc(sizeof *nsrr);
543 DPRINTF(("save_ns: malloc failed"));
546 rdata = ns_rr_rdata(rr);
547 rdlen = ns_rr_rdlen(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")
556 nsrr->name = strdup(tname);
557 if (nsrr->name == NULL) {
558 DPRINTF(("save_ns: strdup failed"));
562 INIT_LINK(nsrr, link);
563 INIT_LIST(nsrr->addrs);
565 APPEND(*nsrrsp, nsrr, link);
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)));
578 save_a(res_state statp, ns_msg *msg, ns_sect sect,
579 const char *owner, ns_class class, int opts,
584 for (i = 0; i < ns_msg_count(*msg, sect); i++) {
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));
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)
599 if ((opts & RES_IPV6ONLY) != 0 && ns_rr_type(rr) != ns_t_aaaa)
601 if ((opts & RES_IPV4ONLY) != 0 && ns_rr_type(rr) != ns_t_a)
603 arr = malloc(sizeof *arr);
605 DPRINTF(("save_a: malloc failed"));
608 INIT_LINK(arr, link);
609 memset(&arr->addr, 0, sizeof(arr->addr));
610 switch (ns_rr_type(rr)) {
612 arr->addr.sin.sin_family = AF_INET;
614 arr->addr.sin.sin_len = sizeof(arr->addr.sin);
616 memcpy(&arr->addr.sin.sin_addr, ns_rr_rdata(rr),
618 arr->addr.sin.sin_port = htons(NAMESERVER_PORT);
619 nsrr->flags |= RR_NS_HAVE_V4;
622 arr->addr.sin6.sin6_family = AF_INET6;
624 arr->addr.sin6.sin6_len = sizeof(arr->addr.sin6);
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;
633 APPEND(nsrr->addrs, arr, link);
639 free_nsrrset(rrset_ns *nsrrsp) {
642 while ((nsrr = HEAD(*nsrrsp)) != NULL)
643 free_nsrr(nsrrsp, nsrr);
647 free_nsrr(rrset_ns *nsrrsp, rr_ns *nsrr) {
651 while ((arr = HEAD(nsrr->addrs)) != NULL) {
652 UNLINK(nsrr->addrs, arr, link);
655 DE_CONST(nsrr->name, tmp);
657 UNLINK(*nsrrsp, nsrr, link);
662 find_ns(rrset_ns *nsrrsp, const char *dname) {
665 for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = NEXT(nsrr, link))
666 if (ns_samename(nsrr->name, dname) == 1)
672 do_query(res_state statp, const char *dname, ns_class class, ns_type qtype,
673 u_char *resp, ns_msg *msg)
675 u_char req[NS_PACKETSZ];
678 n = res_nmkquery(statp, ns_o_query, dname, class, qtype,
679 NULL, 0, NULL, req, NS_PACKETSZ);
681 DPRINTF(("do_query: res_nmkquery failed"));
684 n = res_nsend(statp, req, n, resp, NS_MAXMSG);
686 DPRINTF(("do_query: res_nsend failed"));
690 DPRINTF(("do_query: res_nsend returned 0"));
694 if (ns_initparse(resp, n, msg) < 0) {
695 DPRINTF(("do_query: ns_initparse failed"));
699 for (i = 0; i < ns_msg_count(*msg, ns_s_an); i++) {
702 if (ns_parserr(msg, ns_s_an, i, &rr) < 0) {
703 DPRINTF(("do_query: ns_parserr failed"));
706 n += (ns_rr_class(rr) == class &&
707 (ns_rr_type(rr) == ns_t_cname ||
708 ns_rr_type(rr) == ns_t_dname));
714 res_dprintf(const char *fmt, ...) {
718 fputs(";; res_findzonecut: ", stderr);
719 vfprintf(stderr, fmt, ap);