auth_srv.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Copyright (C) 2009 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. // $Id$
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <iostream>
  18. #include <vector>
  19. #include <exceptions/exceptions.h>
  20. #include <dns/buffer.h>
  21. #include <dns/exceptions.h>
  22. #include <dns/messagerenderer.h>
  23. #include <dns/name.h>
  24. #include <dns/question.h>
  25. #include <dns/rrset.h>
  26. #include <dns/rrttl.h>
  27. #include <dns/message.h>
  28. #include <config/ccsession.h>
  29. #include <cc/data.h>
  30. #include <exceptions/exceptions.h>
  31. #include <datasrc/query.h>
  32. #include <datasrc/data_source.h>
  33. #include <datasrc/static_datasrc.h>
  34. #include <datasrc/sqlite3_datasrc.h>
  35. #include <cc/data.h>
  36. #include <auth/common.h>
  37. #include <auth/auth_srv.h>
  38. #include <boost/lexical_cast.hpp>
  39. using namespace std;
  40. using namespace isc;
  41. using namespace isc::datasrc;
  42. using namespace isc::dns;
  43. using namespace isc::dns::rdata;
  44. using namespace isc::data;
  45. using namespace isc::config;
  46. class AuthSrvImpl {
  47. private:
  48. // prohibit copy
  49. AuthSrvImpl(const AuthSrvImpl& source);
  50. AuthSrvImpl& operator=(const AuthSrvImpl& source);
  51. public:
  52. AuthSrvImpl(const bool use_cache);
  53. isc::data::ElementPtr setDbFile(const isc::data::ElementPtr config);
  54. std::string db_file_;
  55. ModuleCCSession* cs_;
  56. MetaDataSrc data_sources_;
  57. /// We keep a pointer to the currently running sqlite datasource
  58. /// so that we can specifically remove that one should the database
  59. /// file change
  60. ConstDataSrcPtr cur_datasrc_;
  61. bool verbose_mode_;
  62. /// Currently non-configurable, but will be.
  63. static const uint16_t DEFAULT_LOCAL_UDPSIZE = 4096;
  64. /// Hot spot cache
  65. isc::datasrc::HotCache cache_;
  66. };
  67. AuthSrvImpl::AuthSrvImpl(const bool use_cache) :
  68. cs_(NULL), verbose_mode_(false)
  69. {
  70. // cur_datasrc_ is automatically initialized by the default constructor,
  71. // effectively being an empty (sqlite) data source. once ccsession is up
  72. // the datasource will be set by the configuration setting
  73. // add static data source
  74. data_sources_.addDataSrc(ConstDataSrcPtr(new StaticDataSrc));
  75. // enable or disable the cache
  76. cache_.setEnabled(use_cache);
  77. }
  78. AuthSrv::AuthSrv(const bool use_cache) : impl_(new AuthSrvImpl(use_cache)) {
  79. }
  80. AuthSrv::~AuthSrv() {
  81. delete impl_;
  82. }
  83. namespace {
  84. class QuestionInserter {
  85. public:
  86. QuestionInserter(Message* message) : message_(message) {}
  87. void operator()(const QuestionPtr question) {
  88. message_->addQuestion(question);
  89. }
  90. Message* message_;
  91. };
  92. void
  93. makeErrorMessage(Message& message, MessageRenderer& renderer,
  94. const Rcode& rcode, const bool verbose_mode)
  95. {
  96. // extract the parameters that should be kept.
  97. // XXX: with the current implementation, it's not easy to set EDNS0
  98. // depending on whether the query had it. So we'll simply omit it.
  99. const qid_t qid = message.getQid();
  100. const bool rd = message.getHeaderFlag(MessageFlag::RD());
  101. const bool cd = message.getHeaderFlag(MessageFlag::CD());
  102. const Opcode& opcode = message.getOpcode();
  103. vector<QuestionPtr> questions;
  104. // If this is an error to a query, we should also copy the question section.
  105. if (opcode == Opcode::QUERY()) {
  106. questions.assign(message.beginQuestion(), message.endQuestion());
  107. }
  108. message.clear(Message::RENDER);
  109. message.setQid(qid);
  110. message.setOpcode(opcode);
  111. message.setHeaderFlag(MessageFlag::QR());
  112. message.setUDPSize(AuthSrvImpl::DEFAULT_LOCAL_UDPSIZE);
  113. if (rd) {
  114. message.setHeaderFlag(MessageFlag::RD());
  115. }
  116. if (cd) {
  117. message.setHeaderFlag(MessageFlag::CD());
  118. }
  119. for_each(questions.begin(), questions.end(), QuestionInserter(&message));
  120. message.setRcode(rcode);
  121. message.toWire(renderer);
  122. if (verbose_mode) {
  123. cerr << "[b10-auth] sending an error response (" <<
  124. boost::lexical_cast<string>(renderer.getLength())
  125. << " bytes):\n" << message.toText() << endl;
  126. }
  127. }
  128. }
  129. void
  130. AuthSrv::setVerbose(const bool on) {
  131. impl_->verbose_mode_ = on;
  132. }
  133. bool
  134. AuthSrv::getVerbose() const {
  135. return (impl_->verbose_mode_);
  136. }
  137. void
  138. AuthSrv::setConfigSession(ModuleCCSession* cs) {
  139. impl_->cs_ = cs;
  140. }
  141. ModuleCCSession*
  142. AuthSrv::configSession() const {
  143. return (impl_->cs_);
  144. }
  145. bool
  146. AuthSrv::processMessage(InputBuffer& request_buffer, Message& message,
  147. MessageRenderer& response_renderer,
  148. const bool udp_buffer)
  149. {
  150. // First, check the header part. If we fail even for the base header,
  151. // just drop the message.
  152. try {
  153. message.parseHeader(request_buffer);
  154. // Ignore all responses.
  155. if (message.getHeaderFlag(MessageFlag::QR())) {
  156. if (impl_->verbose_mode_) {
  157. cerr << "[b10-auth] received unexpected response, ignoring" << endl;
  158. }
  159. return (false);
  160. }
  161. } catch (const Exception& ex) {
  162. return (false);
  163. }
  164. // Parse the message. On failure, return an appropriate error.
  165. try {
  166. message.fromWire(request_buffer);
  167. } catch (const DNSProtocolError& error) {
  168. if (impl_->verbose_mode_) {
  169. cerr << "[b10-auth] returning " << error.getRcode().toText() << ": "
  170. << error.what() << endl;
  171. }
  172. makeErrorMessage(message, response_renderer, error.getRcode(),
  173. impl_->verbose_mode_);
  174. return (true);
  175. } catch (const Exception& ex) {
  176. if (impl_->verbose_mode_) {
  177. cerr << "[b10-auth] returning SERVFAIL: " << ex.what() << endl;
  178. }
  179. makeErrorMessage(message, response_renderer, Rcode::SERVFAIL(),
  180. impl_->verbose_mode_);
  181. return (true);
  182. } // other exceptions will be handled at a higher layer.
  183. if (impl_->verbose_mode_) {
  184. cerr << "[b10-auth] received a message:\n" << message.toText() << endl;
  185. }
  186. // Perform further protocol-level validation.
  187. // In this implementation, we only support normal queries
  188. if (message.getOpcode() != Opcode::QUERY()) {
  189. if (impl_->verbose_mode_) {
  190. cerr << "[b10-auth] unsupported opcode" << endl;
  191. }
  192. makeErrorMessage(message, response_renderer, Rcode::NOTIMP(),
  193. impl_->verbose_mode_);
  194. return (true);
  195. }
  196. if (message.getRRCount(Section::QUESTION()) != 1) {
  197. makeErrorMessage(message, response_renderer, Rcode::FORMERR(),
  198. impl_->verbose_mode_);
  199. return (true);
  200. }
  201. const bool dnssec_ok = message.isDNSSECSupported();
  202. const uint16_t remote_bufsize = message.getUDPSize();
  203. message.makeResponse();
  204. message.setHeaderFlag(MessageFlag::AA());
  205. message.setRcode(Rcode::NOERROR());
  206. message.setDNSSECSupported(dnssec_ok);
  207. message.setUDPSize(AuthSrvImpl::DEFAULT_LOCAL_UDPSIZE);
  208. try {
  209. Query query(message, impl_->cache_, dnssec_ok);
  210. impl_->data_sources_.doQuery(query);
  211. } catch (const Exception& ex) {
  212. if (impl_->verbose_mode_) {
  213. cerr << "[b10-auth] Internal error, returning SERVFAIL: " <<
  214. ex.what() << endl;
  215. }
  216. makeErrorMessage(message, response_renderer, Rcode::SERVFAIL(),
  217. impl_->verbose_mode_);
  218. return (true);
  219. }
  220. response_renderer.setLengthLimit(udp_buffer ? remote_bufsize : 65535);
  221. message.toWire(response_renderer);
  222. if (impl_->verbose_mode_) {
  223. cerr << "[b10-auth] sending a response (" <<
  224. boost::lexical_cast<string>(response_renderer.getLength())
  225. << " bytes):\n" << message.toText() << endl;
  226. }
  227. return (true);
  228. }
  229. ElementPtr
  230. AuthSrvImpl::setDbFile(const isc::data::ElementPtr config) {
  231. ElementPtr answer = isc::config::createAnswer();
  232. ElementPtr final;
  233. if (config && config->contains("database_file")) {
  234. db_file_ = config->get("database_file")->stringValue();
  235. final = config;
  236. } else if (cs_ != NULL) {
  237. bool is_default;
  238. string item("database_file");
  239. ElementPtr value = cs_->getValue(is_default, item);
  240. final = Element::createMap();
  241. // If the value is the default, and we are running from
  242. // a specific directory ('from build'), we need to use
  243. // a different value than the default (which may not exist)
  244. // (btw, this should not be done here in the end, i think
  245. // the from-source script should have a check for this,
  246. // but for that we need offline access to config, so for
  247. // now this is a decent solution)
  248. if (is_default && getenv("B10_FROM_BUILD")) {
  249. value = Element::create(string(getenv("B10_FROM_BUILD")) +
  250. "/bind10_zones.sqlite3");
  251. }
  252. final->set(item, value);
  253. db_file_ = value->stringValue();
  254. } else {
  255. return (answer);
  256. }
  257. if (verbose_mode_) {
  258. cerr << "[b10-auth] Data source database file: " << db_file_ << endl;
  259. }
  260. // create SQL data source
  261. // Note: the following step is tricky to be exception-safe and to ensure
  262. // exception guarantee: We first need to perform all operations that can
  263. // fail, while acquiring resources in the RAII manner. We then perform
  264. // delete and swap operations which should not fail.
  265. DataSrcPtr datasrc_ptr(DataSrcPtr(new Sqlite3DataSrc));
  266. datasrc_ptr->init(final);
  267. data_sources_.addDataSrc(datasrc_ptr);
  268. // The following code should be exception free.
  269. if (cur_datasrc_ != NULL) {
  270. data_sources_.removeDataSrc(cur_datasrc_);
  271. }
  272. cur_datasrc_ = datasrc_ptr;
  273. return (answer);
  274. }
  275. ElementPtr
  276. AuthSrv::updateConfig(isc::data::ElementPtr new_config) {
  277. try {
  278. // the ModuleCCSession has already checked if we have
  279. // the correct ElementPtr type as specified in our .spec file
  280. ElementPtr answer = isc::config::createAnswer();
  281. answer = impl_->setDbFile(new_config);
  282. return answer;
  283. } catch (const isc::Exception& error) {
  284. if (impl_->verbose_mode_) {
  285. cerr << "[b10-auth] error: " << error.what() << endl;
  286. }
  287. return isc::config::createAnswer(1, error.what());
  288. }
  289. }