Skip to content

Commit 1a2b139

Browse files
authored
Merge pull request #3321 from gberkes/refactor/default-pcre2
Refactor/default pcre2
2 parents a555e5a + d68aef3 commit 1a2b139

File tree

16 files changed

+129
-100
lines changed

16 files changed

+129
-100
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- {label: "wo geoip", opt: "--without-geoip" }
2727
- {label: "wo ssdeep", opt: "--without-ssdeep" }
2828
- {label: "with lmdb", opt: "--with-lmdb" }
29-
- {label: "with pcre2", opt: "--with-pcre2" }
29+
- {label: "with pcre", opt: "--with-pcre" }
3030
exclude:
3131
- platform: {label: "x32"}
3232
configure: {label: "wo geoip"}
@@ -88,7 +88,7 @@ jobs:
8888
- {label: "wo geoip", opt: "--without-geoip" }
8989
- {label: "wo ssdeep", opt: "--without-ssdeep" }
9090
- {label: "with lmdb", opt: "--with-lmdb" }
91-
- {label: "with pcre2", opt: "--with-pcre2" }
91+
- {label: "with pcre", opt: "--with-pcre" }
9292
steps:
9393
- name: Setup Dependencies
9494
# curl, pcre2 not installed because they're already

build/pcre.m4

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ AC_ARG_WITH(
2121
[test_paths="${with_pcre}"],
2222
[test_paths="/usr/local/libpcre /usr/local/pcre /usr/local /opt/libpcre /opt/pcre /opt /usr /opt/local"])
2323
24-
if test "x${with_pcre2}" != "x" && test "x${with_pcre2}" != "xno"; then
25-
AC_MSG_NOTICE([pcre2 specified; omitting check for pcre])
24+
if test "x${with_pcre}" == "x" && test "x${with_pcre}" != "xno"; then
25+
AC_MSG_NOTICE([Support for pcre not requested; omitting check for pcre])
2626
else
2727
2828
AC_MSG_CHECKING([for libpcre config script])
@@ -106,6 +106,7 @@ else
106106
LIBS=$save_LIBS
107107
fi
108108
109+
PCRE_CFLAGS="-DWITH_PCRE ${PCRE_CFLAGS}"
109110
AC_SUBST(PCRE_CONFIG)
110111
AC_SUBST(PCRE_VERSION)
111112
AC_SUBST(PCRE_CPPFLAGS)

build/pcre2.m4

+13-6
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ if test "x${with_pcre2}" == "xno"; then
2929
AC_MSG_NOTICE([Support for PCRE2 was disabled by the utilization of --without-pcre2 or --with-pcre2=no])
3030
PCRE2_DISABLED=yes
3131
else
32-
if test "x${with_pcre2}" == "xyes"; then
33-
PCRE2_MANDATORY=yes
34-
AC_MSG_NOTICE([PCRE2 support was marked as mandatory by the utilization of --with-pcre2=yes])
35-
fi
32+
PCRE2_MANDATORY=yes
33+
AC_MSG_NOTICE([PCRE2 is enabled by default.])
34+
# if test "x${with_pcre2}" == "xyes"; then
35+
# PCRE2_MANDATORY=yes
36+
# AC_MSG_NOTICE([PCRE2 support was marked as mandatory by the utilization of --with-pcre2=yes])
37+
# fi
3638
# for x in ${PCRE2_POSSIBLE_LIB_NAMES}; do
3739
# CHECK_FOR_PCRE2_AT(${x})
3840
# if test -n "${PCRE2_VERSION}"; then
@@ -96,9 +98,14 @@ else
9698
AC_MSG_NOTICE([PCRE2 is disabled by default.])
9799
else
98100
PCRE2_FOUND=1
99-
AC_MSG_NOTICE([using PCRE2 v${PCRE2_VERSION}])
100-
PCRE2_CFLAGS="-DWITH_PCRE2 ${PCRE2_CFLAGS}"
101+
PCRE2_CFLAGS="${PCRE2_CFLAGS}"
101102
PCRE2_DISPLAY="${PCRE2_LDADD}, ${PCRE2_CFLAGS}"
103+
AC_MSG_NOTICE([using PCRE2_VERSION ${PCRE2_VERSION}])
104+
AC_MSG_NOTICE([using PCRE2_LDADD ${PCRE2_LDADD}])
105+
AC_MSG_NOTICE([using PCRE2_LIBS ${PCRE2_LIBS}])
106+
AC_MSG_NOTICE([using PCRE2_LDFLAGS ${PCRE2_LDFLAGS}])
107+
AC_MSG_NOTICE([using PCRE2_CFLAGS ${PCRE2_CFLAGS}])
108+
AC_MSG_NOTICE([using PCRE2_DISPLAY ${PCRE2_DISPLAY}])
102109
AC_SUBST(PCRE2_VERSION)
103110
AC_SUBST(PCRE2_LDADD)
104111
AC_SUBST(PCRE2_LIBS)

configure.ac

+20-14
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,19 @@ AM_CONDITIONAL([YAJL_VERSION], [test "$YAJL_VERSION" != ""])
109109

110110
# Check for LibGeoIP
111111
PROG_GEOIP
112-
AM_CONDITIONAL([GEOIP_CFLAGS], [test "GEOIP_CFLAGS" != ""])
113112

114113
# Check for MaxMind
115114
PROG_MAXMIND
116-
AM_CONDITIONAL([MAXMIND_CFLAGS], [test "MAXMIND_CFLAGS" != ""])
117115

118116

119117
# Check for LMDB
120118
PROG_LMDB
121-
AM_CONDITIONAL([LMDB_CFLAGS], [test "LMDB_CFLAGS" != ""])
122119

123120
# Check for SSDEEP
124121
CHECK_SSDEEP
125-
AM_CONDITIONAL([SSDEEP_CFLAGS], [test "SSDEEP_CFLAGS" != ""])
126122

127123
# Check for LUA
128124
CHECK_LUA
129-
AM_CONDITIONAL([LUA_CFLAGS], [test "LUA_CFLAGS" != ""])
130125

131126

132127
#
@@ -146,16 +141,16 @@ CHECK_LIBXML2
146141

147142

148143
#
149-
# Check for libpcre
144+
# Check for libpcre only if explicitly requested
150145
#
151-
CHECK_PCRE
152-
153-
154-
#
155-
# Check for pcre2
156-
#
157-
PROG_PCRE2
158-
AM_CONDITIONAL([PCRE2_CFLAGS], [test "PCRE2_CFLAGS" != ""])
146+
if test "x${with_pcre}" != "x" && test "x${with_pcre}" != "xno"; then
147+
CHECK_PCRE
148+
else
149+
#
150+
# Check for pcre2
151+
#
152+
PROG_PCRE2
153+
fi
159154

160155

161156
# Checks for header files.
@@ -587,6 +582,17 @@ if test "x$LUA_FOUND" = "x2"; then
587582
echo " + LUA ....disabled"
588583
fi
589584

585+
##PCRE
586+
if test "x${with_pcre}" != "x" \
587+
&& test "x${with_pcre}" != "xno" \
588+
&& test "x${PCRE_VERSION}" == "x"; then
589+
AC_MSG_NOTICE([*** pcre library not found.])
590+
else
591+
echo " + PCRE ....found "
592+
echo " using pcre v${PCRE_VERSION}"
593+
echo " ${PCRE_LDADD}, ${PCRE_CFLAGS}"
594+
fi
595+
590596

591597
## PCRE2
592598
if test "x$PCRE2_FOUND" = "x0"; then

examples/multithread/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ multithread_LDADD = \
1414
$(MAXMIND_LDADD) \
1515
$(LUA_LDADD) \
1616
$(PCRE_LDADD) \
17+
$(PCRE2_LDADD) \
1718
$(SSDEEP_LDADD) \
1819
$(YAJL_LDADD)
1920

@@ -46,6 +47,7 @@ multithread_CPPFLAGS = \
4647
$(LMDB_CFLAGS) \
4748
$(LUA_CFLAGS) \
4849
$(PCRE_CFLAGS) \
50+
$(PCRE2_CFLAGS) \
4951
$(LIBXML2_CFLAGS)
5052

5153

examples/reading_logs_via_rule_message/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ simple_request_LDADD = \
1414
$(MAXMIND_LDADD) \
1515
$(LUA_LDADD) \
1616
$(PCRE_LDADD) \
17+
$(PCRE2_LDADD) \
1718
$(SSDEEP_LDADD) \
1819
$(YAJL_LDADD)
1920

@@ -46,6 +47,7 @@ simple_request_CPPFLAGS = \
4647
$(LMDB_CFLAGS) \
4748
$(LUA_CFLAGS) \
4849
$(PCRE_CFLAGS) \
50+
$(PCRE2_CFLAGS) \
4951
$(LIBXML2_CFLAGS)
5052

5153

examples/reading_logs_with_offset/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ read_LDADD = \
1414
$(LMDB_LDADD) \
1515
$(LUA_LDADD) \
1616
$(PCRE_LDADD) \
17+
$(PCRE2_LDADD) \
1718
$(SSDEEP_LDADD) \
1819
$(YAJL_LDADD)
1920

@@ -46,6 +47,7 @@ read_CPPFLAGS = \
4647
$(LMDB_CFLAGS) \
4748
$(LUA_CFLAGS) \
4849
$(PCRE_CFLAGS) \
50+
$(PCRE2_CFLAGS) \
4951
$(LIBXML2_CFLAGS)
5052

5153

examples/using_bodies_in_chunks/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ simple_request_LDADD = \
1414
$(LMDB_LDADD) \
1515
$(LUA_LDADD) \
1616
$(PCRE_LDADD) \
17+
$(PCRE2_LDADD) \
1718
$(SSDEEP_LDADD) \
1819
$(YAJL_LDADD)
1920

@@ -46,6 +47,7 @@ simple_request_CPPFLAGS = \
4647
$(LMDB_CFLAGS) \
4748
$(LUA_CFLAGS) \
4849
$(PCRE_CFLAGS) \
50+
$(PCRE2_CFLAGS) \
4951
$(LIBXML2_CFLAGS)
5052

5153
MAINTAINERCLEANFILES = \

src/operators/verify_cc.cc

+22-22
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
#include "src/operators/operator.h"
2323

24-
#ifndef WITH_PCRE2
24+
#ifdef WITH_PCRE
2525
#if PCRE_HAVE_JIT
2626
#define pcre_study_opt PCRE_STUDY_JIT_COMPILE
2727
#else
28-
#define pcre_study_opt 0
28+
constexpr int pcre_study_opt = 0;
2929
#endif
3030
#endif
3131

@@ -34,20 +34,20 @@ namespace modsecurity {
3434
namespace operators {
3535

3636
VerifyCC::~VerifyCC() {
37-
#if WITH_PCRE2
37+
#ifndef WITH_PCRE
3838
pcre2_code_free(m_pc);
3939
#else
40-
if (m_pc != NULL) {
40+
if (m_pc != nullptr) {
4141
pcre_free(m_pc);
42-
m_pc = NULL;
42+
m_pc = nullptr;
4343
}
44-
if (m_pce != NULL) {
44+
if (m_pce != nullptr) {
4545
#if PCRE_HAVE_JIT
4646
pcre_free_study(m_pce);
4747
#else
4848
pcre_free(m_pce);
4949
#endif
50-
m_pce = NULL;
50+
m_pce = nullptr;
5151
}
5252
#endif
5353
}
@@ -94,33 +94,33 @@ int VerifyCC::luhnVerify(const char *ccnumber, int len) {
9494

9595

9696
bool VerifyCC::init(const std::string &param2, std::string *error) {
97-
#ifdef WITH_PCRE2
97+
#ifndef WITH_PCRE
9898
PCRE2_SPTR pcre2_pattern = reinterpret_cast<PCRE2_SPTR>(m_param.c_str());
9999
uint32_t pcre2_options = (PCRE2_DOTALL|PCRE2_MULTILINE);
100100
int errornumber = 0;
101101
PCRE2_SIZE erroroffset = 0;
102102
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED,
103-
pcre2_options, &errornumber, &erroroffset, NULL);
104-
if (m_pc == NULL) {
103+
pcre2_options, &errornumber, &erroroffset, nullptr);
104+
if (m_pc == nullptr) {
105105
return false;
106106
}
107107
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
108108
#else
109-
const char *errptr = NULL;
109+
const char *errptr = nullptr;
110110
int erroffset = 0;
111111

112112
m_pc = pcre_compile(m_param.c_str(), PCRE_DOTALL|PCRE_MULTILINE,
113-
&errptr, &erroffset, NULL);
114-
if (m_pc == NULL) {
113+
&errptr, &erroffset, nullptr);
114+
if (m_pc == nullptr) {
115115
error->assign(errptr);
116116
return false;
117117
}
118118

119119
m_pce = pcre_study(m_pc, pcre_study_opt, &errptr);
120-
if (m_pce == NULL) {
121-
if (errptr == NULL) {
120+
if (m_pce == nullptr) {
121+
if (errptr == nullptr) {
122122
/*
123-
* Per pcre_study(3) m_pce == NULL && errptr == NULL means
123+
* Per pcre_study(3) m_pce == nullptr && errptr == nullptr means
124124
* that no addional information is found, so no need to study
125125
*/
126126
return true;
@@ -136,21 +136,21 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
136136

137137
bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
138138
const std::string& i, RuleMessage &ruleMessage) {
139-
#ifdef WITH_PCRE2
139+
#ifndef WITH_PCRE
140140
PCRE2_SIZE offset = 0;
141141
size_t target_length = i.length();
142142
PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(i.c_str());
143-
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
143+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr);
144144

145145
int ret;
146146
for (offset = 0; offset < target_length; offset++) {
147147

148148
if (m_pcje == 0) {
149-
ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL);
149+
ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, nullptr);
150150
}
151151

152152
if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) {
153-
ret = pcre2_match(m_pc, pcre2_i, target_length, offset, PCRE2_NO_JIT, match_data, NULL);
153+
ret = pcre2_match(m_pc, pcre2_i, target_length, offset, PCRE2_NO_JIT, match_data, nullptr);
154154
}
155155

156156
/* If there was no match, then we are done. */
@@ -192,15 +192,15 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
192192
"\" at " + i + ". [offset " +
193193
std::to_string(offset) + "]");
194194
}
195-
#ifdef WITH_PCRE2
195+
#ifndef WITH_PCRE
196196
pcre2_match_data_free(match_data);
197197
#endif
198198
return true;
199199
}
200200
}
201201
}
202202

203-
#ifdef WITH_PCRE2
203+
#ifndef WITH_PCRE
204204
pcre2_match_data_free(match_data);
205205
#endif
206206

src/operators/verify_cc.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifndef SRC_OPERATORS_VERIFY_CC_H_
1717
#define SRC_OPERATORS_VERIFY_CC_H_
1818

19-
#if WITH_PCRE2
19+
#ifndef WITH_PCRE
2020
#define PCRE2_CODE_UNIT_WIDTH 8
2121
#include <pcre2.h>
2222
#else
@@ -38,12 +38,12 @@ class VerifyCC : public Operator {
3838
/** @ingroup ModSecurity_Operator */
3939
explicit VerifyCC(std::unique_ptr<RunTimeString> param)
4040
: Operator("VerifyCC", std::move(param)),
41-
#if WITH_PCRE2
42-
m_pc(NULL),
41+
#ifndef WITH_PCRE
42+
m_pc(nullptr),
4343
m_pcje(PCRE2_ERROR_JIT_BADOPTION) { }
4444
#else
45-
m_pc(NULL),
46-
m_pce(NULL) { }
45+
m_pc(nullptr),
46+
m_pce(nullptr) { }
4747
#endif
4848
~VerifyCC() override;
4949

@@ -52,7 +52,7 @@ class VerifyCC : public Operator {
5252
RuleMessage &ruleMessage) override;
5353
bool init(const std::string &param, std::string *error) override;
5454
private:
55-
#if WITH_PCRE2
55+
#ifndef WITH_PCRE
5656
pcre2_code *m_pc;
5757
int m_pcje;
5858
#else

src/parser/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ libmodsec_parser_la_CPPFLAGS = \
2525
$(YAJL_CFLAGS) \
2626
$(LMDB_CFLAGS) \
2727
$(PCRE_CFLAGS) \
28+
$(PCRE2_CFLAGS) \
2829
$(LIBXML2_CFLAGS)
2930

3031
test.cc: seclang-parser.hh

0 commit comments

Comments
 (0)