database.h 75 KB

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