auth_srv.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <dns/buffer.h>
  24. #include <dns/messagerenderer.h>
  25. #include <dns/name.h>
  26. #include <dns/question.h>
  27. #include <dns/rrset.h>
  28. #include <dns/rrttl.h>
  29. #include <dns/message.h>
  30. #include <config/ccsession.h>
  31. #include <cc/data.h>
  32. #include <exceptions/exceptions.h>
  33. #include <auth/query.h>
  34. #include <auth/data_source.h>
  35. #include <auth/static_datasrc.h>
  36. #include <auth/sqlite3_datasrc.h>
  37. #include <cc/data.h>
  38. #include "common.h"
  39. #include "auth_srv.h"
  40. #include <boost/lexical_cast.hpp>
  41. using namespace std;
  42. using namespace isc::auth;
  43. using namespace isc::dns;
  44. using namespace isc::dns::rdata;
  45. using namespace isc::data;
  46. using namespace isc::config;
  47. class AuthSrvImpl {
  48. private:
  49. // prohibit copy
  50. AuthSrvImpl(const AuthSrvImpl& source);
  51. AuthSrvImpl& operator=(const AuthSrvImpl& source);
  52. public:
  53. AuthSrvImpl();
  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. int
  74. AuthSrv::processMessage(InputBuffer& request_buffer,
  75. Message& message,
  76. MessageRenderer& response_renderer,
  77. const bool udp_buffer, const bool verbose_mode)
  78. {
  79. try {
  80. message.fromWire(request_buffer);
  81. } catch (...) {
  82. cerr << "[AuthSrv] parse failed" << endl;
  83. return (-1);
  84. }
  85. if (verbose_mode) {
  86. cerr << "[AuthSrv] received a message:\n" << message.toText() << endl;
  87. }
  88. if (message.getRRCount(Section::QUESTION()) != 1) {
  89. return (-1);
  90. }
  91. const bool dnssec_ok = message.isDNSSECSupported();
  92. const uint16_t remote_bufsize = message.getUDPSize();
  93. message.makeResponse();
  94. message.setHeaderFlag(MessageFlag::AA());
  95. message.setRcode(Rcode::NOERROR());
  96. message.setDNSSECSupported(dnssec_ok);
  97. message.setUDPSize(4096); // XXX: hardcoding
  98. Query query(message, dnssec_ok);
  99. impl_->data_sources_.doQuery(query);
  100. response_renderer.setLengthLimit(udp_buffer ? remote_bufsize : 65535);
  101. message.toWire(response_renderer);
  102. if (verbose_mode) {
  103. cerr << "sending a response (" <<
  104. boost::lexical_cast<string>(response_renderer.getLength())
  105. << " bytes):\n" << message.toText() << endl;
  106. }
  107. return (0);
  108. }
  109. ElementPtr
  110. AuthSrv::setDbFile(const isc::data::ElementPtr config) {
  111. if (config) {
  112. impl_->db_file_ = config->get("database_file")->stringValue();
  113. cout << "[AuthSrv] Data source database file: " << impl_->db_file_
  114. << endl;
  115. }
  116. try {
  117. // create SQL data source
  118. // config may be empty here; in that case it will load the default
  119. // database file
  120. Sqlite3DataSrc* sd = new Sqlite3DataSrc;
  121. sd->init(config);
  122. if (impl_->cur_datasrc_) {
  123. impl_->data_sources_.removeDataSrc(impl_->cur_datasrc_);
  124. }
  125. ConstDataSrcPtr csd = ConstDataSrcPtr(sd);
  126. impl_->data_sources_.addDataSrc(csd);
  127. impl_->cur_datasrc_ = csd;
  128. return isc::config::createAnswer(0);
  129. } catch (isc::Exception error) {
  130. cout << "[AuthSrv] error: " << error.what() << endl;
  131. return isc::config::createAnswer(1, error.what());
  132. }
  133. }
  134. ElementPtr
  135. AuthSrv::updateConfig(isc::data::ElementPtr new_config) {
  136. ElementPtr answer = isc::config::createAnswer(0);
  137. if (new_config) {
  138. // the ModuleCCSession has already checked if we have
  139. // the correct ElementPtr type as specified in our .spec file
  140. if (new_config->contains("database_file")) {
  141. answer = setDbFile(new_config);
  142. }
  143. }
  144. // if we have no sqlite3 data source, use the default
  145. if (impl_->cur_datasrc_ == NULL) {
  146. setDbFile(ElementPtr());
  147. }
  148. return answer;
  149. }