json_config_parser.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. HostCollection hosts;
  137. HostReservationsListParser<HostReservationParser4> parser;
  138. parser.parse(subnet_->getID(), reservations, hosts);
  139. for (auto h = hosts.begin(); h != hosts.end(); ++h) {
  140. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(*h);
  141. }
  142. }
  143. return (sn4ptr);
  144. }
  145. protected:
  146. /// @brief Instantiates the IPv4 Subnet based on a given IPv4 address
  147. /// and prefix length.
  148. ///
  149. /// @param addr is IPv4 address of the subnet.
  150. /// @param len is the prefix length
  151. void initSubnet(isc::data::ConstElementPtr params,
  152. isc::asiolink::IOAddress addr, uint8_t len) {
  153. // The renew-timer and rebind-timer are optional. If not set, the
  154. // option 58 and 59 will not be sent to a client. In this case the
  155. // client will use default values based on the valid-lifetime.
  156. Triplet<uint32_t> t1 = getInteger(params, "renew-timer");
  157. Triplet<uint32_t> t2 = getInteger(params, "rebind-timer");
  158. // The valid-lifetime is mandatory. It may be specified for a
  159. // particular subnet. If not, the global value should be present.
  160. // If there is no global value, exception is thrown.
  161. Triplet<uint32_t> valid = getInteger(params, "valid-lifetime");
  162. // Subnet ID is optional. If it is not supplied the value of 0 is used,
  163. // which means autogenerate. The value was inserted earlier by calling
  164. // SimpleParser4::setAllDefaults.
  165. SubnetID subnet_id = static_cast<SubnetID>(getInteger(params, "id"));
  166. stringstream s;
  167. s << addr << "/" << static_cast<int>(len) << " with params: ";
  168. // t1 and t2 are optional may be not specified.
  169. if (!t1.unspecified()) {
  170. s << "t1=" << t1 << ", ";
  171. }
  172. if (!t2.unspecified()) {
  173. s << "t2=" << t2 << ", ";
  174. }
  175. s <<"valid-lifetime=" << valid;
  176. LOG_INFO(dhcp4_logger, DHCP4_CONFIG_NEW_SUBNET).arg(s.str());
  177. Subnet4Ptr subnet4(new Subnet4(addr, len, t1, t2, valid, subnet_id));
  178. subnet_ = subnet4;
  179. // Set the match-client-id value for the subnet. It is always present.
  180. // If not explicitly specified, the default value was filled in when
  181. // SimpleParser4::setAllDefaults was called.
  182. bool match_client_id = getBoolean(params, "match-client-id");
  183. subnet4->setMatchClientId(match_client_id);
  184. // Set next-server. The default value is 0.0.0.0. Nevertheless, the
  185. // user could have messed that up by specifying incorrect value.
  186. // To avoid using 0.0.0.0, user can specify "".
  187. string next_server;
  188. try {
  189. next_server = getString(params, "next-server");
  190. if (!next_server.empty()) {
  191. subnet4->setSiaddr(IOAddress(next_server));
  192. }
  193. } catch (...) {
  194. ConstElementPtr next = params->get("next-server");
  195. string pos;
  196. if (next)
  197. pos = next->getPosition().str();
  198. else
  199. pos = params->getPosition().str();
  200. isc_throw(DhcpConfigError, "invalid parameter next-server : "
  201. << next_server << "(" << pos << ")");
  202. }
  203. // 4o6 specific parameter: 4o6-interface. If not explicitly specified,
  204. // it will have the default value of "".
  205. string iface4o6 = getString(params, "4o6-interface");
  206. if (!iface4o6.empty()) {
  207. subnet4->get4o6().setIface4o6(iface4o6);
  208. subnet4->get4o6().enabled(true);
  209. }
  210. // 4o6 specific parameter: 4o6-subnet. If not explicitly specified, it
  211. // will have the default value of "".
  212. string subnet4o6 = getString(params, "4o6-subnet");
  213. if (!subnet4o6.empty()) {
  214. size_t slash = subnet4o6.find("/");
  215. if (slash == std::string::npos) {
  216. isc_throw(DhcpConfigError, "Missing / in the 4o6-subnet parameter:"
  217. << subnet4o6 << ", expected format: prefix6/length");
  218. }
  219. string prefix = subnet4o6.substr(0, slash);
  220. string lenstr = subnet4o6.substr(slash + 1);
  221. uint8_t len = 128;
  222. try {
  223. len = boost::lexical_cast<unsigned int>(lenstr.c_str());
  224. } catch (const boost::bad_lexical_cast &) {
  225. isc_throw(DhcpConfigError, "Invalid prefix length specified in "
  226. "4o6-subnet parameter: " << subnet4o6 << ", expected 0..128 value");
  227. }
  228. subnet4->get4o6().setSubnet4o6(IOAddress(prefix), len);
  229. subnet4->get4o6().enabled(true);
  230. }
  231. // Try 4o6 specific parameter: 4o6-interface-id
  232. std::string ifaceid = getString(params, "4o6-interface-id");
  233. if (!ifaceid.empty()) {
  234. OptionBuffer tmp(ifaceid.begin(), ifaceid.end());
  235. OptionPtr opt(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  236. subnet4->get4o6().setInterfaceId(opt);
  237. subnet4->get4o6().enabled(true);
  238. }
  239. /// client-class processing is now generic and handled in the common
  240. /// code (see @ref isc::data::SubnetConfigParser::createSubnet)
  241. }
  242. };
  243. /// @brief this class parses list of DHCP4 subnets
  244. ///
  245. /// This is a wrapper parser that handles the whole list of Subnet4
  246. /// definitions. It iterates over all entries and creates Subnet4ConfigParser
  247. /// for each entry.
  248. class Subnets4ListConfigParser : public isc::data::SimpleParser {
  249. public:
  250. /// @brief parses contents of the list
  251. ///
  252. /// Iterates over all entries on the list, parses its content
  253. /// (by instantiating Subnet6ConfigParser) and adds to specified
  254. /// configuration.
  255. ///
  256. /// @param subnets_list pointer to a list of IPv4 subnets
  257. /// @return number of subnets created
  258. size_t parse(SrvConfigPtr cfg, ConstElementPtr subnets_list) {
  259. size_t cnt = 0;
  260. BOOST_FOREACH(ConstElementPtr subnet_json, subnets_list->listValue()) {
  261. Subnet4ConfigParser parser;
  262. Subnet4Ptr subnet = parser.parse(subnet_json);
  263. if (subnet) {
  264. // Adding a subnet to the Configuration Manager may fail if the
  265. // subnet id is invalid (duplicate). Thus, we catch exceptions
  266. // here to append a position in the configuration string.
  267. try {
  268. cfg->getCfgSubnets4()->add(subnet);
  269. cnt++;
  270. } catch (const std::exception& ex) {
  271. isc_throw(DhcpConfigError, ex.what() << " ("
  272. << subnet_json->getPosition() << ")");
  273. }
  274. }
  275. }
  276. return (cnt);
  277. }
  278. };
  279. /// @brief Parser that takes care of global DHCPv4 parameters.
  280. ///
  281. /// See @ref parse method for a list of supported parameters.
  282. class Dhcp4ConfigParser : public isc::data::SimpleParser {
  283. public:
  284. /// @brief Sets global parameters in staging configuration
  285. ///
  286. /// @param global global configuration scope
  287. /// @param cfg Server configuration (parsed parameters will be stored here)
  288. ///
  289. /// Currently this method sets the following global parameters:
  290. ///
  291. /// - echo-client-id
  292. /// - decline-probation-period
  293. /// - dhcp4o6-port
  294. ///
  295. /// @throw DhcpConfigError if parameters are missing or
  296. /// or having incorrect values.
  297. void parse(SrvConfigPtr cfg, ConstElementPtr global) {
  298. // Set whether v4 server is supposed to echo back client-id
  299. // (yes = RFC6842 compatible, no = backward compatibility)
  300. bool echo_client_id = getBoolean(global, "echo-client-id");
  301. cfg->setEchoClientId(echo_client_id);
  302. // Set the probation period for decline handling.
  303. uint32_t probation_period =
  304. getUint32(global, "decline-probation-period");
  305. cfg->setDeclinePeriod(probation_period);
  306. // Set the DHCPv4-over-DHCPv6 interserver port.
  307. uint16_t dhcp4o6_port = getUint16(global, "dhcp4o6-port");
  308. cfg->setDhcp4o6Port(dhcp4o6_port);
  309. }
  310. };
  311. } // anonymous namespace
  312. namespace isc {
  313. namespace dhcp {
  314. /// @brief Initialize the command channel based on the staging configuration
  315. ///
  316. /// Only close the current channel, if the new channel configuration is
  317. /// different. This avoids disconnecting a client and hence not sending them
  318. /// a command result, unless they specifically alter the channel configuration.
  319. /// In that case the user simply has to accept they'll be disconnected.
  320. ///
  321. void configureCommandChannel() {
  322. // Get new socket configuration.
  323. ConstElementPtr sock_cfg =
  324. CfgMgr::instance().getStagingCfg()->getControlSocketInfo();
  325. // Get current socket configuration.
  326. ConstElementPtr current_sock_cfg =
  327. CfgMgr::instance().getCurrentCfg()->getControlSocketInfo();
  328. // Determine if the socket configuration has changed. It has if
  329. // both old and new configuration is specified but respective
  330. // data elements are't equal.
  331. bool sock_changed = (sock_cfg && current_sock_cfg &&
  332. !sock_cfg->equals(*current_sock_cfg));
  333. // If the previous or new socket configuration doesn't exist or
  334. // the new configuration differs from the old configuration we
  335. // close the exisitng socket and open a new socket as appropriate.
  336. // Note that closing an existing socket means the clien will not
  337. // receive the configuration result.
  338. if (!sock_cfg || !current_sock_cfg || sock_changed) {
  339. // Close the existing socket (if any).
  340. isc::config::CommandMgr::instance().closeCommandSocket();
  341. if (sock_cfg) {
  342. // This will create a control socket and install the external
  343. // socket in IfaceMgr. That socket will be monitored when
  344. // Dhcp4Srv::receivePacket() calls IfaceMgr::receive4() and
  345. // callback in CommandMgr will be called, if necessary.
  346. isc::config::CommandMgr::instance().openCommandSocket(sock_cfg);
  347. }
  348. }
  349. }
  350. isc::data::ConstElementPtr
  351. configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set,
  352. bool check_only) {
  353. if (!config_set) {
  354. ConstElementPtr answer = isc::config::createAnswer(1,
  355. string("Can't parse NULL config"));
  356. return (answer);
  357. }
  358. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND,
  359. DHCP4_CONFIG_START).arg(config_set->str());
  360. // Before starting any subnet operations, let's reset the subnet-id counter,
  361. // so newly recreated configuration starts with first subnet-id equal 1.
  362. Subnet::resetSubnetID();
  363. // Remove any existing timers.
  364. if (!check_only) {
  365. TimerMgr::instance()->unregisterTimers();
  366. }
  367. // Revert any runtime option definitions configured so far and not committed.
  368. LibDHCP::revertRuntimeOptionDefs();
  369. // Let's set empty container in case a user hasn't specified any configuration
  370. // for option definitions. This is equivalent to commiting empty container.
  371. LibDHCP::setRuntimeOptionDefs(OptionDefSpaceContainer());
  372. // Answer will hold the result.
  373. ConstElementPtr answer;
  374. // Rollback informs whether error occurred and original data
  375. // have to be restored to global storages.
  376. bool rollback = false;
  377. // config_pair holds the details of the current parser when iterating over
  378. // the parsers. It is declared outside the loops so in case of an error,
  379. // the name of the failing parser can be retrieved in the "catch" clause.
  380. ConfigPair config_pair;
  381. try {
  382. SrvConfigPtr srv_cfg = CfgMgr::instance().getStagingCfg();
  383. // This is a way to convert ConstElementPtr to ElementPtr.
  384. // We need a config that can be edited, because we will insert
  385. // default values and will insert derived values as well.
  386. ElementPtr mutable_cfg = boost::const_pointer_cast<Element>(config_set);
  387. // Set all default values if not specified by the user.
  388. SimpleParser4::setAllDefaults(mutable_cfg);
  389. // And now derive (inherit) global parameters to subnets, if not specified.
  390. SimpleParser4::deriveParameters(mutable_cfg);
  391. // We need definitions first
  392. ConstElementPtr option_defs = mutable_cfg->get("option-def");
  393. if (option_defs) {
  394. OptionDefListParser parser;
  395. CfgOptionDefPtr cfg_option_def = srv_cfg->getCfgOptionDef();
  396. parser.parse(cfg_option_def, option_defs);
  397. }
  398. // Make parsers grouping.
  399. const std::map<std::string, ConstElementPtr>& values_map =
  400. mutable_cfg->mapValue();
  401. BOOST_FOREACH(config_pair, values_map) {
  402. // In principle we could have the following code structured as a series
  403. // of long if else if clauses. That would give a marginal performance
  404. // boost, but would make the code less readable. We had serious issues
  405. // with the parser code debugability, so I decided to keep it as a
  406. // series of independent ifs.
  407. if (config_pair.first == "option-def") {
  408. // This is converted to SimpleParser and is handled already above.
  409. continue;
  410. }
  411. if (config_pair.first == "option-data") {
  412. OptionDataListParser parser(AF_INET);
  413. CfgOptionPtr cfg_option = srv_cfg->getCfgOption();
  414. parser.parse(cfg_option, config_pair.second);
  415. continue;
  416. }
  417. if (config_pair.first == "control-socket") {
  418. ControlSocketParser parser;
  419. parser.parse(*srv_cfg, config_pair.second);
  420. continue;
  421. }
  422. if (config_pair.first == "host-reservation-identifiers") {
  423. HostReservationIdsParser4 parser;
  424. parser.parse(config_pair.second);
  425. continue;
  426. }
  427. if (config_pair.first == "interfaces-config") {
  428. IfacesConfigParser parser(AF_INET);
  429. CfgIfacePtr cfg_iface = srv_cfg->getCfgIface();
  430. parser.parse(cfg_iface, config_pair.second);
  431. continue;
  432. }
  433. if (config_pair.first == "expired-leases-processing") {
  434. ExpirationConfigParser parser;
  435. parser.parse(config_pair.second);
  436. continue;
  437. }
  438. if (config_pair.first == "hooks-libraries") {
  439. HooksLibrariesParser hooks_parser;
  440. HooksConfig& libraries = srv_cfg->getHooksConfig();
  441. hooks_parser.parse(libraries, config_pair.second);
  442. libraries.verifyLibraries(config_pair.second->getPosition());
  443. continue;
  444. }
  445. // Legacy DhcpConfigParser stuff below
  446. if (config_pair.first == "dhcp-ddns") {
  447. // Apply defaults
  448. D2ClientConfigParser::setAllDefaults(config_pair.second);
  449. D2ClientConfigParser parser;
  450. D2ClientConfigPtr cfg = parser.parse(config_pair.second);
  451. srv_cfg->setD2ClientConfig(cfg);
  452. continue;
  453. }
  454. if (config_pair.first == "client-classes") {
  455. ClientClassDefListParser parser;
  456. ClientClassDictionaryPtr dictionary =
  457. parser.parse(config_pair.second, AF_INET);
  458. srv_cfg->setClientClassDictionary(dictionary);
  459. continue;
  460. }
  461. // Please move at the end when migration will be finished.
  462. if (config_pair.first == "lease-database") {
  463. DbAccessParser parser(DbAccessParser::LEASE_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 == "hosts-database") {
  469. DbAccessParser parser(DbAccessParser::HOSTS_DB);
  470. CfgDbAccessPtr cfg_db_access = srv_cfg->getCfgDbAccess();
  471. parser.parse(cfg_db_access, config_pair.second);
  472. continue;
  473. }
  474. if (config_pair.first == "subnet4") {
  475. SrvConfigPtr srv_cfg = CfgMgr::instance().getStagingCfg();
  476. Subnets4ListConfigParser subnets_parser;
  477. // parse() returns number of subnets parsed. We may log it one day.
  478. subnets_parser.parse(srv_cfg, config_pair.second);
  479. continue;
  480. }
  481. // Timers are not used in the global scope. Their values are derived
  482. // to specific subnets (see SimpleParser6::deriveParameters).
  483. // decline-probation-period, dhcp4o6-port, echo-client-id are
  484. // handled in global_parser.parse() which sets global parameters.
  485. // match-client-id is derived to subnet scope level.
  486. if ( (config_pair.first == "renew-timer") ||
  487. (config_pair.first == "rebind-timer") ||
  488. (config_pair.first == "valid-lifetime") ||
  489. (config_pair.first == "decline-probation-period") ||
  490. (config_pair.first == "dhcp4o6-port") ||
  491. (config_pair.first == "echo-client-id") ||
  492. (config_pair.first == "match-client-id") ||
  493. (config_pair.first == "next-server")) {
  494. continue;
  495. }
  496. // If we got here, no code handled this parameter, so we bail out.
  497. isc_throw(DhcpConfigError,
  498. "unsupported global configuration parameter: " << config_pair.first
  499. << " (" << config_pair.second->getPosition() << ")");
  500. }
  501. // Apply global options in the staging config.
  502. Dhcp4ConfigParser global_parser;
  503. global_parser.parse(srv_cfg, mutable_cfg);
  504. } catch (const isc::Exception& ex) {
  505. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_FAIL)
  506. .arg(config_pair.first).arg(ex.what());
  507. answer = isc::config::createAnswer(1, ex.what());
  508. // An error occurred, so make sure that we restore original data.
  509. rollback = true;
  510. } catch (...) {
  511. // For things like bad_cast in boost::lexical_cast
  512. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_EXCEPTION).arg(config_pair.first);
  513. answer = isc::config::createAnswer(1, "undefined configuration"
  514. " processing error");
  515. // An error occurred, so make sure that we restore original data.
  516. rollback = true;
  517. }
  518. if (check_only) {
  519. rollback = true;
  520. if (!answer) {
  521. answer = isc::config::createAnswer(0,
  522. "Configuration seems sane. Control-socket, hook-libraries, and D2 "
  523. "configuration were sanity checked, but not applied.");
  524. }
  525. }
  526. // So far so good, there was no parsing error so let's commit the
  527. // configuration. This will add created subnets and option values into
  528. // the server's configuration.
  529. // This operation should be exception safe but let's make sure.
  530. if (!rollback) {
  531. try {
  532. // Setup the command channel.
  533. configureCommandChannel();
  534. // No need to commit interface names as this is handled by the
  535. // CfgMgr::commit() function.
  536. // Apply the staged D2ClientConfig, used to be done by parser commit
  537. D2ClientConfigPtr cfg;
  538. cfg = CfgMgr::instance().getStagingCfg()->getD2ClientConfig();
  539. CfgMgr::instance().setD2ClientConfig(cfg);
  540. // This occurs last as if it succeeds, there is no easy way
  541. // revert it. As a result, the failure to commit a subsequent
  542. // change causes problems when trying to roll back.
  543. const HooksConfig& libraries =
  544. CfgMgr::instance().getStagingCfg()->getHooksConfig();
  545. libraries.loadLibraries();
  546. }
  547. catch (const isc::Exception& ex) {
  548. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_FAIL).arg(ex.what());
  549. answer = isc::config::createAnswer(2, ex.what());
  550. rollback = true;
  551. } catch (...) {
  552. // For things like bad_cast in boost::lexical_cast
  553. LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_EXCEPTION);
  554. answer = isc::config::createAnswer(2, "undefined configuration"
  555. " parsing error");
  556. rollback = true;
  557. }
  558. }
  559. // Rollback changes as the configuration parsing failed.
  560. if (rollback) {
  561. // Revert to original configuration of runtime option definitions
  562. // in the libdhcp++.
  563. LibDHCP::revertRuntimeOptionDefs();
  564. return (answer);
  565. }
  566. LOG_INFO(dhcp4_logger, DHCP4_CONFIG_COMPLETE)
  567. .arg(CfgMgr::instance().getStagingCfg()->
  568. getConfigSummary(SrvConfig::CFGSEL_ALL4));
  569. // Everything was fine. Configuration is successful.
  570. answer = isc::config::createAnswer(0, "Configuration successful.");
  571. return (answer);
  572. }
  573. }; // end of isc::dhcp namespace
  574. }; // end of isc namespace