database.h 75 KB

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