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") {
66 if (parameters->get(
"password") && parameters->get(
"password-file")) {
68 <<
"'password-file'");
74 for (
char const*
const& key : {
75 "type",
"user",
"password",
"password-file",
"host",
"name",
76 "trust-anchor",
"cert-file",
"key-file",
"ssl-mode",
"cipher-list" }) {
79 db_parameters.emplace(key, value->stringValue());
84 for (
char const*
const& key : {
85 "connect-timeout",
"reconnect-wait-time",
"max-reconnect-tries",
86 "read-timeout",
"write-timeout",
"tcp-user-timeout"}) {
89 int64_t integer_value(value->intValue());
90 auto const max(numeric_limits<uint32_t>::max());
91 if (integer_value < 0 || max < integer_value) {
93 key <<
" value: " << integer_value
94 <<
" is out of range, expected value: 0.."
98 boost::lexical_cast<string>(integer_value);
105 string param_name =
"on-fail";
108 on_fail_action = param->stringValue();
111 db_parameters.emplace(param_name, on_fail_action);
113 param_name =
"retry-on-startup";
114 param = parameters->get(param_name);
116 db_parameters.emplace(param_name,
117 param->boolValue() ?
"true" :
"false");
122 param = parameters->get(param_name);
124 port = param->intValue();
125 if ((port < 0) || (port > numeric_limits<uint16_t>::max())) {
127 <<
" is out of range, expected value: 0.."
128 << numeric_limits<uint16_t>::max());
130 db_parameters.emplace(param_name,
131 boost::lexical_cast<string>(port));
137 string const db_type(db_parameters[
"type"]);
152 for (
char const*
const& key : {
"type",
"pattern",
"facility" }) {
155 syslog_parameters.emplace(key, value->stringValue());
159 map = syslog_parameters;
165 file_parameters[
"type"] =
"logfile";
168 map = file_parameters;
173 for (
char const*
const& key : {
"path",
"base-name",
"time-unit",
"prerotate",
"postrotate" }) {
176 if (key == std::string(
"path")) {
179 file_parameters.emplace(key, valid_path);
183 file_parameters.emplace(key, value->stringValue());
187 file_parameters.emplace(key, value->stringValue());
192 for (
char const*
const& key : {
"count" }) {
195 int64_t integer_value(value->intValue());
196 auto const max(numeric_limits<uint32_t>::max());
197 if (integer_value < 0 || max < integer_value) {
199 key <<
" value: " << integer_value
200 <<
" is out of range, expected value: 0.."
203 file_parameters[key] = boost::lexical_cast<string>(integer_value);
208 for (
char const*
const& key : {
"mark-continuation-lines" }) {
211 file_parameters.emplace(key,
212 value->boolValue() ?
"true" :
"false");
215 map = file_parameters;
225 for (
char const*
const& key : {
"request-parser-format",
"response-parser-format",
"timestamp-format" }) {
227 if (value && !value->stringValue().empty()) {
228 map.emplace(key, value->stringValue());
236 struct timespec timestamp =
now();
237 localtime_r(×tamp.tv_sec, &time_info);
244 clock_gettime(CLOCK_REALTIME, &
now);
267 string tmp_format = format;
268 for (
auto it = tmp_format.begin(); it < tmp_format.end(); ++it) {
269 if (*it ==
'%' && ((it + 1) < tmp_format.end())) {
270 if (*(it + 1) ==
'Q') {
272 string::size_type pos = it - tmp_format.begin();
275 usec << setw(6) << setfill(
'0') << (time.tv_nsec / 1000);
276 string microseconds = usec.str();
277 microseconds.insert(3, 1,
'.');
278 tmp_format.replace(it, it + 2, microseconds);
280 it = tmp_format.begin() + pos + microseconds.length() - 1;
289 localtime_r(&time.tv_sec, &time_info);
291 if (!strftime(buffer,
sizeof(buffer), tmp_format.c_str(), &time_info)) {
293 "strftime returned 0. Maybe the timestamp format '"
295 <<
"' result is too long, maximum length allowed: "
298 return (
string(buffer));
307 if (secs == 0xffffffff) {
308 return (
"infinite duration");
311 uint32_t seconds = secs % 60;
312 uint32_t remainder = secs / 60;
313 uint32_t minutes = remainder % 60;
315 uint32_t hours = remainder % 24;
316 uint32_t days = remainder / 24;
321 os << days <<
" days ";
324 os << hours <<
" hrs "
325 << minutes <<
" mins "
326 << seconds <<
" secs";
336 return (
string(bytes.cbegin(), bytes.cend()));
367 timestamp_format_ = timestamp_format;
378 return (
"unknown-action");
384 if (!legal_log_path_checker_ || reset) {
385 legal_log_path_checker_.reset(
new file::PathChecker(LEGAL_LOG_DIR,
"KEA_LEGAL_LOG_DIR"));
386 if (!explicit_path.empty()) {
387 legal_log_path_checker_->getPath(
true, explicit_path);
391 return (legal_log_path_checker_->getPath());
396 if (!legal_log_path_checker_) {
400 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 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.