Skip to content

Commit 6fa0f1b

Browse files
committed
Migrate to PCRE2
PCRE has been deprecated for long enough. Ticket: ENT-10629 Changelog: CFEngine now uses PCRE2 for regular expressions
1 parent c74ea5f commit 6fa0f1b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+227
-242
lines changed

.github/workflows/job-static-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
sudo apt-get install -y dpkg-dev debhelper g++ libncurses5 pkg-config \
4242
build-essential libpam0g-dev fakeroot gcc make autoconf buildah \
4343
liblmdb-dev libacl1-dev libcurl4-openssl-dev libyaml-dev libxml2-dev \
44-
libssl-dev libpcre3-dev
44+
libssl-dev libpcre2-dev
4545
4646
- name: Run Autogen
4747
run: NO_CONFIGURE=1 PROJECT=community ./buildscripts/build-scripts/autogen

.github/workflows/macos_unit_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
with:
1212
submodules: recursive
1313
- name: Install dependencies
14-
run: brew install lmdb automake openssl pcre
14+
run: brew install lmdb automake openssl pcre2
1515
- name: Run autotools / configure
1616
run: ./autogen.sh --enable-debug
1717
- name: Compile and link

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ Keep in mind that these are guidelines, there will always be some situations whe
294294
```
295295
AM_CFLAGS = \
296296
<TAB>$(OPENSSL_CFLAGS) \
297-
<TAB>$(PCRE_CFLAGS) \
297+
<TAB>$(PCRE2_CFLAGS) \
298298
<TAB>$(ENTERPRISE_CFLAGS)
299299
```
300300
* Inside an `if`, you cannot indent with tabs (lines will be silently skipped):

INSTALL

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ In order to build CFEngine you need the following tools and libraries installed:
1616

1717
* PAM library
1818
* OpenSSL library
19-
* PCRE library
19+
* PCRE2 library
2020
* POSIX threads (pthreads) library, if not provided by the operating system
2121
* Latest available LMDB (Lightning Memory-mapped DataBase), Tokyo Cabinet or QDBM
2222

@@ -119,33 +119,33 @@ $ sudo yum install epel-release && sudo yum update
119119
Or on RHEL, replacing the version number with yours:
120120
$ sudo subscription-manager repos --enable codeready-builder-for-rhel-9-x86_64-rpms && sudo yum update
121121

122-
$ sudo yum install -y gcc gdb make git libtool autoconf automake byacc flex openssl-devel pcre-devel lmdb-devel pam-devel flex-devel libyaml-devel fakeroot libxml2-devel
122+
$ sudo yum install -y gcc gdb make git libtool autoconf automake byacc flex openssl-devel pcre2-devel lmdb-devel pam-devel flex-devel libyaml-devel fakeroot libxml2-devel
123123

124124
For SELinux support you will need selinux-policy-devel package and specify `--with-selinux-policy` to `autogen.sh` or `configure`
125125

126126
* Debian (Debian 12 2023-10-09)
127127

128-
$ sudo apt-get install -y build-essential git libtool autoconf automake bison flex libssl-dev libpcre3-dev libbison-dev libacl1 libacl1-dev lmdb-utils liblmdb-dev libpam0g-dev libtool libyaml-dev libxml2-dev
128+
$ sudo apt-get install -y build-essential git libtool autoconf automake bison flex libssl-dev libpcre2-dev libbison-dev libacl1 libacl1-dev lmdb-utils liblmdb-dev libpam0g-dev libtool libyaml-dev libxml2-dev
129129

130130
* FreeBSD (12.1 2020-04-07)
131131

132132
See docs/BSD.md
133133

134134
* SUSE (Tumbleweed 2020-02-02)
135135

136-
$ sudo zypper install gdb gcc make lmdb autoconf automake libtool git python3 pcre-devel libopenssl-devel pam-devel
136+
$ sudo zypper install gdb gcc make lmdb autoconf automake libtool git python3 pcre2-devel libopenssl-devel pam-devel
137137

138138
* AlpineOS (3.11.3 x86_64 2020-04-13)
139139

140-
$ sudo apk add alpine-sdk lmdb-dev openssl-dev bison flex-dev acl-dev pcre-dev autoconf automake libtool git python3 gdb
140+
$ sudo apk add alpine-sdk lmdb-dev openssl-dev bison flex-dev acl-dev pcre2-dev autoconf automake libtool git python3 gdb
141141
$ ./autogen.sh --without-pam
142142

143143
* Termux (2020-04-24)
144144

145-
$ pkg install build-essential git autoconf automake bison flex liblmdb openssl pcre libacl libyaml
145+
$ pkg install build-essential git autoconf automake bison flex liblmdb openssl pcre2 libacl libyaml
146146
$ ./autogen.sh --without-pam
147147

148148
* OSX (2021-10-20)
149149

150-
brew install openssl lmdb autoconf automake libtool bison flex pcre m4 gcc make
150+
brew install openssl lmdb autoconf automake libtool bison flex pcre2 m4 gcc make
151151
./autogen.sh --enable-debug

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This file contains a copy of:
77

88
CFEngine is provided under the terms of the GNU General Public License version 3
99
(below), with explicit permission to link with the OpenSSL library, BerkeleyDB
10-
library and and PCRE library.
10+
library and PCRE2 library.
1111

1212
On some systems, code under the Frontier Artistic License
1313
(/libcompat/snprintf) might become compiled. This is compatible with the

cf-agent/Makefile.am

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ AM_CPPFLAGS = -I$(srcdir)/../libpromises -I$(srcdir)/../libntech/libutils \
3030
@CPPFLAGS@ \
3131
$(ENTERPRISE_CPPFLAGS) \
3232
$(OPENSSL_CPPFLAGS) \
33-
$(PCRE_CPPFLAGS) \
33+
$(PCRE2_CPPFLAGS) \
3434
$(LIBVIRT_CPPFLAGS) \
3535
$(POSTGRESQL_CPPFLAGS) \
3636
$(MYSQL_CPPFLAGS) \
@@ -41,7 +41,7 @@ AM_CFLAGS = \
4141
@CFLAGS@ \
4242
$(ENTERPRISE_CFLAGS) \
4343
$(OPENSSL_CFLAGS) \
44-
$(PCRE_CFLAGS) \
44+
$(PCRE2_CFLAGS) \
4545
$(LIBVIRT_CFLAGS) \
4646
$(POSTGRESQL_CFLAGS) \
4747
$(MYSQL_CFLAGS) \
@@ -51,7 +51,7 @@ AM_CFLAGS = \
5151
AM_LDFLAGS = \
5252
@LDFLAGS@ \
5353
$(OPENSSL_LDFLAGS) \
54-
$(PCRE_LDFLAGS) \
54+
$(PCRE2_LDFLAGS) \
5555
$(LIBVIRT_LDFLAGS) \
5656
$(POSTGRESQL_LDFLAGS) \
5757
$(MYSQL_LDFLAGS) \
@@ -64,7 +64,7 @@ endif
6464

6565
libcf_agent_la_LIBADD = ../libpromises/libpromises.la \
6666
$(OPENSSL_LIBS) \
67-
$(PCRE_LIBS) \
67+
$(PCRE2_LIBS) \
6868
$(LIBVIRT_LIBS) \
6969
$(POSTGRESQL_LIBS) \
7070
$(MYSQL_LIBS) \

cf-agent/files_editxml.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#include <cf3.defs.h>
2626

27-
#include <pcre.h>
2827
#include <actuator.h>
2928
#include <eval_context.h>
3029
#include <promises.h>
@@ -42,6 +41,7 @@
4241
#include <policy.h>
4342
#include <ornaments.h>
4443
#include <verify_classes.h>
44+
#include <regex.h> /* StringMatch() */
4545

4646
enum editxmltypesequence
4747
{
@@ -2981,20 +2981,7 @@ xmlChar *CharToXmlChar(char c[CF_BUFSIZE])
29812981

29822982
static bool ContainsRegex(const char* rawstring, const char* regex)
29832983
{
2984-
int ovector[OVECCOUNT], rc;
2985-
const char *errorstr;
2986-
int erroffset;
2987-
2988-
pcre *rx = pcre_compile(regex, 0, &errorstr, &erroffset, NULL);
2989-
2990-
if ((rc = pcre_exec(rx, NULL, rawstring, strlen(rawstring), 0, 0, ovector, OVECCOUNT)) >= 0)
2991-
{
2992-
pcre_free(rx);
2993-
return true;
2994-
}
2995-
2996-
pcre_free(rx);
2997-
return false;
2984+
return StringMatch(regex, rawstring, NULL, NULL);
29982985
}
29992986

30002987
/*********************************************************************/

cf-agent/verify_users_pam.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <files_lib.h>
3636
#include <eval_context.h>
3737
#include <regex.h> // CompileRegex()
38+
#include <buffer.h> // BufferData()
3839

3940
#include <cf3.defs.h>
4041
#include <verify_methods.h>
@@ -146,8 +147,8 @@ static bool GetAIXShadowHash(const char *puser, const char **result)
146147
size_t puser_len = strlen(puser);
147148
char name_regex_str[strlen(puser) + 3];
148149

149-
pcre *name_regex = CompileRegex("^(\\S+):");
150-
pcre *hash_regex = CompileRegex("^\\s+password\\s*=\\s*(\\S+)");
150+
pcre2_code *name_regex = CompileRegex("^(\\S+):");
151+
pcre2_code *hash_regex = CompileRegex("^\\s+password\\s*=\\s*(\\S+)");
151152
bool in_user_section = false;
152153

153154
while (true)
@@ -162,13 +163,13 @@ static bool GetAIXShadowHash(const char *puser, const char **result)
162163
goto end;
163164
}
164165

165-
int submatch_vec[6];
166166

167-
int pcre_result = pcre_exec(name_regex, NULL, buf, strlen(buf), 0, 0, submatch_vec, 6);
168-
if (pcre_result >= 0)
167+
size_t match_start;
168+
size_t match_end;
169+
if (StringMatchWithPrecompiledRegex(name_regex, buf, &match_start, &match_end))
169170
{
170-
if (submatch_vec[3] - submatch_vec[2] == puser_len
171-
&& strncmp(buf + submatch_vec[2], puser, puser_len) == 0)
171+
/* Compare the part without the ':' */
172+
if (StringEqualN(buf, puser, match_end - match_start - 1))
172173
{
173174
in_user_section = true;
174175
}
@@ -178,35 +179,27 @@ static bool GetAIXShadowHash(const char *puser, const char **result)
178179
}
179180
continue;
180181
}
181-
else if (pcre_result != PCRE_ERROR_NOMATCH)
182-
{
183-
errno = EINVAL;
184-
goto end;
185-
}
186-
187182
if (!in_user_section)
188183
{
189184
continue;
190185
}
191186

192-
pcre_result = pcre_exec(hash_regex, NULL, buf, strlen(buf), 0, 0, submatch_vec, 6);
193-
if (pcre_result >= 0)
187+
Seq *captures = StringMatchCapturesWithPrecompiledRegex(hash_regex, buf, false);
188+
if (captures != NULL)
194189
{
195-
memcpy(hash_buf, buf + submatch_vec[2], submatch_vec[3] - submatch_vec[2]);
190+
/* captures are buffers, the first one being the full match, the
191+
* second being the first capture group, etc. */
192+
StringCopy(BufferData(SeqAt(captures, 1)), hash_buf, sizeof(hash_buf));
196193
*result = hash_buf;
197194
ret = true;
198-
goto end;
199-
}
200-
else if (pcre_result != PCRE_ERROR_NOMATCH)
201-
{
202-
errno = EINVAL;
195+
SeqDestroy(captures);
203196
goto end;
204197
}
205198
}
206199

207200
end:
208-
pcre_free(name_regex);
209-
pcre_free(hash_regex);
201+
pcre2_code_free(name_regex);
202+
pcre2_code_free(hash_regex);
210203
free(buf);
211204
fclose(fptr);
212205
return ret;

cf-check/Makefile.am

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,28 @@ AM_CPPFLAGS = -I$(srcdir)/../libntech/libutils \
2727
-I$(srcdir)/../libntech/libcompat \
2828
-I$(srcdir)/../libcfecompat \
2929
@CPPFLAGS@ \
30-
$(PCRE_CPPFLAGS) \
30+
$(PCRE2_CPPFLAGS) \
3131
$(LIBYAML_CPPFLAGS) \
3232
$(LMDB_CPPFLAGS) \
3333
$(OPENSSL_CPPFLAGS)
3434

3535
AM_CFLAGS = \
3636
@CFLAGS@ \
3737
$(LMDB_CFLAGS) \
38-
$(PCRE_CFLAGS) \
38+
$(PCRE2_CFLAGS) \
3939
$(LIBYAML_CFLAGS) \
4040
$(PTHREAD_CFLAGS)
4141

4242
AM_LDFLAGS = \
4343
@LDFLAGS@ \
44-
$(PCRE_LDFLAGS) \
44+
$(PCRE2_LDFLAGS) \
4545
$(LIBYAML_LDFLAGS) \
4646
$(LMDB_LDFLAGS)
4747

4848
libcf_check_la_LIBADD = ../libntech/libutils/libutils.la \
4949
../libcfecompat/libcfecompat.la \
5050
$(LMDB_LIBS) \
51-
$(PCRE_LIBS) \
51+
$(PCRE2_LIBS) \
5252
$(LIBYAML_LIBS) \
5353
$(PTHREAD_LIBS) \
5454
$(OPENSSL_LIBS)

cf-execd/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ AM_CPPFLAGS = \
3030
-I$(srcdir)/../libcfnet \
3131
-I$(srcdir)/../libenv \
3232
-I$(srcdir)/../cf-check \
33-
$(PCRE_CPPFLAGS) \
33+
$(PCRE2_CPPFLAGS) \
3434
$(OPENSSL_CPPFLAGS) \
3535
$(ENTERPRISE_CPPFLAGS)
3636

3737
AM_CFLAGS = \
38-
$(PCRE_CFLAGS) \
38+
$(PCRE2_CFLAGS) \
3939
$(OPENSSL_CFLAGS) \
4040
$(PTHREAD_CFLAGS) \
4141
$(ENTERPRISE_CFLAGS)

0 commit comments

Comments
 (0)