blob: 4a3b05ad466dd36f3d234a9d7626cf8c6f23a2c8 [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 FOURNIERedc15c32013-12-13 15:36:59 +0100234 pattern->ptr.reg = (struct my_regex *)trash->str;
235 pattern->ptr.reg->regstr = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100236
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100237 return 1;
238}
239
240/* Parse a range of positive integers delimited by either ':' or '-'. If only
241 * one integer is read, it is set as both min and max. An operator may be
242 * specified as the prefix, among this list of 5 :
243 *
244 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
245 *
246 * The default operator is "eq". It supports range matching. Ranges are
247 * rejected for other operators. The operator may be changed at any time.
248 * The operator is stored in the 'opaque' argument.
249 *
250 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100251 * the caller will have to free it. The function returns zero on error, and
252 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100253 *
254 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100255int pat_parse_int(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100257 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100258
259 pattern->type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100260
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100261 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100262 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100263 goto not_valid_range;
264
265 /* Search ':' or '-' separator. */
266 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
267 ptr++;
268
269 /* If separator not found. */
270 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100271 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
272 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100273 return 0;
274 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100275 pattern->val.range.max = pattern->val.range.min;
276 pattern->val.range.min_set = 1;
277 pattern->val.range.max_set = 1;
278 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 }
280
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100282 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
284 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100285
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286 pattern->val.range.min_set = 0;
287 pattern->val.range.max_set = 1;
288 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100289 }
290
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100291 /* If separator is the last character. */
292 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100293 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100294 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100295
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296 pattern->val.range.min_set = 1;
297 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100298 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100299 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300
301 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100302 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100303 goto not_valid_range;
304
305 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
306 goto not_valid_range;
307
308 if (pattern->val.range.min > pattern->val.range.max)
309 goto not_valid_range;
310
311 pattern->val.range.min_set = 1;
312 pattern->val.range.max_set = 1;
313 return 1;
314
315 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100316 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100317 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100318}
319
320/* Parse a range of positive 2-component versions delimited by either ':' or
321 * '-'. The version consists in a major and a minor, both of which must be
322 * smaller than 65536, because internally they will be represented as a 32-bit
323 * integer.
324 * If only one version is read, it is set as both min and max. Just like for
325 * pure integers, an operator may be specified as the prefix, among this list
326 * of 5 :
327 *
328 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
329 *
330 * The default operator is "eq". It supports range matching. Ranges are
331 * rejected for other operators. The operator may be changed at any time.
332 * The operator is stored in the 'opaque' argument. This allows constructs
333 * such as the following one :
334 *
335 * acl obsolete_ssl ssl_req_proto lt 3
336 * acl unsupported_ssl ssl_req_proto gt 3.1
337 * acl valid_ssl ssl_req_proto 3.0-3.1
338 *
339 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100340int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100341{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100342 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100343
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100344 pattern->type = SMP_T_UINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100345
346 /* Search ':' or '-' separator. */
347 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
348 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100349
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100350 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100351 if (*ptr == '\0' && ptr > text) {
352 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
353 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100354 return 0;
355 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100356 pattern->val.range.max = pattern->val.range.min;
357 pattern->val.range.min_set = 1;
358 pattern->val.range.max_set = 1;
359 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100360 }
361
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100363 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100365 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100367 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100368 pattern->val.range.min_set = 0;
369 pattern->val.range.max_set = 1;
370 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100371 }
372
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100373 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100374 if (ptr == &text[strlen(text)-1]) {
375 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
376 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100377 return 0;
378 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379 pattern->val.range.min_set = 1;
380 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100381 return 1;
382 }
383
384 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100385 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
386 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100387 return 0;
388 }
389 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100390 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100391 return 0;
392 }
393 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100394 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100395 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100396 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100397 pattern->val.range.min_set = 1;
398 pattern->val.range.max_set = 1;
399 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100400}
401
402/* Parse an IP address and an optional mask in the form addr[/mask].
403 * The addr may either be an IPv4 address or a hostname. The mask
404 * may either be a dotted mask or a number of bits. Returns 1 if OK,
405 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
406 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100407int pat_parse_ip(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100408{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100409 if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100410 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411 return 1;
412 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100413 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100414 pattern->type = SMP_T_IPV6;
415 return 1;
416 }
417 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100418 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100419 return 0;
420 }
421}
422
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100423/*
424 *
425 * These functions are exported and may be used by any other component.
426 *
427 * This fucntion just take a sample <smp> and check if this sample match
428 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
429 * PAT_NOMATCH.
430 *
431 */
432
433/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100434struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100435{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100436 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100437}
438
439
440/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100441struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100442{
443 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100444 struct ebmb_node *node;
445 char prev;
446 struct pattern_tree *elt;
447 struct pattern_list *lst;
448 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100449
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100450 /* Lookup a string in the expression's pattern tree. */
451 if (!eb_is_empty(&expr->pattern_tree)) {
452 /* we may have to force a trailing zero on the test pattern */
453 prev = smp->data.str.str[smp->data.str.len];
454 if (prev)
455 smp->data.str.str[smp->data.str.len] = '\0';
456 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
457 if (prev)
458 smp->data.str.str[smp->data.str.len] = prev;
459
460 if (node) {
461 if (fill) {
462 elt = ebmb_entry(node, struct pattern_tree, node);
463 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100464 static_pattern.ref = elt->ref;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100465 static_pattern.flags = PAT_F_TREE;
466 static_pattern.type = SMP_T_STR;
467 static_pattern.ptr.str = (char *)elt->node.key;
468 }
469 return &static_pattern;
470 }
471 }
472
473 /* look in the list */
474 list_for_each_entry(lst, &expr->patterns, list) {
475 pattern = &lst->pat;
476
477 if (pattern->len != smp->data.str.len)
478 continue;
479
480 icase = pattern->flags & PAT_F_IGNORE_CASE;
481 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
482 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
483 return pattern;
484 }
485
486 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100487}
488
489/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100490struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100491{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100492 struct pattern_list *lst;
493 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100494
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100495 /* Look in the list. */
496 list_for_each_entry(lst, &expr->patterns, list) {
497 pattern = &lst->pat;
498
499 if (pattern->len != smp->data.str.len)
500 continue;
501
502 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
503 return pattern;
504 }
505
506 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100507}
508
509/* Executes a regex. It temporarily changes the data to add a trailing zero,
510 * and restores the previous character when leaving.
511 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100512struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100513{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100514 struct pattern_list *lst;
515 struct pattern *pattern;
516
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100517 /* look in the list */
518 list_for_each_entry(lst, &expr->patterns, list) {
519 pattern = &lst->pat;
520
521 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
522 return pattern;
523 }
524 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100525}
526
527/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100528struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100529{
530 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100531 struct pattern_list *lst;
532 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100533
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100534 list_for_each_entry(lst, &expr->patterns, list) {
535 pattern = &lst->pat;
536
537 if (pattern->len > smp->data.str.len)
538 continue;
539
540 icase = pattern->flags & PAT_F_IGNORE_CASE;
541 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
542 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
543 continue;
544
545 return pattern;
546 }
547 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100548}
549
550/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100551struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100552{
553 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100554 struct pattern_list *lst;
555 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100556
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100557 list_for_each_entry(lst, &expr->patterns, list) {
558 pattern = &lst->pat;
559
560 if (pattern->len > smp->data.str.len)
561 continue;
562
563 icase = pattern->flags & PAT_F_IGNORE_CASE;
564 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
565 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
566 continue;
567
568 return pattern;
569 }
570 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100571}
572
573/* Checks that the pattern is included inside the tested string.
574 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
575 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100576struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100577{
578 int icase;
579 char *end;
580 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100581 struct pattern_list *lst;
582 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100583
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100584 list_for_each_entry(lst, &expr->patterns, list) {
585 pattern = &lst->pat;
586
587 if (pattern->len > smp->data.str.len)
588 continue;
589
590 end = smp->data.str.str + smp->data.str.len - pattern->len;
591 icase = pattern->flags & PAT_F_IGNORE_CASE;
592 if (icase) {
593 for (c = smp->data.str.str; c <= end; c++) {
594 if (tolower(*c) != tolower(*pattern->ptr.str))
595 continue;
596 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
597 return pattern;
598 }
599 } else {
600 for (c = smp->data.str.str; c <= end; c++) {
601 if (*c != *pattern->ptr.str)
602 continue;
603 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
604 return pattern;
605 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100606 }
607 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100608 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100609}
610
611/* This one is used by other real functions. It checks that the pattern is
612 * included inside the tested string, but enclosed between the specified
613 * delimiters or at the beginning or end of the string. The delimiters are
614 * provided as an unsigned int made by make_4delim() and match up to 4 different
615 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
616 */
617static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
618{
619 int may_match, icase;
620 char *c, *end;
621 char *ps;
622 int pl;
623
624 pl = pattern->len;
625 ps = pattern->ptr.str;
626
627 while (pl > 0 && is_delimiter(*ps, delimiters)) {
628 pl--;
629 ps++;
630 }
631
632 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
633 pl--;
634
635 if (pl > smp->data.str.len)
636 return PAT_NOMATCH;
637
638 may_match = 1;
639 icase = pattern->flags & PAT_F_IGNORE_CASE;
640 end = smp->data.str.str + smp->data.str.len - pl;
641 for (c = smp->data.str.str; c <= end; c++) {
642 if (is_delimiter(*c, delimiters)) {
643 may_match = 1;
644 continue;
645 }
646
647 if (!may_match)
648 continue;
649
650 if (icase) {
651 if ((tolower(*c) == tolower(*ps)) &&
652 (strncasecmp(ps, c, pl) == 0) &&
653 (c == end || is_delimiter(c[pl], delimiters)))
654 return PAT_MATCH;
655 } else {
656 if ((*c == *ps) &&
657 (strncmp(ps, c, pl) == 0) &&
658 (c == end || is_delimiter(c[pl], delimiters)))
659 return PAT_MATCH;
660 }
661 may_match = 0;
662 }
663 return PAT_NOMATCH;
664}
665
666/* Checks that the pattern is included inside the tested string, but enclosed
667 * between the delimiters '?' or '/' or at the beginning or end of the string.
668 * Delimiters at the beginning or end of the pattern are ignored.
669 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100670struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100671{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100672 struct pattern_list *lst;
673 struct pattern *pattern;
674
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100675 list_for_each_entry(lst, &expr->patterns, list) {
676 pattern = &lst->pat;
677 if (match_word(smp, pattern, make_4delim('/', '?', '?', '?')))
678 return pattern;
679 }
680 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100681}
682
683/* Checks that the pattern is included inside the tested string, but enclosed
684 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
685 * the string. Delimiters at the beginning or end of the pattern are ignored.
686 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100687struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100688{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100689 struct pattern_list *lst;
690 struct pattern *pattern;
691
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100692 list_for_each_entry(lst, &expr->patterns, list) {
693 pattern = &lst->pat;
694 if (match_word(smp, pattern, make_4delim('/', '?', '.', ':')))
695 return pattern;
696 }
697 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100698}
699
700/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100701struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100702{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100703 struct pattern_list *lst;
704 struct pattern *pattern;
705
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100706 list_for_each_entry(lst, &expr->patterns, list) {
707 pattern = &lst->pat;
708 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
709 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
710 return pattern;
711 }
712 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100713}
714
715/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100716struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100717{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100718 struct pattern_list *lst;
719 struct pattern *pattern;
720
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100721 list_for_each_entry(lst, &expr->patterns, list) {
722 pattern = &lst->pat;
723 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
724 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
725 return pattern;
726 }
727 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100728}
729
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100730struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100731{
732 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100733 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100734 struct in_addr *s;
735 struct ebmb_node *node;
736 struct pattern_tree *elt;
737 struct pattern_list *lst;
738 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100739
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100740 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100741 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100742 /* Lookup an IPv4 address in the expression's pattern tree using
743 * the longest match method.
744 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100745 s = &smp->data.ipv4;
746 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
747 if (node) {
748 if (fill) {
749 elt = ebmb_entry(node, struct pattern_tree, node);
750 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100751 static_pattern.ref = elt->ref;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100752 static_pattern.flags = PAT_F_TREE;
753 static_pattern.type = SMP_T_IPV4;
754 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
755 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
756 return NULL;
757 }
758 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100759 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100760
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100761 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
762 * sample address to IPv6 with the mapping method using the ::ffff:
763 * prefix, and try to lookup in the IPv6 tree.
764 */
765 memset(&tmp6, 0, 10);
766 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
767 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
768 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
769 if (node) {
770 if (fill) {
771 elt = ebmb_entry(node, struct pattern_tree, node);
772 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100773 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100774 static_pattern.flags = PAT_F_TREE;
775 static_pattern.type = SMP_T_IPV6;
776 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
777 static_pattern.val.ipv6.mask = elt->node.node.pfx;
778 }
779 return &static_pattern;
780 }
781 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100782
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100783 /* The input sample is IPv6. Try to match in the trees. */
784 if (smp->type == SMP_T_IPV6) {
785 /* Lookup an IPv6 address in the expression's pattern tree using
786 * the longest match method.
787 */
788 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
789 if (node) {
790 if (fill) {
791 elt = ebmb_entry(node, struct pattern_tree, node);
792 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100793 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100794 static_pattern.flags = PAT_F_TREE;
795 static_pattern.type = SMP_T_IPV6;
796 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
797 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100798 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100799 return &static_pattern;
800 }
801
802 /* Try to convert 6 to 4 when the start of the ipv6 address match the
803 * following forms :
804 * - ::ffff:ip:v4 (ipv4 mapped)
805 * - ::0000:ip:v4 (old ipv4 mapped)
806 * - 2002:ip:v4:: (6to4)
807 */
808 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
809 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
810 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
811 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
812 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
813 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
814 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
815 else
816 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
817 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
818
819 /* Lookup an IPv4 address in the expression's pattern tree using the longest
820 * match method.
821 */
822 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
823 if (node) {
824 if (fill) {
825 elt = ebmb_entry(node, struct pattern_tree, node);
826 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100827 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100828 static_pattern.flags = PAT_F_TREE;
829 static_pattern.type = SMP_T_IPV4;
830 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
831 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
832 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100833 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100834 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100835 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100836 }
837 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100838
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100839 /* Lookup in the list. the list contain only IPv4 patterns */
840 list_for_each_entry(lst, &expr->patterns, list) {
841 pattern = &lst->pat;
842
843 /* The input sample is IPv4, use it as is. */
844 if (smp->type == SMP_T_IPV4) {
845 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100846 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100847 else if (smp->type == SMP_T_IPV6) {
848 /* v4 match on a V6 sample. We want to check at least for
849 * the following forms :
850 * - ::ffff:ip:v4 (ipv4 mapped)
851 * - ::0000:ip:v4 (old ipv4 mapped)
852 * - 2002:ip:v4:: (6to4)
853 */
854 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
855 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
856 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
857 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
858 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100859 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100860 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
861 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
862 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100863 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100864 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100865 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100866 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100867
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100868 /* Check if the input sample match the current pattern. */
869 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100870 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100871 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100872 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100873}
874
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100875void free_pattern_tree(struct eb_root *root)
876{
877 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100878 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100879
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100880 node = eb_first(root);
881 while (node) {
882 next = eb_next(node);
883 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100884 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100885 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100886 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100887 node = next;
888 }
889}
890
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100891void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100892{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100893 struct pattern_list *pat, *tmp;
894
895 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
896 free(pat->pat.smp);
897 free(pat);
898 }
899
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100900 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100901 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100902 LIST_INIT(&expr->patterns);
903}
904
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100905void pat_prune_ptr(struct pattern_expr *expr)
906{
907 struct pattern_list *pat, *tmp;
908
909 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
910 free(pat->pat.ptr.ptr);
911 free(pat->pat.smp);
912 free(pat);
913 }
914
915 free_pattern_tree(&expr->pattern_tree);
916 free_pattern_tree(&expr->pattern_tree_2);
917 LIST_INIT(&expr->patterns);
918}
919
920void pat_prune_reg(struct pattern_expr *expr)
921{
922 struct pattern_list *pat, *tmp;
923
924 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
925 regex_free(pat->pat.ptr.ptr);
926 free(pat->pat.smp);
927 free(pat);
928 }
929
930 free_pattern_tree(&expr->pattern_tree);
931 free_pattern_tree(&expr->pattern_tree_2);
932 LIST_INIT(&expr->patterns);
933}
934
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100935/*
936 *
937 * The following functions are used for the pattern indexation
938 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100939 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100940
941int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100942{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100943 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100944
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100945 /* allocate pattern */
946 patl = calloc(1, sizeof(*patl));
947 if (!patl) {
948 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100949 return 0;
950 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100951
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100952 /* duplicate pattern */
953 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100954
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100955 /* chain pattern in the expression */
956 LIST_ADDQ(&expr->patterns, &patl->list);
957
958 /* that's ok */
959 return 1;
960}
961
962int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
963{
964 struct pattern_list *patl;
965
966 /* allocate pattern */
967 patl = calloc(1, sizeof(*patl));
968 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100969 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100970
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100971 /* duplicate pattern */
972 memcpy(&patl->pat, pat, sizeof(*pat));
973 patl->pat.ptr.ptr = malloc(patl->pat.len);
974 if (!patl->pat.ptr.ptr) {
975 free(patl);
976 memprintf(err, "out of memory while indexing pattern");
977 return 0;
978 }
979 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100980
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100981 /* chain pattern in the expression */
982 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100983
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100984 /* that's ok */
985 return 1;
986}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100987
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100988int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
989{
990 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100991
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100992 /* allocate pattern */
993 patl = calloc(1, sizeof(*patl));
994 if (!patl) {
995 memprintf(err, "out of memory while indexing pattern");
996 return 0;
997 }
998
999 /* duplicate pattern */
1000 memcpy(&patl->pat, pat, sizeof(*pat));
1001 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1002 if (!patl->pat.ptr.str) {
1003 free(patl);
1004 memprintf(err, "out of memory while indexing pattern");
1005 return 0;
1006 }
1007 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1008 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001009
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001010 /* chain pattern in the expression */
1011 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001012
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001013 /* that's ok */
1014 return 1;
1015}
1016
1017int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1018{
1019 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001020
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001021 /* allocate pattern */
1022 patl = calloc(1, sizeof(*patl));
1023 if (!patl) {
1024 memprintf(err, "out of memory while indexing pattern");
1025 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001026 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001027
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001028 /* duplicate pattern */
1029 memcpy(&patl->pat, pat, sizeof(*pat));
1030
1031 /* allocate regex */
1032 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1033 if (!patl->pat.ptr.reg) {
1034 free(patl);
1035 memprintf(err, "out of memory while indexing pattern");
1036 return 0;
1037 }
1038
1039 /* compile regex */
1040 if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
1041 free(patl);
1042 free(patl->pat.ptr.reg);
1043 return 0;
1044 }
1045
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001046 /* chain pattern in the expression */
1047 LIST_ADDQ(&expr->patterns, &patl->list);
1048
1049 /* that's ok */
1050 return 1;
1051}
1052
1053int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1054{
1055 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001056 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001057
1058 /* Only IPv4 can be indexed */
1059 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001060 /* in IPv4 case, check if the mask is contiguous so that we can
1061 * insert the network into the tree. A continuous mask has only
1062 * ones on the left. This means that this mask + its lower bit
1063 * added once again is null.
1064 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001065 mask = ntohl(pat->val.ipv4.mask.s_addr);
1066 if (mask + (mask & -mask) == 0) {
1067 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001068
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001069 /* node memory allocation */
1070 node = calloc(1, sizeof(*node) + 4);
1071 if (!node) {
1072 memprintf(err, "out of memory while loading pattern");
1073 return 0;
1074 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001075
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001076 /* copy the pointer to sample associated to this node */
1077 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001078 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001079
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001080 /* FIXME: insert <addr>/<mask> into the tree here */
1081 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1082 node->node.node.pfx = mask;
1083 if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node)
1084 free(node); /* was a duplicate */
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;
1109 if (ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16) != &node->node)
1110 free(node); /* was a duplicate */
1111
1112 /* that's ok */
1113 return 1;
1114 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001115
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001116 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001117}
1118
1119int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1120{
1121 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001122 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001123
1124 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001125 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001126 memprintf(err, "internal error: string expected, but the type is '%s'",
1127 smp_to_type[pat->type]);
1128 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001129 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001130
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001131 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1132 if (pat->flags & PAT_F_IGNORE_CASE)
1133 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001134
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001135 /* Process the key len */
1136 len = strlen(pat->ptr.str) + 1;
1137
1138 /* node memory allocation */
1139 node = calloc(1, sizeof(*node) + len);
1140 if (!node) {
1141 memprintf(err, "out of memory while loading pattern");
1142 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001143 }
1144
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001145 /* copy the pointer to sample associated to this node */
1146 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001147 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001148
1149 /* copy the string */
1150 memcpy(node->node.key, pat->ptr.str, len);
1151
1152 /* index the new node */
1153 if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node)
1154 free(node); /* was a duplicate */
1155
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);
1282 expr->pattern_tree = EB_ROOT_UNIQUE;
1283 expr->pattern_tree_2 = EB_ROOT_UNIQUE;
1284}
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,
1400 const char *value)
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 FOURNIERe369ca22014-01-29 16:24:55 +01001405 int ret = 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001406
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001407 /* Modify pattern from reference. */
1408 sample = strdup(value);
1409 if (!sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001410 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001411 free(elt->sample);
1412 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001413
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001414 /* Load sample in each reference. */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001415 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001416 if (!expr->pat_head->parse_smp)
1417 continue;
1418
1419 smp = pattern_find_smp(expr, elt);
1420 if (smp && *smp) {
1421 if (!expr->pat_head->parse_smp(sample, *smp)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001422 *smp = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001423 ret = 0;
1424 }
1425 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001426 }
1427
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001428 return ret;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001429}
1430
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001431/* This function modify the sample of the first pattern that match the <key>. */
1432int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value)
1433{
1434 struct pat_ref_elt *elt;
1435
1436 /* Look for pattern in the reference. */
1437 list_for_each_entry(elt, &ref->head, list) {
1438 if (elt == refelt) {
1439 pat_ref_set_elt(ref, elt, value);
1440 return 1;
1441 }
1442 }
1443 return 0;
1444}
1445
1446/* This function modify the sample of the first pattern that match the <key>. */
1447int pat_ref_set(struct pat_ref *ref, const char *key, const char *value)
1448{
1449 struct pat_ref_elt *elt;
1450 int ret = 0;
1451
1452 /* Look for pattern in the reference. */
1453 list_for_each_entry(elt, &ref->head, list) {
1454 if (strcmp(key, elt->pattern) == 0) {
1455 pat_ref_set_elt(ref, elt, value);
1456 ret = 1;
1457 }
1458 }
1459 return ret;
1460}
1461
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001462/* This function create new reference. <ref> is the reference name.
1463 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1464 * be unique. The user must check the reference with "pat_ref_lookup()"
1465 * before calling this function. If the fucntion fail, it return NULL,
1466 * else return new struct pat_ref.
1467 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001468struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001469{
1470 struct pat_ref *ref;
1471
1472 ref = malloc(sizeof(*ref));
1473 if (!ref)
1474 return NULL;
1475
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001476 if (display) {
1477 ref->display = strdup(display);
1478 if (!ref->display) {
1479 free(ref);
1480 return NULL;
1481 }
1482 }
1483 else
1484 ref->display = NULL;
1485
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001486 ref->reference = strdup(reference);
1487 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001488 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001489 free(ref);
1490 return NULL;
1491 }
1492
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001493 ref->flags = flags;
1494 ref->unique_id = -1;
1495
1496 LIST_INIT(&ref->head);
1497 LIST_INIT(&ref->pat);
1498
1499 LIST_ADDQ(&pattern_reference, &ref->list);
1500
1501 return ref;
1502}
1503
1504/* This function create new reference. <unique_id> is the unique id. If
1505 * the value of <unique_id> is -1, the unique id is calculated later.
1506 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1507 * be unique. The user must check the reference with "pat_ref_lookup()"
1508 * or pat_ref_lookupid before calling this function. If the function
1509 * fail, it return NULL, else return new struct pat_ref.
1510 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001511struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001512{
1513 struct pat_ref *ref;
1514
1515 ref = malloc(sizeof(*ref));
1516 if (!ref)
1517 return NULL;
1518
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001519 if (display) {
1520 ref->display = strdup(display);
1521 if (!ref->display) {
1522 free(ref);
1523 return NULL;
1524 }
1525 }
1526 else
1527 ref->display = NULL;
1528
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001529 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001530 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001531 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001532 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 adds entry to <ref>. It can failed with memory error.
1541 * If the function fails, it returns 0.
1542 */
1543int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1544{
1545 struct pat_ref_elt *elt;
1546
1547 elt = malloc(sizeof(*elt));
1548 if (!elt)
1549 return 0;
1550
1551 elt->line = line;
1552
1553 elt->pattern = strdup(pattern);
1554 if (!elt->pattern) {
1555 free(elt);
1556 return 0;
1557 }
1558
1559 if (sample) {
1560 elt->sample = strdup(sample);
1561 if (!elt->sample) {
1562 free(elt->pattern);
1563 free(elt);
1564 return 0;
1565 }
1566 }
1567 else
1568 elt->sample = NULL;
1569
1570 LIST_ADDQ(&ref->head, &elt->list);
1571
1572 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001573}
1574
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001575/* This function create sample found in <elt>, parse the pattern also
1576 * found in <elt> and insert it in <expr>. The function copy <patflags>
1577 * in <expr>. If the function fails, it returns0 and <err> is filled.
1578 * In succes case, the function returns 1.
1579 */
1580static inline
1581int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1582 int patflags, char **err)
1583{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001584 struct sample_storage *smp;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001585 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001586
1587 /* Create sample */
1588 if (elt->sample && expr->pat_head->parse_smp) {
1589 /* New sample. */
1590 smp = malloc(sizeof(*smp));
1591 if (!smp)
1592 return 0;
1593
1594 /* Parse value. */
1595 if (!expr->pat_head->parse_smp(elt->sample, smp)) {
1596 memprintf(err, "unable to parse '%s'", elt->sample);
1597 free(smp);
1598 return 0;
1599 }
1600
1601 }
1602 else
1603 smp = NULL;
1604
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001605 /* initialise pattern */
1606 memset(&pattern, 0, sizeof(pattern));
1607 pattern.flags = patflags;
1608 pattern.smp = smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001609 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001610
1611 /* parse pattern */
1612 if (!expr->pat_head->parse(elt->pattern, &pattern, err)) {
1613 free(smp);
1614 return 0;
1615 }
1616
1617 /* index pattern */
1618 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001619 free(smp);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001620 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001621 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001622
1623 return 1;
1624}
1625
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001626/* This function adds entry to <ref>. It can failed with memory error.
1627 * The new entry is added at all the pattern_expr registered in this
1628 * reference. The function stop on the first error encountered. It
1629 * returns 0 and err is filled.
1630 *
1631 * If an error is encountered, The complete add operation is cancelled.
1632 */
1633int pat_ref_add(struct pat_ref *ref,
1634 const char *pattern, const char *sample,
1635 char **err)
1636{
1637 struct pat_ref_elt *elt;
1638 struct pattern_expr *expr;
1639
1640 elt = malloc(sizeof(*elt));
1641 if (!elt) {
1642 memprintf(err, "out of memory error");
1643 return 0;
1644 }
1645
1646 elt->line = -1;
1647
1648 elt->pattern = strdup(pattern);
1649 if (!elt->pattern) {
1650 free(elt);
1651 memprintf(err, "out of memory error");
1652 return 0;
1653 }
1654
1655 if (sample) {
1656 elt->sample = strdup(sample);
1657 if (!elt->sample) {
1658 free(elt->pattern);
1659 free(elt);
1660 memprintf(err, "out of memory error");
1661 return 0;
1662 }
1663 }
1664 else
1665 elt->sample = NULL;
1666
1667 LIST_ADDQ(&ref->head, &elt->list);
1668
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001669 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001670 if (!pat_ref_push(elt, expr, 0, err)) {
1671 /* Try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001672 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001673 return 0;
1674 }
1675 }
1676
1677 return 1;
1678}
1679
1680/* This function prune all entries of <ref>. This function
1681 * prune the associated pattern_expr.
1682 */
1683void pat_ref_prune(struct pat_ref *ref)
1684{
1685 struct pat_ref_elt *elt, *safe;
1686 struct pattern_expr *expr;
1687
1688 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1689 LIST_DEL(&elt->list);
1690 free(elt->pattern);
1691 free(elt->sample);
1692 free(elt);
1693 }
1694
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001695 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001696 expr->pat_head->prune(expr);
1697}
1698
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001699/* This function lookup for existing reference <ref> in pattern_head <head>. */
1700struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1701{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001702 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001703
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001704 list_for_each_entry(expr, &head->head, list)
1705 if (expr->expr->ref == ref)
1706 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001707 return NULL;
1708}
1709
1710/* This function create new pattern_expr associated to the reference <ref>.
1711 * <ref> can be NULL. If an error is occured, the function returns NULL and
1712 * <err> is filled. Otherwise, the function returns new pattern_expr linked
1713 * with <head> and <ref>.
1714 */
1715struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err)
1716{
1717 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001718 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001719
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001720 /* Memory and initialization of the chain element. */
1721 list = malloc(sizeof(*list));
1722 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001723 memprintf(err, "out of memory");
1724 return NULL;
1725 }
1726
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001727 /* Look for existing similar expr. No that only the index, parse and
1728 * parse_smp function must be identical for having similar pattern.
1729 * The other function depends of theses first.
1730 */
1731 if (ref) {
1732 list_for_each_entry(expr, &ref->pat, list)
1733 if (expr->pat_head->index == head->index &&
1734 expr->pat_head->parse == head->parse &&
1735 expr->pat_head->parse_smp == head->parse_smp)
1736 break;
1737 if (&expr->list == &ref->pat)
1738 expr = NULL;
1739 }
1740 else
1741 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001742
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001743 /* If no similar expr was found, we create new expr. */
1744 if (!expr) {
1745 /* Get a lot of memory for the expr struct. */
1746 expr = malloc(sizeof(*expr));
1747 if (!expr) {
1748 memprintf(err, "out of memory");
1749 return NULL;
1750 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001751
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001752 /* Initialize this new expr. */
1753 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001754
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001755 /* This new pattern expression reference one of his heads. */
1756 expr->pat_head = head;
1757
1758 /* Link with ref, or to self to facilitate LIST_DEL() */
1759 if (ref)
1760 LIST_ADDQ(&ref->pat, &expr->list);
1761 else
1762 LIST_INIT(&expr->list);
1763
1764 expr->ref = ref;
1765
1766 /* We must free this pattern if it is no more used. */
1767 list->do_free = 1;
1768 }
1769 else {
1770 /* If the pattern used already exists, it is already linked
1771 * with ref and we must not free it.
1772 */
1773 list->do_free = 0;
1774 }
1775
1776 /* The new list element reference the pattern_expr. */
1777 list->expr = expr;
1778
1779 /* Link the list element with the pattern_head. */
1780 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001781 return expr;
1782}
1783
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001784/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1785 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001786 *
1787 * The file contains one key + value per line. Lines which start with '#' are
1788 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
1789 * then the first "word" (series of non-space/tabs characters), and the value is
1790 * what follows this series of space/tab till the end of the line excluding
1791 * trailing spaces/tabs.
1792 *
1793 * Example :
1794 *
1795 * # this is a comment and is ignored
1796 * 62.212.114.60 1wt.eu \n
1797 * <-><-----------><---><----><---->
1798 * | | | | `--- trailing spaces ignored
1799 * | | | `-------- value
1800 * | | `--------------- middle spaces ignored
1801 * | `------------------------ key
1802 * `-------------------------------- leading spaces ignored
1803 *
1804 * Return non-zero in case of succes, otherwise 0.
1805 */
1806int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
1807{
1808 FILE *file;
1809 char *c;
1810 int ret = 0;
1811 int line = 0;
1812 char *key_beg;
1813 char *key_end;
1814 char *value_beg;
1815 char *value_end;
1816
1817 file = fopen(filename, "r");
1818 if (!file) {
1819 memprintf(err, "failed to open pattern file <%s>", filename);
1820 return 0;
1821 }
1822
1823 /* now parse all patterns. The file may contain only one pattern
1824 * followed by one value per line. The start spaces, separator spaces
1825 * and and spaces are stripped. Each can contain comment started by '#'
1826 */
1827 while (fgets(trash.str, trash.size, file) != NULL) {
1828 line++;
1829 c = trash.str;
1830
1831 /* ignore lines beginning with a dash */
1832 if (*c == '#')
1833 continue;
1834
1835 /* strip leading spaces and tabs */
1836 while (*c == ' ' || *c == '\t')
1837 c++;
1838
1839 /* empty lines are ignored too */
1840 if (*c == '\0' || *c == '\r' || *c == '\n')
1841 continue;
1842
1843 /* look for the end of the key */
1844 key_beg = c;
1845 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
1846 c++;
1847
1848 key_end = c;
1849
1850 /* strip middle spaces and tabs */
1851 while (*c == ' ' || *c == '\t')
1852 c++;
1853
1854 /* look for the end of the value, it is the end of the line */
1855 value_beg = c;
1856 while (*c && *c != '\n' && *c != '\r')
1857 c++;
1858 value_end = c;
1859
1860 /* trim possibly trailing spaces and tabs */
1861 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
1862 value_end--;
1863
1864 /* set final \0 and check entries */
1865 *key_end = '\0';
1866 *value_end = '\0';
1867
1868 /* insert values */
1869 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
1870 memprintf(err, "out of memory");
1871 goto out_close;
1872 }
1873 }
1874
1875 /* succes */
1876 ret = 1;
1877
1878 out_close:
1879 fclose(file);
1880 return ret;
1881}
1882
1883/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1884 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001885 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001886int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001887{
1888 FILE *file;
1889 char *c;
1890 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001891 int ret = 0;
1892 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001893
1894 file = fopen(filename, "r");
1895 if (!file) {
1896 memprintf(err, "failed to open pattern file <%s>", filename);
1897 return 0;
1898 }
1899
1900 /* now parse all patterns. The file may contain only one pattern per
1901 * line. If the line contains spaces, they will be part of the pattern.
1902 * The pattern stops at the first CR, LF or EOF encountered.
1903 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001904 while (fgets(trash.str, trash.size, file) != NULL) {
1905 line++;
1906 c = trash.str;
1907
1908 /* ignore lines beginning with a dash */
1909 if (*c == '#')
1910 continue;
1911
1912 /* strip leading spaces and tabs */
1913 while (*c == ' ' || *c == '\t')
1914 c++;
1915
1916
1917 arg = c;
1918 while (*c && *c != '\n' && *c != '\r')
1919 c++;
1920 *c = 0;
1921
1922 /* empty lines are ignored too */
1923 if (c == arg)
1924 continue;
1925
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001926 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001927 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1928 goto out_close;
1929 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001930 }
1931
1932 ret = 1; /* success */
1933
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001934 out_close:
1935 fclose(file);
1936 return ret;
1937}
1938
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001939int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001940 const char *filename, int patflags, int load_smp,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001941 char **err, const char *display)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001942{
1943 struct pat_ref *ref;
1944 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001945 struct pat_ref_elt *elt;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001946
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001947 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001948 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001949
1950 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001951 if (!ref) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001952 ref = pat_ref_new(filename, display, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001953 if (!ref) {
1954 memprintf(err, "out of memory");
1955 return 0;
1956 }
1957
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001958 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01001959 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001960 if (!pat_ref_read_from_file_smp(ref, filename, err))
1961 return 0;
1962 }
1963 else {
1964 if (!pat_ref_read_from_file(ref, filename, err))
1965 return 0;
1966 }
1967 }
1968 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01001969 /* The reference already exists, check the map compatibility. */
1970
1971 /* If the load require samples and the flag PAT_REF_SMP is not set,
1972 * the reference doesn't contain sample, and cannot be used.
1973 */
1974 if (load_smp) {
1975 if (!(ref->flags & PAT_REF_SMP)) {
1976 memprintf(err, "The file \"%s\" is already used as one column file "
1977 "and cannot be used by as two column file.",
1978 filename);
1979 return 0;
1980 }
1981 }
1982 else {
1983 /* The load doesn't require samples. If the flag PAT_REF_SMP is
1984 * set, the reference contains a sample, and cannot be used.
1985 */
1986 if (ref->flags & PAT_REF_SMP) {
1987 memprintf(err, "The file \"%s\" is already used as two column file "
1988 "and cannot be used by as one column file.",
1989 filename);
1990 return 0;
1991 }
1992 }
1993
1994 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001995 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001996 }
1997
1998 /* Now, we can loading patterns from the reference. */
1999
2000 /* Lookup for existing reference in the head. If the reference
2001 * doesn't exists, create it.
2002 */
2003 expr = pattern_lookup_expr(head, ref);
2004 if (!expr) {
2005 expr = pattern_new_expr(head, ref, err);
2006 if (!expr)
2007 return 0;
2008 }
2009
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002010 /* Load reference content in the pattern expression. */
2011 list_for_each_entry(elt, &ref->head, list) {
2012 if (!pat_ref_push(elt, expr, patflags, err)) {
2013 if (elt->line > 0)
2014 memprintf(err, "%s at line %d of file '%s'",
2015 *err, elt->line, filename);
2016 return 0;
2017 }
2018 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002019
2020 return 1;
2021}
2022
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002023/* This function executes a pattern match on a sample. It applies pattern <expr>
2024 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2025 * non-null if the sample match. If <fill> is true and the sample match, the
2026 * function returns the matched pattern. In many cases, this pattern can be a
2027 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002028 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002029struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002030{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002031 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002032 struct pattern *pat;
2033
2034 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002035 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002036 static_pattern.smp = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002037 static_pattern.ref = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002038 static_pattern.flags = 0;
2039 static_pattern.type = SMP_T_UINT;
2040 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002041 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002042 return &static_pattern;
2043 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002044
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002045 /* convert input to string */
2046 if (!sample_convert(smp, head->expect_type))
2047 return NULL;
2048
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002049 list_for_each_entry(list, &head->head, list) {
2050 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002051 if (pat)
2052 return pat;
2053 }
2054 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002055}
2056
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002057/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002058void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002059{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002060 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002061
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002062 list_for_each_entry_safe(list, safe, &head->head, list) {
2063 LIST_DEL(&list->list);
2064 if (list->do_free) {
2065 LIST_DEL(&list->expr->list);
2066 head->prune(list->expr);
2067 free(list->expr);
2068 }
2069 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002070 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002071}
2072
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002073/* This function lookup for a pattern matching the <key> and return a
2074 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2075 * the function returns NULL. If the key cannot be parsed, the function
2076 * fill <err>.
2077 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002078struct sample_storage **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002079{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002080 struct ebmb_node *node;
2081 struct pattern_tree *elt;
2082 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002083
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002084 for (node = ebmb_first(&expr->pattern_tree);
2085 node;
2086 node = ebmb_next(node)) {
2087 elt = container_of(node, struct pattern_tree, node);
2088 if (elt->ref == ref)
2089 return &elt->smp;
2090 }
2091
2092 for (node = ebmb_first(&expr->pattern_tree_2);
2093 node;
2094 node = ebmb_next(node)) {
2095 elt = container_of(node, struct pattern_tree, node);
2096 if (elt->ref == ref)
2097 return &elt->smp;
2098 }
2099
2100 list_for_each_entry(pat, &expr->patterns, list)
2101 if (pat->pat.ref == ref)
2102 return &pat->pat.smp;
2103
2104 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002105}
2106
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002107/* This function search all the pattern matching the <key> and delete it.
2108 * If the parsing of the input key fails, the function returns 0 and the
2109 * <err> is filled, else return 1;
2110 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002111int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002112{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002113 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002114 return 1;
2115}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002116
2117/* This function finalize the configuration parsing. Its set all the
2118 * automatic ids
2119 */
2120void pattern_finalize_config(void)
2121{
2122 int i = 0;
2123 struct pat_ref *ref, *ref2, *ref3;
2124 struct list pr = LIST_HEAD_INIT(pr);
2125
2126 list_for_each_entry(ref, &pattern_reference, list) {
2127 if (ref->unique_id == -1) {
2128 /* Look for the first free id. */
2129 while (1) {
2130 list_for_each_entry(ref2, &pattern_reference, list) {
2131 if (ref2->unique_id == i) {
2132 i++;
2133 break;
2134 }
2135 }
2136 if (&ref2->list == &pattern_reference);
2137 break;
2138 }
2139
2140 /* Uses the unique id and increment it for the next entry. */
2141 ref->unique_id = i;
2142 i++;
2143 }
2144 }
2145
2146 /* This sort the reference list by id. */
2147 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2148 LIST_DEL(&ref->list);
2149 list_for_each_entry(ref3, &pr, list) {
2150 if (ref->unique_id < ref3->unique_id) {
2151 LIST_ADDQ(&ref3->list, &ref->list);
2152 break;
2153 }
2154 }
2155 if (&ref3->list == &pr)
2156 LIST_ADDQ(&pr, &ref->list);
2157 }
2158
2159 /* swap root */
2160 LIST_ADD(&pr, &pattern_reference);
2161 LIST_DEL(&pr);
2162}