database.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. #include <vector>
  15. #include <datasrc/database.h>
  16. #include <exceptions/exceptions.h>
  17. #include <dns/name.h>
  18. #include <dns/rrttl.h>
  19. #include <dns/rdata.h>
  20. #include <dns/rdataclass.h>
  21. #include <datasrc/data_source.h>
  22. #include <datasrc/logger.h>
  23. #include <boost/foreach.hpp>
  24. using isc::dns::Name;
  25. namespace isc {
  26. namespace datasrc {
  27. DatabaseClient::DatabaseClient(boost::shared_ptr<DatabaseAccessor>
  28. database) :
  29. database_(database)
  30. {
  31. if (database_.get() == NULL) {
  32. isc_throw(isc::InvalidParameter,
  33. "No database provided to DatabaseClient");
  34. }
  35. }
  36. DataSourceClient::FindResult
  37. DatabaseClient::findZone(const Name& name) const {
  38. std::pair<bool, int> zone(database_->getZone(name));
  39. // Try exact first
  40. if (zone.first) {
  41. return (FindResult(result::SUCCESS,
  42. ZoneFinderPtr(new Finder(database_,
  43. zone.second))));
  44. }
  45. // Than super domains
  46. // Start from 1, as 0 is covered above
  47. for (size_t i(1); i < name.getLabelCount(); ++i) {
  48. zone = database_->getZone(name.split(i));
  49. if (zone.first) {
  50. return (FindResult(result::PARTIALMATCH,
  51. ZoneFinderPtr(new Finder(database_,
  52. zone.second))));
  53. }
  54. }
  55. // No, really nothing
  56. return (FindResult(result::NOTFOUND, ZoneFinderPtr()));
  57. }
  58. DatabaseClient::Finder::Finder(boost::shared_ptr<DatabaseAccessor>
  59. database, int zone_id) :
  60. database_(database),
  61. zone_id_(zone_id)
  62. { }
  63. namespace {
  64. // Adds the given Rdata to the given RRset
  65. // If the rrset is an empty pointer, a new one is
  66. // created with the given name, class, type and ttl
  67. // The type is checked if the rrset exists, but the
  68. // name is not.
  69. //
  70. // Then adds the given rdata to the set
  71. //
  72. // Raises a DataSourceError if the type does not
  73. // match, or if the given rdata string does not
  74. // parse correctly for the given type and class
  75. //
  76. // The DatabaseAccessor is passed to print the
  77. // database name in the log message if the TTL is
  78. // modified
  79. void addOrCreate(isc::dns::RRsetPtr& rrset,
  80. const isc::dns::Name& name,
  81. const isc::dns::RRClass& cls,
  82. const isc::dns::RRType& type,
  83. const isc::dns::RRTTL& ttl,
  84. const std::string& rdata_str,
  85. const DatabaseAccessor& db
  86. )
  87. {
  88. if (!rrset) {
  89. rrset.reset(new isc::dns::RRset(name, cls, type, ttl));
  90. } else {
  91. // This is a check to make sure find() is not messing things up
  92. assert(type == rrset->getType());
  93. if (ttl != rrset->getTTL()) {
  94. if (ttl < rrset->getTTL()) {
  95. rrset->setTTL(ttl);
  96. }
  97. logger.info(DATASRC_DATABASE_FIND_TTL_MISMATCH)
  98. .arg(db.getDBName()).arg(name).arg(cls)
  99. .arg(type).arg(rrset->getTTL());
  100. }
  101. }
  102. try {
  103. rrset->addRdata(isc::dns::rdata::createRdata(type, cls, rdata_str));
  104. } catch (const isc::dns::rdata::InvalidRdataText& ivrt) {
  105. // at this point, rrset may have been initialised for no reason,
  106. // and won't be used. But the caller would drop the shared_ptr
  107. // on such an error anyway, so we don't care.
  108. isc_throw(DataSourceError,
  109. "bad rdata in database for " << name << " "
  110. << type << ": " << ivrt.what());
  111. }
  112. }
  113. // This class keeps a short-lived store of RRSIG records encountered
  114. // during a call to find(). If the backend happens to return signatures
  115. // before the actual data, we might not know which signatures we will need
  116. // So if they may be relevant, we store the in this class.
  117. //
  118. // (If this class seems useful in other places, we might want to move
  119. // it to util. That would also provide an opportunity to add unit tests)
  120. class RRsigStore {
  121. public:
  122. // Adds the given signature Rdata to the store
  123. // The signature rdata MUST be of the RRSIG rdata type
  124. // (the caller must make sure of this).
  125. // NOTE: if we move this class to a public namespace,
  126. // we should add a type_covered argument, so as not
  127. // to have to do this cast here.
  128. void addSig(isc::dns::rdata::RdataPtr sig_rdata) {
  129. const isc::dns::RRType& type_covered =
  130. static_cast<isc::dns::rdata::generic::RRSIG*>(
  131. sig_rdata.get())->typeCovered();
  132. sigs[type_covered].push_back(sig_rdata);
  133. }
  134. // If the store contains signatures for the type of the given
  135. // rrset, they are appended to it.
  136. void appendSignatures(isc::dns::RRsetPtr& rrset) const {
  137. std::map<isc::dns::RRType,
  138. std::vector<isc::dns::rdata::RdataPtr> >::const_iterator
  139. found = sigs.find(rrset->getType());
  140. if (found != sigs.end()) {
  141. BOOST_FOREACH(isc::dns::rdata::RdataPtr sig, found->second) {
  142. rrset->addRRsig(sig);
  143. }
  144. }
  145. }
  146. private:
  147. std::map<isc::dns::RRType, std::vector<isc::dns::rdata::RdataPtr> > sigs;
  148. };
  149. }
  150. std::pair<bool, isc::dns::RRsetPtr>
  151. DatabaseClient::Finder::getRRset(const isc::dns::Name& name,
  152. const isc::dns::RRType* type,
  153. bool want_cname, bool want_dname,
  154. bool want_ns)
  155. {
  156. RRsigStore sig_store;
  157. database_->searchForRecords(zone_id_, name.toText());
  158. bool records_found = false;
  159. isc::dns::RRsetPtr result_rrset;
  160. std::string columns[DatabaseAccessor::COLUMN_COUNT];
  161. while (database_->getNextRecord(columns, DatabaseAccessor::COLUMN_COUNT)) {
  162. if (!records_found) {
  163. records_found = true;
  164. }
  165. try {
  166. const isc::dns::RRType cur_type(columns[DatabaseAccessor::
  167. TYPE_COLUMN]);
  168. const isc::dns::RRTTL cur_ttl(columns[DatabaseAccessor::
  169. TTL_COLUMN]);
  170. // Ths sigtype column was an optimization for finding the
  171. // relevant RRSIG RRs for a lookup. Currently this column is
  172. // not used in this revised datasource implementation. We
  173. // should either start using it again, or remove it from use
  174. // completely (i.e. also remove it from the schema and the
  175. // backend implementation).
  176. // Note that because we don't use it now, we also won't notice
  177. // it if the value is wrong (i.e. if the sigtype column
  178. // contains an rrtype that is different from the actual value
  179. // of the 'type covered' field in the RRSIG Rdata).
  180. //cur_sigtype(columns[SIGTYPE_COLUMN]);
  181. if (type != NULL && cur_type == *type) {
  182. if (result_rrset &&
  183. result_rrset->getType() == isc::dns::RRType::CNAME()) {
  184. isc_throw(DataSourceError, "CNAME found but it is not "
  185. "the only record for " + name.toText());
  186. } else if (result_rrset && want_ns &&
  187. result_rrset->getType() == isc::dns::RRType::NS()) {
  188. // In case there's a NS, we should find the delegation, not
  189. // the data
  190. continue;
  191. }
  192. addOrCreate(result_rrset, name, getClass(), cur_type, cur_ttl,
  193. columns[DatabaseAccessor::RDATA_COLUMN],
  194. *database_);
  195. } else if (want_cname && cur_type == isc::dns::RRType::CNAME()) {
  196. // There should be no other data, so result_rrset should
  197. // be empty.
  198. if (result_rrset) {
  199. isc_throw(DataSourceError, "CNAME found but it is not "
  200. "the only record for " + name.toText());
  201. }
  202. addOrCreate(result_rrset, name, getClass(), cur_type, cur_ttl,
  203. columns[DatabaseAccessor::RDATA_COLUMN],
  204. *database_);
  205. } else if (want_ns && cur_type == isc::dns::RRType::NS()) {
  206. if (result_rrset &&
  207. // In case we already found some data here, we should
  208. // replace it with the NS, we should delegate
  209. result_rrset->getType() != isc::dns::RRType::NS()) {
  210. result_rrset = isc::dns::RRsetPtr();
  211. }
  212. addOrCreate(result_rrset, name, getClass(), cur_type, cur_ttl,
  213. columns[DatabaseAccessor::RDATA_COLUMN],
  214. *database_);
  215. } else if (want_dname && cur_type == isc::dns::RRType::DNAME()) {
  216. // There should be max one RR of DNAME present
  217. if (result_rrset &&
  218. result_rrset->getType() == isc::dns::RRType::DNAME()) {
  219. isc_throw(DataSourceError, "DNAME with multiple RRs in " +
  220. name.toText());
  221. }
  222. addOrCreate(result_rrset, name, getClass(), cur_type, cur_ttl,
  223. columns[DatabaseAccessor::RDATA_COLUMN],
  224. *database_);
  225. } else if (cur_type == isc::dns::RRType::RRSIG()) {
  226. // If we get signatures before we get the actual data, we
  227. // can't know which ones to keep and which to drop...
  228. // So we keep a separate store of any signature that may be
  229. // relevant and add them to the final RRset when we are
  230. // done.
  231. // A possible optimization here is to not store them for
  232. // types we are certain we don't need
  233. sig_store.addSig(isc::dns::rdata::createRdata(cur_type,
  234. getClass(), columns[DatabaseAccessor::RDATA_COLUMN]));
  235. }
  236. } catch (const isc::dns::InvalidRRType& irt) {
  237. isc_throw(DataSourceError, "Invalid RRType in database for " <<
  238. name << ": " << columns[DatabaseAccessor::
  239. TYPE_COLUMN]);
  240. } catch (const isc::dns::InvalidRRTTL& irttl) {
  241. isc_throw(DataSourceError, "Invalid TTL in database for " <<
  242. name << ": " << columns[DatabaseAccessor::
  243. TTL_COLUMN]);
  244. } catch (const isc::dns::rdata::InvalidRdataText& ird) {
  245. isc_throw(DataSourceError, "Invalid rdata in database for " <<
  246. name << ": " << columns[DatabaseAccessor::
  247. RDATA_COLUMN]);
  248. }
  249. }
  250. if (result_rrset) {
  251. sig_store.appendSignatures(result_rrset);
  252. }
  253. return std::pair<bool, isc::dns::RRsetPtr>(records_found, result_rrset);
  254. }
  255. ZoneFinder::FindResult
  256. DatabaseClient::Finder::find(const isc::dns::Name& name,
  257. const isc::dns::RRType& type,
  258. isc::dns::RRsetList*,
  259. const FindOptions)
  260. {
  261. // This variable is used to determine the difference between
  262. // NXDOMAIN and NXRRSET
  263. bool records_found = false;
  264. isc::dns::RRsetPtr result_rrset;
  265. ZoneFinder::Result result_status = SUCCESS;
  266. std::pair<bool, isc::dns::RRsetPtr> found;
  267. logger.debug(DBG_TRACE_DETAILED, DATASRC_DATABASE_FIND_RECORDS)
  268. .arg(database_->getDBName()).arg(name).arg(type);
  269. try {
  270. // First, do we have any kind of delegation (NS/DNAME) here?
  271. Name origin(getOrigin());
  272. size_t originLabelCount(origin.getLabelCount());
  273. size_t currentLabelCount(name.getLabelCount());
  274. // This is how many labels we remove to get origin
  275. size_t removeLabels(currentLabelCount - originLabelCount);
  276. // Now go trough all superdomains from origin down
  277. for (int i(removeLabels); i > 0; -- i) {
  278. Name superdomain(name.split(i));
  279. // Look if there's NS or DNAME (but ignore the NS in origin)
  280. found = getRRset(superdomain, NULL, false, true,
  281. i != removeLabels);
  282. if (found.second) {
  283. // We found something redirecting somewhere else
  284. // (it can be only NS or DNAME here)
  285. result_rrset = found.second;
  286. if (result_rrset->getType() == isc::dns::RRType::NS()) {
  287. result_status = DELEGATION;
  288. } else {
  289. result_status = DNAME;
  290. }
  291. // Don't search more
  292. break;
  293. }
  294. }
  295. if (!result_rrset) { // Only if we didn't find a redirect already
  296. // Try getting the final result and extract it
  297. // It is special if there's a CNAME or NS, DNAME is ignored here
  298. // And we don't consider the NS in origin
  299. found = getRRset(name, &type, true, false, name != origin);
  300. records_found = found.first;
  301. result_rrset = found.second;
  302. if (result_rrset && type != isc::dns::RRType::NS() &&
  303. result_rrset->getType() == isc::dns::RRType::NS()) {
  304. result_status = DELEGATION;
  305. } else if (result_rrset && type != isc::dns::RRType::CNAME() &&
  306. result_rrset->getType() == isc::dns::RRType::CNAME()) {
  307. result_status = CNAME;
  308. }
  309. }
  310. } catch (const DataSourceError& dse) {
  311. logger.error(DATASRC_DATABASE_FIND_ERROR)
  312. .arg(database_->getDBName()).arg(dse.what());
  313. // call cleanup and rethrow
  314. database_->resetSearch();
  315. throw;
  316. } catch (const isc::Exception& isce) {
  317. logger.error(DATASRC_DATABASE_FIND_UNCAUGHT_ISC_ERROR)
  318. .arg(database_->getDBName()).arg(isce.what());
  319. // cleanup, change it to a DataSourceError and rethrow
  320. database_->resetSearch();
  321. isc_throw(DataSourceError, isce.what());
  322. } catch (const std::exception& ex) {
  323. logger.error(DATASRC_DATABASE_FIND_UNCAUGHT_ERROR)
  324. .arg(database_->getDBName()).arg(ex.what());
  325. database_->resetSearch();
  326. throw;
  327. }
  328. if (!result_rrset) {
  329. if (records_found) {
  330. logger.debug(DBG_TRACE_DETAILED,
  331. DATASRC_DATABASE_FOUND_NXRRSET)
  332. .arg(database_->getDBName()).arg(name)
  333. .arg(getClass()).arg(type);
  334. result_status = NXRRSET;
  335. } else {
  336. logger.debug(DBG_TRACE_DETAILED,
  337. DATASRC_DATABASE_FOUND_NXDOMAIN)
  338. .arg(database_->getDBName()).arg(name)
  339. .arg(getClass()).arg(type);
  340. result_status = NXDOMAIN;
  341. }
  342. } else {
  343. logger.debug(DBG_TRACE_DETAILED,
  344. DATASRC_DATABASE_FOUND_RRSET)
  345. .arg(database_->getDBName()).arg(*result_rrset);
  346. }
  347. return (FindResult(result_status, result_rrset));
  348. }
  349. Name
  350. DatabaseClient::Finder::getOrigin() const {
  351. // TODO Implement
  352. return (Name("."));
  353. }
  354. isc::dns::RRClass
  355. DatabaseClient::Finder::getClass() const {
  356. // TODO Implement
  357. return isc::dns::RRClass::IN();
  358. }
  359. }
  360. }