-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Support reopening audit and debug logs #3521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3/master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -347,3 +347,39 @@ extern "C" int msc_rules_cleanup(RulesSet *rules) { | |
|
|
||
| } // namespace modsecurity | ||
|
|
||
|
|
||
| extern "C" int msc_rules_reopen_logs(modsecurity::RulesSet *rules, | ||
| const char **error) { | ||
| bool succeeded = true; | ||
| std::string errorStr; | ||
|
|
||
| if (rules->m_auditLog != nullptr) { | ||
| std::string auditError; | ||
| if (!rules->m_auditLog->reopen(&auditError)) { | ||
| succeeded = false; | ||
| errorStr += auditError; | ||
| } | ||
| } | ||
|
|
||
| if (rules->m_debugLog != nullptr) { | ||
| std::string debugError; | ||
| if (!rules->m_debugLog->reopenDebugLogFile(&debugError)) { | ||
| succeeded = false; | ||
| if (!errorStr.empty()) { | ||
| errorStr += " "; | ||
| } | ||
| errorStr += debugError; | ||
| } | ||
| } | ||
|
|
||
| if (!succeeded) { | ||
| if (!errorStr.empty()) { | ||
| *error = strdup(errorStr.c_str()); | ||
| } else { | ||
| *error = strdup("Unknown error reopening logs"); | ||
| } | ||
| } | ||
|
|
||
| return succeeded ? 0 : -1; | ||
| } | ||
|
Comment on lines
+383
to
+384
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,10 +16,16 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "src/utils/shared_files.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <fcntl.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <sys/types.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <sys/stat.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef WIN32 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <algorithm> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define _CRT_SECURE_NO_DEPRECATE 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check failure on line 26 in src/utils/shared_files.cc
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+27
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace modsecurity { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace utils { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -81,6 +87,43 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool SharedFiles::reopen(const std::string& fileName, std::string *error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auto it = m_handlers.find(fileName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (it == m_handlers.end()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error->assign("Cannot find open file to reopen: " + fileName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct stat target_stat; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct stat current_stat; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stat(fileName.c_str(), &target_stat) == 0 && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fstat(fileno(it->second.fp), ¤t_stat) == 0 && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_stat.st_dev == target_stat.st_dev && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_stat.st_ino == target_stat.st_ino | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FILE *fp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef WIN32 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fopen_s(&fp, fileName.c_str(), "a"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fp = fopen(fileName.c_str(), "a"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 113 in src/utils/shared_files.cc
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fp == nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error->assign("Failed to reopen file: " + fileName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fclose(it->second.fp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it->second.fp = fp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+119
to
+122
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fclose(it->second.fp); | |
| it->second.fp = fp; | |
| // Acquire the same exclusive lock used by write() to avoid races on it->second.fp | |
| #ifndef WIN32 | |
| struct flock lock {}; | |
| lock.l_start = lock.l_len = lock.l_whence = 0; | |
| lock.l_type = F_WRLCK; | |
| if (fcntl(fileno(it->second.fp), F_SETLKW, &lock) == -1) { | |
| // Failed to acquire lock; close the newly opened file and report error | |
| fclose(fp); | |
| error->assign("Failed to lock file during reopen: " + fileName); | |
| return false; | |
| } | |
| #else | |
| DWORD dwWaitResult = WaitForSingleObject(it->second.hMutex, INFINITE); | |
| if (dwWaitResult != WAIT_OBJECT_0) { | |
| // Failed to acquire mutex; close the newly opened file and report error | |
| fclose(fp); | |
| error->assign("Couldn't lock shared file during reopen: " + fileName); | |
| return false; | |
| } | |
| #endif | |
| fclose(it->second.fp); | |
| it->second.fp = fp; | |
| // Release the exclusive lock | |
| #ifndef WIN32 | |
| lock.l_type = F_UNLCK; | |
| fcntl(fileno(it->second.fp), F_SETLKW, &lock); | |
| #else | |
| ::ReleaseMutex(it->second.hMutex); | |
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a new virtual method to
DebugLogchanges the class vtable layout, which is an ABI break for downstream C++ consumers that link against libmodsecurity but were compiled against an older header. Sinceheaders/modsecurity/*.hare installed public headers (src/Makefile.ampkginclude_HEADERS), consider making this method non-virtual (if polymorphic override isn’t required) or otherwise ensure an intentional ABI/SONAME bump and document it.