1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431 | // Copyright (C) 2017-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <asiolink/io_address.h>
#include <asiolink/addr_utilities.h>
#include <cc/data.h>
#include <dhcp/hwaddr.h>
#include <dhcpsrv/lease.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/cfg_consistency.h>
#include <dhcpsrv/lease_mgr.h>
#include <lease_cmds_exceptions.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lease_parser.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <config.h>
using namespace std;
using namespace isc::dhcp;
using namespace isc::data;
using namespace isc::asiolink;
namespace isc {
namespace lease_cmds {
Lease4Ptr
Lease4Parser::parse(ConstSrvConfigPtr& cfg,
const ConstElementPtr& lease_info,
bool& force_create) {
if (!lease_info) {
isc_throw(BadValue, "lease information missing");
}
// These are mandatory parameters.
IOAddress addr = getAddress(lease_info, "ip-address");
if (!addr.isV4()) {
isc_throw(BadValue, "Non-IPv4 address specified: " << addr);
}
// Not a most straightforward conversion, but it works.
string hwaddr_txt = getString(lease_info, "hw-address");
HWAddr hwaddr = HWAddr::fromText(hwaddr_txt);
HWAddrPtr hwaddr_ptr = HWAddrPtr(new HWAddr(hwaddr));
// Now sort out the subnet-id. If specified, it must have correct value.
// If not specified, Kea will try to sort it out.
SubnetID subnet_id = 0;
if (lease_info->contains("subnet-id")) {
subnet_id = getUint32(lease_info, "subnet-id");
}
uint32_t pool_id = 0;
if (lease_info->contains("pool-id")) {
pool_id = getUint32(lease_info, "pool-id");
}
// Check if the subnet-id specified is sane.
ConstSubnet4Ptr subnet;
if (subnet_id) {
// If subnet-id is specified, it has to match.
subnet = cfg->getCfgSubnets4()->getBySubnetId(subnet_id);
if (!subnet) {
isc_throw(LeaseCmdsConflict, "Invalid subnet-id: No IPv4 subnet with subnet-id="
<< subnet_id << " currently configured.");
}
// Check if the address specified really belongs to the subnet.
if (!subnet->inRange(addr)) {
isc_throw(LeaseCmdsConflict, "The address " << addr.toText() << " does not belong "
"to subnet " << subnet->toText() << ", subnet-id=" << subnet_id);
}
} else {
// Subnet-id was not specified. Let's try to figure it out on our own.
subnet = cfg->getCfgSubnets4()->selectSubnet(addr);
if (!subnet) {
isc_throw(LeaseCmdsConflict, "subnet-id not specified and failed to find a"
<< " subnet for address " << addr);
}
subnet_id = subnet->getID();
}
// Client-id is optional.
ClientIdPtr client_id;
if (lease_info->contains("client-id")) {
string txt = getString(lease_info, "client-id");
client_id = ClientId::fromText(txt);
}
// These parameters are optional. If not specified, we'll derive them from
// the current subnet configuration, if possible.
uint32_t valid_lft = 0;
if (lease_info->contains("valid-lft")) {
valid_lft = getUint32(lease_info, "valid-lft");
} else {
valid_lft = subnet->getValid();
}
/// Let's calculate client last transmission time (cltt). If expiration
/// timestamp is specified explicitly, we will use that. Note there are no
/// checks whether this is in the past. There may be valid cases when user
/// wants to insert expired leases, e.g. when migrating from one DHCP server
/// to another and wants to migrate the database as is, without discarding
/// any leases.
time_t cltt;
if (lease_info->contains("expire")) {
int64_t expire_time = getInteger(lease_info, "expire");
if (expire_time <= 0) {
isc_throw(BadValue , "expiration time must be positive for address "
<< addr);
} else if (expire_time < valid_lft) {
isc_throw(BadValue, "expiration time must be greater than valid lifetime"
" for address " << addr);
}
cltt = static_cast<time_t>(expire_time - valid_lft);
} else {
cltt = time(NULL);
}
bool fqdn_fwd = false;
if (lease_info->contains("fqdn-fwd")) {
fqdn_fwd = getBoolean(lease_info, "fqdn-fwd");
}
bool fqdn_rev = false;
if (lease_info->contains("fqdn-rev")) {
fqdn_rev = getBoolean(lease_info, "fqdn-rev");
}
string hostname;
if (lease_info->contains("hostname")) {
hostname = getString(lease_info, "hostname");
}
if (hostname.empty() && (fqdn_fwd || fqdn_rev)) {
isc_throw(BadValue, "No hostname specified and either forward or reverse"
" fqdn was set to true.");
}
uint32_t state = 0;
if (lease_info->contains("state")) {
state = getUint8(lease_info, "state");
}
// Check if the state value is sane.
if (state > Lease::STATE_RELEASED) {
isc_throw(BadValue, "Invalid state value: " << state << ", supported "
"values are: 0 (default), 1 (declined), 2 (expired-reclaimed)"
" and 3 (released)");
}
// Handle user context.
ConstElementPtr ctx = lease_info->get("user-context");
if (ctx && (ctx->getType() != Element::map)) {
isc_throw(BadValue, "Invalid user context '" << ctx->str()
<< "' is not a JSON map.");
}
// Handle comment.
ConstElementPtr comment = lease_info->get("comment");
if (comment) {
if (ctx && ctx->contains("comment")) {
isc_throw(BadValue, "Duplicated comment entry '" << comment->str()
<< "' in user context '" << ctx->str() << "'");
}
ElementPtr copied;
if (ctx) {
copied = copy(ctx, 0);
} else {
copied = Element::createMap();
}
copied->set("comment", comment);
ctx = copied;
}
// Let's fabricate some data and we're ready to go.
Lease4Ptr l(new Lease4(addr, hwaddr_ptr, client_id, valid_lft,
cltt, subnet_id,
fqdn_fwd, fqdn_rev, hostname));
l->state_ = state;
l->setContext(ctx);
l->pool_id_ = pool_id;
// Sanitize extended info.
if (ctx) {
auto check = cfg->getConsistency()->getExtendedInfoSanityCheck();
LeaseMgr::upgradeLease4ExtendedInfo(l, check);
LeaseMgr::extractLease4ExtendedInfo(l);
}
// Retrieve the optional flag indicating if the lease must be created when it
// doesn't exist during the update.
force_create = false;
if (lease_info->contains("force-create")) {
force_create = getBoolean(lease_info, "force-create");
}
return (l);
}
Lease6Ptr
Lease6Parser::parse(ConstSrvConfigPtr& cfg,
const ConstElementPtr& lease_info,
bool& force_create) {
if (!lease_info) {
isc_throw(BadValue, "lease information missing");
}
// These are mandatory parameters.
IOAddress addr = getAddress(lease_info, "ip-address");
if (addr.isV4()) {
isc_throw(BadValue, "Non-IPv6 address specified: " << addr);
}
// Not a most straightforward conversion, but it works.
string duid_txt = getString(lease_info, "duid");
DUID duid = DUID::fromText(duid_txt);
DuidPtr duid_ptr = DuidPtr(new DUID(duid));
Lease::Type type = Lease::TYPE_NA;
uint8_t prefix_len = 128;
if (lease_info->contains("type")) {
string txt = getString(lease_info, "type");
if (txt == "IA_NA") {
type = Lease::TYPE_NA;
} else if (txt == "IA_TA") {
type = Lease::TYPE_TA;
} else if (txt == "IA_PD") {
type = Lease::TYPE_PD;
prefix_len = getUint8(lease_info, "prefix-len");
} else {
isc_throw(BadValue, "Incorrect lease type: " << txt << ", the only "
"supported values are: IA_NA and IA_PD");
}
}
// Now sort out the subnet-id. If specified, it must have correct value.
// If not specified, Kea will try to sort it out.
SubnetID subnet_id = 0;
if (lease_info->contains("subnet-id")) {
subnet_id = getUint32(lease_info, "subnet-id");
}
uint32_t pool_id = 0;
if (lease_info->contains("pool-id")) {
pool_id = getUint32(lease_info, "pool-id");
}
// Check if the subnet-id specified is sane.
ConstSubnet6Ptr subnet;
if (subnet_id) {
// If subnet-id is specified, it has to match.
subnet = cfg->getCfgSubnets6()->getBySubnetId(subnet_id);
if (!subnet) {
isc_throw(LeaseCmdsConflict, "Invalid subnet-id: No IPv6 subnet with subnet-id="
<< subnet_id << " currently configured.");
}
// Check if the address specified really belongs to the subnet.
if ((type == Lease::TYPE_NA) && !subnet->inRange(addr)) {
isc_throw(LeaseCmdsConflict, "The address " << addr.toText() << " does not belong "
"to subnet " << subnet->toText() << ", subnet-id=" << subnet_id);
}
} else {
if (type != Lease::TYPE_NA) {
isc_throw(BadValue, "Subnet-id is 0 or not specified. This is allowed for"
" address leases only, not prefix leases.");
}
// Subnet-id was not specified. Let's try to figure it out on our own.
subnet = cfg->getCfgSubnets6()->selectSubnet(addr);
if (!subnet) {
isc_throw(LeaseCmdsConflict, "subnet-id not specified and failed to find a "
"subnet for address " << addr);
}
subnet_id = subnet->getID();
}
uint32_t iaid = getUint32(lease_info, "iaid");
// Hw-address is optional in v6 leases.
HWAddrPtr hwaddr_ptr;
if (lease_info->contains("hw-address")) {
string hwaddr_txt = getString(lease_info, "hw-address");
HWAddr hwaddr = HWAddr::fromText(hwaddr_txt);
hwaddr_ptr = HWAddrPtr(new HWAddr(hwaddr));
}
// These parameters are optional. If not specified, we'll derive them
// from the current subnet configuration, if possible.
uint32_t valid_lft = 0;
if (lease_info->contains("valid-lft")) {
valid_lft = getUint32(lease_info, "valid-lft");
} else {
valid_lft = subnet->getValid();
}
// These parameters are optional. If not specified, we'll derive them
// from the current subnet configuration, if possible.
uint32_t pref_lft = 0;
if (lease_info->contains("preferred-lft")) {
pref_lft = getUint32(lease_info, "preferred-lft");
} else {
pref_lft = subnet->getValid();
}
/// Let's calculate client last transmission time (cltt). If expiration
/// timestamp is specified explicitly, we will use that. Note there are
/// no checks whether this is in the past. There may be valid cases when
/// user wants to insert expired leases, e.g. when migrating from one
/// DHCP server to another and wants to migrate the database as is, without
/// discarding any leases.
time_t cltt;
if (lease_info->contains("expire")) {
int64_t expire_time = getInteger(lease_info, "expire");
if (expire_time <= 0) {
isc_throw(BadValue , "expiration time must be positive for address "
<< addr);
} else if (expire_time < valid_lft) {
isc_throw(BadValue, "expiration time must be greater than valid lifetime"
" for address " << addr);
}
cltt = static_cast<time_t>(expire_time - valid_lft);
} else {
cltt = time(NULL);
}
bool fqdn_fwd = false;
if (lease_info->contains("fqdn-fwd")) {
fqdn_fwd = getBoolean(lease_info, "fqdn-fwd");
}
bool fqdn_rev = false;
if (lease_info->contains("fqdn-rev")) {
fqdn_rev = getBoolean(lease_info, "fqdn-rev");
}
string hostname;
if (lease_info->contains("hostname")) {
hostname = getString(lease_info, "hostname");
}
if (hostname.empty() && (fqdn_fwd || fqdn_rev)) {
isc_throw(BadValue, "No hostname specified and either forward or reverse"
" fqdn was set to true.");
}
uint32_t state = 0;
if (lease_info->contains("state")) {
state = getUint8(lease_info, "state");
}
// Check if the state value is sane.
if (state > Lease::STATE_RELEASED) {
isc_throw(BadValue, "Invalid state value: " << state << ", supported "
"values are: 0 (default), 1 (declined), 2 (expired-reclaimed)"
" and 3 (released)");
}
if ((state == Lease::STATE_DECLINED) && (type == Lease::TYPE_PD)) {
isc_throw(isc::InvalidOperation,
"Invalid declined state for PD prefix.");
}
// Handle user context.
ConstElementPtr ctx = lease_info->get("user-context");
if (ctx && (ctx->getType() != Element::map)) {
isc_throw(BadValue, "Invalid user context '" << ctx->str()
<< "' is not a JSON map.");
}
// Handle comment.
ConstElementPtr comment = lease_info->get("comment");
if (comment) {
if (ctx && ctx->contains("comment")) {
isc_throw(BadValue, "Duplicated comment entry '" << comment->str()
<< "' in user context '" << ctx->str() << "'");
}
ElementPtr copied;
if (ctx) {
copied = copy(ctx, 0);
} else {
copied = Element::createMap();
}
copied->set("comment", comment);
ctx = copied;
}
// Check if the prefix length is sane
if (prefix_len == 0 || prefix_len > 128) {
isc_throw(BadValue, "Invalid prefix length: "
<< static_cast<unsigned>(prefix_len));
}
if (prefix_len != 128) {
IOAddress first_address = firstAddrInPrefix(addr, prefix_len);
if (first_address != addr) {
isc_throw(BadValue, "Prefix address: " << addr
<< " exceeds prefix/prefix-len pair: " << first_address
<< "/" << static_cast<uint32_t>(prefix_len));
}
}
// Let's fabricate some data and we're ready to go.
Lease6Ptr l(new Lease6(type, addr, duid_ptr, iaid, pref_lft, valid_lft,
subnet_id, fqdn_fwd, fqdn_rev, hostname,
hwaddr_ptr, prefix_len));
l->cltt_ = cltt;
l->state_ = state;
l->setContext(ctx);
l->pool_id_ = pool_id;
// Sanitize extended info.
if (ctx) {
auto check = cfg->getConsistency()->getExtendedInfoSanityCheck();
LeaseMgr::upgradeLease6ExtendedInfo(l, check);
}
// Retrieve the optional flag indicating if the lease must be created when it
// doesn't exist during the update.
force_create = false;
if (lease_info->contains("force-create")) {
force_create = getBoolean(lease_info, "force-create");
}
return (l);
}
} // end of namespace lease_cmds
} // end of namespace isc
|