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
// Copyright (C) 2018-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.

#include <config.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 <rotating_file.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#ifdef HAVE_MYSQL
#include <mysql_legal_log.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#endif
#ifdef HAVE_PGSQL
#include <pgsql_legal_log.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#endif

#include <database/database_connection.h>
#include <dhcpsrv/cfgmgr.h>
#include <eval/eval_context.h>
#include <util/reconnect_ctl.h>

#include <boost/date_time/posix_time/posix_time.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <errno.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream><--- 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 <time.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace legal_log {

using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::db;
using namespace isc::dhcp;
using namespace isc::eval;
using namespace isc::hooks;
using namespace isc::util;


void
BackendStore::parseFile(const ConstElementPtr& parameters) {
    // Defaults
    std::string path(LEGAL_LOG_DIR);
    std::string base("kea-legal");
    RotatingFile::TimeUnit unit(RotatingFile::TimeUnit::Day);
    int64_t count(1);
    std::string prerotate;
    std::string postrotate;

    // Prioritize parameters.
    if (parameters) {
        if (parameters->get("path")) {
            path = parameters->get("path")->stringValue();
        }
        if (parameters->get("base-name")) {
            base = parameters->get("base-name")->stringValue();
        }
        if (parameters->get("time-unit")) {
            std::string time_unit(parameters->get("time-unit")->stringValue());
            if (time_unit == "second") {
                unit = RotatingFile::TimeUnit::Second;
            } else if (time_unit == "day") {
                unit = RotatingFile::TimeUnit::Day;
            } else if (time_unit == "month") {
                unit = RotatingFile::TimeUnit::Month;
            } else if (time_unit == "year") {
                unit = RotatingFile::TimeUnit::Year;
            } else {
                isc_throw(BadValue, "unknown time unit type: " << time_unit
                            << ", expected one of: second, day, month, year");
            }
        }
        if (parameters->get("count")) {
            count = parameters->get("count")->intValue();
            if ((count < 0) ||
                (count > std::numeric_limits<uint32_t>::max())) {
                isc_throw(OutOfRange, "count value: " << count
                        << " is out of range, expected value: 0.."
                        << std::numeric_limits<uint32_t>::max());
            }
        }
        if (parameters->get("prerotate")) {
            prerotate = parameters->get("prerotate")->stringValue();
        }
        if (parameters->get("postrotate")) {
            postrotate = parameters->get("postrotate")->stringValue();
        }
    }

    BackendStore::instance() = boost::make_shared<RotatingFile>(
        path, base, unit, count, prerotate, postrotate);
}

void
BackendStore::parseDatabase(const ConstElementPtr& parameters) {
    // Should never happen with the code flow at the time of writing, but
    // let's get this check out of the way.
    if (!parameters) {
        isc_throw(BadValue, "no parameters specified for the hook library");
    }

    DatabaseConnection::ParameterMap db_parameters;

    // Strings
    for (char const* const& key : {
         "type", "user", "password", "host", "name", "trust-anchor",
         "cert-file", "key-file", "cipher-list" }) {
        ConstElementPtr const value(parameters->get(key));
        if (value) {
            db_parameters.emplace(key, value->stringValue());
        }
    }

    // uint32_t
    for (char const* const& key : {
         "connect-timeout", "reconnect-wait-time", "max-reconnect-tries"}) {
        ConstElementPtr const value(parameters->get(key));
        if (value) {
            int64_t integer_value(value->intValue());
            auto const max(std::numeric_limits<uint32_t>::max());
            if (integer_value < 0 || max < integer_value) {
                isc_throw(OutOfRange,
                          key << " value: " << integer_value
                              << " is out of range, expected value: 0.."
                              << max);
            }
            db_parameters[key] =
                boost::lexical_cast<std::string>(integer_value);
        }
    }

    std::string param_name = "tcp-nodelay";
    ConstElementPtr param = parameters->get(param_name);
    if (param) {
        db_parameters.emplace(param_name,
                              param->boolValue() ? "true" : "false");
    }

    // Always set "on-fail" to "serve-retry-continue" if not explicitly
    // configured.
    std::string on_fail_action = ReconnectCtl::onFailActionToText(OnFailAction::SERVE_RETRY_CONTINUE);
    param_name = "on-fail";
    param = parameters->get(param_name);
    if (param) {
        on_fail_action = param->stringValue();
        ReconnectCtl::onFailActionFromText(on_fail_action);
    }
    db_parameters.emplace(param_name, on_fail_action);

    param_name = "retry-on-startup";
    param = parameters->get(param_name);
    if (param) {
        db_parameters.emplace(param_name,
                              param->boolValue() ? "true" : "false");
    }

    int64_t port = 0;<--- Variable 'port' is assigned a value that is never used.
    param_name = "port";
    param = parameters->get(param_name);
    if (param) {
        port = param->intValue();
        if ((port < 0) || (port > std::numeric_limits<uint16_t>::max())) {
            isc_throw(OutOfRange, param_name << " value: " << port
                      << " is out of range, expected value: 0.."
                      << std::numeric_limits<uint16_t>::max());
        }
        db_parameters.emplace(param_name,
                              boost::lexical_cast<std::string>(port));
    }

    std::string redacted =<--- Variable 'redacted' is assigned a value that is never used.
        DatabaseConnection::redactedAccessString(db_parameters);

    std::string const dbtype(db_parameters["type"]);
#ifdef HAVE_MYSQL
    if (dbtype == "mysql") {
        LOG_INFO(legal_log_logger, LEGAL_LOG_MYSQL_DB).arg(redacted);
        BackendStore::instance().reset(new MySqlStore(db_parameters));
        return;
    }
#endif
#ifdef HAVE_PGSQL
    if (dbtype == "postgresql") {
        LOG_INFO(legal_log_logger, LEGAL_LOG_PGSQL_DB).arg(redacted);
        BackendStore::instance().reset(new PgSqlStore(db_parameters));
        return;
    }
#endif

    if ((dbtype == "mysql") || (dbtype == "postgresql")) {
        std::string with = (dbtype == "postgresql" ? "pgsql" : dbtype);
        isc_throw(InvalidType, "The Kea server has not been compiled with "
                  "support for database type: " << dbtype
                  << ". Did you forget to use --with-"
                  << with << " during compilation?");
    }

    isc_throw(InvalidType, "Database access parameter 'type' does "
              "not specify a supported database backend: " << dbtype);
}

void
BackendStore::parseExtraParameters(const ConstElementPtr& parameters) {
    if (!parameters) {
        return;
    }

    ConstElementPtr param = parameters->get("request-parser-format");
    if (param && !param->stringValue().empty()) {
        BackendStore::instance()->setRequestFormatExpression(param->stringValue());
    }

    param = parameters->get("response-parser-format");
    if (param && !param->stringValue().empty()) {
        BackendStore::instance()->setResponseFormatExpression(param->stringValue());
    }

    param = parameters->get("timestamp-format");
    if (param && !param->stringValue().empty()) {
        BackendStore::instance()->setTimestampFormat(param->stringValue());
    }
}

IOServicePtr BackendStore::io_service_;
DatabaseConnection::ParameterMap BackendStore::parameters_;
ConstElementPtr BackendStore::config_;

struct tm
BackendStore::currentTimeInfo() const {
    struct tm time_info;
    struct timespec timestamp = now();
    localtime_r(&timestamp.tv_sec, &time_info);
    return (time_info);
}

struct timespec
BackendStore::now() const {
    struct timespec now;
    clock_gettime(CLOCK_REALTIME, &now);
    return (now);
}

std::string
BackendStore::getNowString() const {
    // Get a text representation of the current time.
    return (getNowString(timestamp_format_));
}

std::string
BackendStore::getNowString(const std::string& format) const {
    // Get a text representation of the current time.
    return (getTimeString(now(), format));
}

std::string
BackendStore::getTimeString(const struct timespec& time, const std::string& format) {
    // Get a text representation of the requested time.

    // First a quick and dirty support for fractional seconds: Replace any "%Q"
    // tokens in the format string with the microsecond count from the timespec,
    // before handing it off to strftime().
    std::string tmp_format = format;
    for (auto it = tmp_format.begin(); it < tmp_format.end(); ++it) {
        if (*it == '%' && ((it + 1) < tmp_format.end())) {
            if (*(it + 1) == 'Q') {
                // Save the current position.
                std::string::size_type pos = it - tmp_format.begin();
                // Render the microsecond count.
                std::ostringstream usec;
                usec << std::setw(6) << std::setfill('0') << (time.tv_nsec / 1000);
                std::string microseconds = usec.str();
                microseconds.insert(3, 1, '.');
                tmp_format.replace(it, it + 2, microseconds);
                // Reinitialize the iterator after manipulating the string.
                it = tmp_format.begin() + pos + microseconds.length() - 1;
            } else {
                ++it;
            }
        }
    }

    char buffer[128];
    struct tm time_info;
    localtime_r(&time.tv_sec, &time_info);

    if (!strftime(buffer, sizeof(buffer), tmp_format.c_str(), &time_info)) {
        isc_throw(BackendStoreError,
                  "strftime returned 0. Maybe the timestamp format '"
                      << tmp_format
                      << "' result is too long, maximum length allowed: "
                      << sizeof(buffer));
    }
    return (std::string(buffer));
}

std::string
BackendStore::genDurationString(const uint32_t secs) {
    // Because Kea handles lease lifetimes as uint32_t and supports
    // a value of 0xFFFFFFFF (infinite lifetime), we don't use things like
    // boost:posix_time::time_duration as they work on longs.  Therefore
    // we'll figure it out ourselves.  Besides, the math ain't that hard.
    if (secs == 0xffffffff) {
        return ("infinite duration");
    }

    uint32_t seconds = secs % 60;
    uint32_t remainder = secs / 60;
    uint32_t minutes = remainder % 60;
    remainder /= 60;
    uint32_t hours = remainder % 24;
    uint32_t days = remainder / 24;

    std::ostringstream os;
    // Only spit out days if we have em.
    if (days) {
        os << days << " days ";
    }

    os << hours << " hrs "
       << minutes << " mins "
       << seconds << " secs";

    return (os.str());
}

std::string
BackendStore::vectorHexDump(const std::vector<uint8_t>& bytes,
                         const std::string& delimiter) {
    std::stringstream tmp;
    tmp << std::hex;
    bool delim = false;
    for (auto const& it : bytes) {
        if (delim) {
            tmp << delimiter;
        }
        tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(it);
        delim = true;
    }
    return (tmp.str());
}

std::string
BackendStore::vectorDump(const std::vector<uint8_t>& bytes) {
    if (bytes.empty()) {
        return (std::string());
    }
    return (std::string(bytes.cbegin(), bytes.cend()));
}

void
BackendStore::setRequestFormatExpression(const std::string& extended_format) {
    Option::Universe universe;
    if (CfgMgr::instance().getFamily() == AF_INET) {
        universe = Option::V4;
    } else {
        universe = Option::V6;
    }
    EvalContext eval_ctx(universe);
    eval_ctx.parseString(extended_format, EvalContext::PARSER_STRING);
    request_expression_.reset(new Expression(eval_ctx.expression_));
}

void
BackendStore::setResponseFormatExpression(const std::string& extended_format) {
    Option::Universe universe;
    if (CfgMgr::instance().getFamily() == AF_INET) {
        universe = Option::V4;
    } else {
        universe = Option::V6;
    }
    EvalContext eval_ctx(universe);
    eval_ctx.parseString(extended_format, EvalContext::PARSER_STRING);
    response_expression_.reset(new Expression(eval_ctx.expression_));
}

void
BackendStore::setTimestampFormat(const std::string& timestamp_format) {
    timestamp_format_ = timestamp_format;
}

const std::string
actionToVerb(Action action) {
    switch (action) {
    case Action::ASSIGN:
        return ("assigned");
    case Action::RELEASE:
        return ("released");
    default:
        return ("unknown-action");
    }
}

} // namespace legal_log
} // namespace isc