main.cc 7.8 KB

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