auth_srv.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <auth/query.h>
  32. #include <auth/data_source.h>
  33. #include <auth/static_datasrc.h>
  34. #include <auth/sqlite3_datasrc.h>
  35. #include <cc/data.h>
  36. #include "common.h"
  37. #include "auth_srv.h"
  38. #include <boost/lexical_cast.hpp>
  39. using namespace std;
  40. using namespace isc;
  41. using namespace isc::auth;
  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();
  53. isc::data::ElementPtr setDbFile(const isc::data::ElementPtr config);
  54. std::string db_file_;
  55. isc::auth::MetaDataSrc data_sources_;
  56. /// We keep a pointer to the currently running sqlite datasource
  57. /// so that we can specifically remove that one should the database
  58. /// file change
  59. isc::auth::ConstDataSrcPtr cur_datasrc_;
  60. };
  61. AuthSrvImpl::AuthSrvImpl() {
  62. // cur_datasrc_ is automatically initialized by the default constructor,
  63. // effectively being an empty (sqlite) data source. once ccsession is up
  64. // the datasource will be set by the configuration setting
  65. // (or the default one if none is set)
  66. // add static data source
  67. data_sources_.addDataSrc(ConstDataSrcPtr(new StaticDataSrc));
  68. }
  69. AuthSrv::AuthSrv() : impl_(new AuthSrvImpl) {}
  70. AuthSrv::~AuthSrv() {
  71. delete impl_;
  72. }
  73. namespace {
  74. class QuestionInserter {
  75. public:
  76. QuestionInserter(Message* message) : message_(message) {}
  77. void operator()(const QuestionPtr question) {
  78. message_->addQuestion(question);
  79. }
  80. Message* message_;
  81. };
  82. void
  83. makeErrorMessage(Message& message, MessageRenderer& renderer,
  84. const Rcode& rcode)
  85. {
  86. // extract the parameters that should be kept.
  87. // XXX: with the current implementation, it's not easy to set EDNS0
  88. // depending on whether the query had it. So we'll simply omit it.
  89. const qid_t qid = message.getQid();
  90. const bool rd = message.getHeaderFlag(MessageFlag::RD());
  91. const bool cd = message.getHeaderFlag(MessageFlag::CD());
  92. const Opcode& opcode = message.getOpcode();
  93. vector<QuestionPtr> questions;
  94. // If this is an error to a query, we should also copy the question section.
  95. if (opcode == Opcode::QUERY()) {
  96. questions.assign(message.beginQuestion(), message.endQuestion());
  97. }
  98. message.clear(Message::RENDER);
  99. message.setQid(qid);
  100. message.setOpcode(opcode);
  101. message.setHeaderFlag(MessageFlag::QR());
  102. if (rd) {
  103. message.setHeaderFlag(MessageFlag::RD());
  104. }
  105. if (cd) {
  106. message.setHeaderFlag(MessageFlag::CD());
  107. }
  108. for_each(questions.begin(), questions.end(), QuestionInserter(&message));
  109. message.setRcode(rcode);
  110. message.toWire(renderer);
  111. }
  112. }
  113. bool
  114. AuthSrv::processMessage(InputBuffer& request_buffer, Message& message,
  115. MessageRenderer& response_renderer,
  116. const bool udp_buffer, const bool verbose_mode)
  117. {
  118. // First, check the header part. If we fail even for the base header,
  119. // just drop the message.
  120. try {
  121. message.parseHeader(request_buffer);
  122. // Ignore all responses.
  123. if (message.getHeaderFlag(MessageFlag::QR())) {
  124. if (verbose_mode) {
  125. cerr << "received unexpected response, ignoring" << endl;
  126. }
  127. return (false);
  128. }
  129. } catch (const Exception& ex) {
  130. return (false);
  131. }
  132. // Parse the message. On failure, return an appropriate error.
  133. try {
  134. message.fromWire(request_buffer);
  135. } catch (const DNSProtocolError& error) {
  136. cerr << "returning protocol error" << endl;
  137. makeErrorMessage(message, response_renderer, error.getRcode());
  138. return (true);
  139. } catch (const Exception& ex) {
  140. cerr << "returning servfail" << endl;
  141. makeErrorMessage(message, response_renderer, Rcode::SERVFAIL());
  142. return (true);
  143. } // other exceptions will be handled at a higher layer.
  144. if (verbose_mode) {
  145. cerr << "[AuthSrv] received a message:\n" << message.toText() << endl;
  146. }
  147. // Perform further protocol-level validation.
  148. // In this implementation, we only support normal queries
  149. if (message.getOpcode() != Opcode::QUERY()) {
  150. if (verbose_mode) {
  151. cerr << "unsupported opcode" << endl;
  152. }
  153. makeErrorMessage(message, response_renderer, Rcode::NOTIMP());
  154. return (true);
  155. }
  156. if (message.getRRCount(Section::QUESTION()) != 1) {
  157. makeErrorMessage(message, response_renderer, Rcode::FORMERR());
  158. return (true);
  159. }
  160. const bool dnssec_ok = message.isDNSSECSupported();
  161. const uint16_t remote_bufsize = message.getUDPSize();
  162. message.makeResponse();
  163. message.setHeaderFlag(MessageFlag::AA());
  164. message.setRcode(Rcode::NOERROR());
  165. message.setDNSSECSupported(dnssec_ok);
  166. message.setUDPSize(4096); // XXX: hardcoding
  167. try {
  168. Query query(message, dnssec_ok);
  169. impl_->data_sources_.doQuery(query);
  170. } catch(...) {
  171. message.setRcode(Rcode::SERVFAIL());
  172. }
  173. response_renderer.setLengthLimit(udp_buffer ? remote_bufsize : 65535);
  174. message.toWire(response_renderer);
  175. if (verbose_mode) {
  176. cerr << "sending a response (" <<
  177. boost::lexical_cast<string>(response_renderer.getLength())
  178. << " bytes):\n" << message.toText() << endl;
  179. }
  180. return (true);
  181. }
  182. ElementPtr
  183. AuthSrvImpl::setDbFile(const isc::data::ElementPtr config) {
  184. if (config) {
  185. db_file_ = config->get("database_file")->stringValue();
  186. cout << "[AuthSrv] Data source database file: " << db_file_ << endl;
  187. }
  188. // create SQL data source
  189. // config may be empty here; in that case it will load the default
  190. // database file
  191. // Note: the following step is tricky to be exception-safe and to ensure
  192. // exception guarantee: We first need to perform all operations that can
  193. // fail, while acquiring resources in the RAII manner. We then perform
  194. // delete and swap operations which should not fail.
  195. DataSrcPtr datasrc_ptr(DataSrcPtr(new Sqlite3DataSrc));
  196. datasrc_ptr->init(config);
  197. ElementPtr answer = isc::config::createAnswer(0);
  198. data_sources_.addDataSrc(datasrc_ptr);
  199. // The following code should be exception free.
  200. if (cur_datasrc_ != NULL) {
  201. data_sources_.removeDataSrc(cur_datasrc_);
  202. }
  203. cur_datasrc_ = datasrc_ptr;
  204. return answer;
  205. }
  206. ElementPtr
  207. AuthSrv::updateConfig(isc::data::ElementPtr new_config) {
  208. try {
  209. ElementPtr answer = isc::config::createAnswer(0);
  210. if (new_config != NULL) {
  211. // the ModuleCCSession has already checked if we have
  212. // the correct ElementPtr type as specified in our .spec file
  213. if (new_config->contains("database_file")) {
  214. answer = impl_->setDbFile(new_config);
  215. }
  216. }
  217. // if we have no sqlite3 data source, use the default
  218. if (impl_->cur_datasrc_ == NULL) {
  219. impl_->setDbFile(ElementPtr());
  220. }
  221. return answer;
  222. } catch (const isc::Exception& error) {
  223. cout << "[AuthSrv] error: " << error.what() << endl;
  224. return isc::config::createAnswer(1, error.what());
  225. }
  226. }