Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2 | * include/common/regex.h |
| 3 | * This file defines everything related to regular expressions. |
| 4 | * |
| 5 | * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_REGEX_H |
| 23 | #define _COMMON_REGEX_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Thierry FOURNIER | e28f1ec | 2013-10-09 15:23:01 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
Thierry FOURNIER | ec9a58c | 2015-11-26 19:33:54 +0100 | [diff] [blame] | 26 | #include <string.h> |
Thierry FOURNIER | e28f1ec | 2013-10-09 15:23:01 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 28 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
| 30 | #ifdef USE_PCRE |
| 31 | #include <pcre.h> |
| 32 | #include <pcreposix.h> |
Christian Ruppert | de89871 | 2014-11-18 13:03:58 +0100 | [diff] [blame] | 33 | |
| 34 | /* For pre-8.20 PCRE compatibility */ |
| 35 | #ifndef PCRE_STUDY_JIT_COMPILE |
| 36 | #define PCRE_STUDY_JIT_COMPILE 0 |
| 37 | #endif |
| 38 | |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 39 | #elif USE_PCRE2 |
| 40 | #include <pcre2.h> |
| 41 | #include <pcre2posix.h> |
| 42 | |
| 43 | #else /* no PCRE, nor PCRE2 */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 44 | #include <regex.h> |
| 45 | #endif |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 46 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 47 | struct my_regex { |
| 48 | #ifdef USE_PCRE |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 49 | pcre *reg; |
| 50 | pcre_extra *extra; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 51 | #ifdef USE_PCRE_JIT |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 52 | #ifndef PCRE_CONFIG_JIT |
| 53 | #error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT." |
| 54 | #endif |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 55 | #endif |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 56 | #elif USE_PCRE2 |
| 57 | pcre2_code *reg; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 58 | #else /* no PCRE */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 59 | regex_t regex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #endif |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 61 | }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | |
| 63 | /* what to do when a header matches a regex */ |
| 64 | #define ACT_ALLOW 0 /* allow the request */ |
| 65 | #define ACT_REPLACE 1 /* replace the matching header */ |
| 66 | #define ACT_REMOVE 2 /* remove the matching header */ |
| 67 | #define ACT_DENY 3 /* deny the request */ |
| 68 | #define ACT_PASS 4 /* pass this header without allowing or denying the request */ |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 69 | #define ACT_TARPIT 5 /* tarpit the connection matching this request */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | |
| 71 | struct hdr_exp { |
| 72 | struct hdr_exp *next; |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 73 | struct my_regex *preg; /* expression to look for */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 75 | const char *replace; /* expression to set instead */ |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 76 | void *cond; /* a possible condition or NULL */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | extern regmatch_t pmatch[MAX_MATCH]; |
| 80 | |
Thierry FOURNIER | ed5a4ae | 2013-10-14 14:07:36 +0200 | [diff] [blame] | 81 | /* "str" is the string that contain the regex to compile. |
| 82 | * "regex" is preallocated memory. After the execution of this function, this |
| 83 | * struct contain the compiled regex. |
| 84 | * "cs" is the case sensitive flag. If cs is true, case sensitive is enabled. |
| 85 | * "cap" is capture flag. If cap if true the regex can capture into |
| 86 | * parenthesis strings. |
| 87 | * "err" is the standar error message pointer. |
| 88 | * |
| 89 | * The function return 1 is succes case, else return 0 and err is filled. |
| 90 | */ |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 91 | int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err); |
Willy Tarreau | c874653 | 2014-05-28 23:05:07 +0200 | [diff] [blame] | 92 | int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches); |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 93 | const char *check_replace_string(const char *str); |
Thierry FOURNIER | 09af0d6 | 2014-06-18 11:35:54 +0200 | [diff] [blame] | 94 | const char *chain_regex(struct hdr_exp **head, struct my_regex *preg, |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 95 | int action, const char *replace, void *cond); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 97 | /* If the function doesn't match, it returns false, else it returns true. |
| 98 | */ |
| 99 | static inline int regex_exec(const struct my_regex *preg, char *subject) { |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 100 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 101 | if (pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, 0, NULL, 0) < 0) |
| 102 | return 0; |
| 103 | return 1; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 104 | #elif defined(USE_PCRE2) |
| 105 | pcre2_match_data *pm; |
| 106 | int ret; |
| 107 | |
| 108 | pm = pcre2_match_data_create_from_pattern(preg->reg, NULL); |
| 109 | ret = pcre2_match(preg->reg, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), |
| 110 | 0, 0, pm, NULL); |
| 111 | pcre2_match_data_free(pm); |
| 112 | if (ret < 0) |
| 113 | return 0; |
| 114 | return 1; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 115 | #else |
| 116 | int match; |
| 117 | match = regexec(&preg->regex, subject, 0, NULL, 0); |
| 118 | if (match == REG_NOMATCH) |
| 119 | return 0; |
| 120 | return 1; |
| 121 | #endif |
| 122 | } |
| 123 | |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 124 | /* Note that <subject> MUST be at least <length+1> characters long and must |
| 125 | * be writable because the function will temporarily force a zero past the |
| 126 | * last character. |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 127 | * |
| 128 | * If the function doesn't match, it returns false, else it returns true. |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 129 | */ |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 130 | static inline int regex_exec2(const struct my_regex *preg, char *subject, int length) { |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 131 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 132 | if (pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0) < 0) |
| 133 | return 0; |
| 134 | return 1; |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 135 | #elif defined(USE_PCRE2) |
| 136 | pcre2_match_data *pm; |
| 137 | int ret; |
| 138 | |
| 139 | pm = pcre2_match_data_create_from_pattern(preg->reg, NULL); |
| 140 | ret = pcre2_match(preg->reg, (PCRE2_SPTR)subject, (PCRE2_SIZE)length, |
| 141 | 0, 0, pm, NULL); |
| 142 | pcre2_match_data_free(pm); |
| 143 | if (ret < 0) |
| 144 | return 0; |
| 145 | return 1; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 146 | #else |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 147 | int match; |
| 148 | char old_char = subject[length]; |
| 149 | subject[length] = 0; |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 150 | match = regexec(&preg->regex, subject, 0, NULL, 0); |
Thierry FOURNIER | ef37a66 | 2013-10-15 13:41:44 +0200 | [diff] [blame] | 151 | subject[length] = old_char; |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 152 | if (match == REG_NOMATCH) |
| 153 | return 0; |
| 154 | return 1; |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 155 | #endif |
| 156 | } |
| 157 | |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 158 | int regex_exec_match(const struct my_regex *preg, const char *subject, |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 159 | size_t nmatch, regmatch_t pmatch[], int flags); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 160 | 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] | 161 | size_t nmatch, regmatch_t pmatch[], int flags); |
Thierry FOURNIER | b8f980c | 2014-06-11 13:59:05 +0200 | [diff] [blame] | 162 | |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 163 | static inline void regex_free(struct my_regex *preg) { |
Thierry FOURNIER | 2620276 | 2014-06-18 11:50:51 +0200 | [diff] [blame] | 164 | #if defined(USE_PCRE) || defined(USE_PCRE_JIT) |
| 165 | pcre_free(preg->reg); |
Christian Ruppert | de89871 | 2014-11-18 13:03:58 +0100 | [diff] [blame] | 166 | /* PCRE < 8.20 requires pcre_free() while >= 8.20 requires pcre_study_free(), |
| 167 | * which is easily detected using PCRE_CONFIG_JIT. |
| 168 | */ |
| 169 | #ifdef PCRE_CONFIG_JIT |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 170 | pcre_free_study(preg->extra); |
Christian Ruppert | de89871 | 2014-11-18 13:03:58 +0100 | [diff] [blame] | 171 | #else /* PCRE_CONFIG_JIT */ |
| 172 | pcre_free(preg->extra); |
| 173 | #endif /* PCRE_CONFIG_JIT */ |
David Carlier | f2592b2 | 2016-11-21 21:25:58 +0000 | [diff] [blame] | 174 | #elif defined(USE_PCRE2) || defined(USE_PCRE2_JIT) |
| 175 | pcre2_code_free(preg->reg); |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 176 | #else |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 177 | regfree(&preg->regex); |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 178 | #endif |
| 179 | } |
| 180 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 181 | #endif /* _COMMON_REGEX_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * Local variables: |
| 185 | * c-indent-level: 8 |
| 186 | * c-basic-offset: 8 |
| 187 | * End: |
| 188 | */ |