Kea 2.7.4
d2_cfg_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2024 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
25namespace {
26
27typedef std::vector<uint8_t> ByteAddress;
28
29} // end of unnamed namespace
30
31// *********************** D2CfgContext *************************
32
34 : d2_params_(new D2Params()),
35 forward_mgr_(new DdnsDomainListMgr("forward-ddns")),
36 reverse_mgr_(new DdnsDomainListMgr("reverse-ddns")),
37 keys_(new TSIGKeyInfoMap()),
38 unix_control_socket_(ConstElementPtr()),
39 http_control_socket_(HttpCommandConfigPtr()) {
40}
41
43 d2_params_ = rhs.d2_params_;
44 if (rhs.forward_mgr_) {
45 forward_mgr_.reset(new DdnsDomainListMgr(rhs.forward_mgr_->getName()));
46 forward_mgr_->setDomains(rhs.forward_mgr_->getDomains());
47 }
48
49 if (rhs.reverse_mgr_) {
50 reverse_mgr_.reset(new DdnsDomainListMgr(rhs.reverse_mgr_->getName()));
51 reverse_mgr_->setDomains(rhs.reverse_mgr_->getDomains());
52 }
53
54 keys_ = rhs.keys_;
55
56 unix_control_socket_ = rhs.unix_control_socket_;
57
58 http_control_socket_ = rhs.http_control_socket_;
59
60 hooks_config_ = rhs.hooks_config_;
61}
62
65
69 // Set user-context
71 // Set ip-address
72 const IOAddress& ip_address = d2_params_->getIpAddress();
73 d2->set("ip-address", Element::create(ip_address.toText()));
74 // Set port
75 size_t port = d2_params_->getPort();
76 d2->set("port", Element::create(static_cast<int64_t>(port)));
77 // Set dns-server-timeout
78 size_t dns_server_timeout = d2_params_->getDnsServerTimeout();
79 d2->set("dns-server-timeout",
80 Element::create(static_cast<int64_t>(dns_server_timeout)));
81 // Set ncr-protocol
82 const dhcp_ddns::NameChangeProtocol& ncr_protocol =
83 d2_params_->getNcrProtocol();
84 d2->set("ncr-protocol",
86 // Set ncr-format
87 const dhcp_ddns::NameChangeFormat& ncr_format = d2_params_->getNcrFormat();
88 d2->set("ncr-format",
90 // Set forward-ddns
91 ElementPtr forward_ddns = Element::createMap();
92 forward_ddns->set("ddns-domains", forward_mgr_->toElement());
93 d2->set("forward-ddns", forward_ddns);
94 // Set reverse-ddns
95 ElementPtr reverse_ddns = Element::createMap();
96 reverse_ddns->set("ddns-domains", reverse_mgr_->toElement());
97 d2->set("reverse-ddns", reverse_ddns);
98 // Set tsig-keys
99 ElementPtr tsig_keys = Element::createList();
100 for (auto const& key : *keys_) {
101 tsig_keys->add(key.second->toElement());
102 }
103 d2->set("tsig-keys", tsig_keys);
104 // Set control-sockets.
105 ElementPtr control_sockets = Element::createList();
106 if (!isNull(unix_control_socket_)) {
107 control_sockets->add(UserContext::toElement(unix_control_socket_));
108 }
109 if (http_control_socket_) {
110 control_sockets->add(http_control_socket_->toElement());
111 }
112 if (!control_sockets->empty()) {
113 d2->set("control-sockets", control_sockets);
114 }
115 // Set hooks-libraries
116 d2->set("hooks-libraries", hooks_config_.toElement());
117 // Set DhcpDdns
119 result->set("DhcpDdns", d2);
120
121 return (result);
122}
123
124// *********************** D2CfgMgr *************************
125
126const char* D2CfgMgr::IPV4_REV_ZONE_SUFFIX = "in-addr.arpa.";
127
128const char* D2CfgMgr::IPV6_REV_ZONE_SUFFIX = "ip6.arpa.";
129
132
135
140
141bool
143 // Forward updates are not enabled if no forward servers are defined.
144 return (getD2CfgContext()->getForwardMgr()->size() > 0);
145}
146
147bool
149 // Reverse updates are not enabled if no reverse servers are defined.
150 return (getD2CfgContext()->getReverseMgr()->size() > 0);
151}
152
153bool
154D2CfgMgr::matchForward(const std::string& fqdn, DdnsDomainPtr& domain) {
155 if (fqdn.empty()) {
156 // This is a programmatic error and should not happen.
157 isc_throw(D2CfgError, "matchForward passed an empty fqdn");
158 }
159
160 // Fetch the forward manager from the D2 context.
161 DdnsDomainListMgrPtr mgr = getD2CfgContext()->getForwardMgr();
162
163 // Call the manager's match method and return the result.
164 return (mgr->matchDomain(fqdn, domain));
165}
166
167bool
168D2CfgMgr::matchReverse(const std::string& ip_address, DdnsDomainPtr& domain) {
169 // Note, reverseIpAddress will throw if the ip_address is invalid.
170 std::string reverse_address = reverseIpAddress(ip_address);
171
172 // Fetch the reverse manager from the D2 context.
173 DdnsDomainListMgrPtr mgr = getD2CfgContext()->getReverseMgr();
174
175 return (mgr->matchDomain(reverse_address, domain));
176}
177
178std::string
179D2CfgMgr::reverseIpAddress(const std::string& address) {
180 try {
181 // Convert string address into an IOAddress and invoke the
182 // appropriate reverse method.
183 isc::asiolink::IOAddress ioaddr(address);
184 if (ioaddr.isV4()) {
185 return (reverseV4Address(ioaddr));
186 }
187
188 return (reverseV6Address(ioaddr));
189
190 } catch (const isc::Exception& ex) {
191 isc_throw(D2CfgError, "D2CfgMgr cannot reverse address: "
192 << address << " : " << ex.what());
193 }
194}
195
196std::string
198 if (!ioaddr.isV4()) {
199 isc_throw(D2CfgError, "D2CfgMgr address is not IPv4 address :"
200 << ioaddr);
201 }
202
203 // Get the address in byte vector form.
204 const ByteAddress bytes = ioaddr.toBytes();
205
206 // Walk backwards through vector outputting each octet and a dot.
207 std::ostringstream stream;
208
209 for (auto const& rit : boost::adaptors::reverse(bytes)) {
210 stream << static_cast<unsigned int>(rit) << ".";
211 }
212
213 // Tack on the suffix and we're done.
214 stream << IPV4_REV_ZONE_SUFFIX;
215 return(stream.str());
216}
217
218std::string
220 if (!ioaddr.isV6()) {
221 isc_throw(D2CfgError, "D2Cfg address is not IPv6 address: " << ioaddr);
222 }
223
224 // Turn the address into a string of digits.
225 const ByteAddress bytes = ioaddr.toBytes();
226 const std::string digits = isc::util::encode::encodeHex(bytes);
227
228 // Walk backwards through string outputting each digits and a dot.
229 std::ostringstream stream;
230
231 for (auto const& rit : boost::adaptors::reverse(digits)) {
232 stream << static_cast<char>(rit) << ".";
233 }
234
235 // Tack on the suffix and we're done.
236 stream << IPV6_REV_ZONE_SUFFIX;
237 return(stream.str());
238}
239
240const D2ParamsPtr&
244
249
254
255std::string
257 return (getD2Params()->getConfigSummary());
258}
259
260void
262 D2SimpleParser::setAllDefaults(mutable_config);
263}
264
266D2CfgMgr::parse(isc::data::ConstElementPtr config_set, bool check_only) {
267 // Do a sanity check first.
268 if (!config_set) {
269 isc_throw(D2CfgError, "Mandatory config parameter not provided");
270 }
271
273
274 // Set the defaults
275 ElementPtr cfg = boost::const_pointer_cast<Element>(config_set);
277
278 // And parse the configuration.
279 ConstElementPtr answer;
280 std::string excuse;
281 try {
282 // Do the actual parsing
283 D2SimpleParser parser;
284 parser.parse(ctx, cfg, check_only);
285 } catch (const isc::Exception& ex) {
286 excuse = ex.what();
287 answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
288 } catch (...) {
289 excuse = "undefined configuration parsing error";
290 answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
291 }
292
293 // At this stage the answer was created only in case of exception.
294 if (answer) {
295 if (check_only) {
297 } else {
299 }
300 return (answer);
301 }
302
303 if (check_only) {
305 "Configuration check successful");
306 } else {
307
308 // Calculate hash of the configuration that was just set.
309 ConstElementPtr config = getContext()->toElement();
310 std::string hash = BaseCommandMgr::getHash(config);
312 params->set("hash", Element::create(hash));
313
315 "Configuration applied successfully.", params);
316 }
317
318 return (answer);
319}
320
321std::list<std::list<std::string>>
323 static std::list<std::list<std::string>> const list({
324 {"tsig-keys", "[]"},
325 {"hooks-libraries", "[]", "parameters", "*"},
326 });
327 return list;
328}
329
330} // namespace d2
331} // namespace isc
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:35
virtual ~D2CfgContext()
Destructor.
Definition d2_cfg_mgr.cc:63
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition d2_cfg_mgr.cc:67
D2CfgContext()
Constructor.
Definition d2_cfg_mgr.cc:33
Exception thrown when the error during configuration handling occurs.
Definition d2_config.h:136
virtual ~D2CfgMgr()
Destructor.
bool matchForward(const std::string &fqdn, DdnsDomainPtr &domain)
Matches a given FQDN to a forward domain.
virtual process::ConfigPtr createNewContext() override
Creates an new, blank D2CfgContext context.
static std::string reverseIpAddress(const std::string &address)
Generate a reverse order string for the given IP address.
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:204
bool reverseUpdatesEnabled()
Returns whether or not reverse updates are enabled.
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.
static const char * IPV6_REV_ZONE_SUFFIX
Reverse zone suffix added to IPv6 addresses for reverse lookups.
Definition d2_cfg_mgr.h:193
static std::string reverseV4Address(const isc::asiolink::IOAddress &ioaddr)
Generate a reverse order string for the given IP address.
bool forwardUpdatesEnabled()
Returns whether or not forward updates are enabled.
bool matchReverse(const std::string &ip_address, DdnsDomainPtr &domain)
Matches a given IP address to a reverse domain.
virtual std::string getConfigSummary(const uint32_t selection) override
Returns configuration summary in the textual format.
static std::string reverseV6Address(const isc::asiolink::IOAddress &ioaddr)
Generate a reverse order string for the given IP address.
isc::config::HttpCommandConfigPtr getHttpControlSocketInfo()
Convenience method fetches information about HTTP/HTTPS control socket from context.
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.
static const char * IPV4_REV_ZONE_SUFFIX
Reverse zone suffix added to IPv4 addresses for reverse lookups.
Definition d2_cfg_mgr.h:188
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
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
isc::data::ElementPtr toElement() const
Unparse a configuration object.
Base class for all configurations.
Definition config_base.h:33
virtual isc::data::ElementPtr toElement() const
Converts to Element representation.
Configuration Manager.
Definition d_cfg_mgr.h:108
ConfigPtr & getContext()
Fetches the configuration context.
Definition d_cfg_mgr.h:151
This file contains several functions and constants that are used for handling commands and responses ...
#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.
boost::shared_ptr< HttpCommandConfig > HttpCommandConfigPtr
Pointer to a HttpCommandConfig object.
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:175
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
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:26
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:29
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a NULL pointer.
Definition data.cc:1148
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
NameChangeFormat
Defines the list of data wire formats supported.
Definition ncr_msg.h:59
NameChangeProtocol
Defines the list of socket protocols supported.
Definition ncr_io.h:69
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:35
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.