query.c revision d1cbf714097e900ed1703529584d3e1a50e8a4a8
615eff12e0e3565c52758292e10080a25d872941William King/*
ca41b452ede6feaa9d8739ec3cae19389a7b0d03Bob Halley * Copyright (C) 1999, 2000 Internet Software Consortium.
615eff12e0e3565c52758292e10080a25d872941William King *
615eff12e0e3565c52758292e10080a25d872941William King * Permission to use, copy, modify, and distribute this software for any
615eff12e0e3565c52758292e10080a25d872941William King * purpose with or without fee is hereby granted, provided that the above
615eff12e0e3565c52758292e10080a25d872941William King * copyright notice and this permission notice appear in all copies.
615eff12e0e3565c52758292e10080a25d872941William King *
615eff12e0e3565c52758292e10080a25d872941William King * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
615eff12e0e3565c52758292e10080a25d872941William King * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
615eff12e0e3565c52758292e10080a25d872941William King * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
615eff12e0e3565c52758292e10080a25d872941William King * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
615eff12e0e3565c52758292e10080a25d872941William King * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
615eff12e0e3565c52758292e10080a25d872941William King * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
615eff12e0e3565c52758292e10080a25d872941William King * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
615eff12e0e3565c52758292e10080a25d872941William King * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
615eff12e0e3565c52758292e10080a25d872941William King */
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King/* $Id: query.c,v 1.134 2000/10/07 00:09:16 bwelling Exp $ */
615eff12e0e3565c52758292e10080a25d872941William King
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <config.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/mem.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <isc/util.h>
615eff12e0e3565c52758292e10080a25d872941William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King#include <dns/db.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <dns/events.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <dns/message.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <dns/rdata.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <dns/rdatalist.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <dns/rdataset.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/rdatasetiter.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <dns/rdatastruct.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/rdatatype.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/resolver.h>
fd9f6afdff9faf55411dfd5f828529601cab0511Andreas Gustafsson#include <dns/result.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <dns/tkey.h>
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington#include <dns/view.h>
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington#include <dns/zone.h>
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington#include <dns/zt.h>
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King#include <named/client.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <named/log.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <named/server.h>
615eff12e0e3565c52758292e10080a25d872941William King#include <named/xfrout.h>
615eff12e0e3565c52758292e10080a25d872941William King
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#define PARTIALANSWER(c) (((c)->query.attributes & \
615eff12e0e3565c52758292e10080a25d872941William King NS_QUERYATTR_PARTIALANSWER) != 0)
615eff12e0e3565c52758292e10080a25d872941William King#define USECACHE(c) (((c)->query.attributes & \
615eff12e0e3565c52758292e10080a25d872941William King NS_QUERYATTR_CACHEOK) != 0)
615eff12e0e3565c52758292e10080a25d872941William King#define RECURSIONOK(c) (((c)->query.attributes & \
615eff12e0e3565c52758292e10080a25d872941William King NS_QUERYATTR_RECURSIONOK) != 0)
615eff12e0e3565c52758292e10080a25d872941William King#define RECURSING(c) (((c)->query.attributes & \
615eff12e0e3565c52758292e10080a25d872941William King NS_QUERYATTR_RECURSING) != 0)
615eff12e0e3565c52758292e10080a25d872941William King#define CACHEGLUEOK(c) (((c)->query.attributes & \
615eff12e0e3565c52758292e10080a25d872941William King NS_QUERYATTR_CACHEGLUEOK) != 0)
615eff12e0e3565c52758292e10080a25d872941William King#define WANTRECURSION(c) (((c)->query.attributes & \
615eff12e0e3565c52758292e10080a25d872941William King NS_QUERYATTR_WANTRECURSION) != 0)
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King#if 0
615eff12e0e3565c52758292e10080a25d872941William King#define CTRACE(m) isc_log_write(ns_g_lctx, \
615eff12e0e3565c52758292e10080a25d872941William King NS_LOGCATEGORY_CLIENT, \
615eff12e0e3565c52758292e10080a25d872941William King NS_LOGMODULE_QUERY, \
615eff12e0e3565c52758292e10080a25d872941William King ISC_LOG_DEBUG(3), \
615eff12e0e3565c52758292e10080a25d872941William King "client %p: %s", client, (m))
615eff12e0e3565c52758292e10080a25d872941William King#define QTRACE(m) isc_log_write(ns_g_lctx, \
615eff12e0e3565c52758292e10080a25d872941William King NS_LOGCATEGORY_GENERAL, \
615eff12e0e3565c52758292e10080a25d872941William King NS_LOGMODULE_QUERY, \
615eff12e0e3565c52758292e10080a25d872941William King ISC_LOG_DEBUG(3), \
615eff12e0e3565c52758292e10080a25d872941William King "query %p: %s", query, (m))
615eff12e0e3565c52758292e10080a25d872941William King#else
615eff12e0e3565c52758292e10080a25d872941William King#define CTRACE(m) ((void)m)
615eff12e0e3565c52758292e10080a25d872941William King#define QTRACE(m) ((void)m)
615eff12e0e3565c52758292e10080a25d872941William King#endif
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King#define DNS_GETDB_NOEXACT 0x01U
615eff12e0e3565c52758292e10080a25d872941William King#define DNS_GETDB_NOLOG 0x02U
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William Kingstatic isc_result_t
615eff12e0e3565c52758292e10080a25d872941William Kingquery_simplefind(void *arg, dns_name_t *name, dns_rdatatype_t type,
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington isc_stdtime_t now,
615eff12e0e3565c52758292e10080a25d872941William King dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
4124057bf54f0fc75b06b35487130bbd9249ee7fBrian Wellington
1b6d529cb5ee0ad44f8518e1b8c2cbca54bbdf18David Lawrencestatic inline void
615eff12e0e3565c52758292e10080a25d872941William Kingquery_adda6rrset(void *arg, dns_name_t *name, dns_rdataset_t *rdataset,
615eff12e0e3565c52758292e10080a25d872941William King dns_rdataset_t *sigrdataset);
615eff12e0e3565c52758292e10080a25d872941William King
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellingtonstatic void
615eff12e0e3565c52758292e10080a25d872941William Kingquery_find(ns_client_t *client, dns_fetchevent_t *event);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
615eff12e0e3565c52758292e10080a25d872941William Kingstatic inline void
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrencequery_maybeputqname(ns_client_t *client) {
615eff12e0e3565c52758292e10080a25d872941William King if (client->query.restarts > 0) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington /*
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * client->query.qname was dynamically allocated.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrence dns_message_puttempname(client->message,
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington &client->query.qname);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington client->query.qname = NULL;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington }
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington}
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellingtonstatic inline void
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrencequery_reset(ns_client_t *client, isc_boolean_t everything) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington isc_buffer_t *dbuf, *dbuf_next;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington ns_dbversion_t *dbversion, *dbversion_next;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington unsigned int i;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
615eff12e0e3565c52758292e10080a25d872941William King /*
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * Reset the query state of a client to its default state.
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrence */
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrence
615eff12e0e3565c52758292e10080a25d872941William King /*
615eff12e0e3565c52758292e10080a25d872941William King * Cancel the fetch if it's running.
615eff12e0e3565c52758292e10080a25d872941William King */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (client->query.fetch != NULL) {
615eff12e0e3565c52758292e10080a25d872941William King dns_resolver_cancelfetch(client->query.fetch);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington client->query.fetch = NULL;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington }
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrence /*
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * Cleanup any active versions.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dbversion != NULL;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dbversion = dbversion_next) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dbversion_next = ISC_LIST_NEXT(dbversion, link);
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrence dns_db_closeversion(dbversion->db, &dbversion->version,
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington ISC_FALSE);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_db_detach(&dbversion->db);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington ISC_LIST_APPEND(client->query.freeversions, dbversion, link);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington }
615eff12e0e3565c52758292e10080a25d872941William King ISC_LIST_INIT(client->query.activeversions);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrence if (client->query.authdb != NULL)
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrence dns_db_detach(&client->query.authdb);
615eff12e0e3565c52758292e10080a25d872941William King
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington /*
615eff12e0e3565c52758292e10080a25d872941William King * Clean up free versions.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
615eff12e0e3565c52758292e10080a25d872941William King for (dbversion = ISC_LIST_HEAD(client->query.freeversions), i = 0;
615eff12e0e3565c52758292e10080a25d872941William King dbversion != NULL;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington dbversion = dbversion_next, i++) {
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington dbversion_next = ISC_LIST_NEXT(dbversion, link);
4124057bf54f0fc75b06b35487130bbd9249ee7fBrian Wellington /*
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington * If we're not freeing everything, we keep the first three
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington * dbversions structures around.
4124057bf54f0fc75b06b35487130bbd9249ee7fBrian Wellington */
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington if (i > 3 || everything) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington ISC_LIST_UNLINK(client->query.freeversions, dbversion,
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington link);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_mem_put(client->mctx, dbversion,
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington sizeof *dbversion);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington }
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington }
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington for (dbuf = ISC_LIST_HEAD(client->query.namebufs);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington dbuf != NULL;
fd9f6afdff9faf55411dfd5f828529601cab0511Andreas Gustafsson dbuf = dbuf_next) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington dbuf_next = ISC_LIST_NEXT(dbuf, link);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington if (dbuf_next != NULL || everything) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington ISC_LIST_UNLINK(client->query.namebufs, dbuf, link);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_buffer_free(&dbuf);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington }
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington }
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington query_maybeputqname(client);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington client->query.attributes = (NS_QUERYATTR_RECURSIONOK |
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington NS_QUERYATTR_CACHEOK);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington client->query.restarts = 0;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington client->query.origqname = NULL;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington client->query.qname = NULL;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington client->query.qrdataset = NULL;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington client->query.dboptions = 0;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington client->query.fetchoptions = 0;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington client->query.gluedb = NULL;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington client->query.authdb = NULL;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington}
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonstatic void
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonquery_next(ns_client_t *client) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington query_reset(client, ISC_FALSE);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington}
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonvoid
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonns_query_free(ns_client_t *client) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington query_reset(client, ISC_TRUE);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington}
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonstatic inline isc_result_t
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonquery_newnamebuf(ns_client_t *client) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_buffer_t *dbuf;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_result_t result;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington CTRACE("query_newnamebuf");
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington /*
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington * Allocate a name buffer.
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington */
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington dbuf = NULL;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington result = isc_buffer_allocate(client->mctx, &dbuf, 1024);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington if (result != ISC_R_SUCCESS) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington CTRACE("query_newnamebuf: isc_buffer_allocate failed: done");
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington return (result);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington }
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington ISC_LIST_APPEND(client->query.namebufs, dbuf, link);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington CTRACE("query_newnamebuf: done");
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington return (ISC_R_SUCCESS);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington}
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonstatic inline isc_buffer_t *
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonquery_getnamebuf(ns_client_t *client) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_buffer_t *dbuf;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_result_t result;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_region_t r;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington CTRACE("query_getnamebuf");
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington /*
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington * Return a name buffer with space for a maximal name, allocating
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington * a new one if necessary.
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington */
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington if (ISC_LIST_EMPTY(client->query.namebufs)) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington result = query_newnamebuf(client);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington if (result != ISC_R_SUCCESS) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington CTRACE("query_getnamebuf: query_newnamebuf failed: done");
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington return (NULL);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
81ab85fd39c3f6c781ecac725d830364a311479dBrian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington dbuf = ISC_LIST_TAIL(client->query.namebufs);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington INSIST(dbuf != NULL);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_buffer_availableregion(dbuf, &r);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington if (r.length < 255) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington result = query_newnamebuf(client);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington if (result != ISC_R_SUCCESS) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington CTRACE("query_getnamebuf: query_newnamebuf failed: done");
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence return (NULL);
81ab85fd39c3f6c781ecac725d830364a311479dBrian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington }
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington dbuf = ISC_LIST_TAIL(client->query.namebufs);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_buffer_availableregion(dbuf, &r);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington INSIST(r.length >= 255);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington }
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington CTRACE("query_getnamebuf: done");
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington return (dbuf);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence}
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonstatic inline void
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonquery_keepname(ns_client_t *client, dns_name_t *name, isc_buffer_t *dbuf) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington isc_region_t r;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington CTRACE("query_keepname");
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington /*
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington * 'name' is using space in 'dbuf', but 'dbuf' has not yet been
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington * adjusted to take account of that. We do the adjustment.
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington */
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) != 0);
615eff12e0e3565c52758292e10080a25d872941William King
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington dns_name_toregion(name, &r);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_buffer_add(dbuf, r.length);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dns_name_setbuffer(name, NULL);
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
4124057bf54f0fc75b06b35487130bbd9249ee7fBrian Wellington}
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William Kingstatic inline void
615eff12e0e3565c52758292e10080a25d872941William Kingquery_releasename(ns_client_t *client, dns_name_t **namep) {
615eff12e0e3565c52758292e10080a25d872941William King dns_name_t *name = *namep;
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King /*
615eff12e0e3565c52758292e10080a25d872941William King * 'name' is no longer needed. Return it to our pool of temporary
615eff12e0e3565c52758292e10080a25d872941William King * names. If it is using a name buffer, relinquish its exclusive
615eff12e0e3565c52758292e10080a25d872941William King * rights on the buffer.
615eff12e0e3565c52758292e10080a25d872941William King */
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King CTRACE("query_releasename");
615eff12e0e3565c52758292e10080a25d872941William King if (dns_name_hasbuffer(name)) {
a7d792804f564aa0889a94f64913b11bffd9738cBrian Wellington INSIST((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED)
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence != 0);
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence client->query.attributes &= ~NS_QUERYATTR_NAMEBUFUSED;
615eff12e0e3565c52758292e10080a25d872941William King }
615eff12e0e3565c52758292e10080a25d872941William King dns_message_puttempname(client->message, namep);
615eff12e0e3565c52758292e10080a25d872941William King CTRACE("query_releasename: done");
615eff12e0e3565c52758292e10080a25d872941William King}
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William Kingstatic inline dns_name_t *
615eff12e0e3565c52758292e10080a25d872941William Kingquery_newname(ns_client_t *client, isc_buffer_t *dbuf,
615eff12e0e3565c52758292e10080a25d872941William King isc_buffer_t *nbuf)
615eff12e0e3565c52758292e10080a25d872941William King{
615eff12e0e3565c52758292e10080a25d872941William King dns_name_t *name;
615eff12e0e3565c52758292e10080a25d872941William King isc_region_t r;
615eff12e0e3565c52758292e10080a25d872941William King isc_result_t result;
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King REQUIRE((client->query.attributes & NS_QUERYATTR_NAMEBUFUSED) == 0);
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King CTRACE("query_newname");
615eff12e0e3565c52758292e10080a25d872941William King name = NULL;
615eff12e0e3565c52758292e10080a25d872941William King result = dns_message_gettempname(client->message, &name);
615eff12e0e3565c52758292e10080a25d872941William King if (result != ISC_R_SUCCESS) {
615eff12e0e3565c52758292e10080a25d872941William King CTRACE("query_newname: dns_message_gettempname failed: done");
615eff12e0e3565c52758292e10080a25d872941William King return (NULL);
615eff12e0e3565c52758292e10080a25d872941William King }
615eff12e0e3565c52758292e10080a25d872941William King isc_buffer_availableregion(dbuf, &r);
615eff12e0e3565c52758292e10080a25d872941William King isc_buffer_init(nbuf, r.base, r.length);
615eff12e0e3565c52758292e10080a25d872941William King dns_name_init(name, NULL);
615eff12e0e3565c52758292e10080a25d872941William King dns_name_setbuffer(name, nbuf);
615eff12e0e3565c52758292e10080a25d872941William King client->query.attributes |= NS_QUERYATTR_NAMEBUFUSED;
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence CTRACE("query_newname: done");
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence return (name);
615eff12e0e3565c52758292e10080a25d872941William King}
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William Kingstatic inline dns_rdataset_t *
615eff12e0e3565c52758292e10080a25d872941William Kingquery_newrdataset(ns_client_t *client) {
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington dns_rdataset_t *rdataset;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington isc_result_t result;
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King CTRACE("query_newrdataset");
615eff12e0e3565c52758292e10080a25d872941William King rdataset = NULL;
615eff12e0e3565c52758292e10080a25d872941William King result = dns_message_gettemprdataset(client->message, &rdataset);
615eff12e0e3565c52758292e10080a25d872941William King if (result != ISC_R_SUCCESS) {
615eff12e0e3565c52758292e10080a25d872941William King CTRACE("query_newrdataset: "
615eff12e0e3565c52758292e10080a25d872941William King "dns_message_gettemprdataset failed: done");
615eff12e0e3565c52758292e10080a25d872941William King return (NULL);
615eff12e0e3565c52758292e10080a25d872941William King }
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington dns_rdataset_init(rdataset);
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King CTRACE("query_newrdataset: done");
615eff12e0e3565c52758292e10080a25d872941William King return (rdataset);
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington}
4124057bf54f0fc75b06b35487130bbd9249ee7fBrian Wellington
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellingtonstatic inline void
615eff12e0e3565c52758292e10080a25d872941William Kingquery_putrdataset(ns_client_t *client, dns_rdataset_t **rdatasetp) {
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington dns_rdataset_t *rdataset = *rdatasetp;
a7d792804f564aa0889a94f64913b11bffd9738cBrian Wellington
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence CTRACE("query_putrdataset");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (rdataset != NULL) {
615eff12e0e3565c52758292e10080a25d872941William King if (dns_rdataset_isassociated(rdataset))
615eff12e0e3565c52758292e10080a25d872941William King dns_rdataset_disassociate(rdataset);
615eff12e0e3565c52758292e10080a25d872941William King dns_message_puttemprdataset(client->message, rdatasetp);
615eff12e0e3565c52758292e10080a25d872941William King }
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington CTRACE("query_putrdataset: done");
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington}
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William Kingstatic inline isc_result_t
615eff12e0e3565c52758292e10080a25d872941William Kingquery_newdbversion(ns_client_t *client, unsigned int n) {
615eff12e0e3565c52758292e10080a25d872941William King unsigned int i;
615eff12e0e3565c52758292e10080a25d872941William King ns_dbversion_t *dbversion;
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King for (i = 0; i < n; i++) {
615eff12e0e3565c52758292e10080a25d872941William King dbversion = isc_mem_get(client->mctx, sizeof *dbversion);
615eff12e0e3565c52758292e10080a25d872941William King if (dbversion != NULL) {
615eff12e0e3565c52758292e10080a25d872941William King dbversion->db = NULL;
4124057bf54f0fc75b06b35487130bbd9249ee7fBrian Wellington dbversion->version = NULL;
615eff12e0e3565c52758292e10080a25d872941William King ISC_LIST_APPEND(client->query.freeversions, dbversion,
615eff12e0e3565c52758292e10080a25d872941William King link);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence } else {
1bdc3fd29a6f615a2c1361613c36768e478c0cc2Brian Wellington /*
a7d792804f564aa0889a94f64913b11bffd9738cBrian Wellington * We only return ISC_R_NOMEMORY if we couldn't
615eff12e0e3565c52758292e10080a25d872941William King * allocate anything.
615eff12e0e3565c52758292e10080a25d872941William King */
615eff12e0e3565c52758292e10080a25d872941William King if (i == 0)
615eff12e0e3565c52758292e10080a25d872941William King return (ISC_R_NOMEMORY);
615eff12e0e3565c52758292e10080a25d872941William King else
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence return (ISC_R_SUCCESS);
1bdc3fd29a6f615a2c1361613c36768e478c0cc2Brian Wellington }
a7d792804f564aa0889a94f64913b11bffd9738cBrian Wellington }
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King return (ISC_R_SUCCESS);
615eff12e0e3565c52758292e10080a25d872941William King}
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William Kingstatic inline ns_dbversion_t *
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrencequery_getdbversion(ns_client_t *client) {
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrence isc_result_t result;
446aa1ec8cb8f231d8649cfd705c90c6d0976143David Lawrence ns_dbversion_t *dbversion;
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King if (ISC_LIST_EMPTY(client->query.freeversions)) {
615eff12e0e3565c52758292e10080a25d872941William King result = query_newdbversion(client, 1);
615eff12e0e3565c52758292e10080a25d872941William King if (result != ISC_R_SUCCESS)
615eff12e0e3565c52758292e10080a25d872941William King return (NULL);
615eff12e0e3565c52758292e10080a25d872941William King }
615eff12e0e3565c52758292e10080a25d872941William King dbversion = ISC_LIST_HEAD(client->query.freeversions);
615eff12e0e3565c52758292e10080a25d872941William King INSIST(dbversion != NULL);
615eff12e0e3565c52758292e10080a25d872941William King ISC_LIST_UNLINK(client->query.freeversions, dbversion, link);
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King return (dbversion);
615eff12e0e3565c52758292e10080a25d872941William King}
615eff12e0e3565c52758292e10080a25d872941William King
1b6d529cb5ee0ad44f8518e1b8c2cbca54bbdf18David Lawrenceisc_result_t
615eff12e0e3565c52758292e10080a25d872941William Kingns_query_init(ns_client_t *client) {
615eff12e0e3565c52758292e10080a25d872941William King isc_result_t result;
615eff12e0e3565c52758292e10080a25d872941William King
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington ISC_LIST_INIT(client->query.namebufs);
615eff12e0e3565c52758292e10080a25d872941William King ISC_LIST_INIT(client->query.activeversions);
615eff12e0e3565c52758292e10080a25d872941William King ISC_LIST_INIT(client->query.freeversions);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence client->query.restarts = 0;
615eff12e0e3565c52758292e10080a25d872941William King client->query.qname = NULL;
615eff12e0e3565c52758292e10080a25d872941William King client->query.fetch = NULL;
615eff12e0e3565c52758292e10080a25d872941William King client->query.authdb = NULL;
615eff12e0e3565c52758292e10080a25d872941William King query_reset(client, ISC_FALSE);
615eff12e0e3565c52758292e10080a25d872941William King result = query_newdbversion(client, 3);
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington if (result != ISC_R_SUCCESS)
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington return (result);
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington dns_a6_init(&client->query.a6ctx, query_simplefind, query_adda6rrset,
615eff12e0e3565c52758292e10080a25d872941William King NULL, NULL, client);
615eff12e0e3565c52758292e10080a25d872941William King return (query_newnamebuf(client));
615eff12e0e3565c52758292e10080a25d872941William King}
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William Kingstatic inline ns_dbversion_t *
615eff12e0e3565c52758292e10080a25d872941William Kingquery_findversion(ns_client_t *client, dns_db_t *db,
615eff12e0e3565c52758292e10080a25d872941William King isc_boolean_t *newzonep)
615eff12e0e3565c52758292e10080a25d872941William King{
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_dbversion_t *dbversion;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
615eff12e0e3565c52758292e10080a25d872941William King /*
615eff12e0e3565c52758292e10080a25d872941William King * We may already have done a query related to this
615eff12e0e3565c52758292e10080a25d872941William King * database. If so, we must be sure to make subsequent
615eff12e0e3565c52758292e10080a25d872941William King * queries from the same version.
0942b8e0d660895ae96ffbd447fd0290fe60539cMichael Graff */
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington for (dbversion = ISC_LIST_HEAD(client->query.activeversions);
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington dbversion != NULL;
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington dbversion = ISC_LIST_NEXT(dbversion, link)) {
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington if (dbversion->db == db)
11836b7a55fa62ee6628e4e41845a0ef7084e383David Lawrence break;
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington }
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington
a7d792804f564aa0889a94f64913b11bffd9738cBrian Wellington if (dbversion == NULL) {
0942b8e0d660895ae96ffbd447fd0290fe60539cMichael Graff /*
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington * This is a new zone for this query. Add it to
a7d792804f564aa0889a94f64913b11bffd9738cBrian Wellington * the active list.
615eff12e0e3565c52758292e10080a25d872941William King */
0942b8e0d660895ae96ffbd447fd0290fe60539cMichael Graff dbversion = query_getdbversion(client);
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington if (dbversion == NULL)
8d0470dbdb0953796a4ccad60db635bcb6bc9086Bob Halley return (NULL);
0942b8e0d660895ae96ffbd447fd0290fe60539cMichael Graff dns_db_attach(db, &dbversion->db);
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington dns_db_currentversion(db, &dbversion->version);
8d0470dbdb0953796a4ccad60db635bcb6bc9086Bob Halley dbversion->queryok = ISC_FALSE;
615eff12e0e3565c52758292e10080a25d872941William King ISC_LIST_APPEND(client->query.activeversions,
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington dbversion, link);
11836b7a55fa62ee6628e4e41845a0ef7084e383David Lawrence *newzonep = ISC_TRUE;
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington } else
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington *newzonep = ISC_FALSE;
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington return (dbversion);
615eff12e0e3565c52758292e10080a25d872941William King}
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonstatic inline isc_result_t
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellingtonquery_getzonedb(ns_client_t *client, dns_name_t *name, unsigned int options,
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence dns_zone_t **zonep, dns_db_t **dbp, dns_dbversion_t **versionp)
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence{
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence isc_result_t result;
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence isc_boolean_t check_acl, new_zone;
7c37bc1a67a80209e05f6b1c01fa7fde7d0dd200Brian Wellington dns_acl_t *queryacl;
615eff12e0e3565c52758292e10080a25d872941William King ns_dbversion_t *dbversion;
615eff12e0e3565c52758292e10080a25d872941William King unsigned int ztoptions;
615eff12e0e3565c52758292e10080a25d872941William King dns_zone_t *zone = NULL;
615eff12e0e3565c52758292e10080a25d872941William King dns_db_t *db = NULL;
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King REQUIRE(zonep != NULL && *zonep == NULL);
615eff12e0e3565c52758292e10080a25d872941William King REQUIRE(dbp != NULL && *dbp == NULL);
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King /*
615eff12e0e3565c52758292e10080a25d872941William King * Find a zone database to answer the query.
615eff12e0e3565c52758292e10080a25d872941William King */
615eff12e0e3565c52758292e10080a25d872941William King ztoptions = ((options & DNS_GETDB_NOEXACT) != 0) ?
615eff12e0e3565c52758292e10080a25d872941William King DNS_ZTFIND_NOEXACT : 0;
615eff12e0e3565c52758292e10080a25d872941William King
615eff12e0e3565c52758292e10080a25d872941William King result = dns_zt_find(client->view->zonetable, name, ztoptions, NULL,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King &zone);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence result = dns_zone_getdb(zone, &db);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (result != ISC_R_SUCCESS)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King goto fail;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * If this is the first time we are called (that is, looking up
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * the actual name in the query section) remember this database.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King *
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * If authdb is non-NULL, we have been here before, and the
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * found database is always returned.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King *
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * This limits our searching to the zone where the first name
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * (the query target) is found. This prevents following CNAMES
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * or DNAMES into other zones and prevents returning additional
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * data from other zones.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (!client->view->additionalfromauth) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (client->query.authdb != NULL) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (db != client->query.authdb)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King goto refuse;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence } else {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dns_db_attach(db, &client->query.authdb);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * If the zone has an ACL, we'll check it, otherwise
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * we use the view's "allow-query" ACL. Each ACL is only checked
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * once per query.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King *
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * Also, get the database version to use.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King check_acl = ISC_TRUE; /* Keep compiler happy. */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King queryacl = NULL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * Get the current version of this database.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dbversion = query_findversion(client, db, &new_zone);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (dbversion == NULL) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King result = DNS_R_SERVFAIL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King goto fail;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (new_zone) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King check_acl = ISC_TRUE;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King } else if (!dbversion->queryok) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King goto refuse;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King } else {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King check_acl = ISC_FALSE;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King queryacl = dns_zone_getqueryacl(zone);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (queryacl == NULL) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King queryacl = client->view->queryacl;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if ((client->query.attributes &
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King NS_QUERYATTR_QUERYOKVALID) != 0) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * We've evaluated the view's queryacl already. If
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * NS_QUERYATTR_QUERYOK is set, then the client is
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * allowed to make queries, otherwise the query should
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * be refused.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King check_acl = ISC_FALSE;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if ((client->query.attributes &
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King NS_QUERYATTR_QUERYOK) == 0)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King goto refuse;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King } else {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * We haven't evaluated the view's queryacl yet.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King check_acl = ISC_TRUE;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (check_acl) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence result = ns_client_checkacl(client, "query", queryacl,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ISC_TRUE,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence log ? ISC_LOG_INFO : ISC_LOG_DEBUG(3));
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (queryacl == client->view->queryacl) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (result == ISC_R_SUCCESS) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * We were allowed by the default
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * "allow-query" ACL. Remember this so we
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * don't have to check again.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King client->query.attributes |=
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King NS_QUERYATTR_QUERYOK;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * We've now evaluated the view's query ACL, and
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * the NS_QUERYATTR_QUERYOK attribute is now valid.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
18670af94df89d403465aecc607f3e860acb5931Bob Halley }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (result != ISC_R_SUCCESS)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King goto refuse;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /* Approved. */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * Remember the result of the ACL check so we
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * don't have to check again.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dbversion->queryok = ISC_TRUE;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /* Transfer ownership. */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King *zonep = zone;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King *dbp = db;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King *versionp = dbversion->version;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King return (ISC_R_SUCCESS);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King refuse:
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King result = DNS_R_REFUSED;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King fail:
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (zone != NULL)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_zone_detach(&zone);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (db != NULL)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_db_detach(&db);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King return (result);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King}
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William Kingstatic inline isc_result_t
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William Kingquery_getcachedb(ns_client_t *client, dns_db_t **dbp, unsigned int options)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King{
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_result_t result;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_boolean_t check_acl;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_db_t *db = NULL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King REQUIRE(dbp != NULL && *dbp == NULL);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * Find a cache database to answer the query.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * This may fail with DNS_R_REFUSED if the client
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * is not allowed to use the cache.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (!USECACHE(client))
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King return (DNS_R_REFUSED);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_db_attach(client->view->cachedb, &db);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if ((client->query.attributes &
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King NS_QUERYATTR_QUERYOKVALID) != 0) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * We've evaluated the view's queryacl already. If
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * NS_QUERYATTR_QUERYOK is set, then the client is
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * allowed to make queries, otherwise the query should
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * be refused.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King check_acl = ISC_FALSE;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if ((client->query.attributes &
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence NS_QUERYATTR_QUERYOK) == 0)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence goto refuse;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King } else {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * We haven't evaluated the view's queryacl yet.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington check_acl = ISC_TRUE;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (check_acl) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_boolean_t log = ISC_TF((options & DNS_GETDB_NOLOG) == 0);
fa6c5e38f4b904aa85f3375f1ca830d4bf8f5877Andreas Gustafsson result = ns_client_checkacl(client, "query",
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King client->view->queryacl,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King ISC_TRUE,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King log ? ISC_LOG_INFO : ISC_LOG_DEBUG(3));
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (result == ISC_R_SUCCESS) {
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington /*
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington * We were allowed by the default
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington * "allow-query" ACL. Remember this so we
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * don't have to check again.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence client->query.attributes |=
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence NS_QUERYATTR_QUERYOK;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * We've now evaluated the view's query ACL, and
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * the NS_QUERYATTR_QUERYOK attribute is now valid.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King client->query.attributes |= NS_QUERYATTR_QUERYOKVALID;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (result != ISC_R_SUCCESS)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King goto refuse;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /* Approved. */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /* Transfer ownership. */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King *dbp = db;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King return (ISC_R_SUCCESS);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King refuse:
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King result = DNS_R_REFUSED;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (db != NULL)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_db_detach(&db);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King return (result);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King}
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William Kingstatic inline isc_result_t
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William Kingquery_getdb(ns_client_t *client, dns_name_t *name, unsigned int options,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_zone_t **zonep, dns_db_t **dbp, dns_dbversion_t **versionp,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_boolean_t *is_zonep)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King{
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_result_t result;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence result = query_getzonedb(client, name, options, zonep, dbp, versionp);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (result == ISC_R_SUCCESS) {
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington *is_zonep = ISC_TRUE;
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington } else if (result == ISC_R_NOTFOUND) {
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington result = query_getcachedb(client, dbp, options);
11836b7a55fa62ee6628e4e41845a0ef7084e383David Lawrence *is_zonep = ISC_FALSE;
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington }
a9bc95f22ef2dd4a12e79be99412c9f18b814a5dBrian Wellington
fa6c5e38f4b904aa85f3375f1ca830d4bf8f5877Andreas Gustafsson return (result);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King}
fa6c5e38f4b904aa85f3375f1ca830d4bf8f5877Andreas Gustafsson
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William Kingstatic isc_result_t
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William Kingquery_simplefind(void *arg, dns_name_t *name, dns_rdatatype_t type,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_stdtime_t now,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King{
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence ns_client_t *client = arg;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_result_t result;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_fixedname_t foundname;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_db_t *db;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_dbversion_t *version;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King unsigned int dboptions;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_boolean_t is_zone;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_rdataset_t zrdataset, zsigrdataset;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_zone_t *zone;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King REQUIRE(NS_CLIENT_VALID(client));
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King REQUIRE(rdataset != NULL);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence REQUIRE(sigrdataset != NULL);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_rdataset_init(&zrdataset);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_rdataset_init(&zsigrdataset);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington /*
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * Find a database to answer the query.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington zone = NULL;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington db = NULL;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington version = NULL;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington result = query_getdb(client, name, 0, &zone, &db, &version, &is_zone);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (result != ISC_R_SUCCESS)
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington goto cleanup;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington db_find:
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington /*
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * Now look for an answer in the database.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_fixedname_init(&foundname);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dboptions = client->query.dboptions;
fa6c5e38f4b904aa85f3375f1ca830d4bf8f5877Andreas Gustafsson if (db == client->query.gluedb || (!is_zone && CACHEGLUEOK(client)))
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dboptions |= DNS_DBFIND_GLUEOK;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence result = dns_db_find(db, name, version, type, dboptions,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King now, NULL, dns_fixedname_name(&foundname),
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington rdataset, sigrdataset);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (result == DNS_R_DELEGATION ||
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King result == ISC_R_NOTFOUND) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (dns_rdataset_isassociated(rdataset))
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_rdataset_disassociate(rdataset);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (dns_rdataset_isassociated(sigrdataset))
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdataset_disassociate(sigrdataset);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (is_zone) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (USECACHE(client)) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * Either the answer is in the cache, or we
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington * don't know it.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King is_zone = ISC_FALSE;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King version = NULL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_db_detach(&db);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_db_attach(client->view->cachedb, &db);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King goto db_find;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King } else {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence /*
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * We don't have the data in the cache. If we've got
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * glue from the zone, use it.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (dns_rdataset_isassociated(&zrdataset)) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdataset_clone(&zrdataset, rdataset);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (dns_rdataset_isassociated(&zsigrdataset))
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington dns_rdataset_clone(&zsigrdataset,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King sigrdataset);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King result = ISC_R_SUCCESS;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King goto cleanup;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * We don't know the answer.
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King result = ISC_R_NOTFOUND;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King } else if (result == DNS_R_GLUE) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (USECACHE(client) && RECURSIONOK(client)) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * We found an answer, but the cache may be better.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * Remember what we've got and go look in the cache.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington is_zone = ISC_FALSE;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington version = NULL;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_rdataset_clone(rdataset, &zrdataset);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_rdataset_disassociate(rdataset);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (dns_rdataset_isassociated(sigrdataset)) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_rdataset_clone(sigrdataset, &zsigrdataset);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_rdataset_disassociate(sigrdataset);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington }
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_db_detach(&db);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dns_db_attach(client->view->cachedb, &db);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington goto db_find;
fa6c5e38f4b904aa85f3375f1ca830d4bf8f5877Andreas Gustafsson }
fa6c5e38f4b904aa85f3375f1ca830d4bf8f5877Andreas Gustafsson /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * Otherwise, the glue is the best answer.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
fa6c5e38f4b904aa85f3375f1ca830d4bf8f5877Andreas Gustafsson result = ISC_R_SUCCESS;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King } else if (result != ISC_R_SUCCESS) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (dns_rdataset_isassociated(rdataset))
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdataset_disassociate(rdataset);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (dns_rdataset_isassociated(sigrdataset))
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdataset_disassociate(sigrdataset);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington result = ISC_R_NOTFOUND;
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King cleanup:
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (dns_rdataset_isassociated(&zrdataset)) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdataset_disassociate(&zrdataset);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (dns_rdataset_isassociated(&zsigrdataset))
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdataset_disassociate(&zsigrdataset);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (db != NULL)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_db_detach(&db);
1b6d529cb5ee0ad44f8518e1b8c2cbca54bbdf18David Lawrence if (zone != NULL)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_zone_detach(&zone);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King return (result);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence}
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic inline isc_boolean_t
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William Kingquery_isduplicate(ns_client_t *client, dns_name_t *name,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdatatype_t type, dns_name_t **mnamep)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King{
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_section_t section;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_name_t *mname = NULL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_result_t result;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King CTRACE("query_isduplicate");
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King for (section = DNS_SECTION_ANSWER;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King section <= DNS_SECTION_ADDITIONAL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King section++) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King result = dns_message_findname(client->message, section,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King name, type, 0, &mname, NULL);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (result == ISC_R_SUCCESS) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * We've already got this RRset in the response.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King CTRACE("query_isduplicate: true: done");
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King return (ISC_TRUE);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King } else if (result == DNS_R_NXRRSET) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * The name exists, but the rdataset does not.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (section == DNS_SECTION_ADDITIONAL)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King break;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King } else
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King RUNTIME_CHECK(result == DNS_R_NXDOMAIN);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King mname = NULL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King }
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * If the dns_name_t we're lookup up is already in the message,
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * we don't want to trigger the caller's name replacement logic.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King if (name == mname)
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King mname = NULL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *mnamep = mname;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King CTRACE("query_isduplicate: false: done");
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King return (ISC_FALSE);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King}
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William Kingstatic isc_result_t
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William Kingquery_addadditional(void *arg, dns_name_t *name, dns_rdatatype_t qtype) {
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King ns_client_t *client = arg;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_result_t result, eresult;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_dbnode_t *node;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_db_t *db;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_name_t *fname, *mname;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdataset_t *rdataset, *sigrdataset, *a6rdataset, *trdataset;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_buffer_t *dbuf;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_buffer_t b;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_dbversion_t *version;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King isc_boolean_t added_something, need_addname;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_zone_t *zone;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King dns_rdatatype_t type;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King REQUIRE(NS_CLIENT_VALID(client));
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King REQUIRE(qtype != dns_rdatatype_any);
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence CTRACE("query_addadditional");
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King
2768168c728f871e4b823aa4ec1658087a20902aWilliam King /*
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King * Initialization.
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King */
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King eresult = ISC_R_SUCCESS;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King fname = NULL;
615eff12e0e3565c52758292e10080a25d872941William King rdataset = NULL;
615eff12e0e3565c52758292e10080a25d872941William King sigrdataset = NULL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King a6rdataset = NULL;
e5966f9b9a13fc83afaf9b74ac1f7eea6a647817William King trdataset = NULL;
615eff12e0e3565c52758292e10080a25d872941William King db = NULL;
615eff12e0e3565c52758292e10080a25d872941William King version = NULL;
node = NULL;
added_something = ISC_FALSE;
need_addname = ISC_FALSE;
zone = NULL;
/*
* We treat type A additional section processing as if it
* were "any address type" additional section processing.
* To avoid multiple lookups, we do an 'any' database
* lookup and iterate over the node.
*/
if (qtype == dns_rdatatype_a)
type = dns_rdatatype_any;
else
type = qtype;
/*
* Get some resources.
*/
dbuf = query_getnamebuf(client);
if (dbuf == NULL)
goto cleanup;
fname = query_newname(client, dbuf, &b);
rdataset = query_newrdataset(client);
sigrdataset = query_newrdataset(client);
if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
goto cleanup;
/*
* Look for a zone database that might contain authoritative
* additional data.
*/
result = query_getzonedb(client, name, DNS_GETDB_NOLOG,
&zone, &db, &version);
if (result != ISC_R_SUCCESS)
goto try_cache;
CTRACE("query_addadditional: db_find");
/*
* Since we are looking for authoritative data, we do not set
* the GLUEOK flag. Glue will be looked for later, but not
* necessarily in the same database.
*/
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 (dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
if (node != NULL)
dns_db_detachnode(db, &node);
version = NULL;
dns_db_detach(&db);
/*
* No authoritative data was found. The cache is our next best bet.
*/
try_cache:
result = query_getcachedb(client, &db, DNS_GETDB_NOLOG);
if (result != ISC_R_SUCCESS)
/*
* Most likely the client isn't allowed to query the cache.
*/
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 (dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
if (node != NULL)
dns_db_detachnode(db, &node);
dns_db_detach(&db);
try_glue:
/*
* No cached data was found. Glue is our last chance.
* RFC1035 sayeth:
*
* NS records cause both the usual additional section
* processing to locate a type A record, and, when used
* in a referral, a special search of the zone in which
* they reside for glue information.
*
* This is the "special search". Note that we must search
* the zone where the NS record resides, not the zone it
* points to, and that we only do the search in the delegation
* case (identified by client->query.gluedb being set).
*/
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:
/*
* We have found a potential additional data rdataset, or
* at least a node to iterate over.
*/
query_keepname(client, fname, dbuf);
/*
* If we have an rdataset, add it to the additional data
* section.
*/
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;
/*
* Note: we only add SIGs if we've added the type they cover,
* so we do not need to check if the SIG rdataset is already
* in the response.
*/
if (dns_rdataset_isassociated(sigrdataset)) {
ISC_LIST_APPEND(fname->list, sigrdataset, link);
sigrdataset = NULL;
}
}
if (qtype == dns_rdatatype_a) {
/*
* We now go looking for A, A6, and AAAA records, along with
* their signatures.
*
* XXXRTH This code could be more efficient.
*/
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 {
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);
/*
* Negative cache entries don't have sigrdatasets.
*/
INSIST(! 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 (dns_rdataset_isassociated(sigrdataset)) {
ISC_LIST_APPEND(fname->list,
sigrdataset, link);
sigrdataset =
query_newrdataset(client);
}
rdataset = query_newrdataset(client);
if (rdataset == NULL || sigrdataset == NULL)
goto addname;
} else {
dns_rdataset_disassociate(rdataset);
if (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(! 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 (dns_rdataset_isassociated(sigrdataset)) {
ISC_LIST_APPEND(fname->list,
sigrdataset, link);
sigrdataset =
query_newrdataset(client);
}
rdataset = query_newrdataset(client);
if (rdataset == NULL || sigrdataset == NULL)
goto addname;
} else
dns_rdataset_disassociate(rdataset);
}
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(! 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 (dns_rdataset_isassociated(sigrdataset)) {
ISC_LIST_APPEND(fname->list,
sigrdataset, link);
sigrdataset = NULL;
}
rdataset = NULL;
}
}
}
addname:
CTRACE("query_addadditional: addname");
/*
* If we haven't added anything, then we're done.
*/
if (!added_something)
goto cleanup;
/*
* We may have added our rdatasets to an existing name, if so, then
* need_addname will be ISC_FALSE. Whether we used an existing name
* or a new one, we must set fname to NULL to prevent cleanup.
*/
if (need_addname)
dns_message_addname(client->message, fname,
DNS_SECTION_ADDITIONAL);
fname = NULL;
/*
* 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.
*/
if (type == dns_rdatatype_a || type == dns_rdatatype_aaaa) {
/*
* 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. Note: we
* do NOT include A6 in the list of types with such treatment
* in additional data because we'd have to do it for each A6
* in the A6 chain.
*
* XXXRTH We should lower the priority here. Alternatively,
* we could raise the priority of glue records.
*/
eresult = query_addadditional(client, name, dns_rdatatype_key);
} else if (type == dns_rdatatype_srv && trdataset != NULL) {
/*
* If we're adding SRV records to the additional data
* section, it's helpful if we add the SRV additional data
* as well.
*/
eresult = dns_rdataset_additionaldata(trdataset,
query_addadditional,
client);
}
/*
* If we added an A6 rdataset, we should also add everything we
* know about the A6 chains. We wait until now to do this so that
* they'll come after any additional data added above.
*/
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);
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;
/*
* Add an rrset to the additional data section.
*/
REQUIRE(NS_CLIENT_VALID(client));
REQUIRE(rdataset->type == dns_rdatatype_a6);
/*
* Get some resources...
*/
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);
csigrdataset = query_newrdataset(client);
if (fname == NULL || crdataset == NULL || csigrdataset == NULL)
goto cleanup;
if (dns_name_concatenate(name, NULL, fname, NULL) != ISC_R_SUCCESS)
goto cleanup;
dns_rdataset_clone(rdataset, crdataset);
if (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;
/*
* Note: we only add SIGs if we've added the type they cover, so
* we do not need to check if the SIG rdataset is already in the
* response.
*/
if (dns_rdataset_isassociated(csigrdataset)) {
ISC_LIST_APPEND(fname->list, csigrdataset, link);
csigrdataset = NULL;
}
fname = NULL;
/*
* In spite of RFC 2535 section 3.5, we don't currently try to add
* KEY RRs for the A6 records. It's just too much work.
*/
cleanup:
query_putrdataset(client, &crdataset);
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;
/*
* Add 'rdataset' and any pertinent additional data to
* 'fname', a name in the response message for 'client'.
*/
CTRACE("query_addrdataset");
ISC_LIST_APPEND(fname->list, rdataset, link);
/*
* Add additional data.
*
* We don't care if dns_a6_foreach or dns_rdataset_additionaldata()
* fail.
*/
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);
/*
* 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 treat A6 records the same way.
*
* We don't care if query_addadditional() fails.
*/
if (type == dns_rdatatype_ns || type == dns_rdatatype_soa ||
type == dns_rdatatype_a || type == dns_rdatatype_aaaa ||
type == dns_rdatatype_a6) {
/*
* XXXRTH We should lower the priority here. Alternatively,
* we could raise the priority of glue records.
*/
(void)query_addadditional(client, fname, dns_rdatatype_key);
}
CTRACE("query_addrdataset: done");
}
#define ANSWERED(rds) (((rds)->attributes & DNS_RDATASETATTR_ANSWERED) != 0)
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;
/*
* To the current response for 'client', add the answer RRset
* '*rdatasetp' and an optional signature set '*sigrdatasetp', with
* owner name '*namep', to section 'section', unless they are
* already there. Also add any pertinent additional data.
*
* If 'dbuf' is not NULL, then '*namep' is the name whose data is
* stored in 'dbuf'. In this case, query_addrrset() guarantees that
* when it returns the name will either have been kept or released.
*/
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) {
/*
* We've already got an RRset of the given name and type.
* There's nothing else to do;
*/
CTRACE("query_addrrset: dns_message_findname succeeded: done");
if (dbuf != NULL)
query_releasename(client, namep);
return;
} else if (result == DNS_R_NXDOMAIN) {
/*
* The name doesn't exist.
*/
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);
}
/*
* Note: we only add SIGs if we've added the type they cover, so
* we do not need to check if the SIG rdataset is already in the
* response.
*/
query_addrdataset(client, mname, rdataset);
*rdatasetp = NULL;
if (sigrdataset != NULL && dns_rdataset_isassociated(sigrdataset)) {
/*
* We have a signature. Add it to the response.
*/
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, *sigrdataset;
CTRACE("query_addsoa");
/*
* Initialization.
*/
eresult = ISC_R_SUCCESS;
name = NULL;
rdataset = NULL;
node = NULL;
dns_fixedname_init(&foundname);
fname = dns_fixedname_name(&foundname);
/*
* Get resources and make 'name' be the database origin.
*/
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);
sigrdataset = query_newrdataset(client);
if (rdataset == NULL || sigrdataset == NULL) {
eresult = DNS_R_SERVFAIL;
goto cleanup;
}
/*
* Find the SOA.
*/
result = dns_db_find(db, name, NULL, dns_rdatatype_soa,
client->query.dboptions, 0, &node,
fname, rdataset, sigrdataset);
if (result != ISC_R_SUCCESS) {
/*
* This is bad. We tried to get the SOA RR at the zone top
* and it didn't work!
*/
eresult = DNS_R_SERVFAIL;
} else {
/*
* Extract the SOA MINIMUM.
*/
dns_rdata_soa_t soa;
dns_rdata_t rdata;
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;
sigrdataset->ttl = 0;
}
/*
* Add the SOA and its SIG to the response, with the
* TTLs adjusted per RFC2308 section 3.
*/
if (rdataset->ttl > soa.minimum)
rdataset->ttl = soa.minimum;
if (sigrdataset->ttl > soa.minimum)
sigrdataset->ttl = soa.minimum;
query_addrrset(client, &name, &rdataset, &sigrdataset, NULL,
DNS_SECTION_AUTHORITY);
}
cleanup:
query_putrdataset(client, &rdataset);
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, *sigrdataset;
CTRACE("query_addns");
/*
* Initialization.
*/
eresult = ISC_R_SUCCESS;
name = NULL;
rdataset = NULL;
node = NULL;
dns_fixedname_init(&foundname);
fname = dns_fixedname_name(&foundname);
/*
* Get resources and make 'name' be the database origin.
*/
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);
sigrdataset = query_newrdataset(client);
if (rdataset == NULL || sigrdataset == NULL) {
CTRACE("query_addns: query_newrdataset failed");
eresult = DNS_R_SERVFAIL;
goto cleanup;
}
/*
* Find the NS rdataset.
*/
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");
/*
* This is bad. We tried to get the NS rdataset at the zone
* top and it didn't work!
*/
eresult = DNS_R_SERVFAIL;
} else {
query_addrrset(client, &name, &rdataset, &sigrdataset, NULL,
DNS_SECTION_AUTHORITY);
}
cleanup:
CTRACE("query_addns: cleanup");
query_putrdataset(client, &rdataset);
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_addcname(ns_client_t *client, dns_name_t *qname, dns_name_t *tname,
dns_ttl_t ttl, dns_name_t **anamep)
{
dns_rdataset_t *rdataset;
dns_rdatalist_t *rdatalist;
dns_rdata_t *rdata;
isc_result_t result;
isc_region_t r;
CTRACE("query_addcname");
/*
* We assume the name data referred to by qname and tname won't
* go away.
*/
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);
dns_name_clone(qname, *anamep);
rdatalist->type = dns_rdatatype_cname;
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 = dns_rdatatype_cname;
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;
/*
* Find the right database.
*/
result = query_getdb(client, client->query.qname, 0, &zone, &db,
&version, &is_zone);
if (result != ISC_R_SUCCESS)
goto cleanup;
db_find:
/*
* We'll need some resources...
*/
dbuf = query_getnamebuf(client);
if (dbuf == NULL)
goto cleanup;
fname = query_newname(client, dbuf, &b);
rdataset = query_newrdataset(client);
sigrdataset = query_newrdataset(client);
if (fname == NULL || rdataset == NULL || sigrdataset == NULL)
goto cleanup;
/*
* Now look for the zonecut.
*/
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)) {
/*
* We found a zonecut in the cache, but our
* zone delegation is better.
*/
use_zone = ISC_TRUE;
}
} else if (result == ISC_R_NOTFOUND && zfname != NULL) {
/*
* We didn't find anything in the cache, but we
* have a zone delegation, so use it.
*/
use_zone = ISC_TRUE;
} else
goto cleanup;
}
if (use_zone) {
query_releasename(client, &fname);
fname = zfname;
zfname = NULL;
/*
* We've already done query_keepname() on
* zfname, so we must set dbuf to NULL to
* prevent query_addrrset() from trying to
* call query_keepname() again.
*/
dbuf = NULL;
query_putrdataset(client, &rdataset);
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->trust == dns_trust_pending))
goto cleanup;
query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
DNS_SECTION_AUTHORITY);
cleanup:
query_putrdataset(client, &rdataset);
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);
query_putrdataset(client, &zsigrdataset);
if (zfname != NULL)
query_releasename(client, &zfname);
dns_db_detach(&zdb);
}
}
static inline isc_result_t
query_checktype(dns_rdatatype_t type) {
/*
* XXXRTH OPT still needs to be added.
* Should get help with this from rdata.c
*/
switch (type) {
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);
}
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;
/*
* Resume a query after recursion.
*/
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) {
/*
* This is the fetch we've been waiting for.
*/
INSIST(devent->fetch == client->query.fetch);
client->query.fetch = NULL;
fetch_cancelled = ISC_FALSE;
/*
* Update client->now.
*/
isc_stdtime_get(&client->now);
} else {
/*
* This is a fetch completion event for a cancelled fetch.
* Clean up and don't resume the find.
*/
fetch_cancelled = ISC_TRUE;
}
INSIST(client->query.fetch == NULL);
client->query.attributes &= ~NS_QUERYATTR_RECURSING;
dns_resolver_destroyfetch(&devent->fetch);
/*
* If this client is shutting down, or this transaction
* has timed out, do not resume the find.
*/
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);
query_putrdataset(client, &devent->sigrdataset);
isc_event_free(&event);
ns_client_next(client, ISC_R_CANCELED);
/*
* This may destroy the client.
*/
ns_client_detach(&client);
} else {
RWLOCK(&ns_g_server->conflock, isc_rwlocktype_read);
dns_zonemgr_lockconf(ns_g_server->zonemgr,
isc_rwlocktype_read);
dns_view_attach(client->view, &client->lockview);
RWLOCK(&client->lockview->conflock, isc_rwlocktype_read);
query_find(client, devent);
RWUNLOCK(&client->lockview->conflock, isc_rwlocktype_read);
dns_view_detach(&client->lockview);
dns_zonemgr_unlockconf(ns_g_server->zonemgr,
isc_rwlocktype_read);
RWUNLOCK(&ns_g_server->conflock, isc_rwlocktype_read);
}
}
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;
/*
* We are about to recurse, which means that this client will
* be unavailable for serving new requests for an indeterminate
* amount of time. If this client is currently responsible
* for handling incoming queries, set up a new client
* object to handle them while we are waiting for a
* response.
*/
if (! client->mortal) {
result = isc_quota_attach(&ns_g_server->recursionquota,
&client->recursionquota);
if (result == ISC_R_SUCCESS)
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);
}
}
/*
* Invoke the resolver.
*/
REQUIRE(nameservers == NULL || nameservers->type == dns_rdatatype_ns);
REQUIRE(client->query.fetch == NULL);
rdataset = query_newrdataset(client);
if (rdataset == NULL)
return (ISC_R_NOMEMORY);
sigrdataset = query_newrdataset(client);
if (rdataset == NULL) {
query_putrdataset(client, &rdataset);
return (ISC_R_NOMEMORY);
}
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) {
/*
* Record that we're waiting for an event. A client which
* is shutting down will not be destroyed until all the
* events have been received.
*/
} else {
query_putrdataset(client, &rdataset);
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;
isc_result_t result;
dns_zone_t *pzone;
isc_boolean_t is_zone;
dns_fixedname_t pfoundname;
/*
* 'name' is at a zone cut. Try to find a KEY for 'name' in
* the deepest ancestor zone of 'name' (if any). If it exists,
* update *zonep, *dbp, *nodep, rdataset, and sigrdataset and
* return ISC_R_SUCCESS. If not, leave them alone and return a
* non-success status.
*/
pzone = NULL;
pdb = NULL;
pnode = NULL;
pversion = NULL;
dns_rdataset_init(&prdataset);
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;
}
result = dns_db_find(pdb, name, pversion, dns_rdatatype_key,
client->query.dboptions,
client->now, &pnode,
dns_fixedname_name(&pfoundname),
&prdataset, &psigrdataset);
if (result == ISC_R_SUCCESS) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
dns_rdataset_clone(&prdataset, rdataset);
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 (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 void
query_find(ns_client_t *client, dns_fetchevent_t *event) {
dns_db_t *db, *zdb;
dns_dbnode_t *node;
dns_rdatatype_t qtype, type;
dns_name_t *fname, *zfname, *tname, *prefix;
dns_rdataset_t *rdataset, *trdataset;
dns_rdataset_t *sigrdataset, *zrdataset, *zsigrdataset;
dns_rdata_t rdata;
dns_rdatasetiter_t *rdsiter;
isc_boolean_t want_restart, authoritative, is_zone;
unsigned int qcount, n, nlabels, nbits;
dns_namereln_t namereln;
int order;
isc_buffer_t *dbuf;
isc_region_t r;
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");
/*
* One-time initialization.
*
* It's especially important to initialize anything that the cleanup
* code might cleanup.
*/
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) {
/*
* We're returning from recursion. Restore the query context
* and resume.
*/
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;
/*
* We'll need some resources...
*/
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_concatenate(tname, NULL, fname, NULL);
if (result != ISC_R_SUCCESS) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
result = event->result;
goto resume;
} else
client->query.qrdataset = NULL;
restart:
CTRACE("query_find: restart");
want_restart = ISC_FALSE;
authoritative = ISC_FALSE;
version = NULL;
/*
* First we must find the right database.
*/
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;
/*
* Find the first unanswered type in the question section.
*/
qtype = 0;
qcount = 0;
client->query.qrdataset = NULL;
for (trdataset = ISC_LIST_HEAD(client->query.origqname->list);
trdataset != NULL;
trdataset = ISC_LIST_NEXT(trdataset, link)) {
if (!ANSWERED(trdataset)) {
if (client->query.qrdataset == NULL) {
client->query.qrdataset = trdataset;
qtype = trdataset->type;
}
qcount++;
}
}
/*
* We had better have found something!
*/
INSIST(client->query.qrdataset != NULL && qcount > 0);
/*
* If there's more than one question, we'll eventually retrieve the
* node and iterate it, trying to find answers. For now, we simply
* refuse requests with more than one question.
*/
if (qcount == 1)
type = qtype;
else {
CTRACE("find_query: REFUSED: qcount != 1");
QUERY_ERROR(DNS_R_REFUSED);
goto cleanup;
}
/*
* See if the type is OK.
*/
result = query_checktype(qtype);
if (result != ISC_R_SUCCESS) {
CTRACE("find_query: non supported query type");
QUERY_ERROR(result);
goto cleanup;
}
/*
* If it's a SIG query, we'll iterate the node.
*/
if (qtype == dns_rdatatype_sig)
type = dns_rdatatype_any;
db_find:
CTRACE("query_find: db_find");
/*
* We'll need some resources...
*/
dbuf = query_getnamebuf(client);
if (dbuf == NULL) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
fname = query_newname(client, dbuf, &b);
rdataset = query_newrdataset(client);
sigrdataset = query_newrdataset(client);
if (fname == NULL || rdataset == NULL || sigrdataset == NULL) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
/*
* Now look for an answer in the database.
*/
result = dns_db_find(db, client->query.qname, version, type,
client->query.dboptions, client->now,
&node, fname, rdataset, sigrdataset);
/*
* We interrupt our normal query processing to bring you this special
* case...
*
* RFC 2535 (DNSSEC), section 2.3.4, discusses various special
* cases that can occur at delegation points.
*
* One of these cases is that the NULL KEY for an unsecure zone
* may occur in the delegating zone instead of in the delegated zone.
* If we're authoritative for both zones, we need to look for the
* key in the delegator if we didn't find it in the delegatee. If
* we didn't do this, a client doing DNSSEC validation could fail
* because it couldn't get the NULL KEY.
*/
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))) {
/*
* We're looking for a KEY at the top of an unsecure zone,
* and we didn't find it.
*/
result = query_findparentkey(client, client->query.qname,
&zone, &db, &version, &node,
rdataset, sigrdataset);
if (result == ISC_R_SUCCESS) {
/*
* We found the parent KEY.
*
* zone, db, version, node, rdataset, and sigrdataset
* have all been updated to refer to the parent's
* data. We will resume query processing as if
* we had looked for the KEY in the parent zone in
* the first place.
*
* We need to set fname correctly. We do this here
* instead of in query_findparentkey() because
* dns_name_concatenate() can fail (though it shouldn't
* ever do so since we should have enough space).
*/
result = dns_name_concatenate(client->query.qname,
NULL, fname, NULL);
if (result != ISC_R_SUCCESS) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
} else {
/*
* We couldn't find the KEY in a parent zone.
* Continue with processing of the original
* results of dns_db_find().
*/
result = DNS_R_NXRRSET;
}
}
resume:
CTRACE("query_find: resume");
switch (result) {
case ISC_R_SUCCESS:
/*
* This case is handled in the main line below.
*/
break;
case DNS_R_GLUE:
case DNS_R_ZONECUT:
/*
* These cases are handled in the main line below.
*/
INSIST(is_zone);
authoritative = ISC_FALSE;
break;
case ISC_R_NOTFOUND:
/*
* The cache doesn't even have the root NS. Get them from
* the hints DB.
*/
INSIST(!is_zone);
INSIST(client->view->hints != NULL);
if (db != NULL)
dns_db_detach(&db);
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) {
/*
* We can't even find the hints for the root
* nameservers!
*/
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
/*
* XXXRTH We should trigger root server priming here.
*/
/* FALLTHROUGH */
case DNS_R_DELEGATION:
authoritative = ISC_FALSE;
if (is_zone) {
/*
* We're authoritative for an ancestor of QNAME.
*/
if (!USECACHE(client) || !RECURSIONOK(client)) {
/*
* If we don't have a cache, this is the best
* answer.
*
* If the client is making a nonrecursive
* query we always give out the authoritative
* delegation. This way even if we get
* junk in our cache, we won't fail in our
* role as the delegating authority if another
* nameserver asks us about a delegated
* subzone.
*
* We enable the retrieval of glue for this
* database by setting client->query.gluedb.
*/
client->query.gluedb = db;
query_addrrset(client, &fname, &rdataset,
&sigrdataset, dbuf,
DNS_SECTION_AUTHORITY);
client->query.gluedb = NULL;
} else {
/*
* We might have a better answer or delegation
* in the cache. We'll remember the current
* values of fname, rdataset, and sigrdataset.
* We'll then go looking for QNAME in the
* cache. If we find something better, we'll
* use it instead.
*/
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)) {
/*
* We've already got a delegation from
* authoritative data, and it is better
* than what we found in the cache. Use
* it instead of the cache delegation.
*/
query_releasename(client, &fname);
fname = zfname;
zfname = NULL;
/*
* We've already done query_keepname() on
* zfname, so we must set dbuf to NULL to
* prevent query_addrrset() from trying to
* call query_keepname() again.
*/
dbuf = NULL;
query_putrdataset(client, &rdataset);
query_putrdataset(client, &sigrdataset);
rdataset = zrdataset;
zrdataset = NULL;
sigrdataset = zsigrdataset;
zsigrdataset = NULL;
/*
* We don't clean up zdb here because we
* may still need it. It will get cleaned
* up by the main cleanup code.
*/
}
if (RECURSIONOK(client)) {
/*
* Recurse!
*/
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 {
/*
* This is the best answer.
*/
client->query.gluedb = zdb;
client->query.attributes |=
NS_QUERYATTR_CACHEGLUEOK;
query_addrrset(client, &fname,
&rdataset, &sigrdataset,
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)) {
/*
* If we've got a NXT record, we need to save the
* name now because we're going call query_addsoa()
* below, and it needs to use the name buffer.
*/
query_keepname(client, fname, dbuf);
/*
* We don't want the cleanup code to try to release
* fname if we fail below, so we set it to NULL.
*/
tname = fname;
fname = NULL;
} else {
/*
* We're not going to use fname, and need to release
* our hold on the name buffer so query_addsoa()
* may use it.
*/
query_releasename(client, &fname);
}
/*
* Add SOA.
*/
result = query_addsoa(client, db, ISC_FALSE);
if (result != ISC_R_SUCCESS) {
QUERY_ERROR(result);
goto cleanup;
}
/*
* Add NXT record if we found one.
*/
if (dns_rdataset_isassociated(rdataset)) {
query_addrrset(client, &tname, &rdataset, &sigrdataset,
NULL, DNS_SECTION_AUTHORITY);
if (tname != NULL)
dns_message_puttempname(client->message,
&tname);
}
goto cleanup;
case DNS_R_NXDOMAIN:
INSIST(is_zone);
if (client->query.restarts > 0) {
/*
* We hit a dead end following a CNAME or DNAME.
*/
goto cleanup;
}
if (dns_rdataset_isassociated(rdataset)) {
/*
* If we've got a NXT record, we need to save the
* name now because we're going call query_addsoa()
* below, and it needs to use the name buffer.
*/
query_keepname(client, fname, dbuf);
/*
* We don't want the cleanup code to try to release
* fname if we fail below, so we set it to NULL.
*/
tname = fname;
fname = NULL;
} else {
/*
* We're not going to use fname, and need to release
* our hold on the name buffer so query_addsoa()
* may use it.
*/
query_releasename(client, &fname);
}
/*
* Add SOA. If the query was for a SOA record force the
* ttl to zero so that it is possible for clients to find
* the containing zone of a arbitary name with a stub
* resolver and not have it cached.
*/
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;
}
/*
* Add NXT record if we found one.
*/
if (dns_rdataset_isassociated(rdataset)) {
query_addrrset(client, &tname, &rdataset, &sigrdataset,
NULL, DNS_SECTION_AUTHORITY);
if (tname != NULL)
dns_message_puttempname(client->message,
&tname);
}
/*
* Set message rcode.
*/
client->message->rcode = dns_rcode_nxdomain;
goto cleanup;
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
INSIST(!is_zone);
authoritative = ISC_FALSE;
/*
* Set message rcode, if required.
*/
if (result == DNS_R_NCACHENXDOMAIN)
client->message->rcode = dns_rcode_nxdomain;
/*
* We don't call query_addrrset() because we don't need any
* of its extra features (and things would probably break!).
*/
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:
/*
* 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).
*/
trdataset = rdataset;
/*
* Add the CNAME to the answer section.
*/
query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
DNS_SECTION_ANSWER);
/*
* We set the PARTIALANSWER attribute so that if anything goes
* wrong later on, we'll return what we've got so far.
*/
client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
/*
* Reset qname to be the target name of the CNAME and restart
* the query.
*/
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)
goto cleanup;
dns_rdataset_current(trdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &cname, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_name_init(tname, NULL);
dns_name_clone(&cname.cname, tname);
dns_rdata_freestruct(&cname);
query_maybeputqname(client);
client->query.qname = tname;
want_restart = ISC_TRUE;
goto addauth;
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.
*/
namereln = dns_name_fullcompare(client->query.qname, fname,
&order, &nlabels, &nbits);
INSIST(namereln == dns_namereln_subdomain);
/*
* 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).
*/
trdataset = rdataset;
/*
* Add the DNAME to the answer section.
*/
query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
DNS_SECTION_ANSWER);
/*
* We set the PARTIALANSWER attribute so that if anything goes
* wrong later on, we'll return what we've got so far.
*/
client->query.attributes |= NS_QUERYATTR_PARTIALANSWER;
/*
* Get the target name of the DNAME.
*/
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)
goto cleanup;
dns_rdataset_current(trdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &dname, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_name_init(tname, NULL);
dns_name_clone(&dname.dname, tname);
dns_rdata_freestruct(&dname);
/*
* Construct the new qname.
*/
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)
goto cleanup;
INSIST(fname == NULL);
dbuf = query_getnamebuf(client);
if (dbuf == NULL)
goto cleanup;
fname = query_newname(client, dbuf, &b);
if (fname == NULL)
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) {
/*
* RFC 2672, section 4.1, subsection 3c says
* we should return YXDOMAIN if the constructed
* name would be too long.
*/
client->message->rcode = dns_rcode_yxdomain;
}
goto cleanup;
}
query_keepname(client, fname, dbuf);
/*
* Synthesize a CNAME for this DNAME.
*
* We want to synthesize a CNAME since if we don't
* then older software that doesn't understand DNAME
* will not chain like it should.
*
* We do not try to synthesize a signature because we hope
* that security aware servers will understand DNAME. Also,
* even if we had an online key, making a signature
* on-the-fly is costly, and not really legitimate anyway
* since the synthesized CNAME is NOT in the zone.
*/
dns_name_init(tname, NULL);
query_addcname(client, client->query.qname, fname,
trdataset->ttl, &tname);
if (tname != NULL)
dns_message_puttempname(client->message, &tname);
/*
* Switch to the new qname and restart.
*/
query_maybeputqname(client);
client->query.qname = fname;
fname = NULL;
want_restart = ISC_TRUE;
goto addauth;
default:
/*
* Something has gone wrong.
*/
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
if (type == dns_rdatatype_any) {
/*
* XXXRTH Need to handle zonecuts with special case
* code.
*/
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;
}
/*
* Calling query_addrrset() with a non-NULL dbuf is going
* to either keep or release the name. We don't want it to
* release fname, since we may have to call query_addrrset()
* more than once. That means we have to call query_keepname()
* now, and pass a NULL dbuf to query_addrrset().
*
* Since we've done the keepname, it's important that we
* set fname to NULL before we leave this 'if' block
* otherwise we might try to cleanup fname even though we've
* kept it!
*/
query_keepname(client, fname, dbuf);
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) {
tname = fname;
query_addrrset(client, &tname, &rdataset, NULL,
NULL, DNS_SECTION_ANSWER);
n++;
/*
* We shouldn't ever fail to add 'rdataset'
* because it's already in the answer.
*/
INSIST(rdataset == NULL);
rdataset = query_newrdataset(client);
if (rdataset == NULL)
break;
} else {
/*
* We're not interested in this rdataset.
*/
dns_rdataset_disassociate(rdataset);
}
result = dns_rdatasetiter_next(rdsiter);
}
/*
* As mentioned above, we must now clear fname since we have
* kept it.
*/
fname = NULL;
if (n == 0) {
/*
* We didn't match any rdatasets.
*/
if (qtype == dns_rdatatype_sig &&
result == ISC_R_NOMORE) {
/*
* XXXRTH If this is a secure zone and we
* didn't find any SIGs, we should generate
* an error unless we were searching for
* glue. Ugh.
*/
/*
* We were searching for SIG records in
* a nonsecure zone. Send a "no error,
* no data" response.
*/
/*
* Add SOA.
*/
result = query_addsoa(client, db, ISC_FALSE);
if (result == ISC_R_SUCCESS)
result = ISC_R_NOMORE;
} else {
/*
* Something went wrong.
*/
result = DNS_R_SERVFAIL;
}
}
dns_rdatasetiter_destroy(&rdsiter);
if (result != ISC_R_NOMORE) {
QUERY_ERROR(DNS_R_SERVFAIL);
goto cleanup;
}
} else {
/*
* This is the "normal" case -- an ordinary question to which
* we know the answer.
*/
query_addrrset(client, &fname, &rdataset, &sigrdataset, dbuf,
DNS_SECTION_ANSWER);
/*
* We shouldn't ever fail to add 'rdataset'
* because it's already in the answer.
*/
INSIST(rdataset == NULL);
/*
* Remember that we've answered this question.
*/
client->query.qrdataset->attributes |=
DNS_RDATASETATTR_ANSWERED;
}
addauth:
CTRACE("query_find: addauth");
/*
* Add NS records to the authority section (if we haven't already
* added them to the answer section).
*/
if (!want_restart) {
if (is_zone) {
if (!(qtype == dns_rdatatype_ns &&
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");
/*
* General cleanup.
*/
query_putrdataset(client, &rdataset);
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);
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));
/*
* AA bit.
*/
if (client->query.restarts == 0 && !authoritative) {
/*
* We're not authoritative, so we must ensure the AA bit
* isn't set.
*/
client->message->flags &= ~DNS_MESSAGEFLAG_AA;
}
/*
* Restart the query?
*/
if (want_restart && client->query.restarts < MAX_RESTARTS) {
client->query.restarts++;
goto restart;
}
if (eresult != ISC_R_SUCCESS &&
(!PARTIALANSWER(client) || WANTRECURSION(client))) {
/*
* If we don't have any answer to give the client,
* or if the client requested recursion and thus wanted
* the complete answer, send an error response.
*/
ns_client_error(client, eresult);
ns_client_detach(&client);
} else if (!RECURSING(client)) {
/*
* We are done. Make a final tweak to the AA bit if the
* auth-nxdomain config option says so, then send the
* response.
*/
if (client->message->rcode == dns_rcode_nxdomain &&
client->view->auth_nxdomain == ISC_TRUE)
client->message->flags |= DNS_MESSAGEFLAG_AA;
ns_client_send(client);
ns_client_detach(&client);
}
CTRACE("query_find: done");
}
static inline void
log_query(ns_client_t *client) {
isc_buffer_t b;
char namebuf[DNS_NAME_FORMATSIZE];
char text[256];
isc_region_t r;
dns_rdataset_t *rdataset;
int level = ISC_LOG_DEBUG(1);
if (! isc_log_wouldlog(ns_g_lctx, level))
return;
/* XXXRTH Allow this to be turned off! */
dns_name_format(client->query.qname, namebuf, sizeof(namebuf));
isc_buffer_init(&b, (unsigned char *)text, sizeof(text));
for (rdataset = ISC_LIST_HEAD(client->query.qname->list);
rdataset != NULL;
rdataset = ISC_LIST_NEXT(rdataset, link)) {
isc_buffer_availableregion(&b, &r);
if (r.length < 1)
return;
*r.base = ' ';
isc_buffer_add(&b, 1);
if (dns_rdatatype_totext(rdataset->type, &b) != ISC_R_SUCCESS)
return;
}
isc_buffer_usedregion(&b, &r);
ns_client_log(client, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_QUERY,
level, "query: %s%.*s", namebuf,
(int)r.length, (char *)r.base);
}
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;
CTRACE("ns_query_start");
/*
* Ensure that appropriate cleanups occur.
*/
client->next = query_next;
if ((message->flags & DNS_MESSAGEFLAG_RD) != 0)
client->query.attributes |= NS_QUERYATTR_WANTRECURSION;
if ((client->view->cachedb == NULL)
|| (!client->view->additionalfromcache)) {
/*
* We don't have a cache. Turn off cache support and
* recursion.
*/
client->query.attributes &=
~(NS_QUERYATTR_RECURSIONOK|NS_QUERYATTR_CACHEOK);
} else if ((client->attributes & NS_CLIENTATTR_RA) == 0 ||
(message->flags & DNS_MESSAGEFLAG_RD) == 0) {
/*
* If the client isn't allowed to recurse (due to
* "recursion no", the allow-recursion ACL, or the
* lack of a resolver in this view), or if it
* doesn't want recursion, turn recursion off.
*/
client->query.attributes &= ~NS_QUERYATTR_RECURSIONOK;
}
/*
* Get the question name.
*/
result = dns_message_firstname(message, DNS_SECTION_QUESTION);
if (result != ISC_R_SUCCESS) {
ns_client_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) {
/*
* There's more than one QNAME in the question
* section.
*/
ns_client_error(client, DNS_R_FORMERR);
} else
ns_client_error(client, result);
return;
}
log_query(client);
/*
* Check for multiple question queries, since edns1 is dead.
*/
if (message->counts[DNS_SECTION_QUESTION] > 1) {
ns_client_error(client, DNS_R_FORMERR);
return;
}
/*
* Check for meta-queries like IXFR and AXFR.
*/
rdataset = ISC_LIST_HEAD(client->query.qname->list);
INSIST(rdataset != NULL);
if (dns_rdatatype_ismeta(rdataset->type)) {
switch (rdataset->type) {
case dns_rdatatype_any:
break; /* Let query_find handle it. */
case dns_rdatatype_ixfr:
case dns_rdatatype_axfr:
ns_xfr_start(client, rdataset->type);
return;
case dns_rdatatype_maila:
case dns_rdatatype_mailb:
ns_client_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)
ns_client_send(client);
else
ns_client_error(client, result);
return;
default: /* TSIG, etc. */
ns_client_error(client, DNS_R_FORMERR);
return;
}
}
/*
* If the client has requested that DNSSEC checking be disabled,
* allow lookups to return pending data and instruct the resolver
* to return data before validation has completed.
*/
if (message->flags & DNS_MESSAGEFLAG_CD) {
client->query.dboptions |= DNS_DBFIND_PENDINGOK;
client->query.fetchoptions |= DNS_FETCHOPT_NOVALIDATE;
}
/*
* This is an ordinary query.
*/
result = dns_message_reply(message, ISC_TRUE);
if (result != ISC_R_SUCCESS) {
ns_client_next(client, result);
return;
}
/*
* Assume authoritative response until it is known to be
* otherwise.
*/
message->flags |= DNS_MESSAGEFLAG_AA;
/*
* Set AD. We need only clear it if we add "pending" data to
* a response.
*/
message->flags |= DNS_MESSAGEFLAG_AD;
qclient = NULL;
ns_client_attach(client, &qclient);
query_find(qclient, NULL);
}