auth_config.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (C) 2010 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 <dns/name.h>
  15. #include <dns/rrclass.h>
  16. #include <cc/data.h>
  17. #include <datasrc/memory_datasrc.h>
  18. #include <datasrc/zonetable.h>
  19. #include <datasrc/factory.h>
  20. #include <auth/auth_srv.h>
  21. #include <auth/auth_config.h>
  22. #include <auth/common.h>
  23. #include <server_common/portconfig.h>
  24. #include <boost/foreach.hpp>
  25. #include <boost/shared_ptr.hpp>
  26. #include <boost/scoped_ptr.hpp>
  27. #include <set>
  28. #include <string>
  29. #include <utility>
  30. #include <vector>
  31. using namespace std;
  32. using namespace isc::dns;
  33. using namespace isc::data;
  34. using namespace isc::datasrc;
  35. using namespace isc::server_common::portconfig;
  36. namespace {
  37. /// A derived \c AuthConfigParser for the version value
  38. /// (which is not used at this moment)
  39. class VersionConfig : public AuthConfigParser {
  40. public:
  41. VersionConfig() {}
  42. virtual void build(ConstElementPtr) {};
  43. virtual void commit() {};
  44. };
  45. /// A special parser for testing: it throws from commit() despite the
  46. /// suggested convention of the class interface.
  47. class ThrowerCommitConfig : public AuthConfigParser {
  48. public:
  49. virtual void build(ConstElementPtr) {} // ignore param, do nothing
  50. virtual void commit() {
  51. throw 10;
  52. }
  53. };
  54. /**
  55. * \brief Configuration parser for listen_on.
  56. *
  57. * It parses and sets the listening addresses of the server.
  58. *
  59. * It acts in unusual way. Since actually binding (changing) the sockets
  60. * is an operation that is expected to throw often, it shouldn't happen
  61. * in commit. Thefere we do it in build. But if the config is not committed
  62. * then, we would have it wrong. So we store the old addresses and if
  63. * commit is not called before destruction of the object, we return the
  64. * old addresses (which is the same kind of dangerous operation, but it is
  65. * expected that if we just managed to bind some and had the old ones binded
  66. * before, it should work).
  67. *
  68. * We might do something better in future (like open only the ports that are
  69. * extra, put them in in commit and close the old ones), but that's left out
  70. * for now.
  71. */
  72. class ListenAddressConfig : public AuthConfigParser {
  73. public:
  74. ListenAddressConfig(AuthSrv& server) :
  75. server_(server)
  76. { }
  77. ~ ListenAddressConfig() {
  78. if (rollbackAddresses_.get() != NULL) {
  79. server_.setListenAddresses(*rollbackAddresses_);
  80. }
  81. }
  82. private:
  83. typedef auto_ptr<AddressList> AddrListPtr;
  84. public:
  85. virtual void build(ConstElementPtr config) {
  86. AddressList newAddresses = parseAddresses(config, "listen_on");
  87. AddrListPtr old(new AddressList(server_.getListenAddresses()));
  88. server_.setListenAddresses(newAddresses);
  89. /*
  90. * Set the rollback addresses only after successful setting of the
  91. * new addresses, so we don't try to rollback if the setup is
  92. * unsuccessful (the above can easily throw).
  93. */
  94. rollbackAddresses_ = old;
  95. }
  96. virtual void commit() {
  97. rollbackAddresses_.release();
  98. }
  99. private:
  100. AuthSrv& server_;
  101. /**
  102. * This is the old address list, if we expect to roll back. When we commit,
  103. * this is set to NULL.
  104. */
  105. AddrListPtr rollbackAddresses_;
  106. };
  107. } // end of unnamed namespace
  108. AuthConfigParser*
  109. createAuthConfigParser(AuthSrv& server, const std::string& config_id) {
  110. // For the initial implementation we use a naive if-else blocks for
  111. // simplicity. In future we'll probably generalize it using map-like
  112. // data structure, and may even provide external register interface so
  113. // that it can be dynamically customized.
  114. if (config_id == "listen_on") {
  115. return (new ListenAddressConfig(server));
  116. } else if (config_id == "_commit_throw") {
  117. // This is for testing purpose only and should not appear in the
  118. // actual configuration syntax. While this could crash the caller
  119. // as a result, the server implementation is expected to perform
  120. // syntax level validation and should be safe in practice. In future,
  121. // we may introduce dynamic registration of configuration parsers,
  122. // and then this test can be done in a cleaner and safer way.
  123. return (new ThrowerCommitConfig());
  124. } else if (config_id == "version") {
  125. // Currently, the version identifier is ignored, but it should
  126. // later be used to mark backwards incompatible changes in the
  127. // config data
  128. return (new VersionConfig());
  129. } else if (config_id == "datasources") {
  130. // TODO: Ignored for now, since the value is probably used by
  131. // other modules. Once they have been removed from there, remove
  132. // it from here and the spec file.
  133. // We need to return something. The VersionConfig is empty now,
  134. // so we may abuse that one, as it is a short-term solution only.
  135. return (new VersionConfig());
  136. } else {
  137. isc_throw(AuthConfigError, "Unknown configuration identifier: " <<
  138. config_id);
  139. }
  140. }
  141. void
  142. configureAuthServer(AuthSrv& server, ConstElementPtr config_set) {
  143. if (!config_set) {
  144. isc_throw(AuthConfigError,
  145. "Null pointer is passed to configuration parser");
  146. }
  147. typedef boost::shared_ptr<AuthConfigParser> ParserPtr;
  148. vector<ParserPtr> parsers;
  149. typedef pair<string, ConstElementPtr> ConfigPair;
  150. try {
  151. BOOST_FOREACH(ConfigPair config_pair, config_set->mapValue()) {
  152. // We should eventually integrate the sqlite3 DB configuration to
  153. // this framework, but to minimize diff we begin with skipping that
  154. // part.
  155. if (config_pair.first == "database_file") {
  156. continue;
  157. }
  158. ParserPtr parser(createAuthConfigParser(server,
  159. config_pair.first));
  160. parser->build(config_pair.second);
  161. parsers.push_back(parser);
  162. }
  163. } catch (const AuthConfigError& ex) {
  164. throw; // simply rethrowing it
  165. } catch (const isc::Exception& ex) {
  166. isc_throw(AuthConfigError, "Server configuration failed: " <<
  167. ex.what());
  168. }
  169. try {
  170. BOOST_FOREACH(ParserPtr parser, parsers) {
  171. parser->commit();
  172. }
  173. } catch (...) {
  174. throw FatalError("Unrecoverable error: "
  175. "a configuration parser threw in commit");
  176. }
  177. }