Kea 3.3.4
d2_cfg_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2026 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
7#include <config.h>
8
9#include <d2srv/d2_log.h>
10#include <d2srv/d2_cfg_mgr.h>
14#include <util/encode/encode.h>
15#include <boost/range/adaptor/reversed.hpp>
16
17using namespace isc::asiolink;
18using namespace isc::config;
19using namespace isc::data;
20using namespace isc::process;
21
22namespace isc {
23namespace d2 {
24
25const std::string DDNS_REV_ZONE_DEFAULT4 = "in-addr.arpa.";
26const std::string DDNS_REV_ZONE_DEFAULT6 = "ip6.arpa.";
27
28namespace {
29
30typedef std::vector<uint8_t> ByteAddress;
31
32} // end of unnamed namespace
33
34// *********************** D2CfgContext *************************
35
37 : d2_params_(new D2Params()),
38 forward_mgr_(new DdnsDomainListMgr("forward-ddns")),
39 reverse_mgr_(new DdnsDomainListMgr("reverse-ddns")),
40 keys_(new TSIGKeyInfoMap()),
41 unix_control_socket_(ConstElementPtr()),
42 http_control_socket_(ConstElementPtr()) {
43}
44
46 d2_params_ = rhs.d2_params_;
47 if (rhs.forward_mgr_) {
48 forward_mgr_.reset(new DdnsDomainListMgr(rhs.forward_mgr_->getName()));
49 forward_mgr_->setDomains(rhs.forward_mgr_->getDomains());
50 }
51
52 if (rhs.reverse_mgr_) {
53 reverse_mgr_.reset(new DdnsDomainListMgr(rhs.reverse_mgr_->getName()));
54 reverse_mgr_->setDomains(rhs.reverse_mgr_->getDomains());
55 }
56
57 keys_ = rhs.keys_;
58
59 unix_control_socket_ = rhs.unix_control_socket_;
60
61 http_control_socket_ = rhs.http_control_socket_;
62
63 hooks_config_ = rhs.hooks_config_;
64}
65
68
72 // Set user-context
74 // Set ip-address
75 const IOAddress& ip_address = d2_params_->getIpAddress();
76 d2->set("ip-address", Element::create(ip_address.toText()));
77 // Set port
78 size_t port = d2_params_->getPort();
79 d2->set("port", Element::create(static_cast<int64_t>(port)));
80 // Set dns-server-timeout
81 size_t dns_server_timeout = d2_params_->getDnsServerTimeout();
82 d2->set("dns-server-timeout",
83 Element::create(static_cast<int64_t>(dns_server_timeout)));
84 // Set ncr-protocol
85 const dhcp_ddns::NameChangeProtocol& ncr_protocol =
86 d2_params_->getNcrProtocol();
87 d2->set("ncr-protocol",
89 // Set ncr-format
90 const dhcp_ddns::NameChangeFormat& ncr_format = d2_params_->getNcrFormat();
91 d2->set("ncr-format",
93 // Set forward-ddns
94 ElementPtr forward_ddns = Element::createMap();
95 forward_ddns->set("ddns-domains", forward_mgr_->toElement());
96 d2->set("forward-ddns", forward_ddns);
97 // Set reverse-ddns
98 ElementPtr reverse_ddns = Element::createMap();
99 reverse_ddns->set("ddns-domains", reverse_mgr_->toElement());
100 d2->set("reverse-ddns", reverse_ddns);
101 // Set tsig-keys
102 ElementPtr tsig_keys = Element::createList();
103 for (auto const& key : *keys_) {
104 tsig_keys->add(key.second->toElement());
105 }
106 d2->set("tsig-keys", tsig_keys);
107 // Set control-sockets.
108 ElementPtr control_sockets = Element::createList();
109 if (!isNull(unix_control_socket_)) {
110 for (auto const& socket : unix_control_socket_->listValue()) {
111 control_sockets->add(UserContext::toElement(socket));
112 }
113 }
114 if (!isNull(http_control_socket_)) {
115 for (auto const& socket : http_control_socket_->listValue()) {
116 control_sockets->add(UserContext::toElement(socket));
117 }
118 }
119 if (!control_sockets->empty()) {
120 d2->set("control-sockets", control_sockets);
121 }
122 // Set hooks-libraries
123 d2->set("hooks-libraries", hooks_config_.toElement());
124 // Set DhcpDdns
126 result->set("DhcpDdns", d2);
127
128 return (result);
129}
130
131// *********************** D2CfgMgr *************************
132
135
138
143
144bool
146 // Forward updates are not enabled if no forward servers are defined.
147 return (getD2CfgContext()->getForwardMgr()->size() > 0);
148}
149
150bool
152 // Reverse updates are not enabled if no reverse servers are defined.
153 return (getD2CfgContext()->getReverseMgr()->size() > 0);
154}
155
156bool
157D2CfgMgr::matchForward(const std::string& fqdn, DdnsDomainPtr& domain) {
158 if (fqdn.empty()) {
159 // This is a programmatic error and should not happen.
160 isc_throw(D2CfgError, "matchForward passed an empty fqdn");
161 }
162
163 // Fetch the forward manager from the D2 context.
164 DdnsDomainListMgrPtr mgr = getD2CfgContext()->getForwardMgr();
165
166 // Call the manager's match method and return the result.
167 return (mgr->matchDomain(fqdn, domain));
168}
169
170bool
171D2CfgMgr::matchReverse(const std::string& ip_address, DdnsDomainPtr& domain,
172 const std::string& reverse_zone /* = "" */) {
173 // Note, reverseIpAddress will throw if the ip_address is invalid.
174 std::string reverse_address = reverseIpAddress(ip_address, reverse_zone);
175
176 // Fetch the reverse manager from the D2 context.
177 DdnsDomainListMgrPtr mgr = getD2CfgContext()->getReverseMgr();
178
179 return (mgr->matchDomain(reverse_address, domain));
180}
181
182std::string
183D2CfgMgr::reverseIpAddress(const std::string& address,
184 const std::string& reverse_zone /* = "" */) {
185 try {
186 // Convert string address into an IOAddress and invoke the
187 // appropriate reverse method.
188 isc::asiolink::IOAddress ioaddr(address);
189 if (ioaddr.isV4()) {
190 return (reverseV4Address(ioaddr, reverse_zone));
191 }
192
193 return (reverseV6Address(ioaddr, reverse_zone));
194
195 } catch (const isc::Exception& ex) {
196 isc_throw(D2CfgError, "D2CfgMgr cannot reverse address: "
197 << address << " : " << ex.what());
198 }
199}
200
201std::string
202D2CfgMgr::reverseV4Address(const isc::asiolink::IOAddress& ioaddr, const std::string& reverse_zone) {
203 if (!ioaddr.isV4()) {
204 isc_throw(D2CfgError, "D2CfgMgr address is not IPv4 address :"
205 << ioaddr);
206 }
207
208 // Get the address in byte vector form.
209 const ByteAddress bytes = ioaddr.toBytes();
210
211 // Walk backwards through vector outputting each octet and a dot.
212 std::ostringstream stream;
213
214 for (auto const& rit : boost::adaptors::reverse(bytes)) {
215 stream << static_cast<unsigned int>(rit) << ".";
216 }
217
218 // Tack on the suffix and we're done.
219 if (reverse_zone.empty()) {
220 stream << DDNS_REV_ZONE_DEFAULT4;
221 } else {
222 stream << reverse_zone;
223 if (reverse_zone.back() != '.') {
224 stream << ".";
225 }
226 }
227
228 return(stream.str());
229}
230
231std::string
232D2CfgMgr::reverseV6Address(const isc::asiolink::IOAddress& ioaddr, const std::string& reverse_zone) {
233 if (!ioaddr.isV6()) {
234 isc_throw(D2CfgError, "D2Cfg address is not IPv6 address: " << ioaddr);
235 }
236
237 // Turn the address into a string of digits.
238 const ByteAddress bytes = ioaddr.toBytes();
239 const std::string digits = isc::util::encode::encodeHex(bytes);
240
241 // Walk backwards through string outputting each digits and a dot.
242 std::ostringstream stream;
243
244 for (auto const& rit : boost::adaptors::reverse(digits)) {
245 stream << static_cast<char>(rit) << ".";
246 }
247
248 // Tack on the suffix and we're done.
249 if (reverse_zone.empty()) {
250 stream << DDNS_REV_ZONE_DEFAULT6;
251 } else {
252 stream << reverse_zone;
253 if (reverse_zone.back() != '.') {
254 stream << ".";
255 }
256 }
257
258 return(stream.str());
259}
260
261const D2ParamsPtr&
265
270
275
276std::string
278 return (getD2Params()->getConfigSummary());
279}
280
281void
283 D2SimpleParser::setAllDefaults(mutable_config);
284}
285
287D2CfgMgr::parse(isc::data::ConstElementPtr config_set, bool check_only) {
288 // Do a sanity check first.
289 if (!config_set) {
290 isc_throw(D2CfgError, "Mandatory config parameter not provided");
291 }
292
294
295 // Set the defaults
296 ElementPtr cfg = boost::const_pointer_cast<Element>(config_set);
298
299 // And parse the configuration.
300 ConstElementPtr answer;
301 std::string excuse;
302 try {
303 // Do the actual parsing
304 D2SimpleParser parser;
305 parser.parse(ctx, cfg, check_only);
306 } catch (const isc::Exception& ex) {
307 excuse = ex.what();
308 answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
309 } catch (...) {
310 excuse = "undefined configuration parsing error";
311 answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
312 }
313
314 // At this stage the answer was created only in case of exception.
315 if (answer) {
316 if (check_only) {
318 } else {
320 }
321 return (answer);
322 }
323
324 if (check_only) {
326 "Configuration check successful");
327 } else {
328
329 // Calculate hash of the configuration that was just set.
330 ConstElementPtr config = getContext()->toElement();
331 std::string hash = BaseCommandMgr::getHash(config);
333 params->set("hash", Element::create(hash));
334
336 "Configuration applied successfully.", params);
337 }
338
339 return (answer);
340}
341
342std::list<std::list<std::string>>
344 static std::list<std::list<std::string>> const list({
345 {"tsig-keys", "[]"},
346 {"hooks-libraries", "[]", "parameters", "*"},
347 });
348 return list;
349}
350
351} // namespace d2
352} // namespace isc
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:300
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:355
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:350
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static std::string getHash(const isc::data::ConstElementPtr &config)
returns a hash of a given Element structure
DHCP-DDNS Configuration Context.
Definition d2_cfg_mgr.h:41
virtual ~D2CfgContext()
Destructor.
Definition d2_cfg_mgr.cc:66
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition d2_cfg_mgr.cc:70
D2CfgContext()
Constructor.
Definition d2_cfg_mgr.cc:36
Exception thrown when the error during configuration handling occurs.
Definition d2_config.h:136
static std::string reverseV4Address(const isc::asiolink::IOAddress &ioaddr, const std::string &reverse_zone)
Generate a reverse order string for the given IP address.
virtual ~D2CfgMgr()
Destructor.
static std::string reverseV6Address(const isc::asiolink::IOAddress &ioaddr, const std::string &reverse_zone)
Generate a reverse order string for the given IP address.
bool matchForward(const std::string &fqdn, DdnsDomainPtr &domain)
Matches a given FQDN to a forward domain.
const isc::data::ConstElementPtr getHttpControlSocketInfo()
Convenience method fetches information about HTTP/HTTPS control socket from context.
virtual process::ConfigPtr createNewContext() override
Creates an new, blank D2CfgContext context.
const isc::data::ConstElementPtr getUnixControlSocketInfo()
Convenience method fetches information about UNIX control socket from context.
D2CfgContextPtr getD2CfgContext()
Convenience method that returns the D2 configuration context.
Definition d2_cfg_mgr.h:210
bool reverseUpdatesEnabled()
Returns whether or not reverse updates are enabled.
bool matchReverse(const std::string &ip_address, DdnsDomainPtr &domain, const std::string &reverse_zone="")
Matches a given IP address to a reverse domain.
virtual void setCfgDefaults(isc::data::ElementPtr mutable_config) override
Adds default values to the given config.
std::list< std::list< std::string > > jsonPathsToRedact() const final override
Return a list of all paths that contain passwords or secrets.
D2CfgMgr()
Constructor.
bool forwardUpdatesEnabled()
Returns whether or not forward updates are enabled.
virtual std::string getConfigSummary(const uint32_t selection) override
Returns configuration summary in the textual format.
static std::string reverseIpAddress(const std::string &address, const std::string &reverse_zone="")
Generate a reverse order string for the given IP address.
const D2ParamsPtr & getD2Params()
Convenience method fetches the D2Params from context.
virtual isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only) override
Parses configuration of the D2.
Acts as a storage vault for D2 global scalar parameters.
Definition d2_config.h:143
void parse(const D2CfgContextPtr &ctx, const isc::data::ConstElementPtr &config, bool check_only)
Parses the whole D2 configuration.
static size_t setAllDefaults(data::ElementPtr global)
Sets all defaults for D2 configuration.
Provides storage for and management of a list of DNS domains.
Definition d2_config.h:646
Base class for all configurations.
Definition config_base.h:33
virtual isc::data::ElementPtr toElement() const
Converts to Element representation.
DCfgMgrBase(ConfigPtr context)
Constructor.
Definition d_cfg_mgr.cc:37
ConfigPtr & getContext()
Fetches the configuration context.
Definition d_cfg_mgr.h:148
This file contains several functions and constants that are used for handling commands and responses ...
if(!(yy_init))
Definition d2_lexer.cc:1517
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
boost::shared_ptr< DdnsDomainListMgr > DdnsDomainListMgrPtr
Defines a pointer for DdnsDomain instances.
Definition d2_cfg_mgr.h:181
const std::string DDNS_REV_ZONE_DEFAULT6
Default reverse DNS domain for V6.
Definition d2_cfg_mgr.cc:26
boost::shared_ptr< DdnsDomain > DdnsDomainPtr
Defines a pointer for DdnsDomain instances.
Definition d2_config.h:624
const isc::log::MessageID DHCP_DDNS_CONFIG_CHECK_FAIL
Definition d2_messages.h:17
const std::string DDNS_REV_ZONE_DEFAULT4
Default reverse DNS domain for V4.
Definition d2_cfg_mgr.cc:25
std::map< std::string, TSIGKeyInfoPtr > TSIGKeyInfoMap
Defines a map of TSIGKeyInfos, keyed by the name.
Definition d2_config.h:419
boost::shared_ptr< D2CfgContext > D2CfgContextPtr
Pointer to a configuration context.
Definition d2_cfg_mgr.h:32
isc::log::Logger d2_logger("dhcpddns")
Defines the logger used within D2.
Definition d2_log.h:18
boost::shared_ptr< D2Params > D2ParamsPtr
Defines a pointer for D2Params instances.
Definition d2_config.h:257
const isc::log::MessageID DHCP_DDNS_CONFIG_FAIL
Definition d2_messages.h:18
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a null pointer.
Definition data.cc:1231
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
NameChangeProtocol
Defines the list of socket protocols supported.
Definition ncr_io.h:70
NameChangeFormat
Defines the list of data wire formats supported.
Definition ncr_msg.h:63
std::string ncrProtocolToString(NameChangeProtocol protocol)
Function which converts NameChangeProtocol enums to text labels.
Definition ncr_io.cc:36
std::string ncrFormatToString(NameChangeFormat format)
Function which converts NameChangeFormat enums to text labels.
Definition ncr_msg.cc:32
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Definition encode.cc:361
Defines the logger used by the top-level component of kea-lfc.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
static data::ElementPtr toElement(data::ConstElementPtr map)
Copy an Element map.