main.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <dns/message.h>
  27. #include <dns/messagerenderer.h>
  28. #include <cc/session.h>
  29. #include <cc/data.h>
  30. #include <config/ccsession.h>
  31. #include <xfr/xfrout_client.h>
  32. #include <auth/spec_config.h>
  33. #include <auth/common.h>
  34. #include <auth/auth_config.h>
  35. #include <auth/command.h>
  36. #include <auth/change_user.h>
  37. #include <auth/auth_srv.h>
  38. #include <asiodns/asiodns.h>
  39. #include <asiolink/asiolink.h>
  40. #include <log/dummylog.h>
  41. #include <server_common/keyring.h>
  42. using namespace std;
  43. using namespace isc::data;
  44. using namespace isc::cc;
  45. using namespace isc::config;
  46. using namespace isc::dns;
  47. using namespace isc::util;
  48. using namespace isc::xfr;
  49. using namespace isc::asiolink;
  50. using namespace isc::asiodns;
  51. namespace {
  52. bool verbose_mode = false;
  53. /* need global var for config/command handlers.
  54. * todo: turn this around, and put handlers in the authserver
  55. * class itself? */
  56. AuthSrv *auth_server;
  57. ConstElementPtr
  58. my_config_handler(ConstElementPtr new_config) {
  59. return (auth_server->updateConfig(new_config));
  60. }
  61. ConstElementPtr
  62. my_command_handler(const string& command, ConstElementPtr args) {
  63. assert(auth_server != NULL);
  64. return (execAuthServerCommand(*auth_server, command, args));
  65. }
  66. void
  67. usage() {
  68. cerr << "Usage: b10-auth [-u user] [-nv]"
  69. << endl;
  70. cerr << "\t-n: do not cache answers in memory" << endl;
  71. cerr << "\t-u: change process UID to the specified user" << endl;
  72. cerr << "\t-v: verbose output" << endl;
  73. exit(1);
  74. }
  75. } // end of anonymous namespace
  76. int
  77. main(int argc, char* argv[]) {
  78. int ch;
  79. const char* uid = NULL;
  80. bool cache = true;
  81. while ((ch = getopt(argc, argv, ":nu:v")) != -1) {
  82. switch (ch) {
  83. case 'n':
  84. cache = false;
  85. break;
  86. case 'u':
  87. uid = optarg;
  88. break;
  89. case 'v':
  90. verbose_mode = true;
  91. isc::log::denabled = true;
  92. break;
  93. case '?':
  94. default:
  95. usage();
  96. }
  97. }
  98. if (argc - optind > 0) {
  99. usage();
  100. }
  101. int ret = 0;
  102. // XXX: we should eventually pass io_service here.
  103. Session* cc_session = NULL;
  104. Session* xfrin_session = NULL;
  105. Session* statistics_session = NULL;
  106. bool xfrin_session_established = false; // XXX (see Trac #287)
  107. bool statistics_session_established = false; // XXX (see Trac #287)
  108. ModuleCCSession* config_session = NULL;
  109. XfroutClient xfrout_client(getXfroutSocketPath());
  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(cache, xfrout_client);
  119. auth_server->setVerbose(verbose_mode);
  120. cout << "[b10-auth] Server created." << endl;
  121. SimpleCallback* checkin = auth_server->getCheckinProvider();
  122. IOService& io_service = auth_server->getIOService();
  123. DNSLookup* lookup = auth_server->getDNSLookupProvider();
  124. DNSAnswer* answer = auth_server->getDNSAnswerProvider();
  125. DNSService dns_service(io_service, checkin, lookup, answer);
  126. auth_server->setDNSService(dns_service);
  127. cout << "[b10-auth] DNSServices created." << endl;
  128. cc_session = new Session(io_service.get_io_service());
  129. cout << "[b10-auth] Configuration session channel created." << endl;
  130. config_session = new ModuleCCSession(specfile, *cc_session,
  131. my_config_handler,
  132. my_command_handler);
  133. cout << "[b10-auth] Configuration channel established." << endl;
  134. xfrin_session = new Session(io_service.get_io_service());
  135. cout << "[b10-auth] Xfrin session channel created." << endl;
  136. xfrin_session->establish(NULL);
  137. xfrin_session_established = true;
  138. cout << "[b10-auth] Xfrin session channel established." << endl;
  139. statistics_session = new Session(io_service.get_io_service());
  140. cout << "[b10-auth] Statistics session channel created." << endl;
  141. statistics_session->establish(NULL);
  142. statistics_session_established = true;
  143. cout << "[b10-auth] Statistics session channel established." << endl;
  144. auth_server->setXfrinSession(xfrin_session);
  145. auth_server->setStatisticsSession(statistics_session);
  146. // Configure the server. configureAuthServer() is expected to install
  147. // all initial configurations, but as a short term workaround we
  148. // handle the traditional "database_file" setup by directly calling
  149. // updateConfig().
  150. // if server load configure failed, we won't exit, give user second chance
  151. // to correct the configure.
  152. auth_server->setConfigSession(config_session);
  153. try {
  154. configureAuthServer(*auth_server, config_session->getFullConfig());
  155. auth_server->updateConfig(ElementPtr());
  156. } catch (const AuthConfigError& ex) {
  157. cout << "[bin10-auth] Server load config failed:" << ex.what() << endl;
  158. }
  159. if (uid != NULL) {
  160. changeUser(uid);
  161. }
  162. cout << "[b10-auth] Loading TSIG keys" << endl;
  163. isc::server_common::initKeyring(*config_session);
  164. cout << "[b10-auth] Server started." << endl;
  165. io_service.run();
  166. } catch (const std::exception& ex) {
  167. cerr << "[b10-auth] Server failed: " << ex.what() << endl;
  168. ret = 1;
  169. }
  170. if (statistics_session_established) {
  171. statistics_session->disconnect();
  172. }
  173. if (xfrin_session_established) {
  174. xfrin_session->disconnect();
  175. }
  176. delete statistics_session;
  177. delete xfrin_session;
  178. delete config_session;
  179. delete cc_session;
  180. delete auth_server;
  181. return (ret);
  182. }