blob: 10931a7c22638580b17e00d048a8b9f103c89d6f [file] [log] [blame]
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001/*
2 * Pattern management functions.
3 *
4 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <stdio.h>
15
16#include <common/config.h>
17#include <common/standard.h>
18
19#include <types/global.h>
20#include <types/pattern.h>
21
22#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010023#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010024
25#include <ebsttree.h>
26
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010027char *pat_match_names[PAT_MATCH_NUM] = {
28 [PAT_MATCH_FOUND] = "found",
29 [PAT_MATCH_BOOL] = "bool",
30 [PAT_MATCH_INT] = "int",
31 [PAT_MATCH_IP] = "ip",
32 [PAT_MATCH_BIN] = "bin",
33 [PAT_MATCH_LEN] = "len",
34 [PAT_MATCH_STR] = "str",
35 [PAT_MATCH_BEG] = "beg",
36 [PAT_MATCH_SUB] = "sub",
37 [PAT_MATCH_DIR] = "dir",
38 [PAT_MATCH_DOM] = "dom",
39 [PAT_MATCH_END] = "end",
40 [PAT_MATCH_REG] = "reg",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010041};
42
Thierry FOURNIERedc15c32013-12-13 15:36:59 +010043int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010044 [PAT_MATCH_FOUND] = pat_parse_nothing,
45 [PAT_MATCH_BOOL] = pat_parse_nothing,
46 [PAT_MATCH_INT] = pat_parse_int,
47 [PAT_MATCH_IP] = pat_parse_ip,
48 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010049 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010050 [PAT_MATCH_STR] = pat_parse_str,
51 [PAT_MATCH_BEG] = pat_parse_str,
52 [PAT_MATCH_SUB] = pat_parse_str,
53 [PAT_MATCH_DIR] = pat_parse_str,
54 [PAT_MATCH_DOM] = pat_parse_str,
55 [PAT_MATCH_END] = pat_parse_str,
56 [PAT_MATCH_REG] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057};
58
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010059int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
60 [PAT_MATCH_FOUND] = pat_idx_list_val,
61 [PAT_MATCH_BOOL] = pat_idx_list_val,
62 [PAT_MATCH_INT] = pat_idx_list_val,
63 [PAT_MATCH_IP] = pat_idx_tree_ip,
64 [PAT_MATCH_BIN] = pat_idx_list_ptr,
65 [PAT_MATCH_LEN] = pat_idx_list_val,
66 [PAT_MATCH_STR] = pat_idx_tree_str,
67 [PAT_MATCH_BEG] = pat_idx_list_str,
68 [PAT_MATCH_SUB] = pat_idx_list_str,
69 [PAT_MATCH_DIR] = pat_idx_list_str,
70 [PAT_MATCH_DOM] = pat_idx_list_str,
71 [PAT_MATCH_END] = pat_idx_list_str,
72 [PAT_MATCH_REG] = pat_idx_list_reg,
73};
74
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010075void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
Thierry FOURNIERb1136502014-01-15 11:38:49 +010076 [PAT_MATCH_FOUND] = pat_del_list_val,
77 [PAT_MATCH_BOOL] = pat_del_list_val,
78 [PAT_MATCH_INT] = pat_del_list_val,
79 [PAT_MATCH_IP] = pat_del_tree_ip,
80 [PAT_MATCH_BIN] = pat_del_list_ptr,
81 [PAT_MATCH_LEN] = pat_del_list_val,
82 [PAT_MATCH_STR] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010083 [PAT_MATCH_BEG] = pat_del_list_ptr,
84 [PAT_MATCH_SUB] = pat_del_list_ptr,
85 [PAT_MATCH_DIR] = pat_del_list_ptr,
86 [PAT_MATCH_DOM] = pat_del_list_ptr,
87 [PAT_MATCH_END] = pat_del_list_ptr,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010088 [PAT_MATCH_REG] = pat_del_list_reg,
89};
90
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010091void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
92 [PAT_MATCH_FOUND] = pat_prune_val,
93 [PAT_MATCH_BOOL] = pat_prune_val,
94 [PAT_MATCH_INT] = pat_prune_val,
95 [PAT_MATCH_IP] = pat_prune_val,
96 [PAT_MATCH_BIN] = pat_prune_ptr,
97 [PAT_MATCH_LEN] = pat_prune_val,
98 [PAT_MATCH_STR] = pat_prune_ptr,
99 [PAT_MATCH_BEG] = pat_prune_ptr,
100 [PAT_MATCH_SUB] = pat_prune_ptr,
101 [PAT_MATCH_DIR] = pat_prune_ptr,
102 [PAT_MATCH_DOM] = pat_prune_ptr,
103 [PAT_MATCH_END] = pat_prune_ptr,
104 [PAT_MATCH_REG] = pat_prune_reg,
105};
106
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100107struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100108 [PAT_MATCH_FOUND] = NULL,
109 [PAT_MATCH_BOOL] = pat_match_nothing,
110 [PAT_MATCH_INT] = pat_match_int,
111 [PAT_MATCH_IP] = pat_match_ip,
112 [PAT_MATCH_BIN] = pat_match_bin,
113 [PAT_MATCH_LEN] = pat_match_len,
114 [PAT_MATCH_STR] = pat_match_str,
115 [PAT_MATCH_BEG] = pat_match_beg,
116 [PAT_MATCH_SUB] = pat_match_sub,
117 [PAT_MATCH_DIR] = pat_match_dir,
118 [PAT_MATCH_DOM] = pat_match_dom,
119 [PAT_MATCH_END] = pat_match_end,
120 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100121};
122
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100123/* Just used for checking configuration compatibility */
124int pat_match_types[PAT_MATCH_NUM] = {
125 [PAT_MATCH_FOUND] = SMP_T_UINT,
126 [PAT_MATCH_BOOL] = SMP_T_UINT,
127 [PAT_MATCH_INT] = SMP_T_UINT,
128 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100129 [PAT_MATCH_BIN] = SMP_T_BIN,
130 [PAT_MATCH_LEN] = SMP_T_STR,
131 [PAT_MATCH_STR] = SMP_T_STR,
132 [PAT_MATCH_BEG] = SMP_T_STR,
133 [PAT_MATCH_SUB] = SMP_T_STR,
134 [PAT_MATCH_DIR] = SMP_T_STR,
135 [PAT_MATCH_DOM] = SMP_T_STR,
136 [PAT_MATCH_END] = SMP_T_STR,
137 [PAT_MATCH_REG] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100138};
139
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100140/* this struct is used to return information */
141static struct pattern static_pattern;
142
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100143/* This is the root of the list of all pattern_ref avalaibles. */
144struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
145
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100146/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100147 *
148 * The following functions are not exported and are used by internals process
149 * of pattern matching
150 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100151 */
152
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100153/* Background: Fast way to find a zero byte in a word
154 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
155 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
156 *
157 * To look for 4 different byte values, xor the word with those bytes and
158 * then check for zero bytes:
159 *
160 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
161 * where <delimiter> is the 4 byte values to look for (as an uint)
162 * and <c> is the character that is being tested
163 */
164static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
165{
166 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
167 return (mask - 0x01010101) & ~mask & 0x80808080U;
168}
169
170static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
171{
172 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
173}
174
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100175
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100176/*
177 *
178 * These functions are exported and may be used by any other component.
179 *
180 * The following functions are used for parsing pattern matching
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100181 * input value. The <text> contain the string to be parsed. <pattern>
182 * must be a preallocated pattern. The pat_parse_* functions fill this
183 * structure with the parsed value. <usage> can be PAT_U_COMPILE or
184 * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
185 * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
186 * use "trash" or return pointers to the input strings. In both cases,
187 * the caller must use the value PAT_U_LOOKUP with caution. <err> is
188 * filled with an error message built with memprintf() function.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100189 *
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100190 * In succes case, the pat_parse_* function return 1. If the function
191 * fail, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100192 *
193 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100194
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100195/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100196int pat_parse_nothing(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100197{
198 return 1;
199}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100200
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100202int pat_parse_str(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100203{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100204 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100205 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100206 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100207 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100208}
209
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100210/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100211int pat_parse_bin(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100212{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100213 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100214
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100215 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100216 trash = get_trash_chunk();
217 pattern->len = trash->size;
218 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100219 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100220}
221
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100222/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100223int pat_parse_reg(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100224{
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100225 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100226
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100227 trash = get_trash_chunk();
228 if (trash->size < sizeof(*pattern->ptr.reg)) {
229 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
230 (int)sizeof(*pattern->ptr.reg), trash->size);
231 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100232 }
233
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100234 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100236 return 1;
237}
238
239/* Parse a range of positive integers delimited by either ':' or '-'. If only
240 * one integer is read, it is set as both min and max. An operator may be
241 * specified as the prefix, among this list of 5 :
242 *
243 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
244 *
245 * The default operator is "eq". It supports range matching. Ranges are
246 * rejected for other operators. The operator may be changed at any time.
247 * The operator is stored in the 'opaque' argument.
248 *
249 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100250 * the caller will have to free it. The function returns zero on error, and
251 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100252 *
253 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100254int pat_parse_int(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100255{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100256 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100257
258 pattern->type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100259
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100260 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100261 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100262 goto not_valid_range;
263
264 /* Search ':' or '-' separator. */
265 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
266 ptr++;
267
268 /* If separator not found. */
269 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100270 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
271 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100272 return 0;
273 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100274 pattern->val.range.max = pattern->val.range.min;
275 pattern->val.range.min_set = 1;
276 pattern->val.range.max_set = 1;
277 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100278 }
279
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100280 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100281 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100282 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
283 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100284
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100285 pattern->val.range.min_set = 0;
286 pattern->val.range.max_set = 1;
287 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100288 }
289
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100290 /* If separator is the last character. */
291 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100292 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100293 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100294
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100295 pattern->val.range.min_set = 1;
296 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100297 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100298 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100299
300 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100301 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100302 goto not_valid_range;
303
304 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
305 goto not_valid_range;
306
307 if (pattern->val.range.min > pattern->val.range.max)
308 goto not_valid_range;
309
310 pattern->val.range.min_set = 1;
311 pattern->val.range.max_set = 1;
312 return 1;
313
314 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100315 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100316 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100317}
318
319/* Parse a range of positive 2-component versions delimited by either ':' or
320 * '-'. The version consists in a major and a minor, both of which must be
321 * smaller than 65536, because internally they will be represented as a 32-bit
322 * integer.
323 * If only one version is read, it is set as both min and max. Just like for
324 * pure integers, an operator may be specified as the prefix, among this list
325 * of 5 :
326 *
327 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
328 *
329 * The default operator is "eq". It supports range matching. Ranges are
330 * rejected for other operators. The operator may be changed at any time.
331 * The operator is stored in the 'opaque' argument. This allows constructs
332 * such as the following one :
333 *
334 * acl obsolete_ssl ssl_req_proto lt 3
335 * acl unsupported_ssl ssl_req_proto gt 3.1
336 * acl valid_ssl ssl_req_proto 3.0-3.1
337 *
338 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100339int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100340{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100341 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100342
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100343 pattern->type = SMP_T_UINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100344
345 /* Search ':' or '-' separator. */
346 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
347 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100348
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100349 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100350 if (*ptr == '\0' && ptr > text) {
351 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
352 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100353 return 0;
354 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100355 pattern->val.range.max = pattern->val.range.min;
356 pattern->val.range.min_set = 1;
357 pattern->val.range.max_set = 1;
358 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100359 }
360
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100361 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100362 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100363 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100364 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100365 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100366 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100367 pattern->val.range.min_set = 0;
368 pattern->val.range.max_set = 1;
369 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100370 }
371
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100372 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100373 if (ptr == &text[strlen(text)-1]) {
374 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
375 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100376 return 0;
377 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100378 pattern->val.range.min_set = 1;
379 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100380 return 1;
381 }
382
383 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100384 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
385 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100386 return 0;
387 }
388 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100389 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100390 return 0;
391 }
392 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100393 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100394 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100395 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100396 pattern->val.range.min_set = 1;
397 pattern->val.range.max_set = 1;
398 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100399}
400
401/* Parse an IP address and an optional mask in the form addr[/mask].
402 * The addr may either be an IPv4 address or a hostname. The mask
403 * may either be a dotted mask or a number of bits. Returns 1 if OK,
404 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
405 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100406int pat_parse_ip(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100407{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100408 if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100409 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100410 return 1;
411 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100412 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100413 pattern->type = SMP_T_IPV6;
414 return 1;
415 }
416 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100417 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100418 return 0;
419 }
420}
421
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100422/*
423 *
424 * These functions are exported and may be used by any other component.
425 *
426 * This fucntion just take a sample <smp> and check if this sample match
427 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
428 * PAT_NOMATCH.
429 *
430 */
431
432/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100433struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100434{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100435 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436}
437
438
439/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100440struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100441{
442 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100443 struct ebmb_node *node;
444 char prev;
445 struct pattern_tree *elt;
446 struct pattern_list *lst;
447 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100448
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100449 /* Lookup a string in the expression's pattern tree. */
450 if (!eb_is_empty(&expr->pattern_tree)) {
451 /* we may have to force a trailing zero on the test pattern */
452 prev = smp->data.str.str[smp->data.str.len];
453 if (prev)
454 smp->data.str.str[smp->data.str.len] = '\0';
455 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
456 if (prev)
457 smp->data.str.str[smp->data.str.len] = prev;
458
459 if (node) {
460 if (fill) {
461 elt = ebmb_entry(node, struct pattern_tree, node);
462 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100463 static_pattern.ref = elt->ref;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100464 static_pattern.flags = PAT_F_TREE;
465 static_pattern.type = SMP_T_STR;
466 static_pattern.ptr.str = (char *)elt->node.key;
467 }
468 return &static_pattern;
469 }
470 }
471
472 /* look in the list */
473 list_for_each_entry(lst, &expr->patterns, list) {
474 pattern = &lst->pat;
475
476 if (pattern->len != smp->data.str.len)
477 continue;
478
479 icase = pattern->flags & PAT_F_IGNORE_CASE;
480 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
481 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
482 return pattern;
483 }
484
485 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100486}
487
488/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100489struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100490{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100491 struct pattern_list *lst;
492 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100493
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100494 /* Look in the list. */
495 list_for_each_entry(lst, &expr->patterns, list) {
496 pattern = &lst->pat;
497
498 if (pattern->len != smp->data.str.len)
499 continue;
500
501 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
502 return pattern;
503 }
504
505 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100506}
507
508/* Executes a regex. It temporarily changes the data to add a trailing zero,
509 * and restores the previous character when leaving.
510 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100511struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100512{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100513 struct pattern_list *lst;
514 struct pattern *pattern;
515
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100516 /* look in the list */
517 list_for_each_entry(lst, &expr->patterns, list) {
518 pattern = &lst->pat;
519
520 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
521 return pattern;
522 }
523 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524}
525
526/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100527struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100528{
529 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100530 struct pattern_list *lst;
531 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100532
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100533 list_for_each_entry(lst, &expr->patterns, list) {
534 pattern = &lst->pat;
535
536 if (pattern->len > smp->data.str.len)
537 continue;
538
539 icase = pattern->flags & PAT_F_IGNORE_CASE;
540 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
541 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
542 continue;
543
544 return pattern;
545 }
546 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100547}
548
549/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100550struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100551{
552 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100553 struct pattern_list *lst;
554 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100555
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100556 list_for_each_entry(lst, &expr->patterns, list) {
557 pattern = &lst->pat;
558
559 if (pattern->len > smp->data.str.len)
560 continue;
561
562 icase = pattern->flags & PAT_F_IGNORE_CASE;
563 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
564 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
565 continue;
566
567 return pattern;
568 }
569 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100570}
571
572/* Checks that the pattern is included inside the tested string.
573 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
574 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100575struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100576{
577 int icase;
578 char *end;
579 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100580 struct pattern_list *lst;
581 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100582
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100583 list_for_each_entry(lst, &expr->patterns, list) {
584 pattern = &lst->pat;
585
586 if (pattern->len > smp->data.str.len)
587 continue;
588
589 end = smp->data.str.str + smp->data.str.len - pattern->len;
590 icase = pattern->flags & PAT_F_IGNORE_CASE;
591 if (icase) {
592 for (c = smp->data.str.str; c <= end; c++) {
593 if (tolower(*c) != tolower(*pattern->ptr.str))
594 continue;
595 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
596 return pattern;
597 }
598 } else {
599 for (c = smp->data.str.str; c <= end; c++) {
600 if (*c != *pattern->ptr.str)
601 continue;
602 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
603 return pattern;
604 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100605 }
606 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100607 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100608}
609
610/* This one is used by other real functions. It checks that the pattern is
611 * included inside the tested string, but enclosed between the specified
612 * delimiters or at the beginning or end of the string. The delimiters are
613 * provided as an unsigned int made by make_4delim() and match up to 4 different
614 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
615 */
616static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
617{
618 int may_match, icase;
619 char *c, *end;
620 char *ps;
621 int pl;
622
623 pl = pattern->len;
624 ps = pattern->ptr.str;
625
626 while (pl > 0 && is_delimiter(*ps, delimiters)) {
627 pl--;
628 ps++;
629 }
630
631 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
632 pl--;
633
634 if (pl > smp->data.str.len)
635 return PAT_NOMATCH;
636
637 may_match = 1;
638 icase = pattern->flags & PAT_F_IGNORE_CASE;
639 end = smp->data.str.str + smp->data.str.len - pl;
640 for (c = smp->data.str.str; c <= end; c++) {
641 if (is_delimiter(*c, delimiters)) {
642 may_match = 1;
643 continue;
644 }
645
646 if (!may_match)
647 continue;
648
649 if (icase) {
650 if ((tolower(*c) == tolower(*ps)) &&
651 (strncasecmp(ps, c, pl) == 0) &&
652 (c == end || is_delimiter(c[pl], delimiters)))
653 return PAT_MATCH;
654 } else {
655 if ((*c == *ps) &&
656 (strncmp(ps, c, pl) == 0) &&
657 (c == end || is_delimiter(c[pl], delimiters)))
658 return PAT_MATCH;
659 }
660 may_match = 0;
661 }
662 return PAT_NOMATCH;
663}
664
665/* Checks that the pattern is included inside the tested string, but enclosed
666 * between the delimiters '?' or '/' or at the beginning or end of the string.
667 * Delimiters at the beginning or end of the pattern are ignored.
668 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100669struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100670{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100671 struct pattern_list *lst;
672 struct pattern *pattern;
673
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100674 list_for_each_entry(lst, &expr->patterns, list) {
675 pattern = &lst->pat;
676 if (match_word(smp, pattern, make_4delim('/', '?', '?', '?')))
677 return pattern;
678 }
679 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100680}
681
682/* Checks that the pattern is included inside the tested string, but enclosed
683 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
684 * the string. Delimiters at the beginning or end of the pattern are ignored.
685 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100686struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100687{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100688 struct pattern_list *lst;
689 struct pattern *pattern;
690
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100691 list_for_each_entry(lst, &expr->patterns, list) {
692 pattern = &lst->pat;
693 if (match_word(smp, pattern, make_4delim('/', '?', '.', ':')))
694 return pattern;
695 }
696 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100697}
698
699/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100700struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100701{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100702 struct pattern_list *lst;
703 struct pattern *pattern;
704
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100705 list_for_each_entry(lst, &expr->patterns, list) {
706 pattern = &lst->pat;
707 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
708 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
709 return pattern;
710 }
711 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100712}
713
714/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100715struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100716{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100717 struct pattern_list *lst;
718 struct pattern *pattern;
719
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100720 list_for_each_entry(lst, &expr->patterns, list) {
721 pattern = &lst->pat;
722 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
723 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
724 return pattern;
725 }
726 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100727}
728
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100729struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100730{
731 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100732 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100733 struct in_addr *s;
734 struct ebmb_node *node;
735 struct pattern_tree *elt;
736 struct pattern_list *lst;
737 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100738
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100739 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100740 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100741 /* Lookup an IPv4 address in the expression's pattern tree using
742 * the longest match method.
743 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100744 s = &smp->data.ipv4;
745 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
746 if (node) {
747 if (fill) {
748 elt = ebmb_entry(node, struct pattern_tree, node);
749 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100750 static_pattern.ref = elt->ref;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100751 static_pattern.flags = PAT_F_TREE;
752 static_pattern.type = SMP_T_IPV4;
753 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
754 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
755 return NULL;
756 }
757 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100758 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100759
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100760 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
761 * sample address to IPv6 with the mapping method using the ::ffff:
762 * prefix, and try to lookup in the IPv6 tree.
763 */
764 memset(&tmp6, 0, 10);
765 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
766 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
767 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
768 if (node) {
769 if (fill) {
770 elt = ebmb_entry(node, struct pattern_tree, node);
771 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100772 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100773 static_pattern.flags = PAT_F_TREE;
774 static_pattern.type = SMP_T_IPV6;
775 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
776 static_pattern.val.ipv6.mask = elt->node.node.pfx;
777 }
778 return &static_pattern;
779 }
780 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100781
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100782 /* The input sample is IPv6. Try to match in the trees. */
783 if (smp->type == SMP_T_IPV6) {
784 /* Lookup an IPv6 address in the expression's pattern tree using
785 * the longest match method.
786 */
787 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
788 if (node) {
789 if (fill) {
790 elt = ebmb_entry(node, struct pattern_tree, node);
791 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100792 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100793 static_pattern.flags = PAT_F_TREE;
794 static_pattern.type = SMP_T_IPV6;
795 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
796 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100797 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100798 return &static_pattern;
799 }
800
801 /* Try to convert 6 to 4 when the start of the ipv6 address match the
802 * following forms :
803 * - ::ffff:ip:v4 (ipv4 mapped)
804 * - ::0000:ip:v4 (old ipv4 mapped)
805 * - 2002:ip:v4:: (6to4)
806 */
807 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
808 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
809 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
810 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
811 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
812 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
813 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
814 else
815 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
816 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
817
818 /* Lookup an IPv4 address in the expression's pattern tree using the longest
819 * match method.
820 */
821 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
822 if (node) {
823 if (fill) {
824 elt = ebmb_entry(node, struct pattern_tree, node);
825 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100826 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100827 static_pattern.flags = PAT_F_TREE;
828 static_pattern.type = SMP_T_IPV4;
829 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
830 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
831 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100832 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100833 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100834 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100835 }
836 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100837
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100838 /* Lookup in the list. the list contain only IPv4 patterns */
839 list_for_each_entry(lst, &expr->patterns, list) {
840 pattern = &lst->pat;
841
842 /* The input sample is IPv4, use it as is. */
843 if (smp->type == SMP_T_IPV4) {
844 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100845 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100846 else if (smp->type == SMP_T_IPV6) {
847 /* v4 match on a V6 sample. We want to check at least for
848 * the following forms :
849 * - ::ffff:ip:v4 (ipv4 mapped)
850 * - ::0000:ip:v4 (old ipv4 mapped)
851 * - 2002:ip:v4:: (6to4)
852 */
853 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
854 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
855 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
856 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
857 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100858 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100859 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
860 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
861 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100862 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100863 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100864 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100865 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100866
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100867 /* Check if the input sample match the current pattern. */
868 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100869 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100870 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100871 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100872}
873
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100874void free_pattern_tree(struct eb_root *root)
875{
876 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100877 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100878
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100879 node = eb_first(root);
880 while (node) {
881 next = eb_next(node);
882 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100883 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100884 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100885 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100886 node = next;
887 }
888}
889
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100890void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100891{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100892 struct pattern_list *pat, *tmp;
893
894 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
895 free(pat->pat.smp);
896 free(pat);
897 }
898
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100899 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100900 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100901 LIST_INIT(&expr->patterns);
902}
903
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100904void pat_prune_ptr(struct pattern_expr *expr)
905{
906 struct pattern_list *pat, *tmp;
907
908 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
909 free(pat->pat.ptr.ptr);
910 free(pat->pat.smp);
911 free(pat);
912 }
913
914 free_pattern_tree(&expr->pattern_tree);
915 free_pattern_tree(&expr->pattern_tree_2);
916 LIST_INIT(&expr->patterns);
917}
918
919void pat_prune_reg(struct pattern_expr *expr)
920{
921 struct pattern_list *pat, *tmp;
922
923 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
924 regex_free(pat->pat.ptr.ptr);
925 free(pat->pat.smp);
926 free(pat);
927 }
928
929 free_pattern_tree(&expr->pattern_tree);
930 free_pattern_tree(&expr->pattern_tree_2);
931 LIST_INIT(&expr->patterns);
932}
933
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100934/*
935 *
936 * The following functions are used for the pattern indexation
937 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100938 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100939
940int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100941{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100942 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100943
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100944 /* allocate pattern */
945 patl = calloc(1, sizeof(*patl));
946 if (!patl) {
947 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100948 return 0;
949 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100950
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100951 /* duplicate pattern */
952 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100953
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100954 /* chain pattern in the expression */
955 LIST_ADDQ(&expr->patterns, &patl->list);
956
957 /* that's ok */
958 return 1;
959}
960
961int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
962{
963 struct pattern_list *patl;
964
965 /* allocate pattern */
966 patl = calloc(1, sizeof(*patl));
967 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100968 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100969
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100970 /* duplicate pattern */
971 memcpy(&patl->pat, pat, sizeof(*pat));
972 patl->pat.ptr.ptr = malloc(patl->pat.len);
973 if (!patl->pat.ptr.ptr) {
974 free(patl);
975 memprintf(err, "out of memory while indexing pattern");
976 return 0;
977 }
978 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100979
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100980 /* chain pattern in the expression */
981 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100982
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100983 /* that's ok */
984 return 1;
985}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100986
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100987int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
988{
989 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100990
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100991 /* allocate pattern */
992 patl = calloc(1, sizeof(*patl));
993 if (!patl) {
994 memprintf(err, "out of memory while indexing pattern");
995 return 0;
996 }
997
998 /* duplicate pattern */
999 memcpy(&patl->pat, pat, sizeof(*pat));
1000 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1001 if (!patl->pat.ptr.str) {
1002 free(patl);
1003 memprintf(err, "out of memory while indexing pattern");
1004 return 0;
1005 }
1006 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1007 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001008
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001009 /* chain pattern in the expression */
1010 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001011
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001012 /* that's ok */
1013 return 1;
1014}
1015
1016int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1017{
1018 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001019
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001020 /* allocate pattern */
1021 patl = calloc(1, sizeof(*patl));
1022 if (!patl) {
1023 memprintf(err, "out of memory while indexing pattern");
1024 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001025 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001026
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001027 /* duplicate pattern */
1028 memcpy(&patl->pat, pat, sizeof(*pat));
1029
1030 /* allocate regex */
1031 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1032 if (!patl->pat.ptr.reg) {
1033 free(patl);
1034 memprintf(err, "out of memory while indexing pattern");
1035 return 0;
1036 }
1037
1038 /* compile regex */
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +01001039 if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001040 free(patl);
1041 free(patl->pat.ptr.reg);
1042 return 0;
1043 }
1044
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001045 /* chain pattern in the expression */
1046 LIST_ADDQ(&expr->patterns, &patl->list);
1047
1048 /* that's ok */
1049 return 1;
1050}
1051
1052int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1053{
1054 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001055 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001056
1057 /* Only IPv4 can be indexed */
1058 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001059 /* in IPv4 case, check if the mask is contiguous so that we can
1060 * insert the network into the tree. A continuous mask has only
1061 * ones on the left. This means that this mask + its lower bit
1062 * added once again is null.
1063 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001064 mask = ntohl(pat->val.ipv4.mask.s_addr);
1065 if (mask + (mask & -mask) == 0) {
1066 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001067
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001068 /* node memory allocation */
1069 node = calloc(1, sizeof(*node) + 4);
1070 if (!node) {
1071 memprintf(err, "out of memory while loading pattern");
1072 return 0;
1073 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001074
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001075 /* copy the pointer to sample associated to this node */
1076 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001077 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001078
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001079 /* FIXME: insert <addr>/<mask> into the tree here */
1080 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1081 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001082
1083 /* Insert the entry. */
1084 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001085
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001086 /* that's ok */
1087 return 1;
1088 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001089 else {
1090 /* If the mask is not contiguous, just add the pattern to the list */
1091 return pat_idx_list_val(expr, pat, err);
1092 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001093 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001094 else if (pat->type == SMP_T_IPV6) {
1095 /* IPv6 also can be indexed */
1096 node = calloc(1, sizeof(*node) + 16);
1097 if (!node) {
1098 memprintf(err, "out of memory while loading pattern");
1099 return 0;
1100 }
1101
1102 /* copy the pointer to sample associated to this node */
1103 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001104 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001105
1106 /* FIXME: insert <addr>/<mask> into the tree here */
1107 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1108 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001109
1110 /* Insert the entry. */
1111 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001112
1113 /* that's ok */
1114 return 1;
1115 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001116
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001117 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001118}
1119
1120int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1121{
1122 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001123 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001124
1125 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001126 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001127 memprintf(err, "internal error: string expected, but the type is '%s'",
1128 smp_to_type[pat->type]);
1129 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001130 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001131
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001132 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1133 if (pat->flags & PAT_F_IGNORE_CASE)
1134 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001135
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001136 /* Process the key len */
1137 len = strlen(pat->ptr.str) + 1;
1138
1139 /* node memory allocation */
1140 node = calloc(1, sizeof(*node) + len);
1141 if (!node) {
1142 memprintf(err, "out of memory while loading pattern");
1143 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001144 }
1145
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001146 /* copy the pointer to sample associated to this node */
1147 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001148 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001149
1150 /* copy the string */
1151 memcpy(node->node.key, pat->ptr.str, len);
1152
1153 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001154 ebst_insert(&expr->pattern_tree, &node->node);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001155
1156 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001157 return 1;
1158}
1159
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001160void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001161{
1162 struct pattern_list *pat;
1163 struct pattern_list *safe;
1164
1165 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1166 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001167 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001168 continue;
1169
1170 /* Delete and free entry. */
1171 LIST_DEL(&pat->list);
1172 free(pat->pat.smp);
1173 free(pat);
1174 }
1175}
1176
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001177void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001178{
1179 struct ebmb_node *node, *next_node;
1180 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001181
1182 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001183 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1184 node;
1185 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1186 /* Extract container of the tree node. */
1187 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001188
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001189 /* Check equality. */
1190 if (elt->ref != ref)
1191 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001192
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001193 /* Delete and free entry. */
1194 ebmb_delete(node);
1195 free(elt->smp);
1196 free(elt);
1197 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001198
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001199 /* Browse each node of the list for IPv4 addresses. */
1200 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001201
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001202 /* browse each node of the tree for IPv6 addresses. */
1203 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1204 node;
1205 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1206 /* Extract container of the tree node. */
1207 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001208
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001209 /* Check equality. */
1210 if (elt->ref != ref)
1211 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001212
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001213 /* Delete and free entry. */
1214 ebmb_delete(node);
1215 free(elt->smp);
1216 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001217 }
1218}
1219
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001220void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001221{
1222 struct pattern_list *pat;
1223 struct pattern_list *safe;
1224
1225 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1226 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001227 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001228 continue;
1229
1230 /* Delete and free entry. */
1231 LIST_DEL(&pat->list);
1232 free(pat->pat.ptr.ptr);
1233 free(pat->pat.smp);
1234 free(pat);
1235 }
1236}
1237
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001238void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001239{
1240 struct ebmb_node *node, *next_node;
1241 struct pattern_tree *elt;
1242
1243 /* browse each node of the tree. */
1244 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1245 node;
1246 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1247 /* Extract container of the tree node. */
1248 elt = container_of(node, struct pattern_tree, node);
1249
1250 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001251 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001252 continue;
1253
1254 /* Delete and free entry. */
1255 ebmb_delete(node);
1256 free(elt->smp);
1257 free(elt);
1258 }
1259}
1260
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001261void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001262{
1263 struct pattern_list *pat;
1264 struct pattern_list *safe;
1265
1266 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1267 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001268 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001269 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001270
1271 /* Delete and free entry. */
1272 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001273 regex_free(pat->pat.ptr.ptr);
1274 free(pat->pat.smp);
1275 free(pat);
1276 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001277}
1278
1279void pattern_init_expr(struct pattern_expr *expr)
1280{
1281 LIST_INIT(&expr->patterns);
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001282 expr->pattern_tree = EB_ROOT;
1283 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001284}
1285
1286void pattern_init_head(struct pattern_head *head)
1287{
1288 LIST_INIT(&head->head);
1289}
1290
1291/* The following functions are relative to the management of the reference
1292 * lists. These lists are used to store the original pattern and associated
1293 * value as string form.
1294 *
1295 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001296 *
1297 * The pattern reference are stored with two identifiers: the unique_id and
1298 * the reference.
1299 *
1300 * The reference identify a file. Each file with the same name point to the
1301 * same reference. We can register many times one file. If the file is modified,
1302 * all his dependencies are also modified. The reference can be used with map or
1303 * acl.
1304 *
1305 * The unique_id identify inline acl. The unique id is unique for each acl.
1306 * You cannot force the same id in the configuration file, because this repoort
1307 * an error.
1308 *
1309 * A particular case appears if the filename is a number. In this case, the
1310 * unique_id is set with the number represented by the filename and the
1311 * reference is also set. This method prevent double unique_id.
1312 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001313 */
1314
1315/* This function lookup for reference. If the reference is found, they return
1316 * pointer to the struct pat_ref, else return NULL.
1317 */
1318struct pat_ref *pat_ref_lookup(const char *reference)
1319{
1320 struct pat_ref *ref;
1321
1322 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001323 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001324 return ref;
1325 return NULL;
1326}
1327
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001328/* This function lookup for unique id. If the reference is found, they return
1329 * pointer to the struct pat_ref, else return NULL.
1330 */
1331struct pat_ref *pat_ref_lookupid(int unique_id)
1332{
1333 struct pat_ref *ref;
1334
1335 list_for_each_entry(ref, &pattern_reference, list)
1336 if (ref->unique_id == unique_id)
1337 return ref;
1338 return NULL;
1339}
1340
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001341/* This function remove all pattern matching the pointer <refelt> from
1342 * the the reference and from each expr member of the reference. This
1343 * function returns 1 if the deletion is done and return 0 is the entry
1344 * is not found.
1345 */
1346int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1347{
1348 struct pattern_expr *expr;
1349 struct pat_ref_elt *elt, *safe;
1350
1351 /* delete pattern from reference */
1352 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1353 if (elt == refelt) {
1354 LIST_DEL(&elt->list);
1355 free(elt->sample);
1356 free(elt->pattern);
1357 free(elt);
1358
1359 list_for_each_entry(expr, &ref->pat, list)
1360 pattern_delete(expr, elt);
1361
1362 return 1;
1363 }
1364 }
1365 return 0;
1366}
1367
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001368/* This function remove all pattern match <key> from the the reference
1369 * and from each expr member of the reference. This fucntion returns 1
1370 * if the deletion is done and return 0 is the entry is not found.
1371 */
1372int pat_ref_delete(struct pat_ref *ref, const char *key)
1373{
1374 struct pattern_expr *expr;
1375 struct pat_ref_elt *elt, *safe;
1376 int found = 0;
1377
1378 /* delete pattern from reference */
1379 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1380 if (strcmp(key, elt->pattern) == 0) {
1381 LIST_DEL(&elt->list);
1382 free(elt->sample);
1383 free(elt->pattern);
1384 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001385
1386 list_for_each_entry(expr, &ref->pat, list)
1387 pattern_delete(expr, elt);
1388
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001389 found = 1;
1390 }
1391 }
1392
1393 if (!found)
1394 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001395 return 1;
1396}
1397
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001398 /* This function modify the sample of the first pattern that match the <key>. */
1399static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001400 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001401{
1402 struct pattern_expr *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001403 struct sample_storage **smp;
1404 char *sample;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001405 struct sample_storage test;
1406
1407 /* Try all needed converters. */
1408 list_for_each_entry(expr, &ref->pat, list) {
1409 if (!expr->pat_head->parse_smp)
1410 continue;
1411
1412 if (!expr->pat_head->parse_smp(value, &test)) {
1413 memprintf(err, "unable to parse '%s'", value);
1414 return 0;
1415 }
1416 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001417
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001418 /* Modify pattern from reference. */
1419 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001420 if (!sample) {
1421 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001422 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001423 }
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001424 free(elt->sample);
1425 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001426
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001427 /* Load sample in each reference. All the conversion are tested
1428 * below, normally these calls dosn't fail.
1429 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001430 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001431 if (!expr->pat_head->parse_smp)
1432 continue;
1433
1434 smp = pattern_find_smp(expr, elt);
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001435 if (smp && *smp && !expr->pat_head->parse_smp(sample, *smp))
1436 *smp = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001437 }
1438
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001439 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001440}
1441
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001442/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001443int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001444{
1445 struct pat_ref_elt *elt;
1446
1447 /* Look for pattern in the reference. */
1448 list_for_each_entry(elt, &ref->head, list) {
1449 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001450 if (!pat_ref_set_elt(ref, elt, value, err))
1451 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001452 return 1;
1453 }
1454 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001455
1456 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001457 return 0;
1458}
1459
1460/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001461int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001462{
1463 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001464 int found = 0;
1465 char *_merr;
1466 char **merr;
1467
1468 if (err) {
1469 merr = &_merr;
1470 *merr = NULL;
1471 }
1472 else
1473 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001474
1475 /* Look for pattern in the reference. */
1476 list_for_each_entry(elt, &ref->head, list) {
1477 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001478 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1479 if (!found)
1480 *err = *merr;
1481 else {
1482 memprintf(err, "%s, %s", *err, *merr);
1483 free(*merr);
1484 *merr = NULL;
1485 }
1486 }
1487 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001488 }
1489 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001490
1491 if (!found) {
1492 memprintf(err, "entry not found");
1493 return 0;
1494 }
1495 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001496}
1497
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001498/* This function create new reference. <ref> is the reference name.
1499 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1500 * be unique. The user must check the reference with "pat_ref_lookup()"
1501 * before calling this function. If the fucntion fail, it return NULL,
1502 * else return new struct pat_ref.
1503 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001504struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001505{
1506 struct pat_ref *ref;
1507
1508 ref = malloc(sizeof(*ref));
1509 if (!ref)
1510 return NULL;
1511
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001512 if (display) {
1513 ref->display = strdup(display);
1514 if (!ref->display) {
1515 free(ref);
1516 return NULL;
1517 }
1518 }
1519 else
1520 ref->display = NULL;
1521
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001522 ref->reference = strdup(reference);
1523 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001524 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001525 free(ref);
1526 return NULL;
1527 }
1528
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001529 ref->flags = flags;
1530 ref->unique_id = -1;
1531
1532 LIST_INIT(&ref->head);
1533 LIST_INIT(&ref->pat);
1534
1535 LIST_ADDQ(&pattern_reference, &ref->list);
1536
1537 return ref;
1538}
1539
1540/* This function create new reference. <unique_id> is the unique id. If
1541 * the value of <unique_id> is -1, the unique id is calculated later.
1542 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1543 * be unique. The user must check the reference with "pat_ref_lookup()"
1544 * or pat_ref_lookupid before calling this function. If the function
1545 * fail, it return NULL, else return new struct pat_ref.
1546 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001547struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001548{
1549 struct pat_ref *ref;
1550
1551 ref = malloc(sizeof(*ref));
1552 if (!ref)
1553 return NULL;
1554
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001555 if (display) {
1556 ref->display = strdup(display);
1557 if (!ref->display) {
1558 free(ref);
1559 return NULL;
1560 }
1561 }
1562 else
1563 ref->display = NULL;
1564
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001565 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001566 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001567 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001568 LIST_INIT(&ref->head);
1569 LIST_INIT(&ref->pat);
1570
1571 LIST_ADDQ(&pattern_reference, &ref->list);
1572
1573 return ref;
1574}
1575
1576/* This function adds entry to <ref>. It can failed with memory error.
1577 * If the function fails, it returns 0.
1578 */
1579int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1580{
1581 struct pat_ref_elt *elt;
1582
1583 elt = malloc(sizeof(*elt));
1584 if (!elt)
1585 return 0;
1586
1587 elt->line = line;
1588
1589 elt->pattern = strdup(pattern);
1590 if (!elt->pattern) {
1591 free(elt);
1592 return 0;
1593 }
1594
1595 if (sample) {
1596 elt->sample = strdup(sample);
1597 if (!elt->sample) {
1598 free(elt->pattern);
1599 free(elt);
1600 return 0;
1601 }
1602 }
1603 else
1604 elt->sample = NULL;
1605
1606 LIST_ADDQ(&ref->head, &elt->list);
1607
1608 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001609}
1610
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001611/* This function create sample found in <elt>, parse the pattern also
1612 * found in <elt> and insert it in <expr>. The function copy <patflags>
1613 * in <expr>. If the function fails, it returns0 and <err> is filled.
1614 * In succes case, the function returns 1.
1615 */
1616static inline
1617int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1618 int patflags, char **err)
1619{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001620 struct sample_storage *smp;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001621 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001622
1623 /* Create sample */
1624 if (elt->sample && expr->pat_head->parse_smp) {
1625 /* New sample. */
1626 smp = malloc(sizeof(*smp));
1627 if (!smp)
1628 return 0;
1629
1630 /* Parse value. */
1631 if (!expr->pat_head->parse_smp(elt->sample, smp)) {
1632 memprintf(err, "unable to parse '%s'", elt->sample);
1633 free(smp);
1634 return 0;
1635 }
1636
1637 }
1638 else
1639 smp = NULL;
1640
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001641 /* initialise pattern */
1642 memset(&pattern, 0, sizeof(pattern));
1643 pattern.flags = patflags;
1644 pattern.smp = smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001645 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001646
1647 /* parse pattern */
1648 if (!expr->pat_head->parse(elt->pattern, &pattern, err)) {
1649 free(smp);
1650 return 0;
1651 }
1652
1653 /* index pattern */
1654 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001655 free(smp);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001656 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001657 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001658
1659 return 1;
1660}
1661
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001662/* This function adds entry to <ref>. It can failed with memory error. The new
1663 * entry is added at all the pattern_expr registered in this reference. The
1664 * function stop on the first error encountered. It returns 0 and err is
1665 * filled. If an error is encountered, the complete add operation is cancelled.
1666 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001667 */
1668int pat_ref_add(struct pat_ref *ref,
1669 const char *pattern, const char *sample,
1670 char **err)
1671{
1672 struct pat_ref_elt *elt;
1673 struct pattern_expr *expr;
1674
1675 elt = malloc(sizeof(*elt));
1676 if (!elt) {
1677 memprintf(err, "out of memory error");
1678 return 0;
1679 }
1680
1681 elt->line = -1;
1682
1683 elt->pattern = strdup(pattern);
1684 if (!elt->pattern) {
1685 free(elt);
1686 memprintf(err, "out of memory error");
1687 return 0;
1688 }
1689
1690 if (sample) {
1691 elt->sample = strdup(sample);
1692 if (!elt->sample) {
1693 free(elt->pattern);
1694 free(elt);
1695 memprintf(err, "out of memory error");
1696 return 0;
1697 }
1698 }
1699 else
1700 elt->sample = NULL;
1701
1702 LIST_ADDQ(&ref->head, &elt->list);
1703
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001704 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001705 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001706 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001707 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001708 return 0;
1709 }
1710 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001711 return 1;
1712}
1713
1714/* This function prune all entries of <ref>. This function
1715 * prune the associated pattern_expr.
1716 */
1717void pat_ref_prune(struct pat_ref *ref)
1718{
1719 struct pat_ref_elt *elt, *safe;
1720 struct pattern_expr *expr;
1721
1722 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1723 LIST_DEL(&elt->list);
1724 free(elt->pattern);
1725 free(elt->sample);
1726 free(elt);
1727 }
1728
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001729 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001730 expr->pat_head->prune(expr);
1731}
1732
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001733/* This function lookup for existing reference <ref> in pattern_head <head>. */
1734struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1735{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001736 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001737
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001738 list_for_each_entry(expr, &head->head, list)
1739 if (expr->expr->ref == ref)
1740 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001741 return NULL;
1742}
1743
1744/* This function create new pattern_expr associated to the reference <ref>.
1745 * <ref> can be NULL. If an error is occured, the function returns NULL and
1746 * <err> is filled. Otherwise, the function returns new pattern_expr linked
1747 * with <head> and <ref>.
1748 */
1749struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err)
1750{
1751 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001752 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001753
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001754 /* Memory and initialization of the chain element. */
1755 list = malloc(sizeof(*list));
1756 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001757 memprintf(err, "out of memory");
1758 return NULL;
1759 }
1760
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001761 /* Look for existing similar expr. No that only the index, parse and
1762 * parse_smp function must be identical for having similar pattern.
1763 * The other function depends of theses first.
1764 */
1765 if (ref) {
1766 list_for_each_entry(expr, &ref->pat, list)
1767 if (expr->pat_head->index == head->index &&
1768 expr->pat_head->parse == head->parse &&
1769 expr->pat_head->parse_smp == head->parse_smp)
1770 break;
1771 if (&expr->list == &ref->pat)
1772 expr = NULL;
1773 }
1774 else
1775 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001776
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001777 /* If no similar expr was found, we create new expr. */
1778 if (!expr) {
1779 /* Get a lot of memory for the expr struct. */
1780 expr = malloc(sizeof(*expr));
1781 if (!expr) {
1782 memprintf(err, "out of memory");
1783 return NULL;
1784 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001785
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001786 /* Initialize this new expr. */
1787 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001788
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001789 /* This new pattern expression reference one of his heads. */
1790 expr->pat_head = head;
1791
1792 /* Link with ref, or to self to facilitate LIST_DEL() */
1793 if (ref)
1794 LIST_ADDQ(&ref->pat, &expr->list);
1795 else
1796 LIST_INIT(&expr->list);
1797
1798 expr->ref = ref;
1799
1800 /* We must free this pattern if it is no more used. */
1801 list->do_free = 1;
1802 }
1803 else {
1804 /* If the pattern used already exists, it is already linked
1805 * with ref and we must not free it.
1806 */
1807 list->do_free = 0;
1808 }
1809
1810 /* The new list element reference the pattern_expr. */
1811 list->expr = expr;
1812
1813 /* Link the list element with the pattern_head. */
1814 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001815 return expr;
1816}
1817
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001818/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1819 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001820 *
1821 * The file contains one key + value per line. Lines which start with '#' are
1822 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
1823 * then the first "word" (series of non-space/tabs characters), and the value is
1824 * what follows this series of space/tab till the end of the line excluding
1825 * trailing spaces/tabs.
1826 *
1827 * Example :
1828 *
1829 * # this is a comment and is ignored
1830 * 62.212.114.60 1wt.eu \n
1831 * <-><-----------><---><----><---->
1832 * | | | | `--- trailing spaces ignored
1833 * | | | `-------- value
1834 * | | `--------------- middle spaces ignored
1835 * | `------------------------ key
1836 * `-------------------------------- leading spaces ignored
1837 *
1838 * Return non-zero in case of succes, otherwise 0.
1839 */
1840int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
1841{
1842 FILE *file;
1843 char *c;
1844 int ret = 0;
1845 int line = 0;
1846 char *key_beg;
1847 char *key_end;
1848 char *value_beg;
1849 char *value_end;
1850
1851 file = fopen(filename, "r");
1852 if (!file) {
1853 memprintf(err, "failed to open pattern file <%s>", filename);
1854 return 0;
1855 }
1856
1857 /* now parse all patterns. The file may contain only one pattern
1858 * followed by one value per line. The start spaces, separator spaces
1859 * and and spaces are stripped. Each can contain comment started by '#'
1860 */
1861 while (fgets(trash.str, trash.size, file) != NULL) {
1862 line++;
1863 c = trash.str;
1864
1865 /* ignore lines beginning with a dash */
1866 if (*c == '#')
1867 continue;
1868
1869 /* strip leading spaces and tabs */
1870 while (*c == ' ' || *c == '\t')
1871 c++;
1872
1873 /* empty lines are ignored too */
1874 if (*c == '\0' || *c == '\r' || *c == '\n')
1875 continue;
1876
1877 /* look for the end of the key */
1878 key_beg = c;
1879 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
1880 c++;
1881
1882 key_end = c;
1883
1884 /* strip middle spaces and tabs */
1885 while (*c == ' ' || *c == '\t')
1886 c++;
1887
1888 /* look for the end of the value, it is the end of the line */
1889 value_beg = c;
1890 while (*c && *c != '\n' && *c != '\r')
1891 c++;
1892 value_end = c;
1893
1894 /* trim possibly trailing spaces and tabs */
1895 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
1896 value_end--;
1897
1898 /* set final \0 and check entries */
1899 *key_end = '\0';
1900 *value_end = '\0';
1901
1902 /* insert values */
1903 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
1904 memprintf(err, "out of memory");
1905 goto out_close;
1906 }
1907 }
1908
1909 /* succes */
1910 ret = 1;
1911
1912 out_close:
1913 fclose(file);
1914 return ret;
1915}
1916
1917/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1918 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001919 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001920int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001921{
1922 FILE *file;
1923 char *c;
1924 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001925 int ret = 0;
1926 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001927
1928 file = fopen(filename, "r");
1929 if (!file) {
1930 memprintf(err, "failed to open pattern file <%s>", filename);
1931 return 0;
1932 }
1933
1934 /* now parse all patterns. The file may contain only one pattern per
1935 * line. If the line contains spaces, they will be part of the pattern.
1936 * The pattern stops at the first CR, LF or EOF encountered.
1937 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001938 while (fgets(trash.str, trash.size, file) != NULL) {
1939 line++;
1940 c = trash.str;
1941
1942 /* ignore lines beginning with a dash */
1943 if (*c == '#')
1944 continue;
1945
1946 /* strip leading spaces and tabs */
1947 while (*c == ' ' || *c == '\t')
1948 c++;
1949
1950
1951 arg = c;
1952 while (*c && *c != '\n' && *c != '\r')
1953 c++;
1954 *c = 0;
1955
1956 /* empty lines are ignored too */
1957 if (c == arg)
1958 continue;
1959
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001960 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001961 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1962 goto out_close;
1963 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001964 }
1965
1966 ret = 1; /* success */
1967
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001968 out_close:
1969 fclose(file);
1970 return ret;
1971}
1972
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001973int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001974 const char *filename, int patflags, int load_smp,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001975 char **err, const char *display)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001976{
1977 struct pat_ref *ref;
1978 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001979 struct pat_ref_elt *elt;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001980
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001981 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001982 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001983
1984 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001985 if (!ref) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001986 ref = pat_ref_new(filename, display, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001987 if (!ref) {
1988 memprintf(err, "out of memory");
1989 return 0;
1990 }
1991
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001992 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01001993 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001994 if (!pat_ref_read_from_file_smp(ref, filename, err))
1995 return 0;
1996 }
1997 else {
1998 if (!pat_ref_read_from_file(ref, filename, err))
1999 return 0;
2000 }
2001 }
2002 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002003 /* The reference already exists, check the map compatibility. */
2004
2005 /* If the load require samples and the flag PAT_REF_SMP is not set,
2006 * the reference doesn't contain sample, and cannot be used.
2007 */
2008 if (load_smp) {
2009 if (!(ref->flags & PAT_REF_SMP)) {
2010 memprintf(err, "The file \"%s\" is already used as one column file "
2011 "and cannot be used by as two column file.",
2012 filename);
2013 return 0;
2014 }
2015 }
2016 else {
2017 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2018 * set, the reference contains a sample, and cannot be used.
2019 */
2020 if (ref->flags & PAT_REF_SMP) {
2021 memprintf(err, "The file \"%s\" is already used as two column file "
2022 "and cannot be used by as one column file.",
2023 filename);
2024 return 0;
2025 }
2026 }
2027
2028 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002029 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002030 }
2031
2032 /* Now, we can loading patterns from the reference. */
2033
2034 /* Lookup for existing reference in the head. If the reference
2035 * doesn't exists, create it.
2036 */
2037 expr = pattern_lookup_expr(head, ref);
2038 if (!expr) {
2039 expr = pattern_new_expr(head, ref, err);
2040 if (!expr)
2041 return 0;
2042 }
2043
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002044 /* Load reference content in the pattern expression. */
2045 list_for_each_entry(elt, &ref->head, list) {
2046 if (!pat_ref_push(elt, expr, patflags, err)) {
2047 if (elt->line > 0)
2048 memprintf(err, "%s at line %d of file '%s'",
2049 *err, elt->line, filename);
2050 return 0;
2051 }
2052 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002053
2054 return 1;
2055}
2056
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002057/* This function executes a pattern match on a sample. It applies pattern <expr>
2058 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2059 * non-null if the sample match. If <fill> is true and the sample match, the
2060 * function returns the matched pattern. In many cases, this pattern can be a
2061 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002062 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002063struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002064{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002065 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002066 struct pattern *pat;
2067
2068 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002069 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002070 static_pattern.smp = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002071 static_pattern.ref = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002072 static_pattern.flags = 0;
2073 static_pattern.type = SMP_T_UINT;
2074 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002075 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002076 return &static_pattern;
2077 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002078
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002079 /* convert input to string */
2080 if (!sample_convert(smp, head->expect_type))
2081 return NULL;
2082
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002083 list_for_each_entry(list, &head->head, list) {
2084 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002085 if (pat)
2086 return pat;
2087 }
2088 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002089}
2090
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002091/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002092void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002093{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002094 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002095
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002096 list_for_each_entry_safe(list, safe, &head->head, list) {
2097 LIST_DEL(&list->list);
2098 if (list->do_free) {
2099 LIST_DEL(&list->expr->list);
2100 head->prune(list->expr);
2101 free(list->expr);
2102 }
2103 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002104 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002105}
2106
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002107/* This function lookup for a pattern matching the <key> and return a
2108 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2109 * the function returns NULL. If the key cannot be parsed, the function
2110 * fill <err>.
2111 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002112struct sample_storage **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002113{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002114 struct ebmb_node *node;
2115 struct pattern_tree *elt;
2116 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002117
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002118 for (node = ebmb_first(&expr->pattern_tree);
2119 node;
2120 node = ebmb_next(node)) {
2121 elt = container_of(node, struct pattern_tree, node);
2122 if (elt->ref == ref)
2123 return &elt->smp;
2124 }
2125
2126 for (node = ebmb_first(&expr->pattern_tree_2);
2127 node;
2128 node = ebmb_next(node)) {
2129 elt = container_of(node, struct pattern_tree, node);
2130 if (elt->ref == ref)
2131 return &elt->smp;
2132 }
2133
2134 list_for_each_entry(pat, &expr->patterns, list)
2135 if (pat->pat.ref == ref)
2136 return &pat->pat.smp;
2137
2138 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002139}
2140
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002141/* This function search all the pattern matching the <key> and delete it.
2142 * If the parsing of the input key fails, the function returns 0 and the
2143 * <err> is filled, else return 1;
2144 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002145int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002146{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002147 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002148 return 1;
2149}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002150
2151/* This function finalize the configuration parsing. Its set all the
2152 * automatic ids
2153 */
2154void pattern_finalize_config(void)
2155{
2156 int i = 0;
2157 struct pat_ref *ref, *ref2, *ref3;
2158 struct list pr = LIST_HEAD_INIT(pr);
2159
2160 list_for_each_entry(ref, &pattern_reference, list) {
2161 if (ref->unique_id == -1) {
2162 /* Look for the first free id. */
2163 while (1) {
2164 list_for_each_entry(ref2, &pattern_reference, list) {
2165 if (ref2->unique_id == i) {
2166 i++;
2167 break;
2168 }
2169 }
2170 if (&ref2->list == &pattern_reference);
2171 break;
2172 }
2173
2174 /* Uses the unique id and increment it for the next entry. */
2175 ref->unique_id = i;
2176 i++;
2177 }
2178 }
2179
2180 /* This sort the reference list by id. */
2181 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2182 LIST_DEL(&ref->list);
2183 list_for_each_entry(ref3, &pr, list) {
2184 if (ref->unique_id < ref3->unique_id) {
2185 LIST_ADDQ(&ref3->list, &ref->list);
2186 break;
2187 }
2188 }
2189 if (&ref3->list == &pr)
2190 LIST_ADDQ(&pr, &ref->list);
2191 }
2192
2193 /* swap root */
2194 LIST_ADD(&pr, &pattern_reference);
2195 LIST_DEL(&pr);
2196}