2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1999 by Internet Software Consortium.
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.
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.
20 #include "port_before.h"
22 #include <sys/param.h>
23 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <arpa/nameser.h>
40 #include "port_after.h"
44 /* Data structures. */
47 LINK(struct rr_a
) link
;
48 union res_sockaddr_union addr
;
50 typedef LIST(rr_a
) rrset_a
;
52 typedef struct rr_ns
{
53 LINK(struct rr_ns
) link
;
58 typedef LIST(rr_ns
) rrset_ns
;
60 #define RR_NS_HAVE_V4 0x01
61 #define RR_NS_HAVE_V6 0x02
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,
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
,
83 static void res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
87 #define DPRINTF(x) do {\
88 int save_errno = errno; \
89 if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
96 * find enclosing zone for a <dname,class>, and some server addresses
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
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
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).
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
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
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.
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
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.
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.
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
)
154 union res_sockaddr_union
*u
;
157 opts
|= RES_IPV4ONLY
;
158 opts
&= ~RES_IPV6ONLY
;
160 u
= calloc(naddrs
, sizeof(*u
));
164 result
= res_findzonecut2(statp
, dname
, class, opts
, zname
, zsize
,
167 for (i
= 0; i
< result
; i
++) {
168 addrs
[i
] = u
[i
].sin
.sin_addr
;
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
,
179 char mname
[NS_MAXDNAME
];
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
;
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))
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))
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
);
210 DPRINTF(("FINISH n=%d (%s)", n
, (n
< 0) ? strerror(errno
) : "OK"));
211 free_nsrrset(&nsrrs
);
212 statp
->pfcode
= save_pfcode
;
219 satisfy(res_state statp
, const char *mname
, rrset_ns
*nsrrsp
,
220 union res_sockaddr_union
*addrs
, int naddrs
)
226 nsrr
= find_ns(nsrrsp
, mname
);
228 x
= add_addrs(statp
, nsrr
, addrs
, naddrs
);
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
);
242 DPRINTF(("satisfy(%s): %d", mname
, n
));
247 add_addrs(res_state statp
, rr_ns
*nsrr
,
248 union res_sockaddr_union
*addrs
, int naddrs
)
253 for (arr
= HEAD(nsrr
->addrs
); arr
!= NULL
; arr
= NEXT(arr
, link
)) {
256 *addrs
++ = arr
->addr
;
260 DPRINTF(("add_addrs: %d", n
));
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
,
269 char tname
[NS_MAXDNAME
];
271 int n
, i
, ancount
, nscount
;
277 * Find closest enclosing SOA, even if it's for the root zone.
280 /* First canonicalize dname (exactly one unescaped trailing "."). */
281 if (ns_makecanon(dname
, tname
, sizeof tname
) < 0)
285 resp
= malloc(NS_MAXMSG
);
289 /* Now grovel the subdomains, hunting for an SOA answer or auth. */
291 /* Leading or inter-label '.' are skipped here. */
292 while (*dname
== '.')
295 /* Is there an SOA? */
296 n
= do_query(statp
, dname
, class, ns_t_soa
, resp
, &msg
);
298 DPRINTF(("get_soa: do_query('%s', %s) failed (%d)",
299 dname
, p_class(class), n
));
303 DPRINTF(("get_soa: CNAME or DNAME found"));
304 sect
= ns_s_max
, n
= 0;
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
;
314 sect
= ns_s_max
, n
= 0;
316 for (i
= 0; i
< n
; i
++) {
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
));
326 if (ns_rr_type(rr
) == ns_t_cname
||
327 ns_rr_type(rr
) == ns_t_dname
)
329 if (ns_rr_type(rr
) != ns_t_soa
||
330 ns_rr_class(rr
) != class)
335 if (ns_samedomain(dname
, t
) == 0) {
337 ("get_soa: ns_samedomain('%s', '%s') == 0",
345 if (ns_samename(dname
, t
) == 1 ||
346 ns_samedomain(dname
, t
) == 0) {
348 ("get_soa: ns_samename() || !ns_samedomain('%s', '%s')",
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));
366 rdata
= ns_rr_rdata(rr
);
367 if (ns_name_uncompress(resp
, ns_msg_end(msg
), rdata
,
369 DPRINTF(("get_soa: ns_name_uncompress failed")
373 if (save_ns(statp
, &msg
, ns_s_ns
,
374 zname
, class, opts
, nsrrsp
) < 0) {
375 DPRINTF(("get_soa: save_ns failed"));
382 /* If we're out of labels, then not even "." has an SOA! */
386 /* Find label-terminating "."; top of loop will skip it. */
387 while (*dname
!= '.') {
389 if (*++dname
== '\0') {
396 DPRINTF(("get_soa: out of labels"));
397 errno
= EDESTADDRREQ
;
404 get_ns(res_state statp
, const char *zname
, ns_class
class, int opts
,
411 resp
= malloc(NS_MAXMSG
);
415 /* Go and get the NS RRs for this zone. */
416 n
= do_query(statp
, zname
, class, ns_t_ns
, resp
, &msg
);
418 DPRINTF(("get_ns: do_query('%s', %s) failed (%d)",
419 zname
, p_class(class), n
));
424 /* Remember the NS RRs and associated A RRs that came back. */
425 if (save_ns(statp
, &msg
, ns_s_an
, zname
, class, opts
, nsrrsp
) < 0) {
426 DPRINTF(("get_ns save_ns('%s', %s) failed",
427 zname
, p_class(class)));
437 get_glue(res_state statp
, ns_class
class, int opts
, rrset_ns
*nsrrsp
) {
438 rr_ns
*nsrr
, *nsrr_n
;
441 resp
= malloc(NS_MAXMSG
);
445 /* Go and get the A RRs for each empty NS RR on our list. */
446 for (nsrr
= HEAD(*nsrrsp
); nsrr
!= NULL
; nsrr
= nsrr_n
) {
450 nsrr_n
= NEXT(nsrr
, link
);
452 if ((nsrr
->flags
& RR_NS_HAVE_V4
) == 0) {
453 n
= do_query(statp
, nsrr
->name
, class, ns_t_a
,
457 ("get_glue: do_query('%s', %s') failed",
458 nsrr
->name
, p_class(class)));
463 "get_glue: do_query('%s', %s') CNAME or DNAME found",
464 nsrr
->name
, p_class(class)));
466 if (save_a(statp
, &msg
, ns_s_an
, nsrr
->name
, class,
468 DPRINTF(("get_glue: save_r('%s', %s) failed",
469 nsrr
->name
, p_class(class)));
474 if ((nsrr
->flags
& RR_NS_HAVE_V6
) == 0) {
475 n
= do_query(statp
, nsrr
->name
, class, ns_t_aaaa
,
479 ("get_glue: do_query('%s', %s') failed",
480 nsrr
->name
, p_class(class)));
485 "get_glue: do_query('%s', %s') CNAME or DNAME found",
486 nsrr
->name
, p_class(class)));
488 if (save_a(statp
, &msg
, ns_s_an
, nsrr
->name
, class,
490 DPRINTF(("get_glue: save_r('%s', %s) failed",
491 nsrr
->name
, p_class(class)));
496 /* If it's still empty, it's just chaff. */
497 if (EMPTY(nsrr
->addrs
)) {
498 DPRINTF(("get_glue: removing empty '%s' NS",
500 free_nsrr(nsrrsp
, nsrr
);
512 save_ns(res_state statp
, ns_msg
*msg
, ns_sect sect
,
513 const char *owner
, ns_class
class, int opts
,
518 for (i
= 0; i
< ns_msg_count(*msg
, sect
); i
++) {
519 char tname
[MAXDNAME
];
524 if (ns_parserr(msg
, sect
, i
, &rr
) < 0) {
525 DPRINTF(("save_ns: ns_parserr(%s, %d) failed",
526 p_section(sect
, ns_o_query
), i
));
529 if (ns_rr_type(rr
) != ns_t_ns
||
530 ns_rr_class(rr
) != class ||
531 ns_samename(ns_rr_name(rr
), owner
) != 1)
533 nsrr
= find_ns(nsrrsp
, ns_rr_name(rr
));
535 nsrr
= malloc(sizeof *nsrr
);
537 DPRINTF(("save_ns: malloc failed"));
540 rdata
= ns_rr_rdata(rr
);
541 if (ns_name_uncompress(ns_msg_base(*msg
),
542 ns_msg_end(*msg
), rdata
,
543 tname
, sizeof tname
) < 0) {
544 DPRINTF(("save_ns: ns_name_uncompress failed")
549 nsrr
->name
= strdup(tname
);
550 if (nsrr
->name
== NULL
) {
551 DPRINTF(("save_ns: strdup failed"));
555 INIT_LINK(nsrr
, link
);
556 INIT_LIST(nsrr
->addrs
);
558 APPEND(*nsrrsp
, nsrr
, link
);
560 if (save_a(statp
, msg
, ns_s_ar
,
561 nsrr
->name
, class, opts
, nsrr
) < 0) {
562 DPRINTF(("save_ns: save_r('%s', %s) failed",
563 nsrr
->name
, p_class(class)));
571 save_a(res_state statp
, ns_msg
*msg
, ns_sect sect
,
572 const char *owner
, ns_class
class, int opts
,
577 for (i
= 0; i
< ns_msg_count(*msg
, sect
); i
++) {
581 if (ns_parserr(msg
, sect
, i
, &rr
) < 0) {
582 DPRINTF(("save_a: ns_parserr(%s, %d) failed",
583 p_section(sect
, ns_o_query
), i
));
586 if ((ns_rr_type(rr
) != ns_t_a
&&
587 ns_rr_type(rr
) != ns_t_aaaa
) ||
588 ns_rr_class(rr
) != class ||
589 ns_samename(ns_rr_name(rr
), owner
) != 1 ||
590 ns_rr_rdlen(rr
) != NS_INADDRSZ
)
592 if ((opts
& RES_IPV6ONLY
) != 0 && ns_rr_type(rr
) != ns_t_aaaa
)
594 if ((opts
& RES_IPV4ONLY
) != 0 && ns_rr_type(rr
) != ns_t_a
)
596 arr
= malloc(sizeof *arr
);
598 DPRINTF(("save_a: malloc failed"));
601 INIT_LINK(arr
, link
);
602 memset(&arr
->addr
, 0, sizeof(arr
->addr
));
603 switch (ns_rr_type(rr
)) {
605 arr
->addr
.sin
.sin_family
= AF_INET
;
607 arr
->addr
.sin
.sin_len
= sizeof(arr
->addr
.sin
);
609 memcpy(&arr
->addr
.sin
.sin_addr
, ns_rr_rdata(rr
),
611 arr
->addr
.sin
.sin_port
= htons(NAMESERVER_PORT
);
612 nsrr
->flags
|= RR_NS_HAVE_V4
;
615 arr
->addr
.sin6
.sin6_family
= AF_INET6
;
617 arr
->addr
.sin6
.sin6_len
= sizeof(arr
->addr
.sin6
);
619 memcpy(&arr
->addr
.sin6
.sin6_addr
, ns_rr_rdata(rr
), 16);
620 arr
->addr
.sin
.sin_port
= htons(NAMESERVER_PORT
);
621 nsrr
->flags
|= RR_NS_HAVE_V6
;
626 APPEND(nsrr
->addrs
, arr
, link
);
632 free_nsrrset(rrset_ns
*nsrrsp
) {
635 while ((nsrr
= HEAD(*nsrrsp
)) != NULL
)
636 free_nsrr(nsrrsp
, nsrr
);
640 free_nsrr(rrset_ns
*nsrrsp
, rr_ns
*nsrr
) {
644 while ((arr
= HEAD(nsrr
->addrs
)) != NULL
) {
645 UNLINK(nsrr
->addrs
, arr
, link
);
648 DE_CONST(nsrr
->name
, tmp
);
650 UNLINK(*nsrrsp
, nsrr
, link
);
655 find_ns(rrset_ns
*nsrrsp
, const char *dname
) {
658 for (nsrr
= HEAD(*nsrrsp
); nsrr
!= NULL
; nsrr
= NEXT(nsrr
, link
))
659 if (ns_samename(nsrr
->name
, dname
) == 1)
665 do_query(res_state statp
, const char *dname
, ns_class
class, ns_type qtype
,
666 u_char
*resp
, ns_msg
*msg
)
668 u_char req
[NS_PACKETSZ
];
671 n
= res_nmkquery(statp
, ns_o_query
, dname
, class, qtype
,
672 NULL
, 0, NULL
, req
, NS_PACKETSZ
);
674 DPRINTF(("do_query: res_nmkquery failed"));
677 n
= res_nsend(statp
, req
, n
, resp
, NS_MAXMSG
);
679 DPRINTF(("do_query: res_nsend failed"));
683 DPRINTF(("do_query: res_nsend returned 0"));
687 if (ns_initparse(resp
, n
, msg
) < 0) {
688 DPRINTF(("do_query: ns_initparse failed"));
692 for (i
= 0; i
< ns_msg_count(*msg
, ns_s_an
); i
++) {
695 if (ns_parserr(msg
, ns_s_an
, i
, &rr
) < 0) {
696 DPRINTF(("do_query: ns_parserr failed"));
699 n
+= (ns_rr_class(rr
) == class &&
700 (ns_rr_type(rr
) == ns_t_cname
||
701 ns_rr_type(rr
) == ns_t_dname
));
707 res_dprintf(const char *fmt
, ...) {
711 fputs(";; res_findzonecut: ", stderr
);
712 vfprintf(stderr
, fmt
, ap
);