json_config_parser.cc 26 KB

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