json_config_parser.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. // Copyright (C) 2012-2014 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/ccsession.h>
  15. #include <dhcp4/dhcp4_log.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option_definition.h>
  18. #include <dhcpsrv/cfgmgr.h>
  19. #include <dhcp4/json_config_parser.h>
  20. #include <dhcpsrv/dbaccess_parser.h>
  21. #include <dhcpsrv/dhcp_parsers.h>
  22. #include <dhcpsrv/option_space_container.h>
  23. #include <util/encode/hex.h>
  24. #include <util/strutil.h>
  25. #include <boost/foreach.hpp>
  26. #include <boost/lexical_cast.hpp>
  27. #include <boost/algorithm/string.hpp>
  28. #include <limits>
  29. #include <iostream>
  30. #include <vector>
  31. #include <map>
  32. using namespace std;
  33. using namespace isc;
  34. using namespace isc::dhcp;
  35. using namespace isc::data;
  36. using namespace isc::asiolink;
  37. namespace {
  38. /// @brief Parser for DHCP4 option data value.
  39. ///
  40. /// This parser parses configuration entries that specify value of
  41. /// a single option specific to DHCP4. It provides the DHCP4-specific
  42. /// implementation of the abstract class OptionDataParser.
  43. class Dhcp4OptionDataParser : public OptionDataParser {
  44. public:
  45. /// @brief Constructor.
  46. ///
  47. /// @param dummy first param, option names are always "Dhcp4/option-data[n]"
  48. /// @param options is the option storage in which to store the parsed option
  49. /// upon "commit".
  50. /// @param global_context is a pointer to the global context which
  51. /// stores global scope parameters, options, option defintions.
  52. Dhcp4OptionDataParser(const std::string&,
  53. OptionStoragePtr options, ParserContextPtr global_context)
  54. :OptionDataParser("", options, global_context) {
  55. }
  56. /// @brief static factory method for instantiating Dhcp4OptionDataParsers
  57. ///
  58. /// @param param_name name of the parameter to be parsed.
  59. /// @param options storage where the parameter value is to be stored.
  60. /// @param global_context is a pointer to the global context which
  61. /// stores global scope parameters, options, option defintions.
  62. /// @return returns a pointer to a new OptionDataParser. Caller is
  63. /// is responsible for deleting it when it is no longer needed.
  64. static OptionDataParser* factory(const std::string& param_name,
  65. OptionStoragePtr options, ParserContextPtr global_context) {
  66. return (new Dhcp4OptionDataParser(param_name, options, global_context));
  67. }
  68. protected:
  69. /// @brief Finds an option definition within the server's option space
  70. ///
  71. /// Given an option space and an option code, find the correpsonding
  72. /// option defintion within the server's option defintion storage.
  73. ///
  74. /// @param option_space name of the parameter option space
  75. /// @param option_code numeric value of the parameter to find
  76. /// @return OptionDefintionPtr of the option defintion or an
  77. /// empty OptionDefinitionPtr if not found.
  78. /// @throw DhcpConfigError if the option space requested is not valid
  79. /// for this server.
  80. virtual OptionDefinitionPtr findServerSpaceOptionDefinition (
  81. std::string& option_space, uint32_t option_code) {
  82. OptionDefinitionPtr def;
  83. if (option_space == "dhcp4" &&
  84. LibDHCP::isStandardOption(Option::V4, option_code)) {
  85. def = LibDHCP::getOptionDef(Option::V4, option_code);
  86. } else if (option_space == "dhcp6") {
  87. isc_throw(DhcpConfigError, "'dhcp6' option space name is reserved"
  88. << " for DHCPv6 server");
  89. } else {
  90. // Check if this is a vendor-option. If it is, get vendor-specific
  91. // definition.
  92. uint32_t vendor_id = SubnetConfigParser::optionSpaceToVendorId(option_space);
  93. if (vendor_id) {
  94. def = LibDHCP::getVendorOptionDef(Option::V4, vendor_id, option_code);
  95. }
  96. }
  97. return (def);
  98. }
  99. };
  100. /// @brief Parser for IPv4 pool definitions.
  101. ///
  102. /// This is the IPv4 derivation of the PoolParser class and handles pool
  103. /// definitions, i.e. a list of entries of one of two syntaxes: min-max and
  104. /// prefix/len for IPv4 pools. Pool4 objects are created and stored in chosen
  105. /// PoolStorage container.
  106. ///
  107. /// It is useful for parsing Dhcp4/subnet4[X]/pool parameters.
  108. class Pool4Parser : public PoolParser {
  109. public:
  110. /// @brief Constructor.
  111. ///
  112. /// @param param_name name of the parameter. Note, it is passed through
  113. /// but unused, parameter is currently always "Dhcp4/subnet4[X]/pool"
  114. /// @param pools storage container in which to store the parsed pool
  115. /// upon "commit"
  116. Pool4Parser(const std::string& param_name, PoolStoragePtr pools)
  117. :PoolParser(param_name, pools) {
  118. }
  119. protected:
  120. /// @brief Creates a Pool4 object given a IPv4 prefix and the prefix length.
  121. ///
  122. /// @param addr is the IPv4 prefix of the pool.
  123. /// @param len is the prefix length.
  124. /// @param ignored dummy parameter to provide symmetry between the
  125. /// PoolParser derivations. The V6 derivation requires a third value.
  126. /// @return returns a PoolPtr to the new Pool4 object.
  127. PoolPtr poolMaker (IOAddress &addr, uint32_t len, int32_t) {
  128. return (PoolPtr(new Pool4(addr, len)));
  129. }
  130. /// @brief Creates a Pool4 object given starting and ending IPv4 addresses.
  131. ///
  132. /// @param min is the first IPv4 address in the pool.
  133. /// @param max is the last IPv4 address in the pool.
  134. /// @param ignored dummy parameter to provide symmetry between the
  135. /// PoolParser derivations. The V6 derivation requires a third value.
  136. /// @return returns a PoolPtr to the new Pool4 object.
  137. PoolPtr poolMaker (IOAddress &min, IOAddress &max, int32_t) {
  138. return (PoolPtr(new Pool4(min, max)));
  139. }
  140. };
  141. class Pools4ListParser : public PoolsListParser {
  142. public:
  143. Pools4ListParser(const std::string& dummy, PoolStoragePtr pools)
  144. :PoolsListParser(dummy, pools) {
  145. }
  146. protected:
  147. virtual ParserPtr poolParserMaker(PoolStoragePtr storage) {
  148. return (ParserPtr(new Pool4Parser("pool", storage)));
  149. }
  150. };
  151. /// @brief This class parses a single IPv4 subnet.
  152. ///
  153. /// This is the IPv4 derivation of the SubnetConfigParser class and it parses
  154. /// the whole subnet definition. It creates parsersfor received configuration
  155. /// parameters as needed.
  156. class Subnet4ConfigParser : public SubnetConfigParser {
  157. public:
  158. /// @brief Constructor
  159. ///
  160. /// @param ignored first parameter
  161. /// stores global scope parameters, options, option defintions.
  162. Subnet4ConfigParser(const std::string&)
  163. :SubnetConfigParser("", globalContext(), IOAddress("0.0.0.0")) {
  164. }
  165. /// @brief Parses a single IPv4 subnet configuration and adds to the
  166. /// Configuration Manager.
  167. ///
  168. /// @param subnet A new subnet being configured.
  169. void build(ConstElementPtr subnet) {
  170. SubnetConfigParser::build(subnet);
  171. if (subnet_) {
  172. Subnet4Ptr sub4ptr = boost::dynamic_pointer_cast<Subnet4>(subnet_);
  173. if (!sub4ptr) {
  174. // If we hit this, it is a programming error.
  175. isc_throw(Unexpected,
  176. "Invalid cast in Subnet4ConfigParser::commit");
  177. }
  178. // Set relay information if it was parsed
  179. if (relay_info_) {
  180. sub4ptr->setRelayInfo(*relay_info_);
  181. }
  182. // Adding a subnet to the Configuration Manager may fail if the
  183. // subnet id is invalid (duplicate). Thus, we catch exceptions
  184. // here to append a position in the configuration string.
  185. try {
  186. isc::dhcp::CfgMgr::instance().addSubnet4(sub4ptr);
  187. } catch (const std::exception& ex) {
  188. isc_throw(DhcpConfigError, ex.what() << " ("
  189. << subnet->getPosition() << ")");
  190. }
  191. }
  192. }
  193. /// @brief Commits subnet configuration.
  194. ///
  195. /// This function is currently no-op because subnet should already
  196. /// be added into the Config Manager in the build().
  197. void commit() { }
  198. protected:
  199. /// @brief Creates parsers for entries in subnet definition.
  200. ///
  201. /// @param config_id name of the entry
  202. ///
  203. /// @return parser object for specified entry name. Note the caller is
  204. /// responsible for deleting the parser created.
  205. /// @throw isc::dhcp::DhcpConfigError if trying to create a parser
  206. /// for unknown config element
  207. DhcpConfigParser* createSubnetConfigParser(const std::string& config_id) {
  208. DhcpConfigParser* parser = NULL;
  209. if ((config_id.compare("valid-lifetime") == 0) ||
  210. (config_id.compare("renew-timer") == 0) ||
  211. (config_id.compare("rebind-timer") == 0) ||
  212. (config_id.compare("id") == 0)) {
  213. parser = new Uint32Parser(config_id, uint32_values_);
  214. } else if ((config_id.compare("subnet") == 0) ||
  215. (config_id.compare("interface") == 0) ||
  216. (config_id.compare("client-class") == 0) ||
  217. (config_id.compare("next-server") == 0)) {
  218. parser = new StringParser(config_id, string_values_);
  219. } else if (config_id.compare("pools") == 0) {
  220. parser = new Pools4ListParser(config_id, pools_);
  221. } else if (config_id.compare("relay") == 0) {
  222. parser = new RelayInfoParser(config_id, relay_info_, Option::V4);
  223. } else if (config_id.compare("option-data") == 0) {
  224. parser = new OptionDataListParser(config_id, options_,
  225. global_context_,
  226. Dhcp4OptionDataParser::factory);
  227. } else {
  228. isc_throw(NotImplemented, "unsupported parameter: " << config_id);
  229. }
  230. return (parser);
  231. }
  232. /// @brief Determines if the given option space name and code describe
  233. /// a standard option for the DHCP4 server.
  234. ///
  235. /// @param option_space is the name of the option space to consider
  236. /// @param code is the numeric option code to consider
  237. /// @return returns true if the space and code are part of the server's
  238. /// standard options.
  239. bool isServerStdOption(std::string option_space, uint32_t code) {
  240. return ((option_space.compare("dhcp4") == 0)
  241. && LibDHCP::isStandardOption(Option::V4, code));
  242. }
  243. /// @brief Returns the option definition for a given option code from
  244. /// the DHCP4 server's standard set of options.
  245. /// @param code is the numeric option code of the desired option definition.
  246. /// @return returns a pointer the option definition
  247. OptionDefinitionPtr getServerStdOptionDefinition (uint32_t code) {
  248. return (LibDHCP::getOptionDef(Option::V4, code));
  249. }
  250. /// @brief Issues a DHCP4 server specific warning regarding duplicate subnet
  251. /// options.
  252. ///
  253. /// @param code is the numeric option code of the duplicate option
  254. /// @param addr is the subnet address
  255. /// @todo a means to know the correct logger and perhaps a common
  256. /// message would allow this method to be emitted by the base class.
  257. virtual void duplicate_option_warning(uint32_t code,
  258. isc::asiolink::IOAddress& addr) {
  259. LOG_WARN(dhcp4_logger, DHCP4_CONFIG_OPTION_DUPLICATE)
  260. .arg(code).arg(addr.toText());
  261. }
  262. /// @brief Instantiates the IPv4 Subnet based on a given IPv4 address
  263. /// and prefix length.
  264. ///
  265. /// @param addr is IPv4 address of the subnet.
  266. /// @param len is the prefix length
  267. void initSubnet(isc::asiolink::IOAddress addr, uint8_t len) {
  268. // The renew-timer and rebind-timer are optional. If not set, the
  269. // option 58 and 59 will not be sent to a client. In this case the
  270. // client will use default values based on the valid-lifetime.
  271. Triplet<uint32_t> t1 = getOptionalParam("renew-timer");
  272. Triplet<uint32_t> t2 = getOptionalParam("rebind-timer");
  273. // The valid-lifetime is mandatory. It may be specified for a
  274. // particular subnet. If not, the global value should be present.
  275. // If there is no global value, exception is thrown.
  276. Triplet<uint32_t> valid = getParam("valid-lifetime");
  277. // Subnet ID is optional. If it is not supplied the value of 0 is used,
  278. // which means autogenerate.
  279. SubnetID subnet_id =
  280. static_cast<SubnetID>(uint32_values_->getOptionalParam("id", 0));
  281. stringstream s;
  282. s << addr << "/" << static_cast<int>(len) << " with params: ";
  283. // t1 and t2 are optional may be not specified.
  284. if (!t1.unspecified()) {
  285. s << "t1=" << t1 << ", ";
  286. }
  287. if (!t2.unspecified()) {
  288. s << "t2=" << t2 << ", ";
  289. }
  290. s <<"valid-lifetime=" << valid;
  291. LOG_INFO(dhcp4_logger, DHCP4_CONFIG_NEW_SUBNET).arg(s.str());
  292. Subnet4Ptr subnet4(new Subnet4(addr, len, t1, t2, valid, subnet_id));
  293. subnet_ = subnet4;
  294. // Try global value first
  295. try {
  296. string next_server = globalContext()->string_values_->getParam("next-server");
  297. if (!next_server.empty()) {
  298. subnet4->setSiaddr(IOAddress(next_server));
  299. }
  300. } catch (const DhcpConfigError&) {
  301. // Don't care. next_server is optional. We can live without it
  302. } catch (...) {
  303. isc_throw(DhcpConfigError, "invalid parameter next-server ("
  304. << globalContext()->string_values_->getPosition("next-server")
  305. << ")");
  306. }
  307. // Try subnet specific value if it's available
  308. try {
  309. string next_server = string_values_->getParam("next-server");
  310. if (!next_server.empty()) {
  311. subnet4->setSiaddr(IOAddress(next_server));
  312. }
  313. } catch (const DhcpConfigError&) {
  314. // Don't care. next_server is optional. We can live without it
  315. } catch (...) {
  316. isc_throw(DhcpConfigError, "invalid parameter next-server ("
  317. << string_values_->getPosition("next-server")
  318. << ")");
  319. }
  320. // Try setting up client class (if specified)
  321. try {
  322. string client_class = string_values_->getParam("client-class");
  323. subnet4->allowClientClass(client_class);
  324. } catch (const DhcpConfigError&) {
  325. // That's ok if it fails. client-class is optional.
  326. }
  327. }
  328. };
  329. /// @brief this class parses list of DHCP4 subnets
  330. ///
  331. /// This is a wrapper parser that handles the whole list of Subnet4
  332. /// definitions. It iterates over all entries and creates Subnet4ConfigParser
  333. /// for each entry.
  334. class Subnets4ListConfigParser : public DhcpConfigParser {
  335. public:
  336. /// @brief constructor
  337. ///
  338. /// @param dummy first argument, always ignored. All parsers accept a
  339. /// string parameter "name" as their first argument.
  340. Subnets4ListConfigParser(const std::string&) {
  341. }
  342. /// @brief parses contents of the list
  343. ///
  344. /// Iterates over all entries on the list and creates Subnet4ConfigParser
  345. /// for each entry.
  346. ///
  347. /// @param subnets_list pointer to a list of IPv4 subnets
  348. void build(ConstElementPtr subnets_list) {
  349. // @todo: Implement more subtle reconfiguration than toss
  350. // the old one and replace with the new one.
  351. // remove old subnets
  352. CfgMgr::instance().deleteSubnets4();
  353. BOOST_FOREACH(ConstElementPtr subnet, subnets_list->listValue()) {
  354. ParserPtr parser(new Subnet4ConfigParser("subnet"));
  355. parser->build(subnet);
  356. subnets_.push_back(parser);
  357. }
  358. }
  359. /// @brief commits subnets definitions.
  360. ///
  361. /// Iterates over all Subnet4 parsers. Each parser contains definitions of
  362. /// a single subnet and its parameters and commits each subnet separately.
  363. void commit() {
  364. BOOST_FOREACH(ParserPtr subnet, subnets_) {
  365. subnet->commit();
  366. }
  367. }
  368. /// @brief Returns Subnet4ListConfigParser object
  369. /// @param param_name name of the parameter
  370. /// @return Subnets4ListConfigParser object
  371. static DhcpConfigParser* factory(const std::string& param_name) {
  372. return (new Subnets4ListConfigParser(param_name));
  373. }
  374. /// @brief collection of subnet parsers.
  375. ParserCollection subnets_;
  376. };
  377. } // anonymous namespace
  378. namespace isc {
  379. namespace dhcp {
  380. /// @brief creates global parsers
  381. ///
  382. /// This method creates global parsers that parse global parameters, i.e.
  383. /// those that take format of Dhcp4/param1, Dhcp4/param2 and so forth.
  384. ///
  385. /// @param config_id pointer to received global configuration entry
  386. /// @return parser for specified global DHCPv4 parameter
  387. /// @throw NotImplemented if trying to create a parser for unknown
  388. /// config element
  389. DhcpConfigParser* createGlobalDhcp4ConfigParser(const std::string& config_id,
  390. ConstElementPtr element) {
  391. DhcpConfigParser* parser = NULL;
  392. if ((config_id.compare("valid-lifetime") == 0) ||
  393. (config_id.compare("renew-timer") == 0) ||
  394. (config_id.compare("rebind-timer") == 0)) {
  395. parser = new Uint32Parser(config_id,
  396. globalContext()->uint32_values_);
  397. } else if (config_id.compare("interfaces") == 0) {
  398. parser = new InterfaceListConfigParser(config_id);
  399. } else if (config_id.compare("subnet4") == 0) {
  400. parser = new Subnets4ListConfigParser(config_id);
  401. } else if (config_id.compare("option-data") == 0) {
  402. parser = new OptionDataListParser(config_id,
  403. globalContext()->options_,
  404. globalContext(),
  405. Dhcp4OptionDataParser::factory);
  406. } else if (config_id.compare("option-def") == 0) {
  407. parser = new OptionDefListParser(config_id, globalContext());
  408. } else if ((config_id.compare("version") == 0) ||
  409. (config_id.compare("next-server") == 0)) {
  410. parser = new StringParser(config_id,
  411. globalContext()->string_values_);
  412. } else if (config_id.compare("lease-database") == 0) {
  413. parser = new DbAccessParser(config_id, *globalContext());
  414. } else if (config_id.compare("hooks-libraries") == 0) {
  415. parser = new HooksLibrariesParser(config_id);
  416. } else if (config_id.compare("echo-client-id") == 0) {
  417. parser = new BooleanParser(config_id, globalContext()->boolean_values_);
  418. } else if (config_id.compare("dhcp-ddns") == 0) {
  419. parser = new D2ClientConfigParser(config_id);
  420. } else {
  421. isc_throw(DhcpConfigError,
  422. "unsupported global configuration parameter: "
  423. << config_id << " (" << element->getPosition() << ")");
  424. }
  425. return (parser);
  426. }
  427. void commitGlobalOptions() {
  428. // Although the function is modest for now, it is certain that the number
  429. // of global switches will increase over time, hence the name.
  430. // Set whether v4 server is supposed to echo back client-id (yes = RFC6842
  431. // compatible, no = backward compatibility)
  432. try {
  433. bool echo_client_id = globalContext()->boolean_values_->getParam("echo-client-id");
  434. CfgMgr::instance().echoClientId(echo_client_id);
  435. } catch (...) {
  436. // Ignore errors. This flag is optional
  437. }
  438. }
  439. isc::data::ConstElementPtr
  440. configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set) {
  441. if (!config_set) {
  442. ConstElementPtr answer = isc::config::createAnswer(1,
  443. string("Can't parse NULL config"));
  444. return (answer);
  445. }
  446. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND,
  447. DHCP4_CONFIG_START).arg(config_set->str());
  448. // Before starting any subnet operations, let's reset the subnet-id counter,
  449. // so newly recreated configuration starts with first subnet-id equal 1.
  450. Subnet::resetSubnetID();
  451. // Some of the values specified in the configuration depend on
  452. // other values. Typically, the values in the subnet4 structure
  453. // depend on the global values. Also, option values configuration
  454. // must be performed after the option definitions configurations.
  455. // Thus we group parsers and will fire them in the right order:
  456. // all parsers other than subnet4 and option-data parser,
  457. // option-data parser, subnet4 parser.
  458. ParserCollection independent_parsers;
  459. ParserPtr subnet_parser;
  460. ParserPtr option_parser;
  461. ParserPtr iface_parser;
  462. // Some of the parsers alter the state of the system in a way that can't
  463. // easily be undone. (Or alter it in a way such that undoing the change has
  464. // the same risk of failure as doing the change.)
  465. ParserPtr hooks_parser;
  466. // The subnet parsers implement data inheritance by directly
  467. // accessing global storage. For this reason the global data
  468. // parsers must store the parsed data into global storages
  469. // immediately. This may cause data inconsistency if the
  470. // parsing operation fails after the global storage has been
  471. // modified. We need to preserve the original global data here
  472. // so as we can rollback changes when an error occurs.
  473. ParserContext original_context(*globalContext());
  474. // Answer will hold the result.
  475. ConstElementPtr answer;
  476. // Rollback informs whether error occured and original data
  477. // have to be restored to global storages.
  478. bool rollback = false;
  479. // config_pair holds the details of the current parser when iterating over
  480. // the parsers. It is declared outside the loops so in case of an error,
  481. // the name of the failing parser can be retrieved in the "catch" clause.
  482. ConfigPair config_pair;
  483. try {
  484. // Make parsers grouping.
  485. const std::map<std::string, ConstElementPtr>& values_map =
  486. config_set->mapValue();
  487. BOOST_FOREACH(config_pair, values_map) {
  488. ParserPtr parser(createGlobalDhcp4ConfigParser(config_pair.first,
  489. config_pair.second));
  490. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PARSER_CREATED)
  491. .arg(config_pair.first);
  492. if (config_pair.first == "subnet4") {
  493. subnet_parser = parser;
  494. } else if (config_pair.first == "option-data") {
  495. option_parser = parser;
  496. } else if (config_pair.first == "interfaces") {
  497. // The interface parser is independent from any other
  498. // parser and can be run here before any other parsers.
  499. iface_parser = parser;
  500. parser->build(config_pair.second);
  501. } else if (config_pair.first == "hooks-libraries") {
  502. // Executing commit will alter currently-loaded hooks
  503. // libraries. Check if the supplied libraries are valid,
  504. // but defer the commit until everything else has committed.
  505. hooks_parser = parser;
  506. parser->build(config_pair.second);
  507. } else {
  508. // Those parsers should be started before other
  509. // parsers so we can call build straight away.
  510. independent_parsers.push_back(parser);
  511. parser->build(config_pair.second);
  512. // The commit operation here may modify the global storage
  513. // but we need it so as the subnet6 parser can access the
  514. // parsed data.
  515. parser->commit();
  516. }
  517. }
  518. // The option values parser is the next one to be run.
  519. std::map<std::string, ConstElementPtr>::const_iterator option_config =
  520. values_map.find("option-data");
  521. if (option_config != values_map.end()) {
  522. config_pair.first = "option-data";
  523. option_parser->build(option_config->second);
  524. option_parser->commit();
  525. }
  526. // The subnet parser is the last one to be run.
  527. std::map<std::string, ConstElementPtr>::const_iterator subnet_config =
  528. values_map.find("subnet4");
  529. if (subnet_config != values_map.end()) {
  530. config_pair.first = "subnet4";
  531. subnet_parser->build(subnet_config->second);
  532. }
  533. } catch (const isc::Exception& ex) {
  534. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_FAIL)
  535. .arg(config_pair.first).arg(ex.what());
  536. answer = isc::config::createAnswer(1,
  537. string("Configuration parsing failed: ") + ex.what());
  538. // An error occured, so make sure that we restore original data.
  539. rollback = true;
  540. } catch (...) {
  541. // For things like bad_cast in boost::lexical_cast
  542. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_EXCEPTION).arg(config_pair.first);
  543. answer = isc::config::createAnswer(1,
  544. string("Configuration parsing failed"));
  545. // An error occured, so make sure that we restore original data.
  546. rollback = true;
  547. }
  548. // So far so good, there was no parsing error so let's commit the
  549. // configuration. This will add created subnets and option values into
  550. // the server's configuration.
  551. // This operation should be exception safe but let's make sure.
  552. if (!rollback) {
  553. try {
  554. if (subnet_parser) {
  555. subnet_parser->commit();
  556. }
  557. if (iface_parser) {
  558. iface_parser->commit();
  559. }
  560. // Apply global options
  561. commitGlobalOptions();
  562. // This occurs last as if it succeeds, there is no easy way
  563. // revert it. As a result, the failure to commit a subsequent
  564. // change causes problems when trying to roll back.
  565. if (hooks_parser) {
  566. hooks_parser->commit();
  567. }
  568. }
  569. catch (const isc::Exception& ex) {
  570. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_FAIL).arg(ex.what());
  571. answer = isc::config::createAnswer(2,
  572. string("Configuration commit failed: ") + ex.what());
  573. rollback = true;
  574. } catch (...) {
  575. // For things like bad_cast in boost::lexical_cast
  576. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_EXCEPTION);
  577. answer = isc::config::createAnswer(2,
  578. string("Configuration commit failed"));
  579. rollback = true;
  580. }
  581. }
  582. // Rollback changes as the configuration parsing failed.
  583. if (rollback) {
  584. globalContext().reset(new ParserContext(original_context));
  585. return (answer);
  586. }
  587. LOG_INFO(dhcp4_logger, DHCP4_CONFIG_COMPLETE)
  588. .arg(CfgMgr::instance().getConfiguration()->
  589. getConfigSummary(Configuration::CFGSEL_ALL4));
  590. // Everything was fine. Configuration is successful.
  591. answer = isc::config::createAnswer(0, "Configuration committed.");
  592. return (answer);
  593. }
  594. ParserContextPtr& globalContext() {
  595. static ParserContextPtr global_context_ptr(new ParserContext(Option::V4));
  596. return (global_context_ptr);
  597. }
  598. }; // end of isc::dhcp namespace
  599. }; // end of isc namespace