query.c revision 84d821c5218f61a8e66463da92eedf4b26f7ce0a
/*
* Copyright (C) 1999 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM 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.
*/
#include <config.h>
#include <isc/assertions.h>
#include <dns/dispatch.h>
#include <dns/fixedname.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
NS_QUERYATTR_PARTIALANSWER) != 0)
static inline void
}
}
/*
* We don't need to free items from these two lists because they
* will be taken care of when the message is reset.
*/
}
static void
(void)result;
}
void
}
static inline isc_result_t
if (result != ISC_R_SUCCESS)
return (result);
return (ISC_R_SUCCESS);
}
static inline isc_dynbuffer_t *
isc_region_t r;
if (result != ISC_R_SUCCESS)
return (NULL);
}
if (r.length < 255) {
if (result != ISC_R_SUCCESS)
return (NULL);
}
return (dbuf);
}
static inline void
isc_region_t r;
/*
* 'name' is using space in 'dbuf', but 'dbuf' has not yet been
* adjusted to take account of that. We do the adjustment.
*/
dns_name_toregion(name, &r);
}
static inline void
if (dns_name_hasbuffer(name)) {
!= 0);
}
}
static inline dns_name_t *
{
isc_region_t r;
if (result != ISC_R_SUCCESS)
return (NULL);
} else
return (name);
}
static inline dns_rdataset_t *
&rdataset);
if (result != ISC_R_SUCCESS)
return (NULL);
} else
return (rdataset);
}
return (query_newnamebuf(client));
}
static isc_result_t
isc_buffer_t b;
/* XXXRTH Other requirements. */
/*
* XXXRTH Should special case 'A' type. If type is 'A', we should
* look for A6 and AAAA too.
*/
/*
* Initialization.
*/
/*
* Get some resources...
*/
goto cleanup;
goto cleanup;
/*
* Find a database to answer the query.
*/
goto cleanup;
/*
* Now look for an answer in the database.
*/
switch (result) {
case DNS_R_SUCCESS:
case DNS_R_GLUE:
break;
default:
goto cleanup;
}
/*
* Suppress duplicates.
*/
for (section = DNS_SECTION_ANSWER;
section++) {
if (result == ISC_R_SUCCESS) {
/*
* We've already got this RRset in the response.
*/
goto cleanup;
}
}
/*
* In a few cases, we want to add additional data for additional
* data. It's simpler to just deal with special cases here than
* to try to create a general purpose mechanism and allow the
* rdata implementations to do it themselves.
*
* This involves recursion, but the depth is limited. The
* most complex case is adding a SRV rdataset, which involves
* recursing to add address records, which in turn can cause
* recursion to add KEYs.
*/
type == dns_rdatatype_a6) {
/*
* RFC 2535 section 3.5 says that when A or AAAA records are
* retrieved as additional data, any KEY RRs for the owner name
* should be added to the additional data section. We include
* A6 in the list of types with such treatment.
*
* XXXRTH We should lower the priority here. Alternatively,
* we could raise the priority of glue records.
*/
} else if (type == dns_rdatatype_srv) {
/*
* If we're adding SRV records to the additional data
* section, it's helpful if we add the SRV additional data
* as well.
*/
client);
}
}
dns_db_detach(&db);
return (eresult);
}
static inline void
{
/*
* We don't care if dns_rdataset_additionaldata() fails.
*/
client);
/*
* RFC 2535 section 3.5 says that when NS, SOA, A, or AAAA records
* are retrieved, any KEY RRs for the owner name should be added
* to the additional data section. We include A6 in the list of
* types with such treatment.
*
* We don't care if query_additional() fails.
*/
type == dns_rdatatype_a6) {
/*
* XXXRTH We should lower the priority here. Alternatively,
* we could raise the priority of glue records.
*/
}
}
static inline void
{
if (result == ISC_R_SUCCESS) {
/*
* We've already got an RRset of the given name and type.
* There's nothing else to do;
*/
return;
}
if (result == DNS_R_NXDOMAIN) {
/*
* The name doesn't exist.
*/
} else
}
static inline isc_result_t
/*
* Initialization.
*/
/*
* Get resources and make 'name' be the database origin.
*/
if (result != ISC_R_SUCCESS)
return (result);
goto cleanup;
}
/*
* Find the SOA.
*/
if (result != ISC_R_SUCCESS) {
/*
* This is bad. We tried to get the SOA RR at the zone top
* and it didn't work!
*
* The note above about temporary leakage applies here too.
*/
} else
}
return (eresult);
}
static inline isc_result_t
/*
* XXXRTH SIG is here only temporarily.
* OPT still needs to be added.
* Should get help with this from rdata.c
*/
switch (type) {
case dns_rdatatype_sig:
return (DNS_R_NOTIMP);
case dns_rdatatype_tkey:
return (DNS_R_NOTIMP);
case dns_rdatatype_tsig:
return (DNS_R_FORMERR);
case dns_rdatatype_ixfr:
case dns_rdatatype_axfr:
case dns_rdatatype_mailb:
case dns_rdatatype_maila:
return (DNS_R_REFUSED);
default:
break;
}
return (ISC_R_SUCCESS);
}
#define MAX_RESTARTS 16
#define QUERY_ERROR(r) \
do { \
eresult = r; \
want_restart = ISC_FALSE; \
} while (0)
static void
int order;
isc_region_t r;
isc_buffer_t b;
/*
* One-time initialization.
*
* It's especially important to initialize anything that the cleanup
* code might cleanup.
*/
restarts = 0;
}
/*
* First we must find the right database.
*/
if (result == ISC_R_NOTFOUND) {
/*
* We're not directly authoritative for this query name, nor
* is it a subdomain of any zone for which we're
* authoritative.
*/
if (!use_cache) {
/*
* If we can't use the cache, either because we
* don't have one or because its use has been
* disallowed, there's no more progress we can make
* on this query.
*/
goto cleanup;
}
/*
* Something is broken.
*/
goto cleanup;
}
if (is_zone)
/*
* Find the first unanswered type in the question section.
*/
qcount = 0;
}
qcount++;
}
}
/*
* We had better have found something!
*/
/*
* If there's more than one question, we'll retrieve the node and
* iterate it, trying to find answers.
*/
if (qcount == 1)
else {
/* XXXRTH */
goto cleanup;
}
/*
* See if the type is OK.
*/
if (result != ISC_R_SUCCESS) {
goto cleanup;
}
/*
* We'll need some resources...
*/
goto cleanup;
}
goto cleanup;
}
/*
* Now look for an answer in the database.
*/
switch (result) {
case DNS_R_SUCCESS:
case DNS_R_ZONECUT:
/*
* These cases are handled in the main line below.
*/
break;
case DNS_R_DELEGATION:
if (is_zone) {
/*
* We're authoritative for an ancestor of QNAME.
*/
if (!use_cache) {
/*
* We don't have a cache, so this is the best
* answer.
*/
} else {
/*
* We might have a better answer or delegation
* in the cache. We'll remember the current
* values of fname and rdataset, and then
* go looking for QNAME in the cache. If we
* find something better, we'll use it instead.
*/
goto cleanup;
}
} else {
/*
* Recurse using the best delegation.
*/
}
goto cleanup;
case DNS_R_GLUE:
break;
case DNS_R_NXRDATASET:
/*
* Add SOA.
*/
if (result != ISC_R_SUCCESS) {
goto cleanup;
}
/*
* XXXRTH Add NXT record here.
*/
goto cleanup;
case DNS_R_NXDOMAIN:
if (restarts > 0) {
/*
* We hit a dead end following a CNAME or DNAME.
*/
goto cleanup;
}
/*
* Set message rcode.
*/
/*
* Add SOA.
*/
if (result != ISC_R_SUCCESS) {
goto cleanup;
}
/*
* XXXRTH Add NXT chain here.
*/
goto cleanup;
case DNS_R_NOTFOUND:
goto cleanup;
case DNS_R_CNAME:
/*
* Keep a copy of the rdataset. We have to do this because
* query_addrrset may clear 'rdataset' (to prevent the
* cleanup code from cleaning it up).
*/
/*
* Add the CNAME to the answer section.
*/
/*
* We set the PARTIALANSWER attribute so that if anything goes
* wrong later on, we'll return what we've got so far.
*/
/*
* Reset qname to be the target name of the CNAME and restart
* the query.
*/
if (result != ISC_R_SUCCESS)
goto cleanup;
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_name_fromregion(tname, &r);
goto cleanup;
case DNS_R_DNAME:
/*
* Compare the current qname to the found name. We need
* to know how many labels and bits are in common because
* we're going to have to split qname later on.
*/
/*
* Keep a copy of the rdataset. We have to do this because
* query_addrrset may clear 'rdataset' (to prevent the
* cleanup code from cleaning it up).
*/
/*
* Add the DNAME to the answer section.
*/
/*
* We set the PARTIALANSWER attribute so that if anything goes
* wrong later on, we'll return what we've got so far.
*/
/*
* Get the target name of the DNAME.
*/
if (result != ISC_R_SUCCESS)
goto cleanup;
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_name_fromregion(tname, &r);
/*
* Construct the new qname and restart the query.
*/
if (result != ISC_R_SUCCESS)
goto cleanup;
goto cleanup;
goto cleanup;
if (result != ISC_R_SUCCESS) {
if (result == ISC_R_NOSPACE) {
/*
* draft-ietf-dnsind-dname-03.txt, section
* 4.1, subsection 3c says we should
* return YXDOMAIN if the constructed
* name would be too long.
*/
}
goto cleanup;
}
goto cleanup;
default:
/*
* Something has gone wrong.
*/
goto cleanup;
}
if (type == dns_rdatatype_any) {
/*
* XXXRTH Need to handle zonecuts with special case
* code.
*/
n = 0;
if (result != ISC_R_SUCCESS) {
goto cleanup;
}
while (result == ISC_R_SUCCESS) {
n++;
/*
* We shouldn't ever fail to add 'rdataset' because
* it's already in the answer.
*/
/*
* We set dbuf to NULL because we only want the
* query_keepname() call in query_addrrset() to be
* called once.
*/
&rdataset);
if (result == ISC_R_SUCCESS) {
}
}
/*
* If we added at least one RRset, then we must clear fname,
* otherwise the cleanup code might cause it to be reused.
*/
if (n > 0)
if (result != DNS_R_NOMORE) {
goto cleanup;
}
} else {
/*
* This is the "normal" case -- an ordinary question to which
* we know the answer.
*/
/*
* Remember that we've answered this question.
*/
}
/*
* XXXRTH Handle additional questions above. Find all the question
* types we can from the node we found, and (if recursion is
* OK) launch queries for any types we don't have answers to.
*
* Special case: they make an ANY query as well as some
* other type. Perhaps ANY should be disallowed in a
* multiple question query?
*/
}
dns_db_detach(&db);
/*
* We're not authoritative, so we must ensure the AA bit
* isn't set.
*/
}
restarts++;
goto restart;
}
else
}
/*
* XXXRTH Problem areas.
*
* If we're authoritative for both a parent and a child, the
* child is non-secure, and we are asked for the KEY of the
* nonsecure child, we need to get it from the parent.
* If we're not auth for the parent, then we have to go
* looking for it in the cache. How do we even know who
* the parent is? We probably won't find this KEY when doing
* additional data KEY retrievals, but that's probably OK,
* since it's a SHOULD not a MUST. We don't want to be doing
* tons of work just to fill out additional data.
*
* Similar problems occur with NXT queries, since there can
* be NXT records at a delegation point in both the parent
* and the child. RFC 2535 section 5.5 says that on explicit
* query we should return both, if available. That seems
* to imply we shouldn't recurse to get the missing one
* if only one is available. Is that right?
*/
void
/*
* Ensure that appropriate cleanups occur.
*/
/*
* XXXRTH Deal with allow-query and allow-recursion here.
*/
/*
* If the client doesn't want recursion, turn it off.
*/
/*
* Get the question name.
*/
if (result != ISC_R_SUCCESS) {
return;
}
if (result != ISC_R_NOMORE) {
if (result == ISC_R_SUCCESS) {
/*
* There's more than one QNAME in the question
* section.
*/
} else
return;
}
/*
* Check for illegal meta-classes and meta-types in
* multiple question queries (edns1 section 5.1).
*/
return;
}
return;
}
}
}
/*
* Check for meta-queries like IXFR and AXFR.
*/
case dns_rdatatype_any:
break; /* Let query_find handle it. */
case dns_rdatatype_ixfr:
case dns_rdatatype_axfr:
#ifdef notyet
return;
#endif
case dns_rdatatype_maila:
case dns_rdatatype_mailb:
return;
default: /* TSIG, etc. */
return;
}
}
}
/* This is an ordinary query. */
if (result != ISC_R_SUCCESS) {
return;
}
/*
* Assume authoritative response until it is known to be
* otherwise.
*/
}