1 /*
2 * include/haproxy/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 _HAPROXY_PATTERN_H
23 #define _HAPROXY_PATTERN_H
24
25 #include <string.h>
26
27 #include <haproxy/api.h>
28 #include <haproxy/pattern-t.h>
29 #include <haproxy/sample-t.h>
30
31 /* pattern management function arrays */
32 extern char *pat_match_names[PAT_MATCH_NUM];
33 extern int pat_match_types[PAT_MATCH_NUM];
34
35 extern int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **);
36 extern int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **);
37 extern void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *);
38 extern void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *);
39 extern struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int);
40
41 /* This is the root of the list of all pattern_ref avalaibles. */
42 extern struct list pattern_reference;
43
44 int pattern_finalize_config(void);
45
46 /* return the PAT_MATCH_* index for match name "name", or < 0 if not found */
pat_find_match_name(const char * name)47 static 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
57 /* This function executes a pattern match on a sample. It applies pattern <expr>
58 * to sample <smp>. The function returns NULL if the sample don't 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.
62 */
63 struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill);
64
65 /*
66 *
67 * The following function gets "pattern", duplicate it and index it in "expr"
68 *
69 */
70 int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err);
71 int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err);
72 int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err);
73 int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err);
74 int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err);
75 int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err);
76 int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err);
77 int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err);
78
79 /*
80 *
81 * The following functions search pattern <pattern> into the pattern
82 * expression <expr>. If the pattern is found, delete it. This function
83 * never fails.
84 *
85 */
86 void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref);
87 void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref);
88 void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref);
89 void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref);
90 void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref);
91
92 /*
93 *
94 * The following functions clean all entries of a pattern expression and
95 * reset the tree and list root.
96 *
97 */
98 void pat_prune_val(struct pattern_expr *expr);
99 void pat_prune_ptr(struct pattern_expr *expr);
100 void pat_prune_reg(struct pattern_expr *expr);
101
102 /*
103 *
104 * The following functions are general purpose pattern matching functions.
105 *
106 */
107
108
109 /* ignore the current line */
110 int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err);
111
112 /* Parse an integer. It is put both in min and max. */
113 int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err);
114
115 /* Parse an version. It is put both in min and max. */
116 int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err);
117
118 /* Parse a range of integers delimited by either ':' or '-'. If only one
119 * integer is read, it is set as both min and max.
120 */
121 int pat_parse_range(const char *text, struct pattern *pattern, int mflags, char **err);
122
123 /* Parse a string. It is allocated and duplicated. */
124 int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err);
125
126 /* Parse a hexa binary definition. It is allocated and duplicated. */
127 int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err);
128
129 /* Parse a regex. It is allocated. */
130 int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err);
131
132 /* Parse an IP address and an optional mask in the form addr[/mask].
133 * The addr may either be an IPv4 address or a hostname. The mask
134 * may either be a dotted mask or a number of bits. Returns 1 if OK,
135 * otherwise 0.
136 */
137 int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err);
138
139 /* NB: For two strings to be identical, it is required that their lengths match */
140 struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill);
141
142 /* NB: For two binary buffers to be identical, it is required that their lengths match */
143 struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill);
144
145 /* Checks that the length of the pattern in <test> is included between min and max */
146 struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill);
147
148 /* Checks that the integer in <test> is included between min and max */
149 struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill);
150
151 /* always return false */
152 struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill);
153
154 /* Checks that the pattern matches the end of the tested string. */
155 struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill);
156
157 /* Checks that the pattern matches the beginning of the tested string. */
158 struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill);
159
160 /* Checks that the pattern is included inside the tested string. */
161 struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill);
162
163 /* Checks that the pattern is included inside the tested string, but enclosed
164 * between slashes or at the beginning or end of the string. Slashes at the
165 * beginning or end of the pattern are ignored.
166 */
167 struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill);
168
169 /* Checks that the pattern is included inside the tested string, but enclosed
170 * between dots or at the beginning or end of the string. Dots at the beginning
171 * or end of the pattern are ignored.
172 */
173 struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill);
174
175 /* Check that the IPv4 address in <test> matches the IP/mask in pattern */
176 struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill);
177
178 /* Executes a regex. It temporarily changes the data to add a trailing zero,
179 * and restores the previous character when leaving.
180 */
181 struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill);
182 struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill);
183
184 /*
185 * pattern_ref manipulation.
186 */
187 struct pat_ref *pat_ref_lookup(const char *reference);
188 struct pat_ref *pat_ref_lookupid(int unique_id);
189 struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags);
190 struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags);
191 struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key);
192 struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, const char *sample, int line);
193 int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr, int patflags, char **err);
194 int pat_ref_add(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
195 int pat_ref_set(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
196 int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err);
197 int pat_ref_delete(struct pat_ref *ref, const char *key);
198 int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt);
199 int pat_ref_prune(struct pat_ref *ref);
200 int pat_ref_load(struct pat_ref *ref, struct pattern_expr *expr, int patflags, int soe, char **err);
201 void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace);
202
203
204 /*
205 * pattern_head manipulation.
206 */
207 void pattern_init_head(struct pattern_head *head);
208 void pattern_prune(struct pattern_head *head);
209 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);
210
211 /*
212 * pattern_expr manipulation.
213 */
214 void pattern_init_expr(struct pattern_expr *expr);
215 struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref);
216 struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
217 int patflags, char **err, int *reuse);
218 struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *elt);
219 int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref);
220
221
222 #endif
223