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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530 | // Copyright (C) 2017-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.
/// @file command_callouts.cc Defines lease4_select and lease4_renew callout functions.
#include <config.h>
#include <cc/command_interpreter.h>
#include <cc/data.h>
#include <cc/simple_parser.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/cfg_subnets4.h>
#include <dhcpsrv/cfg_subnets6.h>
#include <dhcpsrv/subnet.h>
#include <hooks/callout_manager.h>
#include <hooks/hooks.h>
#include <util/str.h>
#include <legal_log_log.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <backend_store.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <subnets_user_context.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unordered_set><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc;
using namespace isc::config;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::hooks;
using namespace isc::util;
using namespace isc::legal_log;
using namespace std;
// Functions accessed by the hooks framework use C linkage to avoid the name
// mangling that accompanies use of the C++ compiler as well as to avoid
// issues related to namespaces.
extern "C" {
/// @brief Fetch value for a string element if it is an element scope
///
/// Exception safe fetcher which looks for a string element with the
/// given name within an given element scope. Rather than throw if the
/// element is not found, it returns true if found, false if not.
///
/// @param arguments element scope to search, assumed to be a map
/// @param name name of the desired element
/// @param[out] value reference to a string in which the found value is
/// returned. Will be set to "" if the element is not found.
///
/// @return true if the element was found, false if not
bool getOptionalString(ConstElementPtr& arguments, const string& name,<--- Parameter 'arguments' can be declared as reference to const
string& value) {
value = "";
try {
value = SimpleParser::getString(arguments, name);
return (true);
} catch (...) {
// we don't care why
}
return (false);
}
/// @brief Fetch value for a integer element if it is an element scope
///
/// Exception safe fetcher which looks for an integer element with the
/// given name within an given element scope. Rather than throw if the
/// element is not found, it returns true if found, false if not.
///
/// @param arguments element scope to search, assumed to be a map
/// @param name name of the desired element
/// @param[out] value reference to an integer in which the found value is
/// returned. Will be set to 0 if the element is not found.
///
/// @return true if the element was found, false if not
bool getOptionalInt(ConstElementPtr& arguments, const string& name,<--- Parameter 'arguments' can be declared as reference to const
int64_t& value) {
value = 0;
try {
value = SimpleParser::getInteger(arguments, name);
return (true);
} catch (...) {
// we don't care why
}
return (false);
}
/// @brief Returns true if an element scope describes a prefix delegation
///
/// Searches the given scope of an lease type element, "type". If it
/// exists and it's value is "IA_PD" or "2" return true, false otherwise.
///
/// @param arguments element scope to search, assumed to be a map
///
/// @return true if the scope describes a prefix delegation
bool isPrefix(ConstElementPtr arguments) {
string type_str;
if (getOptionalString(arguments, "type", type_str)) {
return (type_str == "IA_PD" || type_str == "2");
}
return (false);
}
/// @brief Outputs text describing lease duration to a stream
///
/// Looks for either a lease valid lifetime or expiry from which to
/// generate the duration text using @ref
/// isc::legal_log::BackendStore::genDurationString().
///
/// @param os output stream to which the text is output
/// @param arguments element scope which may contain duration values
void addDuration(ostringstream& os, ConstElementPtr& arguments) {
int64_t duration = 0;
if (!getOptionalInt(arguments, "valid-lft", duration)) {
int64_t expire = 0;
if (getOptionalInt(arguments, "expire", expire)) {
duration = expire - BackendStore::instance()->now().tv_sec;
}
}
if (duration > 0) {
os << " for " << BackendStore::genDurationString(duration);
}
}
/// @brief Outputs text describing lease optional user context to a stream
///
/// Looks for an user context or a comment and display it
///
/// @param os output stream to which the text is output
/// @param arguments element scope which may contain user context or comment
void addContext(ostringstream& os, ConstElementPtr& arguments) {
ConstElementPtr comment = arguments->get("comment");
ConstElementPtr ctx = arguments->get("user-context");
if (comment) {
ElementPtr copied;
if (ctx) {
copied = copy(ctx, 0);
} else {
copied = Element::createMap();
}
copied->set("comment", comment);
ctx = copied;
}
if (ctx) {
os << ", context: " << ctx->str();
}
}
/// @brief Check if command has optional subnet-id parameter and if it does,
/// check that the respective subnet has logging enabled.
///
/// @param arguments The command arguments.
///
/// @return true if logging is enabled (default), false otherwise.
bool checkLoggingEnabledSubnet4(ConstElementPtr& arguments) {
// Check if the subnet identifier has been specified for the command.
// In some cases it may be missing, i.e. lease4-del command, when deleting a
// lease by an IP address.
int64_t subnet_id_value = 0;
if (getOptionalInt(arguments, "subnet-id", subnet_id_value) && (subnet_id_value > 0)) {
// The subnet identifier is present and valid.
SubnetID subnet_id = static_cast<SubnetID>(subnet_id_value);
CfgSubnets4Ptr cfg = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4();
// Get the subnet by id and if the subnet is present, check if its user
// context to see if legal logging is enabled for this subnet.
ConstSubnet4Ptr subnet = cfg->getBySubnetId(subnet_id);
if (isLoggingDisabled(subnet)) {
return (false);
}
}
return (true);
}
/// @brief Handle lease4 related commands.
///
/// @param name The command name.
/// @param arguments The command arguments.
/// @param response The command response.
int handleLease4Cmds(string& name, ConstElementPtr& arguments,<--- Parameter 'name' can be declared as reference to const
ConstElementPtr& /*response*/) {
if (!BackendStore::instance()) {
LOG_ERROR(legal_log_logger,
LEGAL_LOG_COMMAND_NO_LEGAL_STORE);
return (1);
}
try {
if (!checkLoggingEnabledSubnet4(arguments)) {
return (0);
}
ostringstream os;
ostringstream osa;
string origin;
getOptionalString(arguments, "origin", origin);
if (origin == "ha-partner") {
os << "HA partner";
} else {
os << "Administrator";
}
if ((name == "lease4-add") || name == "lease4-update") {
if (name == "lease4-add") {
os << " added a lease of address: ";
} else {
os << " updated information on the lease of address: ";
}
osa << SimpleParser::getString(arguments, "ip-address");
os << SimpleParser::getString(arguments, "ip-address")
<< " to a device with hardware address: "
<< SimpleParser::getString(arguments, "hw-address");
string client_id;
if (getOptionalString(arguments, "client-id", client_id)) {
os << ", client-id: " << client_id;
// It is not uncommon to provide a printable client ID
try {
auto bin = ClientId::fromText(client_id)->getClientId();
if (str::isPrintable(bin)) {
os << " (" << BackendStore::vectorDump(bin) << ")";
}
} catch (...) {
// Ignore any error
}
}
addDuration(os, arguments);
addContext(os, arguments);
} else if (name == "lease4-del") {
string ip_address;
if (getOptionalString(arguments, "ip-address", ip_address)) {
osa << SimpleParser::getString(arguments, "ip-address");
os << " deleted the lease for address: "
<< SimpleParser::getString(arguments, "ip-address");
} else {
os << " deleted a lease for a device identified by: "
<< SimpleParser::getString(arguments, "identifier-type")
<< " of " << SimpleParser::getString(arguments, "identifier");
}
}
BackendStore::instance()->writeln(os.str(), osa.str());
} catch (const exception& ex) {
LOG_ERROR(legal_log_logger, LEGAL_LOG_COMMAND_WRITE_ERROR)
.arg(ex.what());
return (1);
}
return (0);
}
/// @brief Check if command has optional subnet-id parameter and if it does,
/// check that the respective subnet has logging enabled.
///
/// @param arguments The command arguments.
///
/// @return true if logging is enabled (default), false otherwise.
bool checkLoggingEnabledSubnet6(ConstElementPtr& arguments) {
// Check if the subnet identifier has been specified for the command.
// In some cases it may be missing, i.e. lease6-del command, when deleting a
// lease by an IP address or prefix or lease6-bulk-apply command.
int64_t subnet_id_value = 0;
if (getOptionalInt(arguments, "subnet-id", subnet_id_value) && (subnet_id_value > 0)) {
// The subnet identifier is present and valid.
SubnetID subnet_id = static_cast<SubnetID>(subnet_id_value);
CfgSubnets6Ptr cfg = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6();
// Get the subnet by id and if the subnet is present, check if its user
// context to see if legal logging is enabled for this subnet.
ConstSubnet6Ptr subnet = cfg->getBySubnetId(subnet_id);
if (isLoggingDisabled(subnet)) {
return (false);
}
}
return (true);
}
/// @brief Handle lease6 related commands.
///
/// @param name The command name.
/// @param arguments The command arguments.
/// @param response The command response.
int handleLease6Cmds(string& name, ConstElementPtr& arguments,<--- Parameter 'name' can be declared as reference to const
ConstElementPtr& response) {
if (!BackendStore::instance()) {
LOG_ERROR(legal_log_logger,
LEGAL_LOG_COMMAND_NO_LEGAL_STORE);
return (1);
}
try {
if (!checkLoggingEnabledSubnet6(arguments)) {
return (0);
}
ostringstream os;
ostringstream osa;
string origin;
getOptionalString(arguments, "origin", origin);
if (origin == "ha-partner") {
os << "HA partner";
} else {
os << "Administrator";
}
if ((name == "lease6-add") || name == "lease6-update") {
if (name == "lease6-add") {
os << " added a lease of";
} else {
os << " updated information on the lease of";
}
osa << SimpleParser::getString(arguments, "ip-address");
if (isPrefix(arguments)) {
os << " prefix: "
<< SimpleParser::getString(arguments, "ip-address")
<< "/"
<< SimpleParser::getInteger(arguments, "prefix-len");
osa << "/"
<< SimpleParser::getInteger(arguments, "prefix-len");
} else {
os << " address: "
<< SimpleParser::getString(arguments, "ip-address");
}
os << " to a device with DUID: "
<< SimpleParser::getString(arguments, "duid");
string hw_address;
if (getOptionalString(arguments, "hw-address", hw_address)) {
os << ", hardware address: " << hw_address;
}
addDuration(os, arguments);
addContext(os, arguments);
} else if (name == "lease6-del") {
string ip_address;
if (getOptionalString(arguments, "ip-address", ip_address)) {
osa << SimpleParser::getString(arguments, "ip-address");
os << " deleted the lease for address: "
<< SimpleParser::getString(arguments, "ip-address");
} else {
os << " deleted a lease for a device identified by: "
<< SimpleParser::getString(arguments, "identifier-type")
<< " of " << SimpleParser::getString(arguments, "identifier");
}
} else if (name == "lease6-bulk-apply") {
// At least one of the 'deleted-leases' or 'leases' must be present.
auto deleted_leases = arguments->get("deleted-leases");
auto leases = arguments->get("leases");
if (!deleted_leases && !leases) {
isc_throw(BadValue, "neither 'deleted-leases' nor 'leases' parameter"
" specified");
}
// Make sure that 'deleted-leases' is a list, if present.
if (deleted_leases && (deleted_leases->getType() != Element::list)) {
isc_throw(BadValue, "the 'deleted-leases' parameter must be a list");
}
// Make sure that 'leases' is a list, if present.
if (leases && (leases->getType() != Element::list)) {
isc_throw(BadValue, "the 'leases' parameter must be a list");
}
ConstElementPtr failed_deleted_leases;
ConstElementPtr failed_leases;
auto response_args = response->get("arguments");
if (response_args) {
failed_deleted_leases = response_args->get("failed-deleted-leases");
failed_leases = response_args->get("failed-leases");
}
int status = 0;
if (deleted_leases) {
unordered_set<string> failed_deleted_leases_set;
if (failed_deleted_leases) {
// Make sure that 'failed-deleted-leases' is a list, if present.
if (failed_deleted_leases->getType() != Element::list) {
isc_throw(BadValue, "the 'failed-deleted-leases' parameter must be a list");
}
auto leases_list = failed_deleted_leases->listValue();
for (auto const& lease_params : leases_list) {
auto address = lease_params->get("ip-address");
if (address) {
failed_deleted_leases_set.emplace(address->stringValue());
}
}
}
auto leases_list = deleted_leases->listValue();
for (auto const& lease_params : leases_list) {
auto address = lease_params->get("ip-address");
if (address && failed_deleted_leases_set.count(address->stringValue()) == 0) {
ElementPtr copy;
if (!origin.empty()) {
copy = data::copy(lease_params);
copy->set("origin", Element::create(origin));
} else {
copy = lease_params;
}
ConstElementPtr args(copy);
ConstElementPtr resp;
string cmd_name("lease6-del");
int result = handleLease6Cmds(cmd_name, args, resp);
if (result) {
status = result;
}
}
}
}
if (leases) {
unordered_set<string> failed_leases_set;
if (failed_leases) {
// Make sure that 'failed-leases' is a list, if present.
if (failed_leases->getType() != Element::list) {
isc_throw(BadValue, "the 'failed-leases' parameter must be a list");
}
auto leases_list = failed_leases->listValue();
for (auto const& lease_params : leases_list) {
auto address = lease_params->get("ip-address");
if (address) {
failed_leases_set.emplace(address->stringValue());
}
}
}
auto leases_list = leases->listValue();
for (auto const& lease_params : leases_list) {
auto address = lease_params->get("ip-address");
if (address && failed_leases_set.count(address->stringValue()) == 0) {
ElementPtr copy;
if (!origin.empty()) {
copy = data::copy(lease_params);
copy->set("origin", Element::create(origin));
} else {
copy = lease_params;
}
ConstElementPtr args(copy);
ConstElementPtr resp;
string cmd_name("lease6-update");
int result = handleLease6Cmds(cmd_name, args, resp);
if (result) {
status = result;
}
}
}
}
return (status);
}
BackendStore::instance()->writeln(os.str(), osa.str());
} catch (const exception& ex) {
LOG_ERROR(legal_log_logger, LEGAL_LOG_COMMAND_WRITE_ERROR)
.arg(ex.what());
return (1);
}
return (0);
}
/// @brief This callout is called at the "command_processed" hook point.
///
/// Generates an entry in the legal log for the given control channel
/// command.
///
/// It is possible to disable logging for selected subnets by specifying a
/// "legal-logging" boolean parameter within the subnet's user context. If
/// this parameter is not specified it defaults to 'true', in which case the
/// logging is performed. When this value is set to 'false' the log is not
/// produced and the function returns.
///
/// @param handle CalloutHandle which provides access to context.
///
/// @return 0 upon success, non-zero otherwise.
int command_processed(CalloutHandle& handle) {<--- Parameter 'handle' can be declared as reference to const
if (!BackendStore::instance()) {
LOG_ERROR(legal_log_logger,
LEGAL_LOG_COMMAND_NO_LEGAL_STORE);
return (1);
}
string name;<--- The scope of the variable 'name' can be reduced. [+]The scope of the variable 'name' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
ConstElementPtr arguments;
ConstElementPtr response;
try {
handle.getArgument("name", name);
handle.getArgument("arguments", arguments);
handle.getArgument("response", response);
int result = SimpleParser::getInteger(response, "result");
if (result != 0) {
// We don't log failed commands
return (0);
}
// We are only interested in the following commands.
static unordered_set<string> const supported = {
"lease4-add", "lease4-update","lease4-del", "lease6-add",
"lease6-update", "lease6-del", "lease6-bulk-apply"
};
if (supported.count(name) == 0) {
return (0);
}
// We need to get a different configuration pointer depending if we're
// in v4 or v6 universe. The easiest way to check the universe is by the
// command name.
if (name.find("lease4-") != string::npos) {
return (handleLease4Cmds(name, arguments, response));
} else if (name.find("lease6-") != string::npos) {
return (handleLease6Cmds(name, arguments, response));
}
} catch (const exception& ex) {
LOG_ERROR(legal_log_logger, LEGAL_LOG_COMMAND_WRITE_ERROR)
.arg(ex.what());
return (1);
}
return (0);
}
} // end extern "C"
|