Skip to content

Commit 9c8f8f0

Browse files
committed
Support for PCRE2 in mlogc
1 parent 8fc0b51 commit 9c8f8f0

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

CHANGES

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
DD mmm YYYY - 2.9.x (to be released)
22
-------------------
33

4-
* Support for PCRE2
4+
* Support for PCRE2 in mlogc
55
[Issue #2737, #2827 - @martinhsv]
6+
* Support for PCRE2
7+
[Issue #2737 - @martinhsv]
68

79
07 Sep 2022 - 2.9.6
810
-------------------

mlogc/Makefile.am

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ mlogc_SOURCES = mlogc.c
77

88
mlogc_CPPFLAGS = @APR_CPPFLAGS@ \
99
@PCRE_CPPFLAGS@ \
10+
@PCRE2_CPPFLAGS@ \
1011
@CURL_CPPFLAGS@ \
1112
-I$(top_srcdir)/apache2
1213

1314
mlogc_CFLAGS = @APR_CFLAGS@ \
1415
@CURL_CFLAGS@ \
15-
@PCRE_CFLAGS@
16+
@PCRE_CFLAGS@ \
17+
@PCRE2_CFLAGS@
1618

1719
mlogc_LDFLAGS = @APR_LDFLAGS@ \
1820
@CURL_LDFLAGS@ \
19-
@PCRE_LDFLAGS@
21+
@PCRE_LDFLAGS@ \
22+
@PCRE2_LDFLAGS@
2023

2124
mlogc_LDADD = @APR_LDADD@ \
2225
@CURL_LDADD@ \
23-
@PCRE_LDADD@
26+
@PCRE_LDADD@ \
27+
@PCRE2_LDADD@

mlogc/mlogc.c

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ModSecurity for Apache 2.x, http://www.modsecurity.org/
3-
* Copyright (c) 2004-2013 Trustwave Holdings, Inc. (http://www.trustwave.com/)
3+
* Copyright (c) 2004-2022 Trustwave Holdings, Inc. (http://www.trustwave.com/)
44
*
55
* You may not use this file except in compliance with
66
* the License.  You may obtain a copy of the License at
@@ -28,7 +28,12 @@
2828
#if APR_HAVE_UNISTD_H
2929
#include <unistd.h> /* for getpid() */
3030
#endif
31+
#ifdef WITH_PCRE2
32+
#define PCRE2_CODE_UNIT_WIDTH 8
33+
#include <pcre2.h>
34+
#else
3135
#include <pcre.h>
36+
#endif
3237
#include <curl/curl.h>
3338
#include <fcntl.h>
3439
#include <sys/stat.h>
@@ -147,7 +152,13 @@ static int keep_alive = 150; /* Not used yet. */
147152
static int keep_alive_timeout = 300; /* Not used yet. */
148153
static int keep_entries = 0;
149154
static const char *log_repository = NULL;
155+
#ifdef WITH_PCRE2
156+
static pcre2_code *logline_regex = NULL;
157+
static pcre2_code *requestline_regex = NULL;
158+
#else
150159
static void *logline_regex = NULL;
160+
static void *requestline_regex = NULL;
161+
#endif
151162
static int max_connections = 10;
152163
static int max_worker_requests = 1000;
153164
static apr_global_mutex_t *gmutex = NULL;
@@ -161,7 +172,6 @@ static int ssl_validation = 0;
161172
static int tlsprotocol = 1;
162173
static curl_version_info_data* curlversion = NULL;
163174
/* static apr_time_t queue_time = 0; */
164-
static void *requestline_regex = NULL;
165175
static int running = 0;
166176
static const char *sensor_password = NULL;
167177
static const char *sensor_username = NULL;
@@ -1208,6 +1218,10 @@ static void logc_init(void)
12081218
int i, erroffset;
12091219
/* cURL major, minor and patch version */
12101220
short cmaj, cmin, cpat = 0;
1221+
#ifdef WITH_PCRE2
1222+
int pcre2_errorcode = 0;
1223+
PCRE2_SIZE pcre2_erroffset = 0;
1224+
#endif
12111225

12121226
queue = apr_array_make(pool, 64, sizeof(entry_t *));
12131227
if (queue == NULL) {
@@ -1311,16 +1325,26 @@ static void logc_init(void)
13111325
error_log(LOG_DEBUG2, NULL, "TLSv1.2 is unsupported in cURL %d.%d.%d", cmaj, cmin, cpat);
13121326
}
13131327

1328+
#ifdef WITH_PCRE2
1329+
logline_regex = pcre2_compile(logline_pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS,
1330+
&pcre2_errorcode, &pcre2_erroffset, NULL);
1331+
#else
13141332
logline_regex = pcre_compile(logline_pattern, PCRE_CASELESS,
13151333
&errptr, &erroffset, NULL);
1334+
#endif
13161335
if (logline_regex == NULL) {
13171336
error_log(LOG_ERROR, NULL,
13181337
"Failed to compile pattern: %s\n", logline_pattern);
13191338
logc_shutdown(1);
13201339
}
13211340

1322-
requestline_regex = pcre_compile(requestline_pattern,
1323-
PCRE_CASELESS, &errptr, &erroffset, NULL);
1341+
#ifdef WITH_PCRE2
1342+
requestline_regex = pcre2_compile(requestline_pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS,
1343+
&pcre2_errorcode, &pcre2_erroffset, NULL);
1344+
#else
1345+
requestline_regex = pcre_compile(requestline_pattern, PCRE_CASELESS,
1346+
&errptr, &erroffset, NULL);
1347+
#endif
13241348
if (requestline_regex == NULL) {
13251349
error_log(LOG_ERROR, NULL,
13261350
"Failed to compile pattern: %s\n", requestline_pattern);
@@ -1431,6 +1455,9 @@ static void * APR_THREAD_FUNC thread_worker(apr_thread_t *thread, void *data)
14311455
apr_status_t rc;
14321456
apr_finfo_t finfo;
14331457
int capturevector[CAPTUREVECTORSIZE];
1458+
#ifdef WITH_PCRE2
1459+
pcre2_match_data *pcre2_match_data = NULL;
1460+
#endif
14341461
int take_new = 1;
14351462
apr_pool_t *tpool;
14361463
struct curl_slist *headerlist = NULL;
@@ -1536,9 +1563,24 @@ static void * APR_THREAD_FUNC thread_worker(apr_thread_t *thread, void *data)
15361563
num_requests++;
15371564
}
15381565

1566+
#ifdef WITH_PCRE2
1567+
pcre2_match_data = pcre2_match_data_create_from_pattern(logline_regex, NULL);
1568+
rc = pcre2_match(logline_regex, entry->line, entry->line_size, 0, 0,
1569+
pcre2_match_data, NULL);
1570+
if (rc > 0) {
1571+
PCRE2_SIZE *pcre2_ovector = pcre2_get_ovector_pointer(pcre2_match_data);
1572+
for (int i = 0; i < rc; i++) {
1573+
capturevector[2*i] = pcre2_ovector[2*i];
1574+
capturevector[2*i+1] = pcre2_ovector[2*i+1];
1575+
}
1576+
}
1577+
pcre2_match_data_free(pcre2_match_data);
1578+
if (rc == PCRE2_ERROR_NOMATCH) {
1579+
#else
15391580
rc = pcre_exec(logline_regex, NULL, entry->line, entry->line_size, 0, 0,
15401581
capturevector, CAPTUREVECTORSIZE);
1541-
if (rc == PCRE_ERROR_NOMATCH) { /* No match. */
1582+
if (rc == PCRE_ERROR_NOMATCH) {
1583+
#endif
15421584
error_log(LOG_WARNING, thread,
15431585
"Invalid entry (failed to match regex): %s",
15441586
_log_escape(tpool, entry->line, entry->line_size));
@@ -2292,14 +2334,23 @@ static void usage(void) {
22922334
* Version text.
22932335
*/
22942336
static void version(void) {
2337+
#ifdef WITH_PCRE2
2338+
char pcre2_loaded_version_buffer[80] ={0};
2339+
char *pcre_loaded_version = pcre2_loaded_version_buffer;
2340+
pcre2_config(PCRE2_CONFIG_VERSION, pcre_loaded_version);
2341+
#endif
22952342
fprintf(stderr,
22962343
"ModSecurity Log Collector (mlogc) v%s\n", VERSION);
22972344
fprintf(stderr,
22982345
" APR: compiled=\"%s\"; "
22992346
"loaded=\"%s\"\n", APR_VERSION_STRING, apr_version_string());
23002347
fprintf(stderr,
23012348
" PCRE: compiled=\"%d.%d\"; "
2349+
#ifdef WITH_PCRE2
2350+
"loaded=\"%s\"\n", PCRE2_MAJOR, PCRE2_MINOR, pcre_loaded_version);
2351+
#else
23022352
"loaded=\"%s\"\n", PCRE_MAJOR, PCRE_MINOR, pcre_version());
2353+
#endif
23032354
fprintf(stderr,
23042355
" cURL: compiled=\"%s\"; "
23052356
"loaded=\"%s\"\n", LIBCURL_VERSION, curl_version());

0 commit comments

Comments
 (0)