zone.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. #ifndef __ZONE_H
  15. #define __ZONE_H 1
  16. #include <dns/name.h>
  17. #include <dns/rrset.h>
  18. #include <dns/rrtype.h>
  19. #include <datasrc/result.h>
  20. #include <utility>
  21. #include <vector>
  22. namespace isc {
  23. namespace datasrc {
  24. /// \brief The base class to search a zone for RRsets
  25. ///
  26. /// The \c ZoneFinder class is an abstract base class for representing
  27. /// an object that performs DNS lookups in a specific zone accessible via
  28. /// a data source. In general, different types of data sources (in-memory,
  29. /// database-based, etc) define their own derived classes of \c ZoneFinder,
  30. /// implementing ways to retrieve the required data through the common
  31. /// interfaces declared in the base class. Each concrete \c ZoneFinder
  32. /// object is therefore (conceptually) associated with a specific zone
  33. /// of one specific data source instance.
  34. ///
  35. /// The origin name and the RR class of the associated zone are available
  36. /// via the \c getOrigin() and \c getClass() methods, respectively.
  37. ///
  38. /// The most important method of this class is \c find(), which performs
  39. /// the lookup for a given domain and type. See the description of the
  40. /// method for details.
  41. ///
  42. /// \note It's not clear whether we should request that a zone finder form a
  43. /// "transaction", that is, whether to ensure the finder is not susceptible
  44. /// to changes made by someone else than the creator of the finder. If we
  45. /// don't request that, for example, two different lookup results for the
  46. /// same name and type can be different if other threads or programs make
  47. /// updates to the zone between the lookups. We should revisit this point
  48. /// as we gain more experiences.
  49. class ZoneFinder {
  50. public:
  51. /// Result codes of the \c find() method.
  52. ///
  53. /// Note: the codes are tentative. We may need more, or we may find
  54. /// some of them unnecessary as we implement more details.
  55. ///
  56. /// See the description of \c find() for further details of how
  57. /// these results should be interpreted.
  58. enum Result {
  59. SUCCESS, ///< An exact match is found.
  60. DELEGATION, ///< The search encounters a zone cut.
  61. NXDOMAIN, ///< There is no domain name that matches the search name
  62. NXRRSET, ///< There is a matching name but no RRset of the search type
  63. CNAME, ///< The search encounters and returns a CNAME RR
  64. DNAME ///< The search encounters and returns a DNAME RR
  65. };
  66. /// Special attribute flags on the result of the \c find() method
  67. ///
  68. /// The flag values defined here are intended to signal to the caller
  69. /// that it may need special handling on the result. This is particularly
  70. /// of concern when DNSSEC is requested. For example, for negative
  71. /// responses the caller would want to know whether the zone is signed
  72. /// with NSEC or NSEC3 so that it can subsequently provide necessary
  73. /// proof of the result.
  74. ///
  75. /// The caller is generally expected to get access to the information
  76. /// via read-only getter methods of \c FindContext so that it won't rely
  77. /// on specific details of the representation of the flags. So these
  78. /// definitions are basically only meaningful for data source
  79. /// implementations.
  80. enum FindResultFlags {
  81. RESULT_DEFAULT = 0, ///< The default flags
  82. RESULT_WILDCARD = 1, ///< find() resulted in a wildcard match
  83. RESULT_NSEC_SIGNED = 2, ///< The zone is signed with NSEC RRs
  84. RESULT_NSEC3_SIGNED = 4 ///< The zone is signed with NSEC3 RRs
  85. };
  86. /// Find options.
  87. ///
  88. /// The option values are used as a parameter for \c find().
  89. /// These are values of a bitmask type. Bitwise operations can be
  90. /// performed on these values to express compound options.
  91. enum FindOptions {
  92. FIND_DEFAULT = 0, ///< The default options
  93. FIND_GLUE_OK = 1, ///< Allow search under a zone cut
  94. FIND_DNSSEC = 2, ///< Require DNSSEC data in the answer
  95. ///< (RRSIG, NSEC, etc.). The implementation
  96. ///< is allowed to include it even if it is
  97. ///< not set.
  98. NO_WILDCARD = 4 ///< Do not try wildcard matching.
  99. };
  100. protected:
  101. /// \brief A convenient tuple representing a set of find() results.
  102. ///
  103. /// This helper structure is specifically expected to be used as an input
  104. /// for the construct of the \c Context class object used by derived
  105. /// ZoneFinder implementations. This is therefore defined as protected.
  106. struct ResultContext {
  107. ResultContext(Result code_param,
  108. isc::dns::ConstRRsetPtr rrset_param,
  109. FindResultFlags flags_param = RESULT_DEFAULT) :
  110. code(code_param), rrset(rrset_param), flags(flags_param)
  111. {}
  112. const Result code;
  113. const isc::dns::ConstRRsetPtr rrset;
  114. const FindResultFlags flags;
  115. };
  116. public:
  117. /// \brief Context of the result of a find() call.
  118. ///
  119. /// This class encapsulates results and (possibly) associated context
  120. /// of a call to the \c find() method. The public member variables of
  121. /// this class reprsent the result of the call. They are a
  122. /// straightforward tuple of the result code and a pointer (and
  123. /// optionally special flags) to the found RRset.
  124. ///
  125. /// These member variables will be initialized on construction and never
  126. /// change, so for convenience we allow the applications to refer to some
  127. /// of the members directly. For some others we provide read-only accessor
  128. /// methods to hide specific representation.
  129. ///
  130. /// Another role of this class is to provide the interface to some common
  131. /// processing logic that may be necessary using the result of \c find().
  132. /// Specifically, it's expected to be used in the context of DNS query
  133. /// handling, where the caller would need to look into the data source
  134. /// again based on the \c find() result. For example, it would need to
  135. /// get A and/or AAAA records for some of the answer or authority RRs.
  136. ///
  137. /// This class defines (a set of) method(s) that can be commonly used
  138. /// for such purposes for any type of data source (as long as it conforms
  139. /// to the public \c find() interface). In some cases, a specific data
  140. /// source implementation may want to (and can) optimize the processing
  141. /// exploiting its internal data structure and the knowledge of the context
  142. /// of the precedent \c find() call. Such a data source implementation
  143. /// can define a derived class of the base Context and override the
  144. /// specific virtual method.
  145. ///
  146. /// This class object is generally expected to be associated with the
  147. /// ZoneFinder that originally performed the \c find() call, and expects
  148. /// the finder is valid throughout the lifetime of this object. It's
  149. /// caller's responsibility to ensure that assumption.
  150. class Context {
  151. public:
  152. /// \brief The constructor for the normal find call.
  153. ///
  154. /// This constructor is expected to be called from the \c find()
  155. /// method when it constructs the return value.
  156. ///
  157. /// \param finder The ZoneFinder on which find() is called.
  158. /// \param options The find options specified for the find() call.
  159. /// \param result The result of the find() call.
  160. Context(ZoneFinder& finder, FindOptions options,
  161. const ResultContext& result) :
  162. code(result.code), rrset(result.rrset),
  163. finder_(finder), flags_(result.flags), options_(options)
  164. {}
  165. /// \brief The constructor for the normal findAll call.
  166. ///
  167. /// This constructor is expected to be called from the \c findAll()
  168. /// method when it constructs the return value.
  169. ///
  170. /// It copies the vector that is to be returned to the caller of
  171. /// \c findAll() for possible subsequent use. Note that it cannot
  172. /// simply hold a reference to the vector because the caller may
  173. /// alter it after the \c findAll() call.
  174. ///
  175. /// \param finder The ZoneFinder on which findAll() is called.
  176. /// \param options The find options specified for the findAll() call.
  177. /// \param result The result of the findAll() call (whose rrset is
  178. /// expected to be NULL).
  179. /// \param all_set Reference to the vector given by the caller of
  180. /// \c findAll(), storing the RRsets to be returned.
  181. Context(ZoneFinder& finder, FindOptions options,
  182. const ResultContext& result,
  183. const std::vector<isc::dns::ConstRRsetPtr> &all_set) :
  184. code(result.code), rrset(result.rrset),
  185. finder_(finder), flags_(result.flags), options_(options),
  186. all_set_(all_set)
  187. {}
  188. const Result code;
  189. const isc::dns::ConstRRsetPtr rrset;
  190. /// Return true iff find() results in a wildcard match.
  191. bool isWildcard() const { return ((flags_ & RESULT_WILDCARD) != 0); }
  192. /// Return true when the underlying zone is signed with NSEC.
  193. ///
  194. /// The \c find() implementation allows this to return false if
  195. /// \c FIND_DNSSEC isn't specified regardless of whether the zone
  196. /// is signed or which of NSEC/NSEC3 is used.
  197. ///
  198. /// When this is returned, the implementation of find() must ensure
  199. /// that \c rrset be a valid NSEC RRset as described in \c find()
  200. /// documentation.
  201. bool isNSECSigned() const {
  202. return ((flags_ & RESULT_NSEC_SIGNED) != 0);
  203. }
  204. /// Return true when the underlying zone is signed with NSEC3.
  205. ///
  206. /// The \c find() implementation allows this to return false if
  207. /// \c FIND_DNSSEC isn't specified regardless of whether the zone
  208. /// is signed or which of NSEC/NSEC3 is used.
  209. bool isNSEC3Signed() const {
  210. return ((flags_ & RESULT_NSEC3_SIGNED) != 0);
  211. }
  212. /// \brief Find and return additional RRsets corresponding to the
  213. /// result of \c find().
  214. ///
  215. /// If this context is based on a normal find() call that resulted
  216. /// in SUCCESS or DELEGATION, it examines the returned RRset (in many
  217. /// cases NS, sometimes MX or others), searches the data source for
  218. /// specified type of additional RRs for each RDATA of the RRset
  219. /// (e.g., A or AAAA for the name server addresses), and stores the
  220. /// result in the given vector. The vector may not be empty; this
  221. /// method appends any found RRsets to it, without touching existing
  222. /// elements.
  223. ///
  224. /// If this context is based on a findAll() call that resulted in
  225. /// SUCCESS, it performs the same process for each RRset returned in
  226. /// the \c findAll() call.
  227. ///
  228. /// The caller specifies desired RR types of the additional RRsets
  229. /// in \c requested_types. Normally it consists of A and/or AAAA
  230. /// types, but other types can be specified.
  231. ///
  232. /// This method is meaningful only when the precedent find()/findAll()
  233. /// call resulted in SUCCESS or DELEGATION. Otherwise this method
  234. /// does nothing.
  235. ///
  236. /// \param requested_types A vector of RR types for desired additional
  237. /// RRsets.
  238. /// \param result A vector to which any found additional RRsets are
  239. /// to be inserted.
  240. void getAdditional(
  241. const std::vector<isc::dns::RRType>& requested_types,
  242. std::vector<isc::dns::ConstRRsetPtr>& result)
  243. {
  244. // Perform common checks, and delegate the process the default
  245. // or specialized implementation.
  246. if (code != SUCCESS && code != DELEGATION) {
  247. return;
  248. }
  249. getAdditionalImpl(requested_types, result);
  250. }
  251. protected:
  252. /// \brief Actual implementation of getAdditional().
  253. ///
  254. /// This base class defines a default implementation that can be
  255. /// used for any type of data sources. A data source implementation
  256. /// can override it.
  257. virtual void getAdditionalImpl(
  258. const std::vector<isc::dns::RRType>& requested_types,
  259. std::vector<isc::dns::ConstRRsetPtr>& result);
  260. private:
  261. ZoneFinder& finder_;
  262. const FindResultFlags flags_;
  263. const FindOptions options_;
  264. std::vector<isc::dns::ConstRRsetPtr> all_set_;
  265. };
  266. ///
  267. /// \name Constructors and Destructor.
  268. ///
  269. //@{
  270. protected:
  271. /// The default constructor.
  272. ///
  273. /// This is intentionally defined as \c protected as this base class should
  274. /// never be instantiated (except as part of a derived class).
  275. ZoneFinder() {}
  276. public:
  277. /// The destructor.
  278. virtual ~ZoneFinder() {}
  279. //@}
  280. ///
  281. /// \name Getter Methods
  282. ///
  283. /// These methods should never throw an exception.
  284. //@{
  285. /// Return the origin name of the zone.
  286. virtual isc::dns::Name getOrigin() const = 0;
  287. /// Return the RR class of the zone.
  288. virtual isc::dns::RRClass getClass() const = 0;
  289. //@}
  290. ///
  291. /// \name Search Methods
  292. ///
  293. //@{
  294. /// Search the zone for a given pair of domain name and RR type.
  295. ///
  296. /// Each derived version of this method searches the underlying backend
  297. /// for the data that best matches the given name and type.
  298. /// This method is expected to be "intelligent", and identifies the
  299. /// best possible answer for the search key. Specifically,
  300. ///
  301. /// - If the search name belongs under a zone cut, it returns the code
  302. /// of \c DELEGATION and the NS RRset at the zone cut.
  303. /// - If there is no matching name, it returns the code of \c NXDOMAIN.
  304. /// - If there is a matching name but no RRset of the search type, it
  305. /// returns the code of \c NXRRSET. This case includes the search name
  306. /// matches an empty node of the zone.
  307. /// - If there is a CNAME RR of the searched name but there is no
  308. /// RR of the searched type of the name (so this type is different from
  309. /// CNAME), it returns the code of \c CNAME and that CNAME RR.
  310. /// Note that if the searched RR type is CNAME, it is considered
  311. /// a successful match, and the code of \c SUCCESS will be returned.
  312. /// - If the search name matches a delegation point of DNAME, it returns
  313. /// the code of \c DNAME and that DNAME RR.
  314. ///
  315. /// No RRset will be returned in the \c NXDOMAIN and \c NXRRSET cases
  316. /// (\c rrset member of \c FindContext will be NULL), unless DNSSEC data
  317. /// are required. See below for the cases with DNSSEC.
  318. ///
  319. /// The returned \c FindContext object can also provide supplemental
  320. /// information about the search result via its methods returning a
  321. /// boolean value. Such information may be useful for the caller if
  322. /// the caller wants to collect additional DNSSEC proofs based on the
  323. /// search result.
  324. ///
  325. /// The \c options parameter specifies customized behavior of the search.
  326. /// Their semantics is as follows (they are or bit-field):
  327. ///
  328. /// - \c FIND_GLUE_OK Allow search under a zone cut. By default the search
  329. /// will stop once it encounters a zone cut. If this option is specified
  330. /// it remembers information about the highest zone cut and continues
  331. /// the search until it finds an exact match for the given name or it
  332. /// detects there is no exact match. If an exact match is found,
  333. /// RRsets for that name are searched just like the normal case;
  334. /// otherwise, if the search has encountered a zone cut, \c DELEGATION
  335. /// with the information of the highest zone cut will be returned.
  336. /// - \c FIND_DNSSEC Request that DNSSEC data (like NSEC, RRSIGs) are
  337. /// returned with the answer. It is allowed for the data source to
  338. /// include them even when not requested.
  339. /// - \c NO_WILDCARD Do not try wildcard matching. This option is of no
  340. /// use for normal lookups; it's intended to be used to get a DNSSEC
  341. /// proof of the non existence of any matching wildcard or non existence
  342. /// of an exact match when a wildcard match is found.
  343. ///
  344. /// In general, \c name is expected to be included in the zone, that is,
  345. /// it should be equal to or a subdomain of the zone origin. Otherwise
  346. /// this method will return \c NXDOMAIN with an empty RRset. But such a
  347. /// case should rather be considered a caller's bug.
  348. ///
  349. /// \note For this reason it's probably better to throw an exception
  350. /// than returning \c NXDOMAIN. This point should be revisited in a near
  351. /// future version. In any case applications shouldn't call this method
  352. /// for an out-of-zone name.
  353. ///
  354. /// <b>DNSSEC considerations:</b>
  355. /// The result when DNSSEC data are required can be very complicated,
  356. /// especially if it involves negative result or wildcard match.
  357. /// Specifically, if an application calls this method for DNS query
  358. /// processing with DNSSEC data, and if the search result code is
  359. /// either \c NXDOMAIN or \c NXRRRSET, and/or \c isWildcard() returns
  360. /// true, then the application will need to find additional NSEC or
  361. /// NSEC3 records for supplemental proofs. This method helps the
  362. /// application for such post search processing.
  363. ///
  364. /// First, it tells the application whether the zone is signed with
  365. /// NSEC or NSEC3 via the \c isNSEC(3)Signed() method. Any sanely signed
  366. /// zone should be signed with either (and only one) of these two types
  367. /// of RRs; however, the application should expect that the zone could
  368. /// be broken and these methods could both return false. But this method
  369. /// should ensure that not both of these methods return true.
  370. ///
  371. /// In case it's signed with NSEC3, there is no further information
  372. /// returned from this method.
  373. ///
  374. /// In case it's signed with NSEC, this method will possibly return
  375. /// a related NSEC RRset in the \c rrset member of \c FindContext.
  376. /// What kind of NSEC is returned depends on the result code
  377. /// (\c NXDOMAIN or \c NXRRSET) and on whether it's a wildcard match:
  378. ///
  379. /// - In case of NXDOMAIN, the returned NSEC covers the queried domain
  380. /// that proves that the query name does not exist in the zone. Note
  381. /// that this does not necessarily prove it doesn't even match a
  382. /// wildcard (even if the result of NXDOMAIN can only happen when
  383. /// there's no matching wildcard either). It is caller's
  384. /// responsibility to provide a proof that there is no matching
  385. /// wildcard if that proof is necessary.
  386. /// - In case of NXRRSET, we need to consider the following cases
  387. /// referring to Section 3.1.3 of RFC4035:
  388. ///
  389. /// -# (Normal) no data: there is a matching non-wildcard name with a
  390. /// different RR type. This is the "No Data" case of the RFC.
  391. /// -# (Normal) empty non terminal: there is no matching (exact or
  392. /// wildcard) name, but there is a subdomain with an RR of the query
  393. /// name. This is one case of "Name Error" of the RFC.
  394. /// -# Wildcard empty non terminal: similar to 2a, but the empty name
  395. /// is a wildcard, and matches the query name by wildcard expansion.
  396. /// This is a special case of "Name Error" of the RFC.
  397. /// -# Wildcard no data: there is no exact match name, but there is a
  398. /// wildcard name that matches the query name with a different type
  399. /// of RR. This is the "Wildcard No Data" case of the RFC.
  400. ///
  401. /// In case 1, \c find() returns NSEC of the matching name.
  402. ///
  403. /// In case 2, \c find() will return NSEC for the interval where the
  404. /// empty nonterminal lives. The end of the interval is the subdomain
  405. /// causing existence of the empty nonterminal (if there's
  406. /// sub.x.example.com, and no record in x.example.com, then
  407. /// x.example.com exists implicitly - is the empty nonterminal and
  408. /// sub.x.example.com is the subdomain causing it). Note that this NSEC
  409. /// proves not only the existence of empty non terminal name but also
  410. /// the non existence of possibly matching wildcard name, because
  411. /// there can be no better wildcard match than the exact matching empty
  412. /// name.
  413. ///
  414. /// In case 3, \c find() will return NSEC for the interval where the
  415. /// wildcard empty nonterminal lives. Cases 2 and 3 are especially
  416. /// complicated and confusing. See the examples below.
  417. ///
  418. /// In case 4, \c find() will return NSEC of the matching wildcard name.
  419. ///
  420. /// Examples: if zone "example.com" has the following record:
  421. /// \code
  422. /// a.example.com. NSEC a.b.example.com.
  423. /// \endcode
  424. /// a call to \c find() for "b.example.com." with the FIND_DNSSEC option
  425. /// will result in NXRRSET, and this NSEC will be returned.
  426. /// Likewise, if zone "example.org" has the following record,
  427. /// \code
  428. /// a.example.org. NSEC x.*.b.example.org.
  429. /// \endcode
  430. /// a call to \c find() for "y.b.example.org" with FIND_DNSSEC will
  431. /// result in NXRRSET and this NSEC; \c isWildcard() on the returned
  432. /// \c FindContext object will return true.
  433. ///
  434. /// \exception std::bad_alloc Memory allocation such as for constructing
  435. /// the resulting RRset fails
  436. /// \exception DataSourceError Derived class specific exception, e.g.
  437. /// when encountering a bad zone configuration or database connection
  438. /// failure. Although these are considered rare, exceptional events,
  439. /// it can happen under relatively usual conditions (unlike memory
  440. /// allocation failure). So, in general, the application is expected
  441. /// to catch this exception, either specifically or as a result of
  442. /// catching a base exception class, and handle it gracefully.
  443. ///
  444. /// \param name The domain name to be searched for.
  445. /// \param type The RR type to be searched for.
  446. /// \param options The search options.
  447. /// \return A \c FindContext object enclosing the search result
  448. /// (see above).
  449. virtual boost::shared_ptr<Context> find(const isc::dns::Name& name,
  450. const isc::dns::RRType& type,
  451. const FindOptions options
  452. = FIND_DEFAULT) = 0;
  453. ///
  454. /// \brief Finds all RRsets in the given name.
  455. ///
  456. /// This function works almost exactly in the same way as the find one. The
  457. /// only difference is, when the lookup is successful (eg. the code is
  458. /// SUCCESS), all the RRsets residing in the named node are
  459. /// copied into the \c target parameter and the rrset member of the result
  460. /// is NULL. All the other (unsuccessful) cases are handled the same,
  461. /// including returning delegations, NSEC/NSEC3 availability and NSEC
  462. /// proofs, wildcard information etc. The options parameter works the
  463. /// same way and it should conform to the same exception restrictions.
  464. ///
  465. /// \param name \see find, parameter name
  466. /// \param target the successfull result is returned through this
  467. /// \param options \see find, parameter options
  468. /// \return \see find and it's result
  469. virtual boost::shared_ptr<Context> findAll(
  470. const isc::dns::Name& name,
  471. std::vector<isc::dns::ConstRRsetPtr> &target,
  472. const FindOptions options = FIND_DEFAULT) = 0;
  473. /// A helper structure to represent the search result of \c findNSEC3().
  474. ///
  475. /// The idea is similar to that of \c FindContext, but \c findNSEC3() has
  476. /// special interface and semantics, we use a different structure to
  477. /// represent the result.
  478. struct FindNSEC3Result {
  479. FindNSEC3Result(bool param_matched, uint8_t param_closest_labels,
  480. isc::dns::ConstRRsetPtr param_closest_proof,
  481. isc::dns::ConstRRsetPtr param_next_proof) :
  482. matched(param_matched), closest_labels(param_closest_labels),
  483. closest_proof(param_closest_proof),
  484. next_proof(param_next_proof)
  485. {}
  486. /// true iff closest_proof is a matching NSEC3
  487. const bool matched;
  488. /// The number of labels of the identified closest encloser.
  489. const uint8_t closest_labels;
  490. /// Either the NSEC3 for the closest provable encloser of the given
  491. /// name or NSEC3 that covers the name
  492. const isc::dns::ConstRRsetPtr closest_proof;
  493. /// When non NULL, NSEC3 for the next closer name.
  494. const isc::dns::ConstRRsetPtr next_proof;
  495. };
  496. /// Search the zone for the NSEC3 RR(s) that prove existence or non
  497. /// existence of a give name.
  498. ///
  499. /// It searches the NSEC3 namespace of the zone (how that namespace is
  500. /// implemented can vary in specific data source implementation) for NSEC3
  501. /// RRs that match or cover the NSEC3 hash value for the given name.
  502. ///
  503. /// If \c recursive is false, it will first look for the NSEC3 that has
  504. /// a matching hash. If it doesn't exist, it identifies the covering NSEC3
  505. /// for the hash. In either case the search stops at that point and the
  506. /// found NSEC3 RR(set) will be returned in the closest_proof member of
  507. /// \c FindNSEC3Result. \c matched is true or false depending on
  508. /// the found NSEC3 is a matched one or covering one. \c next_proof
  509. /// is always NULL. closest_labels must be equal to the number of
  510. /// labels of \c name (and therefore meaningless).
  511. ///
  512. /// If \c recursive is true, it will continue the search toward the zone
  513. /// apex (origin name) until it finds a provable encloser, that is,
  514. /// an ancestor of \c name that has a matching NSEC3. This is the closest
  515. /// provable encloser of \c name as defined in RFC5155. In this case,
  516. /// if the found encloser is not equal to \c name, the search should
  517. /// have seen a covering NSEC3 for the immediate child of the found
  518. /// encloser. That child name is the next closer name as defined in
  519. /// RFC5155. In this case, this method returns the NSEC3 for the
  520. /// closest encloser in \c closest_proof, and the NSEC3 for the next
  521. /// closer name in \c next_proof of \c FindNSEC3Result. This set of
  522. /// NSEC3 RRs provide the closest encloser proof as defined in RFC5155.
  523. /// closest_labels will be set to the number of labels of the identified
  524. /// closest encloser. This will be useful when the caller needs to
  525. /// construct the closest encloser name from the original \c name.
  526. /// If, on the other hand, the found closest name is equal to \c name,
  527. /// this method simply returns it in \c closest_proof. \c next_proof
  528. /// is set to NULL. In all cases \c matched is set to true.
  529. /// closest_labels will be set to the number of labels of \c name.
  530. ///
  531. /// When looking for NSEC3, this method retrieves NSEC3 parameters from
  532. /// the corresponding zone to calculate hash values. Actual implementation
  533. /// of how to do this will differ in different data sources. If the
  534. /// NSEC3 parameters are not available \c DataSourceError exception
  535. /// will be thrown.
  536. ///
  537. /// \note This implicitly means this method assumes the zone does not
  538. /// have more than one set of parameters. This assumption should be
  539. /// reasonable in actual deployment and will help simplify the interface
  540. /// and implementation. But if there's a real need for supporting
  541. /// multiple sets of parameters in a single zone, we will have to
  542. /// extend this method so that, e.g., the caller can specify the parameter
  543. /// set.
  544. ///
  545. /// In general, this method expects the zone is properly signed with NSEC3
  546. /// RRs. Specifically, it assumes at least the apex node has a matching
  547. /// NSEC3 RR (so the search in the recursive mode must always succeed);
  548. /// it also assumes that it can retrieve NSEC parameters (iterations,
  549. /// algorithm, and salt) from the zone as noted above. If these
  550. /// assumptions aren't met, \c DataSourceError exception will be thrown.
  551. ///
  552. /// \exception InvalidParameter name is not a subdomain of the zone origin
  553. /// \exception DataSourceError Low-level or internal datasource errors
  554. /// happened, or the zone isn't properly signed with NSEC3
  555. /// (NSEC3 parameters cannot be found, no NSEC3s are available, etc).
  556. /// \exception std::bad_alloc The underlying implementation involves
  557. /// memory allocation and it fails
  558. ///
  559. /// \param name The name for which NSEC3 RRs are to be found. It must
  560. /// be a subdomain of the zone.
  561. /// \param recursive Whether or not search should continue until it finds
  562. /// a provable encloser (see above).
  563. ///
  564. /// \return The search result and whether or not the closest_proof is
  565. /// a matching NSEC3, in the form of \c FindNSEC3Result object.
  566. virtual FindNSEC3Result
  567. findNSEC3(const isc::dns::Name& name, bool recursive) = 0;
  568. /// \brief Get previous name in the zone
  569. ///
  570. /// Gets the previous name in the DNSSEC order. This can be used
  571. /// to find the correct NSEC records for proving nonexistence
  572. /// of domains.
  573. ///
  574. /// The concrete implementation might throw anything it thinks appropriate,
  575. /// however it is recommended to stick to the ones listed here. The user
  576. /// of this method should be able to handle any exceptions.
  577. ///
  578. /// This method does not include under-zone-cut data (glue data).
  579. ///
  580. /// \param query The name for which one we look for a previous one. The
  581. /// queried name doesn't have to exist in the zone.
  582. /// \return The preceding name
  583. ///
  584. /// \throw NotImplemented in case the data source backend doesn't support
  585. /// DNSSEC or there is no previous in the zone (NSEC records might be
  586. /// missing in the DB, the queried name is less or equal to the apex).
  587. /// \throw DataSourceError for low-level or internal datasource errors
  588. /// (like broken connection to database, wrong data living there).
  589. /// \throw std::bad_alloc For allocation errors.
  590. virtual isc::dns::Name findPreviousName(const isc::dns::Name& query)
  591. const = 0;
  592. //@}
  593. };
  594. /// \brief Operator to combine FindOptions
  595. ///
  596. /// We would need to manually static-cast the options if we put or
  597. /// between them, which is undesired with bit-flag options. Therefore
  598. /// we hide the cast here, which is the simplest solution and it still
  599. /// provides reasonable level of type safety.
  600. inline ZoneFinder::FindOptions operator |(ZoneFinder::FindOptions a,
  601. ZoneFinder::FindOptions b)
  602. {
  603. return (static_cast<ZoneFinder::FindOptions>(static_cast<unsigned>(a) |
  604. static_cast<unsigned>(b)));
  605. }
  606. /// \brief Operator to combine FindResultFlags
  607. ///
  608. /// Similar to the same operator for \c FindOptions. Refer to the description
  609. /// of that function.
  610. inline ZoneFinder::FindResultFlags operator |(
  611. ZoneFinder::FindResultFlags a,
  612. ZoneFinder::FindResultFlags b)
  613. {
  614. return (static_cast<ZoneFinder::FindResultFlags>(
  615. static_cast<unsigned>(a) | static_cast<unsigned>(b)));
  616. }
  617. /// \brief A pointer-like type pointing to a \c ZoneFinder object.
  618. typedef boost::shared_ptr<ZoneFinder> ZoneFinderPtr;
  619. /// \brief A pointer-like type pointing to an immutable \c ZoneFinder object.
  620. typedef boost::shared_ptr<const ZoneFinder> ConstZoneFinderPtr;
  621. /// \brief A pointer-like type pointing to a \c ZoneFinder::Context object.
  622. typedef boost::shared_ptr<ZoneFinder::Context> ZoneFinderContextPtr;
  623. /// \brief A pointer-like type pointing to an immutable
  624. /// \c ZoneFinder::Context object.
  625. typedef boost::shared_ptr<ZoneFinder::Context> ConstZoneFinderContextPtr;
  626. /// The base class to make updates to a single zone.
  627. ///
  628. /// On construction, each derived class object will start a "transaction"
  629. /// for making updates to a specific zone (this means a constructor of
  630. /// a derived class would normally take parameters to identify the zone
  631. /// to be updated). The underlying realization of a "transaction" will differ
  632. /// for different derived classes; if it uses a general purpose database
  633. /// as a backend, it will involve performing some form of "begin transaction"
  634. /// statement for the database.
  635. ///
  636. /// Updates (adding or deleting RRs) are made via \c addRRset() and
  637. /// \c deleteRRset() methods. Until the \c commit() method is called the
  638. /// changes are local to the updater object. For example, they won't be
  639. /// visible via a \c ZoneFinder object except the one returned by the
  640. /// updater's own \c getFinder() method. The \c commit() completes the
  641. /// transaction and makes the changes visible to others.
  642. ///
  643. /// This class does not provide an explicit "rollback" interface. If
  644. /// something wrong or unexpected happens during the updates and the
  645. /// caller wants to cancel the intermediate updates, the caller should
  646. /// simply destruct the updater object without calling \c commit().
  647. /// The destructor is supposed to perform the "rollback" operation,
  648. /// depending on the internal details of the derived class.
  649. ///
  650. /// \note This initial implementation provides a quite simple interface of
  651. /// adding and deleting RRs (see the description of the related methods).
  652. /// It may be revisited as we gain more experiences.
  653. class ZoneUpdater {
  654. protected:
  655. /// The default constructor.
  656. ///
  657. /// This is intentionally defined as protected to ensure that this base
  658. /// class is never instantiated directly.
  659. ZoneUpdater() {}
  660. public:
  661. /// The destructor
  662. ///
  663. /// Each derived class implementation must ensure that if \c commit()
  664. /// has not been performed by the time of the call to it, then it
  665. /// "rollbacks" the updates made via the updater so far.
  666. virtual ~ZoneUpdater() {}
  667. /// Return a finder for the zone being updated.
  668. ///
  669. /// The returned finder provides the functionalities of \c ZoneFinder
  670. /// for the zone as updates are made via the updater. That is, before
  671. /// making any update, the finder will be able to find all RRsets that
  672. /// exist in the zone at the time the updater is created. If RRsets
  673. /// are added or deleted via \c addRRset() or \c deleteRRset(),
  674. /// this finder will find the added ones or miss the deleted ones
  675. /// respectively.
  676. ///
  677. /// The finder returned by this method is effective only while the updates
  678. /// are performed, i.e., from the construction of the corresponding
  679. /// updater until \c commit() is performed or the updater is destructed
  680. /// without commit. The result of a subsequent call to this method (or
  681. /// the use of the result) after that is undefined.
  682. ///
  683. /// \return A reference to a \c ZoneFinder for the updated zone
  684. virtual ZoneFinder& getFinder() = 0;
  685. /// Add an RRset to a zone via the updater
  686. ///
  687. /// This may be revisited in a future version, but right now the intended
  688. /// behavior of this method is simple: It "naively" adds the specified
  689. /// RRset to the zone specified on creation of the updater.
  690. /// It performs minimum level of validation on the specified RRset:
  691. /// - Whether the RR class is identical to that for the zone to be updated
  692. /// - Whether the RRset is not empty, i.e., it has at least one RDATA
  693. /// - Whether the RRset is not associated with an RRSIG, i.e.,
  694. /// whether \c getRRsig() on the RRset returns a NULL pointer.
  695. ///
  696. /// and otherwise does not check any oddity. For example, it doesn't
  697. /// check whether the owner name of the specified RRset is a subdomain
  698. /// of the zone's origin; it doesn't care whether or not there is already
  699. /// an RRset of the same name and RR type in the zone, and if there is,
  700. /// whether any of the existing RRs have duplicate RDATA with the added
  701. /// ones. If these conditions matter the calling application must examine
  702. /// the existing data beforehand using the \c ZoneFinder returned by
  703. /// \c getFinder().
  704. ///
  705. /// The validation requirement on the associated RRSIG is temporary.
  706. /// If we find it more reasonable and useful to allow adding a pair of
  707. /// RRset and its RRSIG RRset as we gain experiences with the interface,
  708. /// we may remove this restriction. Until then we explicitly check it
  709. /// to prevent accidental misuse.
  710. ///
  711. /// Conceptually, on successful call to this method, the zone will have
  712. /// the specified RRset, and if there is already an RRset of the same
  713. /// name and RR type, these two sets will be "merged". "Merged" means
  714. /// that a subsequent call to \c ZoneFinder::find() for the name and type
  715. /// will result in success and the returned RRset will contain all
  716. /// previously existing and newly added RDATAs with the TTL being the
  717. /// minimum of the two RRsets. The underlying representation of the
  718. /// "merged" RRsets may vary depending on the characteristic of the
  719. /// underlying data source. For example, if it uses a general purpose
  720. /// database that stores each RR of the same RRset separately, it may
  721. /// simply be a larger sets of RRs based on both the existing and added
  722. /// RRsets; the TTLs of the RRs may be different within the database, and
  723. /// there may even be duplicate RRs in different database rows. As long
  724. /// as the RRset returned via \c ZoneFinder::find() conforms to the
  725. /// concept of "merge", the actual internal representation is up to the
  726. /// implementation.
  727. ///
  728. /// This method must not be called once commit() is performed. If it
  729. /// calls after \c commit() the implementation must throw a
  730. /// \c DataSourceError exception.
  731. ///
  732. /// If journaling was requested when getting this updater, it will reject
  733. /// to add the RRset if the squence doesn't look like and IXFR (see
  734. /// DataSourceClient::getUpdater). In such case isc::BadValue is thrown.
  735. ///
  736. /// \todo As noted above we may have to revisit the design details as we
  737. /// gain experiences:
  738. ///
  739. /// - we may want to check (and maybe reject) if there is already a
  740. /// duplicate RR (that has the same RDATA).
  741. /// - we may want to check (and maybe reject) if there is already an
  742. /// RRset of the same name and RR type with different TTL
  743. /// - we may even want to check if there is already any RRset of the
  744. /// same name and RR type.
  745. /// - we may want to add an "options" parameter that can control the
  746. /// above points
  747. /// - we may want to have this method return a value containing the
  748. /// information on whether there's a duplicate, etc.
  749. ///
  750. /// \exception DataSourceError Called after \c commit(), RRset is invalid
  751. /// (see above), internal data source error
  752. /// \exception isc::BadValue Journaling is enabled and the current RRset
  753. /// doesn't fit into the IXFR sequence (see above).
  754. /// \exception std::bad_alloc Resource allocation failure
  755. ///
  756. /// \param rrset The RRset to be added
  757. virtual void addRRset(const isc::dns::AbstractRRset& rrset) = 0;
  758. /// Delete an RRset from a zone via the updater
  759. ///
  760. /// Like \c addRRset(), the detailed semantics and behavior of this method
  761. /// may have to be revisited in a future version. The following are
  762. /// based on the initial implementation decisions.
  763. ///
  764. /// On successful completion of this method, it will remove from the zone
  765. /// the RRs of the specified owner name and RR type that match one of
  766. /// the RDATAs of the specified RRset. There are several points to be
  767. /// noted:
  768. /// - Existing RRs that don't match any of the specified RDATAs will
  769. /// remain in the zone.
  770. /// - Any RRs of the specified RRset that doesn't exist in the zone will
  771. /// simply be ignored; the implementation of this method is not supposed
  772. /// to check that condition.
  773. /// - The TTL of the RRset is ignored; matching is only performed by
  774. /// the owner name, RR type and RDATA
  775. ///
  776. /// Ignoring the TTL may not look sensible, but it's based on the
  777. /// observation that it will result in more intuitive result, especially
  778. /// when the underlying data source is a general purpose database.
  779. /// See also \c DatabaseAccessor::deleteRecordInZone() on this point.
  780. /// It also matches the dynamic update protocol (RFC2136), where TTLs
  781. /// are ignored when deleting RRs.
  782. ///
  783. /// \note Since the TTL is ignored, this method could take the RRset
  784. /// to be deleted as a tuple of name, RR type, and a list of RDATAs.
  785. /// But in practice, it's quite likely that the caller has the RRset
  786. /// in the form of the \c RRset object (e.g., extracted from a dynamic
  787. /// update request message), so this interface would rather be more
  788. /// convenient. If it turns out not to be true we can change or extend
  789. /// the method signature.
  790. ///
  791. /// This method performs minimum level of validation on the specified
  792. /// RRset:
  793. /// - Whether the RR class is identical to that for the zone to be updated
  794. /// - Whether the RRset is not empty, i.e., it has at least one RDATA
  795. /// - Whether the RRset is not associated with an RRSIG, i.e.,
  796. /// whether \c getRRsig() on the RRset returns a NULL pointer.
  797. ///
  798. /// This method must not be called once commit() is performed. If it
  799. /// calls after \c commit() the implementation must throw a
  800. /// \c DataSourceError exception.
  801. ///
  802. /// If journaling was requested when getting this updater, it will reject
  803. /// to add the RRset if the squence doesn't look like and IXFR (see
  804. /// DataSourceClient::getUpdater). In such case isc::BadValue is thrown.
  805. ///
  806. /// \todo As noted above we may have to revisit the design details as we
  807. /// gain experiences:
  808. ///
  809. /// - we may want to check (and maybe reject) if some or all of the RRs
  810. /// for the specified RRset don't exist in the zone
  811. /// - we may want to allow an option to "delete everything" for specified
  812. /// name and/or specified name + RR type.
  813. /// - as mentioned above, we may want to include the TTL in matching the
  814. /// deleted RRs
  815. /// - we may want to add an "options" parameter that can control the
  816. /// above points
  817. /// - we may want to have this method return a value containing the
  818. /// information on whether there's any RRs that are specified but don't
  819. /// exit, the number of actually deleted RRs, etc.
  820. ///
  821. /// \exception DataSourceError Called after \c commit(), RRset is invalid
  822. /// (see above), internal data source error
  823. /// \exception isc::BadValue Journaling is enabled and the current RRset
  824. /// doesn't fit into the IXFR sequence (see above).
  825. /// \exception std::bad_alloc Resource allocation failure
  826. ///
  827. /// \param rrset The RRset to be deleted
  828. virtual void deleteRRset(const isc::dns::AbstractRRset& rrset) = 0;
  829. /// Commit the updates made in the updater to the zone
  830. ///
  831. /// This method completes the "transaction" started at the creation
  832. /// of the updater. After successful completion of this method, the
  833. /// updates will be visible outside the scope of the updater.
  834. /// The actual internal behavior will defer for different derived classes.
  835. /// For a derived class with a general purpose database as a backend,
  836. /// for example, this method would perform a "commit" statement for the
  837. /// database.
  838. ///
  839. /// This operation can only be performed at most once. A duplicate call
  840. /// must result in a DatasourceError exception.
  841. ///
  842. /// \exception DataSourceError Duplicate call of the method,
  843. /// internal data source error
  844. /// \exception isc::BadValue Journaling is enabled and the update is not
  845. /// complete IXFR sequence.
  846. virtual void commit() = 0;
  847. };
  848. /// \brief A pointer-like type pointing to a \c ZoneUpdater object.
  849. typedef boost::shared_ptr<ZoneUpdater> ZoneUpdaterPtr;
  850. /// The base class for retrieving differences between two versions of a zone.
  851. ///
  852. /// On construction, each derived class object will internally set up
  853. /// retrieving sequences of differences between two specific version of
  854. /// a specific zone managed in a particular data source. So the constructor
  855. /// of a derived class would normally take parameters to identify the zone
  856. /// and the two versions for which the differences should be retrieved.
  857. /// See \c DataSourceClient::getJournalReader for more concrete details
  858. /// used in this API.
  859. ///
  860. /// Once constructed, an object of this class will act like an iterator
  861. /// over the sequences. Every time the \c getNextDiff() method is called
  862. /// it returns one element of the differences in the form of an \c RRset
  863. /// until it reaches the end of the entire sequences.
  864. class ZoneJournalReader {
  865. public:
  866. /// Result codes used by a factory method for \c ZoneJournalReader
  867. enum Result {
  868. SUCCESS, ///< A \c ZoneJournalReader object successfully created
  869. NO_SUCH_ZONE, ///< Specified zone does not exist in the data source
  870. NO_SUCH_VERSION ///< Specified versions do not exist in the diff storage
  871. };
  872. protected:
  873. /// The default constructor.
  874. ///
  875. /// This is intentionally defined as protected to ensure that this base
  876. /// class is never instantiated directly.
  877. ZoneJournalReader() {}
  878. public:
  879. /// The destructor
  880. virtual ~ZoneJournalReader() {}
  881. /// Return the next difference RR of difference sequences.
  882. ///
  883. /// In this API, the difference between two versions of a zone is
  884. /// conceptually represented as IXFR-style difference sequences:
  885. /// Each difference sequence is a sequence of RRs: an older version of
  886. /// SOA (to be deleted), zero or more other deleted RRs, the
  887. /// post-transaction SOA (to be added), and zero or more other
  888. /// added RRs. (Note, however, that the underlying data source
  889. /// implementation may or may not represent the difference in
  890. /// straightforward realization of this concept. The mapping between
  891. /// the conceptual difference and the actual implementation is hidden
  892. /// in each derived class).
  893. ///
  894. /// This method provides an application with a higher level interface
  895. /// to retrieve the difference along with the conceptual model: the
  896. /// \c ZoneJournalReader object iterates over the entire sequences
  897. /// from the beginning SOA (which is to be deleted) to one of the
  898. /// added RR of with the ending SOA, and each call to this method returns
  899. /// one RR in the form of an \c RRset that contains exactly one RDATA
  900. /// in the order of the sequences.
  901. ///
  902. /// Note that the ordering of the sequences specifies the semantics of
  903. /// each difference: add or delete. For example, the first RR is to
  904. /// be deleted, and the last RR is to be added. So the return value
  905. /// of this method does not explicitly indicate whether the RR is to be
  906. /// added or deleted.
  907. ///
  908. /// This method ensures the returned \c RRset represents an RR, that is,
  909. /// it contains exactly one RDATA. However, it does not necessarily
  910. /// ensure that the resulting sequences are in the form of IXFR-style.
  911. /// For example, the first RR is supposed to be an SOA, and it should
  912. /// normally be the case, but this interface does not necessarily require
  913. /// the derived class implementation ensure this. Normally the
  914. /// differences are expected to be stored using this API (via a
  915. /// \c ZoneUpdater object), and as long as that is the case and the
  916. /// underlying implementation follows the requirement of the API, the
  917. /// result of this method should be a valid IXFR-style sequences.
  918. /// So this API does not mandate the almost redundant check as part of
  919. /// the interface. If the application needs to make it sure 100%, it
  920. /// must check the resulting sequence itself.
  921. ///
  922. /// Once the object reaches the end of the sequences, this method returns
  923. /// \c Null. Any subsequent call will result in an exception of
  924. /// class \c InvalidOperation.
  925. ///
  926. /// \exception InvalidOperation The method is called beyond the end of
  927. /// the difference sequences.
  928. /// \exception DataSourceError Underlying data is broken and the RR
  929. /// cannot be created or other low level data source error.
  930. ///
  931. /// \return An \c RRset that contains one RDATA corresponding to the
  932. /// next difference in the sequences.
  933. virtual isc::dns::ConstRRsetPtr getNextDiff() = 0;
  934. };
  935. /// \brief A pointer-like type pointing to a \c ZoneUpdater object.
  936. typedef boost::shared_ptr<ZoneJournalReader> ZoneJournalReaderPtr;
  937. } // end of datasrc
  938. } // end of isc
  939. #endif // __ZONE_H
  940. // Local Variables:
  941. // mode: c++
  942. // End: