main.cc 9.6 KB

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