config_parser.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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/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. /// @brief This class parses a single IPv4 subnet.
  142. ///
  143. /// This is the IPv4 derivation of the SubnetConfigParser class and it parses
  144. /// the whole subnet definition. It creates parsersfor received configuration
  145. /// parameters as needed.
  146. class Subnet4ConfigParser : public SubnetConfigParser {
  147. public:
  148. /// @brief Constructor
  149. ///
  150. /// @param ignored first parameter
  151. /// stores global scope parameters, options, option defintions.
  152. Subnet4ConfigParser(const std::string&)
  153. :SubnetConfigParser("", globalContext(), IOAddress("0.0.0.0")) {
  154. }
  155. /// @brief Adds the created subnet to a server's configuration.
  156. /// @throw throws Unexpected if dynamic cast fails.
  157. void commit() {
  158. if (subnet_) {
  159. Subnet4Ptr sub4ptr = boost::dynamic_pointer_cast<Subnet4>(subnet_);
  160. if (!sub4ptr) {
  161. // If we hit this, it is a programming error.
  162. isc_throw(Unexpected,
  163. "Invalid cast in Subnet4ConfigParser::commit");
  164. }
  165. // Set relay information if it was parsed
  166. if (relay_info_) {
  167. sub4ptr->setRelayInfo(*relay_info_);
  168. }
  169. isc::dhcp::CfgMgr::instance().addSubnet4(sub4ptr);
  170. }
  171. }
  172. protected:
  173. /// @brief Creates parsers for entries in subnet definition.
  174. ///
  175. /// @param config_id name of the entry
  176. ///
  177. /// @return parser object for specified entry name. Note the caller is
  178. /// responsible for deleting the parser created.
  179. /// @throw isc::dhcp::DhcpConfigError if trying to create a parser
  180. /// for unknown config element
  181. DhcpConfigParser* createSubnetConfigParser(const std::string& config_id) {
  182. DhcpConfigParser* parser = NULL;
  183. if ((config_id.compare("valid-lifetime") == 0) ||
  184. (config_id.compare("renew-timer") == 0) ||
  185. (config_id.compare("rebind-timer") == 0) ||
  186. (config_id.compare("id") == 0)) {
  187. parser = new Uint32Parser(config_id, uint32_values_);
  188. } else if ((config_id.compare("subnet") == 0) ||
  189. (config_id.compare("interface") == 0) ||
  190. (config_id.compare("client-class") == 0) ||
  191. (config_id.compare("next-server") == 0)) {
  192. parser = new StringParser(config_id, string_values_);
  193. } else if (config_id.compare("pool") == 0) {
  194. parser = new Pool4Parser(config_id, pools_);
  195. } else if (config_id.compare("relay") == 0) {
  196. parser = new RelayInfoParser(config_id, relay_info_, Option::V4);
  197. } else if (config_id.compare("option-data") == 0) {
  198. parser = new OptionDataListParser(config_id, options_,
  199. global_context_,
  200. Dhcp4OptionDataParser::factory);
  201. } else {
  202. isc_throw(NotImplemented,
  203. "parser error: Subnet4 parameter not supported: " << config_id);
  204. }
  205. return (parser);
  206. }
  207. /// @brief Determines if the given option space name and code describe
  208. /// a standard option for the DCHP4 server.
  209. ///
  210. /// @param option_space is the name of the option space to consider
  211. /// @param code is the numeric option code to consider
  212. /// @return returns true if the space and code are part of the server's
  213. /// standard options.
  214. bool isServerStdOption(std::string option_space, uint32_t code) {
  215. return ((option_space.compare("dhcp4") == 0)
  216. && LibDHCP::isStandardOption(Option::V4, code));
  217. }
  218. /// @brief Returns the option definition for a given option code from
  219. /// the DHCP4 server's standard set of options.
  220. /// @param code is the numeric option code of the desired option definition.
  221. /// @return returns a pointer the option definition
  222. OptionDefinitionPtr getServerStdOptionDefinition (uint32_t code) {
  223. return (LibDHCP::getOptionDef(Option::V4, code));
  224. }
  225. /// @brief Issues a DHCP4 server specific warning regarding duplicate subnet
  226. /// options.
  227. ///
  228. /// @param code is the numeric option code of the duplicate option
  229. /// @param addr is the subnet address
  230. /// @todo a means to know the correct logger and perhaps a common
  231. /// message would allow this method to be emitted by the base class.
  232. virtual void duplicate_option_warning(uint32_t code,
  233. isc::asiolink::IOAddress& addr) {
  234. LOG_WARN(dhcp4_logger, DHCP4_CONFIG_OPTION_DUPLICATE)
  235. .arg(code).arg(addr.toText());
  236. }
  237. /// @brief Instantiates the IPv4 Subnet based on a given IPv4 address
  238. /// and prefix length.
  239. ///
  240. /// @param addr is IPv4 address of the subnet.
  241. /// @param len is the prefix length
  242. void initSubnet(isc::asiolink::IOAddress addr, uint8_t len) {
  243. // Get all 'time' parameters using inheritance.
  244. // If the subnet-specific value is defined then use it, else
  245. // use the global value. The global value must always be
  246. // present. If it is not, it is an internal error and exception
  247. // is thrown.
  248. Triplet<uint32_t> t1 = getParam("renew-timer");
  249. Triplet<uint32_t> t2 = getParam("rebind-timer");
  250. Triplet<uint32_t> valid = getParam("valid-lifetime");
  251. // Subnet ID is optional. If it is not supplied the value of 0 is used,
  252. // which means autogenerate.
  253. SubnetID subnet_id =
  254. static_cast<SubnetID>(uint32_values_->getOptionalParam("id", 0));
  255. stringstream tmp;
  256. tmp << addr << "/" << (int)len
  257. << " with params t1=" << t1 << ", t2=" << t2 << ", valid=" << valid;
  258. LOG_INFO(dhcp4_logger, DHCP4_CONFIG_NEW_SUBNET).arg(tmp.str());
  259. Subnet4Ptr subnet4(new Subnet4(addr, len, t1, t2, valid, subnet_id));
  260. subnet_ = subnet4;
  261. // Try global value first
  262. try {
  263. string next_server = globalContext()->string_values_->getParam("next-server");
  264. if (!next_server.empty()) {
  265. subnet4->setSiaddr(IOAddress(next_server));
  266. }
  267. } catch (const DhcpConfigError&) {
  268. // Don't care. next_server is optional. We can live without it
  269. }
  270. // Try subnet specific value if it's available
  271. try {
  272. string next_server = string_values_->getParam("next-server");
  273. if (!next_server.empty()) {
  274. subnet4->setSiaddr(IOAddress(next_server));
  275. }
  276. } catch (const DhcpConfigError&) {
  277. // Don't care. next_server is optional. We can live without it
  278. }
  279. // Try setting up client class (if specified)
  280. try {
  281. string client_class = string_values_->getParam("client-class");
  282. subnet4->allowClientClass(client_class);
  283. } catch (const DhcpConfigError&) {
  284. // That's ok if it fails. client-class is optional.
  285. }
  286. }
  287. };
  288. /// @brief this class parses list of DHCP4 subnets
  289. ///
  290. /// This is a wrapper parser that handles the whole list of Subnet4
  291. /// definitions. It iterates over all entries and creates Subnet4ConfigParser
  292. /// for each entry.
  293. class Subnets4ListConfigParser : public DhcpConfigParser {
  294. public:
  295. /// @brief constructor
  296. ///
  297. /// @param dummy first argument, always ignored. All parsers accept a
  298. /// string parameter "name" as their first argument.
  299. Subnets4ListConfigParser(const std::string&) {
  300. }
  301. /// @brief parses contents of the list
  302. ///
  303. /// Iterates over all entries on the list and creates Subnet4ConfigParser
  304. /// for each entry.
  305. ///
  306. /// @param subnets_list pointer to a list of IPv4 subnets
  307. void build(ConstElementPtr subnets_list) {
  308. BOOST_FOREACH(ConstElementPtr subnet, subnets_list->listValue()) {
  309. ParserPtr parser(new Subnet4ConfigParser("subnet"));
  310. parser->build(subnet);
  311. subnets_.push_back(parser);
  312. }
  313. }
  314. /// @brief commits subnets definitions.
  315. ///
  316. /// Iterates over all Subnet4 parsers. Each parser contains definitions of
  317. /// a single subnet and its parameters and commits each subnet separately.
  318. void commit() {
  319. // @todo: Implement more subtle reconfiguration than toss
  320. // the old one and replace with the new one.
  321. // remove old subnets
  322. CfgMgr::instance().deleteSubnets4();
  323. BOOST_FOREACH(ParserPtr subnet, subnets_) {
  324. subnet->commit();
  325. }
  326. }
  327. /// @brief Returns Subnet4ListConfigParser object
  328. /// @param param_name name of the parameter
  329. /// @return Subnets4ListConfigParser object
  330. static DhcpConfigParser* factory(const std::string& param_name) {
  331. return (new Subnets4ListConfigParser(param_name));
  332. }
  333. /// @brief collection of subnet parsers.
  334. ParserCollection subnets_;
  335. };
  336. } // anonymous namespace
  337. namespace isc {
  338. namespace dhcp {
  339. /// @brief creates global parsers
  340. ///
  341. /// This method creates global parsers that parse global parameters, i.e.
  342. /// those that take format of Dhcp4/param1, Dhcp4/param2 and so forth.
  343. ///
  344. /// @param config_id pointer to received global configuration entry
  345. /// @return parser for specified global DHCPv4 parameter
  346. /// @throw NotImplemented if trying to create a parser for unknown
  347. /// config element
  348. DhcpConfigParser* createGlobalDhcp4ConfigParser(const std::string& config_id) {
  349. DhcpConfigParser* parser = NULL;
  350. if ((config_id.compare("valid-lifetime") == 0) ||
  351. (config_id.compare("renew-timer") == 0) ||
  352. (config_id.compare("rebind-timer") == 0)) {
  353. parser = new Uint32Parser(config_id,
  354. globalContext()->uint32_values_);
  355. } else if (config_id.compare("interfaces") == 0) {
  356. parser = new InterfaceListConfigParser(config_id);
  357. } else if (config_id.compare("subnet4") == 0) {
  358. parser = new Subnets4ListConfigParser(config_id);
  359. } else if (config_id.compare("option-data") == 0) {
  360. parser = new OptionDataListParser(config_id,
  361. globalContext()->options_,
  362. globalContext(),
  363. Dhcp4OptionDataParser::factory);
  364. } else if (config_id.compare("option-def") == 0) {
  365. parser = new OptionDefListParser(config_id,
  366. globalContext()->option_defs_);
  367. } else if ((config_id.compare("version") == 0) ||
  368. (config_id.compare("next-server") == 0)) {
  369. parser = new StringParser(config_id,
  370. globalContext()->string_values_);
  371. } else if (config_id.compare("lease-database") == 0) {
  372. parser = new DbAccessParser(config_id, *globalContext());
  373. } else if (config_id.compare("hooks-libraries") == 0) {
  374. parser = new HooksLibrariesParser(config_id);
  375. } else if (config_id.compare("echo-client-id") == 0) {
  376. parser = new BooleanParser(config_id, globalContext()->boolean_values_);
  377. } else if (config_id.compare("dhcp-ddns") == 0) {
  378. parser = new D2ClientConfigParser(config_id);
  379. } else {
  380. isc_throw(NotImplemented,
  381. "Parser error: Global configuration parameter not supported: "
  382. << config_id);
  383. }
  384. return (parser);
  385. }
  386. void commitGlobalOptions() {
  387. // Although the function is modest for now, it is certain that the number
  388. // of global switches will increase over time, hence the name.
  389. // Set whether v4 server is supposed to echo back client-id (yes = RFC6842
  390. // compatible, no = backward compatibility)
  391. try {
  392. bool echo_client_id = globalContext()->boolean_values_->getParam("echo-client-id");
  393. CfgMgr::instance().echoClientId(echo_client_id);
  394. } catch (...) {
  395. // Ignore errors. This flag is optional
  396. }
  397. }
  398. isc::data::ConstElementPtr
  399. configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set) {
  400. if (!config_set) {
  401. ConstElementPtr answer = isc::config::createAnswer(1,
  402. string("Can't parse NULL config"));
  403. return (answer);
  404. }
  405. /// @todo: Append most essential info here (like "2 new subnets configured")
  406. string config_details;
  407. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND,
  408. DHCP4_CONFIG_START).arg(config_set->str());
  409. // Before starting any subnet operations, let's reset the subnet-id counter,
  410. // so newly recreated configuration starts with first subnet-id equal 1.
  411. Subnet::resetSubnetID();
  412. // Some of the values specified in the configuration depend on
  413. // other values. Typically, the values in the subnet4 structure
  414. // depend on the global values. Also, option values configuration
  415. // must be performed after the option definitions configurations.
  416. // Thus we group parsers and will fire them in the right order:
  417. // all parsers other than subnet4 and option-data parser,
  418. // option-data parser, subnet4 parser.
  419. ParserCollection independent_parsers;
  420. ParserPtr subnet_parser;
  421. ParserPtr option_parser;
  422. ParserPtr iface_parser;
  423. // Some of the parsers alter the state of the system in a way that can't
  424. // easily be undone. (Or alter it in a way such that undoing the change has
  425. // the same risk of failure as doing the change.)
  426. ParserPtr hooks_parser;
  427. // The subnet parsers implement data inheritance by directly
  428. // accessing global storage. For this reason the global data
  429. // parsers must store the parsed data into global storages
  430. // immediately. This may cause data inconsistency if the
  431. // parsing operation fails after the global storage has been
  432. // modified. We need to preserve the original global data here
  433. // so as we can rollback changes when an error occurs.
  434. ParserContext original_context(*globalContext());
  435. // Answer will hold the result.
  436. ConstElementPtr answer;
  437. // Rollback informs whether error occured and original data
  438. // have to be restored to global storages.
  439. bool rollback = false;
  440. // config_pair holds the details of the current parser when iterating over
  441. // the parsers. It is declared outside the loops so in case of an error,
  442. // the name of the failing parser can be retrieved in the "catch" clause.
  443. ConfigPair config_pair;
  444. try {
  445. // Make parsers grouping.
  446. const std::map<std::string, ConstElementPtr>& values_map =
  447. config_set->mapValue();
  448. BOOST_FOREACH(config_pair, values_map) {
  449. ParserPtr parser(createGlobalDhcp4ConfigParser(config_pair.first));
  450. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PARSER_CREATED)
  451. .arg(config_pair.first);
  452. if (config_pair.first == "subnet4") {
  453. subnet_parser = parser;
  454. } else if (config_pair.first == "option-data") {
  455. option_parser = parser;
  456. } else if (config_pair.first == "interfaces") {
  457. // The interface parser is independent from any other
  458. // parser and can be run here before any other parsers.
  459. iface_parser = parser;
  460. parser->build(config_pair.second);
  461. } else if (config_pair.first == "hooks-libraries") {
  462. // Executing commit will alter currently-loaded hooks
  463. // libraries. Check if the supplied libraries are valid,
  464. // but defer the commit until everything else has committed.
  465. hooks_parser = parser;
  466. parser->build(config_pair.second);
  467. } else {
  468. // Those parsers should be started before other
  469. // parsers so we can call build straight away.
  470. independent_parsers.push_back(parser);
  471. parser->build(config_pair.second);
  472. // The commit operation here may modify the global storage
  473. // but we need it so as the subnet6 parser can access the
  474. // parsed data.
  475. parser->commit();
  476. }
  477. }
  478. // The option values parser is the next one to be run.
  479. std::map<std::string, ConstElementPtr>::const_iterator option_config =
  480. values_map.find("option-data");
  481. if (option_config != values_map.end()) {
  482. option_parser->build(option_config->second);
  483. option_parser->commit();
  484. }
  485. // The subnet parser is the last one to be run.
  486. std::map<std::string, ConstElementPtr>::const_iterator subnet_config =
  487. values_map.find("subnet4");
  488. if (subnet_config != values_map.end()) {
  489. subnet_parser->build(subnet_config->second);
  490. }
  491. } catch (const isc::Exception& ex) {
  492. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_FAIL)
  493. .arg(config_pair.first).arg(ex.what());
  494. answer = isc::config::createAnswer(1,
  495. string("Configuration parsing failed: ") + ex.what());
  496. // An error occured, so make sure that we restore original data.
  497. rollback = true;
  498. } catch (...) {
  499. // For things like bad_cast in boost::lexical_cast
  500. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_EXCEPTION).arg(config_pair.first);
  501. answer = isc::config::createAnswer(1,
  502. string("Configuration parsing failed"));
  503. // An error occured, so make sure that we restore original data.
  504. rollback = true;
  505. }
  506. // So far so good, there was no parsing error so let's commit the
  507. // configuration. This will add created subnets and option values into
  508. // the server's configuration.
  509. // This operation should be exception safe but let's make sure.
  510. if (!rollback) {
  511. try {
  512. if (subnet_parser) {
  513. subnet_parser->commit();
  514. }
  515. if (iface_parser) {
  516. iface_parser->commit();
  517. }
  518. // Apply global options
  519. commitGlobalOptions();
  520. // This occurs last as if it succeeds, there is no easy way
  521. // revert it. As a result, the failure to commit a subsequent
  522. // change causes problems when trying to roll back.
  523. if (hooks_parser) {
  524. hooks_parser->commit();
  525. }
  526. }
  527. catch (const isc::Exception& ex) {
  528. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_FAIL).arg(ex.what());
  529. answer = isc::config::createAnswer(2,
  530. string("Configuration commit failed: ") + ex.what());
  531. rollback = true;
  532. } catch (...) {
  533. // For things like bad_cast in boost::lexical_cast
  534. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_EXCEPTION);
  535. answer = isc::config::createAnswer(2,
  536. string("Configuration commit failed"));
  537. rollback = true;
  538. }
  539. }
  540. // Rollback changes as the configuration parsing failed.
  541. if (rollback) {
  542. globalContext().reset(new ParserContext(original_context));
  543. return (answer);
  544. }
  545. LOG_INFO(dhcp4_logger, DHCP4_CONFIG_COMPLETE).arg(config_details);
  546. // Everything was fine. Configuration is successful.
  547. answer = isc::config::createAnswer(0, "Configuration committed.");
  548. return (answer);
  549. }
  550. ParserContextPtr& globalContext() {
  551. static ParserContextPtr global_context_ptr(new ParserContext(Option::V4));
  552. return (global_context_ptr);
  553. }
  554. }; // end of isc::dhcp namespace
  555. }; // end of isc namespace