2013-11-28 10:05:19 +00:00
|
|
|
/*
|
|
|
|
* include/proto/pattern.h
|
|
|
|
* This file provides structures and types for pattern matching.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2013 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PROTO_PATTERN_H
|
|
|
|
#define _PROTO_PATTERN_H
|
|
|
|
|
2013-11-28 21:24:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-11-28 21:21:02 +00:00
|
|
|
#include <common/config.h>
|
|
|
|
#include <common/standard.h>
|
|
|
|
#include <types/pattern.h>
|
|
|
|
|
2014-04-02 18:55:23 +00:00
|
|
|
/* pattern management function arrays */
|
|
|
|
extern char *pat_match_names[PAT_MATCH_NUM];
|
2014-04-28 09:18:57 +00:00
|
|
|
extern int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **);
|
2014-04-02 18:55:23 +00:00
|
|
|
extern int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **);
|
|
|
|
extern void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *);
|
|
|
|
extern void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *);
|
|
|
|
extern struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int);
|
|
|
|
extern int pat_match_types[PAT_MATCH_NUM];
|
|
|
|
|
2020-02-27 15:45:50 +00:00
|
|
|
int pattern_finalize_config(void);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
2013-11-28 21:24:25 +00:00
|
|
|
/* return the PAT_MATCH_* index for match name "name", or < 0 if not found */
|
|
|
|
static inline int pat_find_match_name(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PAT_MATCH_NUM; i++)
|
|
|
|
if (strcmp(name, pat_match_names[i]) == 0)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-28 10:05:19 +00:00
|
|
|
/* This function executes a pattern match on a sample. It applies pattern <expr>
|
2020-03-10 07:06:11 +00:00
|
|
|
* to sample <smp>. The function returns NULL if the sample don't match. It returns
|
2014-01-17 14:25:13 +00:00
|
|
|
* non-null if the sample match. If <fill> is true and the sample match, the
|
|
|
|
* function returns the matched pattern. In many cases, this pattern can be a
|
|
|
|
* static buffer.
|
2013-11-28 10:05:19 +00:00
|
|
|
*/
|
2014-02-11 10:31:40 +00:00
|
|
|
struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
2013-12-13 14:12:32 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* The following function gets "pattern", duplicate it and index it in "expr"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err);
|
2016-02-10 21:55:20 +00:00
|
|
|
int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err);
|
2013-12-13 14:12:32 +00:00
|
|
|
int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err);
|
2014-05-10 06:53:48 +00:00
|
|
|
int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err);
|
2013-12-13 14:12:32 +00:00
|
|
|
|
2014-01-15 10:38:49 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* The following functions search pattern <pattern> into the pattern
|
|
|
|
* expression <expr>. If the pattern is found, delete it. This function
|
|
|
|
* never fails.
|
|
|
|
*
|
|
|
|
*/
|
2014-01-28 15:43:36 +00:00
|
|
|
void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref);
|
|
|
|
void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref);
|
|
|
|
void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref);
|
|
|
|
void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref);
|
|
|
|
void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref);
|
2014-01-15 10:38:49 +00:00
|
|
|
|
2014-01-14 15:24:51 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* The following functions clean all entries of a pattern expression and
|
|
|
|
* reset the tree and list root.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void pat_prune_val(struct pattern_expr *expr);
|
|
|
|
void pat_prune_ptr(struct pattern_expr *expr);
|
|
|
|
void pat_prune_reg(struct pattern_expr *expr);
|
|
|
|
|
2013-11-28 10:05:19 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* The following functions are general purpose pattern matching functions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* ignore the current line */
|
2014-04-28 09:18:57 +00:00
|
|
|
int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse an integer. It is put both in min and max. */
|
2014-04-28 09:18:57 +00:00
|
|
|
int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse an version. It is put both in min and max. */
|
2014-04-28 09:18:57 +00:00
|
|
|
int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse a range of integers delimited by either ':' or '-'. If only one
|
|
|
|
* integer is read, it is set as both min and max.
|
|
|
|
*/
|
2014-04-28 09:18:57 +00:00
|
|
|
int pat_parse_range(const char *text, struct pattern *pattern, int mflags, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse a string. It is allocated and duplicated. */
|
2014-04-28 09:18:57 +00:00
|
|
|
int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse a hexa binary definition. It is allocated and duplicated. */
|
2014-04-28 09:18:57 +00:00
|
|
|
int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse a regex. It is allocated. */
|
2014-04-28 09:18:57 +00:00
|
|
|
int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse an IP address and an optional mask in the form addr[/mask].
|
|
|
|
* The addr may either be an IPv4 address or a hostname. The mask
|
|
|
|
* may either be a dotted mask or a number of bits. Returns 1 if OK,
|
|
|
|
* otherwise 0.
|
|
|
|
*/
|
2014-04-28 09:18:57 +00:00
|
|
|
int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
2014-01-21 10:25:41 +00:00
|
|
|
/* NB: For two strings to be identical, it is required that their lengths match */
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill);
|
2014-01-21 10:25:41 +00:00
|
|
|
|
|
|
|
/* NB: For two binary buffers to be identical, it is required that their lengths match */
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill);
|
2014-01-21 10:25:41 +00:00
|
|
|
|
|
|
|
/* Checks that the length of the pattern in <test> is included between min and max */
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill);
|
2014-01-21 10:25:41 +00:00
|
|
|
|
|
|
|
/* Checks that the integer in <test> is included between min and max */
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill);
|
2014-01-21 10:25:41 +00:00
|
|
|
|
2013-11-28 10:05:19 +00:00
|
|
|
/* always return false */
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern matches the end of the tested string. */
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern matches the beginning of the tested string. */
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern is included inside the tested string. */
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern is included inside the tested string, but enclosed
|
|
|
|
* between slashes or at the beginning or end of the string. Slashes at the
|
|
|
|
* beginning or end of the pattern are ignored.
|
|
|
|
*/
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern is included inside the tested string, but enclosed
|
|
|
|
* between dots or at the beginning or end of the string. Dots at the beginning
|
|
|
|
* or end of the pattern are ignored.
|
|
|
|
*/
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Check that the IPv4 address in <test> matches the IP/mask in pattern */
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Executes a regex. It temporarily changes the data to add a trailing zero,
|
|
|
|
* and restores the previous character when leaving.
|
|
|
|
*/
|
2013-12-16 13:22:13 +00:00
|
|
|
struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill);
|
2016-02-10 21:55:20 +00:00
|
|
|
struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
2014-02-11 10:31:40 +00:00
|
|
|
/*
|
|
|
|
* pattern_ref manipulation.
|
|
|
|
*/
|
|
|
|
struct pat_ref *pat_ref_lookup(const char *reference);
|
2014-03-11 13:29:22 +00:00
|
|
|
struct pat_ref *pat_ref_lookupid(int unique_id);
|
2014-02-11 02:31:34 +00:00
|
|
|
struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags);
|
|
|
|
struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags);
|
2014-04-25 14:57:03 +00:00
|
|
|
struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key);
|
2014-02-11 10:31:40 +00:00
|
|
|
int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line);
|
2014-04-28 09:18:57 +00:00
|
|
|
int pat_ref_add(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
|
2014-01-29 18:08:49 +00:00
|
|
|
int pat_ref_set(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
|
|
|
|
int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err);
|
2014-02-11 10:31:40 +00:00
|
|
|
int pat_ref_delete(struct pat_ref *ref, const char *key);
|
2014-01-28 15:43:36 +00:00
|
|
|
int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt);
|
2014-02-11 10:31:40 +00:00
|
|
|
void pat_ref_prune(struct pat_ref *ref);
|
|
|
|
int pat_ref_load(struct pat_ref *ref, struct pattern_expr *expr, int patflags, int soe, char **err);
|
2014-03-21 20:45:15 +00:00
|
|
|
void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace);
|
|
|
|
|
2014-02-11 10:31:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pattern_head manipulation.
|
|
|
|
*/
|
|
|
|
void pattern_init_head(struct pattern_head *head);
|
|
|
|
void pattern_prune(struct pattern_head *head);
|
2014-02-11 13:36:45 +00:00
|
|
|
int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, const char *filename, int patflags, int load_smp, char **err, const char *file, int line);
|
2014-02-11 10:31:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pattern_expr manipulation.
|
|
|
|
*/
|
2013-11-28 17:22:00 +00:00
|
|
|
void pattern_init_expr(struct pattern_expr *expr);
|
2014-02-11 10:31:40 +00:00
|
|
|
struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref);
|
2014-11-24 10:14:42 +00:00
|
|
|
struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
|
2017-07-03 15:54:23 +00:00
|
|
|
int patflags, char **err, int *reuse);
|
2015-08-13 22:02:11 +00:00
|
|
|
struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *elt);
|
2014-01-28 15:43:36 +00:00
|
|
|
int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref);
|
2013-12-10 14:08:01 +00:00
|
|
|
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
#endif
|