blob: 30a5e3fc7f771e0e6675046dbdefe2023595aec5 [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>
26
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
29#ifdef USE_PCRE
30#include <pcre.h>
31#include <pcreposix.h>
Christian Ruppertde898712014-11-18 13:03:58 +010032
33/* For pre-8.20 PCRE compatibility */
34#ifndef PCRE_STUDY_JIT_COMPILE
35#define PCRE_STUDY_JIT_COMPILE 0
36#endif
37
Thierry FOURNIER799c0422013-12-06 20:36:20 +010038#else /* no PCRE */
39#include <regex.h>
40#endif
Hiroaki Nakamura70351322013-01-13 15:00:42 +090041
Thierry FOURNIER799c0422013-12-06 20:36:20 +010042struct my_regex {
43#ifdef USE_PCRE
Thierry FOURNIER26202762014-06-18 11:50:51 +020044 pcre *reg;
45 pcre_extra *extra;
Hiroaki Nakamura70351322013-01-13 15:00:42 +090046#ifdef USE_PCRE_JIT
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020047#ifndef PCRE_CONFIG_JIT
48#error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT."
49#endif
Hiroaki Nakamura70351322013-01-13 15:00:42 +090050#endif
Hiroaki Nakamura70351322013-01-13 15:00:42 +090051#else /* no PCRE */
Thierry FOURNIER799c0422013-12-06 20:36:20 +010052 regex_t regex;
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#endif
Thierry FOURNIER799c0422013-12-06 20:36:20 +010054};
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
56/* what to do when a header matches a regex */
57#define ACT_ALLOW 0 /* allow the request */
58#define ACT_REPLACE 1 /* replace the matching header */
59#define ACT_REMOVE 2 /* remove the matching header */
60#define ACT_DENY 3 /* deny the request */
61#define ACT_PASS 4 /* pass this header without allowing or denying the request */
Willy Tarreaub8750a82006-09-03 09:56:00 +020062#define ACT_TARPIT 5 /* tarpit the connection matching this request */
Willy Tarreaubaaee002006-06-26 02:48:02 +020063
64struct hdr_exp {
65 struct hdr_exp *next;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +020066 struct my_regex *preg; /* expression to look for */
Willy Tarreaubaaee002006-06-26 02:48:02 +020067 int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
Willy Tarreaub17916e2006-10-15 15:17:57 +020068 const char *replace; /* expression to set instead */
Willy Tarreauf4f04122010-01-28 18:10:50 +010069 void *cond; /* a possible condition or NULL */
Willy Tarreaubaaee002006-06-26 02:48:02 +020070};
71
72extern regmatch_t pmatch[MAX_MATCH];
73
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +020074/* "str" is the string that contain the regex to compile.
75 * "regex" is preallocated memory. After the execution of this function, this
76 * struct contain the compiled regex.
77 * "cs" is the case sensitive flag. If cs is true, case sensitive is enabled.
78 * "cap" is capture flag. If cap if true the regex can capture into
79 * parenthesis strings.
80 * "err" is the standar error message pointer.
81 *
82 * The function return 1 is succes case, else return 0 and err is filled.
83 */
Thierry FOURNIER799c0422013-12-06 20:36:20 +010084int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err);
Willy Tarreauc8746532014-05-28 23:05:07 +020085int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches);
Willy Tarreaub17916e2006-10-15 15:17:57 +020086const char *check_replace_string(const char *str);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +020087const char *chain_regex(struct hdr_exp **head, struct my_regex *preg,
Willy Tarreauf4f04122010-01-28 18:10:50 +010088 int action, const char *replace, void *cond);
Willy Tarreaubaaee002006-06-26 02:48:02 +020089
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +020090/* If the function doesn't match, it returns false, else it returns true.
91 */
92static inline int regex_exec(const struct my_regex *preg, char *subject) {
Thierry FOURNIER26202762014-06-18 11:50:51 +020093#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +020094 if (pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, 0, NULL, 0) < 0)
95 return 0;
96 return 1;
97#else
98 int match;
99 match = regexec(&preg->regex, subject, 0, NULL, 0);
100 if (match == REG_NOMATCH)
101 return 0;
102 return 1;
103#endif
104}
105
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200106/* Note that <subject> MUST be at least <length+1> characters long and must
107 * be writable because the function will temporarily force a zero past the
108 * last character.
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200109 *
110 * If the function doesn't match, it returns false, else it returns true.
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200111 */
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200112static inline int regex_exec2(const struct my_regex *preg, char *subject, int length) {
Thierry FOURNIER26202762014-06-18 11:50:51 +0200113#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200114 if (pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0) < 0)
115 return 0;
116 return 1;
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900117#else
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200118 int match;
119 char old_char = subject[length];
120 subject[length] = 0;
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100121 match = regexec(&preg->regex, subject, 0, NULL, 0);
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200122 subject[length] = old_char;
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200123 if (match == REG_NOMATCH)
124 return 0;
125 return 1;
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900126#endif
127}
128
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200129int regex_exec_match(const struct my_regex *preg, const char *subject,
Willy Tarreau15a53a42015-01-21 13:39:42 +0100130 size_t nmatch, regmatch_t pmatch[], int flags);
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200131int regex_exec_match2(const struct my_regex *preg, char *subject, int length,
Willy Tarreau15a53a42015-01-21 13:39:42 +0100132 size_t nmatch, regmatch_t pmatch[], int flags);
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200133
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100134static inline void regex_free(struct my_regex *preg) {
Thierry FOURNIER26202762014-06-18 11:50:51 +0200135#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
136 pcre_free(preg->reg);
Christian Ruppertde898712014-11-18 13:03:58 +0100137/* PCRE < 8.20 requires pcre_free() while >= 8.20 requires pcre_study_free(),
138 * which is easily detected using PCRE_CONFIG_JIT.
139 */
140#ifdef PCRE_CONFIG_JIT
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900141 pcre_free_study(preg->extra);
Christian Ruppertde898712014-11-18 13:03:58 +0100142#else /* PCRE_CONFIG_JIT */
143 pcre_free(preg->extra);
144#endif /* PCRE_CONFIG_JIT */
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900145#else
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100146 regfree(&preg->regex);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900147#endif
148}
149
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200150#endif /* _COMMON_REGEX_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151
152/*
153 * Local variables:
154 * c-indent-level: 8
155 * c-basic-offset: 8
156 * End:
157 */