query.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /// Add NSEC RRs that prove an NXDOMAIN result.
  68. ///
  69. /// This corresponds to Section 3.1.3.2 of RFC 4035.
  70. void addNXDOMAINProof(isc::datasrc::ZoneFinder& finder,
  71. isc::dns::ConstRRsetPtr nsec);
  72. /// \brief Look up additional data (i.e., address records for the names
  73. /// included in NS or MX records) and add them to the additional section.
  74. ///
  75. /// Note: Any additional data which has already been provided in the
  76. /// answer section (i.e., if the original query happend to be for the
  77. /// address of the DNS server), it should be omitted from the additional.
  78. ///
  79. /// This method may throw a exception because its underlying methods may
  80. /// throw exceptions.
  81. ///
  82. /// \param zone The ZoneFinder through which the additional data for the
  83. /// query is to be found.
  84. /// \param rrset The RRset (i.e., NS or MX rrset) which require additional
  85. /// processing.
  86. void addAdditional(isc::datasrc::ZoneFinder& zone,
  87. const isc::dns::RRset& rrset);
  88. /// \brief Find address records for a specified name.
  89. ///
  90. /// Search the specified zone for AAAA/A RRs of each of the NS/MX RDATA
  91. /// (domain name), and insert the found ones into the additional section
  92. /// if address records are available. By default the search will stop
  93. /// once it encounters a zone cut.
  94. ///
  95. /// Note: we need to perform the search in the "GLUE OK" mode for NS RDATA,
  96. /// which means that we should include A/AAAA RRs under a zone cut.
  97. /// The glue records must exactly match the name in the NS RDATA, without
  98. /// CNAME or wildcard processing.
  99. ///
  100. /// \param zone The \c ZoneFinder through which the address records is to
  101. /// be found.
  102. /// \param qname The name in rrset RDATA.
  103. /// \param options The search options.
  104. void addAdditionalAddrs(isc::datasrc::ZoneFinder& zone,
  105. const isc::dns::Name& qname,
  106. const isc::datasrc::ZoneFinder::FindOptions options
  107. = isc::datasrc::ZoneFinder::FIND_DEFAULT);
  108. /// \brief Look up a zone's NS RRset and their address records for an
  109. /// authoritative answer, and add them to the additional section.
  110. ///
  111. /// On returning an authoritative answer, insert a zone's NS into the
  112. /// authority section and AAAA/A RRs of each of the NS RDATA into the
  113. /// additional section.
  114. ///
  115. /// <b>Notes to developer:</b>
  116. ///
  117. /// We should omit address records which has already been provided in the
  118. /// answer section from the additional.
  119. ///
  120. /// For now, in order to optimize the additional section processing, we
  121. /// include AAAA/A RRs under a zone cut in additional section. (BIND 9
  122. /// excludes under-cut RRs; NSD include them.)
  123. ///
  124. /// \param finder The \c ZoneFinder through which the NS and additional
  125. /// data for the query are to be found.
  126. void addAuthAdditional(isc::datasrc::ZoneFinder& finder);
  127. public:
  128. /// Constructor from query parameters.
  129. ///
  130. /// This constructor never throws an exception.
  131. ///
  132. /// \param datasrc_client The datasource wherein the answer to the query is
  133. /// to be found.
  134. /// \param qname The query name
  135. /// \param qtype The RR type of the query
  136. /// \param response The response message to store the answer to the query.
  137. /// \param dnssec If the answer should include signatures and NSEC/NSEC3 if
  138. /// possible.
  139. Query(const isc::datasrc::DataSourceClient& datasrc_client,
  140. const isc::dns::Name& qname, const isc::dns::RRType& qtype,
  141. isc::dns::Message& response, bool dnssec = false) :
  142. datasrc_client_(datasrc_client), qname_(qname), qtype_(qtype),
  143. response_(response), dnssec_(dnssec),
  144. dnssec_opt_(dnssec ? isc::datasrc::ZoneFinder::FIND_DNSSEC :
  145. isc::datasrc::ZoneFinder::FIND_DEFAULT)
  146. {}
  147. /// Process the query.
  148. ///
  149. /// This method first identifies the zone that best matches the query
  150. /// name (and in some cases RR type when the search is dependent on the
  151. /// type) and then searches the zone for an entry that best matches the
  152. /// query name.
  153. /// It then updates the response message accordingly; for example, a
  154. /// successful search would result in adding a corresponding RRset to
  155. /// the answer section of the response.
  156. ///
  157. /// If no matching zone is found in the datasource, the RCODE of
  158. /// SERVFAIL will be set in the response.
  159. /// <b>Note:</b> this is different from the error code that BIND 9 returns
  160. /// by default when it's configured as an authoritative-only server (and
  161. /// from the behavior of the BIND 10 datasrc library, which was implemented
  162. /// to be compatible with BIND 9).
  163. /// The difference comes from the fact that BIND 9 returns REFUSED as a
  164. /// result of access control check on the use of its cache.
  165. /// Since BIND 10's authoritative server doesn't have the notion of cache
  166. /// by design, it doesn't make sense to return REFUSED. On the other hand,
  167. /// providing compatible behavior may have its own benefit, so this point
  168. /// should be revisited later.
  169. ///
  170. /// This might throw BadZone or any of its specific subclasses, but that
  171. /// shouldn't happen in real-life (as BadZone means wrong data, it should
  172. /// have been rejected upon loading).
  173. void process();
  174. /// \short Bad zone data encountered.
  175. ///
  176. /// This is thrown when process encounteres misconfigured zone in a way
  177. /// it can't continue. This throws, not sets the Rcode, because such
  178. /// misconfigured zone should not be present in the data source and
  179. /// should have been rejected sooner.
  180. struct BadZone : public isc::Exception {
  181. BadZone(const char* file, size_t line, const char* what) :
  182. Exception(file, line, what)
  183. {}
  184. };
  185. /// \short Zone is missing its SOA record.
  186. ///
  187. /// We tried to add a SOA into the authoritative section, but the zone
  188. /// does not contain one.
  189. struct NoSOA : public BadZone {
  190. NoSOA(const char* file, size_t line, const char* what) :
  191. BadZone(file, line, what)
  192. {}
  193. };
  194. /// \short Zone is missing its apex NS records.
  195. ///
  196. /// We tried to add apex NS records into the authority section, but the
  197. /// zone does not contain any.
  198. struct NoApexNS: public BadZone {
  199. NoApexNS(const char* file, size_t line, const char* what) :
  200. BadZone(file, line, what)
  201. {}
  202. };
  203. /// An invalid result is given when a valid NSEC is expected
  204. ///
  205. // This can only happen when the underlying data source implementation or
  206. /// the zone is broken. By throwing an exception we treat such cases
  207. /// as SERVFAIL.
  208. struct BadNSEC : public BadZone {
  209. BadNSEC(const char* file, size_t line, const char* what) :
  210. BadZone(file, line, what)
  211. {}
  212. };
  213. private:
  214. const isc::datasrc::DataSourceClient& datasrc_client_;
  215. const isc::dns::Name& qname_;
  216. const isc::dns::RRType& qtype_;
  217. isc::dns::Message& response_;
  218. const bool dnssec_;
  219. const isc::datasrc::ZoneFinder::FindOptions dnssec_opt_;
  220. };
  221. }
  222. }
  223. // Local Variables:
  224. // mode: c++
  225. // End: