main.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (C) 2009-2011 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 <config.h>
  15. #include <exceptions/exceptions.h>
  16. #include <util/buffer.h>
  17. #include <util/io/socketsession.h>
  18. #include <dns/message.h>
  19. #include <dns/messagerenderer.h>
  20. #include <cc/session.h>
  21. #include <cc/data.h>
  22. #include <config/ccsession.h>
  23. #include <xfr/xfrout_client.h>
  24. #include <auth/spec_config.h>
  25. #include <auth/common.h>
  26. #include <auth/auth_config.h>
  27. #include <auth/command.h>
  28. #include <auth/auth_srv.h>
  29. #include <auth/auth_log.h>
  30. #include <auth/datasrc_configurator.h>
  31. #include <asiodns/asiodns.h>
  32. #include <asiolink/asiolink.h>
  33. #include <log/logger_support.h>
  34. #include <server_common/keyring.h>
  35. #include <server_common/socket_request.h>
  36. #include <boost/bind.hpp>
  37. #include <boost/scoped_ptr.hpp>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <sys/select.h>
  41. #include <netdb.h>
  42. #include <netinet/in.h>
  43. #include <stdlib.h>
  44. #include <errno.h>
  45. #include <cassert>
  46. #include <iostream>
  47. using namespace std;
  48. using namespace isc::asiodns;
  49. using namespace isc::asiolink;
  50. using namespace isc::auth;
  51. using namespace isc::cc;
  52. using namespace isc::config;
  53. using namespace isc::data;
  54. using namespace isc::dns;
  55. using namespace isc::log;
  56. using namespace isc::util;
  57. using namespace isc::util::io;
  58. using namespace isc::xfr;
  59. namespace {
  60. /* need global var for config/command handlers.
  61. * todo: turn this around, and put handlers in the authserver
  62. * class itself? */
  63. AuthSrv* auth_server;
  64. ConstElementPtr
  65. my_config_handler(ConstElementPtr new_config) {
  66. return (auth_server->updateConfig(new_config));
  67. }
  68. ConstElementPtr
  69. my_command_handler(const string& command, ConstElementPtr args) {
  70. assert(auth_server != NULL);
  71. return (execAuthServerCommand(*auth_server, command, args));
  72. }
  73. void
  74. datasrcConfigHandler(AuthSrv* server, DataSourceConfigurator* configurator,
  75. const std::string&, isc::data::ConstElementPtr config,
  76. const isc::config::ConfigData&)
  77. {
  78. assert(server != NULL);
  79. if (config->contains("classes")) {
  80. configurator->reconfigure(*server, config->get("classes"));
  81. }
  82. }
  83. void
  84. usage() {
  85. cerr << "Usage: b10-auth [-v]"
  86. << endl;
  87. cerr << "\t-v: verbose logging (debug-level)" << endl;
  88. exit(1);
  89. }
  90. } // end of anonymous namespace
  91. int
  92. main(int argc, char* argv[]) {
  93. int ch;
  94. bool verbose = false;
  95. while ((ch = getopt(argc, argv, ":nu:v")) != -1) {
  96. switch (ch) {
  97. case 'v':
  98. verbose = true;
  99. break;
  100. case '?':
  101. default:
  102. usage();
  103. }
  104. }
  105. if (argc - optind > 0) {
  106. usage();
  107. }
  108. // Initialize logging. If verbose, we'll use maximum verbosity.
  109. isc::log::initLogger(AUTH_NAME,
  110. (verbose ? isc::log::DEBUG : isc::log::INFO),
  111. isc::log::MAX_DEBUG_LEVEL, NULL);
  112. int ret = 0;
  113. // XXX: we should eventually pass io_service here.
  114. Session* cc_session = NULL;
  115. Session* xfrin_session = NULL;
  116. bool xfrin_session_established = false; // XXX (see Trac #287)
  117. ModuleCCSession* config_session = NULL;
  118. XfroutClient xfrout_client(getXfroutSocketPath());
  119. SocketSessionForwarder ddns_forwarder(getDDNSSocketPath());
  120. boost::scoped_ptr<DataSourceConfigurator> datasrc_configurator;
  121. try {
  122. string specfile;
  123. if (getenv("B10_FROM_BUILD")) {
  124. specfile = string(getenv("B10_FROM_BUILD")) +
  125. "/src/bin/auth/auth.spec";
  126. } else {
  127. specfile = string(AUTH_SPECFILE_LOCATION);
  128. }
  129. auth_server = new AuthSrv(xfrout_client, ddns_forwarder);
  130. LOG_INFO(auth_logger, AUTH_SERVER_CREATED);
  131. SimpleCallback* checkin = auth_server->getCheckinProvider();
  132. IOService& io_service = auth_server->getIOService();
  133. DNSLookup* lookup = auth_server->getDNSLookupProvider();
  134. DNSAnswer* answer = auth_server->getDNSAnswerProvider();
  135. DNSService dns_service(io_service, checkin, lookup, answer);
  136. auth_server->setDNSService(dns_service);
  137. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_DNS_SERVICES_CREATED);
  138. cc_session = new Session(io_service.get_io_service());
  139. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_CONFIG_CHANNEL_CREATED);
  140. // Initialize the Socket Requestor
  141. isc::server_common::initSocketRequestor(*cc_session, AUTH_NAME);
  142. // We delay starting listening to new commands/config just before we
  143. // go into the main loop to avoid confusion due to mixture of
  144. // synchronous and asynchronous operations (this would happen in
  145. // initial communication with the boss that takes place in
  146. // updateConfig() for listen_on and in initializing TSIG keys below).
  147. // Until then all operations on the CC session will take place
  148. // synchronously.
  149. config_session = new ModuleCCSession(specfile, *cc_session,
  150. my_config_handler,
  151. my_command_handler, false);
  152. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_CONFIG_CHANNEL_ESTABLISHED);
  153. xfrin_session = new Session(io_service.get_io_service());
  154. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_XFRIN_CHANNEL_CREATED);
  155. xfrin_session->establish(NULL);
  156. xfrin_session_established = true;
  157. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_XFRIN_CHANNEL_ESTABLISHED);
  158. auth_server->setXfrinSession(xfrin_session);
  159. // Configure the server. configureAuthServer() is expected to install
  160. // all initial configurations, but as a short term workaround we
  161. // handle the traditional "database_file" setup by directly calling
  162. // updateConfig().
  163. // if server load configure failed, we won't exit, give user second
  164. // chance to correct the configure.
  165. auth_server->setConfigSession(config_session);
  166. try {
  167. configureAuthServer(*auth_server, config_session->getFullConfig());
  168. auth_server->updateConfig(ElementPtr());
  169. } catch (const AuthConfigError& ex) {
  170. LOG_ERROR(auth_logger, AUTH_CONFIG_LOAD_FAIL).arg(ex.what());
  171. }
  172. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_LOAD_TSIG);
  173. isc::server_common::initKeyring(*config_session);
  174. auth_server->setTSIGKeyRing(&isc::server_common::keyring);
  175. // Start the data source configuration
  176. datasrc_configurator.reset(new DataSourceConfigurator);
  177. config_session->addRemoteConfig("data_sources",
  178. boost::bind(datasrcConfigHandler,
  179. auth_server,
  180. datasrc_configurator.get(),
  181. _1, _2, _3),
  182. false);
  183. // HACK: The default is not passed to the handler. This one will
  184. // get the default (or, current value). Further updates will work
  185. // the usual way.
  186. datasrc_configurator->reconfigure(
  187. *auth_server,
  188. config_session->getRemoteConfigValue("data_sources", "classes"));
  189. // Now start asynchronous read.
  190. config_session->start();
  191. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_CONFIG_CHANNEL_STARTED);
  192. // Successfully initialized.
  193. LOG_INFO(auth_logger, AUTH_SERVER_STARTED);
  194. // Ping any interested module that (a new) auth is up
  195. // Currently, only the DDNS module is notified, but we could consider
  196. // make an announcement channel for these (one-way) messages
  197. cc_session->group_sendmsg(
  198. isc::config::createCommand(AUTH_STARTED_NOTIFICATION), "DDNS");
  199. io_service.run();
  200. } catch (const std::exception& ex) {
  201. LOG_FATAL(auth_logger, AUTH_SERVER_FAILED).arg(ex.what());
  202. ret = 1;
  203. }
  204. if (xfrin_session_established) {
  205. xfrin_session->disconnect();
  206. }
  207. if (datasrc_configurator) {
  208. config_session->removeRemoteConfig("data_sources");
  209. }
  210. delete xfrin_session;
  211. delete config_session;
  212. delete cc_session;
  213. delete auth_server;
  214. return (ret);
  215. }