main.cc 6.3 KB

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