main.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_config.h>
  31. #include <auth/datasrc_clients_mgr.h>
  32. #include <asiodns/asiodns.h>
  33. #include <asiolink/asiolink.h>
  34. #include <log/logger_support.h>
  35. #include <server_common/keyring.h>
  36. #include <server_common/socket_request.h>
  37. #include <boost/bind.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, bool* first_time,
  75. ModuleCCSession* config_session, const std::string&,
  76. isc::data::ConstElementPtr config,
  77. const isc::config::ConfigData&)
  78. {
  79. assert(server != NULL);
  80. // Note: remote config handler is requested to be exception free.
  81. // While the code below is not 100% exception free, such an exception
  82. // is really fatal and the server should actually stop. So we don't
  83. // bother to catch them; the exception would be propagated to the
  84. // top level of the server and terminate it.
  85. if (config->contains("classes")) {
  86. isc::datasrc::DataSrcClientListsPtr lists;
  87. if (*first_time) {
  88. // HACK: The default is not passed to the handler in the first
  89. // callback. This one will get the default (or, current value).
  90. // Further updates will work the usual way.
  91. assert(config_session != NULL);
  92. *first_time = false;
  93. server->getDataSrcClientsMgr().reconfigure(
  94. config_session->getRemoteConfigValue("data_sources",
  95. "classes"));
  96. } else {
  97. server->getDataSrcClientsMgr().reconfigure(config->get("classes"));
  98. }
  99. }
  100. }
  101. void
  102. usage() {
  103. cerr << "Usage: b10-auth [-v]"
  104. << endl;
  105. cerr << "\t-v: verbose logging (debug-level)" << endl;
  106. exit(1);
  107. }
  108. } // end of anonymous namespace
  109. int
  110. main(int argc, char* argv[]) {
  111. int ch;
  112. bool verbose = false;
  113. while ((ch = getopt(argc, argv, ":nu:v")) != -1) {
  114. switch (ch) {
  115. case 'v':
  116. verbose = true;
  117. break;
  118. case '?':
  119. default:
  120. usage();
  121. }
  122. }
  123. if (argc - optind > 0) {
  124. usage();
  125. }
  126. // Initialize logging. If verbose, we'll use maximum verbosity.
  127. isc::log::initLogger(AUTH_NAME,
  128. (verbose ? isc::log::DEBUG : isc::log::INFO),
  129. isc::log::MAX_DEBUG_LEVEL, NULL);
  130. int ret = 0;
  131. // XXX: we should eventually pass io_service here.
  132. Session* cc_session = NULL;
  133. Session* xfrin_session = NULL;
  134. bool xfrin_session_established = false; // XXX (see Trac #287)
  135. ModuleCCSession* config_session = NULL;
  136. XfroutClient xfrout_client(getXfroutSocketPath());
  137. SocketSessionForwarder ddns_forwarder(getDDNSSocketPath());
  138. try {
  139. string specfile;
  140. if (getenv("B10_FROM_BUILD")) {
  141. specfile = string(getenv("B10_FROM_BUILD")) +
  142. "/src/bin/auth/auth.spec";
  143. } else {
  144. specfile = string(AUTH_SPECFILE_LOCATION);
  145. }
  146. auth_server = new AuthSrv(xfrout_client, ddns_forwarder);
  147. LOG_INFO(auth_logger, AUTH_SERVER_CREATED);
  148. SimpleCallback* checkin = auth_server->getCheckinProvider();
  149. IOService& io_service = auth_server->getIOService();
  150. DNSLookup* lookup = auth_server->getDNSLookupProvider();
  151. DNSAnswer* answer = auth_server->getDNSAnswerProvider();
  152. DNSService dns_service(io_service, checkin, lookup, answer);
  153. auth_server->setDNSService(dns_service);
  154. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_DNS_SERVICES_CREATED);
  155. cc_session = new Session(io_service.get_io_service());
  156. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_CONFIG_CHANNEL_CREATED);
  157. // Initialize the Socket Requestor
  158. isc::server_common::initSocketRequestor(*cc_session, AUTH_NAME);
  159. // We delay starting listening to new commands/config just before we
  160. // go into the main loop to avoid confusion due to mixture of
  161. // synchronous and asynchronous operations (this would happen in
  162. // initial communication with the boss that takes place in
  163. // updateConfig() for listen_on and in initializing TSIG keys below).
  164. // Until then all operations on the CC session will take place
  165. // synchronously.
  166. config_session = new ModuleCCSession(specfile, *cc_session,
  167. my_config_handler,
  168. my_command_handler, false);
  169. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_CONFIG_CHANNEL_ESTABLISHED);
  170. xfrin_session = new Session(io_service.get_io_service());
  171. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_XFRIN_CHANNEL_CREATED);
  172. xfrin_session->establish(NULL);
  173. xfrin_session_established = true;
  174. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_XFRIN_CHANNEL_ESTABLISHED);
  175. auth_server->setXfrinSession(xfrin_session);
  176. // Configure the server. configureAuthServer() is expected to install
  177. // all initial configurations, but as a short term workaround we
  178. // handle the traditional "database_file" setup by directly calling
  179. // updateConfig().
  180. // if server load configure failed, we won't exit, give user second
  181. // chance to correct the configure.
  182. auth_server->setConfigSession(config_session);
  183. try {
  184. configureAuthServer(*auth_server, config_session->getFullConfig());
  185. auth_server->updateConfig(ElementPtr());
  186. } catch (const AuthConfigError& ex) {
  187. LOG_ERROR(auth_logger, AUTH_CONFIG_LOAD_FAIL).arg(ex.what());
  188. }
  189. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_LOAD_TSIG);
  190. isc::server_common::initKeyring(*config_session);
  191. auth_server->setTSIGKeyRing(&isc::server_common::keyring);
  192. // Start the data source configuration. We pass first_time and
  193. // config_session for the hack described in datasrcConfigHandler.
  194. bool first_time = true;
  195. config_session->addRemoteConfig("data_sources",
  196. boost::bind(datasrcConfigHandler,
  197. auth_server, &first_time,
  198. config_session,
  199. _1, _2, _3),
  200. false);
  201. // Now start asynchronous read.
  202. config_session->start();
  203. LOG_DEBUG(auth_logger, DBG_AUTH_START, AUTH_CONFIG_CHANNEL_STARTED);
  204. // Successfully initialized.
  205. LOG_INFO(auth_logger, AUTH_SERVER_STARTED);
  206. // Ping any interested module that (a new) auth is up
  207. // Currently, only the DDNS module is notified, but we could consider
  208. // make an announcement channel for these (one-way) messages
  209. cc_session->group_sendmsg(
  210. isc::config::createCommand(AUTH_STARTED_NOTIFICATION), "DDNS");
  211. io_service.run();
  212. } catch (const std::exception& ex) {
  213. LOG_FATAL(auth_logger, AUTH_SERVER_FAILED).arg(ex.what());
  214. ret = 1;
  215. }
  216. if (xfrin_session_established) {
  217. xfrin_session->disconnect();
  218. }
  219. // If we haven't registered callback for data sources, this will be just
  220. // no-op.
  221. config_session->removeRemoteConfig("data_sources");
  222. delete xfrin_session;
  223. delete config_session;
  224. delete cc_session;
  225. delete auth_server;
  226. return (ret);
  227. }