19#include <boost/date_time/posix_time/posix_time.hpp>
29using namespace isc::asiolink;
30using namespace isc::data;
31using namespace isc::db;
32using namespace isc::dhcp;
33using namespace isc::eval;
34using namespace isc::hooks;
35using namespace isc::util;
36using namespace isc::util::file;
46 if (!parameters || !parameters->get(
"type") ||
47 parameters->get(
"type")->stringValue() ==
"logfile") {
49 }
else if (parameters->get(
"type")->stringValue() ==
"syslog") {
68 for (
char const*
const& key : {
69 "type",
"user",
"password",
"host",
"name",
"trust-anchor",
70 "cert-file",
"key-file",
"ssl-mode",
"cipher-list" }) {
73 db_parameters.emplace(key, value->stringValue());
78 for (
char const*
const& key : {
79 "connect-timeout",
"reconnect-wait-time",
"max-reconnect-tries",
80 "read-timeout",
"write-timeout",
"tcp-user-timeout"}) {
83 int64_t integer_value(value->intValue());
84 auto const max(numeric_limits<uint32_t>::max());
85 if (integer_value < 0 || max < integer_value) {
87 key <<
" value: " << integer_value
88 <<
" is out of range, expected value: 0.."
92 boost::lexical_cast<string>(integer_value);
96 string param_name =
"tcp-nodelay";
99 db_parameters.emplace(param_name,
100 param->boolValue() ?
"true" :
"false");
106 param_name =
"on-fail";
107 param = parameters->get(param_name);
109 on_fail_action = param->stringValue();
112 db_parameters.emplace(param_name, on_fail_action);
114 param_name =
"retry-on-startup";
115 param = parameters->get(param_name);
117 db_parameters.emplace(param_name,
118 param->boolValue() ?
"true" :
"false");
123 param = parameters->get(param_name);
125 port = param->intValue();
126 if ((port < 0) || (port > numeric_limits<uint16_t>::max())) {
128 <<
" is out of range, expected value: 0.."
129 << numeric_limits<uint16_t>::max());
131 db_parameters.emplace(param_name,
132 boost::lexical_cast<string>(port));
138 string const db_type(db_parameters[
"type"]);
153 for (
char const*
const& key : {
"type",
"pattern",
"facility" }) {
156 syslog_parameters.emplace(key, value->stringValue());
160 map = syslog_parameters;
166 file_parameters[
"type"] =
"logfile";
169 map = file_parameters;
174 for (
char const*
const& key : {
"path",
"base-name",
"time-unit",
"prerotate",
"postrotate" }) {
177 if (key == std::string(
"path")) {
180 file_parameters.emplace(key, valid_path);
184 file_parameters.emplace(key, value->stringValue());
188 file_parameters.emplace(key, value->stringValue());
193 for (
char const*
const& key : {
"count" }) {
196 int64_t integer_value(value->intValue());
197 auto const max(numeric_limits<uint32_t>::max());
198 if (integer_value < 0 || max < integer_value) {
200 key <<
" value: " << integer_value
201 <<
" is out of range, expected value: 0.."
204 file_parameters[key] = boost::lexical_cast<string>(integer_value);
207 map = file_parameters;
217 for (
char const*
const& key : {
"request-parser-format",
"response-parser-format",
"timestamp-format" }) {
219 if (value && !value->stringValue().empty()) {
220 map.emplace(key, value->stringValue());
228 struct timespec timestamp =
now();
229 localtime_r(×tamp.tv_sec, &time_info);
236 clock_gettime(CLOCK_REALTIME, &
now);
259 string tmp_format = format;
260 for (
auto it = tmp_format.begin(); it < tmp_format.end(); ++it) {
261 if (*it ==
'%' && ((it + 1) < tmp_format.end())) {
262 if (*(it + 1) ==
'Q') {
264 string::size_type pos = it - tmp_format.begin();
267 usec << setw(6) << setfill(
'0') << (time.tv_nsec / 1000);
268 string microseconds = usec.str();
269 microseconds.insert(3, 1,
'.');
270 tmp_format.replace(it, it + 2, microseconds);
272 it = tmp_format.begin() + pos + microseconds.length() - 1;
281 localtime_r(&time.tv_sec, &time_info);
283 if (!strftime(buffer,
sizeof(buffer), tmp_format.c_str(), &time_info)) {
285 "strftime returned 0. Maybe the timestamp format '"
287 <<
"' result is too long, maximum length allowed: "
290 return (
string(buffer));
299 if (secs == 0xffffffff) {
300 return (
"infinite duration");
303 uint32_t seconds = secs % 60;
304 uint32_t remainder = secs / 60;
305 uint32_t minutes = remainder % 60;
307 uint32_t hours = remainder % 24;
308 uint32_t days = remainder / 24;
313 os << days <<
" days ";
316 os << hours <<
" hrs "
317 << minutes <<
" mins "
318 << seconds <<
" secs";
325 const string& delimiter) {
329 for (
auto const& it : bytes) {
333 tmp << setw(2) << setfill('0') << static_cast<unsigned int>(it);
344 return (
string(bytes.cbegin(), bytes.cend()));
375 timestamp_format_ = timestamp_format;
386 return (
"unknown-action");
392 if (!legal_log_path_checker_ || reset) {
393 legal_log_path_checker_.reset(
new file::PathChecker(LEGAL_LOG_DIR,
"KEA_LEGAL_LOG_DIR"));
394 if (!explicit_path.empty()) {
395 legal_log_path_checker_->getPath(
true, explicit_path);
399 return (legal_log_path_checker_->getPath());
404 if (!legal_log_path_checker_) {
408 return (legal_log_path_checker_->validateDirectory(logpath));
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
static std::string redactedAccessString(const ParameterMap ¶meters)
Redact database access string.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
static CfgMgr & instance()
returns a single instance of Configuration Manager
Thrown if a LegalLogMgr encounters an error.
virtual struct tm currentTimeInfo() const
Returns the current local date and time.
void setRequestFormatExpression(const std::string &extended_format)
Sets request extended format expression for custom logging.
static void parseSyslog(const isc::data::ConstElementPtr ¶meters, isc::db::DatabaseConnection::ParameterMap &map)
Parse syslog specification.
static void parseFile(const isc::data::ConstElementPtr ¶meters, isc::db::DatabaseConnection::ParameterMap &map)
Parse file specification.
static std::string vectorHexDump(const std::vector< uint8_t > &bytes, const std::string &delimiter=":")
Creates a string of hex digit pairs from a vector of bytes.
static void parseDatabase(const isc::data::ConstElementPtr ¶meters, isc::db::DatabaseConnection::ParameterMap &map)
Parse database specification.
void setResponseFormatExpression(const std::string &extended_format)
Sets response extended format expression for custom logging.
static std::string genDurationString(const uint32_t secs)
Translates seconds into a text string of days, hours, minutes and seconds.
static std::string validatePath(const std::string logpath)
Validates a log path against the supported path for legal log files.
virtual struct timespec now() const
Returns the current system time.
static std::string getTimeString(const struct timespec &time, const std::string &format)
Returns a time as string.
static std::string getLogPath(bool reset=false, const std::string explicit_path="")
Fetches the supported legal log file path.
static std::string vectorDump(const std::vector< uint8_t > &bytes)
Creates a string from a vector of printable bytes.
LegalLogMgr(const isc::db::DatabaseConnection::ParameterMap parameters)
Constructor.
static void parseConfig(const isc::data::ConstElementPtr ¶meters, isc::db::DatabaseConnection::ParameterMap &map)
Parse database specification.
void setTimestampFormat(const std::string ×tamp_format)
Sets the timestamp format used for logging.
static void parseExtraParameters(const isc::data::ConstElementPtr ¶meters, isc::db::DatabaseConnection::ParameterMap &map)
Parse extra parameters which are not related to backend connection.
virtual std::string getNowString() const
Returns the current date and time as string.
Universe
defines option universe DHCPv4 or DHCPv6
Evaluation context, an interface to the expression evaluation.
bool parseString(const std::string &str, ParserType type=PARSER_BOOL)
Run the parser on the string specified.
@ PARSER_STRING
expression is expected to evaluate to string
isc::dhcp::Expression expression_
Parsed expression (output tokens are stored here)
static std::string onFailActionToText(OnFailAction action)
Convert action to string.
static OnFailAction onFailActionFromText(const std::string &text)
Convert string to action.
Embodies a supported path against which file paths can be validated.
A generic exception that is thrown if a parameter given violates security check but enforcement is la...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the abstract class for backend stores.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
boost::shared_ptr< const Element > ConstElementPtr
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
const string actionToVerb(Action action)
Translates an Action into its corresponding verb.
const isc::log::MessageID LEGAL_LOG_PATH_SECURITY_WARNING
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Action
Describe what kind of event is being logged.
boost::shared_ptr< PathChecker > PathCheckerPtr
Defines a pointer to a PathChecker.
Defines the logger used by the top-level component of kea-lfc.