#endif /* not lint */
/*
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Import. */
#include "port_before.h"
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "port_after.h"
#include <resolv.h>
/* Data structures. */
typedef struct rr_a {
} rr_a;
typedef struct rr_ns {
const char * name;
unsigned int flags;
} rr_ns;
/* Forward. */
union res_sockaddr_union *, int);
union res_sockaddr_union *, int);
rrset_ns *);
static void free_nsrrset(rrset_ns *);
/* Macros. */
#define DPRINTF(x) do {\
int save_errno = errno; \
errno = save_errno; \
} while (0)
/* Public. */
/*%
* find enclosing zone for a <dname,class>, and some server addresses
*
* parameters:
*\li res - resolver context to work within (is modified)
*\li dname - domain name whose enclosing zone is desired
*\li class - class of dname (and its enclosing zone)
*\li zname - found zone name
*\li zsize - allocated size of zname
*\li addrs - found server addresses
*\li naddrs - max number of addrs
*
* return values:
*\li < 0 - an error occurred (check errno)
*\li = 0 - zname is now valid, but addrs[] wasn't changed
*\li > 0 - zname is now valid, and return value is number of addrs[] found
*
* notes:
*\li this function calls res_nsend() which means it depends on correctly
* functioning recursive nameservers (usually defined in /etc/resolv.conf
* or its local equivilent).
*
*\li we start by asking for an SOA<dname,class>. if we get one as an
* answer, that just means <dname,class> is a zone top, which is fine.
* more than likely we'll be told to go pound sand, in the form of a
* negative answer.
*
*\li note that we are not prepared to deal with referrals since that would
* only come from authority servers and our correctly functioning local
* recursive server would have followed the referral and got us something
* more definite.
*
*\li if the authority section contains an SOA, this SOA should also be the
* closest enclosing zone, since any intermediary zone cuts would've been
* returned as referrals and dealt with by our correctly functioning local
* recursive name server. but an SOA in the authority section should NOT
* match our dname (since that would have been returned in the answer
* section). an authority section SOA has to be "above" our dname.
*
*\li however, since authority section SOA's were once optional, it's
* possible that we'll have to go hunting for the enclosing SOA by
* ripping labels off the front of our dname -- this is known as "doing
* it the hard way."
*
*\li ultimately we want some server addresses, which are ideally the ones
* pertaining to the SOA.MNAME, but only if there is a matching NS RR.
* so the second phase (after we find an SOA) is to go looking for the
* NS RRset for that SOA's zone.
*
*\li no answer section processed by this code is allowed to contain CNAME
* or DNAME RR's. for the SOA query this means we strip a label and
* keep going. for the NS and A queries this means we just give up.
*/
int
{
int result, i;
union res_sockaddr_union *u;
opts |= RES_IPV4ONLY;
opts &= ~RES_IPV6ONLY;
if (u == NULL)
return(-1);
u, naddrs);
for (i = 0; i < result; i++) {
}
free(u);
return (result);
}
int
int naddrs)
{
int n;
DPRINTF(("START dname='%s' class=%s, zsize=%ld, naddrs=%d",
DPRINTF(("get the soa, and see if it has enough glue"));
((opts & RES_EXHAUSTIVE) == 0 &&
goto done;
DPRINTF(("get the ns rrset and see if it has enough glue"));
((opts & RES_EXHAUSTIVE) == 0 &&
goto done;
DPRINTF(("get the missing glue and see if it's finally enough"));
done:
return (n);
}
/* Private. */
static int
{
int n, x;
n = 0;
addrs += x;
naddrs -= x;
n += x;
}
addrs += x;
naddrs -= x;
n += x;
}
return (n);
}
static int
{
int n = 0;
if (naddrs <= 0)
return (0);
naddrs--;
n++;
}
DPRINTF(("add_addrs: %d", n));
return (n);
}
static int
{
/*
* Find closest enclosing SOA, even if it's for the root zone.
*/
/* First canonicalize dname (exactly one unescaped trailing "."). */
goto cleanup;
goto cleanup;
/* Now grovel the subdomains, hunting for an SOA answer or auth. */
for (;;) {
/* Leading or inter-label '.' are skipped here. */
while (*dname == '.')
dname++;
/* Is there an SOA? */
if (n < 0) {
DPRINTF(("get_soa: do_query('%s', %s) failed (%d)",
goto cleanup;
}
if (n > 0) {
DPRINTF(("get_soa: CNAME or DNAME found"));
} else {
else if (nscount > 0)
else
}
for (i = 0; i < n; i++) {
const char *t;
DPRINTF(("get_soa: ns_parserr(%s, %d) failed",
goto cleanup;
}
break;
continue;
t = ns_rr_name(rr);
switch (sect) {
case ns_s_an:
if (ns_samedomain(dname, t) == 0) {
("get_soa: ns_samedomain('%s', '%s') == 0",
dname, t)
);
errno = EPROTOTYPE;
goto cleanup;
}
break;
case ns_s_ns:
ns_samedomain(dname, t) == 0) {
("get_soa: ns_samename() || !ns_samedomain('%s', '%s')",
dname, t)
);
errno = EPROTOTYPE;
goto cleanup;
}
break;
default:
abort();
}
DPRINTF(("get_soa: zname(%lu) too small (%lu)",
(unsigned long)zsize,
(unsigned long)strlen(t) + 1));
goto cleanup;
}
DPRINTF(("get_soa: ns_name_uncompress failed")
);
goto cleanup;
}
DPRINTF(("get_soa: save_ns failed"));
goto cleanup;
}
return (0);
}
/* If we're out of labels, then not even "." has an SOA! */
if (*dname == '\0')
break;
/* Find label-terminating "."; top of loop will skip it. */
while (*dname != '.') {
if (*dname == '\\')
if (*++dname == '\0') {
goto cleanup;
}
dname++;
}
}
DPRINTF(("get_soa: out of labels"));
return (-1);
}
static int
{
int n;
return (-1);
/* Go and get the NS RRs for this zone. */
if (n != 0) {
DPRINTF(("get_ns: do_query('%s', %s) failed (%d)",
return (-1);
}
/* Remember the NS RRs and associated A RRs that came back. */
DPRINTF(("get_ns save_ns('%s', %s) failed",
return (-1);
}
return (0);
}
static int
return(-1);
/* Go and get the A RRs for each empty NS RR on our list. */
int n;
if (n < 0) {
("get_glue: do_query('%s', %s') failed",
goto cleanup;
}
if (n > 0) {
DPRINTF((
"get_glue: do_query('%s', %s') CNAME or DNAME found",
}
DPRINTF(("get_glue: save_r('%s', %s) failed",
goto cleanup;
}
}
if (n < 0) {
("get_glue: do_query('%s', %s') failed",
goto cleanup;
}
if (n > 0) {
DPRINTF((
"get_glue: do_query('%s', %s') CNAME or DNAME found",
}
DPRINTF(("get_glue: save_r('%s', %s) failed",
goto cleanup;
}
}
/* If it's still empty, it's just chaff. */
DPRINTF(("get_glue: removing empty '%s' NS",
}
}
return (0);
return (-1);
}
static int
{
int i;
DPRINTF(("save_ns: ns_parserr(%s, %d) failed",
return (-1);
}
continue;
DPRINTF(("save_ns: malloc failed"));
return (-1);
}
DPRINTF(("save_ns: ns_name_uncompress failed")
);
return (-1);
}
DPRINTF(("save_ns: strdup failed"));
return (-1);
}
}
DPRINTF(("save_ns: save_r('%s', %s) failed",
return (-1);
}
}
return (0);
}
static int
{
int i;
DPRINTF(("save_a: ns_parserr(%s, %d) failed",
return (-1);
}
continue;
continue;
continue;
DPRINTF(("save_a: malloc failed"));
return (-1);
}
switch (ns_rr_type(rr)) {
case ns_t_a:
#ifdef HAVE_SA_LEN
#endif
break;
case ns_t_aaaa:
#ifdef HAVE_SA_LEN
#endif
break;
default:
abort();
}
}
return (0);
}
static void
}
static void
char *tmp;
}
}
static rr_ns *
return (nsrr);
return (NULL);
}
static int
{
int i, n;
if (n < 0) {
DPRINTF(("do_query: res_nmkquery failed"));
return (-1);
}
if (n < 0) {
DPRINTF(("do_query: res_nsend failed"));
return (-1);
}
if (n == 0) {
DPRINTF(("do_query: res_nsend returned 0"));
return (-1);
}
DPRINTF(("do_query: ns_initparse failed"));
return (-1);
}
n = 0;
DPRINTF(("do_query: ns_parserr failed"));
return (-1);
}
}
return (n);
}
static void
}
/*! \file */