blob: 0069b2d1e198a52c5d30b967bb7f6a029a172d7d [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 Tarreaubaaee002006-06-26 02:48:02 +020029
30#ifdef USE_PCRE
31#include <pcre.h>
32#include <pcreposix.h>
Christian Ruppertde898712014-11-18 13:03:58 +010033
34/* For pre-8.20 PCRE compatibility */
35#ifndef PCRE_STUDY_JIT_COMPILE
36#define PCRE_STUDY_JIT_COMPILE 0
37#endif
38
David Carlierf2592b22016-11-21 21:25:58 +000039#elif USE_PCRE2
40#include <pcre2.h>
41#include <pcre2posix.h>
42
43#else /* no PCRE, nor PCRE2 */
Thierry FOURNIER799c0422013-12-06 20:36:20 +010044#include <regex.h>
45#endif
Hiroaki Nakamura70351322013-01-13 15:00:42 +090046
Thierry FOURNIER799c0422013-12-06 20:36:20 +010047struct my_regex {
48#ifdef USE_PCRE
Thierry FOURNIER26202762014-06-18 11:50:51 +020049 pcre *reg;
50 pcre_extra *extra;
Hiroaki Nakamura70351322013-01-13 15:00:42 +090051#ifdef USE_PCRE_JIT
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020052#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 Nakamura70351322013-01-13 15:00:42 +090055#endif
David Carlierf2592b22016-11-21 21:25:58 +000056#elif USE_PCRE2
57 pcre2_code *reg;
Hiroaki Nakamura70351322013-01-13 15:00:42 +090058#else /* no PCRE */
Thierry FOURNIER799c0422013-12-06 20:36:20 +010059 regex_t regex;
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#endif
Thierry FOURNIER799c0422013-12-06 20:36:20 +010061};
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
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 Tarreaub8750a82006-09-03 09:56:00 +020069#define ACT_TARPIT 5 /* tarpit the connection matching this request */
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
71struct hdr_exp {
72 struct hdr_exp *next;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +020073 struct my_regex *preg; /* expression to look for */
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
Willy Tarreaub17916e2006-10-15 15:17:57 +020075 const char *replace; /* expression to set instead */
Willy Tarreauf4f04122010-01-28 18:10:50 +010076 void *cond; /* a possible condition or NULL */
Willy Tarreaubaaee002006-06-26 02:48:02 +020077};
78
Emeric Brun272e2522017-06-15 11:53:49 +020079extern THREAD_LOCAL regmatch_t pmatch[MAX_MATCH];
Willy Tarreaubaaee002006-06-26 02:48:02 +020080
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020081/* "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.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050087 * "err" is the standard error message pointer.
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020088 *
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050089 * The function return 1 is success case, else return 0 and err is filled.
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020090 */
Dragan Dosen26743032019-04-30 15:54:36 +020091struct my_regex *regex_comp(const char *str, int cs, int cap, char **err);
Willy Tarreauc8746532014-05-28 23:05:07 +020092int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches);
Willy Tarreaub17916e2006-10-15 15:17:57 +020093const char *check_replace_string(const char *str);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +020094const char *chain_regex(struct hdr_exp **head, struct my_regex *preg,
Willy Tarreauf4f04122010-01-28 18:10:50 +010095 int action, const char *replace, void *cond);
Willy Tarreaubaaee002006-06-26 02:48:02 +020096
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +020097/* If the function doesn't match, it returns false, else it returns true.
98 */
99static inline int regex_exec(const struct my_regex *preg, char *subject) {
Thierry FOURNIER26202762014-06-18 11:50:51 +0200100#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200101 if (pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, 0, NULL, 0) < 0)
102 return 0;
103 return 1;
David Carlierf2592b22016-11-21 21:25:58 +0000104#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 FOURNIERb8f980c2014-06-11 13:59:05 +0200115#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 FOURNIERef37a662013-10-15 13:41:44 +0200124/* 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 FOURNIERb8f980c2014-06-11 13:59:05 +0200127 *
128 * If the function doesn't match, it returns false, else it returns true.
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200129 */
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200130static inline int regex_exec2(const struct my_regex *preg, char *subject, int length) {
Thierry FOURNIER26202762014-06-18 11:50:51 +0200131#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200132 if (pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0) < 0)
133 return 0;
134 return 1;
David Carlierf2592b22016-11-21 21:25:58 +0000135#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 Nakamura70351322013-01-13 15:00:42 +0900146#else
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200147 int match;
148 char old_char = subject[length];
149 subject[length] = 0;
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100150 match = regexec(&preg->regex, subject, 0, NULL, 0);
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200151 subject[length] = old_char;
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200152 if (match == REG_NOMATCH)
153 return 0;
154 return 1;
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900155#endif
156}
157
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200158int regex_exec_match(const struct my_regex *preg, const char *subject,
Willy Tarreau15a53a42015-01-21 13:39:42 +0100159 size_t nmatch, regmatch_t pmatch[], int flags);
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200160int regex_exec_match2(const struct my_regex *preg, char *subject, int length,
Willy Tarreau15a53a42015-01-21 13:39:42 +0100161 size_t nmatch, regmatch_t pmatch[], int flags);
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200162
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100163static inline void regex_free(struct my_regex *preg) {
Dragan Dosen26743032019-04-30 15:54:36 +0200164 if (!preg)
165 return;
Thierry FOURNIER26202762014-06-18 11:50:51 +0200166#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
167 pcre_free(preg->reg);
Christian Ruppertde898712014-11-18 13:03:58 +0100168/* PCRE < 8.20 requires pcre_free() while >= 8.20 requires pcre_study_free(),
169 * which is easily detected using PCRE_CONFIG_JIT.
170 */
171#ifdef PCRE_CONFIG_JIT
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900172 pcre_free_study(preg->extra);
Christian Ruppertde898712014-11-18 13:03:58 +0100173#else /* PCRE_CONFIG_JIT */
174 pcre_free(preg->extra);
175#endif /* PCRE_CONFIG_JIT */
David Carlierf2592b22016-11-21 21:25:58 +0000176#elif defined(USE_PCRE2) || defined(USE_PCRE2_JIT)
177 pcre2_code_free(preg->reg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900178#else
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100179 regfree(&preg->regex);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900180#endif
Dragan Dosen26743032019-04-30 15:54:36 +0200181 free(preg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900182}
183
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200184#endif /* _COMMON_REGEX_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185
186/*
187 * Local variables:
188 * c-indent-level: 8
189 * c-basic-offset: 8
190 * End:
191 */