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