blob: 066f07edda8f82675efd921ef2aae6887fcdd630 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauf4f04122010-01-28 18:10:50 +01002 * 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 Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_REGEX_H
23#define _COMMON_REGEX_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Thierry FOURNIERe28f1ec2013-10-09 15:23:01 +020025#include <stdlib.h>
Thierry FOURNIERec9a58c2015-11-26 19:33:54 +010026#include <string.h>
Thierry FOURNIERe28f1ec2013-10-09 15:23:01 +020027
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020028#include <common/config.h>
Willy Tarreau5778fea2020-05-09 09:08:09 +020029#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
31#ifdef USE_PCRE
32#include <pcre.h>
33#include <pcreposix.h>
Christian Ruppertde898712014-11-18 13:03:58 +010034
35/* For pre-8.20 PCRE compatibility */
36#ifndef PCRE_STUDY_JIT_COMPILE
37#define PCRE_STUDY_JIT_COMPILE 0
38#endif
39
David Carlierf2592b22016-11-21 21:25:58 +000040#elif USE_PCRE2
41#include <pcre2.h>
42#include <pcre2posix.h>
43
44#else /* no PCRE, nor PCRE2 */
Thierry FOURNIER799c0422013-12-06 20:36:20 +010045#include <regex.h>
46#endif
Hiroaki Nakamura70351322013-01-13 15:00:42 +090047
Thierry FOURNIER799c0422013-12-06 20:36:20 +010048struct my_regex {
49#ifdef USE_PCRE
Thierry FOURNIER26202762014-06-18 11:50:51 +020050 pcre *reg;
51 pcre_extra *extra;
Hiroaki Nakamura70351322013-01-13 15:00:42 +090052#ifdef USE_PCRE_JIT
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020053#ifndef PCRE_CONFIG_JIT
54#error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT."
55#endif
Hiroaki Nakamura70351322013-01-13 15:00:42 +090056#endif
David Carlierf2592b22016-11-21 21:25:58 +000057#elif USE_PCRE2
58 pcre2_code *reg;
Hiroaki Nakamura70351322013-01-13 15:00:42 +090059#else /* no PCRE */
Thierry FOURNIER799c0422013-12-06 20:36:20 +010060 regex_t regex;
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#endif
Thierry FOURNIER799c0422013-12-06 20:36:20 +010062};
Willy Tarreaubaaee002006-06-26 02:48:02 +020063
64/* what to do when a header matches a regex */
65#define ACT_ALLOW 0 /* allow the request */
66#define ACT_REPLACE 1 /* replace the matching header */
67#define ACT_REMOVE 2 /* remove the matching header */
68#define ACT_DENY 3 /* deny the request */
69#define ACT_PASS 4 /* pass this header without allowing or denying the request */
Willy Tarreaub8750a82006-09-03 09:56:00 +020070#define ACT_TARPIT 5 /* tarpit the connection matching this request */
Willy Tarreaubaaee002006-06-26 02:48:02 +020071
72struct hdr_exp {
73 struct hdr_exp *next;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +020074 struct my_regex *preg; /* expression to look for */
Willy Tarreaubaaee002006-06-26 02:48:02 +020075 int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
Willy Tarreaub17916e2006-10-15 15:17:57 +020076 const char *replace; /* expression to set instead */
Willy Tarreauf4f04122010-01-28 18:10:50 +010077 void *cond; /* a possible condition or NULL */
Willy Tarreaubaaee002006-06-26 02:48:02 +020078};
79
Emeric Brun272e2522017-06-15 11:53:49 +020080extern THREAD_LOCAL regmatch_t pmatch[MAX_MATCH];
Willy Tarreaubaaee002006-06-26 02:48:02 +020081
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020082/* "str" is the string that contain the regex to compile.
83 * "regex" is preallocated memory. After the execution of this function, this
84 * struct contain the compiled regex.
85 * "cs" is the case sensitive flag. If cs is true, case sensitive is enabled.
86 * "cap" is capture flag. If cap if true the regex can capture into
87 * parenthesis strings.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050088 * "err" is the standard error message pointer.
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020089 *
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050090 * The function return 1 is success case, else return 0 and err is filled.
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020091 */
Dragan Dosen26743032019-04-30 15:54:36 +020092struct my_regex *regex_comp(const char *str, int cs, int cap, char **err);
Willy Tarreauc8746532014-05-28 23:05:07 +020093int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches);
Willy Tarreaub17916e2006-10-15 15:17:57 +020094const char *check_replace_string(const char *str);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +020095const char *chain_regex(struct hdr_exp **head, struct my_regex *preg,
Willy Tarreauf4f04122010-01-28 18:10:50 +010096 int action, const char *replace, void *cond);
Willy Tarreaubaaee002006-06-26 02:48:02 +020097
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +020098/* If the function doesn't match, it returns false, else it returns true.
99 */
100static inline int regex_exec(const struct my_regex *preg, char *subject) {
Thierry FOURNIER26202762014-06-18 11:50:51 +0200101#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200102 if (pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, 0, NULL, 0) < 0)
103 return 0;
104 return 1;
David Carlierf2592b22016-11-21 21:25:58 +0000105#elif defined(USE_PCRE2)
106 pcre2_match_data *pm;
107 int ret;
108
109 pm = pcre2_match_data_create_from_pattern(preg->reg, NULL);
110 ret = pcre2_match(preg->reg, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject),
111 0, 0, pm, NULL);
112 pcre2_match_data_free(pm);
113 if (ret < 0)
114 return 0;
115 return 1;
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200116#else
117 int match;
118 match = regexec(&preg->regex, subject, 0, NULL, 0);
119 if (match == REG_NOMATCH)
120 return 0;
121 return 1;
122#endif
123}
124
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200125/* Note that <subject> MUST be at least <length+1> characters long and must
126 * be writable because the function will temporarily force a zero past the
127 * last character.
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200128 *
129 * If the function doesn't match, it returns false, else it returns true.
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200130 */
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200131static inline int regex_exec2(const struct my_regex *preg, char *subject, int length) {
Thierry FOURNIER26202762014-06-18 11:50:51 +0200132#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200133 if (pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0) < 0)
134 return 0;
135 return 1;
David Carlierf2592b22016-11-21 21:25:58 +0000136#elif defined(USE_PCRE2)
137 pcre2_match_data *pm;
138 int ret;
139
140 pm = pcre2_match_data_create_from_pattern(preg->reg, NULL);
141 ret = pcre2_match(preg->reg, (PCRE2_SPTR)subject, (PCRE2_SIZE)length,
142 0, 0, pm, NULL);
143 pcre2_match_data_free(pm);
144 if (ret < 0)
145 return 0;
146 return 1;
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900147#else
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200148 int match;
149 char old_char = subject[length];
150 subject[length] = 0;
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100151 match = regexec(&preg->regex, subject, 0, NULL, 0);
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200152 subject[length] = old_char;
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200153 if (match == REG_NOMATCH)
154 return 0;
155 return 1;
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900156#endif
157}
158
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200159int regex_exec_match(const struct my_regex *preg, const char *subject,
Willy Tarreau15a53a42015-01-21 13:39:42 +0100160 size_t nmatch, regmatch_t pmatch[], int flags);
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200161int regex_exec_match2(const struct my_regex *preg, char *subject, int length,
Willy Tarreau15a53a42015-01-21 13:39:42 +0100162 size_t nmatch, regmatch_t pmatch[], int flags);
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200163
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100164static inline void regex_free(struct my_regex *preg) {
Dragan Dosen26743032019-04-30 15:54:36 +0200165 if (!preg)
166 return;
Thierry FOURNIER26202762014-06-18 11:50:51 +0200167#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
168 pcre_free(preg->reg);
Christian Ruppertde898712014-11-18 13:03:58 +0100169/* PCRE < 8.20 requires pcre_free() while >= 8.20 requires pcre_study_free(),
170 * which is easily detected using PCRE_CONFIG_JIT.
171 */
172#ifdef PCRE_CONFIG_JIT
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900173 pcre_free_study(preg->extra);
Christian Ruppertde898712014-11-18 13:03:58 +0100174#else /* PCRE_CONFIG_JIT */
175 pcre_free(preg->extra);
176#endif /* PCRE_CONFIG_JIT */
David Carlierf2592b22016-11-21 21:25:58 +0000177#elif defined(USE_PCRE2) || defined(USE_PCRE2_JIT)
178 pcre2_code_free(preg->reg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900179#else
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100180 regfree(&preg->regex);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900181#endif
Dragan Dosen26743032019-04-30 15:54:36 +0200182 free(preg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900183}
184
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200185#endif /* _COMMON_REGEX_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186
187/*
188 * Local variables:
189 * c-indent-level: 8
190 * c-basic-offset: 8
191 * End:
192 */