main.cc 8.1 KB

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