auth_srv.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <sys/select.h>
  18. #include <netinet/in.h>
  19. #include <netdb.h>
  20. #include <stdlib.h>
  21. #include <cassert>
  22. #include <iostream>
  23. #include <exceptions/exceptions.h>
  24. #include <dns/buffer.h>
  25. #include <dns/exceptions.h>
  26. #include <dns/messagerenderer.h>
  27. #include <dns/name.h>
  28. #include <dns/question.h>
  29. #include <dns/rrset.h>
  30. #include <dns/rrttl.h>
  31. #include <dns/message.h>
  32. #include <config/ccsession.h>
  33. #include <cc/data.h>
  34. #include <exceptions/exceptions.h>
  35. #include <auth/query.h>
  36. #include <auth/data_source.h>
  37. #include <auth/static_datasrc.h>
  38. #include <auth/sqlite3_datasrc.h>
  39. #include <cc/data.h>
  40. #include "common.h"
  41. #include "auth_srv.h"
  42. #include <boost/lexical_cast.hpp>
  43. using namespace std;
  44. using namespace isc;
  45. using namespace isc::auth;
  46. using namespace isc::dns;
  47. using namespace isc::dns::rdata;
  48. using namespace isc::data;
  49. using namespace isc::config;
  50. class AuthSrvImpl {
  51. private:
  52. // prohibit copy
  53. AuthSrvImpl(const AuthSrvImpl& source);
  54. AuthSrvImpl& operator=(const AuthSrvImpl& source);
  55. public:
  56. AuthSrvImpl();
  57. isc::data::ElementPtr setDbFile(const isc::data::ElementPtr config);
  58. std::string db_file_;
  59. isc::auth::MetaDataSrc data_sources_;
  60. /// We keep a pointer to the currently running sqlite datasource
  61. /// so that we can specifically remove that one should the database
  62. /// file change
  63. isc::auth::ConstDataSrcPtr cur_datasrc_;
  64. };
  65. AuthSrvImpl::AuthSrvImpl() {
  66. // cur_datasrc_ is automatically initialized by the default constructor,
  67. // effectively being an empty (sqlite) data source. once ccsession is up
  68. // the datasource will be set by the configuration setting
  69. // (or the default one if none is set)
  70. // add static data source
  71. data_sources_.addDataSrc(ConstDataSrcPtr(new StaticDataSrc));
  72. }
  73. AuthSrv::AuthSrv() : impl_(new AuthSrvImpl) {}
  74. AuthSrv::~AuthSrv() {
  75. delete impl_;
  76. }
  77. static void
  78. makeErrorMessage(Message& message, MessageRenderer& renderer,
  79. const Rcode& rcode)
  80. {
  81. message.makeResponse();
  82. message.setRcode(rcode);
  83. message.setUDPSize(4096); // XXX: hardcoding
  84. message.toWire(renderer);
  85. }
  86. int
  87. AuthSrv::processMessage(InputBuffer& request_buffer,
  88. Message& message,
  89. MessageRenderer& response_renderer,
  90. const bool udp_buffer, const bool verbose_mode)
  91. {
  92. try {
  93. message.fromWire(request_buffer);
  94. } catch (const DNSProtocolError& error) {
  95. cerr << "returning protocol error" << endl;
  96. makeErrorMessage(message, response_renderer, error.getRcode());
  97. return (0);
  98. } catch (const Exception& ex) {
  99. cerr << "returning servfail" << endl;
  100. makeErrorMessage(message, response_renderer, Rcode::SERVFAIL());
  101. return (0);
  102. } // other exceptions will be handled at a higher layer.
  103. if (verbose_mode) {
  104. cerr << "[AuthSrv] received a message:\n" << message.toText() << endl;
  105. }
  106. //
  107. // Incoming Message Validation
  108. //
  109. // Ignore all requests.
  110. if (message.getHeaderFlag(MessageFlag::QR())) {
  111. if (verbose_mode) {
  112. cerr << "received unexpected response, ignoring" << endl;
  113. }
  114. return (-1);
  115. }
  116. // In this implementation, we only support normal queries
  117. if (message.getOpcode() != Opcode::QUERY()) {
  118. if (verbose_mode) {
  119. cerr << "unsupported opcode" << endl;
  120. }
  121. makeErrorMessage(message, response_renderer, Rcode::NOTIMP());
  122. return (0);
  123. }
  124. if (message.getRRCount(Section::QUESTION()) != 1) {
  125. makeErrorMessage(message, response_renderer, Rcode::FORMERR());
  126. return (0);
  127. }
  128. const bool dnssec_ok = message.isDNSSECSupported();
  129. const uint16_t remote_bufsize = message.getUDPSize();
  130. message.makeResponse();
  131. message.setHeaderFlag(MessageFlag::AA());
  132. message.setRcode(Rcode::NOERROR());
  133. message.setDNSSECSupported(dnssec_ok);
  134. message.setUDPSize(4096); // XXX: hardcoding
  135. try {
  136. Query query(message, dnssec_ok);
  137. impl_->data_sources_.doQuery(query);
  138. } catch(...) {
  139. message.setRcode(Rcode::SERVFAIL());
  140. }
  141. response_renderer.setLengthLimit(udp_buffer ? remote_bufsize : 65535);
  142. message.toWire(response_renderer);
  143. if (verbose_mode) {
  144. cerr << "sending a response (" <<
  145. boost::lexical_cast<string>(response_renderer.getLength())
  146. << " bytes):\n" << message.toText() << endl;
  147. }
  148. return (0);
  149. }
  150. ElementPtr
  151. AuthSrvImpl::setDbFile(const isc::data::ElementPtr config) {
  152. if (config) {
  153. db_file_ = config->get("database_file")->stringValue();
  154. cout << "[AuthSrv] Data source database file: " << db_file_ << endl;
  155. }
  156. // create SQL data source
  157. // config may be empty here; in that case it will load the default
  158. // database file
  159. // Note: the following step is tricky to be exception-safe and to ensure
  160. // exception guarantee: We first need to perform all operations that can
  161. // fail, while acquiring resources in the RAII manner. We then perform
  162. // delete and swap operations which should not fail.
  163. DataSrcPtr datasrc_ptr(DataSrcPtr(new Sqlite3DataSrc));
  164. datasrc_ptr->init(config);
  165. ElementPtr answer = isc::config::createAnswer(0);
  166. data_sources_.addDataSrc(datasrc_ptr);
  167. // The following code should be exception free.
  168. if (cur_datasrc_ != NULL) {
  169. data_sources_.removeDataSrc(cur_datasrc_);
  170. }
  171. cur_datasrc_ = datasrc_ptr;
  172. return answer;
  173. }
  174. ElementPtr
  175. AuthSrv::updateConfig(isc::data::ElementPtr new_config) {
  176. try {
  177. ElementPtr answer = isc::config::createAnswer(0);
  178. if (new_config != NULL) {
  179. // the ModuleCCSession has already checked if we have
  180. // the correct ElementPtr type as specified in our .spec file
  181. if (new_config->contains("database_file")) {
  182. answer = impl_->setDbFile(new_config);
  183. }
  184. }
  185. // if we have no sqlite3 data source, use the default
  186. if (impl_->cur_datasrc_ == NULL) {
  187. impl_->setDbFile(ElementPtr());
  188. }
  189. return answer;
  190. } catch (const isc::Exception& error) {
  191. cout << "[AuthSrv] error: " << error.what() << endl;
  192. return isc::config::createAnswer(1, error.what());
  193. }
  194. }