query.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <datasrc/zone.h>
  18. namespace isc {
  19. namespace dns {
  20. class Message;
  21. class Name;
  22. class RRType;
  23. class RRset;
  24. }
  25. namespace datasrc {
  26. class DataSourceClient;
  27. }
  28. namespace auth {
  29. /// The \c Query class represents a standard DNS query that encapsulates
  30. /// processing logic to answer the query.
  31. ///
  32. /// Many of the design details for this class are still in flux.
  33. /// We'll revisit and update them as we add more functionality, for example:
  34. /// - as a related point, we may have to pass the RR class of the query.
  35. /// in the initial implementation the RR class is an attribute of
  36. /// datasource and omitted. It's not clear if this assumption holds with
  37. /// generic data sources. On the other hand, it will help keep
  38. /// implementation simpler, and we might rather want to modify the design
  39. /// of the data source on this point.
  40. /// - return value of process(). rather than or in addition to setting the
  41. /// Rcode, we might use it as a return value of \c process().
  42. /// - we'll have to be able to specify whether DNSSEC is requested.
  43. /// It's an open question whether it should be in the constructor or via a
  44. /// separate attribute setter.
  45. /// - likewise, we'll eventually need to do per zone access control, for which
  46. /// we need querier's information such as its IP address.
  47. /// - datasrc_client and response may better be parameters to process() instead
  48. /// of the constructor.
  49. ///
  50. /// <b>Note:</b> The class name is intentionally the same as the one used in
  51. /// the datasrc library. This is because the plan is to eventually merge
  52. /// the two classes. We could give it a different name such as "AuthQuery"
  53. /// to avoid possible ambiguity, but it may sound redundant in that it's
  54. /// obvious that this class is for authoritative queries.
  55. /// Since the interfaces are very different for now and it's less
  56. /// likely to misuse one of the classes instead of the other
  57. /// accidentally, and since it's considered a temporary development state,
  58. /// we keep this name at the moment.
  59. class Query {
  60. private:
  61. /// \brief Adds a SOA.
  62. ///
  63. /// Adds a SOA of the zone into the authority zone of response_.
  64. /// Can throw NoSOA.
  65. ///
  66. void addSOA(isc::datasrc::ZoneFinder& finder);
  67. /// \brief Adds the DS rrset for the given name, if available
  68. ///
  69. /// This is intended to be called when returning a delegation, and
  70. /// if DNSSEC data is requested. If the DS record is not found
  71. /// (signaled by find() returning NXRRSET), and the zone is signed
  72. /// with NSEC, an NSEC denial of existence proof is added.
  73. ///
  74. /// \exception BadDS raised if find() returns anything other than
  75. /// SUCCESS or NXRRSET when searching for the DS
  76. /// record.
  77. /// \param finder The ZoneFinder where the delegation was found
  78. /// \param ds_name The name of the delegation RRset
  79. void addDS(isc::datasrc::ZoneFinder& finder,
  80. const isc::dns::Name& ds_name);
  81. /// \brief Adds NSEC(3) denial proof for the given NXRRset result
  82. ///
  83. /// If available, NSEC or NSEC3 records are added to the authority
  84. /// section (depending on whether isNSECSigned() or isNSEC3Signed()
  85. /// returns true).
  86. ///
  87. /// \param finder The ZoneFinder that was used to search for the missing
  88. /// data
  89. /// \param db_result The ZoneFinder::FindResult returned by find()
  90. void addNXRRsetProof(isc::datasrc::ZoneFinder& finder,
  91. const isc::datasrc::ZoneFinder::FindResult& db_result);
  92. /// Add NSEC RRs that prove an NXDOMAIN result.
  93. ///
  94. /// This corresponds to Section 3.1.3.2 of RFC 4035.
  95. void addNXDOMAINProof(isc::datasrc::ZoneFinder& finder,
  96. isc::dns::ConstRRsetPtr nsec);
  97. /// Add NSEC RRs that prove a wildcard answer is the best one.
  98. ///
  99. /// This corresponds to Section 3.1.3.3 of RFC 4035.
  100. void addWildcardProof(isc::datasrc::ZoneFinder& finder);
  101. /// \brief Adds one NSEC RR proved no matched QNAME,one NSEC RR proved no
  102. /// matched <QNAME,QTYPE> through wildcard extension.
  103. ///
  104. /// Add NSEC RRs that prove an WILDCARD_NXRRSET result.
  105. /// This corresponds to Section 3.1.3.4 of RFC 4035.
  106. /// \param finder The ZoneFinder through which the authority data for the
  107. /// query is to be found.
  108. /// \param nsec The RRset (NSEC RR) which proved that there is no matched
  109. /// <QNAME,QTTYPE>.
  110. void addWildcardNXRRSETProof(isc::datasrc::ZoneFinder& finder,
  111. isc::dns::ConstRRsetPtr nsec);
  112. /// \brief Look up additional data (i.e., address records for the names
  113. /// included in NS or MX records) and add them to the additional section.
  114. ///
  115. /// Note: Any additional data which has already been provided in the
  116. /// answer section (i.e., if the original query happend to be for the
  117. /// address of the DNS server), it should be omitted from the additional.
  118. ///
  119. /// This method may throw a exception because its underlying methods may
  120. /// throw exceptions.
  121. ///
  122. /// \param zone The ZoneFinder through which the additional data for the
  123. /// query is to be found.
  124. /// \param rrset The RRset (i.e., NS or MX rrset) which require additional
  125. /// processing.
  126. void addAdditional(isc::datasrc::ZoneFinder& zone,
  127. const isc::dns::AbstractRRset& rrset);
  128. /// \brief Find address records for a specified name.
  129. ///
  130. /// Search the specified zone for AAAA/A RRs of each of the NS/MX RDATA
  131. /// (domain name), and insert the found ones into the additional section
  132. /// if address records are available. By default the search will stop
  133. /// once it encounters a zone cut.
  134. ///
  135. /// Note: we need to perform the search in the "GLUE OK" mode for NS RDATA,
  136. /// which means that we should include A/AAAA RRs under a zone cut.
  137. /// The glue records must exactly match the name in the NS RDATA, without
  138. /// CNAME or wildcard processing.
  139. ///
  140. /// \param zone The \c ZoneFinder through which the address records is to
  141. /// be found.
  142. /// \param qname The name in rrset RDATA.
  143. /// \param options The search options.
  144. void addAdditionalAddrs(isc::datasrc::ZoneFinder& zone,
  145. const isc::dns::Name& qname,
  146. const isc::datasrc::ZoneFinder::FindOptions options
  147. = isc::datasrc::ZoneFinder::FIND_DEFAULT);
  148. /// \brief Look up a zone's NS RRset and their address records for an
  149. /// authoritative answer, and add them to the additional section.
  150. ///
  151. /// On returning an authoritative answer, insert a zone's NS into the
  152. /// authority section and AAAA/A RRs of each of the NS RDATA into the
  153. /// additional section.
  154. ///
  155. /// <b>Notes to developer:</b>
  156. ///
  157. /// We should omit address records which has already been provided in the
  158. /// answer section from the additional.
  159. ///
  160. /// For now, in order to optimize the additional section processing, we
  161. /// include AAAA/A RRs under a zone cut in additional section. (BIND 9
  162. /// excludes under-cut RRs; NSD include them.)
  163. ///
  164. /// \param finder The \c ZoneFinder through which the NS and additional
  165. /// data for the query are to be found.
  166. void addAuthAdditional(isc::datasrc::ZoneFinder& finder);
  167. /// \brief Process a DS query possible at the child side of zone cut.
  168. ///
  169. /// This private method is a subroutine of process(), and is called if
  170. /// there's a possibility that this server has authority for the child
  171. /// side of the DS's owner name (and it's detected that the server at
  172. /// least doesn't have authority at the parent side). This method
  173. /// first checks if it has authority for the child, and if does,
  174. /// just build a "no data" response with SOA for the zone origin
  175. /// (possibly with a proof for the no data) as specified in Section
  176. /// 2.2.1.1 of RFC3658.
  177. ///
  178. /// It returns true if this server has authority of the child; otherwise
  179. /// it returns false. In the former case, the caller is expected to
  180. /// terminate the query processing, because it should have been completed
  181. /// within this method.
  182. bool processDSAtChild();
  183. public:
  184. /// Constructor from query parameters.
  185. ///
  186. /// This constructor never throws an exception.
  187. ///
  188. /// \param datasrc_client The datasource wherein the answer to the query is
  189. /// to be found.
  190. /// \param qname The query name
  191. /// \param qtype The RR type of the query
  192. /// \param response The response message to store the answer to the query.
  193. /// \param dnssec If the answer should include signatures and NSEC/NSEC3 if
  194. /// possible.
  195. Query(const isc::datasrc::DataSourceClient& datasrc_client,
  196. const isc::dns::Name& qname, const isc::dns::RRType& qtype,
  197. isc::dns::Message& response, bool dnssec = false) :
  198. datasrc_client_(datasrc_client), qname_(qname), qtype_(qtype),
  199. response_(response), dnssec_(dnssec),
  200. dnssec_opt_(dnssec ? isc::datasrc::ZoneFinder::FIND_DNSSEC :
  201. isc::datasrc::ZoneFinder::FIND_DEFAULT)
  202. {}
  203. /// Process the query.
  204. ///
  205. /// This method first identifies the zone that best matches the query
  206. /// name (and in some cases RR type when the search is dependent on the
  207. /// type) and then searches the zone for an entry that best matches the
  208. /// query name.
  209. /// It then updates the response message accordingly; for example, a
  210. /// successful search would result in adding a corresponding RRset to
  211. /// the answer section of the response.
  212. ///
  213. /// If no matching zone is found in the datasource, the RCODE of
  214. /// SERVFAIL will be set in the response.
  215. /// <b>Note:</b> this is different from the error code that BIND 9 returns
  216. /// by default when it's configured as an authoritative-only server (and
  217. /// from the behavior of the BIND 10 datasrc library, which was implemented
  218. /// to be compatible with BIND 9).
  219. /// The difference comes from the fact that BIND 9 returns REFUSED as a
  220. /// result of access control check on the use of its cache.
  221. /// Since BIND 10's authoritative server doesn't have the notion of cache
  222. /// by design, it doesn't make sense to return REFUSED. On the other hand,
  223. /// providing compatible behavior may have its own benefit, so this point
  224. /// should be revisited later.
  225. ///
  226. /// This might throw BadZone or any of its specific subclasses, but that
  227. /// shouldn't happen in real-life (as BadZone means wrong data, it should
  228. /// have been rejected upon loading).
  229. void process();
  230. /// \short Bad zone data encountered.
  231. ///
  232. /// This is thrown when process encounteres misconfigured zone in a way
  233. /// it can't continue. This throws, not sets the Rcode, because such
  234. /// misconfigured zone should not be present in the data source and
  235. /// should have been rejected sooner.
  236. struct BadZone : public isc::Exception {
  237. BadZone(const char* file, size_t line, const char* what) :
  238. Exception(file, line, what)
  239. {}
  240. };
  241. /// \short Zone is missing its SOA record.
  242. ///
  243. /// We tried to add a SOA into the authoritative section, but the zone
  244. /// does not contain one.
  245. struct NoSOA : public BadZone {
  246. NoSOA(const char* file, size_t line, const char* what) :
  247. BadZone(file, line, what)
  248. {}
  249. };
  250. /// \short Zone is missing its apex NS records.
  251. ///
  252. /// We tried to add apex NS records into the authority section, but the
  253. /// zone does not contain any.
  254. struct NoApexNS: public BadZone {
  255. NoApexNS(const char* file, size_t line, const char* what) :
  256. BadZone(file, line, what)
  257. {}
  258. };
  259. /// An invalid result is given when a valid NSEC is expected
  260. ///
  261. /// This can only happen when the underlying data source implementation or
  262. /// the zone is broken. By throwing an exception we treat such cases
  263. /// as SERVFAIL.
  264. struct BadNSEC : public BadZone {
  265. BadNSEC(const char* file, size_t line, const char* what) :
  266. BadZone(file, line, what)
  267. {}
  268. };
  269. /// An invalid result is given when a valid NSEC3 is expected
  270. ///
  271. /// This can only happen when the underlying data source implementation or
  272. /// the zone is broken. By throwing an exception we treat such cases
  273. /// as SERVFAIL.
  274. struct BadNSEC3 : public BadZone {
  275. BadNSEC3(const char* file, size_t line, const char* what) :
  276. BadZone(file, line, what)
  277. {}
  278. };
  279. /// An invalid result is given when a valid DS records (or NXRRSET) is
  280. /// expected
  281. ///
  282. /// This can only happen when the underlying data source implementation
  283. /// or the zone is broken. A DS query for a known delegation point should
  284. /// either result in SUCCESS (if available) or NXRRSET
  285. struct BadDS : public BadZone {
  286. BadDS(const char* file, size_t line, const char* what) :
  287. BadZone(file, line, what)
  288. {}
  289. };
  290. private:
  291. const isc::datasrc::DataSourceClient& datasrc_client_;
  292. const isc::dns::Name& qname_;
  293. const isc::dns::RRType& qtype_;
  294. isc::dns::Message& response_;
  295. const bool dnssec_;
  296. const isc::datasrc::ZoneFinder::FindOptions dnssec_opt_;
  297. };
  298. }
  299. }
  300. // Local Variables:
  301. // mode: c++
  302. // End: