json_config_parser.cc 27 KB

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