d2_config.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. // Copyright (C) 2013-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 <d2/d2_log.h>
  16. #include <d2/d2_cfg_mgr.h>
  17. #include <dhcpsrv/parsers/dhcp_parsers.h>
  18. #include <exceptions/exceptions.h>
  19. #include <asiolink/io_error.h>
  20. #include <boost/foreach.hpp>
  21. #include <boost/lexical_cast.hpp>
  22. #include <boost/scoped_ptr.hpp>
  23. #include <boost/algorithm/string/predicate.hpp>
  24. #include <sstream>
  25. #include <string>
  26. namespace isc {
  27. namespace d2 {
  28. // *********************** D2Params *************************
  29. const char *D2Params::DFT_IP_ADDRESS = "127.0.0.1";
  30. const size_t D2Params::DFT_PORT = 53001;
  31. const size_t D2Params::DFT_DNS_SERVER_TIMEOUT = 100;
  32. const char *D2Params::DFT_NCR_PROTOCOL = "UDP";
  33. const char *D2Params::DFT_NCR_FORMAT = "JSON";
  34. D2Params::D2Params(const isc::asiolink::IOAddress& ip_address,
  35. const size_t port,
  36. const size_t dns_server_timeout,
  37. const dhcp_ddns::NameChangeProtocol& ncr_protocol,
  38. const dhcp_ddns::NameChangeFormat& ncr_format)
  39. : ip_address_(ip_address),
  40. port_(port),
  41. dns_server_timeout_(dns_server_timeout),
  42. ncr_protocol_(ncr_protocol),
  43. ncr_format_(ncr_format) {
  44. validateContents();
  45. }
  46. D2Params::D2Params()
  47. : ip_address_(isc::asiolink::IOAddress(DFT_IP_ADDRESS)),
  48. port_(DFT_PORT),
  49. dns_server_timeout_(DFT_DNS_SERVER_TIMEOUT),
  50. ncr_protocol_(dhcp_ddns::NCR_UDP),
  51. ncr_format_(dhcp_ddns::FMT_JSON) {
  52. validateContents();
  53. }
  54. D2Params::~D2Params(){};
  55. void
  56. D2Params::validateContents() {
  57. if ((ip_address_.toText() == "0.0.0.0") || (ip_address_.toText() == "::")) {
  58. isc_throw(D2CfgError,
  59. "D2Params: IP address cannot be \"" << ip_address_ << "\"");
  60. }
  61. if (port_ == 0) {
  62. isc_throw(D2CfgError, "D2Params: port cannot be 0");
  63. }
  64. if (dns_server_timeout_ < 1) {
  65. isc_throw(D2CfgError,
  66. "D2Params: DNS server timeout must be larger than 0");
  67. }
  68. if (ncr_format_ != dhcp_ddns::FMT_JSON) {
  69. isc_throw(D2CfgError, "D2Params: NCR Format:"
  70. << dhcp_ddns::ncrFormatToString(ncr_format_)
  71. << " is not yet supported");
  72. }
  73. if (ncr_protocol_ != dhcp_ddns::NCR_UDP) {
  74. isc_throw(D2CfgError, "D2Params: NCR Protocol:"
  75. << dhcp_ddns::ncrProtocolToString(ncr_protocol_)
  76. << " is not yet supported");
  77. }
  78. }
  79. std::string
  80. D2Params::getConfigSummary() const {
  81. std::ostringstream s;
  82. s << "listening on " << getIpAddress() << ", port " << getPort()
  83. << ", using " << ncrProtocolToString(ncr_protocol_);
  84. return (s.str());
  85. }
  86. bool
  87. D2Params::operator == (const D2Params& other) const {
  88. return ((ip_address_ == other.ip_address_) &&
  89. (port_ == other.port_) &&
  90. (dns_server_timeout_ == other.dns_server_timeout_) &&
  91. (ncr_protocol_ == other.ncr_protocol_) &&
  92. (ncr_format_ == other.ncr_format_));
  93. }
  94. bool
  95. D2Params::operator != (const D2Params& other) const {
  96. return (!(*this == other));
  97. }
  98. std::string
  99. D2Params::toText() const {
  100. std::ostringstream stream;
  101. stream << ", ip-address: " << ip_address_.toText()
  102. << ", port: " << port_
  103. << ", dns-server-timeout_: " << dns_server_timeout_
  104. << ", ncr-protocol: "
  105. << dhcp_ddns::ncrProtocolToString(ncr_protocol_)
  106. << ", ncr-format: " << ncr_format_
  107. << dhcp_ddns::ncrFormatToString(ncr_format_);
  108. return (stream.str());
  109. }
  110. std::ostream&
  111. operator<<(std::ostream& os, const D2Params& config) {
  112. os << config.toText();
  113. return (os);
  114. }
  115. // *********************** TSIGKeyInfo *************************
  116. // Note these values match correpsonding values for Bind9's
  117. // dnssec-keygen
  118. const char* TSIGKeyInfo::HMAC_MD5_STR = "HMAC-MD5";
  119. const char* TSIGKeyInfo::HMAC_SHA1_STR = "HMAC-SHA1";
  120. const char* TSIGKeyInfo::HMAC_SHA224_STR = "HMAC-SHA224";
  121. const char* TSIGKeyInfo::HMAC_SHA256_STR = "HMAC-SHA256";
  122. const char* TSIGKeyInfo::HMAC_SHA384_STR = "HMAC-SHA384";
  123. const char* TSIGKeyInfo::HMAC_SHA512_STR = "HMAC-SHA512";
  124. TSIGKeyInfo::TSIGKeyInfo(const std::string& name, const std::string& algorithm,
  125. const std::string& secret, uint32_t digestbits)
  126. :name_(name), algorithm_(algorithm), secret_(secret),
  127. digestbits_(digestbits), tsig_key_() {
  128. remakeKey();
  129. }
  130. TSIGKeyInfo::~TSIGKeyInfo() {
  131. }
  132. const dns::Name&
  133. TSIGKeyInfo::stringToAlgorithmName(const std::string& algorithm_id) {
  134. if (boost::iequals(algorithm_id, HMAC_MD5_STR)) {
  135. return (dns::TSIGKey::HMACMD5_NAME());
  136. } else if (boost::iequals(algorithm_id, HMAC_SHA1_STR)) {
  137. return (dns::TSIGKey::HMACSHA1_NAME());
  138. } else if (boost::iequals(algorithm_id, HMAC_SHA224_STR)) {
  139. return (dns::TSIGKey::HMACSHA224_NAME());
  140. } else if (boost::iequals(algorithm_id, HMAC_SHA256_STR)) {
  141. return (dns::TSIGKey::HMACSHA256_NAME());
  142. } else if (boost::iequals(algorithm_id, HMAC_SHA384_STR)) {
  143. return (dns::TSIGKey::HMACSHA384_NAME());
  144. } else if (boost::iequals(algorithm_id, HMAC_SHA512_STR)) {
  145. return (dns::TSIGKey::HMACSHA512_NAME());
  146. }
  147. isc_throw(BadValue, "Unknown TSIG Key algorithm: " << algorithm_id);
  148. }
  149. void
  150. TSIGKeyInfo::remakeKey() {
  151. try {
  152. // Since our secret value is base64 encoded already, we need to
  153. // build the input string for the appropriate TSIGKey constructor.
  154. // If secret isn't a valid base64 value, the constructor will throw.
  155. std::ostringstream stream;
  156. stream << dns::Name(name_).toText() << ":"
  157. << secret_ << ":"
  158. << stringToAlgorithmName(algorithm_);
  159. if (digestbits_ > 0) {
  160. stream << ":" << digestbits_;
  161. }
  162. tsig_key_.reset(new dns::TSIGKey(stream.str()));
  163. } catch (const std::exception& ex) {
  164. isc_throw(D2CfgError, "Cannot make TSIGKey: " << ex.what());
  165. }
  166. }
  167. // *********************** DnsServerInfo *************************
  168. const char* DnsServerInfo::EMPTY_IP_STR = "0.0.0.0";
  169. DnsServerInfo::DnsServerInfo(const std::string& hostname,
  170. isc::asiolink::IOAddress ip_address, uint32_t port,
  171. bool enabled)
  172. :hostname_(hostname), ip_address_(ip_address), port_(port),
  173. enabled_(enabled) {
  174. }
  175. DnsServerInfo::~DnsServerInfo() {
  176. }
  177. std::string
  178. DnsServerInfo::toText() const {
  179. std::ostringstream stream;
  180. stream << (getIpAddress().toText()) << " port:" << getPort();
  181. return (stream.str());
  182. }
  183. std::ostream&
  184. operator<<(std::ostream& os, const DnsServerInfo& server) {
  185. os << server.toText();
  186. return (os);
  187. }
  188. // *********************** DdnsDomain *************************
  189. DdnsDomain::DdnsDomain(const std::string& name,
  190. DnsServerInfoStoragePtr servers,
  191. const TSIGKeyInfoPtr& tsig_key_info)
  192. : name_(name), servers_(servers),
  193. tsig_key_info_(tsig_key_info) {
  194. }
  195. DdnsDomain::~DdnsDomain() {
  196. }
  197. const std::string
  198. DdnsDomain::getKeyName() const {
  199. if (tsig_key_info_) {
  200. return (tsig_key_info_->getName());
  201. }
  202. return ("");
  203. }
  204. // *********************** DdnsDomainLstMgr *************************
  205. const char* DdnsDomainListMgr::wildcard_domain_name_ = "*";
  206. DdnsDomainListMgr::DdnsDomainListMgr(const std::string& name) : name_(name),
  207. domains_(new DdnsDomainMap()) {
  208. }
  209. DdnsDomainListMgr::~DdnsDomainListMgr () {
  210. }
  211. void
  212. DdnsDomainListMgr::setDomains(DdnsDomainMapPtr domains) {
  213. if (!domains) {
  214. isc_throw(D2CfgError,
  215. "DdnsDomainListMgr::setDomains: Domain list may not be null");
  216. }
  217. domains_ = domains;
  218. // Look for the wild card domain. If present, set the member variable
  219. // to remember it. This saves us from having to look for it every time
  220. // we attempt a match.
  221. DdnsDomainMap::iterator gotit = domains_->find(wildcard_domain_name_);
  222. if (gotit != domains_->end()) {
  223. wildcard_domain_ = gotit->second;
  224. }
  225. }
  226. bool
  227. DdnsDomainListMgr::matchDomain(const std::string& fqdn, DdnsDomainPtr& domain) {
  228. // First check the case of one domain to rule them all.
  229. if ((size() == 1) && (wildcard_domain_)) {
  230. domain = wildcard_domain_;
  231. return (true);
  232. }
  233. // Iterate over the domain map looking for the domain which matches
  234. // the longest portion of the given fqdn.
  235. size_t req_len = fqdn.size();
  236. size_t match_len = 0;
  237. DdnsDomainMapPair map_pair;
  238. DdnsDomainPtr best_match;
  239. BOOST_FOREACH (map_pair, *domains_) {
  240. std::string domain_name = map_pair.first;
  241. size_t dom_len = domain_name.size();
  242. // If the domain name is longer than the fqdn, then it cant be match.
  243. if (req_len < dom_len) {
  244. continue;
  245. }
  246. // If the lengths are identical and the names match we're done.
  247. if (req_len == dom_len) {
  248. if (boost::iequals(fqdn, domain_name)) {
  249. // exact match, done
  250. domain = map_pair.second;
  251. return (true);
  252. }
  253. } else {
  254. // The fqdn is longer than the domain name. Adjust the start
  255. // point of comparison by the excess in length. Only do the
  256. // comparison if the adjustment lands on a boundary. This
  257. // prevents "onetwo.net" from matching "two.net".
  258. size_t offset = req_len - dom_len;
  259. if ((fqdn[offset - 1] == '.') &&
  260. (boost::iequals(fqdn.substr(offset), domain_name))) {
  261. // Fqdn contains domain name, keep it if its better than
  262. // any we have matched so far.
  263. if (dom_len > match_len) {
  264. match_len = dom_len;
  265. best_match = map_pair.second;
  266. }
  267. }
  268. }
  269. }
  270. if (!best_match) {
  271. // There's no match. If they specified a wild card domain use it
  272. // otherwise there's no domain for this entry.
  273. if (wildcard_domain_) {
  274. domain = wildcard_domain_;
  275. return (true);
  276. }
  277. LOG_WARN(dhcp_to_d2_logger, DHCP_DDNS_NO_MATCH).arg(fqdn);
  278. return (false);
  279. }
  280. domain = best_match;
  281. return (true);
  282. }
  283. // *************************** PARSERS ***********************************
  284. // *********************** TSIGKeyInfoParser *************************
  285. TSIGKeyInfoParser::TSIGKeyInfoParser(const std::string& entry_name,
  286. TSIGKeyInfoMapPtr keys)
  287. : entry_name_(entry_name), keys_(keys), local_scalars_() {
  288. if (!keys_) {
  289. isc_throw(D2CfgError, "TSIGKeyInfoParser ctor:"
  290. " key storage cannot be null");
  291. }
  292. }
  293. TSIGKeyInfoParser::~TSIGKeyInfoParser() {
  294. }
  295. void
  296. TSIGKeyInfoParser::build(isc::data::ConstElementPtr key_config) {
  297. isc::dhcp::ConfigPair config_pair;
  298. // For each element in the key configuration:
  299. // 1. Create a parser for the element.
  300. // 2. Invoke the parser's build method passing in the element's
  301. // configuration.
  302. // 3. Invoke the parser's commit method to store the element's parsed
  303. // data to the parser's local storage.
  304. BOOST_FOREACH (config_pair, key_config->mapValue()) {
  305. isc::dhcp::ParserPtr parser(createConfigParser(config_pair.first,
  306. config_pair.second->
  307. getPosition()));
  308. parser->build(config_pair.second);
  309. parser->commit();
  310. }
  311. std::string name;
  312. std::string algorithm;
  313. uint32_t digestbits = 0;
  314. std::string secret;
  315. std::map<std::string, isc::data::Element::Position> pos;
  316. // Fetch the key's parsed scalar values from parser's local storage.
  317. // Only digestbits is optional and doesn't throw when missing
  318. try {
  319. pos["name"] = local_scalars_.getParam("name", name);
  320. pos["algorithm"] = local_scalars_.getParam("algorithm", algorithm);
  321. pos["digest-bits"] = local_scalars_.getParam("digest-bits", digestbits,
  322. DCfgContextBase::OPTIONAL);
  323. pos["secret"] = local_scalars_.getParam("secret", secret);
  324. } catch (const std::exception& ex) {
  325. isc_throw(D2CfgError, "TSIG Key incomplete : " << ex.what()
  326. << " (" << key_config->getPosition() << ")");
  327. }
  328. // Name cannot be blank.
  329. if (name.empty()) {
  330. isc_throw(D2CfgError, "TSIG key must specify name (" << pos["name"] << ")");
  331. }
  332. // Currently, the premise is that key storage is always empty prior to
  333. // parsing so we are always adding keys never replacing them. Duplicates
  334. // are not allowed and should be flagged as a configuration error.
  335. if (keys_->find(name) != keys_->end()) {
  336. isc_throw(D2CfgError, "Duplicate TSIG key name specified : " << name
  337. << " (" << pos["name"] << ")");
  338. }
  339. // Algorithm must be valid.
  340. try {
  341. TSIGKeyInfo::stringToAlgorithmName(algorithm);
  342. } catch (const std::exception& ex) {
  343. isc_throw(D2CfgError, "TSIG key : " << ex.what() << " (" << pos["algorithm"] << ")");
  344. }
  345. // Not zero Digestbits must be an integral number of octets, greater
  346. // than 80 and the half of the full length
  347. if (digestbits > 0) {
  348. if ((digestbits % 8) != 0) {
  349. isc_throw(D2CfgError, "Invalid TSIG key digest_bits specified : " <<
  350. digestbits << " (" << pos["digest-bits"] << ")");
  351. }
  352. if (digestbits < 80) {
  353. isc_throw(D2CfgError, "TSIG key digest_bits too small : " <<
  354. digestbits << " (" << pos["digest-bits"] << ")");
  355. }
  356. if (boost::iequals(algorithm, TSIGKeyInfo::HMAC_SHA224_STR)) {
  357. if (digestbits < 112) {
  358. isc_throw(D2CfgError, "TSIG key digest_bits too small : " <<
  359. digestbits << " (" << pos["digest-bits"]
  360. << ")");
  361. }
  362. } else if (boost::iequals(algorithm, TSIGKeyInfo::HMAC_SHA256_STR)) {
  363. if (digestbits < 128) {
  364. isc_throw(D2CfgError, "TSIG key digest_bits too small : " <<
  365. digestbits << " (" << pos["digest-bits"]
  366. << ")");
  367. }
  368. } else if (boost::iequals(algorithm, TSIGKeyInfo::HMAC_SHA384_STR)) {
  369. if (digestbits < 192) {
  370. isc_throw(D2CfgError, "TSIG key digest_bits too small : " <<
  371. digestbits << " (" << pos["digest-bits"]
  372. << ")");
  373. }
  374. } else if (boost::iequals(algorithm, TSIGKeyInfo::HMAC_SHA512_STR)) {
  375. if (digestbits < 256) {
  376. isc_throw(D2CfgError, "TSIG key digest_bits too small : " <<
  377. digestbits << " (" << pos["digest-bits"]
  378. << ")");
  379. }
  380. }
  381. }
  382. // Secret cannot be blank.
  383. // Cryptolink lib doesn't offer any way to validate these. As long as it
  384. // isn't blank we'll accept it. If the content is bad, the call to in
  385. // TSIGKeyInfo::remakeKey() made in the TSIGKeyInfo ctor will throw.
  386. // We'll deal with that below.
  387. if (secret.empty()) {
  388. isc_throw(D2CfgError, "TSIG key must specify secret (" << pos["secret"] << ")");
  389. }
  390. // Everything should be valid, so create the key instance.
  391. // It is possible for the asiodns::dns::TSIGKey create to fail such as
  392. // with an invalid secret content.
  393. TSIGKeyInfoPtr key_info;
  394. try {
  395. key_info.reset(new TSIGKeyInfo(name, algorithm, secret, digestbits));
  396. } catch (const std::exception& ex) {
  397. isc_throw(D2CfgError, ex.what() << " (" << key_config->getPosition() << ")");
  398. }
  399. // Add the new TSIGKeyInfo to the key storage.
  400. (*keys_)[name]=key_info;
  401. }
  402. isc::dhcp::ParserPtr
  403. TSIGKeyInfoParser::createConfigParser(const std::string& config_id,
  404. const isc::data::Element::Position& pos) {
  405. DhcpConfigParser* parser = NULL;
  406. // Based on the configuration id of the element, create the appropriate
  407. // parser. Scalars are set to use the parser's local scalar storage.
  408. if ((config_id == "name") ||
  409. (config_id == "algorithm") ||
  410. (config_id == "secret")) {
  411. parser = new isc::dhcp::StringParser(config_id,
  412. local_scalars_.getStringStorage());
  413. } else if (config_id == "digest-bits") {
  414. parser = new isc::dhcp::Uint32Parser(config_id,
  415. local_scalars_.getUint32Storage());
  416. } else {
  417. isc_throw(NotImplemented,
  418. "parser error: TSIGKeyInfo parameter not supported: "
  419. << config_id << " (" << pos << ")");
  420. }
  421. // Return the new parser instance.
  422. return (isc::dhcp::ParserPtr(parser));
  423. }
  424. void
  425. TSIGKeyInfoParser::commit() {
  426. }
  427. // *********************** TSIGKeyInfoListParser *************************
  428. TSIGKeyInfoListParser::TSIGKeyInfoListParser(const std::string& list_name,
  429. TSIGKeyInfoMapPtr keys)
  430. :list_name_(list_name), keys_(keys), local_keys_(new TSIGKeyInfoMap()),
  431. parsers_() {
  432. if (!keys_) {
  433. isc_throw(D2CfgError, "TSIGKeyInfoListParser ctor:"
  434. " key storage cannot be null");
  435. }
  436. }
  437. TSIGKeyInfoListParser::~TSIGKeyInfoListParser() {
  438. }
  439. void
  440. TSIGKeyInfoListParser::
  441. build(isc::data::ConstElementPtr key_list) {
  442. int i = 0;
  443. isc::data::ConstElementPtr key_config;
  444. // For each key element in the key list:
  445. // 1. Create a parser for the key element.
  446. // 2. Invoke the parser's build method passing in the key's
  447. // configuration.
  448. // 3. Add the parser to a local collection of parsers.
  449. BOOST_FOREACH(key_config, key_list->listValue()) {
  450. // Create a name for the parser based on its position in the list.
  451. std::string entry_name = boost::lexical_cast<std::string>(i++);
  452. isc::dhcp::ParserPtr parser(new TSIGKeyInfoParser(entry_name,
  453. local_keys_));
  454. parser->build(key_config);
  455. parsers_.push_back(parser);
  456. }
  457. // Now that we know we have a valid list, commit that list to the
  458. // area given to us during construction (i.e. to the d2 context).
  459. *keys_ = *local_keys_;
  460. }
  461. void
  462. TSIGKeyInfoListParser::commit() {
  463. // Invoke commit on each server parser. This will cause each one to
  464. // create it's server instance and commit it to storage.
  465. BOOST_FOREACH(isc::dhcp::ParserPtr parser, parsers_) {
  466. parser->commit();
  467. }
  468. }
  469. // *********************** DnsServerInfoParser *************************
  470. DnsServerInfoParser::DnsServerInfoParser(const std::string& entry_name,
  471. DnsServerInfoStoragePtr servers)
  472. : entry_name_(entry_name), servers_(servers), local_scalars_() {
  473. if (!servers_) {
  474. isc_throw(D2CfgError, "DnsServerInfoParser ctor:"
  475. " server storage cannot be null");
  476. }
  477. }
  478. DnsServerInfoParser::~DnsServerInfoParser() {
  479. }
  480. void
  481. DnsServerInfoParser::build(isc::data::ConstElementPtr server_config) {
  482. isc::dhcp::ConfigPair config_pair;
  483. // For each element in the server configuration:
  484. // 1. Create a parser for the element.
  485. // 2. Invoke the parser's build method passing in the element's
  486. // configuration.
  487. // 3. Invoke the parser's commit method to store the element's parsed
  488. // data to the parser's local storage.
  489. BOOST_FOREACH (config_pair, server_config->mapValue()) {
  490. isc::dhcp::ParserPtr parser(createConfigParser(config_pair.first,
  491. config_pair.second->
  492. getPosition()));
  493. parser->build(config_pair.second);
  494. parser->commit();
  495. }
  496. std::string hostname;
  497. std::string ip_address;
  498. uint32_t port = DnsServerInfo::STANDARD_DNS_PORT;
  499. std::map<std::string, isc::data::Element::Position> pos;
  500. // Fetch the server configuration's parsed scalar values from parser's
  501. // local storage. They're all optional, so no try-catch here.
  502. pos["hostname"] = local_scalars_.getParam("hostname", hostname,
  503. DCfgContextBase::OPTIONAL);
  504. pos["ip-address"] = local_scalars_.getParam("ip-address", ip_address,
  505. DCfgContextBase::OPTIONAL);
  506. pos["port"] = local_scalars_.getParam("port", port,
  507. DCfgContextBase::OPTIONAL);
  508. // The configuration must specify one or the other.
  509. if (hostname.empty() == ip_address.empty()) {
  510. isc_throw(D2CfgError, "Dns Server must specify one or the other"
  511. " of hostname or IP address"
  512. << " (" << server_config->getPosition() << ")");
  513. }
  514. // Port cannot be zero.
  515. if (port == 0) {
  516. isc_throw(D2CfgError, "Dns Server : port cannot be 0"
  517. << " (" << pos["port"] << ")");
  518. }
  519. DnsServerInfoPtr serverInfo;
  520. if (!hostname.empty()) {
  521. /// @todo when resolvable hostname is supported we create the entry
  522. /// as follows:
  523. ///
  524. /// @code
  525. /// // When hostname is specified, create a valid, blank IOAddress
  526. /// // and then create the DnsServerInfo.
  527. /// isc::asiolink::IOAddress io_addr(DnsServerInfo::EMPTY_IP_STR);
  528. /// serverInfo.reset(new DnsServerInfo(hostname, io_addr, port));
  529. ///
  530. /// @endcode
  531. ///
  532. /// Resolution will be done prior to connection during transaction
  533. /// processing.
  534. /// Until then we'll throw unsupported.
  535. isc_throw(D2CfgError, "Dns Server : hostname is not yet supported"
  536. << " (" << pos["hostname"] << ")");
  537. } else {
  538. try {
  539. // Create an IOAddress from the IP address string given and then
  540. // create the DnsServerInfo.
  541. isc::asiolink::IOAddress io_addr(ip_address);
  542. serverInfo.reset(new DnsServerInfo(hostname, io_addr, port));
  543. } catch (const isc::asiolink::IOError& ex) {
  544. isc_throw(D2CfgError, "Dns Server : invalid IP address : "
  545. << ip_address << " (" << pos["ip-address"] << ")");
  546. }
  547. }
  548. // Add the new DnsServerInfo to the server storage.
  549. servers_->push_back(serverInfo);
  550. }
  551. isc::dhcp::ParserPtr
  552. DnsServerInfoParser::createConfigParser(const std::string& config_id,
  553. const isc::data::Element::
  554. Position& pos) {
  555. DhcpConfigParser* parser = NULL;
  556. // Based on the configuration id of the element, create the appropriate
  557. // parser. Scalars are set to use the parser's local scalar storage.
  558. if ((config_id == "hostname") ||
  559. (config_id == "ip-address")) {
  560. parser = new isc::dhcp::StringParser(config_id,
  561. local_scalars_.getStringStorage());
  562. } else if (config_id == "port") {
  563. parser = new isc::dhcp::Uint32Parser(config_id,
  564. local_scalars_.getUint32Storage());
  565. } else {
  566. isc_throw(NotImplemented,
  567. "parser error: DnsServerInfo parameter not supported: "
  568. << config_id << " (" << pos << ")");
  569. }
  570. // Return the new parser instance.
  571. return (isc::dhcp::ParserPtr(parser));
  572. }
  573. void
  574. DnsServerInfoParser::commit() {
  575. }
  576. // *********************** DnsServerInfoListParser *************************
  577. DnsServerInfoListParser::DnsServerInfoListParser(const std::string& list_name,
  578. DnsServerInfoStoragePtr servers)
  579. :list_name_(list_name), servers_(servers), parsers_() {
  580. if (!servers_) {
  581. isc_throw(D2CfgError, "DdnsServerInfoListParser ctor:"
  582. " server storage cannot be null");
  583. }
  584. }
  585. DnsServerInfoListParser::~DnsServerInfoListParser(){
  586. }
  587. void
  588. DnsServerInfoListParser::
  589. build(isc::data::ConstElementPtr server_list){
  590. int i = 0;
  591. isc::data::ConstElementPtr server_config;
  592. // For each server element in the server list:
  593. // 1. Create a parser for the server element.
  594. // 2. Invoke the parser's build method passing in the server's
  595. // configuration.
  596. // 3. Add the parser to a local collection of parsers.
  597. BOOST_FOREACH(server_config, server_list->listValue()) {
  598. // Create a name for the parser based on its position in the list.
  599. std::string entry_name = boost::lexical_cast<std::string>(i++);
  600. isc::dhcp::ParserPtr parser(new DnsServerInfoParser(entry_name,
  601. servers_));
  602. parser->build(server_config);
  603. parsers_.push_back(parser);
  604. }
  605. // Domains must have at least one server.
  606. if (parsers_.size() == 0) {
  607. isc_throw (D2CfgError, "Server List must contain at least one server"
  608. << " (" << server_list->getPosition() << ")");
  609. }
  610. }
  611. void
  612. DnsServerInfoListParser::commit() {
  613. // Invoke commit on each server parser.
  614. BOOST_FOREACH(isc::dhcp::ParserPtr parser, parsers_) {
  615. parser->commit();
  616. }
  617. }
  618. // *********************** DdnsDomainParser *************************
  619. DdnsDomainParser::DdnsDomainParser(const std::string& entry_name,
  620. DdnsDomainMapPtr domains,
  621. TSIGKeyInfoMapPtr keys)
  622. : entry_name_(entry_name), domains_(domains), keys_(keys),
  623. local_servers_(new DnsServerInfoStorage()), local_scalars_() {
  624. if (!domains_) {
  625. isc_throw(D2CfgError,
  626. "DdnsDomainParser ctor, domain storage cannot be null");
  627. }
  628. }
  629. DdnsDomainParser::~DdnsDomainParser() {
  630. }
  631. void
  632. DdnsDomainParser::build(isc::data::ConstElementPtr domain_config) {
  633. // For each element in the domain configuration:
  634. // 1. Create a parser for the element.
  635. // 2. Invoke the parser's build method passing in the element's
  636. // configuration.
  637. // 3. Invoke the parser's commit method to store the element's parsed
  638. // data to the parser's local storage.
  639. isc::dhcp::ConfigPair config_pair;
  640. BOOST_FOREACH(config_pair, domain_config->mapValue()) {
  641. isc::dhcp::ParserPtr parser(createConfigParser(config_pair.first,
  642. config_pair.second->
  643. getPosition()));
  644. parser->build(config_pair.second);
  645. parser->commit();
  646. }
  647. // Now construct the domain.
  648. std::string name;
  649. std::string key_name;
  650. std::map<std::string, isc::data::Element::Position> pos;
  651. // Fetch the parsed scalar values from parser's local storage.
  652. // Any required that are missing will throw.
  653. try {
  654. pos["name"] = local_scalars_.getParam("name", name);
  655. pos["key-name"] = local_scalars_.getParam("key-name", key_name,
  656. DCfgContextBase::OPTIONAL);
  657. } catch (const std::exception& ex) {
  658. isc_throw(D2CfgError, "DdnsDomain incomplete : " << ex.what()
  659. << " (" << domain_config->getPosition() << ")");
  660. }
  661. // Blank domain names are not allowed.
  662. if (name.empty()) {
  663. isc_throw(D2CfgError, "DndsDomain : name cannot be blank ("
  664. << pos["name"] << ")");
  665. }
  666. // Currently, the premise is that domain storage is always empty
  667. // prior to parsing so always adding domains never replacing them.
  668. // Duplicates are not allowed and should be flagged as a configuration
  669. // error.
  670. if (domains_->find(name) != domains_->end()) {
  671. isc_throw(D2CfgError, "Duplicate domain specified:" << name
  672. << " (" << pos["name"] << ")");
  673. }
  674. // Key name is optional. If it is not blank, then find the key in the
  675. /// list of defined keys.
  676. TSIGKeyInfoPtr tsig_key_info;
  677. if (!key_name.empty()) {
  678. if (keys_) {
  679. TSIGKeyInfoMap::iterator kit = keys_->find(key_name);
  680. if (kit != keys_->end()) {
  681. tsig_key_info = kit->second;
  682. }
  683. }
  684. if (!tsig_key_info) {
  685. isc_throw(D2CfgError, "DdnsDomain : " << name
  686. << " specifies an undefined key: " << key_name
  687. << " (" << pos["key-name"] << ")");
  688. }
  689. }
  690. // Instantiate the new domain and add it to domain storage.
  691. DdnsDomainPtr domain(new DdnsDomain(name, local_servers_, tsig_key_info));
  692. // Add the new domain to the domain storage.
  693. (*domains_)[name] = domain;
  694. }
  695. isc::dhcp::ParserPtr
  696. DdnsDomainParser::createConfigParser(const std::string& config_id,
  697. const isc::data::Element::Position& pos) {
  698. DhcpConfigParser* parser = NULL;
  699. // Based on the configuration id of the element, create the appropriate
  700. // parser. Scalars are set to use the parser's local scalar storage.
  701. if ((config_id == "name") ||
  702. (config_id == "key-name")) {
  703. parser = new isc::dhcp::StringParser(config_id,
  704. local_scalars_.getStringStorage());
  705. } else if (config_id == "dns-servers") {
  706. // Server list parser is given in our local server storage. It will pass
  707. // this down to its server parsers and is where they will write their
  708. // server instances upon commit.
  709. parser = new DnsServerInfoListParser(config_id, local_servers_);
  710. } else {
  711. isc_throw(NotImplemented,
  712. "parser error: DdnsDomain parameter not supported: "
  713. << config_id << " (" << pos << ")");
  714. }
  715. // Return the new domain parser instance.
  716. return (isc::dhcp::ParserPtr(parser));
  717. }
  718. void
  719. DdnsDomainParser::commit() {
  720. }
  721. // *********************** DdnsDomainListParser *************************
  722. DdnsDomainListParser::DdnsDomainListParser(const std::string& list_name,
  723. DdnsDomainMapPtr domains,
  724. TSIGKeyInfoMapPtr keys)
  725. :list_name_(list_name), domains_(domains), keys_(keys), parsers_() {
  726. if (!domains_) {
  727. isc_throw(D2CfgError, "DdnsDomainListParser ctor:"
  728. " domain storage cannot be null");
  729. }
  730. }
  731. DdnsDomainListParser::~DdnsDomainListParser(){
  732. }
  733. void
  734. DdnsDomainListParser::
  735. build(isc::data::ConstElementPtr domain_list){
  736. // For each domain element in the domain list:
  737. // 1. Create a parser for the domain element.
  738. // 2. Invoke the parser's build method passing in the domain's
  739. // configuration.
  740. // 3. Add the parser to the local collection of parsers.
  741. int i = 0;
  742. isc::data::ConstElementPtr domain_config;
  743. BOOST_FOREACH(domain_config, domain_list->listValue()) {
  744. std::string entry_name = boost::lexical_cast<std::string>(i++);
  745. isc::dhcp::ParserPtr parser(new DdnsDomainParser(entry_name,
  746. domains_, keys_));
  747. parser->build(domain_config);
  748. parsers_.push_back(parser);
  749. }
  750. }
  751. void
  752. DdnsDomainListParser::commit() {
  753. // Invoke commit on each server parser. This will cause each one to
  754. // create it's server instance and commit it to storage.
  755. BOOST_FOREACH(isc::dhcp::ParserPtr parser, parsers_) {
  756. parser->commit();
  757. }
  758. }
  759. // *********************** DdnsDomainListMgrParser *************************
  760. DdnsDomainListMgrParser::DdnsDomainListMgrParser(const std::string& entry_name,
  761. DdnsDomainListMgrPtr mgr, TSIGKeyInfoMapPtr keys)
  762. : entry_name_(entry_name), mgr_(mgr), keys_(keys),
  763. local_domains_(new DdnsDomainMap()), local_scalars_() {
  764. }
  765. DdnsDomainListMgrParser::~DdnsDomainListMgrParser() {
  766. }
  767. void
  768. DdnsDomainListMgrParser::build(isc::data::ConstElementPtr domain_config) {
  769. // For each element in the domain manager configuration:
  770. // 1. Create a parser for the element.
  771. // 2. Invoke the parser's build method passing in the element's
  772. // configuration.
  773. // 3. Invoke the parser's commit method to store the element's parsed
  774. // data to the parser's local storage.
  775. isc::dhcp::ConfigPair config_pair;
  776. BOOST_FOREACH(config_pair, domain_config->mapValue()) {
  777. isc::dhcp::ParserPtr parser(createConfigParser(config_pair.first,
  778. config_pair.second->
  779. getPosition()));
  780. parser->build(config_pair.second);
  781. parser->commit();
  782. }
  783. // Add the new domain to the domain storage.
  784. mgr_->setDomains(local_domains_);
  785. }
  786. isc::dhcp::ParserPtr
  787. DdnsDomainListMgrParser::createConfigParser(const std::string& config_id,
  788. const isc::data::Element::
  789. Position& pos) {
  790. DhcpConfigParser* parser = NULL;
  791. if (config_id == "ddns-domains") {
  792. // Domain list parser is given our local domain storage. It will pass
  793. // this down to its domain parsers and is where they will write their
  794. // domain instances upon commit.
  795. parser = new DdnsDomainListParser(config_id, local_domains_, keys_);
  796. } else {
  797. isc_throw(NotImplemented, "parser error: "
  798. "DdnsDomainListMgr parameter not supported: " << config_id
  799. << " (" << pos << ")");
  800. }
  801. // Return the new domain parser instance.
  802. return (isc::dhcp::ParserPtr(parser));
  803. }
  804. void
  805. DdnsDomainListMgrParser::commit() {
  806. }
  807. }; // end of isc::dhcp namespace
  808. }; // end of isc namespace