command.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <auth/command.h>
  15. #include <auth/auth_log.h>
  16. #include <auth/auth_srv.h>
  17. #include <cc/data.h>
  18. #include <datasrc/client_list.h>
  19. #include <config/ccsession.h>
  20. #include <exceptions/exceptions.h>
  21. #include <dns/rrclass.h>
  22. #include <string>
  23. #include <boost/scoped_ptr.hpp>
  24. #include <boost/shared_ptr.hpp>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. using boost::scoped_ptr;
  28. using namespace isc::auth;
  29. using namespace isc::config;
  30. using namespace isc::data;
  31. using namespace isc::datasrc;
  32. using namespace isc::dns;
  33. using namespace std;
  34. namespace {
  35. /// An exception that is thrown if an error occurs while handling a command
  36. /// on an \c AuthSrv object.
  37. ///
  38. /// Currently it's only used internally, since \c execAuthServerCommand()
  39. /// (which is the only interface to this module) catches all \c isc::
  40. /// exceptions and converts them.
  41. class AuthCommandError : public isc::Exception {
  42. public:
  43. AuthCommandError(const char* file, size_t line, const char* what) :
  44. isc::Exception(file, line, what) {}
  45. };
  46. /// An abstract base class that represents a single command identifier
  47. /// for an \c AuthSrv object.
  48. ///
  49. /// Each of derived classes of \c AuthCommand, which are hidden inside the
  50. /// implementation, corresponds to a command executed on \c AuthSrv, such as
  51. /// "shutdown". The derived class is responsible to execute the corresponding
  52. /// command with the given command arguments (if any) in its \c exec()
  53. /// method.
  54. ///
  55. /// In the initial implementation the existence of the command classes is
  56. /// hidden inside the implementation since the only public interface is
  57. /// \c execAuthServerCommand(), which does not expose this class.
  58. /// In future, we may want to make this framework more dynamic, i.e.,
  59. /// registering specific derived classes run time outside of this
  60. /// implementation. If and when that happens the definition of the abstract
  61. /// class will be published.
  62. class AuthCommand {
  63. ///
  64. /// \name Constructors and Destructor
  65. ///
  66. /// Note: The copy constructor and the assignment operator are
  67. /// intentionally defined as private to make it explicit that this is a
  68. /// pure base class.
  69. //@{
  70. private:
  71. AuthCommand(const AuthCommand& source);
  72. AuthCommand& operator=(const AuthCommand& source);
  73. protected:
  74. /// \brief The default constructor.
  75. ///
  76. /// This is intentionally defined as \c protected as this base class should
  77. /// never be instantiated (except as part of a derived class).
  78. AuthCommand() {}
  79. public:
  80. /// The destructor.
  81. virtual ~AuthCommand() {}
  82. //@}
  83. /// Execute a single control command.
  84. ///
  85. /// Specific derived methods can throw exceptions. When called via
  86. /// \c execAuthServerCommand(), all BIND 10 exceptions are caught
  87. /// and converted into an error code.
  88. /// The derived method may also throw an exception of class
  89. /// \c AuthCommandError when it encounters an internal error, such as
  90. /// semantics error on the command arguments.
  91. ///
  92. /// This method should return the execution result in the form of
  93. /// \c ConstElementPtr. It will be transparently used as the return
  94. /// value from the command handler called from the corresponding
  95. /// \c CCSession object. For a successful completion of the command,
  96. /// it should suffice to return the return value of
  97. /// \c isc::config::createAnswer() with no argument.
  98. ///
  99. /// \param server The \c AuthSrv object on which the command is executed.
  100. /// \param args Command specific argument.
  101. /// \return Command execution result.
  102. virtual ConstElementPtr exec(AuthSrv& server,
  103. isc::data::ConstElementPtr args) = 0;
  104. };
  105. // Handle the "shutdown" command. An optional parameter "pid" is used to
  106. // see if it is really for our instance.
  107. class ShutdownCommand : public AuthCommand {
  108. public:
  109. virtual ConstElementPtr exec(AuthSrv& server,
  110. isc::data::ConstElementPtr args)
  111. {
  112. // Is the pid argument provided?
  113. if (args && args->contains("pid")) {
  114. // If it is, we check it is the same as our PID
  115. // This might throw in case the type is not an int, but that's
  116. // OK, as it'll get converted to an error on higher level.
  117. const int pid(args->get("pid")->intValue());
  118. const pid_t my_pid(getpid());
  119. if (my_pid != pid) {
  120. // It is not for us
  121. //
  122. // Note that this is completely expected situation, if
  123. // there are multiple instances of the server running and
  124. // another instance is being shut down, we get the message
  125. // too, due to the multicast nature of our message bus.
  126. return (createAnswer());
  127. }
  128. }
  129. LOG_DEBUG(auth_logger, DBG_AUTH_SHUT, AUTH_SHUTDOWN);
  130. server.stop();
  131. return (createAnswer());
  132. }
  133. };
  134. // Handle the "getstats" command. The argument is a list.
  135. class GetStatsCommand : public AuthCommand {
  136. public:
  137. virtual ConstElementPtr exec(AuthSrv& server, isc::data::ConstElementPtr) {
  138. return (createAnswer(0, server.getStatistics()));
  139. }
  140. };
  141. class StartDDNSForwarderCommand : public AuthCommand {
  142. public:
  143. virtual ConstElementPtr exec(AuthSrv& server,
  144. isc::data::ConstElementPtr) {
  145. server.createDDNSForwarder();
  146. return (createAnswer());
  147. }
  148. };
  149. class StopDDNSForwarderCommand : public AuthCommand {
  150. public:
  151. virtual ConstElementPtr exec(AuthSrv& server,
  152. isc::data::ConstElementPtr) {
  153. server.destroyDDNSForwarder();
  154. return (createAnswer());
  155. }
  156. };
  157. // Handle the "loadzone" command.
  158. class LoadZoneCommand : public AuthCommand {
  159. public:
  160. virtual ConstElementPtr exec(AuthSrv& server,
  161. isc::data::ConstElementPtr args)
  162. {
  163. if (args == NULL) {
  164. isc_throw(AuthCommandError, "Null argument");
  165. }
  166. ConstElementPtr class_elem = args->get("class");
  167. RRClass zone_class(class_elem ? RRClass(class_elem->stringValue()) :
  168. RRClass::IN());
  169. ConstElementPtr origin_elem = args->get("origin");
  170. if (!origin_elem) {
  171. isc_throw(AuthCommandError, "Zone origin is missing");
  172. }
  173. Name origin(origin_elem->stringValue());
  174. const boost::shared_ptr<isc::datasrc::ConfigurableClientList>
  175. list(server.getClientList(zone_class));
  176. if (!list) {
  177. isc_throw(AuthCommandError, "There's no client list for "
  178. "class " << zone_class);
  179. }
  180. switch (list->reload(origin)) {
  181. case ConfigurableClientList::ZONE_RELOADED:
  182. // Everything worked fine.
  183. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_LOAD_ZONE)
  184. .arg(zone_class).arg(origin);
  185. return (createAnswer());
  186. case ConfigurableClientList::ZONE_NOT_FOUND:
  187. isc_throw(AuthCommandError, "Zone " << origin << "/" <<
  188. zone_class << " was not found in any configured "
  189. "data source. Configure it first.");
  190. case ConfigurableClientList::ZONE_NOT_CACHED:
  191. isc_throw(AuthCommandError, "Zone " << origin << "/" <<
  192. zone_class << " is not served from memory, but "
  193. "directly from the data source. It is not possible "
  194. "to reload it into memory. Configure it to be cached "
  195. "first.");
  196. case ConfigurableClientList::CACHE_DISABLED:
  197. // This is an internal error. Auth server must have the cache
  198. // enabled.
  199. isc_throw(isc::Unexpected, "Cache disabled in client list of "
  200. "class " << zone_class);
  201. }
  202. return (createAnswer());
  203. }
  204. };
  205. // The factory of command objects.
  206. AuthCommand*
  207. createAuthCommand(const string& command_id) {
  208. // For the initial implementation we use a naive if-else blocks
  209. // (see also createAuthConfigParser())
  210. if (command_id == "shutdown") {
  211. return (new ShutdownCommand());
  212. } else if (command_id == "getstats") {
  213. return (new GetStatsCommand());
  214. } else if (command_id == "loadzone") {
  215. return (new LoadZoneCommand());
  216. } else if (command_id == "start_ddns_forwarder") {
  217. return (new StartDDNSForwarderCommand());
  218. } else if (command_id == "stop_ddns_forwarder") {
  219. return (new StopDDNSForwarderCommand());
  220. } else if (false && command_id == "_throw_exception") {
  221. // This is for testing purpose only and should not appear in the
  222. // actual configuration syntax.
  223. // XXX: ModuleCCSession doesn't seem to validate commands (unlike
  224. // config), so we should disable this case for now.
  225. throw runtime_error("throwing for test");
  226. }
  227. isc_throw(AuthCommandError, "Unknown command identifier: " << command_id);
  228. }
  229. } // end of unnamed namespace
  230. ConstElementPtr
  231. execAuthServerCommand(AuthSrv& server, const string& command_id,
  232. ConstElementPtr args)
  233. {
  234. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_RECEIVED_COMMAND).arg(command_id);
  235. try {
  236. return (scoped_ptr<AuthCommand>(
  237. createAuthCommand(command_id))->exec(server, args));
  238. } catch (const isc::Exception& ex) {
  239. LOG_ERROR(auth_logger, AUTH_COMMAND_FAILED).arg(command_id)
  240. .arg(ex.what());
  241. return (createAnswer(1, ex.what()));
  242. }
  243. }