Kea  1.5.0
lease_parser.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-2018 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 <cc/data.h>
8 #include <dhcp/hwaddr.h>
9 #include <asiolink/io_address.h>
10 #include <dhcpsrv/lease.h>
11 #include <dhcpsrv/cfgmgr.h>
13 #include <lease_parser.h>
14 
15 #include <config.h>
16 
17 using namespace std;
18 using namespace isc::dhcp;
19 using namespace isc::data;
20 using namespace isc::asiolink;
21 
22 namespace isc {
23 namespace lease_cmds {
24 
26 Lease4Parser::parse(ConstSrvConfigPtr& cfg,
27  const ConstElementPtr& lease_info,
28  bool& force_create) {
29  if (!lease_info) {
30  isc_throw(BadValue, "lease information missing");
31  }
32 
33  // These are mandatory parameters.
34  IOAddress addr = getAddress(lease_info, "ip-address");
35  if (!addr.isV4()) {
36  isc_throw(BadValue, "Non-IPv4 address specified: " << addr);
37  }
38 
39  // Not a most straightforward conversion, but it works.
40  string hwaddr_txt = getString(lease_info, "hw-address");
41  HWAddr hwaddr = HWAddr::fromText(hwaddr_txt);
42  HWAddrPtr hwaddr_ptr = HWAddrPtr(new HWAddr(hwaddr));
43 
44  // Now sort out the subnet-id. If specified, it must have correct value.
45  // If not specified, Kea will try to sort it out.
46  SubnetID subnet_id = 0;
47  if (lease_info->contains("subnet-id")) {
48  subnet_id = getUint32(lease_info, "subnet-id");
49  }
50  Subnet4Ptr subnet;
51  if (subnet_id) {
52  // If subnet-id is specified, it has to match.
53  subnet = cfg->getCfgSubnets4()->getSubnet(subnet_id);
54  if (!subnet) {
55  isc_throw(BadValue, "Invalid subnet-id: No IPv4 subnet with subnet-id="
56  << subnet_id << " currently configured.");
57  }
58 
59  if (!subnet->inRange(addr)) {
60  isc_throw(BadValue, "The address " << addr.toText() << " does not belong "
61  "to subnet " << subnet->toText() << ", subnet-id=" << subnet_id);
62  }
63 
64  } else {
65  // Subnet-id was not specified. Let's try to figure it out on our own.
66  subnet = cfg->getCfgSubnets4()->selectSubnet(addr);
67  if (!subnet) {
68  isc_throw(BadValue, "subnet-id not specified and failed to find a"
69  << " subnet for address " << addr);
70  }
71  subnet_id = subnet->getID();
72  }
73 
74  // Client-id is optional.
75  ClientIdPtr client_id;
76  if (lease_info->contains("client-id")) {
77  string txt = getString(lease_info, "client-id");
78  client_id = ClientId::fromText(txt);
79  }
80 
81  // These parameters are optional. If not specified, we'll derive them from
82  // the current subnet configuration, if possible.
83  uint32_t valid_lft = 0;
84  if (lease_info->contains("valid-lft")) {
85  valid_lft = getUint32(lease_info, "valid-lft");
86  } else {
87  valid_lft = subnet->getValid();
88  }
89 
96  time_t cltt;
97  if (lease_info->contains("expire")) {
98  int64_t expire_time = getInteger(lease_info, "expire");
99  if (expire_time <= 0) {
100  isc_throw(BadValue , "expiration time must be positive for address "
101  << addr);
102 
103  } else if (expire_time < valid_lft) {
104  isc_throw(BadValue, "expiration time must be greater than valid lifetime"
105  " for address " << addr);
106  }
107  cltt = static_cast<time_t>(expire_time - valid_lft);
108  } else {
109  cltt = time(NULL);
110  }
111 
112  bool fqdn_fwd = false;
113  if (lease_info->contains("fqdn-fwd")) {
114  fqdn_fwd = getBoolean(lease_info, "fqdn-fwd");
115  }
116  bool fqdn_rev = false;
117  if (lease_info->contains("fqdn-rev")) {
118  fqdn_rev = getBoolean(lease_info, "fqdn-rev");
119  }
120  string hostname;
121  if (lease_info->contains("hostname")) {
122  hostname = getString(lease_info, "hostname");
123  }
124  if (hostname.empty() && (fqdn_fwd || fqdn_rev)) {
125  isc_throw(BadValue, "No hostname specified and either forward or reverse"
126  " fqdn was set to true.");
127  }
128 
129  uint32_t state = 0;
130  if (lease_info->contains("state")) {
131  state = getUint8(lease_info, "state");
132  }
133 
134  // Check if the state value is sane.
135  if (state > Lease::STATE_EXPIRED_RECLAIMED) {
136  isc_throw(BadValue, "Invalid state value: " << state << ", supported "
137  "values are: 0 (default), 1 (declined) and 2 (expired-reclaimed)");
138  }
139 
140  // Handle user context.
141  ConstElementPtr ctx = lease_info->get("user-context");
142  if (ctx && (ctx->getType() != Element::map)) {
143  isc_throw(BadValue, "Invalid user context '" << ctx->str()
144  << "' is not a JSON map.");
145  }
146 
147  // Handle comment.
148  ConstElementPtr comment = lease_info->get("comment");
149  if (comment) {
150  if (ctx && ctx->contains("comment")) {
151  isc_throw(BadValue, "Duplicated comment entry '" << comment->str()
152  << "' in user context '" << ctx->str() << "'");
153  }
154  ElementPtr copied;
155  if (ctx) {
156  copied = copy(ctx, 0);
157  } else {
158  copied = Element::createMap();
159  }
160  copied->set("comment", comment);
161  ctx = copied;
162  }
163 
164  // Let's fabricate some data and we're ready to go.
165  uint32_t t1 = subnet->getT1();
166  uint32_t t2 = subnet->getT2();
167 
168  Lease4Ptr l(new Lease4(addr, hwaddr_ptr, client_id, valid_lft, t1, t2,
169  cltt, subnet_id,
170  fqdn_fwd, fqdn_rev, hostname));
171  l->state_ = state;
172  l->setContext(ctx);
173 
174  // Retrieve the optional flag indicating if the lease must be created when it
175  // doesn't exist during the update.
176  force_create = false;
177  if (lease_info->contains("force-create")) {
178  force_create = getBoolean(lease_info, "force-create");
179  }
180 
181  return (l);
182 }
183 
184 Lease6Ptr
185 Lease6Parser::parse(ConstSrvConfigPtr& cfg,
186  const ConstElementPtr& lease_info,
187  bool& force_create) {
188  if (!lease_info) {
189  isc_throw(BadValue, "lease information missing");
190  }
191 
192  // These are mandatory parameters.
193  IOAddress addr = getAddress(lease_info, "ip-address");
194  if (addr.isV4()) {
195  isc_throw(BadValue, "Non-IPv6 address specified: " << addr);
196  }
197 
198  // Not a most straightforward conversion, but it works.
199  string duid_txt = getString(lease_info, "duid");
200  DUID duid = DUID::fromText(duid_txt);
201  DuidPtr duid_ptr = DuidPtr(new DUID(duid));
202 
203  Lease::Type type = Lease::TYPE_NA;
204  uint8_t prefix_len = 128;
205  if (lease_info->contains("type")) {
206  string txt = getString(lease_info, "type");
207  if (txt == "IA_NA") {
208  type = Lease::TYPE_NA;
209  } else if (txt == "IA_TA") {
210  type = Lease::TYPE_TA;
211  } else if (txt == "IA_PD") {
212  type = Lease::TYPE_PD;
213 
214  prefix_len = getUint8(lease_info, "prefix-len");
215  } else {
216  isc_throw(BadValue, "Incorrect lease type: " << txt << ", the only "
217  "supported values are: na, ta and pd");
218  }
219  }
220 
221  // Now sort out the subnet-id. If specified, it must have correct value.
222  // If not specified, Kea will try to sort it out.
223  SubnetID subnet_id = 0;
224  if (lease_info->contains("subnet-id")) {
225  subnet_id = getUint32(lease_info, "subnet-id");
226  }
227 
228  // Check if the subnet-id specified is sane.
229  Subnet6Ptr subnet;
230  if (subnet_id) {
231  // If subnet-id is specified, it has to match.
232  subnet = cfg->getCfgSubnets6()->getSubnet(subnet_id);
233  if (!subnet) {
234  isc_throw(BadValue, "Invalid subnet-id: No IPv6 subnet with subnet-id="
235  << subnet_id << " currently configured.");
236  }
237 
238  // Check if the address specified really belongs to the subnet.
239  if ((type == Lease::TYPE_NA) && !subnet->inRange(addr)) {
240  isc_throw(BadValue, "The address " << addr.toText() << " does not belong "
241  "to subnet " << subnet->toText() << ", subnet-id=" << subnet_id);
242  }
243 
244  } else {
245  if (type != Lease::TYPE_NA) {
246  isc_throw(BadValue, "Subnet-id is 0 or not specified. This is allowed for"
247  " address leases only, not prefix leases.");
248  }
249  subnet = cfg->getCfgSubnets6()->selectSubnet(addr);
250  if (!subnet) {
251  isc_throw(BadValue, "subnet-id not specified and failed to find a "
252  "subnet for address " << addr);
253  }
254  subnet_id = subnet->getID();
255  }
256 
257  uint32_t iaid = getUint32(lease_info, "iaid");
258 
259  // Hw-address is optional in v6 leases.
260  HWAddrPtr hwaddr_ptr;
261  if (lease_info->contains("hw-address")) {
262  string hwaddr_txt = getString(lease_info, "hw-address");
263  HWAddr hwaddr = HWAddr::fromText(hwaddr_txt);
264  hwaddr_ptr = HWAddrPtr(new HWAddr(hwaddr));
265  }
266 
267  // These parameters are optional. If not specified, we'll derive them
268  // from the current subnet configuration, if possible.
269  uint32_t valid_lft = 0;
270  if (lease_info->contains("valid-lft")) {
271  valid_lft = getUint32(lease_info, "valid-lft");
272  } else {
273  valid_lft = subnet->getValid();
274  }
275 
276  // These parameters are optional. If not specified, we'll derive them
277  // from the current subnet configuration, if possible.
278  uint32_t pref_lft = 0;
279  if (lease_info->contains("preferred-lft")) {
280  pref_lft = getUint32(lease_info, "preferred-lft");
281  } else {
282  pref_lft = subnet->getValid();
283  }
284 
291  time_t cltt;
292  if (lease_info->contains("expire")) {
293  int64_t expire_time = getInteger(lease_info, "expire");
294  if (expire_time <= 0) {
295  isc_throw(BadValue , "expiration time must be positive for address "
296  << addr);
297 
298  } else if (expire_time < valid_lft) {
299  isc_throw(BadValue, "expiration time must be greater than valid lifetime"
300  " for address " << addr);
301  }
302 
303  cltt = static_cast<time_t>(expire_time - valid_lft);
304  } else {
305  cltt = time(NULL);
306  }
307 
308  bool fqdn_fwd = false;
309  if (lease_info->contains("fqdn-fwd")) {
310  fqdn_fwd = getBoolean(lease_info, "fqdn-fwd");
311  }
312  bool fqdn_rev = false;
313  if (lease_info->contains("fqdn-rev")) {
314  fqdn_rev = getBoolean(lease_info, "fqdn-rev");
315  }
316  string hostname;
317  if (lease_info->contains("hostname")) {
318  hostname = getString(lease_info, "hostname");
319  }
320  if (hostname.empty() && (fqdn_fwd || fqdn_rev)) {
321  isc_throw(BadValue, "No hostname specified and either forward or reverse"
322  " fqdn was set to true.");
323  }
324 
325  uint32_t state = 0;
326  if (lease_info->contains("state")) {
327  state = getUint8(lease_info, "state");
328  }
329 
330  // Check if the state value is sane.
331  if (state > Lease::STATE_EXPIRED_RECLAIMED) {
332  isc_throw(BadValue, "Invalid state value: " << state << ", supported "
333  "values are: 0 (default), 1 (declined) and 2 (expired-reclaimed)");
334  }
335 
336  // Handle user context.
337  ConstElementPtr ctx = lease_info->get("user-context");
338  if (ctx && (ctx->getType() != Element::map)) {
339  isc_throw(BadValue, "Invalid user context '" << ctx->str()
340  << "' is not a JSON map.");
341  }
342 
343  // Handle comment.
344  ConstElementPtr comment = lease_info->get("comment");
345  if (comment) {
346  if (ctx && ctx->contains("comment")) {
347  isc_throw(BadValue, "Duplicated comment entry '" << comment->str()
348  << "' in user context '" << ctx->str() << "'");
349  }
350  ElementPtr copied;
351  if (ctx) {
352  copied = copy(ctx, 0);
353  } else {
354  copied = Element::createMap();
355  }
356  copied->set("comment", comment);
357  ctx = copied;
358  }
359 
360  // Let's fabricate some data and we're ready to go.
361  uint32_t t1 = subnet->getT1();
362  uint32_t t2 = subnet->getT2();
363 
364  Lease6Ptr l(new Lease6(type, addr, duid_ptr, iaid, pref_lft, valid_lft, t1, t2,
365  subnet_id, fqdn_fwd, fqdn_rev, hostname,
366  hwaddr_ptr, prefix_len));
367  l->cltt_ = cltt;
368  l->state_ = state;
369  l->setContext(ctx);
370 
371  // Retrieve the optional flag indicating if the lease must be created when it
372  // doesn't exist during the update.
373  force_create = false;
374  if (lease_info->contains("force-create")) {
375  force_create = getBoolean(lease_info, "force-create");
376  }
377 
378  return (l);
379 }
380 
381 };
382 };
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:20
Structure that holds a lease for IPv4 address.
Definition: lease.h:256
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:464
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
boost::shared_ptr< const SrvConfig > ConstSrvConfigPtr
Const pointer to the SrvConfig.
Definition: srv_config.h:710
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1114
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:245
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
Structure that holds a lease for IPv6 address and/or prefix.
Definition: lease.h:471
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:103
Defines the logger used by the top-level component of kea-dhcp-ddns.
Type
Type of lease or pool.
Definition: lease.h:38
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition: subnet.h:629
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:460
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24