main.cc 6.6 KB

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