main.cc 9.3 KB

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