database.h 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. // Copyright (C) 2011 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 DATABASE_DATASRC_H
  15. #define DATABASE_DATASRC_H
  16. #include <string>
  17. #include <boost/scoped_ptr.hpp>
  18. #include <boost/tuple/tuple.hpp>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrset.h>
  21. #include <dns/rrtype.h>
  22. #include <datasrc/data_source.h>
  23. #include <datasrc/client.h>
  24. #include <datasrc/zone.h>
  25. #include <datasrc/logger.h>
  26. #include <dns/name.h>
  27. #include <exceptions/exceptions.h>
  28. #include <map>
  29. #include <set>
  30. namespace isc {
  31. namespace datasrc {
  32. /// \brief Abstraction of lowlevel database with DNS data
  33. ///
  34. /// This class is defines interface to databases. Each supported database
  35. /// will provide methods for accessing the data stored there in a generic
  36. /// manner. The methods are meant to be low-level, without much or any knowledge
  37. /// about DNS and should be possible to translate directly to queries.
  38. ///
  39. /// On the other hand, how the communication with database is done and in what
  40. /// schema (in case of relational/SQL database) is up to the concrete classes.
  41. ///
  42. /// This class is non-copyable, as copying connections to database makes little
  43. /// sense and will not be needed.
  44. ///
  45. /// \todo Is it true this does not need to be copied? For example the zone
  46. /// iterator might need it's own copy. But a virtual clone() method might
  47. /// be better for that than copy constructor.
  48. ///
  49. /// \note The same application may create multiple connections to the same
  50. /// database, having multiple instances of this class. If the database
  51. /// allows having multiple open queries at one connection, the connection
  52. /// class may share it.
  53. class DatabaseAccessor : boost::noncopyable {
  54. public:
  55. /// \brief Data columns for by IteratorContext::getNext()
  56. ///
  57. /// When implementing getNext(), the columns array should be filled with
  58. /// the values as described in this enumeration, in this order, i.e.
  59. /// - TYPE_COLUMN should be the first element (index 0) of the array,
  60. /// - TTL_COLUMN should be the second element (index 1),
  61. /// - etc.
  62. enum RecordColumns {
  63. TYPE_COLUMN = 0, ///< The RRType of the record (A/NS/TXT etc.)
  64. TTL_COLUMN = 1, ///< The TTL of the record (a
  65. SIGTYPE_COLUMN = 2, ///< For RRSIG records, this contains the RRTYPEs
  66. ///< the RRSIG cover. In the current implementation,
  67. ///< this field is ignored.
  68. RDATA_COLUMN = 3, ///< Full text representation of the record's RDATA
  69. NAME_COLUMN = 4, ///< The domain name of this RR
  70. COLUMN_COUNT = 5 ///< The total number of columns, MUST be value of
  71. ///< the largest other element in this enum plus 1.
  72. };
  73. /// \brief Definitions of the fields to be passed to addRecordToZone()
  74. ///
  75. /// Each derived implementation of addRecordToZone() should expect
  76. /// the "columns" array to be filled with the values as described in this
  77. /// enumeration, in this order.
  78. enum AddRecordColumns {
  79. ADD_NAME = 0, ///< The owner name of the record (a domain name)
  80. ADD_REV_NAME = 1, ///< Reversed name of NAME (used for DNSSEC)
  81. ADD_TTL = 2, ///< The TTL of the record (in numeric form)
  82. ADD_TYPE = 3, ///< The RRType of the record (A/NS/TXT etc.)
  83. ADD_SIGTYPE = 4, ///< RRSIGs only: RRTYPEs the RRSIG covers.
  84. ADD_RDATA = 5, ///< Full text representation of the record's RDATA
  85. ADD_COLUMN_COUNT = 6 ///< Number of columns
  86. };
  87. /// \brief Definitions of the fields to be passed to addNSEC3RecordToZone()
  88. ///
  89. /// Each derived implementation of addNSEC3RecordToZone() should expect
  90. /// the "columns" array to be filled with the values as described in this
  91. /// enumeration, in this order.
  92. ///
  93. /// Note that there is no "reversed name" column. Since the conceptual
  94. /// separate namespace for NSEC3 is very simplified and essentially only
  95. /// consists of a single-label names, there is no need for using reversed
  96. /// names to identify the "previous hash".
  97. enum AddNSEC3RecordColumns {
  98. ADD_NSEC3_HASH = 0, ///< The hash (1st) label of the owner name,
  99. ///< excluding the dot character
  100. ADD_NSEC3_TTL = 1, ///< The TTL of the record (in numeric form)
  101. ADD_NSEC3_TYPE = 2, ///< The RRType of the record (either NSEC3 or
  102. ///< RRSIG for NSEC3)
  103. ADD_NSEC3_RDATA = 3, ///< Full text representation of the record's
  104. ///< RDATA
  105. ADD_NSEC3_COLUMN_COUNT = 4 ///< Number of columns
  106. };
  107. /// \brief Definitions of the fields to be passed to deleteRecordInZone().
  108. ///
  109. /// Each derived implementation of deleteRecordInZone() should expect
  110. /// the "params" array to be filled with the values as described in this
  111. /// enumeration, in this order.
  112. ///
  113. /// DEL_RNAME is included in case the reversed form is more convenient
  114. /// for the underlying implementation to identify the record to be
  115. /// deleted (reversed names are generally easier to sort, which may help
  116. /// perform the search faster). It's up to the underlying implementation
  117. /// which one (or both) it uses for the search. DEL_NAME and DEL_RNAME
  118. /// are mutually convertible with the understanding of DNS names, and
  119. /// in that sense redundant. But both are provided so the underlying
  120. /// implementation doesn't have to deal with DNS level concepts.
  121. enum DeleteRecordParams {
  122. DEL_NAME = 0, ///< The owner name of the record (a domain name).
  123. DEL_TYPE = 1, ///< The RRType of the record (A/NS/TXT etc.)
  124. DEL_RDATA = 2, ///< Full text representation of the record's RDATA
  125. DEL_RNAME = 3, ///< As DEL_NAME, but with the labels of domain name
  126. ///< in reverse order (eg. org.example.).
  127. DEL_PARAM_COUNT = 4 ///< Number of parameters
  128. };
  129. /// \brief Definitions of the fields to be passed to
  130. /// deleteNSEC3RecordInZone().
  131. ///
  132. /// Each derived implementation of deleteNSEC3RecordInZone() should expect
  133. /// the "params" array to be filled with the values as described in this
  134. /// enumeration, in this order.
  135. enum DeleteNSEC3RecordParams {
  136. DEL_NSEC3_HASH = 0, ///< The hash (1st) label of the owren name,
  137. ///< excluding the dot character.
  138. DEL_NSEC3_TYPE = 1, ///< The type of RR. Either RRSIG or NSEC3.
  139. DEL_NSEC3_RDATA = 2, ///< Full text representation of the record's
  140. ///< RDATA. Must match the one in the database.
  141. DEL_NSEC3_PARAM_COUNT = 3 ///< Number of parameters.
  142. };
  143. /// \brief Operation mode when adding a record diff.
  144. ///
  145. /// This is used as the "operation" parameter value of addRecordDiff().
  146. enum DiffOperation {
  147. DIFF_ADD = 0, ///< This diff is for adding an RR
  148. DIFF_DELETE = 1 ///< This diff is for deleting an RR
  149. };
  150. /// \brief Definitions of the fields to be passed to addRecordDiff().
  151. ///
  152. /// Each derived implementation of addRecordDiff() should expect
  153. /// the "params" array to be filled with the values as described in this
  154. /// enumeration, in this order.
  155. enum DiffRecordParams {
  156. DIFF_NAME = 0, ///< Owner name of the record (a domain name)
  157. DIFF_TYPE = 1, ///< The RRType of the record (A/NS/TXT etc.)
  158. DIFF_TTL = 2, ///< The TTL of the record (in numeric form)
  159. DIFF_RDATA = 3, ///< Full text representation of record's RDATA
  160. DIFF_PARAM_COUNT = 4 ///< Number of parameters
  161. };
  162. /// \brief Destructor
  163. ///
  164. /// It is empty, but needs a virtual one, since we will use the derived
  165. /// classes in polymorphic way.
  166. virtual ~DatabaseAccessor() {}
  167. /// \brief Retrieve a zone identifier
  168. ///
  169. /// This method looks up a zone for the given name in the database. It
  170. /// should match only exact zone name (eg. name is equal to the zone's
  171. /// apex), as the DatabaseClient will loop through the labels itself and
  172. /// find the most suitable zone.
  173. ///
  174. /// It is not specified if and what implementation of this method may
  175. /// throw, so code should expect anything.
  176. ///
  177. /// \param name The (fully qualified) domain name of the zone's apex to be
  178. /// looked up.
  179. /// \return The first part of the result indicates if a matching zone
  180. /// was found. In case it was, the second part is internal zone ID.
  181. /// This one will be passed to methods finding data in the zone.
  182. /// It is not required to keep them, in which case whatever might
  183. /// be returned - the ID is only passed back to the database as
  184. /// an opaque handle.
  185. virtual std::pair<bool, int> getZone(const std::string& name) const = 0;
  186. /// \brief Add a new zone to the database
  187. ///
  188. /// This method creates a new (and empty) zone in the database.
  189. ///
  190. /// Like for addRecordToZone, implementations are not required to
  191. /// check for the existence of the given zone name, it is the
  192. /// responsibility of the caller to do so.
  193. ///
  194. /// Callers must also start a transaction before calling this method,
  195. /// implementations should throw DataSourceError if this has not been
  196. /// done. Callers should also expect DataSourceErrors for other potential
  197. /// problems.
  198. ///
  199. /// \param name The (fully qualified) domain name of the zone to add.
  200. /// \return The internal zone id of the zone (whether is existed already
  201. /// or was created by this call).
  202. virtual int addZone(const std::string& name) = 0;
  203. /// \brief Delete a zone from the database
  204. ///
  205. /// Like for deleteRecordToZone, implementations are not required to
  206. /// check for the existence of the given zone name, it is the
  207. /// responsibility of the caller to do so.
  208. ///
  209. /// Callers must also start a transaction before calling this method.
  210. /// Implementations should throw InvalidOperation if this has not been
  211. /// done. Callers should also expect DataSourceError for other potential
  212. /// problems specific to the database.
  213. ///
  214. /// \note This method does not delete other database records related to
  215. /// the zone. See \c DataSourceClient::deleteZone for the rationale.
  216. ///
  217. /// \param zone_id The ID of the zone, that would be returned by getZone().
  218. virtual void deleteZone(int zone_id) = 0;
  219. /// \brief This holds the internal context of ZoneIterator for databases
  220. ///
  221. /// While the ZoneIterator implementation from DatabaseClient does all the
  222. /// translation from strings to DNS classes and validation, this class
  223. /// holds the pointer to where the database is at reading the data.
  224. ///
  225. /// It can either hold shared pointer to the connection which created it
  226. /// and have some kind of statement inside (in case single database
  227. /// connection can handle multiple concurrent SQL statements) or it can
  228. /// create a new connection (or, if it is more convenient, the connection
  229. /// itself can inherit both from DatabaseConnection and IteratorContext
  230. /// and just clone itself).
  231. class IteratorContext : public boost::noncopyable {
  232. public:
  233. /// \brief Destructor
  234. ///
  235. /// Virtual destructor, so any descendant class is destroyed correctly.
  236. virtual ~IteratorContext() {}
  237. /// \brief Function to provide next resource record
  238. ///
  239. /// This function should provide data about the next resource record
  240. /// from the data that is searched. The data is not converted yet.
  241. ///
  242. /// Depending on how the iterator was constructed, there is a
  243. /// difference in behaviour; for a 'full zone iterator', created with
  244. /// getAllRecords(), all COLUMN_COUNT elements of the array are
  245. /// overwritten.
  246. /// For a 'name iterator', created with getRecords(), the column
  247. /// NAME_COLUMN is untouched, since what would be added here is by
  248. /// definition already known to the caller (it already passes it as
  249. /// an argument to getRecords()).
  250. ///
  251. /// Once this function returns false, any subsequent call to it should
  252. /// result in false. The implementation of a derived class must ensure
  253. /// it doesn't cause any disruption due to that such as a crash or
  254. /// exception.
  255. ///
  256. /// \note The order of RRs is not strictly set, but the RRs for single
  257. /// RRset must not be interleaved with any other RRs (eg. RRsets must be
  258. /// "together").
  259. ///
  260. /// \param columns The data will be returned through here. The order
  261. /// is specified by the RecordColumns enum, and the size must be
  262. /// COLUMN_COUNT
  263. /// \todo Do we consider databases where it is stored in binary blob
  264. /// format?
  265. /// \throw DataSourceError if there's database-related error. If the
  266. /// exception (or any other in case of derived class) is thrown,
  267. /// the iterator can't be safely used any more.
  268. /// \return true if a record was found, and the columns array was
  269. /// updated. false if there was no more data, in which case
  270. /// the columns array is untouched.
  271. virtual bool getNext(std::string (&columns)[COLUMN_COUNT]) = 0;
  272. };
  273. typedef boost::shared_ptr<IteratorContext> IteratorContextPtr;
  274. /// \brief Creates an iterator context for a specific name.
  275. ///
  276. /// Returns an IteratorContextPtr that contains all records of the
  277. /// given name from the given zone.
  278. ///
  279. /// The implementation of the iterator that is returned may leave the
  280. /// NAME_COLUMN column of the array passed to getNext() untouched, as that
  281. /// data is already known (it is the same as the name argument here)
  282. ///
  283. /// \exception any Since any implementation can be used, the caller should
  284. /// expect any exception to be thrown.
  285. ///
  286. /// \param name The name to search for. This should be a FQDN.
  287. /// \param id The ID of the zone, returned from getZone().
  288. /// \param subdomains If set to true, match subdomains of name instead
  289. /// of name itself. It is used to find empty domains and match
  290. /// wildcards.
  291. /// \return Newly created iterator context. Must not be NULL.
  292. virtual IteratorContextPtr getRecords(const std::string& name,
  293. int id,
  294. bool subdomains = false) const = 0;
  295. /// \brief Creates an iterator context for the records of NSEC3 namespace
  296. /// for the given hash
  297. ///
  298. /// Returns an Iteratorcontextptr that contains all the records of the given
  299. /// hash in the NSEC3 namespace of the given zone.
  300. ///
  301. /// The implementation of the iterator that is returned may leave the
  302. /// NAME_COLUMN column of the array passed to getNext() untouched,
  303. /// as that name is easy to construct on the caller side (both the
  304. /// hash and the name of the zone is known). The SIGTYPE_COLUMN can
  305. /// be omitted as well, as it would be always empty for NSEC3 RRs or
  306. /// contained "NSEC3" in case of RRSIG RRs.
  307. ///
  308. /// The iterator will contain both the NSEC3 records and the corresponding
  309. /// RRSIGs, in arbitrary order.
  310. ///
  311. /// The iterator might be empty (containing no RRs) in case the zone is not
  312. /// signed by NSEC3.
  313. ///
  314. /// \note In case there are multiple NSEC3 chains and they collide
  315. /// (unlikely, but it can happen), this can return multiple NSEC3
  316. /// records.
  317. /// \exception any Since any implementation can be used, the caller should
  318. /// expect any exception to be thrown.
  319. /// \exception isc::NotImplemented in case the database does not support
  320. /// NSEC3
  321. ///
  322. /// \param hash The hash part of the NSEC3 name (eg. for a name of NSEC3
  323. /// RKBUCQT8T78GV6QBCGBHCHC019LG73SJ.example.com., we the hash would be
  324. /// RKBUCQT8T78GV6QBCGBHCHC019LG73SJ).
  325. /// \param id The id of te zone, as returned from getZone().
  326. /// \return Newly created iterator context. Must not be NULL.
  327. virtual IteratorContextPtr getNSEC3Records(const std::string& hash,
  328. int id) const = 0;
  329. /// \brief Creates an iterator context for the whole zone.
  330. ///
  331. /// Returns an IteratorContextPtr that contains all records of the
  332. /// zone with the given zone id.
  333. ///
  334. /// Each call to getNext() on the returned iterator should copy all
  335. /// column fields of the array that is passed, as defined in the
  336. /// RecordColumns enum.
  337. ///
  338. /// \exception any Since any implementation can be used, the caller should
  339. /// expect any exception to be thrown.
  340. ///
  341. /// \param id The ID of the zone, returned from getZone().
  342. /// \return Newly created iterator context. Must not be NULL.
  343. virtual IteratorContextPtr getAllRecords(int id) const = 0;
  344. /// \brief Creates an iterator context for a set of differences.
  345. ///
  346. /// Returns an IteratorContextPtr that contains all difference records for
  347. /// the given zone between two versions of a zone.
  348. ///
  349. /// The difference records are the set of records that would appear in an
  350. /// IXFR serving a request for the difference between two versions of a
  351. /// zone. The records are returned in the same order as they would be in
  352. /// the IXFR. This means that if the the difference between versions of a
  353. /// zone with SOA serial numbers of "start" and "end" is required, and the
  354. /// zone contains the differences between serial number "start" to serial
  355. /// number "intermediate" and from serial number "intermediate" to serial
  356. /// number "end", the returned records will be (in order):
  357. ///
  358. /// \li SOA for serial "start"
  359. /// \li Records removed from the zone between versions "start" and
  360. /// "intermediate" of the zone. The order of these is not guaranteed.
  361. /// \li SOA for serial "intermediate"
  362. /// \li Records added to the zone between versions "start" and
  363. /// "intermediate" of the zone. The order of these is not guaranteed.
  364. /// \li SOA for serial "intermediate"
  365. /// \li Records removed from the zone between versions "intermediate" and
  366. /// "end" of the zone. The order of these is not guaranteed.
  367. /// \li SOA for serial "end"
  368. /// \li Records added to the zone between versions "intermediate" and "end"
  369. /// of the zone. The order of these is not guaranteed.
  370. ///
  371. /// Note that there is no requirement that "start" be less than "end".
  372. /// Owing to serial number arithmetic, it is entirely possible that a later
  373. /// version of a zone will have a smaller SOA serial number than an earlier
  374. /// version.
  375. ///
  376. /// Each call to getNext() on the returned iterator should copy all column
  377. /// fields of the array that is passed, as defined in the RecordColumns
  378. /// enum.
  379. ///
  380. /// \exception any Since any implementation can be used, the caller should
  381. /// expect any exception to be thrown.
  382. ///
  383. /// \param id The ID of the zone, returned from getZone().
  384. /// \param start The SOA serial number of the version of the zone from
  385. /// which the difference sequence should start.
  386. /// \param end The SOA serial number of the version of the zone at which
  387. /// the difference sequence should end.
  388. ///
  389. /// \return Newly created iterator context. Must not be NULL.
  390. virtual IteratorContextPtr
  391. getDiffs(int id, uint32_t start, uint32_t end) const = 0;
  392. /// \brief Start a transaction for updating a zone.
  393. ///
  394. /// Each derived class version of this method starts a database
  395. /// transaction to make updates to the given name of zone (whose class was
  396. /// specified at the construction of the class).
  397. ///
  398. /// If \c replace is true, any existing records of the zone will be
  399. /// deleted on successful completion of updates (after
  400. /// \c commitUpdateZone()); if it's false, the existing records will be
  401. /// intact unless explicitly deleted by \c deleteRecordInZone().
  402. ///
  403. /// A single \c DatabaseAccessor instance can perform at most one
  404. /// transaction; a duplicate call to this method before
  405. /// \c commitUpdateZone() or \c rollbackUpdateZone(), or a call to this
  406. /// method within another transaction started by \c startTransaction()
  407. /// will result in a \c DataSourceError exception.
  408. /// If multiple update attempts need to be performed concurrently (and
  409. /// if the underlying database allows such operation), separate
  410. /// \c DatabaseAccessor instance must be created.
  411. ///
  412. /// \note The underlying database may not allow concurrent updates to
  413. /// the same database instance even if different "connections" (or
  414. /// something similar specific to the database implementation) are used
  415. /// for different sets of updates. For example, it doesn't seem to be
  416. /// possible for SQLite3 unless different databases are used. MySQL
  417. /// allows concurrent updates to different tables of the same database,
  418. /// but a specific operation may block others. As such, this interface
  419. /// doesn't require derived classes to allow concurrent updates with
  420. /// multiple \c DatabaseAccessor instances; however, the implementation
  421. /// is encouraged to do the best for making it more likely to succeed
  422. /// as long as the underlying database system allows concurrent updates.
  423. ///
  424. /// This method returns a pair of \c bool and \c int. Its first element
  425. /// indicates whether the given name of zone is found. If it's false,
  426. /// the transaction isn't considered to be started; a subsequent call to
  427. /// this method with an existing zone name should succeed. Likewise,
  428. /// if a call to this method results in an exception, the transaction
  429. /// isn't considered to be started. Note also that if the zone is not
  430. /// found this method doesn't try to create a new one in the database.
  431. /// It must have been created by some other means beforehand.
  432. ///
  433. /// The second element is the internal zone ID used for subsequent
  434. /// updates. Depending on implementation details of the actual derived
  435. /// class method, it may be different from the one returned by
  436. /// \c getZone(); for example, a specific implementation may use a
  437. /// completely new zone ID when \c replace is true.
  438. ///
  439. /// \exception DataSourceError Duplicate call to this method, call to
  440. /// this method within another transaction, or some internal database
  441. /// related error.
  442. ///
  443. /// \param zone_name A string representation of the zone name to be updated
  444. /// \param replace Whether to replace the entire zone (see above)
  445. ///
  446. /// \return A pair of bool and int, indicating whether the specified zone
  447. /// exists and (if so) the zone ID to be used for the update, respectively.
  448. virtual std::pair<bool, int> startUpdateZone(const std::string& zone_name,
  449. bool replace) = 0;
  450. /// \brief Add a single record to the zone to be updated.
  451. ///
  452. /// This method provides a simple interface to insert a new record
  453. /// (a database "row") to the zone in the update context started by
  454. /// \c startUpdateZone(). The zone to which the record to be added
  455. /// is the one specified at the time of the \c startUpdateZone() call.
  456. ///
  457. /// A successful call to \c startUpdateZone() must have preceded to
  458. /// this call; otherwise a \c DataSourceError exception will be thrown.
  459. ///
  460. /// The row is defined as a vector of strings that has exactly
  461. /// ADD_COLUMN_COUNT number of elements. See AddRecordColumns for
  462. /// the semantics of each element.
  463. ///
  464. /// Derived class methods are not required to check whether the given
  465. /// values in \c columns are valid in terms of the expected semantics;
  466. /// in general, it's the caller's responsibility.
  467. /// For example, TTLs would normally be expected to be a textual
  468. /// representation of decimal numbers, but this interface doesn't require
  469. /// the implementation to perform this level of validation. It may check
  470. /// the values, however, and in that case if it detects an error it
  471. /// should throw a \c DataSourceError exception.
  472. ///
  473. /// Likewise, derived class methods are not required to detect any
  474. /// duplicate record that is already in the zone.
  475. ///
  476. /// \note The underlying database schema may not have a trivial mapping
  477. /// from this style of definition of rows to actual database records.
  478. /// It's the implementation's responsibility to implement the mapping
  479. /// in the actual derived method.
  480. ///
  481. /// \exception DataSourceError Invalid call without starting a transaction,
  482. /// or other internal database error.
  483. ///
  484. /// \param columns An array of strings that defines a record to be added
  485. /// to the zone.
  486. virtual void addRecordToZone(
  487. const std::string (&columns)[ADD_COLUMN_COUNT]) = 0;
  488. /// \brief Add a single NSEC3-related record to the zone to be updated.
  489. ///
  490. /// This method is similar to \c addRecordToZone(), but is expected to
  491. /// be only used for NSEC3 RRs or RRSIG RRs that cover NSEC3. In terms
  492. /// of the DNS protocol, these types of RRs reside in a separate space
  493. /// of the zone. While this interface does not mandate a specific way
  494. /// of implementing the separate namespaces in the underlying database,
  495. /// it would be more convenient for the underlying implementation if the
  496. /// interfaces are separated; for example, the implementation does not
  497. /// have to examine the given data to identify the appropriate namespace.
  498. ///
  499. /// An implementation may choose to skip providing this interface if the
  500. /// zones managed by that data source are known to not support NSEC3.
  501. /// In that case the implementation should throw the
  502. /// \c isc::NotImplemented exception.
  503. ///
  504. /// Note that the \c ADD_NSEC3_HASH column of \c columns is expected to
  505. /// store only the hash label, not the entire owner name. This is similar
  506. /// to the \c hash parameter of \c getNSEC3Records().
  507. ///
  508. /// The RRs to be added using this method are expected to be limited to
  509. /// NSEC3 or RRSIG RRs that cover NSEC3, but it's generally assumed to
  510. /// be the caller's responsibility to ensure that; the implementation
  511. /// is not required to check that condition. The result of adding
  512. /// unexpected type of RRs (and the result of subsequent lookups) is
  513. /// undefined.
  514. ///
  515. /// Other general notes for \c addRecordToZone() also apply to this
  516. /// method.
  517. ///
  518. /// \exception DataSourceError Invalid call without starting a transaction,
  519. /// or other internal database error.
  520. /// \exception isc::NotImplemented in case the database does not support
  521. /// NSEC3
  522. ///
  523. /// \param columns An array of strings that defines a record to be added
  524. /// to the NSEC3 namespace of the zone.
  525. virtual void addNSEC3RecordToZone(
  526. const std::string (&columns)[ADD_NSEC3_COLUMN_COUNT]) = 0;
  527. /// \brief Delete a single record from the zone to be updated.
  528. ///
  529. /// This method provides a simple interface to delete a record
  530. /// (a database "row") from the zone in the update context started by
  531. /// \c startUpdateZone(). The zone from which the record to be deleted
  532. /// is the one specified at the time of the \c startUpdateZone() call.
  533. ///
  534. /// A successful call to \c startUpdateZone() must have preceded to
  535. /// this call; otherwise a \c DataSourceError exception will be thrown.
  536. ///
  537. /// The record to be deleted is specified by a vector of strings that has
  538. /// exactly DEL_PARAM_COUNT number of elements. See DeleteRecordParams
  539. /// for the semantics of each element.
  540. ///
  541. /// \note In IXFR, TTL may also be specified, but we intentionally
  542. /// ignore that in this interface, because it's not guaranteed
  543. /// that all records have the same TTL (unlike the RRset
  544. /// assumption) and there can even be multiple records for the
  545. /// same name, type and rdata with different TTLs. If we only
  546. /// delete one of them, subsequent lookup will still return a
  547. /// positive answer, which would be confusing. It's a higher
  548. /// layer's responsibility to check if there is at least one
  549. /// record in the database that has the given TTL.
  550. ///
  551. /// Like \c addRecordToZone, derived class methods are not required to
  552. /// validate the semantics of the given parameters or to check if there
  553. /// is a record that matches the specified parameter; if there isn't
  554. /// it simply ignores the result.
  555. ///
  556. /// \exception DataSourceError Invalid call without starting a transaction,
  557. /// or other internal database error.
  558. ///
  559. /// \param params An array of strings that defines a record to be deleted
  560. /// from the zone.
  561. virtual void deleteRecordInZone(
  562. const std::string (&params)[DEL_PARAM_COUNT]) = 0;
  563. /// \brief Delete a single NSEC3-related record from the zone to be
  564. /// updated.
  565. ///
  566. /// This method is similar to \c deleteRecordInZone(), but is expected to
  567. /// be only used for NSEC3 RRs or RRSIG RRs that cover NSEC3. The
  568. /// relationship between these two methods is similar to that between
  569. /// \c addRecordToZone() and \c addNSEC3RecordToZone(), and the same
  570. /// notes apply to this method.
  571. ///
  572. /// This method uses the \c DeleteNSEC3RecordParams enum to specify the
  573. /// values.
  574. ///
  575. /// \exception DataSourceError Invalid call without starting a transaction,
  576. /// or other internal database error.
  577. /// \exception isc::NotImplemented in case the database does not support
  578. /// NSEC3
  579. ///
  580. /// \param params An array of strings that defines a record to be deleted
  581. /// from the NSEC3 namespace of the zone.
  582. virtual void deleteNSEC3RecordInZone(
  583. const std::string (&params)[DEL_NSEC3_PARAM_COUNT]) = 0;
  584. /// \brief Start a general transaction.
  585. ///
  586. /// Each derived class version of this method starts a database
  587. /// transaction in a way specific to the database details. Any subsequent
  588. /// operations on the accessor are guaranteed to be not susceptible to
  589. /// any update attempts made during the transaction. The transaction
  590. /// must be terminated by either \c commit() or \c rollback().
  591. ///
  592. /// In practice, this transaction is intended to be used to perform
  593. /// a set of atomic reads and work as a read-only lock. So, in many
  594. /// cases \c commit() and \c rollback() will have the same effect.
  595. ///
  596. /// This transaction cannot coexist with an update transaction started
  597. /// by \c startUpdateZone(). Such an attempt will result in
  598. /// \c DataSourceError.
  599. ///
  600. /// \exception DataSourceError An attempt of nested transaction, or some
  601. /// internal database related error.
  602. virtual void startTransaction() = 0;
  603. /// \brief Commit a transaction.
  604. ///
  605. /// This method completes a transaction started by \c startTransaction
  606. /// or \c startUpdateZone.
  607. ///
  608. /// A successful call to one of the "start" methods must have preceded to
  609. /// this call; otherwise a \c DataSourceError exception will be thrown.
  610. /// Once this method successfully completes, the transaction isn't
  611. /// considered to exist any more. So a new transaction can now be
  612. /// started. On the other hand, a duplicate call to this method after
  613. /// a successful completion of it is invalid and should result in
  614. /// a \c DataSourceError exception.
  615. ///
  616. /// If some internal database error happens, a \c DataSourceError
  617. /// exception must be thrown. In that case the transaction is still
  618. /// considered to be valid; the caller must explicitly rollback it
  619. /// or (if it's confident that the error is temporary) try to commit it
  620. /// again.
  621. ///
  622. /// \exception DataSourceError Call without a transaction, duplicate call
  623. /// to the method or internal database error.
  624. virtual void commit() = 0;
  625. /// \brief Rollback any changes in a transaction made so far.
  626. ///
  627. /// This method rollbacks a transaction started by \c startTransaction or
  628. /// \c startUpdateZone. When it succeeds (it normally should, but see
  629. /// below), the underlying database should be reverted to the point
  630. /// before performing the corresponding "start" method.
  631. ///
  632. /// A successful call to one of the "start" method must have preceded to
  633. /// this call; otherwise a \c DataSourceError exception will be thrown.
  634. /// Once this method successfully completes, the transaction isn't
  635. /// considered to exist any more. So a new transaction can now be
  636. /// started. On the other hand, a duplicate call to this method after
  637. /// a successful completion of it is invalid and should result in
  638. /// a \c DataSourceError exception.
  639. ///
  640. /// Normally this method should not fail. But it may not always be
  641. /// possible to guarantee it depending on the characteristics of the
  642. /// underlying database system. So this interface doesn't require the
  643. /// actual implementation for the error free property. But if a specific
  644. /// implementation of this method can fail, it is encouraged to document
  645. /// when that can happen with its implication.
  646. ///
  647. /// \exception DataSourceError Call without a transaction, duplicate call
  648. /// to the method or internal database error.
  649. virtual void rollback() = 0;
  650. /// \brief Install a single RR diff in difference sequences for zone update.
  651. ///
  652. /// This method inserts parameters of an update operation for a single RR
  653. /// (either adding or deleting one) in the underlying database.
  654. /// (These parameters would normally be a separate database table, but
  655. /// actual realization can differ in specific implementations).
  656. /// The information given via this method generally corresponds to either
  657. /// a single call to \c addRecordToZone() or \c deleteRecordInZone(),
  658. /// and this method is expected to be called immediately after (or before)
  659. /// a call to either of those methods.
  660. ///
  661. /// Note, however, that this method passes more detailed information
  662. /// than those update methods: it passes "serial", even if the diff
  663. /// is not for the SOA RR; it passes TTL for a diff that deletes an RR
  664. /// while in \c deleteRecordInZone() it's omitted. This is because
  665. /// the stored diffs are expected to be retrieved in the form that
  666. /// \c getDiffs() is expected to meet. This means if the caller
  667. /// wants to use this method with other update operations, it must
  668. /// ensure the additional information is ready when this method is called.
  669. ///
  670. /// The caller of this method must ensure that the added diffs via
  671. /// this method in a single transaction form an IXFR-style difference
  672. /// sequences: Each difference sequence is a sequence of RRs:
  673. /// an older version of SOA (to be deleted), zero or more other deleted
  674. /// RRs, the post-transaction SOA (to be added), and zero or more other
  675. /// added RRs. So, for example, the first call to this method in a
  676. /// transaction must always be deleting an SOA. Also, the \c serial
  677. /// parameter must be equal to the value of the serial field of the
  678. /// SOA that was last added or deleted (if the call is to add or delete
  679. /// an SOA RR, \c serial must be identical to the serial of that SOA).
  680. /// The underlying derived class implementation may or may not check
  681. /// this condition, but if the caller doesn't meet the condition
  682. /// a subsequent call to \c getDiffs() will not work as expected.
  683. ///
  684. /// Any call to this method must be in a transaction, and, for now,
  685. /// it must be a transaction triggered by \c startUpdateZone() (that is,
  686. /// it cannot be a transaction started by \c startTransaction()).
  687. /// All calls to this method are considered to be part of an atomic
  688. /// transaction: Until \c commit() is performed, the added diffs are
  689. /// not visible outside the transaction; if \c rollback() is performed,
  690. /// all added diffs are canceled; and the added sequences are not
  691. /// affected by any concurrent attempt of adding diffs (conflict resolution
  692. /// is up to the database implementation).
  693. ///
  694. /// Also for now, all diffs are assumed to be for the zone that is
  695. /// being updated in the context of \c startUpdateZone(). So the
  696. /// \c zone_id parameter must be identical to the zone ID returned by
  697. /// \c startUpdateZone().
  698. ///
  699. /// In a future version we may loosen this condition so that diffs can be
  700. /// added in a generic transaction and may not even have to belong to
  701. /// a single zone. For this possible extension \c zone_id parameter is
  702. /// included even if it's redundant under the current restriction.
  703. ///
  704. /// The support for adding (or retrieving) diffs is optional; if it's
  705. /// not supported in a specific data source, this method for the
  706. /// corresponding derived class will throw an \c NotImplemented exception.
  707. ///
  708. /// \exception DataSourceError Invalid call without starting a transaction,
  709. /// zone ID doesn't match the zone being updated, or other internal
  710. /// database error.
  711. /// \exception NotImplemented Adding diffs is not supported in the
  712. /// data source.
  713. /// \exception Other The concrete derived method may throw other
  714. /// data source specific exceptions.
  715. ///
  716. /// \param zone_id The zone for the diff to be added.
  717. /// \param serial The SOA serial to which the diff belongs.
  718. /// \param operation Either \c DIFF_ADD or \c DIFF_DELETE.
  719. /// \param params An array of strings that defines a record for the diff.
  720. virtual void addRecordDiff(
  721. int zone_id, uint32_t serial, DiffOperation operation,
  722. const std::string (&params)[DIFF_PARAM_COUNT]) = 0;
  723. /// \brief Clone the accessor with the same configuration.
  724. ///
  725. /// Each derived class implementation of this method will create a new
  726. /// accessor of the same derived class with the same configuration
  727. /// (such as the database server address) as that of the caller object
  728. /// and return it.
  729. ///
  730. /// Note that other internal states won't be copied to the new accessor
  731. /// even though the name of "clone" may indicate so. For example, even
  732. /// if the calling accessor is in the middle of a update transaction,
  733. /// the new accessor will not start a transaction to trace the same
  734. /// updates.
  735. ///
  736. /// The intended use case of cloning is to create a separate context
  737. /// where a specific set of database operations can be performed
  738. /// independently from the original accessor. The updater will use it
  739. /// so that multiple updaters can be created concurrently even if the
  740. /// underlying database system doesn't allow running multiple transactions
  741. /// in a single database connection.
  742. ///
  743. /// The underlying database system may not support the functionality
  744. /// that would be needed to implement this method. For example, it
  745. /// may not allow a single thread (or process) to have more than one
  746. /// database connections. In such a case the derived class implementation
  747. /// should throw a \c DataSourceError exception.
  748. ///
  749. /// \return A shared pointer to the cloned accessor.
  750. virtual boost::shared_ptr<DatabaseAccessor> clone() = 0;
  751. /// \brief Returns a string identifying this dabase backend
  752. ///
  753. /// The returned string is mainly intended to be used for
  754. /// debugging/logging purposes.
  755. ///
  756. /// Any implementation is free to choose the exact string content,
  757. /// but it is advisable to make it a name that is distinguishable
  758. /// from the others.
  759. ///
  760. /// \return the name of the database
  761. virtual const std::string& getDBName() const = 0;
  762. /// \brief It returns the previous name in DNSSEC order.
  763. ///
  764. /// Gets the previous name in the DNSSEC order. This can be used
  765. /// to find the correct NSEC records for proving nonexistence
  766. /// of domains.
  767. ///
  768. /// \param rname The name to ask for previous of, in reversed form.
  769. /// We use the reversed form (see isc::dns::Name::reverse),
  770. /// because then the case insensitive order of string representation
  771. /// and the DNSSEC order correspond (eg. org.example.a is followed
  772. /// by org.example.a.b which is followed by org.example.b, etc).
  773. /// \param zone_id The zone to look through.
  774. /// \return The previous name.
  775. /// \note This function must return previous name even in case
  776. /// the queried rname does not exist in the zone.
  777. /// \note This method must skip under-the-zone-cut data (glue data).
  778. /// This might be implemented by looking for NSEC records (as glue
  779. /// data don't have them) in the zone or in some other way.
  780. ///
  781. /// \throw DataSourceError if there's a problem with the database.
  782. /// \throw NotImplemented if this database doesn't support DNSSEC
  783. /// or there's no previous name for the queried one (the NSECs
  784. /// might be missing or the queried name is less or equal the
  785. /// apex of the zone).
  786. virtual std::string findPreviousName(int zone_id,
  787. const std::string& rname) const = 0;
  788. /// \brief It returns the previous hash in the NSEC3 chain.
  789. ///
  790. /// This is used to find previous NSEC3 hashes, to find covering NSEC3 in
  791. /// case none match exactly.
  792. ///
  793. /// In case a hash before the lowest or the lowest is provided,
  794. /// this should return the largest one in the zone (NSEC3 needs a
  795. /// wrap-around semantics).
  796. ///
  797. /// \param zone_id Specifies the zone to look into, as returned by getZone.
  798. /// \param hash The hash to look before.
  799. /// \return The nearest smaller hash than the provided one, or the largest
  800. /// hash in the zone if something smaller or equal to the lowest one
  801. /// is provided.
  802. /// \note If the zone contains multiple NSEC3 chains, you should check that
  803. /// the returned result contains the NSEC3 for correct parameters. If
  804. /// not, query again and get something smaller - this will eventually
  805. /// get to the correct one. This interface and semantics might change
  806. /// in future.
  807. ///
  808. /// \throw DataSourceError if there's a problem with the database or if
  809. /// this zone is not signed with NSEC3.
  810. /// \throw NotImplemented if this database doesn't support NSEC3.
  811. /// \throw anything else, as this might be any implementation.
  812. virtual std::string findPreviousNSEC3Hash(int zone_id,
  813. const std::string& hash)
  814. const = 0;
  815. };
  816. /// \brief Concrete data source client oriented at database backends.
  817. ///
  818. /// This class (together with corresponding versions of ZoneFinder,
  819. /// ZoneIterator, etc.) translates high-level data source queries to
  820. /// low-level calls on DatabaseAccessor. It calls multiple queries
  821. /// if necessary and validates data from the database, allowing the
  822. /// DatabaseAccessor to be just simple translation to SQL/other
  823. /// queries to database.
  824. ///
  825. /// While it is possible to subclass it for specific database in case
  826. /// of special needs, it is not expected to be needed. This should just
  827. /// work as it is with whatever DatabaseAccessor.
  828. class DatabaseClient : public DataSourceClient {
  829. public:
  830. /// \brief Constructor
  831. ///
  832. /// It initializes the client with a database via the given accessor.
  833. ///
  834. /// \exception isc::InvalidParameter if accessor is NULL. It might throw
  835. /// standard allocation exception as well, but doesn't throw anything else.
  836. ///
  837. /// \param rrclass The RR class of the zones that this client will handle.
  838. /// \param accessor The accessor to the database to use to get data.
  839. /// As the parameter suggests, the client takes ownership of the accessor
  840. /// and will delete it when itself deleted.
  841. DatabaseClient(isc::dns::RRClass rrclass,
  842. boost::shared_ptr<DatabaseAccessor> accessor);
  843. /// \brief Corresponding ZoneFinder implementation
  844. ///
  845. /// The zone finder implementation for database data sources. Similarly
  846. /// to the DatabaseClient, it translates the queries to methods of the
  847. /// database.
  848. ///
  849. /// Application should not come directly in contact with this class
  850. /// (it should handle it through generic ZoneFinder pointer), therefore
  851. /// it could be completely hidden in the .cc file. But it is provided
  852. /// to allow testing and for rare cases when a database needs slightly
  853. /// different handling, so it can be subclassed.
  854. ///
  855. /// Methods directly corresponds to the ones in ZoneFinder.
  856. class Finder : public ZoneFinder {
  857. public:
  858. /// \brief Constructor
  859. ///
  860. /// \param database The database (shared with DatabaseClient) to
  861. /// be used for queries (the one asked for ID before).
  862. /// \param zone_id The zone ID which was returned from
  863. /// DatabaseAccessor::getZone and which will be passed to further
  864. /// calls to the database.
  865. /// \param origin The name of the origin of this zone. It could query
  866. /// it from database, but as the DatabaseClient just searched for
  867. /// the zone using the name, it should have it.
  868. Finder(boost::shared_ptr<DatabaseAccessor> database, int zone_id,
  869. const isc::dns::Name& origin);
  870. // The following three methods are just implementations of inherited
  871. // ZoneFinder's pure virtual methods.
  872. virtual isc::dns::Name getOrigin() const;
  873. virtual isc::dns::RRClass getClass() const;
  874. /// \brief Find an RRset in the datasource
  875. ///
  876. /// Searches the datasource for an RRset of the given name and
  877. /// type. If there is a CNAME at the given name, the CNAME rrset
  878. /// is returned.
  879. /// (this implementation is not complete, and currently only
  880. /// does full matches, CNAMES, and the signatures for matches and
  881. /// CNAMEs)
  882. ///
  883. /// \note Maybe counter intuitively, this method is not a const member
  884. /// function. This is intentional; some of the underlying
  885. /// implementations are expected to use a database backend, and would
  886. /// internally contain some abstraction of "database connection". In
  887. /// the most strict sense any (even read only) operation might change
  888. /// the internal state of such a connection, and in that sense the
  889. /// operation cannot be considered "const". In order to avoid giving a
  890. /// false sense of safety to the caller, we indicate a call to this
  891. /// method may have a surprising side effect. That said, this view may
  892. /// be too strict and it may make sense to say the internal database
  893. /// connection doesn't affect external behavior in terms of the
  894. /// interface of this method. As we gain more experiences with various
  895. /// kinds of backends we may revisit the constness.
  896. ///
  897. /// \exception DataSourceError when there is a problem reading
  898. /// the data from the dabase backend.
  899. /// This can be a connection, code, or
  900. /// data (parse) error.
  901. ///
  902. /// \param name The name to find
  903. /// \param type The RRType to find
  904. /// \param options Options about how to search.
  905. /// See ZoneFinder::FindOptions.
  906. virtual ZoneFinderContextPtr find(const isc::dns::Name& name,
  907. const isc::dns::RRType& type,
  908. const FindOptions options =
  909. FIND_DEFAULT);
  910. /// \brief Implementation of the ZoneFinder::findAll method.
  911. ///
  912. /// In short, it is mostly the same thing as find, but it returns all
  913. /// RRsets in the named node through the target parameter in successful
  914. /// case. It acts the same in the unsuccessful one.
  915. virtual ZoneFinderContextPtr findAll(
  916. const isc::dns::Name& name,
  917. std::vector<isc::dns::ConstRRsetPtr>& target,
  918. const FindOptions options = FIND_DEFAULT);
  919. /// Look for NSEC3 for proving (non)existence of given name.
  920. ///
  921. /// See documentation in \c Zone.
  922. virtual FindNSEC3Result
  923. findNSEC3(const isc::dns::Name& name, bool recursive);
  924. /// \brief The zone ID
  925. ///
  926. /// This function provides the stored zone ID as passed to the
  927. /// constructor. This is meant for testing purposes and normal
  928. /// applications shouldn't need it.
  929. int zone_id() const { return (zone_id_); }
  930. /// \brief The database accessor.
  931. ///
  932. /// This function provides the database accessor stored inside as
  933. /// passed to the constructor. This is meant for testing purposes and
  934. /// normal applications shouldn't need it.
  935. const DatabaseAccessor& getAccessor() const {
  936. return (*accessor_);
  937. }
  938. private:
  939. boost::shared_ptr<DatabaseAccessor> accessor_;
  940. const int zone_id_;
  941. const isc::dns::Name origin_;
  942. /// \brief Shortcut name for the result of getRRsets
  943. typedef std::pair<bool, std::map<dns::RRType, dns::RRsetPtr> >
  944. FoundRRsets;
  945. /// \brief Just shortcut for set of types
  946. typedef std::set<dns::RRType> WantedTypes;
  947. /// \brief Internal logit of find and findAll methods.
  948. ///
  949. /// Most of their handling is in the "error" cases and delegations
  950. /// and so on. So they share the logic here and find and findAll provide
  951. /// just an interface for it.
  952. ///
  953. /// Parameters and behaviour is like of those combined together.
  954. /// Unexpected parameters, like type != ANY and having the target, are
  955. /// just that - unexpected and not checked.
  956. ResultContext findInternal(const isc::dns::Name& name,
  957. const isc::dns::RRType& type,
  958. std::vector<isc::dns::ConstRRsetPtr>*
  959. target,
  960. const FindOptions options = FIND_DEFAULT);
  961. /// \brief Searches database for RRsets of one domain.
  962. ///
  963. /// This method scans RRs of single domain specified by name and
  964. /// extracts any RRsets found and requested by parameters.
  965. ///
  966. /// It is used internally by find(), because it is called multiple
  967. /// times (usually with different domains).
  968. ///
  969. /// \param name Which domain name should be scanned.
  970. /// \param types List of types the caller is interested in.
  971. /// \param sigs Return RRSIGs if true is passed. Otherwise, no
  972. /// associated RRSIGs are set on the returned RRsets.
  973. /// \param construct_name If this is NULL, the resulting RRsets have
  974. /// their name set to name. If it is not NULL, it overrides the
  975. /// name and uses this one (this can be used for wildcard
  976. /// synthesized records).
  977. /// \param any If this is true, it records all the types, not only the
  978. /// ones requested by types. It also puts a NULL pointer under the
  979. /// ANY type into the result, if it finds any RRs at all, to easy
  980. /// the identification of success.
  981. /// \param srcContext This can be set to non-NULL value to override the
  982. /// iterator context used for obtaining the data. This can be used,
  983. /// for example, to get data from the NSEC3 namespace.
  984. /// \return A pair, where the first element indicates if the domain
  985. /// contains any RRs at all (not only the requested, it may happen
  986. /// this is set to true, but the second part is empty). The second
  987. /// part is map from RRtypes to RRsets of the corresponding types.
  988. /// If the RRset is not present in DB, the RRtype is not there at
  989. /// all (so you'll not find NULL pointer in the result).
  990. /// \throw DataSourceError If there's a low-level error with the
  991. /// database or the database contains bad data.
  992. FoundRRsets getRRsets(const std::string& name,
  993. const WantedTypes& types,
  994. bool sigs,
  995. const std::string* construct_name = NULL,
  996. bool any = false,
  997. DatabaseAccessor::IteratorContextPtr srcContext =
  998. DatabaseAccessor::IteratorContextPtr());
  999. /// \brief DNSSEC related context for ZoneFinder::findInternal.
  1000. ///
  1001. /// This class is a helper for the ZoneFinder::findInternal method,
  1002. /// encapsulating DNSSEC related information and processing logic.
  1003. /// Specifically, it tells the finder whether the zone under search
  1004. /// is DNSSEC signed or not, and if it is, whether it's with NSEC or
  1005. /// with NSEC3. It also provides a RRset DNSSEC proof RRset for some
  1006. /// specific situations (in practice, this means an NSEC RRs for
  1007. /// negative proof when they are needed and expected).
  1008. ///
  1009. /// The purpose of this class is to keep the main finder implementation
  1010. /// unaware of DNSSEC related details. It's also intended to help
  1011. /// avoid unnecessary lookup for DNSSEC proof RRsets; this class
  1012. /// doesn't look into the DB for these RRsets unless it's known to
  1013. /// be needed. The same optimization could be implemented in the
  1014. /// main code, but it will result in duplicate similar code logic
  1015. /// and make the code more complicated. By encapsulating and unifying
  1016. /// the logic in a single separate class, we can keep the main
  1017. /// search logic readable.
  1018. class FindDNSSECContext {
  1019. public:
  1020. /// \brief Constructor for FindDNSSECContext class.
  1021. ///
  1022. /// This constructor doesn't involve any expensive operation such
  1023. /// as database lookups. It only initializes some internal
  1024. /// states (in a cheap way) and remembers if DNSSEC proof
  1025. /// is requested.
  1026. ///
  1027. /// \param finder The Finder for the findInternal that uses this
  1028. /// context.
  1029. /// \param options Find options given to the finder.
  1030. FindDNSSECContext(Finder& finder, const FindOptions options);
  1031. /// \brief Return DNSSEC related result flags for the context.
  1032. ///
  1033. /// This method returns a FindResultFlags value related to
  1034. /// DNSSEC, based on the context. If DNSSEC proof is requested
  1035. /// and the zone is signed with NSEC/NSEC3, it returns
  1036. /// RESULT_NSEC_SIGNED/RESULT_NSEC3_SIGNED, respectively;
  1037. /// otherwise it returns RESULT_DEFAULT. So the caller can simply
  1038. /// take a logical OR for the returned value of this method and
  1039. /// whatever other flags it's going to set, without knowing
  1040. /// DNSSEC specific information.
  1041. ///
  1042. /// If it's not yet identified whether and how the zone is DNSSEC
  1043. /// signed at the time of the call, it now detects that via
  1044. /// database lookups (if necessary). (And this is because why
  1045. /// this method cannot be a const member function).
  1046. ZoneFinder::FindResultFlags getResultFlags();
  1047. /// \brief Get DNSSEC negative proof for a given name.
  1048. ///
  1049. /// If the zone is considered NSEC-signed and the context
  1050. /// requested DNSSEC proofs, this method tries to find NSEC RRs
  1051. /// for the give name. If \c covering is true, it means a
  1052. /// "no name" proof is requested, so it calls findPreviousName on
  1053. /// the given name and extracts an NSEC record on the result;
  1054. /// otherwise it tries to get NSEC RRs for the given name. If
  1055. /// the NSEC is found, this method returns it; otherwise it returns
  1056. /// NULL.
  1057. ///
  1058. /// In all other cases this method simply returns NULL.
  1059. ///
  1060. /// \param name The name which the NSEC RRset belong to.
  1061. /// \param covering true if a covering NSEC is required; false if
  1062. /// a matching NSEC is required.
  1063. /// \return Any found DNSSEC proof RRset or NULL
  1064. isc::dns::ConstRRsetPtr getDNSSECRRset(
  1065. const isc::dns::Name& name, bool covering);
  1066. /// \brief Get DNSSEC negative proof for a given name.
  1067. ///
  1068. /// If the zone is considered NSEC-signed and the context
  1069. /// requested DNSSEC proofs, this method tries to find NSEC RRset
  1070. /// from the given set (\c found_set) and returns it if found;
  1071. /// in other cases this method simply returns NULL.
  1072. ///
  1073. /// \param found_set The RRset which may contain an NSEC RRset.
  1074. /// \return Any found DNSSEC proof RRset or NULL
  1075. isc::dns::ConstRRsetPtr getDNSSECRRset(const FoundRRsets&
  1076. found_set);
  1077. private:
  1078. /// \brief Returns whether the zone is signed with NSEC3.
  1079. ///
  1080. /// This method returns true if the zone for the finder that
  1081. /// uses this context is considered DNSSEC signed with NSEC3;
  1082. /// otherwise it returns false. If it's not yet detected,
  1083. /// this method now detects that via database lookups (if
  1084. /// necessary).
  1085. bool isNSEC3();
  1086. /// \brief Returns whether the zone is signed with NSEC.
  1087. ///
  1088. /// This is similar to isNSEC3(), but works for NSEC.
  1089. bool isNSEC();
  1090. /// \brief Probe into the database to see if/how the zone is
  1091. /// signed.
  1092. ///
  1093. /// This is a subroutine of isNSEC3() and isNSEC(), and performs
  1094. /// delayed database probe to detect whether the zone used by
  1095. /// the finder is DNSSEC signed, and if it is, with NSEC or NSEC3.
  1096. void probe();
  1097. DatabaseClient::Finder& finder_;
  1098. const bool need_dnssec_;
  1099. bool is_nsec3_;
  1100. bool is_nsec_;
  1101. bool probed_;
  1102. };
  1103. /// \brief A simple wrapper for identifying the previous name
  1104. /// of the given name in the underlying database.
  1105. isc::dns::Name findPreviousName(const isc::dns::Name& name) const;
  1106. /// \brief Search result of \c findDelegationPoint().
  1107. ///
  1108. /// This is a tuple combining the result of the search - a status code
  1109. /// and a pointer to the RRset found - together with additional
  1110. /// information needed for subsequent processing, an indication of
  1111. /// the first NS RRset found in the search and the number of labels
  1112. /// in the last non-empty domain encountered in the search. It is
  1113. /// used by \c findDelegationPoint().
  1114. ///
  1115. /// The last two items are located naturally in the search and although
  1116. /// not strictly part of the result, they are passed back to avoid
  1117. /// another (duplicate) search later in the processing.
  1118. ///
  1119. /// Note that the code and rrset elements are the same as that in
  1120. /// the \c ZoneFinder::FindResult struct: this structure could be
  1121. /// derived from that one, but as it is used just once in the code and
  1122. /// will never be treated as a \c FindResult, the obscurity involved in
  1123. /// deriving it from a parent class was deemed not worthwhile.
  1124. struct DelegationSearchResult {
  1125. DelegationSearchResult(const ZoneFinder::Result param_code,
  1126. const isc::dns::ConstRRsetPtr param_rrset,
  1127. const isc::dns::ConstRRsetPtr param_ns,
  1128. size_t param_last_known) :
  1129. code(param_code), rrset(param_rrset),
  1130. first_ns(param_ns),
  1131. last_known(param_last_known)
  1132. {}
  1133. const ZoneFinder::Result code; ///< Result code
  1134. const isc::dns::ConstRRsetPtr rrset; ///< RRset found
  1135. const isc::dns::ConstRRsetPtr first_ns; ///< First NS found
  1136. const size_t last_known; ///< No. labels in last non-empty domain
  1137. };
  1138. /// \brief Find delegation point
  1139. ///
  1140. /// Given a name, searches through the superdomains from the origin
  1141. /// down, searching for a point that indicates a delegation (i.e. an
  1142. /// NS record or a DNAME).
  1143. ///
  1144. /// The method operates in two modes, non-glue-ok and glue-ok modes:
  1145. ///
  1146. /// In non-glue-ok mode, the search is made purely for the NS or DNAME
  1147. /// RR. The zone is searched from the origin down looking for one
  1148. /// of these RRTypes (and ignoring the NS records at the zone origin).
  1149. /// A status is returned indicating what is found: DNAME, DELEGATION
  1150. /// of SUCCESS, the last indicating that nothing was found, together
  1151. /// with a pointer to the relevant RR.
  1152. ///
  1153. /// In glue-ok mode, the first NS encountered in the search (apart from
  1154. /// the NS at the zone apex) is remembered but otherwise NS records are
  1155. /// ignored and the search attempts to find a DNAME. The result is
  1156. /// returned in the same format, along with a pointer to the first non-
  1157. /// apex NS (if found).
  1158. ///
  1159. /// \param name The name to find
  1160. /// \param options Options about how to search. See the documentation
  1161. /// for ZoneFinder::FindOptions.
  1162. ///
  1163. /// \return Tuple holding the result of the search - the RRset of the
  1164. /// delegation point and the type of the point (DELEGATION or
  1165. /// DNAME) - and associated information. This latter item
  1166. /// comprises two pieces of data: a pointer to the highest
  1167. /// encountered NS, and the number of labels in the last known
  1168. /// non-empty domain. The associated information is found as
  1169. /// a natural part of the search for the delegation point and
  1170. /// is used later in the find() processing; it is passed back
  1171. /// to avoid the need to perform a second search to obtain it.
  1172. DelegationSearchResult
  1173. findDelegationPoint(const isc::dns::Name& name,
  1174. const FindOptions options);
  1175. /// \brief Find wildcard match
  1176. ///
  1177. /// Having found that the name is not an empty non-terminal, this
  1178. /// searches the zone for for wildcards that match the name.
  1179. ///
  1180. /// It searches superdomains of the name from the zone origin down
  1181. /// looking for a wildcard in the zone that matches the name. There
  1182. /// are several cases to consider:
  1183. ///
  1184. /// - If the previous search for a delegation point has found that
  1185. /// there is an NS at the superdomain of the point at which the
  1186. /// wildcard is found, the delegation is returned.
  1187. /// - If there is a match to the name, an appropriate status is
  1188. /// returned (match on requested type, delegation, cname, or just
  1189. /// the indication of a match but no RRs relevant to the query).
  1190. /// - If the match is to an non-empty non-terminal wildcard, a
  1191. /// wildcard NXRRSET is returned.
  1192. ///
  1193. /// Note that if DNSSEC is enabled for the search and the zone uses
  1194. /// NSEC for authenticated denial of existence, the search may
  1195. /// return NSEC records.
  1196. ///
  1197. /// \param name The name to find
  1198. /// \param type The RRType to find
  1199. /// \param options Options about how to search. See the documentation
  1200. /// for ZoneFinder::FindOptions.
  1201. /// \param dresult Result of the search through the zone for a
  1202. /// delegation.
  1203. /// \param target If the type happens to be ANY, it will insert all
  1204. /// the RRsets of the found name (if any is found) here instead
  1205. /// of being returned by the result.
  1206. /// \param dnssec_ctx The dnssec context, it is a DNSSEC wrapper for
  1207. /// find function.
  1208. /// \return Tuple holding the result of the search - the RRset of the
  1209. /// wildcard records matching the name, together with a status
  1210. /// indicating the match type (e.g. CNAME at the wildcard
  1211. /// match, no RRs of the requested type at the wildcard,
  1212. /// success due to an exact match). Also returned if there
  1213. /// is no match is an indication as to whether there was an
  1214. /// NXDOMAIN or an NXRRSET.
  1215. ResultContext findWildcardMatch(const isc::dns::Name& name,
  1216. const isc::dns::RRType& type,
  1217. const FindOptions options,
  1218. const DelegationSearchResult& dresult,
  1219. std::vector<isc::dns::ConstRRsetPtr>*
  1220. target, FindDNSSECContext& dnssec_ctx);
  1221. /// \brief Handle matching results for name
  1222. ///
  1223. /// This is called when something is found in the underlying database
  1224. /// whose domain name is an exact match of the name to be searched for.
  1225. /// It explores four possible cases to decide the final lookup result:
  1226. /// - The name is a zone cut due to an NS RR.
  1227. /// - CNAME is found (while the requested RR type is not CNAME).
  1228. /// In this case multiple CNAMEs are checked and rejected with
  1229. /// a \c DataSourceError exception.
  1230. /// - Requested type is not found at that name.
  1231. /// - A record of the requested type is found, or the query is ANY and
  1232. /// some records were found.
  1233. /// and returns a corresponding find result.
  1234. ///
  1235. /// This method is commonly used for normal (non wildcard) and wildcard
  1236. /// matches.
  1237. ///
  1238. /// \param name The name to find
  1239. /// \param type The RRType to find
  1240. /// \param options Options about how to search. See the documentation
  1241. /// for ZoneFinder::FindOptions.
  1242. /// \param is_origin If name is the zone's origin name.
  1243. /// \param found A set of found RRsets in the search for the name
  1244. /// and type. It could contain one or more of the requested
  1245. /// type, CNAME, NS, and NSEC RRsets of the name.
  1246. /// \param wildname If non NULL, the method is called on a wildcard
  1247. /// match, and points to a string object representing
  1248. /// a textual form of the matched wildcard name;
  1249. /// it's NULL in the case of non wildcard match.
  1250. /// \param target When the query is any, this must be set to a vector
  1251. /// where the result will be stored.
  1252. /// \param dnssec_ctx The dnssec context, it is a DNSSEC wrapper for
  1253. /// find function.
  1254. /// \return Tuple holding the result of the search - the RRset of the
  1255. /// wildcard records matching the name, together with a status
  1256. /// indicating the match type (corresponding to the each of
  1257. /// the above 4 cases). The return value is intended to be
  1258. /// usable as a return value of the caller of this helper
  1259. /// method.
  1260. ResultContext findOnNameResult(const isc::dns::Name& name,
  1261. const isc::dns::RRType& type,
  1262. const FindOptions options,
  1263. const bool is_origin,
  1264. const FoundRRsets& found,
  1265. const std::string* wildname,
  1266. std::vector<isc::dns::ConstRRsetPtr>*
  1267. target, FindDNSSECContext& dnssec_ctx);
  1268. /// \brief Handle no match for name
  1269. ///
  1270. /// This is called when it is known that there is no delegation and
  1271. /// there is no exact match for the name (regardless of RR types
  1272. /// requested). Before returning NXDOMAIN, we need to check two
  1273. /// cases:
  1274. /// - Empty non-terminal: if the name has subdomains in the database,
  1275. /// flag the fact. An NXRRSET will be returned (along with the
  1276. /// NSEC record covering the requested domain name if DNSSEC data
  1277. /// is being returned).
  1278. /// - Wildcard: is there a wildcard record in the zone that matches
  1279. /// requested name? If so, return it. If not, return the relevant
  1280. /// NSEC records (if requested).
  1281. ///
  1282. /// \param name The name to find
  1283. /// \param type The RRType to find
  1284. /// \param options Options about how to search. See the documentation
  1285. /// for ZoneFinder::FindOptions.
  1286. /// \param dresult Result of the search through the zone for a
  1287. /// delegation.
  1288. /// \param target If the query is for type ANY, the successfull result,
  1289. /// if there happens to be one, will be returned through the
  1290. /// parameter, as it doesn't fit into the result.
  1291. /// \param dnssec_ctx The dnssec context, it is a DNSSEC wrapper for
  1292. /// find function.
  1293. /// \return Tuple holding the result of the search - the RRset of the
  1294. /// wildcard records matching the name, together with a status
  1295. /// indicating the match type (e.g. CNAME at the wildcard
  1296. /// match, no RRs of the requested type at the wildcard,
  1297. /// success due to an exact match).
  1298. ResultContext findNoNameResult(const isc::dns::Name& name,
  1299. const isc::dns::RRType& type,
  1300. FindOptions options,
  1301. const DelegationSearchResult& dresult,
  1302. std::vector<isc::dns::ConstRRsetPtr>*
  1303. target, FindDNSSECContext& dnssec_ctx);
  1304. /// Logs condition and creates result
  1305. ///
  1306. /// A convenience function used by findOnNameResult(), it both creates
  1307. /// the FindResult object that find() will return to its caller as well
  1308. /// as logging a debug message for the information being returned.
  1309. ///
  1310. /// \param name Domain name of the RR that was being sought.
  1311. /// \param wildname Domain name string of a matched wildcard name or
  1312. /// NULL for non wildcard match.
  1313. /// \param type Type of RR being sought.
  1314. /// \param code Result of the find operation
  1315. /// \param rrset RRset found as a result of the find (which may be
  1316. /// null).
  1317. /// \param log_id ID of the message being logged. Up to five
  1318. /// parameters are available to the message: data source name,
  1319. /// requested domain name, requested class, requested type
  1320. /// and (but only if the search was successful and returned
  1321. /// an RRset) details of the RRset found.
  1322. ///
  1323. /// \return FindResult object constructed from the code and rrset
  1324. /// arguments.
  1325. ResultContext logAndCreateResult(const isc::dns::Name& name,
  1326. const std::string* wildname,
  1327. const isc::dns::RRType& type,
  1328. ZoneFinder::Result code,
  1329. isc::dns::ConstRRsetPtr rrset,
  1330. const isc::log::MessageID& log_id,
  1331. FindResultFlags flags) const;
  1332. /// \brief Checks if something lives below this domain.
  1333. ///
  1334. /// This looks if there's any subdomain of the given name. It can be
  1335. /// used to test if domain is empty non-terminal.
  1336. ///
  1337. /// \param name The domain to check.
  1338. ///
  1339. /// \return true if the name has subdomains, false if not.
  1340. bool hasSubdomains(const std::string& name);
  1341. /// \brief Convenience type shortcut.
  1342. ///
  1343. /// To find stuff in the result of getRRsets.
  1344. typedef std::map<dns::RRType, dns::RRsetPtr>::const_iterator
  1345. FoundIterator;
  1346. };
  1347. /// \brief Find a zone in the database
  1348. ///
  1349. /// This queries database's getZone to find the best matching zone.
  1350. /// It will propagate whatever exceptions are thrown from that method
  1351. /// (which is not restricted in any way).
  1352. ///
  1353. /// \param name Name of the zone or data contained there.
  1354. /// \return FindResult containing the code and an instance of Finder, if
  1355. /// anything is found. However, application should not rely on the
  1356. /// ZoneFinder being instance of Finder (possible subclass of this class
  1357. /// may return something else and it may change in future versions), it
  1358. /// should use it as a ZoneFinder only.
  1359. virtual FindResult findZone(const isc::dns::Name& name) const;
  1360. /// \brief Create a zone in the database
  1361. ///
  1362. /// This method implements \c DataSourceClient::createZone()
  1363. ///
  1364. /// It starts a transaction, checks if the zone exists, and if it
  1365. /// does not, creates it, commits, and returns true. If the zone
  1366. /// does exist already, it does nothing (except abort the transaction)
  1367. /// and returns false.
  1368. virtual bool createZone(const isc::dns::Name& zone_name);
  1369. virtual bool deleteZone(const isc::dns::Name& zone_name);
  1370. /// \brief Get the zone iterator
  1371. ///
  1372. /// The iterator allows going through the whole zone content. If the
  1373. /// underlying DatabaseConnection is implemented correctly, it should
  1374. /// be possible to have multiple ZoneIterators at once and query data
  1375. /// at the same time.
  1376. ///
  1377. /// \exception DataSourceError if the zone doesn't exist.
  1378. /// \exception isc::NotImplemented if the underlying DatabaseConnection
  1379. /// doesn't implement iteration. But in case it is not implemented
  1380. /// and the zone doesn't exist, DataSourceError is thrown.
  1381. /// \exception Anything else the underlying DatabaseConnection might
  1382. /// want to throw.
  1383. /// \param name The origin of the zone to iterate.
  1384. /// \param separate_rrs If true, the iterator will return each RR as a
  1385. /// new RRset object. If false, the iterator will
  1386. /// combine consecutive RRs with the name and type
  1387. /// into 1 RRset. The capitalization of the RRset will
  1388. /// be that of the first RR read, and TTLs will be
  1389. /// adjusted to the lowest one found.
  1390. /// \return Shared pointer to the iterator (it will never be NULL)
  1391. virtual ZoneIteratorPtr getIterator(const isc::dns::Name& name,
  1392. bool separate_rrs = false) const;
  1393. /// This implementation internally clones the accessor from the one
  1394. /// used in the client and starts a separate transaction using the cloned
  1395. /// accessor. The returned updater will be able to work separately from
  1396. /// the original client.
  1397. virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
  1398. bool replace,
  1399. bool journaling = false) const;
  1400. /// This implementation internally clones the accessor from the one
  1401. /// used in the client for retrieving diffs and iterating over them.
  1402. /// The returned reader object will be able to work separately from
  1403. /// the original client.
  1404. virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
  1405. getJournalReader(const isc::dns::Name& zone, uint32_t begin_serial,
  1406. uint32_t end_serial) const;
  1407. private:
  1408. /// \brief The RR class that this client handles.
  1409. const isc::dns::RRClass rrclass_;
  1410. /// \brief The accessor to our database.
  1411. const boost::shared_ptr<DatabaseAccessor> accessor_;
  1412. };
  1413. }
  1414. }
  1415. #endif // DATABASE_DATASRC_H
  1416. // Local Variables:
  1417. // mode: c++
  1418. // End: