main.cc 9.6 KB

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