command.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <string>
  15. #include <boost/scoped_ptr.hpp>
  16. #include <boost/shared_ptr.hpp>
  17. #include <exceptions/exceptions.h>
  18. #include <dns/rrclass.h>
  19. #include <cc/data.h>
  20. #include <datasrc/memory_datasrc.h>
  21. #include <config/ccsession.h>
  22. #include <auth/auth_log.h>
  23. #include <auth/auth_srv.h>
  24. #include <auth/command.h>
  25. using boost::scoped_ptr;
  26. using namespace isc::auth;
  27. using namespace isc::config;
  28. using namespace isc::data;
  29. using namespace isc::datasrc;
  30. using namespace isc::dns;
  31. using namespace std;
  32. namespace {
  33. /// An exception that is thrown if an error occurs while handling a command
  34. /// on an \c AuthSrv object.
  35. ///
  36. /// Currently it's only used internally, since \c execAuthServerCommand()
  37. /// (which is the only interface to this module) catches all \c isc::
  38. /// exceptions and converts them.
  39. class AuthCommandError : public isc::Exception {
  40. public:
  41. AuthCommandError(const char* file, size_t line, const char* what) :
  42. isc::Exception(file, line, what) {}
  43. };
  44. /// An abstract base class that represents a single command identifier
  45. /// for an \c AuthSrv object.
  46. ///
  47. /// Each of derived classes of \c AuthCommand, which are hidden inside the
  48. /// implementation, corresponds to a command executed on \c AuthSrv, such as
  49. /// "shutdown". The derived class is responsible to execute the corresponding
  50. /// command with the given command arguments (if any) in its \c exec()
  51. /// method.
  52. ///
  53. /// In the initial implementation the existence of the command classes is
  54. /// hidden inside the implementation since the only public interface is
  55. /// \c execAuthServerCommand(), which does not expose this class.
  56. /// In future, we may want to make this framework more dynamic, i.e.,
  57. /// registering specific derived classes run time outside of this
  58. /// implementation. If and when that happens the definition of the abstract
  59. /// class will be published.
  60. class AuthCommand {
  61. ///
  62. /// \name Constructors and Destructor
  63. ///
  64. /// Note: The copy constructor and the assignment operator are
  65. /// intentionally defined as private to make it explicit that this is a
  66. /// pure base class.
  67. //@{
  68. private:
  69. AuthCommand(const AuthCommand& source);
  70. AuthCommand& operator=(const AuthCommand& source);
  71. protected:
  72. /// \brief The default constructor.
  73. ///
  74. /// This is intentionally defined as \c protected as this base class should
  75. /// never be instantiated (except as part of a derived class).
  76. AuthCommand() {}
  77. public:
  78. /// The destructor.
  79. virtual ~AuthCommand() {}
  80. //@}
  81. /// Execute a single control command.
  82. ///
  83. /// Specific derived methods can throw exceptions. When called via
  84. /// \c execAuthServerCommand(), all BIND 10 exceptions are caught
  85. /// and converted into an error code.
  86. /// The derived method may also throw an exception of class
  87. /// \c AuthCommandError when it encounters an internal error, such as
  88. /// semantics error on the command arguments.
  89. ///
  90. /// \param server The \c AuthSrv object on which the command is executed.
  91. /// \param args Command specific argument.
  92. virtual void exec(AuthSrv& server, isc::data::ConstElementPtr args) = 0;
  93. };
  94. // Handle the "shutdown" command. No argument is assumed.
  95. class ShutdownCommand : public AuthCommand {
  96. public:
  97. virtual void exec(AuthSrv& server, isc::data::ConstElementPtr) {
  98. server.stop();
  99. }
  100. };
  101. // Handle the "sendstats" command. No argument is assumed.
  102. class SendStatsCommand : public AuthCommand {
  103. public:
  104. virtual void exec(AuthSrv& server, isc::data::ConstElementPtr) {
  105. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_RECEIVED_SENDSTATS);
  106. server.submitStatistics();
  107. }
  108. };
  109. // Handle the "loadzone" command.
  110. class LoadZoneCommand : public AuthCommand {
  111. public:
  112. virtual void exec(AuthSrv& server, isc::data::ConstElementPtr args) {
  113. // parse and validate the args.
  114. if (!validate(server, args)) {
  115. return;
  116. }
  117. // Load a new zone and replace the current zone with the new one.
  118. // TODO: eventually this should be incremental or done in some way
  119. // that doesn't block other server operations.
  120. // TODO: we may (should?) want to check the "last load time" and
  121. // the timestamp of the file and skip loading if the file isn't newer.
  122. boost::shared_ptr<InMemoryZoneFinder> zone_finder(
  123. new InMemoryZoneFinder(old_zone_finder->getClass(),
  124. old_zone_finder->getOrigin()));
  125. zone_finder->load(old_zone_finder->getFileName());
  126. old_zone_finder->swap(*zone_finder);
  127. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_LOAD_ZONE)
  128. .arg(zone_finder->getOrigin()).arg(zone_finder->getClass());
  129. }
  130. private:
  131. // zone finder to be updated with the new file.
  132. boost::shared_ptr<InMemoryZoneFinder> old_zone_finder;
  133. // A helper private method to parse and validate command parameters.
  134. // On success, it sets 'old_zone_finder' to the zone to be updated.
  135. // It returns true if everything is okay; and false if the command is
  136. // valid but there's no need for further process.
  137. bool validate(AuthSrv& server, isc::data::ConstElementPtr args) {
  138. if (args == NULL) {
  139. isc_throw(AuthCommandError, "Null argument");
  140. }
  141. // In this initial implementation, we assume memory data source
  142. // for class IN by default.
  143. ConstElementPtr datasrc_elem = args->get("datasrc");
  144. if (datasrc_elem) {
  145. if (datasrc_elem->stringValue() == "sqlite3") {
  146. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_SQLITE3);
  147. return (false);
  148. } else if (datasrc_elem->stringValue() != "memory") {
  149. // (note: at this point it's guaranteed that datasrc_elem
  150. // is of string type)
  151. isc_throw(AuthCommandError,
  152. "Data source type " << datasrc_elem->stringValue()
  153. << " is not supported");
  154. }
  155. }
  156. ConstElementPtr class_elem = args->get("class");
  157. const RRClass zone_class = class_elem ?
  158. RRClass(class_elem->stringValue()) : RRClass::IN();
  159. AuthSrv::InMemoryClientPtr datasrc(server.getInMemoryClient(zone_class));
  160. if (datasrc == NULL) {
  161. isc_throw(AuthCommandError, "Memory data source is disabled");
  162. }
  163. ConstElementPtr origin_elem = args->get("origin");
  164. if (!origin_elem) {
  165. isc_throw(AuthCommandError, "Zone origin is missing");
  166. }
  167. const Name origin(origin_elem->stringValue());
  168. // Get the current zone
  169. const InMemoryClient::FindResult result = datasrc->findZone(origin);
  170. if (result.code != result::SUCCESS) {
  171. isc_throw(AuthCommandError, "Zone " << origin <<
  172. " is not found in data source");
  173. }
  174. old_zone_finder = boost::dynamic_pointer_cast<InMemoryZoneFinder>(
  175. result.zone_finder);
  176. return (true);
  177. }
  178. };
  179. // The factory of command objects.
  180. AuthCommand*
  181. createAuthCommand(const string& command_id) {
  182. // For the initial implementation we use a naive if-else blocks
  183. // (see also createAuthConfigParser())
  184. if (command_id == "shutdown") {
  185. return (new ShutdownCommand());
  186. } else if (command_id == "sendstats") {
  187. return (new SendStatsCommand());
  188. } else if (command_id == "loadzone") {
  189. return (new LoadZoneCommand());
  190. } else if (false && command_id == "_throw_exception") {
  191. // This is for testing purpose only and should not appear in the
  192. // actual configuration syntax.
  193. // XXX: ModuleCCSession doesn't seem to validate commands (unlike
  194. // config), so we should disable this case for now.
  195. throw runtime_error("throwing for test");
  196. }
  197. isc_throw(AuthCommandError, "Unknown command identifier: " << command_id);
  198. }
  199. } // end of unnamed namespace
  200. ConstElementPtr
  201. execAuthServerCommand(AuthSrv& server, const string& command_id,
  202. ConstElementPtr args)
  203. {
  204. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_RECEIVED_COMMAND).arg(command_id);
  205. try {
  206. scoped_ptr<AuthCommand>(createAuthCommand(command_id))->exec(server,
  207. args);
  208. } catch (const isc::Exception& ex) {
  209. LOG_ERROR(auth_logger, AUTH_COMMAND_FAILED).arg(command_id)
  210. .arg(ex.what());
  211. return (createAnswer(1, ex.what()));
  212. }
  213. return (createAnswer());
  214. }