Adds support the Parallel audit log index creation

The index is now being generated.
This commit is contained in:
Felipe Zimmerle
2015-07-13 16:48:57 -03:00
parent 96a777a5cf
commit f13a1bd880
8 changed files with 157 additions and 2 deletions

View File

@@ -75,7 +75,8 @@ ACTIONS = \
actions/transformations/utf8_to_unicode.cc
UTILS = \
utils/sha1.cc
utils/sha1.cc \
utils/md5.cc
libmodsecurity_la_SOURCES = \
parser/seclang-parser.yy \

View File

@@ -688,6 +688,51 @@ ModSecurityIntervention *Assay::intervention() {
}
std::string Assay::toOldAuditLogFormatIndex(const std::string &filename,
double size, const std::string &md5) {
std::stringstream ss;
struct tm timeinfo;
char tstr[300];
memset(tstr, '\0', 300);
localtime_r(&this->timeStamp, &timeinfo);
strftime(tstr, 299, "[%d/%b/%Y:%H:%M:%S %z]", &timeinfo);
ss << dash_if_empty(this->resolve_variable("REQUEST_HEADERS:Host")) << " ";
ss << dash_if_empty(this->m_clientIpAddress) << " ";
/** TODO: Check variable */
ss << dash_if_empty(this->resolve_variable("REMOTE_USER")) << " ";
/** TODO: Check variable */
ss << dash_if_empty(this->resolve_variable("LOCAL_USER")) << " ";
ss << tstr << " ";
ss << "\"";
ss << this->m_protocol << " ";
ss << this->m_uri << " ";
ss << "HTTP/" << m_httpVersion;
ss << "\" ";
ss << this->httpCodeReturned << " ";
ss << this->m_responseBody.tellp();
/** TODO: Check variable */
ss << dash_if_empty(this->resolve_variable("REFERER")) << " ";
ss << "\"";
ss << dash_if_empty(this->resolve_variable("REQUEST_HEADERS:User-Agent"));
ss << "\" ";
ss << this->id << " ";
/** TODO: Check variable */
ss << dash_if_empty(this->resolve_variable("REFERER")) << " ";
ss << filename << " ";
ss << "0" << " ";
ss << std::to_string(size) << " ";
ss << "md5:" << md5 << std::endl;
return ss.str();
}
std::string Assay::toOldAuditLogFormat(int parts, const std::string &trailer) {
std::stringstream audit_log;
struct tm timeinfo;

View File

@@ -27,10 +27,22 @@
#include "src/audit_log.h"
#include "modsecurity/assay.h"
#include "src/utils.h"
#include "utils/md5.h"
namespace ModSecurity {
AuditLogWriterParallel::~AuditLogWriterParallel() {
if (log1.is_open()) {
log1.close();
}
if (log2.is_open()) {
log2.close();
}
}
inline std::string AuditLogWriterParallel::logFilePath(time_t *t,
int part) {
struct tm timeinfo;
@@ -65,6 +77,15 @@ inline std::string AuditLogWriterParallel::logFilePath(time_t *t,
bool AuditLogWriterParallel::init() {
/** TODO:: Check if the directory exists. */
/** TODO:: Checking if we have permission to write in the target dir */
if (!m_audit->m_path1.empty()) {
log1.open(m_audit->m_path1, std::fstream::out | std::fstream::app);
}
if (!m_audit->m_path2.empty()) {
log2.open(m_audit->m_path2, std::fstream::out | std::fstream::app);
}
return true;
}
@@ -100,7 +121,21 @@ bool AuditLogWriterParallel::write(Assay *assay, int parts) {
fwrite(log.c_str(), log.length(), 1, fp);
fclose(fp);
if (log1.is_open() && log2.is_open()) {
log2 << assay->toOldAuditLogFormatIndex(fileName, log.length(),
md5(log));
}
if (log1.is_open() && !log2.is_open()) {
log1 << assay->toOldAuditLogFormatIndex(fileName, log.length(),
md5(log));
}
if (!log1.is_open() && log2.is_open()) {
log2 << assay->toOldAuditLogFormatIndex(fileName, log.length(),
md5(log));
}
return true;
}
} // namespace ModSecurity

View File

@@ -31,7 +31,7 @@ class AuditLogWriterParallel : public AuditLogWriter {
explicit AuditLogWriterParallel(AuditLog *audit)
: AuditLogWriter(audit) { }
~AuditLogWriterParallel() { }
~AuditLogWriterParallel();
bool init() override;
bool write(Assay *assay, int parts) override;
@@ -69,6 +69,8 @@ class AuditLogWriterParallel : public AuditLogWriter {
YearMonthDayAndTimeFileName = 8,
};
std::ofstream log1;
std::ofstream log2;
inline std::string logFilePath(time_t *t, int part);
};

View File

@@ -57,6 +57,15 @@ double random_number(const double from, const double to) {
}
std::string dash_if_empty(const std::string& str) {
if (str.empty()) {
return "-";
}
return str;
}
double generate_assay_unique_id() {
return random_number(0, 100);
}

View File

@@ -28,6 +28,7 @@ namespace ModSecurity {
double generate_assay_unique_id();
std::string ascTime(time_t *t);
void createDir(std::string dir, int mode);
std::string dash_if_empty(const std::string& str);
} // namespace ModSecurity
#define SRC_UTILS_H_