1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <match_scope.h>
26 
27 #include <matching.h>
28 #include <eval_context.h>
29 #include <string_lib.h>                                   /* StringFromLong */
30 #include <regex.h>                                        /* CompileRegex */
31 
32 
33 /* Sets variables */
RegExMatchSubString(EvalContext * ctx,pcre * rx,const char * teststring,int * start,int * end)34 static bool RegExMatchSubString(EvalContext *ctx, pcre *rx, const char *teststring, int *start, int *end)
35 {
36     int ovector[OVECCOUNT];
37     int rc = 0;
38 
39     if ((rc = pcre_exec(rx, NULL, teststring, strlen(teststring), 0, 0, ovector, OVECCOUNT)) >= 0)
40     {
41         *start = ovector[0];
42         *end = ovector[1];
43 
44         EvalContextVariableClearMatch(ctx);
45 
46         for (int i = 0; i < rc; i++)        /* make backref vars $(1),$(2) etc */
47         {
48             const char *backref_start = teststring + ovector[i * 2];
49             int backref_len = ovector[i * 2 + 1] - ovector[i * 2];
50 
51             if (backref_len < CF_MAXVARSIZE)
52             {
53                 char substring[CF_MAXVARSIZE];
54                 char *index = StringFromLong(i);
55                 strlcpy(substring, backref_start, MIN(CF_MAXVARSIZE, backref_len + 1));
56                 EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_MATCH, index, substring, CF_DATA_TYPE_STRING, "source=regex");
57                 free(index);
58             }
59         }
60     }
61     else
62     {
63         *start = 0;
64         *end = 0;
65     }
66 
67     pcre_free(rx);
68     return rc >= 0;
69 }
70 
71 /* Sets variables */
RegExMatchFullString(EvalContext * ctx,pcre * rx,const char * teststring)72 static bool RegExMatchFullString(EvalContext *ctx, pcre *rx, const char *teststring)
73 {
74     int match_start;
75     int match_len;
76 
77     if (RegExMatchSubString(ctx, rx, teststring, &match_start, &match_len))
78     {
79         return ((size_t) match_start == 0) && ((size_t) match_len == strlen(teststring));
80     }
81     else
82     {
83         return false;
84     }
85 }
86 
FullTextMatch(EvalContext * ctx,const char * regexp,const char * teststring)87 bool FullTextMatch(EvalContext *ctx, const char *regexp, const char *teststring)
88 {
89     pcre *rx;
90 
91     if (strcmp(regexp, teststring) == 0)
92     {
93         return true;
94     }
95 
96     rx = CompileRegex(regexp);
97     if (rx == NULL)
98     {
99         return false;
100     }
101 
102     if (RegExMatchFullString(ctx, rx, teststring))
103     {
104         return true;
105     }
106     else
107     {
108         return false;
109     }
110 }
111 
ValidateRegEx(const char * regex)112 bool ValidateRegEx(const char *regex)
113 {
114     pcre *rx = CompileRegex(regex);
115     bool regex_valid = rx != NULL;
116 
117     pcre_free(rx);
118     return regex_valid;
119 }
120 
BlockTextMatch(EvalContext * ctx,const char * regexp,const char * teststring,int * start,int * end)121 bool BlockTextMatch(EvalContext *ctx, const char *regexp, const char *teststring, int *start, int *end)
122 {
123     pcre *rx = CompileRegex(regexp);
124 
125     if (rx == NULL)
126     {
127         return false;
128     }
129 
130     if (RegExMatchSubString(ctx, rx, teststring, start, end))
131     {
132         return true;
133     }
134     else
135     {
136         return false;
137     }
138 }
139