zone_finder.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. // Copyright (C) 2012 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 <datasrc/memory/zone_finder.h>
  15. #include <datasrc/memory/domaintree.h>
  16. #include <datasrc/memory/treenode_rrset.h>
  17. #include <datasrc/zone.h>
  18. #include <datasrc/data_source.h>
  19. #include <dns/labelsequence.h>
  20. #include <dns/name.h>
  21. #include <dns/rrset.h>
  22. #include <dns/rrtype.h>
  23. #include <util/buffer.h>
  24. #include <util/encode/base32hex.h>
  25. #include <util/hash/sha1.h>
  26. #include <datasrc/logger.h>
  27. using namespace isc::dns;
  28. using namespace isc::datasrc::memory;
  29. using namespace isc::datasrc;
  30. using namespace isc::util;
  31. using namespace isc::util::encode;
  32. using namespace isc::util::hash;
  33. namespace isc {
  34. namespace datasrc {
  35. namespace memory {
  36. namespace {
  37. /// Creates a TreeNodeRRsetPtr for the given RdataSet at the given Node, for
  38. /// the given RRClass
  39. ///
  40. /// We should probably have some pool so these do not need to be allocated
  41. /// dynamically.
  42. ///
  43. /// \param node The ZoneNode found by the find() calls
  44. /// \param rdataset The RdataSet to create the RRsetPtr for
  45. /// \param rrclass The RRClass as passed by the client
  46. /// \param realname If given, the TreeNodeRRset is created with this name
  47. /// (e.g. for wildcard substitution)
  48. ///
  49. /// Returns an empty TreeNodeRRsetPtr if node is NULL or if rdataset is NULL.
  50. TreeNodeRRsetPtr
  51. createTreeNodeRRset(const ZoneNode* node,
  52. const RdataSet* rdataset,
  53. const RRClass& rrclass,
  54. const Name* realname = NULL)
  55. {
  56. if (node != NULL && rdataset != NULL) {
  57. if (realname != NULL) {
  58. return TreeNodeRRsetPtr(new TreeNodeRRset(*realname, rrclass, node,
  59. rdataset, true));
  60. } else {
  61. return TreeNodeRRsetPtr(new TreeNodeRRset(rrclass, node,
  62. rdataset, true));
  63. }
  64. } else {
  65. return TreeNodeRRsetPtr();
  66. }
  67. }
  68. /// Maintain intermediate data specific to the search context used in
  69. /// \c find().
  70. ///
  71. /// It will be passed to \c cutCallback() (see below) and record a possible
  72. /// zone cut node and related RRset (normally NS or DNAME).
  73. struct FindState {
  74. FindState(bool glue_ok) :
  75. zonecut_node_(NULL),
  76. dname_node_(NULL),
  77. rrset_(NULL),
  78. glue_ok_(glue_ok)
  79. {}
  80. // These will be set to a domain node of the highest delegation point,
  81. // if any. In fact, we could use a single variable instead of both.
  82. // But then we would need to distinquish these two cases by something
  83. // else and it seemed little more confusing when this was written.
  84. const ZoneNode* zonecut_node_;
  85. const ZoneNode* dname_node_;
  86. // Delegation RRset (NS or DNAME), if found.
  87. const RdataSet* rrset_;
  88. // Whether to continue search below a delegation point.
  89. // Set at construction time.
  90. const bool glue_ok_;
  91. };
  92. // A callback called from possible zone cut nodes and nodes with DNAME.
  93. // This will be passed from findNode() to \c RBTree::find().
  94. bool cutCallback(const ZoneNode& node, FindState* state) {
  95. // We need to look for DNAME first, there's allowed case where
  96. // DNAME and NS coexist in the apex. DNAME is the one to notice,
  97. // the NS is authoritative, not delegation (corner case explicitly
  98. // allowed by section 3 of 2672)
  99. const RdataSet* found_dname = RdataSet::find(node.getData(),
  100. RRType::DNAME());
  101. if (found_dname != NULL) {
  102. LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_MEM_DNAME_ENCOUNTERED);
  103. state->dname_node_ = &node;
  104. state->rrset_ = found_dname;
  105. return (true);
  106. }
  107. // Look for NS
  108. const RdataSet* found_ns = RdataSet::find(node.getData(), RRType::NS());
  109. if (found_ns != NULL) {
  110. // We perform callback check only for the highest zone cut in the
  111. // rare case of nested zone cuts.
  112. if (state->zonecut_node_ != NULL) {
  113. return (false);
  114. }
  115. LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_MEM_NS_ENCOUNTERED);
  116. // BIND 9 checks if this node is not the origin. That's probably
  117. // because it can support multiple versions for dynamic updates
  118. // and IXFR, and it's possible that the callback is called at
  119. // the apex and the DNAME doesn't exist for a particular version.
  120. // It cannot happen for us (at least for now), so we don't do
  121. // that check.
  122. state->zonecut_node_ = &node;
  123. state->rrset_ = found_ns;
  124. // Unless glue is allowed the search stops here, so we return
  125. // false; otherwise return true to continue the search.
  126. return (!state->glue_ok_);
  127. }
  128. // This case should not happen because we enable callback only
  129. // when we add an RR searched for above.
  130. assert(0);
  131. // This is here to avoid warning (therefore compilation error)
  132. // in case assert is turned off. Otherwise we could get "Control
  133. // reached end of non-void function".
  134. return (false);
  135. }
  136. // convenience function to fill in the final details
  137. //
  138. // Set up ZoneFinderResultContext object as a return value of find(),
  139. // taking into account wildcard matches and DNSSEC information. We set
  140. // the NSEC/NSEC3 flag when applicable regardless of the find option; the
  141. // caller would simply ignore these when they didn't request DNSSEC
  142. // related results.
  143. //
  144. // Also performs the conversion of node + RdataSet into a TreeNodeRRsetPtr
  145. //
  146. // if wild is true, the RESULT_WILDCARD flag will be set.
  147. // If qname is not NULL, this is the query name, to be used in wildcard
  148. // substitution instead of the Node's name).
  149. isc::datasrc::memory::ZoneFinderResultContext
  150. createFindResult(const RRClass& rrclass,
  151. const ZoneData& zone_data,
  152. ZoneFinder::Result code,
  153. const RdataSet* rrset,
  154. const ZoneNode* node,
  155. bool wild = false,
  156. const Name* qname = NULL) {
  157. ZoneFinder::FindResultFlags flags = ZoneFinder::RESULT_DEFAULT;
  158. const Name* rename = NULL;
  159. if (wild) {
  160. flags = flags | ZoneFinder::RESULT_WILDCARD;
  161. // only use the rename qname if wild is true
  162. rename = qname;
  163. }
  164. if (code == ZoneFinder::NXRRSET || code == ZoneFinder::NXDOMAIN || wild) {
  165. if (zone_data.isNSEC3Signed()) {
  166. flags = flags | ZoneFinder::RESULT_NSEC3_SIGNED;
  167. } else if (zone_data.isSigned()) {
  168. flags = flags | ZoneFinder::RESULT_NSEC_SIGNED;
  169. }
  170. }
  171. return (ZoneFinderResultContext(code, createTreeNodeRRset(node, rrset,
  172. rrclass, rename),
  173. flags, node));
  174. }
  175. // A helper function for NSEC-signed zones. It searches the zone for
  176. // the "closest" NSEC corresponding to the search context stored in
  177. // node_path (it should contain sufficient information to identify the
  178. // previous name of the query name in the zone). In some cases the
  179. // immediate closest name may not have NSEC (when it's under a zone cut
  180. // for glue records, or even when the zone is partly broken), so this
  181. // method continues the search until it finds a name that has NSEC,
  182. // and returns the one found first. Due to the prerequisite (see below),
  183. // it should always succeed.
  184. //
  185. // node_path must store valid search context (in practice, it's expected
  186. // to be set by findNode()); otherwise the underlying RBTree implementation
  187. // throws.
  188. //
  189. // If the zone is not considered NSEC-signed or DNSSEC records were not
  190. // required in the original search context (specified in options), this
  191. // method doesn't bother to find NSEC, and simply returns NULL. So, by
  192. // definition of "NSEC-signed", when it really tries to find an NSEC it
  193. // should succeed; there should be one at least at the zone origin.
  194. const RdataSet*
  195. getClosestNSEC(const ZoneData& zone_data,
  196. ZoneChain& node_path,
  197. const ZoneNode** nsec_node,
  198. ZoneFinder::FindOptions options)
  199. {
  200. if (!zone_data.isSigned() ||
  201. (options & ZoneFinder::FIND_DNSSEC) == 0 ||
  202. zone_data.isNSEC3Signed()) {
  203. return (NULL);
  204. }
  205. const ZoneNode* prev_node;
  206. while ((prev_node = zone_data.getZoneTree().previousNode(node_path))
  207. != NULL) {
  208. if (!prev_node->isEmpty()) {
  209. const RdataSet* found =
  210. RdataSet::find(prev_node->getData(), RRType::NSEC());
  211. if (found != NULL) {
  212. *nsec_node = prev_node;
  213. return (found);
  214. }
  215. }
  216. }
  217. // This must be impossible and should be an internal bug.
  218. // See the description at the method declaration.
  219. assert(false);
  220. // Even though there is an assert here, strict compilers
  221. // will still need some return value.
  222. return (NULL);
  223. }
  224. // A helper function for the NXRRSET case in find(). If the zone is
  225. // NSEC-signed and DNSSEC records are requested, try to find NSEC
  226. // on the given node, and return it if found; return NULL for all other
  227. // cases.
  228. const RdataSet*
  229. getNSECForNXRRSET(const ZoneData& zone_data,
  230. ZoneFinder::FindOptions options,
  231. const ZoneNode* node)
  232. {
  233. if (zone_data.isSigned() &&
  234. !zone_data.isNSEC3Signed() &&
  235. (options & ZoneFinder::FIND_DNSSEC) != 0) {
  236. const RdataSet* found = RdataSet::find(node->getData(),
  237. RRType::NSEC());
  238. if (found != NULL) {
  239. return (found);
  240. }
  241. }
  242. return (NULL);
  243. }
  244. // Structure to hold result data of the findNode() call
  245. class FindNodeResult {
  246. public:
  247. // Bitwise flags to represent supplemental information of the
  248. // search result:
  249. // Search resulted in a wildcard match.
  250. static const unsigned int FIND_WILDCARD = 1;
  251. // Search encountered a zone cut due to NS but continued to look for
  252. // a glue.
  253. static const unsigned int FIND_ZONECUT = 2;
  254. FindNodeResult(ZoneFinder::Result code_param,
  255. const ZoneNode* node_param,
  256. const RdataSet* rrset_param,
  257. unsigned int flags_param = 0) :
  258. code(code_param),
  259. node(node_param),
  260. rrset(rrset_param),
  261. flags(flags_param)
  262. {}
  263. const ZoneFinder::Result code;
  264. const ZoneNode* node;
  265. const RdataSet* rrset;
  266. const unsigned int flags;
  267. };
  268. // Implementation notes: this method identifies an ZoneNode that best matches
  269. // the give name in terms of DNS query handling. In many cases,
  270. // DomainTree::find() will result in EXACTMATCH or PARTIALMATCH (note that
  271. // the given name is generally expected to be contained in the zone, so
  272. // even if it doesn't exist, it should at least match the zone origin).
  273. // If it finds an exact match, that's obviously the best one. The partial
  274. // match case is more complicated.
  275. //
  276. // We first need to consider the case where search hits a delegation point,
  277. // either due to NS or DNAME. They are indicated as either dname_node_ or
  278. // zonecut_node_ being non NULL. Usually at most one of them will be
  279. // something else than NULL (it might happen both are NULL, in which case we
  280. // consider it NOT FOUND). There's one corner case when both might be
  281. // something else than NULL and it is in case there's a DNAME under a zone
  282. // cut and we search in glue OK mode ‒ in that case we don't stop on the
  283. // domain with NS and ignore it for the answer, but it gets set anyway. Then
  284. // we find the DNAME and we need to act by it, therefore we first check for
  285. // DNAME and then for NS. In all other cases it doesn't matter, as at least
  286. // one of them is NULL.
  287. //
  288. // Next, we need to check if the ZoneTree search stopped at a node for a
  289. // subdomain of the search name (so the comparison result that stopped the
  290. // search is "SUPERDOMAIN"), it means the stopping node is an empty
  291. // non-terminal node. In this case the search name is considered to exist
  292. // but no data should be found there.
  293. //
  294. // If none of above is the case, we then consider whether there's a matching
  295. // wildcard. DomainTree::find() records the node if it encounters a
  296. // "wildcarding" node, i.e., the immediate ancestor of a wildcard name
  297. // (e.g., wild.example.com for *.wild.example.com), and returns it if it
  298. // doesn't find any node that better matches the query name. In this case
  299. // we'll check if there's indeed a wildcard below the wildcarding node.
  300. //
  301. // Note, first, that the wildcard is checked after the empty
  302. // non-terminal domain case above, because if that one triggers, it
  303. // means we should not match according to 4.3.3 of RFC 1034 (the query
  304. // name is known to exist).
  305. //
  306. // Before we try to find a wildcard, we should check whether there's
  307. // an existing node that would cancel the wildcard match. If
  308. // DomainTree::find() stopped at a node which has a common ancestor
  309. // with the query name, it might mean we are comparing with a
  310. // non-wildcard node. In that case, we check which part is common. If
  311. // we have something in common that lives below the node we got (the
  312. // one above *), then we should cancel the match according to section
  313. // 4.3.3 of RFC 1034 (as the name between the wildcard domain and the
  314. // query name is known to exist).
  315. //
  316. // If there's no node below the wildcarding node that shares a common ancestor
  317. // of the query name, we can conclude the wildcard is the best match.
  318. // We'll then identify the wildcard node via an incremental search. Note that
  319. // there's no possibility that the query name is at an empty non terminal
  320. // node below the wildcarding node at this stage; that case should have been
  321. // caught above.
  322. //
  323. // If none of the above succeeds, we conclude the name doesn't exist in
  324. // the zone, and throw an OutOfZone exception.
  325. FindNodeResult findNode(const ZoneData& zone_data,
  326. const Name& name,
  327. ZoneChain& node_path,
  328. ZoneFinder::FindOptions options)
  329. {
  330. ZoneNode* node = NULL;
  331. FindState state((options & ZoneFinder::FIND_GLUE_OK) != 0);
  332. const ZoneTree& tree(zone_data.getZoneTree());
  333. ZoneTree::Result result = tree.find(isc::dns::LabelSequence(name),
  334. &node, node_path, cutCallback,
  335. &state);
  336. const unsigned int zonecut_flag =
  337. (state.zonecut_node_ != NULL) ? FindNodeResult::FIND_ZONECUT : 0;
  338. if (result == ZoneTree::EXACTMATCH) {
  339. return (FindNodeResult(ZoneFinder::SUCCESS, node, state.rrset_,
  340. zonecut_flag));
  341. } else if (result == ZoneTree::PARTIALMATCH) {
  342. assert(node != NULL);
  343. if (state.dname_node_ != NULL) { // DNAME
  344. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DNAME_FOUND).
  345. arg(state.dname_node_->getName());
  346. return (FindNodeResult(ZoneFinder::DNAME, state.dname_node_,
  347. state.rrset_));
  348. }
  349. if (state.zonecut_node_ != NULL) { // DELEGATION due to NS
  350. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DELEG_FOUND).
  351. arg(state.zonecut_node_->getName());
  352. return (FindNodeResult(ZoneFinder::DELEGATION,
  353. state.zonecut_node_,
  354. state.rrset_));
  355. }
  356. if (node_path.getLastComparisonResult().getRelation() ==
  357. NameComparisonResult::SUPERDOMAIN) { // empty node, so NXRRSET
  358. LOG_DEBUG(logger, DBG_TRACE_DATA,
  359. DATASRC_MEM_SUPER_STOP).arg(name);
  360. const ZoneNode* nsec_node;
  361. const RdataSet* nsec_rds = getClosestNSEC(zone_data,
  362. node_path,
  363. &nsec_node,
  364. options);
  365. return (FindNodeResult(ZoneFinder::NXRRSET, nsec_node,
  366. nsec_rds));
  367. }
  368. // Nothing really matched.
  369. // May be a wildcard, but check only if not disabled
  370. if (node->getFlag(ZoneData::WILDCARD_NODE) &&
  371. (options & ZoneFinder::NO_WILDCARD) == 0) {
  372. if (node_path.getLastComparisonResult().getRelation() ==
  373. NameComparisonResult::COMMONANCESTOR) {
  374. // This means, e.g., we have *.wild.example and
  375. // bar.foo.wild.example and are looking for
  376. // baz.foo.wild.example. The common ancestor, foo.wild.example,
  377. // should cancel wildcard. Treat it as NXDOMAIN.
  378. LOG_DEBUG(logger, DBG_TRACE_DATA,
  379. DATASRC_MEM_WILDCARD_CANCEL).arg(name);
  380. const ZoneNode* nsec_node;
  381. const RdataSet* nsec_rds = getClosestNSEC(zone_data,
  382. node_path,
  383. &nsec_node,
  384. options);
  385. return (FindNodeResult(ZoneFinder::NXDOMAIN, nsec_node,
  386. nsec_rds));
  387. }
  388. uint8_t ls_buf[LabelSequence::MAX_SERIALIZED_LENGTH];
  389. // Create the wildcard name (i.e. take "*" and extend it
  390. // with all node labels down to the wildcard node
  391. LabelSequence wildcard_ls(LabelSequence::WILDCARD(), ls_buf);
  392. const ZoneNode* extend_with = node;
  393. while (extend_with != NULL) {
  394. wildcard_ls.extend(extend_with->getLabels(), ls_buf);
  395. extend_with = extend_with->getUpperNode();
  396. }
  397. // Clear the node_path so that we don't keep incorrect (NSEC)
  398. // context
  399. node_path.clear();
  400. ZoneTree::Result result = tree.find(LabelSequence(wildcard_ls),
  401. &node, node_path, cutCallback,
  402. &state);
  403. // Otherwise, why would the domain_flag::WILD be there if
  404. // there was no wildcard under it?
  405. assert(result == ZoneTree::EXACTMATCH);
  406. return (FindNodeResult(ZoneFinder::SUCCESS, node, state.rrset_,
  407. FindNodeResult::FIND_WILDCARD | zonecut_flag));
  408. }
  409. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_NOT_FOUND).arg(name);
  410. const ZoneNode* nsec_node;
  411. const RdataSet* nsec_rds = getClosestNSEC(zone_data, node_path,
  412. &nsec_node, options);
  413. return (FindNodeResult(ZoneFinder::NXDOMAIN, nsec_node, nsec_rds));
  414. } else {
  415. // If the name is neither an exact or partial match, it is
  416. // out of bailiwick, which is considered an error.
  417. isc_throw(OutOfZone, name.toText() << " not in " <<
  418. zone_data.getOriginNode()->getName());
  419. }
  420. }
  421. } // end anonymous namespace
  422. inline void
  423. iterateSHA1(SHA1Context* ctx, const uint8_t* input, size_t inlength,
  424. const uint8_t* salt, size_t saltlen,
  425. uint8_t output[SHA1_HASHSIZE])
  426. {
  427. SHA1Reset(ctx);
  428. SHA1Input(ctx, input, inlength);
  429. SHA1Input(ctx, salt, saltlen); // this works whether saltlen == or > 0
  430. SHA1Result(ctx, output);
  431. }
  432. std::string
  433. InMemoryZoneFinderNSEC3Calculate(const Name& name,
  434. const uint16_t iterations,
  435. const uint8_t* salt,
  436. size_t salt_len) {
  437. // We first need to normalize the name by converting all upper case
  438. // characters in the labels to lower ones.
  439. OutputBuffer obuf(Name::MAX_WIRE);
  440. Name name_copy(name);
  441. name_copy.downcase();
  442. name_copy.toWire(obuf);
  443. const uint8_t* const salt_buf = (salt_len > 0) ? salt : NULL;
  444. std::vector<uint8_t> digest(SHA1_HASHSIZE);
  445. uint8_t* const digest_buf = &digest[0];
  446. SHA1Context sha1_ctx;
  447. iterateSHA1(&sha1_ctx, static_cast<const uint8_t*>(obuf.getData()),
  448. obuf.getLength(), salt_buf, salt_len, digest_buf);
  449. for (unsigned int n = 0; n < iterations; ++n) {
  450. iterateSHA1(&sha1_ctx, digest_buf, SHA1_HASHSIZE,
  451. salt_buf, salt_len,
  452. digest_buf);
  453. }
  454. return (encodeBase32Hex(digest));
  455. }
  456. // Specialization of the ZoneFinder::Context for the in-memory finder.
  457. class InMemoryZoneFinder::Context : public ZoneFinder::Context {
  458. public:
  459. /// \brief Constructor.
  460. ///
  461. /// Note that we don't have a specific constructor for the findAll() case.
  462. /// For (successful) type ANY query, found_node points to the
  463. /// corresponding RB node, which is recorded within this specialized
  464. /// context.
  465. Context(ZoneFinder& finder, ZoneFinder::FindOptions options,
  466. ZoneFinderResultContext result) :
  467. ZoneFinder::Context(finder, options,
  468. ResultContext(result.code, result.rrset,
  469. result.flags)),
  470. rrset_(result.rrset), found_node_(result.found_node)
  471. {}
  472. private:
  473. const TreeNodeRRsetPtr rrset_;
  474. const ZoneNode* const found_node_;
  475. };
  476. boost::shared_ptr<ZoneFinder::Context>
  477. InMemoryZoneFinder::find(const isc::dns::Name& name,
  478. const isc::dns::RRType& type,
  479. const FindOptions options)
  480. {
  481. return ZoneFinderContextPtr(new Context(*this, options,
  482. find_internal(name,
  483. type,
  484. NULL,
  485. options)));
  486. }
  487. boost::shared_ptr<ZoneFinder::Context>
  488. InMemoryZoneFinder::findAll(const isc::dns::Name& name,
  489. std::vector<isc::dns::ConstRRsetPtr>& target,
  490. const FindOptions options)
  491. {
  492. return ZoneFinderContextPtr(new Context(*this, options,
  493. find_internal(name,
  494. RRType::ANY(),
  495. &target,
  496. options)));
  497. }
  498. ZoneFinderResultContext
  499. InMemoryZoneFinder::find_internal(const isc::dns::Name& name,
  500. const isc::dns::RRType& type,
  501. std::vector<ConstRRsetPtr>* target,
  502. const FindOptions options)
  503. {
  504. // Get the node. All other cases than an exact match are handled
  505. // in findNode(). We simply construct a result structure and return.
  506. ZoneChain node_path;
  507. const FindNodeResult node_result =
  508. findNode(zone_data_, name, node_path, options);
  509. if (node_result.code != SUCCESS) {
  510. return (createFindResult(rrclass_, zone_data_, node_result.code,
  511. node_result.rrset, node_result.node));
  512. }
  513. const ZoneNode* node = node_result.node;
  514. assert(node != NULL);
  515. // We've found an exact match, may or may not be a result of wildcard.
  516. const bool wild = ((node_result.flags &
  517. FindNodeResult::FIND_WILDCARD) != 0);
  518. // If there is an exact match but the node is empty, it's equivalent
  519. // to NXRRSET.
  520. if (node->isEmpty()) {
  521. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DOMAIN_EMPTY).
  522. arg(name);
  523. const ZoneNode* nsec_node;
  524. const RdataSet* nsec_rds = getClosestNSEC(zone_data_, node_path,
  525. &nsec_node, options);
  526. return (createFindResult(rrclass_, zone_data_, NXRRSET,
  527. nsec_rds,
  528. nsec_node,
  529. wild));
  530. }
  531. const RdataSet* found;
  532. // If the node callback is enabled, this may be a zone cut. If it
  533. // has a NS RR, we should return a delegation, but not in the apex.
  534. // There is one exception: the case for DS query, which should always
  535. // be considered in-zone lookup.
  536. if (node->getFlag(ZoneNode::FLAG_CALLBACK) &&
  537. node != zone_data_.getOriginNode() && type != RRType::DS()) {
  538. found = RdataSet::find(node->getData(), RRType::NS());
  539. if (found != NULL) {
  540. LOG_DEBUG(logger, DBG_TRACE_DATA,
  541. DATASRC_MEM_EXACT_DELEGATION).arg(name);
  542. return (createFindResult(rrclass_, zone_data_, DELEGATION,
  543. found, node, wild, &name));
  544. }
  545. }
  546. // Handle type any query
  547. if (target != NULL && node->getData() != NULL) {
  548. // Empty domain will be handled as NXRRSET by normal processing
  549. const RdataSet* cur_rds = node->getData();
  550. while (cur_rds != NULL) {
  551. target->push_back(createTreeNodeRRset(node, cur_rds, rrclass_,
  552. &name));
  553. cur_rds = cur_rds->getNext();
  554. }
  555. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ANY_SUCCESS).
  556. arg(name);
  557. return (createFindResult(rrclass_, zone_data_, SUCCESS, NULL, node,
  558. wild, &name));
  559. }
  560. const RdataSet* currds = node->getData();
  561. while (currds != NULL) {
  562. currds = currds->getNext();
  563. }
  564. found = RdataSet::find(node->getData(), type);
  565. if (found != NULL) {
  566. // Good, it is here
  567. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_SUCCESS).arg(name).
  568. arg(type);
  569. return (createFindResult(rrclass_, zone_data_, SUCCESS, found, node,
  570. wild, &name));
  571. } else {
  572. // Next, try CNAME.
  573. found = RdataSet::find(node->getData(), RRType::CNAME());
  574. if (found != NULL) {
  575. LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_CNAME).arg(name);
  576. return (createFindResult(rrclass_, zone_data_, CNAME, found, node,
  577. wild, &name));
  578. }
  579. }
  580. // No exact match or CNAME. Get NSEC if necessary and return NXRRSET.
  581. return (createFindResult(rrclass_, zone_data_, NXRRSET,
  582. getNSECForNXRRSET(zone_data_, options, node),
  583. node, wild, &name));
  584. }
  585. isc::datasrc::ZoneFinder::FindNSEC3Result
  586. InMemoryZoneFinder::findNSEC3(const isc::dns::Name& name, bool recursive) {
  587. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_FINDNSEC3).arg(name).
  588. arg(recursive ? "recursive" : "non-recursive");
  589. if (!zone_data_.isNSEC3Signed()) {
  590. isc_throw(DataSourceError,
  591. "findNSEC3 attempt for non NSEC3 signed zone: " <<
  592. getOrigin() << "/" << getClass());
  593. }
  594. const NameComparisonResult cmp_result = name.compare(getOrigin());
  595. if (cmp_result.getRelation() != NameComparisonResult::EQUAL &&
  596. cmp_result.getRelation() != NameComparisonResult::SUBDOMAIN) {
  597. isc_throw(OutOfZone, "findNSEC3 attempt for out-of-zone name: "
  598. << name << ", zone: " << getOrigin() << "/"
  599. << getClass());
  600. }
  601. // Convenient shortcuts
  602. const unsigned int olabels = getOrigin().getLabelCount();
  603. const unsigned int qlabels = name.getLabelCount();
  604. const NSEC3Data* nsec3_data = zone_data_.getNSEC3Data();
  605. // placeholder of the next closer proof
  606. const ZoneNode* covering_node(NULL);
  607. const ZoneTree& tree = nsec3_data->getNSEC3Tree();
  608. ZoneChain chain;
  609. // Examine all names from the query name to the origin name, stripping
  610. // the deepest label one by one, until we find a name that has a matching
  611. // NSEC3 hash.
  612. for (unsigned int labels = qlabels; labels >= olabels; --labels) {
  613. const Name& hname = (labels == qlabels ?
  614. name : name.split(qlabels - labels, labels));
  615. const std::string hlabel =
  616. (nsec3_calculate_) (hname,
  617. nsec3_data->iterations,
  618. nsec3_data->getSaltData(),
  619. nsec3_data->getSaltLen());
  620. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_FINDNSEC3_TRYHASH).
  621. arg(name).arg(labels).arg(hlabel);
  622. ZoneNode* node(NULL);
  623. chain.clear();
  624. const ZoneTree::Result result =
  625. tree.find(Name(hlabel).concatenate(getOrigin()), &node, chain);
  626. if (result == ZoneTree::EXACTMATCH) {
  627. // We found an exact match.
  628. RdataSet* set = node->getData();
  629. ConstRRsetPtr closest = createTreeNodeRRset(node, set, getClass());
  630. ConstRRsetPtr next =
  631. createTreeNodeRRset(covering_node,
  632. (covering_node != NULL ?
  633. covering_node->getData() : NULL),
  634. getClass());
  635. LOG_DEBUG(logger, DBG_TRACE_BASIC,
  636. DATASRC_MEM_FINDNSEC3_MATCH).arg(name).arg(labels).
  637. arg(*closest);
  638. return (FindNSEC3Result(true, labels, closest, next));
  639. } else {
  640. const NameComparisonResult& last_cmp =
  641. chain.getLastComparisonResult();
  642. const ZoneNode* last_node = chain.getLastComparedNode();
  643. assert(last_cmp.getOrder() != 0);
  644. // find() finished in between one of these and last_node:
  645. const ZoneNode* previous_node = last_node->predecessor();
  646. const ZoneNode* next_node = last_node->successor();
  647. // If the given hash is larger than the largest stored hash or
  648. // the first label doesn't match the target, identify the
  649. // "previous" hash value and remember it as the candidate next
  650. // closer proof.
  651. if (((last_cmp.getOrder() < 0) && (previous_node == NULL)) ||
  652. ((last_cmp.getOrder() > 0) && (next_node == NULL))) {
  653. covering_node = last_node->getLargestInSubTree();
  654. } else {
  655. // Otherwise, H(found_entry-1) < given_hash < H(found_entry).
  656. // The covering proof is the first one (and it's valid
  657. // because found is neither begin nor end)
  658. covering_node = previous_node;
  659. }
  660. if (!recursive) { // in non recursive mode, we are done.
  661. ConstRRsetPtr closest =
  662. createTreeNodeRRset(covering_node,
  663. (covering_node != NULL ?
  664. covering_node->getData() :
  665. NULL),
  666. getClass());
  667. if (closest) {
  668. LOG_DEBUG(logger, DBG_TRACE_BASIC,
  669. DATASRC_MEM_FINDNSEC3_COVER).
  670. arg(name).arg(*closest);
  671. }
  672. return (FindNSEC3Result(false, labels,
  673. closest, ConstRRsetPtr()));
  674. }
  675. }
  676. }
  677. isc_throw(DataSourceError, "recursive findNSEC3 mode didn't stop, likely "
  678. "a broken NSEC3 zone: " << getOrigin() << "/"
  679. << getClass());
  680. }
  681. } // namespace memory
  682. } // namespace datasrc
  683. } // namespace isc