#include <config.h>
#include <string.h>
#include <isc/mem.h>
#include <isc/util.h>
#include <dns/adb.h>
#include <dns/byaddr.h>
#include <dns/db.h>
#include <dns/events.h>
#include <dns/message.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/rdatastruct.h>
#include <dns/rdatatype.h>
#include <dns/resolver.h>
#include <dns/result.h>
#include <dns/stats.h>
#include <dns/tkey.h>
#include <dns/view.h>
#include <dns/zone.h>
#include <dns/zt.h>
#include <named/client.h>
#include <named/log.h>
#include <named/server.h>
#include <named/sortlist.h>
#include <named/xfrout.h>
#define PARTIALANSWER(c) (((c)->query.attributes & \
NS_QUERYATTR_PARTIALANSWER) != 0)
#define USECACHE(c) (((c)->query.attributes & \
NS_QUERYATTR_CACHEOK) != 0)
#define RECURSIONOK(c) (((c)->query.attributes & \
NS_QUERYATTR_RECURSIONOK) != 0)
#define RECURSING(c) (((c)->query.attributes & \
NS_QUERYATTR_RECURSING) != 0)
#define CACHEGLUEOK(c) (((c)->query.attributes & \
NS_QUERYATTR_CACHEGLUEOK) != 0)
#define WANTRECURSION(c) (((c)->query.attributes & \
NS_QUERYATTR_WANTRECURSION) != 0)
#define WANTDNSSEC(c) (((c)->query.attributes & \
NS_QUERYATTR_WANTDNSSEC) != 0)
#define NOAUTHORITY(c) (((c)->query.attributes & \
NS_QUERYATTR_NOAUTHORITY) != 0)
#define NOADDITIONAL(c) (((c)->query.attributes & \
NS_QUERYATTR_NOADDITIONAL) != 0)
#if 0
#define CTRACE(m) isc_log_write(ns_g_lctx, \
NS_LOGCATEGORY_CLIENT, \
NS_LOGMODULE_QUERY, \
ISC_LOG_DEBUG(3), \
"client %p: %s", client, (m))
#define QTRACE(m) isc_log_write(ns_g_lctx, \
NS_LOGCATEGORY_GENERAL, \
NS_LOGMODULE_QUERY, \
ISC_LOG_DEBUG(3), \
"query %p: %s", query, (m))
#else
#define CTRACE(m) ((void)m)
#define QTRACE(m) ((void)m)
#endif
#define DNS_GETDB_NOEXACT 0x01U
#define DNS_GETDB_NOLOG 0x02U
static unsigned char ip6int_ndata[] = "\003ip6\003int";
static unsigned char ip6int_offsets[] = { 0, 4, 8 };
static dns_name_t ip6int_name = {
DNS_NAME_MAGIC,
ip6int_ndata, 9, 3,
DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
ip6int_offsets, NULL,
{(void *)-1, (void *)-1},
{NULL, NULL}
};
static isc_result_t
query_simplefind(void *arg, dns_name_t *name, dns_rdatatype_t type,
isc_stdtime_t now,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
static inline void
query_adda6rrset(void *arg, dns_name_t *name, dns_rdataset_t *rdataset,
dns_rdataset_t *sigrdataset);
static void
query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype);
static void
synth_fwd_start(ns_client_t *client);
static void
synth_fwd_startfind(ns_client_t *client);
static void
synth_fwd_respond(ns_client_t *client, dns_adbfind_t *find);
static void
synth_fwd_finddone(isc_task_t *task, isc_event_t *ev);
static void
synth_finish(ns_client_t *client, isc_result_t result);
static void
synth_rev_start(ns_client_t *client);
static void
synth_rev_byaddrdone_arpa(isc_task_t *task, isc_event_t *event);
static void
synth_rev_byaddrdone_int(isc_task_t *task, isc_event_t *event);
static void
synth_rev_respond(ns_client_t *client, dns_byaddrevent_t *bevent);
static inline void
inc_stats(ns_client_t *client, dns_statscounter_t counter) {
dns_zone_t *zone = client->query.authzone;
REQUIRE(counter < DNS_STATS_NCOUNTERS);
ns_g_server->querystats[counter]++;
if (zone != NULL) {
isc_uint64_t *zonestats = dns_zone_getstatscounters(zone);
if (zonestats != NULL)
zonestats[counter]++;
}
}
static void
query_send(ns_client_t *client) {
dns_statscounter_t counter;
if (client->message->rcode == dns_rcode_noerror) {
if (ISC_LIST_EMPTY(client->message->sections[DNS_SECTION_ANSWER])) {
if (client->query.isreferral) {
counter = dns_statscounter_referral;
} else {
counter = dns_statscounter_nxrrset;
}
} else {
counter = dns_statscounter_success;
}
} else if (client->message->rcode == dns_rcode_nxdomain) {
counter = dns_statscounter_nxdomain;
} else {
counter = dns_statscounter_failure;
}
inc_stats(client, counter);
ns_client_send(client);
}
static void
query_error(ns_client_t *client, isc_result_t result) {
inc_stats(client, dns_statscounter_failure);
ns_client_error(client, result);
}
static void
query_next(ns_client_t *client, isc_result_t result) {
inc_stats(client, dns_statscounter_failure);
ns_client_next(client, result);
}
static inline void
query_maybeputqname(ns_client_t *client) {
if (client->query.restarts > 0) {
dns_message_puttempname(client->message,
&client->query.qname);
client->query.qname = NULL;
}
}
static inline void
query_reset(ns_client_t *client, isc_boolean_t everything) {
isc_buffer_t *dbuf, *dbuf_next;
ns_dbversion_t *dbversion, *dbversion_next;
unsigned int i;
if (client->query.fetch != NULL) {
dns_resolver_cancelfetch(client->query.fetch);
client->query.fetch = NULL;
}
for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
dbversion != NULL;
dbversion = dbversion_next) {
dbversion_next = ISC_LIST_NEXT(dbversion, link);
dns_db_closeversion(dbversion->db, &dbversion->version,
ISC_FALSE);
dns_db_detach(&dbversion->db);
ISC_LIST_INITANDAPPEND(client->query.freeversions,
dbversion, link);
}
ISC_LIST_INIT(client->query.activeversions);
if (client->query.authdb != NULL)
dns_db_detach(&client->query.authdb);
if (client->query.authzone != NULL)
dns_zone_detach(&client->query.authzone);
for (dbversion = ISC_LIST_HEAD(client->query.freeversions), i = 0;
dbversion != NULL;
dbversion = dbversion_next, i++) {
dbversion_next = ISC_LIST_NEXT(dbversion, link);
if (i > 3 || everything) {
ISC_LIST_UNLINK(client->query.freeversions, dbversion,
link);
isc_mem_put(client->mctx, dbversion,
sizeof *dbversion);
}
}
for (dbuf = ISC_LIST_HEAD(client->query.namebufs);
dbuf != NULL;
dbuf = dbuf_next) {
dbuf_next = ISC_LIST_NEXT(dbuf, link);
if (dbuf_next != NULL || everything) {
ISC_LIST_UNLINK(client->query.namebufs, dbuf, link);
isc_buffer_free(&dbuf);
}
}
query_maybeputqname(client);
client->query.attributes = (NS_QUERYATTR_RECURSIONOK |
NS_QUERYATTR_CACHEOK);
client->query.restarts = 0;
client->query.timerset = ISC_FALSE;
client->query.origqname = NULL;
client->query.qname = NULL;
client->query.dboptions = 0;
client->query.fetchoptions = 0;
client->query.gluedb = NULL;
client->query.authdbset = ISC_FALSE;
client->query.isreferral = ISC_FALSE;
}
static void
query_next_callback(ns_client_t *client) {
query_reset(client, ISC_FALSE);
}
void
ns_query_free(ns_client_t *client) {
query_reset(client, ISC_TRUE);
}
static inline isc_result_t
query_newnamebuf(ns_client_t *client) {
isc_buffer_t *dbuf;
isc_result_t result;
CTRACE("query_newnamebuf");
dbuf = NULL;
result = isc_buffer_allocate(client->mctx, &dbuf, 1024);
if (result != ISC_R_SUCCESS) {
CTRACE("query_newnamebuf: isc_buffer_allocate failed: done");
return (result);
}
ISC_LIST_APPEND(client->query.namebufs, dbuf, link);
CTRACE("query_newnamebuf: done");
return (ISC_R_SUCCESS);
}
static inline isc_buffer_t *
query_getnamebuf(ns_client_t *client) {
isc_buffer_t *dbuf;
isc_result_t result;
isc_region_t r;
CTRACE("query_getnamebuf");
if (ISC_LIST_EMPTY(client->query.namebufs)) {
result = query_newnamebuf(client);
if (result != ISC_R_SUCCESS) {
CTRACE("query_getnamebuf: query_newnamebuf failed: done");
return (NULL);
}
}
dbuf = ISC_LIST_TAIL(client->query.namebufs);
INSIST(dbuf != NULL);
isc_buffer_availableregion(dbuf, &r);
if (r.length < 255) {
result = query_newnamebuf(client);
if (result != ISC_R_SUCCESS) {
CTRACE("query_getnamebuf: query_newnamebuf failed: done");
return (NULL);
}
dbuf = ISC_LIST_TAIL(client->query.namebufs);
isc_buffer_availableregion(dbuf, &r);
INSIST(r.length >= 255);
}
CTRACE("query_getnamebuf: done");
return (dbuf);
}
static inline void
query_keepname(ns_client_t *client, dns_name_t *name, isc_buffer_t *dbuf) {
isc_region_t r;
CTRACE("query_keepname");
REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) != 0);
dns_name_toregion(name, &r);
isc_buffer_add(dbuf, r.length);
dns_name_setbuffer(name, NULL);
client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
}
static inline void
query_releasename(ns_client_t *client, dns_name_t **namep) {
dns_name_t *name = *namep;
CTRACE("query_releasename");
if (dns_name_hasbuffer(name)) {
INSIST((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED)
!= 0);
client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
}
dns_message_puttempname(client->message, namep);
CTRACE("query_releasename: done");
}
static inline dns_name_t *
query_newname(ns_client_t *client, isc_buffer_t *dbuf,
isc_buffer_t *nbuf)
{
dns_name_t *name;
isc_region_t r;
isc_result_t result;
REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) == 0);
CTRACE("query_newname");
name = NULL;
result = dns_message_gettempname(client->message, &name);
if (result != ISC_R_SUCCESS) {
CTRACE("query_newname: dns_message_gettempname failed: done");
return (NULL);
}
isc_buffer_availableregion(dbuf, &r);
isc_buffer_init(nbuf, r.base, r.length);
dns_name_init(name, NULL);
dns_name_setbuffer(name, nbuf);
client->query.attributes |= NS_QUERYATTR_NAMEBUFUSED;
CTRACE("query_newname: done");
return (name);
}
static inline dns_rdataset_t *
query_newrdataset(ns_client_t *client) {
dns_rdataset_t *rdataset;
isc_result_t result;
CTRACE("query_newrdataset");
rdataset = NULL;
result = dns_message_gettemprdataset(client->message, &rdataset);
if (result != ISC_R_SUCCESS) {
CTRACE("query_newrdataset: "
"dns_message_gettemprdataset failed: done");
return (NULL);
}
dns_rdataset_init(rdataset);
CTRACE("query_newrdataset: done");
return (rdataset);
}
static inline void
query_putrdataset(ns_client_t *client, dns_rdataset_t **rdatasetp) {
dns_rdataset_t *rdataset = *rdatasetp;
CTRACE("query_putrdataset");
if (rdataset != NULL) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
dns_message_puttemprdataset(client->message, rdatasetp);
}
CTRACE("query_putrdataset: done");
}
static inline isc_result_t
query_newdbversion(ns_client_t *client, unsigned int n) {
unsigned int i;
ns_dbversion_t *dbversion;
for (i = 0; i < n; i++) {
dbversion = isc_mem_get(client->mctx, sizeof *dbversion);
if (dbversion != NULL) {
dbversion->db = NULL;
dbversion->version = NULL;
ISC_LIST_INITANDAPPEND(client->query.freeversions,
dbversion, link);
} else {
if (i == 0)
return (ISC_R_NOMEMORY);
else
return (ISC_R_SUCCESS);
}
}
return (ISC_R_SUCCESS);
}
static inline ns_dbversion_t *
query_getdbversion(ns_client_t *client) {
isc_result_t result;
ns_dbversion_t *dbversion;
if (ISC_LIST_EMPTY(client->query.freeversions)) {
result = query_newdbversion(client, 1);
if (result != ISC_R_SUCCESS)
return (NULL);
}
dbversion = ISC_LIST_HEAD(client->query.freeversions);
INSIST(dbversion != NULL);
ISC_LIST_UNLINK(client->query.freeversions, dbversion, link);
return (dbversion);
}
isc_result_t
ns_query_init(ns_client_t *client) {
isc_result_t result;
ISC_LIST_INIT(client->query.namebufs);
ISC_LIST_INIT(client->query.activeversions);
ISC_LIST_INIT(client->query.freeversions);
client->query.restarts = 0;
client->query.timerset = ISC_FALSE;
client->query.qname = NULL;
client->query.fetch = NULL;
client->query.authdb = NULL;
client->query.authzone = NULL;
client->query.authdbset = ISC_FALSE;
client->query.isreferral = ISC_FALSE;
query_reset(client, ISC_FALSE);
result = query_newdbversion(client, 3);
if (result != ISC_R_SUCCESS)
return (result);
dns_a6_init(&client->query.a6ctx, query_simplefind, query_adda6rrset,
NULL, NULL, client);
return (query_newnamebuf(client));
}
static inline ns_dbversion_t *
query_findversion(ns_client_t *client, dns_db_t *db,
isc_boolean_t *newzonep)
{
ns_dbversion_t *dbversion;
for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
dbversion != NULL;
dbversion = ISC_LIST_NEXT(dbversion, link)) {
if (dbversion->db == db)
break;
}
if (dbversion == NULL) {
dbversion = query_getdbversion(client);
if (dbversion == NULL)
return (NULL);
dns_db_attach(db, &dbversion->db);
dns_db_currentversion(db, &dbversion->version);
dbversion->queryok = ISC_FALSE;
ISC_LIST_APPEND(client->query.activeversions,
dbversion, link);
*newzonep = ISC_TRUE;
} else
*newzonep = ISC_FALSE;
return (dbversion);
}
static inline isc_result_t
query_getzonedb(ns_client_t *client, dns_name_t *name, unsigned int options,
dns_zone_t **zonep, dns_db_t **dbp, dns_dbversion_t **versionp)
{
isc_result_t result;
isc_boolean_t check_acl, new_zone;
dns_acl_t *queryacl;
ns_dbversion_t *dbversion;
unsigned int ztoptions;
dns_zone_t *zone = NULL;
dns_db_t *db = NULL;
REQUIRE(zonep != NULL && *zonep == NULL);
REQUIRE(dbp != NULL && *dbp == NULL);
ztoptions = ((options & DNS_GETDB_NOEXACT) != 0) ?
DNS_ZTFIND_NOEXACT : 0;
result = dns_zt_find(client->view->zonetable, name, ztoptions, NULL,
&zone);
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
result = dns_zone_getdb(zone, &db);
if (result != ISC_R_SUCCESS)
goto fail;
if (!client->view->additionalfromauth &&
client->query.authdbset &&
db != client->query.authdb)
goto refuse;
check_acl = ISC_TRUE;
queryacl = NULL;
dbversion = query_findversion(client, db, &new_zone);
if (dbversion == NULL) {
result = DNS_R_SERVFAIL;
goto fail;
}
if (new_zone) {
check_acl = ISC_TRUE;
} else if (!dbversion->queryok) {
goto refuse;
} else {
check_acl = ISC_FALSE;
}
queryacl = dns_zone_getqueryacl(zone);
if (queryacl == NULL) {
queryacl = client->view->queryacl;
if ((client->query.attributes &
NS_QUERYATTR_QUERYOKVALID) != 0) {
check_acl = ISC_FALSE;
if ((client->query.attributes &
NS_QUERYATTR_QUERYOK) == 0)
goto refuse;
} else {
check_acl = ISC_TRUE;
}
}
if (check_acl) {
isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
result = ns_client_checkaclsilent(client, queryacl, ISC_TRUE);
if (log) {
char msg[DNS_NAME_FORMATSIZE + DNS_RDATACLASS_FORMATSIZE
+ sizeof "query '/'"];
if (result == ISC_R_SUCCESS) {
if (isc_log_wouldlog(ns_g_lctx,
ISC_LOG_DEBUG(3)))
{
ns_client_aclmsg("query", name,
client->view->rdclass,
msg, sizeof(msg));
ns_client_log(client,
DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_QUERY,
ISC_LOG_DEBUG(3),
"%s approved", msg);
}
} else {
ns_client_aclmsg("query", name,
client->view->rdclass,
msg, sizeof(msg));
ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
NS_LOGMODULE_QUERY, ISC_LOG_INFO,
"%s denied", msg);
}
}
if (queryacl == client->view->queryacl) {
if (result == ISC_R_SUCCESS) {
client->query.attributes |=
NS_QUERYATTR_QUERYOK;
}
client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
}
if (result != ISC_R_SUCCESS)
goto refuse;
}
dbversion->queryok = ISC_TRUE;
*zonep = zone;
*dbp = db;
*versionp = dbversion->version;
return (ISC_R_SUCCESS);
refuse:
result = DNS_R_REFUSED;
fail:
if (zone != NULL)
dns_zone_detach(&zone);
if (db != NULL)
dns_db_detach(&db);
return (result);
}
static inline isc_result_t
query_getcachedb(ns_client_t *client, dns_db_t **dbp, unsigned int options)
{
isc_result_t result;
isc_boolean_t check_acl;
dns_db_t *db = NULL;
REQUIRE(dbp != NULL && *dbp == NULL);
if (!USECACHE(client))
return (DNS_R_REFUSED);
dns_db_attach(client->view->cachedb, &db);
if ((client->query.attributes &
NS_QUERYATTR_QUERYOKVALID) != 0) {
check_acl = ISC_FALSE;
if ((client->query.attributes &
NS_QUERYATTR_QUERYOK) == 0)
goto refuse;
} else {
check_acl = ISC_TRUE;
}
if (check_acl) {
isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
result = ns_client_checkacl(client, "query (cache)",
client->view->queryacl,
ISC_TRUE,
log ? ISC_LOG_INFO :
ISC_LOG_DEBUG(3));
if (result == ISC_R_SUCCESS) {
client->query.attributes |=
NS_QUERYATTR_QUERYOK;
}
client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
if (result != ISC_R_SUCCESS)
goto refuse;
}
*dbp = db;
return (ISC_R_SUCCESS);
refuse:
result = DNS_R_REFUSED;
if (db != NULL)
dns_db_detach(&db);
return (result);
}
static inline isc_result_t
query_getdb(ns_client_t *client, dns_name_t *name, unsigned int options,
dns_zone_t **zonep, dns_db_t **dbp, dns_dbversion_t **versionp,
isc_boolean_t *is_zonep)
{
isc_result_t result;
result = query_getzonedb(client, name, options, zonep, dbp, versionp);
if (result == ISC_R_SUCCESS) {
*is_zonep = ISC_TRUE;
} else if (result == ISC_R_NOTFOUND) {
result = query_getcachedb(client, dbp, options);
*is_zonep = ISC_FALSE;
}
return (result);
}
static isc_result_t
query_simplefind(void *arg, dns_name_t *name, dns_rdatatype_t type,
isc_stdtime_t now,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
{
ns_client_t *client = arg;
isc_result_t result;
dns_fixedname_t foundname;
dns_db_t *db;
dns_dbversion_t *version;
unsigned int dboptions;
isc_boolean_t is_zone;
dns_rdataset_t zrdataset, zsigrdataset;
dns_zone_t *zone;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(rdataset != NULL);
dns_rdataset_init(&zrdataset);
if (sigrdataset != NULL)
dns_rdataset_init(&zsigrdataset);
zone = NULL;
db = NULL;
version = NULL;
result = query_getdb(client, name, 0, &zone, &db, &version, &is_zone);
if (result != ISC_R_SUCCESS)
goto cleanup;
db_find:
dns_fixedname_init(&foundname);
dboptions = client->query.dboptions;
if (db == client->query.gluedb || (!is_zone && CACHEGLUEOK(client)))
dboptions |= DNS_DBFIND_GLUEOK;
result = dns_db_find(db, name, version, type, dboptions,
now, NULL, dns_fixedname_name(&foundname),
rdataset, sigrdataset);
if (result == DNS_R_DELEGATION ||
result == ISC_R_NOTFOUND) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
if (is_zone) {
if (USECACHE(client)) {
is_zone = ISC_FALSE;
version = NULL;
dns_db_detach(&db);
dns_db_attach(client->view->cachedb, &db);
goto db_find;
}
} else {
if (dns_rdataset_isassociated(&zrdataset)) {
dns_rdataset_clone(&zrdataset, rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(&zsigrdataset))
dns_rdataset_clone(&zsigrdataset,
sigrdataset);
result = ISC_R_SUCCESS;
goto cleanup;
}
}
result = ISC_R_NOTFOUND;
} else if (result == DNS_R_GLUE) {
if (USECACHE(client) && RECURSIONOK(client)) {
is_zone = ISC_FALSE;
version = NULL;
dns_rdataset_clone(rdataset, &zrdataset);
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
{
dns_rdataset_clone(sigrdataset, &zsigrdataset);
dns_rdataset_disassociate(sigrdataset);
}
dns_db_detach(&db);
dns_db_attach(client->view->cachedb, &db);
goto db_find;
}
result = ISC_R_SUCCESS;
} else if (result != ISC_R_SUCCESS) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
result = ISC_R_NOTFOUND;
}
cleanup:
if (dns_rdataset_isassociated(&zrdataset)) {
dns_rdataset_disassociate(&zrdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(&zsigrdataset))
dns_rdataset_disassociate(&zsigrdataset);
}
if (db != NULL)
dns_db_detach(&db);
if (zone != NULL)
dns_zone_detach(&zone);
return (result);
}
static inline isc_boolean_t
query_isduplicate(ns_client_t *client, dns_name_t *name,
dns_rdatatype_t type, dns_name_t **mnamep)
{
dns_section_t section;
dns_name_t *mname = NULL;
isc_result_t result;
CTRACE("query_isduplicate");
for (section = DNS_SECTION_ANSWER;
section <= DNS_SECTION_ADDITIONAL;
section++) {
result = dns_message_findname(client->message, section,
name, type, 0, &mname, NULL);
if (result == ISC_R_SUCCESS) {
CTRACE("query_isduplicate: true: done");
return (ISC_TRUE);
} else if (result == DNS_R_NXRRSET) {
if (section == DNS_SECTION_ADDITIONAL)
break;
} else
RUNTIME_CHECK(result == DNS_R_NXDOMAIN);
mname = NULL;
}
if (name == mname)
mname = NULL;
*mnamep = mname;
CTRACE("query_isduplicate: false: done");
return (ISC_FALSE);
}
static isc_result_t
query_addadditional(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
ns_client_t *client = arg;
isc_result_t result, eresult;
dns_dbnode_t *node;
dns_db_t *db;
dns_name_t *fname, *mname;
dns_rdataset_t *rdataset, *sigrdataset, *a6rdataset, *trdataset;
isc_buffer_t *dbuf;
isc_buffer_t b;
dns_dbversion_t *version;
isc_boolean_t added_something, need_addname;
dns_zone_t *zone;
dns_rdatatype_t type;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(qtype != dns_rdatatype_any);
if (!WANTDNSSEC(client) && dns_rdatatype_isdnssec(qtype))
return (ISC_R_SUCCESS);
CTRACE("query_addadditional");
eresult = ISC_R_SUCCESS;
fname = NULL;
rdataset = NULL;
sigrdataset = NULL;
a6rdataset = NULL;
trdataset = NULL;
db = NULL;
version = NULL;
node = NULL;
added_something = ISC_FALSE;
need_addname = ISC_FALSE;
zone = NULL;
if (qtype == dns_rdatatype_a)
type = dns_rdatatype_any;
else
type = qtype;
dbuf = query_getnamebuf(client);
if (dbuf == NULL)
goto cleanup;
fname = query_newname(client, dbuf, &b);
rdataset = query_newrdataset(client);
if (fname == NULL || rdataset == NULL)
goto cleanup;
if (WANTDNSSEC(client)) {
sigrdataset = query_newrdataset(client);
if (sigrdataset == NULL)
goto cleanup;
}
result = query_getzonedb(client, name, DNS_GETDB_NOLOG,
&zone, &db, &version);
if (result != ISC_R_SUCCESS)
goto try_cache;
CTRACE("query_addadditional: db_find");
node = NULL;
result = dns_db_find(db, name, version, type, client->query.dboptions,
client->now, &node, fname, rdataset,
sigrdataset);
if (result == ISC_R_SUCCESS)
goto found;
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
if (node != NULL)
dns_db_detachnode(db, &node);
version = NULL;
dns_db_detach(&db);
try_cache:
result = query_getcachedb(client, &db, DNS_GETDB_NOLOG);
if (result != ISC_R_SUCCESS)
goto try_glue;
result = dns_db_find(db, name, version, type, client->query.dboptions,
client->now, &node, fname, rdataset,
sigrdataset);
if (result == ISC_R_SUCCESS)
goto found;
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
if (node != NULL)
dns_db_detachnode(db, &node);
dns_db_detach(&db);
try_glue:
if (client->query.gluedb == NULL)
goto cleanup;
dns_db_attach(client->query.gluedb, &db);
result = dns_db_find(db, name, version, type,
client->query.dboptions | DNS_DBFIND_GLUEOK,
client->now, &node, fname, rdataset,
sigrdataset);
if (!(result == ISC_R_SUCCESS ||
result == DNS_R_ZONECUT ||
result == DNS_R_GLUE))
goto cleanup;
found:
query_keepname(client, fname, dbuf);
mname = NULL;
if (dns_rdataset_isassociated(rdataset) &&
!query_isduplicate(client, fname, type, &mname)) {
if (mname != NULL) {
query_releasename(client, &fname);
fname = mname;
} else
need_addname = ISC_TRUE;
ISC_LIST_APPEND(fname->list, rdataset, link);
trdataset = rdataset;
rdataset = NULL;
added_something = ISC_TRUE;
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
{
ISC_LIST_APPEND(fname->list, sigrdataset, link);
sigrdataset = NULL;
}
}
if (qtype == dns_rdatatype_a) {
if (rdataset != NULL) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
} else {
rdataset = query_newrdataset(client);
if (rdataset == NULL)
goto addname;
}
if (sigrdataset != NULL) {
if (dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
} else if (WANTDNSSEC(client)) {
sigrdataset = query_newrdataset(client);
if (sigrdataset == NULL)
goto addname;
}
result = dns_db_findrdataset(db, node, version,
dns_rdatatype_a, 0,
client->now, rdataset,
sigrdataset);
if (result == DNS_R_NCACHENXDOMAIN)
goto addname;
if (result == DNS_R_NCACHENXRRSET) {
dns_rdataset_disassociate(rdataset);
INSIST(sigrdataset == NULL ||
! dns_rdataset_isassociated(sigrdataset));
}
if (result == ISC_R_SUCCESS) {
mname = NULL;
if (!query_isduplicate(client, fname,
dns_rdatatype_a, &mname)) {
if (mname != NULL) {
query_releasename(client, &fname);
fname = mname;
} else
need_addname = ISC_TRUE;
ISC_LIST_APPEND(fname->list, rdataset, link);
added_something = ISC_TRUE;
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
{
ISC_LIST_APPEND(fname->list,
sigrdataset, link);
sigrdataset =
query_newrdataset(client);
}
rdataset = query_newrdataset(client);
if (rdataset == NULL)
goto addname;
if (WANTDNSSEC(client) && sigrdataset == NULL)
goto addname;
} else {
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
}
}
result = dns_db_findrdataset(db, node, version,
dns_rdatatype_a6, 0,
client->now, rdataset,
sigrdataset);
if (result == DNS_R_NCACHENXDOMAIN)
goto addname;
if (result == DNS_R_NCACHENXRRSET) {
dns_rdataset_disassociate(rdataset);
INSIST(sigrdataset == NULL ||
! dns_rdataset_isassociated(sigrdataset));
}
if (result == ISC_R_SUCCESS) {
mname = NULL;
if (!query_isduplicate(client, fname,
dns_rdatatype_a6, &mname)) {
if (mname != NULL) {
query_releasename(client, &fname);
fname = mname;
} else
need_addname = ISC_TRUE;
a6rdataset = rdataset;
ISC_LIST_APPEND(fname->list, rdataset, link);
added_something = ISC_TRUE;
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
{
ISC_LIST_APPEND(fname->list,
sigrdataset, link);
sigrdataset =
query_newrdataset(client);
}
rdataset = query_newrdataset(client);
if (rdataset == NULL)
goto addname;
if (WANTDNSSEC(client) && sigrdataset == NULL)
goto addname;
} else {
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
}
}
result = dns_db_findrdataset(db, node, version,
dns_rdatatype_aaaa, 0,
client->now, rdataset,
sigrdataset);
if (result == DNS_R_NCACHENXDOMAIN)
goto addname;
if (result == DNS_R_NCACHENXRRSET) {
dns_rdataset_disassociate(rdataset);
INSIST(sigrdataset == NULL ||
! dns_rdataset_isassociated(sigrdataset));
}
if (result == ISC_R_SUCCESS) {
mname = NULL;
if (!query_isduplicate(client, fname,
dns_rdatatype_aaaa, &mname)) {
if (mname != NULL) {
query_releasename(client, &fname);
fname = mname;
} else
need_addname = ISC_TRUE;
ISC_LIST_APPEND(fname->list, rdataset, link);
added_something = ISC_TRUE;
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
{
ISC_LIST_APPEND(fname->list,
sigrdataset, link);
sigrdataset = NULL;
}
rdataset = NULL;
}
}
}
addname:
CTRACE("query_addadditional: addname");
if (!added_something)
goto cleanup;
if (need_addname)
dns_message_addname(client->message, fname,
DNS_SECTION_ADDITIONAL);
fname = NULL;
if (type == dns_rdatatype_a || type == dns_rdatatype_aaaa) {
eresult = query_addadditional(client, name, dns_rdatatype_key);
} else if (type == dns_rdatatype_srv && trdataset != NULL) {
eresult = dns_rdataset_additionaldata(trdataset,
query_addadditional,
client);
}
if (a6rdataset != NULL) {
dns_a6_reset(&client->query.a6ctx);
dns_a6_foreach(&client->query.a6ctx, a6rdataset, client->now);
}
cleanup:
CTRACE("query_addadditional: cleanup");
query_putrdataset(client, &rdataset);
if (sigrdataset != NULL)
query_putrdataset(client, &sigrdataset);
if (fname != NULL)
query_releasename(client, &fname);
if (node != NULL)
dns_db_detachnode(db, &node);
if (db != NULL)
dns_db_detach(&db);
if (zone != NULL)
dns_zone_detach(&zone);
CTRACE("query_addadditional: done");
return (eresult);
}
static void
query_adda6rrset(void *arg, dns_name_t *name, dns_rdataset_t *rdataset,
dns_rdataset_t *sigrdataset)
{
ns_client_t *client = arg;
dns_rdataset_t *crdataset, *csigrdataset;
isc_buffer_t b, *dbuf;
dns_name_t *fname, *mname;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(rdataset->type == dns_rdatatype_a6);
fname = NULL;
crdataset = NULL;
csigrdataset = NULL;
dbuf = query_getnamebuf(client);
if (dbuf == NULL)
goto cleanup;
fname = query_newname(client, dbuf, &b);
crdataset = query_newrdataset(client);
if (fname == NULL || crdataset == NULL)
goto cleanup;
if (sigrdataset != NULL) {
csigrdataset = query_newrdataset(client);
if (csigrdataset == NULL)
goto cleanup;
}
if (dns_name_copy(name, fname, NULL) != ISC_R_SUCCESS)
goto cleanup;
dns_rdataset_clone(rdataset, crdataset);
if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset))
dns_rdataset_clone(sigrdataset, csigrdataset);
mname = NULL;
if (query_isduplicate(client, fname, crdataset->type, &mname))
goto cleanup;
if (mname != NULL) {
query_releasename(client, &fname);
fname = mname;
} else {
query_keepname(client, fname, dbuf);
dns_message_addname(client->message, fname,
DNS_SECTION_ADDITIONAL);
}
ISC_LIST_APPEND(fname->list, crdataset, link);
crdataset = NULL;
if (sigrdataset != NULL && dns_rdataset_isassociated(csigrdataset)) {
ISC_LIST_APPEND(fname->list, csigrdataset, link);
csigrdataset = NULL;
}
fname = NULL;
cleanup:
query_putrdataset(client, &crdataset);
if (sigrdataset != NULL)
query_putrdataset(client, &csigrdataset);
if (fname != NULL)
query_releasename(client, &fname);
}
static inline void
query_addrdataset(ns_client_t *client, dns_name_t *fname,
dns_rdataset_t *rdataset)
{
dns_rdatatype_t type = rdataset->type;
CTRACE("query_addrdataset");
ISC_LIST_APPEND(fname->list, rdataset, link);
if (NOADDITIONAL(client))
return;
if (type == dns_rdatatype_a6) {
dns_a6_reset(&client->query.a6ctx);
(void)dns_a6_foreach(&client->query.a6ctx, rdataset,
client->now);
} else
(void)dns_rdataset_additionaldata(rdataset,
query_addadditional, client);
if (type == dns_rdatatype_ns || type == dns_rdatatype_soa ||
type == dns_rdatatype_a || type == dns_rdatatype_aaaa ||
type == dns_rdatatype_a6) {
(void)query_addadditional(client, fname, dns_rdatatype_key);
}
CTRACE("query_addrdataset: done");
}
static void
query_addrrset(ns_client_t *client, dns_name_t **namep,
dns_rdataset_t **rdatasetp, dns_rdataset_t **sigrdatasetp,
isc_buffer_t *dbuf, dns_section_t section)
{
dns_name_t *name, *mname;
dns_rdataset_t *rdataset, *mrdataset, *sigrdataset;
isc_result_t result;
CTRACE("query_addrrset");
name = *namep;
rdataset = *rdatasetp;
if (sigrdatasetp != NULL)
sigrdataset = *sigrdatasetp;
else
sigrdataset = NULL;
mname = NULL;
mrdataset = NULL;
result = dns_message_findname(client->message, section,
name, rdataset->type, rdataset->covers,
&mname, &mrdataset);
if (result == ISC_R_SUCCESS) {
CTRACE("query_addrrset: dns_message_findname succeeded: done");
if (dbuf != NULL)
query_releasename(client, namep);
return;
} else if (result == DNS_R_NXDOMAIN) {
if (dbuf != NULL)
query_keepname(client, name, dbuf);
dns_message_addname(client->message, name, section);
*namep = NULL;
mname = name;
} else {
RUNTIME_CHECK(result == DNS_R_NXRRSET);
if (dbuf != NULL)
query_releasename(client, namep);
}
query_addrdataset(client, mname, rdataset);
*rdatasetp = NULL;
if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset)) {
ISC_LIST_APPEND(mname->list, sigrdataset, link);
*sigrdatasetp = NULL;
}
CTRACE("query_addrrset: done");
}
static inline isc_result_t
query_addsoa(ns_client_t *client, dns_db_t *db, isc_boolean_t zero_ttl) {
dns_name_t *name, *fname;
dns_dbnode_t *node;
isc_result_t result, eresult;
dns_fixedname_t foundname;
dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
dns_rdataset_t **sigrdatasetp = NULL;
CTRACE("query_addsoa");
eresult = ISC_R_SUCCESS;
name = NULL;
rdataset = NULL;
node = NULL;
dns_fixedname_init(&foundname);
fname = dns_fixedname_name(&foundname);
result = dns_message_gettempname(client->message, &name);
if (result != ISC_R_SUCCESS)
return (result);
dns_name_init(name, NULL);
dns_name_clone(dns_db_origin(db), name);
rdataset = query_newrdataset(client);
if (rdataset == NULL) {
eresult = DNS_R_SERVFAIL;
goto cleanup;
}
if (WANTDNSSEC(client)) {
sigrdataset = query_newrdataset(client);
if (sigrdataset == NULL) {
eresult = DNS_R_SERVFAIL;
goto cleanup;
}
}
result = dns_db_find(db, name, NULL, dns_rdatatype_soa,
client->query.dboptions, 0, &node,
fname, rdataset, sigrdataset);
if (result != ISC_R_SUCCESS) {
eresult = DNS_R_SERVFAIL;
} else {
dns_rdata_soa_t soa;
dns_rdata_t rdata = DNS_RDATA_INIT;
result = dns_rdataset_first(rdataset);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
dns_rdataset_current(rdataset, &rdata);
dns_rdata_tostruct(&rdata, &soa, NULL);
if (zero_ttl) {
rdataset->ttl = 0;
if (sigrdataset != NULL)
sigrdataset->ttl = 0;
}
if (rdataset->ttl > soa.minimum)
rdataset->ttl = soa.minimum;
if (sigrdataset != NULL && sigrdataset->ttl > soa.minimum)
sigrdataset->ttl = soa.minimum;
if (sigrdataset != NULL)
sigrdatasetp = &sigrdataset;
else
sigrdatasetp = NULL;
query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
DNS_SECTION_AUTHORITY);
}
cleanup:
query_putrdataset(client, &rdataset);
if (sigrdataset != NULL)
query_putrdataset(client, &sigrdataset);
if (name != NULL)
query_releasename(client, &name);
if (node != NULL)
dns_db_detachnode(db, &node);
return (eresult);
}
static inline isc_result_t
query_addns(ns_client_t *client, dns_db_t *db) {
dns_name_t *name, *fname;
dns_dbnode_t *node;
isc_result_t result, eresult;
dns_fixedname_t foundname;
dns_rdataset_t *rdataset = NULL, *sigrdataset = NULL;
dns_rdataset_t **sigrdatasetp = NULL;
CTRACE("query_addns");
eresult = ISC_R_SUCCESS;
name = NULL;
rdataset = NULL;
node = NULL;
dns_fixedname_init(&foundname);
fname = dns_fixedname_name(&foundname);
result = dns_message_gettempname(client->message, &name);
if (result != ISC_R_SUCCESS) {
CTRACE("query_addns: dns_message_gettempname failed: done");
return (result);
}
dns_name_init(name, NULL);
dns_name_clone(dns_db_origin(db), name);
rdataset = query_newrdataset(client);
if (rdataset == NULL) {
CTRACE("query_addns: query_newrdataset failed");
eresult = DNS_R_SERVFAIL;
goto cleanup;
}
if (WANTDNSSEC(client)) {
sigrdataset = query_newrdataset(client);
if (sigrdataset == NULL) {
CTRACE("query_addns: query_newrdataset failed");
eresult = DNS_R_SERVFAIL;
goto cleanup;
}
}
CTRACE("query_addns: calling dns_db_find");
result = dns_db_find(db, name, NULL, dns_rdatatype_ns,
client->query.dboptions, 0, &node,
fname, rdataset, sigrdataset);
CTRACE("query_addns: dns_db_find complete");
if (result != ISC_R_SUCCESS) {
CTRACE("query_addns: dns_db_find failed");
eresult = DNS_R_SERVFAIL;
} else {
if (sigrdataset != NULL)
sigrdatasetp = &sigrdataset;
else
sigrdatasetp = NULL;
query_addrrset(client, &name, &rdataset, sigrdatasetp, NULL,
DNS_SECTION_AUTHORITY);
}
cleanup:
CTRACE("query_addns: cleanup");
query_putrdataset(client, &rdataset);
if (sigrdataset != NULL)
query_putrdataset(client, &sigrdataset);
if (name != NULL)
query_releasename(client, &name);
if (node != NULL)
dns_db_detachnode(db, &node);
CTRACE("query_addns: done");
return (eresult);
}
static inline isc_result_t
query_addcnamelike(ns_client_t *client, dns_name_t *qname, dns_name_t *tname,
dns_ttl_t ttl, dns_name_t **anamep, dns_rdatatype_t type)
{
dns_rdataset_t *rdataset;
dns_rdatalist_t *rdatalist;
dns_rdata_t *rdata;
isc_result_t result;
isc_region_t r;
REQUIRE(anamep != NULL);
rdatalist = NULL;
result = dns_message_gettemprdatalist(client->message, &rdatalist);
if (result != ISC_R_SUCCESS)
return (result);
rdata = NULL;
result = dns_message_gettemprdata(client->message, &rdata);
if (result != ISC_R_SUCCESS)
return (result);
rdataset = NULL;
result = dns_message_gettemprdataset(client->message, &rdataset);
if (result != ISC_R_SUCCESS)
return (result);
dns_rdataset_init(rdataset);
result = dns_name_dup(qname, client->mctx, *anamep);
if (result != ISC_R_SUCCESS) {
dns_message_puttemprdataset(client->message, &rdataset);
return (result);
}
rdatalist->type = type;
rdatalist->covers = 0;
rdatalist->rdclass = client->message->rdclass;
rdatalist->ttl = ttl;
dns_name_toregion(tname, &r);
rdata->data = r.base;
rdata->length = r.length;
rdata->rdclass = client->message->rdclass;
rdata->type = type;
ISC_LIST_INIT(rdatalist->rdata);
ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
dns_rdatalist_tordataset(rdatalist, rdataset);
query_addrrset(client, anamep, &rdataset, NULL, NULL,
DNS_SECTION_ANSWER);
if (rdataset != NULL) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
dns_message_puttemprdataset(client->message, &rdataset);
}
return (ISC_R_SUCCESS);
}
static void
query_addbestns(ns_client_t *client) {
dns_db_t *db, *zdb;
dns_dbnode_t *node;
dns_name_t *fname, *zfname;
dns_rdataset_t *rdataset, *sigrdataset, *zrdataset, *zsigrdataset;
isc_boolean_t is_zone, use_zone;
isc_buffer_t *dbuf;
isc_result_t result;
dns_dbversion_t *version;
dns_zone_t *zone;
isc_buffer_t b;
CTRACE("query_addbestns");
fname = NULL;
zfname = NULL;
rdataset = NULL;
zrdataset = NULL;
sigrdataset = NULL;
zsigrdataset = NULL;
node = NULL;
db = NULL;
zdb = NULL;
version = NULL;
zone = NULL;
is_zone = ISC_FALSE;
use_zone = ISC_FALSE;
result = query_getdb(client, client->query.qname, 0, &zone, &db,
&version, &is_zone);
if (result != ISC_R_SUCCESS)
goto cleanup;
db_find:
dbuf = query_getnamebuf(client);
if (dbuf == NULL)
goto cleanup;
fname = query_newname(client, dbuf, &b);
rdataset = query_newrdataset(client);
if (fname == NULL || rdataset == NULL)
goto cleanup;
if (WANTDNSSEC(client)) {
sigrdataset = query_newrdataset(client);
if (sigrdataset == NULL)
goto cleanup;
}
if (is_zone) {
result = dns_db_find(db, client->query.qname, version,
dns_rdatatype_ns, client->query.dboptions,
client->now, &node, fname,
rdataset, sigrdataset);
if (result != DNS_R_DELEGATION)
goto cleanup;
if (USECACHE(client)) {
query_keepname(client, fname, dbuf);
zdb = db;
zfname = fname;
zrdataset = rdataset;
zsigrdataset = sigrdataset;
dns_db_detachnode(db, &node);
version = NULL;
db = NULL;
dns_db_attach(client->view->cachedb, &db);
is_zone = ISC_FALSE;
goto db_find;
}
} else {
result = dns_db_findzonecut(db, client->query.qname,
client->query.dboptions,
client->now, &node, fname,
rdataset, sigrdataset);
if (result == ISC_R_SUCCESS) {
if (zfname != NULL &&
!dns_name_issubdomain(fname, zfname)) {
use_zone = ISC_TRUE;
}
} else if (result == ISC_R_NOTFOUND && zfname != NULL) {
use_zone = ISC_TRUE;
} else
goto cleanup;
}
if (use_zone) {
query_releasename(client, &fname);
fname = zfname;
zfname = NULL;
dbuf = NULL;
query_putrdataset(client, &rdataset);
if (sigrdataset != NULL)
query_putrdataset(client, &sigrdataset);
rdataset = zrdataset;
zrdataset = NULL;
sigrdataset = zsigrdataset;
zsigrdataset = NULL;
}
if ((client->query.dboptions & DNS_DBFIND_PENDINGOK) == 0 &&
(rdataset->trust == dns_trust_pending ||
(sigrdataset != NULL && sigrdataset->trust == dns_trust_pending)))
goto cleanup;
query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
DNS_SECTION_AUTHORITY);
cleanup:
query_putrdataset(client, &rdataset);
if (sigrdataset != NULL)
query_putrdataset(client, &sigrdataset);
if (fname != NULL)
query_releasename(client, &fname);
if (node != NULL)
dns_db_detachnode(db, &node);
if (db != NULL)
dns_db_detach(&db);
if (zone != NULL)
dns_zone_detach(&zone);
if (zdb != NULL) {
query_putrdataset(client, &zrdataset);
if (zsigrdataset != NULL)
query_putrdataset(client, &zsigrdataset);
if (zfname != NULL)
query_releasename(client, &zfname);
dns_db_detach(&zdb);
}
}
static void
query_resume(isc_task_t *task, isc_event_t *event) {
dns_fetchevent_t *devent = (dns_fetchevent_t *)event;
ns_client_t *client;
isc_boolean_t fetch_cancelled, client_shuttingdown;
UNUSED(task);
REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
client = devent->ev_arg;
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(task == client->task);
REQUIRE(RECURSING(client));
if (devent->fetch != NULL) {
INSIST(devent->fetch == client->query.fetch);
client->query.fetch = NULL;
fetch_cancelled = ISC_FALSE;
isc_stdtime_get(&client->now);
} else {
fetch_cancelled = ISC_TRUE;
}
INSIST(client->query.fetch == NULL);
client->query.attributes &= ~NS_QUERYATTR_RECURSING;
dns_resolver_destroyfetch(&devent->fetch);
client_shuttingdown = ns_client_shuttingdown(client);
if (fetch_cancelled || client_shuttingdown) {
if (devent->node != NULL)
dns_db_detachnode(devent->db, &devent->node);
if (devent->db != NULL)
dns_db_detach(&devent->db);
query_putrdataset(client, &devent->rdataset);
if (devent->sigrdataset != NULL)
query_putrdataset(client, &devent->sigrdataset);
isc_event_free(&event);
query_next(client, ISC_R_CANCELED);
ns_client_detach(&client);
} else {
query_find(client, devent, 0);
}
}
static isc_result_t
query_recurse(ns_client_t *client, dns_rdatatype_t qtype, dns_name_t *qdomain,
dns_rdataset_t *nameservers)
{
isc_result_t result;
dns_rdataset_t *rdataset, *sigrdataset;
inc_stats(client, dns_statscounter_recursion);
if (client->recursionquota == NULL) {
result = isc_quota_attach(&ns_g_server->recursionquota,
&client->recursionquota);
if (result == ISC_R_SUCCESS && !client->mortal &&
(client->attributes & NS_CLIENTATTR_TCP) == 0)
result = ns_client_replace(client);
if (result != ISC_R_SUCCESS) {
ns_client_log(client, NS_LOGCATEGORY_CLIENT,
NS_LOGMODULE_QUERY, ISC_LOG_WARNING,
"no more recursive clients: %s",
isc_result_totext(result));
return (result);
}
}
REQUIRE(nameservers == NULL || nameservers->type == dns_rdatatype_ns);
REQUIRE(client->query.fetch == NULL);
rdataset = query_newrdataset(client);
if (rdataset == NULL)
return (ISC_R_NOMEMORY);
if (WANTDNSSEC(client)) {
sigrdataset = query_newrdataset(client);
if (sigrdataset == NULL) {
query_putrdataset(client, &rdataset);
return (ISC_R_NOMEMORY);
}
} else
sigrdataset = NULL;
if (client->query.timerset == ISC_FALSE)
ns_client_settimeout(client, 60);
result = dns_resolver_createfetch(client->view->resolver,
client->query.qname,
qtype, qdomain, nameservers,
NULL, client->query.fetchoptions,
client->task,
query_resume, client,
rdataset, sigrdataset,
&client->query.fetch);
if (result == ISC_R_SUCCESS) {
} else {
query_putrdataset(client, &rdataset);
if (sigrdataset != NULL)
query_putrdataset(client, &sigrdataset);
}
return (result);
}
static inline isc_result_t
query_findparentkey(ns_client_t *client, dns_name_t *name,
dns_zone_t **zonep, dns_db_t **dbp,
dns_dbversion_t **versionp, dns_dbnode_t **nodep,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
{
dns_db_t *pdb;
dns_dbnode_t *pnode;
dns_dbversion_t *pversion;
dns_rdataset_t prdataset, psigrdataset;
dns_rdataset_t *psigrdatasetp;
isc_result_t result;
dns_zone_t *pzone;
isc_boolean_t is_zone;
dns_fixedname_t pfoundname;
pzone = NULL;
pdb = NULL;
pnode = NULL;
pversion = NULL;
dns_rdataset_init(&prdataset);
if (sigrdataset != NULL)
dns_rdataset_init(&psigrdataset);
is_zone = ISC_FALSE;
dns_fixedname_init(&pfoundname);
result = query_getdb(client, name, DNS_GETDB_NOEXACT,
&pzone, &pdb, &pversion, &is_zone);
if (result != ISC_R_SUCCESS)
goto cleanup;
if (!is_zone) {
result = ISC_R_FAILURE;
goto cleanup;
}
if (sigrdataset != NULL)
psigrdatasetp = &psigrdataset;
else
psigrdatasetp = NULL;
result = dns_db_find(pdb, name, pversion, dns_rdatatype_key,
client->query.dboptions,
client->now, &pnode,
dns_fixedname_name(&pfoundname),
&prdataset, psigrdatasetp);
if (result == ISC_R_SUCCESS) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
dns_rdataset_clone(&prdataset, rdataset);
if (sigrdataset != NULL) {
if (dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
if (dns_rdataset_isassociated(&psigrdataset))
dns_rdataset_clone(&psigrdataset, sigrdataset);
}
if (*nodep != NULL)
dns_db_detachnode(*dbp, nodep);
*nodep = pnode;
pnode = NULL;
*versionp = pversion;
if (*dbp != NULL)
dns_db_detach(dbp);
*dbp = pdb;
pdb = NULL;
if (*zonep != NULL)
dns_zone_detach(zonep);
*zonep = pzone;
pzone = NULL;
}
cleanup:
if (dns_rdataset_isassociated(&prdataset))
dns_rdataset_disassociate(&prdataset);
if (sigrdataset != NULL && dns_rdataset_isassociated(&psigrdataset))
dns_rdataset_disassociate(&psigrdataset);
if (pnode != NULL)
dns_db_detachnode(pdb, &pnode);
if (pdb != NULL)
dns_db_detach(&pdb);
if (pzone != NULL)
dns_zone_detach(&pzone);
return (result);
}
#define MAX_RESTARTS 16
#define QUERY_ERROR(r) \
do { \
eresult = r; \
want_restart = ISC_FALSE; \
} while (0)
static isc_result_t
rdata_tonetaddr(dns_rdata_t *rdata, isc_netaddr_t *netaddr) {
struct in_addr ina;
struct in6_addr in6a;
switch (rdata->type) {
case dns_rdatatype_a:
INSIST(rdata->length == 4);
memcpy(&ina.s_addr, rdata->data, 4);
isc_netaddr_fromin(netaddr, &ina);
return (ISC_R_SUCCESS);
case dns_rdatatype_aaaa:
INSIST(rdata->length == 16);
memcpy(in6a.s6_addr, rdata->data, 16);
isc_netaddr_fromin6(netaddr, &in6a);
return (ISC_R_SUCCESS);
default:
return (ISC_R_NOTIMPLEMENTED);
}
}
static int
query_sortlist_order_2element(dns_rdata_t *rdata, void *arg) {
isc_netaddr_t netaddr;
if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
return (INT_MAX);
return (ns_sortlist_addrorder2(&netaddr, arg));
}
static int
query_sortlist_order_1element(dns_rdata_t *rdata, void *arg) {
isc_netaddr_t netaddr;
if (rdata_tonetaddr(rdata, &netaddr) != ISC_R_SUCCESS)
return (INT_MAX);
return (ns_sortlist_addrorder1(&netaddr, arg));
}
static void
setup_query_sortlist(ns_client_t *client) {
isc_netaddr_t netaddr;
dns_rdatasetorderfunc_t order = NULL;
void *order_arg = NULL;
isc_netaddr_fromsockaddr(&netaddr, &client->peeraddr);
switch (ns_sortlist_setup(client->view->sortlist,
&netaddr, &order_arg)) {
case NS_SORTLISTTYPE_1ELEMENT:
order = query_sortlist_order_1element;
break;
case NS_SORTLISTTYPE_2ELEMENT:
order = query_sortlist_order_2element;
break;
case NS_SORTLISTTYPE_NONE:
order = NULL;
break;
default:
INSIST(0);
break;
}
dns_message_setsortorder(client->message, order, order_arg);
}
static void
query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype) {
dns_db_t *db, *zdb;
dns_dbnode_t *node;
dns_rdatatype_t type;
dns_name_t *fname, *zfname, *tname, *prefix;
dns_rdataset_t *rdataset, *trdataset;
dns_rdataset_t *sigrdataset, *zrdataset, *zsigrdataset;
dns_rdataset_t **sigrdatasetp;
dns_rdata_t rdata = DNS_RDATA_INIT;
dns_rdatasetiter_t *rdsiter;
isc_boolean_t want_restart, authoritative, is_zone;
unsigned int n, nlabels, nbits;
dns_namereln_t namereln;
int order;
isc_buffer_t *dbuf;
isc_buffer_t b;
isc_result_t result, eresult;
dns_fixedname_t fixed;
dns_dbversion_t *version;
dns_zone_t *zone;
dns_rdata_cname_t cname;
dns_rdata_dname_t dname;
CTRACE("query_find");
eresult = ISC_R_SUCCESS;
fname = NULL;
zfname = NULL;
rdataset = NULL;
zrdataset = NULL;
sigrdataset = NULL;
zsigrdataset = NULL;
node = NULL;
db = NULL;
zdb = NULL;
version = NULL;
zone = NULL;
if (event != NULL) {
want_restart = ISC_FALSE;
authoritative = ISC_FALSE;
is_zone = ISC_FALSE;
qtype = event->qtype;
if (qtype == dns_rdatatype_sig)
type = dns_rdatatype_any;
else
type = qtype;
db = event->db;
node = event->node;
rdataset = event->rdataset;
sigrdataset = event->sigrdataset;
dbuf = query_getnamebuf(client);
if (dbuf == NULL) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
fname = query_newname(client, dbuf, &b);
if (fname == NULL) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
tname = dns_fixedname_name(&event->foundname);
result = dns_name_copy(tname, fname, NULL);
if (result != ISC_R_SUCCESS) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
result = event->result;
goto resume;
}
if (qtype == dns_rdatatype_sig)
type = dns_rdatatype_any;
else
type = qtype;
restart:
CTRACE("query_find: restart");
want_restart = ISC_FALSE;
authoritative = ISC_FALSE;
version = NULL;
result = query_getdb(client, client->query.qname, 0, &zone, &db,
&version, &is_zone);
if (result != ISC_R_SUCCESS) {
if (result == DNS_R_REFUSED)
QUERY_ERROR(DNS_R_REFUSED);
else
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
if (is_zone)
authoritative = ISC_TRUE;
if (event == NULL && client->query.restarts == 0) {
if (is_zone) {
dns_zone_attach(zone, &client->query.authzone);
dns_db_attach(db, &client->query.authdb);
}
client->query.authdbset = ISC_TRUE;
}
db_find:
CTRACE("query_find: db_find");
dbuf = query_getnamebuf(client);
if (dbuf == NULL) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
fname = query_newname(client, dbuf, &b);
rdataset = query_newrdataset(client);
if (fname == NULL || rdataset == NULL) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
if (WANTDNSSEC(client)) {
sigrdataset = query_newrdataset(client);
if (sigrdataset == NULL) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
}
result = dns_db_find(db, client->query.qname, version, type,
client->query.dboptions, client->now,
&node, fname, rdataset, sigrdataset);
if (type == dns_rdatatype_key &&
is_zone &&
result == DNS_R_NXRRSET &&
!dns_db_issecure(db) &&
dns_name_equal(client->query.qname, dns_db_origin(db))) {
result = query_findparentkey(client, client->query.qname,
&zone, &db, &version, &node,
rdataset, sigrdataset);
if (result == ISC_R_SUCCESS) {
result = dns_name_copy(client->query.qname,
fname, NULL);
if (result != ISC_R_SUCCESS) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
} else {
result = DNS_R_NXRRSET;
}
}
resume:
CTRACE("query_find: resume");
switch (result) {
case ISC_R_SUCCESS:
break;
case DNS_R_GLUE:
case DNS_R_ZONECUT:
INSIST(is_zone);
authoritative = ISC_FALSE;
break;
case ISC_R_NOTFOUND:
INSIST(!is_zone);
if (db != NULL)
dns_db_detach(&db);
if (client->view->hints == NULL) {
result = ISC_R_FAILURE;
} else {
dns_db_attach(client->view->hints, &db);
result = dns_db_find(db, dns_rootname,
NULL, dns_rdatatype_ns,
0, client->now, &node, fname,
rdataset, sigrdataset);
}
if (result != ISC_R_SUCCESS) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
if (node != NULL)
dns_db_detachnode(db, &node);
if (RECURSIONOK(client)) {
result = query_recurse(client, qtype,
NULL, NULL);
if (result == ISC_R_SUCCESS)
client->query.attributes |=
NS_QUERYATTR_RECURSING;
else {
QUERY_ERROR(DNS_R_SERVFAIL);
}
goto cleanup;
} else {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
}
case DNS_R_DELEGATION:
authoritative = ISC_FALSE;
if (is_zone) {
if (!USECACHE(client) || !RECURSIONOK(client)) {
client->query.gluedb = db;
client->query.isreferral = ISC_TRUE;
client->query.attributes &=
~NS_QUERYATTR_NOADDITIONAL;
if (sigrdataset != NULL)
sigrdatasetp = &sigrdataset;
else
sigrdatasetp = NULL;
query_addrrset(client, &fname,
&rdataset, sigrdatasetp,
dbuf, DNS_SECTION_AUTHORITY);
client->query.gluedb = NULL;
} else {
query_keepname(client, fname, dbuf);
zdb = db;
zfname = fname;
zrdataset = rdataset;
zsigrdataset = sigrdataset;
dns_db_detachnode(db, &node);
version = NULL;
db = NULL;
dns_db_attach(client->view->cachedb, &db);
is_zone = ISC_FALSE;
goto db_find;
}
} else {
if (zfname != NULL &&
!dns_name_issubdomain(fname, zfname)) {
query_releasename(client, &fname);
fname = zfname;
zfname = NULL;
dbuf = NULL;
query_putrdataset(client, &rdataset);
if (sigrdataset != NULL)
query_putrdataset(client,
&sigrdataset);
rdataset = zrdataset;
zrdataset = NULL;
sigrdataset = zsigrdataset;
zsigrdataset = NULL;
}
if (RECURSIONOK(client)) {
if (type == dns_rdatatype_key)
result = query_recurse(client, qtype,
NULL, NULL);
else
result = query_recurse(client, qtype,
fname, rdataset);
if (result == ISC_R_SUCCESS)
client->query.attributes |=
NS_QUERYATTR_RECURSING;
else
QUERY_ERROR(DNS_R_SERVFAIL);
} else {
client->query.attributes |=
NS_QUERYATTR_CACHEGLUEOK;
client->query.gluedb = zdb;
client->query.isreferral = ISC_TRUE;
client->query.attributes &=
~NS_QUERYATTR_NOADDITIONAL;
if (sigrdataset != NULL)
sigrdatasetp = &sigrdataset;
else
sigrdatasetp = NULL;
query_addrrset(client, &fname,
&rdataset, sigrdatasetp,
dbuf, DNS_SECTION_AUTHORITY);
client->query.gluedb = NULL;
client->query.attributes &=
~NS_QUERYATTR_CACHEGLUEOK;
}
}
goto cleanup;
case DNS_R_NXRRSET:
INSIST(is_zone);
if (dns_rdataset_isassociated(rdataset)) {
query_keepname(client, fname, dbuf);
} else {
query_releasename(client, &fname);
}
result = query_addsoa(client, db, ISC_FALSE);
if (result != ISC_R_SUCCESS) {
QUERY_ERROR(result);
goto cleanup;
}
if (dns_rdataset_isassociated(rdataset)) {
if (WANTDNSSEC(client))
query_addrrset(client, &fname, &rdataset,
&sigrdataset,
NULL, DNS_SECTION_AUTHORITY);
}
goto cleanup;
case DNS_R_NXDOMAIN:
INSIST(is_zone);
if (dns_rdataset_isassociated(rdataset)) {
query_keepname(client, fname, dbuf);
} else {
query_releasename(client, &fname);
}
if (qtype == dns_rdatatype_soa)
result = query_addsoa(client, db, ISC_TRUE);
else
result = query_addsoa(client, db, ISC_FALSE);
if (result != ISC_R_SUCCESS) {
QUERY_ERROR(result);
goto cleanup;
}
if (dns_rdataset_isassociated(rdataset)) {
if (WANTDNSSEC(client))
query_addrrset(client, &fname, &rdataset,
&sigrdataset,
NULL, DNS_SECTION_AUTHORITY);
}
client->message->rcode = dns_rcode_nxdomain;
goto cleanup;
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
INSIST(!is_zone);
authoritative = ISC_FALSE;
if (result == DNS_R_NCACHENXDOMAIN)
client->message->rcode = dns_rcode_nxdomain;
query_keepname(client, fname, dbuf);
dns_message_addname(client->message, fname,
DNS_SECTION_AUTHORITY);
ISC_LIST_APPEND(fname->list, rdataset, link);
fname = NULL;
rdataset = NULL;
goto cleanup;
case DNS_R_CNAME:
trdataset = rdataset;
if (sigrdataset != NULL)
sigrdatasetp = &sigrdataset;
else
sigrdatasetp = NULL;
query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
DNS_SECTION_ANSWER);
client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
tname = NULL;
result = dns_message_gettempname(client->message, &tname);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_rdataset_first(trdataset);
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(client->message, &tname);
goto cleanup;
}
dns_rdataset_current(trdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &cname, NULL);
dns_rdata_reset(&rdata);
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(client->message, &tname);
goto cleanup;
}
dns_name_init(tname, NULL);
result = dns_name_dup(&cname.cname, client->mctx, tname);
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(client->message, &tname);
dns_rdata_freestruct(&cname);
goto cleanup;
}
dns_rdata_freestruct(&cname);
query_maybeputqname(client);
client->query.qname = tname;
want_restart = ISC_TRUE;
goto addauth;
case DNS_R_DNAME:
namereln = dns_name_fullcompare(client->query.qname, fname,
&order, &nlabels, &nbits);
INSIST(namereln == dns_namereln_subdomain);
trdataset = rdataset;
if (sigrdataset != NULL)
sigrdatasetp = &sigrdataset;
else
sigrdatasetp = NULL;
query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
DNS_SECTION_ANSWER);
client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
tname = NULL;
result = dns_message_gettempname(client->message, &tname);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_rdataset_first(trdataset);
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(client->message, &tname);
goto cleanup;
}
dns_rdataset_current(trdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &dname, NULL);
dns_rdata_reset(&rdata);
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(client->message, &tname);
goto cleanup;
}
dns_name_init(tname, NULL);
dns_name_clone(&dname.dname, tname);
dns_rdata_freestruct(&dname);
dns_fixedname_init(&fixed);
prefix = dns_fixedname_name(&fixed);
result = dns_name_split(client->query.qname, nlabels, nbits,
prefix, NULL);
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(client->message, &tname);
goto cleanup;
}
INSIST(fname == NULL);
dbuf = query_getnamebuf(client);
if (dbuf == NULL) {
dns_message_puttempname(client->message, &tname);
goto cleanup;
}
fname = query_newname(client, dbuf, &b);
if (fname == NULL) {
dns_message_puttempname(client->message, &tname);
goto cleanup;
}
result = dns_name_concatenate(prefix, tname, fname, NULL);
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(client->message, &tname);
if (result == ISC_R_NOSPACE) {
client->message->rcode = dns_rcode_yxdomain;
}
goto cleanup;
}
query_keepname(client, fname, dbuf);
dns_name_init(tname, NULL);
query_addcnamelike(client, client->query.qname, fname,
0, &tname, dns_rdatatype_cname);
if (tname != NULL)
dns_message_puttempname(client->message, &tname);
query_maybeputqname(client);
client->query.qname = fname;
fname = NULL;
want_restart = ISC_TRUE;
goto addauth;
default:
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
if (type == dns_rdatatype_any) {
n = 0;
rdsiter = NULL;
result = dns_db_allrdatasets(db, node, version, 0, &rdsiter);
if (result != ISC_R_SUCCESS) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
query_keepname(client, fname, dbuf);
tname = fname;
result = dns_rdatasetiter_first(rdsiter);
while (result == ISC_R_SUCCESS) {
dns_rdatasetiter_current(rdsiter, rdataset);
if ((qtype == dns_rdatatype_any ||
rdataset->type == qtype) && rdataset->type != 0) {
query_addrrset(client,
fname != NULL ? &fname : &tname,
&rdataset, NULL,
NULL, DNS_SECTION_ANSWER);
n++;
INSIST(tname != NULL);
if (rdataset != NULL)
query_putrdataset(client, &rdataset);
rdataset = query_newrdataset(client);
if (rdataset == NULL)
break;
} else {
dns_rdataset_disassociate(rdataset);
}
result = dns_rdatasetiter_next(rdsiter);
}
if (fname != NULL)
dns_message_puttempname(client->message, &fname);
if (n == 0) {
if (qtype == dns_rdatatype_sig &&
result == ISC_R_NOMORE) {
result = query_addsoa(client, db, ISC_FALSE);
if (result == ISC_R_SUCCESS)
result = ISC_R_NOMORE;
} else {
result = DNS_R_SERVFAIL;
}
}
dns_rdatasetiter_destroy(&rdsiter);
if (result != ISC_R_NOMORE) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
} else {
if (sigrdataset != NULL)
sigrdatasetp = &sigrdataset;
else
sigrdatasetp = NULL;
query_addrrset(client, &fname, &rdataset, sigrdatasetp, dbuf,
DNS_SECTION_ANSWER);
INSIST(rdataset == NULL);
}
addauth:
CTRACE("query_find: addauth");
if (!want_restart && !NOAUTHORITY(client)
) {
if (is_zone) {
if (!((qtype == dns_rdatatype_ns ||
qtype == dns_rdatatype_any) &&
dns_name_equal(client->query.qname,
dns_db_origin(db))))
query_addns(client, db);
} else if (qtype != dns_rdatatype_ns) {
if (fname != NULL)
query_releasename(client, &fname);
query_addbestns(client);
}
}
cleanup:
CTRACE("query_find: cleanup");
query_putrdataset(client, &rdataset);
if (sigrdataset != NULL)
query_putrdataset(client, &sigrdataset);
if (fname != NULL)
query_releasename(client, &fname);
if (node != NULL)
dns_db_detachnode(db, &node);
if (db != NULL)
dns_db_detach(&db);
if (zone != NULL)
dns_zone_detach(&zone);
if (zdb != NULL) {
query_putrdataset(client, &zrdataset);
if (zsigrdataset != NULL)
query_putrdataset(client, &zsigrdataset);
if (zfname != NULL)
query_releasename(client, &zfname);
dns_db_detach(&zdb);
}
if (event != NULL)
isc_event_free((isc_event_t **)(&event));
if (client->query.restarts == 0 && !authoritative) {
client->message->flags &= ~DNS_MESSAGEFLAG_AA;
}
if (want_restart && client->query.restarts < MAX_RESTARTS) {
client->query.restarts++;
goto restart;
}
if (eresult != ISC_R_SUCCESS &&
(!PARTIALANSWER(client) || WANTRECURSION(client))) {
query_error(client, eresult);
ns_client_detach(&client);
} else if (!RECURSING(client)) {
setup_query_sortlist(client);
if (client->message->rcode == dns_rcode_nxdomain &&
client->view->auth_nxdomain == ISC_TRUE)
client->message->flags |= DNS_MESSAGEFLAG_AA;
query_send(client);
ns_client_detach(&client);
}
CTRACE("query_find: done");
}
static inline void
log_query(ns_client_t *client) {
char namebuf[DNS_NAME_FORMATSIZE];
char typename[DNS_RDATATYPE_FORMATSIZE];
char classname[DNS_RDATACLASS_FORMATSIZE];
dns_rdataset_t *rdataset;
int level = ISC_LOG_INFO;
if (! isc_log_wouldlog(ns_g_lctx, level))
return;
rdataset = ISC_LIST_HEAD(client->query.qname->list);
INSIST(rdataset != NULL);
dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
dns_rdataclass_format(rdataset->rdclass, classname, sizeof(classname));
dns_rdatatype_format(rdataset->type, typename, sizeof(typename));
ns_client_log(client, NS_LOGCATEGORY_QUERIES, NS_LOGMODULE_QUERY,
level, "query: %s %s %s", namebuf, classname, typename);
}
void
ns_query_start(ns_client_t *client) {
isc_result_t result;
dns_message_t *message = client->message;
dns_rdataset_t *rdataset;
ns_client_t *qclient;
dns_rdatatype_t qtype;
CTRACE("ns_query_start");
client->next = query_next_callback;
if ((message->flags & DNS_MESSAGEFLAG_RD) != 0)
client->query.attributes |= NS_QUERYATTR_WANTRECURSION;
#ifdef ISC_RFC2535
if ((client->extflags & DNS_MESSAGEEXTFLAG_DO) != 0 ||
(message->flags & DNS_MESSAGEFLAG_AD) != 0)
client->query.attributes |= NS_QUERYATTR_WANTDNSSEC;
#endif
if (client->view->minimalresponses)
client->query.attributes |= (NS_QUERYATTR_NOAUTHORITY |
NS_QUERYATTR_NOADDITIONAL);
if ((client->view->cachedb == NULL)
|| (!client->view->additionalfromcache)) {
client->query.attributes &=
~(NS_QUERYATTR_RECURSIONOK|NS_QUERYATTR_CACHEOK);
} else if ((client->attributes & NS_CLIENTATTR_RA) == 0 ||
(message->flags & DNS_MESSAGEFLAG_RD) == 0) {
client->query.attributes &= ~NS_QUERYATTR_RECURSIONOK;
}
result = dns_message_firstname(message, DNS_SECTION_QUESTION);
if (result != ISC_R_SUCCESS) {
query_error(client, result);
return;
}
dns_message_currentname(message, DNS_SECTION_QUESTION,
&client->query.qname);
client->query.origqname = client->query.qname;
result = dns_message_nextname(message, DNS_SECTION_QUESTION);
if (result != ISC_R_NOMORE) {
if (result == ISC_R_SUCCESS) {
query_error(client, DNS_R_FORMERR);
} else
query_error(client, result);
return;
}
if (ns_g_server->log_queries)
log_query(client);
if (message->counts[DNS_SECTION_QUESTION] > 1) {
query_error(client, DNS_R_FORMERR);
return;
}
rdataset = ISC_LIST_HEAD(client->query.qname->list);
INSIST(rdataset != NULL);
qtype = rdataset->type;
if (dns_rdatatype_ismeta(qtype)) {
switch (qtype) {
case dns_rdatatype_any:
break;
case dns_rdatatype_ixfr:
case dns_rdatatype_axfr:
ns_xfr_start(client, rdataset->type);
return;
case dns_rdatatype_maila:
case dns_rdatatype_mailb:
query_error(client, DNS_R_NOTIMP);
return;
case dns_rdatatype_tkey:
result = dns_tkey_processquery(client->message,
ns_g_server->tkeyctx,
client->view->dynamickeys);
if (result == ISC_R_SUCCESS)
query_send(client);
else
query_error(client, result);
return;
default:
query_error(client, DNS_R_FORMERR);
return;
}
}
if (message->flags & DNS_MESSAGEFLAG_CD ||
qtype == dns_rdatatype_sig)
{
client->query.dboptions |= DNS_DBFIND_PENDINGOK;
client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
}
result = dns_message_reply(message, ISC_TRUE);
if (result != ISC_R_SUCCESS) {
query_next(client, result);
return;
}
message->flags |= DNS_MESSAGEFLAG_AA;
if (WANTDNSSEC(client))
message->flags |= DNS_MESSAGEFLAG_AD;
if (RECURSIONOK(client) &&
(qtype == dns_rdatatype_aaaa || qtype == dns_rdatatype_ptr) &&
client->message->rdclass == dns_rdataclass_in &&
ns_client_checkacl(client, "v6 synthesis",
client->view->v6synthesisacl,
ISC_FALSE, ISC_LOG_DEBUG(9)) == ISC_R_SUCCESS)
{
if (qtype == dns_rdatatype_aaaa) {
qclient = NULL;
ns_client_attach(client, &qclient);
synth_fwd_start(qclient);
return;
} else {
INSIST(qtype == dns_rdatatype_ptr);
if (dns_name_countlabels(client->query.qname) == 32 + 3 &&
dns_name_issubdomain(client->query.qname, &ip6int_name)) {
qclient = NULL;
ns_client_attach(client, &qclient);
synth_rev_start(qclient);
return;
}
}
}
qclient = NULL;
ns_client_attach(client, &qclient);
query_find(qclient, NULL, qtype);
}
static void
synth_fwd_start(ns_client_t *client) {
ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_QUERY,
ISC_LOG_DEBUG(5), "generating synthetic AAAA response");
synth_fwd_startfind(client);
}
static void
synth_fwd_startfind(ns_client_t *client) {
dns_adbfind_t *find = NULL;
isc_result_t result;
dns_fixedname_t target_fixed;
dns_name_t *target;
dns_fixedname_init(&target_fixed);
target = dns_fixedname_name(&target_fixed);
find_again:
result = dns_adb_createfind(client->view->adb, client->task,
synth_fwd_finddone, client, client->query.qname,
dns_rootname,
DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_RETURNLAME |
DNS_ADBFIND_INET6, client->now,
target, 0, &find);
ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_QUERY,
ISC_LOG_DEBUG(5), "find returned %s",
isc_result_totext(result));
if (result == DNS_R_ALIAS) {
dns_name_t *ptarget = NULL;
dns_name_t *tname = NULL;
isc_buffer_t *dbuf;
isc_buffer_t b;
dbuf = query_getnamebuf(client);
if (dbuf == NULL)
goto fail;
ptarget = query_newname(client, dbuf, &b);
if (ptarget == NULL)
goto fail;
dns_name_copy(target, ptarget, NULL);
dns_adb_destroyfind(&find);
result = dns_message_gettempname(client->message, &tname);
if (result != ISC_R_SUCCESS)
goto fail;
dns_name_init(tname, NULL);
result = query_addcnamelike(client, client->query.qname,
ptarget, 0 , &tname,
dns_rdatatype_cname);
if (tname != NULL)
dns_message_puttempname(client->message, &tname);
if (result != ISC_R_SUCCESS)
goto fail;
query_maybeputqname(client);
client->query.qname = ptarget;
query_keepname(client, ptarget, dbuf);
ptarget = NULL;
if (client->query.restarts < MAX_RESTARTS) {
client->query.restarts++;
goto find_again;
} else {
result = ISC_R_SUCCESS;
goto done;
}
} else if (result != ISC_R_SUCCESS) {
if (find != NULL)
dns_adb_destroyfind(&find);
goto fail;
}
if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_QUERY,
ISC_LOG_DEBUG(5), "find will send event");
} else {
synth_fwd_respond(client, find);
dns_adb_destroyfind(&find);
}
return;
fail:
result = DNS_R_SERVFAIL;
done:
synth_finish(client, result);
}
static void
synth_fwd_finddone(isc_task_t *task, isc_event_t *ev) {
ns_client_t *client = ev->ev_arg;
dns_adbfind_t *find = ev->ev_sender;
isc_eventtype_t evtype = ev->ev_type;
UNUSED(task);
ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_QUERY,
ISC_LOG_DEBUG(5), "got find event");
if (evtype == DNS_EVENT_ADBNOMOREADDRESSES)
synth_fwd_respond(client, find);
else if (evtype == DNS_EVENT_ADBMOREADDRESSES)
synth_fwd_startfind(client);
else
synth_finish(client, DNS_R_SERVFAIL);
isc_event_free(&ev);
dns_adb_destroyfind(&find);
}
static void
synth_fwd_respond(ns_client_t *client, dns_adbfind_t *find) {
dns_adbaddrinfo_t *ai;
dns_name_t *tname = NULL;
dns_rdataset_t *rdataset = NULL;
dns_rdatalist_t *rdatalist = NULL;
isc_result_t result;
result = dns_message_gettempname(client->message, &tname);
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_name_init(tname, NULL);
result = dns_message_gettemprdatalist(client->message, &rdatalist);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = dns_message_gettemprdataset(client->message, &rdataset);
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_rdataset_init(rdataset);
ISC_LIST_INIT(rdatalist->rdata);
rdatalist->type = dns_rdatatype_aaaa;
rdatalist->covers = 0;
rdatalist->rdclass = client->message->rdclass;
rdatalist->ttl = 0;
dns_name_clone(client->query.qname, tname);
for (ai = ISC_LIST_HEAD(find->list);
ai != NULL;
ai = ISC_LIST_NEXT(ai, publink)) {
dns_rdata_t *rdata = NULL;
struct sockaddr_in6 *sin6 = &ai->sockaddr.type.sin6;
if (sin6->sin6_family != AF_INET6)
continue;
result = dns_message_gettemprdata(client->message, &rdata);
if (result != ISC_R_SUCCESS)
goto cleanup;
rdata->data = (unsigned char *) &sin6->sin6_addr;
rdata->length = 16;
rdata->rdclass = client->message->rdclass;
rdata->type = dns_rdatatype_aaaa;
ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
}
dns_rdatalist_tordataset(rdatalist, rdataset);
query_addrrset(client, &tname, &rdataset, NULL, NULL,
DNS_SECTION_ANSWER);
cleanup:
if (tname != NULL)
dns_message_puttempname(client->message, &tname);
if (rdataset != NULL) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
dns_message_puttemprdataset(client->message, &rdataset);
}
synth_finish(client, result);
}
static void
synth_finish(ns_client_t *client, isc_result_t result) {
if (result == ISC_R_SUCCESS)
query_send(client);
else
query_error(client, result);
ns_client_detach(&client);
}
static signed char ascii2hex[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
static isc_result_t
label2hex(dns_name_t *name, int i, int *hexp) {
isc_region_t label;
int hexval;
dns_name_getlabel(name, i, &label);
if (label.length != 2 || label.base[0] != '\001')
return (ISC_R_FAILURE);
hexval = ascii2hex[label.base[1]];
if (hexval == -1)
return (ISC_R_FAILURE);
*hexp = hexval;
return (ISC_R_SUCCESS);
}
static isc_result_t
nibbles2netaddr(dns_name_t *name, isc_netaddr_t *na) {
isc_result_t result;
struct in6_addr ina6;
unsigned char *addrdata = (unsigned char *) &ina6;
int i;
for (i = 0; i < 16; i++) {
int hex0, hex1;
result = label2hex(name, 2 * i, &hex0);
if (result != ISC_R_SUCCESS)
return (result);
result = label2hex(name, 2 * i + 1, &hex1);
if (result != ISC_R_SUCCESS)
return (result);
addrdata[15-i] = (hex1 << 4) | hex0;
}
isc_netaddr_fromin6(na, &ina6);
return (ISC_R_SUCCESS);
}
static void
synth_rev_start(ns_client_t *client) {
isc_result_t result;
dns_byaddr_t *byaddr_dummy = NULL;
ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_QUERY,
ISC_LOG_DEBUG(5), "generating synthetic PTR response");
result = nibbles2netaddr(client->query.qname, &client->query.synth.na);
if (result != ISC_R_SUCCESS) {
result = DNS_R_NXDOMAIN;
goto cleanup;
}
result = dns_byaddr_create(client->mctx,
&client->query.synth.na,
client->view,
0, client->task,
synth_rev_byaddrdone_arpa,
client, &byaddr_dummy);
if (result == ISC_R_SUCCESS)
return;
cleanup:
synth_finish(client, result);
}
static void
synth_rev_byaddrdone_arpa(isc_task_t *task, isc_event_t *event) {
isc_result_t result;
dns_byaddrevent_t *bevent = (dns_byaddrevent_t *)event;
ns_client_t *client = event->ev_arg;
dns_byaddr_t *byaddr = event->ev_sender;
dns_byaddr_t *byaddr_dummy = NULL;
UNUSED(task);
if (bevent->result == ISC_R_SUCCESS) {
synth_rev_respond(client, bevent);
} else {
result = dns_byaddr_create(client->mctx,
&client->query.synth.na,
client->view,
DNS_BYADDROPT_IPV6NIBBLE,
client->task,
synth_rev_byaddrdone_int,
client, &byaddr_dummy);
if (result != ISC_R_SUCCESS)
synth_finish(client, result);
}
dns_byaddr_destroy(&byaddr);
isc_event_free(&event);
}
static void
synth_rev_byaddrdone_int(isc_task_t *task, isc_event_t *event) {
dns_byaddrevent_t *bevent = (dns_byaddrevent_t *)event;
ns_client_t *client = event->ev_arg;
dns_byaddr_t *byaddr = event->ev_sender;
UNUSED(task);
if (bevent->result == ISC_R_SUCCESS) {
synth_rev_respond(client, bevent);
} else if (bevent->result == DNS_R_NCACHENXDOMAIN ||
bevent->result == DNS_R_NCACHENXRRSET ||
bevent->result == DNS_R_NXDOMAIN ||
bevent->result == DNS_R_NXRRSET) {
synth_finish(client, DNS_R_NXDOMAIN);
} else {
synth_finish(client, bevent->result);
}
isc_event_free(&event);
dns_byaddr_destroy(&byaddr);
}
static void
synth_rev_respond(ns_client_t *client, dns_byaddrevent_t *bevent) {
isc_result_t result = ISC_R_SUCCESS;
dns_name_t *name;
for (name = ISC_LIST_HEAD(bevent->names);
name != NULL;
name = ISC_LIST_NEXT(name, link))
{
dns_name_t *tname = NULL;
result = dns_message_gettempname(client->message, &tname);
if (result != ISC_R_SUCCESS)
goto fail;
dns_name_init(tname, NULL);
result = query_addcnamelike(client, client->query.qname,
name, 0 ,
&tname, dns_rdatatype_ptr);
if (tname != NULL)
dns_message_puttempname(client->message, &tname);
if (result != ISC_R_SUCCESS)
goto fail;
}
fail:
synth_finish(client, result);
}