1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <matching.h>
#include <eval_context.h>
#include <vars.h>
#include <promises.h>
#include <item_lib.h>
#include <conversion.h>
#include <scope.h>
#include <misc_lib.h>
#include <rlist.h>
#include <regex.h> /* CompileRegex,StringMatchFull */
#include <string_lib.h>
/* Pure, non-thread-safe */
static char *FirstBackReference(Regex *regex, const char *teststring)
{
static char backreference[CF_BUFSIZE]; /* GLOBAL_R, no initialization needed */
memset(backreference, 0, CF_BUFSIZE);
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
int result = pcre2_match(regex, (PCRE2_SPTR) teststring, PCRE2_ZERO_TERMINATED,
0, 0, match_data, NULL);
/* pcre2_match() returns the highest capture group number + 1, i.e. 1 means
* a match with 0 capture groups. 0 means the vector of offsets is small,
* negative numbers are errors (incl. no match). */
if (result > 0)
{
uint32_t num_pairs = pcre2_get_ovector_count(match_data);
if (num_pairs <= 1)
{
/* There was no match */
strlcpy(backreference, "CF_NOMATCH", CF_MAXVARSIZE);
pcre2_match_data_free(match_data);
RegexDestroy(regex);
return backreference;
}
size_t *ovector = pcre2_get_ovector_pointer(match_data);
/* ovector[0] and ovector[1] are for the start and end of the whole
* match, the capture groups follow in [2] and [3], etc. */
const char *backref_start = teststring + ovector[2];
size_t backref_len = ovector[3] - ovector[2];
if (backref_len < CF_MAXVARSIZE)
{
strncpy(backreference, backref_start, backref_len);
}
}
pcre2_match_data_free(match_data);
RegexDestroy(regex);
return backreference;
}
char *ExtractFirstReference(const char *regexp, const char *teststring)
{
char *backreference;
if ((regexp == NULL) || (teststring == NULL))
{
return "";
}
Regex *rx = CompileRegex(regexp);
if (rx == NULL)
{
return "";
}
backreference = FirstBackReference(rx, teststring);
if (strlen(backreference) == 0)
{
strlcpy(backreference, "CF_NOMATCH", CF_MAXVARSIZE);
}
return backreference;
}
bool IsRegex(const char *str)
{
const char *sp;
bool ret = false;
enum { r_norm, r_norepeat, r_literal } special = r_norepeat;
int bracket = 0;
int paren = 0;
/* Try to see when something is intended as a regular expression */
for (sp = str; *sp != '\0'; sp++)
{
if (special == r_literal)
{
special = r_norm;
continue;
}
else if (*sp == '\\')
{
special = r_literal;
continue;
}
else if (bracket && (*sp != ']'))
{
if (*sp == '[')
{
return false;
}
continue;
}
switch (*sp)
{
case '^':
special = (sp == str) ? r_norepeat : r_norm;
break;
case '*':
case '+':
if (special == r_norepeat)
{
return false;
}
special = r_norepeat;
ret = true;
break;
case '[':
special = r_norm;
bracket++;
ret = true;
break;
case ']':
if (bracket == 0)
{
return false;
}
bracket = 0;
special = r_norm;
break;
case '(':
special = r_norepeat;
paren++;
break;
case ')':
special = r_norm;
paren--;
if (paren < 0)
{
return false;
}
break;
case '|':
special = r_norepeat;
if (paren > 0)
{
ret = true;
}
break;
default:
special = r_norm;
}
}
if ((bracket != 0) || (paren != 0) || (special == r_literal))
{
return false;
}
else
{
return ret;
}
}
bool IsPathRegex(const char *str)
{
bool result = IsRegex(str);
if (result)
{
int s = 0, r = 0; /* Count square and round brackets. */
for (const char *sp = str; *sp != '\0'; sp++)
{
switch (*sp)
{
case '[':
s++;
break;
case ']':
s--;
break;
case '(':
r++;
break;
case ')':
r--;
break;
default:
if (*sp == FILE_SEPARATOR && (r || s))
{
Log(LOG_LEVEL_ERR,
"Path regular expression %s seems to use expressions containing the directory symbol %c", str,
FILE_SEPARATOR);
Log(LOG_LEVEL_ERR, "Use a work-around to avoid pathological behaviour");
return false;
}
break;
}
}
}
return result;
}
/* Checks whether item matches a list of wildcards */
bool IsRegexItemIn(const EvalContext *ctx, const Item *list, const char *regex)
{
for (const Item *ptr = list; ptr != NULL; ptr = ptr->next)
{
if (ctx != NULL && ptr->classes != NULL &&
!IsDefinedClass(ctx, ptr->classes))
{
continue;
}
/* Cheap pre-test: */
if (strcmp(regex, ptr->name) == 0)
{
return true;
}
/* Make it commutative */
if (StringMatchFull(regex, ptr->name) || StringMatchFull(ptr->name, regex))
{
return true;
}
}
return false;
}
/* Escapes non-alphanumeric chars, except sequence given in noEscSeq */
void EscapeSpecialChars(const char *str, char *strEsc, size_t strEscSz, char *noEscSeq, char *noEscList)
{
size_t strEscPos = 0;
if (noEscSeq == NULL)
{
noEscSeq = "";
}
if (noEscList == NULL)
{
noEscList = "";
}
memset(strEsc, 0, strEscSz);
for (const char *sp = str; (*sp != '\0') && (strEscPos < strEscSz - 2); sp++)
{
if (strncmp(sp, noEscSeq, strlen(noEscSeq)) == 0)
{
if (strEscSz <= strEscPos + strlen(noEscSeq))
{
Log(LOG_LEVEL_ERR,
"EscapeSpecialChars: Output string truncated. in='%s' out='%s'",
str, strEsc);
break;
}
strlcat(strEsc, noEscSeq, strEscSz);
strEscPos += strlen(noEscSeq);
sp += strlen(noEscSeq);
}
if (strchr(noEscList,*sp) != NULL)
{
// Found current char (*sp) in noEscList, do nothing
}
else if ((*sp != '\0') && (!isalnum((int)*sp)))
{
strEsc[strEscPos++] = '\\';
}
strEsc[strEscPos++] = *sp;
}
}
size_t EscapeRegexCharsLen(const char *str)
{
size_t ret = 2;
for (const char *sp = str; *sp != '\0'; sp++)
{
switch (*sp)
{
case '.':
case '*':
ret++;
break;
default:
break;
}
ret++;
}
return ret;
}
void EscapeRegexChars(char *str, char *strEsc, int strEscSz)
{
char *sp;
int strEscPos = 0;
memset(strEsc, 0, strEscSz);
for (sp = str; (*sp != '\0') && (strEscPos < strEscSz - 2); sp++)
{
switch (*sp)
{
case '.':
case '*':
strEsc[strEscPos++] = '\\';
break;
default:
break;
}
strEsc[strEscPos++] = *sp;
}
}
/* Escapes characters esc in the string str of size strSz */
char *EscapeChar(char *str, size_t strSz, char esc)
{
char strDup[CF_BUFSIZE];
size_t strPos, strDupPos;
if (sizeof(strDup) < strSz)
{
ProgrammingError("Too large string passed to EscapeCharInplace()");
}
snprintf(strDup, sizeof(strDup), "%s", str);
memset(str, 0, strSz);
for (strPos = 0, strDupPos = 0; strPos < strSz - 2; strPos++, strDupPos++)
{
if (strDup[strDupPos] == esc)
{
str[strPos] = '\\';
strPos++;
}
str[strPos] = strDup[strDupPos];
}
return str;
}
void AnchorRegex(const char *regex, char *out, int outSz)
{
if (NULL_OR_EMPTY(regex))
{
memset(out, 0, outSz);
}
else
{
snprintf(out, outSz, "^(%s)$", regex);
}
}
char *AnchorRegexNew(const char *regex)
{
if (NULL_OR_EMPTY(regex))
{
return xstrdup("^$");
}
char *ret = NULL;
xasprintf(&ret, "^(%s)$", regex);
return ret;
}
bool HasRegexMetaChars(const char *string)
{
if (!string)
{
return false;
}
if (string[strcspn(string, "\\^${}[]().*+?|<>-&")] == '\0') /* i.e. no metachars appear in string */
{
return false;
}
return true;
}
|