json_config_parser.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <cc/command_interpreter.h>
  8. #include <dhcp4/dhcp4_log.h>
  9. #include <dhcp4/simple_parser4.h>
  10. #include <dhcp/libdhcp++.h>
  11. #include <dhcp/option_definition.h>
  12. #include <dhcpsrv/cfg_option.h>
  13. #include <dhcpsrv/cfgmgr.h>
  14. #include <dhcpsrv/parsers/client_class_def_parser.h>
  15. #include <dhcp4/json_config_parser.h>
  16. #include <dhcpsrv/parsers/dbaccess_parser.h>
  17. #include <dhcpsrv/parsers/dhcp_parsers.h>
  18. #include <dhcpsrv/parsers/expiration_config_parser.h>
  19. #include <dhcpsrv/parsers/host_reservation_parser.h>
  20. #include <dhcpsrv/parsers/host_reservations_list_parser.h>
  21. #include <dhcpsrv/parsers/ifaces_config_parser.h>
  22. #include <dhcpsrv/timer_mgr.h>
  23. #include <hooks/hooks_parser.h>
  24. #include <config/command_mgr.h>
  25. #include <util/encode/hex.h>
  26. #include <util/strutil.h>
  27. #include <boost/foreach.hpp>
  28. #include <boost/lexical_cast.hpp>
  29. #include <boost/algorithm/string.hpp>
  30. #include <limits>
  31. #include <iostream>
  32. #include <netinet/in.h>
  33. #include <vector>
  34. #include <map>
  35. using namespace std;
  36. using namespace isc;
  37. using namespace isc::dhcp;
  38. using namespace isc::data;
  39. using namespace isc::asiolink;
  40. using namespace isc::hooks;
  41. namespace {
  42. /// @brief Parser for IPv4 pool definitions.
  43. ///
  44. /// This is the IPv4 derivation of the PoolParser class and handles pool
  45. /// definitions, i.e. a list of entries of one of two syntaxes: min-max and
  46. /// prefix/len for IPv4 pools. Pool4 objects are created and stored in chosen
  47. /// PoolStorage container.
  48. ///
  49. /// It is useful for parsing Dhcp4/subnet4[X]/pool parameters.
  50. class Pool4Parser : public PoolParser {
  51. protected:
  52. /// @brief Creates a Pool4 object given a IPv4 prefix and the prefix length.
  53. ///
  54. /// @param addr is the IPv4 prefix of the pool.
  55. /// @param len is the prefix length.
  56. /// @param ignored dummy parameter to provide symmetry between the
  57. /// PoolParser derivations. The V6 derivation requires a third value.
  58. /// @return returns a PoolPtr to the new Pool4 object.
  59. PoolPtr poolMaker (IOAddress &addr, uint32_t len, int32_t) {
  60. return (PoolPtr(new Pool4(addr, len)));
  61. }
  62. /// @brief Creates a Pool4 object given starting and ending IPv4 addresses.
  63. ///
  64. /// @param min is the first IPv4 address in the pool.
  65. /// @param max is the last IPv4 address in the pool.
  66. /// @param ignored dummy parameter to provide symmetry between the
  67. /// PoolParser derivations. The V6 derivation requires a third value.
  68. /// @return returns a PoolPtr to the new Pool4 object.
  69. PoolPtr poolMaker (IOAddress &min, IOAddress &max, int32_t) {
  70. return (PoolPtr(new Pool4(min, max)));
  71. }
  72. };
  73. /// @brief Specialization of the pool list parser for DHCPv4
  74. class Pools4ListParser : PoolsListParser {
  75. public:
  76. /// @brief parses the actual structure
  77. ///
  78. /// This method parses the actual list of pools.
  79. ///
  80. /// @param pools storage container in which to store the parsed pool.
  81. /// @param pools_list a list of pool structures
  82. /// @throw isc::dhcp::DhcpConfigError when pool parsing fails
  83. void parse(PoolStoragePtr pools,
  84. isc::data::ConstElementPtr pools_list) {
  85. BOOST_FOREACH(ConstElementPtr pool, pools_list->listValue()) {
  86. Pool4Parser parser;
  87. parser.parse(pools, pool, AF_INET);
  88. }
  89. }
  90. };
  91. /// @anchor Subnet4ConfigParser
  92. /// @brief This class parses a single IPv4 subnet.
  93. ///
  94. /// This is the IPv4 derivation of the SubnetConfigParser class and it parses
  95. /// the whole subnet definition. It creates parsersfor received configuration
  96. /// parameters as needed.
  97. class Subnet4ConfigParser : public SubnetConfigParser {
  98. public:
  99. /// @brief Constructor
  100. ///
  101. /// stores global scope parameters, options, option definitions.
  102. Subnet4ConfigParser()
  103. :SubnetConfigParser(AF_INET) {
  104. }
  105. /// @brief Parses a single IPv4 subnet configuration and adds to the
  106. /// Configuration Manager.
  107. ///
  108. /// @param subnet A new subnet being configured.
  109. /// @return a pointer to created Subnet4 object
  110. Subnet4Ptr parse(ConstElementPtr subnet) {
  111. /// Parse Pools first.
  112. ConstElementPtr pools = subnet->get("pools");
  113. if (pools) {
  114. Pools4ListParser parser;
  115. parser.parse(pools_, pools);
  116. }
  117. SubnetPtr generic = SubnetConfigParser::parse(subnet);
  118. if (!generic) {
  119. isc_throw(DhcpConfigError,
  120. "Failed to create an IPv4 subnet (" <<
  121. subnet->getPosition() << ")");
  122. }
  123. Subnet4Ptr sn4ptr = boost::dynamic_pointer_cast<Subnet4>(subnet_);
  124. if (!sn4ptr) {
  125. // If we hit this, it is a programming error.
  126. isc_throw(Unexpected,
  127. "Invalid Subnet4 cast in Subnet4ConfigParser::parse");
  128. }
  129. // Set relay information if it was parsed
  130. if (relay_info_) {
  131. sn4ptr->setRelayInfo(*relay_info_);
  132. }
  133. // Parse Host Reservations for this subnet if any.
  134. ConstElementPtr reservations = subnet->get("reservations");
  135. if (reservations) {
  136. HostReservationsListParser<HostReservationParser4> parser;
  137. parser.parse(subnet_->getID(), reservations);
  138. }
  139. return (sn4ptr);
  140. }
  141. protected:
  142. /// @brief Instantiates the IPv4 Subnet based on a given IPv4 address
  143. /// and prefix length.
  144. ///
  145. /// @param addr is IPv4 address of the subnet.
  146. /// @param len is the prefix length
  147. void initSubnet(isc::data::ConstElementPtr params,
  148. isc::asiolink::IOAddress addr, uint8_t len) {
  149. // The renew-timer and rebind-timer are optional. If not set, the
  150. // option 58 and 59 will not be sent to a client. In this case the
  151. // client will use default values based on the valid-lifetime.
  152. Triplet<uint32_t> t1 = getInteger(params, "renew-timer");
  153. Triplet<uint32_t> t2 = getInteger(params, "rebind-timer");
  154. // The valid-lifetime is mandatory. It may be specified for a
  155. // particular subnet. If not, the global value should be present.
  156. // If there is no global value, exception is thrown.
  157. Triplet<uint32_t> valid = getInteger(params, "valid-lifetime");
  158. // Subnet ID is optional. If it is not supplied the value of 0 is used,
  159. // which means autogenerate. The value was inserted earlier by calling
  160. // SimpleParser4::setAllDefaults.
  161. SubnetID subnet_id = static_cast<SubnetID>(getInteger(params, "id"));
  162. stringstream s;
  163. s << addr << "/" << static_cast<int>(len) << " with params: ";
  164. // t1 and t2 are optional may be not specified.
  165. if (!t1.unspecified()) {
  166. s << "t1=" << t1 << ", ";
  167. }
  168. if (!t2.unspecified()) {
  169. s << "t2=" << t2 << ", ";
  170. }
  171. s <<"valid-lifetime=" << valid;
  172. LOG_INFO(dhcp4_logger, DHCP4_CONFIG_NEW_SUBNET).arg(s.str());
  173. Subnet4Ptr subnet4(new Subnet4(addr, len, t1, t2, valid, subnet_id));
  174. subnet_ = subnet4;
  175. // Set the match-client-id value for the subnet. It is always present.
  176. // If not explicitly specified, the default value was filled in when
  177. // SimpleParser4::setAllDefaults was called.
  178. bool match_client_id = getBoolean(params, "match-client-id");
  179. subnet4->setMatchClientId(match_client_id);
  180. // Set next-server. The default value is 0.0.0.0. Nevertheless, the
  181. // user could have messed that up by specifying incorrect value.
  182. // To avoid using 0.0.0.0, user can specify "".
  183. string next_server;
  184. try {
  185. next_server = getString(params, "next-server");
  186. if (!next_server.empty()) {
  187. subnet4->setSiaddr(IOAddress(next_server));
  188. }
  189. } catch (...) {
  190. ConstElementPtr next = params->get("next-server");
  191. string pos;
  192. if (next)
  193. pos = next->getPosition().str();
  194. else
  195. pos = params->getPosition().str();
  196. isc_throw(DhcpConfigError, "invalid parameter next-server : "
  197. << next_server << "(" << pos << ")");
  198. }
  199. // 4o6 specific parameter: 4o6-interface. If not explicitly specified,
  200. // it will have the default value of "".
  201. string iface4o6 = getString(params, "4o6-interface");
  202. if (!iface4o6.empty()) {
  203. subnet4->get4o6().setIface4o6(iface4o6);
  204. subnet4->get4o6().enabled(true);
  205. }
  206. // 4o6 specific parameter: 4o6-subnet. If not explicitly specified, it
  207. // will have the default value of "".
  208. string subnet4o6 = getString(params, "4o6-subnet");
  209. if (!subnet4o6.empty()) {
  210. size_t slash = subnet4o6.find("/");
  211. if (slash == std::string::npos) {
  212. isc_throw(DhcpConfigError, "Missing / in the 4o6-subnet parameter:"
  213. << subnet4o6 << ", expected format: prefix6/length");
  214. }
  215. string prefix = subnet4o6.substr(0, slash);
  216. string lenstr = subnet4o6.substr(slash + 1);
  217. uint8_t len = 128;
  218. try {
  219. len = boost::lexical_cast<unsigned int>(lenstr.c_str());
  220. } catch (const boost::bad_lexical_cast &) {
  221. isc_throw(DhcpConfigError, "Invalid prefix length specified in "
  222. "4o6-subnet parameter: " << subnet4o6 << ", expected 0..128 value");
  223. }
  224. subnet4->get4o6().setSubnet4o6(IOAddress(prefix), len);
  225. subnet4->get4o6().enabled(true);
  226. }
  227. // Try 4o6 specific parameter: 4o6-interface-id
  228. std::string ifaceid = getString(params, "4o6-interface-id");
  229. if (!ifaceid.empty()) {
  230. OptionBuffer tmp(ifaceid.begin(), ifaceid.end());
  231. OptionPtr opt(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  232. subnet4->get4o6().setInterfaceId(opt);
  233. subnet4->get4o6().enabled(true);
  234. }
  235. /// client-class processing is now generic and handled in the common
  236. /// code (see @ref isc::data::SubnetConfigParser::createSubnet)
  237. }
  238. };
  239. /// @brief this class parses list of DHCP4 subnets
  240. ///
  241. /// This is a wrapper parser that handles the whole list of Subnet4
  242. /// definitions. It iterates over all entries and creates Subnet4ConfigParser
  243. /// for each entry.
  244. class Subnets4ListConfigParser : public isc::data::SimpleParser {
  245. public:
  246. /// @brief parses contents of the list
  247. ///
  248. /// Iterates over all entries on the list, parses its content
  249. /// (by instantiating Subnet6ConfigParser) and adds to specified
  250. /// configuration.
  251. ///
  252. /// @param subnets_list pointer to a list of IPv4 subnets
  253. /// @return number of subnets created
  254. size_t parse(SrvConfigPtr cfg, ConstElementPtr subnets_list) {
  255. size_t cnt = 0;
  256. BOOST_FOREACH(ConstElementPtr subnet_json, subnets_list->listValue()) {
  257. Subnet4ConfigParser parser;
  258. Subnet4Ptr subnet = parser.parse(subnet_json);
  259. if (subnet) {
  260. // Adding a subnet to the Configuration Manager may fail if the
  261. // subnet id is invalid (duplicate). Thus, we catch exceptions
  262. // here to append a position in the configuration string.
  263. try {
  264. cfg->getCfgSubnets4()->add(subnet);
  265. cnt++;
  266. } catch (const std::exception& ex) {
  267. isc_throw(DhcpConfigError, ex.what() << " ("
  268. << subnet_json->getPosition() << ")");
  269. }
  270. }
  271. }
  272. return (cnt);
  273. }
  274. };
  275. /// @brief Parser that takes care of global DHCPv4 parameters.
  276. ///
  277. /// See @ref parse method for a list of supported parameters.
  278. class Dhcp4ConfigParser : public isc::data::SimpleParser {
  279. public:
  280. /// @brief Sets global parameters in staging configuration
  281. ///
  282. /// @param global global configuration scope
  283. /// @param cfg Server configuration (parsed parameters will be stored here)
  284. ///
  285. /// Currently this method sets the following global parameters:
  286. ///
  287. /// - echo-client-id
  288. /// - decline-probation-period
  289. /// - dhcp4o6-port
  290. ///
  291. /// @throw DhcpConfigError if parameters are missing or
  292. /// or having incorrect values.
  293. void parse(SrvConfigPtr cfg, ConstElementPtr global) {
  294. // Set whether v4 server is supposed to echo back client-id
  295. // (yes = RFC6842 compatible, no = backward compatibility)
  296. bool echo_client_id = getBoolean(global, "echo-client-id");
  297. cfg->setEchoClientId(echo_client_id);
  298. // Set the probation period for decline handling.
  299. uint32_t probation_period =
  300. getUint32(global, "decline-probation-period");
  301. cfg->setDeclinePeriod(probation_period);
  302. // Set the DHCPv4-over-DHCPv6 interserver port.
  303. uint16_t dhcp4o6_port = getUint16(global, "dhcp4o6-port");
  304. cfg->setDhcp4o6Port(dhcp4o6_port);
  305. }
  306. };
  307. } // anonymous namespace
  308. namespace isc {
  309. namespace dhcp {
  310. /// @brief Initialize the command channel based on the staging configuration
  311. ///
  312. /// Only close the current channel, if the new channel configuration is
  313. /// different. This avoids disconnecting a client and hence not sending them
  314. /// a command result, unless they specifically alter the channel configuration.
  315. /// In that case the user simply has to accept they'll be disconnected.
  316. ///
  317. void configureCommandChannel() {
  318. // Get new socket configuration.
  319. ConstElementPtr sock_cfg =
  320. CfgMgr::instance().getStagingCfg()->getControlSocketInfo();
  321. // Get current socket configuration.
  322. ConstElementPtr current_sock_cfg =
  323. CfgMgr::instance().getCurrentCfg()->getControlSocketInfo();
  324. // Determine if the socket configuration has changed. It has if
  325. // both old and new configuration is specified but respective
  326. // data elements are't equal.
  327. bool sock_changed = (sock_cfg && current_sock_cfg &&
  328. !sock_cfg->equals(*current_sock_cfg));
  329. // If the previous or new socket configuration doesn't exist or
  330. // the new configuration differs from the old configuration we
  331. // close the exisitng socket and open a new socket as appropriate.
  332. // Note that closing an existing socket means the clien will not
  333. // receive the configuration result.
  334. if (!sock_cfg || !current_sock_cfg || sock_changed) {
  335. // Close the existing socket (if any).
  336. isc::config::CommandMgr::instance().closeCommandSocket();
  337. if (sock_cfg) {
  338. // This will create a control socket and install the external
  339. // socket in IfaceMgr. That socket will be monitored when
  340. // Dhcp4Srv::receivePacket() calls IfaceMgr::receive4() and
  341. // callback in CommandMgr will be called, if necessary.
  342. isc::config::CommandMgr::instance().openCommandSocket(sock_cfg);
  343. }
  344. }
  345. }
  346. isc::data::ConstElementPtr
  347. configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set,
  348. bool check_only) {
  349. if (!config_set) {
  350. ConstElementPtr answer = isc::config::createAnswer(1,
  351. string("Can't parse NULL config"));
  352. return (answer);
  353. }
  354. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND,
  355. DHCP4_CONFIG_START).arg(config_set->str());
  356. // Before starting any subnet operations, let's reset the subnet-id counter,
  357. // so newly recreated configuration starts with first subnet-id equal 1.
  358. Subnet::resetSubnetID();
  359. // Remove any existing timers.
  360. TimerMgr::instance()->unregisterTimers();
  361. // Revert any runtime option definitions configured so far and not committed.
  362. LibDHCP::revertRuntimeOptionDefs();
  363. // Let's set empty container in case a user hasn't specified any configuration
  364. // for option definitions. This is equivalent to commiting empty container.
  365. LibDHCP::setRuntimeOptionDefs(OptionDefSpaceContainer());
  366. // Answer will hold the result.
  367. ConstElementPtr answer;
  368. // Rollback informs whether error occurred and original data
  369. // have to be restored to global storages.
  370. bool rollback = false;
  371. // config_pair holds the details of the current parser when iterating over
  372. // the parsers. It is declared outside the loops so in case of an error,
  373. // the name of the failing parser can be retrieved in the "catch" clause.
  374. ConfigPair config_pair;
  375. try {
  376. SrvConfigPtr srv_cfg = CfgMgr::instance().getStagingCfg();
  377. // This is a way to convert ConstElementPtr to ElementPtr.
  378. // We need a config that can be edited, because we will insert
  379. // default values and will insert derived values as well.
  380. ElementPtr mutable_cfg = boost::const_pointer_cast<Element>(config_set);
  381. // Set all default values if not specified by the user.
  382. SimpleParser4::setAllDefaults(mutable_cfg);
  383. // And now derive (inherit) global parameters to subnets, if not specified.
  384. SimpleParser4::deriveParameters(mutable_cfg);
  385. // We need definitions first
  386. ConstElementPtr option_defs = mutable_cfg->get("option-def");
  387. if (option_defs) {
  388. OptionDefListParser parser;
  389. CfgOptionDefPtr cfg_option_def = srv_cfg->getCfgOptionDef();
  390. parser.parse(cfg_option_def, option_defs);
  391. }
  392. // Make parsers grouping.
  393. const std::map<std::string, ConstElementPtr>& values_map =
  394. mutable_cfg->mapValue();
  395. BOOST_FOREACH(config_pair, values_map) {
  396. // In principle we could have the following code structured as a series
  397. // of long if else if clauses. That would give a marginal performance
  398. // boost, but would make the code less readable. We had serious issues
  399. // with the parser code debugability, so I decided to keep it as a
  400. // series of independent ifs.
  401. if (config_pair.first == "option-def") {
  402. // This is converted to SimpleParser and is handled already above.
  403. continue;
  404. }
  405. if (config_pair.first == "option-data") {
  406. OptionDataListParser parser(AF_INET);
  407. CfgOptionPtr cfg_option = srv_cfg->getCfgOption();
  408. parser.parse(cfg_option, config_pair.second);
  409. continue;
  410. }
  411. if (config_pair.first == "control-socket") {
  412. ControlSocketParser parser;
  413. parser.parse(*srv_cfg, config_pair.second);
  414. continue;
  415. }
  416. if (config_pair.first == "host-reservation-identifiers") {
  417. HostReservationIdsParser4 parser;
  418. parser.parse(config_pair.second);
  419. continue;
  420. }
  421. if (config_pair.first == "interfaces-config") {
  422. IfacesConfigParser parser(AF_INET);
  423. CfgIfacePtr cfg_iface = srv_cfg->getCfgIface();
  424. parser.parse(cfg_iface, config_pair.second);
  425. continue;
  426. }
  427. if (config_pair.first == "expired-leases-processing") {
  428. ExpirationConfigParser parser;
  429. parser.parse(config_pair.second);
  430. continue;
  431. }
  432. if (config_pair.first == "hooks-libraries") {
  433. HooksLibrariesParser hooks_parser;
  434. HooksConfig& libraries = srv_cfg->getHooksConfig();
  435. hooks_parser.parse(libraries, config_pair.second);
  436. libraries.verifyLibraries(config_pair.second->getPosition());
  437. continue;
  438. }
  439. // Legacy DhcpConfigParser stuff below
  440. if (config_pair.first == "dhcp-ddns") {
  441. // Apply defaults
  442. D2ClientConfigParser::setAllDefaults(config_pair.second);
  443. D2ClientConfigParser parser;
  444. D2ClientConfigPtr cfg = parser.parse(config_pair.second);
  445. srv_cfg->setD2ClientConfig(cfg);
  446. continue;
  447. }
  448. if (config_pair.first == "client-classes") {
  449. ClientClassDefListParser parser;
  450. ClientClassDictionaryPtr dictionary =
  451. parser.parse(config_pair.second, AF_INET);
  452. srv_cfg->setClientClassDictionary(dictionary);
  453. continue;
  454. }
  455. // Please move at the end when migration will be finished.
  456. if (config_pair.first == "lease-database") {
  457. DbAccessParser parser(DbAccessParser::LEASE_DB);
  458. CfgDbAccessPtr cfg_db_access = srv_cfg->getCfgDbAccess();
  459. parser.parse(cfg_db_access, config_pair.second);
  460. continue;
  461. }
  462. if (config_pair.first == "hosts-database") {
  463. DbAccessParser parser(DbAccessParser::HOSTS_DB);
  464. CfgDbAccessPtr cfg_db_access = srv_cfg->getCfgDbAccess();
  465. parser.parse(cfg_db_access, config_pair.second);
  466. continue;
  467. }
  468. if (config_pair.first == "subnet4") {
  469. SrvConfigPtr srv_cfg = CfgMgr::instance().getStagingCfg();
  470. Subnets4ListConfigParser subnets_parser;
  471. // parse() returns number of subnets parsed. We may log it one day.
  472. subnets_parser.parse(srv_cfg, config_pair.second);
  473. continue;
  474. }
  475. // Timers are not used in the global scope. Their values are derived
  476. // to specific subnets (see SimpleParser6::deriveParameters).
  477. // decline-probation-period, dhcp4o6-port, echo-client-id are
  478. // handled in global_parser.parse() which sets global parameters.
  479. // match-client-id is derived to subnet scope level.
  480. if ( (config_pair.first == "renew-timer") ||
  481. (config_pair.first == "rebind-timer") ||
  482. (config_pair.first == "valid-lifetime") ||
  483. (config_pair.first == "decline-probation-period") ||
  484. (config_pair.first == "dhcp4o6-port") ||
  485. (config_pair.first == "echo-client-id") ||
  486. (config_pair.first == "match-client-id") ||
  487. (config_pair.first == "next-server")) {
  488. continue;
  489. }
  490. // If we got here, no code handled this parameter, so we bail out.
  491. isc_throw(DhcpConfigError,
  492. "unsupported global configuration parameter: " << config_pair.first
  493. << " (" << config_pair.second->getPosition() << ")");
  494. }
  495. // Apply global options in the staging config.
  496. Dhcp4ConfigParser global_parser;
  497. global_parser.parse(srv_cfg, mutable_cfg);
  498. } catch (const isc::Exception& ex) {
  499. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_FAIL)
  500. .arg(config_pair.first).arg(ex.what());
  501. answer = isc::config::createAnswer(1, ex.what());
  502. // An error occurred, so make sure that we restore original data.
  503. rollback = true;
  504. } catch (...) {
  505. // For things like bad_cast in boost::lexical_cast
  506. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_EXCEPTION).arg(config_pair.first);
  507. answer = isc::config::createAnswer(1, "undefined configuration"
  508. " processing error");
  509. // An error occurred, so make sure that we restore original data.
  510. rollback = true;
  511. }
  512. if (check_only) {
  513. rollback = true;
  514. if (!answer) {
  515. answer = isc::config::createAnswer(0,
  516. "Configuration seems sane. Control-socket, hook-libraries, and D2 "
  517. "configuration were sanity checked, but not applied.");
  518. }
  519. }
  520. // So far so good, there was no parsing error so let's commit the
  521. // configuration. This will add created subnets and option values into
  522. // the server's configuration.
  523. // This operation should be exception safe but let's make sure.
  524. if (!rollback) {
  525. try {
  526. // Setup the command channel.
  527. configureCommandChannel();
  528. // No need to commit interface names as this is handled by the
  529. // CfgMgr::commit() function.
  530. // Apply the staged D2ClientConfig, used to be done by parser commit
  531. D2ClientConfigPtr cfg;
  532. cfg = CfgMgr::instance().getStagingCfg()->getD2ClientConfig();
  533. CfgMgr::instance().setD2ClientConfig(cfg);
  534. // This occurs last as if it succeeds, there is no easy way
  535. // revert it. As a result, the failure to commit a subsequent
  536. // change causes problems when trying to roll back.
  537. const HooksConfig& libraries =
  538. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  539. libraries.loadLibraries();
  540. }
  541. catch (const isc::Exception& ex) {
  542. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_FAIL).arg(ex.what());
  543. answer = isc::config::createAnswer(2, ex.what());
  544. rollback = true;
  545. } catch (...) {
  546. // For things like bad_cast in boost::lexical_cast
  547. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_EXCEPTION);
  548. answer = isc::config::createAnswer(2, "undefined configuration"
  549. " parsing error");
  550. rollback = true;
  551. }
  552. }
  553. // Rollback changes as the configuration parsing failed.
  554. if (rollback) {
  555. // Revert to original configuration of runtime option definitions
  556. // in the libdhcp++.
  557. LibDHCP::revertRuntimeOptionDefs();
  558. return (answer);
  559. }
  560. LOG_INFO(dhcp4_logger, DHCP4_CONFIG_COMPLETE)
  561. .arg(CfgMgr::instance().getStagingCfg()->
  562. getConfigSummary(SrvConfig::CFGSEL_ALL4));
  563. // Everything was fine. Configuration is successful.
  564. answer = isc::config::createAnswer(0, "Configuration successful.");
  565. return (answer);
  566. }
  567. }; // end of isc::dhcp namespace
  568. }; // end of isc namespace