Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Regex and string management functions. |
| 3 | * |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
Willy Tarreau | 7a9ac6d | 2016-12-21 19:13:14 +0100 | [diff] [blame] | 17 | #include <types/global.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 18 | #include <common/config.h> |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 19 | #include <common/defaults.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <common/regex.h> |
| 21 | #include <common/standard.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #include <proto/log.h> |
| 23 | |
| 24 | /* regex trash buffer used by various regex tests */ |
| 25 | regmatch_t pmatch[MAX_MATCH]; /* rm_so, rm_eo for regular expressions */ |
| 26 | |
Willy Tarreau | c874653 | 2014-05-28 23:05:07 +0200 | [diff] [blame] | 27 | int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | { |
| 29 | char *old_dst = dst; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 30 | char* dst_end = dst + dst_size; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
| 32 | while (*str) { |
| 33 | if (*str == '\\') { |
| 34 | str++; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 35 | if (!*str) |
| 36 | return -1; |
| 37 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 38 | if (isdigit((unsigned char)*str)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | int len, num; |
| 40 | |
| 41 | num = *str - '0'; |
| 42 | str++; |
| 43 | |
| 44 | if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) { |
| 45 | len = matches[num].rm_eo - matches[num].rm_so; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 46 | |
| 47 | if (dst + len >= dst_end) |
| 48 | return -1; |
| 49 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | memcpy(dst, src + matches[num].rm_so, len); |
| 51 | dst += len; |
| 52 | } |
| 53 | |
| 54 | } else if (*str == 'x') { |
| 55 | unsigned char hex1, hex2; |
| 56 | str++; |
| 57 | |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 58 | if (!*str) |
| 59 | return -1; |
| 60 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | hex1 = toupper(*str++) - '0'; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 62 | |
| 63 | if (!*str) |
| 64 | return -1; |
| 65 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | hex2 = toupper(*str++) - '0'; |
| 67 | |
| 68 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 69 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 70 | |
| 71 | if (dst >= dst_end) |
| 72 | return -1; |
| 73 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | *dst++ = (hex1<<4) + hex2; |
| 75 | } else { |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 76 | if (dst >= dst_end) |
| 77 | return -1; |
| 78 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | *dst++ = *str++; |
| 80 | } |
| 81 | } else { |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 82 | if (dst >= dst_end) |
| 83 | return -1; |
| 84 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 85 | *dst++ = *str++; |
| 86 | } |
| 87 | } |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 88 | if (dst >= dst_end) |
| 89 | return -1; |
| 90 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | *dst = '\0'; |
| 92 | return dst - old_dst; |
| 93 | } |
| 94 | |
| 95 | /* returns NULL if the replacement string <str> is valid, or the pointer to the first error */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 96 | const char *check_replace_string(const char *str) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | { |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 98 | const char *err = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | while (*str) { |
| 100 | if (*str == '\\') { |
| 101 | err = str; /* in case of a backslash, we return the pointer to it */ |
| 102 | str++; |
| 103 | if (!*str) |
| 104 | return err; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 105 | else if (isdigit((unsigned char)*str)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | err = NULL; |
| 107 | else if (*str == 'x') { |
| 108 | str++; |
| 109 | if (!ishex(*str)) |
| 110 | return err; |
| 111 | str++; |
| 112 | if (!ishex(*str)) |
| 113 | return err; |
| 114 | err = NULL; |
| 115 | } |
| 116 | else { |
| 117 | Warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str); |
| 118 | err = NULL; |
| 119 | } |
| 120 | } |
| 121 | str++; |
| 122 | } |
| 123 | return err; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /* returns the pointer to an error in the replacement string, or NULL if OK */ |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 128 | const char *chain_regex(struct hdr_exp **head, struct my_regex *preg, |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 129 | int action, const char *replace, void *cond) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 130 | { |
| 131 | struct hdr_exp *exp; |
| 132 | |
| 133 | if (replace != NULL) { |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 134 | const char *err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | err = check_replace_string(replace); |
| 136 | if (err) |
| 137 | return err; |
| 138 | } |
| 139 | |
| 140 | while (*head != NULL) |
| 141 | head = &(*head)->next; |
| 142 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 143 | exp = calloc(1, sizeof(*exp)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 144 | |
| 145 | exp->preg = preg; |
| 146 | exp->replace = replace; |
| 147 | exp->action = action; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 148 | exp->cond = cond; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 149 | *head = exp; |
| 150 | |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 154 | /* This function apply regex. It take const null terminated char as input. |
| 155 | * If the function doesn't match, it returns false, else it returns true. |
| 156 | * When it is compiled with JIT, this function execute strlen on the subject. |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 157 | * Currently the only supported flag is REG_NOTBOL. |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 158 | */ |
| 159 | int regex_exec_match(const struct my_regex *preg, const char *subject, |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 160 | size_t nmatch, regmatch_t pmatch[], int flags) { |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 161 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 162 | int ret; |
| 163 | int matches[MAX_MATCH * 3]; |
| 164 | int enmatch; |
| 165 | int i; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 166 | int options; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 167 | |
| 168 | /* Silently limit the number of allowed matches. max |
| 169 | * match i the maximum value for match, in fact this |
| 170 | * limit is not applyied. |
| 171 | */ |
| 172 | enmatch = nmatch; |
| 173 | if (enmatch > MAX_MATCH) |
| 174 | enmatch = MAX_MATCH; |
| 175 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 176 | options = 0; |
| 177 | if (flags & REG_NOTBOL) |
| 178 | options |= PCRE_NOTBOL; |
| 179 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 180 | /* The value returned by pcre_exec() is one more than the highest numbered |
| 181 | * pair that has been set. For example, if two substrings have been captured, |
| 182 | * the returned value is 3. If there are no capturing subpatterns, the return |
| 183 | * value from a successful match is 1, indicating that just the first pair of |
| 184 | * offsets has been set. |
| 185 | * |
| 186 | * It seems that this function returns 0 if it detect more matches than avalaible |
| 187 | * space in the matches array. |
| 188 | */ |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 189 | ret = pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, options, matches, enmatch * 3); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 190 | if (ret < 0) |
| 191 | return 0; |
| 192 | |
| 193 | if (ret == 0) |
| 194 | ret = enmatch; |
| 195 | |
| 196 | for (i=0; i<nmatch; i++) { |
| 197 | /* Copy offset. */ |
| 198 | if (i < ret) { |
| 199 | pmatch[i].rm_so = matches[(i*2)]; |
| 200 | pmatch[i].rm_eo = matches[(i*2)+1]; |
| 201 | continue; |
| 202 | } |
| 203 | /* Set the unmatvh flag (-1). */ |
| 204 | pmatch[i].rm_so = -1; |
| 205 | pmatch[i].rm_eo = -1; |
| 206 | } |
| 207 | return 1; |
| 208 | #else |
| 209 | int match; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 210 | |
| 211 | flags &= REG_NOTBOL; |
| 212 | match = regexec(&preg->regex, subject, nmatch, pmatch, flags); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 213 | if (match == REG_NOMATCH) |
| 214 | return 0; |
| 215 | return 1; |
| 216 | #endif |
| 217 | } |
| 218 | |
| 219 | /* This function apply regex. It take a "char *" ans length as input. The |
| 220 | * <subject> can be modified during the processing. If the function doesn't |
| 221 | * match, it returns false, else it returns true. |
| 222 | * When it is compiled with standard POSIX regex or PCRE, this function add |
| 223 | * a temporary null chracters at the end of the <subject>. The <subject> must |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 224 | * have a real length of <length> + 1. Currently the only supported flag is |
| 225 | * REG_NOTBOL. |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 226 | */ |
| 227 | int regex_exec_match2(const struct my_regex *preg, char *subject, int length, |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 228 | size_t nmatch, regmatch_t pmatch[], int flags) { |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 229 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 230 | int ret; |
| 231 | int matches[MAX_MATCH * 3]; |
| 232 | int enmatch; |
| 233 | int i; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 234 | int options; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 235 | |
| 236 | /* Silently limit the number of allowed matches. max |
| 237 | * match i the maximum value for match, in fact this |
| 238 | * limit is not applyied. |
| 239 | */ |
| 240 | enmatch = nmatch; |
| 241 | if (enmatch > MAX_MATCH) |
| 242 | enmatch = MAX_MATCH; |
| 243 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 244 | options = 0; |
| 245 | if (flags & REG_NOTBOL) |
| 246 | options |= PCRE_NOTBOL; |
| 247 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 248 | /* The value returned by pcre_exec() is one more than the highest numbered |
| 249 | * pair that has been set. For example, if two substrings have been captured, |
| 250 | * the returned value is 3. If there are no capturing subpatterns, the return |
| 251 | * value from a successful match is 1, indicating that just the first pair of |
| 252 | * offsets has been set. |
| 253 | * |
| 254 | * It seems that this function returns 0 if it detect more matches than avalaible |
| 255 | * space in the matches array. |
| 256 | */ |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 257 | ret = pcre_exec(preg->reg, preg->extra, subject, length, 0, options, matches, enmatch * 3); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 258 | if (ret < 0) |
| 259 | return 0; |
| 260 | |
| 261 | if (ret == 0) |
| 262 | ret = enmatch; |
| 263 | |
| 264 | for (i=0; i<nmatch; i++) { |
| 265 | /* Copy offset. */ |
| 266 | if (i < ret) { |
| 267 | pmatch[i].rm_so = matches[(i*2)]; |
| 268 | pmatch[i].rm_eo = matches[(i*2)+1]; |
| 269 | continue; |
| 270 | } |
| 271 | /* Set the unmatvh flag (-1). */ |
| 272 | pmatch[i].rm_so = -1; |
| 273 | pmatch[i].rm_eo = -1; |
| 274 | } |
| 275 | return 1; |
| 276 | #else |
| 277 | char old_char = subject[length]; |
| 278 | int match; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 279 | |
| 280 | flags &= REG_NOTBOL; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 281 | subject[length] = 0; |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 282 | match = regexec(&preg->regex, subject, nmatch, pmatch, flags); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 283 | subject[length] = old_char; |
| 284 | if (match == REG_NOMATCH) |
| 285 | return 0; |
| 286 | return 1; |
| 287 | #endif |
| 288 | } |
| 289 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 290 | int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err) |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 291 | { |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 292 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 293 | int flags = 0; |
| 294 | const char *error; |
| 295 | int erroffset; |
| 296 | |
| 297 | if (!cs) |
| 298 | flags |= PCRE_CASELESS; |
| 299 | if (!cap) |
| 300 | flags |= PCRE_NO_AUTO_CAPTURE; |
| 301 | |
| 302 | regex->reg = pcre_compile(str, flags, &error, &erroffset, NULL); |
| 303 | if (!regex->reg) { |
| 304 | memprintf(err, "regex '%s' is invalid (error=%s, erroffset=%d)", str, error, erroffset); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | regex->extra = pcre_study(regex->reg, PCRE_STUDY_JIT_COMPILE, &error); |
Christian Ruppert | 955f461 | 2014-10-29 17:05:53 +0100 | [diff] [blame] | 309 | if (!regex->extra && error != NULL) { |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 310 | pcre_free(regex->reg); |
| 311 | memprintf(err, "failed to compile regex '%s' (error=%s)", str, error); |
| 312 | return 0; |
| 313 | } |
| 314 | #else |
| 315 | int flags = REG_EXTENDED; |
| 316 | |
| 317 | if (!cs) |
| 318 | flags |= REG_ICASE; |
| 319 | if (!cap) |
| 320 | flags |= REG_NOSUB; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 321 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 322 | if (regcomp(®ex->regex, str, flags) != 0) { |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 323 | memprintf(err, "regex '%s' is invalid", str); |
| 324 | return 0; |
| 325 | } |
| 326 | #endif |
| 327 | return 1; |
| 328 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 329 | |
Willy Tarreau | 7a9ac6d | 2016-12-21 19:13:14 +0100 | [diff] [blame] | 330 | __attribute__((constructor)) |
| 331 | static void __regex_init(void) |
| 332 | { |
| 333 | char *ptr = NULL; |
| 334 | |
| 335 | #ifdef USE_PCRE |
| 336 | memprintf(&ptr, "Built with PCRE version : %s", (HAP_XSTRING(Z PCRE_PRERELEASE)[1] == 0)? |
| 337 | HAP_XSTRING(PCRE_MAJOR.PCRE_MINOR PCRE_DATE) : |
| 338 | HAP_XSTRING(PCRE_MAJOR.PCRE_MINOR) HAP_XSTRING(PCRE_PRERELEASE PCRE_DATE)); |
| 339 | memprintf(&ptr, "%s\nRunning on PCRE version : %s", ptr, pcre_version()); |
| 340 | |
| 341 | memprintf(&ptr, "%s\nPCRE library supports JIT : %s", ptr, |
| 342 | #ifdef USE_PCRE_JIT |
| 343 | ({ |
| 344 | int r; |
| 345 | pcre_config(PCRE_CONFIG_JIT, &r); |
| 346 | r ? "yes" : "no (libpcre build without JIT?)"; |
| 347 | }) |
| 348 | #else |
| 349 | "no (USE_PCRE_JIT not set)" |
| 350 | #endif |
| 351 | ); |
| 352 | #else |
| 353 | memprintf(&ptr, "Built without PCRE support (using libc's regex instead)"); |
| 354 | #endif |
| 355 | hap_register_build_opts(ptr, 1); |
| 356 | } |
| 357 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 358 | /* |
| 359 | * Local variables: |
| 360 | * c-indent-level: 8 |
| 361 | * c-basic-offset: 8 |
| 362 | * End: |
| 363 | */ |