query.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <exceptions/exceptions.h>
  17. #include <dns/rrset.h>
  18. #include <datasrc/zone.h>
  19. #include <boost/noncopyable.hpp>
  20. #include <functional>
  21. #include <vector>
  22. namespace isc {
  23. namespace dns {
  24. class Message;
  25. class Name;
  26. class RRType;
  27. class RRset;
  28. }
  29. namespace datasrc {
  30. class ClientList;
  31. }
  32. namespace auth {
  33. /// The \c Query class represents a standard DNS query that encapsulates
  34. /// processing logic to answer the query.
  35. ///
  36. /// Many of the design details for this class are still in flux.
  37. /// We'll revisit and update them as we add more functionality, for example:
  38. /// - as a related point, we may have to pass the RR class of the query.
  39. /// in the initial implementation the RR class is an attribute of
  40. /// datasource and omitted. It's not clear if this assumption holds with
  41. /// generic data sources. On the other hand, it will help keep
  42. /// implementation simpler, and we might rather want to modify the design
  43. /// of the data source on this point.
  44. /// - return value of process(). rather than or in addition to setting the
  45. /// Rcode, we might use it as a return value of \c process().
  46. /// - we'll have to be able to specify whether DNSSEC is requested.
  47. /// It's an open question whether it should be in the constructor or via a
  48. /// separate attribute setter.
  49. /// - likewise, we'll eventually need to do per zone access control, for which
  50. /// we need querier's information such as its IP address.
  51. ///
  52. /// <b>Note:</b> The class name is intentionally the same as the one used in
  53. /// the datasrc library. This is because the plan is to eventually merge
  54. /// the two classes. We could give it a different name such as "AuthQuery"
  55. /// to avoid possible ambiguity, but it may sound redundant in that it's
  56. /// obvious that this class is for authoritative queries.
  57. /// Since the interfaces are very different for now and it's less
  58. /// likely to misuse one of the classes instead of the other
  59. /// accidentally, and since it's considered a temporary development state,
  60. /// we keep this name at the moment.
  61. class Query : boost::noncopyable {
  62. private:
  63. /// \brief Initial reserved size for the vectors in Query
  64. ///
  65. /// The value is larger than we expect the vectors to even become, and
  66. /// has been chosen arbitrarily. The reason to set them quite high is
  67. /// to prevent reallocation on addition.
  68. static const size_t RESERVE_RRSETS = 64;
  69. /// \brief Adds a SOA.
  70. ///
  71. /// Adds a SOA of the zone into the authority zone of response_.
  72. /// Can throw NoSOA.
  73. ///
  74. void addSOA(isc::datasrc::ZoneFinder& finder);
  75. /// \brief Adds the DS rrset for the given name, if available
  76. ///
  77. /// This is intended to be called when returning a delegation, and
  78. /// if DNSSEC data is requested. If the DS record is not found
  79. /// (signaled by find() returning NXRRSET), and the zone is signed
  80. /// with NSEC, an NSEC denial of existence proof is added.
  81. ///
  82. /// \exception BadDS raised if find() returns anything other than
  83. /// SUCCESS or NXRRSET when searching for the DS
  84. /// record.
  85. /// \param finder The ZoneFinder where the delegation was found
  86. /// \param ds_name The name of the delegation RRset
  87. void addDS(isc::datasrc::ZoneFinder& finder,
  88. const isc::dns::Name& ds_name);
  89. /// \brief Adds NSEC(3) denial proof for the given NXRRset result
  90. ///
  91. /// If available, NSEC or NSEC3 records are added to the authority
  92. /// section (depending on whether isNSECSigned() or isNSEC3Signed()
  93. /// returns true).
  94. ///
  95. /// \param finder The ZoneFinder that was used to search for the missing
  96. /// data
  97. /// \param db_result The ZoneFinder::FindResult returned by find()
  98. void addNXRRsetProof(isc::datasrc::ZoneFinder& finder,
  99. const isc::datasrc::ZoneFinder::Context& db_context);
  100. /// Add NSEC RRs that prove an NXDOMAIN result.
  101. ///
  102. /// This corresponds to Section 3.1.3.2 of RFC 4035.
  103. void addNXDOMAINProofByNSEC(isc::datasrc::ZoneFinder& finder,
  104. isc::dns::ConstRRsetPtr nsec);
  105. /// Add NSEC3 RRs that prove an NXDOMAIN result.
  106. ///
  107. /// This corresponds to Section 7.2.2 of RFC 5155.
  108. void addNXDOMAINProofByNSEC3(isc::datasrc::ZoneFinder& finder);
  109. /// Add NSEC or NSEC3 RRs that prove a wildcard answer is the best one.
  110. ///
  111. /// This corresponds to Section 3.1.3.3 of RFC 4035 and Section 7.2.6
  112. /// of RFC5155.
  113. void addWildcardProof(
  114. isc::datasrc::ZoneFinder& finder,
  115. const isc::datasrc::ZoneFinder::Context& db_context);
  116. /// \brief Adds one NSEC RR proved no matched QNAME,one NSEC RR proved no
  117. /// matched <QNAME,QTYPE> through wildcard extension.
  118. ///
  119. /// Add NSEC RRs that prove an WILDCARD_NXRRSET result.
  120. /// This corresponds to Section 3.1.3.4 of RFC 4035.
  121. /// \param finder The ZoneFinder through which the authority data for the
  122. /// query is to be found.
  123. /// \param nsec The RRset (NSEC RR) which proved that there is no matched
  124. /// <QNAME,QTTYPE>.
  125. void addWildcardNXRRSETProof(isc::datasrc::ZoneFinder& finder,
  126. isc::dns::ConstRRsetPtr nsec);
  127. /// \brief Look up a zone's NS RRset and their address records for an
  128. /// authoritative answer, and add them to the additional section.
  129. ///
  130. /// On returning an authoritative answer, insert a zone's NS into the
  131. /// authority section and AAAA/A RRs of each of the NS RDATA into the
  132. /// additional section.
  133. ///
  134. /// <b>Notes to developer:</b>
  135. ///
  136. /// We should omit address records which has already been provided in the
  137. /// answer section from the additional.
  138. ///
  139. /// For now, in order to optimize the additional section processing, we
  140. /// include AAAA/A RRs under a zone cut in additional section. (BIND 9
  141. /// excludes under-cut RRs; NSD include them.)
  142. ///
  143. /// \param finder The \c ZoneFinder through which the NS and additional
  144. /// data for the query are to be found.
  145. void addAuthAdditional(isc::datasrc::ZoneFinder& finder,
  146. std::vector<isc::dns::ConstRRsetPtr>& additionals);
  147. /// \brief Process a DS query possible at the child side of zone cut.
  148. ///
  149. /// This private method is a subroutine of process(), and is called if
  150. /// there's a possibility that this server has authority for the child
  151. /// side of the DS's owner name (and it's detected that the server at
  152. /// least doesn't have authority at the parent side). This method
  153. /// first checks if it has authority for the child, and if does,
  154. /// just build a "no data" response with SOA for the zone origin
  155. /// (possibly with a proof for the no data) as specified in Section
  156. /// 2.2.1.1 of RFC3658.
  157. ///
  158. /// It returns true if this server has authority of the child; otherwise
  159. /// it returns false. In the former case, the caller is expected to
  160. /// terminate the query processing, because it should have been completed
  161. /// within this method.
  162. bool processDSAtChild();
  163. /// \brief Add NSEC3 to the response for a closest encloser proof for a
  164. /// given name.
  165. ///
  166. /// This method calls \c findNSEC3() of the given zone finder for the
  167. /// given name in the recursive mode, and adds the returned NSEC3(s) to
  168. /// the authority section of the response message associated with the
  169. /// \c Query object.
  170. ///
  171. /// It returns the number of labels of the closest encloser (returned via
  172. /// the \c findNSEC3() call) in case the caller needs to use that value
  173. /// for subsequent processing, i.e, constructing the best possible wildcard
  174. /// name that (would) match the query name.
  175. ///
  176. /// Unless \c exact_ok is true, \c name is expected to be non existent,
  177. /// in which case findNSEC3() in the recursive mode must return both
  178. /// closest and next proofs. If the latter is NULL, it means a run time
  179. /// collision (or the zone is broken in other way), and this method throws
  180. /// a BadNSEC3 exception.
  181. ///
  182. /// If \c exact_ok is true, this method takes into account the case
  183. /// where the name exists and may or may not be at a zone cut to an
  184. /// optout zone. In this case, depending on whether the zone is optout
  185. /// or not, findNSEC3() may return non-NULL or NULL next_proof
  186. /// (respectively). This method adds the next proof if and only if
  187. /// findNSEC3() returns non NULL value for it. The Opt-Out flag
  188. /// must be set or cleared accordingly, but this method doesn't check that
  189. /// in this level (as long as the zone is signed validly and findNSEC3()
  190. /// for the data source is implemented as documented, the condition
  191. /// should be met; otherwise we'd let the validator detect the error).
  192. ///
  193. /// By default this method always adds the closest proof.
  194. /// If \c add_closest is false, it only adds the next proof to the message.
  195. /// This correspond to the case of "wildcard answer responses" as described
  196. /// in Section 7.2.6 of RFC5155.
  197. uint8_t addClosestEncloserProof(isc::datasrc::ZoneFinder& finder,
  198. const isc::dns::Name& name, bool exact_ok,
  199. bool add_closest = true);
  200. /// \brief Add matching or covering NSEC3 to the response for a give name.
  201. ///
  202. /// This method calls \c findNSEC3() of the given zone finder for the
  203. /// given name in the non recursive mode, and adds the returned NSEC3 to
  204. /// the authority section of the response message associated with the
  205. /// \c Query object.
  206. ///
  207. /// Depending on the caller's context, the returned NSEC3 is one and
  208. /// only one of matching or covering NSEC3. If \c match is true the
  209. /// returned NSEC3 must be a matching one; otherwise it must be a covering
  210. /// one. If this assumption isn't met this method throws a BadNSEC3
  211. /// exception (if it must be a matching NSEC3 but is not, it means a broken
  212. /// zone, maybe with incorrect optout NSEC3s; if it must be a covering
  213. /// NSEC3 but is not, it means a run time collision; or the \c findNSEC3()
  214. /// implementation is broken for both cases.)
  215. void addNSEC3ForName(isc::datasrc::ZoneFinder& finder,
  216. const isc::dns::Name& name, bool match);
  217. /// Set up the Query object for a new query lookup
  218. ///
  219. /// This is the first step of the process() method, and initializes
  220. /// the member data
  221. ///
  222. /// \param client_list The datasource list wherein the answer to the query
  223. /// is to be found.
  224. /// \param qname The query name
  225. /// \param qtype The RR type of the query
  226. /// \param response The response message to store the answer to the query.
  227. /// \param dnssec If the answer should include signatures and NSEC/NSEC3 if
  228. /// possible.
  229. void initialize(datasrc::ClientList& client_list,
  230. const isc::dns::Name& qname, const isc::dns::RRType& qtype,
  231. isc::dns::Message& response, bool dnssec = false);
  232. /// \brief Resets any partly built response data, and internal pointers
  233. ///
  234. /// Called by the QueryCleaner object upon its destruction
  235. void reset();
  236. /// \brief Internal class used for cleanup of Query members
  237. ///
  238. /// The process() call creates an object of this class, which
  239. /// upon its destruction, calls Query::reset(), so that outside
  240. /// of single calls to process(), the query state is always clean.
  241. class QueryCleaner {
  242. public:
  243. QueryCleaner(isc::auth::Query& query) : query_(query) {}
  244. ~QueryCleaner() { query_.reset(); }
  245. private:
  246. isc::auth::Query& query_;
  247. };
  248. protected:
  249. // Following methods declared protected so they can be accessed
  250. // by unit tests.
  251. void createResponse();
  252. public:
  253. /// Default constructor.
  254. ///
  255. /// Query parameters will be set by the call to process()
  256. ///
  257. Query() :
  258. client_list_(NULL), qname_(NULL), qtype_(NULL),
  259. dnssec_(false), dnssec_opt_(isc::datasrc::ZoneFinder::FIND_DEFAULT),
  260. response_(NULL)
  261. {
  262. answers_.reserve(RESERVE_RRSETS);
  263. authorities_.reserve(RESERVE_RRSETS);
  264. additionals_.reserve(RESERVE_RRSETS);
  265. }
  266. /// Process the query.
  267. ///
  268. /// This method first identifies the zone that best matches the query
  269. /// name (and in some cases RR type when the search is dependent on the
  270. /// type) and then searches the zone for an entry that best matches the
  271. /// query name.
  272. /// It then updates the response message accordingly; for example, a
  273. /// successful search would result in adding a corresponding RRset to
  274. /// the answer section of the response.
  275. ///
  276. /// If no matching zone is found in the datasource, the RCODE of
  277. /// SERVFAIL will be set in the response.
  278. /// <b>Note:</b> this is different from the error code that BIND 9 returns
  279. /// by default when it's configured as an authoritative-only server (and
  280. /// from the behavior of the BIND 10 datasrc library, which was implemented
  281. /// to be compatible with BIND 9).
  282. /// The difference comes from the fact that BIND 9 returns REFUSED as a
  283. /// result of access control check on the use of its cache.
  284. /// Since BIND 10's authoritative server doesn't have the notion of cache
  285. /// by design, it doesn't make sense to return REFUSED. On the other hand,
  286. /// providing compatible behavior may have its own benefit, so this point
  287. /// should be revisited later.
  288. ///
  289. /// This might throw BadZone or any of its specific subclasses, but that
  290. /// shouldn't happen in real-life (as BadZone means wrong data, it should
  291. /// have been rejected upon loading).
  292. ///
  293. /// \param client_list The datasource list wherein the answer to the query
  294. /// is to be found.
  295. /// \param qname The query name
  296. /// \param qtype The RR type of the query
  297. /// \param response The response message to store the answer to the query.
  298. /// \param dnssec If the answer should include signatures and NSEC/NSEC3 if
  299. /// possible.
  300. void process(datasrc::ClientList& client_list,
  301. const isc::dns::Name& qname, const isc::dns::RRType& qtype,
  302. isc::dns::Message& response, bool dnssec = false);
  303. /// \short Bad zone data encountered.
  304. ///
  305. /// This is thrown when process encounteres misconfigured zone in a way
  306. /// it can't continue. This throws, not sets the Rcode, because such
  307. /// misconfigured zone should not be present in the data source and
  308. /// should have been rejected sooner.
  309. struct BadZone : public isc::Exception {
  310. BadZone(const char* file, size_t line, const char* what) :
  311. Exception(file, line, what)
  312. {}
  313. };
  314. /// \short Zone is missing its SOA record.
  315. ///
  316. /// We tried to add a SOA into the authoritative section, but the zone
  317. /// does not contain one.
  318. struct NoSOA : public BadZone {
  319. NoSOA(const char* file, size_t line, const char* what) :
  320. BadZone(file, line, what)
  321. {}
  322. };
  323. /// \short Zone is missing its apex NS records.
  324. ///
  325. /// We tried to add apex NS records into the authority section, but the
  326. /// zone does not contain any.
  327. struct NoApexNS: public BadZone {
  328. NoApexNS(const char* file, size_t line, const char* what) :
  329. BadZone(file, line, what)
  330. {}
  331. };
  332. /// An invalid result is given when a valid NSEC is expected
  333. ///
  334. /// This can only happen when the underlying data source implementation or
  335. /// the zone is broken. By throwing an exception we treat such cases
  336. /// as SERVFAIL.
  337. struct BadNSEC : public BadZone {
  338. BadNSEC(const char* file, size_t line, const char* what) :
  339. BadZone(file, line, what)
  340. {}
  341. };
  342. /// An invalid result is given when a valid NSEC3 is expected
  343. ///
  344. /// This can only happen when the underlying data source implementation or
  345. /// the zone is broken. By throwing an exception we treat such cases
  346. /// as SERVFAIL.
  347. struct BadNSEC3 : public BadZone {
  348. BadNSEC3(const char* file, size_t line, const char* what) :
  349. BadZone(file, line, what)
  350. {}
  351. };
  352. /// An invalid result is given when a valid DS records (or NXRRSET) is
  353. /// expected
  354. ///
  355. /// This can only happen when the underlying data source implementation
  356. /// or the zone is broken. A DS query for a known delegation point should
  357. /// either result in SUCCESS (if available) or NXRRSET
  358. struct BadDS : public BadZone {
  359. BadDS(const char* file, size_t line, const char* what) :
  360. BadZone(file, line, what)
  361. {}
  362. };
  363. /// \brief Response Creator Class
  364. ///
  365. /// This is a helper class of Query, and is expected to be used during the
  366. /// construction of the response message. This class performs the
  367. /// duplicate RRset detection check. It keeps a list of RRsets added
  368. /// to the message and does not add an RRset if it is the same as one
  369. /// already added.
  370. ///
  371. /// This class is essentially private to Query, but is visible to public
  372. /// for testing purposes. It's not expected to be used from a normal
  373. /// application.
  374. class ResponseCreator {
  375. public:
  376. /// \brief Constructor
  377. ///
  378. /// Reserves space for the list of RRsets. Although the
  379. /// ResponseCreator will be used to create a message from the
  380. /// contents of the Query object's answers_, authorities_ and
  381. /// additionals_ elements, and each of these are sized to
  382. /// RESERVE_RRSETS, it is _extremely_ unlikely that all three will be
  383. /// filled to capacity. So we reserve more elements than in each of
  384. /// these components, but not three times the amount.
  385. ///
  386. /// As with the answers_, authorities_ and additionals_ elements, the
  387. /// reservation is made in the constructor to avoid dynamic allocation
  388. /// of memory. The ResponseCreator is a member variable of the Query
  389. /// object so is constructed once and lasts as long as that object.
  390. /// Internal state is cleared through the clear() method.
  391. ResponseCreator() {
  392. added_.reserve(2 * RESERVE_RRSETS);
  393. }
  394. /// \brief Reset internal state
  395. void clear() {
  396. added_.clear();
  397. }
  398. /// \brief Complete the response message with filling in the
  399. /// response sections.
  400. ///
  401. /// This is the final step of the Query::process() method, and within
  402. /// that method, it should be called before it returns (if any
  403. /// response data is to be added)
  404. ///
  405. /// This will take a message to build and each RRsets for the answer,
  406. /// authority, and additional sections, and add them to their
  407. /// corresponding sections in the given message. The RRsets are
  408. /// filtered such that a particular RRset appears only once in the
  409. /// message. Any RRSIGs attached to the RRsets will be included
  410. /// when they are rendered.
  411. void create(
  412. isc::dns::Message& message,
  413. const std::vector<isc::dns::ConstRRsetPtr>& answers_,
  414. const std::vector<isc::dns::ConstRRsetPtr>& authorities_,
  415. const std::vector<isc::dns::ConstRRsetPtr>& additionals_);
  416. private:
  417. // \brief RRset comparison functor.
  418. struct IsSameKind : public std::binary_function<
  419. const isc::dns::AbstractRRset*,
  420. const isc::dns::AbstractRRset*,
  421. bool> {
  422. bool operator()(const isc::dns::AbstractRRset* r1,
  423. const isc::dns::AbstractRRset* r2) const {
  424. return (r1->isSameKind(*r2));
  425. }
  426. };
  427. /// Insertion operation
  428. ///
  429. /// \param message Message to which the RRset is to be added
  430. /// \param section Section of the message in which the RRset is put
  431. /// \param rrset Pointer to RRset to be added to the message
  432. void addRRset(isc::dns::Message& message,
  433. const isc::dns::Message::Section section,
  434. const isc::dns::ConstRRsetPtr& rrset);
  435. private:
  436. /// List of RRsets already added to the message
  437. std::vector<const isc::dns::AbstractRRset*> added_;
  438. };
  439. private:
  440. const isc::datasrc::ClientList* client_list_;
  441. const isc::dns::Name* qname_;
  442. const isc::dns::RRType* qtype_;
  443. bool dnssec_;
  444. isc::datasrc::ZoneFinder::FindOptions dnssec_opt_;
  445. ResponseCreator response_creator_;
  446. isc::dns::Message* response_;
  447. std::vector<isc::dns::ConstRRsetPtr> answers_;
  448. std::vector<isc::dns::ConstRRsetPtr> authorities_;
  449. std::vector<isc::dns::ConstRRsetPtr> additionals_;
  450. };
  451. }
  452. }
  453. // Local Variables:
  454. // mode: c++
  455. // End: