auth_config.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright (C) 2010 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 <dns/name.h>
  15. #include <dns/rrclass.h>
  16. #include <cc/data.h>
  17. #include <datasrc/memory_datasrc.h>
  18. #include <datasrc/zonetable.h>
  19. #include <datasrc/factory.h>
  20. #include <auth/auth_srv.h>
  21. #include <auth/auth_config.h>
  22. #include <auth/common.h>
  23. #include <server_common/portconfig.h>
  24. #include <boost/foreach.hpp>
  25. #include <boost/shared_ptr.hpp>
  26. #include <boost/scoped_ptr.hpp>
  27. #include <set>
  28. #include <string>
  29. #include <utility>
  30. #include <vector>
  31. using namespace std;
  32. using namespace isc::dns;
  33. using namespace isc::data;
  34. using namespace isc::datasrc;
  35. using namespace isc::server_common::portconfig;
  36. namespace {
  37. /// A derived \c AuthConfigParser class for the "datasources" configuration
  38. /// identifier.
  39. class DatasourcesConfig : public AuthConfigParser {
  40. public:
  41. DatasourcesConfig(AuthSrv& server) : server_(server)
  42. {}
  43. virtual void build(ConstElementPtr config_value);
  44. virtual void commit();
  45. private:
  46. AuthSrv& server_;
  47. vector<boost::shared_ptr<AuthConfigParser> > datasources_;
  48. set<string> configured_sources_;
  49. vector<pair<RRClass, DataSourceClientContainerPtr> > clients_;
  50. };
  51. /// A derived \c AuthConfigParser for the version value
  52. /// (which is not used at this moment)
  53. class VersionConfig : public AuthConfigParser {
  54. public:
  55. VersionConfig() {}
  56. virtual void build(ConstElementPtr) {};
  57. virtual void commit() {};
  58. };
  59. void
  60. DatasourcesConfig::build(ConstElementPtr config_value) {
  61. BOOST_FOREACH(ConstElementPtr datasrc_elem, config_value->listValue()) {
  62. // The caller is supposed to perform syntax-level checks, but we'll
  63. // do minimum level of validation ourselves so that we won't crash due
  64. // to a buggy application.
  65. ConstElementPtr datasrc_type = datasrc_elem->get("type");
  66. if (!datasrc_type) {
  67. isc_throw(AuthConfigError, "Missing data source type");
  68. }
  69. if (configured_sources_.find(datasrc_type->stringValue()) !=
  70. configured_sources_.end()) {
  71. isc_throw(AuthConfigError, "Data source type '" <<
  72. datasrc_type->stringValue() << "' already configured");
  73. }
  74. // Apart from that it's not really easy to get at the default
  75. // class value for the class here, it should probably really
  76. // be a property of the instantiated data source. For now
  77. // use hardcoded default IN.
  78. const RRClass rrclass =
  79. datasrc_elem->contains("class") ?
  80. RRClass(datasrc_elem->get("class")->stringValue()) : RRClass::IN();
  81. // Right now, we only support the in-memory data source for the
  82. // RR class of IN. We reject other cases explicitly by hardcoded
  83. // checks. This will soon be generalized, at which point these
  84. // checks will also have to be cleaned up.
  85. if (rrclass != RRClass::IN()) {
  86. isc_throw(isc::InvalidParameter, "Unsupported data source class: "
  87. << rrclass);
  88. }
  89. if (datasrc_type->stringValue() != "memory") {
  90. isc_throw(AuthConfigError, "Unsupported data source type: "
  91. << datasrc_type->stringValue());
  92. }
  93. // Create a new client for the specified data source and store it
  94. // in the local vector. For now, we always build a new client
  95. // from the scratch, and replace any existing ones with the new ones.
  96. // We might eventually want to optimize building zones (in case of
  97. // reloading) by selectively loading fresh zones for data source
  98. // where zone loading is expensive (such as in-memory).
  99. clients_.push_back(
  100. pair<RRClass, DataSourceClientContainerPtr>(
  101. rrclass,
  102. DataSourceClientContainerPtr(new DataSourceClientContainer(
  103. datasrc_type->stringValue(),
  104. datasrc_elem))));
  105. configured_sources_.insert(datasrc_type->stringValue());
  106. }
  107. }
  108. void
  109. DatasourcesConfig::commit() {
  110. // As noted in build(), the current implementation only supports the
  111. // in-memory data source for class IN, and build() should have ensured
  112. // it. So, depending on the vector is empty or not, we either clear
  113. // or install an in-memory data source for the server.
  114. //
  115. // When we generalize it, we'll somehow install all data source clients
  116. // built in the vector, clearing deleted ones from the server.
  117. if (clients_.empty()) {
  118. server_.setInMemoryClient(RRClass::IN(),
  119. DataSourceClientContainerPtr());
  120. } else {
  121. server_.setInMemoryClient(clients_.front().first,
  122. clients_.front().second);
  123. }
  124. }
  125. /// A derived \c AuthConfigParser class for the "statistics-internal"
  126. /// configuration identifier.
  127. class StatisticsIntervalConfig : public AuthConfigParser {
  128. public:
  129. StatisticsIntervalConfig(AuthSrv& server) :
  130. server_(server), interval_(0)
  131. {}
  132. virtual void build(ConstElementPtr config_value) {
  133. const int32_t config_interval = config_value->intValue();
  134. if (config_interval < 0) {
  135. isc_throw(AuthConfigError, "Negative statistics interval value: "
  136. << config_interval);
  137. }
  138. if (config_interval > 86400) {
  139. isc_throw(AuthConfigError, "Statistics interval value "
  140. << config_interval
  141. << " must be equal to or shorter than 86400");
  142. }
  143. interval_ = config_interval;
  144. }
  145. virtual void commit() {
  146. // setStatisticsTimerInterval() is not 100% exception free. But
  147. // exceptions should happen only in a very rare situation, so we
  148. // let them be thrown and subsequently regard them as a fatal error.
  149. server_.setStatisticsTimerInterval(interval_);
  150. }
  151. private:
  152. AuthSrv& server_;
  153. uint32_t interval_;
  154. };
  155. /// A special parser for testing: it throws from commit() despite the
  156. /// suggested convention of the class interface.
  157. class ThrowerCommitConfig : public AuthConfigParser {
  158. public:
  159. virtual void build(ConstElementPtr) {} // ignore param, do nothing
  160. virtual void commit() {
  161. throw 10;
  162. }
  163. };
  164. /**
  165. * \brief Configuration parser for listen_on.
  166. *
  167. * It parses and sets the listening addresses of the server.
  168. *
  169. * It acts in unusual way. Since actually binding (changing) the sockets
  170. * is an operation that is expected to throw often, it shouldn't happen
  171. * in commit. Thefere we do it in build. But if the config is not committed
  172. * then, we would have it wrong. So we store the old addresses and if
  173. * commit is not called before destruction of the object, we return the
  174. * old addresses (which is the same kind of dangerous operation, but it is
  175. * expected that if we just managed to bind some and had the old ones binded
  176. * before, it should work).
  177. *
  178. * We might do something better in future (like open only the ports that are
  179. * extra, put them in in commit and close the old ones), but that's left out
  180. * for now.
  181. */
  182. class ListenAddressConfig : public AuthConfigParser {
  183. public:
  184. ListenAddressConfig(AuthSrv& server) :
  185. server_(server)
  186. { }
  187. ~ ListenAddressConfig() {
  188. if (rollbackAddresses_.get() != NULL) {
  189. server_.setListenAddresses(*rollbackAddresses_);
  190. }
  191. }
  192. private:
  193. typedef auto_ptr<AddressList> AddrListPtr;
  194. public:
  195. virtual void build(ConstElementPtr config) {
  196. AddressList newAddresses = parseAddresses(config, "listen_on");
  197. AddrListPtr old(new AddressList(server_.getListenAddresses()));
  198. server_.setListenAddresses(newAddresses);
  199. /*
  200. * Set the rollback addresses only after successful setting of the
  201. * new addresses, so we don't try to rollback if the setup is
  202. * unsuccessful (the above can easily throw).
  203. */
  204. rollbackAddresses_ = old;
  205. }
  206. virtual void commit() {
  207. rollbackAddresses_.release();
  208. }
  209. private:
  210. AuthSrv& server_;
  211. /**
  212. * This is the old address list, if we expect to roll back. When we commit,
  213. * this is set to NULL.
  214. */
  215. AddrListPtr rollbackAddresses_;
  216. };
  217. } // end of unnamed namespace
  218. AuthConfigParser*
  219. createAuthConfigParser(AuthSrv& server, const std::string& config_id) {
  220. // For the initial implementation we use a naive if-else blocks for
  221. // simplicity. In future we'll probably generalize it using map-like
  222. // data structure, and may even provide external register interface so
  223. // that it can be dynamically customized.
  224. if (config_id == "datasources") {
  225. return (new DatasourcesConfig(server));
  226. } else if (config_id == "statistics-interval") {
  227. return (new StatisticsIntervalConfig(server));
  228. } else if (config_id == "listen_on") {
  229. return (new ListenAddressConfig(server));
  230. } else if (config_id == "_commit_throw") {
  231. // This is for testing purpose only and should not appear in the
  232. // actual configuration syntax. While this could crash the caller
  233. // as a result, the server implementation is expected to perform
  234. // syntax level validation and should be safe in practice. In future,
  235. // we may introduce dynamic registration of configuration parsers,
  236. // and then this test can be done in a cleaner and safer way.
  237. return (new ThrowerCommitConfig());
  238. } else if (config_id == "version") {
  239. // Currently, the version identifier is ignored, but it should
  240. // later be used to mark backwards incompatible changes in the
  241. // config data
  242. return (new VersionConfig());
  243. } else {
  244. isc_throw(AuthConfigError, "Unknown configuration identifier: " <<
  245. config_id);
  246. }
  247. }
  248. void
  249. configureAuthServer(AuthSrv& server, ConstElementPtr config_set) {
  250. if (!config_set) {
  251. isc_throw(AuthConfigError,
  252. "Null pointer is passed to configuration parser");
  253. }
  254. typedef boost::shared_ptr<AuthConfigParser> ParserPtr;
  255. vector<ParserPtr> parsers;
  256. typedef pair<string, ConstElementPtr> ConfigPair;
  257. try {
  258. BOOST_FOREACH(ConfigPair config_pair, config_set->mapValue()) {
  259. // We should eventually integrate the sqlite3 DB configuration to
  260. // this framework, but to minimize diff we begin with skipping that
  261. // part.
  262. if (config_pair.first == "database_file") {
  263. continue;
  264. }
  265. ParserPtr parser(createAuthConfigParser(server,
  266. config_pair.first));
  267. parser->build(config_pair.second);
  268. parsers.push_back(parser);
  269. }
  270. } catch (const AuthConfigError& ex) {
  271. throw; // simply rethrowing it
  272. } catch (const isc::Exception& ex) {
  273. isc_throw(AuthConfigError, "Server configuration failed: " <<
  274. ex.what());
  275. }
  276. try {
  277. BOOST_FOREACH(ParserPtr parser, parsers) {
  278. parser->commit();
  279. }
  280. } catch (...) {
  281. throw FatalError("Unrecoverable error: "
  282. "a configuration parser threw in commit");
  283. }
  284. }