blob: 830630b24993bdbce25192a17619c4ba9aefc4fb [file] [log] [blame]
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001/*
2 * include/proto/pattern.h
3 * This file provides structures and types for pattern matching.
4 *
5 * Copyright (C) 2000-2013 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 */
21
22#ifndef _PROTO_PATTERN_H
23#define _PROTO_PATTERN_H
24
Willy Tarreau6f8fe312013-11-28 22:24:25 +010025#include <string.h>
26
Willy Tarreau0cba6072013-11-28 22:21:02 +010027#include <common/config.h>
28#include <common/standard.h>
29#include <types/pattern.h>
30
Thierry FOURNIER7148ce62013-12-06 19:06:43 +010031/* parse the <args> with <expr> compliant parser. <pattern> is a context for
Thierry FOURNIERed66c292013-11-28 11:05:19 +010032 * the current parsed acl. It must initialized at NULL:
33 *
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010034 * struct pattern *pattern = NULL
35 * pattern_register(..., &pattern, ...);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010036 *
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010037 * patflag are a lot of 'PAT_F_*' flags pattern compatible. see
Thierry FOURNIERed66c292013-11-28 11:05:19 +010038 * <types/acl.h>.
39 *
Thierry FOURNIER7148ce62013-12-06 19:06:43 +010040 * The function returns 1 if the processing is ok, return 0
41 * if the parser fails, with <err> message filled.
Thierry FOURNIERed66c292013-11-28 11:05:19 +010042 */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +010043int pattern_register(struct pattern_head *head, int unique_id, int refflags, const char *arg, struct sample_storage *smp, int patflags, char **err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010044void pattern_finalize_config(void);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010045
Willy Tarreau6f8fe312013-11-28 22:24:25 +010046/* return the PAT_MATCH_* index for match name "name", or < 0 if not found */
47static inline int pat_find_match_name(const char *name)
48{
49 int i;
50
51 for (i = 0; i < PAT_MATCH_NUM; i++)
52 if (strcmp(name, pat_match_names[i]) == 0)
53 return i;
54 return -1;
55}
56
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057/* This function executes a pattern match on a sample. It applies pattern <expr>
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +010058 * to sample <smp>. The function returns NULL if the sample dont match. It returns
59 * non-null if the sample match. If <fill> is true and the sample match, the
60 * function returns the matched pattern. In many cases, this pattern can be a
61 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010063struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010064
65/*
66 *
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010067 * The following function gets "pattern", duplicate it and index it in "expr"
68 *
69 */
70int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err);
71int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err);
72int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err);
73int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err);
74int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err);
75int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err);
76
77/*
78 *
Thierry FOURNIERb1136502014-01-15 11:38:49 +010079 * The following functions search pattern <pattern> into the pattern
80 * expression <expr>. If the pattern is found, delete it. This function
81 * never fails.
82 *
83 */
84void pat_del_list_val(struct pattern_expr *expr, struct pattern *pat);
85void pat_del_tree_ip(struct pattern_expr *expr, struct pattern *pat);
86void pat_del_list_ptr(struct pattern_expr *expr, struct pattern *pat);
87void pat_del_tree_str(struct pattern_expr *expr, struct pattern *pat);
88void pat_del_list_str(struct pattern_expr *expr, struct pattern *pat);
89void pat_del_list_reg(struct pattern_expr *expr, struct pattern *pat);
90
91/*
92 *
Thierry FOURNIER55d0b102014-01-15 11:25:26 +010093 * The following function lookup the pattern and return a pointer on the
94 * pointer containing the sample expression. This is useful to replace
95 * the sample.
96 *
97 */
98struct sample_storage **pat_find_smp_list_val(struct pattern_expr *expr, struct pattern *pattern);
99struct sample_storage **pat_find_smp_tree_ip(struct pattern_expr *expr, struct pattern *pattern);
100struct sample_storage **pat_find_smp_list_ptr(struct pattern_expr *expr, struct pattern *pattern);
101struct sample_storage **pat_find_smp_tree_str(struct pattern_expr *expr, struct pattern *pattern);
102struct sample_storage **pat_find_smp_list_str(struct pattern_expr *expr, struct pattern *pattern);
103struct sample_storage **pat_find_smp_list_reg(struct pattern_expr *expr, struct pattern *pattern);
104
105/*
106 *
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100107 * The following functions clean all entries of a pattern expression and
108 * reset the tree and list root.
109 *
110 */
111void pat_prune_val(struct pattern_expr *expr);
112void pat_prune_ptr(struct pattern_expr *expr);
113void pat_prune_reg(struct pattern_expr *expr);
114
115/*
116 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100117 * The following functions are general purpose pattern matching functions.
118 *
119 */
120
121
122/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100123int pat_parse_nothing(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100124
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100125/* Parse an integer. It is put both in min and max. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100126int pat_parse_int(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100127
128/* Parse an version. It is put both in min and max. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100129int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100130
131/* Parse a range of integers delimited by either ':' or '-'. If only one
132 * integer is read, it is set as both min and max.
133 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100134int pat_parse_range(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100135
136/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100137int pat_parse_str(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100138
139/* Parse a hexa binary definition. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100140int pat_parse_bin(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100141
142/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100143int pat_parse_reg(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100144
145/* Parse an IP address and an optional mask in the form addr[/mask].
146 * The addr may either be an IPv4 address or a hostname. The mask
147 * may either be a dotted mask or a number of bits. Returns 1 if OK,
148 * otherwise 0.
149 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100150int pat_parse_ip(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100151
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100152/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100153struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100154
155/* NB: For two binary buffers to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100156struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100157
158/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100159struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100160
161/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100162struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100163
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100164/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100165struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166
167/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100168struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100169
170/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100171struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100172
173/* Checks that the pattern is included inside the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100174struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100175
176/* Checks that the pattern is included inside the tested string, but enclosed
177 * between slashes or at the beginning or end of the string. Slashes at the
178 * beginning or end of the pattern are ignored.
179 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100180struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100181
182/* Checks that the pattern is included inside the tested string, but enclosed
183 * between dots or at the beginning or end of the string. Dots at the beginning
184 * or end of the pattern are ignored.
185 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100186struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100187
188/* Check that the IPv4 address in <test> matches the IP/mask in pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100189struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100190
191/* Executes a regex. It temporarily changes the data to add a trailing zero,
192 * and restores the previous character when leaving.
193 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100194struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100195
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100196/*
197 * pattern_ref manipulation.
198 */
199struct pat_ref *pat_ref_lookup(const char *reference);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100200struct pat_ref *pat_ref_lookupid(int unique_id);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100201struct pat_ref *pat_ref_new(const char *reference, unsigned int flags);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100202struct pat_ref *pat_ref_newid(int unique_id, unsigned int flags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100203int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line);
204int pat_ref_add(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
205int pat_ref_set(struct pat_ref *ref, const char *pattern, const char *sample);
206int pat_ref_delete(struct pat_ref *ref, const char *key);
207void pat_ref_prune(struct pat_ref *ref);
208int pat_ref_load(struct pat_ref *ref, struct pattern_expr *expr, int patflags, int soe, char **err);
209
210/*
211 * pattern_head manipulation.
212 */
213void pattern_init_head(struct pattern_head *head);
214void pattern_prune(struct pattern_head *head);
215int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, const char *filename, int patflags, char **err);
216
217/*
218 * pattern_expr manipulation.
219 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100220void pattern_init_expr(struct pattern_expr *expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100221struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref);
222struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err);
Thierry FOURNIER55d0b102014-01-15 11:25:26 +0100223struct sample_storage **pattern_find_smp(const char *key, struct pattern_expr *expr, char **err);
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100224int pattern_delete(const char *key, struct pattern_expr *expr, char **err);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +0100225
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100226
227#endif