command.cc 9.2 KB

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