blob: 3238fbb43fb86ed4bd21218025869c3fdee3c2f5 [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
Thierry FOURNIER46006bd2014-03-21 21:45:15 +010022#include <proto/log.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010023#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010024#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010025
26#include <ebsttree.h>
27
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010028char *pat_match_names[PAT_MATCH_NUM] = {
29 [PAT_MATCH_FOUND] = "found",
30 [PAT_MATCH_BOOL] = "bool",
31 [PAT_MATCH_INT] = "int",
32 [PAT_MATCH_IP] = "ip",
33 [PAT_MATCH_BIN] = "bin",
34 [PAT_MATCH_LEN] = "len",
35 [PAT_MATCH_STR] = "str",
36 [PAT_MATCH_BEG] = "beg",
37 [PAT_MATCH_SUB] = "sub",
38 [PAT_MATCH_DIR] = "dir",
39 [PAT_MATCH_DOM] = "dom",
40 [PAT_MATCH_END] = "end",
41 [PAT_MATCH_REG] = "reg",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010042};
43
Thierry FOURNIERedc15c32013-12-13 15:36:59 +010044int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010045 [PAT_MATCH_FOUND] = pat_parse_nothing,
46 [PAT_MATCH_BOOL] = pat_parse_nothing,
47 [PAT_MATCH_INT] = pat_parse_int,
48 [PAT_MATCH_IP] = pat_parse_ip,
49 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010050 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010051 [PAT_MATCH_STR] = pat_parse_str,
52 [PAT_MATCH_BEG] = pat_parse_str,
53 [PAT_MATCH_SUB] = pat_parse_str,
54 [PAT_MATCH_DIR] = pat_parse_str,
55 [PAT_MATCH_DOM] = pat_parse_str,
56 [PAT_MATCH_END] = pat_parse_str,
57 [PAT_MATCH_REG] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010058};
59
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010060int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
61 [PAT_MATCH_FOUND] = pat_idx_list_val,
62 [PAT_MATCH_BOOL] = pat_idx_list_val,
63 [PAT_MATCH_INT] = pat_idx_list_val,
64 [PAT_MATCH_IP] = pat_idx_tree_ip,
65 [PAT_MATCH_BIN] = pat_idx_list_ptr,
66 [PAT_MATCH_LEN] = pat_idx_list_val,
67 [PAT_MATCH_STR] = pat_idx_tree_str,
68 [PAT_MATCH_BEG] = pat_idx_list_str,
69 [PAT_MATCH_SUB] = pat_idx_list_str,
70 [PAT_MATCH_DIR] = pat_idx_list_str,
71 [PAT_MATCH_DOM] = pat_idx_list_str,
72 [PAT_MATCH_END] = pat_idx_list_str,
73 [PAT_MATCH_REG] = pat_idx_list_reg,
74};
75
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010076void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
Thierry FOURNIERb1136502014-01-15 11:38:49 +010077 [PAT_MATCH_FOUND] = pat_del_list_val,
78 [PAT_MATCH_BOOL] = pat_del_list_val,
79 [PAT_MATCH_INT] = pat_del_list_val,
80 [PAT_MATCH_IP] = pat_del_tree_ip,
81 [PAT_MATCH_BIN] = pat_del_list_ptr,
82 [PAT_MATCH_LEN] = pat_del_list_val,
83 [PAT_MATCH_STR] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010084 [PAT_MATCH_BEG] = pat_del_list_ptr,
85 [PAT_MATCH_SUB] = pat_del_list_ptr,
86 [PAT_MATCH_DIR] = pat_del_list_ptr,
87 [PAT_MATCH_DOM] = pat_del_list_ptr,
88 [PAT_MATCH_END] = pat_del_list_ptr,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010089 [PAT_MATCH_REG] = pat_del_list_reg,
90};
91
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010092void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
93 [PAT_MATCH_FOUND] = pat_prune_val,
94 [PAT_MATCH_BOOL] = pat_prune_val,
95 [PAT_MATCH_INT] = pat_prune_val,
96 [PAT_MATCH_IP] = pat_prune_val,
97 [PAT_MATCH_BIN] = pat_prune_ptr,
98 [PAT_MATCH_LEN] = pat_prune_val,
99 [PAT_MATCH_STR] = pat_prune_ptr,
100 [PAT_MATCH_BEG] = pat_prune_ptr,
101 [PAT_MATCH_SUB] = pat_prune_ptr,
102 [PAT_MATCH_DIR] = pat_prune_ptr,
103 [PAT_MATCH_DOM] = pat_prune_ptr,
104 [PAT_MATCH_END] = pat_prune_ptr,
105 [PAT_MATCH_REG] = pat_prune_reg,
106};
107
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100108struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100109 [PAT_MATCH_FOUND] = NULL,
110 [PAT_MATCH_BOOL] = pat_match_nothing,
111 [PAT_MATCH_INT] = pat_match_int,
112 [PAT_MATCH_IP] = pat_match_ip,
113 [PAT_MATCH_BIN] = pat_match_bin,
114 [PAT_MATCH_LEN] = pat_match_len,
115 [PAT_MATCH_STR] = pat_match_str,
116 [PAT_MATCH_BEG] = pat_match_beg,
117 [PAT_MATCH_SUB] = pat_match_sub,
118 [PAT_MATCH_DIR] = pat_match_dir,
119 [PAT_MATCH_DOM] = pat_match_dom,
120 [PAT_MATCH_END] = pat_match_end,
121 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100122};
123
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100124/* Just used for checking configuration compatibility */
125int pat_match_types[PAT_MATCH_NUM] = {
126 [PAT_MATCH_FOUND] = SMP_T_UINT,
127 [PAT_MATCH_BOOL] = SMP_T_UINT,
128 [PAT_MATCH_INT] = SMP_T_UINT,
129 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100130 [PAT_MATCH_BIN] = SMP_T_BIN,
131 [PAT_MATCH_LEN] = SMP_T_STR,
132 [PAT_MATCH_STR] = SMP_T_STR,
133 [PAT_MATCH_BEG] = SMP_T_STR,
134 [PAT_MATCH_SUB] = SMP_T_STR,
135 [PAT_MATCH_DIR] = SMP_T_STR,
136 [PAT_MATCH_DOM] = SMP_T_STR,
137 [PAT_MATCH_END] = SMP_T_STR,
138 [PAT_MATCH_REG] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100139};
140
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100141/* this struct is used to return information */
142static struct pattern static_pattern;
143
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100144/* This is the root of the list of all pattern_ref avalaibles. */
145struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
146
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100147/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100148 *
149 * The following functions are not exported and are used by internals process
150 * of pattern matching
151 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100152 */
153
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100154/* Background: Fast way to find a zero byte in a word
155 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
156 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
157 *
158 * To look for 4 different byte values, xor the word with those bytes and
159 * then check for zero bytes:
160 *
161 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
162 * where <delimiter> is the 4 byte values to look for (as an uint)
163 * and <c> is the character that is being tested
164 */
165static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
166{
167 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
168 return (mask - 0x01010101) & ~mask & 0x80808080U;
169}
170
171static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
172{
173 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
174}
175
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100176
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100177/*
178 *
179 * These functions are exported and may be used by any other component.
180 *
181 * The following functions are used for parsing pattern matching
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100182 * input value. The <text> contain the string to be parsed. <pattern>
183 * must be a preallocated pattern. The pat_parse_* functions fill this
184 * structure with the parsed value. <usage> can be PAT_U_COMPILE or
185 * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
186 * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
187 * use "trash" or return pointers to the input strings. In both cases,
188 * the caller must use the value PAT_U_LOOKUP with caution. <err> is
189 * filled with an error message built with memprintf() function.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100190 *
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100191 * In succes case, the pat_parse_* function return 1. If the function
192 * fail, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100193 *
194 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100195
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100196/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100197int pat_parse_nothing(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100198{
199 return 1;
200}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100201
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100202/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100203int pat_parse_str(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100205 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100206 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100207 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100208 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100209}
210
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100211/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100212int pat_parse_bin(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100213{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100214 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100215
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100216 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100217 trash = get_trash_chunk();
218 pattern->len = trash->size;
219 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100220 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100221}
222
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100224int pat_parse_reg(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225{
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100226 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100227
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100228 trash = get_trash_chunk();
229 if (trash->size < sizeof(*pattern->ptr.reg)) {
230 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
231 (int)sizeof(*pattern->ptr.reg), trash->size);
232 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233 }
234
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100235 pattern->ptr.str = (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 FOURNIERb7729c92014-02-11 16:24:41 +0100409 if (str2net(text, !(pattern->flags & PAT_F_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100410 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100412 return 1;
413 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100414 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100415 pattern->type = SMP_T_IPV6;
416 return 1;
417 }
418 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100419 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100420 return 0;
421 }
422}
423
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100424/*
425 *
426 * These functions are exported and may be used by any other component.
427 *
428 * This fucntion just take a sample <smp> and check if this sample match
429 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
430 * PAT_NOMATCH.
431 *
432 */
433
434/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100435struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436{
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100437 if (smp->data.uint) {
438 if (fill) {
439 static_pattern.smp = NULL;
440 static_pattern.ref = NULL;
441 static_pattern.flags = 0;
442 static_pattern.type = 0;
443 static_pattern.ptr.str = NULL;
444 }
445 return &static_pattern;
446 }
447 else
448 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100449}
450
451
452/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100453struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100454{
455 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100456 struct ebmb_node *node;
457 char prev;
458 struct pattern_tree *elt;
459 struct pattern_list *lst;
460 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100461
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100462 /* Lookup a string in the expression's pattern tree. */
463 if (!eb_is_empty(&expr->pattern_tree)) {
464 /* we may have to force a trailing zero on the test pattern */
465 prev = smp->data.str.str[smp->data.str.len];
466 if (prev)
467 smp->data.str.str[smp->data.str.len] = '\0';
468 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
469 if (prev)
470 smp->data.str.str[smp->data.str.len] = prev;
471
472 if (node) {
473 if (fill) {
474 elt = ebmb_entry(node, struct pattern_tree, node);
475 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100476 static_pattern.ref = elt->ref;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100477 static_pattern.flags = PAT_F_TREE;
478 static_pattern.type = SMP_T_STR;
479 static_pattern.ptr.str = (char *)elt->node.key;
480 }
481 return &static_pattern;
482 }
483 }
484
485 /* look in the list */
486 list_for_each_entry(lst, &expr->patterns, list) {
487 pattern = &lst->pat;
488
489 if (pattern->len != smp->data.str.len)
490 continue;
491
492 icase = pattern->flags & PAT_F_IGNORE_CASE;
493 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
494 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
495 return pattern;
496 }
497
498 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100499}
500
501/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100502struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100503{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100504 struct pattern_list *lst;
505 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100506
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100507 /* Look in the list. */
508 list_for_each_entry(lst, &expr->patterns, list) {
509 pattern = &lst->pat;
510
511 if (pattern->len != smp->data.str.len)
512 continue;
513
514 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
515 return pattern;
516 }
517
518 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100519}
520
521/* Executes a regex. It temporarily changes the data to add a trailing zero,
522 * and restores the previous character when leaving.
523 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100524struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100525{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100526 struct pattern_list *lst;
527 struct pattern *pattern;
528
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100529 /* look in the list */
530 list_for_each_entry(lst, &expr->patterns, list) {
531 pattern = &lst->pat;
532
533 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
534 return pattern;
535 }
536 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100537}
538
539/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100540struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100541{
542 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100543 struct pattern_list *lst;
544 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100545
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100546 list_for_each_entry(lst, &expr->patterns, list) {
547 pattern = &lst->pat;
548
549 if (pattern->len > smp->data.str.len)
550 continue;
551
552 icase = pattern->flags & PAT_F_IGNORE_CASE;
553 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
554 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
555 continue;
556
557 return pattern;
558 }
559 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100560}
561
562/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100563struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100564{
565 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100566 struct pattern_list *lst;
567 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100568
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100569 list_for_each_entry(lst, &expr->patterns, list) {
570 pattern = &lst->pat;
571
572 if (pattern->len > smp->data.str.len)
573 continue;
574
575 icase = pattern->flags & PAT_F_IGNORE_CASE;
576 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
577 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
578 continue;
579
580 return pattern;
581 }
582 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100583}
584
585/* Checks that the pattern is included inside the tested string.
586 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
587 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100588struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100589{
590 int icase;
591 char *end;
592 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100593 struct pattern_list *lst;
594 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100595
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100596 list_for_each_entry(lst, &expr->patterns, list) {
597 pattern = &lst->pat;
598
599 if (pattern->len > smp->data.str.len)
600 continue;
601
602 end = smp->data.str.str + smp->data.str.len - pattern->len;
603 icase = pattern->flags & PAT_F_IGNORE_CASE;
604 if (icase) {
605 for (c = smp->data.str.str; c <= end; c++) {
606 if (tolower(*c) != tolower(*pattern->ptr.str))
607 continue;
608 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
609 return pattern;
610 }
611 } else {
612 for (c = smp->data.str.str; c <= end; c++) {
613 if (*c != *pattern->ptr.str)
614 continue;
615 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
616 return pattern;
617 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100618 }
619 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100620 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100621}
622
623/* This one is used by other real functions. It checks that the pattern is
624 * included inside the tested string, but enclosed between the specified
625 * delimiters or at the beginning or end of the string. The delimiters are
626 * provided as an unsigned int made by make_4delim() and match up to 4 different
627 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
628 */
629static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
630{
631 int may_match, icase;
632 char *c, *end;
633 char *ps;
634 int pl;
635
636 pl = pattern->len;
637 ps = pattern->ptr.str;
638
639 while (pl > 0 && is_delimiter(*ps, delimiters)) {
640 pl--;
641 ps++;
642 }
643
644 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
645 pl--;
646
647 if (pl > smp->data.str.len)
648 return PAT_NOMATCH;
649
650 may_match = 1;
651 icase = pattern->flags & PAT_F_IGNORE_CASE;
652 end = smp->data.str.str + smp->data.str.len - pl;
653 for (c = smp->data.str.str; c <= end; c++) {
654 if (is_delimiter(*c, delimiters)) {
655 may_match = 1;
656 continue;
657 }
658
659 if (!may_match)
660 continue;
661
662 if (icase) {
663 if ((tolower(*c) == tolower(*ps)) &&
664 (strncasecmp(ps, c, pl) == 0) &&
665 (c == end || is_delimiter(c[pl], delimiters)))
666 return PAT_MATCH;
667 } else {
668 if ((*c == *ps) &&
669 (strncmp(ps, c, pl) == 0) &&
670 (c == end || is_delimiter(c[pl], delimiters)))
671 return PAT_MATCH;
672 }
673 may_match = 0;
674 }
675 return PAT_NOMATCH;
676}
677
678/* Checks that the pattern is included inside the tested string, but enclosed
679 * between the delimiters '?' or '/' or at the beginning or end of the string.
680 * Delimiters at the beginning or end of the pattern are ignored.
681 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100682struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100683{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100684 struct pattern_list *lst;
685 struct pattern *pattern;
686
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100687 list_for_each_entry(lst, &expr->patterns, list) {
688 pattern = &lst->pat;
689 if (match_word(smp, pattern, make_4delim('/', '?', '?', '?')))
690 return pattern;
691 }
692 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100693}
694
695/* Checks that the pattern is included inside the tested string, but enclosed
696 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
697 * the string. Delimiters at the beginning or end of the pattern are ignored.
698 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100699struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100700{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100701 struct pattern_list *lst;
702 struct pattern *pattern;
703
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100704 list_for_each_entry(lst, &expr->patterns, list) {
705 pattern = &lst->pat;
706 if (match_word(smp, pattern, make_4delim('/', '?', '.', ':')))
707 return pattern;
708 }
709 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100710}
711
712/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100713struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100714{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100715 struct pattern_list *lst;
716 struct pattern *pattern;
717
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100718 list_for_each_entry(lst, &expr->patterns, list) {
719 pattern = &lst->pat;
720 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
721 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
722 return pattern;
723 }
724 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100725}
726
727/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100728struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100729{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100730 struct pattern_list *lst;
731 struct pattern *pattern;
732
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100733 list_for_each_entry(lst, &expr->patterns, list) {
734 pattern = &lst->pat;
735 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
736 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
737 return pattern;
738 }
739 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100740}
741
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100742struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100743{
744 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100745 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100746 struct in_addr *s;
747 struct ebmb_node *node;
748 struct pattern_tree *elt;
749 struct pattern_list *lst;
750 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100751
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100752 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100753 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100754 /* Lookup an IPv4 address in the expression's pattern tree using
755 * the longest match method.
756 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100757 s = &smp->data.ipv4;
758 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
759 if (node) {
760 if (fill) {
761 elt = ebmb_entry(node, struct pattern_tree, node);
762 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100763 static_pattern.ref = elt->ref;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100764 static_pattern.flags = PAT_F_TREE;
765 static_pattern.type = SMP_T_IPV4;
766 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
767 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
768 return NULL;
769 }
770 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100771 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100772
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100773 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
774 * sample address to IPv6 with the mapping method using the ::ffff:
775 * prefix, and try to lookup in the IPv6 tree.
776 */
777 memset(&tmp6, 0, 10);
778 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
779 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
780 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
781 if (node) {
782 if (fill) {
783 elt = ebmb_entry(node, struct pattern_tree, node);
784 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100785 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100786 static_pattern.flags = PAT_F_TREE;
787 static_pattern.type = SMP_T_IPV6;
788 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
789 static_pattern.val.ipv6.mask = elt->node.node.pfx;
790 }
791 return &static_pattern;
792 }
793 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100794
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100795 /* The input sample is IPv6. Try to match in the trees. */
796 if (smp->type == SMP_T_IPV6) {
797 /* Lookup an IPv6 address in the expression's pattern tree using
798 * the longest match method.
799 */
800 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
801 if (node) {
802 if (fill) {
803 elt = ebmb_entry(node, struct pattern_tree, node);
804 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100805 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100806 static_pattern.flags = PAT_F_TREE;
807 static_pattern.type = SMP_T_IPV6;
808 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
809 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100810 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100811 return &static_pattern;
812 }
813
814 /* Try to convert 6 to 4 when the start of the ipv6 address match the
815 * following forms :
816 * - ::ffff:ip:v4 (ipv4 mapped)
817 * - ::0000:ip:v4 (old ipv4 mapped)
818 * - 2002:ip:v4:: (6to4)
819 */
820 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
821 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
822 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
823 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
824 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
825 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
826 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
827 else
828 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
829 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
830
831 /* Lookup an IPv4 address in the expression's pattern tree using the longest
832 * match method.
833 */
834 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
835 if (node) {
836 if (fill) {
837 elt = ebmb_entry(node, struct pattern_tree, node);
838 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100839 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100840 static_pattern.flags = PAT_F_TREE;
841 static_pattern.type = SMP_T_IPV4;
842 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
843 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
844 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100845 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100846 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100847 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100848 }
849 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100850
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100851 /* Lookup in the list. the list contain only IPv4 patterns */
852 list_for_each_entry(lst, &expr->patterns, list) {
853 pattern = &lst->pat;
854
855 /* The input sample is IPv4, use it as is. */
856 if (smp->type == SMP_T_IPV4) {
857 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100858 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100859 else if (smp->type == SMP_T_IPV6) {
860 /* v4 match on a V6 sample. We want to check at least for
861 * the following forms :
862 * - ::ffff:ip:v4 (ipv4 mapped)
863 * - ::0000:ip:v4 (old ipv4 mapped)
864 * - 2002:ip:v4:: (6to4)
865 */
866 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
867 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
868 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
869 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
870 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100871 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100872 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
873 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
874 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100875 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100876 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100877 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100878 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100879
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100880 /* Check if the input sample match the current pattern. */
881 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100882 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100883 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100884 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100885}
886
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100887void free_pattern_tree(struct eb_root *root)
888{
889 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100890 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100891
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100892 node = eb_first(root);
893 while (node) {
894 next = eb_next(node);
895 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100896 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100897 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100898 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100899 node = next;
900 }
901}
902
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100903void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100904{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100905 struct pattern_list *pat, *tmp;
906
907 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
908 free(pat->pat.smp);
909 free(pat);
910 }
911
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100912 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100913 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100914 LIST_INIT(&expr->patterns);
915}
916
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100917void pat_prune_ptr(struct pattern_expr *expr)
918{
919 struct pattern_list *pat, *tmp;
920
921 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
922 free(pat->pat.ptr.ptr);
923 free(pat->pat.smp);
924 free(pat);
925 }
926
927 free_pattern_tree(&expr->pattern_tree);
928 free_pattern_tree(&expr->pattern_tree_2);
929 LIST_INIT(&expr->patterns);
930}
931
932void pat_prune_reg(struct pattern_expr *expr)
933{
934 struct pattern_list *pat, *tmp;
935
936 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
937 regex_free(pat->pat.ptr.ptr);
938 free(pat->pat.smp);
939 free(pat);
940 }
941
942 free_pattern_tree(&expr->pattern_tree);
943 free_pattern_tree(&expr->pattern_tree_2);
944 LIST_INIT(&expr->patterns);
945}
946
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100947/*
948 *
949 * The following functions are used for the pattern indexation
950 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100951 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100952
953int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100954{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100955 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100956
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100957 /* allocate pattern */
958 patl = calloc(1, sizeof(*patl));
959 if (!patl) {
960 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100961 return 0;
962 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100963
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100964 /* duplicate pattern */
965 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100966
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100967 /* chain pattern in the expression */
968 LIST_ADDQ(&expr->patterns, &patl->list);
969
970 /* that's ok */
971 return 1;
972}
973
974int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
975{
976 struct pattern_list *patl;
977
978 /* allocate pattern */
979 patl = calloc(1, sizeof(*patl));
980 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100981 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100982
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100983 /* duplicate pattern */
984 memcpy(&patl->pat, pat, sizeof(*pat));
985 patl->pat.ptr.ptr = malloc(patl->pat.len);
986 if (!patl->pat.ptr.ptr) {
987 free(patl);
988 memprintf(err, "out of memory while indexing pattern");
989 return 0;
990 }
991 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100992
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100993 /* chain pattern in the expression */
994 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100995
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100996 /* that's ok */
997 return 1;
998}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100999
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001000int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1001{
1002 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001003
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001004 /* allocate pattern */
1005 patl = calloc(1, sizeof(*patl));
1006 if (!patl) {
1007 memprintf(err, "out of memory while indexing pattern");
1008 return 0;
1009 }
1010
1011 /* duplicate pattern */
1012 memcpy(&patl->pat, pat, sizeof(*pat));
1013 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1014 if (!patl->pat.ptr.str) {
1015 free(patl);
1016 memprintf(err, "out of memory while indexing pattern");
1017 return 0;
1018 }
1019 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1020 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001021
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001022 /* chain pattern in the expression */
1023 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001024
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001025 /* that's ok */
1026 return 1;
1027}
1028
1029int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1030{
1031 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001032
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001033 /* allocate pattern */
1034 patl = calloc(1, sizeof(*patl));
1035 if (!patl) {
1036 memprintf(err, "out of memory while indexing pattern");
1037 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001038 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001039
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001040 /* duplicate pattern */
1041 memcpy(&patl->pat, pat, sizeof(*pat));
1042
1043 /* allocate regex */
1044 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1045 if (!patl->pat.ptr.reg) {
1046 free(patl);
1047 memprintf(err, "out of memory while indexing pattern");
1048 return 0;
1049 }
1050
1051 /* compile regex */
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +01001052 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 +01001053 free(patl);
1054 free(patl->pat.ptr.reg);
1055 return 0;
1056 }
1057
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001058 /* chain pattern in the expression */
1059 LIST_ADDQ(&expr->patterns, &patl->list);
1060
1061 /* that's ok */
1062 return 1;
1063}
1064
1065int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1066{
1067 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001068 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001069
1070 /* Only IPv4 can be indexed */
1071 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001072 /* in IPv4 case, check if the mask is contiguous so that we can
1073 * insert the network into the tree. A continuous mask has only
1074 * ones on the left. This means that this mask + its lower bit
1075 * added once again is null.
1076 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001077 mask = ntohl(pat->val.ipv4.mask.s_addr);
1078 if (mask + (mask & -mask) == 0) {
1079 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001080
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001081 /* node memory allocation */
1082 node = calloc(1, sizeof(*node) + 4);
1083 if (!node) {
1084 memprintf(err, "out of memory while loading pattern");
1085 return 0;
1086 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001087
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001088 /* copy the pointer to sample associated to this node */
1089 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001090 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001091
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001092 /* FIXME: insert <addr>/<mask> into the tree here */
1093 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1094 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001095
1096 /* Insert the entry. */
1097 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001098
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001099 /* that's ok */
1100 return 1;
1101 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001102 else {
1103 /* If the mask is not contiguous, just add the pattern to the list */
1104 return pat_idx_list_val(expr, pat, err);
1105 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001106 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001107 else if (pat->type == SMP_T_IPV6) {
1108 /* IPv6 also can be indexed */
1109 node = calloc(1, sizeof(*node) + 16);
1110 if (!node) {
1111 memprintf(err, "out of memory while loading pattern");
1112 return 0;
1113 }
1114
1115 /* copy the pointer to sample associated to this node */
1116 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001117 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001118
1119 /* FIXME: insert <addr>/<mask> into the tree here */
1120 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1121 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001122
1123 /* Insert the entry. */
1124 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001125
1126 /* that's ok */
1127 return 1;
1128 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001129
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001130 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001131}
1132
1133int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1134{
1135 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001136 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001137
1138 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001139 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001140 memprintf(err, "internal error: string expected, but the type is '%s'",
1141 smp_to_type[pat->type]);
1142 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001143 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001144
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001145 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1146 if (pat->flags & PAT_F_IGNORE_CASE)
1147 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001148
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001149 /* Process the key len */
1150 len = strlen(pat->ptr.str) + 1;
1151
1152 /* node memory allocation */
1153 node = calloc(1, sizeof(*node) + len);
1154 if (!node) {
1155 memprintf(err, "out of memory while loading pattern");
1156 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001157 }
1158
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001159 /* copy the pointer to sample associated to this node */
1160 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001161 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001162
1163 /* copy the string */
1164 memcpy(node->node.key, pat->ptr.str, len);
1165
1166 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001167 ebst_insert(&expr->pattern_tree, &node->node);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001168
1169 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001170 return 1;
1171}
1172
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001173void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001174{
1175 struct pattern_list *pat;
1176 struct pattern_list *safe;
1177
1178 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1179 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001180 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001181 continue;
1182
1183 /* Delete and free entry. */
1184 LIST_DEL(&pat->list);
1185 free(pat->pat.smp);
1186 free(pat);
1187 }
1188}
1189
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001190void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001191{
1192 struct ebmb_node *node, *next_node;
1193 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001194
1195 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001196 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1197 node;
1198 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1199 /* Extract container of the tree node. */
1200 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001201
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001202 /* Check equality. */
1203 if (elt->ref != ref)
1204 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001205
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001206 /* Delete and free entry. */
1207 ebmb_delete(node);
1208 free(elt->smp);
1209 free(elt);
1210 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001211
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001212 /* Browse each node of the list for IPv4 addresses. */
1213 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001214
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001215 /* browse each node of the tree for IPv6 addresses. */
1216 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1217 node;
1218 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1219 /* Extract container of the tree node. */
1220 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001221
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001222 /* Check equality. */
1223 if (elt->ref != ref)
1224 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001225
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001226 /* Delete and free entry. */
1227 ebmb_delete(node);
1228 free(elt->smp);
1229 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001230 }
1231}
1232
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001233void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001234{
1235 struct pattern_list *pat;
1236 struct pattern_list *safe;
1237
1238 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1239 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001240 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001241 continue;
1242
1243 /* Delete and free entry. */
1244 LIST_DEL(&pat->list);
1245 free(pat->pat.ptr.ptr);
1246 free(pat->pat.smp);
1247 free(pat);
1248 }
1249}
1250
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001251void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001252{
1253 struct ebmb_node *node, *next_node;
1254 struct pattern_tree *elt;
1255
1256 /* browse each node of the tree. */
1257 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1258 node;
1259 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1260 /* Extract container of the tree node. */
1261 elt = container_of(node, struct pattern_tree, node);
1262
1263 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001264 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001265 continue;
1266
1267 /* Delete and free entry. */
1268 ebmb_delete(node);
1269 free(elt->smp);
1270 free(elt);
1271 }
1272}
1273
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001274void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001275{
1276 struct pattern_list *pat;
1277 struct pattern_list *safe;
1278
1279 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1280 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001281 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001282 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001283
1284 /* Delete and free entry. */
1285 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001286 regex_free(pat->pat.ptr.ptr);
1287 free(pat->pat.smp);
1288 free(pat);
1289 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001290}
1291
1292void pattern_init_expr(struct pattern_expr *expr)
1293{
1294 LIST_INIT(&expr->patterns);
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001295 expr->pattern_tree = EB_ROOT;
1296 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001297}
1298
1299void pattern_init_head(struct pattern_head *head)
1300{
1301 LIST_INIT(&head->head);
1302}
1303
1304/* The following functions are relative to the management of the reference
1305 * lists. These lists are used to store the original pattern and associated
1306 * value as string form.
1307 *
1308 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001309 *
1310 * The pattern reference are stored with two identifiers: the unique_id and
1311 * the reference.
1312 *
1313 * The reference identify a file. Each file with the same name point to the
1314 * same reference. We can register many times one file. If the file is modified,
1315 * all his dependencies are also modified. The reference can be used with map or
1316 * acl.
1317 *
1318 * The unique_id identify inline acl. The unique id is unique for each acl.
1319 * You cannot force the same id in the configuration file, because this repoort
1320 * an error.
1321 *
1322 * A particular case appears if the filename is a number. In this case, the
1323 * unique_id is set with the number represented by the filename and the
1324 * reference is also set. This method prevent double unique_id.
1325 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001326 */
1327
1328/* This function lookup for reference. If the reference is found, they return
1329 * pointer to the struct pat_ref, else return NULL.
1330 */
1331struct pat_ref *pat_ref_lookup(const char *reference)
1332{
1333 struct pat_ref *ref;
1334
1335 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001336 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001337 return ref;
1338 return NULL;
1339}
1340
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001341/* This function lookup for unique id. If the reference is found, they return
1342 * pointer to the struct pat_ref, else return NULL.
1343 */
1344struct pat_ref *pat_ref_lookupid(int unique_id)
1345{
1346 struct pat_ref *ref;
1347
1348 list_for_each_entry(ref, &pattern_reference, list)
1349 if (ref->unique_id == unique_id)
1350 return ref;
1351 return NULL;
1352}
1353
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001354/* This function remove all pattern matching the pointer <refelt> from
1355 * the the reference and from each expr member of the reference. This
1356 * function returns 1 if the deletion is done and return 0 is the entry
1357 * is not found.
1358 */
1359int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1360{
1361 struct pattern_expr *expr;
1362 struct pat_ref_elt *elt, *safe;
1363
1364 /* delete pattern from reference */
1365 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1366 if (elt == refelt) {
1367 LIST_DEL(&elt->list);
1368 free(elt->sample);
1369 free(elt->pattern);
1370 free(elt);
1371
1372 list_for_each_entry(expr, &ref->pat, list)
1373 pattern_delete(expr, elt);
1374
1375 return 1;
1376 }
1377 }
1378 return 0;
1379}
1380
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001381/* This function remove all pattern match <key> from the the reference
1382 * and from each expr member of the reference. This fucntion returns 1
1383 * if the deletion is done and return 0 is the entry is not found.
1384 */
1385int pat_ref_delete(struct pat_ref *ref, const char *key)
1386{
1387 struct pattern_expr *expr;
1388 struct pat_ref_elt *elt, *safe;
1389 int found = 0;
1390
1391 /* delete pattern from reference */
1392 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1393 if (strcmp(key, elt->pattern) == 0) {
1394 LIST_DEL(&elt->list);
1395 free(elt->sample);
1396 free(elt->pattern);
1397 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001398
1399 list_for_each_entry(expr, &ref->pat, list)
1400 pattern_delete(expr, elt);
1401
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001402 found = 1;
1403 }
1404 }
1405
1406 if (!found)
1407 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001408 return 1;
1409}
1410
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001411 /* This function modify the sample of the first pattern that match the <key>. */
1412static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001413 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001414{
1415 struct pattern_expr *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001416 struct sample_storage **smp;
1417 char *sample;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001418 struct sample_storage test;
1419
1420 /* Try all needed converters. */
1421 list_for_each_entry(expr, &ref->pat, list) {
1422 if (!expr->pat_head->parse_smp)
1423 continue;
1424
1425 if (!expr->pat_head->parse_smp(value, &test)) {
1426 memprintf(err, "unable to parse '%s'", value);
1427 return 0;
1428 }
1429 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001430
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001431 /* Modify pattern from reference. */
1432 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001433 if (!sample) {
1434 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001435 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001436 }
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001437 free(elt->sample);
1438 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001439
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001440 /* Load sample in each reference. All the conversion are tested
1441 * below, normally these calls dosn't fail.
1442 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001443 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001444 if (!expr->pat_head->parse_smp)
1445 continue;
1446
1447 smp = pattern_find_smp(expr, elt);
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001448 if (smp && *smp && !expr->pat_head->parse_smp(sample, *smp))
1449 *smp = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001450 }
1451
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001452 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001453}
1454
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001455/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001456int 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 +01001457{
1458 struct pat_ref_elt *elt;
1459
1460 /* Look for pattern in the reference. */
1461 list_for_each_entry(elt, &ref->head, list) {
1462 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001463 if (!pat_ref_set_elt(ref, elt, value, err))
1464 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001465 return 1;
1466 }
1467 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001468
1469 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001470 return 0;
1471}
1472
1473/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001474int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001475{
1476 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001477 int found = 0;
1478 char *_merr;
1479 char **merr;
1480
1481 if (err) {
1482 merr = &_merr;
1483 *merr = NULL;
1484 }
1485 else
1486 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001487
1488 /* Look for pattern in the reference. */
1489 list_for_each_entry(elt, &ref->head, list) {
1490 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001491 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1492 if (!found)
1493 *err = *merr;
1494 else {
1495 memprintf(err, "%s, %s", *err, *merr);
1496 free(*merr);
1497 *merr = NULL;
1498 }
1499 }
1500 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001501 }
1502 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001503
1504 if (!found) {
1505 memprintf(err, "entry not found");
1506 return 0;
1507 }
1508 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001509}
1510
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001511/* This function create new reference. <ref> is the reference name.
1512 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1513 * be unique. The user must check the reference with "pat_ref_lookup()"
1514 * before calling this function. If the fucntion fail, it return NULL,
1515 * else return new struct pat_ref.
1516 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001517struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001518{
1519 struct pat_ref *ref;
1520
1521 ref = malloc(sizeof(*ref));
1522 if (!ref)
1523 return NULL;
1524
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001525 if (display) {
1526 ref->display = strdup(display);
1527 if (!ref->display) {
1528 free(ref);
1529 return NULL;
1530 }
1531 }
1532 else
1533 ref->display = NULL;
1534
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001535 ref->reference = strdup(reference);
1536 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001537 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001538 free(ref);
1539 return NULL;
1540 }
1541
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001542 ref->flags = flags;
1543 ref->unique_id = -1;
1544
1545 LIST_INIT(&ref->head);
1546 LIST_INIT(&ref->pat);
1547
1548 LIST_ADDQ(&pattern_reference, &ref->list);
1549
1550 return ref;
1551}
1552
1553/* This function create new reference. <unique_id> is the unique id. If
1554 * the value of <unique_id> is -1, the unique id is calculated later.
1555 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1556 * be unique. The user must check the reference with "pat_ref_lookup()"
1557 * or pat_ref_lookupid before calling this function. If the function
1558 * fail, it return NULL, else return new struct pat_ref.
1559 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001560struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001561{
1562 struct pat_ref *ref;
1563
1564 ref = malloc(sizeof(*ref));
1565 if (!ref)
1566 return NULL;
1567
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001568 if (display) {
1569 ref->display = strdup(display);
1570 if (!ref->display) {
1571 free(ref);
1572 return NULL;
1573 }
1574 }
1575 else
1576 ref->display = NULL;
1577
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001578 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001579 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001580 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001581 LIST_INIT(&ref->head);
1582 LIST_INIT(&ref->pat);
1583
1584 LIST_ADDQ(&pattern_reference, &ref->list);
1585
1586 return ref;
1587}
1588
1589/* This function adds entry to <ref>. It can failed with memory error.
1590 * If the function fails, it returns 0.
1591 */
1592int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1593{
1594 struct pat_ref_elt *elt;
1595
1596 elt = malloc(sizeof(*elt));
1597 if (!elt)
1598 return 0;
1599
1600 elt->line = line;
1601
1602 elt->pattern = strdup(pattern);
1603 if (!elt->pattern) {
1604 free(elt);
1605 return 0;
1606 }
1607
1608 if (sample) {
1609 elt->sample = strdup(sample);
1610 if (!elt->sample) {
1611 free(elt->pattern);
1612 free(elt);
1613 return 0;
1614 }
1615 }
1616 else
1617 elt->sample = NULL;
1618
1619 LIST_ADDQ(&ref->head, &elt->list);
1620
1621 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001622}
1623
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001624/* This function create sample found in <elt>, parse the pattern also
1625 * found in <elt> and insert it in <expr>. The function copy <patflags>
1626 * in <expr>. If the function fails, it returns0 and <err> is filled.
1627 * In succes case, the function returns 1.
1628 */
1629static inline
1630int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1631 int patflags, char **err)
1632{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001633 struct sample_storage *smp;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001634 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001635
1636 /* Create sample */
1637 if (elt->sample && expr->pat_head->parse_smp) {
1638 /* New sample. */
1639 smp = malloc(sizeof(*smp));
1640 if (!smp)
1641 return 0;
1642
1643 /* Parse value. */
1644 if (!expr->pat_head->parse_smp(elt->sample, smp)) {
1645 memprintf(err, "unable to parse '%s'", elt->sample);
1646 free(smp);
1647 return 0;
1648 }
1649
1650 }
1651 else
1652 smp = NULL;
1653
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001654 /* initialise pattern */
1655 memset(&pattern, 0, sizeof(pattern));
1656 pattern.flags = patflags;
1657 pattern.smp = smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001658 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001659
1660 /* parse pattern */
1661 if (!expr->pat_head->parse(elt->pattern, &pattern, err)) {
1662 free(smp);
1663 return 0;
1664 }
1665
1666 /* index pattern */
1667 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001668 free(smp);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001669 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001670 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001671
1672 return 1;
1673}
1674
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001675/* This function adds entry to <ref>. It can failed with memory error. The new
1676 * entry is added at all the pattern_expr registered in this reference. The
1677 * function stop on the first error encountered. It returns 0 and err is
1678 * filled. If an error is encountered, the complete add operation is cancelled.
1679 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001680 */
1681int pat_ref_add(struct pat_ref *ref,
1682 const char *pattern, const char *sample,
1683 char **err)
1684{
1685 struct pat_ref_elt *elt;
1686 struct pattern_expr *expr;
1687
1688 elt = malloc(sizeof(*elt));
1689 if (!elt) {
1690 memprintf(err, "out of memory error");
1691 return 0;
1692 }
1693
1694 elt->line = -1;
1695
1696 elt->pattern = strdup(pattern);
1697 if (!elt->pattern) {
1698 free(elt);
1699 memprintf(err, "out of memory error");
1700 return 0;
1701 }
1702
1703 if (sample) {
1704 elt->sample = strdup(sample);
1705 if (!elt->sample) {
1706 free(elt->pattern);
1707 free(elt);
1708 memprintf(err, "out of memory error");
1709 return 0;
1710 }
1711 }
1712 else
1713 elt->sample = NULL;
1714
1715 LIST_ADDQ(&ref->head, &elt->list);
1716
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001717 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001718 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001719 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001720 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001721 return 0;
1722 }
1723 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001724 return 1;
1725}
1726
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001727/* This function prune <ref>, replace all reference by the references
1728 * of <replace>, and reindex all the news values.
1729 *
1730 * The pattern are loaded in best effort and the errors are ignored,
1731 * but writed in the logs.
1732 */
1733void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1734{
1735 struct pattern_expr *expr;
1736 struct pat_ref_elt *elt;
1737 char *err = NULL;
1738
1739 pat_ref_prune(ref);
1740
1741 LIST_ADD(&replace->head, &ref->head);
1742 LIST_DEL(&replace->head);
1743
1744 list_for_each_entry(elt, &ref->head, list) {
1745 list_for_each_entry(expr, &ref->pat, list) {
1746 if (!pat_ref_push(elt, expr, 0, &err)) {
1747 send_log(NULL, LOG_NOTICE, "%s", err);
1748 free(err);
1749 err = NULL;
1750 }
1751 }
1752 }
1753}
1754
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001755/* This function prune all entries of <ref>. This function
1756 * prune the associated pattern_expr.
1757 */
1758void pat_ref_prune(struct pat_ref *ref)
1759{
1760 struct pat_ref_elt *elt, *safe;
1761 struct pattern_expr *expr;
1762
1763 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1764 LIST_DEL(&elt->list);
1765 free(elt->pattern);
1766 free(elt->sample);
1767 free(elt);
1768 }
1769
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001770 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001771 expr->pat_head->prune(expr);
1772}
1773
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001774/* This function lookup for existing reference <ref> in pattern_head <head>. */
1775struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1776{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001777 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001778
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001779 list_for_each_entry(expr, &head->head, list)
1780 if (expr->expr->ref == ref)
1781 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001782 return NULL;
1783}
1784
1785/* This function create new pattern_expr associated to the reference <ref>.
1786 * <ref> can be NULL. If an error is occured, the function returns NULL and
1787 * <err> is filled. Otherwise, the function returns new pattern_expr linked
1788 * with <head> and <ref>.
1789 */
1790struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err)
1791{
1792 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001793 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001794
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001795 /* Memory and initialization of the chain element. */
1796 list = malloc(sizeof(*list));
1797 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001798 memprintf(err, "out of memory");
1799 return NULL;
1800 }
1801
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001802 /* Look for existing similar expr. No that only the index, parse and
1803 * parse_smp function must be identical for having similar pattern.
1804 * The other function depends of theses first.
1805 */
1806 if (ref) {
1807 list_for_each_entry(expr, &ref->pat, list)
1808 if (expr->pat_head->index == head->index &&
1809 expr->pat_head->parse == head->parse &&
1810 expr->pat_head->parse_smp == head->parse_smp)
1811 break;
1812 if (&expr->list == &ref->pat)
1813 expr = NULL;
1814 }
1815 else
1816 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001817
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001818 /* If no similar expr was found, we create new expr. */
1819 if (!expr) {
1820 /* Get a lot of memory for the expr struct. */
1821 expr = malloc(sizeof(*expr));
1822 if (!expr) {
1823 memprintf(err, "out of memory");
1824 return NULL;
1825 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001826
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001827 /* Initialize this new expr. */
1828 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001829
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001830 /* This new pattern expression reference one of his heads. */
1831 expr->pat_head = head;
1832
1833 /* Link with ref, or to self to facilitate LIST_DEL() */
1834 if (ref)
1835 LIST_ADDQ(&ref->pat, &expr->list);
1836 else
1837 LIST_INIT(&expr->list);
1838
1839 expr->ref = ref;
1840
1841 /* We must free this pattern if it is no more used. */
1842 list->do_free = 1;
1843 }
1844 else {
1845 /* If the pattern used already exists, it is already linked
1846 * with ref and we must not free it.
1847 */
1848 list->do_free = 0;
1849 }
1850
1851 /* The new list element reference the pattern_expr. */
1852 list->expr = expr;
1853
1854 /* Link the list element with the pattern_head. */
1855 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001856 return expr;
1857}
1858
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001859/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1860 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001861 *
1862 * The file contains one key + value per line. Lines which start with '#' are
1863 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
1864 * then the first "word" (series of non-space/tabs characters), and the value is
1865 * what follows this series of space/tab till the end of the line excluding
1866 * trailing spaces/tabs.
1867 *
1868 * Example :
1869 *
1870 * # this is a comment and is ignored
1871 * 62.212.114.60 1wt.eu \n
1872 * <-><-----------><---><----><---->
1873 * | | | | `--- trailing spaces ignored
1874 * | | | `-------- value
1875 * | | `--------------- middle spaces ignored
1876 * | `------------------------ key
1877 * `-------------------------------- leading spaces ignored
1878 *
1879 * Return non-zero in case of succes, otherwise 0.
1880 */
1881int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
1882{
1883 FILE *file;
1884 char *c;
1885 int ret = 0;
1886 int line = 0;
1887 char *key_beg;
1888 char *key_end;
1889 char *value_beg;
1890 char *value_end;
1891
1892 file = fopen(filename, "r");
1893 if (!file) {
1894 memprintf(err, "failed to open pattern file <%s>", filename);
1895 return 0;
1896 }
1897
1898 /* now parse all patterns. The file may contain only one pattern
1899 * followed by one value per line. The start spaces, separator spaces
1900 * and and spaces are stripped. Each can contain comment started by '#'
1901 */
1902 while (fgets(trash.str, trash.size, file) != NULL) {
1903 line++;
1904 c = trash.str;
1905
1906 /* ignore lines beginning with a dash */
1907 if (*c == '#')
1908 continue;
1909
1910 /* strip leading spaces and tabs */
1911 while (*c == ' ' || *c == '\t')
1912 c++;
1913
1914 /* empty lines are ignored too */
1915 if (*c == '\0' || *c == '\r' || *c == '\n')
1916 continue;
1917
1918 /* look for the end of the key */
1919 key_beg = c;
1920 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
1921 c++;
1922
1923 key_end = c;
1924
1925 /* strip middle spaces and tabs */
1926 while (*c == ' ' || *c == '\t')
1927 c++;
1928
1929 /* look for the end of the value, it is the end of the line */
1930 value_beg = c;
1931 while (*c && *c != '\n' && *c != '\r')
1932 c++;
1933 value_end = c;
1934
1935 /* trim possibly trailing spaces and tabs */
1936 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
1937 value_end--;
1938
1939 /* set final \0 and check entries */
1940 *key_end = '\0';
1941 *value_end = '\0';
1942
1943 /* insert values */
1944 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
1945 memprintf(err, "out of memory");
1946 goto out_close;
1947 }
1948 }
1949
1950 /* succes */
1951 ret = 1;
1952
1953 out_close:
1954 fclose(file);
1955 return ret;
1956}
1957
1958/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1959 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001960 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001961int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001962{
1963 FILE *file;
1964 char *c;
1965 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001966 int ret = 0;
1967 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001968
1969 file = fopen(filename, "r");
1970 if (!file) {
1971 memprintf(err, "failed to open pattern file <%s>", filename);
1972 return 0;
1973 }
1974
1975 /* now parse all patterns. The file may contain only one pattern per
1976 * line. If the line contains spaces, they will be part of the pattern.
1977 * The pattern stops at the first CR, LF or EOF encountered.
1978 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001979 while (fgets(trash.str, trash.size, file) != NULL) {
1980 line++;
1981 c = trash.str;
1982
1983 /* ignore lines beginning with a dash */
1984 if (*c == '#')
1985 continue;
1986
1987 /* strip leading spaces and tabs */
1988 while (*c == ' ' || *c == '\t')
1989 c++;
1990
1991
1992 arg = c;
1993 while (*c && *c != '\n' && *c != '\r')
1994 c++;
1995 *c = 0;
1996
1997 /* empty lines are ignored too */
1998 if (c == arg)
1999 continue;
2000
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002001 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002002 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2003 goto out_close;
2004 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002005 }
2006
2007 ret = 1; /* success */
2008
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002009 out_close:
2010 fclose(file);
2011 return ret;
2012}
2013
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002014int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002015 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002016 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002017{
2018 struct pat_ref *ref;
2019 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002020 struct pat_ref_elt *elt;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002021
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002022 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002023 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002024
2025 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002026 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002027 chunk_printf(&trash,
2028 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2029 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2030
2031 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002032 if (!ref) {
2033 memprintf(err, "out of memory");
2034 return 0;
2035 }
2036
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002037 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002038 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002039 if (!pat_ref_read_from_file_smp(ref, filename, err))
2040 return 0;
2041 }
2042 else {
2043 if (!pat_ref_read_from_file(ref, filename, err))
2044 return 0;
2045 }
2046 }
2047 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002048 /* The reference already exists, check the map compatibility. */
2049
2050 /* If the load require samples and the flag PAT_REF_SMP is not set,
2051 * the reference doesn't contain sample, and cannot be used.
2052 */
2053 if (load_smp) {
2054 if (!(ref->flags & PAT_REF_SMP)) {
2055 memprintf(err, "The file \"%s\" is already used as one column file "
2056 "and cannot be used by as two column file.",
2057 filename);
2058 return 0;
2059 }
2060 }
2061 else {
2062 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2063 * set, the reference contains a sample, and cannot be used.
2064 */
2065 if (ref->flags & PAT_REF_SMP) {
2066 memprintf(err, "The file \"%s\" is already used as two column file "
2067 "and cannot be used by as one column file.",
2068 filename);
2069 return 0;
2070 }
2071 }
2072
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002073 /* Extends display */
2074 chunk_printf(&trash, "%s", ref->display);
2075 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2076 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2077 free(ref->display);
2078 ref->display = strdup(trash.str);
2079 if (!ref->display) {
2080 memprintf(err, "out of memory");
2081 return 0;
2082 }
2083
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002084 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002085 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002086 }
2087
2088 /* Now, we can loading patterns from the reference. */
2089
2090 /* Lookup for existing reference in the head. If the reference
2091 * doesn't exists, create it.
2092 */
2093 expr = pattern_lookup_expr(head, ref);
2094 if (!expr) {
2095 expr = pattern_new_expr(head, ref, err);
2096 if (!expr)
2097 return 0;
2098 }
2099
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002100 /* Load reference content in the pattern expression. */
2101 list_for_each_entry(elt, &ref->head, list) {
2102 if (!pat_ref_push(elt, expr, patflags, err)) {
2103 if (elt->line > 0)
2104 memprintf(err, "%s at line %d of file '%s'",
2105 *err, elt->line, filename);
2106 return 0;
2107 }
2108 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002109
2110 return 1;
2111}
2112
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002113/* This function executes a pattern match on a sample. It applies pattern <expr>
2114 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2115 * non-null if the sample match. If <fill> is true and the sample match, the
2116 * function returns the matched pattern. In many cases, this pattern can be a
2117 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002118 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002119struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002120{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002121 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002122 struct pattern *pat;
2123
2124 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002125 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002126 static_pattern.smp = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002127 static_pattern.ref = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002128 static_pattern.flags = 0;
2129 static_pattern.type = SMP_T_UINT;
2130 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002131 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002132 return &static_pattern;
2133 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002134
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002135 /* convert input to string */
2136 if (!sample_convert(smp, head->expect_type))
2137 return NULL;
2138
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002139 list_for_each_entry(list, &head->head, list) {
2140 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002141 if (pat)
2142 return pat;
2143 }
2144 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002145}
2146
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002147/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002148void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002149{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002150 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002152 list_for_each_entry_safe(list, safe, &head->head, list) {
2153 LIST_DEL(&list->list);
2154 if (list->do_free) {
2155 LIST_DEL(&list->expr->list);
2156 head->prune(list->expr);
2157 free(list->expr);
2158 }
2159 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002160 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002161}
2162
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002163/* This function lookup for a pattern matching the <key> and return a
2164 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2165 * the function returns NULL. If the key cannot be parsed, the function
2166 * fill <err>.
2167 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002168struct sample_storage **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002169{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002170 struct ebmb_node *node;
2171 struct pattern_tree *elt;
2172 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002173
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002174 for (node = ebmb_first(&expr->pattern_tree);
2175 node;
2176 node = ebmb_next(node)) {
2177 elt = container_of(node, struct pattern_tree, node);
2178 if (elt->ref == ref)
2179 return &elt->smp;
2180 }
2181
2182 for (node = ebmb_first(&expr->pattern_tree_2);
2183 node;
2184 node = ebmb_next(node)) {
2185 elt = container_of(node, struct pattern_tree, node);
2186 if (elt->ref == ref)
2187 return &elt->smp;
2188 }
2189
2190 list_for_each_entry(pat, &expr->patterns, list)
2191 if (pat->pat.ref == ref)
2192 return &pat->pat.smp;
2193
2194 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002195}
2196
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002197/* This function search all the pattern matching the <key> and delete it.
2198 * If the parsing of the input key fails, the function returns 0 and the
2199 * <err> is filled, else return 1;
2200 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002201int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002202{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002203 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002204 return 1;
2205}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002206
2207/* This function finalize the configuration parsing. Its set all the
2208 * automatic ids
2209 */
2210void pattern_finalize_config(void)
2211{
2212 int i = 0;
2213 struct pat_ref *ref, *ref2, *ref3;
2214 struct list pr = LIST_HEAD_INIT(pr);
2215
2216 list_for_each_entry(ref, &pattern_reference, list) {
2217 if (ref->unique_id == -1) {
2218 /* Look for the first free id. */
2219 while (1) {
2220 list_for_each_entry(ref2, &pattern_reference, list) {
2221 if (ref2->unique_id == i) {
2222 i++;
2223 break;
2224 }
2225 }
2226 if (&ref2->list == &pattern_reference);
2227 break;
2228 }
2229
2230 /* Uses the unique id and increment it for the next entry. */
2231 ref->unique_id = i;
2232 i++;
2233 }
2234 }
2235
2236 /* This sort the reference list by id. */
2237 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2238 LIST_DEL(&ref->list);
2239 list_for_each_entry(ref3, &pr, list) {
2240 if (ref->unique_id < ref3->unique_id) {
2241 LIST_ADDQ(&ref3->list, &ref->list);
2242 break;
2243 }
2244 }
2245 if (&ref3->list == &pr)
2246 LIST_ADDQ(&pr, &ref->list);
2247 }
2248
2249 /* swap root */
2250 LIST_ADD(&pr, &pattern_reference);
2251 LIST_DEL(&pr);
2252}