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