database.h 76 KB

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