query.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dns/message.h>
  15. #include <dns/rcode.h>
  16. #include <dns/rrtype.h>
  17. #include <dns/rrset.h>
  18. #include <dns/rdataclass.h>
  19. #include <datasrc/client.h>
  20. #include <datasrc/client_list.h>
  21. #include <auth/query.h>
  22. #include <boost/foreach.hpp>
  23. #include <boost/bind.hpp>
  24. #include <boost/function.hpp>
  25. #include <cassert>
  26. #include <algorithm> // for std::max
  27. #include <functional>
  28. #include <vector>
  29. using namespace std;
  30. using namespace isc::dns;
  31. using namespace isc::datasrc;
  32. using namespace isc::dns::rdata;
  33. // This is a "constant" vector storing desired RR types for the additional
  34. // section. The vector is filled first time it's used.
  35. namespace {
  36. const vector<RRType>&
  37. A_AND_AAAA() {
  38. static vector<RRType> needed_types;
  39. if (needed_types.empty()) {
  40. needed_types.push_back(RRType::A());
  41. needed_types.push_back(RRType::AAAA());
  42. }
  43. return (needed_types);
  44. }
  45. }
  46. namespace isc {
  47. namespace auth {
  48. void
  49. Query::ResponseCreator::addRRset(isc::dns::Message& message,
  50. const isc::dns::Message::Section section,
  51. const ConstRRsetPtr& rrset)
  52. {
  53. /// Is this RRset already in the list of RRsets added to the message?
  54. const std::vector<const AbstractRRset*>::const_iterator i =
  55. std::find_if(added_.begin(), added_.end(),
  56. std::bind1st(Query::ResponseCreator::IsSameKind(),
  57. rrset.get()));
  58. if (i == added_.end()) {
  59. // No - add it to both the message and the list of RRsets processed.
  60. // The const-cast is wrong, but the message interface seems to insist.
  61. message.addRRset(section,
  62. boost::const_pointer_cast<AbstractRRset>(rrset));
  63. added_.push_back(rrset.get());
  64. }
  65. }
  66. void
  67. Query::ResponseCreator::create(Message& response,
  68. const vector<ConstRRsetPtr>& answers,
  69. const vector<ConstRRsetPtr>& authorities,
  70. const vector<ConstRRsetPtr>& additionals)
  71. {
  72. // Inserter should be reset each time the query is reset, so should be
  73. // empty at this point.
  74. assert(added_.empty());
  75. // Add the RRsets to the message. The order of sections is important,
  76. // as the ResponseCreator remembers RRsets added and will not add
  77. // duplicates. Adding in the order answer, authory, additional will
  78. // guarantee that if there are duplicates, the single RRset added will
  79. // appear in the most important section.
  80. BOOST_FOREACH(const ConstRRsetPtr& rrset, answers) {
  81. addRRset(response, Message::SECTION_ANSWER, rrset);
  82. }
  83. BOOST_FOREACH(const ConstRRsetPtr& rrset, authorities) {
  84. addRRset(response, Message::SECTION_AUTHORITY, rrset);
  85. }
  86. BOOST_FOREACH(const ConstRRsetPtr& rrset, additionals) {
  87. addRRset(response, Message::SECTION_ADDITIONAL, rrset);
  88. }
  89. }
  90. void
  91. Query::addSOA(ZoneFinder& finder) {
  92. // This method is always called in finding SOA for a negative response,
  93. // so we specify the use of min(RRTTL, SOA MINTTL) as specified in
  94. // Section 3 of RFC2308.
  95. ZoneFinderContextPtr soa_ctx = finder.findAtOrigin(RRType::SOA(), true,
  96. dnssec_opt_);
  97. if (soa_ctx->code != ZoneFinder::SUCCESS) {
  98. isc_throw(NoSOA, "There's no SOA record in zone " <<
  99. finder.getOrigin().toText());
  100. } else {
  101. authorities_.push_back(soa_ctx->rrset);
  102. }
  103. }
  104. // Note: unless the data source client implementation or the zone content
  105. // is broken, 'nsec' should be a valid NSEC RR. Likewise, the call to
  106. // find() in this method should result in NXDOMAIN and an NSEC RR that proves
  107. // the non existent of matching wildcard. If these assumptions aren't met
  108. // due to a buggy data source implementation or a broken zone, we'll let
  109. // underlying libdns++ modules throw an exception, which would result in
  110. // either an SERVFAIL response or just ignoring the query. We at least prevent
  111. // a complete crash due to such broken behavior.
  112. void
  113. Query::addNXDOMAINProofByNSEC(ZoneFinder& finder, ConstRRsetPtr nsec) {
  114. if (nsec->getRdataCount() == 0) {
  115. isc_throw(BadNSEC, "NSEC for NXDOMAIN is empty");
  116. }
  117. // Add the NSEC proving NXDOMAIN to the authority section.
  118. authorities_.push_back(nsec);
  119. // Next, identify the best possible wildcard name that would match
  120. // the query name. It's the longer common suffix with the qname
  121. // between the owner or the next domain of the NSEC that proves NXDOMAIN,
  122. // prefixed by the wildcard label, "*". For example, for query name
  123. // a.b.example.com, if the NXDOMAIN NSEC is
  124. // b.example.com. NSEC c.example.com., the longer suffix is b.example.com.,
  125. // and the best possible wildcard is *.b.example.com. If the NXDOMAIN
  126. // NSEC is a.example.com. NSEC c.b.example.com., the longer suffix
  127. // is the next domain of the NSEC, and we get the same wildcard name.
  128. const int qlabels = qname_->getLabelCount();
  129. const int olabels = qname_->compare(nsec->getName()).getCommonLabels();
  130. const int nlabels = qname_->compare(
  131. dynamic_cast<const generic::NSEC&>(nsec->getRdataIterator()->
  132. getCurrent()).
  133. getNextName()).getCommonLabels();
  134. const int common_labels = std::max(olabels, nlabels);
  135. const Name wildname(Name("*").concatenate(qname_->split(qlabels -
  136. common_labels)));
  137. // Confirm the wildcard doesn't exist (this should result in NXDOMAIN;
  138. // otherwise we shouldn't have got NXDOMAIN for the original query in
  139. // the first place).
  140. ConstZoneFinderContextPtr fcontext =
  141. finder.find(wildname, RRType::NSEC(), dnssec_opt_);
  142. if (fcontext->code != ZoneFinder::NXDOMAIN || !fcontext->rrset ||
  143. fcontext->rrset->getRdataCount() == 0) {
  144. isc_throw(BadNSEC, "Unexpected result for wildcard NXDOMAIN proof");
  145. }
  146. // Add the (no-) wildcard proof. This can be the same NSEC we already
  147. // added, but we'd add it here anyway; duplicate checks will take place
  148. // later in a unified manner.
  149. authorities_.push_back(fcontext->rrset);
  150. }
  151. uint8_t
  152. Query::addClosestEncloserProof(ZoneFinder& finder, const Name& name,
  153. bool exact_ok, bool add_closest)
  154. {
  155. const ZoneFinder::FindNSEC3Result result = finder.findNSEC3(name, true);
  156. // Validity check (see the method description). Note that a completely
  157. // broken findNSEC3 implementation could even return NULL RRset in
  158. // closest_proof. We don't explicitly check such case; addRRset() will
  159. // throw an exception, and it will be converted to SERVFAIL at the caller.
  160. if (!exact_ok && !result.next_proof) {
  161. isc_throw(BadNSEC3, "Matching NSEC3 found for a non existent name: "
  162. << qname_);
  163. }
  164. if (add_closest) {
  165. authorities_.push_back(result.closest_proof);
  166. }
  167. if (result.next_proof) {
  168. authorities_.push_back(result.next_proof);
  169. }
  170. return (result.closest_labels);
  171. }
  172. void
  173. Query::addNSEC3ForName(ZoneFinder& finder, const Name& name, bool match) {
  174. const ZoneFinder::FindNSEC3Result result = finder.findNSEC3(name, false);
  175. // See the comment for addClosestEncloserProof(). We don't check a
  176. // totally bogus case where closest_proof is NULL here.
  177. if (match != result.matched) {
  178. isc_throw(BadNSEC3, "Unexpected "
  179. << (result.matched ? "matching" : "covering")
  180. << " NSEC3 found for " << name);
  181. }
  182. authorities_.push_back(result.closest_proof);
  183. }
  184. void
  185. Query::addNXDOMAINProofByNSEC3(ZoneFinder& finder) {
  186. // Firstly get the NSEC3 proves for Closest Encloser Proof
  187. // See Section 7.2.1 of RFC 5155.
  188. const uint8_t closest_labels =
  189. addClosestEncloserProof(finder, *qname_, false);
  190. // Next, construct the wildcard name at the closest encloser, i.e.,
  191. // '*' followed by the closest encloser, and add NSEC3 for it.
  192. const Name wildname(Name("*").concatenate(
  193. qname_->split(qname_->getLabelCount() - closest_labels)));
  194. addNSEC3ForName(finder, wildname, false);
  195. }
  196. void
  197. Query::addWildcardProof(ZoneFinder& finder,
  198. const ZoneFinder::Context& db_context)
  199. {
  200. if (db_context.isNSECSigned()) {
  201. // Case for RFC4035 Section 3.1.3.3.
  202. //
  203. // The query name shouldn't exist in the zone if there were no wildcard
  204. // substitution. Confirm that by specifying NO_WILDCARD. It should
  205. // result in NXDOMAIN and an NSEC RR that proves it should be returned.
  206. ConstZoneFinderContextPtr fcontext =
  207. finder.find(*qname_, RRType::NSEC(),
  208. dnssec_opt_ | ZoneFinder::NO_WILDCARD);
  209. if (fcontext->code != ZoneFinder::NXDOMAIN || !fcontext->rrset ||
  210. fcontext->rrset->getRdataCount() == 0) {
  211. isc_throw(BadNSEC,
  212. "Unexpected NSEC result for wildcard proof");
  213. }
  214. authorities_.push_back(fcontext->rrset);
  215. } else if (db_context.isNSEC3Signed()) {
  216. // Case for RFC 5155 Section 7.2.6.
  217. //
  218. // Note that the closest encloser must be the immediate ancestor
  219. // of the matching wildcard, so NSEC3 for its next closer (and only
  220. // that NSEC3) is what we are expected to provided per the RFC (if
  221. // this assumption isn't met the zone is broken anyway).
  222. addClosestEncloserProof(finder, *qname_, false, false);
  223. }
  224. }
  225. void
  226. Query::addWildcardNXRRSETProof(ZoneFinder& finder, ConstRRsetPtr nsec) {
  227. // There should be one NSEC RR which was found in the zone to prove
  228. // that there is not matched <QNAME,QTYPE> via wildcard expansion.
  229. if (nsec->getRdataCount() == 0) {
  230. isc_throw(BadNSEC, "NSEC for WILDCARD_NXRRSET is empty");
  231. }
  232. ConstZoneFinderContextPtr fcontext =
  233. finder.find(*qname_, RRType::NSEC(),
  234. dnssec_opt_ | ZoneFinder::NO_WILDCARD);
  235. if (fcontext->code != ZoneFinder::NXDOMAIN || !fcontext->rrset ||
  236. fcontext->rrset->getRdataCount() == 0) {
  237. isc_throw(BadNSEC, "Unexpected result for no match QNAME proof");
  238. }
  239. authorities_.push_back(fcontext->rrset);
  240. }
  241. void
  242. Query::addDS(ZoneFinder& finder, const Name& dname) {
  243. ConstZoneFinderContextPtr ds_context =
  244. finder.find(dname, RRType::DS(), dnssec_opt_);
  245. if (ds_context->code == ZoneFinder::SUCCESS) {
  246. authorities_.push_back(ds_context->rrset);
  247. } else if (ds_context->code == ZoneFinder::NXRRSET &&
  248. ds_context->isNSECSigned()) {
  249. addNXRRsetProof(finder, *ds_context);
  250. } else if (ds_context->code == ZoneFinder::NXRRSET &&
  251. ds_context->isNSEC3Signed()) {
  252. // Add no DS proof with NSEC3 as specified in RFC 5155 Section 7.2.7.
  253. addClosestEncloserProof(finder, dname, true);
  254. } else if (ds_context->code != ZoneFinder::NXRRSET) {
  255. // We know this domain should exist, so the result must be NXRRSET.
  256. // If not, the zone is broken, so we'll return SERVFAIL by triggering
  257. // an exception.
  258. isc_throw(BadDS, "Unexpected result for DS lookup for delegation");
  259. }
  260. }
  261. void
  262. Query::addNXRRsetProof(ZoneFinder& finder,
  263. const ZoneFinder::Context& db_context)
  264. {
  265. if (db_context.isNSECSigned() && db_context.rrset) {
  266. authorities_.push_back(db_context.rrset);
  267. if (db_context.isWildcard()) {
  268. addWildcardNXRRSETProof(finder, db_context.rrset);
  269. }
  270. } else if (db_context.isNSEC3Signed() && !db_context.isWildcard()) {
  271. // Section 7.2.3 and 7.2.4 of RFC 5155 with clarification by errata
  272. // http://www.rfc-editor.org/errata_search.php?rfc=5155&eid=3441
  273. // In the end, these two cases are basically the same: if the qname is
  274. // equal to or derived from insecure delegation covered by an Opt-Out
  275. // NSEC3 RR, include the closest provable encloser proof; otherwise we
  276. // have a matching NSEC3, so we include it.
  277. //
  278. // Note: This implementation does not check in the former case whether
  279. // the NSEC3 for the next closer has Opt-Out bit on; this must be the
  280. // case as long as the zone is correctly signed, and if it's broken
  281. // we'd just return what we are given and have the validator detect it.
  282. addClosestEncloserProof(finder, *qname_, true);
  283. } else if (db_context.isNSEC3Signed() && db_context.isWildcard()) {
  284. // Case for RFC 5155 Section 7.2.5: add closest encloser proof for the
  285. // qname, construct the matched wildcard name and add NSEC3 for it.
  286. const uint8_t closest_labels =
  287. addClosestEncloserProof(finder, *qname_, false);
  288. const Name wname = Name("*").concatenate(
  289. qname_->split(qname_->getLabelCount() - closest_labels));
  290. addNSEC3ForName(finder, wname, true);
  291. }
  292. }
  293. void
  294. Query::addAuthAdditional(ZoneFinder& finder,
  295. vector<ConstRRsetPtr>& additionals)
  296. {
  297. // Fill in authority and addtional sections.
  298. ConstZoneFinderContextPtr ns_context =
  299. finder.findAtOrigin(RRType::NS(), false, dnssec_opt_);
  300. // zone origin name should have NS records
  301. if (ns_context->code != ZoneFinder::SUCCESS) {
  302. isc_throw(NoApexNS, "There's no apex NS records in zone " <<
  303. finder.getOrigin().toText());
  304. }
  305. authorities_.push_back(ns_context->rrset);
  306. ns_context->getAdditional(A_AND_AAAA(), additionals);
  307. }
  308. namespace {
  309. // A simple wrapper for DataSourceClient::findZone(). Normally we can simply
  310. // check the closest zone to the qname, but for type DS query we need to
  311. // look into the parent zone. Nevertheless, if there is no "parent" (i.e.,
  312. // the qname consists of a single label, which also means it's the root name),
  313. // we should search the deepest zone we have (which should be the root zone;
  314. // otherwise it's a query error).
  315. ClientList::FindResult
  316. findZone(const ClientList& list, const Name& qname, RRType qtype) {
  317. if (qtype != RRType::DS() || qname.getLabelCount() == 1) {
  318. return (list.find(qname));
  319. }
  320. return (list.find(qname.split(1)));
  321. }
  322. }
  323. void
  324. Query::process(datasrc::ClientList& client_list,
  325. const isc::dns::Name& qname, const isc::dns::RRType& qtype,
  326. isc::dns::Message& response, bool dnssec)
  327. {
  328. // Set up the cleaner object so internal pointers and vectors are
  329. // always reset after scope leaves this method
  330. QueryCleaner cleaner(*this);
  331. // Set up query parameters for the rest of the (internal) methods
  332. initialize(client_list, qname, qtype, response, dnssec);
  333. // Found a zone which is the nearest ancestor to QNAME
  334. const ClientList::FindResult result = findZone(*client_list_, *qname_,
  335. *qtype_);
  336. // If we have no matching authoritative zone for the query name, return
  337. // REFUSED. In short, this is to be compatible with BIND 9, but the
  338. // background discussion is not that simple. See the relevant topic
  339. // at the BIND 10 developers's ML:
  340. // https://lists.isc.org/mailman/htdig/bind10-dev/2010-December/001633.html
  341. if (result.dsrc_client_ == NULL) {
  342. // If we tried to find a "parent zone" for a DS query and failed,
  343. // we may still have authority at the child side. If we do, the query
  344. // has to be handled there.
  345. if (*qtype_ == RRType::DS() && qname_->getLabelCount() > 1 &&
  346. processDSAtChild()) {
  347. return;
  348. }
  349. response_->setHeaderFlag(Message::HEADERFLAG_AA, false);
  350. response_->setRcode(Rcode::REFUSED());
  351. return;
  352. }
  353. ZoneFinder& zfinder = *result.finder_;
  354. // We have authority for a zone that contain the query name (possibly
  355. // indirectly via delegation). Look into the zone.
  356. response_->setHeaderFlag(Message::HEADERFLAG_AA);
  357. response_->setRcode(Rcode::NOERROR());
  358. boost::function0<ZoneFinderContextPtr> find;
  359. const bool qtype_is_any = (*qtype_ == RRType::ANY());
  360. if (qtype_is_any) {
  361. find = boost::bind(&ZoneFinder::findAll, &zfinder, *qname_,
  362. boost::ref(answers_), dnssec_opt_);
  363. } else {
  364. find = boost::bind(&ZoneFinder::find, &zfinder, *qname_, *qtype_,
  365. dnssec_opt_);
  366. }
  367. ZoneFinderContextPtr db_context(find());
  368. switch (db_context->code) {
  369. case ZoneFinder::DNAME: {
  370. // First, put the dname into the answer
  371. answers_.push_back(db_context->rrset);
  372. /*
  373. * Empty DNAME should never get in, as it is impossible to
  374. * create one in master file.
  375. *
  376. * FIXME: Other way to prevent this should be done
  377. */
  378. assert(db_context->rrset->getRdataCount() > 0);
  379. // Get the data of DNAME
  380. RdataIteratorPtr rit = db_context->rrset->getRdataIterator();
  381. const rdata::generic::DNAME& dname(
  382. dynamic_cast<const rdata::generic::DNAME&>(rit->getCurrent()));
  383. // The yet unmatched prefix dname
  384. const Name prefix(qname_->split(0, qname_->getLabelCount() -
  385. db_context->rrset->getName().getLabelCount()));
  386. // If we put it together, will it be too long?
  387. // (The prefix contains trailing ., which will be removed
  388. if (prefix.getLength() - Name::ROOT_NAME().getLength() +
  389. dname.getDname().getLength() > Name::MAX_WIRE) {
  390. /*
  391. * In case the synthesized name is too long, section 4.1
  392. * of RFC 2672 mandates we return YXDOMAIN.
  393. */
  394. response_->setRcode(Rcode::YXDOMAIN());
  395. break;
  396. }
  397. // The new CNAME we are creating (it will be unsigned even
  398. // with DNSSEC, the DNAME is signed and it can be validated
  399. // by that)
  400. RRsetPtr cname(new RRset(*qname_, db_context->rrset->getClass(),
  401. RRType::CNAME(), db_context->rrset->getTTL()));
  402. // Construct the new target by replacing the end
  403. cname->addRdata(rdata::generic::CNAME(qname_->split(0,
  404. qname_->getLabelCount() -
  405. db_context->rrset->getName().getLabelCount()).
  406. concatenate(dname.getDname())));
  407. answers_.push_back(cname);
  408. break;
  409. }
  410. case ZoneFinder::CNAME:
  411. /*
  412. * We don't do chaining yet. Therefore handling a CNAME is
  413. * mostly the same as handling SUCCESS, but we didn't get
  414. * what we expected. It means no exceptions in ANY or NS
  415. * on the origin (though CNAME in origin is probably
  416. * forbidden anyway).
  417. *
  418. * So, just put it there.
  419. */
  420. answers_.push_back(db_context->rrset);
  421. // If the answer is a result of wildcard substitution,
  422. // add a proof that there's no closer name.
  423. if (dnssec_ && db_context->isWildcard()) {
  424. addWildcardProof(*result.finder_, *db_context);
  425. }
  426. break;
  427. case ZoneFinder::SUCCESS:
  428. // If query type is ANY, the rrs have already been added
  429. if (!qtype_is_any) {
  430. answers_.push_back(db_context->rrset);
  431. }
  432. // Retrieve additional records for the answer
  433. db_context->getAdditional(A_AND_AAAA(), additionals_);
  434. // If apex NS records haven't been provided in the answer
  435. // section, insert apex NS records into the authority section
  436. // and AAAA/A RRS of each of the NS RDATA into the additional
  437. // section.
  438. // Checking the findZone() is a lightweight check to see if
  439. // qname is the zone origin.
  440. if (!result.exact_match_ ||
  441. db_context->code != ZoneFinder::SUCCESS ||
  442. (*qtype_ != RRType::NS() && !qtype_is_any))
  443. {
  444. addAuthAdditional(*result.finder_, additionals_);
  445. }
  446. // If the answer is a result of wildcard substitution,
  447. // add a proof that there's no closer name.
  448. if (dnssec_ && db_context->isWildcard()) {
  449. addWildcardProof(*result.finder_, *db_context);
  450. }
  451. break;
  452. case ZoneFinder::DELEGATION:
  453. // If a DS query resulted in delegation, we also need to check
  454. // if we are an authority of the child, too. If so, we need to
  455. // complete the process in the child as specified in Section
  456. // 2.2.1.2. of RFC3658.
  457. if (*qtype_ == RRType::DS() && processDSAtChild()) {
  458. return;
  459. }
  460. response_->setHeaderFlag(Message::HEADERFLAG_AA, false);
  461. authorities_.push_back(db_context->rrset);
  462. // Retrieve additional records for the name servers
  463. db_context->getAdditional(A_AND_AAAA(), additionals_);
  464. // If DNSSEC is requested, see whether there is a DS
  465. // record for this delegation.
  466. if (dnssec_) {
  467. addDS(*result.finder_, db_context->rrset->getName());
  468. }
  469. break;
  470. case ZoneFinder::NXDOMAIN:
  471. response_->setRcode(Rcode::NXDOMAIN());
  472. addSOA(*result.finder_);
  473. if (dnssec_) {
  474. if (db_context->isNSECSigned() && db_context->rrset) {
  475. addNXDOMAINProofByNSEC(zfinder, db_context->rrset);
  476. } else if (db_context->isNSEC3Signed()) {
  477. addNXDOMAINProofByNSEC3(zfinder);
  478. }
  479. }
  480. break;
  481. case ZoneFinder::NXRRSET:
  482. addSOA(*result.finder_);
  483. if (dnssec_) {
  484. addNXRRsetProof(zfinder, *db_context);
  485. }
  486. break;
  487. default:
  488. // This is basically a bug of the data source implementation,
  489. // but could also happen in the middle of development where
  490. // we try to add a new result code.
  491. isc_throw(isc::NotImplemented, "Unknown result code");
  492. break;
  493. }
  494. response_creator_.create(*response_, answers_, authorities_, additionals_);
  495. }
  496. void
  497. Query::initialize(datasrc::ClientList& client_list,
  498. const isc::dns::Name& qname, const isc::dns::RRType& qtype,
  499. isc::dns::Message& response, bool dnssec)
  500. {
  501. client_list_ = &client_list;
  502. qname_ = &qname;
  503. qtype_ = &qtype;
  504. response_ = &response;
  505. dnssec_ = dnssec;
  506. dnssec_opt_ = (dnssec ? isc::datasrc::ZoneFinder::FIND_DNSSEC :
  507. isc::datasrc::ZoneFinder::FIND_DEFAULT);
  508. }
  509. void
  510. Query::reset() {
  511. client_list_ = NULL;
  512. qname_ = NULL;
  513. qtype_ = NULL;
  514. response_ = NULL;
  515. answers_.clear();
  516. authorities_.clear();
  517. additionals_.clear();
  518. response_creator_.clear();
  519. }
  520. bool
  521. Query::processDSAtChild() {
  522. const ClientList::FindResult zresult = client_list_->find(*qname_, true);
  523. if (zresult.dsrc_client_ == NULL) {
  524. return (false);
  525. }
  526. // We are receiving a DS query at the child side of the owner name,
  527. // where the DS isn't supposed to belong. We should return a "no data"
  528. // response as described in Section 3.1.4.1 of RFC4035 and Section
  529. // 2.2.1.1 of RFC 3658. find(DS) should result in NXRRSET, in which
  530. // case (and if DNSSEC is required) we also add the proof for that,
  531. // but even if find() returns an unexpected result, we don't bother.
  532. // The important point in this case is to return SOA so that the resolver
  533. // that happens to contact us can hunt for the appropriate parent zone
  534. // by seeing the SOA.
  535. response_->setHeaderFlag(Message::HEADERFLAG_AA);
  536. response_->setRcode(Rcode::NOERROR());
  537. addSOA(*zresult.finder_);
  538. ConstZoneFinderContextPtr ds_context =
  539. zresult.finder_->find(*qname_, RRType::DS(), dnssec_opt_);
  540. if (ds_context->code == ZoneFinder::NXRRSET) {
  541. if (dnssec_) {
  542. addNXRRsetProof(*zresult.finder_, *ds_context);
  543. }
  544. }
  545. response_creator_.create(*response_, answers_, authorities_, additionals_);
  546. return (true);
  547. }
  548. }
  549. }