1 #if !defined(lint) && !defined(SABER)
2 static const char rcsid[] = "$Id: res_findzonecut.c,v 1.14.2.1 2001/05/17 20:47:35 mellon Exp $";
6 * Copyright (c) 1999 by Internet Software Consortium.
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
13 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24 #include <sys/param.h>
25 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
39 #include <isc-dhcp/list.h>
41 #include "minires/minires.h"
42 #include "arpa/nameser.h"
44 /* Data structures. */
47 ISC_LINK(struct rr_a) link;
50 typedef ISC_LIST(rr_a) rrset_a;
52 typedef struct rr_ns {
53 ISC_LINK(struct rr_ns) link;
57 typedef ISC_LIST(rr_ns) rrset_ns;
61 static int satisfy(res_state,
62 const char *, rrset_ns *, struct in_addr *, int);
63 static int add_addrs(res_state, rr_ns *, struct in_addr *, int);
64 static ns_rcode get_soa(res_state, const char *, ns_class,
65 char *, size_t, char *, size_t,
67 static isc_result_t get_ns(res_state, const char *, ns_class, rrset_ns *);
68 static isc_result_t get_glue(res_state, ns_class, rrset_ns *);
69 static isc_result_t save_ns(res_state, ns_msg *, ns_sect,
70 const char *, ns_class, rrset_ns *);
71 static isc_result_t save_a(res_state, ns_msg *, ns_sect,
72 const char *, ns_class, rrset_a *);
73 static void free_nsrrset(rrset_ns *);
74 static void free_nsrr(rrset_ns *, rr_ns *);
75 static rr_ns * find_ns(rrset_ns *, const char *);
76 static isc_result_t do_query(res_state, const char *, ns_class, ns_type,
77 double *, ns_msg *, int *);
83 * res_findzonecut(res, dname, class, zname, zsize, addrs, naddrs)
84 * find enclosing zone for a <dname,class>, and some server addresses
86 * res - resolver context to work within (is modified)
87 * dname - domain name whose enclosing zone is desired
88 * class - class of dname (and its enclosing zone)
89 * zname - found zone name
90 * zsize - allocated size of zname
91 * addrs - found server addresses
92 * naddrs - max number of addrs
94 * < 0 - an error occurred (check errno)
95 * = 0 - zname is now valid, but addrs[] wasn't changed
96 * > 0 - zname is now valid, and return value is number of addrs[] found
98 * this function calls res_nsend() which means it depends on correctly
99 * functioning recursive nameservers (usually defined in /etc/resolv.conf
100 * or its local equivilent).
102 * we start by asking for an SOA<dname,class>. if we get one as an
103 * answer, that just means <dname,class> is a zone top, which is fine.
104 * more than likely we'll be told to go pound sand, in the form of a
107 * note that we are not prepared to deal with referrals since that would
108 * only come from authority servers and our correctly functioning local
109 * recursive server would have followed the referral and got us something
112 * if the authority section contains an SOA, this SOA should also be the
113 * closest enclosing zone, since any intermediary zone cuts would've been
114 * returned as referrals and dealt with by our correctly functioning local
115 * recursive name server. but an SOA in the authority section should NOT
116 * match our dname (since that would have been returned in the answer
117 * section). an authority section SOA has to be "above" our dname.
119 * we cannot fail to find an SOA in this way. ultimately we'll return
120 * a zname indicating the root zone if that's the closest enclosing zone.
121 * however, since authority section SOA's were once optional, it's
122 * possible that we'll have to go hunting for the enclosing SOA by
123 * ripping labels off the front of our dname -- this is known as "doing
126 * ultimately we want some server addresses, which are ideally the ones
127 * pertaining to the SOA.MNAME, but only if there is a matching NS RR.
128 * so the second phase (after we find an SOA) is to go looking for the
129 * NS RRset for that SOA's zone.
131 * no answer section processed by this code is allowed to contain CNAME
132 * or DNAME RR's. for the SOA query this means we strip a label and
133 * keep going. for the NS and A queries this means we just give up.
137 res_findzonecut(res_state statp, const char *dname, ns_class class, int opts,
138 char *zname, size_t zsize, struct in_addr *addrs, int naddrs,
139 int *count, void *zcookie)
141 char mname[NS_MAXDNAME];
147 DPRINTF(("START dname='%s' class=%s, zsize=%ld, naddrs=%d",
148 dname, p_class(class), (long)zsize, naddrs));
149 save_pfcode = statp->pfcode;
150 statp->pfcode |= RES_PRF_HEAD2 | RES_PRF_HEAD1 | RES_PRF_HEADX |
151 RES_PRF_QUES | RES_PRF_ANS |
152 RES_PRF_AUTH | RES_PRF_ADD;
153 ISC_LIST_INIT(nsrrs);
155 DPRINTF (("look for a predefined zone statement"));
156 rcode = find_cached_zone (dname, class, zname, zsize,
157 addrs, naddrs, &n, zcookie);
158 if (rcode == ISC_R_SUCCESS)
161 DPRINTF(("get the soa, and see if it has enough glue"));
162 if ((rcode = get_soa(statp, dname, class, zname, zsize,
163 mname, sizeof mname, &nsrrs)) != ISC_R_SUCCESS ||
164 ((opts & RES_EXHAUSTIVE) == 0 &&
165 (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
168 DPRINTF(("get the ns rrset and see if it has enough glue"));
169 if ((rcode = get_ns(statp, zname, class, &nsrrs)) != ISC_R_SUCCESS ||
170 ((opts & RES_EXHAUSTIVE) == 0 &&
171 (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
174 DPRINTF(("get the missing glue and see if it's finally enough"));
175 if ((rcode = get_glue(statp, class, &nsrrs)) == ISC_R_SUCCESS)
176 n = satisfy(statp, mname, &nsrrs, addrs, naddrs);
178 /* If we found the zone, cache it. */
180 cache_found_zone (class, zname, addrs, n);
182 DPRINTF(("FINISH n=%d (%s)", n, (n < 0) ? strerror(errno) : "OK"));
183 free_nsrrset(&nsrrs);
184 statp->pfcode = save_pfcode;
193 satisfy(res_state statp,
194 const char *mname, rrset_ns *nsrrsp, struct in_addr *addrs, int naddrs)
200 nsrr = find_ns(nsrrsp, mname);
202 x = add_addrs(statp, nsrr, addrs, naddrs);
207 for (nsrr = ISC_LIST_HEAD(*nsrrsp);
208 nsrr != NULL && naddrs > 0;
209 nsrr = ISC_LIST_NEXT(nsrr, link))
210 if (ns_samename(nsrr->name, mname) != 1) {
211 x = add_addrs(statp, nsrr, addrs, naddrs);
216 DPRINTF(("satisfy(%s): %d", mname, n));
221 add_addrs(res_state statp, rr_ns *nsrr, struct in_addr *addrs, int naddrs) {
225 for (arr = ISC_LIST_HEAD(nsrr->addrs);
226 arr != NULL; arr = ISC_LIST_NEXT(arr, link)) {
229 *addrs++ = arr->addr;
233 DPRINTF(("add_addrs: %d", n));
238 get_soa(res_state statp, const char *dname, ns_class class,
239 char *zname, size_t zsize, char *mname, size_t msize,
242 char tname[NS_MAXDNAME];
243 double resp[NS_PACKETSZ / sizeof (double)];
244 int n, i, ancount, nscount;
251 * Find closest enclosing SOA, even if it's for the root zone.
254 /* First canonicalize dname (exactly one unescaped trailing "."). */
255 status = ns_makecanon(dname, tname, sizeof tname);
256 if (status != ISC_R_SUCCESS)
260 /* Now grovel the subdomains, hunting for an SOA answer or auth. */
262 /* Leading or inter-label '.' are skipped here. */
263 while (*dname == '.')
266 /* Is there an SOA? */
267 rcode = do_query(statp, dname, class, ns_t_soa,
269 if (rcode != ISC_R_SUCCESS) {
270 DPRINTF(("get_soa: do_query('%s', %s) failed (%d)",
271 dname, p_class(class), n));
275 DPRINTF(("get_soa: CNAME or DNAME found"));
276 sect = ns_s_max, n = 0;
278 ancount = ns_msg_count(msg, ns_s_an);
279 nscount = ns_msg_count(msg, ns_s_ns);
280 if (ancount > 0 && rcode == ISC_R_SUCCESS)
281 sect = ns_s_an, n = ancount;
282 else if (nscount > 0)
283 sect = ns_s_ns, n = nscount;
285 sect = ns_s_max, n = 0;
287 for (i = 0; i < n; i++) {
293 rcode = ns_parserr(&msg, sect, i, &rr) < 0;
294 if (rcode != ISC_R_SUCCESS) {
295 DPRINTF(("get_soa: ns_parserr(%s, %d) failed",
296 p_section(sect, ns_o_query), i));
299 if (ns_rr_type(rr) == ns_t_cname ||
300 ns_rr_type(rr) == ns_t_dname)
302 if (ns_rr_type(rr) != ns_t_soa ||
303 ns_rr_class(rr) != class)
308 if (ns_samedomain(dname, t) == 0) {
309 DPRINTF(("get_soa: %s'%s', '%s') == 0",
310 "ns_samedomain(", dname, t));
311 return ISC_R_NOTZONE;
315 if (ns_samename(dname, t) == 1 ||
316 ns_samedomain(dname, t) == 0) {
317 DPRINTF(("get_soa: %smain('%s', '%s')",
318 "ns_samename() || !ns_samedo",
320 return ISC_R_NOTZONE;
326 if (strlen(t) + 1 > zsize) {
327 DPRINTF(("get_soa: zname(%d) too small (%d)",
328 zsize, strlen(t) + 1));
329 return ISC_R_NOSPACE;
332 rdata = ns_rr_rdata(rr);
333 rdlen = ns_rr_rdlen(rr);
334 if (ns_name_uncompress((u_char *)resp,
335 ns_msg_end(msg), rdata,
337 DPRINTF(("get_soa: %s failed",
338 "ns_name_uncompress"));
339 return ISC_R_NOMEMORY;
341 rcode = save_ns(statp, &msg,
342 ns_s_ns, zname, class, nsrrsp);
343 if (rcode != ISC_R_SUCCESS) {
344 DPRINTF(("get_soa: save_ns failed"));
347 return ISC_R_SUCCESS;
350 /* If we're out of labels, then not even "." has an SOA! */
354 /* Find label-terminating "."; top of loop will skip it. */
355 while (*dname != '.') {
357 if (*++dname == '\0') {
363 DPRINTF(("get_soa: out of labels"));
364 return ISC_R_DESTADDRREQ;
368 get_ns(res_state statp, const char *zname, ns_class class, rrset_ns *nsrrsp) {
369 double resp[NS_PACKETSZ / sizeof (double)];
374 /* Go and get the NS RRs for this zone. */
375 rcode = do_query(statp, zname, class, ns_t_ns, resp, &msg, &n);
376 if (rcode != ISC_R_SUCCESS) {
377 DPRINTF(("get_ns: do_query('zname', %s) failed (%d)",
378 zname, p_class(class), rcode));
382 /* Remember the NS RRs and associated A RRs that came back. */
383 rcode = save_ns(statp, &msg, ns_s_an, zname, class, nsrrsp);
384 if (rcode != ISC_R_SUCCESS) {
385 DPRINTF(("get_ns save_ns('%s', %s) failed",
386 zname, p_class(class)));
390 return ISC_R_SUCCESS;
394 get_glue(res_state statp, ns_class class, rrset_ns *nsrrsp) {
395 rr_ns *nsrr, *nsrr_n;
397 /* Go and get the A RRs for each empty NS RR on our list. */
398 for (nsrr = ISC_LIST_HEAD(*nsrrsp); nsrr != NULL; nsrr = nsrr_n) {
399 double resp[NS_PACKETSZ / sizeof (double)];
404 nsrr_n = ISC_LIST_NEXT(nsrr, link);
406 if (ISC_LIST_EMPTY(nsrr->addrs)) {
407 rcode = do_query(statp, nsrr->name, class, ns_t_a,
409 if (rcode != ISC_R_SUCCESS) {
410 DPRINTF(("get_glue: do_query('%s', %s') failed",
411 nsrr->name, p_class(class)));
416 "get_glue: do_query('%s', %s') CNAME or DNAME found",
417 nsrr->name, p_class(class)));
419 rcode = save_a(statp, &msg, ns_s_an, nsrr->name, class,
421 if (rcode != ISC_R_SUCCESS) {
422 DPRINTF(("get_glue: save_r('%s', %s) failed",
423 nsrr->name, p_class(class)));
426 /* If it's still empty, it's just chaff. */
427 if (ISC_LIST_EMPTY(nsrr->addrs)) {
428 DPRINTF(("get_glue: removing empty '%s' NS",
430 free_nsrr(nsrrsp, nsrr);
434 return ISC_R_SUCCESS;
438 save_ns(res_state statp, ns_msg *msg, ns_sect sect,
439 const char *owner, ns_class class,
445 for (i = 0; i < ns_msg_count(*msg, sect); i++) {
446 char tname[MAXDNAME];
452 rcode = ns_parserr(msg, sect, i, &rr);
453 if (rcode != ISC_R_SUCCESS) {
454 DPRINTF(("save_ns: ns_parserr(%s, %d) failed",
455 p_section(sect, ns_o_query), i));
458 if (ns_rr_type(rr) != ns_t_ns ||
459 ns_rr_class(rr) != class ||
460 ns_samename(ns_rr_name(rr), owner) != 1)
462 nsrr = find_ns(nsrrsp, ns_rr_name(rr));
464 nsrr = malloc(sizeof *nsrr);
466 DPRINTF(("save_ns: malloc failed"));
467 return ISC_R_NOMEMORY;
469 rdata = ns_rr_rdata(rr);
470 rdlen = ns_rr_rdlen(rr);
471 if (ns_name_uncompress(ns_msg_base(*msg),
472 ns_msg_end(*msg), rdata,
473 tname, sizeof tname) < 0) {
474 DPRINTF(("save_ns: ns_name_uncompress failed"));
476 return ISC_R_NOMEMORY;
478 nsrr->name = strdup(tname);
479 if (nsrr->name == NULL) {
480 DPRINTF(("save_ns: strdup failed"));
482 return ISC_R_NOMEMORY;
484 ISC_LIST_INIT(nsrr->addrs);
485 ISC_LIST_APPEND(*nsrrsp, nsrr, link);
487 rcode = save_a(statp, msg, ns_s_ar,
488 nsrr->name, class, &nsrr->addrs);
489 if (rcode != ISC_R_SUCCESS) {
490 DPRINTF(("save_ns: save_r('%s', %s) failed",
491 nsrr->name, p_class(class)));
495 return ISC_R_SUCCESS;
499 save_a(res_state statp, ns_msg *msg, ns_sect sect,
500 const char *owner, ns_class class,
506 for (i = 0; i < ns_msg_count(*msg, sect); i++) {
510 rcode = ns_parserr(msg, sect, i, &rr);
511 if (rcode != ISC_R_SUCCESS) {
512 DPRINTF(("save_a: ns_parserr(%s, %d) failed",
513 p_section(sect, ns_o_query), i));
516 if (ns_rr_type(rr) != ns_t_a ||
517 ns_rr_class(rr) != class ||
518 ns_samename(ns_rr_name(rr), owner) != 1 ||
519 ns_rr_rdlen(rr) != NS_INADDRSZ)
521 arr = malloc(sizeof *arr);
523 DPRINTF(("save_a: malloc failed"));
524 return ISC_R_NOMEMORY;
526 memcpy(&arr->addr, ns_rr_rdata(rr), NS_INADDRSZ);
527 ISC_LIST_APPEND(*arrsp, arr, link);
529 return ISC_R_SUCCESS;
533 free_nsrrset(rrset_ns *nsrrsp) {
536 while ((nsrr = ISC_LIST_HEAD(*nsrrsp)) != NULL)
537 free_nsrr(nsrrsp, nsrr);
541 free_nsrr(rrset_ns *nsrrsp, rr_ns *nsrr) {
544 while ((arr = ISC_LIST_HEAD(nsrr->addrs)) != NULL) {
545 ISC_LIST_UNLINK(nsrr->addrs, arr, link);
548 free((char *)nsrr->name);
549 ISC_LIST_UNLINK(*nsrrsp, nsrr, link);
554 find_ns(rrset_ns *nsrrsp, const char *dname) {
557 for (nsrr = ISC_LIST_HEAD(*nsrrsp);
558 nsrr != NULL; nsrr = ISC_LIST_NEXT(nsrr, link))
559 if (ns_samename(nsrr->name, dname) == 1)
565 do_query(res_state statp, const char *dname, ns_class class, ns_type qtype,
566 double *resp, ns_msg *msg, int *alias_count)
568 double req[NS_PACKETSZ / sizeof (double)];
573 status = res_nmkquery(statp, ns_o_query, dname, class, qtype,
574 NULL, 0, NULL, req, NS_PACKETSZ, &n);
575 if (status != ISC_R_SUCCESS) {
576 DPRINTF(("do_query: res_nmkquery failed"));
579 status = res_nsend(statp, req, n, resp, NS_PACKETSZ, &n);
580 if (status != ISC_R_SUCCESS) {
581 DPRINTF(("do_query: res_nsend failed"));
585 DPRINTF(("do_query: res_nsend returned 0"));
586 return ISC_R_NOTFOUND;
588 if (ns_initparse((u_char *)resp, n, msg) < 0) {
589 DPRINTF(("do_query: ns_initparse failed"));
590 return ISC_R_NOSPACE;
593 for (i = 0; i < ns_msg_count(*msg, ns_s_an); i++) {
596 status = ns_parserr(msg, ns_s_an, i, &rr);
597 if (status != ISC_R_SUCCESS) {
598 DPRINTF(("do_query: ns_parserr failed"));
601 n += (ns_rr_class(rr) == class &&
602 (ns_rr_type(rr) == ns_t_cname ||
603 ns_rr_type(rr) == ns_t_dname));
607 return ISC_R_SUCCESS;