2006-06-26 00:48:02 +00:00
|
|
|
/*
|
2010-01-28 17:10:50 +00:00
|
|
|
* include/common/regex.h
|
|
|
|
* This file defines everything related to regular expressions.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
* exclusively.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#ifndef _COMMON_REGEX_H
|
|
|
|
#define _COMMON_REGEX_H
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2013-10-09 13:23:01 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2006-06-29 16:54:54 +00:00
|
|
|
#include <common/config.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
#ifdef USE_PCRE
|
|
|
|
#include <pcre.h>
|
|
|
|
#include <pcreposix.h>
|
2013-12-06 19:36:20 +00:00
|
|
|
#else /* no PCRE */
|
|
|
|
#include <regex.h>
|
|
|
|
#endif
|
2013-01-13 06:00:42 +00:00
|
|
|
|
2013-12-06 19:36:20 +00:00
|
|
|
struct my_regex {
|
|
|
|
#ifdef USE_PCRE
|
2014-06-18 09:50:51 +00:00
|
|
|
pcre *reg;
|
|
|
|
pcre_extra *extra;
|
2013-01-13 06:00:42 +00:00
|
|
|
#ifdef USE_PCRE_JIT
|
2013-10-14 12:07:36 +00:00
|
|
|
#ifndef PCRE_CONFIG_JIT
|
|
|
|
#error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT."
|
|
|
|
#endif
|
2013-01-13 06:00:42 +00:00
|
|
|
#endif
|
|
|
|
#else /* no PCRE */
|
2013-12-06 19:36:20 +00:00
|
|
|
regex_t regex;
|
2006-06-26 00:48:02 +00:00
|
|
|
#endif
|
2013-12-06 19:36:20 +00:00
|
|
|
};
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/* what to do when a header matches a regex */
|
|
|
|
#define ACT_ALLOW 0 /* allow the request */
|
|
|
|
#define ACT_REPLACE 1 /* replace the matching header */
|
|
|
|
#define ACT_REMOVE 2 /* remove the matching header */
|
|
|
|
#define ACT_DENY 3 /* deny the request */
|
|
|
|
#define ACT_PASS 4 /* pass this header without allowing or denying the request */
|
2006-09-03 07:56:00 +00:00
|
|
|
#define ACT_TARPIT 5 /* tarpit the connection matching this request */
|
2006-12-17 22:15:24 +00:00
|
|
|
#define ACT_SETBE 6 /* switch the backend */
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
struct hdr_exp {
|
|
|
|
struct hdr_exp *next;
|
2014-06-18 09:35:54 +00:00
|
|
|
struct my_regex *preg; /* expression to look for */
|
2006-06-26 00:48:02 +00:00
|
|
|
int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
|
2006-10-15 13:17:57 +00:00
|
|
|
const char *replace; /* expression to set instead */
|
2010-01-28 17:10:50 +00:00
|
|
|
void *cond; /* a possible condition or NULL */
|
2006-06-26 00:48:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern regmatch_t pmatch[MAX_MATCH];
|
|
|
|
|
2013-10-14 12:07:36 +00:00
|
|
|
/* "str" is the string that contain the regex to compile.
|
|
|
|
* "regex" is preallocated memory. After the execution of this function, this
|
|
|
|
* struct contain the compiled regex.
|
|
|
|
* "cs" is the case sensitive flag. If cs is true, case sensitive is enabled.
|
|
|
|
* "cap" is capture flag. If cap if true the regex can capture into
|
|
|
|
* parenthesis strings.
|
|
|
|
* "err" is the standar error message pointer.
|
|
|
|
*
|
|
|
|
* The function return 1 is succes case, else return 0 and err is filled.
|
|
|
|
*/
|
2013-12-06 19:36:20 +00:00
|
|
|
int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err);
|
2014-05-28 21:05:07 +00:00
|
|
|
int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches);
|
2006-10-15 13:17:57 +00:00
|
|
|
const char *check_replace_string(const char *str);
|
2014-06-18 09:35:54 +00:00
|
|
|
const char *chain_regex(struct hdr_exp **head, struct my_regex *preg,
|
2010-01-28 17:10:50 +00:00
|
|
|
int action, const char *replace, void *cond);
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2014-06-11 11:59:05 +00:00
|
|
|
/* If the function doesn't match, it returns false, else it returns true.
|
|
|
|
*/
|
|
|
|
static inline int regex_exec(const struct my_regex *preg, char *subject) {
|
2014-06-18 09:50:51 +00:00
|
|
|
#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
|
2014-06-11 11:59:05 +00:00
|
|
|
if (pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, 0, NULL, 0) < 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
int match;
|
|
|
|
match = regexec(&preg->regex, subject, 0, NULL, 0);
|
|
|
|
if (match == REG_NOMATCH)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-10-15 11:41:44 +00:00
|
|
|
/* Note that <subject> MUST be at least <length+1> characters long and must
|
|
|
|
* be writable because the function will temporarily force a zero past the
|
|
|
|
* last character.
|
2014-06-11 11:59:05 +00:00
|
|
|
*
|
|
|
|
* If the function doesn't match, it returns false, else it returns true.
|
2013-10-15 11:41:44 +00:00
|
|
|
*/
|
2014-06-11 11:59:05 +00:00
|
|
|
static inline int regex_exec2(const struct my_regex *preg, char *subject, int length) {
|
2014-06-18 09:50:51 +00:00
|
|
|
#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
|
2014-06-11 11:59:05 +00:00
|
|
|
if (pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0) < 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2013-01-13 06:00:42 +00:00
|
|
|
#else
|
2013-10-15 11:41:44 +00:00
|
|
|
int match;
|
|
|
|
char old_char = subject[length];
|
|
|
|
subject[length] = 0;
|
2013-12-06 19:36:20 +00:00
|
|
|
match = regexec(&preg->regex, subject, 0, NULL, 0);
|
2013-10-15 11:41:44 +00:00
|
|
|
subject[length] = old_char;
|
2014-06-11 11:59:05 +00:00
|
|
|
if (match == REG_NOMATCH)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2013-01-13 06:00:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-11 11:59:05 +00:00
|
|
|
int regex_exec_match(const struct my_regex *preg, const char *subject,
|
|
|
|
size_t nmatch, regmatch_t pmatch[]);
|
|
|
|
int regex_exec_match2(const struct my_regex *preg, char *subject, int length,
|
|
|
|
size_t nmatch, regmatch_t pmatch[]);
|
|
|
|
|
2013-12-06 19:36:20 +00:00
|
|
|
static inline void regex_free(struct my_regex *preg) {
|
2014-06-18 09:50:51 +00:00
|
|
|
#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
|
|
|
|
pcre_free(preg->reg);
|
2013-01-13 06:00:42 +00:00
|
|
|
#ifdef USE_PCRE_JIT
|
|
|
|
pcre_free_study(preg->extra);
|
2014-06-18 09:50:51 +00:00
|
|
|
#endif
|
2013-01-13 06:00:42 +00:00
|
|
|
#else
|
2013-12-06 19:36:20 +00:00
|
|
|
regfree(&preg->regex);
|
2013-01-13 06:00:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#endif /* _COMMON_REGEX_H */
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|